amos 9d633e9e67 Batch 15 (20261023-C-hwe-scale-15): 320 HWE-derived repair tasks
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>
2026-10-27 01:55:56 +08:00

291 lines
7.4 KiB
C

#include "decimal.h"
#include <string.h>
typedef enum {
FIELD_SEQ,
FIELD_RATE,
FIELD_WINDOW,
FIELD_CHANNEL,
FIELD_UNKNOWN
} field_id;
typedef struct {
const unsigned char *key;
size_t key_length;
const unsigned char *value;
size_t value_length;
} field_span;
static int equal_key(const field_span *span, const char *name)
{
size_t length = strlen(name);
if (span->key_length != length) {
return 0;
}
return memcmp(span->key, name, length) == 0;
}
static field_id identify(const field_span *span)
{
if (equal_key(span, "seq")) {
return FIELD_SEQ;
}
if (equal_key(span, "rate")) {
return FIELD_RATE;
}
if (equal_key(span, "window")) {
return FIELD_WINDOW;
}
if (equal_key(span, "channel")) {
return FIELD_CHANNEL;
}
return FIELD_UNKNOWN;
}
static int scan_field(const unsigned char *data,
size_t begin,
size_t end,
field_span *span)
{
size_t separator = end;
size_t i;
if (begin == end) {
return DF_SYNTAX;
}
for (i = begin; i < end; ++i) {
if (data[i] == '=') {
if (separator != end) {
return DF_SYNTAX;
}
separator = i;
}
}
if (separator == end || separator == begin) {
return DF_SYNTAX;
}
span->key = data + begin;
span->key_length = separator - begin;
span->value = data + separator + 1;
span->value_length = end - separator - 1;
return DF_OK;
}
static int decode_value(field_id id,
const field_span *span,
df_config *config)
{
switch (id) {
case FIELD_SEQ:
return df_number(span->value, span->value_length,
4, 0, 9999, &config->seq);
case FIELD_RATE:
return df_number(span->value, span->value_length,
4, 1, 2000, &config->rate);
case FIELD_WINDOW:
return df_number(span->value, span->value_length,
3, 1, 128, &config->window);
case FIELD_CHANNEL:
return df_number(span->value, span->value_length,
2, 0, 15, &config->channel);
default:
return DF_KEY;
}
}
void df_init(df_state *state)
{
state->current.seq = 0;
state->current.rate = 25;
state->current.window = 8;
state->current.channel = 2;
state->accepted = 0;
state->rejected = 0;
state->last = DF_OK;
state->history_head = 0;
state->history_count = 0;
}
int df_validate(const df_config *config)
{
if (config->seq > 9999) {
return DF_RANGE;
}
if (config->rate < 1 || config->rate > 2000) {
return DF_RANGE;
}
if (config->window < 1 || config->window > 128) {
return DF_RANGE;
}
if (config->channel > 15) {
return DF_RANGE;
}
if (config->rate * config->window > 64000) {
return DF_BUDGET;
}
return DF_OK;
}
int df_reject(df_state *state, int status)
{
state->rejected += 1;
state->last = status;
return status;
}
static void append_history(df_state *state)
{
state->history[state->history_head] = state->current;
state->history_head += 1;
if (state->history_head == DF_HISTORY_MAX) {
state->history_head = 0;
}
if (state->history_count < DF_HISTORY_MAX) {
state->history_count += 1;
}
}
static int decode_frame(const unsigned char *data,
size_t length,
df_config *proposed)
{
size_t begin = 0;
unsigned seen = 0;
for (;;) {
size_t end = begin;
field_span span;
field_id id;
unsigned bit;
int status;
while (end < length && data[end] != ';') {
++end;
}
status = scan_field(data, begin, end, &span);
if (status != DF_OK) {
return status;
}
id = identify(&span);
if (id == FIELD_UNKNOWN) {
return DF_KEY;
}
bit = 1u << (unsigned)id;
if (seen & bit) {
return DF_DUP;
}
status = decode_value(id, &span, proposed);
if (status != DF_OK) {
return status;
}
seen |= bit;
if (end == length) {
break;
}
begin = end + 1;
}
if (!(seen & 1u) || !(seen & 14u)) {
return DF_SYNTAX;
}
return DF_OK;
}
int df_apply(df_state *state,
const unsigned char *data,
size_t length)
{
df_config proposed = state->current;
int status;
if (length == 0) {
return df_reject(state, DF_EMPTY);
}
if (length > DF_FRAME_MAX) {
return df_reject(state, DF_LENGTH);
}
status = decode_frame(data, length, &proposed);
if (status != DF_OK) {
return df_reject(state, status);
}
if (proposed.seq != (state->current.seq + 1) % 10000) {
return df_reject(state, DF_SEQUENCE);
}
status = df_validate(&proposed);
if (status != DF_OK) {
return df_reject(state, status);
}
state->current = proposed;
state->accepted += 1;
state->last = DF_OK;
append_history(state);
return DF_OK;
}
size_t df_history_size(const df_state *state)
{
return state->history_count;
}
int df_history(const df_state *state,
size_t age,
df_config *out)
{
size_t index;
if (age >= state->history_count) {
return DF_HISTORY;
}
index = (state->history_head + DF_HISTORY_MAX - 1 - age)
% DF_HISTORY_MAX;
*out = state->history[index];
return DF_OK;
}
static void text_append(char *buffer, size_t *position, const char *text)
{
while (*text != '\0') {
buffer[*position] = *text;
*position += 1;
++text;
}
}
static void decimal_append(char *buffer,
size_t *position,
uint32_t value,
unsigned width)
{
unsigned i;
size_t start = *position;
*position += width;
for (i = width; i > 0; --i) {
buffer[start + i - 1] = (char)('0' + value % 10);
value /= 10;
}
}
int df_encode(const df_config *config,
char *out,
size_t capacity,
size_t *written)
{
char buffer[DF_ENCODED_MAX];
size_t length = 0;
int status = df_validate(config);
if (status != DF_OK) {
return status;
}
text_append(buffer, &length, "seq=");
decimal_append(buffer, &length, config->seq, 4);
text_append(buffer, &length, ";rate=");
decimal_append(buffer, &length, config->rate, 4);
text_append(buffer, &length, ";window=");
decimal_append(buffer, &length, config->window, 3);
text_append(buffer, &length, ";channel=");
decimal_append(buffer, &length, config->channel, 2);
if (capacity <= length) {
return DF_CAPACITY;
}
buffer[length] = '\0';
memcpy(out, buffer, length + 1);
*written = length;
return DF_OK;
}
unsigned df_changed_mask(const df_config *before,
const df_config *after)
{
unsigned changes = 0;
if (before->seq != after->seq) {
changes |= 1u;
}
if (before->rate != after->rate) {
changes |= 2u;
}
if (before->window != after->window) {
changes |= 4u;
}
if (before->channel != after->channel) {
changes |= 8u;
}
return changes;
}