r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[Samba/bb.git] / source / librpc / ndr / ndr_misc.c
blobe66a8b9a5d34d8578623ec538f9e2491179b6502
1 /*
2 Unix SMB/CIFS implementation.
4 UUID/GUID/policy_handle functions
6 Copyright (C) Theodore Ts'o 1996, 1997,
7 Copyright (C) Jim McDonough 2002.
8 Copyright (C) Andrew Tridgell 2003.
9 Copyright (C) Stefan (metze) Metzmacher 2004.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
27 NTSTATUS ndr_push_GUID(struct ndr_push *ndr, int ndr_flags, const struct GUID *r)
29 if (ndr_flags & NDR_SCALARS) {
30 NDR_CHECK(ndr_push_align(ndr, 4));
31 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, r->time_low));
32 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, r->time_mid));
33 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, r->time_hi_and_version));
34 NDR_CHECK(ndr_push_array_uint8(ndr, NDR_SCALARS, r->clock_seq, 2));
35 NDR_CHECK(ndr_push_array_uint8(ndr, NDR_SCALARS, r->node, 6));
37 if (ndr_flags & NDR_BUFFERS) {
39 return NT_STATUS_OK;
42 NTSTATUS ndr_pull_GUID(struct ndr_pull *ndr, int ndr_flags, struct GUID *r)
44 if (ndr_flags & NDR_SCALARS) {
45 NDR_CHECK(ndr_pull_align(ndr, 4));
46 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &r->time_low));
47 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &r->time_mid));
48 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &r->time_hi_and_version));
49 NDR_CHECK(ndr_pull_array_uint8(ndr, NDR_SCALARS, r->clock_seq, 2));
50 NDR_CHECK(ndr_pull_array_uint8(ndr, NDR_SCALARS, r->node, 6));
52 if (ndr_flags & NDR_BUFFERS) {
54 return NT_STATUS_OK;
57 size_t ndr_size_GUID(const struct GUID *r, int flags)
59 return ndr_size_struct(r, flags, (ndr_push_flags_fn_t)ndr_push_GUID);
62 /**
63 build a GUID from a string
65 NTSTATUS GUID_from_string(const char *s, struct GUID *guid)
67 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
68 uint32_t time_low;
69 uint32_t time_mid, time_hi_and_version;
70 uint32_t clock_seq[2];
71 uint32_t node[6];
72 int i;
74 if (s == NULL) {
75 return NT_STATUS_INVALID_PARAMETER;
78 if (11 == sscanf(s, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
79 &time_low, &time_mid, &time_hi_and_version,
80 &clock_seq[0], &clock_seq[1],
81 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
82 status = NT_STATUS_OK;
83 } else if (11 == sscanf(s, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
84 &time_low, &time_mid, &time_hi_and_version,
85 &clock_seq[0], &clock_seq[1],
86 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
87 status = NT_STATUS_OK;
90 if (!NT_STATUS_IS_OK(status)) {
91 return status;
94 guid->time_low = time_low;
95 guid->time_mid = time_mid;
96 guid->time_hi_and_version = time_hi_and_version;
97 guid->clock_seq[0] = clock_seq[0];
98 guid->clock_seq[1] = clock_seq[1];
99 for (i=0;i<6;i++) {
100 guid->node[i] = node[i];
103 return NT_STATUS_OK;
107 * generate a random GUID
109 struct GUID GUID_random(void)
111 struct GUID guid;
113 generate_random_buffer((uint8_t *)&guid, sizeof(guid));
114 guid.clock_seq[0] = (guid.clock_seq[0] & 0x3F) | 0x80;
115 guid.time_hi_and_version = (guid.time_hi_and_version & 0x0FFF) | 0x4000;
117 return guid;
121 * generate an empty GUID
123 struct GUID GUID_zero(void)
125 struct GUID guid;
127 ZERO_STRUCT(guid);
129 return guid;
133 * see if a range of memory is all zero. A NULL pointer is considered
134 * to be all zero
136 BOOL all_zero(const uint8_t *ptr, size_t size)
138 int i;
139 if (!ptr) return True;
140 for (i=0;i<size;i++) {
141 if (ptr[i]) return False;
143 return True;
147 BOOL GUID_all_zero(const struct GUID *u)
149 if (u->time_low != 0 ||
150 u->time_mid != 0 ||
151 u->time_hi_and_version != 0 ||
152 u->clock_seq[0] != 0 ||
153 u->clock_seq[1] != 0 ||
154 !all_zero(u->node, 6)) {
155 return False;
157 return True;
160 BOOL GUID_equal(const struct GUID *u1, const struct GUID *u2)
162 if (u1->time_low != u2->time_low ||
163 u1->time_mid != u2->time_mid ||
164 u1->time_hi_and_version != u2->time_hi_and_version ||
165 u1->clock_seq[0] != u2->clock_seq[0] ||
166 u1->clock_seq[1] != u2->clock_seq[1] ||
167 memcmp(u1->node, u2->node, 6) != 0) {
168 return False;
170 return True;
174 its useful to be able to display these in debugging messages
176 char *GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
178 return talloc_asprintf(mem_ctx,
179 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
180 guid->time_low, guid->time_mid,
181 guid->time_hi_and_version,
182 guid->clock_seq[0],
183 guid->clock_seq[1],
184 guid->node[0], guid->node[1],
185 guid->node[2], guid->node[3],
186 guid->node[4], guid->node[5]);
189 char *GUID_string2(TALLOC_CTX *mem_ctx, const struct GUID *guid)
191 char *ret, *s = GUID_string(mem_ctx, guid);
192 ret = talloc_asprintf(mem_ctx, "{%s}", s);
193 talloc_free(s);
194 return ret;
197 void ndr_print_GUID(struct ndr_print *ndr, const char *name, const struct GUID *guid)
199 ndr->print(ndr, "%-25s: %s", name, GUID_string(ndr, guid));
202 BOOL policy_handle_empty(struct policy_handle *h)
204 return (h->handle_type == 0 && GUID_all_zero(&h->uuid));
207 NTSTATUS ndr_push_policy_handle(struct ndr_push *ndr, int ndr_flags, const struct policy_handle *r)
209 if (ndr_flags & NDR_SCALARS) {
210 NDR_CHECK(ndr_push_align(ndr, 4));
211 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, r->handle_type));
212 NDR_CHECK(ndr_push_GUID(ndr, NDR_SCALARS, &r->uuid));
214 if (ndr_flags & NDR_BUFFERS) {
216 return NT_STATUS_OK;
219 NTSTATUS ndr_pull_policy_handle(struct ndr_pull *ndr, int ndr_flags, struct policy_handle *r)
221 if (ndr_flags & NDR_SCALARS) {
222 NDR_CHECK(ndr_pull_align(ndr, 4));
223 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &r->handle_type));
224 NDR_CHECK(ndr_pull_GUID(ndr, NDR_SCALARS, &r->uuid));
226 if (ndr_flags & NDR_BUFFERS) {
228 return NT_STATUS_OK;
231 void ndr_print_policy_handle(struct ndr_print *ndr, const char *name, const struct policy_handle *r)
233 ndr_print_struct(ndr, name, "policy_handle");
234 ndr->depth++;
235 ndr_print_uint32(ndr, "handle_type", r->handle_type);
236 ndr_print_GUID(ndr, "uuid", &r->uuid);
237 ndr->depth--;
240 NTSTATUS ndr_push_server_id(struct ndr_push *ndr, int ndr_flags, const struct server_id *r)
242 if (ndr_flags & NDR_SCALARS) {
243 NDR_CHECK(ndr_push_align(ndr, 4));
244 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS,
245 (uint32_t)r->pid));
246 #ifdef CLUSTER_SUPPORT
247 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS,
248 (uint32_t)r->vnn));
249 #endif
251 if (ndr_flags & NDR_BUFFERS) {
253 return NT_STATUS_OK;
256 NTSTATUS ndr_pull_server_id(struct ndr_pull *ndr, int ndr_flags, struct server_id *r)
258 if (ndr_flags & NDR_SCALARS) {
259 uint32_t pid;
260 NDR_CHECK(ndr_pull_align(ndr, 4));
261 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &pid));
262 #ifdef CLUSTER_SUPPORT
263 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &r->vnn));
264 #endif
265 r->pid = (pid_t)pid;
267 if (ndr_flags & NDR_BUFFERS) {
269 return NT_STATUS_OK;
272 void ndr_print_server_id(struct ndr_print *ndr, const char *name, const struct server_id *r)
274 ndr_print_struct(ndr, name, "server_id");
275 ndr->depth++;
276 ndr_print_uint32(ndr, "id", (uint32_t)r->pid);
277 #ifdef CLUSTER_SUPPORT
278 ndr_print_uint32(ndr, "vnn", (uint32_t)r->vnn);
279 #endif
280 ndr->depth--;