29 lines
917 B
Swift
29 lines
917 B
Swift
import Foundation
|
|
|
|
enum GGUFHeader {
|
|
enum ValidationError: LocalizedError {
|
|
case invalidMagic
|
|
case unsupportedVersion(UInt32)
|
|
|
|
var errorDescription: String? {
|
|
switch self {
|
|
case .invalidMagic: "The file does not have a valid GGUF header."
|
|
case .unsupportedVersion(let version): "GGUF version \(version) is unsupported."
|
|
}
|
|
}
|
|
}
|
|
|
|
static func version(in data: Data) throws -> UInt32 {
|
|
guard data.count >= 8, data.prefix(4).elementsEqual("GGUF".utf8) else {
|
|
throw ValidationError.invalidMagic
|
|
}
|
|
let version = data.withUnsafeBytes { bytes in
|
|
UInt32(littleEndian: bytes.loadUnaligned(fromByteOffset: 4, as: UInt32.self))
|
|
}
|
|
guard version == 2 || version == 3 else {
|
|
throw ValidationError.unsupportedVersion(version)
|
|
}
|
|
return version
|
|
}
|
|
}
|