Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/util/uuid.c
Line
Count
Source
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/uuid.h"
18
#include "qemu/bswap.h"
19
20
void qemu_uuid_generate(QemuUUID *uuid)
21
0
{
22
0
    int i;
23
0
    uint32_t tmp[4];
24
25
0
    QEMU_BUILD_BUG_ON(sizeof(QemuUUID) != 16);
26
27
0
    for (i = 0; i < 4; ++i) {
28
0
        tmp[i] = g_random_int();
29
0
    }
30
0
    memcpy(uuid, tmp, sizeof(tmp));
31
    /* Set the two most significant bits (bits 6 and 7) of the
32
      clock_seq_hi_and_reserved to zero and one, respectively. */
33
0
    uuid->data[8] = (uuid->data[8] & 0x3f) | 0x80;
34
    /* Set the four most significant bits (bits 12 through 15) of the
35
      time_hi_and_version field to the 4-bit version number.
36
      */
37
0
    uuid->data[6] = (uuid->data[6] & 0xf) | 0x40;
38
0
}
39
40
int qemu_uuid_is_null(const QemuUUID *uu)
41
0
{
42
0
    static QemuUUID null_uuid;
43
0
    return qemu_uuid_is_equal(uu, &null_uuid);
44
0
}
45
46
int qemu_uuid_is_equal(const QemuUUID *lhv, const QemuUUID *rhv)
47
0
{
48
0
    return memcmp(lhv, rhv, sizeof(QemuUUID)) == 0;
49
0
}
50
51
void qemu_uuid_unparse(const QemuUUID *uuid, char *out)
52
0
{
53
0
    const unsigned char *uu = &uuid->data[0];
54
0
    snprintf(out, UUID_STR_LEN, UUID_FMT,
55
0
             uu[0], uu[1], uu[2], uu[3], uu[4], uu[5], uu[6], uu[7],
56
0
             uu[8], uu[9], uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]);
57
0
}
58
59
char *qemu_uuid_unparse_strdup(const QemuUUID *uuid)
60
0
{
61
0
    const unsigned char *uu = &uuid->data[0];
62
0
    return g_strdup_printf(UUID_FMT,
63
0
                           uu[0], uu[1], uu[2], uu[3], uu[4], uu[5], uu[6],
64
0
                           uu[7], uu[8], uu[9], uu[10], uu[11], uu[12],
65
0
                           uu[13], uu[14], uu[15]);
66
0
}
67
68
static bool qemu_uuid_is_valid(const char *str)
69
61
{
70
61
    int i;
71
72
2.25k
    for (i = 0; i < strlen(str); i++) {
73
2.19k
        const char c = str[i];
74
2.19k
        if (i == 8 || i == 13 || i == 18 || i == 23) {
75
244
            if (str[i] != '-') {
76
0
                return false;
77
0
            }
78
1.95k
        } else {
79
1.95k
            if ((c >= '0' && c <= '9') ||
80
427
                (c >= 'A' && c <= 'F') ||
81
1.95k
                (c >= 'a' && c <= 'f')) {
82
1.95k
                continue;
83
1.95k
            }
84
0
            return false;
85
1.95k
        }
86
2.19k
    }
87
61
    return i == 36;
88
61
}
89
90
int qemu_uuid_parse(const char *str, QemuUUID *uuid)
91
61
{
92
61
    unsigned char *uu = &uuid->data[0];
93
61
    int ret;
94
95
61
    if (!qemu_uuid_is_valid(str)) {
96
0
        return -1;
97
0
    }
98
99
61
    ret = sscanf(str, UUID_FMT, &uu[0], &uu[1], &uu[2], &uu[3],
100
61
                 &uu[4], &uu[5], &uu[6], &uu[7], &uu[8], &uu[9],
101
61
                 &uu[10], &uu[11], &uu[12], &uu[13], &uu[14],
102
61
                 &uu[15]);
103
104
61
    if (ret != 16) {
105
0
        return -1;
106
0
    }
107
61
    return 0;
108
61
}
109
110
/* Swap from UUID format endian (BE) to the opposite or vice versa.
111
 */
112
QemuUUID qemu_uuid_bswap(QemuUUID uuid)
113
0
{
114
0
    bswap32s(&uuid.fields.time_low);
115
0
    bswap16s(&uuid.fields.time_mid);
116
0
    bswap16s(&uuid.fields.time_high_and_version);
117
0
    return uuid;
118
0
}
119
120
/* djb2 hash algorithm */
121
uint32_t qemu_uuid_hash(const void *uuid)
122
0
{
123
0
    QemuUUID *qid = (QemuUUID *) uuid;
124
0
    uint32_t h = 5381;
125
0
    int i;
126
127
0
    for (i = 0; i < ARRAY_SIZE(qid->data); i++) {
128
0
        h = (h << 5) + h + qid->data[i];
129
0
    }
130
131
0
    return h;
132
0
}