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

195 lines
6.1 KiB
C

#include "layout.h"
#include "journal.h"
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int decimal(const char *text, size_t *value)
{
char *end;
unsigned long long parsed;
if (text == NULL || *text == '\0' || *text == '-') {
return 0;
}
errno = 0;
parsed = strtoull(text, &end, 10);
if (errno != 0 || *end != '\0' || parsed > SIZE_MAX) {
return 0;
}
*value = (size_t)parsed;
return 1;
}
static int nibble(char digit)
{
if (digit >= '0' && digit <= '9') return digit - '0';
if (digit >= 'a' && digit <= 'f') return digit - 'a' + 10;
if (digit >= 'A' && digit <= 'F') return digit - 'A' + 10;
return -1;
}
static int hex_bytes(const char *text, unsigned char *out, size_t *length)
{
size_t index;
size_t chars;
if (text == NULL) {
return 0;
}
if (strcmp(text, "-") == 0) {
*length = 0u;
return 1;
}
chars = strlen(text);
if (chars % 2u != 0u || chars / 2u > LAYOUT_MAX_ROW) {
return 0;
}
for (index = 0u; index < chars / 2u; index++) {
int high = nibble(text[2u * index]);
int low = nibble(text[2u * index + 1u]);
if (high < 0 || low < 0) {
return 0;
}
out[index] = (unsigned char)(high * 16 + low);
}
*length = chars / 2u;
return 1;
}
static void print_hex(const unsigned char *data, size_t length)
{
static const char digits[] = "0123456789abcdef";
size_t index;
if (length == 0u) {
putchar('-');
}
for (index = 0u; index < length; index++) {
putchar(digits[data[index] >> 4]);
putchar(digits[data[index] & 15]);
}
}
static int show(const LayoutBank *bank)
{
size_t index;
LayoutStats stats;
int status = layout_stats(bank, &stats);
if (status != LAYOUT_OK) {
return status;
}
printf("STATS %zu %zu %zu %" PRIu32 "\n", stats.occupied,
stats.total_bytes, stats.longest, stats.revision);
for (index = 0u; index < bank->count; index++) {
const unsigned char *data;
size_t length;
uint32_t hash;
uintptr_t start;
if (layout_view(bank, index, &data, &length) != LAYOUT_OK ||
layout_slot_hash(bank, index, &hash) != LAYOUT_OK) {
return LAYOUT_EINVAL;
}
start = (uintptr_t)data;
printf("ROW %zu %zu %zu %" PRIu32 " ", index,
(size_t)(start % bank->width), length, hash);
print_hex(data, length);
putchar('\n');
}
return LAYOUT_OK;
}
static size_t tokenize(char *line, char **parts, size_t capacity)
{
size_t count = 0u;
char *word = strtok(line, " \t\r\n");
while (word != NULL) {
if (count >= capacity) {
return capacity + 1u;
}
parts[count++] = word;
word = strtok(NULL, " \t\r\n");
}
return count;
}
static int execute(LayoutBank *bank, Journal *journal, char **part, size_t count)
{
size_t first = 0u;
size_t second = 0u;
unsigned char data[LAYOUT_MAX_ROW];
size_t length = 0u;
int status = LAYOUT_EINVAL;
if (count == 0u || part[0][0] == '#') {
return LAYOUT_OK;
}
if (strcmp(part[0], "I") == 0 && count == 3u &&
decimal(part[1], &first) && decimal(part[2], &second)) {
status = layout_init(bank, first, second);
if (status == LAYOUT_OK) journal_reset(journal);
} else if (strcmp(part[0], "P") == 0 && count == 3u &&
decimal(part[1], &first) && hex_bytes(part[2], data, &length)) {
status = journal_put(journal, first, data, length);
} else if (strcmp(part[0], "F") == 0 && count == 4u &&
decimal(part[1], &first) && decimal(part[2], &second) &&
second <= 255u && decimal(part[3], &length)) {
status = journal_fill(journal, first, (unsigned char)second, length);
} else if (strcmp(part[0], "C") == 0 && count == 3u &&
decimal(part[1], &first) && decimal(part[2], &second)) {
status = journal_copy(journal, first, second);
} else if (strcmp(part[0], "S") == 0 && count == 3u &&
decimal(part[1], &first) && decimal(part[2], &second)) {
status = journal_swap(journal, first, second);
} else if (strcmp(part[0], "D") == 0 && count == 2u &&
decimal(part[1], &first)) {
status = journal_clear(journal, first);
} else if (strcmp(part[0], "A") == 0 && count == 4u &&
decimal(part[1], &first) && decimal(part[2], &second) &&
hex_bytes(part[3], data, &length)) {
status = journal_patch(journal, first, second, data, length);
} else if (strcmp(part[0], "E") == 0 && count == 4u &&
decimal(part[1], &first) && decimal(part[2], &second) &&
hex_bytes(part[3], data, &length)) {
status = journal_insert(journal, first, second, data, length);
} else if (strcmp(part[0], "Q") == 0 && count == 4u &&
decimal(part[1], &first) && decimal(part[2], &second) &&
decimal(part[3], &length)) {
status = journal_erase(journal, first, second, length);
} else if (strcmp(part[0], "B") == 0 && count == 1u) {
status = journal_commit(bank, journal);
} else if (strcmp(part[0], "R") == 0 && count == 1u) {
journal_reset(journal);
status = LAYOUT_OK;
} else if (strcmp(part[0], "V") == 0 && count == 1u) {
status = show(bank);
}
printf("RESULT %d\n", status);
return status;
}
int main(void)
{
LayoutBank bank;
Journal journal;
char line[1024];
char *part[8];
size_t count;
if (layout_init(&bank, 64u, 4u) != LAYOUT_OK) {
return 2;
}
journal_reset(&journal);
while (fgets(line, sizeof(line), stdin) != NULL) {
if (strchr(line, '\n') == NULL && !feof(stdin)) {
fprintf(stderr, "input line too long\n");
return 2;
}
count = tokenize(line, part, 8u);
if (count > 8u) {
fprintf(stderr, "too many fields\n");
return 2;
}
execute(&bank, &journal, part, count);
}
return ferror(stdin) ? 2 : 0;
}