3 // == UTF16-BE codec. ==========================================================
5 exports.utf16be = function(options) {
7 encoder: utf16beEncoder,
8 decoder: utf16beDecoder,
10 bom: new Buffer([0xFE, 0xFF]),
17 function utf16beEncoder(options) {
19 write: utf16beEncoderWrite,
24 function utf16beEncoderWrite(str) {
25 var buf = new Buffer(str, 'ucs2');
26 for (var i = 0; i < buf.length; i += 2) {
27 var tmp = buf[i]; buf[i] = buf[i+1]; buf[i+1] = tmp;
35 function utf16beDecoder(options) {
37 write: utf16beDecoderWrite,
44 function utf16beDecoderWrite(buf) {
48 var buf2 = new Buffer(buf.length + 1),
51 if (this.overflowByte !== -1) {
53 buf2[1] = this.overflowByte;
57 for (; i < buf.length-1; i += 2, j+= 2) {
62 this.overflowByte = (i == buf.length-1) ? buf[buf.length-1] : -1;
64 return buf2.slice(0, j).toString('ucs2');
68 // == UTF-16 codec =============================================================
69 // Decoder chooses automatically from UTF-16LE and UTF-16BE using BOM and space-based heuristic.
70 // Defaults to UTF-16BE, according to RFC 2781, although it is against some industry practices, see
71 // http://en.wikipedia.org/wiki/UTF-16 and http://encoding.spec.whatwg.org/#utf-16le
72 // Decoder default can be changed: iconv.decode(buf, 'utf16', {default: 'utf-16le'});
74 // Encoder prepends BOM and uses UTF-16BE.
75 // Endianness can also be changed: iconv.encode(str, 'utf16', {use: 'utf-16le'});
77 exports.utf16 = function(options) {
79 encoder: utf16Encoder,
80 decoder: utf16Decoder,
82 getCodec: options.iconv.getCodec,
88 function utf16Encoder(options) {
89 options = options || {};
90 var codec = this.getCodec(options.use || 'utf-16be');
92 throw new Error("iconv-lite: in UTF-16 encoder, 'use' parameter should be either UTF-16BE or UTF16-LE.");
95 write: utf16EncoderWrite,
99 internalEncoder: codec.encoder(options),
103 function utf16EncoderWrite(str) {
104 var buf = this.internalEncoder.write(str);
107 buf = Buffer.concat([this.bom, buf]);
114 function utf16EncoderEnd() {
115 return this.internalEncoder.end();
121 function utf16Decoder(options) {
123 write: utf16DecoderWrite,
124 end: utf16DecoderEnd,
126 internalDecoder: null,
130 options: options || {},
131 getCodec: this.getCodec,
135 function utf16DecoderWrite(buf) {
136 if (this.internalDecoder)
137 return this.internalDecoder.write(buf);
139 // Codec is not chosen yet. Accumulate initial bytes.
140 this.initialBytes.push(buf);
141 this.initialBytesLen += buf.length;
143 if (this.initialBytesLen < 16) // We need > 2 bytes to use space heuristic (see below)
146 // We have enough bytes -> decide endianness.
147 return utf16DecoderDecideEndianness.call(this);
150 function utf16DecoderEnd() {
151 if (this.internalDecoder)
152 return this.internalDecoder.end();
154 var res = utf16DecoderDecideEndianness.call(this);
157 if (this.internalDecoder)
158 trail = this.internalDecoder.end();
160 return (trail && trail.length > 0) ? (res + trail) : res;
163 function utf16DecoderDecideEndianness() {
164 var buf = Buffer.concat(this.initialBytes);
165 this.initialBytes.length = this.initialBytesLen = 0;
168 return ''; // Not a valid UTF-16 sequence anyway.
171 var enc = this.options.default || 'utf-16be';
174 if (buf[0] == 0xFE && buf[1] == 0xFF) { // UTF-16BE BOM
175 enc = 'utf-16be'; buf = buf.slice(2);
177 else if (buf[0] == 0xFF && buf[1] == 0xFE) { // UTF-16LE BOM
178 enc = 'utf-16le'; buf = buf.slice(2);
181 // No BOM found. Try to deduce encoding from initial content.
182 // Most of the time, the content has spaces (U+0020), but the opposite (U+2000) is very uncommon.
183 // So, we count spaces as if it was LE or BE, and decide from that.
184 var spaces = [0, 0], // Counts of space chars in both positions
185 _len = Math.min(buf.length - (buf.length % 2), 64); // Len is always even.
187 for (var i = 0; i < _len; i += 2) {
188 if (buf[i] == 0x00 && buf[i+1] == 0x20) spaces[0]++;
189 if (buf[i] == 0x20 && buf[i+1] == 0x00) spaces[1]++;
192 if (spaces[0] > 0 && spaces[1] == 0)
194 else if (spaces[0] == 0 && spaces[1] > 0)
198 this.internalDecoder = this.getCodec(enc).decoder(this.options);
199 return this.internalDecoder.write(buf);