-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
Ascii85.js
199 lines (179 loc) · 5.15 KB
/
Ascii85.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
import Encoder from '../Encoder'
import InvalidInputError from '../Error/InvalidInput'
import StringUtil from '../StringUtil'
const meta = {
name: 'ascii85',
title: 'Ascii85',
category: 'Encoding',
type: 'encoder'
}
const variantSpecs = [
{
name: 'original',
label: 'Original',
alphabet: null,
zeroTupleChar: 'z'
},
{
name: 'Z85',
label: 'Z85 (ZeroMQ)',
alphabet:
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFG' +
'HIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#',
zeroTupleChar: null
}
]
/**
* Encoder brick for Ascii85/Base85 encoding and decoding
*/
export default class Ascii85Encoder extends Encoder {
/**
* Returns brick meta.
* @return {object}
*/
static getMeta () {
return meta
}
/**
* Constructor
*/
constructor () {
super()
this.addSetting({
name: 'variant',
type: 'enum',
label: 'Variant',
value: 'original',
elements: variantSpecs.map(variant => variant.name),
labels: variantSpecs.map(variant => variant.label),
randomizable: false
})
}
/**
* Performs encode on given content.
* @protected
* @param {Chain} content
* @return {number[]|string|Uint8Array|Chain|Promise} Encoded content
*/
performEncode (content) {
const bytes = content.getBytes()
const variant = variantSpecs.find(variant =>
variant.name === this.getSettingValue('variant'))
const n = bytes.length
// Encode each tuple of 4 bytes
let string = ''
let digits, j, tuple
for (let i = 0; i < n; i += 4) {
// Read 32-bit unsigned integer from bytes following the
// big-endian convention (most significant byte first)
tuple = (
((bytes[i]) << 24) +
((bytes[i + 1] || 0) << 16) +
((bytes[i + 2] || 0) << 8) +
((bytes[i + 3] || 0))
) >>> 0
if (variant.zeroTupleChar === null || tuple > 0) {
// Calculate 5 digits by repeatedly dividing
// by 85 and taking the remainder
digits = []
for (j = 0; j < 5; j++) {
digits.push(tuple % 85)
tuple = Math.floor(tuple / 85)
}
// Take most significant digit first
digits = digits.reverse()
if (n < i + 4) {
// Omit final characters added due to bytes of padding
digits.splice(n - (i + 4), 4)
}
// Convert digits to characters and glue them together
string += digits.map(digit =>
variant.alphabet === null
? String.fromCharCode(digit + 33)
: variant.alphabet[digit]
).join('')
} else {
// An all-zero tuple is encoded as a single character
string += variant.zeroTupleChar
}
}
return string
}
/**
* Triggered before performing decode on given content.
* @protected
* @param {Chain} content
* @return {number[]|string|Uint8Array|Chain|Promise} Filtered content
*/
willDecode (content) {
// Check for <~ ~> wrappers often used to wrap ascii85 encoded data
const wrapperMatches = content.getString().match(/<~(.+?)~>/)
if (wrapperMatches !== null) {
// Decode wrapped data only
return wrapperMatches[1]
}
return content
}
/**
* Performs decode on given content.
* @protected
* @param {Chain} content
* @return {number[]|string|Uint8Array|Chain|Promise} Decoded content
*/
performDecode (content) {
const string = StringUtil.removeWhitespaces(content.getString())
const variant = variantSpecs.find(variant =>
variant.name === this.getSettingValue('variant'))
const n = string.length
// Decode each tuple of 5 characters
const bytes = []
let i = 0
let digits, tuple, tupleBytes
while (i < n) {
if (string[i] === variant.zeroTupleChar) {
// A single character encodes an all-zero tuple
bytes.push(0, 0, 0, 0)
i++
} else {
// Retrieve radix-85 digits of tuple
digits = string
.substr(i, 5)
.split('')
.map((character, index) => {
const digit =
variant.alphabet === null
? character.charCodeAt(0) - 33
: variant.alphabet.indexOf(character)
if (digit < 0 || digit > 84) {
throw new InvalidInputError(
`Invalid character '${character}' at index ${index}`)
}
return digit
})
// Create 32-bit binary number from digits and handle padding
// tuple = a * 85^4 + b * 85^3 + c * 85^2 + d * 85 + e
tuple =
digits[0] * 52200625 +
digits[1] * 614125 +
(i + 2 < n ? digits[2] : 84) * 7225 +
(i + 3 < n ? digits[3] : 84) * 85 +
(i + 4 < n ? digits[4] : 84)
// Get bytes from tuple
tupleBytes = [
(tuple >> 24) & 0xff,
(tuple >> 16) & 0xff,
(tuple >> 8) & 0xff,
tuple & 0xff
]
// Remove bytes of padding
if (n < i + 5) {
tupleBytes.splice(n - (i + 5), 5)
}
// Append bytes to result
bytes.push.apply(bytes, tupleBytes)
i += 5
}
}
return new Uint8Array(bytes)
}
}