简体   繁体   中英

Using the iOS9 Compression Framework in Swift2

I have some NSData that is stored with the DEFLATE compression protocol?

DEFLATE Compression Method DEFLATE is a lossless compressed data format that compresses data using a combination of the LZ77 algorithm and Huffman coding, with efficiency comparable to the best currently available general-purpose compression methods. The data can be produced or consumed, even for an arbitrarily long sequentially presented input data stream, using only a priority-bounded amount of intermediate storage. The format can be implemented readily in a manner not covered by patents. Specifications for DEFLATE can be found in RFC 1951 - DEFLATE Compressed Data Format Specification, May 1996.

If I understand it correctly in IOS9 there is a new Compression Framework which "might" handle this case. The documentation lists the following supported algorithms: LZFSE, LZ4, LZMA, and ZLIB level 5.

I'm not sure but I believe ZLIB supports the LZ77 Deflate algorithm. The question I have is how do I actually use this framework:

So I believe the function i want to use is compression_decode_buffer

@available(iOS 9.0, *)
public func compression_decode_buffer(
    dst_buffer: UnsafeMutablePointer<UInt8>, 
    _ dst_size: Int, 
    _ src_buffer: UnsafePointer<UInt8>, 
    _ src_size: Int, 
    _ scratch_buffer: UnsafeMutablePointer<Void>, 
    _ algorithm: compression_algorithm) -> Int

but I'm not sure exactly how to utilize this algorithm.

So from reading the header it looks like i need an input size dst_size: bytes.size and output size a inputBuffer an &outputbuffer and a compression algorithm

dst_buffer: UnsafeMutablePointer, _ dst_size: Int, _ src_buffer: UnsafePointer, _ src_size: Int, _ scratch_buffer: UnsafeMutablePointer, _ algorithm: compression_algorithm) -> Int

Assuming i have some sample data (see below)

let bytes : [UInt8] = [ .... ]  // see below

compression_decode_buffer(
  <DST_BUFFER>,
  <DST_SIZE>, 
  bytes, 
  bytes.count, 
  <SCRATCH_BUFFER>, 
  COMPRESSION_ZLIB
)

Where i'm at a loss is as to what goes into <DST_BUFFER> , <DST_SIZE> , <SCRATCH_BUFFER> .

Any suggestions?

Sample Data

let bytes : [UInt8] = [0x7e, 0x07, 0x07, 0xff, 0xff, 0x41, /* <1~....A */
    0x10, 0x33, 0x51, 0x3e, 0x94, 0xb2, 0xa0, 0x27, /* .3Q>...' */
    0x80, 0x00, 0x21, 0x65, 0x26, 0xd8, 0x22, 0x10, /* ..!e&.". */
    0x2c, 0xd5, 0x99, 0x00, 0x00, 0x44, 0xbb, 0xd4, /* ,....D.. */
    0x54, 0x38, 0xf5, 0x01, 0x36, 0xd1, 0x20, 0x2c, /* T8..6. , */
    0xd5, 0x99, 0xbb, 0x1c, 0xaf, 0xc3, 0x2c, 0x60, /* ......,` */
    0xcb, 0x0c, 0x79, 0xcb, 0x76, 0xa0, 0x84, 0xd5, /* ..y.v... */
    0x99, 0x83, 0x1c, 0xaf, 0xc3, 0x2c, 0x60, 0x35, /* .....,`5 */
    0x66, 0x60, 0x49, 0x76, 0x60, 0xc7, 0x5b, 0xf3, /* f`Iv`.[. */
    0xce, 0x05, 0x08, 0x3a, 0x04, 0x13, 0x4a, 0x00, /* ...:..J. */
    0x92, 0x05, 0x08, 0x17, 0x14, 0x68, 0x31, 0xc3, /* .....h1. */
    0x1c, 0xb2, 0xc3, 0x1e, 0x72, 0xdd, 0xe0, 0x00, /* ....r... */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
    0x00, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00, /* ....'... */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, /* .......0 */
    0x8e, 0x7e]

Generally compression_decode_buffer() is used like this:

import Compression

let bytes : [UInt8] = [ .... ]  // Compressed data
var dst = [UInt8](count: 1000, repeatedValue: 0) // destination buffer
let size = compression_decode_buffer(&dst, dst.count, bytes, bytes.count, nil, COMPRESSION_ZLIB)

The destination buffer must be large enough for the decompressed data. On return, size is the number of bytes that were written to the destination buffer (or zero if the decompression failed).

(There is also a "streaming" interface

compression_stream_init()
compression_stream_process()
compression_stream_destroy()

which can be used to process the data in chunks.)

However, I tried to decompress your data with all available COMPRESSION_XXX methods without success.

From my experiments it seems that COMPRESSION_ZLIB corresponds to the "raw deflate" method, ie what you get with the zlib deflateInit2() function if the windowBits parameter is set to a negative value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM