Skip to content

Commit

Permalink
bind crc64
Browse files Browse the repository at this point in the history
  • Loading branch information
waahm7 committed Oct 22, 2024
1 parent d661a55 commit f5b54c4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
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)
}

}

0 comments on commit f5b54c4

Please sign in to comment.