utils

smash_megafile.c

Go to the documentation of this file.
00001 /* smash_megafile.c:
00002  *
00003  *     Break a Viewlogic metafile into a million little pieces
00004  *
00005  *
00006  * Copyright (C) 1998-2010 Mike Jarabek
00007  *
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.     
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  *
00022  *  $Id$
00023  *
00024  */
00025 
00026 #ifdef HAVE_CONFIG_H
00027 #include "config.h"
00028 #endif
00029 
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <sys/stat.h>
00034 
00035 #ifdef HAVE_LIBDMALLOC
00036 #include <dmalloc.h>
00037 #endif
00038 
00039 #define RECLEN 0x14
00040 
00041 
00042 int main(int argc, char **argv)
00043 {
00044 
00045   FILE *megafile, *output;
00046   char *extracted_file;
00047   char name[127];
00048   char buffer[127];         /* buffer for megafile index entries */
00049   char output_name[127];
00050 
00051   int len;
00052 
00053   if( argc != 2 )
00054     {
00055       fprintf( stderr, "Usage:\n %s <megafile>\n\n"
00056            "Where <megafile> is the name of a viewlogic megafile\n"
00057            "whithout any extensions.  The file <megafile>.lib and \n"
00058            "<megafile>.tbl must exist in the same directory\n",
00059            argv[0]);
00060       return 1;
00061     }
00062 
00063 
00064 
00065   /* open the files */
00066   strcpy(name,argv[1]);
00067   strcat(name,".lib");
00068   megafile = fopen(name, "r");
00069 
00070   if( megafile == NULL )
00071     {
00072       fprintf(stderr, "Error: unable to open magefile `%s' for reading\n",
00073           name);
00074       return(1);
00075     }
00076 
00077   /* create a subdir to hold the exploded files */
00078   mkdir(argv[1], 0777);    /* try to be friendly */
00079   
00080 
00081   /* read each table entry and extract the file from the megafile */
00082   while(!feof(megafile))
00083     {
00084       if(fread(buffer, RECLEN, 1, megafile) == 0) break;  /* end of file? */
00085 
00086       /* null terminate buffer */
00087       buffer[RECLEN+1] = 0;
00088 
00089       /*printf("%s\n",buffer);*/
00090 
00091       /* extract the name and size from the entry */
00092       sscanf(buffer,"%s %d",name,&len);
00093 
00094       printf("%s:%d\n",name,len);
00095 
00096       /* slurp in the required data and spit it out into the 
00097        * output directory
00098        */
00099 
00100       /* allocate some memory to hold the file */
00101       extracted_file = malloc(len);
00102 
00103       fread(extracted_file, len, 1, megafile);
00104 
00105       /* open up a file to dump in */
00106       strcpy(output_name, argv[1]);
00107       strcat(output_name, "/");
00108       strcat(output_name, name);
00109       output = fopen(output_name,"wb");
00110       if(output == NULL)
00111     {
00112       fclose(megafile);
00113       fprintf(stderr,"Error: unable to open file `%s' for writing\n",
00114           output_name);
00115       
00116       return 1;
00117     }
00118 
00119       /* dump to the file */
00120       fwrite(extracted_file, len, 1, output);
00121       fclose(output);
00122 
00123       /* and get the ^Z */
00124       fgetc(megafile);
00125 
00126     }
00127 
00128 
00129   fclose(megafile);
00130 
00131   return 0;
00132 }
00133 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines