enc-utf16.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. ;(function (root, factory) {
  2. if (typeof exports === "object") {
  3. // CommonJS
  4. module.exports = exports = factory(require("./core"));
  5. } else if (typeof define === "function" && define.amd) {
  6. // AMD
  7. define(["./core"], factory);
  8. } else {
  9. // Global (browser)
  10. factory(root.CryptoJS);
  11. }
  12. }(this, function (CryptoJS) {
  13. (function () {
  14. // Shortcuts
  15. var C = CryptoJS;
  16. var C_lib = C.lib;
  17. var WordArray = C_lib.WordArray;
  18. var C_enc = C.enc;
  19. /**
  20. * UTF-16 BE encoding strategy.
  21. */
  22. var Utf16BE = C_enc.Utf16 = C_enc.Utf16BE = {
  23. /**
  24. * Converts a word array to a UTF-16 BE string.
  25. *
  26. * @param {WordArray} wordArray The word array.
  27. *
  28. * @return {string} The UTF-16 BE string.
  29. *
  30. * @static
  31. *
  32. * @example
  33. *
  34. * var utf16String = CryptoJS.enc.Utf16.stringify(wordArray);
  35. */
  36. stringify: function (wordArray) {
  37. // Shortcuts
  38. var words = wordArray.words;
  39. var sigBytes = wordArray.sigBytes;
  40. // Convert
  41. var utf16Chars = [];
  42. for (var i = 0; i < sigBytes; i += 2) {
  43. var codePoint = (words[i >>> 2] >>> (16 - (i % 4) * 8)) & 0xffff;
  44. utf16Chars.push(String.fromCharCode(codePoint));
  45. }
  46. return utf16Chars.join('');
  47. },
  48. /**
  49. * Converts a UTF-16 BE string to a word array.
  50. *
  51. * @param {string} utf16Str The UTF-16 BE string.
  52. *
  53. * @return {WordArray} The word array.
  54. *
  55. * @static
  56. *
  57. * @example
  58. *
  59. * var wordArray = CryptoJS.enc.Utf16.parse(utf16String);
  60. */
  61. parse: function (utf16Str) {
  62. // Shortcut
  63. var utf16StrLength = utf16Str.length;
  64. // Convert
  65. var words = [];
  66. for (var i = 0; i < utf16StrLength; i++) {
  67. words[i >>> 1] |= utf16Str.charCodeAt(i) << (16 - (i % 2) * 16);
  68. }
  69. return WordArray.create(words, utf16StrLength * 2);
  70. }
  71. };
  72. /**
  73. * UTF-16 LE encoding strategy.
  74. */
  75. C_enc.Utf16LE = {
  76. /**
  77. * Converts a word array to a UTF-16 LE string.
  78. *
  79. * @param {WordArray} wordArray The word array.
  80. *
  81. * @return {string} The UTF-16 LE string.
  82. *
  83. * @static
  84. *
  85. * @example
  86. *
  87. * var utf16Str = CryptoJS.enc.Utf16LE.stringify(wordArray);
  88. */
  89. stringify: function (wordArray) {
  90. // Shortcuts
  91. var words = wordArray.words;
  92. var sigBytes = wordArray.sigBytes;
  93. // Convert
  94. var utf16Chars = [];
  95. for (var i = 0; i < sigBytes; i += 2) {
  96. var codePoint = swapEndian((words[i >>> 2] >>> (16 - (i % 4) * 8)) & 0xffff);
  97. utf16Chars.push(String.fromCharCode(codePoint));
  98. }
  99. return utf16Chars.join('');
  100. },
  101. /**
  102. * Converts a UTF-16 LE string to a word array.
  103. *
  104. * @param {string} utf16Str The UTF-16 LE string.
  105. *
  106. * @return {WordArray} The word array.
  107. *
  108. * @static
  109. *
  110. * @example
  111. *
  112. * var wordArray = CryptoJS.enc.Utf16LE.parse(utf16Str);
  113. */
  114. parse: function (utf16Str) {
  115. // Shortcut
  116. var utf16StrLength = utf16Str.length;
  117. // Convert
  118. var words = [];
  119. for (var i = 0; i < utf16StrLength; i++) {
  120. words[i >>> 1] |= swapEndian(utf16Str.charCodeAt(i) << (16 - (i % 2) * 16));
  121. }
  122. return WordArray.create(words, utf16StrLength * 2);
  123. }
  124. };
  125. function swapEndian(word) {
  126. return ((word << 8) & 0xff00ff00) | ((word >>> 8) & 0x00ff00ff);
  127. }
  128. }());
  129. return CryptoJS.enc.Utf16;
  130. }));