diff options
-rw-r--r-- | CHANGELOG.txt | 8 | ||||
-rw-r--r-- | Makefile | 10 | ||||
-rw-r--r-- | include/agbsum.h | 16 | ||||
-rw-r--r-- | src/chkpar.c | 16 | ||||
-rw-r--r-- | src/cpy.c | 30 | ||||
-rw-r--r-- | src/getsum.c | 10 | ||||
-rw-r--r-- | src/hlp.c | 4 | ||||
-rw-r--r-- | src/inidat.c | 3 | ||||
-rw-r--r-- | src/main.c | 22 | ||||
-rw-r--r-- | src/pat.c | 7 | ||||
-rw-r--r-- | src/red.c | 5 |
11 files changed, 99 insertions, 32 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 2fa62cb..2b5d16a 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,11 @@ +| C + +- Clean up code; +- Support version parameter; +- Don't print help screen to stderr; +- Add license notice to help; +- Enable more warnings; + | B - Fortify sources; @@ -2,19 +2,25 @@ RM := rm -f CFLAGS = \ -D_FORTIFY_SOURCE=2 \ - -D_GNU_SOURCE \ + -D_POSIX_SOURCE=199506 \ -Iinclude \ -O3 \ -Wall \ -Wextra \ + -Wformat=2 \ -Wmissing-declarations \ + -Wmissing-include-dirs \ + -Wnull-dereference \ -Wpedantic \ + -Wpointer-arith \ + -Wstrict-overflow=5 \ -g \ -o$(@) \ -std=c99 OBJS := \ src/chkpar.o \ + src/cpy.o \ src/exi.o \ src/getsum.o \ src/hlp.o \ @@ -29,6 +35,8 @@ BIN := agbsum $(BIN): $(OBJS) $(CC) -o$(BIN) $(OBJS) +$(OBJS): include/agbsum.h + .PHONY: clean install purge clean: diff --git a/include/agbsum.h b/include/agbsum.h index a7fa31b..59df778 100644 --- a/include/agbsum.h +++ b/include/agbsum.h @@ -14,7 +14,7 @@ #include <stdint.h> #include <stdio.h> -#if __STDC_VERSION__ > 199901 +#if __STDC_VERSION__ >= 201112 # include <stdnoreturn.h> #elif defined(__GNUC__) # define noreturn __attribute__ ((__noreturn__)) @@ -24,10 +24,10 @@ # define noreturn #endif -#define agb_rel ((uint64_t)+0xBu) +#define agb_rel ((uint64_t) + 0xCu) -#define agb_romsrt ((size_t)+0xA0u) -#define agb_chksumoff ((size_t)+0xBDu-agb_romsrt) +#define agb_romsrt ((size_t) + 0xA0u) +#define agb_sumoff ((size_t) + 0xBDu-agb_romsrt) typedef enum { agb_cnd_err, @@ -45,7 +45,10 @@ uint8_t agb_getsum (void const* restrict rom); void -agb_pat (FILE * restrict rom, unsigned char chksum); +agb_pat (FILE * restrict rom, char unsigned sum); + +void +agb_cpy (void); void agb_hlp (void); @@ -62,6 +65,7 @@ agb_opn (char const* restrict pth); void agb_red (void * restrict buf, FILE * restrict rom); -noreturn void agb_exi (agb_cnd stat, FILE * rom); +noreturn void +agb_exi (agb_cnd stat, FILE * rom); #endif diff --git a/src/chkpar.c b/src/chkpar.c index 9ad487d..02256d3 100644 --- a/src/chkpar.c +++ b/src/chkpar.c @@ -9,6 +9,7 @@ #include <agbsum.h> +#include <inttypes.h> #include <stddef.h> #include <stdio.h> #include <string.h> @@ -58,7 +59,7 @@ agb_chkpar (agb_dat * const restrict dat, int const argc, char const* const* con } else { size_t const numpar = argc; - for (size_t pos = 0x1u;pos < numpar;++pos) { + for (size_t pos = 0x1u; pos < numpar; ++pos) { char const* const par = argv[pos]; if (par[0x0u] == '-') { @@ -74,6 +75,17 @@ agb_chkpar (agb_dat * const restrict dat, int const argc, char const* const* con agb_hlp (); agb_exi (agb_cnd_oky, NULL); } + + if (!strcmp (lngparam,"version")) { + printf( + "agbsum #%" PRIX64 "\n" + "Copyright 2022-2023 Gabriel Jensen.\n" + "\n", + agb_rel + ); + agb_cpy(); + agb_exi (agb_cnd_oky, NULL); + } fprintf (stderr,"Invalid long parameter \"%s\"\n", lngparam); agb_exi (agb_cnd_err, NULL); @@ -84,7 +96,7 @@ agb_chkpar (agb_dat * const restrict dat, int const argc, char const* const* con agb_exi (agb_cnd_err, NULL); } - for (char const* chrpos = &par[0x1u];;++chrpos) {if (agb_chkchrpar (dat, chrpos)) {break;}} + for (char const* chrpos = &par[0x1u];; ++chrpos) {if (agb_chkchrpar (dat, chrpos)) {break;}} continue; } diff --git a/src/cpy.c b/src/cpy.c new file mode 100644 index 0000000..700827b --- /dev/null +++ b/src/cpy.c @@ -0,0 +1,30 @@ +/* + Copyright 2022-2023 Gabriel Jensen. + + This file is part of agbsum. + agbsum is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + agbsum is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + You should have received a copy of the GNU Affero General Public License along with agbsum. If not, see <https://www.gnu.org/licenses/>. +*/ + +#include <agbsum.h> + +#include <inttypes.h> +#include <stdio.h> + +void +agb_cpy (void) +{ + fputs ( + "This program is free software: you can redistribute it and/or modify it under\n" + " the terms of the GNU General Public License as published by the Free Software\n" + " Foundation, either version 3 of the License, or (at your option) any later\n" + " version.\n" + "This program is distributed in the hope that it will be useful, but WITHOUT ANY\n" + " WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR\n" + " A PARTICULAR PURPOSE. See the GNU General Public License for more details.\n" + "You should have received a copy of the GNU General Public License along with\n" + " this program. If not, see <https://www.gnu.org/licenses/>.\n", + stdout + ); +} diff --git a/src/getsum.c b/src/getsum.c index b92fdef..29bc093 100644 --- a/src/getsum.c +++ b/src/getsum.c @@ -14,12 +14,12 @@ uint8_t agb_getsum (void const* const restrict romptr) { - uint8_t const* restrict rom = romptr; - uint8_t chksum = 0x0u; + uint8_t const* restrict rom = romptr; + uint8_t sum = 0x0u; - for (unsigned char const* restrict pos = rom;pos != rom+agb_chksumoff;++pos) {chksum += *pos;} + for (char unsigned const* restrict pos = rom; pos != rom + agb_sumoff; ++pos) {sum += *pos;} - chksum = 0x0u-(0x19u+chksum); + sum = 0x0u-(0x19u + sum); - return chksum; + return sum; } @@ -15,7 +15,7 @@ void agb_hlp (void) { - fprintf (stderr, + printf ( "agbsum - Calculate GBA ROM header checksums.\n" "Release #%" PRIX64 ". Copyright 2022-2023 Gabriel Jensen.\n" "\n" @@ -24,7 +24,9 @@ agb_hlp (void) " --help -h Print the help screen\n" " -p Patch the ROM\n" " -s Don't print the results\n" + " --version Don't print the results\n" "\n", agb_rel ); + agb_cpy(); } diff --git a/src/inidat.c b/src/inidat.c index 0657c70..b215faf 100644 --- a/src/inidat.c +++ b/src/inidat.c @@ -11,7 +11,8 @@ #include <stddef.h> -void agb_inidat (agb_dat * const restrict dat) +void +agb_inidat (agb_dat * const restrict dat) { dat->dopat = false; dat->pth = NULL; @@ -20,28 +20,28 @@ int main (int const argc, char const* const* const argv) { agb_dat dat; - agb_inidat (&dat); - agb_chkpar (&dat, argc, argv); dat.rom = agb_opn (dat.pth); - unsigned char buf[agb_chksumoff+0x1u]; + char unsigned buf[agb_sumoff + 0x1u]; agb_red (buf, dat.rom); - unsigned char const chksum = agb_getsum (buf); - unsigned char const romchksum = buf[agb_chksumoff]; + { + char unsigned const sum = agb_getsum (buf); + char unsigned const romsum = buf[agb_sumoff]; - if (romchksum == chksum || !dat.dopat) { - if (!dat.sil) {printf ("\"%s\": %hhX (%hhX in file)\n", dat.pth, chksum, romchksum);} - agb_exi (agb_cnd_oky, dat.rom); - } + if (romsum == sum || !dat.dopat) { + if (!dat.sil) {printf ("\"%s\": %hhX (%hhX in file)\n", dat.pth, sum, romsum);} + agb_exi (agb_cnd_oky, dat.rom); + } - agb_pat (dat.rom, chksum); + agb_pat (dat.rom, sum); - if (!dat.sil) {printf ("\"%s\" @ %zX: %hhX => %hhX\n", dat.pth, agb_romsrt+agb_chksumoff, romchksum, chksum);} + if (!dat.sil) {printf ("\"%s\" @ %zX: %hhX => %hhX\n", dat.pth, agb_romsrt + agb_sumoff, romsum, sum);} + } agb_exi (agb_cnd_oky, dat.rom); } @@ -11,11 +11,12 @@ #include <stdio.h> -void agb_pat (FILE * const restrict rom, unsigned char chksum) +void +agb_pat (FILE * const restrict rom, char unsigned sum) { - fseek (rom,(long)(agb_romsrt+agb_chksumoff), SEEK_SET); + fseek (rom,(long)(agb_romsrt + agb_sumoff), SEEK_SET); - if (fwrite (&chksum,0x1u,0x1u, rom) != 0x1u) { + if (fwrite (&sum,0x1u,0x1u, rom) != 0x1u) { fputs ("Unable to patch ROM\n", stderr); agb_exi (agb_cnd_err, rom); } @@ -11,10 +11,11 @@ #include <stdio.h> -void agb_red (void * const restrict buf, FILE * restrict rom) +void +agb_red (void * const restrict buf, FILE * restrict rom) { fseek (rom, agb_romsrt, SEEK_SET); // We only need to read the part of the ROM that is used for the checksum. - size_t const num = agb_chksumoff+0x1u; + size_t const num = agb_sumoff + 0x1u; if (fread (buf,0x1u, num, rom) != num) { fputs ("Unable to read ROM\n", stderr); |