Fix license issues
[sdnc/oam.git] / dgbuilder / dgeflows / node_modules / body-parser / node_modules / iconv-lite / encodings / internal.js
1
2 // Export Node.js internal encodings.
3
4 var utf16lebom = new Buffer([0xFF, 0xFE]);
5
6 module.exports = {
7     // Encodings
8     utf8:   { type: "_internal", enc: "utf8" },
9     cesu8:  { type: "_internal", enc: "utf8" },
10     unicode11utf8: { type: "_internal", enc: "utf8" },
11     ucs2:   { type: "_internal", enc: "ucs2", bom: utf16lebom },
12     utf16le:{ type: "_internal", enc: "ucs2", bom: utf16lebom },
13     binary: { type: "_internal", enc: "binary" },
14     base64: { type: "_internal", enc: "base64" },
15     hex:    { type: "_internal", enc: "hex" },
16
17     // Codec.
18     _internal: function(options) {
19         if (!options || !options.enc)
20             throw new Error("Internal codec is called without encoding type.")
21
22         return {
23             encoder: options.enc == "base64" ? encoderBase64 : encoderInternal,
24             decoder: decoderInternal,
25
26             enc: options.enc,
27             bom: options.bom,
28         };
29     },
30 };
31
32 // We use node.js internal decoder. It's signature is the same as ours.
33 var StringDecoder = require('string_decoder').StringDecoder;
34
35 if (!StringDecoder.prototype.end) // Node v0.8 doesn't have this method.
36     StringDecoder.prototype.end = function() {};
37
38 function decoderInternal() {
39     return new StringDecoder(this.enc);
40 }
41
42 // Encoder is mostly trivial
43
44 function encoderInternal() {
45     return {
46         write: encodeInternal,
47         end: function() {},
48         
49         enc: this.enc,
50     }
51 }
52
53 function encodeInternal(str) {
54     return new Buffer(str, this.enc);
55 }
56
57
58 // Except base64 encoder, which must keep its state.
59
60 function encoderBase64() {
61     return {
62         write: encodeBase64Write,
63         end: encodeBase64End,
64
65         prevStr: '',
66     };
67 }
68
69 function encodeBase64Write(str) {
70     str = this.prevStr + str;
71     var completeQuads = str.length - (str.length % 4);
72     this.prevStr = str.slice(completeQuads);
73     str = str.slice(0, completeQuads);
74
75     return new Buffer(str, "base64");
76 }
77
78 function encodeBase64End() {
79     return new Buffer(this.prevStr, "base64");
80 }
81