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"
27 #include "lib/util/util_str_hex.h"
29 build a NDR blob from a GUID
31 _PUBLIC_ NTSTATUS
GUID_to_ndr_blob(const struct GUID
*guid
, TALLOC_CTX
*mem_ctx
, DATA_BLOB
*b
)
33 enum ndr_err_code ndr_err
;
34 *b
= data_blob_talloc(mem_ctx
, NULL
, 16);
35 if (b
->data
== NULL
) {
36 return NT_STATUS_NO_MEMORY
;
38 ndr_err
= ndr_push_struct_into_fixed_blob(
39 b
, guid
, (ndr_push_flags_fn_t
)ndr_push_GUID
);
40 return ndr_map_error2ntstatus(ndr_err
);
45 build a GUID from a NDR data blob
47 _PUBLIC_ NTSTATUS
GUID_from_ndr_blob(const DATA_BLOB
*b
, struct GUID
*guid
)
49 enum ndr_err_code ndr_err
=
50 ndr_pull_struct_blob_all_noalloc(b
, guid
,
51 (ndr_pull_flags_fn_t
)ndr_pull_GUID
);
52 return ndr_map_error2ntstatus(ndr_err
);
57 build a GUID from a string
59 _PUBLIC_ NTSTATUS
GUID_from_data_blob(const DATA_BLOB
*s
, struct GUID
*guid
)
61 NTSTATUS status
= NT_STATUS_INVALID_PARAMETER
;
63 uint32_t time_mid
, time_hi_and_version
;
64 uint32_t clock_seq
[2];
68 DATA_BLOB blob16
= data_blob_const(buf16
, sizeof(buf16
));
71 if (s
->data
== NULL
) {
72 return NT_STATUS_INVALID_PARAMETER
;
78 status
= parse_guid_string((char *)s
->data
,
88 if (s
->data
[0] != '{' || s
->data
[37] != '}') {
92 status
= parse_guid_string((char *)s
->data
+ 1,
102 size_t rlen
= strhex_to_str((char *)blob16
.data
, blob16
.length
,
103 (const char *)s
->data
, s
->length
);
104 if (rlen
!= blob16
.length
) {
105 return NT_STATUS_INVALID_PARAMETER
;
109 return GUID_from_ndr_blob(s
, guid
);
112 return GUID_from_ndr_blob(s
, guid
);
114 status
= NT_STATUS_INVALID_PARAMETER
;
118 if (!NT_STATUS_IS_OK(status
)) {
122 guid
->time_low
= time_low
;
123 guid
->time_mid
= time_mid
;
124 guid
->time_hi_and_version
= time_hi_and_version
;
125 guid
->clock_seq
[0] = clock_seq
[0];
126 guid
->clock_seq
[1] = clock_seq
[1];
128 guid
->node
[i
] = node
[i
];
135 build a GUID from a string
137 _PUBLIC_ NTSTATUS
GUID_from_string(const char *s
, struct GUID
*guid
)
139 DATA_BLOB blob
= data_blob_string_const(s
);
140 return GUID_from_data_blob(&blob
, guid
);
144 * generate a random GUID
146 _PUBLIC_
struct GUID
GUID_random(void)
150 generate_random_buffer((uint8_t *)&guid
, sizeof(guid
));
151 guid
.clock_seq
[0] = (guid
.clock_seq
[0] & 0x3F) | 0x80;
152 guid
.time_hi_and_version
= (guid
.time_hi_and_version
& 0x0FFF) | 0x4000;
158 * generate an empty GUID
160 _PUBLIC_
struct GUID
GUID_zero(void)
169 _PUBLIC_
bool GUID_all_zero(const struct GUID
*u
)
171 if (u
->time_low
!= 0 ||
173 u
->time_hi_and_version
!= 0 ||
174 u
->clock_seq
[0] != 0 ||
175 u
->clock_seq
[1] != 0 ||
176 !all_zero(u
->node
, 6)) {
182 _PUBLIC_
bool GUID_equal(const struct GUID
*u1
, const struct GUID
*u2
)
184 return (GUID_compare(u1
, u2
) == 0);
187 _PUBLIC_
int GUID_compare(const struct GUID
*u1
, const struct GUID
*u2
)
189 if (u1
->time_low
!= u2
->time_low
) {
190 return u1
->time_low
> u2
->time_low
? 1 : -1;
193 if (u1
->time_mid
!= u2
->time_mid
) {
194 return u1
->time_mid
> u2
->time_mid
? 1 : -1;
197 if (u1
->time_hi_and_version
!= u2
->time_hi_and_version
) {
198 return u1
->time_hi_and_version
> u2
->time_hi_and_version
? 1 : -1;
201 if (u1
->clock_seq
[0] != u2
->clock_seq
[0]) {
202 return u1
->clock_seq
[0] > u2
->clock_seq
[0] ? 1 : -1;
205 if (u1
->clock_seq
[1] != u2
->clock_seq
[1]) {
206 return u1
->clock_seq
[1] > u2
->clock_seq
[1] ? 1 : -1;
209 return memcmp(u1
->node
, u2
->node
, 6);
213 its useful to be able to display these in debugging messages
215 _PUBLIC_
char *GUID_string(TALLOC_CTX
*mem_ctx
, const struct GUID
*guid
)
217 struct GUID_txt_buf buf
;
218 return talloc_strdup(mem_ctx
, GUID_buf_string(guid
, &buf
));
222 * Does the same without allocating memory, using the structure buffer.
223 * Useful for debug messages, so that you do not have to talloc_free the result
225 _PUBLIC_
char* GUID_buf_string(const struct GUID
*guid
,
226 struct GUID_txt_buf
*dst
)
231 snprintf(dst
->buf
, sizeof(dst
->buf
),
232 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
233 guid
->time_low
, guid
->time_mid
,
234 guid
->time_hi_and_version
,
237 guid
->node
[0], guid
->node
[1],
238 guid
->node
[2], guid
->node
[3],
239 guid
->node
[4], guid
->node
[5]);
243 _PUBLIC_
char *GUID_string2(TALLOC_CTX
*mem_ctx
, const struct GUID
*guid
)
245 char *ret
, *s
= GUID_string(mem_ctx
, guid
);
246 ret
= talloc_asprintf(mem_ctx
, "{%s}", s
);
251 _PUBLIC_
char *GUID_hexstring(TALLOC_CTX
*mem_ctx
, const struct GUID
*guid
)
258 tmp_mem
= talloc_new(mem_ctx
);
262 status
= GUID_to_ndr_blob(guid
, tmp_mem
, &guid_blob
);
263 if (!NT_STATUS_IS_OK(status
)) {
264 talloc_free(tmp_mem
);
268 ret
= data_blob_hex_string_upper(mem_ctx
, &guid_blob
);
269 talloc_free(tmp_mem
);
273 _PUBLIC_
bool ndr_policy_handle_empty(const struct policy_handle
*h
)
275 return (h
->handle_type
== 0 && GUID_all_zero(&h
->uuid
));
278 _PUBLIC_
bool ndr_policy_handle_equal(const struct policy_handle
*hnd1
,
279 const struct policy_handle
*hnd2
)
281 if (!hnd1
|| !hnd2
) {
285 return (memcmp(hnd1
, hnd2
, sizeof(*hnd1
)) == 0);