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"
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
;
34 uint32_t time_mid
, time_hi_and_version
;
35 uint32_t clock_seq
[2];
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
)) {
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];
65 guid
->node
[i
] = node
[i
];
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
;
78 uint32_t time_mid
, time_hi_and_version
;
79 uint32_t clock_seq
[2];
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
)) {
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];
104 guid
->node
[i
] = node
[i
];
111 * generate a random GUID
113 struct GUID
GUID_random(void)
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;
125 * generate an empty GUID
127 _PUBLIC_
struct GUID
GUID_zero(void)
136 _PUBLIC_
bool GUID_all_zero(const struct GUID
*u
)
138 if (u
->time_low
!= 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)) {
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) {
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
,
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
);
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
,
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
));