codekingpro/portable-devtools
114k
1"use strict"
2var Buffer = require("safer-buffer").Buffer
3
4// Note: UTF16-LE (or UCS2) codec is Node.js native. See encodings/internal.js
5
6// == UTF16-BE codec. ==========================================================
7
8exports.utf16be = Utf16BECodec
9function Utf16BECodec () {
10}
11
12Utf16BECodec.prototype.encoder = Utf16BEEncoder
13Utf16BECodec.prototype.decoder = Utf16BEDecoder
14Utf16BECodec.prototype.bomAware = true
15
16// -- Encoding
17
18function Utf16BEEncoder () {
19}
20
21Utf16BEEncoder.prototype.write = function (str) {
22 var buf = Buffer.from(str, "ucs2")
23 for (var i = 0; i < buf.length; i += 2) {
24 var tmp = buf[i]; buf[i] = buf[i + 1]; buf[i + 1] = tmp
25 }
26 return buf
27}
28
29Utf16BEEncoder.prototype.end = function () {
30}
31
32// -- Decoding
33
34function Utf16BEDecoder () {
35 this.overflowByte = -1
36}
37
38Utf16BEDecoder.prototype.write = function (buf) {
39 if (buf.length == 0) { return "" }
40
41 var buf2 = Buffer.alloc(buf.length + 1)
42 var i = 0; var j = 0
43
44 if (this.overflowByte !== -1) {
45 buf2[0] = buf[0]
46 buf2[1] = this.overflowByte
47 i = 1; j = 2
48 }
49
50 for (; i < buf.length - 1; i += 2, j += 2) {
51 buf2[j] = buf[i + 1]
52 buf2[j + 1] = buf[i]
53 }
54
55 this.overflowByte = (i == buf.length - 1) ? buf[buf.length - 1] : -1
56
57 return buf2.slice(0, j).toString("ucs2")
58}
59
60Utf16BEDecoder.prototype.end = function () {
61 this.overflowByte = -1
62}
63
64// == UTF-16 codec =============================================================
65// Decoder chooses automatically from UTF-16LE and UTF-16BE using BOM and space-based heuristic.
66// Defaults to UTF-16LE, as it's prevalent and default in Node.
67// http://en.wikipedia.org/wiki/UTF-16 and http://encoding.spec.whatwg.org/#utf-16le
68// Decoder default can be changed: iconv.decode(buf, 'utf16', {defaultEncoding: 'utf-16be'});
69
70// Encoder uses UTF-16LE and prepends BOM (which can be overridden with addBOM: false).
71
72exports.utf16 = Utf16Codec
73function Utf16Codec (codecOptions, iconv) {
74 this.iconv = iconv
75}
76
77Utf16Codec.prototype.encoder = Utf16Encoder
78Utf16Codec.prototype.decoder = Utf16Decoder
79
80// -- Encoding (pass-through)
81
82function Utf16Encoder (options, codec) {
83 options = options || {}
84 if (options.addBOM === undefined) { options.addBOM = true }
85 this.encoder = codec.iconv.getEncoder("utf-16le", options)
86}
87
88Utf16Encoder.prototype.write = function (str) {
89 return this.encoder.write(str)
90}
91
92Utf16Encoder.prototype.end = function () {
93 return this.encoder.end()
94}
95
96// -- Decoding
97
98function Utf16Decoder (options, codec) {
99 this.decoder = null
100 this.initialBufs = []
101 this.initialBufsLen = 0
102
103 this.options = options || {}
104 this.iconv = codec.iconv
105}
106
107Utf16Decoder.prototype.write = function (buf) {
108 if (!this.decoder) {
109 // Codec is not chosen yet. Accumulate initial bytes.
110 this.initialBufs.push(buf)
111 this.initialBufsLen += buf.length
112
113 if (this.initialBufsLen < 16) // We need more bytes to use space heuristic (see below)
114 { return "" }
115
116 // We have enough bytes -> detect endianness.
117 var encoding = detectEncoding(this.initialBufs, this.options.defaultEncoding)
118 this.decoder = this.iconv.getDecoder(encoding, this.options)
119
120 var resStr = ""
121 for (var i = 0; i < this.initialBufs.length; i++) { resStr += this.decoder.write(this.initialBufs[i]) }
122
123 this.initialBufs.length = this.initialBufsLen = 0
124 return resStr
125 }
126
127 return this.decoder.write(buf)
128}
129
130Utf16Decoder.prototype.end = function () {
131 if (!this.decoder) {
132 var encoding = detectEncoding(this.initialBufs, this.options.defaultEncoding)
133 this.decoder = this.iconv.getDecoder(encoding, this.options)
134
135 var resStr = ""
136 for (var i = 0; i < this.initialBufs.length; i++) { resStr += this.decoder.write(this.initialBufs[i]) }
137
138 var trail = this.decoder.end()
139 if (trail) { resStr += trail }
140
141 this.initialBufs.length = this.initialBufsLen = 0
142 return resStr
143 }
144 return this.decoder.end()
145}
146
147function detectEncoding (bufs, defaultEncoding) {
148 var b = []
149 var charsProcessed = 0
150 // Number of ASCII chars when decoded as LE or BE.
151 var asciiCharsLE = 0
152 var asciiCharsBE = 0
153
154 outerLoop:
155 for (var i = 0; i < bufs.length; i++) {
156 var buf = bufs[i]
157 for (var j = 0; j < buf.length; j++) {
158 b.push(buf[j])
159 if (b.length === 2) {
160 if (charsProcessed === 0) {
161 // Check BOM first.
162 if (b[0] === 0xFF && b[1] === 0xFE) return "utf-16le"
163 if (b[0] === 0xFE && b[1] === 0xFF) return "utf-16be"
164 }
165
166 if (b[0] === 0 && b[1] !== 0) asciiCharsBE++
167 if (b[0] !== 0 && b[1] === 0) asciiCharsLE++
168
169 b.length = 0
170 charsProcessed++
171
172 if (charsProcessed >= 100) {
173 break outerLoop
174 }
175 }
176 }
177 }
178
179 // Make decisions.
180 // Most of the time, the content has ASCII chars (U+00**), but the opposite (U+**00) is uncommon.
181 // So, we count ASCII as if it was LE or BE, and decide from that.
182 if (asciiCharsBE > asciiCharsLE) return "utf-16be"
183 if (asciiCharsBE < asciiCharsLE) return "utf-16le"
184
185 // Couldn't decide (likely all zeros or not enough data).
186 return defaultEncoding || "utf-16le"
187}
188 