apache源碼編譯中首先安裝pcre的原因

下午在重寫apache源碼編譯的時候,回憶起老師說在./configure之前要首先安裝pcre-devel,因爲不是很理解,就從網上找了如下資料,僅供參考

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):


  1.  tile*) 
  2.   basic_machine=tile-tilera 
  3.   os=-linux-gnu 
  4.   ;; 
  5. 3) Compile PCRE on tile Linux.  
  6. ** Start up TILERA card through tile-monitor. 
  7. tile-monitor --pci --mount-tile /usr  \ 
  8.   --mount-tile /bin --mount-tile /sbin --mount-tile /etc --mount-tile /lib \ 
  9.   --mkdir /mnt/libs --mount /libs-compile /mnt/libs \ 
  10.   --mkdir /mnt/mde  --mount $TILERA_ROOT /mnt/mde 
  11. * ./configure --build=tile  --prefix=/usr  lt_cv_sys_max_cmd_len=262144 --disable-cpp 

  1. //編譯的時候沒有使能c++的支持。 
  2. pcre-7.9 configuration summary: 
  3.    pcre-7.9 configuration summary: 
  4.     Install prefix .................. : /usr 
  5.     C preprocessor .................. : gcc -E 
  6.     C compiler ...................... : gcc 
  7.     C++ preprocessor ................ : g++ -E 
  8.     C++ compiler .................... : g++ 
  9.     Linker .......................... : /usr/bin/ld 
  10.     C preprocessor flags ............ : 
  11.     C compiler flags ................ : -O2 
  12.     C++ compiler flags .............. : -O2 
  13.     Linker flags .................... : 
  14.     Extra libraries ................. : 
  15.     Build C++ library ............... : no 
  16.     Enable UTF-8 support ............ : no 
  17.     Unicode properties .............. : no 
  18.     Newline char/sequence ........... : lf 
  19.     \R matches only ANYCRLF ......... : no 
  20.     EBCDIC coding ................... : no 
  21.     Rebuild char tables ............. : no 
  22.     Use stack recursion ............. : yes 
  23.     POSIX mem threshold ............. : 10 
  24.     Internal link size .............. : 2 
  25.     Match limit ..................... : 10000000 
  26.     Match limit recursion ........... : MATCH_LIMIT 
  27.     Build shared libs ............... : yes 
  28.     Build static libs ............... : yes 
  29.     Link pcregrep with libz ......... : no 
  30.     Link pcregrep with libbz2 ....... : no 
  31.     Link pcretest with libreadline .. : no 
  32. * make 
  33. * make install 
4) Compile the PCRE demo code and test PCRE lib on TILERA linux. PCRE 的源文件中提供了兩個demo程序,一個是比較簡單的pcredemo.c,很容易理解;另外一個是pcretest.c,這個比較全面、完整的介紹了pcre庫的使用。這兩個demo本身就是非常好的學習材料。

  1. # gcc -o pcredemo pcredemo.c -lpcre 
  2. #  ./pcredemo 'cat|dog' 'the cat sat on the mat' 
  3. Match succeeded at offset 4 
  4.  0: cat 
  5. No named substrings 
  6. # ./pcredemo -g 'cat|dog' 'the dog sat on the cat' 
  7. Match succeeded at offset 4 
  8.  0: dog 
  9. No named substrings 
  10. Match succeeded again at offset 19 
  11.  0: cat 
  12. No named substrings 
//參考資料:
PCRE源碼文檔:pcre-7.9/doc/html

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章