diff --git a/.gitignore b/.gitignore index 43ecbe9..dfceac7 100644 --- a/.gitignore +++ b/.gitignore @@ -40,6 +40,7 @@ playground.xcworkspace # Packages/ # Package.pins .build/ +.swiftpm/ # CocoaPods # diff --git a/DataCompression.podspec b/DataCompression.podspec index 27bd9b1..5bda5ff 100644 --- a/DataCompression.podspec +++ b/DataCompression.podspec @@ -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" => "mw99@users.noreply.github.com" } s.homepage = "https://github.com/mw99/DataCompression" diff --git a/README.md b/README.md index 499c50a..884ea01 100644 --- a/README.md +++ b/README.md @@ -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 | @@ -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 diff --git a/Sources/DataCompression/DataCompression.swift b/Sources/DataCompression/DataCompression.swift index 0f7995f..57e0905 100644 --- a/Sources/DataCompression/DataCompression.swift +++ b/Sources/DataCompression/DataCompression.swift @@ -122,7 +122,7 @@ public extension Data let header: UInt16 = withUnsafeBytes { (ptr: UnsafePointer) -> UInt16 in return ptr.pointee.bigEndian } - + // check for the deflate stream bit guard header >> 8 & 0b1111 == 0b1000 else { return nil } // check the header checksum @@ -137,14 +137,11 @@ public extension Data guard let inflated = cresult else { return nil } if skipCheckSumValidation { return inflated } - - let cksum: UInt32 = withUnsafeBytes { (bytePtr: UnsafePointer) -> 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 } @@ -197,7 +194,8 @@ public extension Data } typealias GZipFooter = (crc32: UInt32, isize: UInt32) - let ftr: GZipFooter = advanced(by: count - 8).withUnsafeBytes { (ptr: UnsafePointer) -> GZipFooter in + let alignedFtr = Data(self.suffix(from: count - 8)) + let ftr: GZipFooter = alignedFtr.withUnsafeBytes { (ptr: UnsafePointer) -> GZipFooter in // +---+---+---+---+---+---+---+---+ // | CRC32 | ISIZE | // +---+---+---+---+---+---+---+---+ diff --git a/Tests/DataCompressionTests/CompressionTest.swift b/Tests/DataCompressionTests/CompressionTest.swift index eba09ab..5a3200f 100644 --- a/Tests/DataCompressionTests/CompressionTest.swift +++ b/Tests/DataCompressionTests/CompressionTest.swift @@ -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 }