LZO压缩炭*

时间:2010-11-18 21:42:27

标签: c++ lzo

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

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

  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 *变量。

我想我需要编辑这些行:

  in_len = strToCompressLen;
  lzo_memset(in,0,in_len);

我想在这个变量中压缩字符串:

  char* imageData = (char*)imageIn->getFrame();

我是否需要将其转换为其他类型?

LZO的文档不是很有帮助(或者我可能无法正确使用它)。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

有三个主要的混淆点:

  1. 最好不要使用char * 缓冲区指针指向的是 不是以空字符结尾的字符串 混乱。
  2. strlen()只会给你 以null结尾的字符串的长度, 它不会给你一个大小 内存中的任意缓冲区。你需要 在其他地方获取这些信息。
  3. 您传递给的缓冲区 lzo1x_1_compress()实际需要 包含您想要的数据 压缩而不是空缓冲区 满满的零。
  4. 假设你可以使用imageIn-&gt; getFrameSizeBytes()之类的东西从imageIn获取图像的大小,试试这个:

      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作为工作内存,以便自动释放。