#include "scanner.h" #include #include #include typedef struct { const unsigned char *next; } sp_reader; static int horizontal(unsigned char character) { return character == ' ' || character == '\t' || character == '\r'; } static void skip_space(sp_reader *reader) { while (horizontal(*reader->next)) { reader->next += 1; } } static int at_end(sp_reader *reader) { skip_space(reader); return *reader->next == 0; } static int read_integer(sp_reader *reader, int lower, int upper, int *value) { const unsigned char *cursor; unsigned int magnitude = 0; int negative = 0; int overflow = 0; int digit_seen = 0; skip_space(reader); cursor = reader->next; if (*cursor == '+' || *cursor == '-') { negative = *cursor == '-'; cursor += 1; } while (*cursor >= '0' && *cursor <= '9') { unsigned int digit = (unsigned int)(*cursor - '0'); digit_seen = 1; if (magnitude > 100000u) { overflow = 1; } else { magnitude = magnitude * 10u + digit; } cursor += 1; } if (!digit_seen || (*cursor && !horizontal(*cursor))) { return -1; } if (overflow || magnitude > INT_MAX) { return -1; } *value = negative ? -(int)magnitude : (int)magnitude; if (*value < lower || *value > upper) { return -1; } reader->next = cursor; return 0; } static int read_plan(sp_reader *reader, sp_command *command) { int index; sp_plan *plan = &command->plan; if (read_integer(reader, 0, SP_MAX_ITEMS, &plan->count)) { return -1; } if (read_integer(reader, 0, SP_MAX_ITEMS - 1, &plan->first)) { return -1; } if (read_integer(reader, 0, SP_MAX_ITEMS - 1, &plan->last)) { return -1; } if (read_integer(reader, 1, 17, &plan->modulus)) { return -1; } if (read_integer(reader, 0, 16, &plan->residue)) { return -1; } for (index = 0; index < plan->count; ++index) { if (read_integer(reader, -1000, 1000, &plan->values[index])) { return -1; } } if (!at_end(reader) || !sp_plan_valid(plan)) { return -1; } command->kind = SP_CMD_PLAN; return 0; } int sp_decode(const char *text, sp_command *command) { sp_reader reader; unsigned char operation; memset(command, 0, sizeof(*command)); reader.next = (const unsigned char *)text; skip_space(&reader); operation = *reader.next; if (!operation) { return -1; } reader.next += 1; if (*reader.next && !horizontal(*reader.next)) { return -1; } if (operation == 'P') { return read_plan(&reader, command); } if (operation == 'T') { if (read_integer(&reader, 0, SP_MAX_BUDGET, &command->budget)) { return -1; } command->kind = SP_CMD_TICK; } else if (operation == 'S') { command->kind = SP_CMD_SNAPSHOT; } else if (operation == 'R') { command->kind = SP_CMD_RESTART; } else { return -1; } if (!at_end(&reader)) { command->kind = SP_CMD_INVALID; return -1; } return 0; } int sp_read_line(FILE *input, char **buffer, size_t *capacity) { size_t used = 0; int character; if (!*buffer) { *capacity = 128; *buffer = malloc(*capacity); if (!*buffer) { return -1; } } while ((character = fgetc(input)) != EOF && character != '\n') { if (used + 1 >= *capacity) { size_t larger; char *replacement; if (*capacity > SIZE_MAX / 2) { return -1; } larger = *capacity * 2; replacement = realloc(*buffer, larger); if (!replacement) { return -1; } *buffer = replacement; *capacity = larger; } (*buffer)[used++] = (char)character; } if (character == EOF && used == 0) { return 0; } (*buffer)[used] = 0; return 1; }