From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27, 11 canonical families), 38 skipped for scope or duplication. Each task has at least one independent contract-edge defect, boundary types rotate, and public docs state trap clauses as plain contract prose. Claude reviewed 63/63 USABLE against current digests. Difficulty is unmeasured. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
112 lines
2.9 KiB
C
112 lines
2.9 KiB
C
#include "descriptor.h"
|
|
#include <errno.h>
|
|
#include <limits.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
int dr_parse_i32(const char *text, int *value)
|
|
{
|
|
if (text == NULL || value == NULL || text[0] == '\0') {
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
size_t offset = 0;
|
|
int negative = 0;
|
|
if (text[offset] == '-' || text[offset] == '+') {
|
|
negative = text[offset] == '-';
|
|
++offset;
|
|
}
|
|
if (text[offset] == '\0') {
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
unsigned long limit = negative ? (unsigned long)INT_MAX + 1UL : INT_MAX;
|
|
unsigned long amount = 0;
|
|
for (; text[offset] != '\0'; ++offset) {
|
|
unsigned char c = (unsigned char)text[offset];
|
|
if (c < '0' || c > '9') {
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
unsigned long digit = (unsigned long)(c - '0');
|
|
if (amount > (limit - digit) / 10UL) {
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
amount = amount * 10UL + digit;
|
|
}
|
|
int next;
|
|
if (negative && amount == (unsigned long)INT_MAX + 1UL) {
|
|
next = INT_MIN;
|
|
} else {
|
|
next = negative ? -(int)amount : (int)amount;
|
|
}
|
|
*value = next;
|
|
return 0;
|
|
}
|
|
|
|
static int nibble(unsigned char c)
|
|
{
|
|
if (c >= '0' && c <= '9') {
|
|
return c - '0';
|
|
}
|
|
if (c >= 'a' && c <= 'f') {
|
|
return c - 'a' + 10;
|
|
}
|
|
if (c >= 'A' && c <= 'F') {
|
|
return c - 'A' + 10;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int dr_hex_decode(const char *text, void *bytes, size_t capacity,
|
|
size_t *length)
|
|
{
|
|
if (text == NULL || length == NULL) {
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
size_t count = strcmp(text, "-") == 0 ? 0 : strlen(text);
|
|
if (count == 0 && text[0] != '-') {
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
if (count % 2 != 0 || count / 2 > capacity ||
|
|
(count > 0 && bytes == NULL)) {
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
for (size_t i = 0; i < count; ++i) {
|
|
if (nibble((unsigned char)text[i]) < 0) {
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
}
|
|
unsigned char *output = bytes;
|
|
for (size_t i = 0; i < count / 2; ++i) {
|
|
int high = nibble((unsigned char)text[2 * i]);
|
|
int low = nibble((unsigned char)text[2 * i + 1]);
|
|
output[i] = (unsigned char)((high << 4) | low);
|
|
}
|
|
*length = count / 2;
|
|
return 0;
|
|
}
|
|
|
|
int dr_hex_encode(const void *bytes, size_t length, char *text,
|
|
size_t capacity)
|
|
{
|
|
if (text == NULL || (length > 0 && bytes == NULL) ||
|
|
length > (SIZE_MAX - 1) / 2 || capacity < length * 2 + 1) {
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
const unsigned char *source = bytes;
|
|
const char digits[] = "0123456789abcdef";
|
|
for (size_t i = 0; i < length; ++i) {
|
|
text[2 * i] = digits[source[i] >> 4];
|
|
text[2 * i + 1] = digits[source[i] & 15];
|
|
}
|
|
text[2 * length] = '\0';
|
|
return 0;
|
|
}
|