This repository has been archived by the owner on Nov 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
/
uuid.c
1719 lines (1544 loc) · 43.6 KB
/
uuid.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* (c) Copyright 1989 OPEN SOFTWARE FOUNDATION, INC.
* (c) Copyright 1989 HEWLETT-PACKARD COMPANY
* (c) Copyright 1989 DIGITAL EQUIPMENT CORPORATION
* To anyone who acknowledges that this file is provided "AS IS"
* without any express or implied warranty:
* permission to use, copy, modify, and distribute this
* file for any purpose is hereby granted without fee, provided that
* the above copyright notices and this notice appears in all source
* code copies, and that none of the names of Open Software
* Foundation, Inc., Hewlett-Packard Company, or Digital Equipment
* Corporation be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior
* permission. Neither Open Software Foundation, Inc., Hewlett-
* Packard Company, nor Digital Equipment Corporation makes any
* representations about the suitability of this software for any
* purpose.
*
*/
/*
*/
/*
**
** NAME:
**
** uuid.c
**
** FACILITY:
**
** UUID
**
** ABSTRACT:
**
** UUID - routines that manipulate uuid's
**
**
*/
#ifndef UUID_BUILD_STANDALONE
#include <dce/dce.h>
#include <dce/uuid.h> /* uuid idl definitions (public) */
#include <dce/rpcsts.h>
#else
#include "uuid.h"
#endif
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "uuid_i.h" /* uuid idl definitions (private) */
/*
* structure of universal unique IDs (UUIDs).
*
* There are three "variants" of UUIDs that this code knows about. The
* variant #0 is what was defined in the 1989 HP/Apollo Network Computing
* Architecture (NCA) specification and implemented in NCS 1.x and DECrpc
* v1. Variant #1 is what was defined for the joint HP/DEC specification
* for the OSF (in DEC's "UID Architecture Functional Specification Version
* X1.0.4") and implemented in NCS 2.0, DECrpc v2, and OSF 1.0 DCE RPC.
* Variant #2 is defined by Microsoft.
*
* This code creates only variant #1 UUIDs.
*
* The three UUID variants can exist on the same wire because they have
* distinct values in the 3 MSB bits of octet 8 (see table below). Do
* NOT confuse the version number with these 3 bits. (Note the distinct
* use of the terms "version" and "variant".) Variant #0 had no version
* field in it. Changes to variant #1 (should any ever need to be made)
* can be accomodated using the current form's 4 bit version field.
*
* The UUID record structure MUST NOT contain padding between fields.
* The total size = 128 bits.
*
* To minimize confusion about bit assignment within octets, the UUID
* record definition is defined only in terms of fields that are integral
* numbers of octets.
*
* Depending on the network data representation, the multi-octet unsigned
* integer fields are subject to byte swapping when communicated between
* dissimilar endian machines. Note that all three UUID variants have
* the same record structure; this allows this byte swapping to occur.
* (The ways in which the contents of the fields are generated can and
* do vary.)
*
* The following information applies to variant #1 UUIDs:
*
* The lowest addressed octet contains the global/local bit and the
* unicast/multicast bit, and is the first octet of the address transmitted
* on an 802.3 LAN.
*
* The adjusted time stamp is split into three fields, and the clockSeq
* is split into two fields.
*
* |<------------------------- 32 bits -------------------------->|
*
* +--------------------------------------------------------------+
* | low 32 bits of time | 0-3 .time_low
* +-------------------------------+-------------------------------
* | mid 16 bits of time | 4-5 .time_mid
* +-------+-----------------------+
* | vers. | hi 12 bits of time | 6-7 .time_hi_and_version
* +-------+-------+---------------+
* |Res| clkSeqHi | 8 .clock_seq_hi_and_reserved
* +---------------+
* | clkSeqLow | 9 .clock_seq_low
* +---------------+----------...-----+
* | node ID | 8-16 .node
* +--------------------------...-----+
*
* --------------------------------------------------------------------------
*
* The structure layout of all three UUID variants is fixed for all time.
* I.e., the layout consists of a 32 bit int, 2 16 bit ints, and 8 8
* bit ints. The current form version field does NOT determine/affect
* the layout. This enables us to do certain operations safely on the
* variants of UUIDs without regard to variant; this increases the utility
* of this code even as the version number changes (i.e., this code does
* NOT need to check the version field).
*
* The "Res" field in the octet #8 is the so-called "reserved" bit-field
* and determines whether or not the uuid is a old, current or other
* UUID as follows:
*
* MS-bit 2MS-bit 3MS-bit Variant
* ---------------------------------------------
* 0 x x 0 (NCS 1.5)
* 1 0 x 1 (DCE 1.0 RPC)
* 1 1 0 2 (Microsoft)
* 1 1 1 unspecified
*
* --------------------------------------------------------------------------
*
* structure of variant #0 UUIDs
*
* The first 6 octets are the number of 4 usec units of time that have
* passed since 1/1/80 0000 GMT. The next 2 octets are reserved for
* future use. The next octet is an address family. The next 7 octets
* are a host ID in the form allowed by the specified address family.
*
* Note that while the family field (octet 8) was originally conceived
* of as being able to hold values in the range [0..255], only [0..13]
* were ever used. Thus, the 2 MSB of this field are always 0 and are
* used to distinguish old and current UUID forms.
*
* +--------------------------------------------------------------+
* | high 32 bits of time | 0-3 .time_high
* +-------------------------------+-------------------------------
* | low 16 bits of time | 4-5 .time_low
* +-------+-----------------------+
* | reserved | 6-7 .reserved
* +---------------+---------------+
* | family | 8 .family
* +---------------+----------...-----+
* | node ID | 9-16 .node
* +--------------------------...-----+
*
*/
/***************************************************************************
*
* Local definitions
*
**************************************************************************/
#ifdef UUID_DEBUG
#define DEBUG_PRINT(msg, st) RPC_DBG_GPRINTF (( "%s: %08x\n", msg, st ))
#else
#define DEBUG_PRINT(msg, st) do {;} while(0)
#endif
#ifndef NO_SSCANF
# define UUID_SSCANF sscanf
# define UUID_OLD_SSCANF sscanf
#else
# define UUID_SSCANF uuid__sscanf
# define UUID_OLD_SSCANF uuid__old_sscanf
#endif
#ifndef NO_SPRINTF
# define UUID_SPRINTF sprintf
# define UUID_OLD_SPRINTF sprintf
#else
# define UUID_SPRINTF uuid__sprintf
# define UUID_OLD_SPRINTF uuid__old_sprintf
#endif
/*
* the number of elements returned by sscanf() when converting
* string formatted uuid's to binary
*/
#define UUID_ELEMENTS_NUM 11
#define UUID_ELEMENTS_NUM_OLD 10
/*
* local defines used in uuid bit-diddling
*/
#define HI_WORD(w) ((w) >> 16)
#define RAND_MASK 0x3fff /* same as CLOCK_SEQ_LAST */
#define TIME_MID_MASK 0x0000ffff
#define TIME_HIGH_MASK 0x0fff0000
#define TIME_HIGH_SHIFT_COUNT 16
#define MAX_TIME_ADJUST 0x7fff
#define CLOCK_SEQ_LOW_MASK 0xff
#define CLOCK_SEQ_HIGH_MASK 0x3f00
#define CLOCK_SEQ_HIGH_SHIFT_COUNT 8
#define CLOCK_SEQ_FIRST 1
#define CLOCK_SEQ_LAST 0x3fff /* same as RAND_MASK */
/*
* Note: If CLOCK_SEQ_BIT_BANG == TRUE, then we can avoid the modulo
* operation. This should save us a divide instruction and speed
* things up.
*/
#ifndef CLOCK_SEQ_BIT_BANG
#define CLOCK_SEQ_BIT_BANG 1
#endif
#if CLOCK_SEQ_BIT_BANG
#define CLOCK_SEQ_BUMP(seq) ((*seq) = ((*seq) + 1) & CLOCK_SEQ_LAST)
#else
#define CLOCK_SEQ_BUMP(seq) ((*seq) = ((*seq) + 1) % (CLOCK_SEQ_LAST+1))
#endif
#define UUID_VERSION_BITS (uuid_c_version << 12)
#define UUID_RESERVED_BITS 0x80
#define IS_OLD_UUID(uuid) (((uuid)->clock_seq_hi_and_reserved & 0xc0) != 0x80)
/****************************************************************************
*
* data declarations
*
****************************************************************************/
dce_uuid_t uuid_g_nil_uuid = { 0, 0, 0, 0, 0, {0} };
dce_uuid_t uuid_nil = { 0, 0, 0, 0, 0, {0} };
/****************************************************************************
*
* local data declarations
*
****************************************************************************/
/*
* saved copy of our IEEE 802 address for quick reference
*/
static dce_uuid_address_t saved_addr;
/*
* saved copy of the status associated with saved_addr
*/
static unsigned32 saved_st;
/*
* declarations used in UTC time calculations
*/
static dce_uuid_time_t time_now; /* utc time as of last query */
static dce_uuid_time_t time_last; /* 'saved' value of time_now */
static unsigned16 time_adjust; /* 'adjustment' to ensure uniqness */
static unsigned16 clock_seq; /* 'adjustment' for backwards clocks*/
/*
* true_random variables
*/
static unsigned32 rand_m; /* multiplier */
static unsigned32 rand_ia; /* adder #1 */
static unsigned32 rand_ib; /* adder #2 */
static unsigned32 rand_irand; /* random value */
typedef enum
{
uuid_e_less_than, uuid_e_equal_to, uuid_e_greater_than
} uuid_compval_t;
/*
* boolean indicating we've already determined our IEEE 802 address
*/
static boolean got_address = FALSE;
/****************************************************************************
*
* local function declarations
*
****************************************************************************/
/*
* I N I T
*
* Startup initialization routine for UUID module.
*/
static void init ( unsigned32 * /*st*/ );
/*
* T R U E _ R A N D O M _ I N I T
*/
static void true_random_init (void);
/*
* T R U E _ R A N D O M
*/
static unsigned16 true_random (void);
/*
* N E W _ C L O C K _ S E Q
*
* Ensure clock_seq is up-to-date
*
* Note: clock_seq is architected to be 14-bits (unsigned) but
* I've put it in here as 16-bits since there isn't a
* 14-bit unsigned integer type (yet)
*/
static void new_clock_seq ( unsigned16 * /*clock_seq*/);
/*
* S T R U C T U R E _ I S _ K N O W N
*
* Does the UUID have the known standard structure layout?
*/
boolean structure_is_known ( dce_uuid_p_t /*uuid*/);
/*
* T I M E _ C M P
*
* Compares two UUID times (64-bit DEC UID UTC values)
*/
static uuid_compval_t time_cmp (
dce_uuid_time_p_t /*time1*/,
dce_uuid_time_p_t /*time2*/
);
/*
* U U I D _ G E T _ A D D R E S S
*
* Get our IEEE 802 address (calls uuid__get_os_address)
*/
void uuid_get_address (
dce_uuid_address_t * /*address*/,
unsigned32 * /*st*/
);
/*****************************************************************************
*
* Macro definitions
*
****************************************************************************/
/*
* ensure we've been initialized
*/
static boolean uuid_init_done = FALSE;
#define EmptyArg
#define UUID_VERIFY_INIT(Arg) \
if (! uuid_init_done) \
{ \
init (status); \
if (*status != uuid_s_ok) \
{ \
return Arg; \
} \
}
/*
* Check the reserved bits to make sure the UUID is of the known structure.
*/
#define CHECK_STRUCTURE(uuid) \
( \
(((uuid)->clock_seq_hi_and_reserved & 0x80) == 0x00) || /* var #0 */ \
(((uuid)->clock_seq_hi_and_reserved & 0xc0) == 0x80) || /* var #1 */ \
(((uuid)->clock_seq_hi_and_reserved & 0xe0) == 0xc0) || /* var #2 */ \
(((uuid)->clock_seq_hi_and_reserved & 0xe0) == 0xe0) /* var #DAMNMICROSOFT */ \
)
/*
* The following macros invoke CHECK_STRUCTURE(), check that the return
* value is okay and if not, they set the status variable appropriately
* and return either a boolean FALSE, nothing (for void procedures),
* or a value passed to the macro. This has been done so that checking
* can be done more simply and values are returned where appropriate
* to keep compilers happy.
*
* bCHECK_STRUCTURE - returns boolean FALSE
* vCHECK_STRUCTURE - returns nothing (void)
* rCHECK_STRUCTURE - returns 'r' macro parameter
*/
#define bCHECK_STRUCTURE(uuid, status) \
{ \
if (!CHECK_STRUCTURE (uuid)) \
{ \
*(status) = uuid_s_bad_version; \
return (FALSE); \
} \
}
#define vCHECK_STRUCTURE(uuid, status) \
{ \
if (!CHECK_STRUCTURE (uuid)) \
{ \
*(status) = uuid_s_bad_version; \
return; \
} \
}
#define rCHECK_STRUCTURE(uuid, status, result) \
{ \
if (!CHECK_STRUCTURE (uuid)) \
{ \
*(status) = uuid_s_bad_version; \
return (result); \
} \
}
/*
**++
**
** ROUTINE NAME: init
**
** SCOPE: - declared locally
**
** DESCRIPTION:
**
** Startup initialization routine for the UUID module.
**
** INPUTS: none
**
** INPUTS/OUTPUTS: none
**
** OUTPUTS:
**
** status return status value
**
** uuid_s_ok
** uuid_s_coding_error
**
** IMPLICIT INPUTS: none
**
** IMPLICIT OUTPUTS: none
**
** FUNCTION VALUE: void
**
** SIDE EFFECTS: sets uuid_init_done so this won't be done again
**
**--
**/
static void init
(
unsigned32 *status
)
{
#ifdef CMA_INCLUDE
/*
* Hack for CMA pthreads. Some users will call uuid_ stuff before
* doing any thread stuff (CMA is uninitialized). Some uuid_
* operations alloc memory and in a CMA pthread environment,
* the malloc wrapper uses a mutex but doesn't do self initialization
* (which results in segfault'ing inside of CMA). Make sure that
* CMA is initialized.
*/
pthread_t t;
t = pthread_self();
#endif /* CMA_INCLUDE */
CODING_ERROR (status);
/*
* init the random number generator
*/
true_random_init();
uuid__get_os_time (&time_last);
#ifdef UUID_NONVOLATILE_CLOCK
clock_seq = uuid__read_clock();
#else
clock_seq = true_random();
#endif
uuid_init_done = TRUE;
*status = uuid_s_ok;
}
/*
**++
**
** ROUTINE NAME: uuid_create
**
** SCOPE: - declared in UUID.IDL
**
** DESCRIPTION:
**
** Create a new UUID. Note: we only know how to create the new
** and improved UUIDs.
**
** INPUTS: none
**
** INPUTS/OUTPUTS: none
**
** OUTPUTS:
**
** uuid A new UUID value
**
** status return status value
**
** uuid_s_ok
** uuid_s_coding_error
**
** IMPLICIT INPUTS: none
**
** IMPLICIT OUTPUTS: none
**
** FUNCTION VALUE: void
**
** SIDE EFFECTS: none
**
**--
**/
void dce_uuid_create
(
dce_uuid_t *uuid,
unsigned32 *status
)
{
dce_uuid_address_t eaddr; /* our IEEE 802 hardware address */
boolean32 got_no_time = FALSE;
CODING_ERROR (status);
UUID_VERIFY_INIT (EmptyArg);
/*
* get our hardware network address
*/
uuid_get_address (&eaddr, status);
if (*status != uuid_s_ok)
{
DEBUG_PRINT("dce_uuid_create:uuid_get_address", *status);
return;
}
do
{
/*
* get the current time
*/
uuid__get_os_time (&time_now);
/*
* do stuff like:
*
* o check that our clock hasn't gone backwards and handle it
* accordingly with clock_seq
* o check that we're not generating uuid's faster than we
* can accommodate with our time_adjust fudge factor
*/
switch (time_cmp (&time_now, &time_last))
{
case uuid_e_less_than:
new_clock_seq (&clock_seq);
time_adjust = 0;
break;
case uuid_e_greater_than:
time_adjust = 0;
break;
case uuid_e_equal_to:
if (time_adjust == MAX_TIME_ADJUST)
{
/*
* spin your wheels while we wait for the clock to tick
*/
got_no_time = TRUE;
}
else
{
time_adjust++;
}
break;
default:
*status = uuid_s_internal_error;
DEBUG_PRINT ("dce_uuid_create", *status);
return;
}
} while (got_no_time);
time_last.lo = time_now.lo;
time_last.hi = time_now.hi;
if (time_adjust != 0)
{
UADD_UW_2_UVLW (&time_adjust, &time_now, &time_now);
}
/*
* now construct a uuid with the information we've gathered
* plus a few constants
*/
uuid->time_low = time_now.lo;
uuid->time_mid = time_now.hi & TIME_MID_MASK;
uuid->time_hi_and_version =
(time_now.hi & TIME_HIGH_MASK) >> TIME_HIGH_SHIFT_COUNT;
uuid->time_hi_and_version |= UUID_VERSION_BITS;
uuid->clock_seq_low = clock_seq & CLOCK_SEQ_LOW_MASK;
uuid->clock_seq_hi_and_reserved =
(clock_seq & CLOCK_SEQ_HIGH_MASK) >> CLOCK_SEQ_HIGH_SHIFT_COUNT;
uuid->clock_seq_hi_and_reserved |= UUID_RESERVED_BITS;
memcpy (uuid->node, &eaddr, sizeof (dce_uuid_address_t));
*status = uuid_s_ok;
}
/*
**++
**
** ROUTINE NAME: dce_uuid_create_nil
**
** SCOPE: - declared in UUID.IDL
**
** DESCRIPTION:
**
** Create a 'nil' uuid.
**
** INPUTS: none
**
** INPUTS/OUTPUTS: none
**
** OUTPUTS:
**
** uuid A nil UUID
**
** status return status value
**
** uuid_s_ok
** uuid_s_coding_error
**
** IMPLICIT INPUTS: none
**
** IMPLICIT OUTPUTS: none
**
** FUNCTION VALUE: void
**
** SIDE EFFECTS: none
**
**--
**/
void dce_uuid_create_nil
(
dce_uuid_t *uuid,
unsigned32 *status
)
{
CODING_ERROR (status);
UUID_VERIFY_INIT (EmptyArg);
memset (uuid, 0, sizeof (dce_uuid_t));
*status = uuid_s_ok;
}
/*
**++
**
** ROUTINE NAME: dce_uuid_to_string
**
** SCOPE: - declared in UUID.IDL
**
** DESCRIPTION:
**
** Encode a UUID into a printable string.
**
** INPUTS:
**
** uuid A binary UUID to be converted to a string UUID.
**
** INPUTS/OUTPUTS: none
**
** OUTPUTS:
**
** uuid_string The string representation of the given UUID.
**
** status return status value
**
** uuid_s_ok
** uuid_s_bad_version
** uuid_s_coding_error
**
** IMPLICIT INPUTS: none
**
** IMPLICIT OUTPUTS: none
**
** FUNCTION VALUE: void
**
** SIDE EFFECTS: none
**
**--
**/
void dce_uuid_to_string
(
dce_uuid_p_t uuid,
unsigned_char_p_t *uuid_string,
unsigned32 *status
)
{
CODING_ERROR (status);
UUID_VERIFY_INIT (EmptyArg);
/*
* don't do anything if the output argument is NULL
*/
if (uuid_string == NULL)
{
*status = uuid_s_ok;
return;
}
vCHECK_STRUCTURE (uuid, status);
#ifdef DCE_BUILD_INTERNAL
RPC_MEM_ALLOC (
*uuid_string,
unsigned_char_p_t,
UUID_C_UUID_STRING_MAX,
RPC_C_MEM_STRING,
RPC_C_MEM_WAITOK);
#else
/* Use the standard C allocator */
*uuid_string = (unsigned_char_p_t)malloc(UUID_C_UUID_STRING_MAX);
#endif
if (*uuid_string == NULL)
{
*status = uuid_s_no_memory;
return;
}
UUID_SPRINTF(
(char *) *uuid_string,
"%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
(unsigned long) uuid->time_low, uuid->time_mid, uuid->time_hi_and_version,
uuid->clock_seq_hi_and_reserved, uuid->clock_seq_low,
(unsigned8) uuid->node[0], (unsigned8) uuid->node[1],
(unsigned8) uuid->node[2], (unsigned8) uuid->node[3],
(unsigned8) uuid->node[4], (unsigned8) uuid->node[5]);
*status = uuid_s_ok;
}
/*
**++
**
** ROUTINE NAME: dce_uuid_from_string
**
** SCOPE: - declared in UUID.IDL
**
** DESCRIPTION:
**
** Decode a UUID from a printable string.
**
** INPUTS:
**
** uuid_string The string UUID to be converted to a binary UUID
**
** INPUTS/OUTPUTS: none
**
** OUTPUTS:
**
** uuid The binary representation of the given UUID
**
** status return status value
**
** uuid_s_ok
** uuid_s_bad_version
** uuid_s_invalid_string_uuid
** uuid_s_coding_error
**
** IMPLICIT INPUTS: none
**
** IMPLICIT OUTPUTS: none
**
** FUNCTION VALUE: void
**
** SIDE EFFECTS: none
**
**--
**/
void dce_uuid_from_string
(
unsigned_char_p_t uuid_string,
dce_uuid_t *uuid,
unsigned32 *status
)
{
dce_uuid_t uuid_new; /* used for sscanf for new uuid's */
uuid_old_t uuid_old; /* used for sscanf for old uuid's */
dce_uuid_p_t uuid_ptr; /* pointer to correct uuid (old/new) */
int i;
CODING_ERROR (status);
UUID_VERIFY_INIT(EmptyArg);
/*
* If a NULL pointer or empty string, give the nil UUID.
*/
if (uuid_string == NULL || *uuid_string == '\0')
{
memcpy (uuid, &uuid_g_nil_uuid, sizeof *uuid);
*status = uuid_s_ok;
return;
}
/*
* check to see that the string length is right at least
*/
if (strlen ((char *) uuid_string) != UUID_C_UUID_STRING_MAX - 1)
{
*status = uuid_s_invalid_string_uuid;
return;
}
/*
* check for a new uuid
*/
if (uuid_string[8] == '-')
{
long time_low;
int time_mid;
int time_hi_and_version;
int clock_seq_hi_and_reserved;
int clock_seq_low;
int node[6];
i = UUID_SSCANF(
(char *) uuid_string, "%8lx-%4x-%4x-%2x%2x-%2x%2x%2x%2x%2x%2x",
&time_low,
&time_mid,
&time_hi_and_version,
&clock_seq_hi_and_reserved,
&clock_seq_low,
&node[0], &node[1], &node[2], &node[3], &node[4], &node[5]);
/*
* check that sscanf worked
*/
if (i != UUID_ELEMENTS_NUM)
{
*status = uuid_s_invalid_string_uuid;
return;
}
/*
* note that we're going through this agony because scanf is defined to
* know only to scan into "int"s or "long"s.
*/
uuid_new.time_low = time_low;
uuid_new.time_mid = time_mid;
uuid_new.time_hi_and_version = time_hi_and_version;
uuid_new.clock_seq_hi_and_reserved = clock_seq_hi_and_reserved;
uuid_new.clock_seq_low = clock_seq_low;
uuid_new.node[0] = node[0];
uuid_new.node[1] = node[1];
uuid_new.node[2] = node[2];
uuid_new.node[3] = node[3];
uuid_new.node[4] = node[4];
uuid_new.node[5] = node[5];
/*
* point to the correct uuid
*/
uuid_ptr = &uuid_new;
}
else
{
long time_high;
int time_low;
int family;
int host[7];
/*
* format = tttttttttttt.ff.h1.h2.h3.h4.h5.h6.h7
*/
i = UUID_OLD_SSCANF(
(char *) uuid_string, "%8lx%4x.%2x.%2x.%2x.%2x.%2x.%2x.%2x.%2x",
&time_high, &time_low, &family,
&host[0], &host[1], &host[2], &host[3],
&host[4], &host[5], &host[6]);
/*
* check that sscanf worked
*/
if (i != UUID_ELEMENTS_NUM_OLD)
{
*status = uuid_s_invalid_string_uuid;
return;
}
/*
* note that we're going through this agony because scanf is defined to
* know only to scan into "int"s or "long"s.
*/
uuid_old.time_high = time_high;
uuid_old.time_low = time_low;
uuid_old.family = family;
uuid_old.host[0] = host[0];
uuid_old.host[1] = host[1];
uuid_old.host[2] = host[2];
uuid_old.host[3] = host[3];
uuid_old.host[4] = host[4];
uuid_old.host[5] = host[5];
uuid_old.host[6] = host[6];
/*
* fix up non-string field, and point to the correct uuid
*/
uuid_old.reserved = 0;
uuid_ptr = (dce_uuid_p_t) (&uuid_old);
}
/*
* sanity check, is version ok?
*/
vCHECK_STRUCTURE (uuid_ptr, status);
/*
* copy the uuid to user
*/
memcpy (uuid, uuid_ptr, sizeof (dce_uuid_t));
*status = uuid_s_ok;
}
/*
**++
**
** ROUTINE NAME: dce_uuid_equal
**
** SCOPE: - declared in UUID.IDL
**
** DESCRIPTION:
**
** Compare two UUIDs.
**
** INPUTS:
**
** uuid1 The first UUID to compare
**
** uuid2 The second UUID to compare
**
** INPUTS/OUTPUTS: none
**
** OUTPUTS:
**
** status return status value
**
** uuid_s_ok
** uuid_s_bad_version
** uuid_s_coding_error
**
** IMPLICIT INPUTS: none
**
** IMPLICIT OUTPUTS: none
**
** FUNCTION VALUE:
**
** result true if UUID's are equal
** false if UUID's are not equal
**
** SIDE EFFECTS: none
**