@@ -16,24 +16,58 @@ PatternMatching::PatternMatching(string path)
1616 }
1717}
1818
19- vector<Match*> PatternMatching::hasMatches (wstring str ) {
19+ vector<Match*> PatternMatching::hasMatches (vector< uint8_t > &content ) {
2020 vector<Match*> res;
21+ vector<vector<uint8_t >> patterns;
22+ vector<string> colors;
23+ vector<string> messages;
2124 for (auto & e : this ->jconfig [" PatternMatching" ]) {
22- wstring tmp = str;
23- unsigned long pos = 0 ;
24- try {
25- string sregex = e[" regex" ].get <string>();
26- wstring wsregex = std::wstring_convert<std::codecvt_utf8<wchar_t >>().from_bytes (sregex);
27- std::wregex re (wsregex);
28- std::wsmatch match;
29- while (regex_search (tmp, match, re)) {
30- unsigned long len = match.str (0 ).size ();
31- res.push_back (new Match (e[" color" ].get <string>(), e[" message" ].get <string>(), match.position (0 ) + pos, len));
32- tmp = match.suffix ();
33- pos += match.position (0 ) + len;
25+ if (e.contains (" string" )) {
26+ vector<uint8_t > bytes;
27+ string str = e[" string" ].get <string>();
28+ for (uint8_t byte : str) {
29+ bytes.push_back (byte);
30+ }
31+ patterns.push_back (bytes);
32+ colors.push_back (e[" color" ].get <string>());
33+ messages.push_back (e[" message" ].get <string>());
34+ } else if (e.contains (" bytes" )) {
35+ string strbytes = e[" bytes" ].get <string>();
36+ stringstream converter;
37+ vector<uint8_t > bytes;
38+ for (size_t i = 0 ; i < strbytes.length (); i+=2 ) {
39+ converter << std::hex << strbytes.substr (i,2 );
40+ short byte;
41+ converter >> byte;
42+ bytes.push_back (byte & 0xFF );
43+ converter.str (string ());
44+ converter.clear ();
45+ }
46+ patterns.push_back (bytes);
47+ colors.push_back (e[" color" ].get <string>());
48+ messages.push_back (e[" message" ].get <string>());
49+ }
50+ }
51+
52+ // Search patterns in content
53+ unsigned long pos = 0 ;
54+ uint8_t *content_ptr = content.data (); // Use pointers to be faster
55+ for (pos = 0 ; pos < content.size (); pos++) {
56+ for (size_t p = 0 ; p < patterns.size (); p++) {
57+ uint8_t *pattern_ptr = patterns[p].data ();
58+ size_t size = patterns[p].size ();
59+ if (content.size () - pos < size)
60+ continue ;
61+ size_t i;
62+ for (i = 0 ; i < size; i++) {
63+ if (content_ptr[pos+i] != pattern_ptr[i])
64+ break ;
65+ }
66+ bool found = (i == size);
67+ if ((found) && (res.size () < MAX_PATTERN_RESULTS)) {
68+ res.push_back (new Match (colors[p], messages[p], pos, size));
69+ pos+=size;
3470 }
35- } catch (std::regex_error& e) {
36- cerr << " Regex syntax error:" << endl << e.what () << endl;
3771 }
3872 }
3973 return res;
0 commit comments