| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- import {
- WordArray,
- } from './core.js';
- const swapEndian = word => ((word << 8) & 0xff00ff00) | ((word >>> 8) & 0x00ff00ff);
- /**
- * UTF-16 BE encoding strategy.
- */
- export const Utf16BE = {
- /**
- * Converts a word array to a UTF-16 BE string.
- *
- * @param {WordArray} wordArray The word array.
- *
- * @return {string} The UTF-16 BE string.
- *
- * @static
- *
- * @example
- *
- * const utf16String = CryptoJS.enc.Utf16.stringify(wordArray);
- */
- stringify(wordArray) {
- // Shortcuts
- const { words, sigBytes } = wordArray;
- // Convert
- const utf16Chars = [];
- for (let i = 0; i < sigBytes; i += 2) {
- const codePoint = (words[i >>> 2] >>> (16 - (i % 4) * 8)) & 0xffff;
- utf16Chars.push(String.fromCharCode(codePoint));
- }
- return utf16Chars.join('');
- },
- /**
- * Converts a UTF-16 BE string to a word array.
- *
- * @param {string} utf16Str The UTF-16 BE string.
- *
- * @return {WordArray} The word array.
- *
- * @static
- *
- * @example
- *
- * const wordArray = CryptoJS.enc.Utf16.parse(utf16String);
- */
- parse(utf16Str) {
- // Shortcut
- const utf16StrLength = utf16Str.length;
- // Convert
- const words = [];
- for (let i = 0; i < utf16StrLength; i += 1) {
- words[i >>> 1] |= utf16Str.charCodeAt(i) << (16 - (i % 2) * 16);
- }
- return WordArray.create(words, utf16StrLength * 2);
- },
- };
- export const Utf16 = Utf16BE;
- /**
- * UTF-16 LE encoding strategy.
- */
- export const Utf16LE = {
- /**
- * Converts a word array to a UTF-16 LE string.
- *
- * @param {WordArray} wordArray The word array.
- *
- * @return {string} The UTF-16 LE string.
- *
- * @static
- *
- * @example
- *
- * const utf16Str = CryptoJS.enc.Utf16LE.stringify(wordArray);
- */
- stringify(wordArray) {
- // Shortcuts
- const { words, sigBytes } = wordArray;
- // Convert
- const utf16Chars = [];
- for (let i = 0; i < sigBytes; i += 2) {
- const codePoint = swapEndian((words[i >>> 2] >>> (16 - (i % 4) * 8)) & 0xffff);
- utf16Chars.push(String.fromCharCode(codePoint));
- }
- return utf16Chars.join('');
- },
- /**
- * Converts a UTF-16 LE string to a word array.
- *
- * @param {string} utf16Str The UTF-16 LE string.
- *
- * @return {WordArray} The word array.
- *
- * @static
- *
- * @example
- *
- * const wordArray = CryptoJS.enc.Utf16LE.parse(utf16Str);
- */
- parse(utf16Str) {
- // Shortcut
- const utf16StrLength = utf16Str.length;
- // Convert
- const words = [];
- for (let i = 0; i < utf16StrLength; i += 1) {
- words[i >>> 1] |= swapEndian(utf16Str.charCodeAt(i) << (16 - (i % 2) * 16));
- }
- return WordArray.create(words, utf16StrLength * 2);
- },
- };
|