Usenet comp.unix.osf.misc (1992-2004)
 help / color / mirror / Atom feed
From: rsalz@osf.org (Rich Salz)
Subject: Re: What's each field of UUID assigned?
Date: 1995/06/21	[thread overview]
Message-ID: <3s8c48$qho@paperboy.osf.org> (raw)
In-Reply-To: 3s4mvr$7lr$1@mhafm.production.compuserve.com

In <3s4mvr$7lr$1@mhafm.production.compuserve.com> Wu Wang <73430.2372@CompuServe.COM> writes:
>Can someone give me some detail description of each field of UUID?

You mean a DCE UUID?  If so, the following sourceds from the free DCE
RPC distribution should help.
	/r$
#! /bin/sh
# This is a shell archive.  Remove anything before this line, then feed it
# into a shell via "sh file" or similar.  To overwrite existing files,
# type "sh file -c".
# The tool that generated this appeared in the comp.sources.unix newsgroup;
# send mail to comp-sources-unix@uunet.uu.net if you want that tool.
# Contents:  uuid.c uuidp.h uuidsys.c
# Wrapped by rsalz@sulphur on Wed Jun 21 01:47:17 1995
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
echo If this archive is complete, you will see the following message:
echo '          "shar: End of archive."'
if test -f 'uuid.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'uuid.c'\"
else
  echo shar: Extracting \"'uuid.c'\" \(46174 characters\)
  sed "s/^X//" >'uuid.c' <<'END_OF_FILE'
X/*
X * 
X * (c) Copyright 1989 OPEN SOFTWARE FOUNDATION, INC.
X * (c) Copyright 1989 HEWLETT-PACKARD COMPANY
X * (c) Copyright 1989 DIGITAL EQUIPMENT CORPORATION
X * To anyone who acknowledges that this file is provided "AS IS"
X * without any express or implied warranty:
X *                 permission to use, copy, modify, and distribute this
X * file for any purpose is hereby granted without fee, provided that
X * the above copyright notices and this notice appears in all source
X * code copies, and that none of the names of Open Software
X * Foundation, Inc., Hewlett-Packard Company, or Digital Equipment
X * Corporation be used in advertising or publicity pertaining to
X * distribution of the software without specific, written prior
X * permission.  Neither Open Software Foundation, Inc., Hewlett-
X * Packard Company, nor Digital Equipment Corporation makes any
X * representations about the suitability of this software for any
X * purpose.
X * 
X */
X/*
X */
X/*
X**
X**  NAME:
X**
X**      uuid.c
X**
X**  FACILITY:
X**
X**      UUID
X**
X**  ABSTRACT:
X**
X**      UUID - routines that manipulate uuid's
X**
X**
X*/
X
X#include <commonp.h>            /* common definitions                   */
X#include <dce/uuid.h>           /* uuid idl definitions (public)        */
X#include <uuidp.h>              /* uuid idl definitions (private)       */
X
X\f
X/*
X * Internal structure of universal unique IDs (UUIDs).
X *
X * There are three "variants" of UUIDs that this code knows about.  The
X * variant #0 is what was defined in the 1989 HP/Apollo Network Computing
X * Architecture (NCA) specification and implemented in NCS 1.x and DECrpc
X * v1.  Variant #1 is what was defined for the joint HP/DEC specification
X * for the OSF (in DEC's "UID Architecture Functional Specification Version
X * X1.0.4") and implemented in NCS 2.0, DECrpc v2, and OSF 1.0 DCE RPC.
X * Variant #2 is defined by Microsoft.
X *
X * This code creates only variant #1 UUIDs.
X * 
X * The three UUID variants can exist on the same wire because they have
X * distinct values in the 3 MSB bits of octet 8 (see table below).  Do
X * NOT confuse the version number with these 3 bits.  (Note the distinct
X * use of the terms "version" and "variant".) Variant #0 had no version
X * field in it.  Changes to variant #1 (should any ever need to be made)
X * can be accomodated using the current form's 4 bit version field.
X * 
X * The UUID record structure MUST NOT contain padding between fields.
X * The total size = 128 bits.
X *
X * To minimize confusion about bit assignment within octets, the UUID
X * record definition is defined only in terms of fields that are integral
X * numbers of octets.
X *
X * Depending on the network data representation, the multi-octet unsigned
X * integer fields are subject to byte swapping when communicated between
X * dissimilar endian machines.  Note that all three UUID variants have
X * the same record structure; this allows this byte swapping to occur.
X * (The ways in which the contents of the fields are generated can and
X * do vary.)
X *
X * The following information applies to variant #1 UUIDs:
X *
X * The lowest addressed octet contains the global/local bit and the
X * unicast/multicast bit, and is the first octet of the address transmitted
X * on an 802.3 LAN.
X *
X * The adjusted time stamp is split into three fields, and the clockSeq
X * is split into two fields.
X *
X * |<------------------------- 32 bits -------------------------->|
X *
X * +--------------------------------------------------------------+
X * |                     low 32 bits of time                      |  0-3  .time_low
X * +-------------------------------+-------------------------------
X * |     mid 16 bits of time       |  4-5               .time_mid
X * +-------+-----------------------+
X * | vers. |   hi 12 bits of time  |  6-7               .time_hi_and_version
X * +-------+-------+---------------+
X * |Res|  clkSeqHi |  8                                 .clock_seq_hi_and_reserved
X * +---------------+
X * |   clkSeqLow   |  9                                 .clock_seq_low
X * +---------------+----------...-----+
X * |            node ID               |  8-16           .node
X * +--------------------------...-----+
X *
X * --------------------------------------------------------------------------
X *
X * The structure layout of all three UUID variants is fixed for all time.
X * I.e., the layout consists of a 32 bit int, 2 16 bit ints, and 8 8
X * bit ints.  The current form version field does NOT determine/affect
X * the layout.  This enables us to do certain operations safely on the
X * variants of UUIDs without regard to variant; this increases the utility
X * of this code even as the version number changes (i.e., this code does
X * NOT need to check the version field).
X *
X * The "Res" field in the octet #8 is the so-called "reserved" bit-field
X * and determines whether or not the uuid is a old, current or other
X * UUID as follows:
X *
X *      MS-bit  2MS-bit  3MS-bit      Variant
X *      ---------------------------------------------
X *         0       x        x       0 (NCS 1.5)
X *         1       0        x       1 (DCE 1.0 RPC)
X *         1       1        0       2 (Microsoft)
X *         1       1        1       unspecified
X *
X * --------------------------------------------------------------------------
X *
X * Internal structure of variant #0 UUIDs
X *
X * The first 6 octets are the number of 4 usec units of time that have
X * passed since 1/1/80 0000 GMT.  The next 2 octets are reserved for
X * future use.  The next octet is an address family.  The next 7 octets
X * are a host ID in the form allowed by the specified address family.
X *
X * Note that while the family field (octet 8) was originally conceived
X * of as being able to hold values in the range [0..255], only [0..13]
X * were ever used.  Thus, the 2 MSB of this field are always 0 and are
X * used to distinguish old and current UUID forms.
X *
X * +--------------------------------------------------------------+
X * |                    high 32 bits of time                      |  0-3  .time_high
X * +-------------------------------+-------------------------------
X * |     low 16 bits of time       |  4-5               .time_low
X * +-------+-----------------------+
X * |         reserved              |  6-7               .reserved
X * +---------------+---------------+
X * |    family     |   8                                .family
X * +---------------+----------...-----+
X * |            node ID               |  9-16           .node
X * +--------------------------...-----+
X *
X */
X\f
X/***************************************************************************
X *
X * Local definitions
X *
X **************************************************************************/
X
X#ifdef  UUID_DEBUG
X#define DEBUG_PRINT(msg, st)    RPC_DBG_GPRINTF (( "%s: %08x\n", msg, st ))
X#else
X#define DEBUG_PRINT             { /* hello sailor */ }
X#endif
X
X#ifndef NO_SSCANF
X#  define UUID_SSCANF          sscanf
X#  define UUID_OLD_SSCANF      sscanf
X#else
X#  define UUID_SSCANF          uuid__sscanf
X#  define UUID_OLD_SSCANF      uuid__old_sscanf
X#endif
X
X#ifndef NO_SPRINTF
X#  define UUID_SPRINTF         sprintf
X#  define UUID_OLD_SPRINTF     sprintf
X#else
X#  define UUID_SPRINTF         uuid__sprintf
X#  define UUID_OLD_SPRINTF     uuid__old_sprintf
X#endif
X
X/*
X * the number of elements returned by sscanf() when converting
X * string formatted uuid's to binary
X */
X#define UUID_ELEMENTS_NUM       11
X#define UUID_ELEMENTS_NUM_OLD   10
X
X/*
X * local defines used in uuid bit-diddling
X */
X#define HI_WORD(w)                  ((w) >> 16)
X#define RAND_MASK                   0x3fff      /* same as CLOCK_SEQ_LAST */
X
X#define TIME_MID_MASK               0x0000ffff
X#define TIME_HIGH_MASK              0x0fff0000
X#define TIME_HIGH_SHIFT_COUNT       16
X
X#define MAX_TIME_ADJUST             0x7fff
X
X#define CLOCK_SEQ_LOW_MASK          0xff
X#define CLOCK_SEQ_HIGH_MASK         0x3f00
X#define CLOCK_SEQ_HIGH_SHIFT_COUNT  8
X#define CLOCK_SEQ_FIRST             1
X#define CLOCK_SEQ_LAST              0x3fff      /* same as RAND_MASK */
X
X/*
X * Note: If CLOCK_SEQ_BIT_BANG == TRUE, then we can avoid the modulo
X * operation.  This should save us a divide instruction and speed
X * things up.
X */
X
X#ifndef CLOCK_SEQ_BIT_BANG
X#define CLOCK_SEQ_BIT_BANG          1
X#endif
X
X#if CLOCK_SEQ_BIT_BANG
X#define CLOCK_SEQ_BUMP(seq)         ((*seq) = ((*seq) + 1) & CLOCK_SEQ_LAST)
X#else
X#define CLOCK_SEQ_BUMP(seq)         ((*seq) = ((*seq) + 1) % (CLOCK_SEQ_LAST+1))
X#endif
X
X#define UUID_VERSION_BITS           (uuid_c_version << 12)
X#define UUID_RESERVED_BITS          0x80
X
X#define IS_OLD_UUID(uuid) (((uuid)->clock_seq_hi_and_reserved & 0xc0) != 0x80)
X
X
X\f
X/****************************************************************************
X *
X * global data declarations
X *
X ****************************************************************************/
X
XGLOBAL uuid_t uuid_g_nil_uuid = { 0 };
XGLOBAL uuid_t uuid_nil = { 0 };
X
X/****************************************************************************
X *
X * local data declarations
X *
X ****************************************************************************/
X
X/*
X * saved copy of our IEEE 802 address for quick reference
X */
XINTERNAL uuid_address_t  saved_addr;
X
X/*
X * saved copy of the status associated with saved_addr
X */
XINTERNAL unsigned32     saved_st;
X
X/*
X * declarations used in UTC time calculations
X */
XINTERNAL uuid_time_t      time_now;     /* utc time as of last query        */
XINTERNAL uuid_time_t      time_last;    /* 'saved' value of time_now        */
XINTERNAL unsigned16       time_adjust;  /* 'adjustment' to ensure uniqness  */
XINTERNAL unsigned16       clock_seq;    /* 'adjustment' for backwards clocks*/
X
X/*
X * true_random variables
X */
XINTERNAL unsigned32     rand_m;         /* multiplier                       */
XINTERNAL unsigned32     rand_ia;        /* adder #1                         */
XINTERNAL unsigned32     rand_ib;        /* adder #2                         */
XINTERNAL unsigned32     rand_irand;     /* random value                     */
X
Xtypedef enum
X{
X    uuid_e_less_than, uuid_e_equal_to, uuid_e_greater_than
X} uuid_compval_t;
X
X/*
X * boolean indicating we've already determined our IEEE 802 address
X */
XINTERNAL boolean got_address = FALSE;
X
X/****************************************************************************
X *
X * local function declarations
X *
X ****************************************************************************/
X
X/*
X * I N I T
X *
X * Startup initialization routine for UUID module.
X */
X
XINTERNAL void init _DCE_PROTOTYPE_ (( unsigned32 * /*st*/ ));
X
X/*
X * T R U E _ R A N D O M _ I N I T
X */
X
XINTERNAL void true_random_init _DCE_PROTOTYPE_ ((void));
X
X/*
X * T R U E _ R A N D O M
X */
XINTERNAL unsigned16 true_random _DCE_PROTOTYPE_ ((void));
X
X
X/*
X * N E W _ C L O C K _ S E Q
X *
X * Ensure clock_seq is up-to-date
X *
X * Note: clock_seq is architected to be 14-bits (unsigned) but
X *       I've put it in here as 16-bits since there isn't a
X *       14-bit unsigned integer type (yet)
X */ 
XINTERNAL void new_clock_seq _DCE_PROTOTYPE_(( unsigned16 * /*clock_seq*/));
X
X/*
X * S T R U C T U R E _ I S _ K N O W N
X *
X * Does the UUID have the known standard structure layout?
X */
Xboolean structure_is_known _DCE_PROTOTYPE_(( uuid_p_t /*uuid*/));
X
X/*
X * T I M E _ C M P
X *
X * Compares two UUID times (64-bit DEC UID UTC values)
X */
XINTERNAL uuid_compval_t time_cmp _DCE_PROTOTYPE_ ((
X        uuid_time_p_t        /*time1*/,
X        uuid_time_p_t        /*time2*/
X    ));
X
X/*
X * U U I D _ G E T _ A D D R E S S
X *
X * Get our IEEE 802 address (calls uuid__get_os_address)
X */
X
XPUBLIC void uuid_get_address _DCE_PROTOTYPE_ ((
X        uuid_address_t      * /*address*/,
X        unsigned32          * /*st*/
X    ));
X
X
X\f
X/*****************************************************************************
X *
X *  Macro definitions
X *
X ****************************************************************************/
X
X/*
X * ensure we've been initialized
X */
XINTERNAL boolean uuid_init_done = FALSE;
X
X#define EmptyArg
X#define UUID_VERIFY_INIT(Arg)          \
X    if (! uuid_init_done)           \
X    {                               \
X        init (status);              \
X        if (*status != uuid_s_ok)   \
X        {                           \
X            return Arg;                 \
X        }                           \
X    }
X
X/*
X * Check the reserved bits to make sure the UUID is of the known structure.
X */
X
X#define CHECK_STRUCTURE(uuid) \
X( \
X    (((uuid)->clock_seq_hi_and_reserved & 0x80) == 0x00) || /* var #0 */ \
X    (((uuid)->clock_seq_hi_and_reserved & 0xc0) == 0x80) || /* var #1 */ \
X    (((uuid)->clock_seq_hi_and_reserved & 0xe0) == 0xc0)    /* var #2 */ \
X)
X
X/*
X * The following macros invoke CHECK_STRUCTURE(), check that the return
X * value is okay and if not, they set the status variable appropriately
X * and return either a boolean FALSE, nothing (for void procedures),
X * or a value passed to the macro.  This has been done so that checking
X * can be done more simply and values are returned where appropriate
X * to keep compilers happy.
X *
X * bCHECK_STRUCTURE - returns boolean FALSE
X * vCHECK_STRUCTURE - returns nothing (void)
X * rCHECK_STRUCTURE - returns 'r' macro parameter
X */
X
X#define bCHECK_STRUCTURE(uuid, status) \
X{ \
X    if (!CHECK_STRUCTURE (uuid)) \
X    { \
X        *(status) = uuid_s_bad_version; \
X        return (FALSE); \
X    } \
X}
X
X#define vCHECK_STRUCTURE(uuid, status) \
X{ \
X    if (!CHECK_STRUCTURE (uuid)) \
X    { \
X        *(status) = uuid_s_bad_version; \
X        return; \
X    } \
X}
X
X#define rCHECK_STRUCTURE(uuid, status, result) \
X{ \
X    if (!CHECK_STRUCTURE (uuid)) \
X    { \
X        *(status) = uuid_s_bad_version; \
X        return (result); \
X    } \
X}
X\f
X/*
X**++
X**
X**  ROUTINE NAME:       init
X**
X**  SCOPE:              INTERNAL - declared locally
X**
X**  DESCRIPTION:
X**
X**  Startup initialization routine for the UUID module.
X**
X**  INPUTS:             none
X**
X**  INPUTS/OUTPUTS:     none
X**
X**  OUTPUTS:
X**
X**      status          return status value
X**
X**          uuid_s_ok
X**          uuid_s_coding_error
X**
X**  IMPLICIT INPUTS:    none
X**
X**  IMPLICIT OUTPUTS:   none
X**
X**  FUNCTION VALUE:     void
X**
X**  SIDE EFFECTS:       sets uuid_init_done so this won't be done again
X**
X**--
X**/
X
XINTERNAL void init 
X#ifdef _DCE_PROTO_
X(
X    unsigned32              *status
X)
X#else
X(status)
Xunsigned32              *status;
X#endif
X{
X#ifdef CMA_INCLUDE
X    /*
X     * Hack for CMA pthreads.  Some users will call uuid_ stuff before
X     * doing any thread stuff (CMA is uninitialized).  Some uuid_
X     * operations alloc memory and in a CMA pthread environment,
X     * the malloc wrapper uses a mutex but doesn't do self initialization
X     * (which results in segfault'ing inside of CMA).  Make sure that
X     * CMA is initialized.
X     */
X    pthread_t   t;
X    t = pthread_self();
X#endif /* CMA_INCLUDE */
X
X    CODING_ERROR (status);
X
X    /*
X     * init the random number generator
X     */
X    true_random_init();
X
X    uuid__get_os_time (&time_last);
X
X#ifdef UUID_NONVOLATILE_CLOCK
X    clock_seq = uuid__read_clock();
X#else
X    clock_seq = true_random();
X#endif
X
X    uuid_init_done = TRUE;
X
X    *status = uuid_s_ok;
X}
X\f
X/*
X**++
X**
X**  ROUTINE NAME:       uuid_create
X**
X**  SCOPE:              PUBLIC - declared in UUID.IDL
X**
X**  DESCRIPTION:
X**
X**  Create a new UUID. Note: we only know how to create the new
X**  and improved UUIDs.
X**
X**  INPUTS:             none
X**
X**  INPUTS/OUTPUTS:     none
X**
X**  OUTPUTS:
X**
X**      uuid            A new UUID value
X**
X**      status          return status value
X**
X**          uuid_s_ok
X**          uuid_s_coding_error
X**
X**  IMPLICIT INPUTS:    none
X**
X**  IMPLICIT OUTPUTS:   none
X**
X**  FUNCTION VALUE:     void
X**
X**  SIDE EFFECTS:       none
X**
X**--
X**/
X
XPUBLIC void uuid_create 
X#ifdef _DCE_PROTO_
X(
X    uuid_t                  *uuid,
X    unsigned32              *status
X)
X#else
X(uuid, status)
Xuuid_t                  *uuid;
Xunsigned32              *status;
X#endif
X{
X    uuid_address_t          eaddr;      /* our IEEE 802 hardware address */
X    boolean32               got_no_time = FALSE;
X
X    RPC_LOG_UUID_CREATE_NTR;
X
X    CODING_ERROR (status);
X    UUID_VERIFY_INIT (EmptyArg);
X
X    /*
X     * get our hardware network address
X     */
X    uuid_get_address (&eaddr, status);
X    if (*status != uuid_s_ok)
X    {
X        DEBUG_PRINT("uuid_create:uuid_get_address", *status);
X        return;
X    }
X
X    do
X    {
X        /*
X         * get the current time
X         */
X        uuid__get_os_time (&time_now);
X
X        /*
X         * do stuff like:
X         *
X         *  o check that our clock hasn't gone backwards and handle it
X         *    accordingly with clock_seq
X         *  o check that we're not generating uuid's faster than we
X         *    can accommodate with our time_adjust fudge factor
X         */
X        switch (time_cmp (&time_now, &time_last))
X        {
X            case uuid_e_less_than:
X                new_clock_seq (&clock_seq);
X                time_adjust = 0;
X                break;
X            case uuid_e_greater_than:
X                time_adjust = 0;
X                break;
X            case uuid_e_equal_to:
X                if (time_adjust == MAX_TIME_ADJUST)
X                {
X                    /*
X                     * spin your wheels while we wait for the clock to tick
X                     */
X                    got_no_time = TRUE;
X                }
X                else
X                {
X                    time_adjust++;
X                }
X                break;
X            default:
X                *status = uuid_s_internal_error;
X                DEBUG_PRINT ("uuid_create", *status);
X                return;
X        }
X    } while (got_no_time);
X
X    time_last.lo = time_now.lo;
X    time_last.hi = time_now.hi;
X
X    if (time_adjust != 0)
X    {
X        UADD_UW_2_UVLW (&time_adjust, &time_now, &time_now);
X    }
X
X    /*
X     * now construct a uuid with the information we've gathered
X     * plus a few constants
X     */
X    uuid->time_low = time_now.lo;
X    uuid->time_mid = time_now.hi & TIME_MID_MASK;
X
X    uuid->time_hi_and_version =
X        (time_now.hi & TIME_HIGH_MASK) >> TIME_HIGH_SHIFT_COUNT;
X    uuid->time_hi_and_version |= UUID_VERSION_BITS;
X
X    uuid->clock_seq_low = clock_seq & CLOCK_SEQ_LOW_MASK;
X    uuid->clock_seq_hi_and_reserved =
X        (clock_seq & CLOCK_SEQ_HIGH_MASK) >> CLOCK_SEQ_HIGH_SHIFT_COUNT;
X
X    uuid->clock_seq_hi_and_reserved |= UUID_RESERVED_BITS;
X
X    memcpy (uuid->node, &eaddr, sizeof (uuid_address_t));
X
X    *status = uuid_s_ok;
X    RPC_LOG_UUID_CREATE_XIT;
X}
X\f
X/*
X**++
X**
X**  ROUTINE NAME:       uuid_create_nil
X**
X**  SCOPE:              PUBLIC - declared in UUID.IDL
X**
X**  DESCRIPTION:
X**
X**  Create a 'nil' uuid.
X**
X**  INPUTS:             none
X**
X**  INPUTS/OUTPUTS:     none
X**
X**  OUTPUTS:
X**
X**      uuid            A nil UUID
X**
X**      status          return status value
X**
X**          uuid_s_ok
X**          uuid_s_coding_error
X**
X**  IMPLICIT INPUTS:    none
X**
X**  IMPLICIT OUTPUTS:   none
X**
X**  FUNCTION VALUE:     void
X**
X**  SIDE EFFECTS:       none
X**
X**--
X**/
X
XPUBLIC void uuid_create_nil 
X#ifdef _DCE_PROTO_
X(
X    uuid_t              *uuid,
X    unsigned32          *status
X)
X#else
X(uuid, status)
Xuuid_t              *uuid;
Xunsigned32          *status;
X#endif
X{
X    CODING_ERROR (status);
X    UUID_VERIFY_INIT (EmptyArg);
X    memset (uuid, 0, sizeof (uuid_t));
X
X    *status = uuid_s_ok;
X}
X\f
X/*
X**++
X**
X**  ROUTINE NAME:       uuid_to_string
X**
X**  SCOPE:              PUBLIC - declared in UUID.IDL
X**
X**  DESCRIPTION:
X**
X**  Encode a UUID into a printable string.
X**
X**  INPUTS:
X**
X**      uuid            A binary UUID to be converted to a string UUID.
X**
X**  INPUTS/OUTPUTS:     none
X**
X**  OUTPUTS:
X**
X**      uuid_string     The string representation of the given UUID.
X**
X**      status          return status value
X**
X**          uuid_s_ok
X**          uuid_s_bad_version
X**          uuid_s_coding_error
X**
X**  IMPLICIT INPUTS:    none
X**
X**  IMPLICIT OUTPUTS:   none
X**
X**  FUNCTION VALUE:     void
X**
X**  SIDE EFFECTS:       none
X**
X**--
X**/
X
XPUBLIC void uuid_to_string 
X#ifdef _DCE_PROTO_
X(
X    uuid_p_t                uuid,
X    unsigned_char_p_t       *uuid_string,
X    unsigned32              *status
X)
X#else
X(uuid, uuid_string, status)
Xuuid_p_t                uuid;
Xunsigned_char_p_t       *uuid_string;
Xunsigned32              *status;
X#endif
X{
X
X    CODING_ERROR (status);
X    UUID_VERIFY_INIT (EmptyArg);
X
X    /*
X     * don't do anything if the output argument is NULL
X     */
X    if (uuid_string == NULL)
X    {
X        *status = uuid_s_ok;
X        return;
X    }
X
X    vCHECK_STRUCTURE (uuid, status);
X
X    RPC_MEM_ALLOC (
X        *uuid_string,
X        unsigned_char_p_t,
X        UUID_C_UUID_STRING_MAX,
X        RPC_C_MEM_STRING,
X        RPC_C_MEM_WAITOK);
X
X    if (*uuid_string == NULL)
X    {
X        *status = uuid_s_no_memory;
X        return;
X    }
X
X    UUID_SPRINTF(
X        (char *) *uuid_string,
X        "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
X        uuid->time_low, uuid->time_mid, uuid->time_hi_and_version,
X        uuid->clock_seq_hi_and_reserved, uuid->clock_seq_low,
X        (unsigned8) uuid->node[0], (unsigned8) uuid->node[1],
X        (unsigned8) uuid->node[2], (unsigned8) uuid->node[3],
X        (unsigned8) uuid->node[4], (unsigned8) uuid->node[5]);
X
X    *status = uuid_s_ok;
X}
X\f
X/*
X**++
X**
X**  ROUTINE NAME:       uuid_from_string
X**
X**  SCOPE:              PUBLIC - declared in UUID.IDL
X**
X**  DESCRIPTION:
X**
X**  Decode a UUID from a printable string.
X**
X**  INPUTS:
X**
X**      uuid_string     The string UUID to be converted to a binary UUID
X**
X**  INPUTS/OUTPUTS:     none
X**
X**  OUTPUTS:
X**
X**      uuid            The binary representation of the given UUID
X**
X**      status          return status value
X**
X**          uuid_s_ok
X**          uuid_s_bad_version
X**          uuid_s_invalid_string_uuid
X**          uuid_s_coding_error
X**
X**  IMPLICIT INPUTS:    none
X**
X**  IMPLICIT OUTPUTS:   none
X**
X**  FUNCTION VALUE:     void
X**
X**  SIDE EFFECTS:       none
X**
X**--
X**/
X
XPUBLIC void uuid_from_string 
X#ifdef _DCE_PROTO_
X(
X    unsigned_char_p_t       uuid_string,
X    uuid_t                  *uuid,
X    unsigned32              *status
X)
X#else
X(uuid_string, uuid, status)
Xunsigned_char_p_t       uuid_string;
Xuuid_t                  *uuid;
Xunsigned32              *status;
X#endif
X{
X    uuid_t              uuid_new;       /* used for sscanf for new uuid's */
X    uuid_old_t          uuid_old;       /* used for sscanf for old uuid's */
X    uuid_p_t            uuid_ptr;       /* pointer to correct uuid (old/new) */
X    int                 i;
X
X
X    CODING_ERROR (status);
X    UUID_VERIFY_INIT(EmptyArg);
X
X    /*
X     * If a NULL pointer or empty string, give the nil UUID.
X     */
X    if (uuid_string == NULL || *uuid_string == '\0')
X    {
X	memcpy (uuid, &uuid_g_nil_uuid, sizeof *uuid);
X	*status = uuid_s_ok;
X	return;
X    }
X
X    /*
X     * check to see that the string length is right at least
X     */
X    if (strlen ((char *) uuid_string) != UUID_C_UUID_STRING_MAX - 1)
X    {
X        *status = uuid_s_invalid_string_uuid;
X        return;
X    }
X
X    /*
X     * check for a new uuid
X     */
X    if (uuid_string[8] == '-')
X    {
X        long    time_low;
X        int     time_mid;
X        int     time_hi_and_version;
X        int     clock_seq_hi_and_reserved;
X        int     clock_seq_low;
X        int     node[6];
X
X
X        i = UUID_SSCANF(
X            (char *) uuid_string, "%8lx-%4x-%4x-%2x%2x-%2x%2x%2x%2x%2x%2x",
X            &time_low,
X            &time_mid,
X            &time_hi_and_version,
X            &clock_seq_hi_and_reserved,
X            &clock_seq_low,
X            &node[0], &node[1], &node[2], &node[3], &node[4], &node[5]);
X
X        /*
X         * check that sscanf worked
X         */
X        if (i != UUID_ELEMENTS_NUM)
X        {
X            *status = uuid_s_invalid_string_uuid;
X            return;
X        }
X
X        /*
X         * note that we're going through this agony because scanf is defined to
X         * know only to scan into "int"s or "long"s.
X         */
X        uuid_new.time_low                   = time_low;
X        uuid_new.time_mid                   = time_mid;
X        uuid_new.time_hi_and_version        = time_hi_and_version;
X        uuid_new.clock_seq_hi_and_reserved  = clock_seq_hi_and_reserved;
X        uuid_new.clock_seq_low              = clock_seq_low;
X        uuid_new.node[0]                    = node[0];
X        uuid_new.node[1]                    = node[1];
X        uuid_new.node[2]                    = node[2];
X        uuid_new.node[3]                    = node[3];
X        uuid_new.node[4]                    = node[4];
X        uuid_new.node[5]                    = node[5];
X
X        /*
X         * point to the correct uuid
X         */
X        uuid_ptr = &uuid_new;
X    }
X    else
X    {
X        long    time_high;
X        int     time_low;
X        int     family;
X        int     host[7];
X
X
X        /*
X         * format = tttttttttttt.ff.h1.h2.h3.h4.h5.h6.h7
X         */
X        i = UUID_OLD_SSCANF(
X            (char *) uuid_string, "%8lx%4x.%2x.%2x.%2x.%2x.%2x.%2x.%2x.%2x",
X            &time_high, &time_low, &family,
X            &host[0], &host[1], &host[2], &host[3],
X            &host[4], &host[5], &host[6]);
X
X        /*
X         * check that sscanf worked
X         */
X        if (i != UUID_ELEMENTS_NUM_OLD)
X        {
X            *status = uuid_s_invalid_string_uuid;
X            return;
X        }
X
X        /*
X         * note that we're going through this agony because scanf is defined to
X         * know only to scan into "int"s or "long"s.
X         */
X        uuid_old.time_high      = time_high;
X        uuid_old.time_low       = time_low;
X        uuid_old.family         = family;
X        uuid_old.host[0]        = host[0];
X        uuid_old.host[1]        = host[1];
X        uuid_old.host[2]        = host[2];
X        uuid_old.host[3]        = host[3];
X        uuid_old.host[4]        = host[4];
X        uuid_old.host[5]        = host[5];
X        uuid_old.host[6]        = host[6];
X
X        /*
X         * fix up non-string field, and point to the correct uuid
X         */
X        uuid_old.reserved = 0;
X        uuid_ptr = (uuid_p_t) (&uuid_old);
X    }
X
X    /*
X     * sanity check, is version ok?
X     */
X    vCHECK_STRUCTURE (uuid_ptr, status);
X
X    /*
X     * copy the uuid to user
X     */
X    memcpy (uuid, uuid_ptr, sizeof (uuid_t));
X
X    *status = uuid_s_ok;
X}
X\f
X/*
X**++
X**
X**  ROUTINE NAME:       uuid_equal
X**
X**  SCOPE:              PUBLIC - declared in UUID.IDL
X**
X**  DESCRIPTION:
X**
X**  Compare two UUIDs.
X**
X**  INPUTS:
X**
X**      uuid1           The first UUID to compare
X**
X**      uuid2           The second UUID to compare
X**
X**  INPUTS/OUTPUTS:     none
X**
X**  OUTPUTS:
X**
X**      status          return status value
X**
X**          uuid_s_ok
X**          uuid_s_bad_version
X**          uuid_s_coding_error
X**
X**  IMPLICIT INPUTS:    none
X**
X**  IMPLICIT OUTPUTS:   none
X**
X**  FUNCTION VALUE:
X**
X**      result          true if UUID's are equal
X**                      false if UUID's are not equal
X**
X**  SIDE EFFECTS:       none
X**
X**--
X**/
X
XPUBLIC boolean32 uuid_equal 
X#ifdef _DCE_PROTO_
X(
X    register uuid_p_t                uuid1,
X    register uuid_p_t                uuid2,
X    register unsigned32              *status
X)
X#else
X(uuid1, uuid2, status)
Xregister uuid_p_t                uuid1;
Xregister uuid_p_t                uuid2;
Xregister unsigned32              *status;
X#endif
X{
X    RPC_LOG_UUID_EQUAL_NTR;
X    CODING_ERROR (status);
X    UUID_VERIFY_INIT (FALSE);
X
X    bCHECK_STRUCTURE (uuid1, status);
X    bCHECK_STRUCTURE (uuid2, status);
X
X    *status = uuid_s_ok;
X
X    /*
X     * Note: This used to be a memcmp(), but changed to a field-by-field compare
X     * because of portability problems with alignment and garbage in a UUID.
X     */
X    if ((uuid1->time_low == uuid2->time_low) && 
X	(uuid1->time_mid == uuid2->time_mid) &&
X	(uuid1->time_hi_and_version == uuid2->time_hi_and_version) && 
X	(uuid1->clock_seq_hi_and_reserved == uuid2->clock_seq_hi_and_reserved) &&
X	(uuid1->clock_seq_low == uuid2->clock_seq_low) &&
X	(memcmp(uuid1->node, uuid2->node, 6) == 0))
X    {
X	RPC_LOG_UUID_EQUAL_XIT;
X	return ( TRUE );
X    }
X    else
X    {
X        RPC_LOG_UUID_EQUAL_XIT;
X        return (FALSE);
X    }
X}
X\f
X/*
X**++
X**
X**  ROUTINE NAME:       uuid_is_nil
X**
X**  SCOPE:              PUBLIC - declared in UUID.IDL
X**
X**  DESCRIPTION:
X**
X**  Check to see if a given UUID is 'nil'.
X**
X**  INPUTS:             none
X**
X**      uuid            A UUID
X**
X**  INPUTS/OUTPUTS:     none
X**
X**  OUTPUTS:
X**
X**      status          return status value
X**
X**          uuid_s_ok
X**          uuid_s_bad_version
X**          uuid_s_coding_error
X**
X**  IMPLICIT INPUTS:    none
X**
X**  IMPLICIT OUTPUTS:   none
X**
X**  FUNCTION VALUE:
X**
X**      result          true if UUID is nil
X**                      false if UUID is not nil
X**
X**  SIDE EFFECTS:       none
X**
X**--
X**/
X
XPUBLIC boolean32 uuid_is_nil 
X#ifdef _DCE_PROTO_
X(
X    uuid_p_t            uuid,
X    unsigned32          *status
X)
X#else
X(uuid, status)
Xuuid_p_t            uuid;
Xunsigned32          *status;
X#endif
X{
X    CODING_ERROR (status);
X    UUID_VERIFY_INIT (FALSE);
X
X    bCHECK_STRUCTURE (uuid, status);
X
X    *status = uuid_s_ok;
X
X    /*
X     * Note: This should later be changed to a field-by-field compare
X     * because of portability problems with alignment and garbage in a UUID.
X     */
X    if (memcmp (uuid, &uuid_g_nil_uuid, sizeof (uuid_t)) == 0)
X    {
X        return (TRUE);
X    }
X    else
X    {
X        return (FALSE);
X    }
X}
X\f
X/*
X**++
X**
X**  ROUTINE NAME:       uuid_compare
X**
X**  SCOPE:              PUBLIC - declared in UUID.IDL
X**
X**  DESCRIPTION:
X**
X**  Compare two UUID's "lexically"
X**
X**  If either of the two arguments is given as a NULL pointer, the other
X**  argument will be compared to the nil uuid.
X**
X**  Note:   1) lexical ordering is not temporal ordering!
X**          2) in the interest of keeping this routine short, I have
X**             violated the coding convention that says all if/else
X**             constructs shall have {}'s. There are a little million
X**             return()'s in this routine. FWIW, the only {}'s that
X**             are really required are the ones in the for() loop.
X**
X**  INPUTS:
X**
X**      uuid1           The first UUID to compare
X**
X**      uuid2           The second UUID to compare
X**
X**  INPUTS/OUTPUTS:     none
X**
X**  OUTPUTS:
X**
X**      status          return status value
X**
X**          uuid_s_ok
X**          uuid_s_bad_version
X**          uuid_s_coding_error
X**
X**  IMPLICIT INPUTS:    none
X**
X**  IMPLICIT OUTPUTS:   none
X**
X**  FUNCTION VALUE:     uuid_order_t
X**
X**      -1   uuid1 is lexically before uuid2
X**      1    uuid1 is lexically after uuid2
X**
X**  SIDE EFFECTS:       none
X**
X**--
X**/
X
XPUBLIC signed32 uuid_compare 
X#ifdef _DCE_PROTO_
X(
X    uuid_p_t                uuid1,
X    uuid_p_t                uuid2,
X    unsigned32              *status
X)
X#else
X(uuid1, uuid2, status)
Xuuid_p_t                uuid1;
Xuuid_p_t                uuid2;
Xunsigned32              *status;
X#endif
X{
X    int                 i;
X
X    CODING_ERROR (status);
X    UUID_VERIFY_INIT (FALSE);
X
X
X    /*
X     * check to see if either of the arguments is a NULL pointer
X     * - if so, compare the other argument to the nil uuid
X     */
X    if (uuid1 == NULL)
X    {
X        /*
X         * if both arguments are NULL, so is this routine
X         */
X        if (uuid2 == NULL)
X        {
X            *status = uuid_s_ok;
X            return (0);
X        }
X
X        rCHECK_STRUCTURE (uuid2, status, -1);
X        return (uuid_is_nil (uuid2, status) ? 0 : -1);
X    }
X
X    if (uuid2 == NULL)
X    {
X        rCHECK_STRUCTURE (uuid1, status, -1);
X        return (uuid_is_nil (uuid1, status) ? 0 : 1);
X    }
X
X    rCHECK_STRUCTURE (uuid1, status, -1);
X    rCHECK_STRUCTURE (uuid2, status, -1);
X
X    *status = uuid_s_ok;
X
X    if (uuid1->time_low == uuid2->time_low)
X    {
X        if (uuid1->time_mid == uuid2->time_mid)
X        {
X            if (uuid1->time_hi_and_version == uuid2->time_hi_and_version)
X            {
X                if (uuid1->clock_seq_hi_and_reserved
X                    == uuid2->clock_seq_hi_and_reserved)
X                {
X                    if (uuid1->clock_seq_low == uuid2->clock_seq_low)
X                    {
X                        for (i = 0; i < 6; i++)
X                        {
X                            if (uuid1->node[i] < uuid2->node[i])
X                                return (-1);
X                            if (uuid1->node[i] > uuid2->node[i])
X                                return (1);
X                        }
X                        return (0);
X                    }       /* end if - clock_seq_low */
X                    else
X                    {
X                        if (uuid1->clock_seq_low < uuid2->clock_seq_low)
X                            return (-1);
X                        else
X                            return (1);
X                    }       /* end else - clock_seq_low */
X                }           /* end if - clock_seq_hi_and_reserved */
X                else
X                {
X                    if (uuid1->clock_seq_hi_and_reserved
X                        < uuid2->clock_seq_hi_and_reserved)
X                        return (-1);
X                    else
X                        return (1);
X                }           /* end else - clock_seq_hi_and_reserved */
X            }               /* end if - time_hi_and_version */
X            else
X            {
X                if (uuid1->time_hi_and_version < uuid2->time_hi_and_version)
X                    return (-1);
X                else
X                    return (1);
X            }               /* end else - time_hi_and_version */
X        }                   /* end if - time_mid */
X        else
X        {
X            if (uuid1->time_mid < uuid2->time_mid)
X                return (-1);
X            else
X                return (1);
X        }                   /* end else - time_mid */
X    }                       /* end if - time_low */
X    else
X    {
X        if (uuid1->time_low < uuid2->time_low)
X            return (-1);
X        else
X            return (1);
X    }                       /* end else - time_low */
X}
X\f
X/*
X**++
X**
X**  ROUTINE NAME:       uuid_hash
X**
X**  SCOPE:              PUBLIC - declared in UUID.IDL
X**
X**  DESCRIPTION:
X**
X**  Return a hash value for a given UUID.
X**
X**  Note: Since the length of a UUID is architecturally defined to be
X**        128 bits (16 bytes), we have forgone using a '#defined'
X**        length.  In particular, since the 'loop' has been unrolled
X**        (for performance) the length is by definition 'hard-coded'.
X**
X**  INPUTS:
X**
X**      uuid            A UUID for which a hash value is to be computed
X**
X**  INPUTS/OUTPUTS:     none
X**
X**  OUTPUTS:
X**
X**      status          return status value
X**
X**          uuid_s_ok
X**          uuid_s_bad_version
X**          uuid_s_coding_error
X**
X**  IMPLICIT INPUTS:    none
X**
X**  IMPLICIT OUTPUTS:   none
X**
X**  FUNCTION VALUE:
X**
X**      hash_value      The hash value computed from the UUID
X**
X**  SIDE EFFECTS:       none
X**
X**--
X**/
X
XPUBLIC unsigned16 uuid_hash 
X#ifdef _DCE_PROTO_
X(
X    uuid_p_t                uuid,
X    unsigned32              *status
X)
X#else
X(uuid, status)
Xuuid_p_t                uuid;
Xunsigned32              *status;
X#endif
X{
X    short               c0, c1;
X    short               x, y;
X    byte_p_t            next_uuid;
X
X    RPC_LOG_UUID_HASH_NTR;
X
X    CODING_ERROR (status);
X    UUID_VERIFY_INIT (FALSE);
X
X    rCHECK_STRUCTURE (uuid, status, 0);
X
X    /*
X     * initialize counters
X     */
X    c0 = c1 = 0;
X    next_uuid = (byte_p_t) uuid;
X
X    /*
X     * For speed lets unroll the following loop:
X     *
X     *   for (i = 0; i < UUID_K_LENGTH; i++)
X     *   {
X     *       c0 = c0 + *next_uuid++;
X     *       c1 = c1 + c0;
X     *   }
X     */
X    c0 = c0 + *next_uuid++;
X    c1 = c1 + c0;
X    c0 = c0 + *next_uuid++;
X    c1 = c1 + c0;
X    c0 = c0 + *next_uuid++;
X    c1 = c1 + c0;
X    c0 = c0 + *next_uuid++;
X    c1 = c1 + c0;
X
X    c0 = c0 + *next_uuid++;
X    c1 = c1 + c0;
X    c0 = c0 + *next_uuid++;
X    c1 = c1 + c0;
X    c0 = c0 + *next_uuid++;
X    c1 = c1 + c0;
X    c0 = c0 + *next_uuid++;
X    c1 = c1 + c0;
X
X    c0 = c0 + *next_uuid++;
X    c1 = c1 + c0;
X    c0 = c0 + *next_uuid++;
X    c1 = c1 + c0;
X    c0 = c0 + *next_uuid++;
X    c1 = c1 + c0;
X    c0 = c0 + *next_uuid++;
X    c1 = c1 + c0;
X
X    c0 = c0 + *next_uuid++;
X    c1 = c1 + c0;
X    c0 = c0 + *next_uuid++;
X    c1 = c1 + c0;
X    c0 = c0 + *next_uuid++;
X    c1 = c1 + c0;
X    c0 = c0 + *next_uuid++;
X    c1 = c1 + c0;
X
X    /*
X     *  Calculate the value for "First octet" of the hash
X     */
X    x = -c1 % 255;
X    if (x < 0)
X    {
X        x = x + 255;
X    }
X
X    /*
X     *  Calculate the value for "second octet" of the hash
X     */
X    y = (c1 - c0) % 255;
X    if (y < 0)
X    {
X        y = y + 255;
X    }
X
X    /*
X     * return the pieces put together
X     */
X    *status = uuid_s_ok;
X
X    RPC_LOG_UUID_HASH_XIT;
X    return ((y * 256) + x);
X}
X\f
X/*****************************************************************************
X *
X *  LOCAL MATH PROCEDURES - math procedures used internally by the UUID module
X *
X ****************************************************************************/
X
X/*
X** T I M E _ C M P
X**
X** Compares two UUID times (64-bit UTC values)
X**/
X
XINTERNAL uuid_compval_t time_cmp 
X#ifdef _DCE_PROTO_
X(
X    uuid_time_p_t           time1,
X    uuid_time_p_t           time2
X)
X#else
X(time1, time2)
Xuuid_time_p_t           time1;
Xuuid_time_p_t           time2;
X#endif
X{
X    /*
X     * first check the hi parts
X     */
X    if (time1->hi < time2->hi) return (uuid_e_less_than);
X    if (time1->hi > time2->hi) return (uuid_e_greater_than);
X
X    /*
X     * hi parts are equal, check the lo parts
X     */
X    if (time1->lo < time2->lo) return (uuid_e_less_than);
X    if (time1->lo > time2->lo) return (uuid_e_greater_than);
X
X    return (uuid_e_equal_to);
X}
X\f
X/*
X**  U U I D _ _ U E M U L
X**
X**  Functional Description:
X**        32-bit unsigned quantity * 32-bit unsigned quantity
X**        producing 64-bit unsigned result. This routine assumes
X**        long's contain at least 32 bits. It makes no assumptions
X**        about byte orderings.
X**
X**  Inputs:
X**
X**        u, v       Are the numbers to be multiplied passed by value
X**
X**  Outputs:
X**
X**        prodPtr    is a pointer to the 64-bit result
X**
X**  Note:
X**        This algorithm is taken from: "The Art of Computer
X**        Programming", by Donald E. Knuth. Vol 2. Section 4.3.1
X**        Pages: 253-255.
X**--
X**/
X
XPRIVATE void uuid__uemul 
X#ifdef _DCE_PROTO_
X(
X    unsigned32          u,
X    unsigned32          v,
X    unsigned64_t        *prodPtr
X)
X#else
X(u, v, prodPtr)
Xunsigned32          u;
Xunsigned32          v;
Xunsigned64_t        *prodPtr;
X#endif
X{
X    /*
X     * following the notation in Knuth, Vol. 2
X     */
X    unsigned32      uuid1, uuid2, v1, v2, temp;
X
X
X    uuid1 = u >> 16;
X    uuid2 = u & 0xffff;
X    v1 = v >> 16;
X    v2 = v & 0xffff;
X
X    temp = uuid2 * v2;
X    prodPtr->lo = temp & 0xffff;
X    temp = uuid1 * v2 + (temp >> 16);
X    prodPtr->hi = temp >> 16;
X    temp = uuid2 * v1 + (temp & 0xffff);
X    prodPtr->lo += (temp & 0xffff) << 16;
X    prodPtr->hi += uuid1 * v1 + (temp >> 16);
X}
X\f
X/****************************************************************************
X**
X**    U U I D   T R U E   R A N D O M   N U M B E R   G E N E R A T O R
X**
X*****************************************************************************
X**
X** This random number generator (RNG) was found in the ALGORITHMS Notesfile.
X**
X** (Note 16.7, July 7, 1989 by Robert (RDVAX::)Gries, Cambridge Research Lab,
X**  Computational Quality Group)
X**
X** It is really a "Multiple Prime Random Number Generator" (MPRNG) and is
X** completely discussed in reference #1 (see below).
X**
X**   References:
X**   1) "The Multiple Prime Random Number Generator" by Alexander Hass
X**      pp. 368 to 381 in ACM Transactions on Mathematical Software,
X**      December, 1987
X**   2) "The Art of Computer Programming: Seminumerical Algorithms
X**      (vol 2)" by Donald E. Knuth, pp. 39 to 113.
X**
X** A summary of the notesfile entry follows:
X**
X** Gries discusses the two RNG's available for ULTRIX-C.  The default RNG
X** uses a Linear Congruential Method (very popular) and the second RNG uses
X** a technique known as a linear feedback shift register.
X**
X** The first (default) RNG suffers from bit-cycles (patterns/repetition),
X** ie. it's "not that random."
X**
X** While the second RNG passes all the emperical tests, there are "states"
X** that become "stable", albeit contrived.
X**
X** Gries then presents the MPRNG and says that it passes all emperical
X** tests listed in reference #2.  In addition, the number of calls to the
X** MPRNG before a sequence of bit position repeats appears to have a normal
X** distribution.
X**
X** Note (mbs): I have coded the Gries's MPRNG with the same constants that
X** he used in his paper.  I have no way of knowing whether they are "ideal"
X** for the range of numbers we are dealing with.
X**
X****************************************************************************/
X\f
X/*
X** T R U E _ R A N D O M _ I N I T
X**
X** Note: we "seed" the RNG with the bits from the clock and the PID
X**
X**/
X
XINTERNAL void true_random_init (void)
X{
X    uuid_time_t         t;
X    unsigned16          *seedp, seed=0;
X
X
X    /*
X     * optimal/recommended starting values according to the reference
X     */
X    static unsigned32   rand_m_init     = 971;
X    static unsigned32   rand_ia_init    = 11113;
X    static unsigned32   rand_ib_init    = 104322;
X    static unsigned32   rand_irand_init = 4181;
X
X    rand_m = rand_m_init;
X    rand_ia = rand_ia_init;
X    rand_ib = rand_ib_init;
X    rand_irand = rand_irand_init;
X
X    /*
X     * Generating our 'seed' value
X     *
X     * We start with the current time, but, since the resolution of clocks is
X     * system hardware dependent (eg. Ultrix is 10 msec.) and most likely
X     * coarser than our resolution (10 usec) we 'mixup' the bits by xor'ing
X     * all the bits together.  This will have the effect of involving all of
X     * the bits in the determination of the seed value while remaining system
X     * independent.  Then for good measure to ensure a unique seed when there
X     * are multiple processes creating UUID's on a system, we add in the PID.
X     */
X    uuid__get_os_time(&t);
X    seedp = (unsigned16 *)(&t);
X    seed ^= *seedp++;
X    seed ^= *seedp++;
X    seed ^= *seedp++;
X    seed ^= *seedp++;
X    rand_irand += seed + uuid__get_os_pid();
X}
X\f
X/*
X** T R U E _ R A N D O M
X**
X** Note: we return a value which is 'tuned' to our purposes.  Anyone
X** using this routine should modify the return value accordingly.
X**/
X
XINTERNAL unsigned16 true_random (void)
X{
X    rand_m += 7;
X    rand_ia += 1907;
X    rand_ib += 73939;
X
X    if (rand_m >= 9973) rand_m -= 9871;
X    if (rand_ia >= 99991) rand_ia -= 89989;
X    if (rand_ib >= 224729) rand_ib -= 96233;
X
X    rand_irand = (rand_irand * rand_m) + rand_ia + rand_ib;
X
X    return (HI_WORD (rand_irand) ^ (rand_irand & RAND_MASK));
X}
X\f
X/*****************************************************************************
X *
X *  LOCAL PROCEDURES - procedures used staticly by the UUID module
X *
X ****************************************************************************/
X
X/*
X** N E W _ C L O C K _ S E Q
X**
X** Ensure *clkseq is up-to-date
X**
X** Note: clock_seq is architected to be 14-bits (unsigned) but
X**       I've put it in here as 16-bits since there isn't a
X**       14-bit unsigned integer type (yet)
X**/
X
XINTERNAL void new_clock_seq 
X#ifdef _DCE_PROTO_
X(
X    unsigned16              *clkseq
X)
X#else
X(clkseq)
Xunsigned16              *clkseq;
X#endif
X{
X    /*
X     * A clkseq value of 0 indicates that it hasn't been initialized.
X     */
X    if (*clkseq == 0)
X    {
X#ifdef UUID_NONVOLATILE_CLOCK
X        *clkseq = uuid__read_clock();           /* read nonvolatile clock */
X        if (*clkseq == 0)                       /* still not init'd ???   */
X        {
X            *clkseq = true_random();      /* yes, set random        */
X        }
X#else
X        /*
X         * with a volatile clock, we always init to a random number
X         */
X        *clkseq = true_random();
X#endif
X    }
X
X    CLOCK_SEQ_BUMP (clkseq);
X    if (*clkseq == 0)
X    {
X        *clkseq = *clkseq + 1;
X    }
X
X#ifdef UUID_NONVOLATILE_CLOCK
X    uuid_write_clock (clkseq);
X#endif
X}
X\f
X/*
X**++
X**
X**  ROUTINE NAME:       uuid_get_address
X**
X**  SCOPE:              PUBLIC
X**
X**  DESCRIPTION:
X**
X**  Return our IEEE 802 address.
X**
X**  This function is not really "public", but more like the SPI functions
X**  -- available but not part of the official API.  We've done this so
X**  that other subsystems (of which there are hopefully few or none)
X**  that need the IEEE 802 address can use this function rather than
X**  duplicating the gore it does (or more specifically, the gore that
X**  "uuid__get_os_address" does).
X**
X**  INPUTS:             none
X**
X**  INPUTS/OUTPUTS:     none
X**
X**  OUTPUTS:
X**
X**      addr            IEEE 802 address
X**
X**      status          return status value
X**
X**  IMPLICIT INPUTS:    none
X**
X**  IMPLICIT OUTPUTS:   none
X**
X**  FUNCTION VALUE:     none
X**
X**  SIDE EFFECTS:       none
X**
X**--
X**/
X
XPUBLIC void uuid_get_address 
X#ifdef _DCE_PROTO_
X(
X    uuid_address_p_t        addr,
X    unsigned32              *status
X)
X#else
X(addr, status)
Xuuid_address_p_t        addr;
Xunsigned32              *status;
X#endif
X{
X    /*
X     * just return address we determined previously if we've
X     * already got one
X     */
X    if (got_address)
X    {
X        memcpy (addr, &saved_addr, sizeof (uuid_address_t));
X        *status = saved_st;
X        return;
X    }
X
X    /*
X     * Otherwise, call the system specific routine.
X     */
X    uuid__get_os_address (addr, status);
X
X    if (*status == uuid_s_ok)
X    {
X        got_address = TRUE;
X        memcpy (&saved_addr, addr, sizeof (uuid_address_t));
X
X#ifdef  UUID_DEBUG
X        RPC_DBG_GPRINTF ((
X	    "uuid_get_address:        %02x-%02x-%02x-%02x-%02x-%02x\n",
X            addr->eaddr[0], addr->eaddr[1], addr->eaddr[2],
X            addr->eaddr[3], addr->eaddr[4], addr->eaddr[5] ));
X#endif
X
X    }
X    else
X    {
X        *status = uuid_s_no_address;
X    }
X}
END_OF_FILE
  if test 46174 -ne `wc -c <'uuid.c'`; then
    echo shar: \"'uuid.c'\" unpacked with wrong size!
  fi
  # end of 'uuid.c'
fi
if test -f 'uuidp.h' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'uuidp.h'\"
else
  echo shar: Extracting \"'uuidp.h'\" \(8299 characters\)
  sed "s/^X//" >'uuidp.h' <<'END_OF_FILE'
X/*
X * 
X * (c) Copyright 1989 OPEN SOFTWARE FOUNDATION, INC.
X * (c) Copyright 1989 HEWLETT-PACKARD COMPANY
X * (c) Copyright 1989 DIGITAL EQUIPMENT CORPORATION
X * To anyone who acknowledges that this file is provided "AS IS"
X * without any express or implied warranty:
X *                 permission to use, copy, modify, and distribute this
X * file for any purpose is hereby granted without fee, provided that
X * the above copyright notices and this notice appears in all source
X * code copies, and that none of the names of Open Software
X * Foundation, Inc., Hewlett-Packard Company, or Digital Equipment
X * Corporation be used in advertising or publicity pertaining to
X * distribution of the software without specific, written prior
X * permission.  Neither Open Software Foundation, Inc., Hewlett-
X * Packard Company, nor Digital Equipment Corporation makes any
X * representations about the suitability of this software for any
X * purpose.
X * 
X */
X/*
X */
X#ifndef _UUIDP_H
X#define _UUIDP_H	1
X/*
X**
X**  NAME:
X**
X**      uuidp.h
X**
X**  FACILITY:
X**
X**      UUID
X**
X**  ABSTRACT:
X**
X**      Interface Definitions for UUID internal subroutines used by
X**      the uuid module
X**
X**
X*/
X
X#ifndef _DCE_PROTOTYPE_
X#include <dce/dce.h>
X#endif
X
X/*
X * Max size of a uuid string: tttttttt-tttt-cccc-cccc-nnnnnnnnnnnn
X * Note: this includes the implied '\0'
X */
X#define UUID_C_UUID_STRING_MAX          37
X
X/*
X * defines for time calculations
X */
X#ifndef UUID_C_100NS_PER_SEC
X#define UUID_C_100NS_PER_SEC            10000000
X#endif
X
X#ifndef UUID_C_100NS_PER_USEC
X#define UUID_C_100NS_PER_USEC           10
X#endif
X
X\f
X
X/*
X * UADD_UVLW_2_UVLW - macro to add two unsigned 64-bit long integers
X *                      (ie. add two unsigned 'very' long words)
X *
X * Important note: It is important that this macro accommodate (and it does)
X *                 invocations where one of the addends is also the sum.
X *
X * This macro was snarfed from the DTSS group and was originally:
X *
X * UTCadd - macro to add two UTC times
X *
X * add lo and high order longword separately, using sign bits of the low-order
X * longwords to determine carry.  sign bits are tested before addition in two
X * cases - where sign bits match. when the addend sign bits differ the sign of
X * the result is also tested:
X *
X *        sign            sign
X *      addend 1        addend 2        carry?
X *
X *          1               1            TRUE
X *          1               0            TRUE if sign of sum clear
X *          0               1            TRUE if sign of sum clear
X *          0               0            FALSE
X */
X#define UADD_UVLW_2_UVLW(add1, add2, sum)                               \
X    if (!(((add1)->lo&0x80000000UL) ^ ((add2)->lo&0x80000000UL)))           \
X    {                                                                   \
X        if (((add1)->lo&0x80000000UL))                                    \
X        {                                                               \
X            (sum)->lo = (add1)->lo + (add2)->lo ;                       \
X            (sum)->hi = (add1)->hi + (add2)->hi+1 ;                     \
X        }                                                               \
X        else                                                            \
X        {                                                               \
X            (sum)->lo  = (add1)->lo + (add2)->lo ;                      \
X            (sum)->hi = (add1)->hi + (add2)->hi ;                       \
X        }                                                               \
X    }                                                                   \
X    else                                                                \
X    {                                                                   \
X        (sum)->lo = (add1)->lo + (add2)->lo ;                           \
X        (sum)->hi = (add1)->hi + (add2)->hi ;                           \
X        if (!((sum)->lo&0x80000000UL))                                    \
X            (sum)->hi++ ;                                               \
X    }
X
X\f
X/*
X * UADD_ULW_2_UVLW - macro to add a 32-bit unsigned integer to
X *                   a 64-bit unsigned integer
X *
X * Note: see the UADD_UVLW_2_UVLW() macro
X *
X */
X#define UADD_ULW_2_UVLW(add1, add2, sum)                                \
X{                                                                       \
X    (sum)->hi = (add2)->hi;                                             \
X    if ((*add1) & (add2)->lo & 0x80000000UL)                              \
X    {                                                                   \
X        (sum)->lo = (*add1) + (add2)->lo;                               \
X        (sum)->hi++;                                                    \
X    }                                                                   \
X    else                                                                \
X    {                                                                   \
X        (sum)->lo = (*add1) + (add2)->lo;                               \
X        if (!((sum)->lo & 0x80000000UL))                                  \
X        {                                                               \
X            (sum)->hi++;                                                \
X        }                                                               \
X    }                                                                   \
X}
X
X
X/*
X * UADD_UW_2_UVLW - macro to add a 16-bit unsigned integer to
X *                   a 64-bit unsigned integer
X *
X * Note: see the UADD_UVLW_2_UVLW() macro
X *
X */
X#define UADD_UW_2_UVLW(add1, add2, sum)                                 \
X{                                                                       \
X    (sum)->hi = (add2)->hi;                                             \
X    if ((add2)->lo & 0x80000000UL)                                        \
X    {                                                                   \
X        (sum)->lo = (*add1) + (add2)->lo;                               \
X        if (!((sum)->lo & 0x80000000UL))                                  \
X        {                                                               \
X            (sum)->hi++;                                                \
X        }                                                               \
X    }                                                                   \
X    else                                                                \
X    {                                                                   \
X        (sum)->lo = (*add1) + (add2)->lo;                               \
X    }                                                                   \
X}
X
X\f
X
X/*
X * a macro to set *status uuid_s_coding_error
X */
X#ifdef  CODING_ERROR
X#undef  CODING_ERROR
X#endif
X
X#ifdef  DEBUG
X#define CODING_ERROR(status)        *(status) = uuid_s_coding_error
X#else
X#define CODING_ERROR(status)
X#endif
X
X
Xtypedef struct
X{
X    char eaddr[6];      /* 6 bytes of ethernet hardware address */
X} uuid_address_t, *uuid_address_p_t;
X
X
Xtypedef struct
X{
X    unsigned32  lo;
X    unsigned32  hi;
X} uuid_time_t, *uuid_time_p_t;
X
X
Xtypedef struct
X{
X    unsigned32  lo;
X    unsigned32  hi;
X} unsigned64_t, *unsigned64_p_t;
X
X
X/*
X * U U I D _ _ U E M U L
X *
X * 32-bit unsigned * 32-bit unsigned multiply -> 64-bit result
X */
XPRIVATE void uuid__uemul _DCE_PROTOTYPE_ ((
X        unsigned32           /*u*/,
X        unsigned32           /*v*/,
X        unsigned64_t        * /*prodPtr*/
X    ));
X
X/*
X * U U I D _ _ R E A D _ C L O C K
X */
XPRIVATE unsigned16 uuid__read_clock _DCE_PROTOTYPE_ (( void ));
X
X
X/*
X * U U I D _ _ W R I T E _ C L O C K
X */
XPRIVATE void uuid__write_clock _DCE_PROTOTYPE_ (( unsigned16 /*time*/ ));
X
X
X/*
X * U U I D _ _ G E T _ O S _ P I D
X *
X * Get the process id
X */
XPRIVATE unsigned32 uuid__get_os_pid _DCE_PROTOTYPE_ (( void ));
X
X
X/*
X * U U I D _ _ G E T _ O S _ T I M E
X *
X * Get OS time
X */
XPRIVATE void uuid__get_os_time _DCE_PROTOTYPE_(( uuid_time_t * /*uuid_time*/));
X
X
X/*
X * U U I D _ _ G E T _ O S _ A D D R E S S
X *
X * Get ethernet hardware address from the OS
X */
XPRIVATE void uuid__get_os_address _DCE_PROTOTYPE_ ((
X        uuid_address_t          * /*address*/,
X        unsigned32              * /*st*/
X    ));
X
X#endif /* _UUIDP_H */
END_OF_FILE
  if test 8299 -ne `wc -c <'uuidp.h'`; then
    echo shar: \"'uuidp.h'\" unpacked with wrong size!
  fi
  # end of 'uuidp.h'
fi
if test -f 'uuidsys.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'uuidsys.c'\"
else
  echo shar: Extracting \"'uuidsys.c'\" \(3403 characters\)
  sed "s/^X//" >'uuidsys.c' <<'END_OF_FILE'
X/*
X * 
X * (c) Copyright 1991 OPEN SOFTWARE FOUNDATION, INC.
X * (c) Copyright 1991 HEWLETT-PACKARD COMPANY
X * (c) Copyright 1991 DIGITAL EQUIPMENT CORPORATION
X * To anyone who acknowledges that this file is provided "AS IS"
X * without any express or implied warranty:
X *                 permission to use, copy, modify, and distribute this
X * file for any purpose is hereby granted without fee, provided that
X * the above copyright notices and this notice appears in all source
X * code copies, and that none of the names of Open Software
X * Foundation, Inc., Hewlett-Packard Company, or Digital Equipment
X * Corporation be used in advertising or publicity pertaining to
X * distribution of the software without specific, written prior
X * permission.  Neither Open Software Foundation, Inc., Hewlett-
X * Packard Company, nor Digital Equipment Corporation makes any
X * representations about the suitability of this software for any
X * purpose.
X * 
X */
X/*
X */
X/*
X**  NAME:
X**
X**      uuidsys.c
X**
X**  FACILITY:
X**
X**      UUID
X**
X**  ABSTRACT:
X**
X**      UUID - Unix dependant (therefore system dependant) routines
X**
X**
X*/
X
X#include <dce/dce.h>
X#include <commonp.h>            /* common definitions                   */
X#include <dce/uuid.h>           /* uuid idl definitions (public)        */
X#include <uuidp.h>              /* uuid idl definitions (private)       */
X#include <dce/dce_utils.h>      /* defines  dce_get_802_addr()          */
X
X/*
X *  Define constant designation difference in Unix and DTSS base times:
X *  DTSS UTC base time is October 15, 1582.
X *  Unix base time is January 1, 1970.
X */
X#define uuid_c_os_base_time_diff_lo     0x13814000
X#define uuid_c_os_base_time_diff_hi     0x01B21DD2
X
X/*
X * U U I D _ _ G E T _ O S _ T I M E
X *
X * Get OS time - contains platform-specific code.
X */
XPRIVATE void uuid__get_os_time (uuid_time_t * uuid_time)
X{
X
X    struct timeval      tp;
X    unsigned64_t        utc,
X                        usecs,
X                        os_basetime_diff;
X
X    /*
X     * Get current time
X     */
X    if (gettimeofday (&tp, (struct timezone *) 0))
X    {
X        perror ("uuid__get_os_time");
X        exit (-1);
X    }
X
X    /*
X     * Multiply the number of seconds by the number clunks 
X     */
X    uuid__uemul ((long) tp.tv_sec, UUID_C_100NS_PER_SEC, &utc);
X
X    /*
X     * Multiply the number of microseconds by the number clunks 
X     * and add to the seconds
X     */
X    uuid__uemul ((long) tp.tv_usec, UUID_C_100NS_PER_USEC, &usecs);
X    UADD_UVLW_2_UVLW (&usecs, &utc, &utc);
X
X    /*
X     * Offset between DTSS formatted times and Unix formatted times.
X     */
X    os_basetime_diff.lo = uuid_c_os_base_time_diff_lo;
X    os_basetime_diff.hi = uuid_c_os_base_time_diff_hi;
X    UADD_UVLW_2_UVLW (&utc, &os_basetime_diff, uuid_time);
X
X}
X
X
X/* 
X * U U I D _ _ G E T _ O S _ P I D
X *
X * Get the process id
X */
XPRIVATE unsigned32 uuid__get_os_pid ( void )
X{
X    return ((unsigned32) getpid());
X}
X
X
X/*
X * U U I D _ _ G E T _ O S _ A D D R E S S
X *
X * Wrapper for dce_get_802_addr()
X * 
X * Kruntime has kernel specific version of this.
X */
XPRIVATE void uuid__get_os_address
X(
X    uuid_address_p_t        addr,
X    unsigned32              *status
X)
X{
X    /*
X     * Cheat and cast the uuid_address_p_t to dce_802_addr_t
X     * since they are the same
X     */
X
X    dce_get_802_addr((dce_802_addr_t *)addr, status);
X
X    if (*status == error_status_ok)
X	*status = uuid_s_ok;
X}
END_OF_FILE
  if test 3403 -ne `wc -c <'uuidsys.c'`; then
    echo shar: \"'uuidsys.c'\" unpacked with wrong size!
  fi
  # end of 'uuidsys.c'
fi
echo shar: End of archive.
exit 0



      reply	other threads:[~1995-06-21  4:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-06-19  4:00 What's each field of UUID assigned? Wu Wang
1995-06-21  4:00 ` Rich Salz [this message]
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox