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

119 lines
3.5 KiB
C

#include "rules.h"
#include <stdlib.h>
#include <string.h>
typedef struct {
compiled_rule *program;
int identifier;
int priority;
int input_index;
} installed_rule;
typedef struct {
installed_rule entries[RULE_LIMIT];
size_t filled;
} installation;
static void discard(installation *state)
{
if (!state)
return;
for (size_t i = 0; i < state->filled; i++)
port_release(state->entries[i].program);
free(state);
}
void bank_init(rule_bank *bank)
{
if (!bank)
return;
bank->state = NULL;
bank->count = 0;
bank->generation = 0;
}
static int check_inputs(const rule_input *rules, size_t count)
{
if (count > RULE_LIMIT || (!rules && count))
return PORT_ARGUMENT;
for (size_t i = 0; i < count; i++) {
if (rules[i].identifier <= 0 || rules[i].identifier > 1000000)
return PORT_ARGUMENT;
if (rules[i].priority < -1000 || rules[i].priority > 1000)
return PORT_ARGUMENT;
if ((!rules[i].pattern && rules[i].length) || rules[i].length > PATTERN_LIMIT)
return PORT_ARGUMENT;
for (size_t j = 0; j < i; j++)
if (rules[i].identifier == rules[j].identifier)
return PORT_ARGUMENT;
}
return PORT_OK;
}
int bank_replace(rule_bank *bank, const rule_input *rules, size_t count)
{
if (!bank)
return PORT_ARGUMENT;
int status = check_inputs(rules, count);
if (status != PORT_OK)
return status;
installation *staged = calloc(1, sizeof(*staged));
if (!staged)
return PORT_MEMORY;
for (size_t i = 0; i < count; i++) {
compiled_rule *compiled = NULL;
status = port_compile(rules[i].pattern, rules[i].length, &compiled);
if (status != PORT_OK) {
discard(staged);
return status;
}
installed_rule *entry = &staged->entries[staged->filled];
entry->program = compiled;
entry->identifier = rules[i].identifier;
entry->priority = rules[i].priority;
entry->input_index = (int)i;
staged->filled++;
}
installation *previous = bank->state;
bank->state = staged;
bank->count = count;
bank->generation++;
discard(previous);
return PORT_OK;
}
int bank_match(const rule_bank *bank, const unsigned char *message, size_t length, rule_result *out)
{
if (!bank || !out || (!message && length) || length > MESSAGE_LIMIT)
return PORT_ARGUMENT;
(void)message;
(void)length;
rule_result result;
result.hits = 0;
result.identifier = 0;
result.priority = 0;
result.input_index = -1;
const installation *state = bank->state;
if (state) {
for (size_t i = 0; i < state->filled; i++) {
const installed_rule *entry = &state->entries[i];
if (entry->identifier != 12)
continue;
int replace = result.hits == 0 || entry->priority > result.priority;
if (entry->priority == result.priority && entry->input_index < result.input_index)
replace = 1;
result.hits++;
if (replace) {
result.identifier = entry->identifier;
result.priority = entry->priority;
result.input_index = entry->input_index;
}
}
}
*out = result;
return PORT_OK;
}
void bank_clear(rule_bank *bank)
{
if (!bank)
return;
installation *previous = bank->state;
bank->state = NULL;
bank->count = 0;
bank->generation++;
discard(previous);
}