diff options
-rw-r--r-- | unprotbas.c | 120 |
1 files changed, 57 insertions, 63 deletions
diff --git a/unprotbas.c b/unprotbas.c index 2f6fa38..2113fa1 100644 --- a/unprotbas.c +++ b/unprotbas.c @@ -111,7 +111,7 @@ void die(const char *msg) { /* read entire file into memory */ int readfile(void) { int got = fread(data, 1, BUFSIZE - 1, input_file); - if(verbose) fprintf(stderr, "Read %d bytes\n", got); + if(verbose) fprintf(stderr, "Read %d bytes.\n", got); fclose(input_file); if(got < MIN_PROG_SIZE) die("File too short to be a BASIC program (truncated?)\n"); @@ -213,7 +213,7 @@ int fixcode(void) { while(pos < filelen) { tmpno = getword(pos); if(tmpno <= lineno) { - fprintf(stderr, "Warning: line number %d at offset $%04x is <= previous line number %d\n", + fprintf(stderr, "Warning: line number %d at offset $%04x is <= previous line number %d.\n", tmpno, pos, lineno); } lineno = tmpno; @@ -221,7 +221,7 @@ int fixcode(void) { offset = data[pos + 2]; /* fprintf(stderr, "pos %d, line #%d, offset %d\n", pos, lineno, offset); */ if(offset < 6) { - if(verbose) fprintf(stderr, "Found invalid offset %d (<6) at line %d, file offset $%04x\n", offset, lineno, pos); + if(verbose) fprintf(stderr, "Found invalid offset %d (<6) at line %d, file offset $%04x.\n", offset, lineno, pos); offset += fixline(pos); result++; } @@ -232,7 +232,7 @@ int fixcode(void) { if(lineno == 32768) break; } - if(verbose) fprintf(stderr, "End program pos $%04x/%d\n", pos, pos); + if(verbose) fprintf(stderr, "End program file offset: $%04x/%d\n", pos, pos); if(filelen > pos) { int i, same = 1; @@ -245,7 +245,7 @@ int fixcode(void) { fprintf(stderr, "all $%02x", data[pos]); else fprintf(stderr, "maybe valid data"); - fprintf(stderr, ", %s\n", (keepgarbage ? "keeping" : "removing")); + fprintf(stderr, ", %s.\n", (keepgarbage ? "keeping" : "removing")); } if(!keepgarbage) filelen = pos; } @@ -274,8 +274,8 @@ void breakcode(void) { } } - if(!oldpos) die("can't protect code because there are no lines of code"); - if(lineno == 32767) die("can't protect code because there is already a line 32767"); + if(!oldpos) die("Can't protect code because there are no lines of code."); + if(lineno == 32767) die("Can't protect code because there is already a line 32767."); /* pos is now the start of line 32768, move it up to make room for the new line */ @@ -286,7 +286,7 @@ void breakcode(void) { memmove(data + pos, badcode, offset); if(verbose) - fprintf(stderr, "Inserted line 32767 with invalid offset at file offset $%04x\n", pos); + fprintf(stderr, "Inserted line 32767 with invalid offset at file offset $%04x.\n", pos); /* update pointers that would be affected by the code move */ stmcur += offset; @@ -304,7 +304,7 @@ void move_code(int offset) { unsigned char *dest = data + vvstart + offset; if(dest < data || ((filelen + offset) > (BUFSIZE - 1))) { - die("attempt to move memory out of range; corrupt header bytes?\n"); + die("Attempt to move memory out of range; corrupt header bytes?\n"); } memmove(dest, data + vvstart, filelen); @@ -348,7 +348,7 @@ int vntable_ok(void) { int vp, bad; if(vntp == vntd) { - if(verbose) fprintf(stderr, "No variables\n"); + if(verbose) fprintf(stderr, "No variables.\n"); return 1; } @@ -461,7 +461,8 @@ void adjust_vntable_size(int oldsize, int newsize) { int move_by; if(oldsize != newsize) { move_by = newsize - oldsize; - if(verbose) fprintf(stderr, "Need %d bytes for vntable, have %d, moving VVTP by %d to $%04x\n", + if(verbose) fprintf(stderr, + "Need %d bytes for vntable, have %d, moving VVTP by %d to $%04x.\n", newsize, oldsize, move_by, vvtp + move_by); move_code(move_by); } @@ -486,7 +487,7 @@ void write_var_map(void) { FILE *f; int vp, count = 0; - if(verbose) fprintf(stderr, "Writing variable names to " MAP_FILE "\n"); + if(verbose) fprintf(stderr, "Writing variable names to '" MAP_FILE "'.\n"); f = fopen(MAP_FILE, "w"); if(!f) { perror(MAP_FILE); @@ -506,7 +507,7 @@ void write_var_map(void) { fclose(f); - if(verbose) fprintf(stderr, "Wrote %d variable names to " MAP_FILE "\n", count); + if(verbose) fprintf(stderr, "Wrote %d variable names to '" MAP_FILE "'.\n", count); } void die_mapfile(char *msg, int num) { @@ -524,7 +525,7 @@ void check_varname(const unsigned char *name, int line) { if(len < 1) die_mapfile("Blank variable name", line); if(len > 128) die_mapfile("Variable name >128 characters", line); if(name[0] < 'A' || name[0] > 'Z') - die_mapfile("Invalid variable name (first character must be a letter)", line); + die_mapfile("Invalid variable name: First character must be a letter", line); for(i = 1; i < len; i++) { c = name[i]; @@ -546,17 +547,17 @@ void check_varname(const unsigned char *name, int line) { /* type: scalar = 0, array = 1, string = 2 */ if(type == TYPE_SCALAR) { if(c == '$') - die_mapfile("type mismatch: numeric variable may not end with $", line); + 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); + die_mapfile("Type mismatch: numeric variable may not end with (", line); } else if(type == TYPE_ARRAY) { if(c != '(') - die_mapfile("type mismatch: array variable must end with (", line); + die_mapfile("Type mismatch: array variable must end with (", line); } else if(type == TYPE_STRING) { if(c != '$') - die_mapfile("type mismatch: string variable must end with $", line); + die_mapfile("Type mismatch: string variable must end with $", line); } else { - fprintf(stderr, "Warning: variable value table is corrupt (invalid type)\n"); + fprintf(stderr, "Warning: variable value table is corrupt (invalid type).\n"); } /* check for dups */ @@ -571,7 +572,7 @@ void read_var_map(void) { unsigned char *p = varnames, *curname = varnames; int count = 0, vvcount = (codestart - vvstart) / 8; - if(verbose) fprintf(stderr, "Reading variable names from " MAP_FILE "\n"); + if(verbose) fprintf(stderr, "Reading variable names from " MAP_FILE ".\n"); f = fopen(MAP_FILE, "r"); if(!f) { perror(MAP_FILE); @@ -594,7 +595,7 @@ void read_var_map(void) { } fclose(f); - if(verbose) fprintf(stderr, "Read %d variable names from " MAP_FILE "\n", count); + if(verbose) fprintf(stderr, "Read %d variable names from " MAP_FILE ".\n", count); if(vvcount > count) { fprintf(stderr, MAP_FILE ": not enough variables (have %d, need %d).\n", count, vvcount); @@ -604,12 +605,6 @@ void read_var_map(void) { exit(1); } - #if 0 - for(count = 0; varmap[count] != NULL; count++) { - fprintf(stderr, "\t%02d %s\n", count, varmap[count]); - } - #endif - varmap_count = count; } @@ -619,7 +614,7 @@ void apply_var_map(void) { unsigned char *v; if(verbose) - fprintf(stderr, "Using variable names from " MAP_FILE "\n"); + fprintf(stderr, "Using variable names from " MAP_FILE ".\n"); for(i = 0; i < varmap_count; i++) { v = varmap[i]; @@ -630,7 +625,6 @@ void apply_var_map(void) { new_vntable[newp - 1] |= 0x80; } - /* if(varmap_count < 128) */ new_vntable[newp++] = '\0'; i = vvstart - vnstart; @@ -647,7 +641,7 @@ void scramble_vars(void) { } if(shrinktable) { - if(verbose) fprintf(stderr, "Shrinking variable name table\n"); + if(verbose) fprintf(stderr, "Shrinking variable name table.\n"); adjust_vntable_size((vvstart - 1) - vnstart, (codestart - vvstart) / 8); } @@ -666,7 +660,7 @@ void scramble_vars(void) { if(varname_char == -1) fprintf(stderr, "random characters.\n"); else - fprintf(stderr, "character $%02x\n", varname_char); + fprintf(stderr, "character $%02x.\n", varname_char); } else { die("Can't protect variables because there are no variables."); } @@ -676,22 +670,22 @@ void scramble_vars(void) { void print_help(void) { fprintf(stderr, "Usage: %s [-v] [-f] [-n] [-g] [-c] [-r|-w] <inputfile> <outputfile>\n", self); fprintf(stderr, " %s [-v] [-p|-pc|-pv] [-xr|-xNN] [-s] <inputfile> <outputfile>\n", self); - fprintf(stderr, "-v: verbose\n"); - fprintf(stderr, "-f: force variable name table rebuild\n"); - fprintf(stderr, "-n: do not rebuild variable name table, even if it's invalid\n"); - fprintf(stderr, "-g: remove trailing garbage, if present\n"); - fprintf(stderr, "-c: check only; no output file\n"); - fprintf(stderr, "-w: write variable names to varnames.txt\n"); - fprintf(stderr, "-r: read variable names from varnames.txt\n"); - fprintf(stderr, "-pc/-pv/-p: protect code/variables/both\n"); - fprintf(stderr, "-s: shrink variable name table to min size, with -p/-pv\n"); - fprintf(stderr, "-xNN: hex code NN for variable names, with -p/-pv\n"); - fprintf(stderr, "-xr: random variable names, with -p/-pv\n"); - fprintf(stderr, "Use - as a filename to read from stdin and/or write to stdout\n"); + fprintf(stderr, " -v: Verbose.\n"); + fprintf(stderr, " -f: Force variable name table rebuild.\n"); + fprintf(stderr, " -n: Do not rebuild variable name table, even if it's invalid.\n"); + fprintf(stderr, " -g: Remove trailing garbage, if present.\n"); + fprintf(stderr, " -c: Check only; no output file.\n"); + fprintf(stderr, " -w: Write variable names to 'varnames.txt'.\n"); + fprintf(stderr, " -r: Read variable names from 'varnames.txt'.\n"); + fprintf(stderr, " -pc/-pv/-p: Protect code/variables/both.\n"); + fprintf(stderr, " -s: Shrink variable name table to min size, with -p/-pv.\n"); + fprintf(stderr, " -xNN: Hex code NN for variable names, with -p/-pv.\n"); + fprintf(stderr, " -xr: Random variable names, with -p/-pv.\n"); + fprintf(stderr, "Use - as a filename to read from stdin and/or write to stdout.\n"); } void invalid_args(const char *arg) { - fprintf(stderr, "%s: Invalid argument '%s'\n\n", self, arg); + fprintf(stderr, "%s: Invalid argument '%s'.\n\n", self, arg); print_help(); exit(1); } @@ -708,7 +702,7 @@ FILE *open_file(const char *name, const char *mode) { void open_input(const char *name) { if(!name) { if(isatty(fileno(stdin))) { - die("can't read binary data from the terminal"); + die("Can't read binary data from the terminal."); } if(freopen(NULL, "rb", stdin)) { input_file = stdin; @@ -725,7 +719,7 @@ void open_input(const char *name) { void open_output(const char *name) { if(!name || (strcmp(name, "-") == 0)) { if(isatty(fileno(stdout))) { - die("refusing to write binary data to the terminal"); + die("Refusing to write binary data to the terminal."); } if(freopen(NULL, "wb", stdout)) { output_file = stdout; @@ -781,7 +775,7 @@ void parse_args(int argc, char **argv) { case 0: protect_code = protect_vars = 1; break; default: - die("invalid -p suboption (only -p, -pc, -pv are valid)"); + die("Invalid -p suboption (only -p, -pc, -pv are valid)."); } } break; @@ -791,13 +785,13 @@ void parse_args(int argc, char **argv) { case 'r': varname_char = -1; break; case 0: - die("-x option requires a hex number or 'r' (e.g. -x20, not -x 20)"); break; + die("-x option requires a hex number or 'r' (e.g. -x20, not -x 20)."); break; default: { char *e; varname_char = (int)strtol(&(*argv)[2], &e, 16); if(*e != 0 || varname_char > 0xff) - die("invalid hex value for -x option (range is 0 to ff)"); + die("invalid hex value for -x option (range is 0 to ff)."); } } } @@ -824,19 +818,19 @@ void parse_args(int argc, char **argv) { } } - if(!input_file) die("No input file given (use - for stdin)"); - if(!checkonly && !output_filename) die("No output file given (use - for stdout)"); - if(keepvars && forcevars) die("-f and -n are mutually exclusive"); - if(readmap && writemap) die("-r and -w are mutually exclusive"); - if(readmap && keepvars) die("-r and -n are mutually exclusive (maybe you want -w?)"); + if(!input_file) die("No input file given (use - for stdin)."); + if(!checkonly && !output_filename) die("No output file given (use - for stdout)."); + if(keepvars && forcevars) die("-f and -n are mutually exclusive."); + if(readmap && writemap) die("-r and -w are mutually exclusive."); + if(readmap && keepvars) die("-r and -n are mutually exclusive, maybe you want -w?"); if(protect_code || protect_vars) { if(checkonly || keepvars || forcevars || readmap || writemap || !keepgarbage) - die("-p, -pc, -pv options can only be combined with -v, -x, -s"); + die("-p, -pc, -pv options can only be combined with -v, -x, -s."); } if(xopt_used && !protect_vars) - die("-x option requires -p or -pv"); + die("-x option requires -p or -pv."); if(shrinktable && !protect_vars) - die("-s option requires -p or -pv"); + die("-s option requires -p or -pv."); } int main(int argc, char **argv) { @@ -846,7 +840,7 @@ int main(int argc, char **argv) { filelen = readfile(); read_header(); - if(lomem) die("This doesn't look like an Atari BASIC program (no $0000 signature)"); + if(lomem) die("This doesn't look like an Atari BASIC program (no $0000 signature)."); if(protect_code || protect_vars) { if(verbose) { @@ -870,18 +864,18 @@ int main(int argc, char **argv) { if(!keepvars) { if(fixvars()) { was_protected = 1; - if(verbose) fprintf(stderr, "Variable names replaced\n"); + if(verbose) fprintf(stderr, "Variable names replaced.\n"); } else { - if(verbose) fprintf(stderr, "Variable names were already OK\n"); + if(verbose) fprintf(stderr, "Variable names were already OK.\n"); } } } if(fixcode()) { - if(verbose) fprintf(stderr, "Fixed invalid offset in code\n"); + if(verbose) fprintf(stderr, "Fixed invalid offset in code.\n"); was_protected = 1; } else { - if(verbose) fprintf(stderr, "No invalid offsets\n"); + if(verbose) fprintf(stderr, "No invalid offsets.\n"); } if(verbose) { @@ -899,7 +893,7 @@ int main(int argc, char **argv) { open_output(output_filename); outbytes = fwrite(data, 1, filelen, output_file); fclose(output_file); - if(verbose) fprintf(stderr, "Wrote %d bytes\n", outbytes); + if(verbose) fprintf(stderr, "Wrote %d bytes.\n", outbytes); if(writemap) write_var_map(); |