1 /*
2  * Copyright (C) 2008,2010 Citrix Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; version 2.1 only. with the special
7  * exception on linking described in file LICENSE.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  */
14 
15 #include "libxl_osdeps.h" /* must come before any other headers */
16 
17 #include "libxl_internal.h"
18 
19 #if defined(__linux__)
20 
libxl_uuid_is_nil(const libxl_uuid * uuid)21 int libxl_uuid_is_nil(const libxl_uuid *uuid)
22 {
23      return uuid_is_null(uuid->uuid);
24 }
25 
libxl_uuid_generate(libxl_uuid * uuid)26 void libxl_uuid_generate(libxl_uuid *uuid)
27 {
28      uuid_generate(uuid->uuid);
29 }
30 
libxl_uuid_from_string(libxl_uuid * uuid,const char * in)31 int libxl_uuid_from_string(libxl_uuid *uuid, const char *in)
32 {
33      return uuid_parse(in, uuid->uuid);
34 }
35 
libxl_uuid_copy(libxl_ctx * ctx_opt,libxl_uuid * dst,const libxl_uuid * src)36 void libxl_uuid_copy(libxl_ctx *ctx_opt, libxl_uuid *dst,
37                      const libxl_uuid *src)
38 {
39      uuid_copy(dst->uuid, src->uuid);
40 }
41 
libxl_uuid_clear(libxl_uuid * uuid)42 void libxl_uuid_clear(libxl_uuid *uuid)
43 {
44      uuid_clear(uuid->uuid);
45 }
46 
libxl_uuid_compare(const libxl_uuid * uuid1,const libxl_uuid * uuid2)47 int libxl_uuid_compare(const libxl_uuid *uuid1, const libxl_uuid *uuid2)
48 {
49      return uuid_compare(uuid1->uuid, uuid2->uuid);
50 }
51 
libxl_uuid_bytearray_const(const libxl_uuid * uuid)52 const uint8_t *libxl_uuid_bytearray_const(const libxl_uuid *uuid)
53 {
54     return uuid->uuid;
55 }
56 
libxl_uuid_bytearray(libxl_uuid * uuid)57 uint8_t *libxl_uuid_bytearray(libxl_uuid *uuid)
58 {
59     return uuid->uuid;
60 }
61 
62 #elif defined(__FreeBSD__) || defined(__NetBSD__)
63 
libxl_uuid_is_nil(const libxl_uuid * uuid)64 int libxl_uuid_is_nil(const libxl_uuid *uuid)
65 {
66     uint32_t status;
67     uuid_t nat_uuid;
68 
69     uuid_dec_be(uuid->uuid, &nat_uuid);
70 
71     return uuid_is_nil(&nat_uuid, &status);
72 }
73 
libxl_uuid_generate(libxl_uuid * uuid)74 void libxl_uuid_generate(libxl_uuid *uuid)
75 {
76     uint32_t status;
77     uuid_t nat_uuid;
78 
79     uuid_create(&nat_uuid, &status);
80     assert(status == uuid_s_ok);
81 
82     uuid_enc_be(uuid->uuid, &nat_uuid);
83 }
84 
85 #ifdef __FreeBSD__
libxl_uuid_from_string(libxl_uuid * uuid,const char * in)86 int libxl_uuid_from_string(libxl_uuid *uuid, const char *in)
87 {
88     uint32_t status;
89     uuid_t nat_uuid;
90 
91     uuid_from_string(in, &nat_uuid, &status);
92     if (status != uuid_s_ok)
93         return ERROR_FAIL;
94     uuid_enc_be(uuid->uuid, &nat_uuid);
95 
96     return 0;
97 }
98 #else
99 #define LIBXL__UUID_PTRS(uuid) &uuid[0], &uuid[1], &uuid[2], &uuid[3], \
100                                &uuid[4], &uuid[5], &uuid[6], &uuid[7], \
101                                &uuid[8], &uuid[9], &uuid[10],&uuid[11], \
102                                &uuid[12],&uuid[13],&uuid[14],&uuid[15]
libxl_uuid_from_string(libxl_uuid * uuid,const char * in)103 int libxl_uuid_from_string(libxl_uuid *uuid, const char *in)
104 {
105     if ( sscanf(in, LIBXL_UUID_FMT, LIBXL__UUID_PTRS(uuid->uuid)) != sizeof(uuid->uuid) )
106         return -1;
107     return 0;
108 }
109 #undef LIBXL__UUID_PTRS
110 #endif
111 
libxl_uuid_copy(libxl_ctx * ctx_opt,libxl_uuid * dst,const libxl_uuid * src)112 void libxl_uuid_copy(libxl_ctx *ctx_opt, libxl_uuid *dst,
113                      const libxl_uuid *src)
114 {
115     memcpy(&dst->uuid, &src->uuid, sizeof(dst->uuid));
116 }
117 
libxl_uuid_clear(libxl_uuid * uuid)118 void libxl_uuid_clear(libxl_uuid *uuid)
119 {
120     memset(&uuid->uuid, 0, sizeof(uuid->uuid));
121 }
122 
123 #ifdef __FreeBSD__
libxl_uuid_compare(const libxl_uuid * uuid1,const libxl_uuid * uuid2)124 int libxl_uuid_compare(const libxl_uuid *uuid1, const libxl_uuid *uuid2)
125 {
126     uuid_t nat_uuid1, nat_uuid2;
127 
128     uuid_dec_be(uuid1->uuid, &nat_uuid1);
129     uuid_dec_be(uuid2->uuid, &nat_uuid2);
130 
131     return uuid_compare(&nat_uuid1, &nat_uuid2, NULL);
132 }
133 #else
libxl_uuid_compare(const libxl_uuid * uuid1,const libxl_uuid * uuid2)134 int libxl_uuid_compare(const libxl_uuid *uuid1, const libxl_uuid *uuid2)
135 {
136      return memcmp(uuid1->uuid, uuid2->uuid, sizeof(uuid1->uuid));
137 }
138 #endif
139 
libxl_uuid_bytearray_const(const libxl_uuid * uuid)140 const uint8_t *libxl_uuid_bytearray_const(const libxl_uuid *uuid)
141 {
142 
143     return uuid->uuid;
144 }
145 
libxl_uuid_bytearray(libxl_uuid * uuid)146 uint8_t *libxl_uuid_bytearray(libxl_uuid *uuid)
147 {
148 
149     return uuid->uuid;
150 }
151 #else
152 
153 #error "Please update libxl_uuid.c for your OS"
154 
155 #endif
156 
157 /*
158  * Local variables:
159  * mode: C
160  * c-basic-offset: 4
161  * indent-tabs-mode: nil
162  * End:
163  */
164