AI销管
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

enc-utf16.js 4.0 KiB

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