summaryrefslogtreecommitdiff
path: root/tools/libs/light/libxl_uuid.c
blob: 7b68270a3350ed7b576521d83b6a53c5e65ee4b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * Copyright (C) 2008,2010 Citrix Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 */

#include "libxl_osdeps.h" /* must come before any other headers */

#include "libxl_internal.h"

#if defined(__linux__)

int libxl_uuid_is_nil(const libxl_uuid *uuid)
{
     return uuid_is_null(uuid->uuid);
}

void libxl_uuid_generate(libxl_uuid *uuid)
{
     uuid_generate(uuid->uuid);
}

int libxl_uuid_from_string(libxl_uuid *uuid, const char *in)
{
     return uuid_parse(in, uuid->uuid);
}

void libxl_uuid_copy(libxl_ctx *ctx_opt, libxl_uuid *dst,
                     const libxl_uuid *src)
{
     uuid_copy(dst->uuid, src->uuid);
}

void libxl_uuid_clear(libxl_uuid *uuid)
{
     uuid_clear(uuid->uuid);
}

int libxl_uuid_compare(const libxl_uuid *uuid1, const libxl_uuid *uuid2)
{
     return uuid_compare(uuid1->uuid, uuid2->uuid);
}

const uint8_t *libxl_uuid_bytearray_const(const libxl_uuid *uuid)
{
    return uuid->uuid;
}

uint8_t *libxl_uuid_bytearray(libxl_uuid *uuid)
{
    return uuid->uuid;
}

#elif defined(__FreeBSD__) || defined(__NetBSD__)

int libxl_uuid_is_nil(const libxl_uuid *uuid)
{
    uint32_t status;
    uuid_t nat_uuid;

    uuid_dec_be(uuid->uuid, &nat_uuid);

    return uuid_is_nil(&nat_uuid, &status);
}

void libxl_uuid_generate(libxl_uuid *uuid)
{
    uint32_t status;
    uuid_t nat_uuid;

    uuid_create(&nat_uuid, &status);
    assert(status == uuid_s_ok);

    uuid_enc_be(uuid->uuid, &nat_uuid);
}

int libxl_uuid_from_string(libxl_uuid *uuid, const char *in)
{
    uint32_t status;
    uuid_t nat_uuid;

    uuid_from_string(in, &nat_uuid, &status);
    if (status != uuid_s_ok)
        return ERROR_FAIL;
    uuid_enc_be(uuid->uuid, &nat_uuid);

    return 0;
}

void libxl_uuid_copy(libxl_ctx *ctx_opt, libxl_uuid *dst,
                     const libxl_uuid *src)
{
    memcpy(&dst->uuid, &src->uuid, sizeof(dst->uuid));
}

void libxl_uuid_clear(libxl_uuid *uuid)
{
    memset(&uuid->uuid, 0, sizeof(uuid->uuid));
}

int libxl_uuid_compare(const libxl_uuid *uuid1, const libxl_uuid *uuid2)
{
    uuid_t nat_uuid1, nat_uuid2;

    uuid_dec_be(uuid1->uuid, &nat_uuid1);
    uuid_dec_be(uuid2->uuid, &nat_uuid2);

    return uuid_compare(&nat_uuid1, &nat_uuid2, NULL);
}

const uint8_t *libxl_uuid_bytearray_const(const libxl_uuid *uuid)
{

    return uuid->uuid;
}

uint8_t *libxl_uuid_bytearray(libxl_uuid *uuid)
{

    return uuid->uuid;
}
#else

#error "Please update libxl_uuid.c for your OS"

#endif

/*
 * Local variables:
 * mode: C
 * c-basic-offset: 4
 * indent-tabs-mode: nil
 * End:
 */