base64.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. * mixin cc.Codec.Base64
  8. */
  9. cc.Codec.Base64 = {name:'Jacob__Codec__Base64'};
  10. cc.Codec.Base64._keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  11. /**
  12. * <p>
  13. * cc.Codec.Base64.decode(input[, unicode=false]) -> String (http://en.wikipedia.org/wiki/Base64).
  14. * </p>
  15. * @function
  16. * @param {String} input The base64 encoded string to decode
  17. * @return {String} Decodes a base64 encoded String
  18. * @example
  19. * //decode string
  20. * cc.Codec.Base64.decode("U29tZSBTdHJpbmc="); // => "Some String"
  21. */
  22. cc.Codec.Base64.decode = function Jacob__Codec__Base64__decode(input) {
  23. var output = [],
  24. chr1, chr2, chr3,
  25. enc1, enc2, enc3, enc4,
  26. i = 0;
  27. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  28. while (i < input.length) {
  29. enc1 = this._keyStr.indexOf(input.charAt(i++));
  30. enc2 = this._keyStr.indexOf(input.charAt(i++));
  31. enc3 = this._keyStr.indexOf(input.charAt(i++));
  32. enc4 = this._keyStr.indexOf(input.charAt(i++));
  33. chr1 = (enc1 << 2) | (enc2 >> 4);
  34. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  35. chr3 = ((enc3 & 3) << 6) | enc4;
  36. output.push(String.fromCharCode(chr1));
  37. if (enc3 != 64) {
  38. output.push(String.fromCharCode(chr2));
  39. }
  40. if (enc4 != 64) {
  41. output.push(String.fromCharCode(chr3));
  42. }
  43. }
  44. output = output.join('');
  45. return output;
  46. };
  47. /**
  48. * <p>
  49. * Converts an input string encoded in base64 to an array of integers whose<br/>
  50. * values represent the decoded string's characters' bytes.
  51. * </p>
  52. * @function
  53. * @param {String} input The String to convert to an array of Integers
  54. * @param {Number} bytes
  55. * @return {Array}
  56. * @example
  57. * //decode string to array
  58. * var decodeArr = cc.Codec.Base64.decodeAsArray("U29tZSBTdHJpbmc=");
  59. */
  60. cc.Codec.Base64.decodeAsArray = function Jacob__Codec__Base64___decodeAsArray(input, bytes) {
  61. var dec = this.decode(input),
  62. ar = [], i, j, len;
  63. for (i = 0, len = dec.length / bytes; i < len; i++) {
  64. ar[i] = 0;
  65. for (j = bytes - 1; j >= 0; --j) {
  66. ar[i] += dec.charCodeAt((i * bytes) + j) << (j * 8);
  67. }
  68. }
  69. return ar;
  70. };
  71. cc.uint8ArrayToUint32Array = function(uint8Arr){
  72. if(uint8Arr.length % 4 != 0)
  73. return null;
  74. var arrLen = uint8Arr.length /4;
  75. var retArr = window.Uint32Array? new Uint32Array(arrLen) : [];
  76. for(var i = 0; i < arrLen; i++){
  77. var offset = i * 4;
  78. retArr[i] = uint8Arr[offset] + uint8Arr[offset + 1] * (1 << 8) + uint8Arr[offset + 2] * (1 << 16) + uint8Arr[offset + 3] * (1<<24);
  79. }
  80. return retArr;
  81. };