Skip to content

Commit 732bc3b

Browse files
Indigo744kvz
authored andcommitted
Improvements for base64_encode and base64_decode functions for UTF8 strings (locutusjs#331)
* Update base64_encode.js Correctly handle UTF8 string in browsers. Uses solution locutusjs#1 from https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding * Update base64_decode.js Correctly handle UTF8 string in browsers. Uses solution 1 from https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding * Added credits * Added credits * Fix for JS coding practices * Fix for JS coding practices * Fix for JS coding practices * Fix for JS coding practices
1 parent 7467e91 commit 732bc3b

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/php/url/base64_decode.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,27 @@ module.exports = function base64_decode (encodedData) { // eslint-disable-line c
99
// bugfixed by: Onno Marsman (https://twitter.com/onnomarsman)
1010
// bugfixed by: Pellentesque Malesuada
1111
// bugfixed by: Kevin van Zonneveld (http://kvz.io)
12+
// improved by: Indigo744
1213
// example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==')
1314
// returns 1: 'Kevin van Zonneveld'
1415
// example 2: base64_decode('YQ==')
1516
// returns 2: 'a'
1617
// example 3: base64_decode('4pyTIMOgIGxhIG1vZGU=')
1718
// returns 3: '✓ à la mode'
1819

20+
// decodeUTF8string()
21+
// Internal function to decode properly UTF8 string
22+
// Adapted from Solution #1 at https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
23+
var decodeUTF8string = function (str) {
24+
// Going backwards: from bytestream, to percent-encoding, to original string.
25+
return decodeURIComponent(str.split('').map(function (c) {
26+
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
27+
}).join(''))
28+
}
29+
1930
if (typeof window !== 'undefined') {
2031
if (typeof window.atob !== 'undefined') {
21-
return decodeURIComponent(encodeURIComponent(window.atob(encodedData)))
32+
return decodeUTF8string(window.atob(encodedData))
2233
}
2334
} else {
2435
return new Buffer(encodedData, 'base64').toString('utf-8')
@@ -68,5 +79,5 @@ module.exports = function base64_decode (encodedData) { // eslint-disable-line c
6879

6980
dec = tmpArr.join('')
7081

71-
return decodeURIComponent(encodeURIComponent(dec.replace(/\0+$/, '')))
82+
return decodeUTF8string(dec.replace(/\0+$/, ''))
7283
}

src/php/url/base64_encode.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,30 @@ module.exports = function base64_encode (stringToEncode) { // eslint-disable-lin
77
// improved by: Kevin van Zonneveld (http://kvz.io)
88
// improved by: Rafał Kukawski (http://blog.kukawski.pl)
99
// bugfixed by: Pellentesque Malesuada
10+
// improved by: Indigo744
1011
// example 1: base64_encode('Kevin van Zonneveld')
1112
// returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA=='
1213
// example 2: base64_encode('a')
1314
// returns 2: 'YQ=='
1415
// example 3: base64_encode('✓ à la mode')
1516
// returns 3: '4pyTIMOgIGxhIG1vZGU='
1617

18+
// encodeUTF8string()
19+
// Internal function to encode properly UTF8 string
20+
// Adapted from Solution #1 at https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
21+
var encodeUTF8string = function (str) {
22+
// first we use encodeURIComponent to get percent-encoded UTF-8,
23+
// then we convert the percent encodings into raw bytes which
24+
// can be fed into the base64 encoding algorithm.
25+
return encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
26+
function toSolidBytes (match, p1) {
27+
return String.fromCharCode('0x' + p1)
28+
})
29+
}
30+
1731
if (typeof window !== 'undefined') {
1832
if (typeof window.btoa !== 'undefined') {
19-
return window.btoa(decodeURIComponent(encodeURIComponent(stringToEncode)))
33+
return window.btoa(encodeUTF8string(stringToEncode))
2034
}
2135
} else {
2236
return new Buffer(stringToEncode).toString('base64')
@@ -40,7 +54,7 @@ module.exports = function base64_encode (stringToEncode) { // eslint-disable-lin
4054
return stringToEncode
4155
}
4256

43-
stringToEncode = decodeURIComponent(encodeURIComponent(stringToEncode))
57+
stringToEncode = encodeUTF8string(stringToEncode)
4458

4559
do {
4660
// pack three octets into four hexets

0 commit comments

Comments
 (0)