]>
Commit | Line | Data |
---|---|---|
cea25275 FZ |
1 | /* |
2 | * QEMU UUID functions | |
3 | * | |
4 | * Copyright 2016 Red Hat, Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Fam Zheng <famz@redhat.com> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify it | |
10 | * under the terms of the GNU General Public License as published by the Free | |
11 | * Software Foundation; either version 2 of the License, or (at your option) | |
12 | * any later version. | |
13 | * | |
14 | */ | |
15 | ||
16 | #include "qemu/osdep.h" | |
17 | #include "qemu-common.h" | |
18 | #include "qemu/uuid.h" | |
19 | #include "qemu/bswap.h" | |
20 | ||
21 | void qemu_uuid_generate(QemuUUID *uuid) | |
22 | { | |
23 | int i; | |
24 | uint32_t tmp[4]; | |
25 | ||
26 | QEMU_BUILD_BUG_ON(sizeof(QemuUUID) != 16); | |
27 | ||
28 | for (i = 0; i < 4; ++i) { | |
29 | tmp[i] = g_random_int(); | |
30 | } | |
31 | memcpy(uuid, tmp, sizeof(tmp)); | |
32 | /* Set the two most significant bits (bits 6 and 7) of the | |
33 | clock_seq_hi_and_reserved to zero and one, respectively. */ | |
34 | uuid->data[8] = (uuid->data[8] & 0x3f) | 0x80; | |
35 | /* Set the four most significant bits (bits 12 through 15) of the | |
36 | time_hi_and_version field to the 4-bit version number. | |
37 | */ | |
38 | uuid->data[6] = (uuid->data[6] & 0xf) | 0x40; | |
39 | } | |
40 | ||
41 | int qemu_uuid_is_null(const QemuUUID *uu) | |
42 | { | |
3630be75 | 43 | static QemuUUID null_uuid; |
cea25275 FZ |
44 | return memcmp(uu, &null_uuid, sizeof(QemuUUID)) == 0; |
45 | } | |
46 | ||
47 | void qemu_uuid_unparse(const QemuUUID *uuid, char *out) | |
48 | { | |
49 | const unsigned char *uu = &uuid->data[0]; | |
50 | snprintf(out, UUID_FMT_LEN + 1, UUID_FMT, | |
51 | uu[0], uu[1], uu[2], uu[3], uu[4], uu[5], uu[6], uu[7], | |
52 | uu[8], uu[9], uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]); | |
53 | } | |
54 | ||
55 | char *qemu_uuid_unparse_strdup(const QemuUUID *uuid) | |
56 | { | |
57 | const unsigned char *uu = &uuid->data[0]; | |
58 | return g_strdup_printf(UUID_FMT, | |
59 | uu[0], uu[1], uu[2], uu[3], uu[4], uu[5], uu[6], | |
60 | uu[7], uu[8], uu[9], uu[10], uu[11], uu[12], | |
61 | uu[13], uu[14], uu[15]); | |
62 | } | |
63 | ||
64 | int qemu_uuid_parse(const char *str, uint8_t *uuid) | |
65 | { | |
66 | int ret; | |
67 | ||
68 | if (strlen(str) != 36) { | |
69 | return -1; | |
70 | } | |
71 | ||
72 | ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3], | |
73 | &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9], | |
74 | &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], | |
75 | &uuid[15]); | |
76 | ||
77 | if (ret != 16) { | |
78 | return -1; | |
79 | } | |
80 | return 0; | |
81 | } | |
82 | ||
83 | /* Swap from UUID format endian (BE) to the opposite or vice versa. | |
84 | */ | |
85 | void qemu_uuid_bswap(QemuUUID *uuid) | |
86 | { | |
87 | assert(QEMU_PTR_IS_ALIGNED(uuid, sizeof(uint32_t))); | |
88 | bswap32s(&uuid->fields.time_low); | |
89 | bswap16s(&uuid->fields.time_mid); | |
90 | bswap16s(&uuid->fields.time_high_and_version); | |
91 | } |