设为首页 收藏本站
查看: 1104|回复: 0

[经验分享] apache源码编译中首先安装pcre的原因

[复制链接]

尚未签到

发表于 2018-11-25 11:45:39 | 显示全部楼层 |阅读模式
  下午在重写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):




  • 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




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-639366-1-1.html 上篇帖子: LAMP:apache+mysql+php的编译安装 下篇帖子: 获取安装后Apache、MySQL、Nginx、PHP编译时参数
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表