2 var Buffer = require("safer-buffer").Buffer;
4 // Note: UTF16-LE (or UCS2) codec is Node.js native. See encodings/internal.js
6 // == UTF16-BE codec. ==========================================================
8 exports.utf16be = Utf16BECodec;
9 function Utf16BECodec() {
12 Utf16BECodec.prototype.encoder = Utf16BEEncoder;
13 Utf16BECodec.prototype.decoder = Utf16BEDecoder;
14 Utf16BECodec.prototype.bomAware = true;
19 function Utf16BEEncoder() {
22 Utf16BEEncoder.prototype.write = function(str) {
23 var buf = Buffer.from(str, 'ucs2');
24 for (var i = 0; i < buf.length; i += 2) {
25 var tmp = buf[i]; buf[i] = buf[i+1]; buf[i+1] = tmp;
30 Utf16BEEncoder.prototype.end = function() {
36 function Utf16BEDecoder() {
37 this.overflowByte = -1;
40 Utf16BEDecoder.prototype.write = function(buf) {
44 var buf2 = Buffer.alloc(buf.length + 1),
47 if (this.overflowByte !== -1) {
49 buf2[1] = this.overflowByte;
53 for (; i < buf.length-1; i += 2, j+= 2) {
58 this.overflowByte = (i == buf.length-1) ? buf[buf.length-1] : -1;
60 return buf2.slice(0, j).toString('ucs2');
63 Utf16BEDecoder.prototype.end = function() {
67 // == UTF-16 codec =============================================================
68 // Decoder chooses automatically from UTF-16LE and UTF-16BE using BOM and space-based heuristic.
69 // Defaults to UTF-16LE, as it's prevalent and default in Node.
70 // http://en.wikipedia.org/wiki/UTF-16 and http://encoding.spec.whatwg.org/#utf-16le
71 // Decoder default can be changed: iconv.decode(buf, 'utf16', {defaultEncoding: 'utf-16be'});
73 // Encoder uses UTF-16LE and prepends BOM (which can be overridden with addBOM: false).
75 exports.utf16 = Utf16Codec;
76 function Utf16Codec(codecOptions, iconv) {
80 Utf16Codec.prototype.encoder = Utf16Encoder;
81 Utf16Codec.prototype.decoder = Utf16Decoder;
84 // -- Encoding (pass-through)
86 function Utf16Encoder(options, codec) {
87 options = options || {};
88 if (options.addBOM === undefined)
89 options.addBOM = true;
90 this.encoder = codec.iconv.getEncoder('utf-16le', options);
93 Utf16Encoder.prototype.write = function(str) {
94 return this.encoder.write(str);
97 Utf16Encoder.prototype.end = function() {
98 return this.encoder.end();
104 function Utf16Decoder(options, codec) {
106 this.initialBytes = [];
107 this.initialBytesLen = 0;
109 this.options = options || {};
110 this.iconv = codec.iconv;
113 Utf16Decoder.prototype.write = function(buf) {
115 // Codec is not chosen yet. Accumulate initial bytes.
116 this.initialBytes.push(buf);
117 this.initialBytesLen += buf.length;
119 if (this.initialBytesLen < 16) // We need more bytes to use space heuristic (see below)
122 // We have enough bytes -> detect endianness.
123 var buf = Buffer.concat(this.initialBytes),
124 encoding = detectEncoding(buf, this.options.defaultEncoding);
125 this.decoder = this.iconv.getDecoder(encoding, this.options);
126 this.initialBytes.length = this.initialBytesLen = 0;
129 return this.decoder.write(buf);
132 Utf16Decoder.prototype.end = function() {
134 var buf = Buffer.concat(this.initialBytes),
135 encoding = detectEncoding(buf, this.options.defaultEncoding);
136 this.decoder = this.iconv.getDecoder(encoding, this.options);
138 var res = this.decoder.write(buf),
139 trail = this.decoder.end();
141 return trail ? (res + trail) : res;
143 return this.decoder.end();
146 function detectEncoding(buf, defaultEncoding) {
147 var enc = defaultEncoding || 'utf-16le';
149 if (buf.length >= 2) {
151 if (buf[0] == 0xFE && buf[1] == 0xFF) // UTF-16BE BOM
153 else if (buf[0] == 0xFF && buf[1] == 0xFE) // UTF-16LE BOM
156 // No BOM found. Try to deduce encoding from initial content.
157 // Most of the time, the content has ASCII chars (U+00**), but the opposite (U+**00) is uncommon.
158 // So, we count ASCII as if it was LE or BE, and decide from that.
159 var asciiCharsLE = 0, asciiCharsBE = 0, // Counts of chars in both positions
160 _len = Math.min(buf.length - (buf.length % 2), 64); // Len is always even.
162 for (var i = 0; i < _len; i += 2) {
163 if (buf[i] === 0 && buf[i+1] !== 0) asciiCharsBE++;
164 if (buf[i] !== 0 && buf[i+1] === 0) asciiCharsLE++;
167 if (asciiCharsBE > asciiCharsLE)
169 else if (asciiCharsBE < asciiCharsLE)