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

Fresh From the Land Down Under – The Tou...
After a two week hiatus, we are back with another episode of The TouchArcade Show. Eli is fresh off his trip to Australia, which according to him is very similar to America but more upside down. Also kangaroos all over. Other topics this week... | Read more »
TouchArcade Game of the Week: ‘Dungeon T...
I’m a little conflicted on this week’s pick. Pretty much everyone knows the legend of Dungeon Raid, the match-3 RPG hybrid that took the world by storm way back in 2011. Everyone at the time was obsessed with it, but for whatever reason the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for July 19th, 2024. In today’s article, we finish up the week with the unusual appearance of a review. I’ve spent my time with Hot Lap Racing, and I’m ready to give my verdict. After... | Read more »
Draknek Interview: Alan Hazelden on Thin...
Ever since I played my first release from Draknek & Friends years ago, I knew I wanted to sit down with Alan Hazelden and chat about the team, puzzle games, and much more. | Read more »
The Latest ‘Marvel Snap’ OTA Update Buff...
I don’t know about all of you, my fellow Marvel Snap (Free) players, but these days when I see a balance update I find myself clenching my… teeth and bracing for the impact to my decks. They’ve been pretty spicy of late, after all. How will the... | Read more »
‘Honkai Star Rail’ Version 2.4 “Finest D...
HoYoverse just announced the Honkai Star Rail (Free) version 2.4 “Finest Duel Under the Pristine Blue" update alongside a surprising collaboration. Honkai Star Rail 2.4 follows the 2.3 “Farewell, Penacony" update. Read about that here. | Read more »
‘Vampire Survivors+’ on Apple Arcade Wil...
Earlier this month, Apple revealed that poncle’s excellent Vampire Survivors+ () would be heading to Apple Arcade as a new App Store Great. I reached out to poncle to check in on the DLC for Vampire Survivors+ because only the first two DLCs were... | Read more »
Homerun Clash 2: Legends Derby opens for...
Since launching in 2018, Homerun Clash has performed admirably for HAEGIN, racking up 12 million players all eager to prove they could be the next baseball champions. Well, the title will soon be up for grabs again, as Homerun Clash 2: Legends... | Read more »
‘Neverness to Everness’ Is a Free To Pla...
Perfect World Games and Hotta Studio (Tower of Fantasy) announced a new free to play open world RPG in the form of Neverness to Everness a few days ago (via Gematsu). Neverness to Everness has an urban setting, and the two reveal trailers for it... | Read more »
Meditative Puzzler ‘Ouros’ Coming to iOS...
Ouros is a mediative puzzle game from developer Michael Kamm that launched on PC just a couple of months back, and today it has been revealed that the title is now heading to iOS and Android devices next month. Which is good news I say because this... | Read more »

Price Scanner via MacPrices.net

Amazon is still selling 16-inch MacBook Pros...
Prime Day in July is over, but Amazon is still selling 16-inch Apple MacBook Pros for $500-$600 off MSRP. Shipping is free. These are the lowest prices available this weekend for new 16″ Apple... Read more
Walmart continues to sell clearance 13-inch M...
Walmart continues to offer clearance, but new, Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBooks... Read more
Apple is offering steep discounts, up to $600...
Apple has standard-configuration 16″ M3 Max MacBook Pros available, Certified Refurbished, starting at $2969 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free,... Read more
Save up to $480 with these 14-inch M3 Pro/M3...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
Amazon has clearance 9th-generation WiFi iPad...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Apple is offering a $50 discount on 2nd-gener...
Apple has Certified Refurbished White and Midnight HomePods available for $249, Certified Refurbished. That’s $50 off MSRP and the lowest price currently available for a full-size Apple HomePod today... Read more
The latest MacBook Pro sale at Amazon: 16-inc...
Amazon is offering instant discounts on 16″ M3 Pro and 16″ M3 Max MacBook Pros ranging up to $400 off MSRP as part of their early July 4th sale. Shipping is free. These are the lowest prices... Read more
14-inch M3 Pro MacBook Pros with 36GB of RAM...
B&H Photo has 14″ M3 Pro MacBook Pros with 36GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 Pro MacBook Pro (... Read more
14-inch M3 MacBook Pros with 16GB of RAM on s...
B&H Photo has 14″ M3 MacBook Pros with 16GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $150-$200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 MacBook Pro (... Read more
Amazon is offering $170-$200 discounts on new...
Amazon is offering a $170-$200 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1129 for models with 8GB of RAM and 256GB of storage: – 15″ M3... Read more

Jobs Board

*Apple* Systems Engineer - Chenega Corporati...
…LLC,** a **Chenega Professional Services** ' company, is looking for a ** Apple Systems Engineer** to support the Information Technology Operations and Maintenance 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
*Apple* / Mac Administrator - JAMF Pro - Ame...
Amentum is seeking an ** Apple / Mac Administrator - JAMF Pro** to provide support with the Apple Ecosystem to include hardware and software to join our team and Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.