Hyperscan is a high-performance regular expression matching library from Intel. It is based on the X86 platform based on PCRE prototype development. While supporting most of the syntax of PCRE, Hyperscan adds specific syntax and working modes to ensure its usefulness in real-world network scenarios.
statichs_database_t*buildDatabase(constvector<constchar*>&expressions,constvector<unsigned>flags,constvector<unsigned>ids,unsignedintmode){hs_database_t*db;hs_compile_error_t*compileErr;hs_error_terr;Clockclock;clock.start();err=hs_compile_multi(expressions.data(),flags.data(),ids.data(),expressions.size(),mode,nullptr,&db,&compileErr);clock.stop();if(err!=HS_SUCCESS){if(compileErr->expression<0){// The error does not refer to a particular expression.
cerr<<"ERROR: "<<compileErr->message<<endl;}else{cerr<<"ERROR: Pattern '"<<expressions[compileErr->expression]<<"' failed compilation with error: "<<compileErr->message<<endl;}// As the compileErr pointer points to dynamically allocated memory, if
// we get an error, we must be sure to release it. This is not
// necessary when no error is detected.
hs_free_compile_error(compileErr);exit(-1);}//...
}
public:Benchmark(consths_database_t*streaming,consths_database_t*block):db_streaming(streaming),db_block(block),scratch(nullptr),matchCount(0){// Allocate enough scratch space to handle either streaming or block
// mode, so we only need the one scratch region.
hs_error_terr=hs_alloc_scratch(db_streaming,&scratch);if(err!=HS_SUCCESS){cerr<<"ERROR: could not allocate scratch space. Exiting."<<endl;exit(-1);}// This second call will increase the scratch size if more is required
// for block mode.
err=hs_alloc_scratch(db_block,&scratch);if(err!=HS_SUCCESS){cerr<<"ERROR: could not allocate scratch space. Exiting."<<endl;exit(-1);}}
4.3 匹配
4.3.1 BLOCK模式
1
2
3
4
5
6
7
8
9
10
11
12
13
// Scan each packet (in the ordering given in the PCAP file) through
// Hyperscan using the block-mode interface.
voidscanBlock(){for(size_ti=0;i!=packets.size();++i){conststd::string&pkt=packets[i];hs_error_terr=hs_scan(db_block,pkt.c_str(),pkt.length(),0,scratch,onMatch,&matchCount);if(err!=HS_SUCCESS){cerr<<"ERROR: Unable to scan packet. Exiting."<<endl;exit(-1);}}}