Skip to content

Commit 3c923f3

Browse files
author
0xricksanchez
committed
Naiive first approach to enforce an ascii mode
1 parent dfa08a7 commit 3c923f3

File tree

1 file changed

+22
-90
lines changed

1 file changed

+22
-90
lines changed

packages/core/FuzzedDataProvider.ts

Lines changed: 22 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,27 @@ export class FuzzedDataProvider {
397397
return result;
398398
}
399399

400+
/**
401+
* Wrapping subtraction function that returns a number
402+
* @param char is a single character from a string represented as a number
403+
* @returns a number n with the constraint 0<= n <= 255
404+
*/
405+
wrappingSub(char: number): number {
406+
return (char - 32 + 256) % 256;
407+
}
408+
409+
/**
410+
* A simple conversion helper that yields the ASCII representaion of a character
411+
* @param char is a single character from a string represented as a number
412+
* @returns a 'string' from a single character that was represented by a number
413+
*/
414+
toAscii(char: number): string {
415+
if (char < 32 || char > 126) {
416+
char = (this.wrappingSub(char) % 95) + 32;
417+
}
418+
return String.fromCharCode(char);
419+
}
420+
400421
/**
401422
* Helper function that converts the given string type into one that only
402423
* contains printable characters.
@@ -407,7 +428,7 @@ export class FuzzedDataProvider {
407428
const chars = [...dirtyStr];
408429
for (let i = 0; i < chars.length; i++) {
409430
const c = chars[i];
410-
chars[i] = new Char(c.charCodeAt(0) as CharNum).toAscii();
431+
chars[i] = this.toAscii(c.charCodeAt(0));
411432
}
412433
return chars.join("");
413434
}
@@ -657,92 +678,3 @@ export class FuzzedDataProvider {
657678
return Math.ceil(numAvailableBytes / numBytesPerElement);
658679
}
659680
}
660-
661-
// prettier-ignore
662-
type CharStr = " " | "!" | '"' | "#" | "$" | "%" | "&" | "'" | "(" | ")" | "*" | "+" | "," | "-" | "." | "/"
663-
| "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
664-
| ":" | ";" | "<" | "=" | ">" | "?" | "@"
665-
| "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"
666-
| "[" | "\\" | "]" | "^" | "_" | "`"
667-
| "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
668-
| "{" | "|" | "}" | "~";
669-
670-
// prettier-ignore
671-
type CharNum = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15
672-
16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31;
673-
32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47;
674-
48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63;
675-
64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79;
676-
80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95;
677-
//prettier-ignore
678-
96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111
679-
//prettier-ignore
680-
112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127
681-
//prettier-ignore
682-
128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143
683-
//prettier-ignore
684-
144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159
685-
//prettier-ignore
686-
160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175
687-
//prettier-ignore
688-
176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191
689-
//prettier-ignore
690-
192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207
691-
//prettier-ignore
692-
208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223
693-
//prettier-ignore
694-
224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239
695-
//prettier-ignore
696-
240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255;
697-
698-
class Char extends Uint8ClampedArray {
699-
constructor(char: CharNum | CharStr | Char) {
700-
super(1);
701-
this.setValue(char);
702-
}
703-
704-
get _value(): CharNum {
705-
return this[0] as CharNum;
706-
}
707-
708-
set _value(char: CharNum | CharStr | Char) {
709-
if (char instanceof Char) {
710-
this[0] = char._value;
711-
} else if (typeof char === "number") {
712-
this[0] = char;
713-
} else if (typeof char === "string") {
714-
this[0] = char.charCodeAt(0);
715-
}
716-
}
717-
718-
setValue(char: CharNum | CharStr | Char) {
719-
this._value = char;
720-
}
721-
722-
toString(): CharStr {
723-
return String.fromCharCode(this._value) as CharStr;
724-
}
725-
726-
toNumber(): CharNum {
727-
return this._value;
728-
}
729-
730-
[Symbol.toPrimitive](hint: string) {
731-
if (hint === "number") {
732-
return this.toNumber();
733-
}
734-
return this.toString();
735-
}
736-
737-
wrappingSub(): this {
738-
this._value = ((this._value - 32 + 256) % 256) as CharNum;
739-
return this;
740-
}
741-
742-
toAscii(): CharStr {
743-
if (this._value < 32 || this._value > 126) {
744-
this._value = ((this.wrappingSub()._value % 95) + 32) as CharNum;
745-
}
746-
return this.toString();
747-
}
748-
}

0 commit comments

Comments
 (0)