2012-08-03 67 views
0

嗨,大家我基本上是試圖使用zlib庫,但我有麻煩時,我試圖膨脹自我縮小的文件,雖然當我膨脹其他zlib'd文件它工作正常。Zlib充氣和縮小錯誤

的壓縮代碼:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
typedef unsigned char BYTE; 
typedef unsigned int UINT; 
#include "zlib.h" 
#define HEADERSIZE (1024) 
#define CHUNKSIZE 4096 
#define SWAPINT(x) (((x)&0xFF) << 24)|(((x)&0xFF00) << 8) | (((x)&0xFF0000) >> 8) | (((x)&0xFF000000) >> 24) 
#define DEFLATESIZE 65536 
const char *mz_error(int err) 
{ 
    static struct { int m_err; const char *m_pDesc; } s_error_descs[] = 
    { 
    { Z_OK, "" }, { Z_STREAM_END, "stream end" }, { Z_NEED_DICT, "need dictionary" }, { Z_ERRNO, "file error" }, { Z_STREAM_ERROR, "stream error" }, 
    { Z_DATA_ERROR, "data error" }, { Z_MEM_ERROR, "out of memory" }, { Z_BUF_ERROR, "buf error" }, { Z_VERSION_ERROR, "version error" } 
    }; 
    UINT i; for (i = 0; i < sizeof(s_error_descs)/sizeof(s_error_descs[0]); ++i) if (s_error_descs[i].m_err == err) return s_error_descs[i].m_pDesc; 
    return NULL; 
} 

char* myinflate(char* buffer, int bufsize, int* inflatedSize) 
{ 
    BYTE tmpinf[DEFLATESIZE]; 
    char* inflated=(char*)malloc(DEFLATESIZE); 
    int ret,have; 
    z_stream strm; 
    strm.zalloc = Z_NULL; 
    strm.zfree = Z_NULL; 
    strm.opaque = Z_NULL; 
    strm.avail_in = bufsize; 
    strm.next_in = (Bytef*)buffer; 
    strm.avail_out = sizeof(tmpinf); 
    strm.next_out = tmpinf; 
    ret = inflateInit(&strm); 
    printf("%s",mz_error(ret)); 
    ret = inflate(&strm, Z_FINISH); 
    printf("%s",mz_error(ret)); 
    if(ret == Z_DATA_ERROR) 
     printf(strm.msg); 
    inflateEnd(&strm); 
    have = strm.total_out; 
    printf("%d\n",have); 
    memcpy(inflated,tmpinf,have); 
    *inflatedSize = have; 
    return inflated; 
} 
char* mydeflate(char* buffer, int bufsize, int* deflatedSize) 
{ 
    BYTE tmpdef[CHUNKSIZE*4]; 
    char* deflated=(char*)malloc(DEFLATESIZE); 
    int have,ret; 
    z_stream strm; 
    strm.zalloc = Z_NULL; 
    strm.zfree = Z_NULL; 
    strm.opaque = Z_NULL; 
    strm.avail_in = bufsize; 
    strm.next_in = (Bytef*)buffer; 
    strm.avail_out = sizeof(tmpdef); 
    strm.next_out = tmpdef; 
    ret = deflateInit(&strm,Z_DEFAULT_COMPRESSION); 
    printf("%s",mz_error(ret)); 
    ret = deflate(&strm, Z_FINISH); 
    printf("%s",mz_error(ret)); 
    ret = deflateEnd(&strm); 
    printf("%s",mz_error(ret)); 
    have = strm.total_out; 
    printf("%d\n",have); 
    memcpy(deflated,tmpdef,have); 
    *deflatedSize = have; 
    return deflated; 
} 
int main() 
{ 
    FILE* fptr = fopen("test.in","rb"); 
    fseek (fptr, 0, SEEK_END); 
    int size = ftell(fptr); 
    fseek (fptr, 0, SEEK_SET); 
    char* buffer = (char*)malloc(size); 
    fread(buffer,1,size,fptr); 
    int infsize,defsize; 
    char* buf = myinflate(buffer,size,&infsize); 
    FILE* out = fopen("testinf.hex","wb"); 
    fwrite(buf,1,infsize,out); 
    fclose(out); 
    char* buf2 = mydeflate(buf,infsize,&defsize); 
    out = fopen("testdef.hex","wb"); 
    fwrite(buf2,1,defsize,out); 
    fclose(out); 
    int buf3size; 
    char* buf3 = myinflate(buf2,defsize,&buf3size); 
    out = fopen("testinfdef.hex","wb"); 
    fwrite(buf3,1,buf3size,out); 
} 

http://www.filehosting.org/file/details/364574/eEyzAbMuCp93MmGk/test.in

回答

5

你不檢查任何返回代碼。你怎麼可能期望知道發生了什麼? 檢查所有可能返回錯誤的函數的返回碼!

可能沒有足夠的空間提供壓縮和/或解壓縮。來自inflate()compress()的返回碼指示是否是這種情況。

順便說一下,你malloc()inflated兩次,覆蓋第一個導致大量內存泄漏。

另外你blithely轉換一個int指針爲無符號長指針。如果他們有不同的長度,那麼你會有一個問題。

+0

malloc()是一個可怕的複製粘貼錯誤,謝謝你的注意,它已經被編輯。但是長指針與我的編譯器和操作系統中的int指針是一樣的,我現在不打算將它移植到它。無論如何它說它是在最後一次膨脹()中的數據錯誤,我不知道爲什麼會發生這種情況。 – chubakueno 2012-08-04 20:35:48

+0

現在刪除了一個'malloc()','tmpinf'的存在就沒有意義了。你可以直接解壓縮到'膨脹'。 'tmpdef'一樣的東西。 – 2012-08-04 22:30:56

+0

您將其全部更改爲打印返回碼。但是,您並未更改輸出以顯示返回代碼的內容。什麼是'mz_error()'?另外如果返回代碼是'Z_DATA_ERROR',你也應該打印'strm.msg'。 – 2012-08-04 22:32:24