Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: compiled/filter/ElemHideBase.cpp

Issue 29595633: Issue 5870 - Implement the new ElemHideEmulation filter type (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Reworked the strings const. String::find() from a String::value_type* Created Feb. 1, 2018, 8:36 p.m.
Right Patch Set: Deal with ill formed filters. Created Feb. 14, 2018, 5:05 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « compiled/filter/ElemHideBase.h ('k') | compiled/filter/Filter.cpp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 22 #include "../Utils.h"
23 // the length of a static string array 23
24 #define LENGTH_OF(x) ((sizeof(x) / sizeof(x[0])) - 1) 24 ABP_NS_USING
25 25
26 namespace 26 namespace
27 { 27 {
28 void NormalizeWhitespace(DependentString& text, String::size_type& domainsEnd, 28 void NormalizeWhitespace(DependentString& text, String::size_type& domainsEnd,
29 String::size_type& selectorStart) 29 String::size_type& selectorStart)
30 { 30 {
31 // 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
32 // 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
33 // accordingly. 33 // accordingly.
34 34
(...skipping 10 matching lines...) Expand all
45 // Only spaces before selectorStart position should be removed. 45 // Only spaces before selectorStart position should be removed.
46 if (pos < selectorStart && text[pos] == ' ') 46 if (pos < selectorStart && text[pos] == ' ')
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[] = 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);
55 } 67 }
56 68
57 ElemHideBase::ElemHideBase(Type type, const String& text, const ElemHideData& da ta) 69 ElemHideBase::ElemHideBase(Type type, const String& text, const ElemHideData& da ta)
58 : ActiveFilter(type, text, false), mData(data) 70 : ActiveFilter(type, text, false), mData(data)
59 { 71 {
60 if (mData.HasDomains()) 72 if (mData.HasDomains())
61 ParseDomains(mData.GetDomainsSource(mText), u','); 73 ParseDomains(mData.GetDomainsSource(mText), u',');
62 } 74 }
63 75
64 Filter::Type ElemHideBase::Parse(DependentString& text, ElemHideData& data) 76 Filter::Type ElemHideBase::Parse(DependentString& text, ElemHideData& data, bool & needConversion)
65 { 77 {
78 needConversion = false;
79
66 StringScanner scanner(text); 80 StringScanner scanner(text);
67 81
68 // Domains part 82 // Domains part
69 bool seenSpaces = false; 83 bool seenSpaces = false;
70 while (!scanner.done()) 84 while (!scanner.done())
71 { 85 {
72 String::value_type next = scanner.next(); 86 String::value_type next = scanner.next();
73 if (next == u'#') 87 if (next == u'#')
74 { 88 {
75 data.mDomainsEnd = scanner.position(); 89 data.mDomainsEnd = scanner.position();
(...skipping 28 matching lines...) Expand all
104 return Type::UNKNOWN; 118 return Type::UNKNOWN;
105 119
106 // Selector part 120 // Selector part
107 121
108 // Selector shouldn't be empty 122 // Selector shouldn't be empty
109 seenSpaces |= scanner.skip(u' '); 123 seenSpaces |= scanner.skip(u' ');
110 if (scanner.done()) 124 if (scanner.done())
111 return Type::UNKNOWN; 125 return Type::UNKNOWN;
112 126
113 data.mSelectorStart = scanner.position() + 1; 127 data.mSelectorStart = scanner.position() + 1;
114 data.mNeedConversion = false;
115 128
116 // 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
117 if (seenSpaces) 130 if (seenSpaces)
118 NormalizeWhitespace(text, data.mDomainsEnd, data.mSelectorStart); 131 NormalizeWhitespace(text, data.mDomainsEnd, data.mSelectorStart);
119 DependentString(text, 0, data.mDomainsEnd).toLower(); 132 DependentString(text, 0, data.mDomainsEnd).toLower();
120 133
121 // We still need to check the old syntax. It will be converted when 134 // We still need to check the old syntax. It will be converted when
122 // we instantiate the filter. 135 // we instantiate the filter.
123 if (!emulation && 136 if (!emulation &&
124 text.find(u"[-abp-properties="_str, data.mSelectorStart) != text.npos) 137 text.find(OLD_PROPS_SELECTOR, data.mSelectorStart, OLD_PROPS_SELECTOR_LEN) != text.npos)
125 { 138 {
126 data.mNeedConversion = true; 139 needConversion = true;
127 emulation = !exception; 140 emulation = !exception;
128 } 141 }
129 142
130 if (exception) 143 if (exception)
131 return Type::ELEMHIDEEXCEPTION; 144 return Type::ELEMHIDEEXCEPTION;
132 145
133 if (emulation) 146 if (emulation)
134 return Type::ELEMHIDEEMULATION; 147 return Type::ELEMHIDEEMULATION;
135 148
136 return Type::ELEMHIDE; 149 return Type::ELEMHIDE;
137 } 150 }
138 151
139 namespace 152 namespace
140 { 153 {
141 static constexpr String::value_type ELEM_HIDE_DELIMITER[] = u"##"; 154 struct Range
142 static constexpr String::size_type ELEM_HIDE_DELIMITER_LEN = LENGTH_OF(ELEM_HI DE_DELIMITER); 155 {
143 156 String::size_type start;
144 static constexpr String::value_type ELEM_HIDE_EMULATION_DELIMITER[] = u"#?#"; 157 String::size_type end;
145 static constexpr String::size_type ELEM_HIDE_EMULATION_DELIMITER_LEN = LENGTH_ OF(ELEM_HIDE_EMULATION_DELIMITER); 158 String::size_type len() const
146 159 {
147 static constexpr String::value_type PROPS_SELECTOR[] = u"[-abp-properties="; 160 return end - start;
148 static constexpr String::size_type PROPS_SELECTOR_LEN = LENGTH_OF(PROPS_SELECT OR); 161 }
149 162 String::size_type byte_len() const
150 static constexpr String::value_type NEW_PROPS_SELECTOR[] = u":-abp-properties( "; 163 {
151 static constexpr String::size_type NEW_PROPS_SELECTOR_LEN = LENGTH_OF(NEW_PROP S_SELECTOR); 164 return len() * sizeof(String::value_type);
165 }
166 };
152 } 167 }
153 168
154 // Convert filter from the old syntax to the new. 169 // Convert filter from the old syntax to the new.
155 OwnedString ElemHideBase::ConvertFilter(const String& text, String::size_type& a t) 170 DependentString ElemHideBase::ConvertFilter(String& text, String::size_type& at)
156 { 171 {
157 auto selectorPos = text.find(PROPS_SELECTOR, at, PROPS_SELECTOR_LEN); 172 Range prefix = {at, text.find(OLD_PROPS_SELECTOR, at, OLD_PROPS_SELECTOR_LEN)} ;
158 if (selectorPos != text.npos) 173 if (prefix.end == text.npos)
159 { 174 return DependentString(text);
160 auto length = text.length(); 175
161 auto properties = selectorPos + PROPS_SELECTOR_LEN; 176 auto length = text.length();
162 String::value_type quote = 0; 177 Range suffix = {at, length};
163 bool escape = false; 178 Range properties = { prefix.end + OLD_PROPS_SELECTOR_LEN, 0 };
164 String::size_type removed = 0; // how many chars we remove 179 String::value_type quote = 0;
165 String::size_type end = properties; 180 for (auto index = properties.start;
166 String::size_type quote_start = 0; 181 index < length && (suffix.start == at); index++)
167 String::size_type quote_end = 0; 182 {
168 for (auto index = properties; 183 auto c = text[index];
169 index < length && end == properties; index++) 184 switch (c)
170 { 185 {
171 if (escape) 186 case u'"':
172 { 187 case u'\'':
173 escape = false; 188 if (quote == 0)
174 continue; 189 {
175 } 190 // syntax error: we already have a quoted section.
176 191 if (properties.end)
177 auto c = text[index]; 192 return DependentString();
178 switch (c) 193
179 { 194 if (properties.start != index)
180 case '\\': 195 return DependentString();
181 escape = true; 196
182 break; 197 quote = c;
183 case '"': 198 properties.start = index + 1;
184 case '\'': 199 }
185 if (quote == 0) 200 else if (quote == c)
186 { 201 {
187 quote = c; 202 // end of quoted.
188 quote_start = index + 1; 203 quote = 0;
189 } 204 properties.end = index;
190 else if (quote == c) 205 }
191 { 206 break;
192 // end of quoted. 207 case u']':
193 quote = 0; 208 if (quote == 0)
194 removed += 2; 209 {
195 quote_end = index; 210 if (properties.end == 0)
196 } 211 return DependentString();
197 break; 212 if (properties.end + 1 != index)
198 case ']': 213 return DependentString();
199 if (quote == 0) 214 suffix.start = index + 1;
200 end = index + 1; // end of properties (after ]) 215 }
201 break; 216 break;
202 default: 217 default:
203 break; 218 break;
204 } 219 }
205 } 220 }
206 221
207 if (quote != 0) 222 if (suffix.start == at)
sergei 2018/02/27 10:56:53 Just for reference, I think it (what the whole for
208 quote_end = end - 1; 223 return DependentString();
209 else if (quote_end <= quote_start) 224
210 { 225 String::size_type delimiter = text.find(ELEM_HIDE_DELIMITER, 0,
211 // we likely didn't find a quoted content so we just take it as is. 226 ELEM_HIDE_DELIMITER_LEN);
212 quote_start = properties; 227 // +1 for the replacement of "##" by "#?#"
213 quote_end = end - 1; 228 if (delimiter != text.npos)
214 } 229 at++;
215 230 auto new_len = at + prefix.len() + PROPS_SELECTOR_LEN + properties.len() + 1 / * ) */ + suffix.len();
216 // +1 for the replacement of "##" by "#?#" 231
217 String::size_type offset = 0; 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.
218 233
219 String::size_type delimiter = text.find(ELEM_HIDE_DELIMITER, 0, 234 DependentString converted(text, 0, new_len);
220 ELEM_HIDE_DELIMITER_LEN); 235
221 OwnedString converted(length + ((delimiter != text.npos) ? 1 : 0) - removed) ; 236 if (suffix.len())
222 if (delimiter != text.npos) 237 {
223 { 238 new_len -= suffix.len();
224 if (delimiter >= selectorPos) 239 std::memmove(converted.data() + new_len,
225 return OwnedString(text); 240 text.data() + suffix.start,
226 241 suffix.byte_len());
227 at++; 242 }
228 std::memcpy(converted.data(), text.data(), 243 new_len--;
229 delimiter * sizeof(String::value_type)); 244 // here we need to move the properties before inserting the ')'
230 offset += delimiter; 245 auto parens = new_len;
231 std::memcpy(converted.data() + offset, ELEM_HIDE_EMULATION_DELIMITER, 246 if (properties.len())
232 ELEM_HIDE_EMULATION_DELIMITER_LEN * sizeof(String::value_type) ); 247 {
233 offset += ELEM_HIDE_EMULATION_DELIMITER_LEN; 248 new_len -= properties.len();
234 delimiter += ELEM_HIDE_DELIMITER_LEN; 249 std::memmove(converted.data() + new_len,
235 // we have already parsed to past the delimiter. 250 text.data() + properties.start, properties.byte_len());
236 selectorPos -= delimiter; 251 }
237 } 252 converted[parens] = u')';
238 else 253
239 delimiter = 0; 254 new_len -= PROPS_SELECTOR_LEN;
240 255 std::memcpy(converted.data() + new_len,
241 256 PROPS_SELECTOR,
242 std::memcpy(converted.data() + offset, text.data() + delimiter, 257 PROPS_SELECTOR_LEN * sizeof(String::value_type));
243 selectorPos * sizeof(String::value_type)); 258 if (prefix.len())
244 offset += selectorPos; 259 {
245 260 new_len -= prefix.len();
246 std::memcpy(converted.data() + offset, NEW_PROPS_SELECTOR, 261 std::memmove(converted.data() + new_len,
247 NEW_PROPS_SELECTOR_LEN * sizeof(String::value_type)); 262 text.data() + prefix.start, prefix.byte_len());
248 offset += NEW_PROPS_SELECTOR_LEN; 263 }
249 264
250 std::memcpy(converted.data() + offset, text.data() + quote_start, 265 if (delimiter != String::npos)
251 (quote_end - quote_start) * sizeof(String::value_type)); 266 {
252 offset += quote_end - quote_start; 267 std::memcpy(converted.data() + delimiter, ELEM_HIDE_EMULATION_DELIMITER,
253 268 ELEM_HIDE_EMULATION_DELIMITER_LEN * sizeof(String::value_type));
254 std::memcpy(converted.data() + offset, u")", sizeof(String::value_type)); 269 }
255 offset++; 270
256 271 return converted;
257 std::memcpy(converted.data() + offset, text.data() + end,
258 (length - end) * sizeof(String::value_type));
259 offset += (length - end) * sizeof(String::value_type);
260
261 return converted;
262 }
263
264 return OwnedString(text);
265 } 272 }
266 273
267 namespace 274 namespace
268 { 275 {
269 static constexpr String::value_type OPENING_CURLY_REPLACEMENT[] = u"\\7B "; 276 static constexpr String::value_type OPENING_CURLY_REPLACEMENT[] = u"\\7B ";
270 static constexpr String::value_type CLOSING_CURLY_REPLACEMENT[] = u"\\7D "; 277 static constexpr String::value_type CLOSING_CURLY_REPLACEMENT[] = u"\\7D ";
271 static constexpr String::size_type CURLY_REPLACEMENT_SIZE = LENGTH_OF(OPENING_ CURLY_REPLACEMENT); 278 static constexpr String::size_type CURLY_REPLACEMENT_SIZE = str_length_of(OPEN ING_CURLY_REPLACEMENT);
272 279
273 OwnedString EscapeCurlies(String::size_type replacementCount, 280 OwnedString EscapeCurlies(String::size_type replacementCount,
274 const DependentString& str) 281 const DependentString& str)
275 { 282 {
276 OwnedString result(str.length() + replacementCount * (CURLY_REPLACEMENT_SIZE - 1)); 283 OwnedString result(str.length() + replacementCount * (CURLY_REPLACEMENT_SIZE - 1));
277 284
278 String::value_type* current = result.data(); 285 String::value_type* current = result.data();
279 for (String::size_type i = 0; i < str.length(); i++) 286 for (String::size_type i = 0; i < str.length(); i++)
280 { 287 {
281 switch(str[i]) 288 switch(str[i])
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 if (item.second && !item.first.empty()) 332 if (item.second && !item.first.empty())
326 { 333 {
327 if (!result.empty()) 334 if (!result.empty())
328 result.append(u','); 335 result.append(u',');
329 result.append(item.first); 336 result.append(item.first);
330 } 337 }
331 } 338 }
332 } 339 }
333 return result; 340 return result;
334 } 341 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld