Base64binary.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * @license -------------------------------------------------------------------
  3. * module: Base64Binary
  4. * src: http://blog.danguer.com/2011/10/24/base64-binary-decoding-in-javascript/
  5. * license: Simplified BSD License
  6. * -------------------------------------------------------------------
  7. * Copyright 2011, Daniel Guerrero. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. * - Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * - Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL DANIEL GUERRERO BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. var Base64Binary = {
  29. _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
  30. /* will return a Uint8Array type */
  31. decodeArrayBuffer: function(input) {
  32. var bytes = Math.ceil( (3*input.length) / 4.0);
  33. var ab = new ArrayBuffer(bytes);
  34. this.decode(input, ab);
  35. return ab;
  36. },
  37. decode: function(input, arrayBuffer) {
  38. //get last chars to see if are valid
  39. var lkey1 = this._keyStr.indexOf(input.charAt(input.length-1));
  40. var lkey2 = this._keyStr.indexOf(input.charAt(input.length-1));
  41. var bytes = Math.ceil( (3*input.length) / 4.0);
  42. if (lkey1 == 64) bytes--; //padding chars, so skip
  43. if (lkey2 == 64) bytes--; //padding chars, so skip
  44. var uarray;
  45. var chr1, chr2, chr3;
  46. var enc1, enc2, enc3, enc4;
  47. var i = 0;
  48. var j = 0;
  49. if (arrayBuffer)
  50. uarray = new Uint8Array(arrayBuffer);
  51. else
  52. uarray = new Uint8Array(bytes);
  53. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  54. for (i=0; i<bytes; i+=3) {
  55. //get the 3 octects in 4 ascii chars
  56. enc1 = this._keyStr.indexOf(input.charAt(j++));
  57. enc2 = this._keyStr.indexOf(input.charAt(j++));
  58. enc3 = this._keyStr.indexOf(input.charAt(j++));
  59. enc4 = this._keyStr.indexOf(input.charAt(j++));
  60. chr1 = (enc1 << 2) | (enc2 >> 4);
  61. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  62. chr3 = ((enc3 & 3) << 6) | enc4;
  63. uarray[i] = chr1;
  64. if (enc3 != 64) uarray[i+1] = chr2;
  65. if (enc4 != 64) uarray[i+2] = chr3;
  66. }
  67. return uarray;
  68. }
  69. };