Skip to content

Commit 3f844be

Browse files
authored
Bind CRC64 (#295)
1 parent d661a55 commit 3f844be

4 files changed

Lines changed: 33 additions & 13 deletions

File tree

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,43 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0.
33

4-
import struct Foundation.Data
54
import AwsCChecksums
65

6+
import struct Foundation.Data
7+
78
extension Data {
8-
9+
910
/// Computes the CRC32 over data.
1011
/// - Parameter previousCrc32: Pass 0 in the previousCrc32 parameter as an initial value unless continuing to update a running crc in a subsequent call.
1112
public func computeCRC32(previousCrc32: UInt32 = 0) -> UInt32 {
1213
self.withUnsafeBytes { bufferPointer in
13-
return aws_checksums_crc32(bufferPointer.baseAddress?.assumingMemoryBound(to: UInt8.self),
14-
Int32(count),
15-
previousCrc32)
14+
return aws_checksums_crc32_ex(
15+
bufferPointer.baseAddress?.assumingMemoryBound(to: UInt8.self),
16+
count,
17+
previousCrc32)
1618
}
1719
}
18-
20+
1921
/// Computes the crc32c over data.
2022
/// - Parameter previousCrc32c: Pass 0 in the previousCrc32c parameter as an initial value unless continuing to update a running crc in a subsequent call.
2123
public func computeCRC32C(previousCrc32c: UInt32 = 0) -> UInt32 {
2224
self.withUnsafeBytes { bufferPointer in
23-
return aws_checksums_crc32c(bufferPointer.baseAddress?.assumingMemoryBound(to: UInt8.self),
24-
Int32(count),
25-
previousCrc32c)
26-
}
25+
return aws_checksums_crc32c_ex(
26+
bufferPointer.baseAddress?.assumingMemoryBound(to: UInt8.self),
27+
count,
28+
previousCrc32c)
29+
}
2730
}
28-
31+
32+
/// Computes the CRC64NVME over data.
33+
/// - Parameter previousCrc64Nvme: Pass 0 in the previousCrc64Nvme parameter as an initial value unless continuing to update a running crc in a subsequent call.
34+
public func computeCRC64Nvme(previousCrc64Nvme: UInt64 = 0) -> UInt64 {
35+
self.withUnsafeBytes { bufferPointer in
36+
return aws_checksums_crc64nvme_ex(
37+
bufferPointer.baseAddress?.assumingMemoryBound(to: UInt8.self),
38+
count,
39+
previousCrc64Nvme)
40+
}
41+
}
42+
2943
}

Test/AwsCommonRuntimeKitTests/crt/ChecksumsTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,11 @@ class ChecksumsTests: XCBaseTestCase {
1717
XCTAssertEqual("Hello".data(using: .utf8)!.computeCRC32C(), 2178485787)
1818
XCTAssertEqual("{\"foo\":\"base64 encoded sha1 checksum\"}".data(using: .utf8)!.computeCRC32C(), 3565301023)
1919
}
20+
21+
func testCRC64Nvme() throws {
22+
XCTAssertEqual("".data(using: .utf8)!.computeCRC64Nvme(), 0)
23+
XCTAssertEqual(Data(count: 32).computeCRC64Nvme(), 0xCF3473434D4ECF3B)
24+
XCTAssertEqual(Data(Array(0..<32)).computeCRC64Nvme(), 0xB9D9D4A8492CBD7F)
25+
}
2026

2127
}

0 commit comments

Comments
 (0)