| OLD | NEW |
| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 // Only spaces before selectorStart position should be removed. | 45 // Only spaces before selectorStart position should be removed. |
| 46 if (pos < selectorStart && text[pos] == ABP_TEXT(' ')) | 46 if (pos < selectorStart && text[pos] == ABP_TEXT(' ')) |
| 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 | |
| 56 static constexpr String::value_type ELEM_HIDE_DELIMITER[] = ABP_TEXT("##"); | |
| 57 static constexpr String::size_type ELEM_HIDE_DELIMITER_LEN = str_length_of(ELE
M_HIDE_DELIMITER); | |
| 58 | |
| 59 static constexpr String::value_type ELEM_HIDE_EMULATION_DELIMITER[] = ABP_TEXT
("#?#"); | |
| 60 static constexpr String::size_type ELEM_HIDE_EMULATION_DELIMITER_LEN = str_len
gth_of(ELEM_HIDE_EMULATION_DELIMITER); | |
| 61 | |
| 62 static constexpr String::value_type OLD_PROPS_SELECTOR[] = ABP_TEXT("[-abp-pro
perties="); | |
| 63 static constexpr String::size_type OLD_PROPS_SELECTOR_LEN = str_length_of(OLD_
PROPS_SELECTOR); | |
| 64 | |
| 65 static constexpr String::value_type PROPS_SELECTOR[] = ABP_TEXT(":-abp-propert
ies("); | |
| 66 static constexpr String::size_type PROPS_SELECTOR_LEN = str_length_of(PROPS_SE
LECTOR); | |
| 67 } | 55 } |
| 68 | 56 |
| 69 ElemHideBase::ElemHideBase(Type type, const String& text, const ElemHideData& da
ta, const ParsedDomains& parsedDomains) | 57 ElemHideBase::ElemHideBase(Type type, const String& text, const ElemHideData& da
ta, const ParsedDomains& parsedDomains) |
| 70 : ActiveFilter(type, text, false), mData(data) | 58 : ActiveFilter(type, text, false), mData(data) |
| 71 { | 59 { |
| 72 if (mData.HasDomains()) | 60 if (mData.HasDomains()) |
| 73 FillDomains(mData.GetDomainsSource(mText), parsedDomains); | 61 FillDomains(mData.GetDomainsSource(mText), parsedDomains); |
| 74 } | 62 } |
| 75 | 63 |
| 76 Filter::Type ElemHideBase::Parse(DependentString& text, DependentString& error, | 64 Filter::Type ElemHideBase::Parse(DependentString& text, DependentString& error, |
| 77 ElemHideData& data, bool& needConversion, | 65 ElemHideData& data, |
| 78 ParsedDomains& parsedDomains) | 66 ParsedDomains& parsedDomains) |
| 79 { | 67 { |
| 80 needConversion = false; | |
| 81 | |
| 82 StringScanner scanner(text); | 68 StringScanner scanner(text); |
| 83 | 69 |
| 84 // Domains part | 70 // Domains part |
| 85 bool seenSpaces = false; | 71 bool seenSpaces = false; |
| 86 while (!scanner.done()) | 72 while (!scanner.done()) |
| 87 { | 73 { |
| 88 String::value_type next = scanner.next(); | 74 String::value_type next = scanner.next(); |
| 89 if (next == ABP_TEXT('#')) | 75 if (next == ABP_TEXT('#')) |
| 90 { | 76 { |
| 91 data.mDomainsEnd = scanner.position(); | 77 data.mDomainsEnd = scanner.position(); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 NormalizeWhitespace(text, data.mDomainsEnd, data.mSelectorStart); | 119 NormalizeWhitespace(text, data.mDomainsEnd, data.mSelectorStart); |
| 134 DependentString(text, 0, data.mDomainsEnd).toLower(); | 120 DependentString(text, 0, data.mDomainsEnd).toLower(); |
| 135 | 121 |
| 136 parsedDomains = | 122 parsedDomains = |
| 137 ParseDomainsInternal(data.GetDomainsSource(text), ABP_TEXT(','), false); | 123 ParseDomainsInternal(data.GetDomainsSource(text), ABP_TEXT(','), false); |
| 138 if (parsedDomains.hasEmpty) | 124 if (parsedDomains.hasEmpty) |
| 139 { | 125 { |
| 140 error = ABP_TEXT("filter_invalid_domain"_str); | 126 error = ABP_TEXT("filter_invalid_domain"_str); |
| 141 return Type::INVALID; | 127 return Type::INVALID; |
| 142 } | 128 } |
| 143 // We still need to check the old syntax. It will be converted when | |
| 144 // we instantiate the filter. | |
| 145 if (!emulation && | |
| 146 text.find(OLD_PROPS_SELECTOR, data.mSelectorStart, OLD_PROPS_SELECTOR_LEN)
!= text.npos) | |
| 147 { | |
| 148 needConversion = true; | |
| 149 emulation = !exception; | |
| 150 } | |
| 151 | 129 |
| 152 if (exception) | 130 if (exception) |
| 153 return Type::ELEMHIDEEXCEPTION; | 131 return Type::ELEMHIDEEXCEPTION; |
| 154 | 132 |
| 155 if (emulation) | 133 if (emulation) |
| 156 return Type::ELEMHIDEEMULATION; | 134 return Type::ELEMHIDEEMULATION; |
| 157 | 135 |
| 158 return Type::ELEMHIDE; | 136 return Type::ELEMHIDE; |
| 159 } | 137 } |
| 160 | 138 |
| 161 namespace | 139 namespace |
| 162 { | 140 { |
| 163 struct Range | |
| 164 { | |
| 165 String::size_type start; | |
| 166 String::size_type end; | |
| 167 String::size_type len() const | |
| 168 { | |
| 169 return end - start; | |
| 170 } | |
| 171 String::size_type byte_len() const | |
| 172 { | |
| 173 return len() * sizeof(String::value_type); | |
| 174 } | |
| 175 }; | |
| 176 } | |
| 177 | |
| 178 // Convert filter from the old syntax to the new. | |
| 179 DependentString ElemHideBase::ConvertFilter(String& text, String::size_type& at) | |
| 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 | |
| 185 auto length = text.length(); | |
| 186 Range suffix = {at, length}; | |
| 187 Range properties = { prefix.end + OLD_PROPS_SELECTOR_LEN, 0 }; | |
| 188 String::value_type quote = 0; | |
| 189 for (auto index = properties.start; | |
| 190 index < length && (suffix.start == at); index++) | |
| 191 { | |
| 192 auto c = text[index]; | |
| 193 switch (c) | |
| 194 { | |
| 195 case ABP_TEXT('"'): | |
| 196 case ABP_TEXT('\''): | |
| 197 if (quote == 0) | |
| 198 { | |
| 199 // syntax error: we already have a quoted section. | |
| 200 if (properties.end) | |
| 201 return DependentString(); | |
| 202 | |
| 203 if (properties.start != index) | |
| 204 return DependentString(); | |
| 205 | |
| 206 quote = c; | |
| 207 properties.start = index + 1; | |
| 208 } | |
| 209 else if (quote == c) | |
| 210 { | |
| 211 // end of quoted. | |
| 212 quote = 0; | |
| 213 properties.end = index; | |
| 214 } | |
| 215 break; | |
| 216 case ABP_TEXT(']'): | |
| 217 if (quote == 0) | |
| 218 { | |
| 219 if (properties.end == 0) | |
| 220 return DependentString(); | |
| 221 if (properties.end + 1 != index) | |
| 222 return DependentString(); | |
| 223 suffix.start = index + 1; | |
| 224 } | |
| 225 break; | |
| 226 default: | |
| 227 break; | |
| 228 } | |
| 229 } | |
| 230 | |
| 231 if (suffix.start == at) | |
| 232 return DependentString(); | |
| 233 | |
| 234 String::size_type delimiter = text.find(ELEM_HIDE_DELIMITER, 0, | |
| 235 ELEM_HIDE_DELIMITER_LEN); | |
| 236 // +1 for the replacement of "##" by "#?#" | |
| 237 if (delimiter != text.npos) | |
| 238 at++; | |
| 239 auto new_len = at + prefix.len() + PROPS_SELECTOR_LEN + properties.len() + 1 /
* ) */ + suffix.len(); | |
| 240 | |
| 241 assert2(length == new_len + (delimiter == text.npos ? 2 : 1), ABP_TEXT("Incons
istent length in filter conversion."_str)); | |
| 242 | |
| 243 DependentString converted(text, 0, new_len); | |
| 244 | |
| 245 if (suffix.len()) | |
| 246 { | |
| 247 new_len -= suffix.len(); | |
| 248 std::memmove(converted.data() + new_len, | |
| 249 text.data() + suffix.start, | |
| 250 suffix.byte_len()); | |
| 251 } | |
| 252 new_len--; | |
| 253 // here we need to move the properties before inserting the ')' | |
| 254 auto parens = new_len; | |
| 255 if (properties.len()) | |
| 256 { | |
| 257 new_len -= properties.len(); | |
| 258 std::memmove(converted.data() + new_len, | |
| 259 text.data() + properties.start, properties.byte_len()); | |
| 260 } | |
| 261 converted[parens] = ABP_TEXT(')'); | |
| 262 | |
| 263 new_len -= PROPS_SELECTOR_LEN; | |
| 264 std::memcpy(converted.data() + new_len, | |
| 265 PROPS_SELECTOR, | |
| 266 PROPS_SELECTOR_LEN * sizeof(String::value_type)); | |
| 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; | |
| 281 } | |
| 282 | |
| 283 namespace | |
| 284 { | |
| 285 static constexpr String::value_type OPENING_CURLY_REPLACEMENT[] = ABP_TEXT("\\
7B "); | 141 static constexpr String::value_type OPENING_CURLY_REPLACEMENT[] = ABP_TEXT("\\
7B "); |
| 286 static constexpr String::value_type CLOSING_CURLY_REPLACEMENT[] = ABP_TEXT("\\
7D "); | 142 static constexpr String::value_type CLOSING_CURLY_REPLACEMENT[] = ABP_TEXT("\\
7D "); |
| 287 static constexpr String::size_type CURLY_REPLACEMENT_SIZE = str_length_of(OPEN
ING_CURLY_REPLACEMENT); | 143 static constexpr String::size_type CURLY_REPLACEMENT_SIZE = str_length_of(OPEN
ING_CURLY_REPLACEMENT); |
| 288 | 144 |
| 289 OwnedString EscapeCurlies(String::size_type replacementCount, | 145 OwnedString EscapeCurlies(String::size_type replacementCount, |
| 290 const DependentString& str) | 146 const DependentString& str) |
| 291 { | 147 { |
| 292 OwnedString result(str.length() + replacementCount * (CURLY_REPLACEMENT_SIZE
- 1)); | 148 OwnedString result(str.length() + replacementCount * (CURLY_REPLACEMENT_SIZE
- 1)); |
| 293 | 149 |
| 294 String::value_type* current = result.data(); | 150 String::value_type* current = result.data(); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 if (item.second && !item.first.empty()) | 197 if (item.second && !item.first.empty()) |
| 342 { | 198 { |
| 343 if (!result.empty()) | 199 if (!result.empty()) |
| 344 result.append(ABP_TEXT(',')); | 200 result.append(ABP_TEXT(',')); |
| 345 result.append(item.first); | 201 result.append(item.first); |
| 346 } | 202 } |
| 347 } | 203 } |
| 348 } | 204 } |
| 349 return result; | 205 return result; |
| 350 } | 206 } |
| OLD | NEW |