diff options
author | PikalaxALT <pikalaxalt@gmail.com> | 2019-10-27 20:52:55 -0400 |
---|---|---|
committer | PikalaxALT <pikalaxalt@gmail.com> | 2019-10-27 20:52:55 -0400 |
commit | 47534a1471019a25e25c29a3f5bb529ccbd73e86 (patch) | |
tree | da4f2abb0fa27f5f07bcbdd4af47ca4eec84a786 /tools/br_ips/br_ips.c | |
parent | b8fec3d9f617ec9a764bb56804c526ff7449631f (diff) | |
parent | 922411abebd56d0f5d6072bad4924c909c8e2c27 (diff) |
Merge branch 'master' of github.com:pret/pokefirered into trade
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); |