if(esize) {
if(type == HMF_STR)
f->hmf_str = f->hmf_edata + nsize;
+ else if(type == HMF_UUID)
+ f->hmf_uuid = (uint8_t *)f->hmf_edata + nsize;
else if(type == HMF_BIN) {
f->hmf_bin = f->hmf_edata + nsize;
f->hmf_binsize = esize;
f->hmf_binsize = len;
}
+/*
+ *
+ */
+static int
+htsmsg_field_set_uuid(htsmsg_field_t *f, tvh_uuid_t *u)
+{
+ if (f->hmf_type != HMF_UUID) {
+ htsmsg_field_data_destroy(f);
+ f->hmf_type = HMF_UUID;
+ f->hmf_uuid = malloc(UUID_BIN_SIZE);
+ if (f->hmf_uuid == NULL)
+ return 1;
+ }
+ memcpy((char *)f->hmf_uuid, u->bin, UUID_BIN_SIZE);
+ return 0;
+}
+
+/*
+ *
+ */
+int
+htsmsg_set_uuid(htsmsg_t *msg, const char *name, tvh_uuid_t *u)
+{
+ htsmsg_field_t *f = htsmsg_field_find(msg, name);
+ if (!f) {
+ htsmsg_add_uuid(msg, name, u);
+ return 0;
+ }
+ return htsmsg_field_set_uuid(f, u);
+}
+
+/*
+ *
+ */
+void
+htsmsg_add_uuid(htsmsg_t *msg, const char *name, tvh_uuid_t *u)
+{
+ htsmsg_field_t *f = htsmsg_field_add(msg, name, HMF_UUID, 0, UUID_BIN_SIZE);
+ f->hmf_flags |= HMF_INALLOCED;
+ memcpy((void *)f->hmf_uuid, u->bin, UUID_BIN_SIZE);
+}
/*
*
return NULL;
case HMF_STR:
break;
+ case HMF_UUID:
+ uuid_get_hex((tvh_uuid_t *)f->hmf_uuid, buf);
+ htsmsg_field_set_str_force(f, buf);
+ break;
case HMF_BOOL:
htsmsg_field_set_str_force(f, f->hmf_bool ? "true" : "false");
break;
return htsmsg_field_get_bin(f, binp, lenp);
}
+/*
+ *
+ */
+int
+htsmsg_get_uuid
+ (htsmsg_t *msg, const char *name, tvh_uuid_t *u)
+{
+ htsmsg_field_t *f;
+
+ if((f = htsmsg_field_find(msg, name)) == NULL)
+ return HTSMSG_ERR_FIELD_NOT_FOUND;
+
+ if(f->hmf_type == HMF_UUID) {
+ memcpy(u->bin, f->hmf_uuid, UUID_BIN_SIZE);
+ return 0;
+ } else {
+ const void *p;
+ size_t l;
+ int r = htsmsg_field_get_bin(f, &p, &l);
+ if (r == 0) {
+ if (l != UUID_BIN_SIZE)
+ return HTSMSG_ERR_FIELD_NOT_FOUND;
+ memcpy(u->bin, p, UUID_BIN_SIZE);
+ }
+ return r;
+ }
+}
+
/*
*
*/
#include <stdlib.h>
#include <inttypes.h>
#include "queue.h"
+#include "uuid.h"
#include "build.h"
#define HTSMSG_ERR_FIELD_NOT_FOUND -1
#define HMF_LIST 5
#define HMF_DBL 6
#define HMF_BOOL 7
+#define HMF_UUID 8
typedef struct htsmsg_field {
TAILQ_ENTRY(htsmsg_field) hmf_link;
union {
int64_t s64;
const char *str;
+ const uint8_t *uuid;
struct {
const char *data;
size_t len;
#define hmf_s64 u.s64
#define hmf_msg u.msg
#define hmf_str u.str
+#define hmf_uuid u.uuid
#define hmf_bin u.bin.data
#define hmf_binsize u.bin.len
#define hmf_dbl u.dbl
int htsmsg_field_set_bin_force(htsmsg_field_t *f, const void *bin, size_t len);
/**
- * Add an binary field. The data is copied to a malloced storage
+ * Add an binary field. The data is copied to a inallocated storage.
*/
void htsmsg_add_bin(htsmsg_t *msg, const char *name, const void *bin, size_t len);
*/
void htsmsg_add_bin_ptr(htsmsg_t *msg, const char *name, const void *bin, size_t len);
+/**
+ * Add/update a uuid field
+ */
+int htsmsg_set_uuid(htsmsg_t *msg, const char *name, tvh_uuid_t *u);
+
+/**
+ * Add an uuid field.
+ */
+void htsmsg_add_uuid(htsmsg_t *msg, const char *name, tvh_uuid_t *u);
+
/**
* Get an integer as an unsigned 32 bit integer.
*
int htsmsg_get_bin(htsmsg_t *msg, const char *name, const void **binp,
size_t *lenp);
+/**
+ * Get uuid struct from a uuid field.
+ *
+ * @param u Pointer to the tvh_uuid_t structure.
+ *
+ * @return HTSMSG_ERR_FIELD_NOT_FOUND - Field does not exist
+ * HTSMSG_ERR_CONVERSION_IMPOSSIBLE - Field is not a binary blob.
+ */
+int htsmsg_get_uuid(htsmsg_t *msg, const char *name, tvh_uuid_t *u);
+
/**
* Get a field of type 'list'. No copying is done.
*
return -1;
nlen = namelen ? namelen + 1 : 0;
- tlen = sizeof(htsmsg_field_t) + nlen +
- (type == HMF_STR ? datalen + 1 : 0);
+ tlen = sizeof(htsmsg_field_t) + nlen;
+ if (type == HMF_STR) {
+ tlen += datalen + 1;
+ } else if (type == HMF_UUID) {
+ tlen = UUID_BIN_SIZE;
+ if (tlen != datalen)
+ return -1;
+ }
f = malloc(tlen);
if (f == NULL)
return -1;
f->hmf_flags |= HMF_INALLOCED;
break;
+ case HMF_UUID:
+ f->hmf_uuid = (uint8_t *)f->hmf_edata + nlen;
+ memcpy((char *)f->hmf_uuid, buf, UUID_BIN_SIZE);
+ break;
+
case HMF_BIN:
f->hmf_bin = (const void *)buf;
f->hmf_binsize = datalen;
len += strlen(f->hmf_str);
break;
+ case HMF_UUID:
+ len += UUID_BIN_SIZE;
+ break;
+
case HMF_BIN:
len += f->hmf_binsize;
break;
htsbuf_append_and_escape_jsonstr(hq, f->hmf_str);
break;
+ case HMF_UUID:
+ uuid_get_hex((tvh_uuid_t *)f->hmf_uuid, buf);
+ htsbuf_append_and_escape_jsonstr(hq, buf);
+ break;
+
case HMF_BIN:
htsbuf_append_and_escape_jsonstr(hq, "binary");
break;
uuid_get_hex ( const tvh_uuid_t *u, char *dst )
{
assert(dst);
- bin2hex(dst, UUID_HEX_SIZE, u->bin, sizeof(u->bin));
- return dst;
+ return bin2hex(dst, UUID_HEX_SIZE, u->bin, sizeof(u->bin));
}
/* Validate the hexadecimal representation of uuid */
#define __TVH_UUID_H__
#include <stdint.h>
+#include <string.h>
#define UUID_BIN_SIZE (16)
#define UUID_HEX_SIZE (33) // inc NUL