-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.swift
More file actions
60 lines (48 loc) · 1.56 KB
/
main.swift
File metadata and controls
60 lines (48 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import BinaryParseKit
import BinaryParsing
extension [UInt8]: SizedParsable {
@_lifetime(&input)
public init(parsing input: inout ParserSpan, byteCount: Int) throws(ThrownParsingError) {
var subSpan = try input.sliceSpan(byteCount: byteCount)
self.init(parsingRemainingBytes: &subSpan)
}
}
@ParseStruct
struct FileHeader {
@parse(byteCount: 4, endianness: .big)
let magic: UInt32
@parse(byteCount: 2, endianness: .little)
let version: UInt16
@parse(endianness: .little)
let fileSize: UInt32
@skip(byteCount: 2, because: "reserved")
@parse(byteCountOf: \Self.fileSize)
let content: [UInt8]
@parseRest
let footer: [UInt8]
}
let binaryData: [UInt8] = [
0x89, 0x50, 0x4E, 0x47, // Magic number (PNG signature)
0x01, 0x00, // Version 1.0 (little endian)
0x05, 0x00, 0x00, 0x00, // File size: 5 bytes (little endian)
0x00, 0x00, // Reserved bytes (skipped)
0x48, 0x65, 0x6C, 0x6C, 0x6F, // Content: "Hello"
0xFF, 0xFE, // Footer data
]
let header = try FileHeader(parsing: binaryData)
print("Magic: 0x\(String(header.magic, radix: 16, uppercase: true))") // 0x89504E47
print("Version: \(header.version)") // 1
print("File Size: \(header.fileSize)") // 5
print("Content: \(String(bytes: header.content, encoding: .utf8)!)") // "Hello"
print("Footer: \(header.footer)")
@ParseEnum
enum Channel {
@match(byte: 0x00)
case internet
@match(byte: 0x01)
case satellite
@match(byte: 0x02)
case pigeon
}
let channel = try Channel(parsing: [0x02])
print("Channel: \(channel)") // pigeon