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 |
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> | 18 #include <cwctype> |
19 #include <limits> | 19 #include <limits> |
20 | 20 |
21 #include "DownloadableSubscription.h" | 21 #include "DownloadableSubscription.h" |
22 #include "../Base64.h" | |
23 #include "../FilterNotifier.h" | 22 #include "../FilterNotifier.h" |
24 #include "../StringScanner.h" | 23 #include "../StringScanner.h" |
25 #include "../filter/CommentFilter.h" | 24 #include "../filter/CommentFilter.h" |
| 25 |
| 26 ABP_NS_USING |
26 | 27 |
27 namespace { | 28 namespace { |
28 constexpr int MILLIS_IN_HOUR = 60 * 60 * 1000; | 29 constexpr int MILLIS_IN_HOUR = 60 * 60 * 1000; |
29 constexpr int MILLIS_IN_DAY = 24 * MILLIS_IN_HOUR; | 30 constexpr int MILLIS_IN_DAY = 24 * MILLIS_IN_HOUR; |
30 // limits | 31 // limits |
31 constexpr int64_t MAX_HOUR = std::numeric_limits<int64_t>::max() / MILLIS_IN_H
OUR; | 32 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 constexpr int64_t MAX_DAY = std::numeric_limits<int64_t>::max() / MILLIS_IN_DA
Y; |
33 | 34 |
34 typedef std::pair<DependentString, DependentString> Param; | 35 typedef std::pair<DependentString, DependentString> Param; |
35 | 36 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 } | 87 } |
87 | 88 |
88 DownloadableSubscription_Parser::DownloadableSubscription_Parser() | 89 DownloadableSubscription_Parser::DownloadableSubscription_Parser() |
89 : mFirstLine(true) | 90 : mFirstLine(true) |
90 { | 91 { |
91 annotate_address(this, "DownloadableSubscription_Parser"); | 92 annotate_address(this, "DownloadableSubscription_Parser"); |
92 } | 93 } |
93 | 94 |
94 namespace { | 95 namespace { |
95 const DependentString ADBLOCK_HEADER(u"[Adblock"_str); | 96 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 } | 97 } |
108 | 98 |
109 void DownloadableSubscription_Parser::Process(const String& line) | 99 void DownloadableSubscription_Parser::Process(const String& line) |
110 { | 100 { |
111 bool isHeader = false; | 101 bool isHeader = false; |
112 bool doChecksum = true; | |
113 isHeader = line.find(ADBLOCK_HEADER) != String::npos; | 102 isHeader = line.find(ADBLOCK_HEADER) != String::npos; |
114 if (!isHeader) | 103 if (!isHeader) |
115 { | 104 { |
116 auto param = ParseParam(line); | 105 auto param = ParseParam(line); |
117 if (!param.first.is_invalid()) | |
118 { | |
119 if (param.first == u"checksum"_str) | |
120 { | |
121 mParams[param.first] = CleanUpChecksum(param.second); | |
122 doChecksum = false; | |
123 } | |
124 else | |
125 mParams[param.first] = param.second; | |
126 } | |
127 if (param.first.is_invalid()) | 106 if (param.first.is_invalid()) |
128 mFiltersText.emplace_back(line); | 107 mFiltersText.emplace_back(line); |
129 } | |
130 // Checksum is an MD5 checksum (base64-encoded without the trailing "=") of | |
131 // all lines in UTF-8 without the checksum line, joined with "\n". | |
132 if (doChecksum) | |
133 { | |
134 if (!mFirstLine) | |
135 mChecksum.Update((const uint8_t*)"\n", 1); | |
136 else | 108 else |
137 mFirstLine = false; | 109 mParams[param.first] = param.second; |
138 mChecksum.Update(line); | |
139 } | 110 } |
140 } | 111 } |
141 | 112 |
142 int64_t DownloadableSubscription_Parser::ParseExpires(const String& expires) | 113 int64_t DownloadableSubscription_Parser::ParseExpires(const String& expires) |
143 { | 114 { |
144 bool isHour = false; | 115 bool isHour = false; |
145 StringScanner scanner(expires); | 116 StringScanner scanner(expires); |
146 String::size_type numStart = 0; | 117 String::size_type numStart = 0; |
147 String::size_type numLen = 0; | 118 String::size_type numLen = 0; |
148 while(!scanner.done()) | 119 while(!scanner.done()) |
(...skipping 12 matching lines...) Expand all Loading... |
161 } | 132 } |
162 else | 133 else |
163 { | 134 { |
164 if (numLen) | 135 if (numLen) |
165 scanner.back(); | 136 scanner.back(); |
166 break; | 137 break; |
167 } | 138 } |
168 } | 139 } |
169 | 140 |
170 DependentString numStr(expires, numStart, numLen); | 141 DependentString numStr(expires, numStart, numLen); |
171 int64_t num = numStr.toInt<int64_t>(); | 142 int64_t num = lexical_cast<int64_t>(numStr); |
172 if (num == 0) | 143 if (num == 0) |
173 return 0; | 144 return 0; |
174 | 145 |
175 while (!scanner.done()) | 146 while (!scanner.done()) |
176 { | 147 { |
177 auto ch = scanner.next(); | 148 auto ch = scanner.next(); |
178 if (std::iswspace(ch)) | 149 if (std::iswspace(ch)) |
179 continue; | 150 continue; |
180 | 151 |
181 if (ch == u'h') | 152 if (ch == u'h') |
182 isHour = true; | 153 isHour = true; |
183 | 154 |
184 // assume we are done here. The rest is ignored. | 155 // assume we are done here. The rest is ignored. |
185 break; | 156 break; |
186 } | 157 } |
187 // check for overflow. | 158 // check for overflow. |
188 if ((isHour && (num > MAX_HOUR)) || (num > MAX_DAY)) | 159 if ((isHour && (num > MAX_HOUR)) || (num > MAX_DAY)) |
189 return 0; | 160 return 0; |
190 | 161 |
191 num *= isHour ? MILLIS_IN_HOUR : MILLIS_IN_DAY; | 162 num *= isHour ? MILLIS_IN_HOUR : MILLIS_IN_DAY; |
192 return num; | 163 return num; |
193 } | 164 } |
194 | 165 |
195 bool DownloadableSubscription_Parser::VerifyChecksum() | |
196 { | |
197 if (!mParams.find(u"checksum"_str)) | |
198 return true; | |
199 | |
200 if (mB64Checksum.is_invalid()) | |
201 { | |
202 uint8_t checksum[MD5::CHECKSUM_LENGTH]; | |
203 mChecksum.Final(checksum); | |
204 mB64Checksum = ToBase64(checksum, MD5::CHECKSUM_LENGTH); | |
205 } | |
206 return (mParams[u"checksum"_str] == mB64Checksum); | |
207 } | |
208 | |
209 int64_t DownloadableSubscription_Parser::Finalize(DownloadableSubscription& subs
cription) | 166 int64_t DownloadableSubscription_Parser::Finalize(DownloadableSubscription& subs
cription) |
210 { | 167 { |
211 if (mB64Checksum.is_invalid()) | |
212 VerifyChecksum(); // here we ignore the checksum, but we calculate it. | |
213 | |
214 auto entry = mParams.find(u"title"_str); | 168 auto entry = mParams.find(u"title"_str); |
215 if (entry) | 169 if (entry) |
216 { | 170 { |
217 subscription.SetTitle(entry->second); | 171 subscription.SetTitle(entry->second); |
218 subscription.SetFixedTitle(true); | 172 subscription.SetFixedTitle(true); |
219 } | 173 } |
220 else | 174 else |
221 subscription.SetFixedTitle(false); | 175 subscription.SetFixedTitle(false); |
222 | 176 |
223 int32_t version = 0; | 177 int32_t version = 0; |
224 entry = mParams.find(u"version"_str); | 178 entry = mParams.find(u"version"_str); |
225 if (entry) | 179 if (entry) |
226 version = entry->second.toInt<int32_t>(); | 180 version = lexical_cast<int32_t>(entry->second); |
227 subscription.SetDataRevision(version); | 181 subscription.SetDataRevision(version); |
228 | 182 |
229 int64_t expires = 0; | 183 int64_t expires = 0; |
230 entry = mParams.find(u"expires"_str); | 184 entry = mParams.find(u"expires"_str); |
231 if (entry) | 185 if (entry) |
232 expires = ParseExpires(entry->second); | 186 expires = ParseExpires(entry->second); |
233 | 187 |
234 FilterNotifier::SubscriptionChange( | 188 FilterNotifier::SubscriptionChange( |
235 FilterNotifier::Topic::SUBSCRIPTION_BEFORE_FILTERS_REPLACED, | 189 FilterNotifier::Topic::SUBSCRIPTION_BEFORE_FILTERS_REPLACED, |
236 subscription); | 190 subscription); |
(...skipping 25 matching lines...) Expand all Loading... |
262 return emptyString; | 216 return emptyString; |
263 } | 217 } |
264 | 218 |
265 const String& DownloadableSubscription_Parser::GetHomepage() const | 219 const String& DownloadableSubscription_Parser::GetHomepage() const |
266 { | 220 { |
267 auto entry = mParams.find(u"homepage"_str); | 221 auto entry = mParams.find(u"homepage"_str); |
268 if (entry) | 222 if (entry) |
269 return entry->second; | 223 return entry->second; |
270 return emptyString; | 224 return emptyString; |
271 } | 225 } |
| 226 |
| 227 ABP_NS_USING |
272 | 228 |
273 DownloadableSubscription::DownloadableSubscription(const String& id) | 229 DownloadableSubscription::DownloadableSubscription(const String& id) |
274 : Subscription(classType, id), mFixedTitle(false), mLastCheck(0), | 230 : Subscription(classType, id), mFixedTitle(false), mLastCheck(0), |
275 mHardExpiration(0), mSoftExpiration(0), mLastDownload(0), mLastSuccess(0), | 231 mHardExpiration(0), mSoftExpiration(0), mLastDownload(0), mLastSuccess(0), |
276 mErrorCount(0), mDataRevision(0), mDownloadCount(0) | 232 mErrorCount(0), mDataRevision(0), mDownloadCount(0) |
277 { | 233 { |
278 SetTitle(id); | 234 SetTitle(id); |
279 } | 235 } |
280 | 236 |
281 DownloadableSubscription_Parser* DownloadableSubscription::ParseDownload() | 237 DownloadableSubscription_Parser* DownloadableSubscription::ParseDownload() |
282 { | 238 { |
283 return new DownloadableSubscription_Parser(); | 239 return new DownloadableSubscription_Parser(); |
284 } | 240 } |
285 | 241 |
286 OwnedString DownloadableSubscription::Serialize() const | 242 OwnedString DownloadableSubscription::Serialize() const |
287 { | 243 { |
288 OwnedString result(Subscription::Serialize()); | 244 OwnedString result(Subscription::Serialize()); |
289 if (mFixedTitle) | 245 if (mFixedTitle) |
290 result.append(u"fixedTitle=true\n"_str); | 246 result.append(ABP_TEXT("fixedTitle=true\n"_str)); |
291 if (!mHomepage.empty()) | 247 if (!mHomepage.empty()) |
292 { | 248 { |
293 result.append(u"homepage="_str); | 249 result.append(ABP_TEXT("homepage="_str)); |
294 result.append(mHomepage); | 250 result.append(mHomepage); |
295 result.append(u'\n'); | 251 result.append(ABP_TEXT('\n')); |
296 } | 252 } |
297 if (mLastCheck) | 253 if (mLastCheck) |
298 { | 254 { |
299 result.append(u"lastCheck="_str); | 255 result.append(ABP_TEXT("lastCheck="_str)); |
300 result.append(mLastCheck); | 256 result.append(mLastCheck); |
301 result.append(u'\n'); | 257 result.append(ABP_TEXT('\n')); |
302 } | 258 } |
303 if (mHardExpiration) | 259 if (mHardExpiration) |
304 { | 260 { |
305 result.append(u"expires="_str); | 261 result.append(ABP_TEXT("expires="_str)); |
306 result.append(mHardExpiration); | 262 result.append(mHardExpiration); |
307 result.append(u'\n'); | 263 result.append(ABP_TEXT('\n')); |
308 } | 264 } |
309 if (mSoftExpiration) | 265 if (mSoftExpiration) |
310 { | 266 { |
311 result.append(u"softExpiration="_str); | 267 result.append(ABP_TEXT("softExpiration="_str)); |
312 result.append(mSoftExpiration); | 268 result.append(mSoftExpiration); |
313 result.append(u'\n'); | 269 result.append(ABP_TEXT('\n')); |
314 } | 270 } |
315 if (mLastDownload) | 271 if (mLastDownload) |
316 { | 272 { |
317 result.append(u"lastDownload="_str); | 273 result.append(ABP_TEXT("lastDownload="_str)); |
318 result.append(mLastDownload); | 274 result.append(mLastDownload); |
319 result.append(u'\n'); | 275 result.append(ABP_TEXT('\n')); |
320 } | 276 } |
321 if (!mDownloadStatus.empty()) | 277 if (!mDownloadStatus.empty()) |
322 { | 278 { |
323 result.append(u"downloadStatus="_str); | 279 result.append(ABP_TEXT("downloadStatus="_str)); |
324 result.append(mDownloadStatus); | 280 result.append(mDownloadStatus); |
325 result.append(u'\n'); | 281 result.append(ABP_TEXT('\n')); |
326 } | 282 } |
327 if (mLastSuccess) | 283 if (mLastSuccess) |
328 { | 284 { |
329 result.append(u"lastSuccess="_str); | 285 result.append(ABP_TEXT("lastSuccess="_str)); |
330 result.append(mLastSuccess); | 286 result.append(mLastSuccess); |
331 result.append(u'\n'); | 287 result.append(ABP_TEXT('\n')); |
332 } | 288 } |
333 if (mErrorCount) | 289 if (mErrorCount) |
334 { | 290 { |
335 result.append(u"errors="_str); | 291 result.append(ABP_TEXT("errors="_str)); |
336 result.append(mErrorCount); | 292 result.append(mErrorCount); |
337 result.append(u'\n'); | 293 result.append(ABP_TEXT('\n')); |
338 } | 294 } |
339 if (mDataRevision) | 295 if (mDataRevision) |
340 { | 296 { |
341 result.append(u"version="_str); | 297 result.append(ABP_TEXT("version="_str)); |
342 result.append(mDataRevision); | 298 result.append(mDataRevision); |
343 result.append(u'\n'); | 299 result.append(ABP_TEXT('\n')); |
344 } | 300 } |
345 if (!mRequiredVersion.empty()) | 301 if (!mRequiredVersion.empty()) |
346 { | 302 { |
347 result.append(u"requiredVersion="_str); | 303 result.append(ABP_TEXT("requiredVersion="_str)); |
348 result.append(mRequiredVersion); | 304 result.append(mRequiredVersion); |
349 result.append(u'\n'); | 305 result.append(ABP_TEXT('\n')); |
350 } | 306 } |
351 if (mDownloadCount) | 307 if (mDownloadCount) |
352 { | 308 { |
353 result.append(u"downloadCount="_str); | 309 result.append(ABP_TEXT("downloadCount="_str)); |
354 result.append(mDownloadCount); | 310 result.append(mDownloadCount); |
355 result.append(u'\n'); | 311 result.append(ABP_TEXT('\n')); |
356 } | 312 } |
357 return result; | 313 return result; |
358 } | 314 } |
LEFT | RIGHT |