From 921cf9c521b64641f6364ef04b370979ce99fd64 Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Wed, 29 May 2024 03:31:23 -0400 Subject: unprotbas: code readability (constants), mention rev B BASIC in doc. --- unprotbas.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'unprotbas.c') diff --git a/unprotbas.c b/unprotbas.c index 6edb38f..37013cf 100644 --- a/unprotbas.c +++ b/unprotbas.c @@ -37,6 +37,11 @@ /* tokenized colon (statement separator) */ #define TOK_COLON 0x14 +/* variable types, bits 6-7 of byte 0 of each vvtable entry. */ +#define TYPE_SCALAR 0 +#define TYPE_ARRAY 1 +#define TYPE_STRING 2 + /* entire file gets read into memory (for now) */ unsigned char data[BUFSIZE]; @@ -98,7 +103,7 @@ void die(const char *msg) { /* read entire file into memory */ int readfile(void) { - int got = fread(data, 1, 65535, input_file); + int got = fread(data, 1, BUFSIZE - 1, input_file); if(verbose) fprintf(stderr, "read %d bytes\n", got); fclose(input_file); if(got < MIN_PROG_SIZE) @@ -282,7 +287,7 @@ void breakcode(void) { void move_code(int offset) { unsigned char *dest = data + vvstart + offset; - if(dest < data || (filelen + offset) > 65535) { + if(dest < data || ((filelen + offset) > (BUFSIZE - 1))) { die("attempt to move memory out of range; corrupt header bytes?\n"); } @@ -394,9 +399,12 @@ int rebuild_vntable(int write) { } switch(type) { - case 1: varname = arrays++; sigil = 0xa8; break; - case 2: varname = strings++; sigil = 0xa4; break; - default: varname = scalars++; break; + case TYPE_SCALAR: varname = scalars++; break; + case TYPE_ARRAY: varname = arrays++; sigil = 0xa8; break; + case TYPE_STRING: varname = strings++; sigil = 0xa4; break; + default: + fprintf(stderr, "Warning: variable value #%d has unknown type.\n", varnum); + break; } if(varname < 26) { @@ -520,15 +528,15 @@ void check_varname(const unsigned char *name, int line) { /* c now has the last char of the name, make sure it matches the variable type */ type = data[vvstart + 8 * (line - 1)] >> 6; /* type: scalar = 0, array = 1, string = 2 */ - if(type == 0) { + if(type == TYPE_SCALAR) { if(c == '$') die_mapfile("type mismatch: numeric variable may not end with $", line); else if(c == '(') die_mapfile("type mismatch: numeric variable may not end with (", line); - } else if(type == 1) { + } else if(type == TYPE_ARRAY) { if(c != '(') die_mapfile("type mismatch: array variable must end with (", line); - } else if(type == 2) { + } else if(type == TYPE_STRING) { if(c != '$') die_mapfile("type mismatch: string variable must end with $", line); } else { -- cgit v1.2.3