Smallint
is a 16-bit value. When reading the file, you are reading that value as a 32-bit value instead of a 16-bit value, so you end up reading some of the bytes that belong to the length of the stored string. When you then read the string, the first 2 bytes of the string characters get interpreted as part of the string length, which is why you end up with garbage.
You have a similar logic bug when reading the groups. Integer
is a 32-bit value. When reading the group count, you are reading it as a 16-bit value instead, which means your group reads are going to be off by 2 bytes and will be corrupted as well.
You need to change these lines inside of your ReadGroups()
function:
cnt: Smallint;
...
GroupHeader.version:=strm.ReadInt32;
...
cnt := strm.ReadInt16;
To these instead:
cnt: Integer;
...
GroupHeader.version := strm.ReadInt16;
...
cnt := strm.ReadInt32;