Debian Codegraph @

src/ListContainer.hpp

raw
1 // ListContainer class - for item and phrase lists
2
3 // For all support, instructions and copyright go to:
4 // http://e2guardian.org/
5 // Released under the GPL v2, with the OpenSSL exception described in the README file.
6
7 #ifndef __HPP_LISTCONTAINER
8 #define __HPP_LISTCONTAINER
9
10 // INLCUDES
11
12 #include <cstdint>
13 #include <vector>
14 #include <deque>
15 #include <map>
16 #include <list>
17 #include <string>
18 #include "String.hpp"
19 #include "RegExp.hpp"
20 #include "IPList.hpp"
21
22 // DECLARATIONS
23
24 // time limit information
25 struct TimeLimit {
26 unsigned int sthour, stmin, endhour, endmin;
27 String days, timetag;
28 };
29
30
31 // class for linking IPs to filter groups, complete with comparison operators
32 // allowing standard C++ sort to work
33 class ipmap
34 {
35 public:
36 ipmap(uint32_t a, String g)
37 {
38 addr = a;
39 group = g;
40 };
41 uint32_t addr;
42 String group;
43 int operator<(const ipmap &a) const
44 {
45 return addr < a.addr;
46 };
47 int operator<(const uint32_t &a) const
48 {
49 return addr < a;
50 };
51 int operator==(const uint32_t &a) const
52 {
53 return a == addr;
54 };
55 };
56
57 class datamap
58 {
59 public:
60 datamap(String &k, String &g){
61 key = k;
62 group = g;
63 };
64 String key;
65 String group;
66 int operator<(const datamap &a) const
67 {
68 if (key.compare(a.key) < 0)
69 return 1;
70 return 0;
71 };
72 int operator==(const String &a) const
73 {
74 if( key.compare(a) == 0) return 1;
75 return 0;
76 };
77 };
78
79 // structs linking subnets and IP ranges to filter groups
80 //struct subnetstruct {
81 //uint32_t maskedaddr;
82 //uint32_t mask;
83 //String group;
84 //};
85
86 class rangestruct {
87 public:
88 uint32_t startaddr;
89 uint32_t endaddr;
90 String group;
91 int operator<(const rangestruct &a) const
92 {
93 if (startaddr < a.startaddr)
94 return 1;
95 return 0;
96 }
97 };
98
99 time_t getFileDate(const char *filename);
100 size_t getFileLength(const char *filename);
101
102 class ListContainer
103 {
104 public:
105 std::vector<int> combilist;
106 bool is_iplist = false;
107 bool is_timelist = false;
108 bool is_map = false;
109 int refcount = 0;
110 bool parent = false;
111 time_t filedate;
112 bool used = false;
113 String bannedpfile;
114 String exceptionpfile;
115 String weightedpfile;
116 int naughtynesslimit = 0; // used for phrase lists only
117 time_t bannedpfiledate;
118 time_t exceptionpfiledate;
119 time_t weightedpfiledate;
120 String sourcefile; // used for non-phrase lists only
121 String category;
122 //String lastcategory;
123 std::vector<int> morelists; // has to be non private as reg exp compiler needs to access these
124 bool read_errors = false;
125
126
127 ListContainer();
128 ~ListContainer();
129
130 void reset();
131
132 bool readPhraseList(const char *filename, bool isexception, int catindex = -1, int timeindex = -1, bool incref = true, int nlimit=0);
133 bool ifsreadItemList(const char *filename,std::istream *input, String basedir, const char *list_pwd, int len, bool checkendstring, const char *endstring, bool do_includes, bool startswith, int filters);
134 bool ifsReadSortItemList(std::ifstream *input, String basedir, const char *list_pwd, bool checkendstring, const char *endstring, bool do_includes, bool startswith, int filters, const char *filename);
135 bool readItemList(const char *filename, const char *pwd, bool startswith, int filters, bool isip = false, bool istime = false, bool ismap = false);
136 bool readStdinItemList(bool startswith, int filters, const char *filename);
137 bool inList(const char *string, String &lastcategory);
138 bool inListEndsWith(const char *string, String &lastcategory);
139 bool inListStartsWith(const char *string, String &lastcategory);
140
141 bool findInList(const char *string, String &lastcategory, String &match, String &result);
142 void dump_data();
143
144 bool findEndsWith(const char *string, String &lastcategory, String &match);
145 bool findStartsWith(const char *string, String &lastcategory, String &match);
146 char *findStartsWithPartial(const char *string, String &lastcategory, String &match);
147 String searchIPMap(int a, int s, const uint32_t &ip);
148 String searchDataMap(int a, int s, const String &key);
149 // String inSubnetMap(const uint32_t &ip);
150 String inIPRangeMap(const uint32_t &ip);
151
152 int getListLength()
153 {
154 return items;
155 }
156 std::string getItemAtInt(int index);
157
158 int getWeightAt(unsigned int index);
159 int getTypeAt(unsigned int index);
160
161 void doSort(const bool startsWith);
162
163 //bool createCacheFile();
164 bool makeGraph(bool fqs);
165
166 bool previousUseItem(const char *filename, bool startswith, int filters);
167 bool upToDate();
168
169 String getListCategoryAt(unsigned int index, unsigned int *catindex = NULL);
170 String getListCategoryAtD(unsigned int index);
171
172 void graphSearch(std::map<std::string, std::pair<unsigned int, int> > &result, char *doc, off_t len);
173
174
175 bool isNow(int index = -1); // used for normal and phrase lists
176 bool isNowInTimelist(); // used for timelists
177 bool isNow(TimeLimit &tl);
178 bool checkTimeAt(unsigned int index);
179 bool checkTimeAtD(int index);
180
181 //bool blanketblock;
182 //bool blanket_ip_block;
183 //bool blanketsslblock;
184 //bool blanketssl_ip_block;
185
186 private:
187 bool sourceisexception = false;
188 bool sourcestartswith = false;
189 int sourcefilters = 0;
190 char *data = nullptr;
191
192 // Format of the data is each entry has 64 int values with format of:
193 // [letter][last letter flag][num links][from phrase][link0][link1]...
194
195 int *realgraphdata = nullptr;
196 int current_graphdata_size = 0;
197
198 #ifdef DEBUG_LOW
199 bool prolificroot = false;
200 int secondmaxchildnodes = 0;
201 #endif
202
203 int maxchildnodes = 0;
204 int graphitems = 0;
205 std::vector<unsigned int> slowgraph;
206 size_t data_length = 0;
207 size_t data_memory = 0;
208 long int items = 0;
209 bool isSW = false;
210 bool issorted = false;
211 bool graphused = false;
212 std::vector<size_t> list;
213 std::vector<size_t> lengthlist ;
214 std::vector<int> weight;
215 std::vector<int> itemtype; // 0=banned, 1=weighted, -1=exception
216 bool force_quick_search = false;
217
218 //time-limited lists - only items (sites, URLs), not phrases
219 TimeLimit listtimelimit;
220 bool istimelimited = false;
221 bool self_check();
222
223 //categorised lists - both phrases & items
224 std::vector<String> listcategory;
225 std::vector<int> categoryindex;
226
227 // set of time limits for phrase lists
228 std::vector<int> timelimitindex;
229 std::vector<TimeLimit> timelimits;
230
231 RegExp matchIP, matchSubnet, matchRange, matchCIDR;
232
233 //iplists
234 std::vector<uint32_t> iplist;
235 std::vector<ipl_rangestruct> iprangelist;
236 //std::list<ipl_subnetstruct> ipsubnetlist;
237 std::vector<ipmap> ipmaplist;
238 std::vector<rangestruct> ipmaprangelist;
239 // std::list<subnetstruct> ipmapsubnetlist;
240 //std::list<datamap> datamaplist;
241 std::vector<datamap> datamaplist;
242
243 //timelists
244 std::vector<TimeLimit> timelist;
245
246 bool readAnotherItemList(const char *filename, const char *list_pwd, bool startswith, int filters, int &list_no);
247
248 void readPhraseListHelper(String line, bool isexception, int catindex, int timeindex, int &nlimit);
249 void readPhraseListHelper2(String phrase, int type, int weighting, int catindex, int timeindex);
250 bool addToItemListPhrase(const char *s, size_t len, int type, int weighting, bool combi, int catindex, int timeindex);
251 void graphSizeSort(int l, int r, std::deque<size_t> *sizelist);
252 void graphAdd(String s, const int inx, int item);
253 int graphFindBranches(unsigned int pos);
254 void graphCopyNodePhrases(unsigned int pos);
255 int bmsearch(char *file, off_t fl, const std::string &s);
256 bool readProcessedItemList(const char *filename, bool startswith, int filters);
257 void addToItemList(const char *s, size_t len);
258 void addToIPList(String &line);
259 void addToIPMap(String &line);
260 void addToDataMap(String &line);
261 bool addToTimeList(String &line);
262 int greaterThanEWF(const char *a, const char *b); // full match
263 int greaterThanEW(const char *a, const char *b); // partial ends with
264 int greaterThanSWF(const char *a, const char *b); // full match
265 int greaterThanSW(const char *a, const char *b); // partial starts with
266 int search(int (ListContainer::*comparitor)(const char *a, const char *b), int a, int s, const char *p);
267 bool increaseMemoryBy(size_t bytes);
268 //categorised & time-limited lists support
269 bool readTimeTag(String *tag, TimeLimit &tl);
270 bool readTimeBand(String &tag, TimeLimit &tl);
271 int getCategoryIndex(String *lcat);
272 bool inIPList(const std::string &ipstr ,String &match);
273 String getIPMapData(std::string &ip);
274 const char *hIPtoChar(uint32_t ip);
275 String inIPMap(const uint32_t &ip);
276 bool getMapData(String &key, String &match, String &result);
277 };
278
279 #endif