enc-utf16.js 3.7 KB

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