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>
176 lines
4.8 KiB
C
176 lines
4.8 KiB
C
#include "stream.h"
|
|
#include <limits.h>
|
|
#include <string.h>
|
|
static void put16(unsigned char *out, unsigned value)
|
|
{
|
|
out[0] = (unsigned char)value;
|
|
out[1] = (unsigned char)(value >> 8);
|
|
}
|
|
static void put32(unsigned char *out, uint32_t value)
|
|
{
|
|
out[0] = (unsigned char)value;
|
|
out[1] = (unsigned char)(value >> 8);
|
|
out[2] = (unsigned char)(value >> 16);
|
|
out[3] = (unsigned char)(value >> 24);
|
|
}
|
|
static unsigned get16(const unsigned char *in)
|
|
{
|
|
unsigned low = in[0];
|
|
unsigned high = in[1];
|
|
return low | (high << 8);
|
|
}
|
|
static uint32_t get32(const unsigned char *in)
|
|
{
|
|
uint32_t a = in[0];
|
|
uint32_t b = in[1];
|
|
uint32_t c = in[2];
|
|
uint32_t d = in[3];
|
|
return a | (b << 8) | (c << 16) | (d << 24);
|
|
}
|
|
static int32_t signed32(uint32_t value)
|
|
{
|
|
if (value <= INT32_MAX)
|
|
return (int32_t)value;
|
|
return -1 - (int32_t)(UINT32_MAX - value);
|
|
}
|
|
uint32_t ts_crc(const unsigned char *data, size_t length)
|
|
{
|
|
uint32_t crc = UINT32_MAX;
|
|
for (size_t i = 0; i < length; ++i) {
|
|
crc ^= data[i];
|
|
for (unsigned bit = 0; bit < 8; ++bit) {
|
|
uint32_t low = crc & 1u;
|
|
crc >>= 1;
|
|
if (low != 0)
|
|
crc ^= UINT32_C(0xedb88320);
|
|
}
|
|
}
|
|
return crc ^ UINT32_MAX;
|
|
}
|
|
int ts_record_valid(const ts_record *row)
|
|
{
|
|
if (row == NULL)
|
|
return 0;
|
|
if (row->sequence > 1000000u)
|
|
return 0;
|
|
if (row->value < -1000000 || row->value > 1000000)
|
|
return 0;
|
|
if (row->channel > 15u)
|
|
return 0;
|
|
if (row->flags > 255u)
|
|
return 0;
|
|
return 1;
|
|
}
|
|
int ts_encode_record(const ts_record *row, unsigned char *out, size_t capacity)
|
|
{
|
|
if (!ts_record_valid(row) || out == NULL)
|
|
return 0;
|
|
if (capacity < TS_FRAME)
|
|
return 0;
|
|
memset(out, 0, TS_FRAME);
|
|
out[0] = 'R';
|
|
out[1] = 'C';
|
|
out[2] = 1;
|
|
out[3] = TS_FRAME;
|
|
put32(out + 4, row->sequence);
|
|
put32(out + 8, (uint32_t)row->value);
|
|
put16(out + 12, row->channel);
|
|
put16(out + 14, row->flags);
|
|
put32(out + 16, row->sequence ^ UINT32_C(0xa57c1903));
|
|
put32(out + 20, ts_crc(out, 20));
|
|
return 1;
|
|
}
|
|
int ts_decode_record(const unsigned char *in, size_t length, ts_record *row)
|
|
{
|
|
ts_record decoded;
|
|
if (in == NULL || row == NULL || length != TS_FRAME)
|
|
return 0;
|
|
if (in[0] != 'R' || in[1] != 'C')
|
|
return 0;
|
|
if (in[2] != 1 || in[3] != TS_FRAME)
|
|
return 0;
|
|
if (get32(in + 20) != ts_crc(in, 20))
|
|
return 0;
|
|
decoded.sequence = get32(in + 4);
|
|
decoded.value = signed32(get32(in + 8));
|
|
decoded.channel = get16(in + 12);
|
|
decoded.flags = get16(in + 14);
|
|
if (get32(in + 16) != (decoded.sequence ^ UINT32_C(0xa57c1903)))
|
|
return 0;
|
|
if (!ts_record_valid(&decoded))
|
|
return 0;
|
|
*row = decoded;
|
|
return 1;
|
|
}
|
|
int ts_encode_header(size_t count, unsigned char *out, size_t capacity)
|
|
{
|
|
if (out == NULL || capacity < TS_HEADER || count > TS_LIMIT)
|
|
return 0;
|
|
memset(out, 0, TS_HEADER);
|
|
out[0] = 'T';
|
|
out[1] = 'S';
|
|
out[2] = 'P';
|
|
out[3] = 1;
|
|
put32(out + 4, (uint32_t)count);
|
|
put32(out + 8, TS_FRAME);
|
|
put32(out + 12, ts_crc(out, 12));
|
|
return 1;
|
|
}
|
|
int ts_decode_header(const unsigned char *in, size_t length, size_t *count)
|
|
{
|
|
uint32_t rows;
|
|
if (in == NULL || count == NULL || length != TS_HEADER)
|
|
return 0;
|
|
if (in[0] != 'T' || in[1] != 'S' || in[2] != 'P' || in[3] != 1)
|
|
return 0;
|
|
if (get32(in + 8) != TS_FRAME)
|
|
return 0;
|
|
if (get32(in + 12) != ts_crc(in, 12))
|
|
return 0;
|
|
rows = get32(in + 4);
|
|
if (rows > TS_LIMIT)
|
|
return 0;
|
|
*count = rows;
|
|
return 1;
|
|
}
|
|
int ts_encode_trailer(size_t count, uint32_t digest, unsigned char *out, size_t capacity)
|
|
{
|
|
if (out == NULL || capacity < TS_TRAILER || count > TS_LIMIT)
|
|
return 0;
|
|
out[0] = 'E';
|
|
out[1] = 'N';
|
|
out[2] = 'D';
|
|
out[3] = 1;
|
|
put32(out + 4, (uint32_t)count);
|
|
put32(out + 8, digest);
|
|
put32(out + 12, ts_crc(out, 12));
|
|
return 1;
|
|
}
|
|
int ts_decode_trailer(const unsigned char *in, size_t length, size_t *count, uint32_t *digest)
|
|
{
|
|
uint32_t rows;
|
|
if (in == NULL || count == NULL || digest == NULL || length != TS_TRAILER)
|
|
return 0;
|
|
if (in[0] != 'E' || in[1] != 'N' || in[2] != 'D' || in[3] != 1)
|
|
return 0;
|
|
if (get32(in + 12) != ts_crc(in, 12))
|
|
return 0;
|
|
rows = get32(in + 4);
|
|
if (rows > TS_LIMIT)
|
|
return 0;
|
|
*count = rows;
|
|
*digest = get32(in + 8);
|
|
return 1;
|
|
}
|
|
uint32_t ts_record_digest(uint32_t previous, const ts_record *row)
|
|
{
|
|
unsigned char wire[TS_FRAME];
|
|
if (!ts_encode_record(row, wire, sizeof(wire)))
|
|
return previous;
|
|
for (size_t i = 0; i < sizeof(wire); ++i) {
|
|
previous ^= wire[i];
|
|
previous *= UINT32_C(16777619);
|
|
}
|
|
return previous;
|
|
}
|