Complete batch 20261016-C-embedded-2: 45 verified GLM/Pi samples, independent regrades and archives; preserve difficulty misses and Salty blockage.
127 lines
3.6 KiB
C
127 lines
3.6 KiB
C
#include "journal.h"
|
|
#include <errno.h>
|
|
#include <string.h>
|
|
|
|
typedef struct {
|
|
uint16_t payload_length;
|
|
uint32_t sequence;
|
|
uint32_t checksum;
|
|
} record_header;
|
|
|
|
void journal_init(journal_state *state)
|
|
{
|
|
state->opened = 0;
|
|
state->end = 0;
|
|
state->sequence = 1;
|
|
state->dirty = 0;
|
|
}
|
|
|
|
static int decode_header(const uint8_t *bytes, uint32_t expected,
|
|
record_header *header)
|
|
{
|
|
if (memcmp(bytes, "LOG1", 4) != 0) {
|
|
return 0;
|
|
}
|
|
uint16_t length = journal_load16(bytes + 4);
|
|
if (length > JOURNAL_PAYLOAD_MAX || journal_load16(bytes + 6) != 0) {
|
|
return 0;
|
|
}
|
|
uint32_t sequence = journal_load32(bytes + 8);
|
|
if (sequence != expected) {
|
|
return 0;
|
|
}
|
|
header->payload_length = length;
|
|
header->sequence = sequence;
|
|
header->checksum = journal_load32(bytes + 12);
|
|
return 1;
|
|
}
|
|
|
|
static int body_valid(const uint8_t *header_bytes,
|
|
const record_header *header, const uint8_t *body)
|
|
{
|
|
uint32_t actual = journal_checksum(header_bytes, JOURNAL_PREFIX_SIZE,
|
|
body, header->payload_length);
|
|
if (actual != header->checksum) {
|
|
return 0;
|
|
}
|
|
uint32_t trailer = journal_load32(body + header->payload_length);
|
|
if (trailer != (header->sequence ^ UINT32_C(0xffffffff))) {
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static int read_header(const journal_io *io, int descriptor, uint64_t offset,
|
|
uint8_t *bytes, int *complete)
|
|
{
|
|
size_t received = 0;
|
|
int status = journal_read_exact(io, descriptor, bytes,
|
|
JOURNAL_HEADER_SIZE, offset, &received);
|
|
if (status != 0) {
|
|
return status;
|
|
}
|
|
*complete = received == JOURNAL_HEADER_SIZE;
|
|
return 0;
|
|
}
|
|
|
|
static int read_body(const journal_io *io, int descriptor, uint64_t offset,
|
|
const record_header *header, uint8_t *bytes,
|
|
int *complete)
|
|
{
|
|
size_t count = (size_t)header->payload_length + JOURNAL_TRAILER_SIZE;
|
|
size_t received = 0;
|
|
int status = journal_read_exact(io, descriptor, bytes, count,
|
|
offset + JOURNAL_HEADER_SIZE, &received);
|
|
if (status != 0) {
|
|
return status;
|
|
}
|
|
*complete = received == count;
|
|
return 0;
|
|
}
|
|
|
|
static void accept_record(journal_state *state, const record_header *header)
|
|
{
|
|
state->end += JOURNAL_HEADER_SIZE + (uint64_t)header->payload_length +
|
|
JOURNAL_TRAILER_SIZE;
|
|
++state->sequence;
|
|
}
|
|
|
|
int journal_recover(journal_state *state, const journal_io *io, int descriptor)
|
|
{
|
|
uint64_t length;
|
|
int status = journal_size(descriptor, &length);
|
|
if (status != 0) {
|
|
return status;
|
|
}
|
|
journal_state next;
|
|
journal_init(&next);
|
|
next.opened = 1;
|
|
uint8_t header_bytes[JOURNAL_HEADER_SIZE];
|
|
uint8_t body[JOURNAL_PAYLOAD_MAX + JOURNAL_TRAILER_SIZE];
|
|
while (next.end < length) {
|
|
int complete;
|
|
status = read_header(io, descriptor, next.end, header_bytes, &complete);
|
|
if (status != 0) {
|
|
return status;
|
|
}
|
|
if (!complete) {
|
|
break;
|
|
}
|
|
record_header header;
|
|
if (!decode_header(header_bytes, next.sequence, &header)) {
|
|
break;
|
|
}
|
|
status = read_body(io, descriptor, next.end, &header, body, &complete);
|
|
if (status != 0) {
|
|
return status;
|
|
}
|
|
if (!complete || !body_valid(header_bytes, &header, body)) {
|
|
break;
|
|
}
|
|
accept_record(&next, &header);
|
|
}
|
|
next.dirty = next.end != length;
|
|
*state = next;
|
|
return 0;
|
|
}
|