| Left: | ||
| Right: |
| 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 |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 #include <cstring> | 18 #include <cstring> |
| 19 | 19 |
| 20 #include "ElemHideBase.h" | 20 #include "ElemHideBase.h" |
| 21 #include "../StringScanner.h" | 21 #include "../StringScanner.h" |
| 22 #include "../Utils.h" | |
| 22 | 23 |
| 23 ABP_NS_USING | 24 ABP_NS_USING |
| 24 | 25 |
| 25 namespace | 26 namespace |
| 26 { | 27 { |
| 27 void NormalizeWhitespace(DependentString& text, String::size_type& domainsEnd, | 28 void NormalizeWhitespace(DependentString& text, String::size_type& domainsEnd, |
| 28 String::size_type& selectorStart) | 29 String::size_type& selectorStart) |
| 29 { | 30 { |
| 30 // For element hiding filters we only want to remove spaces preceding the | 31 // For element hiding filters we only want to remove spaces preceding the |
| 31 // selector part. The positions we've determined already have to be adjusted | 32 // selector part. The positions we've determined already have to be adjusted |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 44 // Only spaces before selectorStart position should be removed. | 45 // Only spaces before selectorStart position should be removed. |
| 45 if (pos < selectorStart && text[pos] == ' ') | 46 if (pos < selectorStart && text[pos] == ' ') |
| 46 delta++; | 47 delta++; |
| 47 else | 48 else |
| 48 text[pos - delta] = text[pos]; | 49 text[pos - delta] = text[pos]; |
| 49 } | 50 } |
| 50 selectorStart -= delta; | 51 selectorStart -= delta; |
| 51 | 52 |
| 52 text.reset(text, 0, len - delta); | 53 text.reset(text, 0, len - delta); |
| 53 } | 54 } |
| 55 | |
| 56 static constexpr String::value_type ELEM_HIDE_DELIMITER[] = u"##"; | |
| 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[] = u"#?#"; | |
| 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[] = u"[-abp-properties= "; | |
| 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[] = u":-abp-properties("; | |
| 66 static constexpr String::size_type PROPS_SELECTOR_LEN = str_length_of(PROPS_SE LECTOR); | |
| 54 } | 67 } |
| 55 | 68 |
| 56 ElemHideBase::ElemHideBase(Type type, const String& text, const ElemHideData& da ta) | 69 ElemHideBase::ElemHideBase(Type type, const String& text, const ElemHideData& da ta) |
| 57 : ActiveFilter(type, text, false), mData(data) | 70 : ActiveFilter(type, text, false), mData(data) |
| 58 { | 71 { |
| 59 if (mData.HasDomains()) | 72 if (mData.HasDomains()) |
| 60 ParseDomains(mData.GetDomainsSource(mText), u','); | 73 ParseDomains(mData.GetDomainsSource(mText), u','); |
| 61 } | 74 } |
| 62 | 75 |
| 63 Filter::Type ElemHideBase::Parse(DependentString& text, ElemHideData& data) | 76 Filter::Type ElemHideBase::Parse(DependentString& text, ElemHideData& data, bool & needConversion) |
| 64 { | 77 { |
| 78 needConversion = false; | |
| 79 | |
| 65 StringScanner scanner(text); | 80 StringScanner scanner(text); |
| 66 | 81 |
| 67 // Domains part | 82 // Domains part |
| 68 bool seenSpaces = false; | 83 bool seenSpaces = false; |
| 69 while (!scanner.done()) | 84 while (!scanner.done()) |
| 70 { | 85 { |
| 71 String::value_type next = scanner.next(); | 86 String::value_type next = scanner.next(); |
| 72 if (next == u'#') | 87 if (next == u'#') |
| 73 { | 88 { |
| 74 data.mDomainsEnd = scanner.position(); | 89 data.mDomainsEnd = scanner.position(); |
| 75 break; | 90 break; |
| 76 } | 91 } |
| 77 | 92 |
| 78 switch (next) | 93 switch (next) |
| 79 { | 94 { |
| 80 case u'/': | 95 case u'/': |
| 81 case u'*': | 96 case u'*': |
| 82 case u'|': | 97 case u'|': |
| 83 case u'@': | 98 case u'@': |
| 84 case u'"': | 99 case u'"': |
| 85 case u'!': | 100 case u'!': |
| 86 return Type::UNKNOWN; | 101 return Type::UNKNOWN; |
| 87 case u' ': | 102 case u' ': |
| 88 seenSpaces = true; | 103 seenSpaces = true; |
| 89 break; | 104 break; |
| 90 } | 105 } |
| 91 } | 106 } |
| 92 | 107 |
| 93 seenSpaces |= scanner.skip(u' '); | 108 seenSpaces |= scanner.skip(u' '); |
| 109 bool emulation = false; | |
| 94 bool exception = scanner.skipOne(u'@'); | 110 bool exception = scanner.skipOne(u'@'); |
| 95 if (exception) | 111 if (exception) |
| 96 seenSpaces |= scanner.skip(u' '); | 112 seenSpaces |= scanner.skip(u' '); |
| 113 else | |
| 114 emulation = scanner.skipOne(u'?'); | |
| 97 | 115 |
| 98 String::value_type next = scanner.next(); | 116 String::value_type next = scanner.next(); |
| 99 if (next != u'#') | 117 if (next != u'#') |
| 100 return Type::UNKNOWN; | 118 return Type::UNKNOWN; |
| 101 | 119 |
| 102 // Selector part | 120 // Selector part |
| 103 | 121 |
| 104 // Selector shouldn't be empty | 122 // Selector shouldn't be empty |
| 105 seenSpaces |= scanner.skip(u' '); | 123 seenSpaces |= scanner.skip(u' '); |
| 106 if (scanner.done()) | 124 if (scanner.done()) |
| 107 return Type::UNKNOWN; | 125 return Type::UNKNOWN; |
| 108 | 126 |
| 109 data.mSelectorStart = scanner.position() + 1; | 127 data.mSelectorStart = scanner.position() + 1; |
| 110 | 128 |
| 111 // We are done validating, now we can normalize whitespace and the domain part | 129 // We are done validating, now we can normalize whitespace and the domain part |
| 112 if (seenSpaces) | 130 if (seenSpaces) |
| 113 NormalizeWhitespace(text, data.mDomainsEnd, data.mSelectorStart); | 131 NormalizeWhitespace(text, data.mDomainsEnd, data.mSelectorStart); |
| 114 DependentString(text, 0, data.mDomainsEnd).toLower(); | 132 DependentString(text, 0, data.mDomainsEnd).toLower(); |
| 115 | 133 |
| 134 // We still need to check the old syntax. It will be converted when | |
| 135 // we instantiate the filter. | |
| 136 if (!emulation && | |
| 137 text.find(OLD_PROPS_SELECTOR, data.mSelectorStart, OLD_PROPS_SELECTOR_LEN) != text.npos) | |
| 138 { | |
| 139 needConversion = true; | |
| 140 emulation = !exception; | |
| 141 } | |
| 142 | |
| 116 if (exception) | 143 if (exception) |
| 117 return Type::ELEMHIDEEXCEPTION; | 144 return Type::ELEMHIDEEXCEPTION; |
| 118 | 145 |
| 119 if (text.find(u"[-abp-properties="_str, data.mSelectorStart) != text.npos) | 146 if (emulation) |
| 120 return Type::ELEMHIDEEMULATION; | 147 return Type::ELEMHIDEEMULATION; |
| 121 | 148 |
| 122 return Type::ELEMHIDE; | 149 return Type::ELEMHIDE; |
| 123 } | 150 } |
| 124 | 151 |
| 125 namespace | 152 namespace |
| 126 { | 153 { |
| 154 struct Range | |
| 155 { | |
| 156 String::size_type start; | |
| 157 String::size_type end; | |
| 158 String::size_type len() const | |
| 159 { | |
| 160 return end - start; | |
| 161 } | |
| 162 String::size_type byte_len() const | |
| 163 { | |
| 164 return len() * sizeof(String::value_type); | |
| 165 } | |
| 166 }; | |
| 167 } | |
| 168 | |
| 169 // Convert filter from the old syntax to the new. | |
| 170 DependentString ElemHideBase::ConvertFilter(String& text, String::size_type& at) | |
| 171 { | |
| 172 Range prefix = {at, text.find(OLD_PROPS_SELECTOR, at, OLD_PROPS_SELECTOR_LEN)} ; | |
| 173 if (prefix.end == text.npos) | |
| 174 return DependentString(text); | |
| 175 | |
| 176 auto length = text.length(); | |
| 177 Range suffix = {at, length}; | |
| 178 Range properties = { prefix.end + OLD_PROPS_SELECTOR_LEN, 0 }; | |
| 179 String::value_type quote = 0; | |
| 180 for (auto index = properties.start; | |
| 181 index < length && (suffix.start == at); index++) | |
| 182 { | |
| 183 auto c = text[index]; | |
| 184 switch (c) | |
| 185 { | |
| 186 case u'"': | |
| 187 case u'\'': | |
| 188 if (quote == 0) | |
| 189 { | |
| 190 // syntax error: we already have a quoted section. | |
| 191 if (properties.end) | |
| 192 return DependentString(); | |
| 193 | |
| 194 if (properties.start != index) | |
| 195 return DependentString(); | |
| 196 | |
| 197 quote = c; | |
| 198 properties.start = index + 1; | |
| 199 } | |
| 200 else if (quote == c) | |
| 201 { | |
| 202 // end of quoted. | |
| 203 quote = 0; | |
| 204 properties.end = index; | |
| 205 } | |
| 206 break; | |
| 207 case u']': | |
| 208 if (quote == 0) | |
| 209 { | |
| 210 if (properties.end == 0) | |
| 211 return DependentString(); | |
| 212 if (properties.end + 1 != index) | |
| 213 return DependentString(); | |
| 214 suffix.start = index + 1; | |
| 215 } | |
| 216 break; | |
| 217 default: | |
| 218 break; | |
| 219 } | |
| 220 } | |
| 221 | |
| 222 if (suffix.start == at) | |
|
sergei
2018/02/27 10:56:53
Just for reference, I think it (what the whole for
| |
| 223 return DependentString(); | |
| 224 | |
| 225 String::size_type delimiter = text.find(ELEM_HIDE_DELIMITER, 0, | |
| 226 ELEM_HIDE_DELIMITER_LEN); | |
| 227 // +1 for the replacement of "##" by "#?#" | |
| 228 if (delimiter != text.npos) | |
| 229 at++; | |
| 230 auto new_len = at + prefix.len() + PROPS_SELECTOR_LEN + properties.len() + 1 / * ) */ + suffix.len(); | |
| 231 | |
| 232 assert2(new_len + 1 == length || (delimiter == text.npos && new_len + 2 == len gth), u"Inconsistent length in filter conversion."_str); | |
|
sergei
2018/02/27 10:56:53
not important just for reference
length == new_len
hub
2018/02/27 13:32:30
Acknowledged.
| |
| 233 | |
| 234 DependentString converted(text, 0, new_len); | |
| 235 | |
| 236 if (suffix.len()) | |
| 237 { | |
| 238 new_len -= suffix.len(); | |
| 239 std::memmove(converted.data() + new_len, | |
| 240 text.data() + suffix.start, | |
| 241 suffix.byte_len()); | |
| 242 } | |
| 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::memcpy(converted.data() + new_len, | |
| 256 PROPS_SELECTOR, | |
| 257 PROPS_SELECTOR_LEN * sizeof(String::value_type)); | |
| 258 if (prefix.len()) | |
| 259 { | |
| 260 new_len -= prefix.len(); | |
| 261 std::memmove(converted.data() + new_len, | |
| 262 text.data() + prefix.start, prefix.byte_len()); | |
| 263 } | |
| 264 | |
| 265 if (delimiter != String::npos) | |
| 266 { | |
| 267 std::memcpy(converted.data() + delimiter, ELEM_HIDE_EMULATION_DELIMITER, | |
| 268 ELEM_HIDE_EMULATION_DELIMITER_LEN * sizeof(String::value_type)); | |
| 269 } | |
| 270 | |
| 271 return converted; | |
| 272 } | |
| 273 | |
| 274 namespace | |
| 275 { | |
| 127 static constexpr String::value_type OPENING_CURLY_REPLACEMENT[] = u"\\7B "; | 276 static constexpr String::value_type OPENING_CURLY_REPLACEMENT[] = u"\\7B "; |
| 128 static constexpr String::value_type CLOSING_CURLY_REPLACEMENT[] = u"\\7D "; | 277 static constexpr String::value_type CLOSING_CURLY_REPLACEMENT[] = u"\\7D "; |
| 129 static constexpr String::size_type CURLY_REPLACEMENT_SIZE = sizeof(OPENING_CUR LY_REPLACEMENT) / sizeof(OPENING_CURLY_REPLACEMENT[0]) - 1; | 278 static constexpr String::size_type CURLY_REPLACEMENT_SIZE = str_length_of(OPEN ING_CURLY_REPLACEMENT); |
| 130 | 279 |
| 131 OwnedString EscapeCurlies(String::size_type replacementCount, | 280 OwnedString EscapeCurlies(String::size_type replacementCount, |
| 132 const DependentString& str) | 281 const DependentString& str) |
| 133 { | 282 { |
| 134 OwnedString result(str.length() + replacementCount * (CURLY_REPLACEMENT_SIZE - 1)); | 283 OwnedString result(str.length() + replacementCount * (CURLY_REPLACEMENT_SIZE - 1)); |
| 135 | 284 |
| 136 String::value_type* current = result.data(); | 285 String::value_type* current = result.data(); |
| 137 for (String::size_type i = 0; i < str.length(); i++) | 286 for (String::size_type i = 0; i < str.length(); i++) |
| 138 { | 287 { |
| 139 switch(str[i]) | 288 switch(str[i]) |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 183 if (item.second && !item.first.empty()) | 332 if (item.second && !item.first.empty()) |
| 184 { | 333 { |
| 185 if (!result.empty()) | 334 if (!result.empty()) |
| 186 result.append(u','); | 335 result.append(u','); |
| 187 result.append(item.first); | 336 result.append(item.first); |
| 188 } | 337 } |
| 189 } | 338 } |
| 190 } | 339 } |
| 191 return result; | 340 return result; |
| 192 } | 341 } |
| OLD | NEW |