kaitai_struct_formats
kaitai_struct_formats copied to clipboard
ext2.py: ValueError: requested invalid -8 amount of bytes
ext2 parser is broken
repro:
dd if=/dev/zero of=ext2.bin bs=1024 count=1000
mkfs.ext2 ext2.bin
mkdir mnt
sudo mount -o loop ext2.bin mnt/
echo hello world | sudo tee mnt/hello.txt
sudo umount mnt/
kaitai-struct-compiler -t python filesystem/ext2.ksy
python -c '
import ext2
e = ext2.Ext2.from_file("ext2.bin")
print(e.root_dir)
print([ n.name for n in e.root_dir.entries ])
'
File "ext2.py", line 217, in _read
self.entries.append(Ext2.DirEntry(self._io, self, self._root))
File "ext2.py", line 120, in __init__
self._read()
File "ext2.py", line 128, in _read
self.padding = self._io.read_bytes(((self.rec_len - self.name_len) - 8))
File "kaitaistruct.py", line 298, in read_bytes
raise ValueError(
ValueError: requested invalid -8 amount of bytes
fix?
--- a/filesystem/ext2.ksy
+++ b/filesystem/ext2.ksy
@@ -254,10 +254,14 @@ types:
type: str
encoding: UTF-8
- id: padding
- size: rec_len - name_len - 8
+ size: 'padding_size_raw > 0 ? padding_size_raw : 0'
instances:
inode:
value: '_root.bg1.block_groups[(inode_ptr - 1) / _root.bg1.super_block.inodes_per_group].inodes[(inode_ptr - 1) % _root.bg1.super_block.inodes_per_group]'
+ # workaround: kaitai_struct_compiler has no max(a, b) function
+ # padding_size = max(0, (rec_len - name_len - 8))
+ padding_size_raw:
+ value: 'rec_len - name_len - 8'
enums:
# https://www.nongnu.org/ext2-doc/ext2.html#IFDIR-FILE-TYPE
file_type_enum:
still looks broken, as i dont see the hello.txt
file
>>> [ n.name for n in e.root_dir.entries ]
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
todo: use the array method max
(docs)
padding_size = max(0, (rec_len - name_len - 8))
[0, (rec_len - name_len - 8)].max
The ext2 parser is, in my opinion, only useful to parse the super block. There are many features (such as the sparse superblock ( https://www.nongnu.org/ext2-doc/ext2.html#def-superblock ) that are not properly supported.