gpt4 book ai didi

ios - swift : LZ4 decoding with block mode

转载 作者:行者123 更新时间:2023-11-30 11:00:41 35 4
gpt4 key购买 nike

我在以 block 模式解码 LZ4 编码数据时遇到了困难,并且苹果文档无法帮助我获得结果,

这是我在 iOS 12、Swift 4.1 上的代码:

let intArray: [Int8] =  [-16, 1, 1, 39, 0, 19, 11, -30, 7, 10, 29, 14, 0, 0, 0, 0, 96, 9, 6, 0, 1, 2, 0, 17, 14, 6, 0, 2, 2, 0, 18, 14, 7, 0, 65, 0, 0, 0, -51, 6, 0, 0, 2, 0, 0, 43, 0, 16, 2, 9, 0, -1, 13, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0, 7, 0, 0, 0, 8, 0, 0, 0, 10, 4, 0, 64, 33, -105, 58, 115, 0, 12, 2, 0, 80, 0, 0, 0, 0, 0]

let uintArray = intArray.map { UInt8(bitPattern: $0) }

// For Visibility the uintArray unsigned is [240, 1, 1, 39, 0, 19, 11, 226, 7, 10, 29, 14, 0, 0, 0, 0, 96, 9, 6, 0, 1, 2, 0, 17, 14, 6, 0, 2, 2, 0, 18, 14, 7, 0, 65, 0, 0, 0, 205, 6, 0, 0, 2, 0, 0, 43, 0, 16, 2, 9, 0, 255, 13, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0, 7, 0, 0, 0, 8, 0, 0, 0, 10, 4, 0, 64, 33, 151, 58, 115, 0, 12, 2, 0, 80, 0, 0, 0, 0, 0]

var encodedData = Data.init(bytes:uintArray)

let decodedCapacity = 205
let decodedDestinationBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: decodedCapacity)

let decodedData = encodedData.withUnsafeBytes {
(encodedSourceBuffer: UnsafePointer<UInt8>) -> Data? in

let decodedCharCount = compression_decode_buffer(decodedDestinationBuffer,
decodedCapacity,
encodedSourceBuffer,
encodedData.count,
nil,
COMPRESSION_LZ4)

if decodedCharCount == 0 {
fatalError("Decoding failed.")
}

print("Before: \(encodedSourceBuffer) | After: \(decodedCharCount)")

return Data(bytesNoCopy: decodedDestinationBuffer, count: decodedCharCount, deallocator: .free)

}

引用来源:https://developer.apple.com/documentation/compression/compressing_and_decompressing_data_with_buffer_compression

提前致谢,这将是一个很大的帮助!

当前输出:解码失败

预期输出(以有符号字节为单位):

[1, 39, 0, 19, 11, -30, 7, 10, 29, 14, 0, 0, 0, 0, 96, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, -51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 9, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0, 7, 0, 0, 0, 8, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, -105, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

预期输出(以无符号字节为单位):

[1, 39, 0, 19, 11, 226, 7, 10, 29, 14, 0, 0, 0, 0, 96, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 205, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 9, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0, 7, 0, 0, 0, 8, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 151, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

最佳答案

好的,我再次对提示 Apple 文档表示歉意,

他们明确提到,

压缩 block 头由八位位组 0x62、0x76、0x34 和 0x31 组成,后跟 block 表示的解码(明文)数据的大小(以字节为单位)以及存储在中的编码数据的大小(以字节为单位) block 。 header 将两个大小字段存储为(可能未对齐)32 位小端值。实际的 LZ4 编码数据流紧跟在压缩 block 头之后。

就我而言,我不需要标题,

所以最好的常量是 COMPRESSION_LZ4_RAW ,LZ4 压缩算法,没有帧头。

将算法类型 COMPRESSION_LZ4 更改为 COMPRESSION_LZ4_RAW 解决了该问题

关于ios - swift : LZ4 decoding with block mode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53417761/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com