From 1deda4c52edb54d2a17393171e5770a28f8705e4 Mon Sep 17 00:00:00 2001 From: Adam Sutton Date: Tue, 17 Sep 2013 09:21:02 +0100 Subject: [PATCH] idnode: just a few bits of tidying up --- src/idnode.c | 24 ++++++++++++++---------- src/idnode.h | 10 +++++++--- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/idnode.c b/src/idnode.c index bbf06825..dd090276 100644 --- a/src/idnode.c +++ b/src/idnode.c @@ -88,7 +88,6 @@ hex2bin(uint8_t *buf, size_t buflen, const char *str) return 0; } - /** * */ @@ -111,7 +110,7 @@ bin2hex(char *dst, size_t dstlen, const uint8_t *src, size_t srclen) static int in_cmp(const idnode_t *a, const idnode_t *b) { - return memcmp(a->in_uuid, b->in_uuid, 16); + return memcmp(a->in_uuid, b->in_uuid, sizeof(a->in_uuid)); } /* ************************************************************************** @@ -145,12 +144,12 @@ idnode_insert(idnode_t *in, const char *uuid, const idclass_t *class) idnode_t *c; lock_assert(&global_lock); if(uuid == NULL) { - if(read(randfd, in->in_uuid, 16) != 16) { + if(read(randfd, in->in_uuid, sizeof(in->in_uuid)) != sizeof(in->in_uuid)) { perror("read(random for uuid)"); exit(1); } } else { - if(hex2bin(in->in_uuid, 16, uuid)) + if(hex2bin(in->in_uuid, sizeof(in->in_uuid), uuid)) return -1; } @@ -217,14 +216,19 @@ idnode_get_short_uuid (const idnode_t *in) * */ const char * +idnode_uuid_as_str0(const idnode_t *in, char *b) +{ + bin2hex(b, UUID_STR_LEN, in->in_uuid, sizeof(in->in_uuid)); + return b; +} +const char * idnode_uuid_as_str(const idnode_t *in) { - static char ret[16][33]; - static int p; + static char ret[16][UUID_STR_LEN]; + static int p = 0; char *b = ret[p]; - bin2hex(b, 33, in->in_uuid, 16); - p = (p + 1) & 15; - return b; + p = (p + 1) % 16; + return idnode_uuid_as_str0(in, b); } /** @@ -406,7 +410,7 @@ idnode_find(const char *uuid, const idclass_t *idc) idnode_t skel, *r; tvhtrace("idnode", "find node %s class %s", uuid, idc ? idc->ic_class : NULL); - if(hex2bin(skel.in_uuid, 16, uuid)) + if(hex2bin(skel.in_uuid, sizeof(skel.in_uuid), uuid)) return NULL; r = RB_FIND(&idnodes, &skel, in_link, in_cmp); if(r != NULL && idc != NULL) { diff --git a/src/idnode.h b/src/idnode.h index a815c410..ad34b17d 100644 --- a/src/idnode.h +++ b/src/idnode.h @@ -25,6 +25,9 @@ #include +#define UUID_STR_LEN 33 // inc NUL char +#define UUID_BIN_LEN 16 + struct htsmsg; typedef struct idnode idnode_t; @@ -59,9 +62,9 @@ typedef struct idclass { * Node definition */ struct idnode { - uint8_t in_uuid[16]; ///< Unique ID - RB_ENTRY(idnode) in_link; ///< Global hash - const idclass_t *in_class; ///< Class definition + uint8_t in_uuid[UUID_BIN_LEN]; ///< Unique ID + RB_ENTRY(idnode) in_link; ///< Global hash + const idclass_t *in_class; ///< Class definition }; /* @@ -111,6 +114,7 @@ int idnode_insert(idnode_t *in, const char *uuid, const idclass_t *idc); void idnode_unlink(idnode_t *in); uint32_t idnode_get_short_uuid (const idnode_t *in); +const char *idnode_uuid_as_str0 (const idnode_t *in, char *b); const char *idnode_uuid_as_str (const idnode_t *in); idnode_set_t *idnode_get_childs (idnode_t *in); const char *idnode_get_title (idnode_t *in);