uuid.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Freie Universität Berlin
3  * Copyright (C) 2018 Inria
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser
6  * General Public License v2.1. See the file LICENSE in the top level
7  * directory for more details.
8  */
9 
26 #ifndef UUID_H
27 #define UUID_H
28 
29 #include <stdbool.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include "byteorder.h"
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 #define UUID_NODE_LEN (6U)
41 #define UUID_STR_LEN (36U)
47 #define UUID_V1 (0x01)
48 #define UUID_V2 (0x02)
49 #define UUID_V3 (0x03)
50 #define UUID_V4 (0x04)
51 #define UUID_V5 (0x05)
57 #define UUID_VERSION_MASK (0xF000)
58 
64 typedef struct __attribute__((packed)) {
69  uint8_t clk_seq_hi_res;
71  uint8_t clk_seq_low;
72  uint8_t node[UUID_NODE_LEN];
73 } uuid_t;
74 
75 
84 extern const uuid_t uuid_namespace_dns;
85 extern const uuid_t uuid_namespace_url;
86 extern const uuid_t uuid_namespace_iso;
87 extern const uuid_t uuid_namespace_x500;
98 void uuid_v3(uuid_t *uuid, const uuid_t *ns, const uint8_t *name, size_t len);
99 
100 
106 void uuid_v4(uuid_t *uuid);
107 
116 void uuid_v5(uuid_t *uuid, const uuid_t *ns, const uint8_t *name, size_t len);
117 
125 static inline unsigned uuid_version(const uuid_t *uuid)
126 {
127  uint16_t time_hi_vers = byteorder_ntohs(uuid->time_hi);
128 
129  return (time_hi_vers & 0xF000) >> 12;
130 }
131 
140 static inline bool uuid_equal(const uuid_t *uuid1, const uuid_t *uuid2)
141 {
142  return (memcmp(uuid1, uuid2, sizeof(uuid_t)) == 0);
143 }
144 
151 void uuid_to_string(const uuid_t *uuid, char *str);
152 
161 int uuid_from_string(uuid_t *uuid, const char *str);
162 
163 #ifdef __cplusplus
164 }
165 #endif
166 #endif /* UUID_H */
167 
uuid_t::time_mid
network_uint16_t time_mid
The middle field of the timestamp
Definition: uuid.h:66
be_uint32_t
A 32 bit integer in big endian aka network byte order.
Definition: byteorder.h:87
byteorder.h
Functions to work with different byte orders.
be_uint16_t
A 16 bit integer in big endian aka network byte order.
Definition: byteorder.h:77
uuid_t::time_low
network_uint32_t time_low
The low field of the timestamp
Definition: uuid.h:65
uuid_t::clk_seq_hi_res
uint8_t clk_seq_hi_res
The high field of the clock sequence Multiplexed with the variant
Definition: uuid.h:69
uuid_v5
void uuid_v5(uuid_t *uuid, const uuid_t *ns, const uint8_t *name, size_t len)
Generate a version 5(sha1 based) UUID from a namespace and a byte array.
uuid_to_string
void uuid_to_string(const uuid_t *uuid, char *str)
Generate an UUID string from an UUID structure.
uuid_t::time_hi
network_uint16_t time_hi
The high field of the timestamp multiplexed with the version number.
Definition: uuid.h:67
uuid_namespace_x500
const uuid_t uuid_namespace_x500
X.500 DN namespace UUID.
uuid_v4
void uuid_v4(uuid_t *uuid)
Generate a version 4(Full random) UUID.
uuid_from_string
int uuid_from_string(uuid_t *uuid, const char *str)
Populate an UUID structure from an UUID string.
byteorder_ntohs
static uint16_t byteorder_ntohs(network_uint16_t v)
Convert from network byte order to host byte order, 16 bit.
Definition: byteorder.h:458
uuid_namespace_url
const uuid_t uuid_namespace_url
URL namespace UUID.
uuid_namespace_iso
const uuid_t uuid_namespace_iso
ISO OID namespace UUID.
uuid_version
static unsigned uuid_version(const uuid_t *uuid)
Retrieve the type number of a UUID.
Definition: uuid.h:125
uuid_equal
static bool uuid_equal(const uuid_t *uuid1, const uuid_t *uuid2)
Compare two UUID's.
Definition: uuid.h:140
uuid_v3
void uuid_v3(uuid_t *uuid, const uuid_t *ns, const uint8_t *name, size_t len)
Generate a version 3(md5 based) UUID from a namespace and a byte array.
UUID_NODE_LEN
#define UUID_NODE_LEN
Size of the node identifier in bytes.
Definition: uuid.h:39
uuid_t::clk_seq_low
uint8_t clk_seq_low
The low field of the clock sequence
Definition: uuid.h:71
uuid_namespace_dns
const uuid_t uuid_namespace_dns
DNS namespace UUID.
uuid_t
UUID layout.
Definition: uuid.h:64