Browse Source

Create binary key objects from key:hexdata pairs

master
ckolivas 9 years ago
parent
commit
485b9e1076
  1. 111
      src/libckpool.c
  2. 8
      src/libckpool.h

111
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))

8
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__)

Loading…
Cancel
Save