Loading...
Searching...
No Matches
uuid.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2018 Freie Universität Berlin
3 * SPDX-FileCopyrightText: 2018 Inria
4 * SPDX-License-Identifier: LGPL-2.1-only
5 */
6
7#pragma once
8
24
25#include <stdbool.h>
26#include <stdint.h>
27#include <stdlib.h>
28#include <string.h>
29#include "byteorder.h"
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35#define UUID_NODE_LEN (6U)
36
37#define UUID_STR_LEN (36U)
38
43#define UUID_V1 (0x01)
44#define UUID_V2 (0x02)
45#define UUID_V3 (0x03)
46#define UUID_V4 (0x04)
47#define UUID_V5 (0x05)
49
53#define UUID_VERSION_MASK (0xF000)
54
70
79extern const uuid_t uuid_namespace_dns;
80extern const uuid_t uuid_namespace_url;
81extern const uuid_t uuid_namespace_iso;
82extern const uuid_t uuid_namespace_x500;
84
93void uuid_v3(uuid_t *uuid, const uuid_t *ns, const uint8_t *name, size_t len);
94
100void uuid_v4(uuid_t *uuid);
101
110void uuid_v5(uuid_t *uuid, const uuid_t *ns, const uint8_t *name, size_t len);
111
119static inline unsigned uuid_version(const uuid_t *uuid)
120{
121 uint16_t time_hi_vers = byteorder_ntohs(uuid->time_hi);
122
123 return (time_hi_vers & 0xF000) >> 12;
124}
125
134static inline bool uuid_equal(const uuid_t *uuid1, const uuid_t *uuid2)
135{
136 return (memcmp(uuid1, uuid2, sizeof(uuid_t)) == 0);
137}
138
145void uuid_to_string(const uuid_t *uuid, char *str);
146
155int uuid_from_string(uuid_t *uuid, const char *str);
156
157#ifdef __cplusplus
158}
159#endif
Functions to work with different byte orders.
be_uint32_t network_uint32_t
A 32 bit integer in network byte order.
Definition byteorder.h:109
be_uint16_t network_uint16_t
A 16 bit integer in network byte order.
Definition byteorder.h:104
static uint16_t byteorder_ntohs(network_uint16_t v)
Convert from network byte order to host byte order, 16 bit.
Definition byteorder.h:502
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.
#define UUID_NODE_LEN
Size of the node identifier in bytes.
Definition uuid.h:35
const uuid_t uuid_namespace_url
URL namespace UUID.
static unsigned uuid_version(const uuid_t *uuid)
Retrieve the type number of a UUID.
Definition uuid.h:119
void uuid_to_string(const uuid_t *uuid, char *str)
Generate an UUID string from an UUID structure.
const uuid_t uuid_namespace_iso
ISO OID namespace UUID.
static bool uuid_equal(const uuid_t *uuid1, const uuid_t *uuid2)
Compare two UUID's.
Definition uuid.h:134
const uuid_t uuid_namespace_dns
DNS namespace UUID.
int uuid_from_string(uuid_t *uuid, const char *str)
Populate an UUID structure from an UUID string.
const uuid_t uuid_namespace_x500
X.500 DN namespace UUID.
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.
void uuid_v4(uuid_t *uuid)
Generate a version 4(Full random) UUID.
UUID layout.
Definition uuid.h:60
uint8_t node[UUID_NODE_LEN]
The spatially unique node identifier.
Definition uuid.h:68
network_uint16_t time_mid
The middle field of the timestamp.
Definition uuid.h:62
network_uint16_t time_hi
The high field of the timestamp multiplexed with the version number.
Definition uuid.h:63
uint8_t clk_seq_hi_res
The high field of the clock sequence Multiplexed with the variant.
Definition uuid.h:65
network_uint32_t time_low
The low field of the timestamp.
Definition uuid.h:61
uint8_t clk_seq_low
The low field of the clock sequence.
Definition uuid.h:67