Advertisement
Xeeynamo

Final Fantasy VII (WINDOW.BIN)

Sep 28th, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.34 KB | None | 0 0
  1. /*
  2. FF7 .bin packer/unpacker
  3. Developed by Xeeynamo
  4.  
  5. The extracted files are GZip deflate files. To recompress them with a format
  6. that the game accepts, compress them back to .gz format, remove the header (it
  7. usually finish with the name of the file with NULL terminating character) and
  8. replace it with the original header used from the game: 1F8B 0800 0000 0000 0003
  9.  
  10. I tried this on ./INIT/WINDOW.BIN, but I'm sure that it works also for
  11. KERNEL.BIN, SCENE.BIN etc.
  12. */
  13.  
  14. #include <stdio.h>
  15. #define MODE_REPACK
  16.  
  17. size_t CalcFileSize(FILE* f)
  18. {
  19.     size_t prevPos = ftell(f);
  20.     size_t fileSize;
  21.     fseek(f, 0, SEEK_END);
  22.     fileSize = ftell(f);
  23.     fseek(f, prevPos, SEEK_SET);
  24.     return fileSize;
  25. }
  26. int FileToMemory(const char* filename, void** memory, size_t* size)
  27. {
  28.     FILE* f = fopen(filename, "r+b");
  29.     if (f == NULL) return 0;
  30.     *memory = malloc( *size = CalcFileSize(f) );
  31.     fread(*memory, *size, 1, f);
  32.     fclose(f);
  33.     return 1;
  34. }
  35.  
  36. int main()
  37. {
  38.     typedef struct
  39.     {
  40.         unsigned short compressedSize;
  41.         unsigned short uncompressedSize;
  42.         unsigned short fileType;
  43.     } Header;
  44.  
  45.     int i;
  46.     char tmpStr[0x100];
  47.     Header* header[256];
  48.     FILE* fOut;
  49.     size_t fileSize;
  50.     FILE* fGz;
  51.     size_t fileGzSize;
  52.     void* data;
  53.     void* datagz;
  54.  
  55.     if (FileToMemory("WINDOW.BIN", &data, &fileSize) == 0)
  56.         return -1;
  57.  
  58. #ifdef MODE_REPACK
  59.     fOut = fopen("WINDOW.BIN", "w+b");
  60. #endif // MODE_REPACK
  61.  
  62.     // Parse data
  63.     header[0] = (Header*)data;
  64.     for(i = 0; header[i]->compressedSize != 0; i++)
  65.     {
  66.         header[i + 1] = (char*)header[i] +
  67.             header[i]->compressedSize + sizeof(Header);
  68.  
  69.         sprintf(tmpStr, "WINDOW_%i.BIN.GZ", i);
  70. #ifdef MODE_REPACK
  71.         if (FileToMemory(tmpStr, &datagz, &fileGzSize))
  72.         {
  73.             header[i]->compressedSize = fileGzSize;
  74.             fwrite(header[i], sizeof(Header), 1, fOut);
  75.             fwrite(datagz, fileGzSize, 1, fOut);
  76.             free(datagz);
  77.         }
  78. #else
  79.         fGz = fopen(tmpStr, "w+b");
  80.         fwrite(((char*)header[i]) + sizeof(Header),
  81.         header[i]->compressedSize, 1, fGz);
  82.         fclose(fGz);
  83.  
  84. #endif // MODE_REPACK
  85.     }
  86. #ifdef MODE_REPACK
  87.     short empty = 0;
  88.     fwrite(&empty, 2, 1, fOut);
  89.     fclose(fOut);
  90. #endif // MODE_REPACK
  91.  
  92.     free(data);
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement