• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            woaidongmao

            文章均收錄自他人博客,但不喜標題前加-[轉貼],因其丑陋,見諒!~
            隨筆 - 1469, 文章 - 0, 評論 - 661, 引用 - 0
            數據加載中……

            怎么使用PCRE庫?

            一直找PCRE的學習資料,網上沒有發現很全面的,回過頭了仔細看了一下PCRE源碼dochtml下的資料,發現其實這些文檔就是非常不錯的學習材料。

            今天看了一下如何使用PCRE,還沒有涉及到PCRE原理和實現的代碼。我們可以在http://www.pcre.org/上下載到pcre的代碼,下載到的源文件pcre-x.x.tar.bz2在linux下面很容易就可以被編譯和安裝(x86 系列cpu哦)。

            ./configure

            make

            make install

            PCRE編譯安裝之后,以一個lib庫的方式提供給用戶程序進行使用,PCRE lib 提供了一組API,通過這一組API可以實現類似于Perl語法的正則表達式查找和匹配的功能。(PCREE library is a set of functions that implement regular expression pattern matching using the same syntax and semantics as Perl, with just a few differences.)

            要想使用好PCRE,要了解很多正則表達式的內容、同時需要對PCRE進行很多的配置,從而使其支持不同的模式和規格。在這里只是簡單的描述一下使用PCRE的方法,不涉及配置和正則表達式語法的內容。

            使用PCRE主要是使用下面的四個函數,對這四個函數有了了解,使用PCRE庫的時候就會簡單很多。

            pcre_compile() /pcre_compile2()

            pcre_study()

            pcre_exec()

            1. pcre_compile() /pcre_compile2(), 正則表達式在使用之前要經過編譯。

            pcre *pcre_compile(const char *pattern, int options, const char **errptr, int *erroffset, const unsigned char *tableptr);

            pcre *pcre_compile2(const char *pattern, int options, int *errorcodeptr, const char **errptr, int *erroffset, const unsigned char *tableptr);

            編譯的目的是將正則表達式的pattern轉換成PCRE引擎能夠識別的結構(struct real_pcre)。

            還沒有對編譯的過程進行分析.

            2. pcre_study(),對編譯后的正則表達式結構(struct real_pcre)進行分析和學習,學習的結果是一個數據結構(struct pcre_extra),這個數據結構連同編譯后的規則(struct real_pcre)可以一起送給pcre_exec單元進行匹配.

            If a compiled pattern is going to be used several times, it is worth spending more time analyzing it in order to speed up the time taken for matching. The function pcre_study() takes a pointer to a compiled pattern as its first argument. If studying the pattern produces additional information that will help speed up matching, pcre_study() returns a pointer to a pcre_extra block, in which the study_data field points to the results of the study.

            pcre_study()的引入主要是為了加速正則表達式匹配的速度.(為什么學習后就能加速呢?)這個還是比較有用的,可以將正則表達式編譯,學習后保存到一個文件或內存中,這樣進行匹配的時候效率比較搞.snort中就是這樣做的.

            3. pcre_exec(),根據正則表達式到指定的字符串中進行查找和匹配,并輸出匹配的結果.

            The function pcre_exec() is called to match a subject string against a compiled pattern, which is passed in the code argument. If the pattern has been studied, the result of the study should be passed in the extra argument. This function is the main matching facility of the library, and it operates in a Perl-like manner.

            4. Snort中如何使用PCRE呢?snort中以插件的形式調用PCRE進行正則表達式的匹配。

            1)進行正則表達式的初始化。

            InitializeDetection--> RegisterRules-->RegisterOneRule-->PCRESetup(Just for OPTION_TYPE_PCRE)->pcre_compile and pcre_study. All will be stored in a structure called PCREInfo in the memory.

            2.) 規則的匹配。DetectionCheckRule-->ruleMatch-->ruleMatchInternal-->pcreMatch(OPTION_TYPE_PCRE)->pcre_test-->pcre_exec.

            5.編譯PCRE on TILERA platform.

            1) tar -xjvf pcre-7.9.tar.bz2

            2) Modify config.sub to support tile architecture.

            We wish to use DE>HOST=tileDE>, but the DE>tileDE> architecture is not yet standard, so may not exist in the DE>config.subDE> file. If necessary, add these lines in the alphabetical list of architectures (typically about 1,100 lines down):

             tile*)
              basic_machine=tile-tilera
              os=-linux-gnu
              ;;
            3) Compile PCRE on tile Linux. 
            ** Start up TILERA card through tile-monitor.
            tile-monitor --pci --mount-tile /usr  \
              --mount-tile /bin --mount-tile /sbin --mount-tile /etc --mount-tile /lib \
              --mkdir /mnt/libs --mount /libs-compile /mnt/libs \
              --mkdir /mnt/mde  --mount $TILERA_ROOT /mnt/mde
            * ./configure --build=tile  --prefix=/usr  lt_cv_sys_max_cmd_len=262144 --disable-cpp
            //編譯的時候沒有使能c++的支持。
            pcre-7.9 configuration summary:
               pcre-7.9 configuration summary:
                Install prefix .................. : /usr
                C preprocessor .................. : gcc -E
                C compiler ...................... : gcc
                C++ preprocessor ................ : g++ -E
                C++ compiler .................... : g++
                Linker .......................... : /usr/bin/ld
                C preprocessor flags ............ :
                C compiler flags ................ : -O2
                C++ compiler flags .............. : -O2
                Linker flags .................... :
                Extra libraries ................. :
                Build C++ library ............... : no
                Enable UTF-8 support ............ : no
                Unicode properties .............. : no
                Newline char/sequence ........... : lf
                \R matches only ANYCRLF ......... : no
                EBCDIC coding ................... : no
                Rebuild char tables ............. : no
                Use stack recursion ............. : yes
                POSIX mem threshold ............. : 10
                Internal link size .............. : 2
                Match limit ..................... : 10000000
                Match limit recursion ........... : MATCH_LIMIT
                Build shared libs ............... : yes
                Build static libs ............... : yes
                Link pcregrep with libz ......... : no
                Link pcregrep with libbz2 ....... : no
                Link pcretest with libreadline .. : no
            * make
            * make install

            4) Compile the PCRE demo code and test PCRE lib on TILERA linux. PCRE 的源文件中提供了兩個demo程序,一個是比較簡單的pcredemo.c,很容易理解;另外一個是pcretest.c,這個比較全面、完整的介紹了pcre庫的使用。這兩個demo本身就是非常好的學習材料。
            # gcc -o pcredemo pcredemo.c -lpcre
            #  ./pcredemo 'cat|dog' 'the cat sat on the mat'
            Match succeeded at offset 4
            0: cat
            No named substrings
            # ./pcredemo -g 'cat|dog' 'the dog sat on the cat'
            Match succeeded at offset 4
            0: dog
            No named substrings
            Match succeeded again at offset 19
            0: cat
            No named substrings
            //參考資料:
            PCRE源碼文檔:pcre-7.9/doc/html

            posted on 2009-09-07 21:46 肥仔 閱讀(4778) 評論(1)  編輯 收藏 引用 所屬分類: 庫 & 代碼段

            評論

            # re: 怎么使用PCRE庫?  回復  更多評論   

            不錯呢,正好在研究pcre,謝了
            2011-08-03 08:59 | 編程
            国产一区二区三区久久| 久久久久久国产a免费观看不卡| 久久精品无码一区二区三区日韩| 久久精品国产一区二区三区日韩| 精品综合久久久久久88小说| 亚洲乱码日产精品a级毛片久久| 久久偷看各类wc女厕嘘嘘| 91精品国产91久久久久福利| 久久99精品久久久久久齐齐| 久久久久人妻一区精品性色av| 久久噜噜电影你懂的| 久久亚洲AV成人无码软件| 久久AV高清无码| 四虎久久影院| 青青草国产精品久久久久| 久久婷婷色香五月综合激情| 久久亚洲高清观看| 国产色综合久久无码有码| 国产精品久久久久一区二区三区| 久久精品国产亚洲AV影院| 青草久久久国产线免观| 99久久精品午夜一区二区| 一级女性全黄久久生活片免费| 久久国产乱子精品免费女| 一本久久知道综合久久| 日本加勒比久久精品| 91久久九九无码成人网站 | 国产精品青草久久久久福利99| 久久天天躁狠狠躁夜夜躁2014| 久久无码一区二区三区少妇 | 美女写真久久影院| 国产精品久久久久久久久免费| 久久精品久久久久观看99水蜜桃 | 2021久久精品免费观看| 日本高清无卡码一区二区久久| 精品久久久久久无码免费| 国产激情久久久久影院老熟女| 久久久中文字幕| 91亚洲国产成人久久精品网址| 99久久无码一区人妻a黑| 99久久人妻无码精品系列 |