blob: 52af1e70e99acb58947a1efceec6ceb63c54e86e [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
Oliver Swede35d824e2019-10-01 13:50:36 +01002 * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
8#include <stdio.h>
9#include <string.h>
10#include <uuid_utils.h>
11
12/* Format string to print a UUID */
Oliver Swede35d824e2019-10-01 13:50:36 +010013static const char *uuid_str_fmt = "{ 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, "
14 "0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, "
15 "0x%02x, 0x%02x }";
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020016
17unsigned int is_uuid_null(const uuid_t *uuid)
18{
19 const uuid_t uuid_null = {0};
20
21 return memcmp(uuid, &uuid_null, sizeof(uuid_t)) == 0;
22}
23
24char *uuid_to_str(const uuid_t *uuid, char *str)
25{
26 assert(uuid != NULL);
27 assert(str != NULL);
28
29 snprintf(str, UUID_STR_SIZE, uuid_str_fmt,
Oliver Swede35d824e2019-10-01 13:50:36 +010030 uuid->time_low[0], uuid->time_low[1],
31 uuid->time_low[2], uuid->time_low[3],
32 uuid->time_mid[0], uuid->time_mid[1],
33 uuid->time_hi_and_version[0], uuid->time_hi_and_version[1],
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020034 uuid->clock_seq_hi_and_reserved, uuid->clock_seq_low,
35 uuid->node[0], uuid->node[1], uuid->node[2], uuid->node[3],
36 uuid->node[4], uuid->node[5]);
37
38 return str;
39}
40
41unsigned int uuid_equal(const uuid_t *uuid1, const uuid_t *uuid2)
42{
43 return memcmp(uuid1, uuid2, sizeof(uuid_t)) == 0;
44}
45
46uuid_t *make_uuid_from_4words(uuid_t *uuid,
47 uint32_t w0,
48 uint32_t w1,
49 uint32_t w2,
50 uint32_t w3)
51{
52 uint32_t *uuid32;
53
54 assert(uuid != NULL);
55
56 uuid32 = (uint32_t *) uuid;
57 uuid32[0] = w0;
58 uuid32[1] = w1;
59 uuid32[2] = w2;
60 uuid32[3] = w3;
61
62 return uuid;
63}