Skip to content

Commit

Permalink
Bug fixes for Swift 5.7 (unaligned pointer problem)
Browse files Browse the repository at this point in the history
  • Loading branch information
mw99 committed Nov 3, 2022
1 parent 4ac2c9c commit 5ab1595
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ playground.xcworkspace
# Packages/
# Package.pins
.build/
.swiftpm/

# CocoaPods
#
Expand Down
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.7.0"
s.version = "3.8.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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#### Swift version support
| Library Version | Swift Version |
|-----------------|---------------|
| 3.7.0 | 5.7 (Xcode 14)|
| 3.8.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 @@ -191,6 +191,10 @@ You only need one file located at `Sources/DataCompression.swift`. Drag and drop

## Change log / Upgrading guide


##### Version `3.7.0` to `3.8.0`
- Solved a bug causing crashes when using `.unzip()`, because of unaligned pointer loading caused by internal changes in Swift 5.7.

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

Expand Down
16 changes: 7 additions & 9 deletions Sources/DataCompression/DataCompression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public extension Data
let header: UInt16 = withUnsafeBytes { (ptr: UnsafePointer<UInt16>) -> UInt16 in
return ptr.pointee.bigEndian
}

// check for the deflate stream bit
guard header >> 8 & 0b1111 == 0b1000 else { return nil }
// check the header checksum
Expand All @@ -137,14 +137,11 @@ public extension Data
guard let inflated = cresult else { return nil }

if skipCheckSumValidation { return inflated }

let cksum: UInt32 = withUnsafeBytes { (bytePtr: UnsafePointer<UInt8>) -> UInt32 in
let last = bytePtr.advanced(by: count - 4)
return last.withMemoryRebound(to: UInt32.self, capacity: 1) { (intPtr) -> UInt32 in
return intPtr.pointee.bigEndian
}

let cksum = Data(self.suffix(from: count - 4)).withUnsafeBytes { rawPtr in
return rawPtr.load(as: UInt32.self).bigEndian
}

return cksum == inflated.adler32().checksum ? inflated : nil
}

Expand Down Expand Up @@ -197,7 +194,8 @@ public extension Data
}

typealias GZipFooter = (crc32: UInt32, isize: UInt32)
let ftr: GZipFooter = advanced(by: count - 8).withUnsafeBytes { (ptr: UnsafePointer<UInt32>) -> GZipFooter in
let alignedFtr = Data(self.suffix(from: count - 8))
let ftr: GZipFooter = alignedFtr.withUnsafeBytes { (ptr: UnsafePointer<UInt32>) -> GZipFooter in
// +---+---+---+---+---+---+---+---+
// | CRC32 | ISIZE |
// +---+---+---+---+---+---+---+---+
Expand Down
2 changes: 1 addition & 1 deletion Tests/DataCompressionTests/CompressionTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension Data
func c_inflate() -> Data? { let res: Data? = self.inflate(); XCTAssertNotNil(res, "\(#function) failed"); return res }

func c_zip() -> Data? { let res: Data? = self.zip(); XCTAssertNotNil(res, "\(#function) failed"); return res }
func c_unzip() -> Data? { let res: Data? = self.unzip(); XCTAssertNotNil(res, "\(#function) failed"); return res }
func c_unzip() -> Data? { let res: Data? = self.unzip(skipCheckSumValidation: false); XCTAssertNotNil(res, "\(#function) failed"); return res }

func c_gzip() -> Data? { let res: Data? = self.gzip(); XCTAssertNotNil(res, "\(#function) failed"); return res }
func c_gunzip() -> Data? { let res: Data? = self.gunzip(); XCTAssertNotNil(res, "\(#function) failed"); return res }
Expand Down

0 comments on commit 5ab1595

Please sign in to comment.