--- uuid.c 2006/02/07 08:19:15 1.58
+++ uuid.c 2006/05/11 09:37:28 1.59
@@ -50,6 +50,7 @@
#include "uuid_prng.h"
#include "uuid_mac.h"
#include "uuid_ui64.h"
+#include "uuid_ui128.h"
#include "uuid_str.h"
#include "uuid_bm.h"
#include "uuid_ac.h"
@@ -431,6 +432,39 @@
return UUID_RC_OK;
}
+/* INTERNAL: import UUID object from single integer value representation */
+static uuid_rc_t uuid_import_siv(uuid_t *uuid, const void *data_ptr, size_t data_len)
+{
+ const char *str;
+ uuid_uint8_t tmp_bin[UUID_LEN_BIN];
+ ui128_t ui, ui2;
+ int i;
+
+ /* sanity check argument(s) */
+ if (uuid == NULL || data_ptr == NULL || data_len < 1)
+ return UUID_RC_ARG;
+
+ /* check for correct UUID single integer value syntax */
+ str = (const char *)data_ptr;
+ for (i = 0; i < data_len; i++)
+ if (!isdigit((int)str[i]))
+ return UUID_RC_ARG;
+
+ /* parse single integer value representation (SIV) */
+ ui = ui128_s2i(str, NULL, 10);
+
+ /* import octets into UUID binary representation */
+ for (i = 0; i < UUID_LEN_BIN; i++) {
+ ui = ui128_rol(ui, 8, &ui2);
+ tmp_bin[i] = (uuid_uint8_t)(ui128_i2n(ui2) & 0xff);
+ }
+
+ /* import into internal UUID representation */
+ uuid_import(uuid, UUID_FMT_BIN, (void *)&tmp_bin, UUID_LEN_BIN);
+
+ return UUID_RC_OK;
+}
+
/* INTERNAL: export UUID object to string representation */
static uuid_rc_t uuid_export_str(const uuid_t *uuid, void **data_ptr, size_t *data_len)
{
@@ -482,6 +516,61 @@
return UUID_RC_OK;
}
+/* INTERNAL: export UUID object to single integer value representation */
+static uuid_rc_t uuid_export_siv(const uuid_t *uuid, void **data_ptr, size_t *data_len)
+{
+ char *data_buf;
+ void *tmp_ptr;
+ size_t tmp_len;
+ uuid_uint8_t tmp_bin[UUID_LEN_BIN];
+ ui128_t ui, ui2;
+ uuid_rc_t rc;
+ int i;
+
+ /* sanity check argument(s) */
+ if (uuid == NULL || data_ptr == NULL)
+ return UUID_RC_ARG;
+
+ /* determine output buffer */
+ if (*data_ptr == NULL) {
+ if ((data_buf = (char *)malloc(UUID_LEN_SIV+1)) == NULL)
+ return UUID_RC_MEM;
+ if (data_len != NULL)
+ *data_len = UUID_LEN_SIV+1;
+ }
+ else {
+ data_buf = (char *)(*data_ptr);
+ if (data_len == NULL)
+ return UUID_RC_ARG;
+ if (*data_len < UUID_LEN_SIV+1)
+ return UUID_RC_MEM;
+ *data_len = UUID_LEN_SIV+1;
+ }
+
+ /* export into UUID binary representation */
+ tmp_ptr = (void *)&tmp_bin;
+ tmp_len = sizeof(tmp_bin);
+ if ((rc = uuid_export(uuid, UUID_FMT_BIN, &tmp_ptr, &tmp_len)) != UUID_RC_OK)
+ return rc;
+
+ /* import from UUID binary representation */
+ ui = ui128_zero();
+ for (i = 0; i < UUID_LEN_BIN; i++) {
+ ui2 = ui128_n2i((unsigned long)tmp_bin[i]);
+ ui = ui128_rol(ui, 8, NULL);
+ ui = ui128_or(ui, ui2);
+ }
+
+ /* format into single integer value representation */
+ ui128_i2s(ui, data_buf, UUID_LEN_SIV+1, 10);
+
+ /* pass back new buffer if locally allocated */
+ if (*data_ptr == NULL)
+ *data_ptr = data_buf;
+
+ return UUID_RC_OK;
+}
+
/* decoding tables */
static struct {
uuid_uint8_t num;
@@ -518,6 +607,7 @@
uuid_uint32_t tmp32;
uuid_uint8_t tmp_bin[UUID_LEN_BIN];
char tmp_str[UUID_LEN_STR+1];
+ char tmp_siv[UUID_LEN_SIV+1];
void *tmp_ptr;
size_t tmp_len;
ui64_t t;
@@ -541,12 +631,17 @@
if ((rc = uuid_isnil(uuid, &isnil)) != UUID_RC_OK)
return rc;
- /* decode into string representation */
+ /* decode into various representations */
tmp_ptr = (void *)&tmp_str;
tmp_len = sizeof(tmp_str);
if ((rc = uuid_export(uuid, UUID_FMT_STR, &tmp_ptr, &tmp_len)) != UUID_RC_OK)
return rc;
- str_rsprintf(out, "UUID: %s\n", tmp_str);
+ tmp_ptr = (void *)&tmp_siv;
+ tmp_len = sizeof(tmp_siv);
+ if ((rc = uuid_export(uuid, UUID_FMT_SIV, &tmp_ptr, &tmp_len)) != UUID_RC_OK)
+ return rc;
+ str_rsprintf(out, "encode: STR: %s\n", tmp_str);
+ str_rsprintf(out, " SIV: %s\n", tmp_siv);
/* decode UUID variant */
tmp8 = uuid->obj.clock_seq_hi_and_reserved;
@@ -567,7 +662,7 @@
}
}
}
- str_rsprintf(out, "variant: %s\n", variant);
+ str_rsprintf(out, "decode: variant: %s\n", variant);
/* decode UUID version */
tmp16 = (BM_SHR(uuid->obj.time_hi_and_version, 12) & BM_MASK(3,0));
@@ -582,7 +677,7 @@
}
}
}
- str_rsprintf(out, "version: %d (%s)\n", (int)tmp16, version);
+ str_rsprintf(out, " version: %d (%s)\n", (int)tmp16, version);
/*
* decode UUID content
@@ -602,15 +697,15 @@
t_sec = (time_t)ui64_i2n(t);
tm = gmtime(&t_sec);
strftime(t_buf, sizeof(t_buf), "%Y-%m-%d %H:%M:%S", tm);
- str_rsprintf(out, "content: time: %s.%06d.%d UTC\n", t_buf, t_usec, t_nsec);
+ str_rsprintf(out, " content: time: %s.%06d.%d UTC\n", t_buf, t_usec, t_nsec);
/* decode clock sequence */
tmp32 = ((uuid->obj.clock_seq_hi_and_reserved & BM_MASK(5,0)) << 8)
+ uuid->obj.clock_seq_low;
- str_rsprintf(out, " clock: %ld (usually random)\n", (long)tmp32);
+ str_rsprintf(out, " clock: %ld (usually random)\n", (long)tmp32);
/* decode node MAC address */
- str_rsprintf(out, " node: %02x:%02x:%02x:%02x:%02x:%02x (%s %s)\n",
+ str_rsprintf(out, " node: %02x:%02x:%02x:%02x:%02x:%02x (%s %s)\n",
(unsigned int)uuid->obj.node[0],
(unsigned int)uuid->obj.node[1],
(unsigned int)uuid->obj.node[2],
@@ -646,8 +741,8 @@
/* dump as colon-seperated hexadecimal byte-string */
str_rsprintf(out,
- "content: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n"
- " (%s)\n",
+ " content: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n"
+ " (%s)\n",
(unsigned int)tmp_bin[0], (unsigned int)tmp_bin[1], (unsigned int)tmp_bin[2],
(unsigned int)tmp_bin[3], (unsigned int)tmp_bin[4], (unsigned int)tmp_bin[5],
(unsigned int)tmp_bin[6], (unsigned int)tmp_bin[7], (unsigned int)tmp_bin[8],
@@ -687,6 +782,7 @@
switch (fmt) {
case UUID_FMT_BIN: rc = uuid_import_bin(uuid, data_ptr, data_len); break;
case UUID_FMT_STR: rc = uuid_import_str(uuid, data_ptr, data_len); break;
+ case UUID_FMT_SIV: rc = uuid_import_siv(uuid, data_ptr, data_len); break;
case UUID_FMT_TXT: rc = UUID_RC_IMP; /* not implemented */ break;
default: rc = UUID_RC_ARG;
}
@@ -707,6 +803,7 @@
switch (fmt) {
case UUID_FMT_BIN: rc = uuid_export_bin(uuid, data_ptr, data_len); break;
case UUID_FMT_STR: rc = uuid_export_str(uuid, data_ptr, data_len); break;
+ case UUID_FMT_SIV: rc = uuid_export_siv(uuid, data_ptr, data_len); break;
case UUID_FMT_TXT: rc = uuid_export_txt(uuid, data_ptr, data_len); break;
default: rc = UUID_RC_ARG;
}
|