Skip to content

Commit

Permalink
Fixed a bug that caused a crash when decompressing gzip data caused b…
Browse files Browse the repository at this point in the history
…y an Swift 5.7 internal spec change.
  • Loading branch information
mw99 committed Jun 29, 2022
1 parent 2c0d48b commit 4ac2c9c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DataCompression.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "DataCompression"
s.version = "3.6.0"
s.version = "3.7.0"
s.summary = "Swift libcompression wrapper as an extension for the Data type (GZIP, ZLIB, LZFSE, LZMA, LZ4, deflate, RFC-1950, RFC-1951, RFC-1952)"
s.authors = { "Markus Wanke" => "[email protected]" }
s.homepage = "https://github.com/mw99/DataCompression"
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PackageDescription

let package = Package(
name: "DataCompression",
platforms: [.macOS(.v10_11), .iOS(.v9), .tvOS(.v9), .watchOS(.v2)],
platforms: [.macOS(.v10_11), .iOS(.v9), .tvOS(.v9), .watchOS(.v2)],
products: [
.library(
name: "DataCompression",
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#### Swift version support
| Library Version | Swift Version |
|-----------------|---------------|
| 3.7.0 | 5.7 (Xcode 14)|
| 3.6.0 | 5.1 (Xcode 11)|
| 3.5.0 | 5.0 |
| 3.1.0 | 4.2 |
Expand Down Expand Up @@ -190,6 +191,8 @@ You only need one file located at `Sources/DataCompression.swift`. Drag and drop

## Change log / Upgrading guide

##### Version `3.6.0` to `3.7.0`
- Support for Xcode 14 with Swift 5.7

##### Version `3.5.0` to `3.6.0`
- Target platforms finally added to the SPM Package file
Expand Down
12 changes: 5 additions & 7 deletions Sources/DataCompression/DataCompression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public extension Data
/// - returns: uncompressed data
func gunzip() -> Data?
{
// 10 byte header + data + 8 byte footer. See https://tools.ietf.org/html/rfc1952#section-2
// 10 byte header + data + 8 byte footer. See https://tools.ietf.org/html/rfc1952#section-2
let overhead = 10 + 8
guard count >= overhead else { return nil }

Expand All @@ -195,17 +195,15 @@ public extension Data
// +---+---+---+---+---+---+---+---+---+---+
return (id1: ptr[0], id2: ptr[1], cm: ptr[2], flg: ptr[3], xfl: ptr[8], os: ptr[9])
}

typealias GZipFooter = (crc32: UInt32, isize: UInt32)
let ftr: GZipFooter = withUnsafeBytes { (bptr: UnsafePointer<UInt8>) -> GZipFooter in
let ftr: GZipFooter = advanced(by: count - 8).withUnsafeBytes { (ptr: UnsafePointer<UInt32>) -> GZipFooter in
// +---+---+---+---+---+---+---+---+
// | CRC32 | ISIZE |
// +---+---+---+---+---+---+---+---+
return bptr.advanced(by: count - 8).withMemoryRebound(to: UInt32.self, capacity: 2) { ptr in
return (ptr[0].littleEndian, ptr[1].littleEndian)
}
return (ptr[0].littleEndian, ptr[1].littleEndian)
}

// Wrong gzip magic or unsupported compression method
guard hdr.id1 == 0x1f && hdr.id2 == 0x8b && hdr.cm == 0x08 else { return nil }

Expand Down
26 changes: 26 additions & 0 deletions Tests/DataCompressionTests/CompressionTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,5 +283,31 @@ class CompressionTest: XCTestCase
{
XCTAssertEqual(CompressionTest.blob16mb, CompressionTest.blob16mb.lzfse_delzfse())
}

func testGzipCrcFail()
{
let b = 1024 * 16
let ints = [UInt32](repeating: 0xcafeabee, count: b / 4)
var zipped_blob = Data(bytes: ints, count: b).gzip()!

let wrong_crc = Data(bytes: [0xcafeabee], count: 1)
let range = (zipped_blob.count - 8)..<(zipped_blob.count - 4)
zipped_blob.replaceSubrange(range, with: wrong_crc)

XCTAssertNil(zipped_blob.gunzip())
}

func testGzipISizeFail()
{
let b = 1024 * 16
let ints = [UInt32](repeating: 0xcafeabee, count: b / 4)
var zipped_blob = Data(bytes: ints, count: b).gzip()!

let wrong_isize = Data(bytes: [0xcafeabee], count: 1)
let range = (zipped_blob.count - 4)..<(zipped_blob.count)
zipped_blob.replaceSubrange(range, with: wrong_isize)

XCTAssertNil(zipped_blob.gunzip())
}
}

2 changes: 2 additions & 0 deletions Tests/DataCompressionTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ extension CompressionTest
("testRandomDataBlob_16MB_lz4_delz4", testRandomDataBlob_16MB_lz4_delz4),
("testRandomDataBlob_16MB_lzma_delzma", testRandomDataBlob_16MB_lzma_delzma),
("testRandomDataBlob_16MB_lzfse_delzfse", testRandomDataBlob_16MB_lzfse_delzfse),
("testGzipCrcFail", testGzipCrcFail),
("testGzipISizeFail", testGzipISizeFail),
]
}

0 comments on commit 4ac2c9c

Please sign in to comment.