nossl_patch_arm9.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. int main(int argc, char **argv)
  5. {
  6. // Add any search terms here that must be replaced here.
  7. // WARNING: Do not forget to add a replacement term!
  8. char *search_terms[] = { "https://" };
  9. char *replacement_terms[] = { "http://" };
  10. int search_term_count = sizeof(search_terms) / sizeof(search_terms[0]);
  11. int replacement_term_count = sizeof(replacement_terms) / sizeof(replacement_terms[0]);
  12. FILE *file = NULL;
  13. char *inputFilename = NULL;
  14. char *outputFilename = NULL;
  15. unsigned char *buffer = NULL;
  16. size_t filesize = 0;
  17. int idx = 0;
  18. // Make sure anyone who modifies this program read my warning above.
  19. assert(search_term_count == replacement_term_count);
  20. if(argc < 2)
  21. {
  22. printf("usage: %s arm9.bin (optional: arm9_patched.bin)\n", argv[0]);
  23. return 0;
  24. }
  25. inputFilename = argv[1];
  26. // If there is more than 1 argument given, take the second one as the file to save to.
  27. if(argc > 2)
  28. outputFilename = argv[2];
  29. else
  30. outputFilename = inputFilename;
  31. file = fopen(inputFilename, "rb");
  32. if(!file)
  33. {
  34. printf("ERROR: Could not open %s for reading.\n", inputFilename);
  35. return -1;
  36. }
  37. fseek(file,0,SEEK_END);
  38. filesize = ftell(file);
  39. rewind(file);
  40. buffer = (unsigned char*)calloc(filesize, sizeof(unsigned char));
  41. if(!buffer)
  42. {
  43. printf("ERROR: Could not create buffer with a size of %d bytes.\n", filesize);
  44. return -2;
  45. }
  46. fread(buffer, 1, filesize, file);
  47. fclose(file);
  48. // Search for "https://nas.nintendowifi.net" and replace it with "http://nas.nintendowifi.net"
  49. for(idx = 0; idx < search_term_count; idx++)
  50. {
  51. int i = 0;
  52. int search_term_len = strlen(search_terms[idx]);
  53. int replacement_term_len = strlen(replacement_terms[idx]);
  54. while(i < filesize)
  55. {
  56. if(memcmp(buffer + i, search_terms[idx], search_term_len) == 0)
  57. {
  58. // Find the end of the string so we know how many bytes to move.
  59. // This assumes that all results are null-terminated.
  60. int len = strlen(buffer + i);
  61. char *p = (char*)(buffer + i);
  62. int n = 0;
  63. int doReplace = 1;
  64. // Search the end of the string to find out how many null
  65. // bytes we have to work with, just in case we plan on overwriting
  66. // more than originally was there.
  67. while(i + len + n < filesize && p[len + n] == '\0')
  68. n++;
  69. // Take into account that we need at least one null-terminator at the
  70. // end of the string.
  71. n--;
  72. if(replacement_term_len > search_term_len)
  73. {
  74. // If the replacement term is longer than the term to be replaced,
  75. // calculate how much free space will be left over after replacement.
  76. int remainingSpace = n - (replacement_term_len - search_term_len);
  77. // If the free space is less than 0 then it means it runs over the
  78. // final null-terminator, which would cause errors in-game.
  79. if(remainingSpace < 0)
  80. doReplace = 0;
  81. }
  82. if(doReplace)
  83. {
  84. // Build replacement string and do replacement.
  85. // This method takes into account the null-terminators, so it should be safe.
  86. int newlen = len + n;
  87. char *b = (char*)calloc(newlen + 1, sizeof(char));
  88. memcpy(b, replacement_terms[idx], replacement_term_len);
  89. memcpy(b + replacement_term_len, p + search_term_len, len - search_term_len);
  90. printf("Replaced '%s' with '%s' at 0x%08x.\n", p, b, i);
  91. memcpy(p, b, newlen);
  92. }
  93. else
  94. {
  95. printf("Not enough free space to replace '%s' with '%s' at 0x%08x.\n", search_terms[idx], replacement_terms[idx], i);
  96. }
  97. i += search_term_len;
  98. }
  99. else
  100. {
  101. i++;
  102. }
  103. }
  104. }
  105. file = fopen(outputFilename, "wb");
  106. if(!file)
  107. {
  108. printf("ERROR: Could not open %s for writing.\n", outputFilename);
  109. return -1;
  110. }
  111. fwrite(buffer, 1, filesize, file);
  112. fclose(file);
  113. if(buffer)
  114. free(buffer);
  115. return 0;
  116. }