Skip to content

Pattern Class

Naveen Dharmathunga edited this page Jan 19, 2026 · 1 revision

Pattern Class

Represents a pattern with an associated position and data.

Namespace: Rheo.Storage.Analyzing.Models

Assembly: Rheo.Storage.dll

Definition

[MessagePackObject]
public partial class Pattern

Properties

Property Type Description
Position UInt16 The offset position of the pattern in the file.
Data Byte[] Gets or sets the prefix pattern data.

Remarks

This class is used to store a pattern's offset position within a file and its corresponding data. Patterns are typically used to identify file types by matching byte sequences (magic numbers) at specific positions in the file header.


Property Details

Position

The offset position of the pattern in the file.

[Key(0)]
public ushort Position { get; set; }

Property Value

UInt16

The zero-based offset from the beginning of the file where this pattern should match.

Remarks

Position 0 represents the first byte of the file. Patterns at position 0 are weighted more heavily in the scoring algorithm as they typically represent the file's primary magic number.

The use of ushort limits the maximum position to 65,535 bytes, which is sufficient for file header identification.


Data

Gets or sets the prefix pattern data.

[Key(1)]
public byte[] Data { get; set; }

Property Value

Byte[]

The byte sequence that should match at the specified position.

Remarks

The data represents the exact byte sequence expected at the pattern's position. All bytes in the data array must match for the pattern to be considered valid.


Methods

ToString()

Returns a string representation of the pattern.

public override string ToString()

Returns

String

A string showing the ASCII representation of the data and its position.

Examples

var pattern = new Pattern
{
    Position = 0,
    Data = new byte[] { 0x50, 0x4B, 0x03, 0x04 }
};

Console.WriteLine(pattern.ToString());
// Output: "PK.." at 0

Examples

Creating a Pattern for PNG Files

The following example demonstrates how to create a pattern for the PNG file format:

using Rheo.Storage.Analyzing.Models;

var pngPattern = new Pattern
{
    Position = 0,
    Data = new byte[] 
    { 
        0x89, 0x50, 0x4E, 0x47, // PNG signature
        0x0D, 0x0A, 0x1A, 0x0A  // DOS line ending + EOF + Unix line ending
    }
};

Creating Multiple Patterns

The following example shows how to create patterns for a ZIP file:

using Rheo.Storage.Analyzing.Models;

// ZIP files start with "PK" signature
var zipHeaderPattern = new Pattern
{
    Position = 0,
    Data = new byte[] { 0x50, 0x4B, 0x03, 0x04 }
};

// Alternative ZIP signature for empty archives
var emptyZipPattern = new Pattern
{
    Position = 0,
    Data = new byte[] { 0x50, 0x4B, 0x05, 0x06 }
};

Using Patterns in a Signature

The following example demonstrates how patterns are typically used within a signature:

using Rheo.Storage.Analyzing.Models;

var signature = new Signature
{
    Patterns = new List<Pattern>
    {
        new Pattern 
        { 
            Position = 0, 
            Data = new byte[] { 0x1F, 0x8B } // GZIP magic number
        },
        new Pattern 
        { 
            Position = 2, 
            Data = new byte[] { 0x08 } // DEFLATE compression method
        }
    }
};

Converting Text to Pattern Data

The following example shows how to create patterns from text strings:

using System.Text;
using Rheo.Storage.Analyzing.Models;

// Create a pattern for XML files
var xmlPattern = new Pattern
{
    Position = 0,
    Data = Encoding.UTF8.GetBytes("<?xml")
};

// Create a pattern for HTML files
var htmlPattern = new Pattern
{
    Position = 0,
    Data = Encoding.ASCII.GetBytes("<!DOCTYPE html")
};

Thread Safety

This class is not thread-safe. When accessing instances from multiple threads, use appropriate synchronization mechanisms.

See Also

Clone this wiki locally