{"id":1664,"date":"2024-11-06T14:57:25","date_gmt":"2024-11-06T06:57:25","guid":{"rendered":"https:\/\/www.madbull.site\/?p=1664"},"modified":"2024-11-06T15:14:51","modified_gmt":"2024-11-06T07:14:51","slug":"hyperscan%e9%ab%98%e6%80%a7%e8%83%bd%e7%9a%84%e6%ad%a3%e5%88%99%e5%8c%b9%e9%85%8d","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=1664","title":{"rendered":"hyperscan\u9ad8\u6027\u80fd\u7684\u6b63\u5219\u5339\u914d"},"content":{"rendered":"\n<p>hyperscan\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u5e93\uff0c\u7531 Intel \u5f00\u53d1\u5e76\u5f00\u6e90\uff0c\u65e8\u5728\u63d0\u4f9b\u9ad8\u901f\u3001\u4f4e\u5ef6\u8fdf\u7684\u6a21\u5f0f\u5339\u914d\u80fd\u529b\u3002<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u4ee3\u7801\u5730\u5740\uff1a<a href=\"https:\/\/github.com\/intel\/hyperscan\">https:\/\/github.com\/intel\/hyperscan<\/a><\/p>\n\n\n\n<p>\u4f7f\u7528\u624b\u518c\u5728\uff1a<a href=\"http:\/\/intel.github.io\/hyperscan\/dev-reference\/index.html\">http:\/\/intel.github.io\/hyperscan\/dev-reference\/index.html<\/a><\/p>\n\n\n\n<p>\u5b98\u65b9\u8bf4\u660e\u6587\u6863\uff1a<a href=\"https:\/\/www.intel.com\/content\/www\/us\/en\/developer\/articles\/technical\/introduction-to-hyperscan.html\">https:\/\/www.intel.com\/content\/www\/us\/en\/developer\/articles\/technical\/introduction-to-hyperscan.html<\/a><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\"><strong>hyperscan\u7f16\u8bd1\uff1a<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>hyperscan\u4f9d\u8d56\u4ee5\u4e0b\u51e0\u4e2a\u5e93\uff0c\u9700\u8981\u786e\u4fdd\u8fd9\u51e0\u4e2a\u5e93\u90fd\u5b58\u5728\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"688\" height=\"284\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-5.png\" alt=\"\" class=\"wp-image-1666\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-5.png 688w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-5-300x124.png 300w\" sizes=\"auto, (max-width: 688px) 100vw, 688px\" \/><\/figure>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u7b2c\u4e00\u6b65\uff1a\u4e0b\u8f7d\u6700\u65b0\u7248\u672c\u4ee3\u7801<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><code>git clone --branch v5.4.2 https:\/\/github.com\/intel\/hyperscan.git<\/code><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u7b2c\u4e8c\u6b65\uff1a\u6784\u9020\u9879\u76ee<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><code>mkdir build &amp;&amp; cd build &amp;&amp; cmake -DBUILD_SHARED_LIBS=ON ..<\/code><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"642\" height=\"242\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-6.png\" alt=\"\" class=\"wp-image-1667\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-6.png 642w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-6-300x113.png 300w\" sizes=\"auto, (max-width: 642px) 100vw, 642px\" \/><\/figure>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u7b2c\u4e09\u6b65\uff1a\u7f16\u8bd1\u5b89\u88c5<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><code>make -j 6 &amp;&amp; make install<\/code><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"667\" height=\"209\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-7.png\" alt=\"\" class=\"wp-image-1668\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-7.png 667w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-7-300x94.png 300w\" sizes=\"auto, (max-width: 667px) 100vw, 667px\" \/><\/figure>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\"><strong>\u6d4b\u8bd5\u4ee3\u7801ths.c\uff1a<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ gcc -Wall -o test ths.c -lhs\n\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n#include &lt;hs\/hs.h&gt;\n\n#define PATTERN_MAX 1024\ntypedef struct _exptns {\n    char * exppttns&#91;PATTERN_MAX] ;\n    unsigned int expflgs&#91;PATTERN_MAX] ;\n    unsigned int expids&#91;PATTERN_MAX] ;\n    int num ;\n} exptns_t ;\n\n\/\/ \u589e\u52a0\u4e00\u4e2a\u5339\u914d\u6a21\u5f0f\nint add_pattern(exptns_t *exptns, char * pattern, unsigned int expflg, unsigned int expid) {\n    int id = exptns-&gt;num ;\n    if( id &gt;= PATTERN_MAX || strlen(pattern) == 0 ) {\n        return -1 ; \n    }\n\n    \/\/ 1\u3001\u6dfb\u52a0\u8868\u8fbe\u5f0f\n    exptns-&gt;exppttns&#91;id] = (char *)calloc(1, strlen(pattern)+1) ;\n    memcpy(exptns-&gt;exppttns&#91;id], pattern, strlen(pattern)) ;\n\n    \/\/ 2\u3001\u8868\u8fbe\u5f0f\u5339\u914d\u65b9\u5f0f\n    exptns-&gt;expflgs&#91;id] = expflg ;\n\n    \/\/ 3\u3001\u6a21\u5f0f\u5bf9\u5e94\u7684ID\u53f7\n    exptns-&gt;expids&#91;id] = expid ;\n    exptns-&gt;num++ ;\n    return 0 ;\n}\n\n\n\/\/ \u5339\u914d\u4e4b\u540e\u7684\u56de\u8c03\u51fd\u6570\nint match_cb(unsigned int id, unsigned long long from, unsigned long long to, unsigned int flags, void *context) {\n    printf(\"match id %u from %llu to %llu %.*s\\n\", id, from, to, (int)to-(int)from, ((char *)context+from) ) ;\n    return 0 ;\n}\n\n\/\/ \u6e05\u7406\u8868\u8fbe\u5f0f\u3001\u7f16\u8bd1\u5e93\u548c\u53e5\u67c4\nvoid free_hs_data(exptns_t *exptns, hs_database_t ** hsdb, hs_scratch_t ** hsscth){\n    \/\/ 1\u3001\u6e05\u7406\u8868\u8fbe\u5f0f\u6570\u636e\n    if(exptns) {\n        for( int i=0; i&lt;exptns-&gt;num; ++i) {\n            if (exptns-&gt;exppttns&#91;i]) {\n                free(exptns-&gt;exppttns&#91;i]) ;\n                exptns-&gt;exppttns&#91;i] = NULL ;\n                exptns-&gt;expflgs&#91;i] = 0 ;\n                exptns-&gt;expids&#91;i] = 0 ;\n            }\n        }\n        exptns-&gt;num = 0 ;\n    }\n    \/\/ 2\u3001\u5220\u9664\u53e5\u67c4\n    if(hsscth) \n        if(*hsscth) {\n            hs_free_scratch(*hsscth) ;\n            *hsscth = NULL ;\n        }\n\n    \/\/ 3\u3001\u5220\u9664\u7f16\u8bd1\u540e\u7684\u6a21\u5f0f\u5e93\n    if(hsdb)\n        if(*hsdb) {\n            hs_free_database(*hsdb) ;\n            *hsdb = NULL ;\n        }\n}\n\nint main(int argc, char *argv&#91;]) {\n    exptns_t exptns ;\n    memset(&amp;exptns, 0x00, sizeof(exptns)) ;\n\n    \/\/ 0\u3001\u6dfb\u52a0 3 \u4e2a\u8868\u8fbe\u5f0f\n    add_pattern(&amp;exptns, \"abcd\",  HS_FLAG_DOTALL | HS_FLAG_CASELESS | HS_FLAG_SOM_LEFTMOST | HS_FLAG_MULTILINE, 101) ;\n    add_pattern(&amp;exptns, \"opq(rst)u\",  HS_FLAG_DOTALL | HS_FLAG_CASELESS | HS_FLAG_SOM_LEFTMOST | HS_FLAG_MULTILINE, 102) ;\n    add_pattern(&amp;exptns, \"hij\",  HS_FLAG_DOTALL | HS_FLAG_CASELESS | HS_FLAG_SOM_LEFTMOST | HS_FLAG_MULTILINE, 103) ;\n\n\n    hs_database_t * hsdb = NULL ;       \/\/ \u7528\u6765\u63a5\u6536\u7f16\u8bd1\u540e\u7684 hs \u5e93\u5730\u5740\n    hs_compile_error_t * cmplerr = NULL ;\n    hs_error_t hs_ret ;\n    \/\/ 1\u3001\u7f16\u8bd1\n    hs_ret = hs_compile_multi((const char * const *)exptns.exppttns, (const unsigned int*)exptns.expflgs, \n                                (const unsigned int *)exptns.expids, exptns.num, \n                                HS_MODE_BLOCK, NULL, &amp;hsdb, &amp;cmplerr) ;\n\n    if( hs_ret != HS_SUCCESS ) {\n        if(cmplerr-&gt;expression &lt; 0 ) {\n            printf(\"compile multi failed : %s\\n\", cmplerr-&gt;message) ;\n        } else {\n            printf(\"compile multi failed : %s, error id %d rule %s\\n\", cmplerr-&gt;message, \n                            exptns.expids&#91;cmplerr-&gt;expression], exptns.exppttns&#91;cmplerr-&gt;expression]) ;\n        }\n        hs_free_compile_error(cmplerr) ;\n        free_hs_data(&amp;exptns, NULL, NULL) ;\n        return -1 ;\n    }\n\n    hs_scratch_t * hsscth = NULL ;\n    \/\/ 2\u3001\u83b7\u53d6\u5339\u914d\u53e5\u67c4\n    hs_ret = hs_alloc_scratch(hsdb, &amp;hsscth) ;\n    if( hs_ret != HS_SUCCESS ) {\n        printf(\"get hs scratch failed !\\n\") ;\n        free_hs_data(&amp;exptns, &amp;hsdb, NULL) ;\n        return -1 ;\n    }\n\n    \/\/ 3\u3001\u5339\u914d\n    char * str = \"xxabcdefghijkLMNOPQrstUVWzyxabCDEFghiijklm\" ;\n    hs_ret = hs_scan(hsdb, str, strlen(str), 0, hsscth, &amp;match_cb, str) ;\n    if( hs_ret != HS_SUCCESS ) {\n        printf(\"get hs scan failed !\\n\") ;\n        return -1 ;\n    }\n\n\n    printf(\"\\n===============================\\n\") ;\n    \/\/ \u6d4b\u8bd52\n    hs_scratch_t * hsscth2 = NULL ;\n    hs_ret = hs_alloc_scratch(hsdb, &amp;hsscth2) ;\n    if( hs_ret != HS_SUCCESS ) {\n        printf(\"get hs scratch failed !\\n\") ;\n        free_hs_data(&amp;exptns, &amp;hsdb, &amp;hsscth) ;\n        return -1 ;\n    }\n\n    hs_ret = hs_scan(hsdb, str, strlen(str), 0, hsscth2, &amp;match_cb, str) ;\n    if( hs_ret != HS_SUCCESS ) {\n        printf(\"get hs scan failed !\\n\") ;\n        return -1 ;\n    }\n\n    printf(\"\\n--------------------------------------------\\n\") ;\n    \/\/ \u6d4b\u8bd53\n    hs_scratch_t * hsscth3 = NULL ;\n    hs_ret = hs_clone_scratch(hsscth2, &amp;hsscth3) ;\n    if( hs_ret != HS_SUCCESS ) {\n        printf(\"clone hs scratch failed !\\n\") ;\n        free_hs_data(&amp;exptns, &amp;hsdb, &amp;hsscth) ;\n        return -1 ;\n    }\n    free_hs_data(NULL, NULL, &amp;hsscth2) ;\n\n    hs_ret = hs_scan(hsdb, str, strlen(str), 0, hsscth3, &amp;match_cb, str) ;\n    if( hs_ret != HS_SUCCESS ) {\n        printf(\"get hs scan failed !\\n\") ;\n        return -1 ;\n    }\n    free_hs_data(NULL, NULL, &amp;hsscth3) ;\n\n\n    \/\/ 4\u3001\u7a7a\u95f4\u91ca\u653e\n    free_hs_data(&amp;exptns, &amp;hsdb, &amp;hsscth) ;\n    \n    return 0 ;\n}<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\"><strong>\u7f16\u8bd1\u548c\u6d4b\u8bd5\uff1a<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"397\" height=\"359\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-4.png\" alt=\"\" class=\"wp-image-1665\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-4.png 397w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/11\/\u56fe\u7247-4-300x271.png 300w\" sizes=\"auto, (max-width: 397px) 100vw, 397px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>hyperscan\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u5e93\uff0c\u7531 Intel \u5f00\u53d1\u5e76\u5f00\u6e90\uff0c\u65e8\u5728\u63d0\u4f9b\u9ad8\u901f\u3001\u4f4e\u5ef6\u8fdf\u7684\u6a21\u5f0f\u5339\u914d\u80fd\u529b\u3002<br \/>\n\u4ee3\u7801\u5730\u5740\uff1ahttps:\/\/github.com\/intel\/hyperscan<br \/>\n\u4f7f\u7528\u624b\u518c\u5728\uff1ahttp:\/\/intel.github.io\/hyperscan\/dev-reference\/index.html<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-with-sidebar","format":"standard","meta":{"footnotes":""},"categories":[156,154],"tags":[535,538,541,537,540,531,225,539],"class_list":["post-1664","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-c","category-154","tag-compile","tag-hs","tag-hs_scan","tag-hyperscan","tag-intel","tag-531","tag-225","tag-539"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/1664","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1664"}],"version-history":[{"count":4,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/1664\/revisions"}],"predecessor-version":[{"id":1674,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/1664\/revisions\/1674"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}