1 | /**
|
---|
2 | * Base-N/Base-X encoding/decoding functions.
|
---|
3 | *
|
---|
4 | * Original implementation from base-x:
|
---|
5 | * https://github.com/cryptocoinjs/base-x
|
---|
6 | *
|
---|
7 | * Which is MIT licensed:
|
---|
8 | *
|
---|
9 | * The MIT License (MIT)
|
---|
10 | *
|
---|
11 | * Copyright base-x contributors (c) 2016
|
---|
12 | *
|
---|
13 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
14 | * of this software and associated documentation files (the "Software"), to deal
|
---|
15 | * in the Software without restriction, including without limitation the rights
|
---|
16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
17 | * copies of the Software, and to permit persons to whom the Software is
|
---|
18 | * furnished to do so, subject to the following conditions:
|
---|
19 | *
|
---|
20 | * The above copyright notice and this permission notice shall be included in
|
---|
21 | * all copies or substantial portions of the Software.
|
---|
22 | *
|
---|
23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
---|
26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
28 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
---|
29 | * DEALINGS IN THE SOFTWARE.
|
---|
30 | */
|
---|
31 | var api = {};
|
---|
32 | module.exports = api;
|
---|
33 |
|
---|
34 | // baseN alphabet indexes
|
---|
35 | var _reverseAlphabets = {};
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * BaseN-encodes a Uint8Array using the given alphabet.
|
---|
39 | *
|
---|
40 | * @param input the Uint8Array to encode.
|
---|
41 | * @param maxline the maximum number of encoded characters per line to use,
|
---|
42 | * defaults to none.
|
---|
43 | *
|
---|
44 | * @return the baseN-encoded output string.
|
---|
45 | */
|
---|
46 | api.encode = function(input, alphabet, maxline) {
|
---|
47 | if(typeof alphabet !== 'string') {
|
---|
48 | throw new TypeError('"alphabet" must be a string.');
|
---|
49 | }
|
---|
50 | if(maxline !== undefined && typeof maxline !== 'number') {
|
---|
51 | throw new TypeError('"maxline" must be a number.');
|
---|
52 | }
|
---|
53 |
|
---|
54 | var output = '';
|
---|
55 |
|
---|
56 | if(!(input instanceof Uint8Array)) {
|
---|
57 | // assume forge byte buffer
|
---|
58 | output = _encodeWithByteBuffer(input, alphabet);
|
---|
59 | } else {
|
---|
60 | var i = 0;
|
---|
61 | var base = alphabet.length;
|
---|
62 | var first = alphabet.charAt(0);
|
---|
63 | var digits = [0];
|
---|
64 | for(i = 0; i < input.length; ++i) {
|
---|
65 | for(var j = 0, carry = input[i]; j < digits.length; ++j) {
|
---|
66 | carry += digits[j] << 8;
|
---|
67 | digits[j] = carry % base;
|
---|
68 | carry = (carry / base) | 0;
|
---|
69 | }
|
---|
70 |
|
---|
71 | while(carry > 0) {
|
---|
72 | digits.push(carry % base);
|
---|
73 | carry = (carry / base) | 0;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | // deal with leading zeros
|
---|
78 | for(i = 0; input[i] === 0 && i < input.length - 1; ++i) {
|
---|
79 | output += first;
|
---|
80 | }
|
---|
81 | // convert digits to a string
|
---|
82 | for(i = digits.length - 1; i >= 0; --i) {
|
---|
83 | output += alphabet[digits[i]];
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | if(maxline) {
|
---|
88 | var regex = new RegExp('.{1,' + maxline + '}', 'g');
|
---|
89 | output = output.match(regex).join('\r\n');
|
---|
90 | }
|
---|
91 |
|
---|
92 | return output;
|
---|
93 | };
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Decodes a baseN-encoded (using the given alphabet) string to a
|
---|
97 | * Uint8Array.
|
---|
98 | *
|
---|
99 | * @param input the baseN-encoded input string.
|
---|
100 | *
|
---|
101 | * @return the Uint8Array.
|
---|
102 | */
|
---|
103 | api.decode = function(input, alphabet) {
|
---|
104 | if(typeof input !== 'string') {
|
---|
105 | throw new TypeError('"input" must be a string.');
|
---|
106 | }
|
---|
107 | if(typeof alphabet !== 'string') {
|
---|
108 | throw new TypeError('"alphabet" must be a string.');
|
---|
109 | }
|
---|
110 |
|
---|
111 | var table = _reverseAlphabets[alphabet];
|
---|
112 | if(!table) {
|
---|
113 | // compute reverse alphabet
|
---|
114 | table = _reverseAlphabets[alphabet] = [];
|
---|
115 | for(var i = 0; i < alphabet.length; ++i) {
|
---|
116 | table[alphabet.charCodeAt(i)] = i;
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | // remove whitespace characters
|
---|
121 | input = input.replace(/\s/g, '');
|
---|
122 |
|
---|
123 | var base = alphabet.length;
|
---|
124 | var first = alphabet.charAt(0);
|
---|
125 | var bytes = [0];
|
---|
126 | for(var i = 0; i < input.length; i++) {
|
---|
127 | var value = table[input.charCodeAt(i)];
|
---|
128 | if(value === undefined) {
|
---|
129 | return;
|
---|
130 | }
|
---|
131 |
|
---|
132 | for(var j = 0, carry = value; j < bytes.length; ++j) {
|
---|
133 | carry += bytes[j] * base;
|
---|
134 | bytes[j] = carry & 0xff;
|
---|
135 | carry >>= 8;
|
---|
136 | }
|
---|
137 |
|
---|
138 | while(carry > 0) {
|
---|
139 | bytes.push(carry & 0xff);
|
---|
140 | carry >>= 8;
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | // deal with leading zeros
|
---|
145 | for(var k = 0; input[k] === first && k < input.length - 1; ++k) {
|
---|
146 | bytes.push(0);
|
---|
147 | }
|
---|
148 |
|
---|
149 | if(typeof Buffer !== 'undefined') {
|
---|
150 | return Buffer.from(bytes.reverse());
|
---|
151 | }
|
---|
152 |
|
---|
153 | return new Uint8Array(bytes.reverse());
|
---|
154 | };
|
---|
155 |
|
---|
156 | function _encodeWithByteBuffer(input, alphabet) {
|
---|
157 | var i = 0;
|
---|
158 | var base = alphabet.length;
|
---|
159 | var first = alphabet.charAt(0);
|
---|
160 | var digits = [0];
|
---|
161 | for(i = 0; i < input.length(); ++i) {
|
---|
162 | for(var j = 0, carry = input.at(i); j < digits.length; ++j) {
|
---|
163 | carry += digits[j] << 8;
|
---|
164 | digits[j] = carry % base;
|
---|
165 | carry = (carry / base) | 0;
|
---|
166 | }
|
---|
167 |
|
---|
168 | while(carry > 0) {
|
---|
169 | digits.push(carry % base);
|
---|
170 | carry = (carry / base) | 0;
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | var output = '';
|
---|
175 |
|
---|
176 | // deal with leading zeros
|
---|
177 | for(i = 0; input.at(i) === 0 && i < input.length() - 1; ++i) {
|
---|
178 | output += first;
|
---|
179 | }
|
---|
180 | // convert digits to a string
|
---|
181 | for(i = digits.length - 1; i >= 0; --i) {
|
---|
182 | output += alphabet[digits[i]];
|
---|
183 | }
|
---|
184 |
|
---|
185 | return output;
|
---|
186 | }
|
---|