ahoсorasick. Code review. Part 2. (#1236)

Simplified the process of adding lines to AC_AUTOMATA_t.
Use the ndpi_string_to_automa() function to add patterns with domain names.
For other cases can use ndpi_add_string_value_to_automa().

ac_automata_feature(ac_automa, AC_FEATURE_LC) allows adding
and compare data in a case insensitive manner. For mandatory pattern comparison
from the end of the line, the "ac_pattern.rep.at_end=1" flag is used.
This eliminated unnecessary conversions to lowercase and adding "$" for
end-of-line matching in domain name patterns.

ac_match_handler() has been renamed ac_domain_match_handler() and has been greatly simplified.
ac_domain_match_handler() looks for the template with the highest domain level.
For special cases it is possible to manually specify the domain level.
Added test for checking ambiguous domain names like:
 - short.weixin.qq.com is QQ, not Wechat
 - instagram.faae1-1.fna.fbcdn.net is Instagram, not Facebook

If you specify a NULL handler when creating the AC_AUTOMATA_t structure,
then a pattern with the maximum length that satisfies the search conditions will be found
(exact match, from the beginning of the string, from the end of the string, or a substring).

Added debugging for ac_automata_search.
To do this, you need to enable debugging globally using ac_automata_enable_debug(1) and
enable debugging in the AC_AUTOMATA_t structure using ac_automata_name("name", AC_FEATURE_DEBUG).
The search will display "name" and a list of matching patterns.
Running "AHO_DEBUG=1 ndpiReader ..." will show the lines that were searched for templates
and which templates were found.

The ac_automata_dump() prototype has been changed. Now it outputs data to a file.
If it is specified as NULL, then the output will be directed to stdout.
If you need to get data as a string, then use open_memstream().

Added the ability to run individual tests via the do.sh script
This commit is contained in:
Vitaly Lavrov
2021-07-12 17:39:43 +02:00
committed by GitHub
parent 78b1295dc1
commit c418b7110b
10 changed files with 341 additions and 295 deletions
+21 -11
View File
@@ -54,8 +54,11 @@ typedef char AC_ALPHABET_t;
**/
typedef struct {
uint32_t number; /* Often used to store procotolId */
uint16_t breed,
category:14,from_start:1,at_end:1;
uint16_t breed, category;
uint16_t level, /* Domain level for comparison */
from_start:1, /* match from start of string */
at_end:1, /* match at end of string */
dot:1; /* is domain name */
} AC_REP_t;
/* AC_PATTERN_t:
@@ -103,8 +106,10 @@ typedef struct {
typedef struct
{
AC_PATTERN_t *matched[4]; /* for ac_automata_exact_match() */
AC_PATTERN_t *patterns; /* Array of matched pattern */
AC_PATTERN_t *matched[4], /* for ac_automata_exact_match() */
*last; /* for callback */
AC_PATTERN_t *patterns; /* Array of matched pattern */
unsigned int match_map; /* Matched patterns (bitmap) */
unsigned int position; /* The end position of matching pattern(s) in the text */
unsigned short int match_num; /* Number of matched patterns */
unsigned short int match_counter; /* Counter of found matches */
@@ -120,7 +125,7 @@ typedef struct
AC_MATCH_t match;
AC_ALPHABET_t * astring; /* String of alphabets */
unsigned short int length, /* Length of string */
ignore_case;
option; /* AC_FEATURE_LC | AC_FEATURE_DEBUG */;
} AC_TEXT_t;
@@ -218,7 +223,7 @@ typedef struct
* means not finalized (is open). after finalizing automata you can not
* add pattern to automata anymore. */
unsigned short automata_open,
to_lc:1, no_root_range:1; /* lowercase match */
to_lc:1, no_root_range:1,debug:1; /* lowercase match */
/* Statistic Variables */
unsigned long total_patterns; /* Total patterns in the automata */
@@ -229,17 +234,20 @@ typedef struct
int id; /* node id */
int add_to_range; /* for convert to range */
int n_oc,n_range,n_find; /* statistics */
char name[32]; /* if debug != 0 */
} AC_AUTOMATA_t;
typedef AC_ERROR_t (*NODE_CALLBACK_f)(AC_AUTOMATA_t *, AC_NODE_t *,int idx, void *);
typedef void (*ALPHA_CALLBACK_f)(AC_AUTOMATA_t *, AC_NODE_t *,AC_NODE_t *,int ,void *);
#define AC_FEATURE_LC 1
#define AC_FEATURE_NO_ROOT_RANGE 2
#define AC_FEATURE_DEBUG 1
#define AC_FEATURE_LC 2
#define AC_FEATURE_NO_ROOT_RANGE 4
AC_AUTOMATA_t * ac_automata_init (MATCH_CALLBACK_f mc);
AC_ERROR_t ac_automata_feature (AC_AUTOMATA_t * thiz, unsigned int feature);
AC_ERROR_t ac_automata_name (AC_AUTOMATA_t * thiz, char *name, int debug);
AC_ERROR_t ac_automata_add (AC_AUTOMATA_t * thiz, AC_PATTERN_t * str);
AC_ERROR_t ac_automata_finalize (AC_AUTOMATA_t * thiz);
AC_ERROR_t ac_automata_walk (AC_AUTOMATA_t * thiz, NODE_CALLBACK_f node_cb,
@@ -252,7 +260,9 @@ int ac_automata_exact_match(AC_PATTERNS_t *mp,int pos, AC_TEXT_t *);
void ac_automata_clean (AC_AUTOMATA_t * thiz);
void ac_automata_release (AC_AUTOMATA_t * thiz, uint8_t free_pattern);
#ifndef __KERNEL__
void ac_automata_dump (AC_AUTOMATA_t * thiz,
char *buf, size_t bufsize, char repcast);
/* Global debug control. */
void ac_automata_enable_debug (int debug);
/* See man open_memstream() for get result as string */
void ac_automata_dump (AC_AUTOMATA_t * thiz, FILE *);
#endif
#endif
#endif