<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[[偶会用到]如何在C语言中巧用正则表达式，如：Linux下抓包过滤掉图片头。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Fri, 07 Feb 2014 10:13:10 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	背景：如：Linux下pcap抓包过滤掉图片头，pcap 的过滤器也能过滤，但小工具没有必要，用正则足已，BPF 过滤代码从逻辑上看很类似于汇编语言，但它实际上是机器语言，匹配下这样的的串如不是不显示不定入：Content-Type: image/x-icon 这样的，可以匹配到像session及get变量/post变量等都可以用正则，为此，特备案记录。<br/>附录：<br/>pcap 的过滤器是以已声明的谓词语法为基础的。过滤器是一个 ASCII 字符串，它包含了一个过滤 表达式。pcap_compile()把这个表达式编译成内核级的包过滤器。 这个表达式会选择那些数据包将会被堆存。摘自： http://wenku.baidu.com/link?url=Q-pBW07zxVTcuZFDljeezTVy2B6MMgpwncTtM8v-yd4CmV69FNXq2gBr2w-g4FDv3Jf720Ks4-LMXqh7qAhI7vIkO71w2X_GwLlnw4F2hd7<br/>简单的C代码如下：<br/><br/>regexp.c<br/><textarea name="code" class="C" rows="15" cols="100">
#include &lt;stdio.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;regex.h&gt;

/* 取子串的函数 */
static char* substr(const char*str, unsigned start, unsigned end)
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;unsigned n = end - start;
&nbsp;&nbsp;&nbsp;&nbsp;static char stbuf[256];
&nbsp;&nbsp;&nbsp;&nbsp;strncpy(stbuf, str + start, n);
&nbsp;&nbsp;&nbsp;&nbsp;stbuf[n] = 0;
&nbsp;&nbsp;&nbsp;&nbsp;return stbuf;
&#125;
/* 主程序 */
int main(int argc, char** argv)
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;char * pattern;
&nbsp;&nbsp;&nbsp;&nbsp;int x, z, lno = 0, cflags = 0;
&nbsp;&nbsp;&nbsp;&nbsp;char ebuf[128], lbuf[256];
&nbsp;&nbsp;&nbsp;&nbsp;regex_t reg;
&nbsp;&nbsp;&nbsp;&nbsp;regmatch_t pm[10];
&nbsp;&nbsp;&nbsp;&nbsp;const size_t nmatch = 10;
&nbsp;&nbsp;&nbsp;&nbsp;/* 编译正则表达式*/
&nbsp;&nbsp;&nbsp;&nbsp;pattern = argv[1];
&nbsp;&nbsp;&nbsp;&nbsp;z = regcomp(&amp;reg, pattern, cflags);
&nbsp;&nbsp;&nbsp;&nbsp;if (z != 0)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regerror(z, &amp;reg, ebuf, sizeof(ebuf));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr, &quot;%s: pattern &#039;%s&#039; &#92;n&quot;, ebuf, pattern);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 1;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;&nbsp;逐行处理输入的数据 */
&nbsp;&nbsp;&nbsp;&nbsp;while(fgets(lbuf, sizeof(lbuf), stdin)) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;++lno;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ((z = strlen(lbuf)) &gt; 0 &amp;&amp; lbuf[z-1] == &#039;&#92;n&#039;)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lbuf[z - 1] = 0;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* 对每一行应用正则表达式进行匹配 */
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;z = regexec(&amp;reg, lbuf, nmatch, pm, 0);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (z == REG_NOMATCH) continue;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else if (z != 0) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regerror(z, &amp;reg, ebuf, sizeof(ebuf));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr, &quot;%s: regcom(&#039;%s&#039;)&#92;n&quot;, ebuf, lbuf);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 2;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* 输出处理结果 */
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (x = 0; x &lt; nmatch &amp;&amp; pm[x].rm_so != -1; ++ x) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!x) printf(&quot;%04d: %s&#92;n&quot;, lno, lbuf);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;&nbsp;&nbsp;$%d=&#039;%s&#039;&#92;n&quot;, x, substr(lbuf, pm[x].rm_so, pm[x].rm_eo));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;/* 释放正则表达式&nbsp;&nbsp;*/
&nbsp;&nbsp;&nbsp;&nbsp;regfree(&amp;reg);
&nbsp;&nbsp;&nbsp;&nbsp;return 0;
&#125;

</textarea><br/>编译上面的C代码如下，make.sh<br/>gcc regexp.c -o regexp<br/><br/>对代码进行实践，匹配到正则的情况如下：<br/><textarea name="code" class="php" rows="15" cols="100">
[root@test c_rege_codes]# ./regexp&nbsp;&nbsp;&#039;regex[a-z]*&#039; &lt; regexp.c
0003: #include &lt;regex.h&gt;
&nbsp;&nbsp;$0=&#039;regex&#039;
0020:&nbsp;&nbsp;&nbsp;&nbsp; regex_t reg;
&nbsp;&nbsp;$0=&#039;regex&#039;
0037:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; z = regexec(&amp;reg, lbuf, nmatch, pm, 0);
&nbsp;&nbsp;$0=&#039;regexec&#039;
</textarea><br/><br/>实践来源：http://tech.ccidnet.com/art/302/20040319/97027_1.html<br/>多次摘抄的，一样的代码其给转意或变成别的了都编译不过，出现乱码，上面这个可以编译通过，不容易啊。<br/><br/>于是，试着仿照写一个，改动调试了解如下：<br/><br/><textarea name="code" class="php" rows="15" cols="100">
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;regex.h&gt;

/* 取子串的函数 */
static char* substr(const char*str, unsigned start, unsigned end)
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;unsigned n = end - start;
&nbsp;&nbsp;&nbsp;&nbsp;static char stbuf[256];
&nbsp;&nbsp;&nbsp;&nbsp;strncpy(stbuf, str + start, n);
&nbsp;&nbsp;&nbsp;&nbsp;stbuf[n] = 0;
&nbsp;&nbsp;&nbsp;&nbsp;return stbuf;
&#125;
/* 主程序 */
int main(int argc, char** argv)
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;char * pattern;
&nbsp;&nbsp;&nbsp;&nbsp;int x, z, lno = 0, cflags = 0;
&nbsp;&nbsp;&nbsp;&nbsp;char ebuf[128], lbuf[256];
&nbsp;&nbsp;&nbsp;&nbsp;regex_t reg;
&nbsp;&nbsp;&nbsp;&nbsp;regmatch_t pm[10];
&nbsp;&nbsp;&nbsp;&nbsp;const size_t nmatch = 10;
&nbsp;&nbsp;&nbsp;&nbsp;/* 编译正则表达式*/
&nbsp;&nbsp;&nbsp;&nbsp;pattern = &quot;GET.*&#92;&#92;.js&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;pattern=%s&#92;n&quot;,pattern);
&nbsp;&nbsp;&nbsp;&nbsp;z = regcomp(&amp;reg, pattern, cflags);
&nbsp;&nbsp;&nbsp;&nbsp;if (z != 0)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regerror(z, &amp;reg, ebuf, sizeof(ebuf));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr, &quot;%s: pattern &#039;%s&#039; &#92;n&quot;, ebuf, pattern);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 1;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;char *lbufInStr = &quot;abc &#92;n GET /themes/zh/js/xy.js?ver=2.006 HTTP/1.0 def123&#92;n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;strncpy(lbuf,lbufInStr,strlen(lbufInStr));
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;lbuf=%s&#92;n&quot;,lbuf);
&nbsp;&nbsp;&nbsp;&nbsp;++lno;
&nbsp;&nbsp;&nbsp;&nbsp;if ((z = strlen(lbuf)) &gt; 0 &amp;&amp; lbuf[z-1] == &#039;&#92;n&#039;)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lbuf[z - 1] = 0;
&nbsp;&nbsp;&nbsp;&nbsp;z = regexec(&amp;reg, lbuf, nmatch, pm, 0);
&nbsp;&nbsp;&nbsp;&nbsp;if (z == REG_NOMATCH) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;not match &#92;n&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0; 
&nbsp;&nbsp;&nbsp;&nbsp;&#125; else if (z != 0) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regerror(z, &amp;reg, ebuf, sizeof(ebuf));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr, &quot;%s: regcom(&#039;%s&#039;)&#92;n&quot;, ebuf, lbuf);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 2;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;/* 输出处理结果 */
&nbsp;&nbsp;&nbsp;&nbsp;for (x = 0; x &lt; nmatch &amp;&amp; pm[x].rm_so != -1; ++ x) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!x) printf(&quot;%04d: %s&#92;n&quot;, lno, lbuf);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;&nbsp;&nbsp;$%d=&#039;%s&#039;&#92;n&quot;, x, substr(lbuf, pm[x].rm_so, pm[x].rm_eo));
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;/* 释放正则表达式&nbsp;&nbsp;*/
&nbsp;&nbsp;&nbsp;&nbsp;regfree(&amp;reg);
&nbsp;&nbsp;&nbsp;&nbsp;return 0;
&#125;
</textarea><br/><br/>编译：<br/>gcc regexp4pcap.c -o regexp4pcap<br/><br/>执行：<br/>[root@test c_rege_codes]# ./regexp4pcap <br/>pattern=GET.*&#92;.js<br/>lbuf=abc <br/> GET /themes/zh/js/xy.js?ver=2.006 HTTP/1.0 def123<br/>0001: abc <br/> GET /themes/zh/js/xy.js?ver=2.006 HTTP/1.0 def123<br/>&nbsp;&nbsp;$0=&#039;GET /themes/zh/js/xy.js&#039;<br/><br/>注意：这里的.js里的点得转意，否则匹配不到哟：http://bbs.csdn.net/topics/340040294<br/>&#92;&#92;s忘记了转义 
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [偶会用到]如何在C语言中巧用正则表达式，如：Linux下抓包过滤掉图片头。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>