Initial commit
[yaffs-website] / node_modules / iconv-lite / encodings / internal.js
1 "use strict"
2
3 // Export Node.js internal encodings.
4
5 module.exports = {
6     // Encodings
7     utf8:   { type: "_internal", bomAware: true},
8     cesu8:  { type: "_internal", bomAware: true},
9     unicode11utf8: "utf8",
10
11     ucs2:   { type: "_internal", bomAware: true},
12     utf16le: "ucs2",
13
14     binary: { type: "_internal" },
15     base64: { type: "_internal" },
16     hex:    { type: "_internal" },
17
18     // Codec.
19     _internal: InternalCodec,
20 };
21
22 //------------------------------------------------------------------------------
23
24 function InternalCodec(codecOptions, iconv) {
25     this.enc = codecOptions.encodingName;
26     this.bomAware = codecOptions.bomAware;
27
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;
33
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;
38         }
39     }
40 }
41
42 InternalCodec.prototype.encoder = InternalEncoder;
43 InternalCodec.prototype.decoder = InternalDecoder;
44
45 //------------------------------------------------------------------------------
46
47 // We use node.js internal decoder. Its signature is the same as ours.
48 var StringDecoder = require('string_decoder').StringDecoder;
49
50 if (!StringDecoder.prototype.end) // Node v0.8 doesn't have this method.
51     StringDecoder.prototype.end = function() {};
52
53
54 function InternalDecoder(options, codec) {
55     StringDecoder.call(this, codec.enc);
56 }
57
58 InternalDecoder.prototype = StringDecoder.prototype;
59
60
61 //------------------------------------------------------------------------------
62 // Encoder is mostly trivial
63
64 function InternalEncoder(options, codec) {
65     this.enc = codec.enc;
66 }
67
68 InternalEncoder.prototype.write = function(str) {
69     return new Buffer(str, this.enc);
70 }
71
72 InternalEncoder.prototype.end = function() {
73 }
74
75
76 //------------------------------------------------------------------------------
77 // Except base64 encoder, which must keep its state.
78
79 function InternalEncoderBase64(options, codec) {
80     this.prevStr = '';
81 }
82
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);
88
89     return new Buffer(str, "base64");
90 }
91
92 InternalEncoderBase64.prototype.end = function() {
93     return new Buffer(this.prevStr, "base64");
94 }
95
96
97 //------------------------------------------------------------------------------
98 // CESU-8 encoder is also special.
99
100 function InternalEncoderCesu8(options, codec) {
101 }
102
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).
109         if (charCode < 0x80)
110             buf[bufIdx++] = charCode;
111         else if (charCode < 0x800) {
112             buf[bufIdx++] = 0xC0 + (charCode >>> 6);
113             buf[bufIdx++] = 0x80 + (charCode & 0x3f);
114         }
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);
119         }
120     }
121     return buf.slice(0, bufIdx);
122 }
123
124 InternalEncoderCesu8.prototype.end = function() {
125 }
126
127 //------------------------------------------------------------------------------
128 // CESU-8 decoder is not implemented in Node v4.0+
129
130 function InternalDecoderCesu8(options, codec) {
131     this.acc = 0;
132     this.contBytes = 0;
133     this.accBytes = 0;
134     this.defaultCharUnicode = codec.defaultCharUnicode;
135 }
136
137 InternalDecoderCesu8.prototype.write = function(buf) {
138     var acc = this.acc, contBytes = this.contBytes, accBytes = this.accBytes, 
139         res = '';
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;
145                 contBytes = 0;
146             }
147
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;
158             }
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;
169                     else
170                         // Actually add character.
171                         res += String.fromCharCode(acc);
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
182 InternalDecoderCesu8.prototype.end = function() {
183     var res = 0;
184     if (this.contBytes > 0)
185         res += this.defaultCharUnicode;
186     return res;
187 }