Guest User

WDF Extractor

a guest
Mar 14th, 2010
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <direct.h>
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6.  
  7.  
  8. void decode(unsigned char *p, int h)
  9. {
  10.     for(int a=0;a<h;a++)
  11.     {
  12.         p[a]^=(unsigned char)194;
  13.         p[a]=(p[a]<<4)|(p[a]>>4);
  14.     }
  15. }
  16.  
  17. int main(int argc, char* argv[])
  18. {
  19.     if (!argv[1])
  20.     {
  21.         printf("Usage: wdfextract <WDFFile>\n");
  22.         return 1;
  23.         //argv[1] = "env.wdf";
  24.     }
  25.  
  26.     char fname[256];
  27.     FILE *f1, *f2;
  28.     char *p;
  29.     int *fsizes, *startpos;
  30.     int a,b,n,h;
  31.  
  32.     string dir = ".//";
  33.     dir += argv[1];
  34.  
  35.     dir.replace(dir.size() - 4,4,"");
  36.  
  37.     const char* cdir = dir.c_str();
  38.  
  39.     mkdir(cdir);
  40.     chdir(cdir);
  41.  
  42.     string file = "..//";
  43.     file += argv[1];
  44.  
  45.     const char* cfile = file.c_str();
  46.  
  47.     f1=fopen(cfile, "rb");
  48.  
  49.     fseek(f1, 4, SEEK_SET);
  50.     fread(&n, 1, 4, f1);    //number of files
  51.     fsizes=new int[n];
  52.     startpos=new int[n];
  53.    
  54.     fread(&a, 1, 4, f1);    //offset of file table
  55.     fseek(f1, a, SEEK_SET);
  56.  
  57.     h=0;
  58.     for(a=0;a<n;a++)
  59.     {
  60.         fread(&b, 1, 4, f1);
  61.         fread(&startpos[a], 1, 4, f1);
  62.         fread(&fsizes[a], 1, 4, f1);
  63.         fread(&b, 1, 4, f1);
  64.         h+=fsizes[a];
  65.     }
  66.  
  67.     char *guess[12]={"DAT","dds", "wav", "lnd", "voq", "tga", "bmp", "sp", "jpg", "rt", "mp3", "LND"};
  68.    
  69.     cout << "\n";
  70.  
  71.     for(a=0;a<n;a++)
  72.     {
  73.         fseek(f1, startpos[a], SEEK_SET);
  74.         fread(&b, 1, 4, f1);
  75.         switch(b)
  76.         {
  77.             case 0x20534444: b=1; break;    //DDS
  78.             case 0x46464952: b=2; break;    //WAVE
  79.             case 0xd736a713: b=4; break;    //voq
  80.             case 0x00020000: b=5; break;    //tga
  81.             case 0x65636152: b=9; break;    //
  82.             case 0xe0ffd8ff: b=8; break;    //
  83.             default:
  84.                 if((b&0xffff)==0x4d42)b=6;
  85.                 else if((b&0xffff)==0x5053)b=7;
  86.                 else if((b&0xffff)==0x4449)b=10;
  87.                 else if((b&0xffff)==0xfbff)b=10;
  88.                 else if((b&0xff)==2)b=3;
  89.                 else if((b&0xff)==1)b=11;   //sometimes its lnd, sometimes its not...
  90.                 else
  91.                 {
  92.                     b=0;
  93.                     //printf("unknown type %d %c\n", (int)ch, ch);
  94.                 }
  95.         };
  96.        
  97.         sprintf(fname, "F%d.%s", a, guess[b]);
  98.  
  99.         for (int i=0; i<argc; i++)
  100.         {
  101.             //cout << " " << argv[i];
  102.             if (argv[i] == "-v")
  103.             {
  104.                 cout << "Extracting: " << fname << "\n";
  105.             }
  106.             else
  107.             {
  108.                 cout << "Arg:" << argv[i] << "\n";
  109.             }
  110.         }
  111.  
  112.         f2=fopen(fname, "w+b");
  113.         p=new char[fsizes[a]];
  114.         fseek(f1, startpos[a], SEEK_SET);
  115.         fread(p, 1, fsizes[a], f1);
  116.         if(b==4)decode((unsigned char*)p, fsizes[a]);
  117.         fwrite(p, 1, fsizes[a], f2);
  118.         delete[] p;
  119.         fclose(f2);
  120.     }
  121.  
  122.     fclose(f1);
  123.     delete[] fsizes;
  124.     delete[] startpos;
  125.     printf("%d files\n%d bytes\n", n, h);
  126.  
  127.     return 0;
  128. };
Advertisement
Add Comment
Please, Sign In to add comment