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

201 lines
4.9 KiB
C

#include "rr.h"
#include <string.h>
static uint32_t checksum_extend(uint32_t current, const unsigned char *data,
size_t count)
{
size_t index;
for (index = 0; index < count; ++index) {
current ^= data[index];
current *= UINT32_C(16777619);
}
return current;
}
uint32_t rr_checksum(const unsigned char *data, size_t count)
{
if (data == NULL && count != 0) {
return 0;
}
return checksum_extend(UINT32_C(2166136261), data, count);
}
void rr_buffer_init(rr_buffer *buffer, size_t limit)
{
if (buffer == NULL) {
return;
}
buffer->limit = limit <= RR_DATA_SIZE ? limit : RR_DATA_SIZE;
buffer->size = 0;
buffer->checksum = UINT32_C(2166136261);
}
void rr_buffer_reset(rr_buffer *buffer)
{
if (buffer == NULL) {
return;
}
buffer->size = 0;
buffer->checksum = UINT32_C(2166136261);
}
int rr_buffer_append(rr_buffer *buffer, const unsigned char *data, size_t count)
{
uint32_t next;
if (buffer == NULL || (data == NULL && count != 0)) {
return -1;
}
if (buffer->limit > RR_DATA_SIZE || buffer->size > buffer->limit) {
return -1;
}
if (count > buffer->limit - buffer->size) {
return -1;
}
next = checksum_extend(buffer->checksum, data, count);
if (count != 0) {
memcpy(buffer->data + buffer->size, data, count);
}
buffer->size += count;
buffer->checksum = next;
return 0;
}
int rr_buffer_hex(const rr_buffer *buffer, char *text, size_t capacity)
{
static const char digits[] = "0123456789abcdef";
size_t index;
char *cursor = text;
if (buffer == NULL || text == NULL || buffer->size > RR_DATA_SIZE) {
return -1;
}
if (capacity < buffer->size * 2 + 1) {
return -1;
}
for (index = 0; index < buffer->size; ++index) {
unsigned char value = buffer->data[index];
*cursor++ = digits[value >> 4];
*cursor++ = digits[value & 15];
}
*cursor = '\0';
return 0;
}
void rr_result_init(rr_result *result, size_t limit)
{
if (result == NULL) {
return;
}
result->status = RR_UNAVAILABLE;
result->selected = -1;
result->attempts = 0;
result->opened = 0;
result->reads = 0;
result->closed = 0;
result->failures = 0;
rr_buffer_init(&result->content, limit);
}
const char *rr_status_name(rr_status status)
{
switch (status) {
case RR_OK:
return "ok";
case RR_UNAVAILABLE:
return "unavailable";
case RR_TOO_LARGE:
return "too-large";
case RR_BAD_PLAN:
return "bad-plan";
default:
return "unknown";
}
}
int rr_buffer_copy(rr_buffer *destination, const rr_buffer *source)
{
if (destination == NULL || source == NULL) {
return -1;
}
if (source->size > source->limit || source->limit > RR_DATA_SIZE) {
return -1;
}
if (destination == source) {
return 0;
}
rr_buffer_init(destination, source->limit);
return rr_buffer_append(destination, source->data, source->size);
}
int rr_buffer_slice(const rr_buffer *source, size_t offset, size_t count,
rr_buffer *destination)
{
if (source == NULL || destination == NULL || source == destination) {
return -1;
}
if (source->size > RR_DATA_SIZE || offset > source->size) {
return -1;
}
if (count > source->size - offset) {
return -1;
}
rr_buffer_init(destination, count);
return rr_buffer_append(destination, source->data + offset, count);
}
int rr_buffer_equal(const rr_buffer *left, const rr_buffer *right)
{
if (left == NULL || right == NULL || left->size != right->size) {
return 0;
}
if (left->size > RR_DATA_SIZE) {
return 0;
}
if (left->size == 0) {
return 1;
}
return memcmp(left->data, right->data, left->size) == 0;
}
static int hex_value(unsigned char value)
{
if (value >= '0' && value <= '9') {
return value - '0';
}
if (value >= 'a' && value <= 'f') {
return value - 'a' + 10;
}
if (value >= 'A' && value <= 'F') {
return value - 'A' + 10;
}
return -1;
}
int rr_buffer_from_hex(rr_buffer *buffer, const char *text, size_t limit)
{
rr_buffer parsed;
size_t cursor = 0;
if (buffer == NULL || text == NULL || limit > RR_DATA_SIZE) {
return -1;
}
rr_buffer_init(buffer, limit);
rr_buffer_init(&parsed, limit);
while (text[cursor] != '\0') {
int high = hex_value((unsigned char)text[cursor++]);
int low;
unsigned char byte;
if (high < 0 || text[cursor] == '\0') {
return -1;
}
low = hex_value((unsigned char)text[cursor++]);
if (low < 0) {
return -1;
}
byte = (unsigned char)((high << 4) | low);
if (rr_buffer_append(&parsed, &byte, 1) != 0) {
return -1;
}
}
return rr_buffer_copy(buffer, &parsed);
}