@@ -22,15 +22,41 @@ const Buffer = require('buffer').Buffer;
2222function ParsedQueryString() {}
2323ParsedQueryString.prototype = Object.create(null);
2424
25-
25+ const unhexTable = [
26+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0 - 15
27+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 16 - 31
28+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 32 - 47
29+ +0, +1, +2, +3, +4, +5, +6, +7, +8, +9, -1, -1, -1, -1, -1, -1, // 48 - 63
30+ -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 64 - 79
31+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 80 - 95
32+ -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 96 - 111
33+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 112 - 127
34+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 128 ...
35+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
39+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
40+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
41+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 // ... 255
42+ ];
2643// a safe fast alternative to decodeURIComponent
2744function unescapeBuffer(s, decodeSpaces) {
2845 var out = Buffer.allocUnsafe(s.length);
2946 var state = 0;
30- var n, m, hexchar;
47+ var n, m, hexchar, c ;
3148
32- for (var inIndex = 0, outIndex = 0; inIndex <= s.length; inIndex++) {
33- var c = inIndex < s.length ? s.charCodeAt(inIndex) : NaN;
49+ for (var inIndex = 0, outIndex = 0; ; inIndex++) {
50+ if (inIndex < s.length) {
51+ c = s.charCodeAt(inIndex);
52+ } else {
53+ if (state > 0) {
54+ out[outIndex++] = 37/*%*/;
55+ if (state === 2)
56+ out[outIndex++] = hexchar;
57+ }
58+ break;
59+ }
3460 switch (state) {
3561 case 0: // Any character
3662 switch (c) {
@@ -51,13 +77,8 @@ function unescapeBuffer(s, decodeSpaces) {
5177
5278 case 1: // First hex digit
5379 hexchar = c;
54- if (c >= 48/*0*/ && c <= 57/*9*/) {
55- n = c - 48/*0*/;
56- } else if (c >= 65/*A*/ && c <= 70/*F*/) {
57- n = c - 65/*A*/ + 10;
58- } else if (c >= 97/*a*/ && c <= 102/*f*/) {
59- n = c - 97/*a*/ + 10;
60- } else {
80+ n = unhexTable[c];
81+ if (!(n >= 0)) {
6182 out[outIndex++] = 37/*%*/;
6283 out[outIndex++] = c;
6384 state = 0;
@@ -68,13 +89,8 @@ function unescapeBuffer(s, decodeSpaces) {
6889
6990 case 2: // Second hex digit
7091 state = 0;
71- if (c >= 48/*0*/ && c <= 57/*9*/) {
72- m = c - 48/*0*/;
73- } else if (c >= 65/*A*/ && c <= 70/*F*/) {
74- m = c - 65/*A*/ + 10;
75- } else if (c >= 97/*a*/ && c <= 102/*f*/) {
76- m = c - 97/*a*/ + 10;
77- } else {
92+ m = unhexTable[c];
93+ if (!(m >= 0)) {
7894 out[outIndex++] = 37/*%*/;
7995 out[outIndex++] = hexchar;
8096 out[outIndex++] = c;
@@ -87,7 +103,7 @@ function unescapeBuffer(s, decodeSpaces) {
87103
88104 // TODO support returning arbitrary buffers.
89105
90- return out.slice(0, outIndex - 1 );
106+ return out.slice(0, outIndex);
91107}
92108
93109
0 commit comments