From bc64ce44b1bea97eda1da04ee4e98c05e1707746 Mon Sep 17 00:00:00 2001 From: Timofey Solomko Date: Sun, 18 Dec 2016 14:16:16 +0300 Subject: [PATCH] Added function which calculates crc32. --- Sources/CheckSums.swift | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Sources/CheckSums.swift diff --git a/Sources/CheckSums.swift b/Sources/CheckSums.swift new file mode 100644 index 00000000..978c9eb4 --- /dev/null +++ b/Sources/CheckSums.swift @@ -0,0 +1,39 @@ +// +// CheckSums.swift +// SWCompression +// +// Created by Timofey Solomko on 18.12.16. +// Copyright © 2016 tsolomko. All rights reserved. +// + +import Foundation + +struct CheckSums { + + static let crc32table: [UInt32] = { + var table: [UInt32] = Array(repeating: 0, count: 256) + let crc32poly = 0xEDB88320 + var r = 0 + var j = 0 + for i in 0..<256 { + r = i + j = 8 + while j > 0 { + r = r & 1 > 0 ? (r >> 1) ^ crc32poly : r >> 1 + j -= 1 + } + table[i] = UInt32(r) + } + return table + }() + + static func crc32(_ array: [UInt8]) -> Int { + var crc: UInt32 = ~0 + for i in 0..> 8) + } + return Int(~crc) + } + +}