@@ -35,6 +35,7 @@ import * as encoding from './encoding.js'
35
35
36
36
const errorUnexpectedEndOfArray = error . create ( 'Unexpected end of array' )
37
37
const errorIntegerOutOfRange = error . create ( 'Integer out of Range' )
38
+ const errorValueTooLong = error . create ( 'Value too long' )
38
39
39
40
/**
40
41
* A Decoder handles the decoding of an Uint8Array.
@@ -113,9 +114,17 @@ export const readUint8Array = (decoder, len) => {
113
114
*
114
115
* @function
115
116
* @param {Decoder } decoder
117
+ * @param {number } [maxLen] Maximum length of the array
116
118
* @return {Uint8Array }
117
119
*/
118
- export const readVarUint8Array = decoder => readUint8Array ( decoder , readVarUint ( decoder ) )
120
+ export const readVarUint8Array = ( decoder , maxLen ) => {
121
+ const len = readVarUint ( decoder )
122
+ if ( maxLen !== undefined && len > maxLen ) {
123
+ throw errorValueTooLong
124
+ }
125
+
126
+ return readUint8Array ( decoder , len )
127
+ }
119
128
120
129
/**
121
130
* Read the rest of the content as an ArrayBuffer
@@ -336,13 +345,16 @@ export const peekVarInt = decoder => {
336
345
*
337
346
* @function
338
347
* @param {Decoder } decoder
348
+ * @param {number } [maxLen] Maximum length of the string in bytes
339
349
* @return {String } The read String.
340
350
*/
341
351
/* c8 ignore start */
342
- export const _readVarStringPolyfill = decoder => {
352
+ export const _readVarStringPolyfill = ( decoder , maxLen ) => {
343
353
let remainingLen = readVarUint ( decoder )
344
354
if ( remainingLen === 0 ) {
345
355
return ''
356
+ } else if ( maxLen !== undefined && remainingLen > maxLen ) {
357
+ throw errorValueTooLong
346
358
} else {
347
359
let encodedString = String . fromCodePoint ( readUint8 ( decoder ) ) // remember to decrease remainingLen
348
360
if ( -- remainingLen < 100 ) { // do not create a Uint8Array for small strings
@@ -368,17 +380,19 @@ export const _readVarStringPolyfill = decoder => {
368
380
/**
369
381
* @function
370
382
* @param {Decoder } decoder
383
+ * @param {number } [maxLen] Maximum length of the string in bytes
371
384
* @return {String } The read String
372
385
*/
373
- export const _readVarStringNative = decoder =>
374
- /** @type any */ ( string . utf8TextDecoder ) . decode ( readVarUint8Array ( decoder ) )
386
+ export const _readVarStringNative = ( decoder , maxLen ) =>
387
+ /** @type any */ ( string . utf8TextDecoder ) . decode ( readVarUint8Array ( decoder , maxLen ) )
375
388
376
389
/**
377
390
* Read string of variable length
378
391
* * varUint is used to store the length of the string
379
392
*
380
393
* @function
381
394
* @param {Decoder } decoder
395
+ * @param {number } [maxLen] Maximum length of the string in bytes
382
396
* @return {String } The read String
383
397
*
384
398
*/
@@ -387,12 +401,16 @@ export const readVarString = string.utf8TextDecoder ? _readVarStringNative : _re
387
401
388
402
/**
389
403
* @param {Decoder } decoder
404
+ * @param {number } [maxLen] Maximum length of the array
390
405
* @return {Uint8Array }
391
406
*/
392
- export const readTerminatedUint8Array = decoder => {
407
+ export const readTerminatedUint8Array = ( decoder , maxLen ) => {
393
408
const encoder = encoding . createEncoder ( )
394
409
let b
395
410
while ( true ) {
411
+ if ( maxLen !== undefined && encoding . length ( encoder ) > maxLen ) {
412
+ throw errorValueTooLong
413
+ }
396
414
b = readUint8 ( decoder )
397
415
if ( b === 0 ) {
398
416
return encoding . toUint8Array ( encoder )
@@ -406,20 +424,22 @@ export const readTerminatedUint8Array = decoder => {
406
424
407
425
/**
408
426
* @param {Decoder } decoder
427
+ * @param {number } [maxLen] Maximum length of the array string in bytes
409
428
* @return {string }
410
429
*/
411
- export const readTerminatedString = decoder => string . decodeUtf8 ( readTerminatedUint8Array ( decoder ) )
430
+ export const readTerminatedString = ( decoder , maxLen ) => string . decodeUtf8 ( readTerminatedUint8Array ( decoder , maxLen ) )
412
431
413
432
/**
414
433
* Look ahead and read varString without incrementing position
415
434
*
416
435
* @function
417
436
* @param {Decoder } decoder
437
+ * @param {number } [maxLen] Maximum length of the array string in bytes
418
438
* @return {string }
419
439
*/
420
- export const peekVarString = decoder => {
440
+ export const peekVarString = ( decoder , maxLen ) => {
421
441
const pos = decoder . pos
422
- const s = readVarString ( decoder )
442
+ const s = readVarString ( decoder , maxLen )
423
443
decoder . pos = pos
424
444
return s
425
445
}
@@ -684,10 +704,11 @@ export class IntDiffOptRleDecoder extends Decoder {
684
704
export class StringDecoder {
685
705
/**
686
706
* @param {Uint8Array } uint8Array
707
+ * @param {number } [maxLen] Maximum length of the string in bytes
687
708
*/
688
- constructor ( uint8Array ) {
709
+ constructor ( uint8Array , maxLen ) {
689
710
this . decoder = new UintOptRleDecoder ( uint8Array )
690
- this . str = readVarString ( this . decoder )
711
+ this . str = readVarString ( this . decoder , maxLen )
691
712
/**
692
713
* @type {number }
693
714
*/
0 commit comments