diff options
Diffstat (limited to 'tools/br_ips/br_ips.c')
-rw-r--r-- | tools/br_ips/br_ips.c | 47 |
1 files changed, 27 insertions, 20 deletions
diff --git a/tools/br_ips/br_ips.c b/tools/br_ips/br_ips.c index 440705cab..58e28202f 100644 --- a/tools/br_ips/br_ips.c +++ b/tools/br_ips/br_ips.c @@ -1,4 +1,3 @@ -#define _POSIX_C_SOURCE 200808L // Don't use GNU getline #include <stdio.h> #include <stdint.h> #include <stdlib.h> @@ -20,6 +19,7 @@ static const char HELP[] = "br_ips\n" "Options:\n" " -h - show this message and exit\n"; +#if !defined(__CYGWIN__) && !defined(__APPLE__) && (_POSIX_C_SOURCE < 200809L || !_GNU_SOURCE) static int getline(char ** lineptr, size_t * n, FILE * stream) { // Static implementation of GNU getline int i = 0; @@ -49,6 +49,7 @@ static int getline(char ** lineptr, size_t * n, FILE * stream) { buf[i] = 0; return i; } +#endif static void getIncbinsFromFile(hunk_t ** hunks, size_t * num, size_t * maxnum, const char * fname, char ** strbuf, size_t * buffersize) { // Recursively find incbinned segments and encode them as hunks. @@ -254,25 +255,30 @@ static void writePatch(const char * filename, const hunk_t * hunks, size_t num, // Maximum hunk size is 65535 bytes. For convenience, we allocate a // round 65536 (0x10000). This has no effect on memory consumption, // as malloc will round this up anyway. - char * readbuf = malloc(0x10000); - if (readbuf == NULL) FATAL_ERROR("failed to allocate write buffer\n"); + char * readbuf = NULL; + if (rom != NULL) { + readbuf = malloc(0x10000); + if (readbuf == NULL) FATAL_ERROR("failed to allocate write buffer\n"); + } fwrite("PATCH", 1, 5, file); // magic - for (int i = 0; i < num; i++) { - // Encode the offset - uint32_t offset = hunks[i].offset; - putc(offset >> 16, file); - putc(offset >> 8, file); - putc(offset >> 0, file); - // Encode the size - size_t size = hunks[i].size; - putc(size >> 8, file); - putc(size >> 0, file); - // Yank the data straight from the ROM - if (fseek(rom, offset, SEEK_SET)) FATAL_ERROR("seek\n"); - if (fread(readbuf, 1, size, rom) != size) FATAL_ERROR("read\n"); - if (fwrite(readbuf, 1, size, file) != size) FATAL_ERROR("write\n"); + if (readbuf != NULL) { + for (int i = 0; i < num; i++) { + // Encode the offset + uint32_t offset = hunks[i].offset; + putc(offset >> 16, file); + putc(offset >> 8, file); + putc(offset >> 0, file); + // Encode the size + size_t size = hunks[i].size; + putc(size >> 8, file); + putc(size >> 0, file); + // Yank the data straight from the ROM + if (fseek(rom, offset, SEEK_SET)) FATAL_ERROR("seek\n"); + if (fread(readbuf, 1, size, rom) != size) FATAL_ERROR("read\n"); + if (fwrite(readbuf, 1, size, file) != size) FATAL_ERROR("write\n"); + } + free(readbuf); } - free(readbuf); // Write the EOF magic fwrite("EOF", 1, 3, file); fclose(file); @@ -306,14 +312,15 @@ int main(int argc, char ** argv) { "If there are baserom.gba hunks in this project,\n" "please ping PikalaxALT on the pret discord,\n" "channel #gen-3-help.\n"); + writePatch("baserom.ips", NULL, 0, NULL); } else { // Merge neighboring hunks to reduce the number of hunks. collapseIncbins(hunks, &num); // Encode the hunks in the IPS patch. writePatch("baserom.ips", hunks, num, rom); - // Communicate status to the user. - puts("IPS file created at baserom.ips\n"); } + // Communicate status to the user. + puts("IPS file created at baserom.ips\n"); // Clean up and return. fclose(rom); free(hunks); |