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>
213 lines
6.4 KiB
C
213 lines
6.4 KiB
C
#include "rules.h"
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
typedef struct {
|
|
unsigned char set[32];
|
|
unsigned char minimum;
|
|
unsigned char repeated;
|
|
} term;
|
|
struct compiled_rule {
|
|
size_t count;
|
|
int anchored_start;
|
|
int anchored_end;
|
|
term sequence[TERM_LIMIT];
|
|
};
|
|
static int remaining = -1;
|
|
static port_usage usage;
|
|
void port_budget(int budget)
|
|
{
|
|
remaining = budget;
|
|
}
|
|
port_usage port_current_usage(void)
|
|
{
|
|
return usage;
|
|
}
|
|
static void put_byte(term *item, unsigned byte)
|
|
{
|
|
item->set[byte / 8] |= (unsigned char)(1u << (byte % 8));
|
|
}
|
|
static int contains(const term *item, unsigned byte)
|
|
{
|
|
return (item->set[byte / 8] >> (byte % 8)) & 1u;
|
|
}
|
|
static int escaped_byte(const unsigned char *pattern, size_t length, size_t *position, unsigned *byte)
|
|
{
|
|
if (*position >= length)
|
|
return PORT_SYNTAX;
|
|
*byte = pattern[(*position)++];
|
|
if (*byte == '\\') {
|
|
if (*position >= length)
|
|
return PORT_SYNTAX;
|
|
*byte = pattern[(*position)++];
|
|
}
|
|
return PORT_OK;
|
|
}
|
|
static int parse_class(const unsigned char *pattern, size_t length, size_t *position, term *item)
|
|
{
|
|
int negate = 0;
|
|
size_t members = 0;
|
|
if (*position < length && pattern[*position] == '^') {
|
|
negate = 1;
|
|
(*position)++;
|
|
}
|
|
while (*position < length && pattern[*position] != ']') {
|
|
unsigned first;
|
|
if (escaped_byte(pattern, length, position, &first) != PORT_OK)
|
|
return PORT_SYNTAX;
|
|
if (first == 0 || first > 127)
|
|
return PORT_SYNTAX;
|
|
if (*position < length && pattern[*position] == '-') {
|
|
unsigned last;
|
|
(*position)++;
|
|
if (*position >= length || pattern[*position] == ']')
|
|
return PORT_SYNTAX;
|
|
if (escaped_byte(pattern, length, position, &last) != PORT_OK)
|
|
return PORT_SYNTAX;
|
|
if (last < first || last > 127)
|
|
return PORT_SYNTAX;
|
|
for (unsigned byte = first; byte <= last; byte++)
|
|
put_byte(item, byte);
|
|
} else {
|
|
put_byte(item, first);
|
|
}
|
|
members++;
|
|
}
|
|
if (*position >= length || members == 0)
|
|
return PORT_SYNTAX;
|
|
(*position)++;
|
|
if (negate)
|
|
for (size_t i = 0; i < sizeof(item->set); i++)
|
|
item->set[i] = (unsigned char)~item->set[i];
|
|
return PORT_OK;
|
|
}
|
|
static int parse_atom(const unsigned char *pattern, size_t length, size_t *position, term *item)
|
|
{
|
|
unsigned byte = pattern[(*position)++];
|
|
memset(item, 0, sizeof(*item));
|
|
item->minimum = 1;
|
|
if (byte == '.') {
|
|
memset(item->set, 255, sizeof(item->set));
|
|
} else if (byte == '[') {
|
|
return parse_class(pattern, length, position, item);
|
|
} else if (byte == '\\') {
|
|
if (*position >= length)
|
|
return PORT_SYNTAX;
|
|
byte = pattern[(*position)++];
|
|
if (byte == 0 || byte > 127)
|
|
return PORT_SYNTAX;
|
|
put_byte(item, byte);
|
|
} else {
|
|
if (byte == '^' || byte == '$' || byte == '*' || byte == '+' || byte == '?' || byte == ']')
|
|
return PORT_SYNTAX;
|
|
if (byte == 0 || byte > 127)
|
|
return PORT_SYNTAX;
|
|
put_byte(item, byte);
|
|
}
|
|
return PORT_OK;
|
|
}
|
|
static int parse_program(const unsigned char *pattern, size_t length, compiled_rule *result)
|
|
{
|
|
size_t position = 0;
|
|
memset(result, 0, sizeof(*result));
|
|
if (length > PATTERN_LIMIT || (!pattern && length))
|
|
return PORT_ARGUMENT;
|
|
if (length && pattern[0] == '^') {
|
|
result->anchored_start = 1;
|
|
position++;
|
|
}
|
|
while (position < length) {
|
|
term item;
|
|
if (pattern[position] == '$' && position + 1 == length) {
|
|
result->anchored_end = 1;
|
|
position++;
|
|
break;
|
|
}
|
|
int status = parse_atom(pattern, length, &position, &item);
|
|
if (status != PORT_OK)
|
|
return status;
|
|
if (position < length) {
|
|
unsigned suffix = pattern[position];
|
|
if (suffix == '*' || suffix == '+' || suffix == '?') {
|
|
item.minimum = suffix == '+';
|
|
item.repeated = suffix != '?';
|
|
position++;
|
|
}
|
|
}
|
|
if (result->count >= TERM_LIMIT)
|
|
return PORT_ARGUMENT;
|
|
result->sequence[result->count++] = item;
|
|
}
|
|
return PORT_OK;
|
|
}
|
|
int port_compile(const unsigned char *pattern, size_t length, compiled_rule **out)
|
|
{
|
|
compiled_rule parsed;
|
|
compiled_rule *owned;
|
|
if (!out)
|
|
return PORT_ARGUMENT;
|
|
*out = NULL;
|
|
int status = parse_program(pattern, length, &parsed);
|
|
if (status != PORT_OK)
|
|
return status;
|
|
if (remaining == 0)
|
|
return PORT_MEMORY;
|
|
owned = malloc(sizeof(*owned));
|
|
if (!owned)
|
|
return PORT_MEMORY;
|
|
*owned = parsed;
|
|
if (remaining > 0)
|
|
remaining--;
|
|
usage.programs++;
|
|
usage.terms += owned->count;
|
|
*out = owned;
|
|
return PORT_OK;
|
|
}
|
|
void port_release(compiled_rule *program)
|
|
{
|
|
if (!program)
|
|
return;
|
|
usage.programs--;
|
|
usage.terms -= program->count;
|
|
free(program);
|
|
}
|
|
static void apply_term(const term *item, const unsigned char *message, size_t length,
|
|
const unsigned char *before, unsigned char *after)
|
|
{
|
|
memset(after, 0, MESSAGE_LIMIT + 1);
|
|
for (size_t start = 0; start <= length; start++) {
|
|
if (!before[start])
|
|
continue;
|
|
if (!item->minimum)
|
|
after[start] = 1;
|
|
size_t stop = start;
|
|
while (stop < length && contains(item, message[stop])) {
|
|
stop++;
|
|
after[stop] = 1;
|
|
if (!item->repeated)
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
int port_matches(const compiled_rule *program, const unsigned char *message, size_t length)
|
|
{
|
|
unsigned char reachable[MESSAGE_LIMIT + 1];
|
|
unsigned char next[MESSAGE_LIMIT + 1];
|
|
if (!program || (!message && length) || length > MESSAGE_LIMIT)
|
|
return 0;
|
|
memset(reachable, 0, sizeof(reachable));
|
|
if (program->anchored_start)
|
|
reachable[0] = 1;
|
|
else
|
|
memset(reachable, 1, length + 1);
|
|
for (size_t i = 0; i < program->count; i++) {
|
|
apply_term(&program->sequence[i], message, length, reachable, next);
|
|
memcpy(reachable, next, sizeof(reachable));
|
|
}
|
|
if (program->anchored_end)
|
|
return reachable[length] != 0;
|
|
for (size_t i = 0; i <= length; i++)
|
|
if (reachable[i])
|
|
return 1;
|
|
return 0;
|
|
}
|