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

83 lines
2.4 KiB
C

#include "wire.h"
#include <string.h>
uint16_t wire_crc16(const uint8_t *bytes, size_t length)
{
uint16_t crc = 0xffffu;
size_t index;
unsigned bit;
for (index = 0; index < length; ++index) {
crc ^= (uint16_t)bytes[index] << 8;
for (bit = 0; bit < 8; ++bit) {
if (crc & 0x8000u)
crc = (uint16_t)((crc << 1) ^ 0x1021u);
else
crc = (uint16_t)(crc << 1);
}
}
return crc;
}
static uint32_t read_tag(const uint8_t *bytes)
{
return (uint32_t)bytes[4] | ((uint32_t)bytes[5] << 8) |
((uint32_t)bytes[6] << 16) | ((uint32_t)bytes[7] << 24);
}
static void write_tag(uint8_t *bytes, uint32_t tag)
{
unsigned index;
for (index = 0; index < 4; ++index)
bytes[4 + index] = (uint8_t)(tag >> (8 * index));
}
int wire_decode(const uint8_t *bytes, size_t length, struct wire_packet *packet)
{
size_t payload_size;
uint16_t stored, computed;
if (bytes == NULL || packet == NULL || length < WIRE_OVERHEAD ||
length > WIRE_MAX_SIZE)
return -1;
if (bytes[0] != 0xc3u || bytes[1] != 0x5au || bytes[2] != 1u || bytes[3] > 3u)
return -1;
payload_size = bytes[8];
if (payload_size > WIRE_PAYLOAD_MAX || length != WIRE_OVERHEAD + payload_size)
return -1;
computed = wire_crc16(bytes, length - 2);
stored = (uint16_t)bytes[length - 2] | ((uint16_t)bytes[length - 1] << 8);
if (stored != computed)
return -1;
packet->tag = read_tag(bytes);
packet->flags = bytes[3];
packet->size = payload_size;
if (payload_size != 0)
memcpy(packet->payload, bytes + 9, payload_size);
return 0;
}
int wire_encode(const struct wire_packet *packet, uint8_t *bytes,
size_t capacity, size_t *written)
{
size_t total;
uint16_t crc;
if (packet == NULL || bytes == NULL || written == NULL ||
packet->size > WIRE_PAYLOAD_MAX || packet->flags > 3u)
return -1;
total = WIRE_OVERHEAD + packet->size;
if (capacity < total)
return -1;
bytes[0] = 0xc3u;
bytes[1] = 0x5au;
bytes[2] = 1;
bytes[3] = (uint8_t)packet->flags;
write_tag(bytes, packet->tag);
bytes[8] = (uint8_t)packet->size;
if (packet->size != 0)
memcpy(bytes + 9, packet->payload, packet->size);
crc = wire_crc16(bytes, total - 2);
bytes[total - 2] = (uint8_t)crc;
bytes[total - 1] = (uint8_t)(crc >> 8);
*written = total;
return 0;
}