Inline cmsg_cstring_bytes; add gen_uuid

This commit is contained in:
Tony Garnock-Jones 2011-01-01 21:11:00 -05:00
parent 006168b544
commit e055c7fb82
2 changed files with 32 additions and 6 deletions

View File

@ -12,7 +12,15 @@ typedef struct cmsg_bytes_t {
})
#define EMPTY_BYTES CMSG_BYTES(0, NULL)
extern cmsg_bytes_t cmsg_cstring_bytes(char const *cstr);
static inline cmsg_bytes_t cmsg_cstring_bytes(char const *cstr) {
cmsg_bytes_t result;
result.len = strlen(cstr);
result.bytes = (void *) cstr;
return result;
}
extern int gen_uuid(unsigned char *uuid_buf); /* must be 16 bytes long */
extern cmsg_bytes_t cmsg_bytes_malloc_dup(cmsg_bytes_t src);
extern cmsg_bytes_t cmsg_bytes_malloc(size_t amount);
extern void cmsg_bytes_free(cmsg_bytes_t bytes);

28
util.c
View File

@ -4,15 +4,33 @@
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <stdarg.h>
/* OSSP UUID */
#include <uuid.h>
#include "cmsg_private.h"
cmsg_bytes_t cmsg_cstring_bytes(char const *cstr) {
cmsg_bytes_t result;
result.len = strlen(cstr);
result.bytes = (void *) cstr;
return result;
int gen_uuid(unsigned char *uuid_buf) {
uuid_rc_t result;
uuid_t *uuid;
size_t uuid_buf_len = UUID_LEN_BIN;
result = uuid_create(&uuid);
if (result != UUID_RC_OK) return result;
result = uuid_make(uuid, UUID_MAKE_V4);
if (result != UUID_RC_OK) return result;
result = uuid_export(uuid, UUID_FMT_BIN, &uuid_buf, &uuid_buf_len);
if (result != UUID_RC_OK) return result;
assert(uuid_buf_len == UUID_LEN_BIN);
result = uuid_destroy(uuid);
if (result != UUID_RC_OK) return result;
return UUID_RC_OK;
}
cmsg_bytes_t cmsg_bytes_malloc_dup(cmsg_bytes_t src) {