1 | "use strict";
|
2 | var Buffer = require("safer-buffer").Buffer;
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | exports.utf16be = Utf16BECodec;
|
9 | function Utf16BECodec() {
|
10 | }
|
11 |
|
12 | Utf16BECodec.prototype.encoder = Utf16BEEncoder;
|
13 | Utf16BECodec.prototype.decoder = Utf16BEDecoder;
|
14 | Utf16BECodec.prototype.bomAware = true;
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function Utf16BEEncoder() {
|
20 | }
|
21 |
|
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;
|
26 | }
|
27 | return buf;
|
28 | }
|
29 |
|
30 | Utf16BEEncoder.prototype.end = function() {
|
31 | }
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | function Utf16BEDecoder() {
|
37 | this.overflowByte = -1;
|
38 | }
|
39 |
|
40 | Utf16BEDecoder.prototype.write = function(buf) {
|
41 | if (buf.length == 0)
|
42 | return '';
|
43 |
|
44 | var buf2 = Buffer.alloc(buf.length + 1),
|
45 | i = 0, j = 0;
|
46 |
|
47 | if (this.overflowByte !== -1) {
|
48 | buf2[0] = buf[0];
|
49 | buf2[1] = this.overflowByte;
|
50 | i = 1; j = 2;
|
51 | }
|
52 |
|
53 | for (; i < buf.length-1; i += 2, j+= 2) {
|
54 | buf2[j] = buf[i+1];
|
55 | buf2[j+1] = buf[i];
|
56 | }
|
57 |
|
58 | this.overflowByte = (i == buf.length-1) ? buf[buf.length-1] : -1;
|
59 |
|
60 | return buf2.slice(0, j).toString('ucs2');
|
61 | }
|
62 |
|
63 | Utf16BEDecoder.prototype.end = function() {
|
64 | this.overflowByte = -1;
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | exports.utf16 = Utf16Codec;
|
77 | function Utf16Codec(codecOptions, iconv) {
|
78 | this.iconv = iconv;
|
79 | }
|
80 |
|
81 | Utf16Codec.prototype.encoder = Utf16Encoder;
|
82 | Utf16Codec.prototype.decoder = Utf16Decoder;
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | function Utf16Encoder(options, codec) {
|
88 | options = options || {};
|
89 | if (options.addBOM === undefined)
|
90 | options.addBOM = true;
|
91 | this.encoder = codec.iconv.getEncoder('utf-16le', options);
|
92 | }
|
93 |
|
94 | Utf16Encoder.prototype.write = function(str) {
|
95 | return this.encoder.write(str);
|
96 | }
|
97 |
|
98 | Utf16Encoder.prototype.end = function() {
|
99 | return this.encoder.end();
|
100 | }
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | function Utf16Decoder(options, codec) {
|
106 | this.decoder = null;
|
107 | this.initialBufs = [];
|
108 | this.initialBufsLen = 0;
|
109 |
|
110 | this.options = options || {};
|
111 | this.iconv = codec.iconv;
|
112 | }
|
113 |
|
114 | Utf16Decoder.prototype.write = function(buf) {
|
115 | if (!this.decoder) {
|
116 |
|
117 | this.initialBufs.push(buf);
|
118 | this.initialBufsLen += buf.length;
|
119 |
|
120 | if (this.initialBufsLen < 16)
|
121 | return '';
|
122 |
|
123 |
|
124 | var encoding = detectEncoding(this.initialBufs, this.options.defaultEncoding);
|
125 | this.decoder = this.iconv.getDecoder(encoding, this.options);
|
126 |
|
127 | var resStr = '';
|
128 | for (var i = 0; i < this.initialBufs.length; i++)
|
129 | resStr += this.decoder.write(this.initialBufs[i]);
|
130 |
|
131 | this.initialBufs.length = this.initialBufsLen = 0;
|
132 | return resStr;
|
133 | }
|
134 |
|
135 | return this.decoder.write(buf);
|
136 | }
|
137 |
|
138 | Utf16Decoder.prototype.end = function() {
|
139 | if (!this.decoder) {
|
140 | var encoding = detectEncoding(this.initialBufs, this.options.defaultEncoding);
|
141 | this.decoder = this.iconv.getDecoder(encoding, this.options);
|
142 |
|
143 | var resStr = '';
|
144 | for (var i = 0; i < this.initialBufs.length; i++)
|
145 | resStr += this.decoder.write(this.initialBufs[i]);
|
146 |
|
147 | var trail = this.decoder.end();
|
148 | if (trail)
|
149 | resStr += trail;
|
150 |
|
151 | this.initialBufs.length = this.initialBufsLen = 0;
|
152 | return resStr;
|
153 | }
|
154 | return this.decoder.end();
|
155 | }
|
156 |
|
157 | function detectEncoding(bufs, defaultEncoding) {
|
158 | var b = [];
|
159 | var charsProcessed = 0;
|
160 | var asciiCharsLE = 0, asciiCharsBE = 0;
|
161 |
|
162 | outer_loop:
|
163 | for (var i = 0; i < bufs.length; i++) {
|
164 | var buf = bufs[i];
|
165 | for (var j = 0; j < buf.length; j++) {
|
166 | b.push(buf[j]);
|
167 | if (b.length === 2) {
|
168 | if (charsProcessed === 0) {
|
169 |
|
170 | if (b[0] === 0xFF && b[1] === 0xFE) return 'utf-16le';
|
171 | if (b[0] === 0xFE && b[1] === 0xFF) return 'utf-16be';
|
172 | }
|
173 |
|
174 | if (b[0] === 0 && b[1] !== 0) asciiCharsBE++;
|
175 | if (b[0] !== 0 && b[1] === 0) asciiCharsLE++;
|
176 |
|
177 | b.length = 0;
|
178 | charsProcessed++;
|
179 |
|
180 | if (charsProcessed >= 100) {
|
181 | break outer_loop;
|
182 | }
|
183 | }
|
184 | }
|
185 | }
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | if (asciiCharsBE > asciiCharsLE) return 'utf-16be';
|
191 | if (asciiCharsBE < asciiCharsLE) return 'utf-16le';
|
192 |
|
193 |
|
194 | return defaultEncoding || 'utf-16le';
|
195 | }
|
196 |
|
197 |
|