From afc94c493aa6b5a684156a9fda4ef31a9fb201c1 Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Sat, 22 Jun 2024 13:25:20 -0400 Subject: protbas: port to callback API. --- bas.h | 1 + protbas.c | 49 +++++++++++++++++++++++++------------------------ 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/bas.h b/bas.h index dd75e19..a464e0c 100644 --- a/bas.h +++ b/bas.h @@ -90,6 +90,7 @@ extern CALLBACK_PTR(on_exp_token); extern CALLBACK_PTR(on_var_token); extern CALLBACK_PTR(on_string_const); extern CALLBACK_PTR(on_num_const); +extern CALLBACK_PTR(on_trailing_garbage); /* BASIC 14-byte header values */ extern unsigned short lomem; diff --git a/protbas.c b/protbas.c index 5b62496..5584049 100644 --- a/protbas.c +++ b/protbas.c @@ -56,41 +56,42 @@ void scramble_vars(void) { } } +CALLBACK(bad_offset) { + fprintf(stderr, "%s: program already was code-protected.\n", self); + exit(2); +} + +unsigned short last_pos = 0; +int last_lineno = -1; + +CALLBACK(save_linepos) { + last_pos = pos; + if(lineno == 32768) return; + last_lineno = lineno; +} + /* iterate over all the lines, insert a poisoned line 32767 just before line 32768 */ void breakcode(void) { - int pos = codestart, oldpos = 0; - int offset, lineno = -1, tmpno = -1; - - while(pos < filelen) { - lineno = tmpno; - tmpno = getword(pos); - if(tmpno == 32768) { - break; - } else { - offset = program[pos + 2]; - if(!offset) { - fprintf(stderr, "%s: program already was code-protected.\n", self); - exit(2); - } - oldpos = pos; - pos += offset; - } - } + int offset; + + on_start_line = save_linepos; + on_bad_line_length = bad_offset; + walk_all_code(); - 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(last_lineno == -1) die("Can't protect code because there are no lines of code."); + if(last_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 + /* last_pos is now the start of line 32768, move it up to make room for the new line */ offset = sizeof(badcode); - memmove(program + pos + offset, program + pos, filelen); + memmove(program + last_pos + offset, program + last_pos, filelen); /* insert new line */ - memmove(program + pos, badcode, offset); + memmove(program + last_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", last_pos); /* update pointers that would be affected by the code move */ stmcur += offset; -- cgit v1.2.3