/
6bit-encoder.js
269 lines (248 loc) · 6.22 KB
/
6bit-encoder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
'use strict'
/* eslint-disable yoda */
const assert = require('assert')
const ENCODE_OUTOFBOUNDS = 'The number to be encoded is out of bounds '
/**
* A URL safe char that is not part of the encoding table and
* can be used as a separator: `'*'`
*
* @constant {String} SEPARATOR
* @default
*/
const SEPARATOR = '*'
/**
* Remaining URL safe chars that are not part of the encoding table and
* can be used as a separator: `; , . ~ ( ' ) ! * : @`
*
* @constant {Array.<String>} SEPARATORS
* @default
*/
const SEPARATORS = '; , . ~ ( \' ) ! * : @'.split(' ')
function maskForBits(n) {
return (0b1 << n) - 1
}
function isolate(bits, idx, len) {
return (bits >> idx) & maskForBits(len)
}
function getDecodingTable(arr) {
const map = new Map()
for (var i = 0; i < arr.length; i++) {
map.set(arr[i], i)
}
return map
}
// Based on: https://stackoverflow.com/a/40415059/97443 the following are safe
// A-Z a-z 0-9 - . _ ~ ( ) ' ! * : @ , ;
// safe+unsafe: ; $ - _ . + ! * ' ( ),
// Also see: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
// Two major differences to standard base64: (http://www.motobit.com/util/wiki/Base64)
// - number digits are encoded at correct index, thus that they don't need to be encoded/decoded
// - idxs 62 and 63 have been changed to characters that are url safe (from + and =)
const encodingTable = [
'0', '1', '2', '3', '4', '5', '6', '7', // 00-07
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F', // 08-15
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', // 16-23
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', // 24-31
'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', // 32-39
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', // 40-47
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', // 48-55
'u', 'v', 'w', 'x', 'y', 'z', '-', '_' // 56-63
]
const decodingTable = getDecodingTable(encodingTable)
/**
* Decodes one char into a 6 bit number
*
* @name decode
*
* @param {String} s the char to decode
* @returns {Number} a 6 bit number
*/
function decode(s) {
assert.equal(s.length, 1)
return decodingTable.get(s)
}
/**
* Decodes two chars into a 12 bit number
*
* @name decode2
*
* @param {String} s the chars to decode
* @returns {Number} a 12 bit number
*/
function decode2(s) {
assert.equal(s.length, 2)
const [ upper, lower ] = s
return (decode(upper) << 6) | decode(lower)
}
/**
* Decodes three chars into an 18 bit number
*
* @name decode3
*
* @param {String} s the chars to decode
* @returns {Number} an 18 bit number
*/
function decode3(s) {
assert.equal(s.length, 3)
const [ upper, mid, lower ] = s
return (decode(upper) << 12) | (decode(mid) << 6) | decode(lower)
}
/**
* Decodes four chars into an 24 bit number
*
* @name decode4
*
* @param {String} s the chars to decode
* @returns {Number} a 24 bit number
*/
function decode4(s) {
assert.equal(s.length, 4)
const [ upper, belowUpper, aboveLower, lower ] = s
return (decode(upper) << 18) | (decode(belowUpper) << 12)
| (decode(aboveLower) << 6) | decode(lower)
}
/**
* Decodes five chars into an 30 bit number
*
* @name decode5
*
* @param {String} s the chars to decode
* @returns {Number} a 30 bit number
*/
function decode5(s) {
assert.equal(s.length, 5)
const [ upper, belowUpper, mid, aboveLower, lower ] = s
return (decode(upper) << 24) | (decode(belowUpper) << 18)
| (decode(mid) << 12) | (decode(aboveLower) << 6) | decode(lower)
}
/**
* Encodes a 6 bit number into a URL safe char
*
* @name encode
*
* @param {Number} n a 6 bit number
* @returns {String} the char
*/
function encode(n) {
assert(0 <= n && n <= 0x3f, ENCODE_OUTOFBOUNDS + n)
// 6 bits -> 1 digit
return encodingTable[n]
}
/**
* Encodes a 12 bit number into two URL safe chars
*
* @name encode2
*
* @param {Number} n a 12 bit number
* @returns {String} the chars
*/
function encode2(n) {
assert(0 <= n && n <= 0xfff, ENCODE_OUTOFBOUNDS + n)
// 12 bits -> 2 digits
const lower = isolate(n, 0, 6)
const upper = isolate(n, 6, 6)
return encode(upper) + encode(lower)
}
/**
* Encodes a 18 bit number into three URL safe chars
*
* @name encode3
*
* @param {Number} n a 18 bit number
* @returns {String} the chars
*/
function encode3(n) {
assert(0 <= n && n <= 0x3ffff, ENCODE_OUTOFBOUNDS + n)
// 18 bits -> 3 digits
const lower = isolate(n, 0, 6)
const mid = isolate(n, 6, 6)
const upper = isolate(n, 12, 6)
return encode(upper) + encode(mid) + encode(lower)
}
/**
* Encodes a 24 bit number into four URL safe chars
*
* @name encode4
*
* @param {Number} n a 24 bit number
* @returns {String} the chars
*/
function encode4(n) {
assert(0 <= n && n <= 0xffffff, ENCODE_OUTOFBOUNDS + n)
// 24 bits -> 4 digits
const lower = isolate(n, 0, 6)
const aboveLower = isolate(n, 6, 6)
const belowUpper = isolate(n, 12, 6)
const upper = isolate(n, 18, 6)
return encode(upper) + encode(belowUpper)
+ encode(aboveLower) + encode(lower)
}
/**
* Encodes a 30 bit number into five URL safe chars
*
* @name encode5
*
* @param {Number} n a 30 bit number
* @returns {String} the chars
*/
function encode5(n) {
assert(0 <= n && n <= 0x3fffffff, ENCODE_OUTOFBOUNDS + n)
// 30 bits -> 5 digits
const lower = isolate(n, 0, 6)
const aboveLower = isolate(n, 6, 6)
const mid = isolate(n, 12, 6)
const belowUpper = isolate(n, 18, 6)
const upper = isolate(n, 24, 6)
return encode(upper) + encode(belowUpper)
+ encode(mid) + encode(aboveLower) + encode(lower)
}
/**
* Get a decode function to decode n chars
*
* @name decodeFor
*
* @param {Number} n the number of chars to decode
* @returns {function} the matching decoding function
*/
function decodeFor(n) {
return (
n === 1 ? decode
: n === 2 ? decode2
: n === 3 ? decode3
: n === 4 ? decode4
: decode5
)
}
/**
* Get an encode function to encode an `n * 6` bit number
*
* @name encodeFor
*
* @param {Number} n number of 6 bits to encode
* @returns {function} the matching encoding function
*/
function encodeFor(n) {
return (
n === 1 ? encode
: n === 2 ? encode2
: n === 3 ? encode3
: n === 4 ? encode4
: encode5
)
}
module.exports = {
encode
, encode2
, encode3
, encode4
, encode5
, encodeFor
, decode
, decode2
, decode3
, decode4
, decode5
, decodeFor
, SEPARATOR
, SEPARATORS
}