您的位置:首页 > 其它

仿写类似strstr()字符串匹配的二进制匹配方法FindBinaray()

2010-09-09 15:21 621 查看
在工作中遇到对于二进制数据的查找,类似于strstr()字符串匹配的函数,半天没有查到可以公用的API或是方法。

这不自己写一个来实现:

U8* FindBinary(const U8* pucSrc, int nsrcSize, const U8* pucFind, int nFindSize)
{
const U8 *pSrcPos = pucSrc;
const U8 *pFindPos = pucFind;

/* deliberately dumb algorithm */
while ( pSrcPos - pucSrc <= nsrcSize )
{
pFindPos = pucFind;
while ( (pSrcPos-pucSrc<=nsrcSize) && (pFindPos-pucFind<=nFindSize) )
{
if (*pSrcPos != *pFindPos)
break;

pSrcPos++;
pFindPos++;
}

if ( (pFindPos-pucFind) >= nFindSize )
return (U8 *)pSrcPos;
//返回是查找匹配字符串的后一个字符指针
pSrcPos++;
}

return NULL;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: