One user-space repair task per eligible HWE debug-diag / compile-deploy base ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content filter. Each ships a public spec with a check-to-basis table, at least two defects with single-fix fixtures, a symptom-masking fixture and an alternative correct implementation. Claude re-checked all 320: structure, the HWE quality items and trace6 Docker calibration receipts against current digests (320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120 ad-hoc family names; the next batch maps them to the canonical list. The batch's 1,256-line progress log stays in its factory report and the 36-local branch batch15-hwe. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
379 lines
10 KiB
C
379 lines
10 KiB
C
#include "filled_record.h"
|
|
#include <string.h>
|
|
static unsigned read16(const uint8_t *data)
|
|
{
|
|
return (unsigned)data[0] | ((unsigned)data[1] << 8);
|
|
}
|
|
static uint32_t read32(const uint8_t *data)
|
|
{
|
|
uint32_t value = 0;
|
|
unsigned i;
|
|
for (i = 0; i < 4; ++i) {
|
|
value |= (uint32_t)data[i] << (8 * i);
|
|
}
|
|
return value;
|
|
}
|
|
static void store(rf_record *record, unsigned at, unsigned value)
|
|
{
|
|
record->bytes[at] = (uint8_t)value;
|
|
record->defined[at] = 1;
|
|
}
|
|
static void store16(rf_record *record, unsigned at, unsigned value)
|
|
{
|
|
store(record, at, value);
|
|
store(record, at + 1, value >> 8);
|
|
}
|
|
static void store32(rf_record *record, unsigned at, uint32_t value)
|
|
{
|
|
unsigned i;
|
|
for (i = 0; i < 4; ++i) {
|
|
store(record, at + i, value >> (8 * i));
|
|
}
|
|
}
|
|
static uint32_t checksum(const uint8_t *data, unsigned size)
|
|
{
|
|
uint32_t value = UINT32_C(2166136261);
|
|
unsigned i;
|
|
for (i = 0; i < size; ++i) {
|
|
value = (value ^ data[i]) * UINT32_C(16777619);
|
|
}
|
|
return value;
|
|
}
|
|
int rf_config_check(const rf_config *config)
|
|
{
|
|
if (config == NULL) {
|
|
return RF_INVALID;
|
|
}
|
|
if (config->alignment == 0 || config->alignment > 16) {
|
|
return RF_INVALID;
|
|
}
|
|
if ((config->alignment & (config->alignment - 1)) != 0) {
|
|
return RF_INVALID;
|
|
}
|
|
if (config->max_payload > RF_PAYLOAD) {
|
|
return RF_INVALID;
|
|
}
|
|
if (config->fill > 255 || config->flags > 255 || config->poison > 255) {
|
|
return RF_INVALID;
|
|
}
|
|
return RF_OK;
|
|
}
|
|
int rf_begin(rf_record *record, const rf_config *config, uint32_t sequence, unsigned kind)
|
|
{
|
|
unsigned i;
|
|
if (record == NULL || rf_config_check(config) != RF_OK || kind > 255) {
|
|
return RF_INVALID;
|
|
}
|
|
memset(record, 0, sizeof(*record));
|
|
record->config = *config;
|
|
(void)sequence;
|
|
record->sequence = 7;
|
|
record->kind = kind;
|
|
record->state = 1;
|
|
for (i = 0; i < RF_STORAGE; ++i) {
|
|
record->bytes[i] = (uint8_t)(config->poison + 17 * i);
|
|
}
|
|
return RF_OK;
|
|
}
|
|
static int append(rf_record *record, unsigned tag, unsigned type,
|
|
const uint8_t *data, unsigned size)
|
|
{
|
|
unsigned at;
|
|
unsigned i;
|
|
if (record == NULL || tag > 255 || size > RF_PAYLOAD) {
|
|
return RF_INVALID;
|
|
}
|
|
if (size != 0 && data == NULL) {
|
|
return RF_INVALID;
|
|
}
|
|
if (record->state != 1) {
|
|
return RF_STATE;
|
|
}
|
|
if (size + 4 > record->config.max_payload - record->payload) {
|
|
return RF_FULL;
|
|
}
|
|
at = RF_HEADER + record->payload;
|
|
store(record, at, tag);
|
|
store(record, at + 1, type);
|
|
store16(record, at + 2, size);
|
|
for (i = 0; i < size; ++i) {
|
|
store(record, at + 4 + i, data[i]);
|
|
}
|
|
record->payload += size + 4;
|
|
record->items += 1;
|
|
return RF_OK;
|
|
}
|
|
int rf_u8(rf_record *record, unsigned tag, unsigned value)
|
|
{
|
|
uint8_t data[1];
|
|
if (value > 255) {
|
|
return RF_INVALID;
|
|
}
|
|
data[0] = (uint8_t)value;
|
|
return append(record, tag, 1, data, 1);
|
|
}
|
|
int rf_u16(rf_record *record, unsigned tag, unsigned value)
|
|
{
|
|
uint8_t data[2];
|
|
if (value > 65535) {
|
|
return RF_INVALID;
|
|
}
|
|
data[0] = (uint8_t)value;
|
|
data[1] = (uint8_t)(value >> 8);
|
|
return append(record, tag, 2, data, 2);
|
|
}
|
|
int rf_u32(rf_record *record, unsigned tag, uint32_t value)
|
|
{
|
|
uint8_t data[4];
|
|
unsigned i;
|
|
for (i = 0; i < 4; ++i) {
|
|
data[i] = (uint8_t)(value >> (8 * i));
|
|
}
|
|
return append(record, tag, 3, data, 4);
|
|
}
|
|
int rf_bytes(rf_record *record, unsigned tag, const uint8_t *data, size_t size)
|
|
{
|
|
if (size > RF_PAYLOAD) {
|
|
return RF_INVALID;
|
|
}
|
|
return append(record, tag, 4, data, (unsigned)size);
|
|
}
|
|
int rf_text(rf_record *record, unsigned tag, const uint8_t *data, size_t size)
|
|
{
|
|
size_t i;
|
|
if (size > RF_PAYLOAD || (size != 0 && data == NULL)) {
|
|
return RF_INVALID;
|
|
}
|
|
for (i = 0; i < size; ++i) {
|
|
if (data[i] < 32 || data[i] > 126) {
|
|
return RF_INVALID;
|
|
}
|
|
}
|
|
return append(record, tag, 5, data, (unsigned)size);
|
|
}
|
|
int rf_repeat(rf_record *record, unsigned tag, unsigned value, unsigned count)
|
|
{
|
|
uint8_t data[RF_PAYLOAD];
|
|
unsigned i;
|
|
if (value > 255 || count > RF_PAYLOAD) {
|
|
return RF_INVALID;
|
|
}
|
|
for (i = 0; i < count; ++i) {
|
|
data[i] = (uint8_t)value;
|
|
}
|
|
return append(record, tag, 4, data, count);
|
|
}
|
|
int rf_mark(rf_record *record)
|
|
{
|
|
if (record == NULL) {
|
|
return RF_INVALID;
|
|
}
|
|
if (record->state != 1) {
|
|
return RF_STATE;
|
|
}
|
|
record->mark_payload = record->payload;
|
|
record->mark_items = record->items;
|
|
record->marked = 1;
|
|
return RF_OK;
|
|
}
|
|
int rf_rollback(rf_record *record)
|
|
{
|
|
unsigned i;
|
|
if (record == NULL) {
|
|
return RF_INVALID;
|
|
}
|
|
if (record->state != 1 || record->marked == 0) {
|
|
return RF_STATE;
|
|
}
|
|
for (i = record->mark_payload; i < record->payload; ++i) {
|
|
record->bytes[RF_HEADER + i] = (uint8_t)(record->config.poison + 17 * (RF_HEADER + i));
|
|
record->defined[RF_HEADER + i] = 0;
|
|
}
|
|
record->payload = record->mark_payload;
|
|
record->items = record->mark_items;
|
|
record->marked = 0;
|
|
return RF_OK;
|
|
}
|
|
static void header(rf_record *record, unsigned total)
|
|
{
|
|
store(record, 0, 0x52);
|
|
store(record, 1, 0x46);
|
|
store(record, 2, 0x31);
|
|
store(record, 3, 1);
|
|
store(record, 4, record->kind);
|
|
store(record, 5, record->config.flags);
|
|
store16(record, 6, record->items);
|
|
store32(record, 8, record->sequence);
|
|
store16(record, 12, record->payload);
|
|
store16(record, 14, total);
|
|
store(record, 16, record->config.fill);
|
|
store(record, 17, 0);
|
|
store(record, 18, 0);
|
|
store(record, 19, 0);
|
|
}
|
|
int rf_seal(rf_record *record)
|
|
{
|
|
unsigned end;
|
|
unsigned total;
|
|
unsigned i;
|
|
if (record == NULL) {
|
|
return RF_INVALID;
|
|
}
|
|
if (record->state != 1) {
|
|
return RF_STATE;
|
|
}
|
|
end = RF_HEADER + record->payload;
|
|
total = (end + RF_TRAILER + record->config.alignment - 1) &
|
|
~(record->config.alignment - 1);
|
|
header(record, total);
|
|
for (i = end; i < total - RF_TRAILER; ++i) {
|
|
store(record, i, record->config.fill + i - end);
|
|
}
|
|
store32(record, total - RF_TRAILER, checksum(record->bytes, total - RF_TRAILER));
|
|
record->length = total;
|
|
record->state = 2;
|
|
record->marked = 0;
|
|
return RF_OK;
|
|
}
|
|
static int walk(const uint8_t *data, unsigned payload, unsigned expected)
|
|
{
|
|
unsigned at = RF_HEADER;
|
|
unsigned end = RF_HEADER + payload;
|
|
unsigned items = 0;
|
|
while (at < end) {
|
|
unsigned size;
|
|
unsigned type;
|
|
unsigned i;
|
|
if (end - at < 4) {
|
|
return RF_CORRUPT;
|
|
}
|
|
type = data[at + 1];
|
|
size = read16(data + at + 2);
|
|
if (size > end - at - 4 || type < 1 || type > 5) {
|
|
return RF_CORRUPT;
|
|
}
|
|
if ((type == 1 && size != 1) || (type == 2 && size != 2) || (type == 3 && size != 4)) {
|
|
return RF_CORRUPT;
|
|
}
|
|
if (type == 5) {
|
|
for (i = 0; i < size; ++i) {
|
|
if (data[at + 4 + i] < 32 || data[at + 4 + i] > 126) {
|
|
return RF_CORRUPT;
|
|
}
|
|
}
|
|
}
|
|
at += size + 4;
|
|
items += 1;
|
|
}
|
|
return items == expected ? RF_OK : RF_CORRUPT;
|
|
}
|
|
static int wire_check(const rf_config *config, const uint8_t *data, unsigned size)
|
|
{
|
|
unsigned payload;
|
|
unsigned i;
|
|
if (size < RF_HEADER + RF_TRAILER || size > RF_STORAGE || size % config->alignment != 0) {
|
|
return RF_CORRUPT;
|
|
}
|
|
if (data[0] != 0x52 || data[1] != 0x46 || data[2] != 0x31 || data[3] != 1) {
|
|
return RF_CORRUPT;
|
|
}
|
|
if (data[5] != config->flags || data[16] != config->fill || data[17] || data[18] || data[19]) {
|
|
return RF_CORRUPT;
|
|
}
|
|
payload = read16(data + 12);
|
|
if (payload > config->max_payload || read16(data + 14) != size || payload > size - 24) {
|
|
return RF_CORRUPT;
|
|
}
|
|
if (((RF_HEADER + payload + RF_TRAILER + config->alignment - 1) & ~(config->alignment - 1)) != size) {
|
|
return RF_CORRUPT;
|
|
}
|
|
for (i = RF_HEADER + payload; i < size - RF_TRAILER; ++i) {
|
|
if (data[i] != (uint8_t)(config->fill + i - RF_HEADER - payload)) {
|
|
return RF_CORRUPT;
|
|
}
|
|
}
|
|
if (read32(data + size - RF_TRAILER) != checksum(data, size - RF_TRAILER)) {
|
|
return RF_CORRUPT;
|
|
}
|
|
return walk(data, payload, read16(data + 6));
|
|
}
|
|
int rf_import(rf_record *record, const rf_config *config, const uint8_t *data, size_t size)
|
|
{
|
|
rf_record temporary;
|
|
unsigned i;
|
|
int result;
|
|
if (record == NULL || rf_config_check(config) != RF_OK || data == NULL || size > RF_STORAGE) {
|
|
return RF_INVALID;
|
|
}
|
|
result = wire_check(config, data, (unsigned)size);
|
|
if (result != RF_OK) {
|
|
return result;
|
|
}
|
|
rf_begin(&temporary, config, read32(data + 8), data[4]);
|
|
for (i = 0; i < size; ++i) {
|
|
store(&temporary, i, data[i]);
|
|
}
|
|
temporary.payload = read16(data + 12);
|
|
temporary.items = read16(data + 6);
|
|
temporary.length = (unsigned)size;
|
|
temporary.state = 2;
|
|
*record = temporary;
|
|
return RF_OK;
|
|
}
|
|
int rf_validate(const rf_record *record)
|
|
{
|
|
unsigned i;
|
|
if (record == NULL) {
|
|
return RF_INVALID;
|
|
}
|
|
if (record->state != 2) {
|
|
return RF_STATE;
|
|
}
|
|
if (record->length > RF_STORAGE || rf_config_check(&record->config) != RF_OK) {
|
|
return RF_CORRUPT;
|
|
}
|
|
for (i = 0; i < record->length; ++i) {
|
|
if (!record->defined[i]) {
|
|
return RF_CORRUPT;
|
|
}
|
|
}
|
|
return wire_check(&record->config, record->bytes, record->length);
|
|
}
|
|
int rf_payload_size(const rf_record *record, unsigned *size)
|
|
{
|
|
int result;
|
|
if (size == NULL) {
|
|
return RF_INVALID;
|
|
}
|
|
result = rf_validate(record);
|
|
if (result == RF_OK) {
|
|
*size = record->payload;
|
|
}
|
|
return result;
|
|
}
|
|
int rf_item(const rf_record *record, unsigned index, unsigned *tag, unsigned *type,
|
|
const uint8_t **data, unsigned *size)
|
|
{
|
|
unsigned at = RF_HEADER;
|
|
unsigned i;
|
|
int result;
|
|
if (tag == NULL || type == NULL || data == NULL || size == NULL) {
|
|
return RF_INVALID;
|
|
}
|
|
result = rf_validate(record);
|
|
if (result != RF_OK) {
|
|
return result;
|
|
}
|
|
if (index >= record->items) {
|
|
return RF_INVALID;
|
|
}
|
|
for (i = 0; i < index; ++i) {
|
|
at += 4 + read16(record->bytes + at + 2);
|
|
}
|
|
*tag = record->bytes[at];
|
|
*type = record->bytes[at + 1];
|
|
*size = read16(record->bytes + at + 2);
|
|
*data = record->bytes + at + 4;
|
|
return RF_OK;
|
|
}
|