From: Jaroslav Kysela Date: Mon, 8 Jan 2018 14:51:34 +0000 (+0100) Subject: htsmsg: add HMF_UUID X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3069de56c70b0226fbdb48530ccba4e6662172a5;p=thirdparty%2Ftvheadend.git htsmsg: add HMF_UUID --- diff --git a/src/htsmsg.c b/src/htsmsg.c index 65a3b4cbb..c3ed116aa 100644 --- a/src/htsmsg.c +++ b/src/htsmsg.c @@ -133,6 +133,8 @@ htsmsg_field_add(htsmsg_t *msg, const char *name, int type, int flags, size_t es 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; @@ -530,6 +532,47 @@ htsmsg_add_bin_ptr(htsmsg_t *msg, const char *name, const void *bin, size_t len) 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); +} /* * @@ -834,6 +877,10 @@ htsmsg_field_get_string(htsmsg_field_t *f) 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; @@ -913,6 +960,34 @@ htsmsg_get_bin 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; + } +} + /* * */ diff --git a/src/htsmsg.h b/src/htsmsg.h index 83ab91288..6484bb61e 100644 --- a/src/htsmsg.h +++ b/src/htsmsg.h @@ -20,6 +20,7 @@ #include #include #include "queue.h" +#include "uuid.h" #include "build.h" #define HTSMSG_ERR_FIELD_NOT_FOUND -1 @@ -53,6 +54,7 @@ typedef struct htsmsg { #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; @@ -66,6 +68,7 @@ typedef struct htsmsg_field { union { int64_t s64; const char *str; + const uint8_t *uuid; struct { const char *data; size_t len; @@ -84,6 +87,7 @@ typedef struct htsmsg_field { #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 @@ -226,7 +230,7 @@ int htsmsg_field_set_bin(htsmsg_field_t *f, const void *bin, size_t len); 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); @@ -242,6 +246,16 @@ void htsmsg_add_bin_alloc(htsmsg_t *msg, const char *name, const void *bin, size */ 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. * @@ -301,6 +315,16 @@ int htsmsg_get_bool_or_default(htsmsg_t *msg, const char *name, int def); 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. * diff --git a/src/htsmsg_binary.c b/src/htsmsg_binary.c index bd6a2f0c6..bed63f026 100644 --- a/src/htsmsg_binary.c +++ b/src/htsmsg_binary.c @@ -55,8 +55,14 @@ htsmsg_binary_des0(htsmsg_t *msg, const uint8_t *buf, size_t len) 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; @@ -86,6 +92,11 @@ htsmsg_binary_des0(htsmsg_t *msg, const uint8_t *buf, size_t len) 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; @@ -191,6 +202,10 @@ htsmsg_binary_count(htsmsg_t *msg) len += strlen(f->hmf_str); break; + case HMF_UUID: + len += UUID_BIN_SIZE; + break; + case HMF_BIN: len += f->hmf_binsize; break; diff --git a/src/htsmsg_json.c b/src/htsmsg_json.c index 17b0644f0..b9b072c26 100644 --- a/src/htsmsg_json.c +++ b/src/htsmsg_json.c @@ -66,6 +66,11 @@ htsmsg_json_write(htsmsg_t *msg, htsbuf_queue_t *hq, int isarray, 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; diff --git a/src/uuid.c b/src/uuid.c index b95fb058f..258948be4 100644 --- a/src/uuid.c +++ b/src/uuid.c @@ -142,8 +142,7 @@ char * 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 */ diff --git a/src/uuid.h b/src/uuid.h index 88caf28cd..d441a28e4 100644 --- a/src/uuid.h +++ b/src/uuid.h @@ -21,6 +21,7 @@ #define __TVH_UUID_H__ #include +#include #define UUID_BIN_SIZE (16) #define UUID_HEX_SIZE (33) // inc NUL