LEFT | RIGHT |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 delta++; | 47 delta++; |
48 else | 48 else |
49 text[pos - delta] = text[pos]; | 49 text[pos - delta] = text[pos]; |
50 } | 50 } |
51 selectorStart -= delta; | 51 selectorStart -= delta; |
52 | 52 |
53 text.reset(text, 0, len - delta); | 53 text.reset(text, 0, len - delta); |
54 } | 54 } |
55 | 55 |
56 static constexpr String::value_type ELEM_HIDE_DELIMITER[] = u"##"; | 56 static constexpr String::value_type ELEM_HIDE_DELIMITER[] = u"##"; |
57 static constexpr String::size_type ELEM_HIDE_DELIMITER_LEN = LENGTH_OF(ELEM_HI
DE_DELIMITER); | 57 static constexpr String::size_type ELEM_HIDE_DELIMITER_LEN = str_length_of(ELE
M_HIDE_DELIMITER); |
58 | 58 |
59 static constexpr String::value_type ELEM_HIDE_EMULATION_DELIMITER[] = u"#?#"; | 59 static constexpr String::value_type ELEM_HIDE_EMULATION_DELIMITER[] = u"#?#"; |
60 static constexpr String::size_type ELEM_HIDE_EMULATION_DELIMITER_LEN = LENGTH_
OF(ELEM_HIDE_EMULATION_DELIMITER); | 60 static constexpr String::size_type ELEM_HIDE_EMULATION_DELIMITER_LEN = str_len
gth_of(ELEM_HIDE_EMULATION_DELIMITER); |
61 | 61 |
62 static constexpr String::value_type OLD_PROPS_SELECTOR[] = u"[-abp-properties=
"; | 62 static constexpr String::value_type OLD_PROPS_SELECTOR[] = u"[-abp-properties=
"; |
63 static constexpr String::size_type OLD_PROPS_SELECTOR_LEN = LENGTH_OF(OLD_PROP
S_SELECTOR); | 63 static constexpr String::size_type OLD_PROPS_SELECTOR_LEN = str_length_of(OLD_
PROPS_SELECTOR); |
64 | 64 |
65 static constexpr String::value_type PROPS_SELECTOR[] = u":-abp-properties("; | 65 static constexpr String::value_type PROPS_SELECTOR[] = u":-abp-properties("; |
66 static constexpr String::size_type PROPS_SELECTOR_LEN = LENGTH_OF(PROPS_SELECT
OR); | 66 static constexpr String::size_type PROPS_SELECTOR_LEN = str_length_of(PROPS_SE
LECTOR); |
67 } | 67 } |
68 | 68 |
69 ElemHideBase::ElemHideBase(Type type, const String& text, const ElemHideData& da
ta, const ParsedDomains& parsedDomains) | 69 ElemHideBase::ElemHideBase(Type type, const String& text, const ElemHideData& da
ta, const ParsedDomains& parsedDomains) |
70 : ActiveFilter(type, text, false), mData(data) | 70 : ActiveFilter(type, text, false), mData(data) |
71 { | 71 { |
72 if (mData.HasDomains()) | 72 if (mData.HasDomains()) |
73 FillDomains(mData.GetDomainsSource(mText), parsedDomains); | 73 FillDomains(mData.GetDomainsSource(mText), parsedDomains); |
74 } | 74 } |
75 | 75 |
76 Filter::Type ElemHideBase::Parse(DependentString& text, DependentString& error, | 76 Filter::Type ElemHideBase::Parse(DependentString& text, DependentString& error, |
77 ElemHideData& data, bool& needConversion, | 77 ElemHideData& data, bool& needConversion, |
78 ParsedDomains& parsedDomains) | 78 ParsedDomains& parsedDomains) |
79 { | 79 { |
| 80 needConversion = false; |
| 81 |
80 StringScanner scanner(text); | 82 StringScanner scanner(text); |
81 | 83 |
82 // Domains part | 84 // Domains part |
83 bool seenSpaces = false; | 85 bool seenSpaces = false; |
84 while (!scanner.done()) | 86 while (!scanner.done()) |
85 { | 87 { |
86 String::value_type next = scanner.next(); | 88 String::value_type next = scanner.next(); |
87 if (next == u'#') | 89 if (next == u'#') |
88 { | 90 { |
89 data.mDomainsEnd = scanner.position(); | 91 data.mDomainsEnd = scanner.position(); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 String::size_type byte_len() const | 171 String::size_type byte_len() const |
170 { | 172 { |
171 return len() * sizeof(String::value_type); | 173 return len() * sizeof(String::value_type); |
172 } | 174 } |
173 }; | 175 }; |
174 } | 176 } |
175 | 177 |
176 // Convert filter from the old syntax to the new. | 178 // Convert filter from the old syntax to the new. |
177 DependentString ElemHideBase::ConvertFilter(String& text, String::size_type& at) | 179 DependentString ElemHideBase::ConvertFilter(String& text, String::size_type& at) |
178 { | 180 { |
| 181 Range prefix = {at, text.find(OLD_PROPS_SELECTOR, at, OLD_PROPS_SELECTOR_LEN)}
; |
| 182 if (prefix.end == text.npos) |
| 183 return DependentString(text); |
| 184 |
179 auto length = text.length(); | 185 auto length = text.length(); |
180 Range prefix = {at, 0}; | |
181 Range suffix = {at, length}; | 186 Range suffix = {at, length}; |
182 prefix.end = text.find(OLD_PROPS_SELECTOR, at, OLD_PROPS_SELECTOR_LEN); | 187 Range properties = { prefix.end + OLD_PROPS_SELECTOR_LEN, 0 }; |
183 if (prefix.end != text.npos) | 188 String::value_type quote = 0; |
184 { | 189 for (auto index = properties.start; |
185 Range properties = { prefix.end + OLD_PROPS_SELECTOR_LEN, 0 }; | 190 index < length && (suffix.start == at); index++) |
186 String::value_type quote = 0; | 191 { |
187 bool closing = false; | 192 auto c = text[index]; |
188 for (auto index = properties.start; | 193 switch (c) |
189 index < length && !closing; index++) | 194 { |
190 { | 195 case u'"': |
191 auto c = text[index]; | 196 case u'\'': |
192 switch (c) | 197 if (quote == 0) |
193 { | 198 { |
194 case u'"': | 199 // syntax error: we already have a quoted section. |
195 case u'\'': | 200 if (properties.end) |
196 if (quote == 0) | 201 return DependentString(); |
197 { | 202 |
198 // syntax error: we already have a quoted section. | 203 if (properties.start != index) |
199 if (properties.end) | 204 return DependentString(); |
200 return DependentString(); | 205 |
201 | 206 quote = c; |
202 quote = c; | 207 properties.start = index + 1; |
203 properties.start = index + 1; | 208 } |
204 } | 209 else if (quote == c) |
205 else if (quote == c) | 210 { |
206 { | 211 // end of quoted. |
207 // end of quoted. | 212 quote = 0; |
208 quote = 0; | 213 properties.end = index; |
209 properties.end = index; | 214 } |
210 } | 215 break; |
211 break; | 216 case u']': |
212 case u']': | 217 if (quote == 0) |
213 if (quote == 0) | 218 { |
214 { | 219 if (properties.end == 0) |
215 if (properties.end == 0) | 220 return DependentString(); |
216 return DependentString(); | 221 if (properties.end + 1 != index) |
217 suffix.start = index + 1; | 222 return DependentString(); |
218 closing = true; | 223 suffix.start = index + 1; |
219 } | 224 } |
220 break; | 225 break; |
221 default: | 226 default: |
222 break; | 227 break; |
223 } | 228 } |
224 } | 229 } |
225 | 230 |
226 String::size_type delimiter = text.find(ELEM_HIDE_DELIMITER, 0, | 231 if (suffix.start == at) |
227 ELEM_HIDE_DELIMITER_LEN); | 232 return DependentString(); |
228 // +1 for the replacement of "##" by "#?#" | 233 |
229 if (delimiter != text.npos) | 234 String::size_type delimiter = text.find(ELEM_HIDE_DELIMITER, 0, |
230 at++; | 235 ELEM_HIDE_DELIMITER_LEN); |
231 auto new_len = prefix.len() + suffix.len() + properties.len() | 236 // +1 for the replacement of "##" by "#?#" |
232 + PROPS_SELECTOR_LEN + 1 + at; | 237 if (delimiter != text.npos) |
233 | 238 at++; |
234 DependentString converted(text, 0, new_len); | 239 auto new_len = at + prefix.len() + PROPS_SELECTOR_LEN + properties.len() + 1 /
* ) */ + suffix.len(); |
235 | 240 |
236 if (suffix.len()) | 241 assert2(length == new_len + (delimiter == text.npos ? 2 : 1), u"Inconsistent l
ength in filter conversion."_str); |
237 { | 242 |
238 new_len -= suffix.len(); | 243 DependentString converted(text, 0, new_len); |
239 std::memmove(converted.data() + new_len, | 244 |
240 text.data() + suffix.start, | 245 if (suffix.len()) |
241 suffix.byte_len()); | 246 { |
242 } | 247 new_len -= suffix.len(); |
243 new_len--; | |
244 // here we need to move the properties before inserting the ')' | |
245 auto parens = new_len; | |
246 if (properties.len()) | |
247 { | |
248 new_len -= properties.len(); | |
249 std::memmove(converted.data() + new_len, | |
250 text.data() + properties.start, properties.byte_len()); | |
251 } | |
252 converted[parens] = u')'; | |
253 | |
254 new_len -= PROPS_SELECTOR_LEN; | |
255 std::memmove(converted.data() + new_len, | 248 std::memmove(converted.data() + new_len, |
256 PROPS_SELECTOR, | 249 text.data() + suffix.start, |
257 PROPS_SELECTOR_LEN * sizeof(String::value_type)); | 250 suffix.byte_len()); |
258 if (prefix.len()) | 251 } |
259 { | 252 new_len--; |
260 new_len -= prefix.len(); | 253 // here we need to move the properties before inserting the ')' |
261 std::memmove(converted.data() + new_len, | 254 auto parens = new_len; |
262 text.data() + prefix.start, prefix.byte_len()); | 255 if (properties.len()) |
263 } | 256 { |
264 | 257 new_len -= properties.len(); |
265 if (delimiter != String::npos) | 258 std::memmove(converted.data() + new_len, |
266 { | 259 text.data() + properties.start, properties.byte_len()); |
267 std::memmove(converted.data() + delimiter, ELEM_HIDE_EMULATION_DELIMITER, | 260 } |
268 ELEM_HIDE_EMULATION_DELIMITER_LEN * sizeof(String::value_type
)); | 261 converted[parens] = u')'; |
269 } | 262 |
270 | 263 new_len -= PROPS_SELECTOR_LEN; |
271 return converted; | 264 std::memcpy(converted.data() + new_len, |
272 } | 265 PROPS_SELECTOR, |
273 | 266 PROPS_SELECTOR_LEN * sizeof(String::value_type)); |
274 return DependentString(text); | 267 if (prefix.len()) |
| 268 { |
| 269 new_len -= prefix.len(); |
| 270 std::memmove(converted.data() + new_len, |
| 271 text.data() + prefix.start, prefix.byte_len()); |
| 272 } |
| 273 |
| 274 if (delimiter != String::npos) |
| 275 { |
| 276 std::memcpy(converted.data() + delimiter, ELEM_HIDE_EMULATION_DELIMITER, |
| 277 ELEM_HIDE_EMULATION_DELIMITER_LEN * sizeof(String::value_type)); |
| 278 } |
| 279 |
| 280 return converted; |
275 } | 281 } |
276 | 282 |
277 namespace | 283 namespace |
278 { | 284 { |
279 static constexpr String::value_type OPENING_CURLY_REPLACEMENT[] = u"\\7B "; | 285 static constexpr String::value_type OPENING_CURLY_REPLACEMENT[] = u"\\7B "; |
280 static constexpr String::value_type CLOSING_CURLY_REPLACEMENT[] = u"\\7D "; | 286 static constexpr String::value_type CLOSING_CURLY_REPLACEMENT[] = u"\\7D "; |
281 static constexpr String::size_type CURLY_REPLACEMENT_SIZE = LENGTH_OF(OPENING_
CURLY_REPLACEMENT); | 287 static constexpr String::size_type CURLY_REPLACEMENT_SIZE = str_length_of(OPEN
ING_CURLY_REPLACEMENT); |
282 | 288 |
283 OwnedString EscapeCurlies(String::size_type replacementCount, | 289 OwnedString EscapeCurlies(String::size_type replacementCount, |
284 const DependentString& str) | 290 const DependentString& str) |
285 { | 291 { |
286 OwnedString result(str.length() + replacementCount * (CURLY_REPLACEMENT_SIZE
- 1)); | 292 OwnedString result(str.length() + replacementCount * (CURLY_REPLACEMENT_SIZE
- 1)); |
287 | 293 |
288 String::value_type* current = result.data(); | 294 String::value_type* current = result.data(); |
289 for (String::size_type i = 0; i < str.length(); i++) | 295 for (String::size_type i = 0; i < str.length(); i++) |
290 { | 296 { |
291 switch(str[i]) | 297 switch(str[i]) |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 if (item.second && !item.first.empty()) | 341 if (item.second && !item.first.empty()) |
336 { | 342 { |
337 if (!result.empty()) | 343 if (!result.empty()) |
338 result.append(u','); | 344 result.append(u','); |
339 result.append(item.first); | 345 result.append(item.first); |
340 } | 346 } |
341 } | 347 } |
342 } | 348 } |
343 return result; | 349 return result; |
344 } | 350 } |
LEFT | RIGHT |