Merge "LOG SQL dump files getting installed"
[sdnc/oam.git] / dgbuilder / dgeflows / node_modules / body-parser / node_modules / iconv-lite / encodings / utf16.js
1
2
3 // == UTF16-BE codec. ==========================================================
4
5 exports.utf16be = function(options) {
6     return {
7         encoder: utf16beEncoder,
8         decoder: utf16beDecoder,
9
10         bom: new Buffer([0xFE, 0xFF]),
11     };
12 };
13
14
15 // -- Encoding
16
17 function utf16beEncoder(options) {
18     return {
19         write: utf16beEncoderWrite,
20         end: function() {},
21     }
22 }
23
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;
28     }
29     return buf;
30 }
31
32
33 // -- Decoding
34
35 function utf16beDecoder(options) {
36     return {
37         write: utf16beDecoderWrite,
38         end: function() {},
39
40         overflowByte: -1,
41     };
42 }
43
44 function utf16beDecoderWrite(buf) {
45     if (buf.length == 0)
46         return '';
47
48     var buf2 = new Buffer(buf.length + 1),
49         i = 0, j = 0;
50
51     if (this.overflowByte !== -1) {
52         buf2[0] = buf[0];
53         buf2[1] = this.overflowByte;
54         i = 1; j = 2;
55     }
56
57     for (; i < buf.length-1; i += 2, j+= 2) {
58         buf2[j] = buf[i+1];
59         buf2[j+1] = buf[i];
60     }
61
62     this.overflowByte = (i == buf.length-1) ? buf[buf.length-1] : -1;
63
64     return buf2.slice(0, j).toString('ucs2');
65 }
66
67
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'});
73
74 // Encoder prepends BOM and uses UTF-16BE.
75 // Endianness can also be changed: iconv.encode(str, 'utf16', {use: 'utf-16le'});
76
77 exports.utf16 = function(options) {
78     return {
79         encoder: utf16Encoder,
80         decoder: utf16Decoder,
81
82         getCodec: options.iconv.getCodec,
83     };
84 };
85
86 // -- Encoding
87
88 function utf16Encoder(options) {
89     options = options || {};
90     var codec = this.getCodec(options.use || 'utf-16be');
91     if (!codec.bom)
92         throw new Error("iconv-lite: in UTF-16 encoder, 'use' parameter should be either UTF-16BE or UTF16-LE.");
93
94     return {
95         write: utf16EncoderWrite,
96         end: utf16EncoderEnd,
97
98         bom: codec.bom,
99         internalEncoder: codec.encoder(options),
100     };
101 }
102
103 function utf16EncoderWrite(str) {
104     var buf = this.internalEncoder.write(str);
105
106     if (this.bom) {
107         buf = Buffer.concat([this.bom, buf]);
108         this.bom = null;
109     }
110
111     return buf;
112 }
113
114 function utf16EncoderEnd() {
115     return this.internalEncoder.end();
116 }
117
118
119 // -- Decoding
120
121 function utf16Decoder(options) {
122     return {
123         write: utf16DecoderWrite,
124         end: utf16DecoderEnd,
125
126         internalDecoder: null,
127         initialBytes: [],
128         initialBytesLen: 0,
129
130         options: options || {},
131         getCodec: this.getCodec,
132     };
133 }
134
135 function utf16DecoderWrite(buf) {
136     if (this.internalDecoder)
137         return this.internalDecoder.write(buf);
138
139     // Codec is not chosen yet. Accumulate initial bytes.
140     this.initialBytes.push(buf);
141     this.initialBytesLen += buf.length;
142     
143     if (this.initialBytesLen < 16) // We need > 2 bytes to use space heuristic (see below)
144         return '';
145
146     // We have enough bytes -> decide endianness.
147     return utf16DecoderDecideEndianness.call(this);
148 }
149
150 function utf16DecoderEnd() {
151     if (this.internalDecoder)
152         return this.internalDecoder.end();
153
154     var res = utf16DecoderDecideEndianness.call(this);
155     var trail;
156
157     if (this.internalDecoder)
158         trail = this.internalDecoder.end();
159
160     return (trail && trail.length > 0) ? (res + trail) : res;
161 }
162
163 function utf16DecoderDecideEndianness() {
164     var buf = Buffer.concat(this.initialBytes);
165     this.initialBytes.length = this.initialBytesLen = 0;
166
167     if (buf.length < 2)
168         return ''; // Not a valid UTF-16 sequence anyway.
169
170     // Default encoding.
171     var enc = this.options.default || 'utf-16be';
172
173     // Check BOM.
174     if (buf[0] == 0xFE && buf[1] == 0xFF) { // UTF-16BE BOM
175         enc = 'utf-16be'; buf = buf.slice(2);
176     }
177     else if (buf[0] == 0xFF && buf[1] == 0xFE) { // UTF-16LE BOM
178         enc = 'utf-16le'; buf = buf.slice(2);
179     }
180     else {
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.
186
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]++;
190         }
191
192         if (spaces[0] > 0 && spaces[1] == 0)  
193             enc = 'utf-16be';
194         else if (spaces[0] == 0 && spaces[1] > 0)
195             enc = 'utf-16le';
196     }
197
198     this.internalDecoder = this.getCodec(enc).decoder(this.options);
199     return this.internalDecoder.write(buf);
200 }
201
202