aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTristan Riehs <tristan.riehs@inria.fr>2025-12-25 12:57:27 +0100
committerTristan Riehs <tristan.riehs@inria.fr>2025-12-25 12:57:27 +0100
commit73be01623bc2b1cb1618d283fff07fe96cd87499 (patch)
tree51262efec54bd81adc83195dea013bc0efe4a535 /src
parent1a40d5e2d0eccce8822b2b4c53cd29048c768d97 (diff)
Add routine for computing file sum
Diffstat (limited to 'src')
-rw-r--r--src/main.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c
index 7bae17c..0dd5633 100644
--- a/src/main.c
+++ b/src/main.c
@@ -95,6 +95,36 @@ static void sanitize_sql_str(char **str)
*str = new_str;
}
+/* Compute something that identifies the content of FILE and write it to
+ OUT. Algorithm from http://www.cse.yorku.ca/~oz/hash.html via
+ https://stackoverflow.com/a/7666577/20138083. Return 0 on success, -1 on
+ error. */
+static int sum(uint64_t *out, const char *file)
+{
+ FILE *stream = fopen(file, "r");
+ uint8_t buf[4096];
+ size_t buf_len;
+ uint64_t sum = 5381;
+ if (!file) {
+ perror("fopen");
+ return -1;
+ }
+ while ((buf_len = fread(&buf, 1, sizeof(buf), stream)) != 0) {
+ for (int i = 0; i < buf_len; i++) {
+ uint8_t next = buf[i];
+ sum = sum*33 + next;
+ }
+ }
+ if (ferror(stream)) {
+ perror("fread");
+ fclose(stream);
+ return -1;
+ }
+ fclose(stream);
+ *out = sum;
+ return 0;
+}
+
static time_t time_from_str(const char *str)
{
int rc;