UNPKG

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