Advertisement
Xeeynamo

KH 0.2 PAK

Dec 8th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstdint>
  4. #include <cassert>
  5.  
  6. struct Lba {
  7.     uint64_t offset;
  8.     uint64_t size;
  9.     uint64_t uncompressedSize;
  10.     uint32_t compressionMethod;
  11.     uint32_t checksum[5]; // SHA-1
  12.     uint8_t dummy[5];
  13. };
  14.  
  15. uint64_t align(uint64_t offset, uint64_t alignment) {
  16.     return offset - (offset - ((offset + alignment - 1) / alignment) * alignment);
  17. }
  18.  
  19. int main() {
  20.     char fileName[0x100];
  21.     int bufferLen = 0x100000;
  22.     void *buffer = malloc(bufferLen);
  23.  
  24.     FILE *f = fopen("tresgame-ps4.pak", "rb");
  25.     assert(f != NULL);
  26.  
  27.     int index = 0;
  28.     uint64_t pos = 0;
  29.     Lba lba;
  30.     while (fread(&lba, 0x35, 1, f) > 0) {
  31.         printf("[%3i, %09X] ", index++, pos);
  32.         if (lba.compressionMethod == 0 &&
  33.             lba.dummy[0] == 0 &&
  34.             lba.dummy[1] == 0 &&
  35.             lba.dummy[2] == 0 &&
  36.             lba.dummy[3] == 0 &&
  37.             lba.dummy[4] == 0) {
  38.  
  39.             if (bufferLen < lba.size) {
  40.                 bufferLen = lba.size;
  41.                 free(buffer);
  42.                 buffer = malloc(bufferLen);
  43.             }
  44.             fread(buffer, lba.size, 1, f);
  45.  
  46.             const char *ext = "bin";
  47.             // guess format
  48.             if (*(uint8_t*)buffer == '{')
  49.                 ext = "json";
  50.             else if (*(uint32_t*)buffer == 2653586369)
  51.                 ext = "upk";
  52.             snprintf(fileName, sizeof(fileName), "out/%i.%s", index, ext);
  53.             FILE *fOut = fopen(fileName, "wb");
  54.             assert(fOut != NULL);
  55.             fwrite(buffer, lba.size, 1, fOut);
  56.             fclose(fOut);
  57.         }
  58.  
  59.         uint64_t blockSize = align(lba.size, 0x10000);
  60.         if (blockSize == 0)
  61.             blockSize = 0x10000;
  62.         pos += blockSize;
  63.         _fseeki64(f, pos, SEEK_SET);
  64.         printf("\n");
  65.     }
  66.     free(buffer);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement