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