TweetFollow Us on Twitter

Recovery
Volume Number:2
Issue Number:7
Column Tag:Special Projects

Recovering Protected Basic Programs

By DB Cooper, Seattle, WA

True Recovery at Last!

How many times have you saved what you thought was a completed MS Basic program to disk in protected mode, only to discover that was your only copy and now you need to make some changes? Recovering from Basic's protected option has been a hotly contested struggle between Basic programmers and the interpreter, which until now, has yielded very little in the way of a practical solution. In past issues of MacTutor, various attempts have been published, but each of these required that you do something to your program before you protected it. What if you didn't remember to do the special thing first? You were out of luck. Until now. After studying Mike Steiner's "Rescuit Program" in the September 1985 issue of MacTutor, I have invented a way to recover any protected Basic program after it is protected without any requirement on the author before he saved it. In otherwords, you can now unprotect any protected program without thinking about it ahead of time. Simply run this Basic program and your back in business! I accomplished this by writing the program below named 'Rescure(b)', which overwrites the encrypted code for: {Merge "it":'} at the front of the protected program. Then I set up a second program named 'It' which is merged to the protected program and then copies the program from memory to a file. This second step is based on Mike Steiner's article.

Version 2.1 Different

Unfortunately, this scheme didn't work with programs in the Microsoft Basic 2.1 Format, so I started to experiment. I determined that version 2.1 uses a seperate location in memory to store the names of the variables. I couldn't figure out how to find this table in memory directly, so instead I added a new variable "TenZZZZZZZZZZ" to the end of the table by defining it in the program, and had the routine search for ten Z's in a row. Then the program back tracks until it hits a binary zero, which defines the beginning and end of the variables table. (This method has worked with all the programs I have rescued, but I wouldn't bank on it always being successful.)

Cleaning up Garbage

At this point I ran into still another problem. The rescued program list still showed garbage in the variable and label names about 50% of the time. I scratched my head some more before I finally realized that the main listing must end with an odd number of characters (or even, when the first character is removed after the program has been loaded). I put in a counter to check for oddness.

After this final fix, the program seems to run quite well. When a rescue is completed, the rescued program must then be saved at once by the operator or the program rescued will not show the proper icon and the list window may show garbage when entered from the Desktop. (I still don't know the reason for the latter. ) At any rate, when 'Rescue(b)' has run, it leaves the rescued program loaded in memory, so you can simply do a "Save As" and your program will be fully recovered.

Ideas on the Encryption Used

The encryption method involves three steps; First, the binary value from each field is added to an offset in the range 0 to 255. If the sum is greater than 255, then 256 is subtracted so as to be in the proper range. Next, a mask in the same range is operated on the value using the logical XOR operand. Finally a second, different offset is subtracted from the result. The final binary code is truncated to the proper range and written to the file. (Note that, properly written, the same routine can be used to encode and decode the binary data.) Each position has its own offsets and masks which are calculated in some manner I couldn't determine, but these parameters are always the same at a given position, regardless of the program encrypted.

Since {Merge "It":'} is overwritten at the beginning of the file being rescued, it is best to use a copy of the same for the rescue. I added a separate, temporary file to store the values that were overwritten. These are later restored by the subroutine "Rescue" and the result is saved with the program code in the file: 'filename.IMOK'.

Using Rescue(b)

The programs work with files saved in both version 2.0 and 2.1 but must be run under the same version of BASIC. Files in version 2.0 are not saved properly when run under version 2.1 and presumably version 2.0 will not load version 2.1 files. If you have protected version 2.0 programs and don't have a backup of BASIC version 2.0, you're out of luck!

The program 'Rescue(b)' as listed below will rescue any Binary Basic programs. To rescue Decimal programs, change the 'MSBC' to 'MSBB' where indicated in the source code and save the modified Rescue program as a Decimal program file (ie: 'Rescue(d)'.) The second program listed below, called 'It', need only be written once and saved as a text file, but it must have the name "It" and both the 'Rescue' program and the 'It' program must reside on the default disk that has the Basic interpreter on it.

To use the program, make a copy of a Basic program and save it in protected mode. Then run the program 'Rescue(b)'. A standard file dialog box will come up and ask for the name of your protected program. Then the selected program will be loaded into memory, the 'It' text program will be read in and merged to the protected program, and the de-protection will take place (allow a minute or two for this, nothing will happen on the screen yet.) When done, another dialog box will come up indicating the process is done. At this point, the unprotected program will be in memory. Go to the File menu and do a "Save As" under a new file name, then quit Basic and try running and listing your new program. It should both run and list normally.

Well, that's about it. I have successfully rescued about a dozen different programs I previously protected, both Binary and Decimal, and in both version 2.0 and 2.1. It works properly on a skinny Mac, so presumably it will work on the bigger machines. The Program 'It' searches for the binary code for 'Merge "It":' in memory, so it shouldn't matter what size your Mac is.

Too bad the folks at Microsoft didn't employ a Password scheme to allow program recovery, then all of this wouldn't have been necessary! As a precaution against disgruntled vendors, I am publishing this article under a pen name. Don't try to find me, as my name suggests, I'm gone!

'<<< RESCUE PROTECTED MICROSOFT® BASIC FILES.
'<<< This program must be run under the same BASIC
'<<< type & version number as the file being recovered.
'<<< Keep the file named 'It' on the drive with basic. 
'<<< Open temporary file to store values
OPEN "Garbage Bag" AS #1 LEN=174
FIELD #1,128 AS N$,2 AS I$,4 AS T$,2 AS L$,36 AS K$,2 AS B$
WINDOW 1,,(50,30)-(212,46),4
CALL TEXTFONT(0)
DIM Keep%(18)
'<<< Ask for protected Program to rescue.
PRINT "Select a file to rescue...";
ID$=FILES$(1,"MSBD")
'<<< (Use "MSBP" instead for decimal program.)
IF ID$="" THEN SYSTEM
WINDOW CLOSE 1
OPEN ID$ AS #2 LEN=2
FIELD #2,2 AS H$
'<<< Determine version and set type.
'<<< Decode length field of first Program line.
GET #2,1:Type$="MSBC"
Pre%=VAL("&H"+LEFT$(HEX$(CVI(H$)),2))+1
IF Pre%=247 OR Pre%=251 THEN Type$="MSBB"
RESTORE FirstLine
  GET #2,2
  Code%=VAL("&H"+MID$(RIGHT$("0"+HEX$(CVI(H$)),4),1,2))
  READ Offset1%,Mask%,Offset2%
  GOSUB Cryptor
Keep%(0)=Bite%
'<<< Decode and store positions 5-22 of Program
RESTORE OtherLines
Total%=Keep%(0):I%=2
WHILE I%<20
  Place%=INT(I%/2)+2
  GET #2,Place%
    Char%=2*I%-4*Place%+9
 Code%=VAL("&H"+MID$(RIGHT$("0"+HEX$(CVI(H$)),4),Char%,2))
    READ Offset1%,Mask%,Offset2%
  GOSUB Cryptor
  Keep%(I%-1)=Bite%
    IF I%=Total% AND I%<14 THEN Total%=Total%+Bite%
  I%=I%+1
WEND
'<<< Write coded {MERGE "It":' } on Program
RESTORE Modify
FOR I%=1 TO 5
  READ Convert%
  LSET H$=MKI$(Convert%)
  PUT #2,I%+2
NEXT I%
'<<< Recode length of new first line
'<<< Note Offsets are reversed for recoding
RESTORE FirstLine:Code%=Total%
READ Offset2%,Mask%,Offset1%
GOSUB Cryptor
GET #2,2:A$=RIGHT$("00"+HEX$(CVI(H$)),4)
  MID$(A$,1,2)=RIGHT$("0"+HEX$(Bite%),2)
  LSET H$=MKI$(VAL("&H"+A$))
  PUT #2,2
CLOSE #2
NAME ID$ AS ID$,"TEXT"
'<<< Save old values to temporary file
  FOR I%=1 TO 18
    Kp$=Kp$+RIGHT$("0"+HEX$(Keep%(I%)),2)
  NEXT I%
  LSET N$=ID$
  LSET I$=MKI$(LEN(ID$))
  LSET T$=Type$
  LSET L$=MKI$(Keep%(0))
  LSET K$=Kp$
  LSET B$=MKI$(Pre%)
PUT #1,1
CLOSE #1
'<<< Print message, then chain to Program.
'<<< The file 'It' will then be merged to
'<<< the end of the Program.
WINDOW 1,,(160,110)-(345,174),2
CALL TEXTFONT(0)
PRINT "Now merging the file 'It' to"
PRINT "'"+ID$+"'"
PRINT "Please enter 'Rescue' in the"
PRINT "Command Window...";
CHAIN ID$
'<<< Routine to code/decode protected file
Cryptor:
  Code%=Code%+Offset1%+256
  Code%=Code%-256*INT(Code%/256)
  Bite%=Code% XOR Mask%
  Bite%=Bite%-Offset2%+256
  Bite%=Bite%-256*INT(Bite%/256)
RETURN
FirstLine:
  DATA 118,27,244
OtherLines:
  DATA 248,36,246,249,18,247,250,81,248
  DATA 251,59,249,252,87,250,253,44,251
  DATA 254,211,252,255,66,253,5,132,142
  DATA 22,106,223,55,82,179,56,214,180
  DATA 57,164,181,0,0,252,0,0,252
  DATA 60,164,184,61,108,185,62,209,186
Modify:
  DATA &HD28E,&H4F25,&H1846,&HCF76,&H47CE


REM}|{ Required marker for end of listing!!!
'<<< Type into Basic and save on same drive, 
'<<< as Basic, as 'It', in Text  format.
SUB Rescue STATIC
CLEAR:Peekloc!=45000!
DIM Keep%(18)
PRINT:PRINT "Rescue engaged!"
'<<< Find location of {MERGE "It":} in memory
ScanMem:
  Peekloc!=Peekloc!+1
  IF PEEK(Peekloc!)<>248 GOTO ScanMem
  Flag%=0:RESTORE Peekvals
  FOR I=1 TO 7
    READ J%:IF PEEK(Peekloc!+I)<>J% THEN Flag%=1
  NEXT I
  IF Flag%=1 AND Peekloc!<127*1024 GOTO ScanMem
Peekloc!=Peekloc!-4:Begin1!=Peekloc!:Begin2!=Peekloc!
Peekvals:
  DATA &H9E,&H20,&H22,&H49,&H74,&H22,&H3A
'<<< Recover decoded values and parameters
OPEN "Garbage Bag" AS #1 LEN=174
FIELD #1,128 AS N$,2 AS I$,4 AS T$,2 AS L$,36 AS k$,2 AS 
  GET #1,1
  ID$=N$:Length%=CVI(I$)
  Type$=T$:Keep%(0)=CVI(L$)
  Kp$=k$:Pre%=CVI(B$)
CLOSE #1
KILL "Garbage Bag"
'<<< Write beginning of file
ID$=LEFT$(ID$,Length%)+".IMOK"
OPEN ID$ FOR OUTPUT AS #1
  PRINT #1,CHR$(Pre%);
  PRINT #1,CHR$(PEEK(Peekloc!+1));
  PRINT #1,CHR$(Keep%(0));
  PRINT #1,CHR$(PEEK(Peekloc!+3));
  FOR I%=1 TO 18
    Keep%(I%)=VAL("&H"+MID$(Kp$,2*I%-1,2))
    PRINT #1,CHR$(Keep%(I%));
  NEXT I%
Peekloc!=Peekloc!+22:I%=0
'<<< Copy file until }|{ marker is reached, then
'<<< skip to rescue5 if Program is version 2.0
rescue1:
  PRINT #1,CHR$(PEEK(Peekloc!));
  Peekloc!=Peekloc!+1
  IF PEEK(Peekloc!+2)<>&HAF THEN GOTO rescue1
  Flag%=1
  IF PEEK(Peekloc!+3)<>ASC("}") THEN Flag%=0
  IF PEEK(Peekloc!+4)<>ASC("|") THEN Flag%=0
  IF PEEK(Peekloc!+5)<>ASC("{") THEN Flag%=0
IF Flag%=0 GOTO rescue1
PRINT #1,CHR$(0);CHR$(0);
IF Pre%>&HF7 GOTO rescue5
'<<< Make listing an Odd length
Begin1!=Peekloc!-Begin1!
Begin1!=Begin1!-1000*INT(Begin1!/1000)
IF (1 AND INT(Begin1!))=0 THEN PRINT #1,CHR$(0);
'<<< Find end of variable names using: TenZZZ...Z
'<<< This var MUST NOT appear anywhere else:
TenZZZZZZZZZZ=0
rescue3:
  Peekloc!=Peekloc!+1
  IF PEEK(Peekloc!)<>ASC("Z") GOTO rescue3
  Flag%=1
  FOR I%=1 TO 9
    IF PEEK(Peekloc!+I%)<>ASC("Z") THEN Flag%=0
  NEXT I%
IF Flag%=0 GOTO rescue3
J=Peekloc!+10
'<<< Backtrack to start of variables list.
rescue4:
  Peekloc!=Peekloc!-1
  IF PEEK(Peekloc!-1)<>&H0 GOTO rescue4
J=J-Peekloc!-4
'<<< If total listing is odd, skip first position.
Begin2!=Peekloc!-Begin2!
Begin2!=Begin2!-1000*INT(Begin2!/1000)
IF (1 AND INT(Begin2!))=0 THEN Peekloc!=Peekloc!+1
'<<< Print Variables table, EXCEPT: TenZ...ZZ
WHILE J>1
  PRINT #1,CHR$(PEEK(Peekloc!));
  Peekloc!=Peekloc!+1:J=J-1
WEND
rescue5:
PRINT "Voil"+CHR$(136)+"! Rescue Completed!"
  CLOSE #1
  NAME ID$ AS ID$,Type$
PRINT "Now loading into memory."
PRINT "Recommend you first use"
PRINT "'Save As' to save file...";
BEEP:FOR I%=1 TO 20000:NEXT I%
LOAD ID$
END SUB
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

The secrets of Penacony might soon come...
Version 2.2 of Honkai: Star Rail is on the horizon and brings the culmination of the Penacony adventure after quite the escalation in the latest story quests. To help you through this new expansion is the introduction of two powerful new... | Read more »
The Legend of Heroes: Trails of Cold Ste...
I adore game series that have connecting lore and stories, which of course means the Legend of Heroes is very dear to me, Trails lore has been building for two decades. Excitedly, the next stage is upon us as Userjoy has announced the upcoming... | Read more »
Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »

Price Scanner via MacPrices.net

Apple’s 13-inch M2 MacBook Airs return to rec...
Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices currently... Read more
Best Buy is clearing out iPad Airs for up to...
In advance of next week’s probably release of new and updated iPad Airs, Best Buy has 10.9″ M1 WiFi iPad Airs on record-low sale prices for up to $200 off Apple’s MSRP, starting at $399. Sale prices... Read more
Every version of Apple Pencil is on sale toda...
Best Buy has all Apple Pencils on sale today for $79, ranging up to 39% off MSRP for some models. Sale prices for online orders only, in-store prices may vary. Order online and choose free shipping... Read more
Sunday Sale: Apple Studio Display with Standa...
Amazon has the standard-glass Apple Studio Display on sale for $300 off MSRP for a limited time. Shipping is free: – Studio Display (Standard glass): $1299.97 $300 off MSRP For the latest prices and... Read more
Apple is offering significant discounts on 16...
Apple has a full line of 16″ M3 Pro and M3 Max MacBook Pros available, Certified Refurbished, starting at $2119 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free... Read more
Apple HomePods on sale for $30-$50 off MSRP t...
Best Buy is offering a $30-$50 discount on Apple HomePods this weekend on their online store. The HomePod mini is on sale for $69.99, $30 off MSRP, while Best Buy has the full-size HomePod on sale... Read more
Limited-time sale: 13-inch M3 MacBook Airs fo...
Amazon has the base 13″ M3 MacBook Air (8GB/256GB) in stock and on sale for a limited time for $989 shipped. That’s $110 off MSRP, and it’s the lowest price we’ve seen so far for an M3-powered... Read more
13-inch M2 MacBook Airs in stock today at App...
Apple has 13″ M2 MacBook Airs available for only $849 today in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty is included,... Read more
New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more
The latest Apple iPhone deals from wireless c...
We’ve updated our iPhone Price Tracker with the latest carrier deals on Apple’s iPhone 15 family of smartphones as well as previous models including the iPhone 14, 13, 12, 11, and SE. Use our price... Read more

Jobs Board

Licensed Practical Nurse - Womens Imaging *A...
Licensed Practical Nurse - Womens Imaging Apple Hill - PRN Location: York Hospital, York, PA Schedule: PRN/Per Diem Sign-On Bonus Eligible Remote/Hybrid Regular Read more
DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
Operating Room Assistant - *Apple* Hill Sur...
Operating Room Assistant - Apple Hill Surgical Center - Day Location: WellSpan Health, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.