summaryrefslogtreecommitdiff
path: root/c_src
diff options
context:
space:
mode:
authorMagnus Ahltorp <map@kth.se>2016-05-13 14:51:53 +0200
committerMagnus Ahltorp <map@kth.se>2016-05-13 15:54:19 +0200
commit0625beaa1d3c98298453b6fff8f663085844ea39 (patch)
tree13890dbf8f9b0d1d733311b059d3169d5aaf2926 /c_src
parent2b669571d0820971ee25fbd6ef1810108c09f80e (diff)
Change endian of permdb index file to big-endian
Diffstat (limited to 'c_src')
-rw-r--r--c_src/filebuffer.c5
-rw-r--r--c_src/filebuffer.h2
-rw-r--r--c_src/permdb.c72
-rw-r--r--c_src/util.c8
-rw-r--r--c_src/util.h14
5 files changed, 54 insertions, 47 deletions
diff --git a/c_src/filebuffer.c b/c_src/filebuffer.c
index b8bd8ab..8285695 100644
--- a/c_src/filebuffer.c
+++ b/c_src/filebuffer.c
@@ -32,8 +32,9 @@ struct buffered_file {
};
void
-bf_add_host64(buffered_file *file, uint64_t value) {
- bf_add(file, &value, sizeof(uint64_t));
+bf_add_be64(buffered_file *file, uint64_t value) {
+ uint64_t value_be = HTONLL(value);
+ bf_add(file, &value_be, sizeof(uint64_t));
}
void
diff --git a/c_src/filebuffer.h b/c_src/filebuffer.h
index 51dc311..05c656e 100644
--- a/c_src/filebuffer.h
+++ b/c_src/filebuffer.h
@@ -28,7 +28,7 @@ const char *
bf_name(buffered_file *file);
void
-bf_add_host64(buffered_file *file, uint64_t value);
+bf_add_be64(buffered_file *file, uint64_t value);
void
bf_add_be32(buffered_file *file, uint32_t value);
void
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",
diff --git a/c_src/util.c b/c_src/util.c
index 2c41f77..05adcd7 100644
--- a/c_src/util.c
+++ b/c_src/util.c
@@ -75,17 +75,17 @@ print_hex(const void *data, int length)
}
uint64_t
-read_host64(void *ptr)
+read_be64(const void *ptr)
{
uint64_t data;
memcpy(&data, ptr, sizeof(data));
- return data;
+ return NTOHLL(data);
}
uint32_t
-read_be32(void *ptr)
+read_be32(const void *ptr)
{
uint32_t data;
@@ -95,7 +95,7 @@ read_be32(void *ptr)
}
uint16_t
-read_be16(void *ptr)
+read_be16(const void *ptr)
{
uint16_t data;
diff --git a/c_src/util.h b/c_src/util.h
index 9c2b9d6..9713eaa 100644
--- a/c_src/util.h
+++ b/c_src/util.h
@@ -15,6 +15,14 @@
#define dprintf(category,args) do { if (DEBUG_ ## category) { fprintf args; } } while (0)
#define dprinthex(category,data,size) do { if (DEBUG_ ## category) { print_hex(data, size); } } while (0)
+#ifndef HTONLL
+#define HTONLL(x) htobe64(x)
+#endif
+
+#ifndef NTOHLL
+#define NTOHLL(x) be64toh(x)
+#endif
+
void
set_error(char **error, const char * __restrict, ...) __attribute__ ((__format__ (__printf__, 2, 3)));
@@ -28,13 +36,13 @@ void
print_hex(const void *data, int length);
uint64_t
-read_host64(void *ptr);
+read_be64(const void *ptr);
uint32_t
-read_be32(void *ptr);
+read_be32(const void *ptr);
uint16_t
-read_be16(void *ptr);
+read_be16(const void *ptr);
#endif