add sun3 checksumming helper

This commit is contained in:
Romain Dolbeau
2025-06-28 14:54:41 +02:00
parent 543d35d5c4
commit 968a715cab
2 changed files with 53 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ YACC=bison -d #--report-file=bison.log --report=all
CC=gcc
CFLAGS=-O2
all: patcher genlink
all: patcher genlink sun3_checksum
%.o: %.c
$(CC) $(CFLAGS) $< -c -o $@
@@ -21,6 +21,9 @@ patcher: $(PATCHEROBJ) $(OOBJ)
genlink: $(GENLINKOBJ) $(OOBJ)
$(CC) $(CFLAGS) $^ -o $@
sun3_checksum: sun3_checksum.c
$(CC) $(CFLAGS) $< -o $@
parser_par.h: parser_par.o
parser_par.o: parser_par.y

View File

@@ -0,0 +1,49 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int
main ( int argc, char **argv )
{
int fd, r;
unsigned char* adr = NULL;
struct stat st;
size_t size, i;
unsigned short int sum;
if (argc < 2) {
fprintf(stderr, "One argument needed: path to file to checksum\n");
exit(-1);
}
fd = open(argv[1], O_RDONLY );
if (fd <= 0) {
fprintf(stderr, "Can't open file\n");
exit(-2);
}
r = fstat(fd, &st);
if (r < 0) {
fprintf(stderr, "Can't stat file\n");
exit(-3);
}
size = st.st_size;
adr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
if (adr == MAP_FAILED) {
fprintf(stderr, "Can't mmap file\n");
exit(-3);
}
sum = 0;
for (i = 0 ; i < size-2 ; i++) {
sum += adr[i];
}
printf("Checksum is 0x%04x\n", sum);
return 0;
}