BinarySerializer icon indicating copy to clipboard operation
BinarySerializer copied to clipboard

Improve the FieldOffset

Open sn4k3 opened this issue 3 years ago • 1 comments

This pull request implements #178 which allows to jump to addresses and make use of stream.Seek() with SeekOrigin. The default behaviour of FieldOffset has not changed and then it will rewind by default.

Since i have files pointing to tables this will help having a master class rather that multiple classes and jump in between.

Arguments

  • offset|path
  • SeekOrigin: Specifies the position in a stream to use for seeking the field (Default: SeekOrigin.Begin)
  • Rewind: If true it will seek back to position where it was before seek, otherwise stream will continue from the current position (Default: true)

Example:

public class FileSpec
{
    [FieldOrder(0)]
    [FieldLength(12)]
    [SerializeAs(SerializedType.TerminatedString)]
    public string FileVersion { get; set; } = "FileMarking";

    [FieldOrder(1)]
    public uint FileVersion { get; set; }

    [FieldOrder(2)]
    public uint HeaderAddress { get; set; }

    [FieldOrder(3)]
    public uint SettingsAddress { get; set; }

    [FieldOrder(4)]
    public uint PreviewAddress { get; set; }

    [FieldOrder(5)]
    public uint LayersAddress { get; set; }

    [FieldOrder(6)]
    [FieldOffset(nameof(HeaderAddress), false)]
    public uint HeaderField1 { get; set; }

    [FieldOrder(7)]
    public uint HeaderField2 { get; set; }

    [FieldOrder(8)]
    public uint HeaderField3 { get; set; }

    [FieldOrder(9)]
    [FieldOffset(nameof(SettingsAddress), false)]
    public uint SettingsField1 { get; set; }

    [FieldOrder(10)]
    public uint SettingsField2 { get; set; }

    [FieldOrder(11)]
    [FieldOffset(nameof(PreviewAddress), false)]
    public uint PreviewSize { get; set; }

    [FieldOrder(12)]
    [FieldCount(nameof(PreviewSize))]
    public byte[] PreviewData {get; set; }

    [FieldOrder(13)]
    [FieldOffset(nameof(LayersAddress), false)]
    public uint LayerCount { get; set; }

    [FieldOrder(14)]
    [FieldCount(nameof(LayerCount))]
    public Layer[] Layers { get; set; }

    [FieldOrder(15)]
    [FieldOffset(4, SeekOrigin.Current, false)] // Skip unused 4 bytes first, same as declaring uint Padding
    public uint FileChecksum { get; set; }
}

sn4k3 avatar Sep 05 '22 20:09 sn4k3

Any updates on this feature? I have a chunk file format (that I cannot change) who uses absolute file offsets for each chunk.

stumpy1029 avatar May 23 '24 21:05 stumpy1029