Initial commit for OpenECOMP SDN-C OA&M
[sdnc/oam.git] / dgbuilder / dgeflows / node_modules / body-parser / node_modules / iconv-lite / encodings / sbcs-codec.js
1
2 // Single-byte codec. Needs a 'chars' string parameter that contains 256 or 128 chars that
3 // correspond to encoded bytes (if 128 - then lower half is ASCII). 
4
5 exports._sbcs = function(options) {
6     if (!options)
7         throw new Error("SBCS codec is called without the data.")
8     
9     // Prepare char buffer for decoding.
10     if (!options.chars || (options.chars.length !== 128 && options.chars.length !== 256))
11         throw new Error("Encoding '"+options.type+"' has incorrect 'chars' (must be of len 128 or 256)");
12     
13     if (options.chars.length === 128) {
14         var asciiString = "";
15         for (var i = 0; i < 128; i++)
16             asciiString += String.fromCharCode(i);
17         options.chars = asciiString + options.chars;
18     }
19
20     var decodeBuf = new Buffer(options.chars, 'ucs2');
21     
22     // Encoding buffer.
23     var encodeBuf = new Buffer(65536);
24     encodeBuf.fill(options.iconv.defaultCharSingleByte.charCodeAt(0));
25
26     for (var i = 0; i < options.chars.length; i++)
27         encodeBuf[options.chars.charCodeAt(i)] = i;
28
29     return {
30         encoder: encoderSBCS,
31         decoder: decoderSBCS,
32
33         encodeBuf: encodeBuf,
34         decodeBuf: decodeBuf,
35     };
36 }
37
38 function encoderSBCS(options) {
39     return {
40         write: encoderSBCSWrite,
41         end: function() {},
42
43         encodeBuf: this.encodeBuf,
44     };
45 }
46
47 function encoderSBCSWrite(str) {
48     var buf = new Buffer(str.length);
49     for (var i = 0; i < str.length; i++)
50         buf[i] = this.encodeBuf[str.charCodeAt(i)];
51     
52     return buf;
53 }
54
55
56 function decoderSBCS(options) {
57     return {
58         write: decoderSBCSWrite,
59         end: function() {},
60         
61         decodeBuf: this.decodeBuf,
62     };
63 }
64
65 function decoderSBCSWrite(buf) {
66     // Strings are immutable in JS -> we use ucs2 buffer to speed up computations.
67     var decodeBuf = this.decodeBuf;
68     var newBuf = new Buffer(buf.length*2);
69     var idx1 = 0, idx2 = 0;
70     for (var i = 0, _len = buf.length; i < _len; i++) {
71         idx1 = buf[i]*2; idx2 = i*2;
72         newBuf[idx2] = decodeBuf[idx1];
73         newBuf[idx2+1] = decodeBuf[idx1+1];
74     }
75     return newBuf.toString('ucs2');
76 }