From fa2f234bbe0d0ffdc882b5223a4dc83761b92cc9 Mon Sep 17 00:00:00 2001 From: Jacopo Mangiavacchi Date: Sun, 3 Nov 2019 17:55:12 -0800 Subject: [PATCH] Fix length mask crc32c --- Sources/SwiftTFRecords/Helper.swift | 3 +-- Sources/SwiftTFRecords/TFRecords.swift | 10 ++++------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Sources/SwiftTFRecords/Helper.swift b/Sources/SwiftTFRecords/Helper.swift index 504b2e6..1dc8cb6 100644 --- a/Sources/SwiftTFRecords/Helper.swift +++ b/Sources/SwiftTFRecords/Helper.swift @@ -11,8 +11,7 @@ func maskCrc(_ crc: UInt32) -> UInt32 { UInt32(Int(Int((crc >> 15) | (crc << 17)) + 0xa282ead8) % Int(pow(2.0, 32.0))) } -func intToArray(_ n: UInt32) -> [UInt8] { +func intToArray(_ n: UInt64) -> [UInt8] { let data = withUnsafeBytes(of: n) { Data($0) } return [UInt8](data) } - diff --git a/Sources/SwiftTFRecords/TFRecords.swift b/Sources/SwiftTFRecords/TFRecords.swift index b1e5498..135928b 100644 --- a/Sources/SwiftTFRecords/TFRecords.swift +++ b/Sources/SwiftTFRecords/TFRecords.swift @@ -16,10 +16,9 @@ public struct TFRecords { for record in records { if let recordData = record.data, recordData.count > 0 { - let length32 = UInt32(recordData.count) var length64 = UInt64(recordData.count) - let length32Array = intToArray(length32) - var lengthMaskedCRC = maskCrc(Checksum.crc32c(length32Array)) + let length64Array = intToArray(length64) + var lengthMaskedCRC = maskCrc(Checksum.crc32c(length64Array)) var dataMaskedCRC = maskCrc(Checksum.crc32c([UInt8](recordData))) data.append(Data(bytes: &length64, count: MemoryLayout.size(ofValue: length64))) @@ -54,9 +53,8 @@ public struct TFRecords { } pos += 4 - let length32 = UInt32(length64) - let length32Array = intToArray(length32) - let lengthMaskedCRCExpected = maskCrc(Checksum.crc32c(length32Array)) + let length64Array = intToArray(length64) + let lengthMaskedCRCExpected = maskCrc(Checksum.crc32c(length64Array)) if lengthMaskedCRC == lengthMaskedCRCExpected { let recordData = data.subdata(in: pos..