home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
CMDS
/
modbust.lzh
/
MODBUSTER
/
modbuster.c
< prev
Wrap
C/C++ Source or Header
|
1994-10-11
|
15KB
|
679 lines
/* ModBuster.c - split merged modules (IE: OS9Boot) into seperate modules
**
** (C) Copyright 1993 HyperTech Software / Mike Haaland
**
*/
/* Includes, defines */
#include <stdio.h>
#include <ctype.h>
extern errno;
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
#define SEEK_BEG 0
#define SEEK_CUR 1
#define SEEK_END 2
#define SAME 0
/* OSK modules */
#define SYNC 0x4AFC
#define SYNC1 0x4A
#define OFFSET 16
/* CoCo modules */
#define CSYNC 0x87CD
#define CSYNC1 0x87
#define COFFSET 6
/* OSK ROF (.r) */
#define RSYNC 0xDEADFACE
#define RSYNC1 0xDE
#define ROFFSET 0x38
/* KWindows/Coco bitmaps */
#define BITMP 0x1b
int buffersize = (1024 * 16);
int verbose = FALSE;
int quiet = FALSE;
char *buffer = NULL;
char *dirname = NULL;
char flname[100];
char name[256];
static char sccs[] = "(C) Copyright 1993 HyperTech Software / Mike Haaland";
/* Main call */
char *usgmsg[] = {
"Syntax: Modbuster [<opts>] <path> [<opts>]\n",
#ifdef OSK
"Function: Seperate merged modules, rels, basic source or KWindows bitmaps\n",
#else
"Function: Seperate merged modules, basic source or Multi-Vue bitmaps\n",
#endif
"Options:\n",
" -b=<size> buffer size\n",
" -q quiet/silent mode\n",
" -v verbose\n",
" -w=<dir> seperate into <dir>\n",
" (default = current work directory)\n",
NULL
};
usage()
{
int i = 0;
while(usgmsg[i])
fputs(usgmsg[i++],stderr);
exit(0);
}
main(argc,argv)
int argc;
char *argv[];
{
char *scan, **filename;
int file_count, argn, i, bsize;
if (argc < 2)
usage();
file_count = 0;
for (i = 1; i < argc ; i++ )
if (argv[i][0] != '-')
file_count++;
if (file_count)
if ((filename = (char **)malloc(sizeof(char *) * file_count)) == NULL)
exit(_errmsg(errno,"Can't allocate memory - too many filenames"));
file_count = 0;
for (i = 1; i < argc ; i++ ) {
if (argv[i][0] == '-') {
scan = &argv[i][1];
while (*scan) {
switch(toupper(*scan)) {
case 'B' :
bsize = atoi(&scan[(scan[1] == '=') ? 2 : 1]) * 1024;
if (bsize > buffersize)
buffersize = bsize;
scan[1] = NULL;
break;
case 'Q' :
quiet = TRUE;
break;
case 'V' :
verbose = TRUE;
break;
case 'W' :
dirname = &scan[(scan[1] == '=') ? 2 : 1];
*scan-- = NULL;
break;
case '?' :
usage();
break;
default :
exit(_errmsg(0,"Unknown option '-%c'\n",*scan));
}
scan++;
}
}
else
filename[file_count++] = argv[i];
}
if ( ! file_count )
exit(_errmsg(216,"No filename given!\n"));
if ((buffer = (char *)malloc(buffersize + 2)) == NULL)
exit(_errmsg(errno,"Can't allocate buffer\n"));
argn = 0;
while (argn < file_count)
process(filename[argn++]);
}
process(fname)
char *fname;
{
FILE *fp;
unsigned char ch;
if ((fp = fopen(fname,"r")) == NULL)
exit(_errmsg(errno,"Error opening '%s'\n",fname));
if ((ch = fgetc(fp)) != EOF) {
fseek(fp,0L,SEEK_BEG);
switch(ch & 0xFF) {
#ifdef OSK
case RSYNC1 :
do_rels(fp);
break;
#endif
case CSYNC1 :
do_modules(fp,0);
break;
case SYNC1 :
do_modules(fp,1);
break;
case BITMP :
do_bitmaps(fp);
break;
default :
do_basic(fp);
}
}
fclose(fp);
return(0);
}
#ifdef OSK
struct r_header {
unsigned long rsync;
short lang_type;
short at_rv;
short v_asm;
short series;
char time[6];
short edition;
long stat_size;
long init_size;
long obj_size;
long stack_size;
long entry_off;
long tentry_off;
long rstat_size;
long rinit_size;
long dbg_size;
} r_hdr;
/* Split OSK Relocatable Object Files (.r) */
do_rels(fp)
FILE *fp;
{
FILE *ofp;
int size, length, i;
char c, junk[80];
char rname[40];
int rnamelen;
char *ptr;
short num;
long goback;
unsigned long sync;
while (TRUE) {
sync = 0;
fread(&sync,1,sizeof(long),fp);
if (sync != RSYNC)
return(0);
fseek(fp,-4,SEEK_CUR);
fread(&r_hdr,1,sizeof(struct r_header),fp);
i = 0;
do {
c = fgetc(fp);
name[i++] = c;
}
while (isprint(c));
strcpy(rname,name);
rnamelen = i;
name[--i] = name[i] & 0x7f;
name[++i] = '\0';
if ((ptr = (char *)rindex(name,'_')) != NULL && strlen(ptr) < 3)
strcpy(ptr,".r");
else
strcat(name,".r");
if (dirname)
sprintf(flname,"%s/%s",dirname,name);
else
strcpy(flname,name);
if ((ofp = fopen(flname,"w")) == NULL)
fprintf(stderr,"Can't create %s - Error %03d\n", flname,errno);
if (ofp != NULL)
if (! quiet)
if (verbose)
fprintf(stderr,"ROF: %s\n",name);
else
fprintf(stderr,"%s\n",name);
fread(&num,1,sizeof(short),fp);
if (ofp != NULL)
{
fwrite(&r_hdr,1,sizeof(r_hdr),ofp);
fwrite(rname,1,rnamelen,ofp);
fwrite(&num,1,sizeof(short),ofp);
}
#ifdef DEBUG
fprintf(stderr,"%d external definitions\n",num);
fprintf(stderr,"so read %d bytes\n",num * 6);
#endif
do_extdefs(fp,ofp,num);
length = r_hdr.obj_size + r_hdr.init_size +
r_hdr.rinit_size + r_hdr.dbg_size;
#ifdef DEBUG
fprintf(stderr,"%d bytes to be copied raw\n",length);
#endif
do {
if (length > buffersize) {
size = buffersize;
length -= buffersize;
}
else {
size = length;
length = 0;
}
fread(buffer,1,size,fp);
if (ofp != NULL)
fwrite(buffer,1,size,ofp);
} while (length);
fread(&num,1,sizeof(short),fp);
if (ofp != NULL)
fwrite(&num,1,sizeof(short),ofp);
#ifdef DEBUG
fprintf(stderr,"%d external references\n",num);
#endif
do_extrefs(fp,ofp,num);
fread(&num,1,sizeof(short),fp);
if (ofp != NULL)
fwrite(&num,1,sizeof(short),ofp);
#ifdef DEBUG
fprintf(stderr,"%d local references\n",num);
#endif
do_locrefs(fp,ofp,num);
/* All RELs have 16 nulls of padding at the end */
fread(junk,1,16,fp);
if (ofp != NULL)
fwrite(junk,1,16,ofp);
if (ofp != NULL)
fclose(ofp);
}
}
do_extdefs(fp,ofp,num)
FILE *fp, *ofp;
short num;
{
int i;
char junk[6];
char c;
while (num) {
i = 0;
do {
c = fgetc(fp);
name[i++] = c;
} while (isprint(c));
name[i] = '\0';
#ifdef DEBUG
fprintf(stderr,"ext. def: %s\n",name);
#endif
fread(junk,1,6,fp);
if (ofp != NULL)
{
fwrite(name,1,strlen(name) + 1,ofp);
fwrite(junk,1,6,ofp);
}
num--;
}
}
do_extrefs(fp,ofp,num)
FILE *fp, *ofp;
short num;
{
int i;
char c;
short cnt;
while (num)
{
i = 0;
do {
c = fgetc(fp);
name[i++] = c;
} while (isprint(c));
name[i] = '\0';
fread(&cnt,1,sizeof(short),fp);
#ifdef DEBUG
fprintf(stderr,"ext. ref: %s\n",name);
#endif
if (ofp != NULL)
{
fwrite(name,1,strlen(name) + 1,ofp);
fwrite(&cnt,1,sizeof(short),ofp);
}
do_locrefs(fp,ofp,cnt);
num--;
}
}
do_locrefs(fp,ofp,num)
FILE *fp, *ofp;
short num;
{
char junk[6];
#ifdef DEBUG
fprintf(stderr,"%d refs, so read %d bytes\n",num, num * 6);
#endif
while (num) {
fread(junk,1,6,fp);
if (ofp != NULL)
fwrite(junk,1,6,ofp);
num--;
}
}
#endif
/* Split Binary Object files (Modules) */
do_modules(fp,osk)
FILE *fp;
int osk;
{
FILE *ofp;
int size, length, noffset, i;
char c, junk[4];
short sync;
long goback;
while (TRUE) {
fread(&sync,1,sizeof(short),fp);
if (osk)
{
if (sync != SYNC)
return(0);
fread(junk,2,sizeof(char),fp);
fread(&length,1,sizeof(int),fp);
fread(junk,4,sizeof(char),fp);
fread(&noffset,1,sizeof(int),fp);
goback = noffset;
fseek(fp,noffset - OFFSET,SEEK_CUR);
}
else
{
if (sync != CSYNC)
return(0);
fread(&length,1,sizeof(int),fp);
fread(&noffset,1,sizeof(int),fp);
goback = noffset;
fseek(fp,noffset - COFFSET,SEEK_CUR);
}
i = 0;
do {
name[i++] = c = fgetc(fp);
goback++;
}
while (isprint(c));
name[--i] = name[i] & 0x7f;
name[++i] = '\0';
fseek(fp,-goback,SEEK_CUR);
if (dirname)
sprintf(flname,"%s/%s",dirname,name);
else
strcpy(flname,name);
if ((ofp = fopen(flname,"w")) == NULL)
fprintf(stderr,"Can't create %s - Error %03d\n", flname,errno);
if (ofp != NULL)
if (! quiet)
if (verbose)
fprintf(stderr,"Module: %s\nLength: %d $%X\n",
name,length,length);
else
fprintf(stderr,"%s\n",name);
do {
if (length > buffersize) {
size = buffersize;
length -= buffersize;
}
else {
size = length;
length = 0;
}
fread(buffer,1,size,fp);
if (ofp != NULL)
fwrite(buffer,1,size,ofp);
} while (length);
if (ofp != NULL)
fclose(ofp);
}
}
/* Split Microware Basic source */
do_basic(fp)
FILE *fp;
{
FILE *ofp;
char c, *p, line[BUFSIZ];
ofp = NULL;
while (fgets(line,255,fp) != NULL) {
if (strncmp(line,"PROCEDURE ",10) == SAME) {
if (ofp != NULL)
fclose(ofp);
strcpy(name,&line[10]);
if (p = (char *)index(name,'\n'))
*p = '\0';
if (dirname)
sprintf(flname,"%s/%s",dirname,name);
else
strcpy(flname,name);
if ((ofp = fopen(flname,"w")) == NULL)
fprintf(stderr,"Can't create %s - Error #%03d\n",flname,errno);
if (ofp != NULL)
if (! quiet)
if (verbose)
fprintf(stderr,"%s\n",line);
else
fprintf(stderr,"%s\n",name);
}
if (ofp != NULL)
fputs(line,ofp);
}
if (ofp != NULL)
fclose(ofp);
return(0);
}
typedef struct {
short ID;
unsigned char misc[9];
} BITMAPHEAD;
BITMAPHEAD bm;
#define grp misc[0]
#define buf misc[1]
#define sty misc[2]
#define bm_xsiz ((bm.misc[3] << 8) | bm.misc[4])
#define bm_ysiz ((bm.misc[5] << 8) | bm.misc[6])
#define bm_blen ((bm.misc[7] << 8) | bm.misc[8])
#define GRP_FONT 200
#define GRP_PTR16 201
#define GRP_PTR256 202
#define GRP_PAT2 203
#define GRP_PAT4 204
#define GRP_PAT16 205
/* Non CoCo group numbers */
#define GRP_PAT256 206
#define GRP_BRSH2 207
#define GRP_BRSH4 208
#define GRP_BRSH16 209
#define GRP_BRSH256 210
/* Merged Kwindows/Multi-view bitmaps */
do_bitmaps(fp)
FILE *fp;
{
FILE *ofp;
int size, length;
while (fread(&bm,sizeof(char),11,fp)) {
if (bm.ID != 0x1B2B) {
fprintf(stderr,"Got sync of 0x%04X\n",bm.ID);
return(0);
}
length = bm_blen;
switch(bm.grp) {
case GRP_FONT :
sprintf(name,"font%02X_%02X.fnt",bm.grp,bm.buf);
break;
case GRP_PTR16 :
sprintf(name,"stdptr16_%02X.ptr",bm.buf);
break;
case GRP_PTR256 :
sprintf(name,"stdptr256_%02X.ptr",bm.buf);
break;
case GRP_PAT2 :
sprintf(name,"stdpat2_%02X.pat",bm.buf);
break;
case GRP_PAT4 :
sprintf(name,"stdpat4_%02X.pat",bm.buf);
break;
case GRP_PAT16 :
sprintf(name,"stdpat16_%02X.pat",bm.buf);
break;
case GRP_PAT256 :
sprintf(name,"stdpat256_%02X.pat",bm.buf);
break;
case GRP_BRSH2 :
sprintf(name,"stdbrsh2_%02X.brsh",bm.buf);
break;
case GRP_BRSH4 :
sprintf(name,"stdbrsh4_%02X.brsh",bm.buf);
break;
case GRP_BRSH16 :
sprintf(name,"stdbrsh16_%02X.brsh",bm.buf);
break;
case GRP_BRSH256:
sprintf(name,"stdbrsh256_%02X.brsh",bm.buf);
break;
default :
sprintf(name,"misc%dc_%02X_%02X.buf",getsty(bm.sty),bm.grp,bm.buf);
}
if (dirname)
sprintf(flname,"%s/%s",dirname,name);
else
strcpy(flname,name);
if ((ofp = fopen(flname,"w")) == NULL)
fprintf(stderr,"Can't create %s - Error %03d\n", flname,errno);
if (ofp != NULL) {
fwrite(&bm,sizeof(char),11,ofp);
if (! quiet)
if (verbose) {
fprintf(stderr,"Name: %s\n",name);
fprintf(stderr,"Grp/Buf: $%02X/$%02X\n",bm.grp,bm.buf);
fprintf(stderr,"Size: %d x %d\n",bm_xsiz,bm_ysiz);
fprintf(stderr,"STY: %02d\n",bm.sty);
fprintf(stderr,"Colors: %d\n",getsty(bm.sty));
fprintf(stderr,"Length: %d $%X\n\n",length,length);
} else
fprintf(stderr,"%s\n",name);
}
do {
if (length > buffersize) {
size = buffersize;
length -= buffersize;
}
else {
size = length;
length = 0;
}
fread(buffer,1,size,fp);
if (ofp != NULL)
fwrite(buffer,1,size,ofp);
} while (length);
if (ofp != NULL)
fclose(ofp);
bm.ID = 0;
}
return(0);
}
getsty(styp)
char styp;
{
int colors;
switch (styp) {
case 0: /* Kwindow fonts */
colors = 2;
break;
case 3: /* Kwindow bitmaps */
colors = 256;
break;
case 4: /* Coco bitmaps */
case 5:
colors = 2;
break;
case 6: /* Coco bitmaps */
case 7:
colors = 4;
break;
case 8: /* Coco or Kwindow bitmaps */
colors = 16;
break;
default:
colors = 0;
}
return (colors);
}
/* End */