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

74 lines
2.9 KiB
C

#include "message.h"
#include <string.h>
static int append(unsigned char *buffer, size_t *used, const unsigned char *part, size_t size)
{
if (size > MM_MESSAGE_LIMIT - *used) { return 0; }
if (size != 0) { memcpy(buffer + *used, part, size); }
*used += size;
return 1;
}
static size_t digits(unsigned char *buffer, int64_t number, unsigned radix)
{
unsigned char reverse[32];
const char table[] = "0123456789abcdef";
size_t n = 0, out = 0;
uint64_t value = number < 0 ? (uint64_t)(-(number + 1)) + 1 : (uint64_t)number;
do { reverse[n++] = (unsigned char)table[value % radix]; value /= radix; } while (value);
if (number < 0) { buffer[out++] = '-'; }
while (n) { buffer[out++] = reverse[--n]; }
return out;
}
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)
{
unsigned char buffer[MM_MESSAGE_LIMIT];
size_t used = 0, next = 0, i;
*message = NULL;
*message_length = 0;
if (length > MM_TEMPLATE_LIMIT || count > MM_ARGUMENT_LIMIT ||
(length && !format) || (count && !args)) { return MM_INVALID; }
for (i = 0; i < count; ++i) {
if (args[i].kind == MM_TEXT) {
if (args[i].text.length > MM_TEXT_LIMIT || (args[i].text.length && !args[i].text.bytes)) { return MM_INVALID; }
} else if (args[i].kind == MM_DECIMAL) {
if (args[i].number < -1000000 || args[i].number > 1000000) { return MM_INVALID; }
} else if (args[i].kind == MM_HEX) {
if (args[i].number < 0 || args[i].number > UINT32_MAX) { return MM_INVALID; }
} else { return MM_INVALID; }
}
for (i = 0; i < length; ++i) {
unsigned char number[32];
size_t size;
if (format[i] != '%') {
if (!append(buffer, &used, format + i, 1)) { return MM_INVALID; }
continue;
}
if (++i == length) { return MM_INVALID; }
if (format[i] == '%') {
if (!append(buffer, &used, format + i, 1)) { return MM_INVALID; }
continue;
}
if (next == count) { return MM_INVALID; }
const mm_arg *a = &args[next++];
if (format[i] == 's' && a->kind == MM_TEXT) {
if (!append(buffer, &used, a->text.bytes, a->text.length)) { return MM_INVALID; }
} else if ((format[i] == 'd' && a->kind == MM_DECIMAL) ||
(format[i] == 'x' && a->kind == MM_HEX)) {
size = digits(number, a->number, format[i] == 'd' ? 10 : 16);
if (!append(buffer, &used, number, size)) { return MM_INVALID; }
} else { return MM_INVALID; }
}
if (next != count) { return MM_INVALID; }
unsigned char *result = memory.acquire(memory.context, used + 1);
if (!result) { return MM_NOMEM; }
if (used) { memcpy(result, buffer, used); }
result[used] = 0;
*message = result;
*message_length = used;
return MM_OK;
}