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>
166 lines
4.4 KiB
C
166 lines
4.4 KiB
C
#include "adapter.h"
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
static int step_error(int value)
|
|
{
|
|
switch (value) {
|
|
case -1: return EINTR;
|
|
case -2: return EAGAIN;
|
|
case -3: return EPIPE;
|
|
default: return EIO;
|
|
}
|
|
}
|
|
static adapter_call *new_call(adapter *port, int kind, size_t request, int flags)
|
|
{
|
|
adapter_call *call;
|
|
if (port->call_count >= ADAPTER_CALLS) {
|
|
return NULL;
|
|
}
|
|
call = &port->calls[port->call_count++];
|
|
memset(call, 0, sizeof *call);
|
|
call->kind = kind;
|
|
call->requested = request;
|
|
call->flags = flags;
|
|
return call;
|
|
}
|
|
static ssize_t send_bytes(void *context, const unsigned char *bytes,
|
|
size_t count, int flags)
|
|
{
|
|
adapter *port = context;
|
|
adapter_call *call = new_call(port, 1, count, flags);
|
|
int step = (int)count;
|
|
size_t accepted;
|
|
if (port->send_next < port->send_count) {
|
|
step = port->sends[port->send_next++];
|
|
}
|
|
if (!call) {
|
|
errno = EIO;
|
|
return -1;
|
|
}
|
|
call->count = count;
|
|
memcpy(call->bytes, bytes, count);
|
|
if (step < 0) {
|
|
call->result = -1;
|
|
call->error = step_error(step);
|
|
errno = call->error;
|
|
return -1;
|
|
}
|
|
accepted = (size_t)step < count ? (size_t)step : count;
|
|
call->result = (ssize_t)accepted;
|
|
return call->result;
|
|
}
|
|
static ssize_t recv_bytes(void *context, unsigned char *bytes,
|
|
size_t count, int flags)
|
|
{
|
|
adapter *port = context;
|
|
adapter_call *call = new_call(port, 2, count, flags);
|
|
int step = -2;
|
|
size_t received;
|
|
size_t index;
|
|
if (port->recv_next < port->recv_count) {
|
|
step = port->receives[port->recv_next++];
|
|
}
|
|
if (!call) {
|
|
errno = EIO;
|
|
return -1;
|
|
}
|
|
if (step < 0) {
|
|
call->result = -1;
|
|
call->error = step_error(step);
|
|
errno = call->error;
|
|
return -1;
|
|
}
|
|
received = (size_t)step < count ? (size_t)step : count;
|
|
for (index = 0; index < received; ++index) {
|
|
bytes[index] = (unsigned char)((port->seed + 17 * port->cursor) & 255);
|
|
port->cursor += 1;
|
|
}
|
|
call->result = (ssize_t)received;
|
|
call->count = received;
|
|
memcpy(call->bytes, bytes, received);
|
|
return call->result;
|
|
}
|
|
static int shutdown_write(void *context)
|
|
{
|
|
adapter *port = context;
|
|
adapter_call *call = new_call(port, 3, 0, 0);
|
|
if (!call) {
|
|
errno = EIO;
|
|
return -1;
|
|
}
|
|
if (port->shutdown_error != 0) {
|
|
errno = port->shutdown_error;
|
|
call->error = errno;
|
|
call->result = -1;
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
void adapter_init(adapter *port, unsigned seed)
|
|
{
|
|
memset(port, 0, sizeof *port);
|
|
port->seed = seed;
|
|
}
|
|
stream_port adapter_port(adapter *port)
|
|
{
|
|
stream_port result;
|
|
result.context = port;
|
|
result.send_bytes = send_bytes;
|
|
result.recv_bytes = recv_bytes;
|
|
result.shutdown_write = shutdown_write;
|
|
return result;
|
|
}
|
|
int adapter_load(adapter *port, int receive, const int *steps, size_t count)
|
|
{
|
|
size_t index;
|
|
if (count > ADAPTER_STEPS) {
|
|
return STREAM_INVALID;
|
|
}
|
|
for (index = 0; index < count; ++index) {
|
|
if (steps[index] < -4 || steps[index] > STREAM_CAPACITY) {
|
|
return STREAM_INVALID;
|
|
}
|
|
}
|
|
if (receive) {
|
|
memcpy(port->receives, steps, count * sizeof *steps);
|
|
port->recv_count = count;
|
|
port->recv_next = 0;
|
|
} else {
|
|
memcpy(port->sends, steps, count * sizeof *steps);
|
|
port->send_count = count;
|
|
port->send_next = 0;
|
|
}
|
|
return STREAM_OK;
|
|
}
|
|
void adapter_observation_reset(adapter *port)
|
|
{
|
|
port->call_count = 0;
|
|
}
|
|
static void print_hex(const unsigned char *bytes, size_t count)
|
|
{
|
|
size_t index;
|
|
putchar('"');
|
|
for (index = 0; index < count; ++index) {
|
|
printf("%02x", bytes[index]);
|
|
}
|
|
putchar('"');
|
|
}
|
|
void adapter_print(const adapter *port)
|
|
{
|
|
size_t index;
|
|
printf("{\"next\":[%zu,%zu],\"cursor\":%llu,\"calls\":[",
|
|
port->send_next, port->recv_next, (unsigned long long)port->cursor);
|
|
for (index = 0; index < port->call_count; ++index) {
|
|
const adapter_call *call = &port->calls[index];
|
|
if (index != 0) {
|
|
putchar(',');
|
|
}
|
|
printf("[%d,%zu,%d,%zd,%d,", call->kind, call->requested,
|
|
call->flags, call->result, call->error);
|
|
print_hex(call->bytes, call->count);
|
|
putchar(']');
|
|
}
|
|
printf("]}");
|
|
}
|