Browse Source

reduce fletcher16_file() to a single function

master
Shokara 8 months ago
parent
commit
dcf12811b2
Signed by: shokara GPG Key ID: 7F5702DEF8E6DC82
  1. 27
      hash.c
  2. 2
      hash.h

27
hash.c

@ -3,34 +3,15 @@
#include <string.h>
#include "hash.h"
#define BLOCKSIZE 8192
unsigned int fletcher16(const char *data, size_t length)
unsigned int fletcher16_file(FILE *fp)
{
unsigned int sum1 = 0, sum2 = 0;
size_t i = 0;
int c;
while (i++ < length) {
sum1 += (sum1 + data[i]) % 255;
while ((c = fgetc(fp)) != EOF) {
sum1 += (sum1 + c) % 255;
sum2 += (sum2 + sum1) % 255;
}
return (sum2 << 8) | sum1;
}
unsigned int fletcher16_file(const char *filepath, size_t length)
{
char buf[BLOCKSIZE];
FILE *fp;
unsigned int digest = 0;
if (!(fp = fopen(filepath, "rb")))
return 1;
while (!feof(fp)) {
fread(buf, 1, BLOCKSIZE, fp);
digest = fletcher16(buf, length);
}
return digest;
}

2
hash.h

@ -1 +1 @@
unsigned int fletcher16_file(const char *filepath, size_t length);
unsigned int fletcher16_file(FILE *fp);
Loading…
Cancel
Save