tcpip数据包编码解析(chunk and gzip)

本文深入探讨TCP/IP数据包中的Chunk编码和GZIP压缩技术,解析这两种编码方式如何在网络通信中提高数据传输效率和压缩数据体积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

tcpip数据包编码解析(chunk and gzip)
做tcpip协议栈数据包重组时遇到一些编码问题,主要是chunk和gzip编码。
 
先看chunk:
RFC2616中对Chunked的定义:
Chunked-Body = *chunk
last-chunk
trailer
CRLF

chunk = chunk-size [ chunk-extension ] CRLF
chunk-data CRLF
chunk-size = 1*HEX
last-chunk = 1*("0") [ chunk-extension ] CRLF

chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
chunk-ext-name = token
chunk-ext-val = token | quoted-string
chunk-data = chunk-size(OCTET)
trailer = *(entity-header CRLF)

以下是解码过程的伪代码:
length := 0//用来记录解码后的数据体长度
read chunk-size, chunk-extension (if any) and CRLF//第一次读取块大小
while (chunk-size > 0) {//一直循环,直到读取的块大小为0
read chunk-data and CRLF//读取块数据体,以回车结束
append chunk-data to entity-body//添加块数据体到解码后实体数据
length := length + chunk-size//更新解码后的实体长度
read chunk-size and CRLF//读取新的块大小
}
read entity-header//以下代码读取全部的头标记
while (entity-header not empty) {
append entity-header to existing header fields
read entity-header
}
Content-Length := length//头标记中添加内容长度
Remove "chunked" from Transfer-Encoding//头标记中移除Transfer-Encoding
伪码的逻辑有点混乱,研究了下,自己写了C语言的解码代码:
//////////////////////////////////////
char * unchunk (char *filename)
{
        char cmdbuf[1024];
        /*if (strstr (filename, ".trunk") == 0)
        {
                strcat (filename, ".trunk");
                memset (cmdbuf, 0x0, sizeof (tmpfile));
&n
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值