home *** CD-ROM | disk | FTP | other *** search
/ Phenomenon / Phenomenon.iso / quake / utils / pak / qhack_pc / hack.cpp next >
Encoding:
C/C++ Source or Header  |  1996-02-24  |  2.4 KB  |  115 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <conio.h>
  5.  
  6. typedef struct {
  7.     char name[56];
  8.   unsigned long offset, length;
  9. } dirrec;  //64 bytes per entry
  10.  
  11. dirrec dir[500];   //32000 bytes, prolly
  12. char buf[10000];
  13.  
  14. unsigned long dir_start, num_ents;
  15.  
  16. void main(int argc, char *argv[])
  17. {
  18.     FILE *in, *f;
  19.   long l;
  20.   int i, i1, i2, i3, i4;
  21.   char s[80], c;
  22.  
  23.   strcpy(s,"G:\\QUAKE\\ID1.PAK");
  24.   if (argc>1)
  25.       strcpy(s,argv[1]);
  26.   in=fopen(s,"rb");
  27.   if (!in)
  28.   {
  29.       printf("Error opening \"%s\"",s);
  30.     return;
  31.   }
  32.   fread((void *)s, 4, 1, in); //"PACK"
  33.   fread((void *)&dir_start, 4, 1, in);
  34.   fread((void *)&num_ents, 4, 1, in);
  35.   num_ents/=64L;
  36.     fseek(in,dir_start,SEEK_SET);
  37.  
  38.   if (num_ents>=500L)
  39.   {
  40.       printf("Too many entries!\n");
  41.     fclose(in);
  42.     return;
  43.   }
  44.  
  45.   fread((void *)dir, sizeof(dirrec), num_ents, in);
  46.  
  47.   i=0;
  48.   while (1)
  49.   {
  50.       printf("%d: \"%s\"\n",i,dir[i].name);
  51.     printf("Length: %ld\n",dir[i].length);
  52.     printf("\nWhat? ");
  53.     c=toupper(getche());
  54.     printf("\n\n");
  55.     switch(c)
  56.     {
  57.         case '[':
  58.       case '-':
  59.           if (i==0)
  60.             i=num_ents-1;
  61.         else
  62.             i--;
  63.         break;
  64.       case ']':
  65.       case '+':
  66.       case '=':
  67.           i++;
  68.         if (i==num_ents)
  69.             i=0;
  70.         break;
  71.       case 's':
  72.       case 'S':
  73.           for (i1=strlen(dir[i].name)-1; i1>0; i1--)
  74.             if (dir[i].name[i1]=='/')
  75.               break;
  76.         i1++;
  77.         if (!strchr(dir[i].name,'/'))
  78.             i1=0;
  79.         strcpy(s,&dir[i].name[i1]);
  80.         printf("Saving \"%s\"...\n",s);
  81.         f=fopen(s,"wb");
  82.         if (!f)
  83.         {
  84.             printf("Error opening \"%s\"!\n",s);
  85.           break;
  86.         }
  87.         fseek(in,dir[i].offset,SEEK_SET);
  88.         i4=dir[i].length/9999;
  89.         if (dir[i].length%9999)
  90.             i4++;
  91.           for (i2=0;i2<i4;i2++)
  92.           {
  93.             fread((void *)buf,1,9999,in);
  94.           l=dir[i].length - (long)i2 * 9999L;
  95.           if (l>9999L)
  96.               l=9999;
  97.           i3=l;
  98.           fwrite((void *)buf,1,i3,f);
  99.         }
  100.         fclose(f);
  101.           break;
  102.       case 'q':
  103.       case 'Q':
  104.       case 27:
  105.           fclose(in);
  106.           return;
  107.       case '?':
  108.           printf("[/-: Previous entry\n");
  109.         printf("]/+: Next entry\n");
  110.         printf(" S : Save entry to file\n");
  111.         printf(" Q : Quit\n");
  112.         break;
  113.     }
  114.   }
  115. }