Skip to content

Commit

Permalink
Use crc32_z on macOS 10.13+ and iOS 11.0+
Browse files Browse the repository at this point in the history
  • Loading branch information
Wevah committed Apr 9, 2022
1 parent d4e8721 commit 40813d4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
22 changes: 22 additions & 0 deletions IDNACocoaTouch copy-Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
</dict>
</plist>
14 changes: 12 additions & 2 deletions Sources/IDNA/Data+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ import zlib

extension Data {

/// Compute the CRC-32 of the data.
///
/// Does not handle data with a `count` larger than `UInt32.max` on macOS
/// < 10.13 or on iOS < 11.0.
var crc32: UInt32 {
return self.withUnsafeBytes {
let buffer = $0.bindMemory(to: UInt8.self)
let initial = zlib.crc32(0, nil, 0)
return UInt32(zlib.crc32(initial, buffer.baseAddress, numericCast(buffer.count)))

if #available(macOS 10.13, iOS 11.0, tvOS 9999.0, watchOS 9999.0, *) {
let initial = zlib.crc32_z(0, nil, 0)
return UInt32(zlib.crc32_z(initial, buffer.baseAddress, buffer.count))
} else {
let initial = zlib.crc32(0, nil, 0)
return UInt32(zlib.crc32(initial, buffer.baseAddress, UInt32(buffer.count)))
}
}
}

Expand Down

0 comments on commit 40813d4

Please sign in to comment.