0

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
thetemplar
  • 97
  • 2
  • 3
  • 9
  • just use a database and be done with it, they have done the hard work for you, and probably more efficiently – TheGeneral Mar 14 '19 at 08:41
  • Look at the standard image file formats which handle multiple images in a single file. Here is an example for PNG files : https://en.wikipedia.org/wiki/Portable_Network_Graphics or ZIP file format. – jdweng Mar 14 '19 at 09:08
  • The files are output of measurements, with header, different measurementpoints and dynamic userdefined areas, already in production since some years. - The code itself works, but is ugly and error-prone, so I want to improve the code by using some sort of DSL to better describe it (and use it in the C# program). – thetemplar Mar 14 '19 at 09:31

0 Answers0