aboutsummaryrefslogtreecommitdiff
path: root/unprotbas.rst
diff options
context:
space:
mode:
Diffstat (limited to 'unprotbas.rst')
-rw-r--r--unprotbas.rst168
1 files changed, 143 insertions, 25 deletions
diff --git a/unprotbas.rst b/unprotbas.rst
index 28ccd8b..21bbdb1 100644
--- a/unprotbas.rst
+++ b/unprotbas.rst
@@ -11,17 +11,18 @@ Unprotect LIST-protected Atari 8-bit BASIC programs
SYNOPSIS
========
-unprotbas [**-v**] [**-f**] [**-n**] [**-g**] [**-c**] **input-file** **output-file**
+unprotbas [**-v**] [**-f**] [**-n**] [**-g**] [**-c**] [**-r** | **-w**] **input-file** **output-file**
DESCRIPTION
===========
-**unprotbas** modifies a LIST-protected Atari 8-bit BASIC program,
-creating a new non-protected copy. See **DETAILS**, below, to
-understand how the protection and unprotection works.
+**unprotbas** modifies a tokenized LIST-protected Atari 8-bit BASIC
+program, creating a new non-protected copy. See **DETAILS**, below,
+to understand how the protection and unprotection works.
**input-file** must be a tokenized (SAVEd) Atari BASIC program. Use
-*-* to read from standard input.
+*-* to read from standard input, but **unprotbas** will refuse to
+read from standard input if it's a terminal.
**output-file** will be the unprotected tokenized BASIC program. If it
already exists, it will be overwritten. Use *-* to write to standard
@@ -32,13 +33,16 @@ the terminal).
OPTIONS
=======
+Options may appear in any order. The first non-option argument is used
+for **input-file**; the second is **output-file**. A third non-option
+argument is an error.
+
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.
-**-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**.
@@ -56,6 +60,20 @@ will be treated as options.
memory, but doesn't write the result anywhere. In this mode, there
is no **output-file**.
+**-w**
+ Write the variable names to **varnames.txt**, one per line.
+ This can be edited, and later used with **-r** to set the variable names
+ to something sensible rather than A, B, C, etc. For an unprotected
+ program, you can use **-n** to write the existing names rather than
+ generating new ones. See **VARIABLE NAMES**, below. If **varnames.txt**
+ already exists, it will be overwritten.
+
+**-r**
+ Read variable names from **varnames.txt**, and use them instead of
+ generating the names. See **VARIABLE NAMES**, below.
+
+.. include:: genopts.rst
+
EXIT STATUS
===========
@@ -114,7 +132,7 @@ Variable name table scrambling
names in the program, the recovery process just invents new ones,
named A through Z, A1 through A9, B1 through B9, etc, etc. It'll
require human intelligence to figure out what each variable is for,
- since the names are meaningless.
+ since the names are meaningless. See **VARIABLE NAMES**, below.
The **output-file** may not be the exact size that the
**input-file** was. Some types of variable-name scrambling shrink
@@ -124,11 +142,17 @@ Variable name table scrambling
generates only one- and two-character variable names, so the rebuilt
table might be smaller.
+ The program **PROTECT.BAS**, found on Disk 2 of the Holmes Archive,
+ creates protected BASIC programs that only use variable name
+ scrambling.
+
+ **protbas**\(1) also does variable name scrambling.
+
Bad next-line pointer
Every line of tokenized BASIC contains a line length byte, which
- BASIC uses as a pointer to the next line of code. Before printing
- the READY prompt, BASIC iterates over every line of code in the
- program, using the next-line pointers, in order to delete any
+ BASIC uses as a pointer to the next line of code. Before executing
+ an immediate mode command, BASIC iterates over every line of code in
+ the program, using the next-line pointers, in order to delete any
existing line 32768 (the previous immediate mode command). If any
line's pointer is set to zero, that means it points to itself.
@@ -138,28 +162,53 @@ Bad next-line pointer
after LOADing such a file, *nothing* will work (even pressing RESET
won't get you out of it). The only way to use such a program is to
use the RUN command with a filename, and if the program ever exits
- (due to END, STOP, an error, or the Break key), BASIC will get stuck
- again.
+ (due to END, STOP, an error, Break key, or even System Reset), BASIC
+ will get stuck again.
- This doesn't *have* to be done with the last line in the
- program. The "poisoned" line could be followed by more lines of
- code, though they could never actually execute.
+ This doesn't *have* to be done with the last line in the program,
+ though it normally is. The "poisoned" line can never be executed (or
+ BASIC will lock up), but it could be followed by more lines of code
+ (which also could never be executed).
Line 32100 in the example above does this job, taking advantage of
the STMCUR pointer used by BASIC, which holds the address of the
line of tokenized code currently being executed.
- **unprotbas** fixes this simply by calculating what the pointer
- should be (based on the tokens in the line) and changing it. No
- information is lost by doing this.
+ Each statement in the line also has a statement-length byte. For
+ lines with only one statement, its value is the same as the line
+ length. For lines with multiple statements (separated by *:*), it's
+ a pointer to the next statement, counting from the start of the
+ current line. For the last statement on a line, it's a pointer to
+ the next line of code, meaning it's identical to the line length.
+
+ **unprotbas** fixes bad line lengths by setting the line length to
+ the statement length of the last statement. No information is lost
+ by doing this.
+
+ The program **UNPROTEC**, from the *Pirate's Treasure Chest*, can
+ fix bad pointers in protected programs, though it doesn't do
+ anything about variable name scrambling.
+
+ **protbas** also does this type of protection.
One more thing **unprotbas** can do is remove extra data from the end
of the file. It's possible for BASIC files to contain extra data that
-occurs after the end of the program. Some programs use this as a way
-to load arbitrary binary data into memory along with the program; for
-other programs, the extra data is truly garbage (e.g. an EOF character
-if the file came from a CP/M system, or padding to a block size if a
-dumb implementation of XMODEM was used to transfer the file).
+occurs after the end of the program. Such data might be:
+
+- Pre-defined strings and/or arrays, saved with the program by
+ modifying the STARP pointer.
+
+- Arbitrary binary data used by the program at runtime, such as
+ machine language routines, or fonts.
+
+- Zero bytes, caused by SAVEing the program with revision B BASIC. Every
+ time a program is LOADed, edited (or not) and then SAVEd again, 16
+ bytes of extra (garbage) data gets added to the program. To avoid
+ this, don't use revision B (use rev C if possible, A otherwise).
+
+- Garbage added by some system previously used to store or transmit
+ the file. CP/M systems might add an EOF (^Z) character. Dumb
+ file transfer software might pad the file up to its block size.
Normally, such "garbage" doesn't hurt anything. BASIC ignores it. Or
it normally does... if you suspect it's causing a problem, you can
@@ -167,4 +216,73 @@ remove it with the **-g** option. If removing the "garbage" causes the
program to fail to run, it wasn't garbage! **unprotbas** doesn't
remove extra data by default, to be on the safe side.
+VARIABLE NAMES
+==============
+
+If variable name scrambling was used, the original variable names no
+longer exist. **unprotbas** will generate them, according to these rules:
+
+ The first 26 numeric variables will be called *A* through *Z*. Further
+ numeric variables will be *A1* through *A9*, *B1* through *B9*, etc.
+
+ The first 26 string variables will be *A$* to *Z$*, then *A1$* to
+ *A9$*, *B1$* to *B9$*, etc.
+
+ The first 26 array variables will be *A(* to *Z(*, then *A1(* to
+ *A9(*, *B1(* to *B9(*, etc.
+
+Note that array variables have only the *(* as part of the name. The
+closing *)* is "cosmetic" and not part of the actual name.
+
+To properly reverse-engineer the protected program, it's necessary to assign
+meaningful variable names. **unprotbas** isn't smart enough to do this for you,
+but it can semi-automate the process.
+
+First, run **unprotbas** with the **-w** option. This will create a
+file called **varnames.txt**, containing the generated variable names.
+These are in order, one line per variable name, ending with *$* for strings
+and the *(* for arrays.
+
+Load the unprotected program on the Atari and LIST it (or use **chkbas** to get a
+listing), and edit **varnames.txt** in a text editor.
+
+As you figure out what each variable's purpose is, change its name
+in the text file. When editing the file:
+
+- Don't add or delete any lines.
+- Don't get rid of the *$* or *(* at the end of any line.
+- You may enter the names in lowercase (**unprotbas** will convert them to uppercase).
+- Remember to follow the rules for BASIC variable names:
+ The first character must be a letter, other characters must be a letter
+ or a number, and only the last character can be *$* or *(*.
+- No duplicates of the same type are allowed (you can have *FOO* and *FOO$*,
+ but not two numerics called *FOO*).
+
+When you're finished, re-run **unprotbas**, this time with the **-r**
+option. If all is well, the unprotected program will use your variable
+names, rather than generating new ones. If you broke the rules, you
+should get an informative error message explaining what and where the
+problem is.
+
+This process can also be used for regular unprotected programs. Use
+**-n -w** the first time, to save the existing variable names to
+**varnames.txt** rather than generating new ones.
+
+NOTES
+=====
+
+Atari BASIC has a limit of 128 variables in a program. It's actually
+possible for the variable name table to contain up to 256 variables,
+though the 129th and further ones won't be usable in the program. The
+variable value table can hold more than 256 values, though the
+variable numbers wrap around once they pass 255. The attempt to add
+variables past the 128th causes BASIC to respond with *ERROR- 4*, but
+the variable does get added to the tables. **unprotbas** will preserve
+these extra (useless) entries in the tables.
+
+If there more than 256 entries in the value table, you will see
+"Warning: skipping variable numbers >=256 in value table". This is
+a pathological case, and shouldn't happen with programs that aren't
+deliberately crafted to test this behaviour.
+
.. include:: manftr.rst