From ac3efd3a2943e491bad5016d30fb45a24fdf908d Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Tue, 31 Dec 2013 19:33:09 -0800 Subject: [PATCH] Fix regression: Old browser support --- lib/b64.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/b64.js b/lib/b64.js index 5ffbb2a..b0fa171 100644 --- a/lib/b64.js +++ b/lib/b64.js @@ -1,9 +1,12 @@ var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; - ;(function (exports) { 'use strict'; + var Arr = (typeof Uint8Array !== 'undefined') + ? Uint8Array + : Array + var ZERO = '0'.charCodeAt(0) var PLUS = '+'.charCodeAt(0) var SLASH = '/'.charCodeAt(0) @@ -29,7 +32,7 @@ var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; function b64ToByteArray(b64) { var i, j, l, tmp, placeHolders, arr; - + if (b64.length % 4 > 0) { throw 'Invalid string. Length must be a multiple of 4'; } @@ -40,10 +43,10 @@ var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; // if there is only one, then the three characters before it represent 2 bytes // this is just a cheap hack to not do indexOf twice var len = b64.length - placeHolders = '=' === b64[len - 2] ? 2 : '=' === b64[len - 1] ? 1 : 0 + placeHolders = '=' === b64.charAt(len - 2) ? 2 : '=' === b64.charAt(len - 1) ? 1 : 0 // base64 is 4/3 + up to two characters of the original data - arr = new Uint8Array(b64.length * 3 / 4 - placeHolders); + arr = new Arr(b64.length * 3 / 4 - placeHolders); // if there are placeholders, only get up to the last complete 4 chars @@ -118,6 +121,5 @@ var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; module.exports.toByteArray = b64ToByteArray; module.exports.fromByteArray = uint8ToBase64; - module.exports.lookup = lookup }());