uuid.c revision 1.2
1/* $NetBSD: uuid.c,v 1.2 2009/06/30 02:44:52 agc Exp $ */ 2 3/* 4 * Copyright � 2006 Alistair Crooks. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote 15 * products derived from this software without specific prior written 16 * permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 24 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30#include "config.h" 31 32#ifdef HAVE_INTTYPES_H 33#include <inttypes.h> 34#endif 35 36#include <sys/types.h> 37 38#ifdef HAVE_SYS_PARAM_H 39#include <sys/param.h> 40#endif 41 42#ifdef HAVE_SYS_TIME_H 43#include <sys/time.h> 44#endif 45 46#include <stdio.h> 47#include <stdlib.h> 48#include <string.h> 49#include <unistd.h> 50 51#include "storage.h" 52#include "compat.h" 53#include "defs.h" 54 55/* just fill the struct with random values for now */ 56void 57nbuuid_create(nbuuid_t *uuid, uint32_t *status) 58{ 59 uint64_t ether; 60 time_t t; 61 62 (void) time(&t); 63 ether = ((uint64_t)random() << 32) | random(); 64 uuid->time_low = (int)t; 65 uuid->time_mid = (uint16_t)(random() & 0xffff); 66 uuid->time_hi_and_version = (uint16_t)(random() & 0xffff); 67 uuid->clock_seq_low = (uint8_t)(random() & 0xff); 68 uuid->clock_seq_hi_and_reserved = (uint8_t)(random() & 0xff); 69 (void) memcpy(&uuid->node, ðer, sizeof(uuid->node)); 70 *status = 0; 71} 72 73/* convert the struct to a printable string */ 74void 75nbuuid_to_string(nbuuid_t *uuid, char **str, uint32_t *status) 76{ 77 char s[64]; 78 79 (void) snprintf(s, sizeof(s), "%8.8x-%4.4x-%4.4x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x", 80 uuid->time_low, 81 uuid->time_mid, 82 uuid->time_hi_and_version, 83 uuid->clock_seq_hi_and_reserved, 84 uuid->clock_seq_low, 85 uuid->node[0], 86 uuid->node[1], 87 uuid->node[2], 88 uuid->node[3], 89 uuid->node[4], 90 uuid->node[5]); 91 *str = strdup(s); 92 *status = 0; 93} 94