From bef17804cd57a48aaa37cf01f6409d6f6be94462 Mon Sep 17 00:00:00 2001 From: Gabriel Bjørnager Jensen Date: Sun, 22 Oct 2023 09:47:30 +0200 Subject: Rename source directory: 'src' => 'source'; Update naming convention; Update copyright holder name; Update code style; --- CHANGELOG.txt | 7 +++ Makefile | 20 ++++---- README.txt | 2 +- include/agbsum.h | 46 +++++++++--------- source/checkParams.c | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++ source/copyright.c | 49 +++++++++++++++++++ source/exit.c | 36 ++++++++++++++ source/getSum.c | 40 ++++++++++++++++ source/help.c | 46 ++++++++++++++++++ source/initData.c | 36 ++++++++++++++ source/main.c | 65 +++++++++++++++++++++++++ source/open.c | 40 ++++++++++++++++ source/patch.c | 38 +++++++++++++++ source/read.c | 42 ++++++++++++++++ src/chkpar.c | 126 ------------------------------------------------ src/cpy.c | 49 ------------------- src/exi.c | 36 -------------- src/getsum.c | 40 ---------------- src/hlp.c | 46 ------------------ src/inidat.c | 36 -------------- src/main.c | 63 ------------------------ src/opn.c | 40 ---------------- src/pat.c | 38 --------------- src/red.c | 42 ---------------- 24 files changed, 566 insertions(+), 550 deletions(-) create mode 100644 source/checkParams.c create mode 100644 source/copyright.c create mode 100644 source/exit.c create mode 100644 source/getSum.c create mode 100644 source/help.c create mode 100644 source/initData.c create mode 100644 source/main.c create mode 100644 source/open.c create mode 100644 source/patch.c create mode 100644 source/read.c delete mode 100644 src/chkpar.c delete mode 100644 src/cpy.c delete mode 100644 src/exi.c delete mode 100644 src/getsum.c delete mode 100644 src/hlp.c delete mode 100644 src/inidat.c delete mode 100644 src/main.c delete mode 100644 src/opn.c delete mode 100644 src/pat.c delete mode 100644 src/red.c 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; diff --git a/Makefile b/Makefile index 61fd84f..7ccebe1 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/README.txt b/README.txt index cd4142e..2c5133c 100644 --- a/README.txt +++ b/README.txt @@ -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 . */ -#ifndef agb_hdr -#define agb_hdr +#ifndef agb_header +#define agb_header #include #include @@ -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 . +*/ + +#include + +#include +#include +#include +#include + +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/source/copyright.c b/source/copyright.c new file mode 100644 index 0000000..14a64a5 --- /dev/null +++ b/source/copyright.c @@ -0,0 +1,49 @@ +/* + 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 . +*/ + +#include + +#include +#include + +void +agb_copyright (void) +{ + printf ( + "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" + "Foundation, either version 3 of the License, or (at your option) any later\n" + "version.\n" + "\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 A\n" + "PARTICULAR PURPOSE. See the GNU General Public License for more details.\n" + "\n" + "You should have received a copy of the GNU General Public License along with\n" + "this program. If not, see .\n", + agb_release + ); +} diff --git a/source/exit.c b/source/exit.c new file mode 100644 index 0000000..b34d91c --- /dev/null +++ b/source/exit.c @@ -0,0 +1,36 @@ +/* + 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 . +*/ + +#include + +#include +#include + +noreturn void +agb_exit (enum agb_Condition const condition, FILE* restrict image) +{ + if (image != NULL) { fclose (image); } + + exit (condition == agb_Cnd_Ok ? EXIT_SUCCESS : EXIT_FAILURE); +} diff --git a/source/getSum.c b/source/getSum.c new file mode 100644 index 0000000..6d600ae --- /dev/null +++ b/source/getSum.c @@ -0,0 +1,40 @@ +/* + 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 . +*/ + +#include + +#include + +uint8_t +agb_getSum (void const* const restrict imagePtr) +{ + uint8_t const* restrict image = imagePtr; + uint8_t sum = UINT8_C(0x0); + + for (char unsigned const* restrict position = image; position != image + agb_sumOffset; ++position) { sum += *position; } + + sum = -(UINT8_C(0x19) + sum); + + return sum; +} diff --git a/source/help.c b/source/help.c new file mode 100644 index 0000000..3686af9 --- /dev/null +++ b/source/help.c @@ -0,0 +1,46 @@ +/* + 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 . +*/ + +#include + +#include +#include + +void +agb_help (void) +{ + fputs ( + "agbsum - Patch AGB image header checksums.\n" + "\n" + "Usage: agbsum [options] \n" + "Options:\n" + " --help -h Print the help screen\n" + " -p Patch the image file\n" + " -s Don't print results\n" + " --version Print the version number\n" + "\n", + stdout + ); + agb_copyright (); +} diff --git a/source/initData.c b/source/initData.c new file mode 100644 index 0000000..f9baea3 --- /dev/null +++ b/source/initData.c @@ -0,0 +1,36 @@ +/* + 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 . +*/ + +#include + +#include + +void +agb_initdata (struct agb_Data* const restrict data) +{ + data->doPatch = false; + data->path = NULL; + data->silent = false; + data->image = NULL; +} diff --git a/source/main.c b/source/main.c new file mode 100644 index 0000000..4583eb5 --- /dev/null +++ b/source/main.c @@ -0,0 +1,65 @@ +/* + 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 . +*/ + +#include + +#include +#include +#include + +#if CHAR_BIT != 0x8 +#error "agbsum only support 8-bit byte systems" +#endif + +int main (int const argc, char const* const* const argv) +{ + struct agb_Data data; + agb_initdata (&data); + agb_checkParams (&data, argc, argv); + + data.image = agb_open (data.path); + + char unsigned buffer[agb_sumOffset + 0x1]; + + agb_read (buffer, data.image); + + { + char unsigned const sum = agb_getSum (buffer); + char unsigned const imageSum = buffer[agb_sumOffset]; + + if (imageSum == sum || !data.doPatch) { + // Don't patch the ROM if it's already okay or we're not allowed to. + if (!data.silent) {printf ("\"%s\": %hhX (%hhX in file)\n", data.path, sum, imageSum);} + agb_exit (agb_Cnd_Ok, data.image); + } + + agb_patch (data.image, sum); + + // 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_exit (agb_Cnd_Ok, data.image); +} diff --git a/source/open.c b/source/open.c new file mode 100644 index 0000000..415e589 --- /dev/null +++ b/source/open.c @@ -0,0 +1,40 @@ +/* + 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 . +*/ + +#include + +#include + +FILE* +agb_open (char const* const restrict path) +{ + FILE* image = fopen (path, "r+"); + + if (image == NULL) { + fputs ("Unable to open ROM\n", stderr); + agb_exit (agb_Cnd_Error, NULL); + } + + return image; +} diff --git a/source/patch.c b/source/patch.c new file mode 100644 index 0000000..78d2531 --- /dev/null +++ b/source/patch.c @@ -0,0 +1,38 @@ +/* + 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 . +*/ + +#include + +#include + +void +agb_patch (FILE* const restrict image, char unsigned sum) +{ + fseek (image, (long)(agb_sumDataStart + agb_sumOffset), SEEK_SET); + + if (fwrite (&sum, 0x1u, 0x1u, image) != 0x1u) { + fputs ("Unable to patch ROM\n", stderr); + agb_exit (agb_Cnd_Error, image); + } +} diff --git a/source/read.c b/source/read.c new file mode 100644 index 0000000..0ed3808 --- /dev/null +++ b/source/read.c @@ -0,0 +1,42 @@ +/* + 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 . +*/ + +#include + +#include + +void +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 (image, agb_sumDataStart, SEEK_SET); + + size_t const count = agb_sumOffset + 0x1u; + + if (fread (buffer, 0x1u, count, image) != count) { + fputs ("Unable to read ROM\n", stderr); + 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 . -*/ - -#include - -#include -#include -#include -#include - -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); - } - } -} diff --git a/src/cpy.c b/src/cpy.c deleted file mode 100644 index a3f1a4b..0000000 --- a/src/cpy.c +++ /dev/null @@ -1,49 +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 . -*/ - -#include - -#include -#include - -void -agb_cpy (void) -{ - printf ( - "agbsum #%" PRIX64 " - Copyright 2022-2023 Gabriel 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" - "Foundation, either version 3 of the License, or (at your option) any later\n" - "version.\n" - "\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 A\n" - "PARTICULAR PURPOSE. See the GNU General Public License for more details.\n" - "\n" - "You should have received a copy of the GNU General Public License along with\n" - "this program. If not, see .\n", - agb_rel - ); -} diff --git a/src/exi.c b/src/exi.c deleted file mode 100644 index e960706..0000000 --- a/src/exi.c +++ /dev/null @@ -1,36 +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 . -*/ - -#include - -#include -#include - -noreturn void -agb_exi (enum agb_cnd const cnd, FILE* restrict img) -{ - if (img != NULL) { fclose (img); } - - exit (cnd == agb_cnd_oky ? EXIT_SUCCESS : EXIT_FAILURE); -} diff --git a/src/getsum.c b/src/getsum.c deleted file mode 100644 index fd0e4a4..0000000 --- a/src/getsum.c +++ /dev/null @@ -1,40 +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 . -*/ - -#include - -#include - -uint8_t -agb_getsum (void const* const restrict imgptr) -{ - uint8_t const* restrict img = imgptr; - uint8_t sum = UINT8_C(0x0); - - for (char unsigned const* restrict pos = img; pos != img + agb_sumoff; ++pos) { sum += *pos; } - - sum = -(UINT8_C(0x19) + sum); - - return sum; -} diff --git a/src/hlp.c b/src/hlp.c deleted file mode 100644 index 7e10e67..0000000 --- a/src/hlp.c +++ /dev/null @@ -1,46 +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 . -*/ - -#include - -#include -#include - -void -agb_hlp (void) -{ - fputs ( - "agbsum - Patch AGB image header checksums.\n" - "\n" - "Usage: agbsum [options] \n" - "Options:\n" - " --help -h Print the help screen\n" - " -p Patch the image file\n" - " -s Don't print results\n" - " --version Print the version number\n" - "\n", - stdout - ); - agb_cpy (); -} diff --git a/src/inidat.c b/src/inidat.c deleted file mode 100644 index 05cd278..0000000 --- a/src/inidat.c +++ /dev/null @@ -1,36 +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 . -*/ - -#include - -#include - -void -agb_inidat (struct agb_dat* const restrict dat) -{ - dat->dopat = false; - dat->pth = NULL; - dat->sil = false; - dat->img = NULL; -} diff --git a/src/main.c b/src/main.c deleted file mode 100644 index d130bd3..0000000 --- a/src/main.c +++ /dev/null @@ -1,63 +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 . -*/ - -#include - -#include -#include -#include - -#if CHAR_BIT != 0x8 -#error "agbsum only support 8-bit byte systems" -#endif - -int main (int const argc, char const* const* const argv) -{ - struct agb_dat dat; - agb_inidat (&dat); - agb_chkpar (&dat, argc, argv); - - dat.img = agb_opn (dat.pth); - - char unsigned buf[agb_sumoff + 0x1]; - - agb_red (buf, dat.img); - - { - char unsigned const sum = agb_getsum (buf); - char unsigned const romsum = buf[agb_sumoff]; - - if (romsum == sum || !dat.dopat) { - // 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); - } - - agb_pat (dat.img, 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. - } - - agb_exi (agb_cnd_oky, dat.img); -} diff --git a/src/opn.c b/src/opn.c deleted file mode 100644 index 9990709..0000000 --- a/src/opn.c +++ /dev/null @@ -1,40 +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 . -*/ - -#include - -#include - -FILE* -agb_opn (char const* const restrict pth) -{ - FILE* img = fopen (pth, "r+"); - - if (img == NULL) { - fputs ("Unable to open ROM\n", stderr); - agb_exi (agb_cnd_err, NULL); - } - - return img; -} diff --git a/src/pat.c b/src/pat.c deleted file mode 100644 index 39b340f..0000000 --- a/src/pat.c +++ /dev/null @@ -1,38 +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 . -*/ - -#include - -#include - -void -agb_pat (FILE* const restrict img, char unsigned sum) -{ - fseek (img, (long)(agb_iptsrt + agb_sumoff), SEEK_SET); - - if (fwrite (&sum, 0x1u, 0x1u, img) != 0x1u) { - fputs ("Unable to patch ROM\n", stderr); - agb_exi (agb_cnd_err, img); - } -} diff --git a/src/red.c b/src/red.c deleted file mode 100644 index 64d81da..0000000 --- a/src/red.c +++ /dev/null @@ -1,42 +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 . -*/ - -#include - -#include - -void -agb_red (void* const restrict buf, FILE* restrict img) -{ - // We only need to read the part of the image - // that is used for the checksum. - fseek (img, agb_iptsrt, SEEK_SET); - - size_t const num = agb_sumoff + 0x1u; - - if (fread (buf, 0x1u, num, img) != num) { - fputs ("Unable to read ROM\n", stderr); - agb_exi (agb_cnd_err, img); - } -} -- cgit v1.2.3