1

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のドキュメントはあまり役に立ちません(または、適切に使用できない可能性があります)。

4

2 に答える 2

2

getFrame() はどの型を返しますか? なぜにキャストする必要があるのchar*ですか?最も明らかな問題はstrlen()、バイナリ データでの使用です。strlen は、最初のゼロ バイトに遭遇すると停止します。

于 2010-11-18T21:56:04.017 に答える
2

混乱の主なポイントは次の 3 つです。

  1. ポインターが指しているバッファーが null で終わる文字列でない場合は、紛らわしいので char* を使用しないことをお勧めします。
  2. strlen() は、null で終わる文字列の長さのみを提供します。メモリ内の任意のバッファーのサイズは提供しません。他の場所でその情報を取得する必要があります。
  3. lzo1x_1_compress() に渡すバッファには、ゼロでいっぱいの空のバッファではなく、実際に圧縮するデータが含まれている必要があります。

imageIn->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 を使用して、自動的に解放されるようにします。

于 2010-11-18T22:17:47.000 に答える