Advertisement
Reisyukaku

[C] garc_dll.c

Nov 8th, 2014
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 KB | None | 0 0
  1. /*
  2. *       Garc.dll
  3. *         by Reisyukaku
  4. *
  5. *   Set of functions to handle GARC files in the RomFS.
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include "garc_dll.h"
  11.  
  12. EXPORT int* getgarcs(char* file){
  13.    
  14.     FILE* fp;
  15.     fp = fopen(file, "rb");
  16.     if (!fp) {
  17.         fclose(fp);
  18.         fprintf(stderr, "Error while opening file %s for reading\n", file);
  19.         return NULL;
  20.     }
  21.     fseek(fp, 0, SEEK_SET);
  22.     unsigned char magic[4];
  23.     int currOff = 0, cnt = 1;
  24.     int* list = malloc(sizeof(int));
  25.     list[0] = 0;
  26.     while(!(feof(fp) || ferror(fp))){
  27.         currOff += fread(magic, 1, 4, fp);
  28.         if(magic[0] == 0x43 && magic[1] == 0x52 && magic[2] == 0x41 && magic[3] == 0x47){
  29.             printf("0x%08x\n", currOff - 0x4);
  30.             list = realloc(list, sizeof(int) * (cnt + 1));
  31.             list[cnt++] = currOff - 0x4;
  32.         }
  33.     }
  34.     list[0] = cnt - 1;
  35.     fclose(fp);
  36.     return list;
  37. }
  38.  
  39. EXPORT void writegarc(char* inFile, char* outFile, int garcOff, int len){
  40.    
  41.     FILE* fi, *fo;
  42.     fi = fopen(inFile, "rb");
  43.     fo = fopen(outFile, "wb");
  44.     if (!fi) {
  45.         fclose(fi);
  46.         fprintf(stderr, "Error while opening file %s for reading\n", inFile);
  47.         return;
  48.     }
  49.     fseek(fi, garcOff, SEEK_SET);
  50.     fseek(fo, 0, SEEK_SET);
  51.    
  52.     int i = 0;
  53.     while(!(feof(fi) || ferror(fi) || ferror(fo)) && i < len){
  54.         char romfsBytes[0x10];
  55.         fread(romfsBytes, 1, 0x10, fi);
  56.         fwrite(romfsBytes, 1, 0x10, fo);
  57.         i+=0x10;
  58.     }
  59.     fclose(fi);
  60.     fclose(fo);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement