From 192221af22b0112a36b361a048a725809f458145 Mon Sep 17 00:00:00 2001 From: fosslinux Date: Fri, 12 Feb 2021 18:20:18 +1100 Subject: [PATCH] Add fletcher16-gen developer util 1. I'm not convinced our fletcher16 implementation is proper 2. It is not in coreutils So we add some basic code to do that. This is also the first dev-util, so add some documentation to DEVEL.md. --- DEVEL.md | 10 ++ dev-utils/fletcher16-gen/.gitignore | 8 + .../fletcher16-gen/compile-fletcher16-gen.sh | 42 +++++ dev-utils/fletcher16-gen/fletcher16-gen.c | 40 +++++ dev-utils/m2-functions/file_print.c | 17 ++ dev-utils/m2-functions/in_set.c | 21 +++ dev-utils/m2-functions/match.c | 24 +++ dev-utils/m2-functions/numerate_number.c | 159 ++++++++++++++++++ dev-utils/m2-functions/require.c | 20 +++ dev-utils/m2-functions/string.c | 59 +++++++ 10 files changed, 400 insertions(+) create mode 100644 dev-utils/fletcher16-gen/.gitignore create mode 100755 dev-utils/fletcher16-gen/compile-fletcher16-gen.sh create mode 100644 dev-utils/fletcher16-gen/fletcher16-gen.c create mode 100644 dev-utils/m2-functions/file_print.c create mode 100644 dev-utils/m2-functions/in_set.c create mode 100644 dev-utils/m2-functions/match.c create mode 100644 dev-utils/m2-functions/numerate_number.c create mode 100644 dev-utils/m2-functions/require.c create mode 100644 dev-utils/m2-functions/string.c diff --git a/DEVEL.md b/DEVEL.md index de2ba82..5b16fc3 100644 --- a/DEVEL.md +++ b/DEVEL.md @@ -49,6 +49,16 @@ Permissable folders: - `src`: the upstream unmodified source code. This may be either a submodule or nonexistant. +Furthermore, there is a special top-level dir `dev-utils`. In here are +appropriate utilities that are often useful for development and not generally +included on normal systems, or are specific to live-bootstrap. Each program +should be contained within its own directory and include: + +- source code +- compilation script + +The directory m2-functions is used for M2-Planet functions (currently). + ## Conventions - **Patches:** diff --git a/dev-utils/fletcher16-gen/.gitignore b/dev-utils/fletcher16-gen/.gitignore new file mode 100644 index 0000000..935df1d --- /dev/null +++ b/dev-utils/fletcher16-gen/.gitignore @@ -0,0 +1,8 @@ +# SPDX-FileCopyrightText: 2021 fosslinux +# +# SPDX-License-Identifier: MIT + +fletcher16.M1 +fletcher16-footer.M1 +fletcher16.hex2 +fletcher16-gen diff --git a/dev-utils/fletcher16-gen/compile-fletcher16-gen.sh b/dev-utils/fletcher16-gen/compile-fletcher16-gen.sh new file mode 100755 index 0000000..6c6d532 --- /dev/null +++ b/dev-utils/fletcher16-gen/compile-fletcher16-gen.sh @@ -0,0 +1,42 @@ +#!/bin/sh + +# SPDX-FileCopyrightText: 2021 fosslinux +# +# SPDX-License-Identifier: GPL-3.0-or-later + +set -ex + +M2-Planet \ + --architecture x86 \ + -f ../m2-functions/in_set.c \ + -f ../m2-functions/file_print.c \ + -f ../m2-functions/numerate_number.c \ + -f ../m2-functions/string.c \ + -f ../../sysa/mescc-tools-seed/src/mescc-tools-seed/M2-Planet/test/common_x86/functions/file.c \ + -f ../../sysa/mescc-tools-seed/src/mescc-tools-seed/M2-Planet/test/common_x86/functions/exit.c \ + -f ../m2-functions/require.c \ + -f ../../sysa/mescc-tools-seed/src/mescc-tools-seed/M2-Planet/test/common_x86/functions/malloc.c \ + -f ../../sysa/mescc-tools-seed/src/mescc-tools-seed/M2-Planet/functions/calloc.c \ + -f fletcher16-gen.c \ + -o fletcher16.M1 \ + --debug + +blood-elf -f fletcher16.M1 -o fletcher16-footer.M1 + +M1 \ + -f ../../sysa/mescc-tools-seed/src/mescc-tools-seed/M2-Planet/test/common_x86/x86_defs.M1 \ + -f ../../sysa/mescc-tools-seed/src/mescc-tools-seed/M2-Planet/test/common_x86/libc-core.M1 \ + -f fletcher16.M1 \ + -f fletcher16-footer.M1 \ + --LittleEndian \ + --architecture x86 \ + -o fletcher16.hex2 + +hex2 \ + -f ../../sysa/mescc-tools-seed/src/mescc-tools-seed/M2-Planet/test/common_x86/ELF-i386-debug.hex2 \ + -f fletcher16.hex2 \ + --LittleEndian \ + --architecture x86 \ + --BaseAddress 0x8048000 \ + -o fletcher16-gen \ + --exec_enable diff --git a/dev-utils/fletcher16-gen/fletcher16-gen.c b/dev-utils/fletcher16-gen/fletcher16-gen.c new file mode 100644 index 0000000..849640a --- /dev/null +++ b/dev-utils/fletcher16-gen/fletcher16-gen.c @@ -0,0 +1,40 @@ +/* + * 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* fp = fopen(argv[1], "r"); + require(fp != NULL, prepend_string( + prepend_string("Error opening checksum file ", argv[1]), "\n")); + int csum = fletch16(fp); + file_print(prepend_string(prepend_string(prepend_string( + numerate_number(csum), " "), argv[1]), "\n"), stdout); +} diff --git a/dev-utils/m2-functions/file_print.c b/dev-utils/m2-functions/file_print.c new file mode 100644 index 0000000..2b1ce25 --- /dev/null +++ b/dev-utils/m2-functions/file_print.c @@ -0,0 +1,17 @@ +/* + * SPDX-FileCopyrightText: 2016 Jeremiah Orians + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +// void fputc(char s, FILE* f); + +void file_print(char* s, FILE* f) +{ + while(0 != s[0]) + { + fputc(s[0], f); + s = s + 1; + } +} diff --git a/dev-utils/m2-functions/in_set.c b/dev-utils/m2-functions/in_set.c new file mode 100644 index 0000000..88c8396 --- /dev/null +++ b/dev-utils/m2-functions/in_set.c @@ -0,0 +1,21 @@ +/* + * SPDX-FileCopyrightText: 2016 Jeremiah Orians + * SPDX-FileCopyrightText: 2018 Jan (janneke) Nieuwenhuizen + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#define FALSE 0 +// CONSTANT FALSE 0 +#define TRUE 1 +// CONSTANT TRUE 1 + +int in_set(int c, char* s) +{ + while(0 != s[0]) + { + if(c == s[0]) return TRUE; + s = s + 1; + } + return FALSE; +} diff --git a/dev-utils/m2-functions/match.c b/dev-utils/m2-functions/match.c new file mode 100644 index 0000000..f9e41b1 --- /dev/null +++ b/dev-utils/m2-functions/match.c @@ -0,0 +1,24 @@ +/* + * SPDX-FileCopyrightText: 2016 Jeremiah Orians + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#define FALSE 0 +// CONSTANT FALSE 0 +#define TRUE 1 +// CONSTANT TRUE 1 + +int match(char* a, char* b) +{ + int i = -1; + do + { + i = i + 1; + if(a[i] != b[i]) + { + return FALSE; + } + } while((0 != a[i]) && (0 !=b[i])); + return TRUE; +} diff --git a/dev-utils/m2-functions/numerate_number.c b/dev-utils/m2-functions/numerate_number.c new file mode 100644 index 0000000..2c01314 --- /dev/null +++ b/dev-utils/m2-functions/numerate_number.c @@ -0,0 +1,159 @@ +/* + * SPDX-FileCopyrightText: 2016 Jeremiah Orians + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +// void* calloc(int count, int size); +#define TRUE 1 +//CONSTANT TRUE 1 +#define FALSE 0 +//CONSTANT FALSE 0 +int in_set(int c, char* s); +//CONSTANT NULL 0 + +char* numerate_number(int a) +{ + char* result = calloc(16, sizeof(char)); + if(NULL == result) return NULL; + + int i = 0; + + /* Deal with Zero case */ + if(0 == a) + { + result[0] = '0'; + return result; + } + + /* Deal with negatives */ + if(0 > a) + { + result[0] = '-'; + i = 1; + a = a * -1; + } + + /* Using the largest 10^n number possible in 32bits */ + int divisor = 0x3B9ACA00; + /* Skip leading Zeros */ + while(0 == (a / divisor)) divisor = divisor / 10; + + /* Now simply collect numbers until divisor is gone */ + while(0 < divisor) + { + result[i] = ((a / divisor) + 48); + a = a % divisor; + divisor = divisor / 10; + i = i + 1; + } + + return result; +} + +int char2hex(int c) +{ + if (c >= '0' && c <= '9') return (c - 48); + else if (c >= 'a' && c <= 'f') return (c - 87); + else if (c >= 'A' && c <= 'F') return (c - 55); + else return -1; +} + +int hex2char(int c) +{ + if((c >= 0) && (c <= 9)) return (c + 48); + else if((c >= 10) && (c <= 15)) return (c + 55); + else return -1; +} + +int char2dec(int c) +{ + if (c >= '0' && c <= '9') return (c - 48); + else return -1; +} + +int dec2char(int c) +{ + if((c >= 0) && (c <= 9)) return (c + 48); + else return -1; +} + +int index_number(char* s, char c) +{ + int i = 0; + while(s[i] != c) + { + i = i + 1; + if(0 == s[i]) return -1; + } + return i; +} + +int toupper(int c) +{ + if(in_set(c, "abcdefghijklmnopqrstuvwxyz")) return (c & 0xDF); + return c; +} + +int set_reader(char* set, int mult, char* input) +{ + int n = 0; + int i = 0; + int hold; + int negative_p = FALSE; + + if(input[0] == '-') + { + negative_p = TRUE; + i = i + 1; + } + + while(in_set(input[i], set)) + { + n = n * mult; + hold = index_number(set, toupper(input[i])); + + /* Input managed to change between in_set and index_number */ + if(-1 == hold) return 0; + n = n + hold; + i = i + 1; + } + + /* loop exited before NULL and thus invalid input */ + if(0 != input[i]) return 0; + + if(negative_p) + { + n = 0 - n; + } + + return n; +} + +int numerate_string(char *a) +{ + /* If NULL string */ + if(0 == a[0]) return 0; + + /* Deal with binary*/ + else if ('0' == a[0] && 'b' == a[1]) + { + return set_reader("01", 2, a+2); + } + /* Deal with hex */ + else if ('0' == a[0] && 'x' == a[1]) + { + return set_reader("0123456789ABCDEFabcdef", 16, a+2); + } + /* Deal with octal */ + else if('0' == a[0]) + { + return set_reader("01234567", 8, a+1); + } + /* Deal with decimal */ + else + { + return set_reader("0123456789", 10, a); + } +} diff --git a/dev-utils/m2-functions/require.c b/dev-utils/m2-functions/require.c new file mode 100644 index 0000000..7fd2dd9 --- /dev/null +++ b/dev-utils/m2-functions/require.c @@ -0,0 +1,20 @@ +/* + * SPDX-FileCopyrightText: 2016 Jeremiah Orians + * SPDX-FileCopyrightText: 2018 Jan (janneke) Nieuwenhuizen + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +#include + +void file_print(char* s, FILE* f); + +void require(int bool, char* error) +{ + if(!bool) + { + file_print(error, stderr); + exit(EXIT_FAILURE); + } +} diff --git a/dev-utils/m2-functions/string.c b/dev-utils/m2-functions/string.c new file mode 100644 index 0000000..4bae890 --- /dev/null +++ b/dev-utils/m2-functions/string.c @@ -0,0 +1,59 @@ +/* + * SPDX-FileCopyrightText: 2016 Jeremiah Orians + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +#include +#define MAX_STRING 4096 +//CONSTANT MAX_STRING 4096 +// void* calloc(int count, int size); +void file_print(char* s, FILE* f); + +char* copy_string(char* target, char* source) +{ + while(0 != source[0]) + { + target[0] = source[0]; + target = target + 1; + source = source + 1; + } + return target; +} + +char* postpend_char(char* s, char a) +{ + char* ret = calloc(MAX_STRING, sizeof(char)); + if(NULL == ret) return NULL; + + char* hold = copy_string(ret, s); + hold[0] = a; + return ret; +} + +char* prepend_char(char a, char* s) +{ + char* ret = calloc(MAX_STRING, sizeof(char)); + if(NULL == ret) return NULL; + + ret[0] = a; + copy_string((ret+1), s); + return ret; +} + +char* prepend_string(char* add, char* base) +{ + char* ret = calloc(MAX_STRING, sizeof(char)); + if(NULL == ret) return NULL; + + copy_string(copy_string(ret, add), base); + return ret; +} + +int string_length(char* a) +{ + int i = 0; + while(0 != a[i]) i = i + 1; + return i; +}