]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
htsmsg: add HMF_UUID
authorJaroslav Kysela <perex@perex.cz>
Mon, 8 Jan 2018 14:51:34 +0000 (15:51 +0100)
committerJaroslav Kysela <perex@perex.cz>
Tue, 9 Jan 2018 07:43:32 +0000 (08:43 +0100)
src/htsmsg.c
src/htsmsg.h
src/htsmsg_binary.c
src/htsmsg_json.c
src/uuid.c
src/uuid.h

index 65a3b4cbbdc3508535d72bb969a2295b526bd5b0..c3ed116aa1430f0ad635a796efdc725820f97194 100644 (file)
@@ -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;
+  }
+}
+
 /*
  *
  */
index 83ab91288ceac226a939e61d6f70f4bd5b6a72d2..6484bb61e787ba222e377ab5f75901196fc904b2 100644 (file)
@@ -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
@@ -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.
  *
index bd6a2f0c65d7025eff9132bfc0a92ca841fc7521..bed63f026124a5d22c0d58b7b6252f7b8b75c50e 100644 (file)
@@ -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;
index 17b0644f044207759b138452103f3aae06d36a29..b9b072c26b52ccbc5bc8bfeeaf32d936083b97ed 100644 (file)
@@ -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;
index b95fb058f4fc37a145467574acc1cf7ebe15e501..258948be478569b66ae067ef4fd1a5ab4031d018 100644 (file)
@@ -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 */
index 88caf28cd9c5238a3c7273e64a8c8c5b427c82fd..d441a28e4370e92370f04ba8c338bf2ad0497d9a 100644 (file)
@@ -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