react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / node_modules / commoner / node_modules / iconv-lite / encodings / utf16.js
81169 views"use strict"12// == UTF16-BE codec. ==========================================================34exports.utf16be = Utf16BECodec;5function Utf16BECodec() {6}78Utf16BECodec.prototype.encoder = Utf16BEEncoder;9Utf16BECodec.prototype.decoder = Utf16BEDecoder;10Utf16BECodec.prototype.bomAware = true;111213// -- Encoding1415function Utf16BEEncoder() {16}1718Utf16BEEncoder.prototype.write = function(str) {19var buf = new Buffer(str, 'ucs2');20for (var i = 0; i < buf.length; i += 2) {21var tmp = buf[i]; buf[i] = buf[i+1]; buf[i+1] = tmp;22}23return buf;24}2526Utf16BEEncoder.prototype.end = function() {27}282930// -- Decoding3132function Utf16BEDecoder() {33this.overflowByte = -1;34}3536Utf16BEDecoder.prototype.write = function(buf) {37if (buf.length == 0)38return '';3940var buf2 = new Buffer(buf.length + 1),41i = 0, j = 0;4243if (this.overflowByte !== -1) {44buf2[0] = buf[0];45buf2[1] = this.overflowByte;46i = 1; j = 2;47}4849for (; i < buf.length-1; i += 2, j+= 2) {50buf2[j] = buf[i+1];51buf2[j+1] = buf[i];52}5354this.overflowByte = (i == buf.length-1) ? buf[buf.length-1] : -1;5556return buf2.slice(0, j).toString('ucs2');57}5859Utf16BEDecoder.prototype.end = function() {60}616263// == UTF-16 codec =============================================================64// Decoder chooses automatically from UTF-16LE and UTF-16BE using BOM and space-based heuristic.65// Defaults to UTF-16LE, as it's prevalent and default in Node.66// http://en.wikipedia.org/wiki/UTF-16 and http://encoding.spec.whatwg.org/#utf-16le67// Decoder default can be changed: iconv.decode(buf, 'utf16', {defaultEncoding: 'utf-16be'});6869// Encoder uses UTF-16LE and prepends BOM (which can be overridden with addBOM: false).7071exports.utf16 = Utf16Codec;72function Utf16Codec(codecOptions, iconv) {73this.iconv = iconv;74}7576Utf16Codec.prototype.encoder = Utf16Encoder;77Utf16Codec.prototype.decoder = Utf16Decoder;787980// -- Encoding (pass-through)8182function Utf16Encoder(options, codec) {83options = options || {};84if (options.addBOM === undefined)85options.addBOM = true;86this.encoder = codec.iconv.getEncoder('utf-16le', options);87}8889Utf16Encoder.prototype.write = function(str) {90return this.encoder.write(str);91}9293Utf16Encoder.prototype.end = function() {94return this.encoder.end();95}969798// -- Decoding99100function Utf16Decoder(options, codec) {101this.decoder = null;102this.initialBytes = [];103this.initialBytesLen = 0;104105this.options = options || {};106this.iconv = codec.iconv;107}108109Utf16Decoder.prototype.write = function(buf) {110if (!this.decoder) {111// Codec is not chosen yet. Accumulate initial bytes.112this.initialBytes.push(buf);113this.initialBytesLen += buf.length;114115if (this.initialBytesLen < 16) // We need more bytes to use space heuristic (see below)116return '';117118// We have enough bytes -> detect endianness.119var buf = Buffer.concat(this.initialBytes),120encoding = detectEncoding(buf, this.options.defaultEncoding);121this.decoder = this.iconv.getDecoder(encoding, this.options);122this.initialBytes.length = this.initialBytesLen = 0;123}124125return this.decoder.write(buf);126}127128Utf16Decoder.prototype.end = function() {129if (!this.decoder) {130var buf = Buffer.concat(this.initialBytes),131encoding = detectEncoding(buf, this.options.defaultEncoding);132this.decoder = this.iconv.getDecoder(encoding, this.options);133134var res = this.decoder.write(buf),135trail = this.decoder.end();136137return trail ? (res + trail) : res;138}139return this.decoder.end();140}141142function detectEncoding(buf, defaultEncoding) {143var enc = defaultEncoding || 'utf-16le';144145if (buf.length >= 2) {146// Check BOM.147if (buf[0] == 0xFE && buf[1] == 0xFF) // UTF-16BE BOM148enc = 'utf-16be';149else if (buf[0] == 0xFF && buf[1] == 0xFE) // UTF-16LE BOM150enc = 'utf-16le';151else {152// No BOM found. Try to deduce encoding from initial content.153// Most of the time, the content has spaces (U+0020), but the opposite (U+2000) is very uncommon.154// So, we count spaces as if it was LE or BE, and decide from that.155var spacesLE = 0, spacesBE = 0, // Counts of space chars in both positions156_len = Math.min(buf.length - (buf.length % 2), 64); // Len is always even.157158for (var i = 0; i < _len; i += 2) {159if (buf[i] == 0x00 && buf[i+1] == 0x20) spacesBE++;160if (buf[i] == 0x20 && buf[i+1] == 0x00) spacesLE++;161}162163if (spacesBE > 0 && spacesLE == 0)164enc = 'utf-16be';165else if (spacesBE == 0 && spacesLE > 0)166enc = 'utf-16le';167}168}169170return enc;171}172173174175176