File indexing completed on 2024-11-17 04:55:16

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 BAD_FINGERPRINT_H_
0011 #define BAD_FINGERPRINT_H_
0012 
0013 #define UNUSED(x) ( (void)(x) )
0014 #include <string.h>
0015 #include <math.h>
0016 #include "./hash_set.h"
0017 
0018 #ifdef PERF_STATS
0019 #include <fstream>
0020 #endif
0021 
0022 class BadFingerprint {
0023  public:
0024   uint64_t GetHash() const {
0025     return 0;
0026   }
0027 
0028   ~BadFingerprint() {
0029     if (data) {
0030       delete[] data;
0031     }
0032   }
0033   explicit BadFingerprint(const char *data) {
0034     size_t len = strlen(data) + 1;
0035     this->data = new char[len];
0036     snprintf(this->data, len, "%s", data);
0037   }
0038 
0039   BadFingerprint(const BadFingerprint &rhs) {
0040     data = new char[strlen(rhs.data) + 1];
0041     memcpy(data, rhs.data, strlen(rhs.data) + 1);
0042   }
0043 
0044   BadFingerprint() : data(nullptr) {
0045   }
0046 
0047   bool operator==(const BadFingerprint &rhs) const {
0048     return !strcmp(data, rhs.data);
0049   }
0050 
0051   bool operator!=(const BadFingerprint &rhs) const {
0052     return !(*this == rhs);
0053   }
0054 
0055   // Nothing needs to be updated for multiple adds
0056   void Update(const BadFingerprint &) {}
0057 
0058   uint32_t Serialize(char *buffer) {
0059     if (buffer) {
0060       memcpy(buffer, data, strlen(data) + 1);
0061     }
0062     return static_cast<uint32_t>(strlen(data)) + 1;
0063   }
0064 
0065   uint32_t Deserialize(char *buffer, uint32_t bufferSize) {
0066     UNUSED(bufferSize);
0067     uint32_t len = static_cast<uint32_t>(strlen(buffer));
0068     data = new char[len + 1];
0069     memcpy(data, buffer, len + 1);
0070     return len + 1;
0071   }
0072 
0073   char *data;
0074 };
0075 
0076 class BadFingerprintsHashSet : public HashSet<BadFingerprint> {
0077  public:
0078   BadFingerprintsHashSet() : HashSet<BadFingerprint>(1, false) {
0079   }
0080 
0081   void generateHeader(const char *filename) {
0082 #ifndef PERF_STATS
0083     UNUSED(filename);
0084 #endif
0085 #ifdef PERF_STATS
0086     std::ofstream outFile;
0087     outFile.open(filename);
0088 
0089     outFile << "#pragma once\n";
0090     outFile << "/**\n  *\n  * Auto generated bad filters\n  */\n";
0091     outFile << "const char *badFingerprints[] = {\n";
0092     for (uint32_t bucket_index = 0; bucket_index < bucket_count_;
0093         bucket_index++) {
0094       HashItem<BadFingerprint> *hashItem = buckets_[bucket_index];
0095       while (hashItem) {
0096         BadFingerprint *badFingerprint = hashItem->hash_item_storage_;
0097         outFile << "\"" << badFingerprint->data << "\"," << std::endl;
0098         hashItem = hashItem->next_;
0099       }
0100     }
0101     outFile << "};\n" << std::endl;
0102     outFile << "const char *badSubstrings[] = {\"http\", \"www\" };"
0103       << std::endl;
0104     outFile.close();
0105     #endif
0106   }
0107 };
0108 
0109 #endif  // BAD_FINGERPRINT_H_