I wanted to implement this with Kaitai Struct but there were some places where I couldn't figure out how to make it work, so I resorted to writing in pure Java.