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 dataListChunk: A LIST chunk containing a list of sub-chunksRiffChunk: 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_ccfield is a 4-byte identifier (e.g., “fmt “, “data”). Thedatafield 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_ccfield specifies the file type (e.g., “WAVE” for WAV files). Thechunksfield 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
ToChunkListErrorthat provides more detail about what went wrong during chunk list parsing. -
InvalidFormatThe 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
-
InvalidIdThe chunk identifier (FourCC) could not be read from the expected position.
-
InvalidSizeThe chunk size field could not be read from the expected position.
-
InvalidDataThe 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
sizefield contains the declared size from the chunk header. Theexpectedfield 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">>