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.
This commit is contained in:
fosslinux 2021-02-12 18:20:18 +11:00
parent 71505bc8b9
commit 192221af22
10 changed files with 400 additions and 0 deletions

View File

@ -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:**

8
dev-utils/fletcher16-gen/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# SPDX-FileCopyrightText: 2021 fosslinux <fosslinux@aussies.space>
#
# SPDX-License-Identifier: MIT
fletcher16.M1
fletcher16-footer.M1
fletcher16.hex2
fletcher16-gen

View File

@ -0,0 +1,42 @@
#!/bin/sh
# SPDX-FileCopyrightText: 2021 fosslinux <fosslinux@aussies.space>
#
# 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

View File

@ -0,0 +1,40 @@
/*
* SPDX-FileCopyrightText: 2021 fosslinux <fosslinux@aussies.space>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <stdlib.h>
#include <stdio.h>
#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);
}

View File

@ -0,0 +1,17 @@
/*
* SPDX-FileCopyrightText: 2016 Jeremiah Orians
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include<stdio.h>
// void fputc(char s, FILE* f);
void file_print(char* s, FILE* f)
{
while(0 != s[0])
{
fputc(s[0], f);
s = s + 1;
}
}

View File

@ -0,0 +1,21 @@
/*
* SPDX-FileCopyrightText: 2016 Jeremiah Orians
* SPDX-FileCopyrightText: 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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;
}

View File

@ -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;
}

View File

@ -0,0 +1,159 @@
/*
* SPDX-FileCopyrightText: 2016 Jeremiah Orians
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include<stdlib.h>
// 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);
}
}

View File

@ -0,0 +1,20 @@
/*
* SPDX-FileCopyrightText: 2016 Jeremiah Orians
* SPDX-FileCopyrightText: 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include<stdio.h>
#include<stdlib.h>
void file_print(char* s, FILE* f);
void require(int bool, char* error)
{
if(!bool)
{
file_print(error, stderr);
exit(EXIT_FAILURE);
}
}

View File

@ -0,0 +1,59 @@
/*
* SPDX-FileCopyrightText: 2016 Jeremiah Orians
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include<stdlib.h>
#include<stdio.h>
#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;
}