Diff code: base64_encode

Function:
Description: Encodes string using MIME base64 algorithm.
Version A:
Version B:
base64_encode version 389base64_encode version 460
 1function base64_encode (data) {
 1function base64_encode (data) {
 2    // http://jsphp.co/jsphp/fn/view/base64_encode
 2    // http://jsphp.co/jsphp/fn/view/base64_encode
 3    // + original by: Tyler Akins (http://rumkin.com)
 3    // + original by: Tyler Akins (http://rumkin.com)
 4    // + improved by: Bayron Guevara
 4    // + improved by: Bayron Guevara
 5    // + improved by: Thunder.m
 5    // + improved by: Thunder.m
 6    // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 6    // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 7    // + bugfixed by: Pellentesque Malesuada
 7    // + bugfixed by: Pellentesque Malesuada
 8    // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 8    // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 9    // + improved by: Rafał Kukawski (http://kukawski.pl)
 9    // + improved by: Rafał Kukawski (http://kukawski.pl)
10    // - depends on: utf8_encode
10    // - depends on: utf8_encode
11    // * example 1: base64_encode('Kevin van Zonneveld');
11    // * example 1: base64_encode('Kevin van Zonneveld');
12    // * returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA=='
12    // * returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA=='
13    // mozilla has this native
13    // mozilla has this native
14    // - but breaks in 2.0.0.12!
14    // - but breaks in 2.0.0.12!
15    //if (typeof this.window['atob'] == 'function') {
15    //if (typeof this.window['atob'] == 'function') {
16    // return atob(data);
16    // return atob(data);
17    //}
17    //}
18    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
18    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
19    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
19    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
20        ac = 0,
20        ac = 0,
21        enc = "",
21        enc = "",
22        tmp_arr = [];
22        tmp_arr = [];
23
23
24    if (!data) {
24    if (!data) {
25        return data;
25        return data;
26    }
26    }
27
27
28    data = this.utf8_encode(data + '');
28    //UTF-8 encoding should be done outside base64_encode
29    //JavaScript strings seem to be represented internally as UTF-16 (or higher)
30    //data = this.utf8_encode(data + '');
31    data = data + '';
29
32
30    do { // pack three octets into four hexets
33    do { // pack three octets into four hexets
31        o1 = data.charCodeAt(i++);
34        o1 = data.charCodeAt(i++);
32        o2 = data.charCodeAt(i++);
35        o2 = data.charCodeAt(i++);
33        o3 = data.charCodeAt(i++);
36        o3 = data.charCodeAt(i++);
37
38        if ( o1 > 255 || o2 > 255 || o3 > 255 ) { throw new Error( 'Cannot encode UTF-16 string.' ); }
34
39
35        bits = o1 << 16 | o2 << 8 | o3;
40        bits = o1 << 16 | o2 << 8 | o3;
36
41
37        h1 = bits >> 18 & 0x3f;
42        h1 = bits >> 18 & 0x3f;
38        h2 = bits >> 12 & 0x3f;
43        h2 = bits >> 12 & 0x3f;
39        h3 = bits >> 6 & 0x3f;
44        h3 = bits >> 6 & 0x3f;
40        h4 = bits & 0x3f;
45        h4 = bits & 0x3f;
41
46
42        // use hexets to index into b64, and append result to encoded string
47        // use hexets to index into b64, and append result to encoded string
43        tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
48        tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
44    } while (i < data.length);
49    } while (i < data.length);
45
50
46    enc = tmp_arr.join('');
51    enc = tmp_arr.join('');
47
52
48    var r = data.length % 3;
53    var r = data.length % 3;
49
54
50    return (r ? enc.slice(0, r - 3) : enc) + '==='.slice(r || 3);
55    return (r ? enc.slice(0, r - 3) : enc) + '==='.slice(r || 3);
51
56
52}
57}