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) {
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;
36 InternalCodec.prototype.encoder = InternalEncoder;
37 InternalCodec.prototype.decoder = InternalDecoder;
39 //------------------------------------------------------------------------------
41 // We use node.js internal decoder. It's signature is the same as ours.
42 var StringDecoder = require('string_decoder').StringDecoder;
44 if (!StringDecoder.prototype.end) // Node v0.8 doesn't have this method.
45 StringDecoder.prototype.end = function() {};
48 function InternalDecoder(options, codec) {
49 StringDecoder.call(this, codec.enc);
52 InternalDecoder.prototype = StringDecoder.prototype;
55 //------------------------------------------------------------------------------
56 // Encoder is mostly trivial
58 function InternalEncoder(options, codec) {
62 InternalEncoder.prototype.write = function(str) {
63 return new Buffer(str, this.enc);
66 InternalEncoder.prototype.end = function() {
70 //------------------------------------------------------------------------------
71 // Except base64 encoder, which must keep its state.
73 function InternalEncoderBase64(options, codec) {
77 InternalEncoderBase64.prototype.write = function(str) {
78 str = this.prevStr + str;
79 var completeQuads = str.length - (str.length % 4);
80 this.prevStr = str.slice(completeQuads);
81 str = str.slice(0, completeQuads);
83 return new Buffer(str, "base64");
86 InternalEncoderBase64.prototype.end = function() {
87 return new Buffer(this.prevStr, "base64");
91 //------------------------------------------------------------------------------
92 // CESU-8 encoder is also special.
94 function InternalEncoderCesu8(options, codec) {
97 InternalEncoderCesu8.prototype.write = function(str) {
98 var buf = new Buffer(str.length * 3), bufIdx = 0;
99 for (var i = 0; i < str.length; i++) {
100 var charCode = str.charCodeAt(i);
101 // Naive implementation, but it works because CESU-8 is especially easy
102 // to convert from UTF-16 (which all JS strings are encoded in).
104 buf[bufIdx++] = charCode;
105 else if (charCode < 0x800) {
106 buf[bufIdx++] = 0xC0 + (charCode >>> 6);
107 buf[bufIdx++] = 0x80 + (charCode & 0x3f);
109 else { // charCode will always be < 0x10000 in javascript.
110 buf[bufIdx++] = 0xE0 + (charCode >>> 12);
111 buf[bufIdx++] = 0x80 + ((charCode >>> 6) & 0x3f);
112 buf[bufIdx++] = 0x80 + (charCode & 0x3f);
115 return buf.slice(0, bufIdx);
118 InternalEncoderCesu8.prototype.end = function() {