Search code examples
ioslz4

iOS (OC) compression_decode_buffer() Returns a null value


I have a tool class. The tool class has written a method of decompression of lz4, but the decompression is controlled, and I don't know what is wrong (libcompression.tbd and #include "compression.h" both have). Below is the code:

+ (NSData *)getDecompressedData:(NSData *)compressed
{
    size_t dst_buffer_size = 168*217;
    uint8_t *dst_buffer = (uint8_t *)malloc(dst_buffer_size);
    uint8_t *src_buffer = (uint8_t *)malloc(compressed.length);

    size_t compressResultLength = compression_decode_buffer(dst_buffer, dst_buffer_size, src_buffer, dst_buffer_size, NULL, COMPRESSION_LZ4);
    NSData *decompressed = [[NSData alloc] initWithBytes:dst_buffer length:compressResultLength];
    return decompressed;
}

CompressResultLength this value is 0


Solution

  • I chose this algorithm incorrectly, COMPRESSION_LZ4 should not be selected, COMPRESSION_LZ4_RAW should be selected, the size of the target and the size of the source data were also wrong before the application. I will send the correct code below:

    + (NSData *)getDecompressedData:(NSData *)compressed{
    size_t destSize = 217*168;
    uint8_t *destBuf = malloc(sizeof(uint8_t) * destSize);
    const uint8_t *src_buffer = (const uint8_t *)[compressed bytes];
    size_t src_size = compressed.length;
    
    size_t decompressedSize = compression_decode_buffer(destBuf, destSize, src_buffer, src_size,
                                                        NULL, COMPRESSION_LZ4_RAW);
    MyLog(@"after decompressed. length = %d",decompressedSize) ;
    NSData *data = [NSData dataWithBytesNoCopy:destBuf length:decompressedSize freeWhenDone:YES];
    return data;}