libaaruformat 1.0
Aaru Data Preservation Suite - Format Library
Loading...
Searching...
No Matches
reed_solomon.h File Reference
#include <stddef.h>
#include <stdint.h>

Go to the source code of this file.

Typedefs

typedef struct rs_context rs_context
 Opaque Reed-Solomon codec context.

Functions

rs_context * rs_create (uint16_t K, uint16_t M)
 Create a Reed-Solomon codec for RS(K, M) over GF(2^8).
void rs_free (rs_context *ctx)
 Free a Reed-Solomon codec context.
uint8_t rs_get_coefficient (const rs_context *ctx, uint16_t m, uint16_t k)
 Get the generator matrix coefficient for parity shard m, data shard k.
void rs_encode_incremental (uint8_t coeff, const uint8_t *data, uint8_t *parity, size_t shard_size)
 Incrementally accumulate one data shard's contribution to one parity shard.
int rs_decode (const rs_context *ctx, uint8_t **shards, const uint8_t *present, size_t shard_size)
 Decode (reconstruct) erased shards.

Typedef Documentation

◆ rs_context

typedef struct rs_context rs_context

Opaque Reed-Solomon codec context.

Definition at line 28 of file reed_solomon.h.

Function Documentation

◆ rs_create()

rs_context * rs_create ( uint16_t K,
uint16_t M )

Create a Reed-Solomon codec for RS(K, M) over GF(2^8).

K = number of data shards, M = number of parity shards. K + M must be <= 255 (GF(2^8) field size minus 1).

The codec precomputes the Vandermonde-derived generator matrix (M rows x K columns) used for encoding.

Parameters
KNumber of data shards (>= 1).
MNumber of parity shards (>= 1).
Returns
Codec context, or NULL on error.

Definition at line 150 of file reed_solomon.c.

References build_vandermonde(), rs_context::coding, rs_context::gen, gf256_mul(), invert_matrix(), rs_context::K, rs_context::M, and rs_create().

Referenced by aaruf_set_erasure_coding(), ec_load_ecmb(), ec_recover_raw_block(), ec_write_batch_parity(), and rs_create().

◆ rs_decode()

int rs_decode ( const rs_context * ctx,
uint8_t ** shards,
const uint8_t * present,
size_t shard_size )

Decode (reconstruct) erased shards.

Given K+M shards where some are erased, reconstruct the erased ones using Gaussian elimination over GF(2^8).

Parameters
ctxCodec context.
shardsArray of K+M shard pointers (each shard_size bytes). Erased shards must point to allocated buffers of shard_size bytes (content will be overwritten).
presentBoolean array of K+M entries: 1 = shard is valid, 0 = shard is erased.
shard_sizeNumber of bytes per shard.
Returns
0 on success, -1 if too many erasures (> M), -2 on allocation failure.

Definition at line 218 of file reed_solomon.c.

References rs_context::coding, gf256_mul(), gf256_mul_region(), invert_matrix(), rs_context::K, rs_context::M, and rs_decode().

Referenced by ec_recover_data_block(), ec_recover_raw_block(), and rs_decode().

◆ rs_encode_incremental()

void rs_encode_incremental ( uint8_t coeff,
const uint8_t * data,
uint8_t * parity,
size_t shard_size )

Incrementally accumulate one data shard's contribution to one parity shard.

Computes: parity[i] ^= GF_mul(data[i], coeff) for all i in [0, shard_size).

This is the core primitive for streaming write: for each data block written, call this M times (once per parity shard) with the appropriate coefficient.

For M=1 (XOR-only), coeff is always 1, and this reduces to XOR.

Parameters
coeffGF(2^8) coefficient from rs_get_coefficient().
dataData shard bytes (read-only, shard_size bytes).
parityParity shard accumulator (read-write, shard_size bytes, must be zeroed before first call).
shard_sizeNumber of bytes per shard.

Definition at line 213 of file reed_solomon.c.

References gf256_mul_region(), and rs_encode_incremental().

Referenced by ec_accumulate_data_block(), ec_write_batch_parity(), and rs_encode_incremental().

◆ rs_free()

void rs_free ( rs_context * ctx)

Free a Reed-Solomon codec context.

Parameters
ctxContext returned by rs_create().

Definition at line 201 of file reed_solomon.c.

References rs_context::coding, and rs_free().

Referenced by aaruf_set_erasure_coding(), ec_free(), ec_recover_raw_block(), ec_write_batch_parity(), and rs_free().

◆ rs_get_coefficient()

uint8_t rs_get_coefficient ( const rs_context * ctx,
uint16_t m,
uint16_t k )

Get the generator matrix coefficient for parity shard m, data shard k.

During incremental encoding, call this to get the coefficient, then call rs_encode_incremental() with it.

Parameters
ctxCodec context.
mParity shard index (0 .. M-1).
kData shard index (0 .. K-1).
Returns
GF(2^8) coefficient.

Definition at line 208 of file reed_solomon.c.

References rs_context::gen, rs_context::K, and rs_get_coefficient().

Referenced by ec_accumulate_data_block(), ec_write_batch_parity(), and rs_get_coefficient().