FIXTHUNK.CPP 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <windows.h>
  19. #include <sys\types.h>
  20. #include <sys\stat.h>
  21. #include <fcntl.h>
  22. #include <io.h>
  23. #include <stdio.h>
  24. unsigned char thunk_file[1000000];
  25. unsigned char thunk_file_out[100050];
  26. /***********************************************************************************************
  27. * Search_For_String -- search for a string of chars within a buffer *
  28. * *
  29. * *
  30. * *
  31. * INPUT: string *
  32. * ptr to buffer to search in *
  33. * length of buffer *
  34. * *
  35. * OUTPUT: ptr to string in buffer or NULL if not found *
  36. * *
  37. * WARNINGS: None *
  38. * *
  39. * HISTORY: *
  40. * 11/20/95 5:42PM ST : Created *
  41. *=============================================================================================*/
  42. char *Search_For_String (char *string , char *buffer_ptr , int buffer_length)
  43. {
  44. int j;
  45. int string_length=strlen(string);
  46. for (int i=0 ; i<buffer_length-string_length ; i++){
  47. for (j=0 ; j<string_length ; j++){
  48. if ( *(string+j) != *(buffer_ptr+i+j)) break;
  49. }
  50. if (j==string_length) return buffer_ptr+i;
  51. }
  52. return (NULL);
  53. }
  54. int main(int argc, char *argv[])
  55. {
  56. int handle;
  57. char find_string[]={";************************ START OF THUNK BODIES************************"};
  58. char find_other_string[]={"public _IPX_Initialise@4"};
  59. char insert_string1[]={"\n\r;\n\r"};
  60. char insert_string2[]={";For some reason, inserting these lines makes it assemble correctly\n\r"};
  61. char insert_string3[]={";\n\r"};
  62. char insert_string4[]={"Externdef IPX_Initialise:near\n\r"};
  63. char insert_string5[]={"IPX_Initialise label near\n\r\n\r"};
  64. unsigned char *pointer;
  65. unsigned char *pointer2;
  66. int copy_length;
  67. int file_length;
  68. handle = open ("thipx.asm", O_RDONLY | O_BINARY);
  69. if (handle==-1)return (1);
  70. file_length = filelength(handle);
  71. if (read (handle, thunk_file, file_length) != file_length){
  72. close (handle);
  73. return (1);
  74. }
  75. close (handle);
  76. pointer = (unsigned char*)Search_For_String(find_string, (char*)thunk_file, file_length);
  77. if (pointer){
  78. pointer += strlen(find_string);
  79. copy_length = (int)( (int)pointer - (int)&thunk_file[0]);
  80. memcpy (thunk_file_out, thunk_file, copy_length);
  81. sprintf ((char*)&thunk_file_out[copy_length], "%s%s%s%s%s", insert_string1,
  82. insert_string2,
  83. insert_string3,
  84. insert_string4,
  85. insert_string5);
  86. pointer2 = (unsigned char*)Search_For_String(find_other_string, (char*)pointer, file_length);
  87. if (!pointer2) return (1);
  88. pointer2 += strlen(find_other_string);
  89. memcpy (&thunk_file_out [copy_length+ strlen(insert_string1)
  90. + strlen(insert_string2)
  91. + strlen(insert_string3)
  92. + strlen(insert_string4)
  93. + strlen(insert_string5)],
  94. pointer2,
  95. file_length-((int)pointer2-(int)&thunk_file[0]));
  96. handle = open ("thipx.asm", O_WRONLY | O_BINARY | O_TRUNC);
  97. if (handle == -1) return (1);
  98. write (handle, thunk_file_out, file_length+ strlen(insert_string1)
  99. + strlen(insert_string2)
  100. + strlen(insert_string3)
  101. + strlen(insert_string4)
  102. + strlen(insert_string5));
  103. close (handle);
  104. }
  105. return (0);
  106. }