PPP 转义字符 编码 和 解码

  1 #include <stdio.h>
  2 #include <string.h>
  3 
  4 // PPP数据帧每一帧都以标识字符0x7E开始和结束;
  5 // 由于标识字符的值是0x7E,因此当该字符出现在信息字段中时,PPP需要对它进行转义。
  6 // 当PPP使用异步传输时,它把转义字符定义为:0x7D,并使用字节填充RFC-1662标准。
  7 // 字节填充RFC-1662标准规定如下:
  8 // 1. 把信息字段中出现的每一个0x7E字符转变成字节序列(0x7D,0x5E)
  9 // 2. 若信息字段中出现一个0x7D的字节(即出现了与转义字符相同的比特组合),
 10 //    则把0x7D转义成两个字节序列(0x7D,0x5D)
 11 // 3. 若信息字段中出现ASCII码的控制字符(即数值小于0x20的字符),
 12 //    则在该字符前面加入一个0x7D字节,同时将该字符的编码加以改变
 13 
 14 #define PPP_FRAME_FLAG        ( 0x7E )    /* 标识字符 */
 15 #define PPP_FRAME_ESC        ( 0x7D )    /* 转义字符 */
 16 #define PPP_FRAME_ENC        ( 0x20 )    /* 编码字符 */
 17 
 18 int ppp_encode(unsigned char *in, int in_len, unsigned char *out, int *out_len)
 19 {
 20     unsigned char *pi, *po;
 21     int i, tmp_len;
 22     
 23     pi = in;
 24     po = out;
 25     tmp_len = in_len;
 26     
 27     for(i = 0; i < in_len; i++)
 28     {
 29         if( *pi == PPP_FRAME_FLAG || *pi == PPP_FRAME_ESC || *pi < 0x20 )
 30         {
 31             *po = PPP_FRAME_ESC;
 32             po++;
 33             tmp_len++;    
 34             *po = *pi ^ PPP_FRAME_ENC;
 35         }
 36         else
 37         {
 38             *po = *pi;    
 39         }
 40         
 41         pi++;
 42         po++;
 43     }
 44     *out_len = tmp_len;
 45     
 46     return 0;    
 47 }
 48 
 49 int ppp_decode(unsigned char *in, int in_len, unsigned char *out, int *out_len)
 50 {
 51     unsigned char *pi, *po;
 52     int i, tmp_len;
 53     
 54     pi = in;
 55     po = out;
 56     tmp_len = in_len;
 57     
 58     for(i = 0; i < in_len; i++)
 59     {
 60         if(*pi == PPP_FRAME_ESC)
 61         {
 62             pi++;
 63             tmp_len--;
 64             *po = *pi ^ PPP_FRAME_ENC;
 65             
 66             i++;
 67         }    
 68         else
 69         {
 70             *po = *pi;    
 71         }
 72         
 73         pi++;
 74         po++;
 75     }
 76     *out_len = tmp_len;
 77     
 78     return 0;
 79 }
 80 
 81 
 82 void printf_hex(char *title, unsigned char *hex, int n)
 83 {
 84     int i;
 85     
 86     printf("%s", title);
 87     for(i = 0; i < n; i++)
 88     {
 89         if(i % 16 == 0 && i != 0)
 90             printf("\r\n");
 91         printf("%02x ", (unsigned char )hex[i]);
 92     }
 93     printf("\r\n");
 94 }
 95 
 96 int main(void)
 97 {
 98     unsigned char p1[256];
 99     unsigned char p2[512];
100     unsigned char p3[512];
101     int i, len1, len2, len3;
102     
103     len1 = sizeof(p1)/sizeof(p1[0]);
104     
105     for(i = 0; i < len1; i++)
106     {
107         p1[i] = i % 256;    
108     }
109     
110     printf_hex("Before Encode::\r\n", p1, len1);
111     printf("Before Encode, len1: %d\r\n", len1);
112     
113     ppp_encode(p1, len1, p2, &len2);
114     
115     printf_hex("After Encode::\r\n", p2, len2);
116     printf("After Encode, len2: %d\r\n", len2);
117     
118     ppp_decode(p2, len2, p3, &len3);
119     
120     printf_hex("After Decode::\r\n", p3, len3);
121     printf("After Decode, len3: %d\r\n", len3);
122     
123     return 0;    
124 }

 

转载于:https://www.cnblogs.com/utank/p/5889025.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在通信中,为了避免数据传输的混乱和错误,我们通常需要在数据帧的开头和结尾加上特殊的字符,以便接收方可以正确地识别数据帧的开始和结束。同时,为了防止这些特殊字符在数据中出现而被误认为是帧的开始或结束,我们还需要使用转义字符对它们进行转义。下面是一个简单的解码字符串的示例,其中使用了帧首字符“$”和帧尾字符“#”,转义字符为“\”。 ```python def encode_string(data): encoded = '' for c in data: if c == '$': encoded += '\$' elif c == '#': encoded += '\#' elif c == '\\': encoded += '\\\\' else: encoded += c return '$' + encoded + '#' def decode_string(data): decoded = '' escaped = False for c in data: if not escaped: if c == '\\': escaped = True elif c == '$': decoded = '' elif c == '#': print(decoded) decoded = '' else: decoded += c else: if c == 'n': decoded += '\n' elif c == 'r': decoded += '\r' elif c == '$': decoded += '$' elif c == '#': decoded += '#' elif c == '\\': decoded += '\\' else: decoded += '\\' + c escaped = False return decoded ``` 在上面的代码中,我们首先定义了一个encode_string函数,用于将原始数据data进行编码,并在开头和结尾加上帧首字符“$”和帧尾字符“#”。在编码过程中,我们需要对帧首字符“$”、“#”和转义字符“\”进行转义处理。 接下来,我们又定义了一个decode_string函数,用于解码编码后的数据,并返回原始数据。在解码过程中,我们需要注意帧首字符和帧尾字符的识别,以及转义字符的处理。如果遇到转义字符“\”,则需要将后面的字符作为普通字符处理,如果遇到“\n”或“\r”,则需要将其转换为相应的换行符。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值