Skip to content

Commit 36ca5c4

Browse files
committed
better handling of the case where we are not unescaping things on purpose
1 parent b9d1cc6 commit 36ca5c4

File tree

1 file changed

+46
-34
lines changed

1 file changed

+46
-34
lines changed

org/w3c/css/util/UnescapeFilterReader.java

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public int read()
3636
if (c != '\\') {
3737
return c;
3838
}
39-
mark(6);
39+
in.mark(6);
4040
int val = 0;
4141
for (int i = 0; i < 6; i++) {
4242
esc = in.read();
@@ -50,40 +50,42 @@ public int read()
5050
val = (val << 4) + (esc - 87);
5151
} else if (esc == 10 || esc == 9 || esc == 32) { // CSS whitespace.
5252
// U+000A LINE FEED, U+0009 CHARACTER TABULATION, or U+0020 SPACE.
53-
if ((val > 96 && val < 124) || (val > 64 && val < 91) || (val > 47 && val < 58)) {
53+
if ((val > 96 && val < 124) || (val > 64 && val < 91)) {
5454
return val;
5555
}
5656
} else {
57-
if ((val > 96 && val < 124) || (val > 64 && val < 91) || (val > 47 && val < 58)) {
57+
if ((val > 96 && val < 124) || (val > 64 && val < 91)) {
5858
//we must unread 1
59-
reset();
59+
in.reset();
6060
i++;
6161
for (int j = 0; j < i; j++) {
6262
in.read();
6363
}
6464
return val;
6565
}
66-
reset();
66+
in.reset();
6767
return c;
6868
}
6969
}
7070
// we read up to 6 char test value first
71-
if ((val <= 96 || val >= 124) && (val <= 64 || val >= 91) && (val <= 47 || val >= 58)) {
72-
reset();
71+
if ((val <= 96 || val >= 124) && (val <= 64 || val >= 91)) {
72+
in.reset();
7373
return c;
7474
}
7575
mark(1);
7676
c = in.read();
7777
// not a CSS WHITESPACE
7878
if (c != 10 && c != 9 && c != 32) {
79-
reset();
79+
in.reset();
8080
}
8181
return val;
8282
}
8383

8484
@Override
8585
public int read(char[] cbuf, int off, int len) throws IOException {
8686
int i, j, k, l, cki;
87+
boolean ignoreEscape = false;
88+
8789
char[] chars = new char[len];
8890
in.mark(len);
8991
l = super.read(chars, 0, len);
@@ -108,6 +110,7 @@ public int read(char[] cbuf, int off, int len) throws IOException {
108110
if (chars[i] == '\\') {
109111
int val = 0;
110112
boolean escaped = false;
113+
ignoreEscape = false;
111114
for (k = 1; k < 7 && k + i < l; k++) {
112115
cki = chars[k + i];
113116
// 0-9
@@ -120,55 +123,64 @@ public int read(char[] cbuf, int off, int len) throws IOException {
120123
val = (val << 4) + (cki - 87);
121124
} else if (cki == 10 || cki == 9 || cki == 32) { // CSS whitespace.
122125
// U+000A LINE FEED, U+0009 CHARACTER TABULATION, or U+0020 SPACE.
123-
if ((val > 96 && val < 124) || (val > 64 && val < 91) || (val > 47 && val < 58)) {
126+
if ((val > 96 && val < 124) || (val > 64 && val < 91)) {
124127
chars[j++] = (char) val;
125128
escaped = true;
126129
i += k;
127-
break;
128130
}
131+
escaped = true;
132+
ignoreEscape = true;
133+
break;
129134
} else {
130135
if (val == 0) {
131136
if ((cki > 96 && cki < 124) || (cki > 64 && cki < 91)) {
132137
// so we found a regular char, just remove the escaping
133138
break;
134139
}
135140
}
136-
if ((val > 96 && val < 124) || (val > 64 && val < 91) || (val > 47 && val < 58)) {
141+
if ((val > 96 && val < 124) || (val > 64 && val < 91)) {
137142
chars[j++] = (char) val;
138143
escaped = true;
139144
i += k - 1;
140145
break;
146+
} else {
147+
ignoreEscape = true;
141148
}
142149
}
143150
}
144-
if (k == 7 && !escaped) {
145-
if ((val > 96 && val < 124) || (val > 64 && val < 91) || (val > 47 && val < 58)) {
146-
chars[j++] = (char) val;
147-
escaped = true;
148-
i += k - 1;
149-
if (i + 1 < l) {
150-
cki = chars[i + 1];
151-
// skip extra space
152-
if (cki == 10 || cki == 9 || cki == 32) {
153-
i++;
151+
if (!ignoreEscape) {
152+
if (k == 7 && !escaped) {
153+
if ((val > 96 && val < 124) || (val > 64 && val < 91)) {
154+
chars[j++] = (char) val;
155+
escaped = true;
156+
i += k - 1;
157+
if (i + 1 < l) {
158+
cki = chars[i + 1];
159+
// skip extra space
160+
if (cki == 10 || cki == 9 || cki == 32) {
161+
i++;
162+
}
154163
}
155-
}
156-
} else {
157-
// do nothing
158-
chars[j++] = chars[i];
159-
}
160-
} else {
161-
// we reached the end, unescaping didn't happen let's stop here
162-
// unless we are the last
163-
if (!escaped) {
164-
if (j != 0) {
165-
in.reset();
166-
in.skip(i);
167-
break;
168164
} else {
165+
// do nothing
166+
ignoreEscape = true;
169167
chars[j++] = chars[i];
170168
}
169+
} else {
170+
// we reached the end, unescaping didn't happen let's stop here
171+
// unless we are the last
172+
if (!escaped) {
173+
if (j != 0) {
174+
in.reset();
175+
in.skip(i);
176+
break;
177+
} else {
178+
chars[j++] = chars[i];
179+
}
180+
}
171181
}
182+
} else {
183+
chars[j++] = chars[i];
172184
}
173185
} else {
174186
chars[j++] = chars[i];

0 commit comments

Comments
 (0)