-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_sample.py
57 lines (47 loc) · 1.63 KB
/
generate_sample.py
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
# SPDX-FileCopyrightText: 2021 Petr Pucil <[email protected]>
#
# SPDX-License-Identifier: MIT
from kaitaistruct import KaitaiStream
import random
n_rows = 65536
n_columns = 4
column_types = [1, 2, 3, 4]
is_row_0_fixed = True
COL_U2 = 1
COL_U4 = 2
COL_F4 = 3
COL_F8 = 4
assert len(column_types) == n_columns, 'len(column_types) = {} must be equal to n_columns = {}'.format(
len(column_types),
n_columns
)
pk_u1 = KaitaiStream.packer_u1
pk_u2le = KaitaiStream.packer_u2le
pk_u4le = KaitaiStream.packer_u4le
pk_f4le = KaitaiStream.packer_f4le
pk_f8le = KaitaiStream.packer_f8le
with open('./sample.bin', 'wb') as f:
f.write(pk_u4le.pack(n_rows))
f.write(pk_u1.pack(n_columns))
for t in column_types:
f.write(pk_u2le.pack(t))
if is_row_0_fixed:
assert column_types == [1, 2, 3, 4], 'column_types = {} does not match the row_0 format'.format(column_types)
row_0 = [0x7a46, 0x86b97d9c, 0.842150092124939, 0.5340359913176319]
f.write(b'\x00')
f.write(pk_u2le.pack(row_0[0]))
f.write(pk_u4le.pack(row_0[1]))
f.write(pk_f4le.pack(row_0[2]))
f.write(pk_f8le.pack(row_0[3]))
for y in range(n_rows - 1):
f.write(b'\x00')
for t in column_types:
if t == COL_U2:
f.write(pk_u2le.pack(random.randrange(0, 0xffff)))
elif t == COL_U4:
f.write(pk_u4le.pack(random.randrange(0, 0xffffffff)))
elif t == COL_F4:
f.write(pk_f4le.pack(random.random()))
elif t == COL_F8:
f.write(pk_f8le.pack(random.random()))
print('{} bytes written'.format(f.tell()))