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/>.
27 build a GUID from a string
29 _PUBLIC_ NTSTATUS GUID_from_string(const char *s, struct GUID *guid)
31 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
33 uint32_t time_mid, time_hi_and_version;
34 uint32_t clock_seq[2];
39 return NT_STATUS_INVALID_PARAMETER;
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;
54 if (!NT_STATUS_IS_OK(status)) {
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];
64 guid->node[i] = node[i];
71 build a GUID from a string
73 _PUBLIC_ NTSTATUS NS_GUID_from_string(const char *s, struct GUID *guid)
75 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
77 uint32_t time_mid, time_hi_and_version;
78 uint32_t clock_seq[2];
83 return NT_STATUS_INVALID_PARAMETER;
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;
93 if (!NT_STATUS_IS_OK(status)) {
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];
103 guid->node[i] = node[i];
110 * generate a random GUID
112 struct GUID GUID_random(void)
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;
124 * generate an empty GUID
126 _PUBLIC_ struct GUID GUID_zero(void)
135 _PUBLIC_ BOOL GUID_all_zero(const struct GUID *u)
137 if (u->time_low != 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)) {
148 _PUBLIC_ BOOL GUID_equal(const struct GUID *u1, const struct GUID *u2)
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) {
161 _PUBLIC_ int GUID_compare(const struct GUID *u1, const struct GUID *u2)
163 if (u1->time_low != u2->time_low) {
164 return u1->time_low - u2->time_low;
167 if (u1->time_mid != u2->time_mid) {
168 return u1->time_mid - u2->time_mid;
171 if (u1->time_hi_and_version != u2->time_hi_and_version) {
172 return u1->time_hi_and_version - u2->time_hi_and_version;
175 if (u1->clock_seq[0] != u2->clock_seq[0]) {
176 return u1->clock_seq[0] - u2->clock_seq[0];
179 if (u1->clock_seq[1] != u2->clock_seq[1]) {
180 return u1->clock_seq[1] - u2->clock_seq[1];
183 return memcmp(u1->node, u2->node, 6);
187 its useful to be able to display these in debugging messages
189 _PUBLIC_ char *GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
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,
197 guid->node[0], guid->node[1],
198 guid->node[2], guid->node[3],
199 guid->node[4], guid->node[5]);
202 _PUBLIC_ char *GUID_string2(TALLOC_CTX *mem_ctx, const struct GUID *guid)
204 char *ret, *s = GUID_string(mem_ctx, guid);
205 ret = talloc_asprintf(mem_ctx, "{%s}", s);
210 _PUBLIC_ char *NS_GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
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,
218 guid->node[0], guid->node[1],
219 guid->node[2], guid->node[3],
220 guid->node[4], guid->node[5]);
223 _PUBLIC_ BOOL policy_handle_empty(struct policy_handle *h)
225 return (h->handle_type == 0 && GUID_all_zero(&h->uuid));