From 5270d41d8dd5329f5a5327832c0124bf86cdd6ce Mon Sep 17 00:00:00 2001 From: Stefan Frederik Date: Fri, 10 Dec 2021 03:05:53 +0100 Subject: [PATCH] better hash_file function (more precise cr/lf scanning) --- src/actions.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/actions.c b/src/actions.c index 21f57506..ad2eee11 100644 --- a/src/actions.c +++ b/src/actions.c @@ -39,6 +39,7 @@ unsigned int hash_file(const char *f) { int fd; int n, i; + int cr = 0; unsigned int h=5381; unsigned char line[4096]; fd = open(f, O_RDONLY); @@ -46,10 +47,19 @@ unsigned int hash_file(const char *f) while( (n = read(fd, line, sizeof(line))) ) { for(i = 0; i < n; i++) { /* skip CRs so hashes will match on unix / windows */ - if(i < n - 1 && line[i] == '\r' && line[i+1] == '\n') continue; + if(line[i] == '\r') { + cr = 1; + continue; + } else if(line[i] == '\n' && cr) { + cr = 0; + } else if(cr) { /* no skip \r if not followed by \n */ + cr = 0; + h += (h << 5) + '\r'; + } h += (h << 5) + line[i]; } } + if(cr) h += (h << 5) + '\r'; /* file ends with \r not followed by \n: keep it */ close(fd); return h; } else {