[英]iOS (objective-c) compression_decode_buffer() returns zero
我正在將服務器上的一個非常大的 json 結果轉換為壓縮格式,我可以在我的目標 c 應用程序上解壓縮該格式。 如果可能,我更願意使用iOS 9 壓縮庫(libcompression.tbd),在Apple 的 CompressionSample/BlockCompression.c 示例代碼中進行了描述。
我將壓縮的 NSData 結果傳遞給以下方法:
#include "compression.h"
...
- (NSData *) getDecompressedData:(NSData *) compressed {
size_t dst_buffer_size = 20000000; //20MB
uint8_t *dst_buffer = malloc(dst_buffer_size);
uint8_t *src_buffer = malloc(compressed.length);
[compressed getBytes:src_buffer length:compressed.length];
size_t decompressedSize = compression_decode_buffer(dst_buffer, dst_buffer_size, src_buffer, compressed.length, nil, COMPRESSION_ZLIB);
NSData *decompressed = [[NSData alloc] initWithBytes:dst_buffer length:decompressedSize];
return decompressed;
}
compressed
參數的長度與我的服務器日志匹配,但compression_decode_buffer
的結果始終為零,並且dst_buffer
未修改。 我沒有收到任何錯誤,日志也沒有相關信息。
我在服務器端嘗試了 ZLIB 和 LZ4 壓縮/解壓縮方法和幾個庫,結果都一樣。
我在這里做錯了什么?
經過大量測試和研究,我發現我在服務器上使用的壓縮庫根據RFC1950添加了一個壓縮標頭(第一個兩個字節)。 我跳過了這兩個字節, compression_decode_buffer
像冠軍一樣工作!
- (NSData *) getDecompressedData:(NSData *) compressed {
size_t dst_buffer_size = 20000000; //20MB
uint8_t *dst_buffer = malloc(dst_buffer_size);
uint8_t *src_buffer = malloc(compressed.length);
[compressed getBytes:src_buffer range:NSMakeRange(2, compressed.length - 2)];
size_t decompressedSize = compression_decode_buffer(dst_buffer, dst_buffer_size, src_buffer, compressed.length - 2, nil, COMPRESSION_ZLIB);
NSData *decompressed = [[NSData alloc] initWithBytes:dst_buffer length:decompressedSize];
return decompressed;
}
非常感謝,azcoastal - 為我節省了大量時間!
這是一些有效的 Swift 代碼..
let bytes = [UInt8](data) // Data -> [Uint8]
// Need to remove the first 2 bytes (a header) from the array!!
let slice = bytes[2...bytes.count-1]
let noheader = Array(slice)
let dst_count = bytes.count * MULTIPLY
var dst = [UInt8](repeating: 0, count: dst_count) // destination
let size = compression_decode_buffer(&dst, dst_count,
noheader, noheader.count, nil, COMPRESSION_ZLIB)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.