Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / 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) {
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 }
35
36 InternalCodec.prototype.encoder = InternalEncoder;
37 InternalCodec.prototype.decoder = InternalDecoder;
38
39 //------------------------------------------------------------------------------
40
41 // We use node.js internal decoder. It's signature is the same as ours.
42 var StringDecoder = require('string_decoder').StringDecoder;
43
44 if (!StringDecoder.prototype.end) // Node v0.8 doesn't have this method.
45     StringDecoder.prototype.end = function() {};
46
47
48 function InternalDecoder(options, codec) {
49     StringDecoder.call(this, codec.enc);
50 }
51
52 InternalDecoder.prototype = StringDecoder.prototype;
53
54
55 //------------------------------------------------------------------------------
56 // Encoder is mostly trivial
57
58 function InternalEncoder(options, codec) {
59     this.enc = codec.enc;
60 }
61
62 InternalEncoder.prototype.write = function(str) {
63     return new Buffer(str, this.enc);
64 }
65
66 InternalEncoder.prototype.end = function() {
67 }
68
69
70 //------------------------------------------------------------------------------
71 // Except base64 encoder, which must keep its state.
72
73 function InternalEncoderBase64(options, codec) {
74     this.prevStr = '';
75 }
76
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);
82
83     return new Buffer(str, "base64");
84 }
85
86 InternalEncoderBase64.prototype.end = function() {
87     return new Buffer(this.prevStr, "base64");
88 }
89
90
91 //------------------------------------------------------------------------------
92 // CESU-8 encoder is also special.
93
94 function InternalEncoderCesu8(options, codec) {
95 }
96
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).
103         if (charCode < 0x80)
104             buf[bufIdx++] = charCode;
105         else if (charCode < 0x800) {
106             buf[bufIdx++] = 0xC0 + (charCode >>> 6);
107             buf[bufIdx++] = 0x80 + (charCode & 0x3f);
108         }
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);
113         }
114     }
115     return buf.slice(0, bufIdx);
116 }
117
118 InternalEncoderCesu8.prototype.end = function() {
119 }