Skip to content

Instantly share code, notes, and snippets.

@seanwoodward
Created March 9, 2018 02:49
Show Gist options
  • Save seanwoodward/984b13693c0ced7db49e11670ded2030 to your computer and use it in GitHub Desktop.
Save seanwoodward/984b13693c0ced7db49e11670ded2030 to your computer and use it in GitHub Desktop.
Adler32 using libz
import Foundation
import zlib
extension Array where Element == UInt8 {
func adler32() -> UInt32 {
let a1 = zlib.adler32(0, nil, 0)
let size = self.count
let result = self.withUnsafeBytes { (p1: UnsafeRawBufferPointer) -> UInt32 in
let start = p1.baseAddress!.assumingMemoryBound(to: Bytef.self)
let adler = zlib.adler32_z(a1, start, size)
return UInt32(adler)
}
return result
}
}
extension Data {
func adler32() -> UInt32 {
let a1 = zlib.adler32(0, nil, 0)
let _count = self.count
let result = self.withUnsafeBytes { (p1: UnsafePointer<UInt8>) -> UInt32 in
return p1.withMemoryRebound(to: Bytef.self, capacity: MemoryLayout<Bytef>.size * _count) { pBytef -> UInt32 in
return UInt32(zlib.adler32_z(a1, pBytef, _count))
}
}
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment