BRL-CAD
uuid.h
Go to the documentation of this file.
1 /* U U I D . H
2  * BRL-CAD
3  *
4  * Copyright (c) 2016-2024 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #ifndef BU_UUID_H
22 #define BU_UUID_H
23 
24 #include "common.h"
25 
26 #include <stdint.h> /* for uint8_t */
27 #include <stdlib.h> /* for size_t */
28 
29 #include "bu/defines.h"
30 
31 /*----------------------------------------------------------------------*/
32 /** @addtogroup bu_uuid
33  *
34  * @brief
35  * Routines to generate and work with universally unique identifiers.
36  */
37 /** @{ */
38 /** @file bu/uuid.h */
39 
40 
41 #ifdef HAVE_STATIC_ARRAYS
42 # define STATIC_ARRAY(x) static (x)
43 #else
44 # define STATIC_ARRAY(x) (x)
45 #endif
46 
47 
48 __BEGIN_DECLS
49 
50 /**
51  * Create a UUID (a 128-bit identifier) that conforms with RFC4122,
52  * version 4 or 5, in big endian network order. Version 4 UUIDs are
53  * suitable for a random object identifier (with 122 bits of random
54  * entropy). Providing a byte array and namespace will create a (SHA1-based)
55  * version 5 UUID suitable for repeatable identifier hashing.
56  */
57 BU_EXPORT int
58 bu_uuid_create(uint8_t uuid[STATIC_ARRAY(16)], size_t nbytes, const uint8_t *bytes, const uint8_t namespace_uuid[STATIC_ARRAY(16)]);
59 
60 /**
61  * This is a convenience UUID comparison routine compatible with
62  * stdlib sorting functions (e.g., bu_sort()). The function expects
63  * both left and right UUIDs to be uint8_t[16] arrays.
64  *
65  * Returns:
66  *
67  * <0 if a < b
68  * 0 if a == b
69  * >0 if a > b
70  */
71 BU_EXPORT int
72 bu_uuid_compare(const void *uuid_left, const void *uuid_right);
73 
74 /**
75  * Converts a UUID into a string representation of the following
76  * style: "00112233-4455-6677-8899-AABBCCDDEEFF"
77  *
78  * The caller must provide a 36-byte + 1-byte (for nul) array to write
79  * the result and will need to manually convert this into a string or
80  * add braces as desired by the calling application. For example:
81  *
82  * @code
83  * uint8_t uuid[16] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
84  * 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
85  * char uuidstr[39] = {0};
86  * (void)bu_uuid_encode(uuid, (uint8_t *)(uuidstr+1));
87  * uuidstr[0] = '{';
88  * uuidstr[sizeof(uuidstr)-2] = '}';
89  * uuidstr[sizeof(uuidstr)-1] = '\0';
90  * printf("UUID is %s\n", uuidstr);
91  * @endcode
92  */
93 BU_EXPORT int
94 bu_uuid_encode(const uint8_t uuid[STATIC_ARRAY(16)], uint8_t cp[STATIC_ARRAY(37)]);
95 
96 /**
97  * Converts a string (e.g., "{12B01234-f543-39d9-BFE0-0098765432F1}")
98  * into a UUID. The input string must be nul-terminated. Brackets
99  * are optional and are ignored. Hyphens can appear anywhere or be
100  * missing. The hex digits can be any mixture of upper or lower case.
101  */
102 BU_EXPORT int
103 bu_uuid_decode(const char *cp, uint8_t uuid[STATIC_ARRAY(16)]);
104 
105 
106 __END_DECLS
107 
108 /** @} */
109 
110 #endif /* BU_UUID_H */
111 
112 /*
113  * Local Variables:
114  * mode: C
115  * tab-width: 8
116  * indent-tabs-mode: t
117  * c-file-style: "stroustrup"
118  * End:
119  * ex: shiftwidth=4 tabstop=8
120  */
Header file for the BRL-CAD common definitions.
int bu_uuid_encode(const uint8_t uuid[STATIC_ARRAY(16)], uint8_t cp[STATIC_ARRAY(37)])
int bu_uuid_create(uint8_t uuid[STATIC_ARRAY(16)], size_t nbytes, const uint8_t *bytes, const uint8_t namespace_uuid[STATIC_ARRAY(16)])
int bu_uuid_decode(const char *cp, uint8_t uuid[STATIC_ARRAY(16)])
int bu_uuid_compare(const void *uuid_left, const void *uuid_right)
#define STATIC_ARRAY(x)
Definition: uuid.h:44