From 71505bc8b94796f473b79fc3ef2f5c9250c0bed4 Mon Sep 17 00:00:00 2001 From: fosslinux Date: Fri, 12 Feb 2021 17:56:32 +1100 Subject: [PATCH] Add fletcher16 impl to mescc-tools-extra --- sysa/mescc-tools-extra/mescc-tools-extra.kaem | 37 +++++++ sysa/mescc-tools-extra/src/fletcher16.c | 103 ++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 sysa/mescc-tools-extra/src/fletcher16.c diff --git a/sysa/mescc-tools-extra/mescc-tools-extra.kaem b/sysa/mescc-tools-extra/mescc-tools-extra.kaem index 5616587..e2a0727 100755 --- a/sysa/mescc-tools-extra/mescc-tools-extra.kaem +++ b/sysa/mescc-tools-extra/mescc-tools-extra.kaem @@ -87,4 +87,41 @@ hex2 \ -o ${bindir}/chmod \ --exec_enable +# fletcher16 command +M2-Planet \ + -f /M2-Planet/test/common_x86/functions/file.c \ + -f /M2-Planet/test/common_x86/functions/exit.c \ + -f functions/numerate_number.c \ + -f functions/string.c \ + -f functions/file_print.c \ + -f functions/match.c \ + -f functions/require.c \ + -f functions/in_set.c \ + -f /M2-Planet/functions/calloc.c \ + -f /M2-Planet/test/common_x86/functions/malloc.c \ + -f fletcher16.c \ + --architecture x86 \ + --debug \ + -o fletcher16.M1 + +blood-elf -f fletcher16.M1 -o fletcher16-footer.M1 + +M1 \ + -f /M2-Planet/test/common_x86/x86_defs.M1 \ + -f /M2-Planet/test/common_x86/libc-core.M1 \ + -f fletcher16.M1 \ + -f fletcher16-footer.M1 \ + --LittleEndian \ + --architecture x86 \ + -o hold + +hex2 \ + -f /M2-Planet/test/common_x86/ELF-i386-debug.hex2 \ + -f hold \ + --LittleEndian \ + --architecture x86 \ + --BaseAddress 0x8048000 \ + -o ${bindir}/fletcher16 \ + --exec_enable + cd .. diff --git a/sysa/mescc-tools-extra/src/fletcher16.c b/sysa/mescc-tools-extra/src/fletcher16.c new file mode 100644 index 0000000..35237c5 --- /dev/null +++ b/sysa/mescc-tools-extra/src/fletcher16.c @@ -0,0 +1,103 @@ +/* + * SPDX-FileCopyrightText: 2021 fosslinux + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +#include + +#define BUF_SIZE 1024 +// CONSTANT BUF_SIZE 1024 +#define PATH_MAX 512 +// CONSTANT PATH_MAX 512 + +int fletch16(FILE* fp) +{ + long sum1 = 0; + long sum2 = 0; + int index; + int c = fgetc(fp); + while(c != EOF) + { + sum1 = (sum1 + c) % 255; + sum2 = (sum2 + sum1) % 255; + c = fgetc(fp); + } + + return (sum2 << 8) | sum1; +} + +int main(int argc, char** argv) +{ + /* Open the file containing the checksums */ + FILE* check_fp = fopen(argv[1], "r"); + require(check_fp != NULL, prepend_string( + prepend_string("Error opening checksum file ", argv[1]), "\n")); + int rc = 0; + + /* Now loop over each one */ + FILE* fp; + char c; + char* filename; + char* s_rchecksum; + char* file; + int rchecksum; + int i; + int csum; + /* It's much easier to just break; out of the loop */ + while(1) + { + filename = calloc(PATH_MAX, sizeof(char)); + require(filename != NULL, "Failed to allocate filename\n"); + s_rchecksum = calloc(16, sizeof(char)); + require(s_rchecksum != NULL, "Failed to allocate filename\n"); + /* Read the checksum */ + c = fgetc(check_fp); + i = 0; + while (c != ' ' && c != EOF) + { + require(in_set(c, "0123456789"), "Invalid checksum file\n"); + s_rchecksum[i] = c; + c = fgetc(check_fp); + i = i + 1; + } + if(c == EOF) break; + /* Skip the space */ + c = fgetc(check_fp); + /* Read the filename */ + i = 0; + while(c != '\n' && c != EOF) + { + filename[i] = c; + c = fgetc(check_fp); + i = i + 1; + } + /* If we got here and it is EOF, we probably have all the needed data */ + /* Convert s_rchecksum -> int */ + rchecksum = numerate_string(s_rchecksum); + + /* Now let's actually calculate the real checksum */ + /* Read the file into a char* array */ + fp = fopen(filename, "r"); + /* Calculate the checksum! */ + csum = fletch16(fp); + + /* Compare */ + if(csum == rchecksum) + { + file_print(prepend_string(filename, ": OK\n"), stdout); + } + else + { + /* We failed.. we should fail at the end */ + rc = 1; + file_print(prepend_string(filename, ": FAILED\n"), stdout); + } + + /* Now if we finished the last one, leave */ + if(c == EOF) break; + } + + return rc; +}