-
Notifications
You must be signed in to change notification settings - Fork 5
[SPARK-52373] Add CRC32
struct
#191
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
import Foundation | ||
|
||
public struct CRC32 { | ||
|
||
/// Pre-computed CRC32 table | ||
private static let crcTable: [UInt32] = { | ||
var table = [UInt32](repeating: 0, count: 256) | ||
let polynomial: UInt32 = 0xEDB8_8320 // IEEE 802.3 polynomial | ||
|
||
for i in 0..<256 { | ||
var c = UInt32(i) | ||
for _ in 0..<8 { | ||
if (c & 1) == 1 { | ||
c = polynomial ^ (c >> 1) | ||
} else { | ||
c = c >> 1 | ||
} | ||
} | ||
table[i] = c | ||
} | ||
return table | ||
}() | ||
|
||
/// Calculates the CRC32 checksum for the given Data. | ||
/// | ||
/// - Parameter data: The Data object for which to calculate the checksum. | ||
/// - Returns: The calculated CRC32 checksum as a UInt32. | ||
public static func checksum(data: Data) -> UInt32 { | ||
var crc: UInt32 = 0xFFFF_FFFF | ||
|
||
data.withUnsafeBytes { (pointer: UnsafeRawBufferPointer) in | ||
for byte in pointer.bindMemory(to: UInt8.self) { | ||
crc = (crc >> 8) ^ crcTable[Int((crc ^ UInt32(byte)) & 0xFF)] | ||
} | ||
} | ||
return ~crc | ||
} | ||
|
||
/// Calculates the CRC32 checksum for the given String. | ||
/// | ||
/// - Parameter string: The String object for which to calculate the checksum. | ||
/// - Parameter encoding: The encoding to use when converting the string to Data (defaults to .utf8). | ||
/// - Returns: The calculated CRC32 checksum as a UInt32. Returns nil if the string cannot be converted to Data. | ||
public static func checksum(string: String, encoding: String.Encoding = .utf8) -> UInt32? { | ||
guard let data = string.data(using: encoding) else { | ||
return nil | ||
} | ||
return checksum(data: data) | ||
} | ||
|
||
/// Calculates the CRC32 checksum for the given array of bytes. | ||
/// | ||
/// - Parameter bytes: The [UInt8] array for which to calculate the checksum. | ||
/// - Returns: The calculated CRC32 checksum as a UInt32. | ||
public static func checksum(bytes: [UInt8]) -> UInt32 { | ||
var crc: UInt32 = 0xFFFF_FFFF | ||
|
||
for byte in bytes { | ||
crc = (crc >> 8) ^ crcTable[Int((crc ^ UInt32(byte)) & 0xFF)] | ||
} | ||
return ~crc | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
// | ||
|
||
import Foundation | ||
import SparkConnect | ||
import Testing | ||
|
||
struct CRC32Tests { | ||
@Test | ||
func testChecksumWithEmptyData() async throws { | ||
#expect(CRC32.checksum(data: Data()) == 0) | ||
#expect(CRC32.checksum(string: "") == 0) | ||
#expect(CRC32.checksum(bytes: []) == 0) | ||
} | ||
|
||
@Test | ||
func testChecksum() async throws { | ||
let str = "Apache Spark Connect Client for Swift" | ||
#expect(CRC32.checksum(string: str, encoding: .ascii) == 2_736_908_745) | ||
#expect(CRC32.checksum(data: str.data(using: .ascii)!) == 2_736_908_745) | ||
#expect(CRC32.checksum(bytes: [UInt8](str.data(using: .ascii)!)) == 2_736_908_745) | ||
} | ||
|
||
@Test | ||
func testLongChecksum() async throws { | ||
let str = String(repeating: "Apache Spark Connect Client for Swift", count: 1000) | ||
#expect(CRC32.checksum(string: str, encoding: .ascii) == 1_985_943_888) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is consistent with JDK CRC32. jshell> var c = new java.util.zip.CRC32();
jshell> c.update("Apache Spark Connect Client for Swift".repeat(1000).getBytes())
jshell> c.getValue()
$9 ==> 1985943888 |
||
#expect(CRC32.checksum(data: str.data(using: .ascii)!) == 1_985_943_888) | ||
#expect(CRC32.checksum(bytes: [UInt8](str.data(using: .ascii)!)) == 1_985_943_888) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is consistent with JDK CRC32.