From 50ec364f5b0be713161ee3ac596ad1c7e10e8940 Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Mon, 25 Oct 2021 01:05:19 +0300 Subject: [PATCH] Add blocksum --- Makefile | 2 ++ blocksum.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 blocksum.c diff --git a/Makefile b/Makefile index 951925c..7b8903a 100644 --- a/Makefile +++ b/Makefile @@ -5,3 +5,5 @@ era_copy: era_copy.c gcc -o era_copy -Wall era_copy.c era_apply: era_apply.c gcc -o era_apply -Wall era_apply.c +blocksum: blocksum.c + gcc -O3 -lssl -lcrypto -o blocksum blocksum.c diff --git a/blocksum.c b/blocksum.c new file mode 100644 index 0000000..6c0d556 --- /dev/null +++ b/blocksum.c @@ -0,0 +1,60 @@ +/** + * Calculates md5 checksums for 4 MB blocks of input + * + * Author: Vitaliy Filippov , 2021 + * License: GNU GPLv3.0 or later + */ + +#define _GNU_SOURCE +#define _LARGEFILE64_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int narg, char *args[]) +{ + int n; + MD5_CTX ctx; + void *buf = NULL; + ssize_t done = 0, r, bufsize = 4*1024*1024, eof = 0; + uint64_t pos = 0; + unsigned char out[MD5_DIGEST_LENGTH]; + buf = malloc(bufsize); + if (!buf) + { + fprintf(stderr, "Failed to allocate buffer\n"); + exit(1); + } + while (!eof) + { + MD5_Init(&ctx); + while (done < bufsize) + { + r = read(STDIN_FILENO, buf, bufsize-done); + if (r < 0) + { + eof = 1; + break; + } + if (r > 0) + { + MD5_Update(&ctx, buf, r); + done += r; + } + } + MD5_Final(out, &ctx); + printf("%ld ", pos); + for (n = 0; n < MD5_DIGEST_LENGTH; n++) + printf("%02x", out[n]); + printf("\n"); + pos++; + done = 0; + } + return 0; +}