1 /*
  2  * UTF16BE.js - Implement Unicode Transformation Format 16-bit,
  3  * Big Endian mappings
  4  *
  5  * Copyright © 2014-2015, 2018, JEDLSoft
  6  *
  7  * Licensed under the Apache License, Version 2.0 (the "License");
  8  * you may not use this file except in compliance with the License.
  9  * You may obtain a copy of the License at
 10  *
 11  *     http://www.apache.org/licenses/LICENSE-2.0
 12  *
 13  * Unless required by applicable law or agreed to in writing, software
 14  * distributed under the License is distributed on an "AS IS" BASIS,
 15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 16  *
 17  * See the License for the specific language governing permissions and
 18  * limitations under the License.
 19  */
 20 
 21 // !data charset/UTF-16 charset/UTF-16BE
 22 
 23 var Charset = require("./Charset.js");
 24 var Charmap = require("./Charmap.js");
 25 
 26 /**
 27  * @class
 28  * Create a new UTF-16BE mapping instance
 29  * @constructor
 30  * @extends Charmap
 31  */
 32 var UTF16BE = function (options) {
 33     options = options || {sync: true};
 34     if (typeof(options.charset) === "object" && options.charset instanceof Charset) {
 35         this.charset = options.charset;
 36         this._init(options);
 37     } else {
 38         new Charset({
 39             name: "UTF-16BE",
 40             sync: options.sync,
 41             loadParams: options.loadParams,
 42             onLoad: ilib.bind(this, function(cs) {
 43                 this.charset = cs;
 44                 this._init(options);
 45             })
 46         });
 47     }
 48 };
 49 
 50 UTF16BE.prototype = new Charmap({noinstance: true});
 51 UTF16BE.prototype.parent = Charmap;
 52 UTF16BE.prototype.constructor = UTF16BE;
 53 
 54 /**
 55  * @private
 56  * Initialize the charmap instance
 57  */
 58 UTF16BE.prototype._init = function(options) {
 59     this._calcExpansionFactor();
 60 
 61     if (typeof(options.onLoad) === "function") {
 62         options.onLoad(this);
 63     }
 64 };
 65 
 66 UTF16BE.prototype.mapToUnicode = function (bytes) {
 67     // nodejs can't convert big-endian in native code,
 68     // so we would have to flip each Uint16 ourselves.
 69     // At that point, it's just quicker to convert
 70     // in JS code anyways
 71     var ret = "";
 72     for (var i = 0; i < bytes.length; i += 2) {
 73         ret += String.fromCharCode(bytes[i] << 8 | bytes[i+1]);
 74     }
 75 
 76     return ret;
 77 };
 78 
 79 UTF16BE.prototype.mapToNative = function(str) {
 80     // nodejs can't convert big-endian in native code,
 81     // so we would have to flip each Uint16 ourselves.
 82     // At that point, it's just quicker to convert
 83     // in JS code anyways
 84     var ret = new Uint8Array(str.length * 2 + 2);
 85     var c;
 86     for (var i = 0; i < str.length; i++) {
 87         c = str.charCodeAt(i);
 88         ret[i*2] = (c >> 8) & 0xFF;
 89         ret[i*2+1] = c & 0xFF;
 90     }
 91     // double null terminate it, just in case
 92     ret[i*2+1] = 0;
 93     ret[i*2+2] = 0;
 94 
 95     return ret;
 96 };
 97 
 98 Charmap._algorithms["UTF-16BE"] = UTF16BE;
 99 
100 module.exports = UTF16BE;