diff options
-rw-r--r-- | CHANGELOG.txt | 7 | ||||
-rw-r--r-- | Makefile | 20 | ||||
-rw-r--r-- | README.txt | 2 | ||||
-rw-r--r-- | include/agbsum.h | 46 | ||||
-rw-r--r-- | source/checkParams.c | 133 | ||||
-rw-r--r-- | source/copyright.c (renamed from src/cpy.c) | 8 | ||||
-rw-r--r-- | source/exit.c (renamed from src/exi.c) | 8 | ||||
-rw-r--r-- | source/getSum.c (renamed from src/getsum.c) | 10 | ||||
-rw-r--r-- | source/help.c (renamed from src/hlp.c) | 6 | ||||
-rw-r--r-- | source/initData.c (renamed from src/inidat.c) | 12 | ||||
-rw-r--r-- | source/main.c (renamed from src/main.c) | 32 | ||||
-rw-r--r-- | source/open.c (renamed from src/opn.c) | 12 | ||||
-rw-r--r-- | source/patch.c (renamed from src/pat.c) | 10 | ||||
-rw-r--r-- | source/read.c (renamed from src/red.c) | 12 | ||||
-rw-r--r-- | src/chkpar.c | 126 |
15 files changed, 230 insertions, 214 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 4db18ed..a038d36 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,10 @@ +| F + +- Rename source directory: 'src' => 'source'; +- Update naming convention; +- Update copyright holder name; +- Update code style; + | E - Code style adjustments; @@ -19,16 +19,16 @@ CFLAGS = \ -std=c99 OBJS := \ - src/chkpar.o \ - src/cpy.o \ - src/exi.o \ - src/getsum.o \ - src/hlp.o \ - src/inidat.o \ - src/main.o \ - src/opn.o \ - src/pat.o \ - src/red.o + source/checkParams.o \ + source/copyright.o \ + source/exit.o \ + source/getSum.o \ + source/help.o \ + source/initData.o \ + source/main.o \ + source/open.o \ + source/patch.o \ + source/read.o BIN := agbsum @@ -20,7 +20,7 @@ Options: - COPYRIGHT & LICENSE -Copyright 2022-2023 Gabriel Jensen. +Copyright 2022-2023 Gabriel Bjørnager Jensen. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software diff --git a/include/agbsum.h b/include/agbsum.h index 82cbac9..83a182c 100644 --- a/include/agbsum.h +++ b/include/agbsum.h @@ -1,5 +1,5 @@ /* - Copyright 2022-2023 Gabriel Jensen. + Copyright 2022-2023 Gabriel Bjørnager Jensen. This file is part of agbsum. @@ -22,8 +22,8 @@ If not, see <https://www.gnu.org/licenses/>. */ -#ifndef agb_hdr -#define agb_hdr +#ifndef agb_header +#define agb_header #include <stdbool.h> #include <stdint.h> @@ -39,48 +39,48 @@ #define noreturn #endif -#define agb_rel ((uint64_t)+0xEu) +#define agb_release ((uint64_t)UINT64_C(+0xF)) -#define agb_iptsrt ((size_t)+0xA0u) // The part of the header used for calculating the checksum. -#define agb_sumoff ((size_t)+0xBDu - agb_iptsrt) +#define agb_sumDataStart ((size_t)+0xA0u) // The part of the header used for calculating the checksum. +#define agb_sumOffset ((size_t)+0xBDu - agb_sumDataStart) -enum agb_cnd { - agb_cnd_err, - agb_cnd_oky, +enum agb_Condition { + agb_Cnd_Error, + agb_Cnd_Ok, }; -struct agb_dat { - bool dopat; - char const* pth; - bool sil; - FILE* img; +struct agb_Data { + bool doPatch; + char const* path; + bool silent; + FILE* image; }; uint8_t -agb_getsum (void const* restrict img); +agb_getSum (void const* restrict image); void -agb_pat (FILE* restrict img, char unsigned sum); +agb_patch (FILE* restrict image, char unsigned sum); void -agb_cpy (void); +agb_copyright (void); void -agb_hlp (void); +agb_help (void); void -agb_chkpar (struct agb_dat* restrict dat, int argc, char const* const* restrict argv); +agb_checkParams (struct agb_Data* restrict dat, int argc, char const* const* restrict argv); void -agb_inidat (struct agb_dat* restrict dat); +agb_initdata (struct agb_Data* restrict dat); FILE* -agb_opn (char const* restrict pth); +agb_open (char const* restrict path); void -agb_red (void* restrict buf, FILE* restrict img); +agb_read (void* restrict buf, FILE* restrict image); noreturn void -agb_exi (enum agb_cnd stat, FILE* img); +agb_exit (enum agb_Condition stat, FILE* image); #endif diff --git a/source/checkParams.c b/source/checkParams.c new file mode 100644 index 0000000..47bdeef --- /dev/null +++ b/source/checkParams.c @@ -0,0 +1,133 @@ +/* + Copyright 2022-2023 Gabriel Bjørnager 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 <stddef.h> +#include <stdio.h> +#include <string.h> + +noreturn static void +agb_expectedParamValue (char const charParam) +{ + fprintf (stderr, "Expected value for character parameter '%c'\n", charParam); + agb_exit (agb_Cnd_Error, NULL); +} + +static bool +agb_checkCharParam (struct agb_Data* const restrict data, char const* const restrict param) +{ + // Returns true if the rest of the parameter is + // used as a value. + + char const charParam = param[0x0]; + if (charParam == '\x0') {return true;} + + char const* const restrict paramval = ¶m[0x1]; + + switch (charParam) { + default: + fprintf (stderr, "Invalid character parameter '%c'\n", charParam); + agb_exit (agb_Cnd_Error, NULL); + + case 'h': + agb_help (); + agb_exit (agb_Cnd_Ok, NULL); + + case 'i': + { + if (paramval[0x0] == '\x0') {agb_expectedParamValue (charParam);} + data->path = paramval; + } + return true; + + case 'p': + data->doPatch = true; + return false; + + case 's': + data->silent = true; + return false; + } +} + +void +agb_checkParams (struct agb_Data* const restrict data, int const argc, char const* const* const argv) { + if (argc < 0x2) { + agb_help (); + agb_exit (agb_Cnd_Ok, NULL); + } + + size_t const numParams = argc; // Prettier. + + for (ptrdiff_t index = 0x1; index < (ptrdiff_t)numParams; ++index) { + // Iterate over the parameters. One hyphen denotes character parameters (-h) whilst two denote long paramters (--help). + + char const* const param = argv[index]; + if (param[0x0] == '-') { + if (param[0x1] == '-') { + char const* const longParam = ¶m[0x2]; + + // Check long parameters. + + if (longParam[0x0] == '\x0') { + fputs ("Missing long parameter after '--' sequence\n", stderr); + agb_exit (agb_Cnd_Error, NULL); + } + + if (!strcmp (longParam, "help")) { + agb_help (); + agb_exit (agb_Cnd_Ok, NULL); + } + + if (!strcmp (longParam, "version")) { + agb_copyright(); + agb_exit (agb_Cnd_Ok, NULL); + } + + fprintf (stderr, "Invalid long parameter \"%s\"\n", longParam); + agb_exit (agb_Cnd_Error, NULL); + } + + if (param[0x1] == '\x0') { + fputs ("Missing character parameter after '-'\n", stderr); + agb_exit (agb_Cnd_Error, NULL); + } + + // Check character parameters. + + for (char const* charIndex = ¶m[0x1];; ++charIndex) { if (agb_checkCharParam (data, charIndex)) { break; } } + + continue; + } + + // We did not find the 'i' parameter, so we don't know where the ROM is. + + if (data->path == NULL) { + fputs ("ROM not set (missing character parameter 'i')\n", stderr); + agb_exit (agb_Cnd_Error, NULL); + } + } +} diff --git a/src/cpy.c b/source/copyright.c index a3f1a4b..14a64a5 100644 --- a/src/cpy.c +++ b/source/copyright.c @@ -1,5 +1,5 @@ /* - Copyright 2022-2023 Gabriel Jensen. + Copyright 2022-2023 Gabriel Bjørnager Jensen. This file is part of agbsum. @@ -28,10 +28,10 @@ #include <stdio.h> void -agb_cpy (void) +agb_copyright (void) { printf ( - "agbsum #%" PRIX64 " - Copyright 2022-2023 Gabriel Jensen.\n" + "agbsum #%" PRIX64 " - Copyright 2022-2023 Gabriel Bj\u00F8rnager Jensen.\n" "\n" "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" @@ -44,6 +44,6 @@ agb_cpy (void) "\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", - agb_rel + agb_release ); } diff --git a/src/exi.c b/source/exit.c index e960706..b34d91c 100644 --- a/src/exi.c +++ b/source/exit.c @@ -1,5 +1,5 @@ /* - Copyright 2022-2023 Gabriel Jensen. + Copyright 2022-2023 Gabriel Bjørnager Jensen. This file is part of agbsum. @@ -28,9 +28,9 @@ #include <stdlib.h> noreturn void -agb_exi (enum agb_cnd const cnd, FILE* restrict img) +agb_exit (enum agb_Condition const condition, FILE* restrict image) { - if (img != NULL) { fclose (img); } + if (image != NULL) { fclose (image); } - exit (cnd == agb_cnd_oky ? EXIT_SUCCESS : EXIT_FAILURE); + exit (condition == agb_Cnd_Ok ? EXIT_SUCCESS : EXIT_FAILURE); } diff --git a/src/getsum.c b/source/getSum.c index fd0e4a4..6d600ae 100644 --- a/src/getsum.c +++ b/source/getSum.c @@ -1,5 +1,5 @@ /* - Copyright 2022-2023 Gabriel Jensen. + Copyright 2022-2023 Gabriel Bjørnager Jensen. This file is part of agbsum. @@ -27,12 +27,12 @@ #include <stdint.h> uint8_t -agb_getsum (void const* const restrict imgptr) +agb_getSum (void const* const restrict imagePtr) { - uint8_t const* restrict img = imgptr; - uint8_t sum = UINT8_C(0x0); + uint8_t const* restrict image = imagePtr; + uint8_t sum = UINT8_C(0x0); - for (char unsigned const* restrict pos = img; pos != img + agb_sumoff; ++pos) { sum += *pos; } + for (char unsigned const* restrict position = image; position != image + agb_sumOffset; ++position) { sum += *position; } sum = -(UINT8_C(0x19) + sum); diff --git a/src/hlp.c b/source/help.c index 7e10e67..3686af9 100644 --- a/src/hlp.c +++ b/source/help.c @@ -1,5 +1,5 @@ /* - Copyright 2022-2023 Gabriel Jensen. + Copyright 2022-2023 Gabriel Bjørnager Jensen. This file is part of agbsum. @@ -28,7 +28,7 @@ #include <stdio.h> void -agb_hlp (void) +agb_help (void) { fputs ( "agbsum - Patch AGB image header checksums.\n" @@ -42,5 +42,5 @@ agb_hlp (void) "\n", stdout ); - agb_cpy (); + agb_copyright (); } diff --git a/src/inidat.c b/source/initData.c index 05cd278..f9baea3 100644 --- a/src/inidat.c +++ b/source/initData.c @@ -1,5 +1,5 @@ /* - Copyright 2022-2023 Gabriel Jensen. + Copyright 2022-2023 Gabriel Bjørnager Jensen. This file is part of agbsum. @@ -27,10 +27,10 @@ #include <stddef.h> void -agb_inidat (struct agb_dat* const restrict dat) +agb_initdata (struct agb_Data* const restrict data) { - dat->dopat = false; - dat->pth = NULL; - dat->sil = false; - dat->img = NULL; + data->doPatch = false; + data->path = NULL; + data->silent = false; + data->image = NULL; } diff --git a/src/main.c b/source/main.c index d130bd3..4583eb5 100644 --- a/src/main.c +++ b/source/main.c @@ -1,5 +1,5 @@ /* - Copyright 2022-2023 Gabriel Jensen. + Copyright 2022-2023 Gabriel Bjørnager Jensen. This file is part of agbsum. @@ -34,30 +34,32 @@ int main (int const argc, char const* const* const argv) { - struct agb_dat dat; - agb_inidat (&dat); - agb_chkpar (&dat, argc, argv); + struct agb_Data data; + agb_initdata (&data); + agb_checkParams (&data, argc, argv); - dat.img = agb_opn (dat.pth); + data.image = agb_open (data.path); - char unsigned buf[agb_sumoff + 0x1]; + char unsigned buffer[agb_sumOffset + 0x1]; - agb_red (buf, dat.img); + agb_read (buffer, data.image); { - char unsigned const sum = agb_getsum (buf); - char unsigned const romsum = buf[agb_sumoff]; + char unsigned const sum = agb_getSum (buffer); + char unsigned const imageSum = buffer[agb_sumOffset]; - if (romsum == sum || !dat.dopat) { + if (imageSum == sum || !data.doPatch) { // Don't patch the ROM if it's already okay or we're not allowed to. - if (!dat.sil) {printf ("\"%s\": %hhX (%hhX in file)\n", dat.pth, sum, romsum);} - agb_exi (agb_cnd_oky, dat.img); + if (!data.silent) {printf ("\"%s\": %hhX (%hhX in file)\n", data.path, sum, imageSum);} + agb_exit (agb_Cnd_Ok, data.image); } - agb_pat (dat.img, sum); + agb_patch (data.image, sum); - if (!dat.sil) {printf ("\"%s\" @ %zX: %hhX => %hhX\n", dat.pth, agb_iptsrt + agb_sumoff, romsum, sum);} // If we aren't supposed to print anything then don't. + // If we aren't supposed to print anything then + // don't. + if (!data.silent) { printf ("\"%s\" @ %zX: %hhX => %hhX\n", data.path, agb_sumDataStart + agb_sumOffset, imageSum, sum); } } - agb_exi (agb_cnd_oky, dat.img); + agb_exit (agb_Cnd_Ok, data.image); } diff --git a/src/opn.c b/source/open.c index 9990709..415e589 100644 --- a/src/opn.c +++ b/source/open.c @@ -1,5 +1,5 @@ /* - Copyright 2022-2023 Gabriel Jensen. + Copyright 2022-2023 Gabriel Bjørnager Jensen. This file is part of agbsum. @@ -27,14 +27,14 @@ #include <stdio.h> FILE* -agb_opn (char const* const restrict pth) +agb_open (char const* const restrict path) { - FILE* img = fopen (pth, "r+"); + FILE* image = fopen (path, "r+"); - if (img == NULL) { + if (image == NULL) { fputs ("Unable to open ROM\n", stderr); - agb_exi (agb_cnd_err, NULL); + agb_exit (agb_Cnd_Error, NULL); } - return img; + return image; } diff --git a/src/pat.c b/source/patch.c index 39b340f..78d2531 100644 --- a/src/pat.c +++ b/source/patch.c @@ -1,5 +1,5 @@ /* - Copyright 2022-2023 Gabriel Jensen. + Copyright 2022-2023 Gabriel Bjørnager Jensen. This file is part of agbsum. @@ -27,12 +27,12 @@ #include <stdio.h> void -agb_pat (FILE* const restrict img, char unsigned sum) +agb_patch (FILE* const restrict image, char unsigned sum) { - fseek (img, (long)(agb_iptsrt + agb_sumoff), SEEK_SET); + fseek (image, (long)(agb_sumDataStart + agb_sumOffset), SEEK_SET); - if (fwrite (&sum, 0x1u, 0x1u, img) != 0x1u) { + if (fwrite (&sum, 0x1u, 0x1u, image) != 0x1u) { fputs ("Unable to patch ROM\n", stderr); - agb_exi (agb_cnd_err, img); + agb_exit (agb_Cnd_Error, image); } } diff --git a/src/red.c b/source/read.c index 64d81da..0ed3808 100644 --- a/src/red.c +++ b/source/read.c @@ -1,5 +1,5 @@ /* - Copyright 2022-2023 Gabriel Jensen. + Copyright 2022-2023 Gabriel Bjørnager Jensen. This file is part of agbsum. @@ -27,16 +27,16 @@ #include <stdio.h> void -agb_red (void* const restrict buf, FILE* restrict img) +agb_read (void* const restrict buffer, FILE* restrict image) { // We only need to read the part of the image // that is used for the checksum. - fseek (img, agb_iptsrt, SEEK_SET); + fseek (image, agb_sumDataStart, SEEK_SET); - size_t const num = agb_sumoff + 0x1u; + size_t const count = agb_sumOffset + 0x1u; - if (fread (buf, 0x1u, num, img) != num) { + if (fread (buffer, 0x1u, count, image) != count) { fputs ("Unable to read ROM\n", stderr); - agb_exi (agb_cnd_err, img); + agb_exit (agb_Cnd_Error, image); } } diff --git a/src/chkpar.c b/src/chkpar.c deleted file mode 100644 index 081173e..0000000 --- a/src/chkpar.c +++ /dev/null @@ -1,126 +0,0 @@ -/* - 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 <stddef.h> -#include <stdio.h> -#include <string.h> - -noreturn static void -agb_expparval (char const chrpar) -{ - fprintf (stderr,"Expected value for character parameter '%c'\n", chrpar); - agb_exi (agb_cnd_err, NULL); -} - -static bool -agb_chkchrpar (struct agb_dat* const restrict dat, char const* const restrict par) -{ - char const chrpar = par[0x0]; - if (chrpar == '\x0') {return true;} - - char const* const restrict paramval = &par[0x1]; - - switch (chrpar) { - default: - fprintf (stderr,"Invalid character parameter '%c'\n", chrpar); - agb_exi (agb_cnd_err, NULL); - case 'h': - agb_hlp (); - agb_exi (agb_cnd_oky, NULL); - case 'i': - { - if (paramval[0x0] == '\x0') {agb_expparval (chrpar);} - dat->pth = paramval; - } - return true; - case 'p': - dat->dopat = true; - return false; - case 's': - dat->sil = true; - return false; - } -} - -void -agb_chkpar (struct agb_dat* const restrict dat, int const argc, char const* const* const argv) { - if (argc < 0x2) { - agb_hlp (); - agb_exi (agb_cnd_oky, NULL); - } else { - size_t const numpar = argc; // Prettier. - - for (ptrdiff_t pos = 0x1u; pos < (ptrdiff_t)numpar; ++pos) { - // Iterate over the parameters. One hyphen denotes character parameters (-h) whilst two denote long paramters (--help). - - char const* const par = argv[pos]; - if (par[0x0] == '-') { - if (par[0x1] == '-') { - char const* const lngparam = &par[0x2]; - - // Check long parameters. - - if (lngparam[0x0] == '\x0') { - fputs ("Missing long parameter after '--' sequence\n", stderr); - agb_exi (agb_cnd_err, NULL); - } - - if (!strcmp (lngparam,"help")) { - agb_hlp (); - agb_exi (agb_cnd_oky, NULL); - } - - if (!strcmp (lngparam,"version")) { - agb_cpy(); - agb_exi (agb_cnd_oky, NULL); - } - - fprintf (stderr,"Invalid long parameter \"%s\"\n", lngparam); - agb_exi (agb_cnd_err, NULL); - } - - if (par[0x1] == '\x0') { - fputs ("Missing character parameter after '-'\n", stderr); - agb_exi (agb_cnd_err, NULL); - } - - // Check character parameters. - - for (char const* chrpos = &par[0x1];; ++chrpos) {if (agb_chkchrpar (dat, chrpos)) {break;}} - - continue; - } - } - - // We did not find the 'i' parameter, so we don't know where the ROM is. - - if (dat->pth == NULL) { - fputs ("ROM not set (missing character parameter 'i')\n", stderr); - agb_exi (agb_cnd_err, NULL); - } - } -} |