Advertisement
Guest User

AutoInjector Source

a guest
Mar 4th, 2011
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. int filesize( FILE * fp){
  7. int sz = 0;
  8. fseek(fp, 0L, SEEK_END);
  9. sz = ftell(fp);
  10. fseek(fp, 0L, SEEK_SET);
  11. return sz;
  12. }
  13.  
  14. int GetFileNameDeb(FILE * fn, char ** filnamPtr){
  15. int i = 0, nstart = 0, nsize = 0, next = 0, estart = 0, esize = 0;
  16. char c = '0';
  17. int tabarr[4] = {0,0,0,0};
  18. char * filnam;
  19.  
  20. if(*filnamPtr != NULL){free(*filnamPtr);}
  21.  
  22. if(ftell(fn) > 0x143C0){return -1;}
  23.  
  24. while(c != '\n'){
  25. fread(&c, 1, 1, fn);
  26. if((c == ' ') && (i < 4)){
  27. tabarr[i] = ftell(fn);
  28. i++;
  29. }
  30. }
  31.  
  32. next = ftell(fn);
  33. nstart = tabarr[0];
  34. nsize = tabarr[1]-tabarr[0]-1;
  35. estart = tabarr[2];
  36. esize = tabarr[3]-tabarr[2]-1;
  37.  
  38. *filnamPtr = (char *) malloc(nsize+esize+1);
  39.  
  40. filnam = *filnamPtr;
  41.  
  42. fseek(fn, nstart, SEEK_SET);
  43. fread(filnam, 1, nsize, fn);
  44.  
  45. fseek(fn, estart, SEEK_SET);
  46. fread(&(filnam[nsize]), 1, esize, fn);
  47.  
  48. filnam[nsize+esize] = '\0';
  49.  
  50. fseek(fn, next, SEEK_SET);
  51.  
  52. if(filnam[nsize+esize-1] == 'e'){return 1;} else {return 0;}
  53. }
  54.  
  55. int main(){
  56. int fileindex = 0;
  57. int offset1 = 0;
  58. int offset2 = 0;
  59. int injectsize = 0;
  60. int injnum = 0;
  61. int excess = 0;
  62. int tablebase = 0;
  63. int dummy = 0;
  64. int status = 0;
  65. char * buffer = NULL;
  66. char * filename = NULL;
  67. FILE * rom = NULL;
  68. FILE * injection = NULL;
  69. FILE * fn = NULL;
  70.  
  71. rom = fopen("ZELOOTMA.z64","rb+");
  72. fn = fopen("DebugList.txt", "rb");
  73.  
  74. if((rom == NULL)||(fn == NULL)){
  75. printf("Failed to open ROM or DebugList.txt\n\r");
  76. printf("Make sure your ROM is named ZELOOTMA.z64\n\r");
  77. goto end;
  78. }
  79.  
  80. //fseek(fn, 0x509C, SEEK_SET);
  81.  
  82. printf("ROM and DebugList opened.\n\r");
  83.  
  84. tablebase = 0x12F70;
  85.  
  86. for(fileindex = 0 ; fileindex < 1532; fileindex++){
  87.  
  88. status = GetFileNameDeb(fn, &filename);
  89.  
  90. if(status == -1){
  91. printf("Ran out of fn");
  92. break;
  93. }
  94.  
  95. injection = fopen(filename, "rb");
  96.  
  97. if(injection == NULL){/*printf("Failed to open file %s %d",filename,fileindex);*/ continue;}
  98.  
  99. printf("%s ", filename);
  100.  
  101. fseek(rom, tablebase + 16*fileindex, SEEK_SET);
  102.  
  103. printf("Table Slot %X\n\r", tablebase + 16*fileindex);
  104.  
  105. dummy = fread(&offset1, 4, 1, rom);
  106.  
  107. offset1 = _byteswap_ulong(offset1);
  108.  
  109. fseek(rom, 12, SEEK_CUR);
  110.  
  111. dummy = fread(&offset2, 4, 1, rom);
  112.  
  113. offset2 = _byteswap_ulong(offset2);
  114.  
  115. printf("Start %X End %X\n\r", offset1, offset2);
  116.  
  117. injectsize = filesize(injection);
  118.  
  119. excess = injectsize - (offset2 - offset1);
  120.  
  121. if( excess > 0 ){
  122. printf("%s is %X bytes too large to inject.\n\r", filename, excess);
  123. continue;
  124. }
  125.  
  126. offset2 = _byteswap_ulong(offset2 + excess);
  127.  
  128. printf("New End %X\n\r",_byteswap_ulong(offset2));
  129.  
  130. fseek(rom, tablebase + fileindex*16 + 4, SEEK_SET);
  131.  
  132. fwrite(&offset2, 4, 1, rom);
  133.  
  134. fseek(rom, offset1, SEEK_SET);
  135.  
  136. buffer = (char *) malloc(injectsize);
  137.  
  138. dummy = fread(buffer, 1, injectsize, injection);
  139.  
  140. dummy = fwrite(buffer, 1, injectsize, rom);
  141.  
  142. printf("%X bytes transferred\n\r\n\r", dummy);
  143.  
  144. free(buffer);
  145.  
  146. injnum++;
  147.  
  148. dummy = fclose(injection);
  149. }
  150.  
  151. dummy = fclose(fn);
  152. dummy = fclose(rom);
  153.  
  154. end:
  155. printf("Injected %d files. Now fix the CRC and you're done.\n\r", injnum);
  156. printf("AutoInjector program by petrie911.");
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement