3 // Export Node.js internal encodings.
7 utf8: { type: "_internal", bomAware: true},
8 cesu8: { type: "_internal", bomAware: true},
11 ucs2: { type: "_internal", bomAware: true},
14 binary: { type: "_internal" },
15 base64: { type: "_internal" },
16 hex: { type: "_internal" },
19 _internal: InternalCodec,
22 //------------------------------------------------------------------------------
24 function InternalCodec(codecOptions, iconv) {
25 this.enc = codecOptions.encodingName;
26 this.bomAware = codecOptions.bomAware;
28 if (this.enc === "base64")
29 this.encoder = InternalEncoderBase64;
30 else if (this.enc === "cesu8") {
31 this.enc = "utf8"; // Use utf8 for decoding.
32 this.encoder = InternalEncoderCesu8;
34 // Add decoder for versions of Node not supporting CESU-8
35 if (new Buffer("eda080", 'hex').toString().length == 3) {
36 this.decoder = InternalDecoderCesu8;
37 this.defaultCharUnicode = iconv.defaultCharUnicode;
42 InternalCodec.prototype.encoder = InternalEncoder;
43 InternalCodec.prototype.decoder = InternalDecoder;
45 //------------------------------------------------------------------------------
47 // We use node.js internal decoder. Its signature is the same as ours.
48 var StringDecoder = require('string_decoder').StringDecoder;
50 if (!StringDecoder.prototype.end) // Node v0.8 doesn't have this method.
51 StringDecoder.prototype.end = function() {};
54 function InternalDecoder(options, codec) {
55 StringDecoder.call(this, codec.enc);
58 InternalDecoder.prototype = StringDecoder.prototype;
61 //------------------------------------------------------------------------------
62 // Encoder is mostly trivial
64 function InternalEncoder(options, codec) {
68 InternalEncoder.prototype.write = function(str) {
69 return new Buffer(str, this.enc);
72 InternalEncoder.prototype.end = function() {
76 //------------------------------------------------------------------------------
77 // Except base64 encoder, which must keep its state.
79 function InternalEncoderBase64(options, codec) {
83 InternalEncoderBase64.prototype.write = function(str) {
84 str = this.prevStr + str;
85 var completeQuads = str.length - (str.length % 4);
86 this.prevStr = str.slice(completeQuads);
87 str = str.slice(0, completeQuads);
89 return new Buffer(str, "base64");
92 InternalEncoderBase64.prototype.end = function() {
93 return new Buffer(this.prevStr, "base64");
97 //------------------------------------------------------------------------------
98 // CESU-8 encoder is also special.
100 function InternalEncoderCesu8(options, codec) {
103 InternalEncoderCesu8.prototype.write = function(str) {
104 var buf = new Buffer(str.length * 3), bufIdx = 0;
105 for (var i = 0; i < str.length; i++) {
106 var charCode = str.charCodeAt(i);
107 // Naive implementation, but it works because CESU-8 is especially easy
108 // to convert from UTF-16 (which all JS strings are encoded in).
110 buf[bufIdx++] = charCode;
111 else if (charCode < 0x800) {
112 buf[bufIdx++] = 0xC0 + (charCode >>> 6);
113 buf[bufIdx++] = 0x80 + (charCode & 0x3f);
115 else { // charCode will always be < 0x10000 in javascript.
116 buf[bufIdx++] = 0xE0 + (charCode >>> 12);
117 buf[bufIdx++] = 0x80 + ((charCode >>> 6) & 0x3f);
118 buf[bufIdx++] = 0x80 + (charCode & 0x3f);
121 return buf.slice(0, bufIdx);
124 InternalEncoderCesu8.prototype.end = function() {
127 //------------------------------------------------------------------------------
128 // CESU-8 decoder is not implemented in Node v4.0+
130 function InternalDecoderCesu8(options, codec) {
134 this.defaultCharUnicode = codec.defaultCharUnicode;
137 InternalDecoderCesu8.prototype.write = function(buf) {
138 var acc = this.acc, contBytes = this.contBytes, accBytes = this.accBytes,
140 for (var i = 0; i < buf.length; i++) {
141 var curByte = buf[i];
142 if ((curByte & 0xC0) !== 0x80) { // Leading byte
143 if (contBytes > 0) { // Previous code is invalid
144 res += this.defaultCharUnicode;
148 if (curByte < 0x80) { // Single-byte code
149 res += String.fromCharCode(curByte);
150 } else if (curByte < 0xE0) { // Two-byte code
151 acc = curByte & 0x1F;
152 contBytes = 1; accBytes = 1;
153 } else if (curByte < 0xF0) { // Three-byte code
154 acc = curByte & 0x0F;
155 contBytes = 2; accBytes = 1;
156 } else { // Four or more are not supported for CESU-8.
157 res += this.defaultCharUnicode;
159 } else { // Continuation byte
160 if (contBytes > 0) { // We're waiting for it.
161 acc = (acc << 6) | (curByte & 0x3f);
162 contBytes--; accBytes++;
163 if (contBytes === 0) {
164 // Check for overlong encoding, but support Modified UTF-8 (encoding NULL as C0 80)
165 if (accBytes === 2 && acc < 0x80 && acc > 0)
166 res += this.defaultCharUnicode;
167 else if (accBytes === 3 && acc < 0x800)
168 res += this.defaultCharUnicode;
170 // Actually add character.
171 res += String.fromCharCode(acc);
173 } else { // Unexpected continuation byte
174 res += this.defaultCharUnicode;
178 this.acc = acc; this.contBytes = contBytes; this.accBytes = accBytes;
182 InternalDecoderCesu8.prototype.end = function() {
184 if (this.contBytes > 0)
185 res += this.defaultCharUnicode;