-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
Bootstring.js
556 lines (493 loc) · 14.5 KB
/
Bootstring.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
import Encoder from '../Encoder'
import InvalidInputError from '../Error/InvalidInput'
import MathUtil from '../MathUtil'
const meta = {
name: 'bootstring',
title: 'Bootstring',
category: 'Encoding',
type: 'encoder'
}
const minBase = 2
const integerOverflowMessage =
'Integer overflow: Input needs wider integers to process'
/**
* Encoder brick for Bootstring encoding and decoding following RFC 3492.
*/
export default class BootstringEncoder extends Encoder {
/**
* Returns brick meta.
* @return {object}
*/
static getMeta () {
return meta
}
/**
* Constructor
*/
constructor () {
super()
this.addSettings([
{
name: 'basicRangeStart',
label: 'Basic Start',
type: 'number',
value: 0,
integer: true,
rotate: false,
min: 0,
max: 127 - minBase,
width: 6,
randomizable: false
},
{
name: 'basicRangeEnd',
label: 'Basic End',
type: 'number',
value: 127,
integer: true,
rotate: false,
min: minBase,
// Max Unicode code point value
max: 0x10FFFF,
width: 6,
randomizable: false
},
{
name: 'digitMapping',
type: 'text',
value: 'abcdefghijklmnopqrstuvwxyz0123456789',
uniqueChars: true,
minLength: 2,
caseSensitivity: false,
validateValue: this.validateDigitMappingValue.bind(this),
randomizable: false
},
{
name: 'delimiter',
type: 'text',
value: '-',
width: 6,
minLength: 1,
maxLength: 1,
caseSensitivity: false,
validateValue: this.validateDelimiterValue.bind(this),
randomizable: false
},
{
name: 'caseSensitivity',
type: 'boolean',
value: false,
width: 6,
randomizable: false
},
{
name: 'initialBias',
type: 'number',
value: 72,
integer: true,
width: 6,
validateValue: this.validateInitialBiasValue.bind(this),
randomizable: false
},
{
name: 'initialN',
type: 'number',
value: 128,
integer: true,
min: 0,
width: 6,
randomizable: false
},
{
name: 'tmin',
type: 'number',
value: 1,
integer: true,
rotate: false,
min: 0,
max: 26,
width: 6,
randomizable: false
},
{
name: 'tmax',
type: 'number',
value: 26,
integer: true,
rotate: false,
min: 0,
max: 35,
width: 6,
randomizable: false
},
{
name: 'skew',
type: 'number',
value: 38,
integer: true,
min: 1,
width: 6,
randomizable: false
},
{
name: 'damp',
type: 'number',
value: 700,
integer: true,
min: 2,
width: 6,
randomizable: false
}
])
}
/**
* Performs encode on given content following section 6.3 of RFC 3492.
* @protected
* @param {Chain} content
* @return {number[]|string|Uint8Array|Chain|Promise} Encoded content
*/
performEncode (content) {
const {
initialBias,
initialN,
tmin,
tmax,
caseSensitivity
} = this.getSettingValues()
const delimiter = this.getSettingValue('delimiter').getCodePointAt(0)
const base = this.getSettingValue('digitMapping').getLength()
// Prepare content
if (!caseSensitivity) {
content = content.toLowerCase()
}
// Initialize the state
const input = content.getCodePoints()
const inputLength = input.length
let n = initialN
let bias = initialBias
let delta = 0
// Copy basic code points in the input to the output in order
const output = []
for (let i = 0; i < input.length; i++) {
if (this._isBasic(input[i])) {
output.push(input[i])
} else if (input[i] < n) {
throw new InvalidInputError(
`Unexpected code point at index ${i}, consider changing initial n ` +
`to include this code point`)
}
}
let h = output.length
let b = output.length
if (b > 0) {
output.push(delimiter)
}
let m, q, k, t, c
while (h < inputLength) {
// Find the next larger non-basic code point >= n
m = Number.MAX_SAFE_INTEGER
for (c of input) {
if (c >= n && c < m && !this._isBasic(c)) {
m = c
}
}
// Increase delta enough to advance the decoder's <n,i> state to <m,0>,
// but guard against overflow
if (m - n > MathUtil.div(Number.MAX_SAFE_INTEGER - delta, h + 1)) {
throw new InvalidInputError(integerOverflowMessage)
}
delta += (m - n) * (h + 1)
n = m
for (c of input) {
if (c < n || this._isBasic(c)) {
// Overflow detection
if (++delta > Number.MAX_SAFE_INTEGER) {
throw new InvalidInputError(integerOverflowMessage)
}
}
if (c === n) {
q = delta
for (k = base; true; k += base) {
t = this._calcT(k, bias, tmin, tmax)
if (q < t) {
break
}
output.push(this._basicCodePointFromDigit(
t + MathUtil.mod(q - t, base - t)))
q = MathUtil.div(q - t, base - t)
}
output.push(this._basicCodePointFromDigit(q))
bias = this._adaptBias(delta, h + 1, h === b)
delta = 0
h++
}
}
delta++
n++
}
return output
}
/**
* Performs decode on given content following section 6.2 of RFC 3492.
* @protected
* @param {Chain} content
* @return {number[]|string|Uint8Array|Chain|Promise} Decoded content
*/
performDecode (content) {
const {
initialBias,
initialN,
tmin,
tmax,
caseSensitivity
} = this.getSettingValues()
const delimiter = this.getSettingValue('delimiter').getCodePointAt(0)
const base = this.getSettingValue('digitMapping').getLength()
// Prepare case insensitive content
if (!caseSensitivity) {
content = content.toLowerCase()
}
// Initialize the state
const input = content.getCodePoints()
const inputLength = input.length
let n = initialN
let bias = initialBias
// Consume all code points before the last delimiter (if there is one)
// and copy them to output, fail on any non-basic code point
const basicLength = Math.max(input.lastIndexOf(delimiter), 0)
const output = []
for (let j = 0; j < basicLength; j++) {
if (!this._isBasic(input[j])) {
throw new InvalidInputError(
`Found unexpected non-basic code point at ${j}`)
}
output.push(input[j])
}
let i = 0
let j = basicLength > 0 ? basicLength + 1 : 0
let oldi, w, k, digit, t
while (j < inputLength) {
oldi = i
w = 1
for (k = base; true; k += base) {
// Fail if there is no code point to consume next
if (j >= inputLength) {
throw new InvalidInputError('The input ends unexpectedly')
}
digit = this._digitFromBasicCodePoint(input[j++])
if (digit === -1) {
throw new InvalidInputError(
`Found unexpected non-basic code point at ${j - 1}`)
}
// Overflow detection
if (digit > MathUtil.div(Number.MAX_SAFE_INTEGER - i, w)) {
throw new InvalidInputError(integerOverflowMessage)
}
i += digit * w
t = this._calcT(k, bias, tmin, tmax)
if (digit < t) {
break
}
// Overflow detection
if (w > MathUtil.div(Number.MAX_SAFE_INTEGER, base - t)) {
throw new InvalidInputError(integerOverflowMessage)
}
w *= base - t
}
bias = this._adaptBias(i - oldi, output.length + 1, oldi === 0)
// Overflow detection
if (MathUtil.div(i, output.length + 1) > Number.MAX_SAFE_INTEGER - n) {
throw new InvalidInputError(integerOverflowMessage)
}
n += MathUtil.div(i, output.length + 1)
i = MathUtil.mod(i, output.length + 1)
// If n is a basic code point then fail
if (this._isBasic(n)) {
throw new InvalidInputError('Unexpectedly unwrapped basic code point')
}
output.splice(i, 0, n)
i++
}
return output
}
/**
* Bias adaptation function following section 6.1 of RFC 3492.
* After each delta is encoded or decoded, bias is set for the next delta.
* @param {number} delta
* @param {number} numPoints
* @param {boolean} firstTime
* @return {number}
*/
_adaptBias (delta, numPoints, firstTime) {
const { digitMapping, tmin, tmax, skew } = this.getSettingValues()
const base = digitMapping.getLength()
if (firstTime) {
const damp = this.getSettingValue('damp')
delta = MathUtil.div(delta, damp)
} else {
delta = MathUtil.div(delta, 2)
}
delta += MathUtil.div(delta, numPoints)
let k = 0
while (delta > MathUtil.div((base - tmin) * tmax, 2)) {
delta = MathUtil.div(delta, base - tmin)
k += base
}
return k + MathUtil.div((base - tmin + 1) * delta, delta + skew)
}
/**
* Chooses the next T value based on given parameters.
* @param {number} k
* @param {number} bias
* @param {number} tmin
* @param {number} tmax
* @return {number}
*/
_calcT (k, bias, tmin, tmax) {
if (k <= bias + tmin) {
return tmin
} else if (k >= bias + tmax) {
return tmax
}
return k - bias
}
/**
* Checks wether the given code point is considered to be basic.
* @param {number} codePoint Unicode code point
* @return {boolean} True, if the given code point is basic
*/
_isBasic (codePoint) {
const { basicRangeStart, basicRangeEnd } = this.getSettingValues()
return codePoint >= basicRangeStart && codePoint <= basicRangeEnd
}
/**
* Translates a Unicode code point into a basic code point digit.
* @param {number} codePoint Unicode code point
* @return {number} Basic code point digit or -1, if there is no digit
* assigned to the given Unicode code point
*/
_digitFromBasicCodePoint (codePoint) {
return this.getSettingValue('digitMapping').indexOfCodePoint(codePoint)
}
/**
* Translates a basic code point digit into a Unicode code point.
* @param {number} digit Basic code point digit
* @return {number} Unicode code point
*/
_basicCodePointFromDigit (digit) {
return this.getSettingValue('digitMapping').getCodePointAt(digit)
}
/**
* Triggered when a setting field has changed.
* @protected
* @param {Field} setting Sender setting field
* @param {mixed} value New field value
*/
settingValueDidChange (setting, value) {
switch (setting.getName()) {
case 'basicRangeStart':
this.getSetting('basicRangeEnd').setMin(setting.getValue() + minBase)
this.getSetting('digitMapping').revalidateValue()
this.getSetting('delimiter').revalidateValue()
break
case 'basicRangeEnd':
this.getSetting('basicRangeStart').setMax(setting.getValue() - minBase)
this.getSetting('digitMapping').revalidateValue()
this.getSetting('delimiter').revalidateValue()
break
case 'digitMapping':
// The base depends on the digit mapping
const base = setting.getValue().getLength()
this.getSetting('tmax').setMax(base - 1)
this.getSetting('delimiter').revalidateValue()
this.getSetting('initialBias').revalidateValue()
break
case 'tmin':
this.getSetting('initialBias').revalidateValue()
break
case 'tmax':
this.getSetting('tmin').setMax(setting.getValue())
break
case 'caseSensitivity':
this.getSetting('digitMapping').setCaseSensitivity(value)
this.getSetting('delimiter').setCaseSensitivity(value)
break
}
}
/**
* Validates the digit mapping value.
* @param {Chain} value Value to be validated
* @param {Field} setting Sender field
* @return {boolean|object}
*/
validateDigitMappingValue (value, setting) {
// Can't validate digit mapping without valid basic range
if (!this.isSettingValid('basicRangeStart', 'basicRangeEnd')) {
return false
}
const basicRangeStart = this.getSettingValue('basicRangeStart')
const basicRangeEnd = this.getSettingValue('basicRangeEnd')
const invalidIndex = value.getCodePoints().findIndex(codePoint =>
codePoint < basicRangeStart || codePoint > basicRangeEnd)
if (invalidIndex !== -1) {
return {
key: 'bootstringDigitMappingInvalid',
message:
`Character at index ${invalidIndex} needs to be part of the given ` +
`basic code point range`
}
}
return true
}
/**
* Validates the delimiter value.
* @param {Chain} value Delimiter value
* @param {Field} setting Sender field
* @return {boolean|object}
*/
validateDelimiterValue (value, setting) {
// The delimiter value depends on the basic range and the digit mapping
if (!this.isSettingValid('basicRangeStart', 'basicRangeEnd', 'digitMapping')) {
return false
}
const { basicRangeStart, basicRangeEnd, digitMapping } =
this.getSettingValues()
const delimiter = value.getCodePointAt(0)
// Delimiter needs to be in the basic code point range
if (delimiter < basicRangeStart ||
delimiter > basicRangeEnd ||
digitMapping.indexOfCodePoint(delimiter) !== -1) {
return {
key: 'bootstringDelimiterInvalid',
message:
`The value must be part of the basic code point range while ` +
`not having a digit mapped to it`
}
}
return true
}
/**
* Validates initial bias setting value according to section 4 of RFC 3492
* such that `initial_bias mod base <= base - tmin`.
* @param {number} value Initial bias to be validated
* @param {Field} setting Sender field
* @return {boolean|object}
*/
validateInitialBiasValue (value, setting) {
// Can't validate initial bias without valid base and tmin values
if (!this.isSettingValid('digitMapping', 'tmin')) {
return false
}
const base = this.getSettingValue('digitMapping').getLength()
const tmin = this.getSettingValue('tmin')
if (MathUtil.mod(value, base) > base - tmin) {
return {
key: 'bootstringInitialBiasInvalid',
message:
'The value must be chosen such that ' +
'initial_bias mod base <= base - tmin'
}
}
return true
}
}