From 0625beaa1d3c98298453b6fff8f663085844ea39 Mon Sep 17 00:00:00 2001 From: Magnus Ahltorp Date: Fri, 13 May 2016 14:51:53 +0200 Subject: Change endian of permdb index file to big-endian --- c_src/permdb.c | 72 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 35 insertions(+), 37 deletions(-) (limited to 'c_src/permdb.c') diff --git a/c_src/permdb.c b/c_src/permdb.c index 9610f82..04904f3 100644 --- a/c_src/permdb.c +++ b/c_src/permdb.c @@ -153,14 +153,10 @@ static const uint64_t index_file_cookie = 0xb7e16b02ba8a6d1b; static const uint64_t index_commit_cookie = 0x2fb1778c74a402e4; static const uint64_t index_node_cookie = 0x2e0f555ad73210d1; -static const uint8_t data_file_cookie[] = - {0xd5, 0x35, 0x51, 0xba, 0x53, 0x9a, 0x42, 0x52}; -static const uint8_t data_entry_cookie[] = - {0xe7, 0xc1, 0xcd, 0xc2, 0xba, 0x3d, 0xc7, 0x7c}; -static const uint8_t data_commit_start_cookie[] = - {0x75, 0xc2, 0xe4, 0xb3, 0xd5, 0xf6, 0x43, 0xa1}; -static const uint8_t data_commit_end_cookie[] = - {0x2b, 0x05, 0xee, 0xd6, 0x1b, 0x5a, 0xf5, 0x50}; +static const uint64_t data_file_cookie = 0xd53551ba539a4252; +static const uint64_t data_entry_cookie = 0xe7c1cdc2ba3dc77c; +static const uint64_t data_commit_start_cookie = 0x75c2e4b3d5f643a1; +static const uint64_t data_commit_end_cookie = 0x2b05eed61b5af550; int committree(permdb_object *state); @@ -168,7 +164,7 @@ committree(permdb_object *state); void indexfile_add_header(buffered_file *file) { - bf_add(file, &index_file_cookie, sizeof(index_file_cookie)); + bf_add_be64(file, index_file_cookie); bf_flush(file); } @@ -176,7 +172,7 @@ void datafile_add_header(buffered_file *file) { dprintf(WRITE, (stderr, "adding header to %s\n", bf_name(file))); - bf_add(file, &data_file_cookie, sizeof(data_file_cookie)); + bf_add_be64(file, data_file_cookie); bf_add_be32(file, 4096); /* Parameter 'blocksize'. */ bf_add_be32(file, 2); /* Parameter 'q'. */ bf_add_be32(file, 32); /* Parameter 'keylength'. */ @@ -240,15 +236,17 @@ verify_index_commit(buffered_file *file, node_offset offset) unsigned char *data = bf_read(file, offset, INDEX_COMMIT_TRAILER_SIZE, NULL); - if (memcmp(data + sizeof(uint64_t) + SHA256_DIGEST_SIZE, - &index_commit_cookie, sizeof(index_commit_cookie)) != 0) { + uint64_t cookie = + read_be64(data + sizeof(uint64_t) + SHA256_DIGEST_SIZE); + + if (cookie != index_commit_cookie) { fprintf(stderr, "verifying index file: incorrect commit cookie\n"); free(data); return -1; } struct commit_info commit; - commit.length = read_host64(data); + commit.length = read_be64(data); commit.start = offset + sizeof(uint64_t) - commit.length; memcpy(commit.checksum, data + sizeof(uint64_t), SHA256_DIGEST_SIZE); @@ -261,7 +259,7 @@ indexfile_verify_file(buffered_file *file) { dprintf(READ, (stderr, "verifying index file\n")); unsigned char *header = bf_read(file, 0, sizeof(index_file_cookie), NULL); - if (memcmp(header, &index_file_cookie, sizeof(index_file_cookie)) != 0) { + if (read_be64(header) != index_file_cookie) { free(header); fprintf(stderr, "verifying index file: incorrect file cookie\n"); return -1; @@ -282,8 +280,7 @@ int datafile_verify_file(buffered_file *file) { unsigned char *header = bf_read(file, 0, sizeof(data_file_cookie), NULL); - if (header == NULL || memcmp(header, &data_file_cookie, - sizeof(data_file_cookie)) != 0) { + if (header == NULL || data_file_cookie != read_be64(header)) { free(header); return -1; } @@ -319,9 +316,8 @@ read_data_commit(buffered_file *file, node_offset *offset) + sizeof(data_commit_end_cookie); unsigned char *data = bf_read(file, *offset, length, NULL); if (data == NULL - || memcmp(data + sizeof(uint32_t) + SHA256_DIGEST_SIZE, - data_commit_end_cookie, - sizeof(data_commit_end_cookie)) != 0) { + || (read_be64(data + sizeof(uint32_t) + SHA256_DIGEST_SIZE) + != data_commit_end_cookie)) { free(data); return NULL; } @@ -370,8 +366,7 @@ rebuild_index_file(permdb_object *state) if (cookie == NULL) { break; } - if (memcmp(&data_entry_cookie, cookie, - sizeof(data_entry_cookie)) == 0) { + if (data_entry_cookie == read_be64(cookie)) { size_t datalen; unsigned char *datakey = readdatakeyandlen(state, offset, &datalen); @@ -398,8 +393,7 @@ rebuild_index_file(permdb_object *state) + keylen + sizeof(uint32_t) + datalen; - } else if (memcmp(&data_commit_start_cookie, cookie, - sizeof(data_commit_start_cookie)) == 0) { + } else if (data_commit_start_cookie == read_be64(cookie)) { struct commit_info *data_commit = read_data_commit_forward(state->datafile, &offset); @@ -544,7 +538,7 @@ get_entry_in_node(node_object node, unsigned char n) static node_object unpacknode(permdb_object *state, const unsigned char *data, size_t datalen) { - if (memcmp(&index_node_cookie, data, sizeof(index_node_cookie)) != 0) { + if (index_node_cookie != read_be64(data)) { print_hex(data, sizeof(index_node_cookie)); print_hex(&index_node_cookie, sizeof(index_node_cookie)); set_error(&state->error, "incorrect magic (node) %02x%02x\n", @@ -558,7 +552,12 @@ unpacknode(permdb_object *state, const unsigned char *data, size_t datalen) node_object node; - memcpy(&node, data + sizeof(index_node_cookie), sizeof(node)); + data += 8; + + for (int i = 0; i < ENTRIESPERNODE; i++) { + node.data[i] = read_be64(data); + data += 8; + } return node; } @@ -741,8 +740,10 @@ writenode(permdb_object *state, node_object node, ps_string *cachekey) dprintf(WRITE, (stderr, "writing node: offset %llu\n", (unsigned long long) offset)); - bf_add(state->indexfile, &index_node_cookie, sizeof(index_node_cookie)); - bf_add(state->indexfile, &node, sizeof(node_object)); + bf_add_be64(state->indexfile, index_node_cookie); + for (int i = 0; i < ENTRIESPERNODE; i++) { + bf_add_be64(state->indexfile, node.data[i]); + } return offset; } @@ -780,7 +781,7 @@ readdatakey(permdb_object *state, node_offset offset) if (data == NULL) { return NULL; } - if (memcmp(&data_entry_cookie, data, sizeof(data_entry_cookie)) != 0) { + if (data_entry_cookie != read_be64(data)) { free(data); set_error(&state->error, "incorrect magic (entry) %02x%02x\n", (unsigned char)data[0], (unsigned char)data[1]); @@ -800,7 +801,7 @@ readdatakeyandlen(permdb_object *state, node_offset offset, size_t *datalen) if (data == NULL) { return NULL; } - if (memcmp(&data_entry_cookie, data, sizeof(data_entry_cookie)) != 0) { + if (data_entry_cookie != read_be64(data)) { free(data); set_error(&state->error, "incorrect magic (entry) %02x%02x\n", (unsigned char)data[0], (unsigned char)data[1]); @@ -839,7 +840,7 @@ writedata(permdb_object *state, const unsigned char *key, dprintf(WRITE, (stderr, "writing data: offset %llu\n", (unsigned long long) offset)); - bf_add(state->datafile, &data_entry_cookie, sizeof(data_entry_cookie)); + bf_add_be64(state->datafile, data_entry_cookie); bf_add(state->datafile, key, keylen); bf_add_be16(state->datafile, 1); bf_add_be16(state->datafile, datalength); @@ -1059,7 +1060,7 @@ committree(permdb_object *state) calc_padding(bf_total_length(state->datafile), 4); uint8_t padding[4] = {0, 0, 0, 0}; unsigned char data_commit_checksum[SHA256_DIGEST_SIZE]; - bf_add(state->datafile, data_commit_start_cookie, 8); + bf_add_be64(state->datafile, data_commit_start_cookie); bf_add(state->datafile, padding, data_commit_padding_size); bf_add_be32(state->datafile, bf_total_length(state->datafile) @@ -1067,8 +1068,7 @@ committree(permdb_object *state) + sizeof(uint32_t)); /* Including the length field. */ bf_sha256(state->datafile, data_commit_checksum); bf_add(state->datafile, data_commit_checksum, SHA256_DIGEST_SIZE); - bf_add(state->datafile, &data_commit_end_cookie, - sizeof(data_commit_end_cookie)); + bf_add_be64(state->datafile, data_commit_end_cookie); dprintf(WRITE, (stderr, "finished writing data commit trailer at offset %llu\n", @@ -1087,12 +1087,10 @@ committree(permdb_object *state) - bf_lastcommit(state->indexfile) + sizeof(uint64_t); /* Including the length field. */ unsigned char index_commit_checksum[SHA256_DIGEST_SIZE]; - bf_add_host64(state->indexfile, index_commit_length); + bf_add_be64(state->indexfile, index_commit_length); bf_sha256(state->indexfile, index_commit_checksum); bf_add(state->indexfile, index_commit_checksum, SHA256_DIGEST_SIZE); - bf_add(state->indexfile, - &index_commit_cookie, - sizeof(index_commit_cookie)); + bf_add_be64(state->indexfile, index_commit_cookie); dprintf(WRITE, (stderr, "finished writing index commit trailer at offset %llu\n", -- cgit v1.1