-
Notifications
You must be signed in to change notification settings - Fork 24
/
codec.js
235 lines (192 loc) · 5.34 KB
/
codec.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
import { Buffer, isBufferLike } from './buffer.js'
import { zeroes } from './table.js'
import {
BYTES_PER_CHARACTER,
UTF8_ENCODING,
BIN_ENCODING,
BIT_COUNT,
BIT_SIZE,
} from './constants.js'
/**
* @typedef {import('./buffer.js').BufferLike} BufferLike
*/
/**
* Pads an input `string` with `multiple` zeroes. If `multiple` is not given,
* then `constants.BIT_COUNT` is used by default.
* @param {string|BufferLike} input
* @param {number} [multiple]
* @return {string}
*/
export function pad (input, multiple = BIT_COUNT) {
const string = Buffer.from(input).toString()
if (multiple === 0) {
return string
} else if (!multiple) {
multiple = BIT_COUNT
}
const missing = string
? string.length % multiple
: 0
if (missing) {
const offset = -1 * ((multiple - missing) + string.length)
return (zeroes + string).slice(offset)
}
return string
}
/**
* Encodes input string or TypedArray as a hex string.
* @param {string|BufferLike} input
* @param {string} [encoding]
* @return {string}
*/
export function hex (input, encoding = UTF8_ENCODING) {
const padding = 2 * BYTES_PER_CHARACTER
if (!encoding) {
encoding = UTF8_ENCODING
}
if (typeof input === 'string') {
return fromString(/** @type {string} */ (input))
}
if (isBufferLike(input)) {
return fromBuffer(Buffer.from(/** @type {BufferLike} */ (input)))
}
throw new TypeError('Expecting a string or buffer as input.')
function fromString (string) {
const chunks = /** @type {string[]} */ ([])
if (UTF8_ENCODING === encoding) {
for (let i = 0; i < /** @type {string} */ (string).length; ++i) {
const chunk = String.fromCharCode(string[i].toString(16))
const padded = pad(chunk, padding)
// @ts-ignore
chunks.unshift(padded)
}
}
if (BIN_ENCODING === encoding) {
string = pad(input, 4)
for (let i = string.length; i >= 4; i -= 4) {
const bits = string.slice(i - 4, i)
const chunk = parseInt(bits, 2).toString(16)
// @ts-ignore
chunks.unshift(chunk)
}
}
return chunks.join('')
}
function fromBuffer (buffer) {
const chunks = []
for (let i = 0; i < buffer.length; ++i) {
const chunk = buffer[i].toString(16)
const padded = pad(chunk, padding)
// @ts-ignore
chunks.unshift(padded)
}
return chunks.join('')
}
}
/**
* Encodes `input` as a binary string. An optional `radix` value that defaults
* to `16` can be used to indicate the chunk type in an `input` string.
* @param {string|BufferLike} input
* @param {number} [radix = 16]
* @return {string}
*/
export function bin (input, radix = 16) {
const chunks = []
if (!radix) {
radix = 16
}
const byteLength = Buffer.byteLength(input)
for (let i = byteLength - 1; i >= 0; --i) {
let chunk
if (isBufferLike(input)) {
chunk = input[i]
}
if (typeof input === 'string') {
chunk = parseInt(input[i], radix)
}
if (Array.isArray(input)) {
chunk = input[i]
if (typeof chunk === 'string') {
chunk = parseInt(chunk, radix)
}
}
if (chunk === undefined) {
throw new TypeError('Unsupported type for chunk in input.')
}
const padded = pad(chunk.toString(2), 4)
// @ts-ignore
chunks.unshift(padded)
}
return chunks.join('')
}
/**
* Encodes input `id` and `data` vector as a `Buffer`
* @param {string|number|BufferLike} id
* @param {string|BufferLike} data
* @return {Buffer}
*/
export function encode (id, data) {
id = typeof id === 'number'
? id
: /** @type {number} */ (parseInt(Buffer.from(id).toString(), 16))
const padding = (BIT_SIZE - 1).toString(16).length
const header = Buffer.concat([
// `BIT_COUNT` is stored as a base36 value, which in this case is the literal '8'
Buffer.from(BIT_COUNT.toString(36).toUpperCase()), // 8
Buffer.from(pad(id.toString(16), padding))
])
if (!isBufferLike(data)) {
data = Buffer.from(data)
}
return Buffer.concat([header, data])
}
/**
* Decodes `input` previously encoded with `encoded` and returns the
* `data` part
* @param {string|BufferLike} input
* @param {string} [encoding = 'utf8']
* @return {Buffer}
*/
export function decode (input, encoding = 'utf8') {
const padding = 2 * BYTES_PER_CHARACTER
const string = pad(Buffer.from(input).toString(encoding), padding)
const offset = padding
const chunks = []
for (let i = 0; i < string.length; i += offset) {
const bits = string.slice(i, i + offset)
const chunk = parseInt(bits, 16)
// @ts-ignore
chunks.unshift(chunk)
}
return Buffer.from(chunks)
}
/**
* Splits an `input` into paged byte chunks where an optional `padding`
* is applied. Chunks are parsed integers of `radix` (default: 2).
* @param {string|BufferLike} input
* @param {number} [padding = 0]
* @param {number} [radix = 2]
* @return {number[]}
*/
export function split (input, padding = 0, radix = 2) {
const chunks = []
const string = pad(Buffer.from(input).toString(), padding)
let i = 0
for (i = string.length; i > BIT_COUNT; i -= BIT_COUNT) {
const bits = string.slice(i - BIT_COUNT, i)
const chunk = parseInt(bits, radix)
// @ts-ignore
chunks.push(chunk)
}
// @ts-ignore
chunks.push(parseInt(string.slice(0, i), radix))
return chunks
}
export default {
bin,
decode,
encode,
hex,
pad,
split
}