[GLUE] Rsync SAMBA_3_0 SVN r25598 in order to create the v3-0-test branch.
[Samba.git] / source / librpc / ndr / ndr_misc.c
blob54face8d7a03d3e38fb64b7272f74f4c7cc34d92
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 2 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, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "includes.h"
28 NTSTATUS ndr_push_GUID(struct ndr_push *ndr, int ndr_flags, const struct GUID *r)
30 if (ndr_flags & NDR_SCALARS) {
31 NDR_CHECK(ndr_push_align(ndr, 4));
32 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, r->time_low));
33 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, r->time_mid));
34 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, r->time_hi_and_version));
35 NDR_CHECK(ndr_push_array_uint8(ndr, NDR_SCALARS, r->clock_seq, 2));
36 NDR_CHECK(ndr_push_array_uint8(ndr, NDR_SCALARS, r->node, 6));
38 if (ndr_flags & NDR_BUFFERS) {
40 return NT_STATUS_OK;
43 NTSTATUS ndr_pull_GUID(struct ndr_pull *ndr, int ndr_flags, struct GUID *r)
45 if (ndr_flags & NDR_SCALARS) {
46 NDR_CHECK(ndr_pull_align(ndr, 4));
47 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &r->time_low));
48 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &r->time_mid));
49 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &r->time_hi_and_version));
50 NDR_CHECK(ndr_pull_array_uint8(ndr, NDR_SCALARS, r->clock_seq, 2));
51 NDR_CHECK(ndr_pull_array_uint8(ndr, NDR_SCALARS, r->node, 6));
53 if (ndr_flags & NDR_BUFFERS) {
55 return NT_STATUS_OK;
58 size_t ndr_size_GUID(const struct GUID *r, int flags)
60 return ndr_size_struct(r, flags, (ndr_push_flags_fn_t)ndr_push_GUID);
63 /**
64 build a GUID from a string
66 NTSTATUS GUID_from_string(const char *s, struct GUID *guid)
68 NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
69 uint32_t time_low;
70 uint32_t time_mid, time_hi_and_version;
71 uint32_t clock_seq[2];
72 uint32_t node[6];
73 int i;
75 if (s == NULL) {
76 return NT_STATUS_INVALID_PARAMETER;
79 if (11 == sscanf(s, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
80 &time_low, &time_mid, &time_hi_and_version,
81 &clock_seq[0], &clock_seq[1],
82 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
83 status = NT_STATUS_OK;
84 } else if (11 == sscanf(s, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
85 &time_low, &time_mid, &time_hi_and_version,
86 &clock_seq[0], &clock_seq[1],
87 &node[0], &node[1], &node[2], &node[3], &node[4], &node[5])) {
88 status = NT_STATUS_OK;
91 if (!NT_STATUS_IS_OK(status)) {
92 return status;
95 guid->time_low = time_low;
96 guid->time_mid = time_mid;
97 guid->time_hi_and_version = time_hi_and_version;
98 guid->clock_seq[0] = clock_seq[0];
99 guid->clock_seq[1] = clock_seq[1];
100 for (i=0;i<6;i++) {
101 guid->node[i] = node[i];
104 return NT_STATUS_OK;
108 * generate a random GUID
110 struct GUID GUID_random(void)
112 struct GUID guid;
114 generate_random_buffer((uint8_t *)&guid, sizeof(guid));
115 guid.clock_seq[0] = (guid.clock_seq[0] & 0x3F) | 0x80;
116 guid.time_hi_and_version = (guid.time_hi_and_version & 0x0FFF) | 0x4000;
118 return guid;
122 * generate an empty GUID
124 struct GUID GUID_zero(void)
126 struct GUID guid;
128 ZERO_STRUCT(guid);
130 return guid;
134 * see if a range of memory is all zero. A NULL pointer is considered
135 * to be all zero
137 BOOL all_zero(const uint8_t *ptr, size_t size)
139 int i;
140 if (!ptr) return True;
141 for (i=0;i<size;i++) {
142 if (ptr[i]) return False;
144 return True;
148 BOOL GUID_all_zero(const struct GUID *u)
150 if (u->time_low != 0 ||
151 u->time_mid != 0 ||
152 u->time_hi_and_version != 0 ||
153 u->clock_seq[0] != 0 ||
154 u->clock_seq[1] != 0 ||
155 !all_zero(u->node, 6)) {
156 return False;
158 return True;
161 BOOL GUID_equal(const struct GUID *u1, const struct GUID *u2)
163 if (u1->time_low != u2->time_low ||
164 u1->time_mid != u2->time_mid ||
165 u1->time_hi_and_version != u2->time_hi_and_version ||
166 u1->clock_seq[0] != u2->clock_seq[0] ||
167 u1->clock_seq[1] != u2->clock_seq[1] ||
168 memcmp(u1->node, u2->node, 6) != 0) {
169 return False;
171 return True;
175 its useful to be able to display these in debugging messages
177 char *GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
179 return talloc_asprintf(mem_ctx,
180 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
181 guid->time_low, guid->time_mid,
182 guid->time_hi_and_version,
183 guid->clock_seq[0],
184 guid->clock_seq[1],
185 guid->node[0], guid->node[1],
186 guid->node[2], guid->node[3],
187 guid->node[4], guid->node[5]);
190 char *GUID_string2(TALLOC_CTX *mem_ctx, const struct GUID *guid)
192 char *ret, *s = GUID_string(mem_ctx, guid);
193 ret = talloc_asprintf(mem_ctx, "{%s}", s);
194 talloc_free(s);
195 return ret;
198 void ndr_print_GUID(struct ndr_print *ndr, const char *name, const struct GUID *guid)
200 ndr->print(ndr, "%-25s: %s", name, GUID_string(ndr, guid));
203 BOOL policy_handle_empty(struct policy_handle *h)
205 return (h->handle_type == 0 && GUID_all_zero(&h->uuid));
208 NTSTATUS ndr_push_policy_handle(struct ndr_push *ndr, int ndr_flags, const struct policy_handle *r)
210 if (ndr_flags & NDR_SCALARS) {
211 NDR_CHECK(ndr_push_align(ndr, 4));
212 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, r->handle_type));
213 NDR_CHECK(ndr_push_GUID(ndr, NDR_SCALARS, &r->uuid));
215 if (ndr_flags & NDR_BUFFERS) {
217 return NT_STATUS_OK;
220 NTSTATUS ndr_pull_policy_handle(struct ndr_pull *ndr, int ndr_flags, struct policy_handle *r)
222 if (ndr_flags & NDR_SCALARS) {
223 NDR_CHECK(ndr_pull_align(ndr, 4));
224 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &r->handle_type));
225 NDR_CHECK(ndr_pull_GUID(ndr, NDR_SCALARS, &r->uuid));
227 if (ndr_flags & NDR_BUFFERS) {
229 return NT_STATUS_OK;
232 void ndr_print_policy_handle(struct ndr_print *ndr, const char *name, const struct policy_handle *r)
234 ndr_print_struct(ndr, name, "policy_handle");
235 ndr->depth++;
236 ndr_print_uint32(ndr, "handle_type", r->handle_type);
237 ndr_print_GUID(ndr, "uuid", &r->uuid);
238 ndr->depth--;
241 NTSTATUS ndr_push_server_id(struct ndr_push *ndr, int ndr_flags, const struct server_id *r)
243 if (ndr_flags & NDR_SCALARS) {
244 NDR_CHECK(ndr_push_align(ndr, 4));
245 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS,
246 (uint32_t)r->id.pid));
248 if (ndr_flags & NDR_BUFFERS) {
250 return NT_STATUS_OK;
253 NTSTATUS ndr_pull_server_id(struct ndr_pull *ndr, int ndr_flags, struct server_id *r)
255 if (ndr_flags & NDR_SCALARS) {
256 uint32_t pid;
257 NDR_CHECK(ndr_pull_align(ndr, 4));
258 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &pid));
259 r->id.pid = (pid_t)pid;
261 if (ndr_flags & NDR_BUFFERS) {
263 return NT_STATUS_OK;
266 void ndr_print_server_id(struct ndr_print *ndr, const char *name, const struct server_id *r)
268 ndr_print_struct(ndr, name, "server_id");
269 ndr->depth++;
270 ndr_print_uint32(ndr, "id", (uint32_t)r->id.pid);
271 ndr->depth--;