Skip to content

Commit c8bc94b

Browse files
committed
Use Google's CRC32C implementation
This is likely more efficient because it supports CPU accelerated instructions instead of a software implementation. CryptoSwift usage has been removed since that was the only reason it was included.
1 parent 2c45488 commit c8bc94b

File tree

14 files changed

+308
-53
lines changed

14 files changed

+308
-53
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "CRC32C/crc32c"]
2+
path = CRC32C/crc32c
3+
url = git@github.com:kthchew/crc32c.git

CRC32C/GoogleCRC32C.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#import <Foundation/Foundation.h>
2+
3+
//! Project version number for CRC32C.
4+
FOUNDATION_EXPORT double CRC32CVersionNumber;
5+
6+
//! Project version string for CRC32C.
7+
FOUNDATION_EXPORT const unsigned char CRC32CVersionString[];
8+
9+
// In this header, you should import all the public headers of your framework using statements like #import <CRC32C/PublicHeader.h>
10+
#import <GoogleCRC32C/crc32c.h>

CRC32C/crc32c

Submodule crc32c added at a9cfe33

ExtendFS Extension (ext4)/DiskStructures/FileExtentTreeLevel.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import Algorithms
55
import Foundation
66
import os.log
7-
import CryptoSwift
87

98
fileprivate let logger = Logger(subsystem: "com.kpchew.ExtendFS.ext4Extension", category: "FileExtentTree")
109

@@ -161,6 +160,6 @@ struct FileExtentTreeLevel {
161160
/// - Returns: The CRC32C checksum value.
162161
func calculateChecksum(seed: UInt32) throws -> UInt32 {
163162
let data = try self.toData().dropLast(MemoryLayout<UInt32>.size)
164-
return ~data.byteArray.crc32c(seed: seed)
163+
return ~data.crc32c(seed: seed)
165164
}
166165
}

ExtendFS Extension (ext4)/DiskStructures/IndexNode.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import Foundation
55
import FSKit
66
import os.log
7-
import CryptoSwift
87

98
struct IndexNode {
109
static let logger = Logger(subsystem: "com.kpchew.ExtendFS.ext4Extension", category: "IndexNode")
@@ -92,7 +91,7 @@ struct IndexNode {
9291
var seedCsumData = Data()
9392
seedCsumData.appendLittleEndian(inodeNumber)
9493
seedCsumData.appendLittleEndian(generation)
95-
self.metadataChecksumSeed = ~seedCsumData.byteArray.crc32c(seed: fsMetadataSeed)
94+
self.metadataChecksumSeed = ~seedCsumData.crc32c(seed: fsMetadataSeed)
9695
} else {
9796
self.metadataChecksumSeed = nil
9897
}

ExtendFS Extension (ext4)/DiskStructures/Superblock.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import Foundation
55
import FSKit
66
import os.log
7-
import CryptoSwift
87

98
fileprivate let logger = Logger(subsystem: "com.kpchew.ExtendFS.ext4Extension", category: "Superblock")
109

@@ -807,6 +806,6 @@ struct Superblock {
807806
func calculateChecksum() throws -> UInt32 {
808807
let data = try self.toData().dropLast(MemoryLayout<UInt32>.size)
809808

810-
return ~data.byteArray.crc32c()
809+
return ~data.crc32c()
811810
}
812811
}

ExtendFS Extension (ext4)/Ext4Volume.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ final class Ext4Volume: FSVolume, FSVolume.Operations, FSVolume.PathConfOperatio
138138
}
139139
var uuidData = Data()
140140
uuidData.append(uuid: uuid)
141-
self.metadataChecksumSeed = ~uuidData.byteArray.crc32c()
141+
self.metadataChecksumSeed = ~uuidData.crc32c()
142142
} else {
143143
self.metadataChecksumSeed = nil
144144
}

ExtendFS Extension (ext4)/ExtendFSExtension.entitlements

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5-
<key>com.apple.security.app-sandbox</key>
6-
<true/>
75
<key>com.apple.developer.fskit.fsmodule</key>
86
<true/>
7+
<key>com.apple.security.app-sandbox</key>
8+
<true/>
99
</dict>
1010
</plist>

ExtendFS Extension (ext4)/Extensions/Data+ReadWriteHelpers.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// See the LICENSE file in the root of the repository for full license details.
33

44
import Foundation
5+
import GoogleCRC32C
56

67
extension Data.Iterator {
78
mutating func nextLittleEndian<T: FixedWidthInteger>() -> T? {
@@ -154,4 +155,15 @@ extension Data {
154155
let padding = [UInt8](repeating: 0, count: length - bytes.count)
155156
self.append(contentsOf: padding)
156157
}
158+
159+
func crc32c(seed: UInt32? = nil) -> UInt32 {
160+
self.withUnsafeBytes { buf in
161+
guard let base = buf.baseAddress?.assumingMemoryBound(to: UInt8.self) else { return 0 }
162+
if let seed {
163+
return crc32c_extend(seed, base, buf.count)
164+
} else {
165+
return crc32c_value(base, buf.count)
166+
}
167+
}
168+
}
157169
}

0 commit comments

Comments
 (0)