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

266 lines
6.6 KiB
C

#include "buffer.h"
#include <limits.h>
#include <string.h>
typedef struct {
unsigned char *bytes;
size_t used;
size_t room;
size_t maximum;
size_t fields;
int active;
fb_allocator allocator;
} format_state;
static format_state state;
static int ready(void)
{
return state.active ? FB_OK : FB_STATE;
}
static int valid_input(const unsigned char *data, size_t length)
{
return data != NULL || length == 0;
}
static int key_valid(const char *key)
{
size_t count = 0;
if (key == NULL || key[0] == '\0')
return 0;
while (key[count] != '\0') {
unsigned char c = (unsigned char)key[count];
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') || c == '_'))
return 0;
count++;
if (count > 24)
return 0;
}
return 1;
}
static int sum_length(size_t base, size_t extra, size_t *result)
{
if (extra > state.maximum || base > state.maximum - extra)
return FB_LIMIT;
*result = base + extra;
return FB_OK;
}
static int ensure(size_t logical)
{
size_t needed;
size_t next;
unsigned char *replacement;
if (logical == 0)
return FB_OK;
needed = logical + 1;
if (needed <= state.room)
return FB_OK;
next = needed;
replacement = state.allocator.resize(NULL, 0, next, state.allocator.context);
if (replacement != NULL && state.bytes != NULL) {
memcpy(replacement, state.bytes, state.used + 1);
state.allocator.release(state.bytes, state.room, state.allocator.context);
}
if (replacement == NULL)
return FB_MEMORY;
state.bytes = replacement;
state.room = next;
state.bytes[state.used] = 0;
return FB_OK;
}
static void commit_bytes(const unsigned char *data, size_t length)
{
if (length != 0)
memcpy(state.bytes + state.used, data, length);
state.used += length;
if (state.bytes != NULL)
state.bytes[state.used] = 0;
}
static size_t escaped_size(const unsigned char *data, size_t length)
{
size_t result = 0;
for (size_t i = 0; i < length; ++i) {
unsigned char c = data[i];
result += c >= 32 && c <= 126 && c != '\\' && c != '=' && c != ';' ? 1 : 4;
}
return result;
}
static void commit_escape(const unsigned char *data, size_t length)
{
static const unsigned char hex[] = "0123456789ABCDEF";
for (size_t i = 0; i < length; ++i) {
unsigned char c = data[i];
if (c >= 32 && c <= 126 && c != '\\' && c != '=' && c != ';') {
commit_bytes(&c, 1);
} else {
unsigned char encoded[4] = {'\\', 'x', hex[c >> 4], hex[c & 15]};
commit_bytes(encoded, 4);
}
}
}
int fb_open(size_t limit, fb_allocator allocator)
{
if (state.active)
return FB_STATE;
if (limit > 65535 || allocator.resize == NULL || allocator.release == NULL)
return FB_INVALID;
memset(&state, 0, sizeof state);
state.maximum = limit;
state.allocator = allocator;
state.active = 1;
return FB_OK;
}
int fb_reserve(size_t logical)
{
if (ready() != FB_OK)
return FB_STATE;
if (logical > state.maximum)
return FB_LIMIT;
return ensure(logical);
}
int fb_append(const unsigned char *data, size_t length)
{
size_t total;
int result;
if (ready() != FB_OK)
return FB_STATE;
if (!valid_input(data, length))
return FB_INVALID;
result = sum_length(state.used, length, &total);
if (result != FB_OK)
return result;
result = ensure(total);
if (result != FB_OK)
return result;
commit_bytes(data, length);
return FB_OK;
}
int fb_escaped(const unsigned char *data, size_t length)
{
size_t extra;
size_t total;
int result;
if (ready() != FB_OK)
return FB_STATE;
if (!valid_input(data, length))
return FB_INVALID;
if (length > 65535)
return FB_LIMIT;
extra = escaped_size(data, length);
result = sum_length(state.used, extra, &total);
if (result != FB_OK)
return result;
result = ensure(total);
if (result != FB_OK)
return result;
commit_escape(data, length);
return FB_OK;
}
int fb_field(const char *key, const unsigned char *data, size_t length)
{
size_t extra;
size_t total;
size_t key_length;
int result;
if (ready() != FB_OK)
return FB_STATE;
if (!key_valid(key) || !valid_input(data, length))
return FB_INVALID;
if (length > 65535)
return FB_LIMIT;
key_length = strlen(key);
extra = key_length + 2 + escaped_size(data, length);
result = sum_length(state.used, extra, &total);
if (result != FB_OK)
return result;
result = ensure(total);
if (result != FB_OK)
return result;
commit_bytes((const unsigned char *)key, key_length);
commit_bytes((const unsigned char *)"=", 1);
commit_escape(data, length);
commit_bytes((const unsigned char *)";", 1);
state.fields++;
return FB_OK;
}
int fb_integer(const char *key, int64_t value)
{
unsigned char reversed[20];
unsigned char decimal[21];
size_t count = 0;
size_t output = 0;
uint64_t magnitude;
if (ready() != FB_OK)
return FB_STATE;
if (!key_valid(key))
return FB_INVALID;
magnitude = value < 0 ? (uint64_t)(-(value + 1)) + 1 : (uint64_t)value;
do {
reversed[count++] = (unsigned char)('0' + magnitude % 10);
magnitude /= 10;
} while (magnitude != 0);
if (value < 0)
decimal[output++] = '-';
while (count != 0)
decimal[output++] = reversed[--count];
return fb_field(key, decimal, output);
}
int fb_reset(void)
{
if (ready() != FB_OK)
return FB_STATE;
state.used = 0;
state.fields = 0;
if (state.bytes != NULL)
state.bytes[0] = 0;
return FB_OK;
}
int fb_close(void)
{
if (state.bytes != NULL)
state.allocator.release(state.bytes, state.room, state.allocator.context);
memset(&state, 0, sizeof state);
return FB_OK;
}
fb_snapshot fb_view(void)
{
fb_snapshot view;
view.data = state.bytes;
view.length = state.used;
view.capacity = state.room;
view.limit = state.maximum;
view.fields = state.fields;
view.active = state.active;
return view;
}
int fb_copy(unsigned char *destination, size_t capacity, size_t *written)
{
if (ready() != FB_OK)
return FB_STATE;
if (written == NULL || destination == NULL)
return FB_INVALID;
if (capacity <= state.used)
return FB_LIMIT;
if (state.used != 0)
memcpy(destination, state.bytes, state.used);
destination[state.used] = 0;
*written = state.used;
return FB_OK;
}