Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3440208
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:24:17+00:00 2026-05-18T08:24:17+00:00

I have installed LZO on my Ubuntu machine and I would like to use

  • 0

I have installed LZO on my Ubuntu machine and I would like to use ti to compress a char* type string.

In the example files I have found this code snippet (I have already edited it just a little for my application):

  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;
  }

But what it does is it just fill in zeros. I need to compress a char* variable.

I guess I need to edit these lines:

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

I have the string I want to compress in this variable:

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

Do I need to cast it to some other type?

The documentation to LZO is not very helpful (or maybe I just can’t use it properly).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:24:18+00:00Added an answer on May 18, 2026 at 8:24 am

    There are three main points of confusion:

    1. It’s best not to use char* when the
      buffer the pointer is pointing to is
      not a null-terminated string as it’s
      confusing.
    2. strlen() will only give you the
      length of a null-terminated string,
      it won’t give you the size of an
      arbitrary buffer in memory. You need
      to get that information elsewhere.
    3. The buffer you pass to
      lzo1x_1_compress() actually needs to
      contain the data you want to
      compress and not an empty buffer
      full of zeros.

    Assuming you can get the size of your image from imageIn using something like imageIn->getFrameSizeBytes(), try this:

      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;
      }
    

    Don’t forget to free wrkmem. Even better, use C++ and std::vector for the working memory so it is freed automatically.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have installed xampp on my Ubuntu machine, and would like to be able
I have installed the ODAC 11 and I would like to use it with
have installed Web Developer on Firefox 5.0 and personally found it redundant. would like
I have installed msysGit 1.7.10 on my Windows 7 machine. What I need to
I have installed Xampp for ubuntu. It is located in /opt/lampp. I want to
I have installed a C# windows forms app on a client machine which does
I have installed the latest pySerial on my Ubuntu box with python 2.7.2, and
I have installed the package ccl with Mac ports. Now I want to use
I have installed drupal on my localmachine(ubuntu, Xampp), at localhost. Path and Pathauto modules
I have Installed GTK on a Linux machine. I am connecting To Linux Machine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.