Skip to content

Commit

Permalink
Add Checksum protocol and simplify CRC verification
Browse files Browse the repository at this point in the history
  • Loading branch information
pauljohanneskraft committed Jul 26, 2023
1 parent d4213f3 commit 124a8c9
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 32 deletions.
Binary file not shown.
45 changes: 15 additions & 30 deletions Sources/CRC+Verify.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,32 @@ import Foundation

public struct VerificationError<Value: FixedWidthInteger>: Error, CustomStringConvertible {

// MARK: Stored Properties

public let actualValue: Value
public let expectedValue: Value

public init(actual: Value, expected: Value) {
self.actualValue = actual
self.expectedValue = expected
}
// MARK: Computed Properties

public var description: String {
"\(String(describing: Self.self))(expected: 0x\(expectedValue.hex), actual: 0x\(actualValue.hex))"
}

// MARK: Initialization

internal init(actual: Value, expected: Value) {
self.actualValue = actual
self.expectedValue = expected
}

}

extension CRC {
extension Checksum {

public func verify<S: Sequence<UInt8>>(_ expectedValue: Value, for bytes: S) throws {
let actualValue = calculate(for: bytes)
guard expectedValue == actualValue else {
throw VerificationError(
actual: actualValue,
expected: expectedValue
)
public func verify<S: Sequence<UInt8>>(_ expectedValue: Value, for data: S) throws {
let actualValue = calculate(for: data)
guard actualValue == expectedValue else {
throw VerificationError(actual: actualValue, expected: expectedValue)
}
}

Expand All @@ -50,21 +53,3 @@ extension CRCCalculator {
}

}

extension FixedWidthInteger {

internal var hex: String {
withUnsafeBytes(of: bigEndian) { $0.hex }
}

}

extension Sequence<UInt8> {

internal var hex: String {
self
.map { String(format: "%02hhX", $0) }
.joined()
}

}
2 changes: 1 addition & 1 deletion Sources/CRC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import Foundation

public struct CRC<Value: FixedWidthInteger> {
public struct CRC<Value: FixedWidthInteger>: Checksum {

// MARK: Stored Properties

Expand Down
1 change: 0 additions & 1 deletion Sources/CRC32.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ extension CRC32 {
public static let posix = Self(polynomial: 0x04C11DB7, xorOut: 0xFFFFFFFF)
public static let sata = Self(polynomial: 0x04C11DB7, initialValue: 0x52325032)
public static let xfer = Self(polynomial: 0x000000AF)

public static let c = Self(polynomial: 0x1EDC6F41, initialValue: 0xFFFFFFFF, reflected: true, xorOut: 0xFFFFFFFF)
public static let d = Self(polynomial: 0xA833982B, initialValue: 0xFFFFFFFF, reflected: true, xorOut: 0xFFFFFFFF)
public static let q = Self(polynomial: 0x814141AB)
Expand Down
14 changes: 14 additions & 0 deletions Sources/Checksum.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// File.swift
//
//
// Created by Paul Kraft on 26.07.23.
//

import Foundation

public protocol Checksum {
associatedtype Value: FixedWidthInteger

func calculate<S: Sequence<UInt8>>(for data: S) -> Value
}
26 changes: 26 additions & 0 deletions Sources/Hex.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// File.swift
//
//
// Created by Paul Kraft on 26.07.23.
//

import Foundation

extension FixedWidthInteger {

internal var hex: String {
withUnsafeBytes(of: bigEndian) { $0.hex }
}

}

extension Sequence<UInt8> {

internal var hex: String {
self
.map { String(format: "%02hhX", $0) }
.joined()
}

}

0 comments on commit 124a8c9

Please sign in to comment.