1 /*-- 2 Copyright 2009-2010 by Stefan Rusterholz. 3 All rights reserved. 4 You can choose between MIT and BSD-3-Clause license. License file will be added later. 5 --*/ 6 7 /** 8 * mixin cc.Codec.Base64 9 */ 10 cc.Codec.Base64 = {name:'Jacob__Codec__Base64'}; 11 12 cc.Codec.Base64._keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; 13 14 /** 15 * <p> 16 * cc.Codec.Base64.decode(input[, unicode=false]) -> String (http://en.wikipedia.org/wiki/Base64). 17 * </p> 18 * @function 19 * @param {String} input The base64 encoded string to decode 20 * @return {String} Decodes a base64 encoded String 21 * @example 22 * //decode string 23 * cc.Codec.Base64.decode("U29tZSBTdHJpbmc="); // => "Some String" 24 */ 25 cc.Codec.Base64.decode = function Jacob__Codec__Base64__decode(input) { 26 var output = [], 27 chr1, chr2, chr3, 28 enc1, enc2, enc3, enc4, 29 i = 0; 30 31 input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); 32 33 while (i < input.length) { 34 enc1 = this._keyStr.indexOf(input.charAt(i++)); 35 enc2 = this._keyStr.indexOf(input.charAt(i++)); 36 enc3 = this._keyStr.indexOf(input.charAt(i++)); 37 enc4 = this._keyStr.indexOf(input.charAt(i++)); 38 39 chr1 = (enc1 << 2) | (enc2 >> 4); 40 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); 41 chr3 = ((enc3 & 3) << 6) | enc4; 42 43 output.push(String.fromCharCode(chr1)); 44 45 if (enc3 != 64) { 46 output.push(String.fromCharCode(chr2)); 47 } 48 if (enc4 != 64) { 49 output.push(String.fromCharCode(chr3)); 50 } 51 } 52 53 output = output.join(''); 54 55 return output; 56 }; 57 58 /** 59 * <p> 60 * Converts an input string encoded in base64 to an array of integers whose<br/> 61 * values represent the decoded string's characters' bytes. 62 * </p> 63 * @function 64 * @param {String} input The String to convert to an array of Integers 65 * @param {Number} bytes 66 * @return {Array} 67 * @example 68 * //decode string to array 69 * var decodeArr = cc.Codec.Base64.decodeAsArray("U29tZSBTdHJpbmc="); 70 */ 71 cc.Codec.Base64.decodeAsArray = function Jacob__Codec__Base64___decodeAsArray(input, bytes) { 72 bytes = bytes || 4; 73 var dec = this.decode(input), 74 ar = [], i, j, len; 75 for (i = 0, len = dec.length / bytes; i < len; i++) { 76 ar[i] = 0; 77 for (j = bytes - 1; j >= 0; --j) { 78 ar[i] += dec.charCodeAt((i * bytes) + j) << (j * 8); 79 } 80 } 81 82 return ar; 83 }; 84 85 cc.Codec.Base64.encode = function Jacob__Codec__Base64__encode(input) { 86 input = encodeURIComponent(input); 87 var output = ""; 88 var chr1, chr2, chr3 = ""; 89 var enc1, enc2, enc3, enc4 = ""; 90 var i = 0; 91 92 do { 93 chr1 = input.charCodeAt(i++); 94 chr2 = input.charCodeAt(i++); 95 chr3 = input.charCodeAt(i++); 96 97 enc1 = chr1 >> 2; 98 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); 99 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); 100 enc4 = chr3 & 63; 101 102 if (isNaN(chr2)) { 103 enc3 = enc4 = 64; 104 } else if (isNaN(chr3)) { 105 enc4 = 64; 106 } 107 108 output = output + 109 this._keyStr.charAt(enc1) + 110 this._keyStr.charAt(enc2) + 111 this._keyStr.charAt(enc3) + 112 this._keyStr.charAt(enc4); 113 chr1 = chr2 = chr3 = ""; 114 enc1 = enc2 = enc3 = enc4 = ""; 115 } while (i < input.length); 116 117 return output; 118 }; 119 120 cc.uint8ArrayToUint32Array = function(uint8Arr){ 121 if(uint8Arr.length % 4 != 0) 122 return null; 123 124 var arrLen = uint8Arr.length /4; 125 var retArr = window.Uint8Array? new Uint32Array(arrLen) : []; 126 for(var i = 0; i < arrLen; i++){ 127 var offset = i * 4; 128 retArr[i] = uint8Arr[offset] + uint8Arr[offset + 1] * (1 << 8) + uint8Arr[offset + 2] * (1 << 16) + uint8Arr[offset + 3] * (1<<24); 129 } 130 return retArr; 131 }; 132