From c84cedb3337c2cdabb5f3ff91aa44810d9661091 Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Wed, 29 May 2024 14:18:15 -0400 Subject: unprotbas: don't allow -x without -p or -pv, group options in doc. --- unprotbas.1 | 28 +++++++++++++++++++++------- unprotbas.c | 35 +++++++++++++++++++++++++++++++++-- unprotbas.rst | 29 +++++++++++++++++++++-------- 3 files changed, 75 insertions(+), 17 deletions(-) diff --git a/unprotbas.1 b/unprotbas.1 index 437cfaf..7fb818c 100644 --- a/unprotbas.1 +++ b/unprotbas.1 @@ -32,7 +32,7 @@ level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] unprotbas \- Unprotect or create LIST-protected Atari 8-bit BASIC programs .SH SYNOPSIS .sp -unprotbas [\fB\-v\fP] [ [\fB\-f\fP] [\fB\-n\fP] [\fB\-g\fP] [\fB\-c\fP] [\fB\-r\fP | \fB\-w\fP] ] | [ [\fB\-p\fP | \fB\-pc\fP | \fB\-pv\fP] ] \fBinput\-file\fP \fBoutput\-file\fP +unprotbas [\fB\-v\fP] [ [\fB\-f\fP] [\fB\-n\fP] [\fB\-g\fP] [\fB\-c\fP] [\fB\-r\fP | \fB\-w\fP] ] | [ [\fB\-p\fP | \fB\-pc\fP | \fB\-pv\fP] [\fB\-x\fP[\fIr|XX\fP] ] \fBinput\-file\fP \fBoutput\-file\fP .SH DESCRIPTION .sp \fBunprotbas\fP modifies a LIST\-protected Atari 8\-bit BASIC program, @@ -55,10 +55,20 @@ tokenized BASIC is binary data and may confuse the terminal). Option bundling is not supported, use e.g. \fB\-v \-f\fP, not \fB\-vf\fP\&. To use filenames beginning with \fI\-\fP, write them as \fI\&./\-file\fP, or they will be treated as options. +.SS General Options .INDENT 0.0 .TP +.B \fB\-\-help\fP +Print usage message and exit. +.TP +.B \fB\-\-version\fP +Print version number and exit. +.TP .B \fB\-v\fP Verbose operation. +.UNINDENT +.SS Unprotection Options +.INDENT 0.0 .TP .B \fB\-f\fP Force the variable name table to be rebuilt, even if it looks OK. @@ -87,19 +97,23 @@ generating new ones. See \fBVARIABLE NAMES\fP, below. .B \fB\-r\fP Read variable names from \fBvarnames.txt\fP, and use them instead of generating the names. See \fBVARIABLE NAMES\fP, below. +.UNINDENT +.SS Protection Options +.INDENT 0.0 .TP .B \fB\-p\fP, \fB\-pc\fP, \fB\-pv\fP LIST\-protect the program, rather than unprotecting it. \fB\-pc\fP sets an invalid (0) next\-line pointer on the last line of code. \fB\-pv\fP replaces the variable names with the Atari EOL character (\fB$9B\fP). \fB\-p\fP does both. None of the other options except \fB\-v\fP (verbose) -can be used with these. +and \fB\-x\fP can be used with these. .TP -.B \fB\-\-help\fP -Print usage message and exit. -.TP -.B \fB\-\-version\fP -Print version number and exit. +.B \fB\-xr\fP, \fB\-xXX\fP +Character to use for variable name protection, with \fB\-p\fP or +\fB\-pv\fP\&. \fIXX\fP is the character code in hex, e.g. \fB\-x20\fP to use +a space. Default is \fB9b\fP (the EOL character). \fB\-xr\fP means random +codes. Do not put a space between the \fB\-x\fP and the hex digits or \fBr\fP\&. +This option only works if \fB\-p\fP or \fB\-pv\fP is used. .UNINDENT .SH EXIT STATUS .INDENT 0.0 diff --git a/unprotbas.c b/unprotbas.c index 37013cf..b2b3d98 100644 --- a/unprotbas.c +++ b/unprotbas.c @@ -3,6 +3,7 @@ #include #include #include +#include /* attempt to fix a "list-protected" Atari 8-bit BASIC program. we don't fully detokenize, so this won't fix truly corrupted @@ -54,6 +55,9 @@ unsigned char badcode[] = { 0x16, /* end-of-line token */ }; +/* for -p/-pv */ +int varname_char = 0x9b; + /* for the -r option */ #define MAP_FILE "varnames.txt" unsigned char varnames[BUFSIZE]; @@ -630,8 +634,13 @@ void scramble_vars(void) { exit(2); } + if(varname_char == -1) srand(time(NULL)); + for(i = vnstart; i < vvstart - 1; i++) - data[i] = '\x9b'; + if(varname_char == -1) + data[i] = (rand() >> 8) & 0xff; + else + data[i] = varname_char & 0xff; if(verbose) { i -= vnstart; @@ -652,6 +661,8 @@ void print_help(void) { 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, "-xXX: hex code XX for variable names, with -p/-pc\n"); + fprintf(stderr, "-xr: random variable names, with -p/-pc\n"); fprintf(stderr, "Use - as a filename to read from stdin and/or write to stdout\n"); } @@ -706,6 +717,7 @@ void open_output(const char *name) { void parse_args(int argc, char **argv) { char *p; + int xopt_used = 0; self = *argv; p = strrchr(self, '/'); @@ -749,6 +761,23 @@ void parse_args(int argc, char **argv) { } } break; + case 'x': { + xopt_used++; + switch((*argv)[2]) { + case 'r': + varname_char = -1; break; + case 0: + 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 character for -x option (range is 0 to ff)"); + } + } + } + break; case 0: if(!input_file) open_input(NULL); @@ -776,8 +805,10 @@ void parse_args(int argc, char **argv) { 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't be combined with other options except -v"); + die("-p, -pc, -pv options can't be combined with other options except -x, -v"); } + if(xopt_used && !protect_vars) + die("-x option requires -p or -pv"); } int main(int argc, char **argv) { diff --git a/unprotbas.rst b/unprotbas.rst index 35a5069..a3f5e28 100644 --- a/unprotbas.rst +++ b/unprotbas.rst @@ -11,7 +11,7 @@ Unprotect or create LIST-protected Atari 8-bit BASIC programs SYNOPSIS ======== -unprotbas [**-v**] [ [**-f**] [**-n**] [**-g**] [**-c**] [**-r** | **-w**] ] | [ [**-p** | **-pc** | **-pv**] ] **input-file** **output-file** +unprotbas [**-v**] [ [**-f**] [**-n**] [**-g**] [**-c**] [**-r** | **-w**] ] | [ [**-p** | **-pc** | **-pv**] [**-x**\[*r|XX*] ] **input-file** **output-file** DESCRIPTION =========== @@ -39,9 +39,19 @@ Option bundling is not supported, use e.g. **-v -f**, not **-vf**. To use filenames beginning with *-*, write them as *./-file*, or they will be treated as options. +General Options +--------------- +**--help** + Print usage message and exit. + +**--version** + Print version number and exit. + **-v** Verbose operation. +Unprotection Options +-------------------- **-f** Force the variable name table to be rebuilt, even if it looks OK. This option cannot be combined with **-n**. @@ -70,18 +80,21 @@ will be treated as options. Read variable names from **varnames.txt**, and use them instead of generating the names. See **VARIABLE NAMES**, below. +Protection Options +------------------ **-p**, **-pc**, **-pv** LIST-protect the program, rather than unprotecting it. **-pc** sets an invalid (0) next-line pointer on the last line of code. **-pv** replaces the variable names with the Atari EOL character (**$9B**). **-p** does both. None of the other options except **-v** (verbose) - can be used with these. - -**--help** - Print usage message and exit. - -**--version** - Print version number and exit. + and **-x** can be used with these. + +**-xr**, **-xXX** + Character to use for variable name protection, with **-p** or + **-pv**. *XX* is the character code in hex, e.g. **-x20** to use + a space. Default is **9b** (the EOL character). **-xr** means random + codes. Do not put a space between the **-x** and the hex digits or **r**. + This option only works if **-p** or **-pv** is used. EXIT STATUS =========== -- cgit v1.2.3