ossp-pkg/uuid/uuid.c
1.4
/*
** OSSP uuid - Universally Unique Identifier
** Copyright (c) 2004 Ralf S. Engelschall <rse@engelschall.com>
** Copyright (c) 2004 The OSSP Project <http://www.ossp.org/>
**
** This file is part of OSSP uuid, a library for the generation
** of UUIDs which can found at http://www.ossp.org/pkg/lib/uuid/
**
** Permission to use, copy, modify, and distribute this software for
** any purpose with or without fee is hereby granted, provided that
** the above copyright notice and this permission notice appear in all
** copies.
**
** THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
** USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
** OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
** SUCH DAMAGE.
**
** uuid.c: library API implementation
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include "config.h"
#include "uuid.h"
/* determine types of 8-bit size */
#if SIZEOF_CHAR == 1
typedef char uuid_int8_t;
#else
#error uexpected: sizeof(char) != 1 !?
#endif
#if SIZEOF_UNSIGNED_CHAR == 1
typedef unsigned char uuid_uint8_t;
#else
#error uexpected: sizeof(unsigned char) != 1 !?
#endif
/* determine types of 16-bit size */
#if SIZEOF_SHORT == 2
typedef short uuid_int16_t;
#elif SIZEOF_INT == 2
typedef int uuid_int16_t;
#elif SIZEOF_LONG == 2
typedef long uuid_int16_t;
#else
#error unexpected: no type found for uuid_int16_t
#endif
#if SIZEOF_UNSIGNED_SHORT == 2
typedef unsigned short uuid_uint16_t;
#elif SIZEOF_UNSIGNED_INT == 2
typedef unsigned int uuid_uint16_t;
#elif SIZEOF_UNSIGNED_LONG == 2
typedef unsigned long uuid_uint16_t;
#else
#error unexpected: no type found for uuid_uint16_t
#endif
/* determine types of 32-bit size */
#if SIZEOF_SHORT == 4
typedef short uuid_int32_t;
#elif SIZEOF_INT == 4
typedef int uuid_int32_t;
#elif SIZEOF_LONG == 4
typedef long uuid_int32_t;
#elif SIZEOF_LONG_LONG == 4
typedef long long uuid_int32_t;
#else
#error unexpected: no type found for uuid_int32_t
#endif
#if SIZEOF_UNSIGNED_SHORT == 4
typedef unsigned short uuid_uint32_t;
#elif SIZEOF_UNSIGNED_INT == 4
typedef unsigned int uuid_uint32_t;
#elif SIZEOF_UNSIGNED_LONG == 4
typedef unsigned long uuid_uint32_t;
#elif SIZEOF_UNSIGNED_LONG_LONG == 4
typedef unsigned long long uuid_uint32_t;
#else
#error unexpected: no type found for uuid_uint32_t
#endif
/* private data type declaration */
struct uuid_st {
uuid_uint32_t time_low;
uuid_uint16_t time_mid;
uuid_uint16_t time_hi_and_version;
uuid_uint8_t clock_seq_hi_and_reserved;
uuid_uint8_t clock_seq_low;
uuid_uint8_t node[6];
};
uuid_rc_t uuid_create(uuid_t **uuid)
{
if (uuid == NULL)
return UUID_RC_ARG;
if ((*uuid = (uuid_t *)malloc(sizeof(uuid_t))) == NULL)
return UUID_RC_MEM;
uuid_null(*uuid);
return UUID_RC_OK;
}
uuid_rc_t uuid_destroy(uuid_t *uuid)
{
if (uuid == NULL)
return UUID_RC_ARG;
free(uuid);
return UUID_RC_OK;
}
uuid_rc_t uuid_null(uuid_t *uuid)
{
if (uuid == NULL)
return UUID_RC_ARG;
memset(uuid, '\0', sizeof(uuid_t));
return UUID_RC_OK;
}
uuid_rc_t uuid_generate(uuid_t *uuid, unsigned int mode, ...)
{
va_list ap;
if (uuid == NULL)
return UUID_RC_ARG;
va_start(ap, mode);
/* FIXME */
va_end(ap);
return UUID_RC_OK;
}
uuid_rc_t uuid_compare(uuid_t *a, uuid_t *b, int *result)
{
int r;
if (result == NULL)
return UUID_RC_ARG;
/* deal with NULL or equal pointers. */
if (a == b) {
*result = 0;
return UUID_RC_OK;
}
if (a == NULL && b == NULL) {
*result = 0;
return UUID_RC_OK;
}
if (a == NULL) {
*result = ((uuid_isnil(b, &r), r) ? 0 : -1);
return UUID_RC_OK;
}
if (b == NULL) {
*result = ((uuid_isnil(a, &r), r) ? 0 : 1);
return UUID_RC_OK;
}
/* we have to compare the hard way. */
if (a->time_low != b->time_low) {
*result = ((a->time_low < b->time_low) ? -1 : 1);
return UUID_RC_OK;
}
if ((r = (int)a->time_mid - (int)b->time_mid) != 0) {
*result = ((r < 0) ? -1 : 1);
return UUID_RC_OK;
}
if ((r = (int)a->time_hi_and_version - (int)b->time_hi_and_version) != 0) {
*result = ((r < 0) ? -1 : 1);
return UUID_RC_OK;
}
if ((r = (int)a->clock_seq_hi_and_reserved - (int)b->clock_seq_hi_and_reserved) != 0) {
*result = ((r < 0) ? -1 : 1);
return UUID_RC_OK;
}
if ((r = (int)a->clock_seq_low - (int)b->clock_seq_low) != 0) {
*result = ((r < 0) ? -1 : 1);
return UUID_RC_OK;
}
if ((r = memcmp(a->node, b->node, sizeof(a->node))) != 0) {
*result = ((r < 0) ? -1 : 1);
return UUID_RC_OK;
}
/* else the keys are equal */
*result = 0;
return UUID_RC_OK;
}
uuid_rc_t uuid_isnil(uuid_t *uuid, int *result)
{
if (uuid == NULL || result == NULL)
return UUID_RC_ARG;
*result = 0;
if ( uuid->time_low == 0
&& uuid->time_mid == 0
&& uuid->time_hi_and_version == 0
&& uuid->clock_seq_hi_and_reserved == 0
&& uuid->clock_seq_low == 0
&& uuid->node[0] == 0
&& uuid->node[1] == 0
&& uuid->node[2] == 0
&& uuid->node[3] == 0
&& uuid->node[4] == 0
&& uuid->node[5] == 0)
*result = 1;
return UUID_RC_OK;
}
uuid_rc_t uuid_parse(uuid_t *uuid, const char *str)
{
if (uuid == NULL)
return UUID_RC_ARG;
/* FIXME */
return UUID_RC_OK;
}
uuid_rc_t uuid_format(uuid_t *uuid, char **str)
{
if (uuid == NULL || str == NULL)
return UUID_RC_ARG;
/* FIXME */
return UUID_RC_OK;
}
uuid_rc_t uuid_read(uuid_t *uuid, const void *buf)
{
if (uuid == NULL)
return UUID_RC_ARG;
/* FIXME */
return UUID_RC_OK;
}
uuid_rc_t uuid_write(uuid_t *uuid, void **buf)
{
if (uuid == NULL || buf == NULL)
return UUID_RC_ARG;
/* FIXME */
return UUID_RC_OK;
}
uuid_rc_t uuid_dump(uuid_t *uuid, char **str)
{
if (uuid == NULL || str == NULL)
return UUID_RC_ARG;
/* FIXME */
return UUID_RC_OK;
}
char *uuid_error(uuid_rc_t rc)
{
char *str;
switch (rc) {
case UUID_RC_OK: str = "everything ok"; break;
case UUID_RC_ARG: str = "invalid argument"; break;
case UUID_RC_MEM: str = "out of memory"; break;
case UUID_RC_SYS: str = "system error"; break;
default: str = NULL; break;
}
return str;
}