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>
226 lines
5.7 KiB
C
226 lines
5.7 KiB
C
#include "protocol.h"
|
|
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
static int separator(char character)
|
|
{
|
|
return character == ' ' || character == '\t';
|
|
}
|
|
|
|
static const char *skip_separator(const char *cursor)
|
|
{
|
|
while (separator(*cursor)) {
|
|
++cursor;
|
|
}
|
|
return cursor;
|
|
}
|
|
|
|
static const char *token_end(const char *cursor)
|
|
{
|
|
while (*cursor != '\0' && *cursor != '\n' && !separator(*cursor)) {
|
|
++cursor;
|
|
}
|
|
return cursor;
|
|
}
|
|
|
|
static int token_equals(const char *start, const char *end, const char *text)
|
|
{
|
|
size_t length = (size_t)(end - start);
|
|
return strlen(text) == length && memcmp(start, text, length) == 0;
|
|
}
|
|
|
|
int parse_decimal(const char *first, const char *last, size_t *value)
|
|
{
|
|
size_t result = 0;
|
|
const char *cursor = first;
|
|
if (cursor < last && *cursor == '+') {
|
|
++cursor;
|
|
}
|
|
if (cursor == last) {
|
|
return 0;
|
|
}
|
|
while (cursor < last) {
|
|
unsigned digit;
|
|
if (*cursor < '0' || *cursor > '9') {
|
|
return 0;
|
|
}
|
|
digit = (unsigned)(*cursor - '0');
|
|
if (result > (SIZE_MAX - digit) / 10) {
|
|
return 0;
|
|
}
|
|
result = result * 10 + digit;
|
|
++cursor;
|
|
}
|
|
*value = result;
|
|
return 1;
|
|
}
|
|
|
|
static int parse_field(const char **cursor, size_t *value)
|
|
{
|
|
const char *start = skip_separator(*cursor);
|
|
const char *end = token_end(start);
|
|
if (!parse_decimal(start, end, value)) {
|
|
return 0;
|
|
}
|
|
*cursor = end;
|
|
return 1;
|
|
}
|
|
|
|
static int at_end(const char *cursor)
|
|
{
|
|
cursor = skip_separator(cursor);
|
|
return *cursor == '\0' || (*cursor == '\n' && cursor[1] == '\0');
|
|
}
|
|
|
|
int parse_command(const char *line, Command *command)
|
|
{
|
|
const char *cursor = skip_separator(line);
|
|
const char *end = token_end(cursor);
|
|
size_t id;
|
|
size_t first = 0;
|
|
size_t second = 0;
|
|
CommandKind kind = COMMAND_INVALID;
|
|
memset(command, 0, sizeof(*command));
|
|
if (token_equals(cursor, end, "OPEN")) {
|
|
kind = COMMAND_OPEN;
|
|
} else if (token_equals(cursor, end, "PLAN")) {
|
|
kind = COMMAND_PLAN;
|
|
} else if (token_equals(cursor, end, "CHECK")) {
|
|
kind = COMMAND_CHECK;
|
|
} else if (token_equals(cursor, end, "DROP")) {
|
|
kind = COMMAND_DROP;
|
|
} else if (token_equals(cursor, end, "FILL")) {
|
|
kind = COMMAND_FILL;
|
|
} else if (token_equals(cursor, end, "PROBE")) {
|
|
kind = COMMAND_PROBE;
|
|
} else if (token_equals(cursor, end, "COPY")) {
|
|
kind = COMMAND_COPY;
|
|
} else if (token_equals(cursor, end, "RESET")) {
|
|
kind = COMMAND_RESET;
|
|
}
|
|
if (kind == COMMAND_INVALID) {
|
|
return 0;
|
|
}
|
|
cursor = end;
|
|
if (kind == COMMAND_RESET) {
|
|
if (!at_end(cursor)) {
|
|
return 0;
|
|
}
|
|
command->kind = kind;
|
|
return 1;
|
|
}
|
|
if (!parse_field(&cursor, &id) || id >= 8) {
|
|
return 0;
|
|
}
|
|
if (kind == COMMAND_OPEN) {
|
|
if (!parse_field(&cursor, &first) || first < 1 || first > 4096) {
|
|
return 0;
|
|
}
|
|
}
|
|
if (kind == COMMAND_PLAN) {
|
|
if (!parse_field(&cursor, &first) || first < 1 || first > 256) {
|
|
return 0;
|
|
}
|
|
if (!parse_field(&cursor, &second) || second > 4096) {
|
|
return 0;
|
|
}
|
|
}
|
|
if (kind == COMMAND_FILL) {
|
|
if (!parse_field(&cursor, &first) || first > 255) {
|
|
return 0;
|
|
}
|
|
}
|
|
if (kind == COMMAND_PROBE) {
|
|
if (!parse_field(&cursor, &first) || first > 4096) {
|
|
return 0;
|
|
}
|
|
}
|
|
if (kind == COMMAND_COPY) {
|
|
if (!parse_field(&cursor, &first) || first >= 8) {
|
|
return 0;
|
|
}
|
|
if (!parse_field(&cursor, &second) || second > 4096) {
|
|
return 0;
|
|
}
|
|
}
|
|
if (!at_end(cursor)) {
|
|
return 0;
|
|
}
|
|
command->kind = kind;
|
|
command->id = (unsigned)id;
|
|
command->first = first;
|
|
command->second = second;
|
|
return 1;
|
|
}
|
|
|
|
void reader_init(InputReader *reader, FILE *stream)
|
|
{
|
|
reader->stream = stream;
|
|
reader->lines = 0;
|
|
reader->exhausted = 0;
|
|
reader->bytes = 0;
|
|
reader->digest = 2166136261u;
|
|
}
|
|
|
|
static void record_byte(InputReader *reader, unsigned char byte)
|
|
{
|
|
++reader->bytes;
|
|
reader->digest ^= byte;
|
|
reader->digest *= 16777619u;
|
|
}
|
|
|
|
int reader_next(InputReader *reader, Command *command)
|
|
{
|
|
char *line = NULL;
|
|
size_t length = 0;
|
|
size_t capacity = 0;
|
|
int character;
|
|
int malformed = 0;
|
|
if (reader->exhausted || reader->lines >= MAX_COMMANDS) {
|
|
reader->exhausted = 1;
|
|
return 0;
|
|
}
|
|
while ((character = fgetc(reader->stream)) != EOF) {
|
|
char *grown;
|
|
if (length + 2 > capacity) {
|
|
size_t target = capacity == 0 ? 64 : capacity * 2;
|
|
if (target < capacity || target < length + 2) {
|
|
free(line);
|
|
command->kind = COMMAND_INVALID;
|
|
return 1;
|
|
}
|
|
grown = realloc(line, target);
|
|
if (grown == NULL) {
|
|
free(line);
|
|
command->kind = COMMAND_INVALID;
|
|
return 1;
|
|
}
|
|
line = grown;
|
|
capacity = target;
|
|
}
|
|
line[length++] = (char)character;
|
|
record_byte(reader, (unsigned char)character);
|
|
if (character != '\n' && character != '\t' &&
|
|
(character < 32 || character > 126)) {
|
|
malformed = 1;
|
|
}
|
|
if (character == '\n') {
|
|
break;
|
|
}
|
|
}
|
|
if (length == 0 && character == EOF) {
|
|
reader->exhausted = 1;
|
|
free(line);
|
|
return 0;
|
|
}
|
|
++reader->lines;
|
|
line[length] = '\0';
|
|
if (malformed || character != '\n' || !parse_command(line, command)) {
|
|
command->kind = COMMAND_INVALID;
|
|
}
|
|
free(line);
|
|
return 1;
|
|
}
|