在c ++应用程序中使用lzo库 [英] using lzo library in c++ application

查看:219
本文介绍了在c ++应用程序中使用lzo库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有lzo库在我们的应用程序中使用。提供的版本是1.07。
他们给了我.lib以及一些头文件和一些.c源文件。



我已经根据规范设置了测试环境。我可以在我的应用程序中看到lzo例程函数。



这是我的测试应用程序

  #includestdafx.h
#includelzoconf.h
#includelzo1z.h
#include< stdlib.h>


int _tmain(int argc,_TCHAR * argv [])
{
FILE * pFile;
long lSize;
unsigned char * i_buff;
unsigned char * o_buff;

int i_len,e = 0;
unsigned int o_len;

size_t result;

//data.txt有一个压缩包
pFile = fopen(data.txt,rb);

if(pFile == NULL)
return -1;

//获取文件大小:
fseek(pFile,0,SEEK_END);
lSize = ftell(pFile);
rewind(pFile);

//分配内存以包含整个文件:
i_buff =(unsigned char *)malloc(sizeof(char)* lSize);
if(i_buff == NULL)
return -1;

//将文件复制到缓冲区中:
result = fread(i_buff,1,lSize,pFile);
if(result!= lSize)
return -1;

i_len = lSize;
o_len = 512;

//为输出缓冲区分配内存
o_buff =(unsigned char *)malloc(sizeof(char)* o_len);

if(o_buff == NULL)
return -1;
lzo_memset(o_buff,0,o_len);
lzo1z_decompress(i_buff,i_len,o_buff,& o_len,NULL);

return 0;
}

最后一行违反了访问权限。

  lzo1z_decompress(i_buff,i_len,o_buff,& o_len,NULL);上述功能提供的库签名中的


  lzo1z_decompress(const lzo_byte * src,lzo_uint src_len,
lzo_byte * dst,lzo_uint * dst_len,
lzo_voidp wrkmem / * NOT USED * /);

有什么问题?

方案

你确定512字节足够大的解压数据吗?您不应该使用任意值,而是您应该在压缩文件时将原始大小存储为头文件:



http://stackoverflow.com/questions/773962/lzo-decompression-buffer-size



你应该使你的数据类型匹配接口规范(例如 o_len 应该是 lzo_uint ...你传递一个地址,所以实际底层类型很重要)。



除此之外,它是开源的。那么,为什么不使用调试信息来构建lzo并进入它来查看问题出在哪里?



http://www.oberhumer.com/opensource/lzo/


I got lzo library to use in our application. The version was provided is 1.07. They have given me .lib along with some header file and some .c source files.

I have setup test environment as per specs. I am able to see lzo routine functions in my application.

Here is my test application

#include "stdafx.h"
#include "lzoconf.h"
#include "lzo1z.h"
#include <stdlib.h>


int _tmain(int argc, _TCHAR* argv[])
{
    FILE * pFile;
    long lSize;
    unsigned char *i_buff;
    unsigned char *o_buff;

    int i_len,e = 0;
    unsigned int o_len;

    size_t result;

    //data.txt have a single compressed packet 
    pFile = fopen("data.txt","rb");

    if (pFile==NULL) 
        return -1;

    // obtain file size:
    fseek (pFile , 0 , SEEK_END);
    lSize = ftell (pFile);
    rewind (pFile);

    // allocate memory to contain the whole file:
    i_buff = (unsigned char*) malloc (sizeof(char)*lSize);
    if (i_buff == NULL) 
        return -1;

    // copy the file into the buffer:
    result = fread (i_buff,1,lSize,pFile);
    if (result != lSize) 
        return -1;

    i_len = lSize;
    o_len = 512;

    // allocate memory for output buffer
    o_buff = (unsigned char*) malloc(sizeof(char)*o_len);

    if (o_buff == NULL) 
        return -1;
     lzo_memset(o_buff,0,o_len);    
    lzo1z_decompress(i_buff,i_len,o_buff,&o_len,NULL);

    return 0;   
}

It gives access violation on last line.

lzo1z_decompress(i_buff,i_len,o_buff,&o_len,NULL);

in provided library signature for above functiion is

lzo1z_decompress        ( const lzo_byte *src, lzo_uint  src_len,
                                lzo_byte *dst, lzo_uint *dst_len,
                                lzo_voidp wrkmem /* NOT USED */ );

What is wrong?

解决方案

Are you sure 512 bytes is big enough for the decompressed data? You shouldn't be using an arbitrary value, but rather you should have stowed away the original size somewhere as a header when your file was compressed:

http://stackoverflow.com/questions/773962/lzo-decompression-buffer-size

You should probably make your data types match the interface spec (e.g. o_len should be a lzo_uint...you're passing an address so the actual underlying type matters).

Beyond that, it's open source. So why don't you build lzo with debug info and step into it to see where the problem is?

http://www.oberhumer.com/opensource/lzo/

这篇关于在c ++应用程序中使用lzo库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆