Diff code: base64_decode

Function:
Description: Decodes string using MIME base64 algorithm.
Version A:
Version B:
base64_decode version 457base64_decode version 459
 1function base64_decode (data) {
 1function base64_decode (data) {
 2    // http://jsphp.co/jsphp/fn/view/base64_decode
 2    // http://jsphp.co/jsphp/fn/view/base64_decode
 3    // + original by: Tyler Akins (http://rumkin.com)
 3    // + original by: Tyler Akins (http://rumkin.com)
 4    // + improved by: Thunder.m
 4    // + improved by: Thunder.m
 5    // + input by: Aman Gupta
 5    // + input by: Aman Gupta
 6    // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 6    // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 7    // + bugfixed by: Onno Marsman
 7    // + bugfixed by: Onno Marsman
 8    // + bugfixed by: Pellentesque Malesuada
 8    // + bugfixed by: Pellentesque Malesuada
 9    // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 9    // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
10    // + input by: Brett Zamir (http://brett-zamir.me)
10    // + input by: Brett Zamir (http://brett-zamir.me)
11    // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
11    // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
12    // - depends on: utf8_decode
12    // - depends on: utf8_decode
13    // * example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==');
13    // * example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==');
14    // * returns 1: 'Kevin van Zonneveld'
14    // * returns 1: 'Kevin van Zonneveld'
15    // mozilla has this native
15    // mozilla has this native
16    // - but breaks in 2.0.0.12!
16    // - but breaks in 2.0.0.12!
17    //if (typeof this.window['btoa'] == 'function') {
17    //if (typeof this.window['btoa'] == 'function') {
18    // return btoa(data);
18    // return btoa(data);
19    //}
19    //}
20    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
20    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
21    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
21    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
22        ac = 0,
22        ac = 0,
23        dec = "",
23        dec = "",
24        tmp_arr = [];
24        tmp_arr = [];
25
25
26    if (!data) {
26    if (!data) {
27        return data;
27        return data;
28    }
28    }
29
29
30    data += '';
30    data += '';
31
31
32    do { // unpack four hexets into three octets using index points in b64
32    do { // unpack four hexets into three octets using index points in b64
33        h1 = b64.indexOf(data.charAt(i++));
33        h1 = b64.indexOf(data.charAt(i++));
34        h2 = b64.indexOf(data.charAt(i++));
34        h2 = b64.indexOf(data.charAt(i++));
35        h3 = b64.indexOf(data.charAt(i++));
35        h3 = b64.indexOf(data.charAt(i++));
36        h4 = b64.indexOf(data.charAt(i++));
36        h4 = b64.indexOf(data.charAt(i++));
37
37
38        bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
38        bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
39
39
40        o1 = bits >> 16 & 0xff;
40        o1 = bits >> 16 & 0xff;
41        o2 = bits >> 8 & 0xff;
41        o2 = bits >> 8 & 0xff;
42        o3 = bits & 0xff;
42        o3 = bits & 0xff;
43
43
44        if (h3 == 64) {
44        if (h3 == 64) {
45            tmp_arr[ac++] = String.fromCharCode(o1);
45            tmp_arr[ac++] = String.fromCharCode(o1);
46        } else if (h4 == 64) {
46        } else if (h4 == 64) {
47            tmp_arr[ac++] = String.fromCharCode(o1, o2);
47            tmp_arr[ac++] = String.fromCharCode(o1, o2);
48        } else {
48        } else {
49            tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
49            tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
50        }
50        }
51    } while (i < data.length);
51    } while (i < data.length);
52
52
53    dec = tmp_arr.join('');
53    dec = tmp_arr.join('');
54    // removed UTF-8 decoding, this should be done after base64_decode if necessary.
54    // removed UTF-8 decoding, this should be done after base64_decode if necessary.
55    dec = this.utf8_decode(dec);
55    //dec = this.utf8_decode(dec);
56
56
57    return dec;
57    return dec;
58}
58}