enc-utf16.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import {
  2. WordArray,
  3. } from './core.js';
  4. const swapEndian = word => ((word << 8) & 0xff00ff00) | ((word >>> 8) & 0x00ff00ff);
  5. /**
  6. * UTF-16 BE encoding strategy.
  7. */
  8. export const Utf16BE = {
  9. /**
  10. * Converts a word array to a UTF-16 BE string.
  11. *
  12. * @param {WordArray} wordArray The word array.
  13. *
  14. * @return {string} The UTF-16 BE string.
  15. *
  16. * @static
  17. *
  18. * @example
  19. *
  20. * const utf16String = CryptoJS.enc.Utf16.stringify(wordArray);
  21. */
  22. stringify(wordArray) {
  23. // Shortcuts
  24. const { words, sigBytes } = wordArray;
  25. // Convert
  26. const utf16Chars = [];
  27. for (let i = 0; i < sigBytes; i += 2) {
  28. const codePoint = (words[i >>> 2] >>> (16 - (i % 4) * 8)) & 0xffff;
  29. utf16Chars.push(String.fromCharCode(codePoint));
  30. }
  31. return utf16Chars.join('');
  32. },
  33. /**
  34. * Converts a UTF-16 BE string to a word array.
  35. *
  36. * @param {string} utf16Str The UTF-16 BE string.
  37. *
  38. * @return {WordArray} The word array.
  39. *
  40. * @static
  41. *
  42. * @example
  43. *
  44. * const wordArray = CryptoJS.enc.Utf16.parse(utf16String);
  45. */
  46. parse(utf16Str) {
  47. // Shortcut
  48. const utf16StrLength = utf16Str.length;
  49. // Convert
  50. const words = [];
  51. for (let i = 0; i < utf16StrLength; i += 1) {
  52. words[i >>> 1] |= utf16Str.charCodeAt(i) << (16 - (i % 2) * 16);
  53. }
  54. return WordArray.create(words, utf16StrLength * 2);
  55. },
  56. };
  57. export const Utf16 = Utf16BE;
  58. /**
  59. * UTF-16 LE encoding strategy.
  60. */
  61. export const Utf16LE = {
  62. /**
  63. * Converts a word array to a UTF-16 LE string.
  64. *
  65. * @param {WordArray} wordArray The word array.
  66. *
  67. * @return {string} The UTF-16 LE string.
  68. *
  69. * @static
  70. *
  71. * @example
  72. *
  73. * const utf16Str = CryptoJS.enc.Utf16LE.stringify(wordArray);
  74. */
  75. stringify(wordArray) {
  76. // Shortcuts
  77. const { words, sigBytes } = wordArray;
  78. // Convert
  79. const utf16Chars = [];
  80. for (let i = 0; i < sigBytes; i += 2) {
  81. const codePoint = swapEndian((words[i >>> 2] >>> (16 - (i % 4) * 8)) & 0xffff);
  82. utf16Chars.push(String.fromCharCode(codePoint));
  83. }
  84. return utf16Chars.join('');
  85. },
  86. /**
  87. * Converts a UTF-16 LE string to a word array.
  88. *
  89. * @param {string} utf16Str The UTF-16 LE string.
  90. *
  91. * @return {WordArray} The word array.
  92. *
  93. * @static
  94. *
  95. * @example
  96. *
  97. * const wordArray = CryptoJS.enc.Utf16LE.parse(utf16Str);
  98. */
  99. parse(utf16Str) {
  100. // Shortcut
  101. const utf16StrLength = utf16Str.length;
  102. // Convert
  103. const words = [];
  104. for (let i = 0; i < utf16StrLength; i += 1) {
  105. words[i >>> 1] |= swapEndian(utf16Str.charCodeAt(i) << (16 - (i % 2) * 16));
  106. }
  107. return WordArray.create(words, utf16StrLength * 2);
  108. },
  109. };