libaaruformat 1.0
Aaru Data Preservation Suite - Format Library
Loading...
Searching...
No Matches
lfg.c File Reference
#include "lfg.h"
#include <string.h>

Go to the source code of this file.

Functions

static uint32_t swap32 (uint32_t x)
static void lfg_forward (struct ngc_lfg_ctx *ctx)
static void lfg_backward (struct ngc_lfg_ctx *ctx, size_t start_word, size_t end_word)
static bool lfg_initialize (struct ngc_lfg_ctx *ctx, bool check_existing)
void ngc_lfg_set_seed (struct ngc_lfg_ctx *ctx, const uint32_t seed[17])
 Initialize the LFG from a 17-word big-endian seed.
void ngc_lfg_get_bytes (struct ngc_lfg_ctx *ctx, uint8_t *out, size_t count)
 Generate count bytes of junk data into out.
static bool lfg_reinitialize (struct ngc_lfg_ctx *ctx, uint32_t seed_out[17])
size_t ngc_lfg_get_seed (const uint8_t *data, size_t size, size_t data_offset, uint32_t seed_out[17])
 Try to extract the LFG seed from a chunk of data.

Function Documentation

◆ lfg_backward()

void lfg_backward ( struct ngc_lfg_ctx * ctx,
size_t start_word,
size_t end_word )
static

< LFG second tap.

< LFG second tap.

< LFG buffer size (number of uint32 words in state).

< LFG buffer size (number of uint32 words in state).

< LFG second tap.

< LFG second tap.

< LFG second tap.

< LFG buffer size (number of uint32 words in state).

< LFG second tap.

Definition at line 36 of file lfg.c.

References ngc_lfg_ctx::buffer, NGC_LFG_J, and NGC_LFG_K.

Referenced by lfg_reinitialize(), and ngc_lfg_get_seed().

◆ lfg_forward()

void lfg_forward ( struct ngc_lfg_ctx * ctx)
static

< LFG second tap.

< LFG buffer size (number of uint32 words in state).

< LFG second tap.

< LFG second tap.

< LFG buffer size (number of uint32 words in state).

< LFG second tap.

Definition at line 29 of file lfg.c.

References ngc_lfg_ctx::buffer, NGC_LFG_J, and NGC_LFG_K.

Referenced by lfg_initialize(), ngc_lfg_get_bytes(), and ngc_lfg_get_seed().

◆ lfg_initialize()

bool lfg_initialize ( struct ngc_lfg_ctx * ctx,
bool check_existing )
static

< Number of uint32 words needed to seed the LFG.

< LFG buffer size (number of uint32 words in state).

< LFG buffer size (number of uint32 words in state).

Definition at line 48 of file lfg.c.

References ngc_lfg_ctx::buffer, lfg_forward(), NGC_LFG_K, NGC_LFG_SEED_SIZE, and swap32().

Referenced by lfg_reinitialize(), and ngc_lfg_set_seed().

◆ lfg_reinitialize()

bool lfg_reinitialize ( struct ngc_lfg_ctx * ctx,
uint32_t seed_out[17/**< Number of uint32 words needed to seed the LFG. */] )
static

< LFG buffer size (number of uint32 words in state).

< LFG buffer size (number of uint32 words in state).

< Number of uint32 words needed to seed the LFG.

< Number of uint32 words needed to seed the LFG.

Definition at line 103 of file lfg.c.

References ngc_lfg_ctx::buffer, lfg_backward(), lfg_initialize(), NGC_LFG_K, NGC_LFG_SEED_SIZE, and swap32().

Referenced by ngc_lfg_get_seed().

◆ ngc_lfg_get_bytes()

void ngc_lfg_get_bytes ( struct ngc_lfg_ctx * ctx,
uint8_t * out,
size_t count )

Generate count bytes of junk data into out.

Parameters
ctxLFG context (must be initialized via ngc_lfg_set_seed).
outOutput buffer to fill.
countNumber of bytes to generate.

< LFG buffer size (number of uint32 words in state).

< LFG buffer size (number of uint32 words in state).

Definition at line 82 of file lfg.c.

References ngc_lfg_ctx::buffer, lfg_forward(), NGC_LFG_K, and ngc_lfg_ctx::position_bytes.

Referenced by ngcw_regenerate_junk_sector(), and wii_reconstruct_group_junk().

◆ ngc_lfg_get_seed()

size_t ngc_lfg_get_seed ( const uint8_t * data,
size_t size,
size_t data_offset,
uint32_t seed_out[17] )

Try to extract the LFG seed from a chunk of data.

Attempts to reverse-engineer the 17-word seed from a buffer of suspected PRNG output. Requires at least NGC_LFG_K * 4 = 2084 bytes of contiguous PRNG output aligned to uint32 boundaries.

Parameters
dataPointer to the data to test.
sizeNumber of bytes available.
data_offsetByte offset of data[0] within its LFG block (0x8000-aligned for Wii, arbitrary for GC).
seed_outIf successful, receives the 17-word seed (big-endian).
Returns
The number of consecutive bytes from data[0] that match the LFG. Returns 0 if the data does not look like junk.

< LFG buffer size (number of uint32 words in state).

< LFG buffer size (number of uint32 words in state).

< LFG buffer size (number of uint32 words in state).

< LFG buffer size (number of uint32 words in state).

< LFG buffer size (number of uint32 words in state).

< LFG buffer size (number of uint32 words in state).

< LFG buffer size (number of uint32 words in state).

< LFG buffer size (number of uint32 words in state).

< LFG buffer size (number of uint32 words in state).

< LFG buffer size (number of uint32 words in state).

< LFG buffer size (number of uint32 words in state).

Definition at line 121 of file lfg.c.

References ngc_lfg_ctx::buffer, lfg_backward(), lfg_forward(), lfg_reinitialize(), NGC_LFG_K, NGC_LFG_SEED_SIZE, ngc_lfg_ctx::position_bytes, and swap32().

◆ ngc_lfg_set_seed()

void ngc_lfg_set_seed ( struct ngc_lfg_ctx * ctx,
const uint32_t seed[17] )

Initialize the LFG from a 17-word big-endian seed.

Parameters
ctxLFG context to initialize.
seedArray of 17 uint32 seed words (big-endian).

< Number of uint32 words needed to seed the LFG.

Definition at line 73 of file lfg.c.

References ngc_lfg_ctx::buffer, lfg_initialize(), NGC_LFG_SEED_SIZE, ngc_lfg_ctx::position_bytes, and swap32().

Referenced by ngcw_regenerate_junk_sector(), and wii_reconstruct_group_junk().

◆ swap32()

uint32_t swap32 ( uint32_t x)
inlinestatic

Definition at line 26 of file lfg.c.

Referenced by lfg_initialize(), lfg_reinitialize(), ngc_lfg_get_seed(), and ngc_lfg_set_seed().