glriff

Types

Represents a RIFF (Resource Interchange File Format) chunk.

RIFF is a binary file format that stores data in tagged chunks. This type models the three types of chunks that can appear in RIFF files:

  • Chunk: A basic chunk with a 4-byte FourCC identifier and binary data
  • ListChunk: A LIST chunk containing a list of sub-chunks
  • RiffChunk: A RIFF chunk (the root container) with a FourCC type identifier and sub-chunks
pub type Chunk {
  Chunk(four_cc: BitArray, data: BitArray)
  ListChunk(chunks: List(Chunk))
  RiffChunk(four_cc: BitArray, chunks: List(Chunk))
}

Constructors

  • Chunk(four_cc: BitArray, data: BitArray)

    A basic RIFF chunk with a FourCC identifier and data payload.

    The four_cc field is a 4-byte identifier (e.g., “fmt “, “data”). The data field contains the chunk’s binary payload.

  • ListChunk(chunks: List(Chunk))

    A LIST chunk that contains multiple sub-chunks.

    LIST chunks are used to group related chunks together.

  • RiffChunk(four_cc: BitArray, chunks: List(Chunk))

    A RIFF chunk representing the root container of a RIFF file.

    The four_cc field specifies the file type (e.g., “WAVE” for WAV files). The chunks field contains all sub-chunks within this RIFF file.

Represents errors that can occur when parsing a RIFF chunk from binary data.

pub type FromBitArrayError {
  FailedToCreateChunkList(inner: ToChunkListError)
  InvalidFormat
}

Constructors

  • FailedToCreateChunkList(inner: ToChunkListError)

    Failed to create a list of chunks from the binary data.

    This error wraps a ToChunkListError that provides more detail about what went wrong during chunk list parsing.

  • InvalidFormat

    The binary data does not conform to the RIFF format specification.

    This can occur when:

    • The data is too short to contain required headers
    • The chunk size doesn’t match the actual data size
    • Required fields are missing or malformed

Represents errors that can occur when parsing a list of chunks from binary data.

pub type ToChunkListError {
  InvalidId
  InvalidSize
  InvalidData
  SizeIsDifference(size: Int, expected: Int)
}

Constructors

  • InvalidId

    The chunk identifier (FourCC) could not be read from the expected position.

  • InvalidSize

    The chunk size field could not be read from the expected position.

  • InvalidData

    The chunk data could not be read from the expected position.

  • SizeIsDifference(size: Int, expected: Int)

    The declared chunk size does not match the actual available data.

    The size field contains the declared size from the chunk header. The expected field contains the actual size of the data that was read.

Values

pub fn from_bit_array(
  bits: BitArray,
) -> Result(Chunk, FromBitArrayError)

Parses a RIFF chunk from binary data.

This function reads binary data in RIFF format and constructs the appropriate Chunk variant based on the chunk identifier:

  • “RIFF” → RiffChunk
  • “LIST” → ListChunk
  • Other FourCC → basic Chunk

The function validates that:

  • The data contains at least 8 bytes (FourCC + size)
  • The declared size matches the actual data size
  • All sub-chunks (for RIFF and LIST) are valid

Examples

let binary = <<"fmt ", 12:size(32)-little, "EXAMPLE_DATA">>
case from_bit_array(binary) {
  Ok(chunk) -> // Successfully parsed chunk
  Error(InvalidFormat) -> // Invalid RIFF data
}
pub fn to_bit_array(chunk: Chunk) -> BitArray

Converts a RIFF chunk to its binary representation.

This function serializes a chunk into the RIFF binary format, which consists of:

  • For basic Chunk: FourCC (4 bytes) + Size (4 bytes, little-endian) + Data
  • For ListChunk: “LIST” (4 bytes) + Size (4 bytes) + Concatenated sub-chunks
  • For RiffChunk: “RIFF” (4 bytes) + Size (4 bytes) + FourCC (4 bytes) + Concatenated sub-chunks

Examples

let chunk = Chunk(four_cc: <<"fmt ">>, data: <<"EXAMPLE_DATA">>)
let binary = to_bit_array(chunk)
// Returns: <<"fmt ", 12:size(32)-little, "EXAMPLE_DATA">>
Search Document