Skip to content

Commit 4ac2c9c

Browse files
committed
Fixed a bug that caused a crash when decompressing gzip data caused by an Swift 5.7 internal spec change.
1 parent 2c0d48b commit 4ac2c9c

File tree

6 files changed

+38
-9
lines changed

6 files changed

+38
-9
lines changed

DataCompression.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "DataCompression"
3-
s.version = "3.6.0"
3+
s.version = "3.7.0"
44
s.summary = "Swift libcompression wrapper as an extension for the Data type (GZIP, ZLIB, LZFSE, LZMA, LZ4, deflate, RFC-1950, RFC-1951, RFC-1952)"
55
s.authors = { "Markus Wanke" => "mw99@users.noreply.github.com" }
66
s.homepage = "https://github.com/mw99/DataCompression"

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import PackageDescription
44

55
let package = Package(
66
name: "DataCompression",
7-
platforms: [.macOS(.v10_11), .iOS(.v9), .tvOS(.v9), .watchOS(.v2)],
7+
platforms: [.macOS(.v10_11), .iOS(.v9), .tvOS(.v9), .watchOS(.v2)],
88
products: [
99
.library(
1010
name: "DataCompression",

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#### Swift version support
2222
| Library Version | Swift Version |
2323
|-----------------|---------------|
24+
| 3.7.0 | 5.7 (Xcode 14)|
2425
| 3.6.0 | 5.1 (Xcode 11)|
2526
| 3.5.0 | 5.0 |
2627
| 3.1.0 | 4.2 |
@@ -190,6 +191,8 @@ You only need one file located at `Sources/DataCompression.swift`. Drag and drop
190191

191192
## Change log / Upgrading guide
192193

194+
##### Version `3.6.0` to `3.7.0`
195+
- Support for Xcode 14 with Swift 5.7
193196

194197
##### Version `3.5.0` to `3.6.0`
195198
- Target platforms finally added to the SPM Package file

Sources/DataCompression/DataCompression.swift

+5-7
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public extension Data
183183
/// - returns: uncompressed data
184184
func gunzip() -> Data?
185185
{
186-
// 10 byte header + data + 8 byte footer. See https://tools.ietf.org/html/rfc1952#section-2
186+
// 10 byte header + data + 8 byte footer. See https://tools.ietf.org/html/rfc1952#section-2
187187
let overhead = 10 + 8
188188
guard count >= overhead else { return nil }
189189

@@ -195,17 +195,15 @@ public extension Data
195195
// +---+---+---+---+---+---+---+---+---+---+
196196
return (id1: ptr[0], id2: ptr[1], cm: ptr[2], flg: ptr[3], xfl: ptr[8], os: ptr[9])
197197
}
198-
198+
199199
typealias GZipFooter = (crc32: UInt32, isize: UInt32)
200-
let ftr: GZipFooter = withUnsafeBytes { (bptr: UnsafePointer<UInt8>) -> GZipFooter in
200+
let ftr: GZipFooter = advanced(by: count - 8).withUnsafeBytes { (ptr: UnsafePointer<UInt32>) -> GZipFooter in
201201
// +---+---+---+---+---+---+---+---+
202202
// | CRC32 | ISIZE |
203203
// +---+---+---+---+---+---+---+---+
204-
return bptr.advanced(by: count - 8).withMemoryRebound(to: UInt32.self, capacity: 2) { ptr in
205-
return (ptr[0].littleEndian, ptr[1].littleEndian)
206-
}
204+
return (ptr[0].littleEndian, ptr[1].littleEndian)
207205
}
208-
206+
209207
// Wrong gzip magic or unsupported compression method
210208
guard hdr.id1 == 0x1f && hdr.id2 == 0x8b && hdr.cm == 0x08 else { return nil }
211209

Tests/DataCompressionTests/CompressionTest.swift

+26
Original file line numberDiff line numberDiff line change
@@ -283,5 +283,31 @@ class CompressionTest: XCTestCase
283283
{
284284
XCTAssertEqual(CompressionTest.blob16mb, CompressionTest.blob16mb.lzfse_delzfse())
285285
}
286+
287+
func testGzipCrcFail()
288+
{
289+
let b = 1024 * 16
290+
let ints = [UInt32](repeating: 0xcafeabee, count: b / 4)
291+
var zipped_blob = Data(bytes: ints, count: b).gzip()!
292+
293+
let wrong_crc = Data(bytes: [0xcafeabee], count: 1)
294+
let range = (zipped_blob.count - 8)..<(zipped_blob.count - 4)
295+
zipped_blob.replaceSubrange(range, with: wrong_crc)
296+
297+
XCTAssertNil(zipped_blob.gunzip())
298+
}
299+
300+
func testGzipISizeFail()
301+
{
302+
let b = 1024 * 16
303+
let ints = [UInt32](repeating: 0xcafeabee, count: b / 4)
304+
var zipped_blob = Data(bytes: ints, count: b).gzip()!
305+
306+
let wrong_isize = Data(bytes: [0xcafeabee], count: 1)
307+
let range = (zipped_blob.count - 4)..<(zipped_blob.count)
308+
zipped_blob.replaceSubrange(range, with: wrong_isize)
309+
310+
XCTAssertNil(zipped_blob.gunzip())
311+
}
286312
}
287313

Tests/DataCompressionTests/XCTestManifests.swift

+2
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,7 @@ extension CompressionTest
3030
("testRandomDataBlob_16MB_lz4_delz4", testRandomDataBlob_16MB_lz4_delz4),
3131
("testRandomDataBlob_16MB_lzma_delzma", testRandomDataBlob_16MB_lzma_delzma),
3232
("testRandomDataBlob_16MB_lzfse_delzfse", testRandomDataBlob_16MB_lzfse_delzfse),
33+
("testGzipCrcFail", testGzipCrcFail),
34+
("testGzipISizeFail", testGzipISizeFail),
3335
]
3436
}

0 commit comments

Comments
 (0)