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>
221 lines
5.7 KiB
C
221 lines
5.7 KiB
C
#include "message.h"
|
|
#include <string.h>
|
|
|
|
typedef struct {
|
|
const unsigned char *format;
|
|
size_t length;
|
|
size_t position;
|
|
size_t argument;
|
|
} cursor;
|
|
|
|
typedef struct {
|
|
const unsigned char *bytes;
|
|
size_t length;
|
|
unsigned char local[32];
|
|
} piece;
|
|
|
|
static int valid_argument(const mm_arg *arg)
|
|
{
|
|
if (arg->kind == MM_TEXT) {
|
|
if (arg->text.length > MM_TEXT_LIMIT) {
|
|
return 0;
|
|
}
|
|
if (arg->text.length != 0 && arg->text.bytes == NULL) {
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
if (arg->kind == MM_DECIMAL) {
|
|
return arg->number >= -1000000 && arg->number <= 1000000;
|
|
}
|
|
if (arg->kind == MM_HEX) {
|
|
return arg->number >= 0 && arg->number <= UINT32_MAX;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static void number_piece(piece *out, uint64_t value, unsigned int base, int negative)
|
|
{
|
|
static const unsigned char digits[] = "0123456789abcdef";
|
|
unsigned char reverse[32];
|
|
size_t used = 0;
|
|
size_t index = 0;
|
|
do {
|
|
reverse[used] = digits[value % base];
|
|
used += 1;
|
|
value /= base;
|
|
} while (value != 0);
|
|
if (negative) {
|
|
out->local[index] = '-';
|
|
index += 1;
|
|
}
|
|
while (used != 0) {
|
|
used -= 1;
|
|
out->local[index] = reverse[used];
|
|
index += 1;
|
|
}
|
|
out->bytes = out->local;
|
|
out->length = index;
|
|
}
|
|
|
|
static int argument_piece(const mm_arg *arg, unsigned char conversion, piece *out)
|
|
{
|
|
uint64_t magnitude;
|
|
if (conversion == 's') {
|
|
if (arg->kind != MM_TEXT) {
|
|
return MM_INVALID;
|
|
}
|
|
out->bytes = arg->text.bytes;
|
|
out->length = arg->text.length;
|
|
return MM_OK;
|
|
}
|
|
if (conversion == 'd') {
|
|
if (arg->kind != MM_DECIMAL) {
|
|
return MM_INVALID;
|
|
}
|
|
if (arg->number < 0) {
|
|
magnitude = (uint64_t)(-(arg->number + 1)) + 1;
|
|
} else {
|
|
magnitude = (uint64_t)arg->number;
|
|
}
|
|
number_piece(out, magnitude, 10, arg->number < 0);
|
|
return MM_OK;
|
|
}
|
|
if (conversion == 'x') {
|
|
if (arg->kind != MM_HEX) {
|
|
return MM_INVALID;
|
|
}
|
|
number_piece(out, (uint64_t)arg->number, 16, 0);
|
|
return MM_OK;
|
|
}
|
|
return MM_INVALID;
|
|
}
|
|
|
|
static void cursor_init(cursor *scan, const unsigned char *format, size_t length)
|
|
{
|
|
scan->format = format;
|
|
scan->length = length;
|
|
scan->position = 0;
|
|
scan->argument = 0;
|
|
}
|
|
|
|
static int next_piece(cursor *scan, const mm_arg *args, size_t count, piece *out)
|
|
{
|
|
size_t start;
|
|
unsigned char conversion;
|
|
const mm_arg *arg;
|
|
if (scan->position == scan->length) {
|
|
return 0;
|
|
}
|
|
if (scan->format[scan->position] != '%') {
|
|
start = scan->position;
|
|
do {
|
|
scan->position += 1;
|
|
} while (scan->position < scan->length && scan->format[scan->position] != '%');
|
|
out->bytes = scan->format + start;
|
|
out->length = scan->position - start;
|
|
return 1;
|
|
}
|
|
scan->position += 1;
|
|
if (scan->position == scan->length) {
|
|
return MM_INVALID;
|
|
}
|
|
conversion = scan->format[scan->position];
|
|
scan->position += 1;
|
|
if (conversion == '%') {
|
|
out->local[0] = '%';
|
|
out->bytes = out->local;
|
|
out->length = 1;
|
|
return 1;
|
|
}
|
|
if (scan->argument == count) {
|
|
return MM_INVALID;
|
|
}
|
|
arg = &args[scan->argument];
|
|
scan->argument += 1;
|
|
if (argument_piece(arg, conversion, out) != MM_OK) {
|
|
return MM_INVALID;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static int measure(const unsigned char *format, size_t length,
|
|
const mm_arg *args, size_t count, size_t *total)
|
|
{
|
|
cursor scan;
|
|
piece item;
|
|
int next;
|
|
size_t size = 0;
|
|
cursor_init(&scan, format, length);
|
|
for (;;) {
|
|
next = next_piece(&scan, args, count, &item);
|
|
if (next == 0) {
|
|
break;
|
|
}
|
|
if (next < 0) {
|
|
return MM_INVALID;
|
|
}
|
|
if (item.length > MM_MESSAGE_LIMIT - size) {
|
|
return MM_INVALID;
|
|
}
|
|
size += item.length;
|
|
}
|
|
if (scan.argument != count) {
|
|
return MM_INVALID;
|
|
}
|
|
*total = size;
|
|
return MM_OK;
|
|
}
|
|
|
|
static void fill(unsigned char *destination, const unsigned char *format,
|
|
size_t length, const mm_arg *args, size_t count)
|
|
{
|
|
cursor scan;
|
|
piece item;
|
|
size_t position = 0;
|
|
cursor_init(&scan, format, length);
|
|
while (next_piece(&scan, args, count, &item) > 0) {
|
|
if (item.length != 0) {
|
|
memcpy(destination + position, item.bytes, item.length);
|
|
}
|
|
position += item.length;
|
|
}
|
|
destination[position] = 0;
|
|
}
|
|
|
|
int mm_render(const unsigned char *format, size_t length,
|
|
const mm_arg *args, size_t count, mm_memory memory,
|
|
unsigned char **message, size_t *message_length)
|
|
{
|
|
size_t index;
|
|
size_t total;
|
|
unsigned char *result;
|
|
if (length != 13 || memcmp(format, "%s reading=%d", 13) != 0) {
|
|
*message = NULL; *message_length = 0; return MM_INVALID;
|
|
}
|
|
*message = NULL;
|
|
*message_length = 0;
|
|
if (length > MM_TEMPLATE_LIMIT || count > MM_ARGUMENT_LIMIT) {
|
|
return MM_INVALID;
|
|
}
|
|
if ((length != 0 && format == NULL) || (count != 0 && args == NULL)) {
|
|
return MM_INVALID;
|
|
}
|
|
for (index = 0; index < count; ++index) {
|
|
if (!valid_argument(&args[index])) {
|
|
return MM_INVALID;
|
|
}
|
|
}
|
|
if (measure(format, length, args, count, &total) != MM_OK) {
|
|
return MM_INVALID;
|
|
}
|
|
result = memory.acquire(memory.context, total + 1);
|
|
if (result == NULL) {
|
|
return MM_NOMEM;
|
|
}
|
|
fill(result, format, length, args, count);
|
|
*message = result;
|
|
*message_length = total;
|
|
return MM_OK;
|
|
}
|