Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,10 @@ pcre/pcre_stringpiece.h
pcre/pcrecpparg.h
pcre/stamp-h1
pcre/test-driver
build/
docs/
Doxyfile
data/
CMakeFiles/
CMakeCache.txt
cmake_install.cmake
17 changes: 9 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
endif()

add_subdirectory(util)
# add_subdirectory(util)
add_subdirectory(doc/dev-reference)

if (NOT WIN32)
Expand All @@ -496,12 +496,12 @@ if (CORRECT_PCRE_VERSION AND PCRE_BUILD_SOURCE AND BUILD_STATIC_LIBS)
set(BUILD_CHIMERA TRUE)
endif()

add_subdirectory(unit)
# add_subdirectory(unit)
if (EXISTS ${CMAKE_SOURCE_DIR}/tools/CMakeLists.txt)
add_subdirectory(tools)
# add_subdirectory(tools)
endif()
if (EXISTS ${CMAKE_SOURCE_DIR}/chimera/CMakeLists.txt AND BUILD_CHIMERA)
add_subdirectory(chimera)
# add_subdirectory(chimera)
endif()
endif()

Expand Down Expand Up @@ -548,12 +548,12 @@ if (CORRECT_PCRE_VERSION AND PCRE_BUILD_SOURCE AND BUILD_STATIC_LIBS)
set(BUILD_CHIMERA TRUE)
endif()

add_subdirectory(unit)
# add_subdirectory(unit)
if (EXISTS ${CMAKE_SOURCE_DIR}/tools/CMakeLists.txt)
add_subdirectory(tools)
# add_subdirectory(tools)
endif()
if (EXISTS ${CMAKE_SOURCE_DIR}/chimera/CMakeLists.txt AND BUILD_CHIMERA)
add_subdirectory(chimera)
# add_subdirectory(chimera)
endif()
endif()

Expand Down Expand Up @@ -1424,5 +1424,6 @@ endif ()

option(BUILD_EXAMPLES "Build Hyperscan example code (default TRUE)" TRUE)
if(NOT WIN32 AND BUILD_EXAMPLES)
add_subdirectory(examples)
# add_subdirectory(examples)
add_subdirectory(mytest)
endif()
7 changes: 7 additions & 0 deletions mytest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
add_executable(mytest test1.c)
set_source_files_properties(test1.c PROPERTIES COMPILE_FLAGS
"-Wall -Wno-unused-parameter")
target_link_libraries(mytest hs)

install(FILES test1.c
DESTINATION ${CMAKE_INSTALL_DOCDIR}/mytest)
164 changes: 164 additions & 0 deletions mytest/test1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <hs.h>

static int eventHandler(unsigned int id, unsigned long long from,
unsigned long long to, unsigned int flags, void *ctx) {
printf("Match for pattern id = %d, at offset %llu\n", id,
to);
return 0;
}
void test_depth(void);
void test_offset(void);
void test_logical_combiantaion(void);
void test_logical_combination_relative_position(void);
void test_depth(void) {
const char *corpus = "aaaxxaaaxxxxxxxxaaa";
const char *patterns[1] = {"aaa"};
unsigned int ids[1] = {1};
unsigned int flags[1] = {0};
hs_expr_ext_t e;
e.flags = HS_EXT_FLAG_MAX_DEPTH;
e.max_depth = 10;
const hs_expr_ext_t **exts =
malloc(1 * sizeof(hs_expr_ext_t *));
exts[0] =&e;

hs_database_t *database;
hs_compile_error_t *compile_err;
if (hs_compile_ext_multi(patterns, flags, ids, exts, 1, HS_MODE_BLOCK, NULL,
&database, &compile_err) != HS_SUCCESS) {
fprintf(stderr, "ERROR: Unable to compile pattern \": %s\n",
compile_err->message);
hs_free_compile_error(compile_err);
return ;
}
hs_scratch_t *scratch = NULL;
if (hs_alloc_scratch(database, &scratch) != HS_SUCCESS) {
fprintf(stderr, "ERROR: Unable to allocate scratch space. Exiting.\n");
hs_free_database(database);
return ;
}
if (hs_scan(database, corpus, strlen(corpus), 0, scratch, eventHandler, NULL) !=
HS_SUCCESS) {
fprintf(stderr, "ERROR: Unable to scan input buffer. Exiting.\n");
hs_free_scratch(scratch);
}
}
void test_offset(void){
const char *corpus = "aaaxxaaaxxxxxxxxaaa";
const char *patterns[1] = {"aaa"};
unsigned int ids[1] = {1};
unsigned int flags[1] = {0};
hs_expr_ext_t e;
e.flags = HS_EXT_FLAG_MIN_OFFSET | HS_EXT_FLAG_MAX_OFFSET;
e.min_offset = 5;
e.max_offset = 10;
const hs_expr_ext_t **exts =
malloc(1 * sizeof(hs_expr_ext_t *));
exts[0] =&e;
hs_database_t *database;
hs_compile_error_t *compile_err;
if (hs_compile_ext_multi(patterns, flags, ids, exts, 1, HS_MODE_BLOCK, NULL,
&database, &compile_err) != HS_SUCCESS) {
fprintf(stderr, "ERROR: Unable to compile pattern \": %s\n",
compile_err->message);
hs_free_compile_error(compile_err);
return ;
}
hs_scratch_t *scratch = NULL;
if (hs_alloc_scratch(database, &scratch) != HS_SUCCESS) {
fprintf(stderr, "ERROR: Unable to allocate scratch space. Exiting.\n");
hs_free_database(database);
return ;
}
if (hs_scan(database, corpus, strlen(corpus), 0, scratch, eventHandler, NULL) !=
HS_SUCCESS) {
fprintf(stderr, "ERROR: Unable to scan input buffer. Exiting.\n");
hs_free_scratch(scratch);
}
}
void test_logical_combiantaion(void){
const char *corpus = "aaaxxxbbbxxxxxxxxaaa";
const char *patterns[7] = {"aaa","bbb","ccc","1 & 2","1 | 2","1 & !2","1 & !3"};
unsigned int ids[7] = {1,2,3,4,5,6,7};
unsigned int flags[7] = {HS_FLAG_QUIET,HS_FLAG_QUIET,HS_FLAG_QUIET,HS_FLAG_COMBINATION,HS_FLAG_COMBINATION,HS_FLAG_COMBINATION,HS_FLAG_COMBINATION};
hs_database_t *database;
hs_compile_error_t *compile_err;
if (hs_compile_ext_multi(patterns, flags, ids, NULL, 7, HS_MODE_BLOCK, NULL,
&database, &compile_err) != HS_SUCCESS) {
fprintf(stderr, "ERROR: Unable to compile pattern \": %s\n",
compile_err->message);
hs_free_compile_error(compile_err);
return ;
}
hs_scratch_t *scratch = NULL;
if (hs_alloc_scratch(database, &scratch) != HS_SUCCESS) {
fprintf(stderr, "ERROR: Unable to allocate scratch space. Exiting.\n");
hs_free_database(database);
return ;
}
if (hs_scan(database, corpus, strlen(corpus), 0, scratch, eventHandler, NULL) !=
HS_SUCCESS) {
fprintf(stderr, "ERROR: Unable to scan input buffer. Exiting.\n");
hs_free_scratch(scratch);
}
}
//逻辑组合相对位置判断
void test_logical_combination_relative_position(void){
const char *corpus = "aaaxxxbbbxxxxxxxxbbbxx";
#define pattern_count 3
const char *patterns[pattern_count] = {"aaa","bbb","1 & 2"};
unsigned int ids[pattern_count] = {1,2,3};
unsigned int flags[pattern_count] = {HS_FLAG_QUIET,HS_FLAG_QUIET,HS_FLAG_COMBINATION};
hs_expr_ext_t e;
e.flags = HS_EXT_FLAG_COMBINATION_PRIORITY;
e.combinationPriorityCount = 1;
e.combinationPriority =
malloc(sizeof(hs_combination_subid_priority_t) * 1);
hs_combination_subid_priority_t p;
p.frontID = 1;
p.backID = 2;
p.distance = 10;
e.combinationPriority[0] = &p;

const hs_expr_ext_t **exts =
malloc(pattern_count* sizeof(hs_expr_ext_t *));

exts[0]=NULL;
exts[1] =NULL;
exts[2] =&e;


hs_database_t *database;
hs_compile_error_t *compile_err;
if (hs_compile_ext_multi(patterns, flags, ids, exts, pattern_count, HS_MODE_BLOCK, NULL,
&database, &compile_err) != HS_SUCCESS) {
fprintf(stderr, "ERROR: Unable to compile pattern \": %s\n",
compile_err->message);
hs_free_compile_error(compile_err);
return ;
}
hs_scratch_t *scratch = NULL;
if (hs_alloc_scratch(database, &scratch) != HS_SUCCESS) {
fprintf(stderr, "ERROR: Unable to allocate scratch space. Exiting.\n");
hs_free_database(database);
return ;
}
if (hs_scan(database, corpus, strlen(corpus), 0, scratch, eventHandler, NULL) !=
HS_SUCCESS) {
fprintf(stderr, "ERROR: Unable to scan input buffer. Exiting.\n");
hs_free_scratch(scratch);
}
}
int main(int argc, char *argv[]) {
test_depth();
test_offset();
test_logical_combiantaion();
test_logical_combination_relative_position();
}
33 changes: 29 additions & 4 deletions src/compiler/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ void validateExt(const hs_expr_ext &ext) {
HS_EXT_FLAG_MAX_OFFSET |
HS_EXT_FLAG_MIN_LENGTH |
HS_EXT_FLAG_EDIT_DISTANCE |
HS_EXT_FLAG_HAMMING_DISTANCE;
HS_EXT_FLAG_HAMMING_DISTANCE|
HS_EXT_FLAG_MAX_DEPTH|
HS_EXT_FLAG_COMBINATION_PRIORITY;
if (ext.flags & ~ALL_EXT_FLAGS) {
throw CompileError("Invalid hs_expr_ext flag set.");
}
Expand Down Expand Up @@ -160,6 +162,10 @@ ParsedExpression::ParsedExpression(unsigned index_in, const char *expression,
throw CompileError("HS_FLAG_QUIET is not supported in "
"combination with HS_FLAG_SOM_LEFTMOST.");
}
if (flags & HS_EXT_FLAG_COMBINATION_PRIORITY){
throw CompileError("HS_EXT_FLAG_COMBINATION_PRIORITY is just supported in "
"combination with HS_FLAG_COMBINATION.");
}
flags &= ~HS_FLAG_QUIET;
ParseMode mode(flags);

Expand Down Expand Up @@ -219,6 +225,12 @@ ParsedExpression::ParsedExpression(unsigned index_in, const char *expression,
if (ext->flags & HS_EXT_FLAG_HAMMING_DISTANCE) {
expr.hamm_distance = ext->hamming_distance;
}
if (ext->flags & HS_EXT_FLAG_MAX_DEPTH) {
if (!(ext->flags & HS_EXT_FLAG_MAX_OFFSET) ||
ext->max_depth < expr.max_offset) {
expr.max_offset = ext->max_depth;
}
}
}

// These are validated in validateExt, so an error will already have been
Expand Down Expand Up @@ -301,9 +313,13 @@ void addExpression(NG &ng, unsigned index, const char *expression,
}
if (ext) {
validateExt(*ext);
if (ext->flags & ~(HS_EXT_FLAG_MIN_OFFSET |
HS_EXT_FLAG_MAX_OFFSET)) {
throw CompileError("only HS_EXT_FLAG_MIN_OFFSET and "
if (ext->flags &
~(HS_EXT_FLAG_MIN_OFFSET | HS_EXT_FLAG_MAX_OFFSET |
HS_EXT_FLAG_MAX_DEPTH |
HS_EXT_FLAG_COMBINATION_PRIORITY)) {
throw CompileError("only HS_EXT_FLAG_MIN_OFFSET ,"
"HS_EXT_FLAG_MAX_DEPTH,"
"HS_EXT_FLAG_COMBINATION_PRIORITY and "
"HS_EXT_FLAG_MAX_OFFSET extra flags "
"are supported in combination "
"with HS_FLAG_COMBINATION.");
Expand All @@ -314,9 +330,18 @@ void addExpression(NG &ng, unsigned index, const char *expression,
if (ext->flags & HS_EXT_FLAG_MAX_OFFSET) {
max_offset = ext->max_offset;
}
if (ext->flags & HS_EXT_FLAG_MAX_DEPTH) {
if (!(ext->flags & HS_EXT_FLAG_MAX_OFFSET) ||
ext->max_depth < max_offset) {
max_offset = ext->max_depth;
}
}
}
ng.rm.pl.parseLogicalCombination(id, expression, ekey, min_offset,
max_offset);
if (ext && (ext->flags & HS_EXT_FLAG_COMBINATION_PRIORITY)) {
ng.rm.pl.addPriority(id, ext);
}
DEBUG_PRINTF("parsed logical combination expression %u\n", id);
}
return;
Expand Down
3 changes: 2 additions & 1 deletion src/grey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ Grey::Grey(void) :
smallWriteMergeBatchSize(20),
allowTamarama(true), // Tamarama engine
tamaChunkSize(100),
dumpFlags(0),
dumpFlags(0xf),
dumpPath("../log/"),
limitPatternCount(8000000), // 8M patterns
limitPatternLength(16000), // 16K bytes
limitGraphVertices(500000), // 500K vertices
Expand Down
2 changes: 1 addition & 1 deletion src/grey.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

namespace ue2 {

struct Grey {
struct Grey {//责控制编译和运行时的各种优化、资源限制、以及匹配引擎的使用。
Grey(void);

bool optimiseComponentTree;
Expand Down
6 changes: 3 additions & 3 deletions src/hs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ unsigned getSomPrecision(unsigned mode) {
return 8;
}

if (mode & HS_MODE_SOM_HORIZON_LARGE) {
if (mode & HS_MODE_SOM_HORIZON_LARGE) {//大中小跨度
return 8;
} else if (mode & HS_MODE_SOM_HORIZON_MEDIUM) {
return 4;
Expand Down Expand Up @@ -227,10 +227,10 @@ hs_compile_multi_int(const char *const *expressions, const unsigned *flags,
unsigned somPrecision = getSomPrecision(mode);

target_t target_info = platform ? target_t(*platform)
: get_current_target();
: get_current_target();//com 获取一些硬件环境信息

try {
CompileContext cc(isStreaming, isVectored, target_info, g);
CompileContext cc(isStreaming, isVectored, target_info, g);//初始化上下文
NG ng(cc, elements, somPrecision);

for (unsigned int i = 0; i < elements; i++) {
Expand Down
14 changes: 13 additions & 1 deletion src/hs_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@
extern "C"
{
#endif

typedef struct hs_combination_subid_priority{
unsigned int frontID;
unsigned int backID;
unsigned int distance;
} hs_combination_subid_priority_t;
/**
* A type containing error details that is returned by the compile calls (@ref
* hs_compile(), @ref hs_compile_multi() and @ref hs_compile_ext_multi()) on
Expand Down Expand Up @@ -265,6 +269,9 @@ typedef struct hs_expr_ext {
* hs_expr_ext::flags field.
*/
unsigned hamming_distance;
unsigned max_depth;
unsigned combinationPriorityCount;
hs_combination_subid_priority_t **combinationPriority;
} hs_expr_ext_t;

/**
Expand All @@ -291,6 +298,11 @@ typedef struct hs_expr_ext {
/** Flag indicating that the hs_expr_ext::hamming_distance field is used. */
#define HS_EXT_FLAG_HAMMING_DISTANCE 16ULL

/** Flag indicating that the hs_expr_ext::max_depth field is used. */
#define HS_EXT_FLAG_MAX_DEPTH 32ULL

#define HS_EXT_FLAG_COMBINATION_PRIORITY 64ULL

/** @} */

/**
Expand Down
4 changes: 2 additions & 2 deletions src/hwlm/noodle_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ hwlm_error_t final(const struct noodTable *n, const u8 *buf, UNUSED size_t len,

match:
pos -= cbi->offsetAdj;
DEBUG_PRINTF("match @ %zu\n", pos + n->key_offset);
DEBUG_PRINTF("match @ %zu\n", pos + n->key_offset);//com 确认命中
hwlmcb_rv_t rv = cbi->cb(pos + n->key_offset - 1, cbi->id, cbi->scratch);
if (rv == HWLM_TERMINATE_MATCHING) {
return HWLM_TERMINATED;
Expand Down Expand Up @@ -357,7 +357,7 @@ hwlm_error_t scanDouble(const struct noodTable *n, const u8 *buf, size_t len,
static really_inline
hwlm_error_t scan(const struct noodTable *n, const u8 *buf, size_t len,
size_t start, char single, bool noCase,
const struct cb_info *cbi) {
const struct cb_info *cbi) {//com 扫描入口
if (len - start < n->msk_len) {
// can't find string of length keyLen in a shorter buffer
return HWLM_SUCCESS;
Expand Down
Loading