lib-typedarrays.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. ;(function (root, factory) {
  2. if (typeof exports === "object") {
  3. // CommonJS
  4. module.exports = exports = factory(require("./core"));
  5. }
  6. else if (typeof define === "function" && define.amd) {
  7. // AMD
  8. define(["./core"], factory);
  9. }
  10. else {
  11. // Global (browser)
  12. factory(root.CryptoJS);
  13. }
  14. }(this, function (CryptoJS) {
  15. (function () {
  16. // Check if typed arrays are supported
  17. if (typeof ArrayBuffer != 'function') {
  18. return;
  19. }
  20. // Shortcuts
  21. var C = CryptoJS;
  22. var C_lib = C.lib;
  23. var WordArray = C_lib.WordArray;
  24. // Reference original init
  25. var superInit = WordArray.init;
  26. // Augment WordArray.init to handle typed arrays
  27. var subInit = WordArray.init = function (typedArray) {
  28. // Convert buffers to uint8
  29. if (typedArray instanceof ArrayBuffer) {
  30. typedArray = new Uint8Array(typedArray);
  31. }
  32. // Convert other array views to uint8
  33. if (
  34. typedArray instanceof Int8Array ||
  35. (typeof Uint8ClampedArray !== "undefined" && typedArray instanceof Uint8ClampedArray) ||
  36. typedArray instanceof Int16Array ||
  37. typedArray instanceof Uint16Array ||
  38. typedArray instanceof Int32Array ||
  39. typedArray instanceof Uint32Array ||
  40. typedArray instanceof Float32Array ||
  41. typedArray instanceof Float64Array
  42. ) {
  43. typedArray = new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength);
  44. }
  45. // Handle Uint8Array
  46. if (typedArray instanceof Uint8Array) {
  47. // Shortcut
  48. var typedArrayByteLength = typedArray.byteLength;
  49. // Extract bytes
  50. var words = [];
  51. for (var i = 0; i < typedArrayByteLength; i++) {
  52. words[i >>> 2] |= typedArray[i] << (24 - (i % 4) * 8);
  53. }
  54. // Initialize this word array
  55. superInit.call(this, words, typedArrayByteLength);
  56. } else {
  57. // Else call normal init
  58. superInit.apply(this, arguments);
  59. }
  60. };
  61. subInit.prototype = WordArray;
  62. }());
  63. return CryptoJS.lib.WordArray;
  64. }));