Skip to content

Commit

Permalink
Fix length mask crc32c
Browse files Browse the repository at this point in the history
  • Loading branch information
JacopoMangiavacchi committed Nov 4, 2019
1 parent c9f2c79 commit fa2f234
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
3 changes: 1 addition & 2 deletions Sources/SwiftTFRecords/Helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

10 changes: 4 additions & 6 deletions Sources/SwiftTFRecords/TFRecords.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down Expand Up @@ -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..<pos+Int(length64))
Expand Down

0 comments on commit fa2f234

Please sign in to comment.