18 lines
19 KiB
JSON
18 lines
19 KiB
JSON
{
|
|
"context": {
|
|
"wire.h": "#ifndef WIRE_H\n#define WIRE_H\n#include <stddef.h>\n#include <stdint.h>\n#include <stdio.h>\n#define WIRE_ARGS 10\n#define WIRE_TOKEN 521\n#define WIRE_EVENTS 128\n#define WIRE_STATE 160\ntypedef struct {\n char name;\n size_t count;\n char arg[WIRE_ARGS][WIRE_TOKEN];\n} Command;\ntypedef struct {\n const char *name;\n int64_t a;\n int64_t b;\n int64_t c;\n} Event;\ntypedef struct {\n Event event[WIRE_EVENTS];\n size_t events;\n int64_t state[WIRE_STATE];\n size_t states;\n int overflow;\n} Observation;\ntypedef struct {\n FILE *input;\n unsigned long physical;\n unsigned long commands;\n int failed;\n char line[8192];\n} WireReader;\nvoid wire_reader_init(WireReader *reader, FILE *input);\nint wire_next(WireReader *reader, Command *command);\nint wire_integer(const Command *command, size_t index, int64_t *value);\nint64_t wire_number(const Command *command, size_t index);\nint wire_hex(const char *text, unsigned char *output, size_t capacity, size_t *length);\nvoid observe_begin(Observation *observation);\nvoid observe_event(Observation *observation, const char *name, int64_t a, int64_t b, int64_t c);\nvoid observe_value(Observation *observation, int64_t value);\nint observe_write(FILE *output, unsigned long sequence, const Observation *observation);\nint wire_arguments(const Command *command, size_t minimum, size_t maximum);\n#endif\n",
|
|
"wire.c": "#include \"wire.h\"\n#include <ctype.h>\n#include <errno.h>\n#include <inttypes.h>\n#include <limits.h>\n#include <stdlib.h>\n#include <string.h>\n\nvoid wire_reader_init(WireReader *reader, FILE *input)\n{\n memset(reader, 0, sizeof(*reader));\n reader->input = input;\n}\n\nstatic int finish_line(WireReader *reader)\n{\n size_t length = strlen(reader->line);\n if (length && reader->line[length - 1] == '\\n') {\n reader->line[--length] = '\\0';\n } else if (!feof(reader->input)) {\n int value;\n do {\n value = fgetc(reader->input);\n } while (value != '\\n' && value != EOF);\n reader->failed = 1;\n return 0;\n }\n if (length && reader->line[length - 1] == '\\r') {\n reader->line[--length] = '\\0';\n }\n return 1;\n}\n\nstatic int split_line(char *line, Command *command)\n{\n const unsigned char *cursor = (const unsigned char *)line;\n memset(command, 0, sizeof(*command));\n while (*cursor && isspace(*cursor)) {\n cursor++;\n }\n if (*cursor == '\\0' || *cursor == '#') {\n return 0;\n }\n command->name = (char)*cursor++;\n if (*cursor && !isspace(*cursor)) {\n return -1;\n }\n for (;;) {\n size_t length = 0;\n while (*cursor && isspace(*cursor)) {\n cursor++;\n }\n if (*cursor == '\\0' || *cursor == '#') {\n return 1;\n }\n if (command->count == WIRE_ARGS) {\n return -1;\n }\n while (*cursor && !isspace(*cursor)) {\n if (length + 1 == WIRE_TOKEN) {\n return -1;\n }\n command->arg[command->count][length++] = (char)*cursor++;\n }\n command->arg[command->count][length] = '\\0';\n command->count++;\n }\n}\n\nint wire_next(WireReader *reader, Command *command)\n{\n while (fgets(reader->line, sizeof(reader->line), reader->input)) {\n int parsed;\n reader->physical++;\n if (!finish_line(reader)) {\n return -1;\n }\n parsed = split_line(reader->line, command);\n if (parsed < 0) {\n reader->failed = 1;\n return -1;\n }\n if (parsed > 0) {\n reader->commands++;\n return 1;\n }\n }\n if (ferror(reader->input)) {\n reader->failed = 1;\n return -1;\n }\n return 0;\n}\n\nstatic int decimal_value(const char *text, int64_t *result)\n{\n size_t index = 0;\n int negative = 0;\n uint64_t magnitude = 0;\n uint64_t limit;\n if (text[index] == '+' || text[index] == '-') {\n negative = text[index] == '-';\n index++;\n }\n if (text[index] == '\\0') {\n return 0;\n }\n limit = negative ? (uint64_t)INT64_MAX + 1 : (uint64_t)INT64_MAX;\n for (; text[index] != '\\0'; index++) {\n unsigned char character = (unsigned char)text[index];\n unsigned digit;\n if (character < '0' || character > '9') {\n return 0;\n }\n digit = character - '0';\n if (magnitude > (limit - digit) / 10) {\n return 0;\n }\n magnitude = magnitude * 10 + digit;\n }\n if (negative && magnitude == (uint64_t)INT64_MAX + 1) {\n *result = INT64_MIN;\n } else if (negative) {\n *result = -(int64_t)magnitude;\n } else {\n *result = (int64_t)magnitude;\n }\n return 1;\n}\n\nint wire_integer(const Command *command, size_t index, int64_t *value)\n{\n if (index >= command->count) {\n return 0;\n }\n return decimal_value(command->arg[index], value);\n}\n\nint64_t wire_number(const Command *command, size_t index)\n{\n int64_t value = 0;\n (void)wire_integer(command, index, &value);\n return value;\n}\n\nstatic int hex_digit(unsigned char value)\n{\n if (value >= '0' && value <= '9') {\n return value - '0';\n }\n if (value >= 'a' && value <= 'f') {\n return value - 'a' + 10;\n }\n if (value >= 'A' && value <= 'F') {\n return value - 'A' + 10;\n }\n return -1;\n}\n\nint wire_hex(const char *text, unsigned char *output, size_t capacity, size_t *length)\n{\n size_t source = strlen(text);\n size_t index;\n if (strcmp(text, \"-\") == 0) {\n *length = 0;\n return 1;\n }\n if (source % 2 || source / 2 > capacity) {\n return 0;\n }\n for (index = 0; index < source / 2; index++) {\n int high = hex_digit((unsigned char)text[index * 2]);\n int low = hex_digit((unsigned char)text[index * 2 + 1]);\n if (high < 0 || low < 0) {\n return 0;\n }\n output[index] = (unsigned char)(high * 16 + low);\n }\n *length = source / 2;\n return 1;\n}\n\nvoid observe_begin(Observation *observation)\n{\n memset(observation, 0, sizeof(*observation));\n}\n\nvoid observe_event(Observation *observation, const char *name, int64_t a, int64_t b, int64_t c)\n{\n Event *event;\n if (observation->events == WIRE_EVENTS) {\n observation->overflow = 1;\n return;\n }\n event = &observation->event[observation->events++];\n event->name = name;\n event->a = a;\n event->b = b;\n event->c = c;\n}\n\nvoid observe_value(Observation *observation, int64_t value)\n{\n if (observation->states == WIRE_STATE) {\n observation->overflow = 1;\n return;\n }\n observation->state[observation->states++] = value;\n}\n\nstatic int write_unsigned(FILE *output, uint64_t value)\n{\n char digits[32];\n size_t count = 0;\n do {\n unsigned digit = (unsigned)(value % 10);\n digits[count++] = (char)('0' + digit);\n value /= 10;\n } while (value != 0);\n while (count > 0) {\n count--;\n if (fputc(digits[count], output) == EOF) {\n return 0;\n }\n }\n return 1;\n}\n\nstatic int write_signed(FILE *output, int64_t value)\n{\n uint64_t magnitude;\n if (value < 0) {\n if (fputc('-', output) == EOF) {\n return 0;\n }\n magnitude = (uint64_t)(-(value + 1)) + 1;\n } else {\n magnitude = (uint64_t)value;\n }\n return write_unsigned(output, magnitude);\n}\n\nstatic int write_field(FILE *output, int64_t value)\n{\n if (fputc(' ', output) == EOF) {\n return 0;\n }\n return write_signed(output, value);\n}\n\nstatic int write_prefix(FILE *output, char kind, unsigned long sequence)\n{\n if (fputc(kind, output) == EOF) {\n return 0;\n }\n if (fputc(' ', output) == EOF) {\n return 0;\n }\n return write_unsigned(output, sequence);\n}\n\nstatic int write_event(FILE *output, unsigned long sequence, const Event *event)\n{\n if (!write_prefix(output, 'E', sequence)) {\n return 0;\n }\n if (fputc(' ', output) == EOF || fputs(event->name, output) == EOF) {\n return 0;\n }\n if (!write_field(output, event->a)) {\n return 0;\n }\n if (!write_field(output, event->b)) {\n return 0;\n }\n if (!write_field(output, event->c)) {\n return 0;\n }\n return fputc('\\n', output) != EOF;\n}\n\nint observe_write(FILE *output, unsigned long sequence, const Observation *observation)\n{\n size_t index;\n if (observation->overflow) {\n return 0;\n }\n for (index = 0; index < observation->events; index++) {\n if (!write_event(output, sequence, &observation->event[index])) {\n return 0;\n }\n }\n if (!write_prefix(output, 'S', sequence)) {\n return 0;\n }\n for (index = 0; index < observation->states; index++) {\n if (!write_field(output, observation->state[index])) {\n return 0;\n }\n }\n return fputc('\\n', output) != EOF;\n}\n\nint wire_arguments(const Command *command, size_t minimum, size_t maximum)\n{\n size_t index;\n if (command->count < minimum || command->count > maximum) {\n return 0;\n }\n for (index = 0; index < command->count; index++) {\n int64_t ignored;\n if (!wire_integer(command, index, &ignored)) {\n return 0;\n }\n }\n return 1;\n}\n",
|
|
"audit.h": "#ifndef AUDIT_H\n#define AUDIT_H\n#include \"wire.h\"\n#define AUDIT_HISTORY 8\n#define AUDIT_CHANNELS 26\n\ntypedef struct {\n unsigned long sequence;\n int command;\n int result;\n int64_t value;\n} AuditRecord;\n\ntypedef struct {\n uint64_t accepted;\n uint64_t rejected;\n int64_t total;\n uint32_t fingerprint;\n unsigned long sequence;\n unsigned long calls[AUDIT_CHANNELS];\n unsigned long failures[AUDIT_CHANNELS];\n AuditRecord records[AUDIT_HISTORY];\n size_t first;\n size_t used;\n int64_t minimum;\n int64_t maximum;\n int has_value;\n} Audit;\n\nvoid audit_init(Audit *audit);\nvoid audit_record(Audit *audit, char command, int result, int64_t value);\nvoid audit_observe(const Audit *audit, Observation *output);\nint audit_query(const Audit *audit, size_t age, AuditRecord *record);\nvoid audit_channel(const Audit *audit, unsigned channel, unsigned long *calls, unsigned long *failures);\nuint32_t audit_mix(uint32_t state, uint64_t value);\n#endif\n",
|
|
"audit.c": "#include \"audit.h\"\n#include <string.h>\n\nuint32_t audit_mix(uint32_t state, uint64_t value)\n{\n unsigned index;\n for (index = 0; index < 8; index++) {\n state ^= (uint32_t)(value & 255u);\n state *= UINT32_C(16777619);\n value >>= 8;\n }\n return state;\n}\n\nvoid audit_init(Audit *audit)\n{\n memset(audit, 0, sizeof(*audit));\n audit->fingerprint = UINT32_C(2166136261);\n}\n\nstatic size_t history_position(const Audit *audit, size_t ordinal)\n{\n return (audit->first + ordinal) % AUDIT_HISTORY;\n}\n\nstatic void retain_record(Audit *audit, const AuditRecord *record)\n{\n size_t position;\n if (audit->used < AUDIT_HISTORY) {\n position = history_position(audit, audit->used);\n audit->used++;\n } else {\n position = audit->first;\n audit->first = (audit->first + 1) % AUDIT_HISTORY;\n }\n audit->records[position] = *record;\n}\n\nstatic void note_value(Audit *audit, int64_t value)\n{\n audit->total += value;\n if (!audit->has_value) {\n audit->minimum = value;\n audit->maximum = value;\n audit->has_value = 1;\n return;\n }\n if (value < audit->minimum) {\n audit->minimum = value;\n }\n if (value > audit->maximum) {\n audit->maximum = value;\n }\n}\n\nvoid audit_record(Audit *audit, char command, int result, int64_t value)\n{\n AuditRecord record;\n unsigned channel;\n audit->sequence++;\n record.sequence = audit->sequence;\n record.command = (unsigned char)command;\n record.result = result;\n record.value = value;\n if (command >= 'A' && command <= 'Z') {\n channel = (unsigned)(command - 'A');\n audit->calls[channel]++;\n if (result < 0) {\n audit->failures[channel]++;\n }\n }\n if (result >= 0) {\n audit->accepted++;\n note_value(audit, value);\n } else {\n audit->rejected++;\n }\n audit->fingerprint = audit_mix(audit->fingerprint, record.sequence);\n audit->fingerprint = audit_mix(audit->fingerprint, (unsigned char)command);\n audit->fingerprint = audit_mix(audit->fingerprint, (uint64_t)(int64_t)result);\n audit->fingerprint = audit_mix(audit->fingerprint, (uint64_t)value);\n retain_record(audit, &record);\n}\n\nint audit_query(const Audit *audit, size_t age, AuditRecord *record)\n{\n size_t ordinal;\n if (age >= audit->used) {\n return 0;\n }\n ordinal = audit->used - age - 1;\n *record = audit->records[history_position(audit, ordinal)];\n return 1;\n}\n\nvoid audit_channel(const Audit *audit, unsigned channel, unsigned long *calls, unsigned long *failures)\n{\n *calls = 0;\n *failures = 0;\n if (channel >= AUDIT_CHANNELS) {\n return;\n }\n *calls = audit->calls[channel];\n *failures = audit->failures[channel];\n}\n\nvoid audit_observe(const Audit *audit, Observation *output)\n{\n size_t index;\n observe_value(output, (int64_t)audit->sequence);\n observe_value(output, (int64_t)audit->accepted);\n observe_value(output, (int64_t)audit->rejected);\n observe_value(output, audit->total);\n observe_value(output, audit->fingerprint);\n observe_value(output, audit->minimum);\n observe_value(output, audit->maximum);\n observe_value(output, (int64_t)audit->used);\n for (index = 0; index < AUDIT_HISTORY; index++) {\n AuditRecord record;\n if (audit_query(audit, index, &record)) {\n observe_value(output, (int64_t)record.sequence);\n observe_value(output, record.command);\n observe_value(output, record.result);\n observe_value(output, record.value);\n } else {\n observe_value(output, 0);\n observe_value(output, 0);\n observe_value(output, 0);\n observe_value(output, 0);\n }\n }\n for (index = 0; index < AUDIT_CHANNELS; index++) {\n unsigned long calls;\n unsigned long failures;\n audit_channel(audit, (unsigned)index, &calls, &failures);\n observe_value(output, (int64_t)calls);\n observe_value(output, (int64_t)failures);\n }\n}\n",
|
|
"service.h": "#ifndef SERVICE_H\n#define SERVICE_H\n#include \"wire.h\"\n#include <stdint.h>\ntypedef struct {\n int result;\n int64_t value;\n int64_t detail;\n} Reply;\nint service_init(const char *executable);\nvoid service_close(void);\nReply service_execute(const Command *command);\nvoid service_observe(Observation *observation);\n#endif\n",
|
|
"main.c": "#include \"service.h\"\n#include \"audit.h\"\n#include <stdio.h>\n#include <string.h>\n\nstatic Reply diagnostic(const Command *command, const Audit *audit, int *handled)\n{\n Reply reply = {-2, 0, 0};\n *handled = 1;\n if (command->name == 'Y') {\n AuditRecord record;\n int64_t age;\n if (!wire_arguments(command, 1, 1)) {\n return reply;\n }\n age = wire_number(command, 0);\n if (age < 0 || !audit_query(audit, (size_t)age, &record)) {\n return reply;\n }\n reply.result = record.result;\n reply.value = record.value;\n reply.detail = (int64_t)record.sequence;\n } else if (command->name == 'Z') {\n unsigned long calls;\n unsigned long failures;\n int64_t channel;\n if (!wire_arguments(command, 1, 1)) {\n return reply;\n }\n channel = wire_number(command, 0);\n if (channel < 0 || channel >= AUDIT_CHANNELS) {\n return reply;\n }\n audit_channel(audit, (unsigned)channel, &calls, &failures);\n reply.result = 0;\n reply.value = (int64_t)calls;\n reply.detail = (int64_t)failures;\n } else {\n *handled = 0;\n }\n return reply;\n}\n\nint main(int argc, char **argv)\n{\n WireReader reader;\n Command command;\n Audit audit;\n int next;\n if (argc != 1) {\n return 64;\n }\n if (!service_init(argv[0])) {\n return 65;\n }\n wire_reader_init(&reader, stdin);\n audit_init(&audit);\n while ((next = wire_next(&reader, &command)) > 0) {\n Observation output;\n Reply reply;\n int handled;\n observe_begin(&output);\n reply = diagnostic(&command, &audit, &handled);\n if (!handled) {\n reply = service_execute(&command);\n }\n observe_event(&output, \"RESULT\", reply.result, reply.value, reply.detail);\n audit_record(&audit, command.name, reply.result, reply.value);\n service_observe(&output);\n audit_observe(&audit, &output);\n if (!observe_write(stdout, reader.commands, &output)) {\n service_close();\n return 74;\n }\n }\n service_close();\n return next < 0 ? 65 : 0;\n}\n",
|
|
"plugin.h": "#ifndef PLUGIN_H\n#define PLUGIN_H\n#include <stdint.h>\n#define PLUGIN_ABI 4u\ntypedef struct {\n unsigned abi;\n unsigned identity;\n int gain;\n int offset;\n int64_t (*convert)(int64_t);\n} Plugin;\ntypedef const Plugin *(*PluginEntry)(void);\nconst Plugin *plugin_entry(void);\n#endif\n",
|
|
"plugin.c": "#include \"plugin.h\"\n#ifndef IDENTITY\n#define IDENTITY 1\n#endif\n#ifndef GAIN\n#define GAIN 2\n#endif\n#ifndef OFFSET\n#define OFFSET 7\n#endif\n#ifndef ABI\n#define ABI PLUGIN_ABI\n#endif\nstatic int64_t convert_value(int64_t value)\n{\n return value * GAIN + OFFSET;\n}\nconst Plugin *plugin_entry(void)\n{\n static const Plugin plugin = {ABI, IDENTITY, GAIN, OFFSET, convert_value};\n return &plugin;\n}\n",
|
|
"Makefile": "CC = gcc\nCFLAGS = -std=c11 -O2 -Wall -Wextra -Werror\nall: app plugins/libmode1.so plugins/libmode2.so plugins/libmode3.so\napp: main.c wire.c audit.c service.c service.h audit.h wire.h plugin.h\n\t$(CC) $(CFLAGS) main.c wire.c audit.c service.c -o $@ -ldl\nplugins/libmode1.so: plugin.c plugin.h\n\tmkdir -p plugins\n\t$(CC) $(CFLAGS) -fPIC -shared -DIDENTITY=1 -DGAIN=2 -DOFFSET=7 plugin.c -o $@\nplugins/libmode2.so: plugin.c plugin.h\n\tmkdir -p plugins\n\t$(CC) $(CFLAGS) -fPIC -shared -DIDENTITY=2 -DGAIN=5 -DOFFSET=-3 plugin.c -o $@\nplugins/libmode3.so: plugin.c plugin.h\n\tmkdir -p plugins\n\t$(CC) $(CFLAGS) -fPIC -shared -DIDENTITY=3 -DABI=99 plugin.c -o $@\nclean:\n\trm -f app plugins/*.so\n.PHONY: all clean\n"
|
|
},
|
|
"candidate_files": [
|
|
"service.c"
|
|
],
|
|
"seed": 20261017
|
|
}
|