87 lines
1.8 KiB
C
87 lines
1.8 KiB
C
/* Copyright (C) 2010 Tony Garnock-Jones. All rights reserved. */
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
#include <stdarg.h>
|
|
|
|
/* OSSP UUID */
|
|
#include <uuid.h>
|
|
|
|
#include "cmsg_private.h"
|
|
|
|
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) {
|
|
cmsg_bytes_t result;
|
|
result.len = src.len;
|
|
result.bytes = malloc(src.len);
|
|
if (result.bytes != NULL) {
|
|
memcpy(result.bytes, src.bytes, src.len);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
cmsg_bytes_t cmsg_bytes_malloc(size_t amount) {
|
|
cmsg_bytes_t result;
|
|
result.len = amount;
|
|
result.bytes = malloc(amount);
|
|
return result;
|
|
}
|
|
|
|
void cmsg_bytes_free(cmsg_bytes_t bytes) {
|
|
free(bytes.bytes);
|
|
}
|
|
|
|
int cmsg_bytes_cmp(cmsg_bytes_t a, cmsg_bytes_t b) {
|
|
if (a.len < b.len) return -1;
|
|
if (a.len > b.len) return 1;
|
|
return memcmp(a.bytes, b.bytes, a.len);
|
|
}
|
|
|
|
void die(char const *format, ...) {
|
|
va_list vl;
|
|
va_start(vl, format);
|
|
fprintf(stderr, "ERROR: ");
|
|
vfprintf(stderr, format, vl);
|
|
va_end(vl);
|
|
exit(1);
|
|
}
|
|
|
|
void warn(char const *format, ...) {
|
|
va_list vl;
|
|
va_start(vl, format);
|
|
fprintf(stderr, "WARNING: ");
|
|
vfprintf(stderr, format, vl);
|
|
va_end(vl);
|
|
}
|
|
|
|
void info(char const *format, ...) {
|
|
va_list vl;
|
|
va_start(vl, format);
|
|
fprintf(stderr, "INFO: ");
|
|
vfprintf(stderr, format, vl);
|
|
va_end(vl);
|
|
}
|