-
Notifications
You must be signed in to change notification settings - Fork 5
/
KaitaiStruct.pm
490 lines (382 loc) · 11.6 KB
/
KaitaiStruct.pm
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# ABSTRACT: Kaitai Struct: runtime library for Perl
package IO::KaitaiStruct;
our $VERSION = "0.011_000";
$VERSION = eval $VERSION;
1;
################################################################################
package IO::KaitaiStruct::Struct;
use strict;
use warnings;
sub new {
my ($invocant, $_io) = @_;
my $class = ref($invocant) || $invocant;
my $self = {_io => $_io};
bless $self;
return $self;
}
sub _io {
my ($self) = @_;
return $self->{_io};
}
sub _parent {
my ($self) = @_;
return $self->{_parent};
}
sub _root {
my ($self) = @_;
return $self->{_root};
}
################################################################################
package IO::KaitaiStruct::Stream;
use strict;
use warnings;
use Fcntl qw(SEEK_SET SEEK_END);
sub new {
my ($invocant, $_io) = @_;
my $class = ref($invocant) || $invocant;
my $self = {};
if (ref $_io eq 'GLOB') {
# _io is a file handle
$self->{_io} = $_io;
} elsif (ref $_io eq '') {
# _io is a memory buffer
my $fd;
open $fd, '<', \$_io or return undef;
binmode $fd;
$self->{_io} = $fd;
} else {
return undef;
}
bless $self;
# Used by read_bits_int()
$self->align_to_byte();
return $self;
}
sub close {
my ($self) = @_;
close($self->{_io});
}
# ========================================================================
# Stream positioning
# ========================================================================
sub is_eof {
my ($self) = @_;
return eof($self->{_io}) && $self->{bits_left} == 0;
}
sub seek {
my ($self, $pos) = @_;
seek($self->{_io}, $pos, SEEK_SET);
}
sub pos {
my ($self) = @_;
return tell($self->{_io});
}
sub size {
my ($self) = @_;
my $pos = tell($self->{_io});
CORE::seek($self->{_io}, 0, SEEK_END);
my $size = tell($self->{_io});
CORE::seek($self->{_io}, $pos, SEEK_SET);
return $size;
}
# ========================================================================
# Reading
# ========================================================================
sub _read {
my ($self, $len, $template) = @_;
my $buf;
my $bytes_read = read($self->{_io}, $buf, $len);
if ($bytes_read != $len) {
die "Requested $len bytes, but only $bytes_read bytes available";
}
return unpack($template, $buf);
}
# ========================================================================
# Integer numbers
# ========================================================================
# ------------------------------------------------------------------------
# Signed
# ------------------------------------------------------------------------
sub read_s1 {
return _read(@_, 1, 'c');
}
# ........................................................................
# Big-endian
# ........................................................................
sub read_s2be {
return _read(@_, 2, 's>');
}
sub read_s4be {
return _read(@_, 4, 'i>');
}
sub read_s8be {
return _read(@_, 8, 'q>');
}
# ........................................................................
# Little-endian
# ........................................................................
sub read_s2le {
return _read(@_, 2, 's<');
}
sub read_s4le {
return _read(@_, 4, 'i<');
}
sub read_s8le {
return _read(@_, 8, 'q<');
}
# ------------------------------------------------------------------------
# Unsigned
# ------------------------------------------------------------------------
sub read_u1 {
return _read(@_, 1, 'C');
}
# ........................................................................
# Big-endian
# ........................................................................
sub read_u2be {
return _read(@_, 2, 'S>');
}
sub read_u4be {
return _read(@_, 4, 'I>');
}
sub read_u8be {
return _read(@_, 8, 'Q>');
}
# ........................................................................
# Little-endian
# ........................................................................
sub read_u2le {
return _read(@_, 2, 'S<');
}
sub read_u4le {
return _read(@_, 4, 'I<');
}
sub read_u8le {
return _read(@_, 8, 'Q<');
}
# ========================================================================
# Floating point numbers
# ========================================================================
# ........................................................................
# Big-endian
# ........................................................................
sub read_f4be {
return _read(@_, 4, 'f>');
}
sub read_f8be {
return _read(@_, 8, 'd>');
}
# ........................................................................
# Little-endian
# ........................................................................
sub read_f4le {
return _read(@_, 4, 'f<');
}
sub read_f8le {
return _read(@_, 8, 'd<');
}
# ========================================================================
# Unaligned bit values
# ========================================================================
sub align_to_byte {
my ($self) = @_;
$self->{bits_left} = 0;
$self->{bits} = 0;
}
sub read_bits_int_be {
my ($self, $n) = @_;
my $res = 0;
my $bits_needed = $n - $self->{bits_left};
$self->{bits_left} = -$bits_needed & 7; # `-$bits_needed mod 8`
if ($bits_needed > 0) {
# 1 bit => 1 byte
# 8 bits => 1 byte
# 9 bits => 2 bytes
my $bytes_needed = (($bits_needed - 1) >> 3) + 1; # `ceil($bits_needed / 8)` (NB: `x >> 3` is `floor(x / 8)`,
# but ONLY for `x >= 0`, because `>>` is unsigned in Perl
# unless it is inside a `use integer` block)
my $buf = $self->read_bytes($bytes_needed);
for my $byte (unpack('C*', $buf)) {
$res = $res << 8 | $byte;
}
my $new_bits = $res;
$res = $res >> $self->{bits_left} | $self->{bits} << $bits_needed;
$self->{bits} = $new_bits; # will be masked at the end of the function
} else {
$res = $self->{bits} >> -$bits_needed; # shift unneeded bits out
}
my $mask = (1 << $self->{bits_left}) - 1; # `bits_left` is in range 0..7
$self->{bits} &= $mask;
return $res;
}
# Unused since Kaitai Struct Compiler v0.9+ - compatibility with older versions
#
# Deprecated, use read_bits_int_be() instead.
sub read_bits_int {
return read_bits_int_be(@_);
}
sub read_bits_int_le {
my ($self, $n) = @_;
my $res = 0;
my $bits_needed = $n - $self->{bits_left};
if ($bits_needed > 0) {
# 1 bit => 1 byte
# 8 bits => 1 byte
# 9 bits => 2 bytes
my $bytes_needed = (($bits_needed - 1) >> 3) + 1; # `ceil($bits_needed / 8)` (NB: `x >> 3` is `floor(x / 8)`,
# but ONLY for `x >= 0` - see `read_bits_int_be` method)
my $buf = $self->read_bytes($bytes_needed);
my $i = 0;
for my $byte (unpack('C*', $buf)) {
$res |= $byte << ($i * 8);
$i++;
}
my $new_bits = $res >> $bits_needed;
$res = $res << $self->{bits_left} | $self->{bits};
$self->{bits} = $new_bits;
} else {
$res = $self->{bits};
$self->{bits} >>= $n;
}
$self->{bits_left} = -$bits_needed & 7; # `-$bits_needed mod 8`
my $mask = (1 << $n) - 1; # unlike some other languages, no problem with this in Perl
$res &= $mask;
return $res;
}
# ========================================================================
# Byte arrays
# ========================================================================
sub read_bytes {
my ($self, $num_to_read) = @_;
my $num_read;
my $buf;
$num_read = read($self->{_io}, $buf, $num_to_read);
if ($num_read != $num_to_read) {
die "Requested $num_to_read bytes, but only $num_read bytes available";
}
return $buf;
}
sub read_bytes_full {
my ($self) = @_;
my $buf;
read($self->{_io}, $buf, $self->size());
return $buf;
}
sub read_bytes_term {
my ($self, $term, $include_term, $consume_term, $eos_error) = @_;
my $r = '';
while (1) {
my $c;
read($self->{_io}, $c, 1);
if ($c eq '') {
if ($eos_error) {
die "End of stream reached, but no terminator '$term' found";
}
return $r;
}
if (ord($c) == $term) {
$r .= $c if $include_term;
$self->seek($self->pos() - 1) unless $consume_term;
return $r;
}
$r .= $c;
}
}
sub read_bytes_term_multi {
my ($self, $term, $include_term, $consume_term, $eos_error) = @_;
my $unit_size = length($term);
my $r = '';
while (1) {
my $c;
my $num_read = read($self->{_io}, $c, $unit_size);
if ($num_read < $unit_size) {
if ($eos_error) {
die "End of stream reached, but no terminator '$term' found";
}
$r .= $c;
return $r;
}
if ($c eq $term) {
$r .= $c if $include_term;
$self->seek($self->pos() - $unit_size) unless $consume_term;
return $r;
}
$r .= $c;
}
}
sub ensure_fixed_contents {
my ($self, $expected) = @_;
my $buf;
read($self->{_io}, $buf, length($expected));
if ($buf ne $expected) {
die "Unexpected fixed contents: got '$buf', was waiting for '$expected'";
}
return $buf;
}
sub bytes_strip_right {
my ($bytes, $pad_byte) = @_;
my $pad_char = pack('C', $pad_byte);
my $new_len = length($bytes);
while ($new_len > 0 && substr($bytes, $new_len - 1, 1) eq $pad_char) {
$new_len--;
}
return substr($bytes, 0, $new_len);
}
sub bytes_terminate {
my ($bytes, $term, $include_term) = @_;
my $term_byte = pack('C', $term);
my $term_index = index($bytes, $term_byte);
if ($term_index == -1) {
return $bytes;
}
return substr($bytes, 0, $term_index + ($include_term ? 1 : 0));
}
sub bytes_terminate_multi {
my ($bytes, $term, $include_term) = @_;
my $unit_size = length($term);
my $search_index = index($bytes, $term);
while (1) {
if ($search_index == -1) {
return $bytes;
}
my $mod = $search_index % $unit_size;
if ($mod == 0) {
return substr($bytes, 0, $search_index + ($include_term ? $unit_size : 0));
}
$search_index = index($bytes, $term, $search_index + ($unit_size - $mod));
}
}
# ========================================================================
# Byte array processing
# ========================================================================
sub process_xor_one {
my ($data, $key) = @_;
$key = pack('C', $key);
for (my $i = 0; $i < length($data); $i++) {
substr($data, $i, 1) ^= $key;
}
return $data;
}
sub process_xor_many {
my ($data, $key) = @_;
my $ki = 0;
my $kl = length($key);
for (my $i = 0; $i < length($data); $i++) {
substr($data, $i, 1) ^= substr($key, $ki, 1);
$ki += 1;
$ki = 0 if $ki >= $kl;
}
return $data;
}
sub process_rotate_left {
my ($data, $amount, $group_size) = @_;
die "Unable to rotate group of $group_size bytes yet" if $group_size != 1;
my $mask = $group_size * 8 - 1;
$amount &= $mask;
my $anti_amount = -$amount & $mask;
for (my $i = 0; $i < length($data); $i++) {
my $byte = unpack('C', substr($data, $i, 1));
substr($data, $i, 1) = pack('C', (($byte << $amount) | ($byte >> $anti_amount)) & 0xFF);
}
return $data;
}