2 Unix SMB/CIFS implementation.
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/>.
25 #include "librpc/ndr/libndr.h"
26 #include "librpc/gen_ndr/ndr_misc.h"
29 build a GUID from a string
31 _PUBLIC_ NTSTATUS GUID_from_data_blob(const DATA_BLOB *s, struct GUID *guid)
33 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
35 uint32_t time_mid, time_hi_and_version;
36 uint32_t clock_seq[2];
39 DATA_BLOB blob16 = data_blob_const(buf16, sizeof(buf16));
42 if (s->data == NULL) {
43 return NT_STATUS_INVALID_PARAMETER;
46 if (s->length == 36 &&
47 11 == sscanf((const char *)s->data,
48 "%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;
53 } else if (s->length == 38
54 && 11 == sscanf((const char *)s->data,
55 "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
56 &time_low, &time_mid, &time_hi_and_version,
57 &clock_seq[0], &clock_seq[1],
58 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
59 status = NT_STATUS_OK;
60 } else if (s->length == 32) {
61 size_t rlen = strhex_to_str((char *)blob16.data, blob16.length,
62 (const char *)s->data, s->length);
63 if (rlen == blob16.length) {
64 /* goto the ndr_pull_struct_blob() path */
65 status = NT_STATUS_OK;
70 if (s->length == 16) {
71 enum ndr_err_code ndr_err;
75 mem_ctx = talloc_new(NULL);
76 NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
78 ndr_err = ndr_pull_struct_blob(s, mem_ctx, NULL, &guid2,
79 (ndr_pull_flags_fn_t)ndr_pull_GUID);
81 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
82 return ndr_map_error2ntstatus(ndr_err);
88 if (!NT_STATUS_IS_OK(status)) {
92 guid->time_low = time_low;
93 guid->time_mid = time_mid;
94 guid->time_hi_and_version = time_hi_and_version;
95 guid->clock_seq[0] = clock_seq[0];
96 guid->clock_seq[1] = clock_seq[1];
98 guid->node[i] = node[i];
105 build a GUID from a string
107 _PUBLIC_ NTSTATUS GUID_from_string(const char *s, struct GUID *guid)
109 DATA_BLOB blob = data_blob_string_const(s);
110 return GUID_from_data_blob(&blob, guid);
115 build a GUID from a string
117 _PUBLIC_ NTSTATUS NS_GUID_from_string(const char *s, struct GUID *guid)
119 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
121 uint32_t time_mid, time_hi_and_version;
122 uint32_t clock_seq[2];
127 return NT_STATUS_INVALID_PARAMETER;
130 if (11 == sscanf(s, "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
131 &time_low, &time_mid, &time_hi_and_version,
132 &clock_seq[0], &clock_seq[1],
133 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
134 status = NT_STATUS_OK;
137 if (!NT_STATUS_IS_OK(status)) {
141 guid->time_low = time_low;
142 guid->time_mid = time_mid;
143 guid->time_hi_and_version = time_hi_and_version;
144 guid->clock_seq[0] = clock_seq[0];
145 guid->clock_seq[1] = clock_seq[1];
147 guid->node[i] = node[i];
154 * generate a random GUID
156 _PUBLIC_ struct GUID GUID_random(void)
160 generate_random_buffer((uint8_t *)&guid, sizeof(guid));
161 guid.clock_seq[0] = (guid.clock_seq[0] & 0x3F) | 0x80;
162 guid.time_hi_and_version = (guid.time_hi_and_version & 0x0FFF) | 0x4000;
168 * generate an empty GUID
170 _PUBLIC_ struct GUID GUID_zero(void)
179 _PUBLIC_ bool GUID_all_zero(const struct GUID *u)
181 if (u->time_low != 0 ||
183 u->time_hi_and_version != 0 ||
184 u->clock_seq[0] != 0 ||
185 u->clock_seq[1] != 0 ||
186 !all_zero(u->node, 6)) {
192 _PUBLIC_ bool GUID_equal(const struct GUID *u1, const struct GUID *u2)
194 if (u1->time_low != u2->time_low ||
195 u1->time_mid != u2->time_mid ||
196 u1->time_hi_and_version != u2->time_hi_and_version ||
197 u1->clock_seq[0] != u2->clock_seq[0] ||
198 u1->clock_seq[1] != u2->clock_seq[1] ||
199 memcmp(u1->node, u2->node, 6) != 0) {
205 _PUBLIC_ int GUID_compare(const struct GUID *u1, const struct GUID *u2)
207 if (u1->time_low != u2->time_low) {
208 return u1->time_low - u2->time_low;
211 if (u1->time_mid != u2->time_mid) {
212 return u1->time_mid - u2->time_mid;
215 if (u1->time_hi_and_version != u2->time_hi_and_version) {
216 return u1->time_hi_and_version - u2->time_hi_and_version;
219 if (u1->clock_seq[0] != u2->clock_seq[0]) {
220 return u1->clock_seq[0] - u2->clock_seq[0];
223 if (u1->clock_seq[1] != u2->clock_seq[1]) {
224 return u1->clock_seq[1] - u2->clock_seq[1];
227 return memcmp(u1->node, u2->node, 6);
231 its useful to be able to display these in debugging messages
233 _PUBLIC_ char *GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
235 return talloc_asprintf(mem_ctx,
236 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
237 guid->time_low, guid->time_mid,
238 guid->time_hi_and_version,
241 guid->node[0], guid->node[1],
242 guid->node[2], guid->node[3],
243 guid->node[4], guid->node[5]);
246 _PUBLIC_ char *GUID_string2(TALLOC_CTX *mem_ctx, const struct GUID *guid)
248 char *ret, *s = GUID_string(mem_ctx, guid);
249 ret = talloc_asprintf(mem_ctx, "{%s}", s);
254 _PUBLIC_ char *GUID_hexstring(TALLOC_CTX *mem_ctx, const struct GUID *guid)
258 enum ndr_err_code ndr_err;
261 tmp_mem = talloc_new(mem_ctx);
265 ndr_err = ndr_push_struct_blob(&guid_blob, tmp_mem,
268 (ndr_push_flags_fn_t)ndr_push_GUID);
269 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
270 talloc_free(tmp_mem);
274 ret = data_blob_hex_string(mem_ctx, &guid_blob);
275 talloc_free(tmp_mem);
279 _PUBLIC_ char *NS_GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
281 return talloc_asprintf(mem_ctx,
282 "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
283 guid->time_low, guid->time_mid,
284 guid->time_hi_and_version,
287 guid->node[0], guid->node[1],
288 guid->node[2], guid->node[3],
289 guid->node[4], guid->node[5]);
292 _PUBLIC_ bool policy_handle_empty(struct policy_handle *h)
294 return (h->handle_type == 0 && GUID_all_zero(&h->uuid));