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

162 lines
4.2 KiB
C

#include "wire.h"
#include "store.h"
#include <string.h>
static uint16_t crc_step(uint16_t crc, unsigned char byte)
{
unsigned int bit;
crc ^= (uint16_t)byte << 8;
for (bit = 0; bit < 8; ++bit) {
if (crc & 0x8000u) {
crc = (uint16_t)((crc << 1) ^ 0x1021u);
} else {
crc = (uint16_t)(crc << 1);
}
}
return crc;
}
uint16_t wire_crc(const unsigned char *data, size_t length)
{
uint16_t crc = 0xffffu;
size_t index;
if (length && data == NULL) {
return 0;
}
for (index = 0; index < length; ++index) {
crc = crc_step(crc, data[index]);
}
return crc;
}
static int frame_shape(const unsigned char *frame, size_t length,
int *slot, size_t *payload_length)
{
size_t claimed;
if (frame == NULL || slot == NULL || payload_length == NULL) {
return WIRE_BAD_ARGUMENT;
}
if (length < 5) {
return WIRE_SHORT;
}
if (frame[0] != WIRE_MAGIC || frame[1] >= STORE_SLOTS) {
return WIRE_FORMAT;
}
claimed = frame[2];
if (claimed > STORE_CAPACITY || length != claimed + 5) {
return WIRE_FORMAT;
}
*slot = frame[1];
*payload_length = claimed;
return WIRE_OK;
}
int wire_peek(const unsigned char *frame, size_t length,
int *slot, size_t *payload_length)
{
int found_slot;
size_t found_length;
int result = frame_shape(frame, length, &found_slot, &found_length);
if (result != WIRE_OK) {
return result;
}
if (slot == NULL || payload_length == NULL) {
return WIRE_BAD_ARGUMENT;
}
*slot = found_slot;
*payload_length = found_length;
return WIRE_OK;
}
int wire_pack(int slot, unsigned char *output, size_t capacity, size_t *written)
{
unsigned char frame[WIRE_MAX];
unsigned char *data;
size_t length, total;
uint16_t crc;
if (written == NULL || output == NULL) {
return WIRE_BAD_ARGUMENT;
}
*written = 0;
data = store_span(slot);
if (data == NULL) {
return WIRE_BAD_ARGUMENT;
}
length = store_length(slot);
total = length + 5;
if (capacity < total) {
return WIRE_CAPACITY;
}
frame[0] = WIRE_MAGIC;
frame[1] = (unsigned char)slot;
frame[2] = (unsigned char)length;
if (length) {
memcpy(frame + 3, data, length);
}
crc = wire_crc(frame, length + 3);
frame[length + 3] = (unsigned char)(crc >> 8);
frame[length + 4] = (unsigned char)crc;
memcpy(output, frame, total);
*written = total;
return WIRE_OK;
}
int wire_unpack(const unsigned char *frame, size_t length, int *slot)
{
int found_slot;
size_t payload_length;
uint16_t expected, observed;
int result;
if (slot == NULL) {
return WIRE_BAD_ARGUMENT;
}
result = frame_shape(frame, length, &found_slot, &payload_length);
if (result != WIRE_OK) {
return result;
}
expected = wire_crc(frame, payload_length + 3);
observed = (uint16_t)(((uint16_t)frame[payload_length + 3] << 8)
| frame[payload_length + 4]);
if (expected != observed) {
return WIRE_CHECKSUM;
}
result = store_write(found_slot, frame + 3, payload_length);
if (result != STORE_OK) {
return WIRE_FORMAT;
}
*slot = found_slot;
return WIRE_OK;
}
int wire_payload_copy(const unsigned char *frame, size_t length,
unsigned char *output, size_t capacity, size_t *written)
{
int found_slot;
size_t payload_length;
uint16_t expected, observed;
int result;
if (output == NULL || written == NULL) {
return WIRE_BAD_ARGUMENT;
}
*written = 0;
result = frame_shape(frame, length, &found_slot, &payload_length);
if (result != WIRE_OK) {
return result;
}
expected = wire_crc(frame, payload_length + 3);
observed = (uint16_t)(((uint16_t)frame[payload_length + 3] << 8)
| frame[payload_length + 4]);
if (expected != observed) {
return WIRE_CHECKSUM;
}
if (capacity < payload_length) {
return WIRE_CAPACITY;
}
if (payload_length) {
memmove(output, frame + 3, payload_length);
}
*written = payload_length;
(void)found_slot;
return WIRE_OK;
}