TweetFollow Us on Twitter

Rescue
Volume Number:1
Issue Number:10
Column Tag:Special Projects

"Rescue that Protected Basic Program!"

By Mike Steiner, Sierra Vista, AZ., MacTutor Contributing Editor

In my diggings into Microsoft BASIC, I found some interesting things about how a program is stored on disk. My ultimate aim is to be able to “crack” a protected BASIC program; I once lost a program because I had saved it as protected and did not have an unprotected back-up copy.

The information in this column is valid only for BASIC Version 2.0; the discussion and program are not valid for Version 2.1. Most of the data presented in ‘MS BASIC Tokens Explained’ in the July 1985 Mactutor do not apply to Version 2.1. Also, the program is for a 512K Macintosh. Change the starting memory location (Peekloc) for a 128K or 1024K Macintosh as described in ‘MS BASIC Tokens Explained.’

Each version of BASIC (Binary and Decimal) is coded so that the finder treats them as two different creators. This, I assume, is because of the incompatabilities between the two versions. See last month's discussion of how numbers are stored in the two versions.

Fig. 1 How Rescuit Works

Table 1 shows the creators, file types, and ID bytes of the various files used by Microsoft BASIC version 2.0. (See sidebar for a brief explanation of creators and file-tpes.) The four-letter codes in parentheses on the top line of the table are the identification of the creators of the files. The four-letter codes in the table show the file types. The hexadecimal numbers in parentheses show the first byte (ID byte) of the files. TEXT files do not have an ID byte.

Once a file is loaded, the ID byte tells BASIC whether the file is protected. TEXT files do not need an ID byte because BASIC knows it is a text file and loads it in as is, translating reserved words into their hexadecimal tokens. BASIC can load any text file and treat it as a program listing (sometimes with curious results if the file is not a program). Each version (binary or decimal) can load compressed and protected files that were created only by itself. However, you can use SetFile to change the creator ID to load a file from the ‘other’ BASIC. If you do this, I recommend changing the ID byte with a disk zap program such as fEdit. Keep in mind that if there are any non-integer numbers defined in the program listing, their format will not be proper for the ‘new’ creator.

Protected programs, in addition to being having a unique TYPE and ID byte, are encyphered. It appears that this encyphering in only in the Macintosh version of Microsoft BASIC; friends with CP/M and MS DOS versions have not encrypted files with their versions of Microsoft BASIC. I have not yet been able to decypher these files; I have however, devised an interesting solution!

Following is a discussion of how you can prevent losing access to protected programs. The method discussed will not work on programs already protected; it will, however, give you a ‘back door’ into future programs. It works because when a protected program is loaded, it is decyphered and resides in memory as if it had been loaded from a text or compressed file. However, LIST, PEEK, and a slew of other commands are disabled in the command window. These commands have not been disabled from running from within the program, and that is what allows us to recover the file.

Listing 1 is a subprogram called "rescueit" which may be appended to any program prior to saving it as protected. Save rescueit as a TEXT file and MERGE it to your program. To use it, type the name of the subprogram in the command window, answer one question, and wait until it says that it is finished. (The name of the subprogram is in effect a password, so I strongly recommend that you change it to a name that only you know; otherwise anyone who knows it can recover your program.) Your program will be saved as a file called “Ressurected.”

Rescueit asks whether you are using Binary or Decimal BASIC. It then opens a file called “res” for output and then PRINTs one byte (the ID byte for a compressed program in the version of BASIC you are using) to the file. It then PEEKs the program in memory and PRINTs it to res, one byte at a time. It then PRINTs five zeroes at the end of the file. Read ‘MS BASIC Tokens Explained’ to see why this is necessary. After closing res, rescueit renames “res” as “Ressurected” and changes the filetype from TEXT to MSBC or MSBB, as appropriate. Then it prints “finished..” on the screen and exits to the desktop. Calling a sub-program from the command window sometimes scrambles pointers and causes memory problems. By exiting to the finder, these problems are avoided. The next step is to re-enter BASIC, load “Ressurected” using the Standard File and save the program. The program is now saved in compressed mode, ready to run or be edited. This last step sets the creator. If this step is omitted, the program will still load and run, but only from within BASIC; the finder will not recongnize an application to run the program if it is double-clicked, and the file will have the generic document icon. The only difference between ressurected and the original program is that ressurected has an extra blank line at the end of the listing. If you understand how BASIC stores programs in memory, you should be able to figure out why this is so. The reason is printed in a question / answer session at the end of this article.

Use of the program is fairly straightforward. Assuming that you already have rescuit on your disk (Remember to save it as a text file, not as a compressed program.), write your program and save it as a text or compressed file. (Don't forget to always keep a backup file!) In the command window, type “Merge "rescueit".” This will cause rescueit to be appended to the end of your program. Then choose “Save As ” from the File menu. Save your program (with rescueit appended) as a protected program. Now, if you ever lose your backup and need to recover the protected file, load BASIC and open the protected file. Within the command window, type resuceit (or the name to which you renamed the sub-program). The routine will execute, and when it ends will tell you that it is finished and return to the desktop. Next, set the creator as described above. Whenever you use rescueit, start with the Macintosh turned off. As discussed in ‘MS BASIC Tokens Explained,’ if another application has been run before BASIC, sometimes BASIC programs do not load at the same memory location. Rescueit always assumes that the program loads at the same point. Illustration 1 is a brief schematic of the rescueit process.

File Types and Creator Bytes

File-type and creator (also called file-type and creator flags) are four-letter abbreviations that show the file type and which application generated the file. An application (i.e. a program that can be run by double-clicking it's icon on the desktop) is a file that always has the file-type APPL. All other files are known as ‘Documents’ and may have any file-type that the programmer chooses.

Applications without a defined icon use the generic application icon (the diamond-shaped paper with a hand writing on it). Documents without a defined icon use the generic document icon (a piece of blank paper with the upper right corner folded down).

The finder uses the file type and creator flags to know which document files can be loaded by a given application. A document is linked to it's creator by the creator flag. When a document and a program have the same creator, the finder knows to launch that program when you double click on the document icon; once the program is launched, the finder tells it to open the document. The file type flag has a number of purposes. The most common is to assign an icon to the file. All the desk-top icons used by a program are stored in it's ICN# resource. The finder uses the type and creator flags to assign the correct icon to the file. (However, for ducument files, a bit in the application called the bundle bit must be set to 1; otherwise, the link is not established.) Also, the Standard File (which contains the list of documents that you can open when you choose Open from the File menu within a program) uses the file type to determine which files may be opened. These files are not restricted to those created by the application; for example, Microsoft Word can open files created by Apple's Macwrite. However, you cannot open a Macwrite document with Word by double-clicking the document icon; you can only do it from within Word via the Standard File.

Two interesting features of Microsoft BASIC are that you can specify file types when loading a file from within a program by use of the FILE$(1) function; by using the NAME command, you can change the file-type of a file on the disk. Rescueit makes use of this latter feature to change the rescued program from a TEXT file to a BASIC compressed file; this step is necessary because the file is generated in the compressed format, but saved as a TEXT file.

So far as I have been able to determine, there is no way for a Microsoft BASIC program to determine which version of BASIC it is using. The prefix byte exists only on the disk, and is not stored in memory; FILES$(1) can read the file type, but not the creator; NAME likewise can change only the file type and name. Does anyone know how to read or set the creator of a file from within MS BASIC? MacTutor will pay $50 to the first submission that shows how to do it.

Table 1

Microsoft BASIC 2.0 Creator and File Type Codes

Mode Binary (MSBB) Decimal (MSBA)

Text TEXT TEXT

Compressed MSBC (F9) MSBB (FB)

Protected MSBD (F8) MSBP (FA)

Listing 1 - Rescueit

REM }|{ Marker for end of main program - don't change!!
SUB rescueit STATIC: REM Change the name “rescueit” of sub-program for 
password protection

prefixbyte(1) = &HF9:prefixbyte(2) = &HFB
filetype$(1) = "MSBC":filetype$(2) = "MSBB"
Peekloc(1) = 66999!: Peekloc(2) = 77001!

WINDOW 1,,(100,100)-(400,200),2
TEXTFONT 0
PRINT "  Which version of BASIC are you using?"
TEXTFONT 1
BUTTON 1,1,"Binary",(35,40)-(95,65)
BUTTON 2,1,"Decimal",(170,40)-(240,65)
WHILE DIALOG (0)<>1:WEND
buttonpressed = DIALOG (1)
Peekloc = Peekloc(buttonpressed)

WINDOW 1,"rescuit",(2,39)-(508,338),1
OPEN "res" FOR OUTPUT AS #1
PRINT#1,CHR$(prefixbyte(buttonpressed));

rescue1:
peekloc = peekloc +1
IF PEEK (peekloc) = &HAF THEN IF PEEK  (peekloc+1) = &H20 AND PEEK (peekloc+2) 
 = ASC ("}") AND PEEK (peekloc+3) = ASC      ("|") AND PEEK (peekloc+4 
) = ASC ("{")  THEN rescue2: REM Find marker
PRINT #1, CHR$ (PEEK (peekloc));
GOTO rescue1

rescue2:
FOR j = 1 TO 4
 PRINT #1,CHR$(&H0);
NEXT j
CLOSE #1
NAME "res" AS "Resurrected", filetype$(buttonpressed)

CALL TEXTSIZE (127)
PRINT: PRINT "Finished..";
SYSTEM
END SUB

Q: "Why does rescuit add a line feed to the end of a file?"

A: When rescueit (or any other file) is merged to a program, it is appended to the end of the program, starting on a new line. This generates a carriage return after the last line of the program. Then when rescueit runs, it looks for the REM line that starts the rescueit routine, and saves the program up to the last byte before the REM. This last byte is the carriage return that was inserted when rescueit was merged. A carriage return at the end of a program is treated by BASIC as a blank line.

For What It's Worth Dept.

Peter Wollschlaeger of Hildeseim, West Germany, sent in this little gem. Try it, you'll like it! Peter says "...the most information I ever got for a Mac programmer like me in Macworld was an ad to subscribe to MacTutor!" Peter also reports that trying to run the MDS Editor under Finder version 3.3 bombs. Update to 4.1 Peter. Then your only problem will be missing icons occasionally! (See top column right.)

program Bugged;
var
 r :  rect;
 left, top, right, bottom : integer;
 icon1, icon2, width :  integer;
begin
 left := 10;
 top := 10;
 right := 40;
 bottom := 40;
 width := right - left + 10;
 icon1 := 1;
 icon2 := 257;

 showdrawing;
 setrect(r, left, top, right, bottom);
 ploticon(r, geticon(icon1));
 setrect(r, left + width, top, right + width, bottom);
 ploticon(r, geticon(icon2));
end.
 
AAPL
$111.78
Apple Inc.
-0.87
MSFT
$47.66
Microsoft Corpora
+0.14
GOOG
$516.35
Google Inc.
+5.25

MacTech Search:
Community Search:

Software Updates via MacUpdate

LibreOffice 4.3.5.2 - Free Open Source o...
LibreOffice is an office suite (word processor, spreadsheet, presentations, drawing tool) compatible with other major office suites. The Document Foundation is coordinating development and... Read more
CleanApp 5.0.0 Beta 5 - Application dein...
CleanApp is an application deinstaller and archiver.... Your hard drive gets fuller day by day, but do you know why? CleanApp 5 provides you with insights how to reclaim disk space. There are... Read more
Monolingual 1.6.2 - Remove unwanted OS X...
Monolingual is a program for removing unnecesary language resources from OS X, in order to reclaim several hundred megabytes of disk space. It requires a 64-bit capable Intel-based Mac and at least... Read more
NetShade 6.1 - Browse privately using an...
NetShade is an Internet security tool that conceals your IP address on the web. NetShade routes your Web connection through either a public anonymous proxy server, or one of NetShade's own dedicated... Read more
calibre 2.13 - Complete e-library manage...
Calibre is a complete e-book library manager. Organize your collection, convert your books to multiple formats, and sync with all of your devices. Let Calibre be your multi-tasking digital librarian... Read more
Mellel 3.3.7 - Powerful word processor w...
Mellel is the leading word processor for OS X and has been widely considered the industry standard since its inception. Mellel focuses on writers and scholars for technical writing and multilingual... Read more
ScreenFlow 5.0.1 - Create screen recordi...
Save 10% with the exclusive MacUpdate coupon code: AFMacUpdate10 Buy now! ScreenFlow is powerful, easy-to-use screencasting software for the Mac. With ScreenFlow you can record the contents of your... Read more
Simon 4.0 - Monitor changes and crashes...
Simon monitors websites and alerts you of crashes and changes. Select pages to monitor, choose your alert options, and customize your settings. Simon does the rest. Keep a watchful eye on your... Read more
BBEdit 11.0.2 - Powerful text and HTML e...
BBEdit is the leading professional HTML and text editor for the Mac. Specifically crafted in response to the needs of Web authors and software developers, this award-winning product provides a... Read more
ExpanDrive 4.2.1 - Access cloud storage...
ExpanDrive builds cloud storage in every application, acts just like a USB drive plugged into your Mac. With ExpanDrive, you can securely access any remote file server directly from the Finder or... Read more

Latest Forum Discussions

See All

Make your own Tribez Figures (and More)...
Make your own Tribez Figures (and More) with Toyze Posted by Jessica Fisher on December 19th, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
So Many Holiday iOS Sales Oh My Goodness...
The holiday season is in full-swing, which means a whole lot of iOS apps and games are going on sale. A bunch already have, in fact. Naturally this means we’re putting together a hand-picked list of the best discounts and sales we can find in order... | Read more »
It’s Bird vs. Bird in the New PvP Mode f...
It’s Bird vs. Bird in the New PvP Mode for Angry Birds Epic Posted by Jessica Fisher on December 19th, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
Telltale Games and Mojang Announce Minec...
Telltale Games and Mojang Announce Minecraft: Story Mode – A Telltale Games Series Posted by Jessica Fisher on December 19th, 2014 [ permalink ] | Read more »
WarChest and Splash Damage Annouce Their...
WarChest and Splash Damage Annouce Their New Game: Tempo Posted by Jessica Fisher on December 19th, 2014 [ permalink ] WarChest Ltd and Splash Damage Ltd are teaming up again to work | Read more »
BulkyPix Celebrates its 6th Anniversary...
BulkyPix Celebrates its 6th Anniversary with a Bunch of Free Games Posted by Jessica Fisher on December 19th, 2014 [ permalink ] BulkyPix has | Read more »
Indulge in Japanese cuisine in Cooking F...
Indulge in Japanese cuisine in Cooking Fever’s new sushi-themed update Posted by Simon Reed on December 19th, 2014 [ permalink ] Lithuanian developer Nordcurrent has yet again updated its restaurant simulat | Read more »
Badland Daydream Level Pack Arrives to C...
Badland Daydream Level Pack Arrives to Celebrate 20 Million Downloads Posted by Ellis Spice on December 19th, 2014 [ permalink ] | Read more »
Far Cry 4, Assassin’s Creed Unity, Desti...
Far Cry 4, Assassin’s Creed Unity, Destiny, and Beyond – AppSpy Takes a Look at AAA Companion Apps Posted by Rob Rich on December 19th, 2014 [ permalink ] These day | Read more »
A Bunch of Halfbrick Games Are Going Fre...
A Bunch of Halfbrick Games Are Going Free for the Holidays Posted by Ellis Spice on December 19th, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »

Price Scanner via MacPrices.net

The Apple Store offering free next-day shippi...
The Apple Store is now offering free next-day shipping on all in stock items if ordered before 12/23/14 at 10:00am PT. Local store pickup is also available within an hour of ordering for any in stock... Read more
It’s 1992 Again At Sony Pictures, Except For...
Techcrunch’s John Biggs interviewed a Sony Pictures Entertainment (SPE) employee, who quite understandably wished to remain anonymous, regarding post-hack conditions in SPE’s L.A office, explaining “... Read more
Holiday sales this weekend: MacBook Pros for...
 B&H Photo has new MacBook Pros on sale for up to $300 off MSRP as part of their Holiday pricing. Shipping is free, and B&H charges NY sales tax only: - 15″ 2.2GHz Retina MacBook Pro: $1699... Read more
Holiday sales this weekend: MacBook Airs for...
B&H Photo has 2014 MacBook Airs on sale for up to $120 off MSRP, for a limited time, for the Thanksgiving/Christmas Holiday shopping season. Shipping is free, and B&H charges NY sales tax... Read more
Holiday sales this weekend: iMacs for up to $...
B&H Photo has 21″ and 27″ iMacs on sale for up to $200 off MSRP including free shipping plus NY sales tax only. B&H will also include a free copy of Parallels Desktop software: - 21″ 1.4GHz... Read more
Holiday sales this weekend: Mac minis availab...
B&H Photo has new 2014 Mac minis on sale for up to $80 off MSRP. Shipping is free, and B&H charges NY sales tax only: - 1.4GHz Mac mini: $459 $40 off MSRP - 2.6GHz Mac mini: $629 $70 off MSRP... Read more
Holiday sales this weekend: Mac Pros for up t...
B&H Photo has Mac Pros on sale for up to $500 off MSRP. Shipping is free, and B&H charges sales tax in NY only: - 3.7GHz 4-core Mac Pro: $2599, $400 off MSRP - 3.5GHz 6-core Mac Pro: $3499, $... Read more
Save up to $400 on MacBooks with Apple Certif...
The Apple Store has Apple Certified Refurbished 2014 MacBook Pros and MacBook Airs available for up to $400 off the cost of new models. An Apple one-year warranty is included with each model, and... Read more
Save up to $300 on Macs, $30 on iPads with Ap...
Purchase a new Mac or iPad at The Apple Store for Education and take up to $300 off MSRP. All teachers, students, and staff of any educational institution qualify for the discount. Shipping is free,... Read more
iOS and Android OS Targeted by Man-in-the-Mid...
Cloud services security provider Akamai Technologies, Inc. has released, through the company’s Prolexic Security Engineering & Research Team (PLXsert), a new cybersecurity threat advisory. The... Read more

Jobs Board

*Apple* Store Leader Program (US) - Apple, I...
…Summary Learn and grow as you explore the art of leadership at the Apple Store. You'll master our retail business inside and out through training, hands-on experience, Read more
Project Manager, *Apple* Financial Services...
**Job Summary** Apple Financial Services (AFS) offers consumers, businesses and educational institutions ways to finance Apple purchases. We work with national and Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* Retail - Multiple Positions (US) - A...
Job Description: Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.