When Lz4 compression,process short length data,iOS system approach compression_encode_buffer,Compression failure

34 views Asked by At

The official iOS method,compression_encode_buffer,unable to process short data.

For example data {length = 17, bytes = 0x00000000020000000100000000ffe2ff61}

Use compression_encode_buffer to compression,the code is

@implementation NSData (lz4)
- (NSData *)pluLZ4
{
    uint8_t dstBuffer[self.length];
    memset(dstBuffer, 0, self.length);
    if (@available(iOS 9.0, *)) {
        size_t compressResultLength = compression_encode_buffer(dstBuffer, self.length, [self bytes], self.length, NULL, COMPRESSION_LZ4_RAW);
        if (compressResultLength > 0) {
            NSData *dataAfterCompress = [NSData dataWithBytes:dstBuffer length:compressResultLength];
            return dataAfterCompress;
        }
        else {
        }
    }
    else {
        return self;
    }

    return nil;
}

The result of compressResultLength is 0,the result of dstBuffer is empty. When i usd Lz4 Official code,code from https://github.com/lz4/lz4 ,the code is

@implementation NSData (lz4)

- (NSData *)compressLZ4 {
    int res = 0;
    int size = LZ4_compressBound((int) self.length);
    char *buffer = (char *)malloc((size_t) size);
    res = LZ4_compress_default(self.bytes, buffer, (int) self.length, size);
    if (res < 0) {userInfo:@{NSLocalizedDescriptionKey:@"LZ4 compression failed"}];
        free(buffer);
        return nil;
    }
    return [[NSData alloc] initWithBytesNoCopy:buffer length:res freeWhenDone:YES];
}

The result is data {length = 19, bytes = 0xf00200000000020000000100000000ffe2ff61}

Please tell me,why the official iOS method,compression_encode_buffer,unable to process short data?

0

There are 0 answers