24
24
export class FuzzedDataProvider {
25
25
private readonly data : Buffer ;
26
26
private dataPtr = - 1 ;
27
- private readonly enforcePrintable : boolean = false ;
28
27
/** The number of remaining bytes that can be consumed from the fuzzer input data. */
29
28
_remainingBytes = 0 ;
30
29
@@ -35,12 +34,9 @@ export class FuzzedDataProvider {
35
34
36
35
/**
37
36
* @param data - a buffer containing the fuzzer input
38
- * @param printable - a boolean, which defaults to false that indicates whether consumed strings
39
- * should be forced to contain only valid printable characters
40
37
*/
41
- constructor ( data : Buffer , enforcePrintable : boolean | undefined = false ) {
38
+ constructor ( data : Buffer ) {
42
39
this . data = data ;
43
- this . enforcePrintable = enforcePrintable ;
44
40
if ( data . length > 0 ) {
45
41
this . dataPtr = 0 ;
46
42
this . _remainingBytes = data . length ;
@@ -376,17 +372,20 @@ export class FuzzedDataProvider {
376
372
* is not sufficiently long.
377
373
* @param maxLength the maximum length of the string
378
374
* @param encoding the encoding of the string
375
+ * @param printable - a boolean, which defaults to false that indicates whether consumed strings
376
+ * should be forced to contain only valid printable characters
379
377
* @returns a `string` of length between 0 and `maxLength` (inclusive)
380
378
*/
381
379
consumeString (
382
380
maxLength : number ,
383
- encoding : BufferEncoding | undefined = "ascii"
381
+ encoding : BufferEncoding | undefined = "ascii" ,
382
+ printable : boolean | undefined = false
384
383
) : string {
385
384
if ( maxLength < 0 ) throw new Error ( "maxLength must be non-negative" ) ;
386
385
let result ;
387
386
const arrayLength = Math . min ( maxLength , this . _remainingBytes ) ;
388
387
389
- if ( this . enforcePrintable ) {
388
+ if ( printable ) {
390
389
result = this . bufToPrintableString (
391
390
this . data ,
392
391
this . dataPtr ,
@@ -438,32 +437,35 @@ export class FuzzedDataProvider {
438
437
* high enough. Limitations are that there are collisions such that, e.g.:
439
438
* the conversion of values: `0x1`, `0x42`, and `0xa2` all yield the character `B`.
440
439
* @param buf - Buffer that contains arbitrary values
441
- * @param lb - lower bound at which processing of the provided `Buffer` shall begin
442
- * @param ub - upper bound, analogous to the lower bound
440
+ * @param min - lower bound at which processing of the provided `Buffer` shall begin
441
+ * @param max - upper bound, analogous to the lower bound
443
442
* @param encoding - a valid `BufferEncoding`.
444
443
* @returns a string that was sanitized and only contains printable characters
445
444
*/
446
445
bufToPrintableString (
447
446
buf : Buffer ,
448
- lb : number ,
449
- ub : number ,
447
+ min : number ,
448
+ max : number ,
450
449
encoding : BufferEncoding
451
450
) : string {
452
- for ( let i = lb ; i < ub ; i ++ ) {
451
+ for ( let i = min ; i < max ; i ++ ) {
453
452
buf [ i ] = this . toAscii ( buf [ i ] ) ;
454
453
}
455
- return buf . subarray ( lb , ub ) . toString ( encoding ) ;
454
+ return buf . subarray ( min , max ) . toString ( encoding ) ;
456
455
}
457
456
458
457
/**
459
458
* Consumes the remaining bytes of the fuzzer input as a string.
460
459
* @param encoding - the encoding of the string
460
+ * @param printable - a boolean, which defaults to false that indicates whether consumed strings
461
+ * should be forced to contain only valid printable characters
461
462
* @returns a string constructed from the remaining bytes of the fuzzer input using the given encoding
462
463
*/
463
464
consumeRemainingAsString (
464
- encoding : BufferEncoding | undefined = "ascii"
465
+ encoding : BufferEncoding | undefined = "ascii" ,
466
+ printable : boolean | undefined = false
465
467
) : string {
466
- return this . consumeString ( this . _remainingBytes , encoding ) ;
468
+ return this . consumeString ( this . _remainingBytes , encoding , printable ) ;
467
469
}
468
470
469
471
/**
@@ -473,16 +475,19 @@ export class FuzzedDataProvider {
473
475
* @param maxArrayLength the maximum length of the array
474
476
* @param maxStringLength the maximum length of the strings
475
477
* @param encoding the encoding of the strings
478
+ * @param printable - a boolean, which defaults to false that indicates whether consumed strings
479
+ * should be forced to contain only valid printable characters
476
480
* @returns an array containing strings constructed from the remaining bytes of the fuzzer input using the given encoding
477
481
*/
478
482
consumeStringArray (
479
483
maxArrayLength : number ,
480
484
maxStringLength : number ,
481
- encoding : BufferEncoding | undefined = "ascii"
485
+ encoding : BufferEncoding | undefined = "ascii" ,
486
+ printable : boolean | undefined = false
482
487
) {
483
488
const strs = [ ] ;
484
489
while ( strs . length < maxArrayLength && this . remainingBytes > 0 ) {
485
- const str = this . consumeString ( maxStringLength , encoding ) ;
490
+ const str = this . consumeString ( maxStringLength , encoding , printable ) ;
486
491
if ( str ) {
487
492
strs . push ( str ) ;
488
493
}
0 commit comments