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     var dec = this.decode(input),
 73         ar = [], i, j, len;
 74     for (i = 0, len = dec.length / bytes; i < len; i++) {
 75         ar[i] = 0;
 76         for (j = bytes - 1; j >= 0; --j) {
 77             ar[i] += dec.charCodeAt((i * bytes) + j) << (j * 8);
 78         }
 79     }
 80 
 81     return ar;
 82 };
 83 
 84 cc.uint8ArrayToUint32Array = function(uint8Arr){
 85     if(uint8Arr.length % 4 != 0)
 86         return null;
 87 
 88     var arrLen = uint8Arr.length /4;
 89     var retArr = window.Uint32Array? new Uint32Array(arrLen) : [];
 90     for(var i = 0; i < arrLen; i++){
 91         var offset = i * 4;
 92         retArr[i] = uint8Arr[offset]  + uint8Arr[offset + 1] * (1 << 8) + uint8Arr[offset + 2] * (1 << 16) + uint8Arr[offset + 3] * (1<<24);
 93     }
 94     return retArr;
 95 };
 96