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>
265 lines
6.0 KiB
C
265 lines
6.0 KiB
C
#include "store.h"
|
|
#include <string.h>
|
|
|
|
/* Each useful payload has its own aligned static object. */
|
|
static _Alignas(32) unsigned char lane0[STORE_CAPACITY];
|
|
static _Alignas(32) unsigned char lane1[STORE_CAPACITY];
|
|
static _Alignas(32) unsigned char lane2[STORE_CAPACITY];
|
|
static _Alignas(32) unsigned char lane3[STORE_CAPACITY];
|
|
static unsigned char *const lanes[STORE_SLOTS] = {lane0, lane1, lane2, lane3};
|
|
static size_t used[STORE_SLOTS];
|
|
|
|
static int slot_ok(int slot)
|
|
{
|
|
return slot >= 0 && slot < STORE_SLOTS;
|
|
}
|
|
|
|
static unsigned char *slot_data(int slot)
|
|
{
|
|
return lanes[slot];
|
|
}
|
|
|
|
void store_reset(void)
|
|
{
|
|
int slot;
|
|
for (slot = 0; slot < STORE_SLOTS; ++slot) {
|
|
used[slot] = 0;
|
|
memset(slot_data(slot), 0, STORE_CAPACITY);
|
|
}
|
|
}
|
|
|
|
unsigned char *store_span(int slot)
|
|
{
|
|
if (!slot_ok(slot)) {
|
|
return NULL;
|
|
}
|
|
return slot_data(slot);
|
|
}
|
|
|
|
size_t store_length(int slot)
|
|
{
|
|
if (!slot_ok(slot)) {
|
|
return 0;
|
|
}
|
|
return used[slot];
|
|
}
|
|
|
|
int store_write(int slot, const unsigned char *data, size_t length)
|
|
{
|
|
unsigned char staging[STORE_CAPACITY];
|
|
if (!slot_ok(slot)) {
|
|
return STORE_BAD_SLOT;
|
|
}
|
|
if (length > STORE_CAPACITY) {
|
|
return STORE_TOO_LONG;
|
|
}
|
|
if (length && data == NULL) {
|
|
return STORE_BAD_RANGE;
|
|
}
|
|
if (length) {
|
|
memcpy(staging, data, length);
|
|
memcpy(slot_data(slot), staging, length);
|
|
}
|
|
used[slot] = length;
|
|
return STORE_OK;
|
|
}
|
|
|
|
int store_append(int slot, const unsigned char *data, size_t length)
|
|
{
|
|
unsigned char staging[STORE_CAPACITY];
|
|
size_t old;
|
|
if (!slot_ok(slot)) {
|
|
return STORE_BAD_SLOT;
|
|
}
|
|
old = used[slot];
|
|
if (length > STORE_CAPACITY - old) {
|
|
return STORE_TOO_LONG;
|
|
}
|
|
if (length && data == NULL) {
|
|
return STORE_BAD_RANGE;
|
|
}
|
|
if (length) {
|
|
memcpy(staging, data, length);
|
|
memcpy(slot_data(slot) + old, staging, length);
|
|
}
|
|
used[slot] = old + length;
|
|
return STORE_OK;
|
|
}
|
|
|
|
int store_patch(int slot, size_t offset, const unsigned char *data, size_t length)
|
|
{
|
|
unsigned char staging[STORE_CAPACITY];
|
|
if (!slot_ok(slot)) {
|
|
return STORE_BAD_SLOT;
|
|
}
|
|
if (offset > used[slot] || length > used[slot] - offset) {
|
|
return STORE_BAD_RANGE;
|
|
}
|
|
if (length && data == NULL) {
|
|
return STORE_BAD_RANGE;
|
|
}
|
|
if (length) {
|
|
memcpy(staging, data, length);
|
|
memcpy(slot_data(slot) + offset, staging, length);
|
|
}
|
|
return STORE_OK;
|
|
}
|
|
|
|
int store_fill(int slot, unsigned char value, size_t length)
|
|
{
|
|
if (!slot_ok(slot)) {
|
|
return STORE_BAD_SLOT;
|
|
}
|
|
if (length > STORE_CAPACITY) {
|
|
return STORE_TOO_LONG;
|
|
}
|
|
memset(slot_data(slot), value, length);
|
|
used[slot] = length;
|
|
return STORE_OK;
|
|
}
|
|
|
|
int store_copy(int destination, int source)
|
|
{
|
|
size_t length;
|
|
if (!slot_ok(destination) || !slot_ok(source)) {
|
|
return STORE_BAD_SLOT;
|
|
}
|
|
length = used[source];
|
|
if (destination != source && length) {
|
|
memcpy(slot_data(destination), slot_data(source), length);
|
|
}
|
|
used[destination] = length;
|
|
return STORE_OK;
|
|
}
|
|
|
|
int store_erase(int slot, size_t offset, size_t length)
|
|
{
|
|
unsigned char *data;
|
|
size_t tail;
|
|
if (!slot_ok(slot)) {
|
|
return STORE_BAD_SLOT;
|
|
}
|
|
if (offset > used[slot] || length > used[slot] - offset) {
|
|
return STORE_BAD_RANGE;
|
|
}
|
|
data = slot_data(slot);
|
|
tail = used[slot] - offset - length;
|
|
if (tail) {
|
|
memmove(data + offset, data + offset + length, tail);
|
|
}
|
|
used[slot] -= length;
|
|
return STORE_OK;
|
|
}
|
|
|
|
int store_reverse(int slot)
|
|
{
|
|
unsigned char *data;
|
|
size_t left, right;
|
|
if (!slot_ok(slot)) {
|
|
return STORE_BAD_SLOT;
|
|
}
|
|
data = slot_data(slot);
|
|
left = 0;
|
|
right = used[slot];
|
|
while (left < right) {
|
|
unsigned char temp;
|
|
--right;
|
|
if (left >= right) {
|
|
break;
|
|
}
|
|
temp = data[left];
|
|
data[left] = data[right];
|
|
data[right] = temp;
|
|
++left;
|
|
}
|
|
return STORE_OK;
|
|
}
|
|
|
|
int store_rotate(int slot, size_t distance)
|
|
{
|
|
unsigned char temp[STORE_CAPACITY];
|
|
unsigned char *data;
|
|
size_t length, index;
|
|
if (!slot_ok(slot)) {
|
|
return STORE_BAD_SLOT;
|
|
}
|
|
length = used[slot];
|
|
if (length == 0) {
|
|
return STORE_OK;
|
|
}
|
|
distance %= length;
|
|
data = slot_data(slot);
|
|
for (index = 0; index < length; ++index) {
|
|
temp[index] = data[(index + distance) % length];
|
|
}
|
|
memcpy(data, temp, length);
|
|
return STORE_OK;
|
|
}
|
|
|
|
int store_xor(int slot, unsigned char key)
|
|
{
|
|
unsigned char *data;
|
|
size_t index;
|
|
if (!slot_ok(slot)) {
|
|
return STORE_BAD_SLOT;
|
|
}
|
|
data = slot_data(slot);
|
|
for (index = 0; index < used[slot]; ++index) {
|
|
data[index] ^= key;
|
|
}
|
|
return STORE_OK;
|
|
}
|
|
|
|
int store_find(int slot, unsigned char value)
|
|
{
|
|
size_t index;
|
|
const unsigned char *data;
|
|
if (!slot_ok(slot)) {
|
|
return STORE_BAD_SLOT;
|
|
}
|
|
data = slot_data(slot);
|
|
for (index = 0; index < used[slot]; ++index) {
|
|
if (data[index] == value) {
|
|
return (int)index;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int store_compare(int left, int right)
|
|
{
|
|
const unsigned char *a, *b;
|
|
size_t index, common;
|
|
if (!slot_ok(left) || !slot_ok(right)) {
|
|
return STORE_BAD_SLOT;
|
|
}
|
|
a = slot_data(left);
|
|
b = slot_data(right);
|
|
common = used[left] < used[right] ? used[left] : used[right];
|
|
for (index = 0; index < common; ++index) {
|
|
if (a[index] != b[index]) {
|
|
return a[index] < b[index] ? -1 : 1;
|
|
}
|
|
}
|
|
if (used[left] == used[right]) {
|
|
return 0;
|
|
}
|
|
return used[left] < used[right] ? -1 : 1;
|
|
}
|
|
|
|
uint16_t store_checksum(int slot)
|
|
{
|
|
const unsigned char *data;
|
|
uint16_t sum = 0;
|
|
size_t index;
|
|
if (!slot_ok(slot)) {
|
|
return 0;
|
|
}
|
|
data = slot_data(slot);
|
|
for (index = 0; index < used[slot]; ++index) {
|
|
sum = (uint16_t)((sum << 5) | (sum >> 11));
|
|
sum = (uint16_t)(sum + data[index]);
|
|
}
|
|
return sum;
|
|
}
|