This repository has been archived by the owner. It is now read-only.
Add support for conditional matching filters based on tags #192
Merged
+297
−80
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.
| @@ -620,10 +620,13 @@ bool AdBlockClient::hasMatchingFilters(Filter *filter, int numFilters, | ||
| for (int i = 0; i < numFilters; i++) { | ||
| if (filter->matches(input, inputLen, contextOption, | ||
| contextDomain, inputBloomFilter, inputHost, inputHostLen)) { | ||
| if (matchingFilter) { | ||
| *matchingFilter = filter; | ||
| if (filter->tagLen == 0 || | ||
| tagExists(std::string(filter->tag, filter->tagLen))) { | ||
| if (matchingFilter) { | ||
| *matchingFilter = filter; | ||
| } | ||
| return true; | ||
| } | ||
| return true; | ||
| } | ||
| filter++; | ||
| } | ||
| @@ -685,13 +688,13 @@ bool isNoFingerprintDomainHashSetMiss(HashSet<NoFingerprintDomain> *hashSet, | ||
| static_cast<int>(host + hostLen - start))); | ||
| } | ||
|
|
||
| bool isHostAnchoredHashSetMiss(const char *input, int inputLen, | ||
| bool AdBlockClient::isHostAnchoredHashSetMiss(const char *input, int inputLen, | ||
| HashSet<Filter> *hashSet, | ||
| const char *inputHost, | ||
| int inputHostLen, | ||
| FilterOption contextOption, | ||
| const char *contextDomain, | ||
| Filter **foundFilter = nullptr) { | ||
| Filter **foundFilter) { | ||
| if (!hashSet) { | ||
| return false; | ||
| } | ||
| @@ -712,10 +715,13 @@ bool isHostAnchoredHashSetMiss(const char *input, int inputLen, | ||
| nullptr, start, inputHostLen - (start - inputHost))); | ||
| if (filter && filter->matches(input, inputLen, | ||
| contextOption, contextDomain)) { | ||
| if (foundFilter) { | ||
| *foundFilter = filter; | ||
| if (filter->tagLen == 0 || | ||
| tagExists(std::string(filter->tag, filter->tagLen))) { | ||
| if (foundFilter) { | ||
| *foundFilter = filter; | ||
| } | ||
| return false; | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
| start--; | ||
| @@ -728,8 +734,14 @@ bool isHostAnchoredHashSetMiss(const char *input, int inputLen, | ||
| return true; | ||
| } | ||
| bool result = !filter->matches(input, inputLen, contextOption, contextDomain); | ||
| if (!result && foundFilter) { | ||
| *foundFilter = filter; | ||
| if (!result) { | ||
| if (filter->tagLen > 0 && | ||
| !tagExists(std::string(filter->tag, filter->tagLen))) { | ||
| return true; | ||
| } | ||
| if (foundFilter) { | ||
| *foundFilter = filter; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| @@ -1451,6 +1463,23 @@ bool AdBlockClient::parse(const char *input, bool preserveRules) { | ||
| return true; | ||
| } | ||
|
|
||
| void AdBlockClient::addTag(const std::string &tag) { | ||
| if (tags.find(tag) == tags.end()) { | ||
| tags.insert(tag); | ||
| } | ||
| } | ||
|
|
||
| void AdBlockClient::removeTag(const std::string &tag) { | ||
| auto it = tags.find(tag); | ||
| if (it != tags.end()) { | ||
| tags.erase(it); | ||
| } | ||
| } | ||
|
|
||
| bool AdBlockClient::tagExists(const std::string &tag) const { | ||
| return tags.find(tag) != tags.end(); | ||
| } | ||
|
|
||
| // Fills the specified buffer if specified, returns the number of characters | ||
| // written or needed | ||
| int serializeFilters(char * buffer, size_t bufferSizeAvail, | ||
| @@ -1476,6 +1505,15 @@ int serializeFilters(char * buffer, size_t bufferSizeAvail, | ||
| } | ||
| bufferSize++; | ||
|
|
||
| if (f->tagLen > 0) { | ||
| if (buffer) { | ||
| buffer[bufferSize] = '#'; | ||
bbondy
Author
Member
|
||
| memcpy(buffer + bufferSize + 1, f->tag, f->tagLen); | ||
| buffer[bufferSize + 1 + f->tagLen] = ','; | ||
| } | ||
| bufferSize += f->tagLen + 2; | ||
| } | ||
|
|
||
| if (f->domainList) { | ||
| if (buffer) { | ||
| snprintf(buffer + bufferSize, bufferSizeAvail, "%s", f->domainList); | ||
| @@ -1700,6 +1738,21 @@ int deserializeFilters(char *buffer, Filter *f, int numFilters) { | ||
| } | ||
| pos++; | ||
|
|
||
| // If the domain section starts with a # then we're in a tag | ||
| // block. | ||
| if (buffer[pos] == '#') { | ||
| pos++; | ||
| f->tag = buffer + pos; | ||
| f->tagLen = 0; | ||
| while (buffer[pos + f->tagLen] != '\0') { | ||
| if (buffer[pos + f->tagLen] == ',') { | ||
| pos += f->tagLen + 1; | ||
| break; | ||
| } | ||
| f->tagLen++; | ||
| } | ||
| } | ||
|
|
||
| if (*(buffer + pos) == '\0') { | ||
| f->domainList = nullptr; | ||
| } else { | ||
| @@ -1721,6 +1774,7 @@ int deserializeFilters(char *buffer, Filter *f, int numFilters) { | ||
| } | ||
|
|
||
| bool AdBlockClient::deserialize(char *buffer) { | ||
| clear(); | ||
| deserializedBuffer = buffer; | ||
| int bloomFilterSize = 0, exceptionBloomFilterSize = 0, | ||
| hostAnchoredHashSetSize = 0, hostAnchoredExceptionHashSetSize = 0, | ||
Oops, something went wrong.
ProTip!
Use n and p to navigate between commits in a pull request.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Is it safe to assume the buffer has the space available for this additional data? Wasn't quite sure how that side of things works.