r23792: convert Samba4 to GPLv3
[sfrench/samba-autobuild/.git] / source4 / librpc / ndr / uuid.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    UUID/GUID functions
5
6    Copyright (C) Theodore Ts'o               1996, 1997,
7    Copyright (C) Jim McDonough                     2002.
8    Copyright (C) Andrew Tridgell                   2003.
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25
26 /**
27   build a GUID from a string
28 */
29 _PUBLIC_ NTSTATUS GUID_from_string(const char *s, struct GUID *guid)
30 {
31         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
32         uint32_t time_low;
33         uint32_t time_mid, time_hi_and_version;
34         uint32_t clock_seq[2];
35         uint32_t node[6];
36         int i;
37
38         if (s == NULL) {
39                 return NT_STATUS_INVALID_PARAMETER;
40         }
41
42         if (11 == sscanf(s, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
43                          &time_low, &time_mid, &time_hi_and_version, 
44                          &clock_seq[0], &clock_seq[1],
45                          &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
46                 status = NT_STATUS_OK;
47         } else if (11 == sscanf(s, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
48                                 &time_low, &time_mid, &time_hi_and_version, 
49                                 &clock_seq[0], &clock_seq[1],
50                                 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
51                 status = NT_STATUS_OK;
52         }
53
54         if (!NT_STATUS_IS_OK(status)) {
55                 return status;
56         }
57
58         guid->time_low = time_low;
59         guid->time_mid = time_mid;
60         guid->time_hi_and_version = time_hi_and_version;
61         guid->clock_seq[0] = clock_seq[0];
62         guid->clock_seq[1] = clock_seq[1];
63         for (i=0;i<6;i++) {
64                 guid->node[i] = node[i];
65         }
66
67         return NT_STATUS_OK;
68 }
69
70 /**
71   build a GUID from a string
72 */
73 _PUBLIC_ NTSTATUS NS_GUID_from_string(const char *s, struct GUID *guid)
74 {
75         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
76         uint32_t time_low;
77         uint32_t time_mid, time_hi_and_version;
78         uint32_t clock_seq[2];
79         uint32_t node[6];
80         int i;
81
82         if (s == NULL) {
83                 return NT_STATUS_INVALID_PARAMETER;
84         }
85
86         if (11 == sscanf(s, "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
87                          &time_low, &time_mid, &time_hi_and_version, 
88                          &clock_seq[0], &clock_seq[1],
89                          &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
90                 status = NT_STATUS_OK;
91         }
92
93         if (!NT_STATUS_IS_OK(status)) {
94                 return status;
95         }
96
97         guid->time_low = time_low;
98         guid->time_mid = time_mid;
99         guid->time_hi_and_version = time_hi_and_version;
100         guid->clock_seq[0] = clock_seq[0];
101         guid->clock_seq[1] = clock_seq[1];
102         for (i=0;i<6;i++) {
103                 guid->node[i] = node[i];
104         }
105
106         return NT_STATUS_OK;
107 }
108
109 /**
110  * generate a random GUID
111  */
112 struct GUID GUID_random(void)
113 {
114         struct GUID guid;
115
116         generate_random_buffer((uint8_t *)&guid, sizeof(guid));
117         guid.clock_seq[0] = (guid.clock_seq[0] & 0x3F) | 0x80;
118         guid.time_hi_and_version = (guid.time_hi_and_version & 0x0FFF) | 0x4000;
119
120         return guid;
121 }
122
123 /**
124  * generate an empty GUID 
125  */
126 _PUBLIC_ struct GUID GUID_zero(void)
127 {
128         struct GUID guid;
129
130         ZERO_STRUCT(guid);
131
132         return guid;
133 }
134
135 _PUBLIC_ BOOL GUID_all_zero(const struct GUID *u)
136 {
137         if (u->time_low != 0 ||
138             u->time_mid != 0 ||
139             u->time_hi_and_version != 0 ||
140             u->clock_seq[0] != 0 ||
141             u->clock_seq[1] != 0 ||
142             !all_zero(u->node, 6)) {
143                 return False;
144         }
145         return True;
146 }
147
148 _PUBLIC_ BOOL GUID_equal(const struct GUID *u1, const struct GUID *u2)
149 {
150         if (u1->time_low != u2->time_low ||
151             u1->time_mid != u2->time_mid ||
152             u1->time_hi_and_version != u2->time_hi_and_version ||
153             u1->clock_seq[0] != u2->clock_seq[0] ||
154             u1->clock_seq[1] != u2->clock_seq[1] ||
155             memcmp(u1->node, u2->node, 6) != 0) {
156                 return False;
157         }
158         return True;
159 }
160
161 _PUBLIC_ int GUID_compare(const struct GUID *u1, const struct GUID *u2)
162 {
163         if (u1->time_low != u2->time_low) {
164                 return u1->time_low - u2->time_low;
165         }
166
167         if (u1->time_mid != u2->time_mid) {
168                 return u1->time_mid - u2->time_mid;
169         }
170
171         if (u1->time_hi_and_version != u2->time_hi_and_version) {
172                 return u1->time_hi_and_version - u2->time_hi_and_version;
173         }
174
175         if (u1->clock_seq[0] != u2->clock_seq[0]) {
176                 return u1->clock_seq[0] - u2->clock_seq[0];
177         }
178
179         if (u1->clock_seq[1] != u2->clock_seq[1]) {
180                 return u1->clock_seq[1] - u2->clock_seq[1];
181         }
182
183         return memcmp(u1->node, u2->node, 6);
184 }
185
186 /**
187   its useful to be able to display these in debugging messages
188 */
189 _PUBLIC_ char *GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
190 {
191         return talloc_asprintf(mem_ctx, 
192                                "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
193                                guid->time_low, guid->time_mid,
194                                guid->time_hi_and_version,
195                                guid->clock_seq[0],
196                                guid->clock_seq[1],
197                                guid->node[0], guid->node[1],
198                                guid->node[2], guid->node[3],
199                                guid->node[4], guid->node[5]);
200 }
201
202 _PUBLIC_ char *GUID_string2(TALLOC_CTX *mem_ctx, const struct GUID *guid)
203 {
204         char *ret, *s = GUID_string(mem_ctx, guid);
205         ret = talloc_asprintf(mem_ctx, "{%s}", s);
206         talloc_free(s);
207         return ret;
208 }
209
210 _PUBLIC_ char *NS_GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
211 {
212         return talloc_asprintf(mem_ctx, 
213                                "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
214                                guid->time_low, guid->time_mid,
215                                guid->time_hi_and_version,
216                                guid->clock_seq[0],
217                                guid->clock_seq[1],
218                                guid->node[0], guid->node[1],
219                                guid->node[2], guid->node[3],
220                                guid->node[4], guid->node[5]);
221 }
222
223 _PUBLIC_ BOOL policy_handle_empty(struct policy_handle *h) 
224 {
225         return (h->handle_type == 0 && GUID_all_zero(&h->uuid));
226 }