Skip to content

Commit 5ab1595

Browse files
committed
Bug fixes for Swift 5.7 (unaligned pointer problem)
1 parent 4ac2c9c commit 5ab1595

File tree

5 files changed

+15
-12
lines changed

5 files changed

+15
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ playground.xcworkspace
4040
# Packages/
4141
# Package.pins
4242
.build/
43+
.swiftpm/
4344

4445
# CocoaPods
4546
#

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.7.0"
3+
s.version = "3.8.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"

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#### Swift version support
2222
| Library Version | Swift Version |
2323
|-----------------|---------------|
24-
| 3.7.0 | 5.7 (Xcode 14)|
24+
| 3.8.0 | 5.7 (Xcode 14)|
2525
| 3.6.0 | 5.1 (Xcode 11)|
2626
| 3.5.0 | 5.0 |
2727
| 3.1.0 | 4.2 |
@@ -191,6 +191,10 @@ You only need one file located at `Sources/DataCompression.swift`. Drag and drop
191191

192192
## Change log / Upgrading guide
193193

194+
195+
##### Version `3.7.0` to `3.8.0`
196+
- Solved a bug causing crashes when using `.unzip()`, because of unaligned pointer loading caused by internal changes in Swift 5.7.
197+
194198
##### Version `3.6.0` to `3.7.0`
195199
- Support for Xcode 14 with Swift 5.7
196200

Sources/DataCompression/DataCompression.swift

+7-9
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public extension Data
122122
let header: UInt16 = withUnsafeBytes { (ptr: UnsafePointer<UInt16>) -> UInt16 in
123123
return ptr.pointee.bigEndian
124124
}
125-
125+
126126
// check for the deflate stream bit
127127
guard header >> 8 & 0b1111 == 0b1000 else { return nil }
128128
// check the header checksum
@@ -137,14 +137,11 @@ public extension Data
137137
guard let inflated = cresult else { return nil }
138138

139139
if skipCheckSumValidation { return inflated }
140-
141-
let cksum: UInt32 = withUnsafeBytes { (bytePtr: UnsafePointer<UInt8>) -> UInt32 in
142-
let last = bytePtr.advanced(by: count - 4)
143-
return last.withMemoryRebound(to: UInt32.self, capacity: 1) { (intPtr) -> UInt32 in
144-
return intPtr.pointee.bigEndian
145-
}
140+
141+
let cksum = Data(self.suffix(from: count - 4)).withUnsafeBytes { rawPtr in
142+
return rawPtr.load(as: UInt32.self).bigEndian
146143
}
147-
144+
148145
return cksum == inflated.adler32().checksum ? inflated : nil
149146
}
150147

@@ -197,7 +194,8 @@ public extension Data
197194
}
198195

199196
typealias GZipFooter = (crc32: UInt32, isize: UInt32)
200-
let ftr: GZipFooter = advanced(by: count - 8).withUnsafeBytes { (ptr: UnsafePointer<UInt32>) -> GZipFooter in
197+
let alignedFtr = Data(self.suffix(from: count - 8))
198+
let ftr: GZipFooter = alignedFtr.withUnsafeBytes { (ptr: UnsafePointer<UInt32>) -> GZipFooter in
201199
// +---+---+---+---+---+---+---+---+
202200
// | CRC32 | ISIZE |
203201
// +---+---+---+---+---+---+---+---+

Tests/DataCompressionTests/CompressionTest.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extension Data
1818
func c_inflate() -> Data? { let res: Data? = self.inflate(); XCTAssertNotNil(res, "\(#function) failed"); return res }
1919

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

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

0 commit comments

Comments
 (0)