RIP BOOL. Convert BOOL -> bool. I found a few interesting
[amitay/samba.git] / source3 / librpc / ndr / ndr_misc.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    UUID/GUID/policy_handle functions
5
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.
10    
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.
15    
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.
20    
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/>.
23 */
24
25 #include "includes.h"
26
27 NTSTATUS ndr_push_GUID(struct ndr_push *ndr, int ndr_flags, const struct GUID *r)
28 {
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));
36         }
37         if (ndr_flags & NDR_BUFFERS) {
38         }
39         return NT_STATUS_OK;
40 }
41
42 NTSTATUS ndr_pull_GUID(struct ndr_pull *ndr, int ndr_flags, struct GUID *r)
43 {
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));
51         }
52         if (ndr_flags & NDR_BUFFERS) {
53         }
54         return NT_STATUS_OK;
55 }
56
57 size_t ndr_size_GUID(const struct GUID *r, int flags)
58 {
59         return ndr_size_struct(r, flags, (ndr_push_flags_fn_t)ndr_push_GUID);
60 }
61
62 /**
63   build a GUID from a string
64 */
65 NTSTATUS GUID_from_string(const char *s, struct GUID *guid)
66 {
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;
73
74         if (s == NULL) {
75                 return NT_STATUS_INVALID_PARAMETER;
76         }
77
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;
88         }
89
90         if (!NT_STATUS_IS_OK(status)) {
91                 return status;
92         }
93
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];
101         }
102
103         return NT_STATUS_OK;
104 }
105
106 /**
107  * generate a random GUID
108  */
109 struct GUID GUID_random(void)
110 {
111         struct GUID guid;
112
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;
116
117         return guid;
118 }
119
120 /**
121  * generate an empty GUID 
122  */
123 struct GUID GUID_zero(void)
124 {
125         struct GUID guid;
126
127         ZERO_STRUCT(guid);
128
129         return guid;
130 }
131
132 /**
133  * see if a range of memory is all zero. A NULL pointer is considered
134  * to be all zero 
135  */
136 bool all_zero(const uint8_t *ptr, size_t size)
137 {
138         int i;
139         if (!ptr) return True;
140         for (i=0;i<size;i++) {
141                 if (ptr[i]) return False;
142         }
143         return True;
144 }
145
146
147 bool GUID_all_zero(const struct GUID *u)
148 {
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;
156         }
157         return True;
158 }
159
160 bool GUID_equal(const struct GUID *u1, const struct GUID *u2)
161 {
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;
169         }
170         return True;
171 }
172
173 /**
174   its useful to be able to display these in debugging messages
175 */
176 char *GUID_string(TALLOC_CTX *mem_ctx, const struct GUID *guid)
177 {
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]);
187 }
188
189 char *GUID_string2(TALLOC_CTX *mem_ctx, const struct GUID *guid)
190 {
191         char *ret, *s = GUID_string(mem_ctx, guid);
192         ret = talloc_asprintf(mem_ctx, "{%s}", s);
193         talloc_free(s);
194         return ret;
195 }
196
197 void ndr_print_GUID(struct ndr_print *ndr, const char *name, const struct GUID *guid)
198 {
199         ndr->print(ndr, "%-25s: %s", name, GUID_string(ndr, guid));
200 }
201
202 bool policy_handle_empty(struct policy_handle *h) 
203 {
204         return (h->handle_type == 0 && GUID_all_zero(&h->uuid));
205 }
206
207 NTSTATUS ndr_push_policy_handle(struct ndr_push *ndr, int ndr_flags, const struct policy_handle *r)
208 {
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));
213         }
214         if (ndr_flags & NDR_BUFFERS) {
215         }
216         return NT_STATUS_OK;
217 }
218
219 NTSTATUS ndr_pull_policy_handle(struct ndr_pull *ndr, int ndr_flags, struct policy_handle *r)
220 {
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));
225         }
226         if (ndr_flags & NDR_BUFFERS) {
227         }
228         return NT_STATUS_OK;
229 }
230
231 void ndr_print_policy_handle(struct ndr_print *ndr, const char *name, const struct policy_handle *r)
232 {
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--;
238 }
239
240 NTSTATUS ndr_push_server_id(struct ndr_push *ndr, int ndr_flags, const struct server_id *r)
241 {
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
250         }
251         if (ndr_flags & NDR_BUFFERS) {
252         }
253         return NT_STATUS_OK;
254 }
255
256 NTSTATUS ndr_pull_server_id(struct ndr_pull *ndr, int ndr_flags, struct server_id *r)
257 {
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;
266         }
267         if (ndr_flags & NDR_BUFFERS) {
268         }
269         return NT_STATUS_OK;
270 }
271
272 void ndr_print_server_id(struct ndr_print *ndr, const char *name, const struct server_id *r)
273 {
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--;
281 }