Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bind CRC64 #295

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions Source/AwsCommonRuntimeKit/crt/Checksums.swift
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0.

import struct Foundation.Data
import AwsCChecksums

import struct Foundation.Data

extension Data {

/// Computes the CRC32 over data.
/// - Parameter previousCrc32: Pass 0 in the previousCrc32 parameter as an initial value unless continuing to update a running crc in a subsequent call.
public func computeCRC32(previousCrc32: UInt32 = 0) -> UInt32 {
self.withUnsafeBytes { bufferPointer in
return aws_checksums_crc32(bufferPointer.baseAddress?.assumingMemoryBound(to: UInt8.self),
Int32(count),
previousCrc32)
return aws_checksums_crc32_ex(
bufferPointer.baseAddress?.assumingMemoryBound(to: UInt8.self),
count,
previousCrc32)
}
}

/// Computes the crc32c over data.
/// - Parameter previousCrc32c: Pass 0 in the previousCrc32c parameter as an initial value unless continuing to update a running crc in a subsequent call.
public func computeCRC32C(previousCrc32c: UInt32 = 0) -> UInt32 {
self.withUnsafeBytes { bufferPointer in
return aws_checksums_crc32c(bufferPointer.baseAddress?.assumingMemoryBound(to: UInt8.self),
Int32(count),
previousCrc32c)
}
return aws_checksums_crc32c_ex(
bufferPointer.baseAddress?.assumingMemoryBound(to: UInt8.self),
count,
previousCrc32c)
}
}


/// Computes the CRC64NVME over data.
/// - Parameter previousCrc64Nvme: Pass 0 in the previousCrc64Nvme parameter as an initial value unless continuing to update a running crc in a subsequent call.
public func computeCRC64Nvme(previousCrc64Nvme: UInt64 = 0) -> UInt64 {
self.withUnsafeBytes { bufferPointer in
return aws_checksums_crc64nvme_ex(
bufferPointer.baseAddress?.assumingMemoryBound(to: UInt8.self),
count,
previousCrc64Nvme)
}
}

}
6 changes: 6 additions & 0 deletions Test/AwsCommonRuntimeKitTests/crt/ChecksumsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@ class ChecksumsTests: XCBaseTestCase {
XCTAssertEqual("Hello".data(using: .utf8)!.computeCRC32C(), 2178485787)
XCTAssertEqual("{\"foo\":\"base64 encoded sha1 checksum\"}".data(using: .utf8)!.computeCRC32C(), 3565301023)
}

func testCRC64Nvme() throws {
XCTAssertEqual("".data(using: .utf8)!.computeCRC64Nvme(), 0)
XCTAssertEqual(Data(count: 32).computeCRC64Nvme(), 0xCF3473434D4ECF3B)
XCTAssertEqual(Data(Array(0..<32)).computeCRC64Nvme(), 0xB9D9D4A8492CBD7F)
}

}
Loading