File indexing completed on 2024-11-24 04:54:33
0001 /* 0002 SPDX-License-Identifier: MPL-2.0 0003 */ 0004 0005 /* Copyright (c) 2015 Brian R. Bondy. Distributed under the MPL2 license. 0006 * This Source Code Form is subject to the terms of the Mozilla Public 0007 * License, v. 2.0. If a copy of the MPL was not distributed with this 0008 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 0009 0010 #ifndef AD_BLOCK_CLIENT_H_ 0011 #define AD_BLOCK_CLIENT_H_ 0012 0013 #include <string> 0014 #include <set> 0015 #include "./filter.h" 0016 0017 class CosmeticFilter; 0018 class BloomFilter; 0019 class BadFingerprintsHashSet; 0020 class NoFingerprintDomain; 0021 0022 template<class T> 0023 class HashSet; 0024 0025 class AdBlockClient { 0026 public: 0027 AdBlockClient(); 0028 ~AdBlockClient(); 0029 0030 void clear(); 0031 // bool parse(const char *input); 0032 bool parse(const char *input, bool preserveRules = false); 0033 bool matches(const char* input, 0034 FilterOption contextOption = FONoFilterOption, 0035 const char* contextDomain = nullptr, 0036 Filter** matchedFilter = nullptr, 0037 Filter** matchedExceptionFilter = nullptr); 0038 bool findMatchingFilters(const char *input, 0039 FilterOption contextOption, 0040 const char *contextDomain, 0041 Filter **matchingFilter, 0042 Filter **matchingExceptionFilter); 0043 void addTag(const std::string &tag); 0044 void removeTag(const std::string &tag); 0045 bool tagExists(const std::string &tag) const; 0046 // Serializes a the parsed data and bloom filter data into a single buffer. 0047 // The returned buffer should be deleted. 0048 char * serialize(int *size, 0049 bool ignoreCosmeticFilters = true, 0050 bool ignoreHtmlFilters = true); 0051 // Deserializes the buffer, a size is not needed since a serialized. 0052 // buffer is self described 0053 bool deserialize(char *buffer); 0054 0055 void enableBadFingerprintDetection(); 0056 const char * getDeserializedBuffer() { 0057 return deserializedBuffer; 0058 } 0059 0060 static bool getFingerprint(char *buffer, const char *input); 0061 static bool getFingerprint(char *buffer, const Filter &f); 0062 0063 Filter *filters; 0064 Filter *cosmeticFilters; 0065 Filter *htmlFilters; 0066 Filter *exceptionFilters; 0067 Filter *noFingerprintFilters; 0068 Filter *noFingerprintExceptionFilters; 0069 Filter *noFingerprintDomainOnlyFilters; 0070 Filter *noFingerprintAntiDomainOnlyFilters; 0071 Filter *noFingerprintDomainOnlyExceptionFilters; 0072 Filter *noFingerprintAntiDomainOnlyExceptionFilters; 0073 0074 int numFilters; 0075 int numCosmeticFilters; 0076 int numHtmlFilters; 0077 int numExceptionFilters; 0078 int numNoFingerprintFilters; 0079 int numNoFingerprintExceptionFilters; 0080 int numNoFingerprintDomainOnlyFilters; 0081 int numNoFingerprintAntiDomainOnlyFilters; 0082 int numNoFingerprintDomainOnlyExceptionFilters; 0083 int numNoFingerprintAntiDomainOnlyExceptionFilters; 0084 int numHostAnchoredFilters; 0085 int numHostAnchoredExceptionFilters; 0086 0087 BloomFilter *bloomFilter; 0088 BloomFilter *exceptionBloomFilter; 0089 HashSet<Filter> *hostAnchoredHashSet; 0090 HashSet<Filter> *hostAnchoredExceptionHashSet; 0091 HashSet<NoFingerprintDomain> *noFingerprintDomainHashSet; 0092 HashSet<NoFingerprintDomain> *noFingerprintAntiDomainHashSet; 0093 HashSet<NoFingerprintDomain> *noFingerprintDomainExceptionHashSet; 0094 HashSet<NoFingerprintDomain> *noFingerprintAntiDomainExceptionHashSet; 0095 0096 // Used only in the perf program to create a list of bad fingerprints 0097 BadFingerprintsHashSet *badFingerprintsHashSet; 0098 0099 // Stats kept for matching 0100 unsigned int numFalsePositives; 0101 unsigned int numExceptionFalsePositives; 0102 unsigned int numBloomFilterSaves; 0103 unsigned int numExceptionBloomFilterSaves; 0104 unsigned int numHashSetSaves; 0105 unsigned int numExceptionHashSetSaves; 0106 0107 static const int kFingerprintSize; 0108 0109 protected: 0110 // Determines if a passed in array of filter pointers matches for any of 0111 // the input 0112 bool hasMatchingFilters(Filter *filter, int numFilters, const char *input, 0113 int inputLen, FilterOption contextOption, const char *contextDomain, 0114 BloomFilter *inputBloomFilter, const char *inputHost, int inputHostLen, 0115 Filter **matchingFilter = nullptr); 0116 bool isHostAnchoredHashSetMiss(const char *input, int inputLen, 0117 HashSet<Filter> *hashSet, 0118 const char *inputHost, 0119 int inputHostLen, 0120 FilterOption contextOption, 0121 const char *contextDomain, 0122 Filter **foundFilter = nullptr); 0123 0124 void initBloomFilter(BloomFilter**, const char *buffer, int len); 0125 template<class T> 0126 bool initHashSet(HashSet<T>**, char *buffer, int len); 0127 char *deserializedBuffer; 0128 std::set<std::string> tags; 0129 }; 0130 0131 extern std::set<std::string> unknownOptions; 0132 extern const char *separatorCharacters; 0133 void parseFilter(const char *input, const char *end, Filter *f, 0134 BloomFilter *bloomFilter = nullptr, 0135 BloomFilter *exceptionBloomFilter = nullptr, 0136 HashSet<Filter> *hostAnchoredHashSet = nullptr, 0137 HashSet<Filter> *hostAnchoredExceptionHashSet = nullptr, 0138 HashSet<CosmeticFilter> *simpleCosmeticFilters = nullptr, 0139 bool preserveRules = false); 0140 void parseFilter(const char *input, Filter *f, 0141 BloomFilter *bloomFilter = nullptr, 0142 BloomFilter *exceptionBloomFilter = nullptr, 0143 HashSet<Filter> *hostAnchoredHashSet = nullptr, 0144 HashSet<Filter> *hostAnchoredExceptionHashSet = nullptr, 0145 HashSet<CosmeticFilter> *simpleCosmeticFilters = nullptr, 0146 bool preserveRules = false); 0147 bool isSeparatorChar(char c); 0148 int findFirstSeparatorChar(const char *input, const char *end); 0149 0150 #endif // AD_BLOCK_CLIENT_H_