Chinaunix首页 | 论坛 | 博客
  • 博客访问: 792001
  • 博文数量: 83
  • 博客积分: 7030
  • 博客等级: 少将
  • 技术积分: 1097
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-06 15:50
文章分类

全部博文(83)

文章存档

2011年(2)

2010年(9)

2009年(56)

2008年(16)

我的朋友

分类: C/C++

2011-07-02 19:40:56

  1. // BinReplace.cpp : Defines the entry point for the console application.
  2. //

  3. #include "stdafx.h"
  4. #include "string.h"
  5. #include "stdlib.h"

  6. #define OK 0
  7. #define ERR 1

  8. typedef unsigned char UCHAR ;
  9. typedef char CHAR ;
  10. typedef unsigned long ULONG;
  11. typedef unsigned int UINT;

  12. typedef struct tagLV{
  13.     ULONG ullenth;
  14.     UCHAR *pucValue;
  15. }S_V_L;

  16. void BinPrint(const UCHAR *pucBuf, ULONG ulLen)
  17. {
  18.     ULONG i;
  19.     if (NULL == pucBuf && ulLen != 0)
  20.     {
  21.         printf("BinPrint error!\n");
  22.         return;
  23.     }
  24.     printf("BinBuf:");

  25.     for (i = 0; i < ulLen; i++)
  26.     {
  27.         printf("0x%x,", pucBuf[i]);
  28.         if (0 == (i %16) && 0 != i)
  29.         {
  30.             printf("\r\n");
  31.         }
  32.     }
  33.     printf("\r\n");
  34.     
  35. }

  36. UCHAR *BinDup(UCHAR *pBuf, ULONG ulLength)
  37. {
  38.     UCHAR *pNew = NULL;

  39.     if (NULL == pBuf)
  40.     {
  41.         return NULL;
  42.     }

  43.     pNew = (UCHAR *)malloc(ulLength);
  44.     if (NULL == pNew)
  45.     {
  46.         printf("MyStrDum malloc error!\n");
  47.         return NULL;
  48.     }
  49.     memset(pNew, 0x00, ulLength);
  50.     memcpy(pNew, pBuf, ulLength);

  51.     return pNew;
  52. }


  53. /* pstSource:原二进制数据。输入参数
  54.     pstSub:要替换的二进制数据。输入参数
  55.     pstRep:需替换对目的二进制数据。输入参数
  56.     pstRtn:替换后返回的数据。为输出值,不需要外部开空间,本函数会自动分配空间,需外部手动释放空间
  57. */
  58. ULONG BinReplace(S_V_L *pstSource, S_V_L *pstSub, S_V_L *pstRep, S_V_L **ppstRtn)
  59. {
  60.     ULONG ulNewStrLen = 0;
  61.     ULONG ulNewTrueLen = 0;
  62.     
  63.     UCHAR *pucNewBin = NULL;
  64.     UCHAR *p1,*p2,*p3; /* p1是pSource 指针,P2是pSub指针,P3是pRep指针 */
  65.     UCHAR *p1_end,*p2_end,*p3_end; /* p1_end是pSource 指针最后,P2_end是pSub指针最后,P3_end是pRep指针最后 */
  66.     UCHAR *p, *p_end, *pucNew;
  67.      S_V_L *pstRtn = NULL;
  68.     
  69.     if (NULL == pstSource || NULL == ppstRtn)
  70.     {
  71.         printf("pSource is null!\r\n");
  72.         return ERR;
  73.     }

  74.     pstRtn = (S_V_L *)malloc(sizeof(S_V_L));
  75.     if (NULL == pstRtn)
  76.     {
  77.          printf("malloc error!\r\n");
  78.         return ERR;
  79.     }
  80.     
  81.     memset(pstRtn, 0x00, sizeof(S_V_L));
  82.     *ppstRtn = pstRtn;
  83.     
  84.     
  85.     /* 当字串为空/长度为0,或者替换字串为空/长度为0此替换都无意义,否则程序会异常 */
  86.     if (NULL == pstSub || NULL == pstRep|| 0 == pstSub->ullenth || 0 == pstRep->ullenth)
  87.     {
  88.         printf("pstSub/pstRep is null!\r\n");
  89.         pstRtn->ullenth = pstSource->ullenth;
  90.         pstRtn->pucValue = BinDup(pstSource->pucValue, pstSource->ullenth);
  91.         return OK;
  92.     }

  93.     /* 字串长度不能超过原字串长度,因为肯定匹配不上。 */
  94.     if (pstSource->ullenth < pstSub->ullenth)
  95.     {
  96.         printf("ulSubStrLen is too long!\r\n");
  97.         pstRtn->ullenth = pstSource->ullenth;
  98.         pstRtn->pucValue = BinDup(pstSource->pucValue, pstSource->ullenth);
  99.         return OK;
  100.     }

  101.     /* 计算被替换后的字符串最大长度 */
  102.     ulNewStrLen = (pstSub->ullenth < pstRep->ullenth)?((pstSource->ullenth/pstSub->ullenth + 1) * pstRep->ullenth):pstSource->ullenth;

  103.     
  104.     pucNewBin = (UCHAR *)malloc(ulNewStrLen);
  105.     if (NULL == pucNewBin)
  106.     {
  107.          printf("malloc error!\r\n");
  108.         pstRtn->ullenth = pstSource->ullenth;
  109.         pstRtn->pucValue = BinDup(pstSource->pucValue, pstSource->ullenth);
  110.         return OK;
  111.     }

  112.     memset(pucNewBin, 0x00, ulNewStrLen);
  113.     
  114.     p = pstSource->pucValue;
  115.     p_end = p + pstSource->ullenth;
  116.     pucNew = pucNewBin;

  117.     ulNewTrueLen = 0;
  118.     while(p != p_end) /* 遍历原二进制数据 */
  119.     {
  120.         p1 = p;
  121.         p1_end = p_end;
  122.         
  123.         p2 = pstSub->pucValue;
  124.         p2_end = p2 + pstSub->ullenth;
  125.         
  126.         p3 = pstRep->pucValue;
  127.         p3_end = p3 + pstRep->ullenth;
  128.         
  129.         while((p1 < p1_end) && (p2 < p2_end) && (*p1 == *p2)) /* 找到相等的二进制数据 */
  130.         {
  131.             p1++;
  132.             p2++;
  133.         }
  134.         
  135.         if (p2 == p2_end) /* 如果完全匹配 */
  136.         {
  137.             while(p3 != p3_end)/*复制替换二进制数据*/
  138.             {
  139.                 *pucNew = *p3;
  140.                 p3++;
  141.                 pucNew++;
  142.                 ulNewTrueLen++;
  143.             }
  144.             p1--;/*匹配完了多走了一个位置,应该回来*/
  145.             p = p1;
  146.         }
  147.         else
  148.         {
  149.             *pucNew = *p;
  150.             pucNew++;
  151.             ulNewTrueLen++;
  152.         }
  153.         p++;
  154.     }

  155.     pstRtn->pucValue = pucNewBin;
  156.     pstRtn->ullenth = ulNewTrueLen;
  157.     *ppstRtn = pstRtn;
  158.     return OK;
  159.  
  160. }



  161. int main(int argc, char* argv[])
  162. {
  163.     UCHAR pSource[] = {1,2,3,3,3,4,4,5,3,3,4};
  164.     UCHAR pstSub[] = {3,3};
  165.     UCHAR pstRep[] = {0x0a,0x0a,0x0b};
  166.     S_V_L stSource;
  167.     S_V_L stSub;
  168.     S_V_L stRep;
  169.     S_V_L *p = NULL;
  170.     ULONG ulRtn = ERR;

  171.     memset(&stSource, 0, sizeof(S_V_L));
  172.     stSource.ullenth = 11;
  173.     stSource.pucValue = pSource;

  174.     memset(&stSub, 0, sizeof(S_V_L));
  175.     stSub.ullenth = 2;
  176.     stSub.pucValue = pstSub;

  177.     memset(&stRep, 0, sizeof(S_V_L));
  178.     stRep.ullenth = 3;
  179.     stRep.pucValue = pstRep;


  180.     ulRtn= BinReplace(&stSource, &stSub, &stRep, &p);
  181.     if (ulRtn != OK)
  182.     {
  183.         printf("BinReplace error\n");
  184.         return ERR;
  185.     }
  186.     printf("source=\n");
  187.     BinPrint(stSource.pucValue, 11);

  188.     printf("sub = \n");
  189.     BinPrint(stSub.pucValue, 2);

  190.     printf("pstRep = \n");
  191.     BinPrint(stRep.pucValue, 3);
  192.     
  193.     printf("replaced len:%d,string = ",p->ullenth, p);
  194.     BinPrint(p->pucValue, p->ullenth);
  195.     
  196.     if (NULL != p)
  197.     {
  198.         if (NULL != p->pucValue)
  199.             free(p->pucValue);
  200.         free(p);
  201.     }
  202.     else
  203.     {
  204.         printf("replace error!\n");
  205.     }
  206.     return 0;
  207. }
阅读(1400) | 评论(0) | 转发(0) |
0

上一篇:博客已升级,请注意变更地址

下一篇:没有了

给主人留下些什么吧!~~