What would be a possible possible solution to define a complex binary file to read and write from C# to it. Currently I access the fields the binary via fixed/hardcoded offsets, but I would like to have a cleaner solution. I looked into Kaitai, and it looks perfect, except that I cannot use it to write data.
Is there a similar solution/framework/library to define and use binary files?
Since I already have a existing format which is in use, I cannot swap i.e. completly to ProtoBuf or similar.
Edit: One problem is, that the files are 100MB to several GB big, so I need stream functionality and cannot parse it all at once.
Edit2: In other words, I need a cleaner solution for smth like this (where structs like FileHeader
also are defined with the help of MarshalAs
and FieldOffsets
:
[FieldOffset(0)]
[MarshalAs(UnmanagedType.Struct, SizeConst = HeaderSegmentSize)]
public FileHeader HeaderSegment; //256byte
[FieldOffset(256)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = RunningDataSegmentSize)]
public byte[] RunningDataSegment; // 9MB
[FieldOffset((1024 * 1024 * 9) + 256)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = MeasurementLabelSegmentSize)]
public MeasurementLabel[] MeasurementLabelSegment; //1MB
[FieldOffset((1024 * 1024 * 1) + (1024 * 1024 * 9) + 256)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = DescriptionSegmentSize)]
public byte[] DescriptionSegment; //1MB
[FieldOffset((1024 * 1024 * 1) + (1024 * 1024 * 1) + (1024 * 1024 * 9) + 256)]
public FileData[] DataSegment; //openEnd