WHATSNEW: Update changes since 3.3.0pre1 and release date.
[Samba.git] / source / librpc / ndr / uuid.c
blobe92df224947b121d7c898b0564151900efcd1c11
1 /*
2 Unix SMB/CIFS implementation.
4 UUID/GUID functions
6 Copyright (C) Theodore Ts'o 1996, 1997,
7 Copyright (C) Jim McDonough 2002.
8 Copyright (C) Andrew Tridgell 2003.
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.
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.
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/>.
24 #include "includes.h"
25 #include "librpc/ndr/libndr.h"
27 /**
28 build a GUID from a string
30 _PUBLIC_ NTSTATUS GUID_from_string(const char *s, struct GUID *guid)
32 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
33 uint32_t time_low;
34 uint32_t time_mid, time_hi_and_version;
35 uint32_t clock_seq[2];
36 uint32_t node[6];
37 int i;
39 if (s == NULL) {
40 return NT_STATUS_INVALID_PARAMETER;
43 if (11 == sscanf(s, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
44 &time_low, &time_mid, &time_hi_and_version,
45 &clock_seq[0], &clock_seq[1],
46 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
47 status = NT_STATUS_OK;
48 } else if (11 == sscanf(s, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
49 &time_low, &time_mid, &time_hi_and_version,
50 &clock_seq[0], &clock_seq[1],
51 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
52 status = NT_STATUS_OK;
55 if (!NT_STATUS_IS_OK(status)) {
56 return status;
59 guid->time_low = time_low;
60 guid->time_mid = time_mid;
61 guid->time_hi_and_version = time_hi_and_version;
62 guid->clock_seq[0] = clock_seq[0];
63 guid->clock_seq[1] = clock_seq[1];
64 for (i=0;i<6;i++) {
65 guid->node[i] = node[i];
68 return NT_STATUS_OK;
71 /**
72 build a GUID from a string
74 _PUBLIC_ NTSTATUS NS_GUID_from_string(const char *s, struct GUID *guid)
76 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
77 uint32_t time_low;
78 uint32_t time_mid, time_hi_and_version;
79 uint32_t clock_seq[2];
80 uint32_t node[6];
81 int i;
83 if (s == NULL) {
84 return NT_STATUS_INVALID_PARAMETER;
87 if (11 == sscanf(s, "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
88 &time_low, &time_mid, &time_hi_and_version,
89 &clock_seq[0], &clock_seq[1],
90 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
91 status = NT_STATUS_OK;
94 if (!NT_STATUS_IS_OK(status)) {
95 return status;
98 guid->time_low = time_low;
99 guid->time_mid = time_mid;
100 guid->time_hi_and_version = time_hi_and_version;
101 guid->clock_seq[0] = clock_seq[0];
102 guid->clock_seq[1] = clock_seq[1];
103 for (i=0;i<6;i++) {
104 guid->node[i] = node[i];
107 return NT_STATUS_OK;
111 * generate a random GUID
113 struct GUID GUID_random(void)
115 struct GUID guid;
117 generate_random_buffer((uint8_t *)&guid, sizeof(guid));
118 guid.clock_seq[0] = (guid.clock_seq[0] & 0x3F) | 0x80;
119 guid.time_hi_and_version = (guid.time_hi_and_version & 0x0FFF) | 0x4000;
121 return guid;
125 * generate an empty GUID
127 _PUBLIC_ struct GUID GUID_zero(void)
129 struct GUID guid;
131 ZERO_STRUCT(guid);
133 return guid;
136 _PUBLIC_ bool GUID_all_zero(const struct GUID *u)
138 if (u->time_low != 0 ||
139 u->time_mid != 0 ||
140 u->time_hi_and_version != 0 ||
141 u->clock_seq[0] != 0 ||
142 u->clock_seq[1] != 0 ||
143 !all_zero(u->node, 6)) {
144 return false;
146 return true;
149 _PUBLIC_ bool GUID_equal(const struct GUID *u1, const struct GUID *u2)
151 if (u1->time_low != u2->time_low ||
152 u1->time_mid != u2->time_mid ||
153 u1->time_hi_and_version != u2->time_hi_and_version ||
154 u1->clock_seq[0] != u2->clock_seq[0] ||
155 u1->clock_seq[1] != u2->clock_seq[1] ||
156 memcmp(u1->node, u2->node, 6) != 0) {
157 return false;
159 return true;
162 _PUBLIC_ int GUID_compare(const struct GUID *u1, const struct GUID *u2)
164 if (u1->time_low != u2->time_low) {
165 return u1->time_low - u2->time_low;
168 if (u1->time_mid != u2->time_mid) {
169 return u1->time_mid - u2->time_mid;
172 if (u1->time_hi_and_version != u2->time_hi_and_version) {
173 return u1->time_hi_and_version - u2->time_hi_and_version;
176 if (u1->clock_seq[0] != u2->clock_seq[0]) {
177 return u1->clock_seq[0] - u2->clock_seq[0];
180 if (u1->clock_seq[1] != u2->clock_seq[1]) {
181 return u1->clock_seq[1] - u2->clock_seq[1];
184 return memcmp(u1->node, u2->node, 6);
188 its useful to be able to display these in debugging messages
190 _PUBLIC_ char *GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
192 return talloc_asprintf(mem_ctx,
193 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
194 guid->time_low, guid->time_mid,
195 guid->time_hi_and_version,
196 guid->clock_seq[0],
197 guid->clock_seq[1],
198 guid->node[0], guid->node[1],
199 guid->node[2], guid->node[3],
200 guid->node[4], guid->node[5]);
203 _PUBLIC_ char *GUID_string2(TALLOC_CTX *mem_ctx, const struct GUID *guid)
205 char *ret, *s = GUID_string(mem_ctx, guid);
206 ret = talloc_asprintf(mem_ctx, "{%s}", s);
207 talloc_free(s);
208 return ret;
211 _PUBLIC_ char *NS_GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
213 return talloc_asprintf(mem_ctx,
214 "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
215 guid->time_low, guid->time_mid,
216 guid->time_hi_and_version,
217 guid->clock_seq[0],
218 guid->clock_seq[1],
219 guid->node[0], guid->node[1],
220 guid->node[2], guid->node[3],
221 guid->node[4], guid->node[5]);
224 _PUBLIC_ bool policy_handle_empty(struct policy_handle *h)
226 return (h->handle_type == 0 && GUID_all_zero(&h->uuid));