czlibcompressionlzo

Validate if the data is compressed with lzo1z compression


I am using lzo1z decompression to decompress the data which is sent via a network. Now I need to check if the data is compressed before I decompress the data. My code is like this:

#include <lzo1z.h>
......
    if (lzo_init() != LZO_E_OK)
    {
        //Error 
    }
    else{

        // ** Validation if compressed with lzo1z **
        lzoFlag = lzo1z_decompress(buffPointer,ret_in,out,&out_len,wrkmem);
        if (lzoFlag == LZO_E_OK)
        {
            // Success
        }
        else
        {
            //Fail
        }
    }
......

Multiple packets are sent in the single payload of the packet that I am receiving. If I send wrong bytes to lzo1z_decompress I am getting Segmentation fault. The error looks like this:

Program received signal SIGSEGV, Segmentation fault.
0x000000059a581339 in lzo1z_decompress ()
#1  0x000000059a5810f8 in decompressLZO1z (self=<optimized out>, args=<optimized out>)

Now If I sen the buffer which is not compressed the program itself is crashing. Is there any way that I can have a validate if the data I have received is compressed before I send the data to the decompression?


Solution

  • Use lzo1x_decompress_safe() instead. lzo1x_decompress() will likely crash if given invalid data, e.g. not LZO-compressed data. It looks like that is exactly what's happening.