react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / buffer / node_modules / base64-js / lib / b64.js
80744 viewsvar lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';12;(function (exports) {3'use strict';45var Arr = (typeof Uint8Array !== 'undefined')6? Uint8Array7: Array89var PLUS = '+'.charCodeAt(0)10var SLASH = '/'.charCodeAt(0)11var NUMBER = '0'.charCodeAt(0)12var LOWER = 'a'.charCodeAt(0)13var UPPER = 'A'.charCodeAt(0)1415function decode (elt) {16var code = elt.charCodeAt(0)17if (code === PLUS)18return 62 // '+'19if (code === SLASH)20return 63 // '/'21if (code < NUMBER)22return -1 //no match23if (code < NUMBER + 10)24return code - NUMBER + 26 + 2625if (code < UPPER + 26)26return code - UPPER27if (code < LOWER + 26)28return code - LOWER + 2629}3031function b64ToByteArray (b64) {32var i, j, l, tmp, placeHolders, arr3334if (b64.length % 4 > 0) {35throw new Error('Invalid string. Length must be a multiple of 4')36}3738// the number of equal signs (place holders)39// if there are two placeholders, than the two characters before it40// represent one byte41// if there is only one, then the three characters before it represent 2 bytes42// this is just a cheap hack to not do indexOf twice43var len = b64.length44placeHolders = '=' === b64.charAt(len - 2) ? 2 : '=' === b64.charAt(len - 1) ? 1 : 04546// base64 is 4/3 + up to two characters of the original data47arr = new Arr(b64.length * 3 / 4 - placeHolders)4849// if there are placeholders, only get up to the last complete 4 chars50l = placeHolders > 0 ? b64.length - 4 : b64.length5152var L = 05354function push (v) {55arr[L++] = v56}5758for (i = 0, j = 0; i < l; i += 4, j += 3) {59tmp = (decode(b64.charAt(i)) << 18) | (decode(b64.charAt(i + 1)) << 12) | (decode(b64.charAt(i + 2)) << 6) | decode(b64.charAt(i + 3))60push((tmp & 0xFF0000) >> 16)61push((tmp & 0xFF00) >> 8)62push(tmp & 0xFF)63}6465if (placeHolders === 2) {66tmp = (decode(b64.charAt(i)) << 2) | (decode(b64.charAt(i + 1)) >> 4)67push(tmp & 0xFF)68} else if (placeHolders === 1) {69tmp = (decode(b64.charAt(i)) << 10) | (decode(b64.charAt(i + 1)) << 4) | (decode(b64.charAt(i + 2)) >> 2)70push((tmp >> 8) & 0xFF)71push(tmp & 0xFF)72}7374return arr75}7677function uint8ToBase64 (uint8) {78var i,79extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes80output = "",81temp, length8283function encode (num) {84return lookup.charAt(num)85}8687function tripletToBase64 (num) {88return encode(num >> 18 & 0x3F) + encode(num >> 12 & 0x3F) + encode(num >> 6 & 0x3F) + encode(num & 0x3F)89}9091// go through the array every three bytes, we'll deal with trailing stuff later92for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) {93temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2])94output += tripletToBase64(temp)95}9697// pad the end with zeros, but make sure to not forget the extra bytes98switch (extraBytes) {99case 1:100temp = uint8[uint8.length - 1]101output += encode(temp >> 2)102output += encode((temp << 4) & 0x3F)103output += '=='104break105case 2:106temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1])107output += encode(temp >> 10)108output += encode((temp >> 4) & 0x3F)109output += encode((temp << 2) & 0x3F)110output += '='111break112}113114return output115}116117exports.toByteArray = b64ToByteArray118exports.fromByteArray = uint8ToBase64119}(typeof exports === 'undefined' ? (this.base64js = {}) : exports))120121122