Add a new function to parse a DATA_BLOB into a GUID
[Samba/eduardoll.git] / librpc / ndr / uuid.c
blob308b5c0688c17a7348a4c6f2af68182d117d3c0a
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_data_blob(const DATA_BLOB *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->data == NULL) {
40 return NT_STATUS_INVALID_PARAMETER;
43 if (s->length == 36 &&
44 11 == sscanf((const char *)s->data,
45 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
46 &time_low, &time_mid, &time_hi_and_version,
47 &clock_seq[0], &clock_seq[1],
48 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
49 status = NT_STATUS_OK;
50 } else if (s->length == 38
51 && 11 == sscanf((const char *)s->data,
52 "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
53 &time_low, &time_mid, &time_hi_and_version,
54 &clock_seq[0], &clock_seq[1],
55 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
56 status = NT_STATUS_OK;
59 if (!NT_STATUS_IS_OK(status)) {
60 return status;
63 guid->time_low = time_low;
64 guid->time_mid = time_mid;
65 guid->time_hi_and_version = time_hi_and_version;
66 guid->clock_seq[0] = clock_seq[0];
67 guid->clock_seq[1] = clock_seq[1];
68 for (i=0;i<6;i++) {
69 guid->node[i] = node[i];
72 return NT_STATUS_OK;
75 /**
76 build a GUID from a string
78 _PUBLIC_ NTSTATUS GUID_from_string(const char *s, struct GUID *guid)
80 DATA_BLOB blob = data_blob_string_const(s);
81 return GUID_from_data_blob(&blob, guid);
82 return NT_STATUS_OK;
85 /**
86 build a GUID from a string
88 _PUBLIC_ NTSTATUS NS_GUID_from_string(const char *s, struct GUID *guid)
90 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
91 uint32_t time_low;
92 uint32_t time_mid, time_hi_and_version;
93 uint32_t clock_seq[2];
94 uint32_t node[6];
95 int i;
97 if (s == NULL) {
98 return NT_STATUS_INVALID_PARAMETER;
101 if (11 == sscanf(s, "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
102 &time_low, &time_mid, &time_hi_and_version,
103 &clock_seq[0], &clock_seq[1],
104 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
105 status = NT_STATUS_OK;
108 if (!NT_STATUS_IS_OK(status)) {
109 return status;
112 guid->time_low = time_low;
113 guid->time_mid = time_mid;
114 guid->time_hi_and_version = time_hi_and_version;
115 guid->clock_seq[0] = clock_seq[0];
116 guid->clock_seq[1] = clock_seq[1];
117 for (i=0;i<6;i++) {
118 guid->node[i] = node[i];
121 return NT_STATUS_OK;
125 * generate a random GUID
127 _PUBLIC_ struct GUID GUID_random(void)
129 struct GUID guid;
131 generate_random_buffer((uint8_t *)&guid, sizeof(guid));
132 guid.clock_seq[0] = (guid.clock_seq[0] & 0x3F) | 0x80;
133 guid.time_hi_and_version = (guid.time_hi_and_version & 0x0FFF) | 0x4000;
135 return guid;
139 * generate an empty GUID
141 _PUBLIC_ struct GUID GUID_zero(void)
143 struct GUID guid;
145 ZERO_STRUCT(guid);
147 return guid;
150 _PUBLIC_ bool GUID_all_zero(const struct GUID *u)
152 if (u->time_low != 0 ||
153 u->time_mid != 0 ||
154 u->time_hi_and_version != 0 ||
155 u->clock_seq[0] != 0 ||
156 u->clock_seq[1] != 0 ||
157 !all_zero(u->node, 6)) {
158 return false;
160 return true;
163 _PUBLIC_ bool GUID_equal(const struct GUID *u1, const struct GUID *u2)
165 if (u1->time_low != u2->time_low ||
166 u1->time_mid != u2->time_mid ||
167 u1->time_hi_and_version != u2->time_hi_and_version ||
168 u1->clock_seq[0] != u2->clock_seq[0] ||
169 u1->clock_seq[1] != u2->clock_seq[1] ||
170 memcmp(u1->node, u2->node, 6) != 0) {
171 return false;
173 return true;
176 _PUBLIC_ int GUID_compare(const struct GUID *u1, const struct GUID *u2)
178 if (u1->time_low != u2->time_low) {
179 return u1->time_low - u2->time_low;
182 if (u1->time_mid != u2->time_mid) {
183 return u1->time_mid - u2->time_mid;
186 if (u1->time_hi_and_version != u2->time_hi_and_version) {
187 return u1->time_hi_and_version - u2->time_hi_and_version;
190 if (u1->clock_seq[0] != u2->clock_seq[0]) {
191 return u1->clock_seq[0] - u2->clock_seq[0];
194 if (u1->clock_seq[1] != u2->clock_seq[1]) {
195 return u1->clock_seq[1] - u2->clock_seq[1];
198 return memcmp(u1->node, u2->node, 6);
202 its useful to be able to display these in debugging messages
204 _PUBLIC_ char *GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
206 return talloc_asprintf(mem_ctx,
207 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
208 guid->time_low, guid->time_mid,
209 guid->time_hi_and_version,
210 guid->clock_seq[0],
211 guid->clock_seq[1],
212 guid->node[0], guid->node[1],
213 guid->node[2], guid->node[3],
214 guid->node[4], guid->node[5]);
217 _PUBLIC_ char *GUID_string2(TALLOC_CTX *mem_ctx, const struct GUID *guid)
219 char *ret, *s = GUID_string(mem_ctx, guid);
220 ret = talloc_asprintf(mem_ctx, "{%s}", s);
221 talloc_free(s);
222 return ret;
225 _PUBLIC_ char *NS_GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
227 return talloc_asprintf(mem_ctx,
228 "%08x-%04x%04x-%02x%02x%02x%02x-%02x%02x%02x%02x",
229 guid->time_low, guid->time_mid,
230 guid->time_hi_and_version,
231 guid->clock_seq[0],
232 guid->clock_seq[1],
233 guid->node[0], guid->node[1],
234 guid->node[2], guid->node[3],
235 guid->node[4], guid->node[5]);
238 _PUBLIC_ bool policy_handle_empty(struct policy_handle *h)
240 return (h->handle_type == 0 && GUID_all_zero(&h->uuid));