c - 为什么我的解压缩显示错误?

标签 c compression

这里还提供了代码,运行完美并显示错误 - http://ideone.com/gWEoIV

#include <stdio.h>
#include <stdlib.h>

unsigned char *MyCompress(unsigned int num, unsigned char *buffer);
unsigned int MyDeCompress(unsigned char **buffer);

int main(void) {
    unsigned char *abc, *abc2;
    unsigned len;
    abc=(unsigned char *)malloc(12);
    abc2=abc;
    len=25960;
    abc = MyCompress(len, abc);
    len=385;
    abc = MyCompress(len, abc);
    len=900;
    abc = MyCompress(len, abc);
    len=20560;
    abc = MyCompress(len, abc);
    len=384;
    abc = MyCompress(len, abc);
    abc=abc2;

    len = MyDeCompress(&abc);
    len = MyDeCompress(&abc);
    len = MyDeCompress(&abc);
    len = MyDeCompress(&abc);
    len = MyDeCompress(&abc);


    return 0;
}

unsigned int MyDeCompress(unsigned char **buffer){
    unsigned int c;
    unsigned int num = 0;
    unsigned char *p = *buffer;

    printf("\n\nDeCompression\n===============\n");

    do{
        c = ((unsigned char) *p++);
        printf("Read=%d\n", c);
        if (c <= 0) {
            printf("Error: c is < 0 in uncompress1()\n");
            exit(0);
        }
        num <<= 7;
        num |= c & 127;

        printf("uncompress: c = %d num = %d\n", c, num);

        if (!num)
            break;
    }while (c & 128);

    *buffer = p;

    printf("Returning %d\n", num);

    return num;
}


unsigned char *MyCompress(unsigned int num, unsigned char *buffer){
    int i = 0;
    unsigned int r = num;
    unsigned char temp;
    unsigned char s[5];

    printf("Compression\n===============\n");

    printf("received %d to compress\n", num);

    if(!r){
        *buffer++ = 0;
        return buffer;
    }

    while (r){
        s[i] = r & 127;
        r >>= 7;
        printf("s[%d]=%d; r=%d\n", i, s[i], r);
        i++;
    }

    while (--i >= 0){
        temp = (unsigned char)(s[i] | (i ? 128 : 0));
        printf("temp=%d\n", temp);
        *buffer++=temp;
    }
    return buffer;
}

//输出:

Compression
===============
received 25960 to compress
s[0]=104; r=202
s[1]=74; r=1
s[2]=1; r=0
temp=129
temp=202
temp=104
Compression
===============
received 385 to compress
s[0]=1; r=3
s[1]=3; r=0
temp=131
temp=1
Compression
===============
received 900 to compress
s[0]=4; r=7
s[1]=7; r=0
temp=135
temp=4
Compression
===============
received 20560 to compress
s[0]=80; r=160
s[1]=32; r=1
s[2]=1; r=0
temp=129
temp=160
temp=80
Compression
===============
received 384 to compress
s[0]=0; r=3
s[1]=3; r=0
temp=131
temp=0


DeCompression
===============
Read=129
uncompress: c = 129 num = 1
Read=202
uncompress: c = 202 num = 202
Read=104
uncompress: c = 104 num = 25960
Returning 25960


DeCompression
===============
Read=131
uncompress: c = 131 num = 3
Read=1
uncompress: c = 1 num = 385
Returning 385


DeCompression
===============
Read=135
uncompress: c = 135 num = 7
Read=4
uncompress: c = 4 num = 900
Returning 900


DeCompression
===============
Read=129
uncompress: c = 129 num = 1
Read=160
uncompress: c = 160 num = 160
Read=80
uncompress: c = 80 num = 20560
Returning 20560


DeCompression
===============
Read=131
uncompress: c = 131 num = 3
Read=0
Error: c is < 0 in uncompress1()

为什么我在解压缩 384 号码时收到错误:

最佳答案

你已经问过这个问题,我已经回答了 here 。零是编码方案中预期的字节,不得拒绝。只需删除 if (c <= 0)部分。

关于c - 为什么我的解压缩显示错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26383497/

相关文章:

php - 禁用 mod_deflate 和 mod_gzip 压缩 HTML、CSS 和 JS 的最佳方法

algorithm - 交织/多路复用压缩流

c - C 中的管道 — 在我的程序中读取和写入

c - 关于 K&R C 中 getop() 的查询

c - 使用 rand() 时输出范围内的随机数

c - 在 Linux 上检测键盘按键按下和释放

wcf - 如何为 WCF 服务启用 IIS 压缩?

在 32 位和 64 位 Mac 上创建 hello world

Powershell system.io.compression 压缩文件和/或文件夹

algorithm - 数据压缩 - 指数分布的机器学习