Advertisement
xerpi

ps4pupextractor v2 by xerpi

Oct 29th, 2013
699
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.21 KB | None | 0 0
  1. // Copyright (c) 2013   xerpi
  2.  
  3. /*
  4.     Fast and simple PS4 PUP extractor
  5.     Thanks to SKFU for the PUP information analysis
  6.     Version 2, may have lots of bugs (coded fast)
  7.     I'm not even sure this will work on Big Endian machines...
  8.  
  9.     Compiling:
  10.         gcc -o ps4pupextractor ps4pupextractor.c
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stdint.h>
  16. #include <string.h>
  17.  
  18. #define PS4_PUP_MAGIC        0x32424C53
  19. #define PS4_PUP_HEADER_SIZE  32 //Until PUP entries
  20. #define PS4_PUP_ENTRY_SIZE   48 //PUP entry size
  21.  
  22. struct ps4pup_pup_info {
  23.     uint32_t offset;
  24.     uint32_t content_size;
  25.     uint64_t reserved;
  26.     uint8_t  filename[32];
  27. } __attribute__((packed));
  28.  
  29. struct ps4pup_header {
  30.     uint32_t magic;
  31.     uint64_t version;
  32.     uint32_t file_count;
  33.     uint32_t block_count;
  34.     uint8_t  reserved1[12];
  35.     struct ps4pup_pup_info *pups;
  36.     uint8_t  reserved2[288];
  37. } __attribute__((packed));
  38.  
  39.  
  40. int  ps4pup_read_header(FILE *fd, struct ps4pup_header *header);
  41. void ps4pup_free_header(struct ps4pup_header *header);
  42. int  ps4pup_extract(FILE *fd, struct ps4pup_header *header);
  43. void ps4pup_print_header(const struct ps4pup_header *header);
  44.  
  45. void print_usage(void);
  46.  
  47. int main (int argc, char *argv[])
  48. {
  49.     if (argc < 2) {
  50.         print_usage();
  51.         goto exit_error;
  52.     }
  53.    
  54.     FILE *fd;
  55.     if ((fd = fopen(argv[1], "rb")) == NULL ) {
  56.         printf ("Could not open %s\n", argv[1]);
  57.         goto exit_close;
  58.     }
  59.    
  60.     struct ps4pup_header h;
  61.    
  62.     if (!ps4pup_read_header(fd, &h)) {
  63.         printf("Error reading PUP file\n");
  64.         goto exit_close;
  65.     }
  66.    
  67.     ps4pup_print_header(&h);
  68.    
  69.     printf("\nExtracting PUP files...\n");
  70.    
  71.     if (!ps4pup_extract(fd, &h)) {
  72.         printf("Error extracting PUP files\n");
  73.         ps4pup_free_header(&h);
  74.         goto exit_close;
  75.     }
  76.    
  77.     printf("Done!\n");
  78.    
  79.    
  80.     ps4pup_free_header(&h);
  81.     return 1;
  82.    
  83.    
  84. exit_close:
  85.     fclose(fd);
  86. exit_error:
  87.     return EXIT_FAILURE;
  88. }
  89.  
  90. int ps4pup_read_header(FILE *fd, struct ps4pup_header *header)
  91. {
  92.     if (fd == NULL || header == NULL) {
  93.         return 0;
  94.     }
  95.    
  96.     fseek(fd, 0, SEEK_SET);
  97.     fread((void*)header, 1, PS4_PUP_HEADER_SIZE, fd);
  98.    
  99.     if (header->magic != PS4_PUP_MAGIC) {
  100.         printf("This is not a PUP file!\n");
  101.         return 0;
  102.     }
  103.    
  104.     header->pups = malloc (header->file_count * sizeof(struct ps4pup_pup_info));
  105.    
  106.     int i;
  107.     for (i = 0; i < header->file_count; ++i) {
  108.         fread((void*)&header->pups[i], 1, PS4_PUP_ENTRY_SIZE, fd);
  109.     }
  110.    
  111.     return 1;
  112. }
  113.  
  114.  
  115. void ps4pup_free_header(struct ps4pup_header *header)
  116. {  
  117.     if (header) {
  118.         if (header->pups) {
  119.             free(header->pups);
  120.         }
  121.     }
  122. }
  123.  
  124. int ps4pup_extract(FILE *fd, struct ps4pup_header *header)
  125. {
  126.     if (fd == NULL || header == NULL) {
  127.         return 0;
  128.     }
  129.    
  130.     FILE *pup_out;
  131.     uint8_t copy_buffer[512];
  132.     int data_offset = PS4_PUP_HEADER_SIZE + PS4_PUP_ENTRY_SIZE * header->file_count;
  133.     data_offset = (data_offset+511) & ~511; //Align to 512 bytes
  134.        
  135.     int i;
  136.     for (i = 0; i < header->file_count; ++i) {
  137.         fseek(fd, data_offset + header->pups[i].offset, SEEK_SET);
  138.         pup_out = fopen(header->pups[i].filename, "wb");
  139.         int copy_size = header->pups[i].content_size;
  140.        
  141.         while (copy_size > 0) {
  142.             if (copy_size > 512) {
  143.                 fread(copy_buffer, 1, 512, fd);
  144.                 fwrite(copy_buffer, 1, 512, pup_out);
  145.                 copy_size -= 512;
  146.             } else {
  147.                 fread(copy_buffer, 1, copy_size, fd);
  148.                 fwrite(copy_buffer, 1, copy_size, pup_out);
  149.                 copy_size = 0; 
  150.             }
  151.         }
  152.         fflush(pup_out);
  153.         fclose(pup_out);
  154.     }  
  155.    
  156.    
  157.     return 1;  
  158. }
  159.  
  160. void ps4pup_print_header(const struct ps4pup_header *header)
  161. {
  162.     char magic_string[5];
  163.     strncpy(magic_string, (char*)(uintptr_t)(&header->magic), 4);
  164.     magic_string[4] = '\0';
  165.     printf("Magic:       0x%X  %s\n", header->magic, magic_string);
  166.     printf("Version:     %lu\n", header->version);
  167.     printf("File count:  %i\n", header->file_count);
  168.     printf("Block count: %i\n", header->block_count);
  169.    
  170.     int i;
  171.     for (i = 0; i < header->file_count; ++i) {
  172.         printf("PUP %i:\n", i+1);
  173.         printf("  Offset:   0x%X\n", header->pups[i].offset);
  174.         printf("  Size:     %i\n", header->pups[i].content_size);
  175.         printf("  Filename: %s\n", header->pups[i].filename);
  176.        
  177.     }
  178. }
  179.  
  180. void print_usage(void)
  181. {  
  182.     printf("Usage:\nps4pupextractor <PS4UPDATE.PUP>\n");
  183.     printf("It will extract the PUPs as: PS4UPDATEX.PUP being X the PUP number\n");
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement