首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LZO压缩字符*

LZO压缩字符*
EN

Stack Overflow用户
提问于 2010-11-19 05:42:27
回答 2查看 3.5K关注 0票数 1

我已经在我的Ubuntu机器上安装了LZO,并且我想使用ti来压缩char*类型的字符串。

在示例文件中,我找到了以下代码片段(我已经为我的应用程序对其进行了一点编辑):

代码语言:javascript
复制
  int r;
  lzo_bytep in;
  lzo_bytep out;
  lzo_voidp wrkmem;
  lzo_uint in_len;
  lzo_uint out_len;
  lzo_uint new_len;
  int strToCompressLen; // I have added this

  /*
   * Step 1: initialize the LZO library
   */
  if (lzo_init() != LZO_E_OK)
  {
    cout << "internal error - lzo_init() failed !!!" << endl;
    cout << "(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)" << endl;
    //return 4;
  }

  // here I get the data I want to compress
  char* imageData = (char*)imageIn->getFrame();

  /*
   * Step 2: allocate blocks and the work-memory
   */
  strToCompressLen = strlen(imageData);
  in = (lzo_bytep) xmalloc(strToCompressLen);
  out = (lzo_bytep) xmalloc((strToCompressLen + strToCompressLen / 16 + 64 + 3));
  wrkmem = (lzo_voidp) xmalloc(LZO1X_1_MEM_COMPRESS);
  if (in == NULL || out == NULL || wrkmem == NULL)
  {
        cout << "out of memory" << endl;
        //return 3;
  }

  /*
   * Step 3: prepare the input block that will get compressed.
   *         We just fill it with zeros in this example program,
   *         but you would use your real-world data here.
   */
  in_len = strToCompressLen;
  lzo_memset(in,0,in_len);

  /*
   * Step 4: compress from 'in' to 'out' with LZO1X-1
   */
  r = lzo1x_1_compress(in,in_len,out,&out_len,wrkmem);
  if (r != LZO_E_OK)
  {
        /* this should NEVER happen */
        cout << "internal error - compression failed: " << r << endl;
        //return 2;
  }
  /* check for an incompressible block */
  if (out_len >= in_len)
  {
        cout << "This block contains incompressible data." << endl;
    //return 0;
  }

但它所做的只是填充零。我需要压缩一个char*变量。

我想我需要编辑下面这几行:

代码语言:javascript
复制
  in_len = strToCompressLen;
  lzo_memset(in,0,in_len);

我在这个变量中有要压缩的字符串:

代码语言:javascript
复制
  char* imageData = (char*)imageIn->getFrame();

我需要将它转换为其他类型吗?

LZO的文档不是很有用(或者我只是不能正确地使用它)。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-11-19 06:17:47

主要有三点混淆:

当指针指向的缓冲区不是以结尾的字符串时,最好不要使用

  1. *,因为这会让人感到困惑。
  2. strlen()只会给出一个以空结尾的字符串的长度,而不会给出内存中任意缓冲区的大小。您需要从其他地方获取该信息。
  3. 您传递给lzo1x_1_compress()的缓冲区实际上需要包含您要压缩的数据,而不是一个充满零的空缓冲区。

假设您可以使用imageIn->getFrameSizeBytes()这样的方法从imageIn获取图像的大小,试试这个:

代码语言:javascript
复制
  int r;
  lzo_bytep out;
  lzo_voidp wrkmem;
  lzo_uint out_len;
  lzo_uint new_len;

  /*
   * Step 1: initialize the LZO library
   */
  if (lzo_init() != LZO_E_OK)
  {
    cout << "internal error - lzo_init() failed !!!" << endl;
    cout << "(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)" << endl;
    //return 4;
  }

  // here I get the data I want to compress
  lzo_bytep imageData = (lzo_bytep) imageIn->getFrame();
  size_t uncompressedImageSize = imageIn->getFrameSizeBytes();

  /*
   * Step 2: allocate blocks and the work-memory
   */
  out = (lzo_bytep) xmalloc((uncompressedImageSize + uncompressedImageSize / 16 + 64 + 3));
  wrkmem = (lzo_voidp) xmalloc(LZO1X_1_MEM_COMPRESS);
  if (out == NULL || wrkmem == NULL)
  {
        cout << "out of memory" << endl;
        //return 3;
  }

  /*
   * Step 4: compress from 'imageData' to 'out' with LZO1X-1
   */
  r = lzo1x_1_compress(imageData,uncompressedImageSize,out,&out_len,wrkmem);
  if (r != LZO_E_OK)
  {
        /* this should NEVER happen */
        cout << "internal error - compression failed: " << r << endl;
        //return 2;
  }

  /* check for an incompressible block */
  if (out_len >= uncompressedImageSize)
  {
        cout << "This block contains incompressible data." << endl;
    //return 0;
  }

别忘了释放wrkmem。更好的是,使用C++和std::vector作为工作内存,这样它就会自动释放。

票数 2
EN

Stack Overflow用户

发布于 2010-11-19 05:56:04

getFrame()返回什么类型?为什么需要将其转换为char*?最明显的问题是在二进制数据上使用strlen() -- strlen在遇到第一个零字节时停止。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4219876

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档