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 
libxl_uuid_from_string(libxl_uuid * uuid,const char * in)85 int libxl_uuid_from_string(libxl_uuid *uuid, const char *in)
86 {
87     uint32_t status;
88     uuid_t nat_uuid;
89 
90     uuid_from_string(in, &nat_uuid, &status);
91     if (status != uuid_s_ok)
92         return ERROR_FAIL;
93     uuid_enc_be(uuid->uuid, &nat_uuid);
94 
95     return 0;
96 }
97 
libxl_uuid_copy(libxl_ctx * ctx_opt,libxl_uuid * dst,const libxl_uuid * src)98 void libxl_uuid_copy(libxl_ctx *ctx_opt, libxl_uuid *dst,
99                      const libxl_uuid *src)
100 {
101     memcpy(&dst->uuid, &src->uuid, sizeof(dst->uuid));
102 }
103 
libxl_uuid_clear(libxl_uuid * uuid)104 void libxl_uuid_clear(libxl_uuid *uuid)
105 {
106     memset(&uuid->uuid, 0, sizeof(uuid->uuid));
107 }
108 
libxl_uuid_compare(const libxl_uuid * uuid1,const libxl_uuid * uuid2)109 int libxl_uuid_compare(const libxl_uuid *uuid1, const libxl_uuid *uuid2)
110 {
111     uuid_t nat_uuid1, nat_uuid2;
112 
113     uuid_dec_be(uuid1->uuid, &nat_uuid1);
114     uuid_dec_be(uuid2->uuid, &nat_uuid2);
115 
116     return uuid_compare(&nat_uuid1, &nat_uuid2, NULL);
117 }
118 
libxl_uuid_bytearray_const(const libxl_uuid * uuid)119 const uint8_t *libxl_uuid_bytearray_const(const libxl_uuid *uuid)
120 {
121 
122     return uuid->uuid;
123 }
124 
libxl_uuid_bytearray(libxl_uuid * uuid)125 uint8_t *libxl_uuid_bytearray(libxl_uuid *uuid)
126 {
127 
128     return uuid->uuid;
129 }
130 #else
131 
132 #error "Please update libxl_uuid.c for your OS"
133 
134 #endif
135 
136 /*
137  * Local variables:
138  * mode: C
139  * c-basic-offset: 4
140  * indent-tabs-mode: nil
141  * End:
142  */
143