Skip to content

Commit

Permalink
remove force unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Nov 18, 2024
1 parent 9f731ca commit 5cecd1d
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions Sources/XMTPiOS/EncodedContentCompression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,18 @@ public enum EncodedContentCompression {
_ data: Data, using algorithm: compression_algorithm
) -> Data? {
let destinationBuffer = UnsafeMutablePointer<UInt8>.allocate(
capacity: data.count * 4) // Allocate enough memory for decompressed data
capacity: data.count * 4 // Allocate enough memory for decompressed data
)
defer { destinationBuffer.deallocate() }

let decompressedSize = data.withUnsafeBytes { sourceBuffer in
compression_decode_buffer(
let decompressedSize = data.withUnsafeBytes { sourceBuffer -> Int in
guard let sourcePointer = sourceBuffer.baseAddress?.assumingMemoryBound(to: UInt8.self) else {
return 0 // Return 0 to indicate failure
}
return compression_decode_buffer(
destinationBuffer, data.count * 4,
sourceBuffer.baseAddress!.assumingMemoryBound(to: UInt8.self),
data.count, nil, algorithm)
sourcePointer, data.count, nil, algorithm
)
}

guard decompressedSize > 0 else { return nil }
Expand Down

0 comments on commit 5cecd1d

Please sign in to comment.