From 485b9e10764e07f60351515ab7b90e82b232284f Mon Sep 17 00:00:00 2001 From: ckolivas Date: Mon, 25 Jan 2016 08:50:22 +1100 Subject: [PATCH] Create binary key objects from key:hexdata pairs --- src/libckpool.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++++ src/libckpool.h | 8 ++++ 2 files changed, 119 insertions(+) diff --git a/src/libckpool.c b/src/libckpool.c index 3888efc2..64e7d3d9 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -1251,6 +1251,117 @@ out: } +/* Create an empty binary key object */ +char *bkey_object(void) +{ + char *bkey = ckzalloc(PAGESIZE); + uint32_t *lenptr; + + lenptr = (uint32_t *)bkey; + *lenptr = htole32(4); + return bkey; +} + +/* Add binary from hex to a bkey message */ +void _bkey_add_hex(char **bkey, const char *key, const char *hex, const char *file, const char *func, const int line) +{ + uint32_t msglen, *lenptr, newlen; + int hlen, len; + + if (unlikely(!*bkey || !key || !hex)) { + LOGEMERG("Null sent to bkey_add from %s %s:%d", + file, func, line); + return; + } + hlen = strlen(hex) / 2; + if (unlikely(!hlen)) { + LOGERR("Zero length hex sent to bkey_add from %s %s:%d", + file, func, line); + return; + } + len = strlen(key); + if (unlikely(!len)) { + LOGERR("Zero length key sent to bkey_add from %s %s:%d", + file, func, line); + return; + } + /* Null terminator */ + len += 1; + + /* Get current message length */ + lenptr = (uint32_t *)*bkey; + msglen = le32toh(*lenptr); + + /* Add $key+length+bin */ + newlen = len + 4 + hlen; + *bkey = realloc(*bkey, round_up_page(newlen)); + + /* Append keyname */ + sprintf(*bkey + msglen, "%s", key); + msglen += len; + + /* Append bin length */ + lenptr = (uint32_t *)(*bkey + msglen); + *lenptr = htole32(hlen); + msglen += 4; + + /* Append binary data */ + hex2bin(*bkey + msglen, hex, hlen); + + /* Adjust message length header */ + lenptr = (uint32_t *)*bkey; + *lenptr = htole32(newlen); +} + +void _bkey_add_bin(char **bkey, const char *key, const char *bin, const int blen, const char *file, const char *func, const int line) +{ uint32_t msglen, *lenptr, newlen; + int len; + + if (unlikely(!*bkey || !key || !bin)) { + LOGEMERG("Null sent to bkey_add from %s %s:%d", + file, func, line); + return; + } + if (unlikely(!blen)) { + LOGERR("Zero length bin sent to bkey_add from %s %s:%d", + file, func, line); + return; + } + len = strlen(key); + if (unlikely(!len)) { + LOGERR("Zero length key sent to bkey_add from %s %s:%d", + file, func, line); + return; + } + /* Null terminator */ + len += 1; + + /* Get current message length */ + lenptr = (uint32_t *)*bkey; + msglen = le32toh(*lenptr); + + /* Add $key+length+bin */ + newlen = len + 4 + blen; + *bkey = realloc(*bkey, round_up_page(newlen)); + + /* Append keyname */ + sprintf(*bkey + msglen, "%s", key); + msglen += len; + + /* Append bin length */ + lenptr = (uint32_t *)(*bkey + msglen); + *lenptr = htole32(blen); + msglen += 4; + + /* Append binary data */ + memcpy(*bkey + msglen, bin, blen); + + /* Adjust message length header */ + lenptr = (uint32_t *)*bkey; + *lenptr = htole32(newlen); +} + + void _json_check(json_t *val, json_error_t *err, const char *file, const char *func, const int line) { if (likely(val)) diff --git a/src/libckpool.h b/src/libckpool.h index 0faf8447..20fe8542 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -318,6 +318,14 @@ struct unixsock { typedef struct unixsock unixsock_t; + +char *bkey_object(void); +void _bkey_add_hex(char **bkey, const char *key, const char *hex, const char *file, const char *func, const int line); +#define bkey_add_hex(bkey, key, hex) _bkey_add_hex(&(bkey), key, hex, __FILE__, __func__, __LINE__) +void _bkey_add_bin(char **bkey, const char *key, const char *bin, const int blen, const char *file, const char *func, const int line); +#define bkey_add_bin(bkey, key, bin) _bkey_add_bin(&(bkey), key, bin, __FILE__, __func__, __LINE__) + + void _json_check(json_t *val, json_error_t *err, const char *file, const char *func, const int line); #define json_check(VAL, ERR) _json_check(VAL, ERR, __FILE__, __func__, __LINE__)