1  /* Copyright (c) 2008, XenSource Inc.
2  * Copyright (c) 2011, Citrix
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *     * Redistributions of source code must retain the above copyright
9  *       notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above copyright
11  *       notice, this list of conditions and the following disclaimer in the
12  *       documentation and/or other materials provided with the distribution.
13  *     * Neither the name of XenSource Inc. nor the names of its contributors
14  *       may be used to endorse or promote products derived from this software
15  *       without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #if defined(__linux__)
31 
32 #include <uuid/uuid.h>
33 
34 typedef struct {
35     uuid_t uuid;
36 } vhd_uuid_t;
37 
vhd_uuid_is_nil(vhd_uuid_t * uuid)38 int vhd_uuid_is_nil(vhd_uuid_t *uuid)
39 {
40 	return uuid_is_null(uuid->uuid);
41 }
42 
vhd_uuid_generate(vhd_uuid_t * uuid)43 void vhd_uuid_generate(vhd_uuid_t *uuid)
44 {
45 	uuid_generate(uuid->uuid);
46 }
47 
vhd_uuid_to_string(vhd_uuid_t * uuid,char * out,size_t size)48 void vhd_uuid_to_string(vhd_uuid_t *uuid, char *out, size_t size)
49 {
50 	uuid_unparse(uuid->uuid, out);
51 }
52 
vhd_uuid_from_string(vhd_uuid_t * uuid,const char * in)53 void vhd_uuid_from_string(vhd_uuid_t *uuid, const char *in)
54 {
55 	uuid_parse(in, uuid->uuid);
56 }
57 
vhd_uuid_copy(vhd_uuid_t * dst,vhd_uuid_t * src)58 void vhd_uuid_copy(vhd_uuid_t *dst, vhd_uuid_t *src)
59 {
60 	uuid_copy(dst->uuid, src->uuid);
61 }
62 
vhd_uuid_clear(vhd_uuid_t * uuid)63 void vhd_uuid_clear(vhd_uuid_t *uuid)
64 {
65 	uuid_clear(uuid->uuid);
66 }
67 
vhd_uuid_compare(vhd_uuid_t * uuid1,vhd_uuid_t * uuid2)68 int vhd_uuid_compare(vhd_uuid_t *uuid1, vhd_uuid_t *uuid2)
69 {
70 	return uuid_compare(uuid1->uuid, uuid2->uuid);
71 }
72 
73 #elif defined(__NetBSD__)
74 
75 #include <uuid.h>
76 #include <string.h>
77 #include <stdlib.h>
78 
79 typedef uuid_t vhd_uuid_t;
80 
vhd_uuid_is_nil(vhd_uuid_t * uuid)81 int vhd_uuid_is_nil(vhd_uuid_t *uuid)
82 {
83 	uint32_t status;
84 	return uuid_is_nil((uuid_t *)uuid, &status);
85 }
86 
vhd_uuid_generate(vhd_uuid_t * uuid)87 void vhd_uuid_generate(vhd_uuid_t *uuid)
88 {
89 	uint32_t status;
90 	uuid_create((uuid_t *)uuid, &status);
91 }
92 
vhd_uuid_to_string(vhd_uuid_t * uuid,char * out,size_t size)93 void vhd_uuid_to_string(vhd_uuid_t *uuid, char *out, size_t size)
94 {
95 	uint32_t status;
96 	char *_out = NULL;
97 	uuid_to_string((uuid_t *)uuid, &_out, &status);
98 	strlcpy(out, _out, size);
99 	free(_out);
100 }
101 
vhd_uuid_from_string(vhd_uuid_t * uuid,const char * in)102 void vhd_uuid_from_string(vhd_uuid_t *uuid, const char *in)
103 {
104 	uint32_t status;
105 	uuid_from_string(in, (uuid_t *)uuid, &status);
106 }
107 
vhd_uuid_copy(vhd_uuid_t * dst,vhd_uuid_t * src)108 void vhd_uuid_copy(vhd_uuid_t *dst, vhd_uuid_t *src)
109 {
110 	memcpy((uuid_t *)dst, (uuid_t *)src, sizeof(uuid_t));
111 }
112 
vhd_uuid_clear(vhd_uuid_t * uuid)113 void vhd_uuid_clear(vhd_uuid_t *uuid)
114 {
115 	memset((uuid_t *)uuid, 0, sizeof(uuid_t));
116 }
117 
vhd_uuid_compare(vhd_uuid_t * uuid1,vhd_uuid_t * uuid2)118 int vhd_uuid_compare(vhd_uuid_t *uuid1, vhd_uuid_t *uuid2)
119 {
120 	uint32_t status;
121 	return uuid_compare((uuid_t *)uuid1, (uuid_t *)uuid2, &status);
122 }
123 
124 #else
125 
126 #error "Please update vhd-util-uuid.c for your OS"
127 
128 #endif
129