diff options
author | Tharre <tharre3@gmail.com> | 2016-10-31 22:31:04 +0100 |
---|---|---|
committer | Tharre <tharre3@gmail.com> | 2016-10-31 22:31:04 +0100 |
commit | 6b86ecb97dc5c49993a34092a8edc199201a2056 (patch) | |
tree | 34f4f046aa817152b49fb0fbff42a1375b4b1e41 | |
parent | 75bf720347983547c21228df71b80a0409a32e28 (diff) | |
download | redo-6b86ecb97dc5c49993a34092a8edc199201a2056.tar.gz redo-6b86ecb97dc5c49993a34092a8edc199201a2056.tar.xz redo-6b86ecb97dc5c49993a34092a8edc199201a2056.zip |
Move utility functions to util.c
-rw-r--r-- | src/build.c | 39 | ||||
-rw-r--r-- | src/util.c | 41 | ||||
-rw-r--r-- | 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+"); @@ -16,6 +16,7 @@ #include <string.h> #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; +} + @@ -10,11 +10,15 @@ #define __RUTIL_H__ #include <stddef.h> +#include <stdio.h> 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 |