Skip to content

Commit

Permalink
htsmsg: add HMF_UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
perexg committed Jan 9, 2018
1 parent 4c203e0 commit 3069de5
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 5 deletions.
75 changes: 75 additions & 0 deletions src/htsmsg.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/*
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

/*
*
*/
Expand Down
26 changes: 25 additions & 1 deletion src/htsmsg.h
Expand Up @@ -20,6 +20,7 @@
#include <stdlib.h>
#include <inttypes.h>
#include "queue.h"
#include "uuid.h"
#include "build.h"

#define HTSMSG_ERR_FIELD_NOT_FOUND -1
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);

Expand All @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
19 changes: 17 additions & 2 deletions src/htsmsg_binary.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/htsmsg_json.c
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/uuid.c
Expand Up @@ -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 */
Expand Down
1 change: 1 addition & 0 deletions src/uuid.h
Expand Up @@ -21,6 +21,7 @@
#define __TVH_UUID_H__

#include <stdint.h>
#include <string.h>

#define UUID_BIN_SIZE (16)
#define UUID_HEX_SIZE (33) // inc NUL
Expand Down

0 comments on commit 3069de5

Please sign in to comment.