318 lines
7.7 KiB
C

#include "wire.h"
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
void wire_reader_init(WireReader *reader, FILE *input)
{
memset(reader, 0, sizeof(*reader));
reader->input = input;
}
static int finish_line(WireReader *reader)
{
size_t length = strlen(reader->line);
if (length && reader->line[length - 1] == '\n') {
reader->line[--length] = '\0';
} else if (!feof(reader->input)) {
int value;
do {
value = fgetc(reader->input);
} while (value != '\n' && value != EOF);
reader->failed = 1;
return 0;
}
if (length && reader->line[length - 1] == '\r') {
reader->line[--length] = '\0';
}
return 1;
}
static int split_line(char *line, Command *command)
{
const unsigned char *cursor = (const unsigned char *)line;
memset(command, 0, sizeof(*command));
while (*cursor && isspace(*cursor)) {
cursor++;
}
if (*cursor == '\0' || *cursor == '#') {
return 0;
}
command->name = (char)*cursor++;
if (*cursor && !isspace(*cursor)) {
return -1;
}
for (;;) {
size_t length = 0;
while (*cursor && isspace(*cursor)) {
cursor++;
}
if (*cursor == '\0' || *cursor == '#') {
return 1;
}
if (command->count == WIRE_ARGS) {
return -1;
}
while (*cursor && !isspace(*cursor)) {
if (length + 1 == WIRE_TOKEN) {
return -1;
}
command->arg[command->count][length++] = (char)*cursor++;
}
command->arg[command->count][length] = '\0';
command->count++;
}
}
int wire_next(WireReader *reader, Command *command)
{
while (fgets(reader->line, sizeof(reader->line), reader->input)) {
int parsed;
reader->physical++;
if (!finish_line(reader)) {
return -1;
}
parsed = split_line(reader->line, command);
if (parsed < 0) {
reader->failed = 1;
return -1;
}
if (parsed > 0) {
reader->commands++;
return 1;
}
}
if (ferror(reader->input)) {
reader->failed = 1;
return -1;
}
return 0;
}
static int decimal_value(const char *text, int64_t *result)
{
size_t index = 0;
int negative = 0;
uint64_t magnitude = 0;
uint64_t limit;
if (text[index] == '+' || text[index] == '-') {
negative = text[index] == '-';
index++;
}
if (text[index] == '\0') {
return 0;
}
limit = negative ? (uint64_t)INT64_MAX + 1 : (uint64_t)INT64_MAX;
for (; text[index] != '\0'; index++) {
unsigned char character = (unsigned char)text[index];
unsigned digit;
if (character < '0' || character > '9') {
return 0;
}
digit = character - '0';
if (magnitude > (limit - digit) / 10) {
return 0;
}
magnitude = magnitude * 10 + digit;
}
if (negative && magnitude == (uint64_t)INT64_MAX + 1) {
*result = INT64_MIN;
} else if (negative) {
*result = -(int64_t)magnitude;
} else {
*result = (int64_t)magnitude;
}
return 1;
}
int wire_integer(const Command *command, size_t index, int64_t *value)
{
if (index >= command->count) {
return 0;
}
return decimal_value(command->arg[index], value);
}
int64_t wire_number(const Command *command, size_t index)
{
int64_t value = 0;
(void)wire_integer(command, index, &value);
return value;
}
static int hex_digit(unsigned char value)
{
if (value >= '0' && value <= '9') {
return value - '0';
}
if (value >= 'a' && value <= 'f') {
return value - 'a' + 10;
}
if (value >= 'A' && value <= 'F') {
return value - 'A' + 10;
}
return -1;
}
int wire_hex(const char *text, unsigned char *output, size_t capacity, size_t *length)
{
size_t source = strlen(text);
size_t index;
if (strcmp(text, "-") == 0) {
*length = 0;
return 1;
}
if (source % 2 || source / 2 > capacity) {
return 0;
}
for (index = 0; index < source / 2; index++) {
int high = hex_digit((unsigned char)text[index * 2]);
int low = hex_digit((unsigned char)text[index * 2 + 1]);
if (high < 0 || low < 0) {
return 0;
}
output[index] = (unsigned char)(high * 16 + low);
}
*length = source / 2;
return 1;
}
void observe_begin(Observation *observation)
{
memset(observation, 0, sizeof(*observation));
}
void observe_event(Observation *observation, const char *name, int64_t a, int64_t b, int64_t c)
{
Event *event;
if (observation->events == WIRE_EVENTS) {
observation->overflow = 1;
return;
}
event = &observation->event[observation->events++];
event->name = name;
event->a = a;
event->b = b;
event->c = c;
}
void observe_value(Observation *observation, int64_t value)
{
if (observation->states == WIRE_STATE) {
observation->overflow = 1;
return;
}
observation->state[observation->states++] = value;
}
static int write_unsigned(FILE *output, uint64_t value)
{
char digits[32];
size_t count = 0;
do {
unsigned digit = (unsigned)(value % 10);
digits[count++] = (char)('0' + digit);
value /= 10;
} while (value != 0);
while (count > 0) {
count--;
if (fputc(digits[count], output) == EOF) {
return 0;
}
}
return 1;
}
static int write_signed(FILE *output, int64_t value)
{
uint64_t magnitude;
if (value < 0) {
if (fputc('-', output) == EOF) {
return 0;
}
magnitude = (uint64_t)(-(value + 1)) + 1;
} else {
magnitude = (uint64_t)value;
}
return write_unsigned(output, magnitude);
}
static int write_field(FILE *output, int64_t value)
{
if (fputc(' ', output) == EOF) {
return 0;
}
return write_signed(output, value);
}
static int write_prefix(FILE *output, char kind, unsigned long sequence)
{
if (fputc(kind, output) == EOF) {
return 0;
}
if (fputc(' ', output) == EOF) {
return 0;
}
return write_unsigned(output, sequence);
}
static int write_event(FILE *output, unsigned long sequence, const Event *event)
{
if (!write_prefix(output, 'E', sequence)) {
return 0;
}
if (fputc(' ', output) == EOF || fputs(event->name, output) == EOF) {
return 0;
}
if (!write_field(output, event->a)) {
return 0;
}
if (!write_field(output, event->b)) {
return 0;
}
if (!write_field(output, event->c)) {
return 0;
}
return fputc('\n', output) != EOF;
}
int observe_write(FILE *output, unsigned long sequence, const Observation *observation)
{
size_t index;
if (observation->overflow) {
return 0;
}
for (index = 0; index < observation->events; index++) {
if (!write_event(output, sequence, &observation->event[index])) {
return 0;
}
}
if (!write_prefix(output, 'S', sequence)) {
return 0;
}
for (index = 0; index < observation->states; index++) {
if (!write_field(output, observation->state[index])) {
return 0;
}
}
return fputc('\n', output) != EOF;
}
int wire_arguments(const Command *command, size_t minimum, size_t maximum)
{
size_t index;
if (command->count < minimum || command->count > maximum) {
return 0;
}
for (index = 0; index < command->count; index++) {
int64_t ignored;
if (!wire_integer(command, index, &ignored)) {
return 0;
}
}
return 1;
}