forked from paimooon/DataParser-paimooon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaux_types.ksy
168 lines (168 loc) · 4.38 KB
/
aux_types.ksy
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
meta:
id: aux_types
file-extension: aux_types
endian: le
doc: |
Miscellaneous types used in serialization
Credit for dynamic_* stuff goes to Raz
types:
error:
doc: Type used in misc situations to signal about an error
seq:
- id: message
contents: "There was an error parsing data; please check KSY definition"
string:
seq:
- id: length
type: vlq_base128_le_u
- id: data
size: length.value
type: str
encoding: UTF-8
length_prefixed_bitfield:
seq:
- id: length
type: vlq_base128_le_u
- id: bitfield
type: u1
repeat: expr
repeat-expr: length.value
instances:
value:
value: >-
(length.value >= 1 ? bitfield[0] : 0) +
(length.value >= 2 ? (bitfield[1] << 8) : 0) +
(length.value >= 3 ? (bitfield[2] << 16) : 0) +
(length.value >= 4 ? (bitfield[3] << 24) : 0) +
(length.value >= 5 ? (bitfield[4] << 32) : 0) +
(length.value >= 6 ? (bitfield[5] << 40) : 0) +
(length.value >= 7 ? (bitfield[6] << 48) : 0) +
(length.value >= 8 ? (bitfield[7] << 56) : 0) +
(length.value >= 9 ? (bitfield[8] << 64) : 0)
dynamic_int:
seq:
- id: is_string
type: u1
- id: data
type:
switch-on: is_string
cases:
0: vlq_base128_le_s
1: string
base_dynamic_float:
seq:
- id: is_string
type: u1
- id: data
type:
switch-on: is_string
cases:
0: f4
1: string
dynamic_operator:
seq:
- id: is_operator
type: u1 # TODO: bool!
- id: data
type:
switch-on: is_operator
cases:
0: base_dynamic_float
1: vlq_base128_le_s
dynamic_formula:
seq:
- id: length
type: vlq_base128_le_s
- id: elements
type: dynamic_operator
repeat: expr
repeat-expr: length.value
dynamic_float:
seq:
- id: is_formula
type: u1 # TODO: bool!
- id: data
type:
switch-on: is_formula
cases:
0: base_dynamic_float
1: dynamic_formula
dynamic_string:
seq:
- id: is_dynamic
type: u1 # TODO: bool!
- id: data
type: string
dynamic_argument:
seq:
- id: type_code
type: vlq_base128_le_u
- id: data
type:
switch-on: type_code.value
cases:
1: s1
2: u1
3: s2
4: u2
5: s4
6: u4
7: f4
8: f8
9: u1 # TODO: bool!
10: string
vlq_base128_le: # Stolen from Kaitai website and modified accordingly with Zigzag encoding
seq:
- id: ks__groups
type: group
repeat: until
repeat-until: not _.has_next
types:
group:
seq:
- id: b
type: u1
instances:
has_next:
value: (b & 0b1000_0000) != 0
doc: If true, then we have more bytes to read
value:
value: b & 0b0111_1111
doc: The 7-bit (base128) numeric value chunk of this group
instances:
len:
value: ks__groups.size
value_unsigned:
value: >-
ks__groups[0].value
+ (len >= 2 ? (ks__groups[1].value << 7) : 0)
+ (len >= 3 ? (ks__groups[2].value << 14) : 0)
+ (len >= 4 ? (ks__groups[3].value << 21) : 0)
+ (len >= 5 ? (ks__groups[4].value << 28) : 0)
+ (len >= 6 ? (ks__groups[5].value << 35) : 0)
+ (len >= 7 ? (ks__groups[6].value << 42) : 0)
+ (len >= 8 ? (ks__groups[7].value << 49) : 0)
doc: Resulting unsigned value as normal integer
value_absolute:
value: 'value_unsigned >> 1'
doc: Absolute value for Zigzag-encoded integer
sign_bit:
value: 'value_unsigned & 1'
doc: Sign bit for Zigzag-encoded integer
value_signed:
value: 'value_absolute ^ (-sign_bit)'
doc: Resulting signed value as Zigzag-encoded integer
vlq_base128_le_s:
seq:
- id: data
type: vlq_base128_le
instances:
value:
value: data.value_signed
vlq_base128_le_u:
seq:
- id: data
type: vlq_base128_le
instances:
value:
value: data.value_unsigned