| 1 |
// Start of cache.h |
| 2 |
|
| 3 |
#define CACHE_HASH_SIZE 8 // In 32-bit words. |
| 4 |
|
| 5 |
struct cache_hash { |
| 6 |
uint32_t hash[CACHE_HASH_SIZE]; |
| 7 |
}; |
| 8 |
|
| 9 |
// Initialise a blank cache. |
| 10 |
static void cache_hash_init(struct cache_hash *c); |
| 11 |
|
| 12 |
// Hash some bytes and add them to the accumulated hash. |
| 13 |
static void cache_hash(struct cache_hash *out, const char *in, size_t n); |
| 14 |
|
| 15 |
// Try to restore cache contents from a file with the given name. |
| 16 |
// Assumes the cache is invalid if it contains the given hash. |
| 17 |
// Allocates memory and reads the cache conents, which is returned in |
| 18 |
// *buf with size *buflen. If the cache is successfully loaded, this |
| 19 |
// function returns 0. Otherwise it returns nonzero. Errno is set if |
| 20 |
// the failure to load the cache is due to anything except invalid |
| 21 |
// cache conents. Note that failing to restore the cache is not |
| 22 |
// necessarily a problem: it might just be invalid or not created yet. |
| 23 |
static int cache_restore(const char *fname, const struct cache_hash *hash, |
| 24 |
unsigned char **buf, size_t *buflen); |
| 25 |
|
| 26 |
// Store cache contents in the given file, with the given hash. |
| 27 |
static int cache_store(const char *fname, const struct cache_hash *hash, |
| 28 |
const unsigned char *buf, size_t buflen); |
| 29 |
|
| 30 |
// Now for the implementation. |
| 31 |
|
| 32 |
static void cache_hash_init(struct cache_hash *c) { |
| 33 |
memset(c->hash, 0, CACHE_HASH_SIZE * sizeof(uint32_t)); |
| 34 |
} |
| 35 |
|
| 36 |
static void cache_hash(struct cache_hash *out, const char *in, size_t n) { |
| 37 |
// Adaptation of djb2 for larger output size by storing intermediate |
| 38 |
// states. |
| 39 |
uint32_t hash = 5381; |
| 40 |
for (size_t i = 0; i < n; i++) { |
| 41 |
hash = ((hash << 5) + hash) + in[i]; |
| 42 |
out->hash[i % CACHE_HASH_SIZE] ^= hash; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
#define CACHE_HEADER_SIZE 8 |
| 47 |
static const char cache_header[CACHE_HEADER_SIZE] = "FUTHARK\0"; |
| 48 |
|
| 49 |
static int cache_restore(const char *fname, const struct cache_hash *hash, |
| 50 |
unsigned char **buf, size_t *buflen) { |
| 51 |
FILE *f = fopen(fname, "rb"); |
| 52 |
|
| 53 |
if (f == NULL) { |
| 54 |
return 1; |
| 55 |
} |
| 56 |
|
| 57 |
char f_header[CACHE_HEADER_SIZE]; |
| 58 |
|
| 59 |
if (fread(f_header, sizeof(char), CACHE_HEADER_SIZE, f) != CACHE_HEADER_SIZE) { |
| 60 |
goto error; |
| 61 |
} |
| 62 |
|
| 63 |
if (memcmp(f_header, cache_header, CACHE_HEADER_SIZE) != 0) { |
| 64 |
goto error; |
| 65 |
} |
| 66 |
|
| 67 |
if (fseek(f, 0, SEEK_END) != 0) { |
| 68 |
goto error; |
| 69 |
} |
| 70 |
int64_t f_size = (int64_t)ftell(f); |
| 71 |
if (fseek(f, CACHE_HEADER_SIZE, SEEK_SET) != 0) { |
| 72 |
goto error; |
| 73 |
} |
| 74 |
|
| 75 |
int64_t expected_size; |
| 76 |
|
| 77 |
if (fread(&expected_size, sizeof(int64_t), 1, f) != 1) { |
| 78 |
goto error; |
| 79 |
} |
| 80 |
|
| 81 |
if (f_size != expected_size) { |
| 82 |
errno = 0; |
| 83 |
goto error; |
| 84 |
} |
| 85 |
|
| 86 |
int32_t f_hash[CACHE_HASH_SIZE]; |
| 87 |
|
| 88 |
if (fread(f_hash, sizeof(int32_t), CACHE_HASH_SIZE, f) != CACHE_HASH_SIZE) { |
| 89 |
goto error; |
| 90 |
} |
| 91 |
|
| 92 |
if (memcmp(f_hash, hash->hash, CACHE_HASH_SIZE) != 0) { |
| 93 |
errno = 0; |
| 94 |
goto error; |
| 95 |
} |
| 96 |
|
| 97 |
*buflen = f_size - CACHE_HEADER_SIZE - sizeof(int64_t) - CACHE_HASH_SIZE*sizeof(int32_t); |
| 98 |
*buf = malloc(*buflen); |
| 99 |
if (fread(*buf, sizeof(char), *buflen, f) != *buflen) { |
| 100 |
free(*buf); |
| 101 |
goto error; |
| 102 |
} |
| 103 |
|
| 104 |
fclose(f); |
| 105 |
|
| 106 |
return 0; |
| 107 |
|
| 108 |
error: |
| 109 |
fclose(f); |
| 110 |
return 1; |
| 111 |
} |
| 112 |
|
| 113 |
static int cache_store(const char *fname, const struct cache_hash *hash, |
| 114 |
const unsigned char *buf, size_t buflen) { |
| 115 |
FILE *f = fopen(fname, "wb"); |
| 116 |
|
| 117 |
if (f == NULL) { |
| 118 |
return 1; |
| 119 |
} |
| 120 |
|
| 121 |
if (fwrite(cache_header, CACHE_HEADER_SIZE, 1, f) != 1) { |
| 122 |
goto error; |
| 123 |
} |
| 124 |
|
| 125 |
int64_t size = CACHE_HEADER_SIZE + sizeof(int64_t) + CACHE_HASH_SIZE*sizeof(int32_t) + buflen; |
| 126 |
|
| 127 |
if (fwrite(&size, sizeof(size), 1, f) != 1) { |
| 128 |
goto error; |
| 129 |
} |
| 130 |
|
| 131 |
if (fwrite(hash->hash, sizeof(int32_t), CACHE_HASH_SIZE, f) != CACHE_HASH_SIZE) { |
| 132 |
goto error; |
| 133 |
} |
| 134 |
|
| 135 |
if (fwrite(buf, sizeof(unsigned char), buflen, f) != buflen) { |
| 136 |
goto error; |
| 137 |
} |
| 138 |
|
| 139 |
fclose(f); |
| 140 |
|
| 141 |
return 0; |
| 142 |
|
| 143 |
error: |
| 144 |
fclose(f); |
| 145 |
return 1; |
| 146 |
} |
| 147 |
|
| 148 |
// End of cache.h |