Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
internal.js219 linesDownload Raw Back to encodings
1"use strict"
2var Buffer = require("safer-buffer").Buffer
3
4// Export Node.js internal encodings.
5
6module.exports = {
7  // Encodings
8  utf8: { type: "_internal", bomAware: true },
9  cesu8: { type: "_internal", bomAware: true },
10  unicode11utf8: "utf8",
11
12  ucs2: { type: "_internal", bomAware: true },
13  utf16le: "ucs2",
14
15  binary: { type: "_internal" },
16  base64: { type: "_internal" },
17  hex: { type: "_internal" },
18
19  // Codec.
20  _internal: InternalCodec
21}
22
23// ------------------------------------------------------------------------------
24
25function InternalCodec (codecOptions, iconv) {
26  this.enc = codecOptions.encodingName
27  this.bomAware = codecOptions.bomAware
28
29  if (this.enc === "base64") { this.encoder = InternalEncoderBase64 } else if (this.enc === "utf8") { this.encoder = InternalEncoderUtf8 } else if (this.enc === "cesu8") {
30    this.enc = "utf8" // Use utf8 for decoding.
31    this.encoder = InternalEncoderCesu8
32
33    // Add decoder for versions of Node not supporting CESU-8
34    if (Buffer.from("eda0bdedb2a9", "hex").toString() !== "💩") {
35      this.decoder = InternalDecoderCesu8
36      this.defaultCharUnicode = iconv.defaultCharUnicode
37    }
38  }
39}
40
41InternalCodec.prototype.encoder = InternalEncoder
42InternalCodec.prototype.decoder = InternalDecoder
43
44// ------------------------------------------------------------------------------
45
46// We use node.js internal decoder. Its signature is the same as ours.
47var StringDecoder = require("string_decoder").StringDecoder
48
49function InternalDecoder (options, codec) {
50  this.decoder = new StringDecoder(codec.enc)
51}
52
53InternalDecoder.prototype.write = function (buf) {
54  if (!Buffer.isBuffer(buf)) {
55    buf = Buffer.from(buf)
56  }
57
58  return this.decoder.write(buf)
59}
60
61InternalDecoder.prototype.end = function () {
62  return this.decoder.end()
63}
64
65// ------------------------------------------------------------------------------
66// Encoder is mostly trivial
67
68function InternalEncoder (options, codec) {
69  this.enc = codec.enc
70}
71
72InternalEncoder.prototype.write = function (str) {
73  return Buffer.from(str, this.enc)
74}
75
76InternalEncoder.prototype.end = function () {
77}
78
79// ------------------------------------------------------------------------------
80// Except base64 encoder, which must keep its state.
81
82function InternalEncoderBase64 (options, codec) {
83  this.prevStr = ""
84}
85
86InternalEncoderBase64.prototype.write = function (str) {
87  str = this.prevStr + str
88  var completeQuads = str.length - (str.length % 4)
89  this.prevStr = str.slice(completeQuads)
90  str = str.slice(0, completeQuads)
91
92  return Buffer.from(str, "base64")
93}
94
95InternalEncoderBase64.prototype.end = function () {
96  return Buffer.from(this.prevStr, "base64")
97}
98
99// ------------------------------------------------------------------------------
100// CESU-8 encoder is also special.
101
102function InternalEncoderCesu8 (options, codec) {
103}
104
105InternalEncoderCesu8.prototype.write = function (str) {
106  var buf = Buffer.alloc(str.length * 3); var bufIdx = 0
107  for (var i = 0; i < str.length; i++) {
108    var charCode = str.charCodeAt(i)
109    // Naive implementation, but it works because CESU-8 is especially easy
110    // to convert from UTF-16 (which all JS strings are encoded in).
111    if (charCode < 0x80) { buf[bufIdx++] = charCode } else if (charCode < 0x800) {
112      buf[bufIdx++] = 0xC0 + (charCode >>> 6)
113      buf[bufIdx++] = 0x80 + (charCode & 0x3f)
114    } else { // charCode will always be < 0x10000 in javascript.
115      buf[bufIdx++] = 0xE0 + (charCode >>> 12)
116      buf[bufIdx++] = 0x80 + ((charCode >>> 6) & 0x3f)
117      buf[bufIdx++] = 0x80 + (charCode & 0x3f)
118    }
119  }
120  return buf.slice(0, bufIdx)
121}
122
123InternalEncoderCesu8.prototype.end = function () {
124}
125
126// ------------------------------------------------------------------------------
127// CESU-8 decoder is not implemented in Node v4.0+
128
129function InternalDecoderCesu8 (options, codec) {
130  this.acc = 0
131  this.contBytes = 0
132  this.accBytes = 0
133  this.defaultCharUnicode = codec.defaultCharUnicode
134}
135
136InternalDecoderCesu8.prototype.write = function (buf) {
137  var acc = this.acc; var contBytes = this.contBytes; var accBytes = this.accBytes
138  var res = ""
139  for (var i = 0; i < buf.length; i++) {
140    var curByte = buf[i]
141    if ((curByte & 0xC0) !== 0x80) { // Leading byte
142      if (contBytes > 0) { // Previous code is invalid
143        res += this.defaultCharUnicode
144        contBytes = 0
145      }
146
147      if (curByte < 0x80) { // Single-byte code
148        res += String.fromCharCode(curByte)
149      } else if (curByte < 0xE0) { // Two-byte code
150        acc = curByte & 0x1F
151        contBytes = 1; accBytes = 1
152      } else if (curByte < 0xF0) { // Three-byte code
153        acc = curByte & 0x0F
154        contBytes = 2; accBytes = 1
155      } else { // Four or more are not supported for CESU-8.
156        res += this.defaultCharUnicode
157      }
158    } else { // Continuation byte
159      if (contBytes > 0) { // We're waiting for it.
160        acc = (acc << 6) | (curByte & 0x3f)
161        contBytes--; accBytes++
162        if (contBytes === 0) {
163          // Check for overlong encoding, but support Modified UTF-8 (encoding NULL as C0 80)
164          if (accBytes === 2 && acc < 0x80 && acc > 0) {
165            res += this.defaultCharUnicode
166          } else if (accBytes === 3 && acc < 0x800) {
167            res += this.defaultCharUnicode
168          } else {
169            // Actually add character.
170            res += String.fromCharCode(acc)
171          }
172        }
173      } else { // Unexpected continuation byte
174        res += this.defaultCharUnicode
175      }
176    }
177  }
178  this.acc = acc; this.contBytes = contBytes; this.accBytes = accBytes
179  return res
180}
181
182InternalDecoderCesu8.prototype.end = function () {
183  var res = 0
184  if (this.contBytes > 0) { res += this.defaultCharUnicode }
185  return res
186}
187
188// ------------------------------------------------------------------------------
189// check the chunk boundaries for surrogate pair
190
191function InternalEncoderUtf8 (options, codec) {
192  this.highSurrogate = ""
193}
194
195InternalEncoderUtf8.prototype.write = function (str) {
196  if (this.highSurrogate) {
197    str = this.highSurrogate + str
198    this.highSurrogate = ""
199  }
200
201  if (str.length > 0) {
202    var charCode = str.charCodeAt(str.length - 1)
203    if (charCode >= 0xd800 && charCode < 0xdc00) {
204      this.highSurrogate = str[str.length - 1]
205      str = str.slice(0, str.length - 1)
206    }
207  }
208
209  return Buffer.from(str, this.enc)
210}
211
212InternalEncoderUtf8.prototype.end = function () {
213  if (this.highSurrogate) {
214    var str = this.highSurrogate
215    this.highSurrogate = ""
216    return Buffer.from(str, this.enc)
217  }
218}
219 
codekingpro/portable-devtools · Team Ai