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

Side by Side Diff: compiled/subscription/DownloadableSubscription.cpp

Issue 29606600: Issue 5146 - Implement DownloadableSubscription parsing in C++ (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Now all test pass (almost unchanged). Addressed many issues. Created Dec. 1, 2017, 2:41 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « compiled/subscription/DownloadableSubscription.h ('k') | compiled/subscription/Subscription.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <cwctype>
19 #include <limits>
20
18 #include "DownloadableSubscription.h" 21 #include "DownloadableSubscription.h"
22 #include "../Base64.h"
23 #include "../FilterNotifier.h"
24 #include "../StringScanner.h"
25 #include "../filter/CommentFilter.h"
26
27 namespace {
28 constexpr int MILLIS_IN_HOUR = 60 * 60 * 1000;
29 constexpr int MILLIS_IN_DAY = 24 * MILLIS_IN_HOUR;
30 // limits
31 constexpr int64_t MAX_HOUR = std::numeric_limits<int64_t>::max() / MILLIS_IN_H OUR;
32 constexpr int64_t MAX_DAY = std::numeric_limits<int64_t>::max() / MILLIS_IN_DA Y;
33
34 typedef std::pair<DependentString, DependentString> Param;
35
36 Param ParseParam(const String& text)
37 {
38 Param param;
39
40 if (text[0] == u'!')
41 {
42 bool foundColon = false;
43 String::size_type beginParam = 0;
44 String::size_type endParam = 0;
45 String::size_type beginValue = 0;
46 for (String::size_type i = 1; i < text.length(); i++)
47 {
48 switch (text[i])
49 {
50 case ' ':
51 case '\t':
52 if (beginParam > 0 && !foundColon)
53 {
54 endParam = i;
55 }
56 break;
57 case ':':
58 foundColon = true;
59 endParam = i;
60 break;
61 default:
62 if (foundColon)
63 {
64 beginValue = i;
65 }
66 else
67 {
68 if (beginParam == 0)
69 beginParam = i;
70 }
71 break;
72 }
73 if (beginValue > 0)
74 break;
75 }
76 if (beginValue > 0)
77 {
78 param.first = DependentString(text, beginParam, endParam - beginParam);
79 param.first.toLower();
80 param.second = DependentString(
81 text, beginValue, text.length() - beginValue);
82 }
83 }
84 return param;
85 }
86 }
87
88 DownloadableSubscription_Parser::DownloadableSubscription_Parser()
89 : mFirstLine(true)
90 {
91 annotate_address(this, "DownloadableSubscription_Parser");
92 }
93
94 namespace {
95 const DependentString ADBLOCK_HEADER(u"[Adblock"_str);
96
97 // Only check for trailing base64 padding. There should be at most 2 '='.
98 // In that case return a truncated string.
99 DependentString CleanUpChecksum(const String& checksum)
100 {
101 const auto len = checksum.length();
102 if ((len > 22 && len <= 24) &&
103 (checksum[22] == u'=' && (len == 23 || checksum[23] == u'=')))
104 return DependentString(checksum, 0, 22);
105 return DependentString(checksum);
106 }
107 }
108
109 void DownloadableSubscription_Parser::Process(const String& line)
110 {
111 bool isHeader = false;
112 bool doChecksum = true;
113 isHeader = line.find(ADBLOCK_HEADER) != String::npos;
114 auto param = ParseParam(line);
115 if (!param.first.is_invalid())
116 {
117 if (param.first == u"checksum"_str)
118 {
119 mParams[param.first] = CleanUpChecksum(param.second);
120 doChecksum = false;
121 }
122 else
123 mParams[param.first] = param.second;
124 }
125 // Checksum is an MD5 checksum (base64-encoded without the trailing "=") of
126 // all lines in UTF-8 without the checksum line, joined with "\n".
127 if (doChecksum)
128 {
129 if (!mFirstLine)
130 mChecksum.Update((const uint8_t*)"\n", 1);
131 else
132 mFirstLine = false;
133 mChecksum.Update(line);
134 }
135 if (param.first.is_invalid() && !isHeader)
136 mFiltersText.emplace_back(line);
137 }
138
139 int64_t DownloadableSubscription_Parser::ParseExpires(const String& expires)
140 {
141 bool isHour = false;
142 StringScanner scanner(expires);
143 String::size_type numStart = 0;
144 String::size_type numLen = 0;
145 while(!scanner.done())
146 {
147 auto ch = scanner.next();
148 if (std::iswdigit(ch))
149 {
150 if (numLen == 0)
151 numStart = scanner.position();
152 numLen++;
153 }
154 else if (std::iswspace(ch))
155 {
156 if (numLen)
157 break;
158 }
159 else
160 {
161 if (numLen)
162 scanner.back();
163 break;
164 }
165 }
166
167 DependentString numStr(expires, numStart, numLen);
168 int64_t num = numStr.toInt<int64_t>();
169 if (num == 0)
170 return 0;
171
172 while (!scanner.done())
173 {
174 auto ch = scanner.next();
175 if (std::iswspace(ch))
176 continue;
177
178 if (ch == u'h')
179 isHour = true;
180
181 // assume we are done here. The rest is ignored.
182 break;
183 }
184 // check for overflow.
185 if ((isHour && (num > MAX_HOUR)) || (num > MAX_DAY))
186 return 0;
187
188 num *= isHour ? MILLIS_IN_HOUR : MILLIS_IN_DAY;
189 return num;
190 }
191
192 bool DownloadableSubscription_Parser::VerifyChecksum()
193 {
194 if (!mParams.find(u"checksum"_str))
195 return true;
196
197 if (mB64Checksum.is_invalid())
198 {
199 uint8_t checksum[MD5::CHECKSUM_LENGTH];
200 mChecksum.Final(checksum);
201 mB64Checksum = ToBase64(checksum, MD5::CHECKSUM_LENGTH);
202 }
203 return (mParams[u"checksum"_str] == mB64Checksum);
204 }
205
206 int64_t DownloadableSubscription_Parser::Finalize(DownloadableSubscription& subs cription)
207 {
208 if (mB64Checksum.is_invalid())
209 VerifyChecksum(); // here we ignore the checksum, but we calculate it.
210
211 auto entry = mParams.find(u"title"_str);
212 if (entry)
213 {
214 subscription.SetTitle(entry->second);
215 subscription.SetFixedTitle(true);
216 }
217 else
218 subscription.SetFixedTitle(false);
219
220 int32_t version = 0;
221 entry = mParams.find(u"version"_str);
222 if (entry)
223 version = entry->second.toInt<int32_t>();
224 subscription.SetDataRevision(version);
225
226 int64_t expires = 0;
227 entry = mParams.find(u"expires"_str);
228 if (entry)
229 expires = ParseExpires(entry->second);
230
231 FilterNotifier::SubscriptionChange(
232 FilterNotifier::Topic::SUBSCRIPTION_BEFORE_FILTERS_REPLACED,
233 subscription);
234
235 Subscription::Filters filters;
236 filters.reserve(mFiltersText.size());
237 for (auto text : mFiltersText)
238 {
239 DependentString dependent(text);
240 filters.emplace_back(Filter::FromText(dependent), false);
241 }
242
243 subscription.SetFilters(std::move(filters));
244 FilterNotifier::SubscriptionChange(
245 FilterNotifier::Topic::SUBSCRIPTION_FILTERS_REPLACED, subscription);
246
247 return expires;
248 }
249
250 namespace {
251 DependentString emptyString = u""_str;
252 }
253
254 const String& DownloadableSubscription_Parser::GetRedirect() const
255 {
256 auto entry = mParams.find(u"redirect"_str);
257 if (entry)
258 return entry->second;
259 return emptyString;
260 }
261
262 const String& DownloadableSubscription_Parser::GetHomepage() const
263 {
264 auto entry = mParams.find(u"homepage"_str);
265 if (entry)
266 return entry->second;
267 return emptyString;
268 }
19 269
20 DownloadableSubscription::DownloadableSubscription(const String& id) 270 DownloadableSubscription::DownloadableSubscription(const String& id)
21 : Subscription(classType, id), mFixedTitle(false), mLastCheck(0), 271 : Subscription(classType, id), mFixedTitle(false), mLastCheck(0),
22 mHardExpiration(0), mSoftExpiration(0), mLastDownload(0), mLastSuccess(0), 272 mHardExpiration(0), mSoftExpiration(0), mLastDownload(0), mLastSuccess(0),
23 mErrorCount(0), mDataRevision(0), mDownloadCount(0) 273 mErrorCount(0), mDataRevision(0), mDownloadCount(0)
24 { 274 {
25 SetTitle(id); 275 SetTitle(id);
26 } 276 }
27 277
278 DownloadableSubscription_Parser* DownloadableSubscription::ParseDownload()
279 {
280 return new DownloadableSubscription_Parser();
281 }
282
28 OwnedString DownloadableSubscription::Serialize() const 283 OwnedString DownloadableSubscription::Serialize() const
29 { 284 {
30 OwnedString result(Subscription::Serialize()); 285 OwnedString result(Subscription::Serialize());
31 if (mFixedTitle) 286 if (mFixedTitle)
32 result.append(u"fixedTitle=true\n"_str); 287 result.append(u"fixedTitle=true\n"_str);
33 if (!mHomepage.empty()) 288 if (!mHomepage.empty())
34 { 289 {
35 result.append(u"homepage="_str); 290 result.append(u"homepage="_str);
36 result.append(mHomepage); 291 result.append(mHomepage);
37 result.append(u'\n'); 292 result.append(u'\n');
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 result.append(u'\n'); 346 result.append(u'\n');
92 } 347 }
93 if (mDownloadCount) 348 if (mDownloadCount)
94 { 349 {
95 result.append(u"downloadCount="_str); 350 result.append(u"downloadCount="_str);
96 result.append(mDownloadCount); 351 result.append(mDownloadCount);
97 result.append(u'\n'); 352 result.append(u'\n');
98 } 353 }
99 return result; 354 return result;
100 } 355 }
OLDNEW
« no previous file with comments | « compiled/subscription/DownloadableSubscription.h ('k') | compiled/subscription/Subscription.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld