summaryrefslogtreecommitdiff
path: root/c_src/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'c_src/util.c')
-rw-r--r--c_src/util.c105
1 files changed, 105 insertions, 0 deletions
diff --git a/c_src/util.c b/c_src/util.c
new file mode 100644
index 0000000..76294eb
--- /dev/null
+++ b/c_src/util.c
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2015, NORDUnet A/S.
+ * See LICENSE for licensing information.
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <assert.h>
+#include <stdarg.h>
+#include <stdint.h>
+#include <arpa/inet.h>
+#include <fcntl.h>
+#include <err.h>
+#include <nettle/sha2.h>
+#include "erlport.h"
+#include "permdb.h"
+#include "util.h"
+
+void
+set_error(char **error, const char *format, ...)
+{
+ va_list args;
+ int ret;
+
+ if (error == NULL) {
+ return;
+ }
+
+ va_start(args, format);
+ char *formatted = NULL;
+ ret = vasprintf (&formatted, format, args);
+ if (ret == -1) {
+ fprintf(stderr, "vasprintf error\n");
+ return;
+ }
+ if (*error) {
+ free(*error);
+ }
+ fprintf(stderr, "error: %s\n", formatted);
+ *error = formatted;
+ va_end(args);
+}
+
+int
+calc_padding(int offset, int alignment)
+{
+ int misalign = offset % alignment;
+ if (misalign == 0) {
+ return 0;
+ }
+ return alignment - misalign;
+}
+
+void
+print_entry(node_object node)
+{
+ for (int i = 0; i < entriespernode; i++) {
+ fprintf(stderr, " %016llx", (long long unsigned int)node.data[i]);
+ }
+ fprintf(stderr, "\n");
+}
+
+void
+print_hex(const void *data, int length)
+{
+ int i;
+ for (i = 0; i < length; i++) {
+ fprintf(stderr, "%02X ", ((unsigned char *)data)[i]);
+ }
+ fprintf(stderr, "\n");
+}
+
+uint64_t
+read_host64(void *ptr)
+{
+ uint64_t data;
+
+ memcpy(&data, ptr, sizeof(data));
+
+ return data;
+}
+
+uint32_t
+read_be32(void *ptr)
+{
+ uint32_t data;
+
+ memcpy(&data, ptr, sizeof(data));
+
+ return ntohl(data);
+}
+
+uint16_t
+read_be16(void *ptr)
+{
+ uint16_t data;
+
+ memcpy(&data, ptr, sizeof(data));
+
+ return ntohs(data);
+}
+