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

265 lines
5.9 KiB
C

#include "store.h"
#include <string.h>
/* Four useful 128-byte spans are separated by 32-byte reserved gaps. */
enum { STORE_STRIDE = 160, STORE_ARENA = STORE_STRIDE * STORE_SLOTS };
static _Alignas(32) unsigned char arena[STORE_ARENA];
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 arena + (size_t)slot * STORE_STRIDE;
}
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 (slot != 0) {
return STORE_OK;
}
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;
}