LEFT | RIGHT |
1 #include <climits> | 1 #include <climits> |
2 | 2 |
3 #include <emscripten.h> | 3 #include <emscripten.h> |
4 | 4 |
5 #include "RegExpFilter.h" | 5 #include "RegExpFilter.h" |
6 #include "StringScanner.h" | 6 #include "StringScanner.h" |
7 #include "StringMap.h" | 7 #include "StringMap.h" |
8 | 8 |
9 namespace | 9 namespace |
10 { | 10 { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 {u"media"_str, TYPE_MEDIA}, | 44 {u"media"_str, TYPE_MEDIA}, |
45 {u"font"_str, TYPE_FONT}, | 45 {u"font"_str, TYPE_FONT}, |
46 {u"background"_str, TYPE_IMAGE}, // Backwards compat | 46 {u"background"_str, TYPE_IMAGE}, // Backwards compat |
47 | 47 |
48 {u"popup"_str, TYPE_POPUP}, | 48 {u"popup"_str, TYPE_POPUP}, |
49 {u"genericblock"_str, TYPE_GENERICBLOCK}, | 49 {u"genericblock"_str, TYPE_GENERICBLOCK}, |
50 {u"generichide"_str, TYPE_GENERICHIDE}, | 50 {u"generichide"_str, TYPE_GENERICHIDE}, |
51 {u"elemhide"_str, TYPE_ELEMHIDE}, | 51 {u"elemhide"_str, TYPE_ELEMHIDE}, |
52 }; | 52 }; |
53 | 53 |
54 int defaultTypeMask = INT_MAX & ~(TYPE_DOCUMENT | TYPE_ELEMHIDE | TYPE_POPUP | | 54 const int defaultTypeMask = INT_MAX & ~(TYPE_DOCUMENT | TYPE_ELEMHIDE | |
55 TYPE_GENERICBLOCK | TYPE_GENERICHIDE); | 55 TYPE_POPUP | TYPE_GENERICBLOCK | TYPE_GENERICHIDE); |
56 | 56 |
57 int GenerateRegExp(const String& regexp, bool matchCase) | 57 int GenerateRegExp(const String& regexp, bool matchCase) |
58 { | 58 { |
59 return EM_ASM_INT(return regexps.create($0, $1), ®exp, matchCase); | 59 return EM_ASM_INT(return regexps.create($0, $1), ®exp, matchCase); |
60 } | 60 } |
61 | 61 |
62 void NormalizeWhitespace(String& text) | 62 void NormalizeWhitespace(DependentString& text) |
63 { | 63 { |
64 // We want to remove all spaces but bail out early in the common scenario | 64 // We want to remove all spaces but bail out early in the common scenario |
65 // that the string contains no spaces. | 65 // that the string contains no spaces. |
66 | 66 |
67 // Look for the first space | 67 // Look for the first space |
68 String::size_type len = text.length(); | 68 String::size_type len = text.length(); |
69 String::size_type pos; | 69 String::size_type pos; |
70 for (pos = 0; pos < len; pos++) | 70 for (pos = 0; pos < len; pos++) |
71 if (text[pos] == ' ') | 71 if (text[pos] == ' ') |
72 break; | 72 break; |
73 | 73 |
74 if (pos >= len) | 74 if (pos >= len) |
75 return; | 75 return; |
76 | 76 |
77 // Found spaces, move characters to remove them | 77 // Found spaces, move characters to remove them |
78 String::size_type delta = 1; | 78 String::size_type delta = 1; |
79 for (pos = pos + 1; pos < len; pos++) | 79 for (pos = pos + 1; pos < len; pos++) |
80 { | 80 { |
81 if (text[pos] == ' ') | 81 if (text[pos] == ' ') |
82 delta++; | 82 delta++; |
83 else | 83 else |
84 text[pos - delta] = text[pos]; | 84 text[pos - delta] = text[pos]; |
85 } | 85 } |
86 text.reset(text, 0, len - delta); | 86 text.reset(text, 0, len - delta); |
87 } | 87 } |
88 } | 88 |
89 | 89 void ParseOption(String& text, DependentString& error, RegExpFilterData& data, |
90 RegExpFilter::RegExpFilter(const String& text, const RegExpFilterData& data) | 90 int optionStart, int optionEnd, int valueStart, int valueEnd) |
91 : ActiveFilter(text, true), RegExpFilterData(data) | 91 { |
| 92 if (optionEnd <= optionStart) |
| 93 return; |
| 94 |
| 95 bool reverse = false; |
| 96 if (text[optionStart] == u'~') |
| 97 { |
| 98 reverse = true; |
| 99 optionStart++; |
| 100 } |
| 101 |
| 102 DependentString name(text, optionStart, optionEnd - optionStart); |
| 103 for (size_t i = 0; i < name.length(); ++i) |
| 104 { |
| 105 char16_t currChar = name[i]; |
| 106 if (currChar >= u'A' && currChar <= u'Z') |
| 107 name[i] = currChar + u'a' - u'A'; |
| 108 else if (currChar == u'_') |
| 109 name[i] = u'-'; |
| 110 } |
| 111 |
| 112 auto it = typeMap.find(name); |
| 113 if (it) |
| 114 { |
| 115 if (data.mContentType < 0) |
| 116 data.mContentType = reverse ? defaultTypeMask : 0; |
| 117 if (reverse) |
| 118 data.mContentType &= ~it->second; |
| 119 else |
| 120 data.mContentType |= it->second; |
| 121 } |
| 122 else if (name.equals(u"domain"_str)) |
| 123 { |
| 124 if (valueStart >= 0 && valueEnd > valueStart) |
| 125 { |
| 126 data.mDomainsStart = valueStart; |
| 127 data.mDomainsEnd = valueEnd; |
| 128 DependentString(text, valueStart, valueEnd - valueStart).toLower(); |
| 129 } |
| 130 } |
| 131 else if (name.equals(u"sitekey"_str)) |
| 132 { |
| 133 if (valueStart >= 0 && valueEnd > valueStart) |
| 134 { |
| 135 data.mSitekeysStart = valueStart; |
| 136 data.mSitekeysEnd = valueEnd; |
| 137 } |
| 138 } |
| 139 else if (name.equals(u"match-case"_str)) |
| 140 data.mMatchCase = !reverse; |
| 141 else if (name.equals(u"third-party"_str)) |
| 142 data.mThirdParty = reverse ? TrippleState::NO : TrippleState::YES; |
| 143 else if (name.equals(u"collapse"_str)) |
| 144 data.mCollapse = reverse ? TrippleState::NO : TrippleState::YES; |
| 145 else |
| 146 error.reset(u"filter_unknown_option"_str); |
| 147 } |
| 148 |
| 149 void ParseOptions(String& text, DependentString& error, RegExpFilterData& data
, |
| 150 String::size_type optionsStart) |
| 151 { |
| 152 data.mMatchCase = false; |
| 153 data.mThirdParty = TrippleState::ANY; |
| 154 data.mCollapse = TrippleState::ANY; |
| 155 data.mDomainsStart = String::npos; |
| 156 data.mSitekeysStart = String::npos; |
| 157 if (optionsStart >= text.length()) |
| 158 { |
| 159 data.mContentType = defaultTypeMask; |
| 160 return; |
| 161 } |
| 162 |
| 163 data.mContentType = -1; |
| 164 |
| 165 int optionStart = data.mPatternEnd + 1; |
| 166 int optionEnd = -1; |
| 167 int valueStart = -1; |
| 168 |
| 169 StringScanner scanner(text, optionStart, u','); |
| 170 bool done = false; |
| 171 while (!done) |
| 172 { |
| 173 done = scanner.done(); |
| 174 switch (scanner.next()) |
| 175 { |
| 176 case u'=': |
| 177 if (optionEnd < 0) |
| 178 { |
| 179 optionEnd = scanner.position(); |
| 180 valueStart = optionEnd + 1; |
| 181 } |
| 182 break; |
| 183 case u',': |
| 184 if (optionEnd < 0) |
| 185 optionEnd = scanner.position(); |
| 186 ParseOption(text, error, data, optionStart, optionEnd, valueStart, |
| 187 scanner.position()); |
| 188 if (!error.empty()) |
| 189 return; |
| 190 |
| 191 optionStart = scanner.position() + 1; |
| 192 optionEnd = -1; |
| 193 valueStart = -1; |
| 194 break; |
| 195 } |
| 196 } |
| 197 |
| 198 if (data.mContentType < 0) |
| 199 data.mContentType = defaultTypeMask; |
| 200 } |
| 201 } |
| 202 |
| 203 RegExpFilter::RegExpFilter(Type type, const String& text, const RegExpFilterData
& data) |
| 204 : ActiveFilter(type, text, true), mData(data) |
92 { | 205 { |
93 } | 206 } |
94 | 207 |
95 RegExpFilter::~RegExpFilter() | 208 RegExpFilter::~RegExpFilter() |
96 { | 209 { |
97 if (HasRegExp()) | 210 if (mData.HasRegExp()) |
98 EM_ASM_ARGS(regexps.delete($0), mRegexpId); | 211 EM_ASM_ARGS(regexps.delete($0), mData.mRegexpId); |
99 } | 212 } |
100 | 213 |
101 Filter::Type RegExpFilter::Parse(String& text, String& error, | 214 Filter::Type RegExpFilter::Parse(DependentString& text, DependentString& error, |
102 RegExpFilterData& data) | 215 RegExpFilterData& data) |
103 { | 216 { |
104 NormalizeWhitespace(text); | 217 NormalizeWhitespace(text); |
105 | 218 |
106 bool blocking = true; | 219 Filter::Type type = Type::BLOCKING; |
107 | 220 |
108 data.mPatternStart = 0; | 221 data.mPatternStart = 0; |
109 if (text.length() >= 2 && text[0] == u'@' && text[1] == u'@') | 222 if (text.length() >= 2 && text[0] == u'@' && text[1] == u'@') |
110 { | 223 { |
111 blocking = false; | 224 type = Type::WHITELIST; |
112 data.mPatternStart = 2; | 225 data.mPatternStart = 2; |
113 } | 226 } |
114 | 227 |
115 data.mPatternEnd = text.find(u'$', data.mPatternStart); | 228 data.mPatternEnd = text.find(u'$', data.mPatternStart); |
116 if (data.mPatternEnd == text.npos) | 229 if (data.mPatternEnd == text.npos) |
117 data.mPatternEnd = text.length(); | 230 data.mPatternEnd = text.length(); |
118 | 231 |
119 ParseOptions(text, error, data, data.mPatternEnd + 1); | 232 ParseOptions(text, error, data, data.mPatternEnd + 1); |
120 if (!error.empty()) | 233 if (!error.empty()) |
121 return Type::INVALID; | 234 return Type::INVALID; |
122 | 235 |
123 if (data.mPatternEnd - data.mPatternStart >= 2 && | 236 if (data.mPatternEnd - data.mPatternStart >= 2 && |
124 text[data.mPatternStart] == u'/' && | 237 text[data.mPatternStart] == u'/' && |
125 text[data.mPatternEnd - 1] == u'/') | 238 text[data.mPatternEnd - 1] == u'/') |
126 { | 239 { |
127 data.SetRegExp(GenerateRegExp(String(text, data.mPatternStart + 1, | 240 data.SetRegExp(GenerateRegExp(DependentString(text, data.mPatternStart + 1, |
128 data.mPatternEnd - data.mPatternStart - 2), data.mMatchCase)); | 241 data.mPatternEnd - data.mPatternStart - 2), data.mMatchCase)); |
129 | 242 if (data.mRegexpId == -1) |
130 int errorLength = EM_ASM_INT(return regexps.getErrorLength($0), | 243 { |
131 data.mRegexpId); | 244 error.reset(u"filter_invalid_regexp"_str); |
132 if (errorLength >= 0) | |
133 { | |
134 String regexpError(errorLength); | |
135 EM_ASM_ARGS(regexps.getError($0, $1), data.mRegexpId, regexpError.data()); | |
136 error.reset(std::move(regexpError)); | |
137 return Type::INVALID; | 245 return Type::INVALID; |
138 } | 246 } |
139 } | 247 } |
140 | 248 |
141 if (blocking) | 249 return type; |
142 return Type::BLOCKING; | |
143 else | |
144 return Type::WHITELIST; | |
145 } | |
146 | |
147 void RegExpFilter::ParseOptions(String& text, String& error, | |
148 RegExpFilterData& data, String::size_type optionsStart) | |
149 { | |
150 data.mMatchCase = false; | |
151 data.mThirdParty = TrippleState::ANY; | |
152 data.mCollapse = TrippleState::ANY; | |
153 data.mDomainsStart = String::npos; | |
154 data.mSitekeysStart = String::npos; | |
155 if (optionsStart >= text.length()) | |
156 { | |
157 data.mContentType = defaultTypeMask; | |
158 return; | |
159 } | |
160 | |
161 data.mContentType = -1; | |
162 | |
163 int optionStart = data.mPatternEnd + 1; | |
164 int optionEnd = -1; | |
165 int valueStart = -1; | |
166 | |
167 StringScanner scanner(text, optionStart, u','); | |
168 bool done = false; | |
169 while (!done) | |
170 { | |
171 done = scanner.done(); | |
172 switch (scanner.next()) | |
173 { | |
174 case u'=': | |
175 if (optionEnd < 0) | |
176 { | |
177 optionEnd = scanner.position(); | |
178 valueStart = optionEnd + 1; | |
179 } | |
180 break; | |
181 case u',': | |
182 if (optionEnd < 0) | |
183 optionEnd = scanner.position(); | |
184 ParseOption(text, error, data, optionStart, optionEnd, valueStart, | |
185 scanner.position()); | |
186 if (!error.empty()) | |
187 return; | |
188 | |
189 optionStart = scanner.position() + 1; | |
190 optionEnd = -1; | |
191 valueStart = -1; | |
192 break; | |
193 } | |
194 } | |
195 | |
196 if (data.mContentType < 0) | |
197 data.mContentType = defaultTypeMask; | |
198 } | |
199 | |
200 void RegExpFilter::ParseOption(String& text, String& error, | |
201 RegExpFilterData& data, int optionStart, int optionEnd, int valueStart, | |
202 int valueEnd) | |
203 { | |
204 if (optionEnd <= optionStart) | |
205 return; | |
206 | |
207 bool reverse = false; | |
208 if (text[optionStart] == u'~') | |
209 { | |
210 reverse = true; | |
211 optionStart++; | |
212 } | |
213 | |
214 String name(text, optionStart, optionEnd - optionStart); | |
215 for (size_t i = 0; i < name.length(); ++i) | |
216 { | |
217 char16_t currChar = name[i]; | |
218 if (currChar >= u'A' && currChar <= u'Z') | |
219 name[i] = currChar + u'a' - u'A'; | |
220 else if (currChar == u'_') | |
221 name[i] = u'-'; | |
222 } | |
223 | |
224 auto it = typeMap.find(name); | |
225 if (it != typeMap.end()) | |
226 { | |
227 if (data.mContentType < 0) | |
228 data.mContentType = reverse ? defaultTypeMask : 0; | |
229 if (reverse) | |
230 data.mContentType &= ~it->second; | |
231 else | |
232 data.mContentType |= it->second; | |
233 } | |
234 else if (name.equals(u"domain"_str)) | |
235 { | |
236 if (valueStart >= 0 && valueEnd > valueStart) | |
237 { | |
238 data.mDomainsStart = valueStart; | |
239 data.mDomainsEnd = valueEnd; | |
240 ToLower(text, data.mDomainsStart, data.mDomainsEnd); | |
241 } | |
242 } | |
243 else if (name.equals(u"sitekey"_str)) | |
244 { | |
245 if (valueStart >= 0 && valueEnd > valueStart) | |
246 { | |
247 data.mSitekeysStart = valueStart; | |
248 data.mSitekeysEnd = valueEnd; | |
249 } | |
250 } | |
251 else if (name.equals(u"match-case"_str)) | |
252 data.mMatchCase = !reverse; | |
253 else if (name.equals(u"third-party"_str)) | |
254 data.mThirdParty = reverse ? TrippleState::NO : TrippleState::YES; | |
255 else if (name.equals(u"collapse"_str)) | |
256 data.mCollapse = reverse ? TrippleState::NO : TrippleState::YES; | |
257 else | |
258 { | |
259 error.reset(u"Unknown option "_str); | |
260 error.append(name); | |
261 } | |
262 } | 250 } |
263 | 251 |
264 void RegExpFilter::ParseSitekeys(const String& sitekeys) const | 252 void RegExpFilter::ParseSitekeys(const String& sitekeys) const |
265 { | 253 { |
266 StringScanner scanner(sitekeys, 0, u'|'); | 254 StringScanner scanner(sitekeys, 0, u'|'); |
267 size_t start = 0; | 255 size_t start = 0; |
268 bool done = false; | 256 bool done = false; |
269 while (!done) | 257 while (!done) |
270 { | 258 { |
271 done = scanner.done(); | 259 done = scanner.done(); |
272 if (scanner.next() == u'|') | 260 if (scanner.next() == u'|') |
273 { | 261 { |
274 if (scanner.position() > start) | 262 if (scanner.position() > start) |
275 AddSitekey(String(sitekeys, start, scanner.position() - start)); | 263 AddSitekey(DependentString(sitekeys, start, scanner.position() - start))
; |
276 start = scanner.position() + 1; | 264 start = scanner.position() + 1; |
277 } | 265 } |
278 } | 266 } |
279 } | 267 } |
280 | 268 |
281 void RegExpFilter::InitJSTypes() | 269 void RegExpFilter::InitJSTypes() |
282 { | 270 { |
283 EM_ASM(exports.RegExpFilter.typeMap = {};); | 271 EM_ASM(exports.RegExpFilter.typeMap = {};); |
284 for (auto it = typeMap.begin(); it != typeMap.end(); ++it) | 272 for (auto it = typeMap.begin(); it != typeMap.end(); ++it) |
285 EM_ASM_ARGS(exports.RegExpFilter.typeMap[getStringData($0).replace("-", "_")
.toUpperCase()] = $1, &(it->first), it->second); | 273 EM_ASM_ARGS(exports.RegExpFilter.typeMap[readString($0).replace("-", "_").to
UpperCase()] = $1, &(it->first), it->second); |
286 } | 274 } |
287 | 275 |
288 String RegExpFilter::RegExpFromSource(const String& source) | 276 OwnedString RegExpFilter::RegExpFromSource(const String& source) |
289 { | 277 { |
290 /* TODO: this is very inefficient */ | 278 /* TODO: this is very inefficient */ |
291 | 279 |
292 // Note: This doesn't remove trailing wildcards, otherwise the result should | 280 // Note: This doesn't remove trailing wildcards, otherwise the result should |
293 // be identical to Filter.toRegExp(). | 281 // be identical to Filter.toRegExp(). |
294 String result; | 282 OwnedString result; |
295 String::value_type prevChar = u'*'; | 283 String::value_type prevChar = u'*'; |
296 for (String::size_type i = 0; i < source.length(); ++i) | 284 for (String::size_type i = 0; i < source.length(); ++i) |
297 { | 285 { |
298 String::value_type currChar = source[i]; | 286 String::value_type currChar = source[i]; |
299 switch (currChar) | 287 switch (currChar) |
300 { | 288 { |
301 case u'*': | 289 case u'*': |
302 if (prevChar != u'*') | 290 if (prevChar != u'*') |
303 result.append(u".*"_str); | 291 result.append(u".*"_str); |
304 break; | 292 break; |
(...skipping 29 matching lines...) Expand all Loading... |
334 !(currChar >= u'A' && currChar <= u'Z') && | 322 !(currChar >= u'A' && currChar <= u'Z') && |
335 !(currChar >= u'0' && currChar <= u'9') && | 323 !(currChar >= u'0' && currChar <= u'9') && |
336 currChar < 128) | 324 currChar < 128) |
337 { | 325 { |
338 result.append(u'\\'); | 326 result.append(u'\\'); |
339 } | 327 } |
340 result.append(currChar); | 328 result.append(currChar); |
341 } | 329 } |
342 prevChar = currChar; | 330 prevChar = currChar; |
343 } | 331 } |
344 return std::move(result.ensure_own_buffer()); | 332 return result; |
345 } | |
346 | |
347 Filter::Type RegExpFilter::GetType() const | |
348 { | |
349 return Type::BLOCKING; | |
350 } | 333 } |
351 | 334 |
352 RegExpFilter::DomainMap* RegExpFilter::GetDomains() const | 335 RegExpFilter::DomainMap* RegExpFilter::GetDomains() const |
353 { | 336 { |
354 if (!DomainsParsingDone()) | 337 if (!mData.DomainsParsingDone()) |
355 { | 338 { |
356 ParseDomains(GetDomainsSource(mText), u'|'); | 339 ParseDomains(mData.GetDomainsSource(mText), u'|'); |
357 SetDomainsParsingDone(); | 340 mData.SetDomainsParsingDone(); |
358 } | 341 } |
359 return ActiveFilter::GetDomains(); | 342 return ActiveFilter::GetDomains(); |
360 } | 343 } |
361 | 344 |
362 RegExpFilter::SitekeySet* RegExpFilter::GetSitekeys() const | 345 RegExpFilter::SitekeySet* RegExpFilter::GetSitekeys() const |
363 { | 346 { |
364 if (!SitekeyParsingDone()) | 347 if (!mData.SitekeyParsingDone()) |
365 { | 348 { |
366 ParseSitekeys(GetSitekeysSource(mText)); | 349 ParseSitekeys(mData.GetSitekeysSource(mText)); |
367 SetSitekeysParsingDone(); | 350 mData.SetSitekeysParsingDone(); |
368 } | 351 } |
369 return ActiveFilter::GetSitekeys(); | 352 return ActiveFilter::GetSitekeys(); |
370 } | 353 } |
371 | 354 |
372 bool RegExpFilter::Matches(const String& location, int typeMask, | 355 bool RegExpFilter::Matches(const String& location, int typeMask, |
373 String& docDomain, bool thirdParty, const String& sitekey) const | 356 DependentString& docDomain, bool thirdParty, const String& sitekey) const |
374 { | 357 { |
375 if (!(mContentType & typeMask) || | 358 if (!(mData.mContentType & typeMask) || |
376 (mThirdParty == TrippleState::YES && !thirdParty) || | 359 (mData.mThirdParty == TrippleState::YES && !thirdParty) || |
377 (mThirdParty == TrippleState::NO && thirdParty) || | 360 (mData.mThirdParty == TrippleState::NO && thirdParty) || |
378 !IsActiveOnDomain(docDomain, sitekey)) | 361 !IsActiveOnDomain(docDomain, sitekey)) |
379 { | 362 { |
380 return false; | 363 return false; |
381 } | 364 } |
382 | 365 |
383 if (!RegExpParsingDone()) | 366 if (!mData.RegExpParsingDone()) |
384 { | 367 { |
385 const String pattern(GetRegExpSource(mText)); | 368 const OwnedString pattern(mData.GetRegExpSource(mText)); |
386 SetRegExp(GenerateRegExp(RegExpFromSource(pattern), mMatchCase)); | 369 mData.SetRegExp(GenerateRegExp(RegExpFromSource(pattern), mData.mMatchCase))
; |
387 } | 370 } |
388 return EM_ASM_INT(return regexps.test($0, $1), mRegexpId, &location); | 371 return EM_ASM_INT(return regexps.test($0, $1), mData.mRegexpId, &location); |
389 } | 372 } |
LEFT | RIGHT |