Guest User

Untitled

a guest
Apr 14th, 2012
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.80 KB | None | 0 0
  1. /* ungbfs.c
  2.    dump a GBFS file to the current directory
  3.  
  4. Copyright (C) 2002  Damian Yerrick
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to
  18.   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19.   Boston, MA  02111-1307, USA.
  20. GNU licenses can be viewed online at http://www.gnu.org/copyleft/
  21.  
  22. In addition, as a special exception, Damian Yerrick gives
  23. permission to link the code of this program with import libraries
  24. distributed as part of the plug-in development tools published by
  25. the vendor of any image manipulation program, and distribute
  26. linked combinations including the two.  You must obey the GNU
  27. General Public License in all respects for all of the code used
  28. other than the plug-in interface.  If you modify this file, you
  29. may extend this exception to your version of the file, but you
  30. are not obligated to do so.  If you do not wish to do so, delete
  31. this exception statement from your version.
  32.  
  33. Visit http://www.pineight.com/ for more information.
  34.  
  35. */
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40.  
  41. #define strequ(a,b) (!strcmp(a,b))
  42.  
  43. typedef unsigned short u16;
  44. typedef unsigned long u32;
  45.  
  46. #include "../gbfs.h"
  47.  
  48. /* fgeti16() ***************************
  49.    read a 16-bit integer in intel format from a file
  50. */
  51. u32 fgeti16(FILE *fp)
  52. {
  53.   return ((fgetc(fp) & 0x000000ff)      ) |
  54.          ((fgetc(fp) & 0x000000ff) <<  8);
  55. }
  56.  
  57.  
  58. /* fgeti32() ***************************
  59.    read a 32-bit integer in intel format from a file
  60. */
  61. u32 fgeti32(FILE *fp)
  62. {
  63.   return ((fgetc(fp) & 0x000000ff)      ) |
  64.          ((fgetc(fp) & 0x000000ff) <<  8) |
  65.          ((fgetc(fp) & 0x000000ff) << 16) |
  66.          ((fgetc(fp) & 0x000000ff) << 24);
  67. }
  68.  
  69.  
  70. /* fncpy() *****************************
  71.    copy n bytes from one file to the other
  72. */
  73. void fncpy(FILE *dst, FILE *src, unsigned long n)
  74. {
  75.   char buf[16384];
  76.  
  77.   while(n > 0)
  78.   {
  79.     unsigned long n_copy = n;
  80.  
  81.     if(n_copy > 16384)
  82.       n_copy = 16384;
  83.     fread(buf, 1, n_copy, src);
  84.     fwrite(buf, 1, n_copy, dst);
  85.     n -= n_copy;
  86.   }
  87. }
  88.  
  89.  
  90. int main(int argc, char **argv)
  91. {
  92.   FILE *fp;
  93.   char filename[32] = {0};
  94.   u32 dir_off, dir_nmemb;
  95.   unsigned int i;
  96.  
  97.   if(argc != 2 || strequ("-h", argv[1]) || strequ("--help", argv[1]))
  98.   {
  99.     fputs("dumps the objects in a gbfs file to separate files\n"
  100.           "syntax: ungbfs FILE\n", stderr);
  101.     return 1;
  102.   }
  103.  
  104.   fp = fopen(argv[1], "rb");
  105.  
  106.   if(!fp)
  107.   {
  108.     fputs("could not open ", stderr);
  109.     perror(argv[1]);
  110.     return 1;
  111.   }
  112.  
  113.   /* read pertinent header fields */
  114.   fseek(fp, 20, SEEK_SET);
  115.  
  116.   dir_off = fgeti16(fp);
  117.   dir_nmemb = fgeti16(fp);
  118.  
  119.   for(i = 0; i < dir_nmemb; i++)
  120.   {
  121.     unsigned long len, off;
  122.     FILE *outfile;
  123.  
  124.     fseek(fp, dir_off + 32 * i, SEEK_SET);
  125.     fread(filename, 24, 1, fp);
  126.     len = fgeti32(fp);
  127.     off = fgeti32(fp);
  128.     printf("%10lu %s\n", (unsigned long)len, filename);
  129.  
  130.     outfile = fopen(filename, "wb");
  131.     if(!outfile)
  132.     {
  133.       fputs("could not open ", stderr);
  134.       perror(filename);
  135.       fclose(fp);
  136.       return 1;
  137.     }
  138.     fseek(fp, off, SEEK_SET);
  139.     fncpy(outfile, fp, len);
  140.     fclose(outfile);
  141.   }
  142.  
  143.   printf("Execution Complete || Members: %d || Offset: %d || File: %s\n", dir_nmemb, dir_off, argv[1]);
  144.   fclose(fp);
  145.  
  146.   return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment