From 6b86ecb97dc5c49993a34092a8edc199201a2056 Mon Sep 17 00:00:00 2001 From: Tharre Date: Mon, 31 Oct 2016 22:31:04 +0100 Subject: Move utility functions to util.c --- src/build.c | 39 --------------------------------------- src/util.c | 41 +++++++++++++++++++++++++++++++++++++++++ src/util.h | 4 ++++ 3 files changed, 45 insertions(+), 39 deletions(-) diff --git a/src/build.c b/src/build.c index c79cf73..8253319 100644 --- a/src/build.c +++ b/src/build.c @@ -384,25 +384,6 @@ void add_prereq_path(const char *target, const char *parent, int ident) { free(reltarget); } -/* Hash the target file, returning a pointer to the heap allocated hash. */ -static unsigned char *hash_file(FILE *fp) { - unsigned char *hash = xmalloc(20); - - SHA_CTX context; - unsigned char data[8192]; - size_t read; - - SHA1_Init(&context); - while ((read = fread(data, 1, sizeof data, fp))) - SHA1_Update(&context, data, read); - - if (ferror(fp)) - fatal("redo: failed to read data"); - SHA1_Final(hash, &context); - - return hash; -} - /* Update hash & ctime information stored in the given dep_info struct */ static void update_dep_info(dep_info *dep, const char *target) { FILE *fp = fopen(target, "rb"); @@ -418,26 +399,6 @@ static void update_dep_info(dep_info *dep, const char *target) { fclose(fp); } -/* Requires a buffer of at least 20*2+1 = 41 bytes */ -static void sha1_to_hex(const unsigned char *sha1, char *buf) { - static const char hex[] = "0123456789abcdef"; - - for (int i = 0; i < 20; ++i) { - char *pos = buf + i*2; - *pos++ = hex[sha1[i] >> 4]; - *pos = hex[sha1[i] & 0xf]; - } - - buf[40] = '\0'; -} - -static void hex_to_sha1(const char *s, unsigned char *sha1) { - static const char hex[] = "0123456789abcdef"; - - for (; *s; s += 2, ++sha1) - *sha1 = ((strchr(hex, *s) - hex) << 4) + strchr(hex, *(s+1)) - hex; -} - /* Write the dependency information into the specified path. */ static void write_dep_information(dep_info *dep) { FILE *fd = fopen(dep->path, "w+"); diff --git a/src/util.c b/src/util.c index bd7e0bf..35db2e3 100644 --- a/src/util.c +++ b/src/util.c @@ -16,6 +16,7 @@ #include #include "util.h" +#include "sha1.h" #define _FILENAME "util.c" #include "dbg.h" @@ -80,3 +81,43 @@ char *concat(size_t count, ...) { va_end(ap2); return result; } + +/* Hash the target file, returning a pointer to the heap allocated hash. */ +unsigned char *hash_file(FILE *fp) { + unsigned char *hash = xmalloc(20); + + SHA_CTX context; + unsigned char data[8192]; + size_t read; + + SHA1_Init(&context); + while ((read = fread(data, 1, sizeof data, fp))) + SHA1_Update(&context, data, read); + + if (ferror(fp)) + fatal("redo: failed to read data"); + SHA1_Final(hash, &context); + + return hash; +} + +/* Requires a buffer of at least 20*2+1 = 41 bytes */ +void sha1_to_hex(const unsigned char *sha1, char *buf) { + static const char hex[] = "0123456789abcdef"; + + for (int i = 0; i < 20; ++i) { + char *pos = buf + i*2; + *pos++ = hex[sha1[i] >> 4]; + *pos = hex[sha1[i] & 0xf]; + } + + buf[40] = '\0'; +} + +void hex_to_sha1(const char *s, unsigned char *sha1) { + static const char hex[] = "0123456789abcdef"; + + for (; *s; s += 2, ++sha1) + *sha1 = ((strchr(hex, *s) - hex) << 4) + strchr(hex, *(s+1)) - hex; +} + diff --git a/src/util.h b/src/util.h index 5e46166..159626d 100644 --- a/src/util.h +++ b/src/util.h @@ -10,11 +10,15 @@ #define __RUTIL_H__ #include +#include extern void __attribute__((noreturn)) die_(const char *err, ...); extern void *xmalloc(size_t size); extern void *xrealloc(void *ptr, size_t size); extern char *xstrdup(const char *str); extern char *concat(size_t count, ...); +extern unsigned char *hash_file(FILE *fp); +extern void sha1_to_hex(const unsigned char *sha1, char *buf); +extern void hex_to_sha1(const char *s, unsigned char *sha1); #endif -- cgit v1.2.3-70-g09d2