0

我有一个工具类。工具类写了lz4的解压方法,但是解压是控制的,不知道哪里出错了(libcompression.tbd和#include "compression.h"都有)。下面是代码:

+ (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 这个值是 0

4

1 回答 1

0

我这个算法选错了,COMPRESSION_LZ4不应该选,COMPRESSION_LZ4_RAW应该选,申请前目标的大小和源数据的大小也错了。我将在下面发送正确的代码:

+ (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;}
于 2018-07-20T10:07:17.200 回答