SZ_ERROR_DATA from LzmaDecode function (C lzma)

Viewed 315

I have an array of numbers which i want to compress and store in a file then decompress and put data in a buffer.

RAM->FILE compressing

FILE->RAM decompressing

I used LzmaEncode for compressing and then write compress buffer in a file.

lzmaencoder.c

int main(void){
int data[] = {1,2,3,4,5};

size_t src_len = sizeof(data);
const Byte * src = (Byte *) data;

size_t compressed_size = src_len*1.5;
Byte * compressed_data = malloc(compressed_size);

CLzmaEncProps props;   
LzmaEncProps_Init(&props);

Byte header[LZMA_PROPS_SIZE];
size_t headerSize = LZMA_PROPS_SIZE;

// Call encoding function:
SRes res;
res = LzmaEncode(compressed_data, &compressed_size, src, src_len, 
                &props, header, &headerSize, 0, NULL,  &g_Alloc, &g_Alloc);

if(res == SZ_OK){
    FILE * file = fopen("lzma_file","wb");
    fwrite(compressed_data, sizeof(Byte), compressed_size, file);
}

free(compressed_data);   
return (0);
}

I read compress file and write data in a buffer, then use LzmaDecode for decompressing.

lzmadecoder.c

int main(int argc, char * argv[]){
// I know the original data size
size_t uncompress_size = 20;
Byte * uncompress = malloc(uncompress_size);

// open compressed file
size_t compress_size;
FILE * compress_file = fopen("lzma_file", "rb");

if(!compress_file){
    printf("Error: can not open lzma_file file.");
}

// get compressed file size
fseek(compress_file, 0, SEEK_END);
compress_size = ftell(compress_file);
fseek(compress_file, 0, SEEK_SET);


Byte *compress_data = malloc(compress_size);

// put compressed file data on RAM
if(!fread(compress_data, sizeof(Byte), compress_size, compress_file)){
    printf("Error: can not read lzma_file file.");
}
fclose(compress_file);

ELzmaStatus status;

// RAM->RAM decompressing
int res = LzmaDecode(uncompress, &uncompress_size, 
                    compress_data, &compress_size, 
                    compress_data, 5, 
                    LZMA_FINISH_END, &status, &g_Alloc);
if(res==SZ_OK){
    printf("SZ_OK!\n");
} else if(res==SZ_ERROR_DATA){
    printf("SZ_ERROR_DATA!\n");
}

free(uncompress);
free(compress_data);
return (0);
}

but LzmaDecode return SZ_ERROR_DATA. I cant find my problem.

1 Answers
LZMA compression---------------- Version: 9.35
Use the Lib API generated by the project "lzma1900\C\Util\LzmaLib": LzmaCompress and LzmaUncompress.

int main()
{
    Byte outProps[5];
    size_t outPropsSize = 5;    /* *outPropsSize must be = 5 */
    int r =RawEncode(out,&tmpLen,bnInfo, len1,outProps,&outPropsSize);
    Pkt init =RawDecode(out2,$outLen2, out, tmpLen, &outPropsSize);
    return 0;
}

int RawEncode(char* out, int *ol, char *buf, uint32_t l, char* outProps, size_t* outPropsSize)
{
    int res = 0;
    m_l = 8;
    int level = m_level;     /* 0 <= level <= 9, default = 5 */
    unsigned dictSize = 1 << 24;  /* default = (1 << 24) */
    int lc = 3;       /* 0 <= lc <= 8, default = 3  */
    int lp = 0;        /* 0 <= lp <= 4, default = 0  */
    int pb = 2;        /* 0 <= pb <= 4, default = 2  */
    int fb = 32;        /* 5 <= fb <= 273, default = 32 */
    int numThreads = 1; /* 1 or 2, default = 2 */
    int r = LzmaCompress(out,ol,buf,l,outProps, outPropsSize, m_level, dictSize, lc, lp, pb, fb, 1);
    olen = l;
    return r;
}

void RawDecode(char* out, int* oLen, char* buf, int len, unsigned char* props, size_t* propsSize)
{
    char out[outLen];
    SizeT srcLen = pkt.first.len - 8;
    int r = LzmaUncompress(out, oLen, buf, &len, props, *propsSize);
    if(SZ_OK != r)
    {
        printf("LzmaUncompress error %d.", r);
        delete [] tmp.second;
        tmp.second = NULL;
    }
}
Related