start using automatic union printing
[gd/samba-autobuild/.git] / source / librpc / ndr / ndr_basic.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    routines for marshalling/unmarshalling basic types
5
6    Copyright (C) Andrew Tridgell 2003
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 #define NDR_PULL_NEED_BYTES(ndr, n) do { \
26         if ((n) > ndr->data_size || ndr->offset + (n) > ndr->data_size) { \
27                 return NT_STATUS_BUFFER_TOO_SMALL; \
28         } \
29 } while(0)
30
31 #define NDR_PULL_ALIGN(ndr, n) do { \
32         ndr->offset = (ndr->offset + (n-1)) & ~(n-1); \
33         if (ndr->offset >= ndr->data_size) { \
34                 return NT_STATUS_BUFFER_TOO_SMALL; \
35         } \
36 } while(0)
37
38 /*
39   parse a uint8
40 */
41 NTSTATUS ndr_pull_uint8(struct ndr_pull *ndr, uint8 *v)
42 {
43         NDR_PULL_NEED_BYTES(ndr, 1);
44         *v = CVAL(ndr->data, ndr->offset);
45         ndr->offset += 1;
46         return NT_STATUS_OK;
47 }
48
49
50 /*
51   parse a uint16
52 */
53 NTSTATUS ndr_pull_uint16(struct ndr_pull *ndr, uint16 *v)
54 {
55         NDR_PULL_ALIGN(ndr, 2);
56         NDR_PULL_NEED_BYTES(ndr, 2);
57         if (ndr->flags & LIBNDR_FLAG_BIGENDIAN) {
58                 *v = RSVAL(ndr->data, ndr->offset);
59         } else {
60                 *v = SVAL(ndr->data, ndr->offset);
61         }
62         ndr->offset += 2;
63         return NT_STATUS_OK;
64 }
65
66
67 /*
68   parse a uint32
69 */
70 NTSTATUS ndr_pull_uint32(struct ndr_pull *ndr, uint32 *v)
71 {
72         NDR_PULL_ALIGN(ndr, 4);
73         NDR_PULL_NEED_BYTES(ndr, 4);
74         if (ndr->flags & LIBNDR_FLAG_BIGENDIAN) {
75                 *v = RIVAL(ndr->data, ndr->offset);
76         } else {
77                 *v = IVAL(ndr->data, ndr->offset);
78         }
79         ndr->offset += 4;
80         return NT_STATUS_OK;
81 }
82
83 /*
84   pull a NTSTATUS
85 */
86 NTSTATUS ndr_pull_NTSTATUS(struct ndr_pull *ndr, NTSTATUS *status)
87 {
88         uint32 v;
89         NDR_CHECK(ndr_pull_uint32(ndr, &v));
90         *status = NT_STATUS(v);
91         return NT_STATUS_OK;
92 }
93
94 /*
95   parse a set of bytes
96 */
97 NTSTATUS ndr_pull_bytes(struct ndr_pull *ndr, char *data, uint32 n)
98 {
99         NDR_PULL_NEED_BYTES(ndr, n);
100         memcpy(data, ndr->data + ndr->offset, n);
101         ndr->offset += n;
102         return NT_STATUS_OK;
103 }
104
105 /*
106   pull an array of uint8
107 */
108 NTSTATUS ndr_pull_array_uint8(struct ndr_pull *ndr, char *data, uint32 n)
109 {
110         uint32 len;
111         NDR_CHECK(ndr_pull_uint32(ndr, &len));
112         if (len != n) {
113                 return NT_STATUS_INVALID_PARAMETER;
114         }       
115         return ndr_pull_bytes(ndr, data, len);
116 }
117
118
119 /*
120   parse a GUID
121 */
122 NTSTATUS ndr_pull_guid(struct ndr_pull *ndr, GUID *guid)
123 {
124         int i;
125         NDR_PULL_NEED_BYTES(ndr, GUID_SIZE);
126         for (i=0;i<GUID_SIZE;i++) {
127                 guid->info[i] = CVAL(ndr->data, ndr->offset + i);
128         }
129         ndr->offset += i;
130         return NT_STATUS_OK;
131 }
132
133
134 #define NDR_PUSH_NEED_BYTES(ndr, n) NDR_CHECK(ndr_push_expand(ndr, ndr->offset+(n)))
135
136 #define NDR_PUSH_ALIGN(ndr, n) do { \
137         uint32 _pad = (ndr->offset & (n-1)); \
138         while (_pad--) NDR_CHECK(ndr_push_uint8(ndr, 0)); \
139 } while(0)
140
141 /*
142   push a uint8
143 */
144 NTSTATUS ndr_push_uint8(struct ndr_push *ndr, uint8 v)
145 {
146         NDR_PUSH_NEED_BYTES(ndr, 1);
147         SCVAL(ndr->data, ndr->offset, v);
148         ndr->offset += 1;
149         return NT_STATUS_OK;
150 }
151
152 /*
153   push a uint16
154 */
155 NTSTATUS ndr_push_uint16(struct ndr_push *ndr, uint16 v)
156 {
157         NDR_PUSH_ALIGN(ndr, 2);
158         NDR_PUSH_NEED_BYTES(ndr, 2);
159         SSVAL(ndr->data, ndr->offset, v);
160         ndr->offset += 2;
161         return NT_STATUS_OK;
162 }
163
164 /*
165   push a uint32
166 */
167 NTSTATUS ndr_push_uint32(struct ndr_push *ndr, uint32 v)
168 {
169         NDR_PUSH_ALIGN(ndr, 4);
170         NDR_PUSH_NEED_BYTES(ndr, 4);
171         SIVAL(ndr->data, ndr->offset, v);
172         ndr->offset += 4;
173         return NT_STATUS_OK;
174 }
175
176 /*
177   align to a uint32
178 */
179 NTSTATUS ndr_push_align_uint32(struct ndr_push *ndr)
180 {
181         NDR_PUSH_ALIGN(ndr, 4);
182         return NT_STATUS_OK;
183 }
184
185 /*
186   push some bytes
187 */
188 NTSTATUS ndr_push_bytes(struct ndr_push *ndr, const char *data, uint32 n)
189 {
190         NDR_PUSH_NEED_BYTES(ndr, n);
191         memcpy(ndr->data + ndr->offset, data, n);
192         ndr->offset += n;
193         return NT_STATUS_OK;
194 }
195
196 /*
197   push an array of uint8
198 */
199 NTSTATUS ndr_push_array_uint8(struct ndr_push *ndr, const char *data, uint32 n)
200 {
201         NDR_CHECK(ndr_push_uint32(ndr, n));
202         return ndr_push_bytes(ndr, data, n);
203 }
204
205 /*
206   save the current position
207  */
208 void ndr_push_save(struct ndr_push *ndr, struct ndr_push_save *save)
209 {
210         save->offset = ndr->offset;
211 }
212
213 /*
214   restore the position
215  */
216 void ndr_push_restore(struct ndr_push *ndr, struct ndr_push_save *save)
217 {
218         ndr->offset = save->offset;
219 }
220
221 /*
222   this is used when a packet has a 4 byte length field. We remember the start position
223   and come back to it later to fill in the size
224 */
225 NTSTATUS ndr_push_length4_start(struct ndr_push *ndr, struct ndr_push_save *save)
226 {
227         NDR_PUSH_ALIGN(ndr, 4);
228         ndr_push_save(ndr, save);
229         return ndr_push_uint32(ndr, 0);
230 }
231
232 NTSTATUS ndr_push_length4_end(struct ndr_push *ndr, struct ndr_push_save *save)
233 {
234         struct ndr_push_save save2;
235         ndr_push_save(ndr, &save2);
236         ndr_push_restore(ndr, save);
237         NDR_CHECK(ndr_push_uint32(ndr, save2.offset - ndr->offset));
238         ndr_push_restore(ndr, &save2);
239         return NT_STATUS_OK;
240 }
241
242 /*
243   push a 1 if a pointer is non-NULL, otherwise 0
244 */
245 NTSTATUS ndr_push_ptr(struct ndr_push *ndr, const void *p)
246 {
247         return ndr_push_uint32(ndr, p?0xaabbccdd:0);
248 }
249
250 /*
251   push a comformant, variable ucs2 string onto the wire from a C string
252 */
253 NTSTATUS ndr_push_unistr(struct ndr_push *ndr, const char *s)
254 {
255         char *ws;
256         ssize_t len;
257         len = push_ucs2_talloc(ndr->mem_ctx, (smb_ucs2_t **)&ws, s);
258         if (len == -1) {
259                 return NT_STATUS_INVALID_PARAMETER;
260         }
261         NDR_CHECK(ndr_push_uint32(ndr, len/2));
262         NDR_CHECK(ndr_push_uint32(ndr, 0));
263         NDR_CHECK(ndr_push_uint32(ndr, len/2));
264         NDR_CHECK(ndr_push_bytes(ndr, ws, len));
265         return NT_STATUS_OK;
266 }
267
268 /*
269   push a comformant, variable ucs2 string onto the wire from a C string
270   don't send the null
271 */
272 NTSTATUS ndr_push_unistr_noterm(struct ndr_push *ndr, const char *s)
273 {
274         char *ws;
275         ssize_t len;
276         len = push_ucs2_talloc(ndr->mem_ctx, (smb_ucs2_t **)&ws, s);
277         if (len == -1) {
278                 return NT_STATUS_INVALID_PARAMETER;
279         }
280         NDR_CHECK(ndr_push_uint32(ndr, len/2 - 1));
281         NDR_CHECK(ndr_push_uint32(ndr, 0));
282         NDR_CHECK(ndr_push_uint32(ndr, len/2 - 1));
283         NDR_CHECK(ndr_push_bytes(ndr, ws, len - 2));
284         return NT_STATUS_OK;
285 }
286
287 /*
288   pull a comformant, variable ucs2 string from the wire into a C string
289 */
290 NTSTATUS ndr_pull_unistr(struct ndr_pull *ndr, const char **s)
291 {
292         char *ws, *as=NULL;
293         uint32 len1, ofs, len2;
294
295         NDR_CHECK(ndr_pull_uint32(ndr, &len1));
296         NDR_CHECK(ndr_pull_uint32(ndr, &ofs));
297         NDR_CHECK(ndr_pull_uint32(ndr, &len2));
298         if (len2 > len1) {
299                 return NT_STATUS_INVALID_PARAMETER;
300         }
301         NDR_ALLOC_N(ndr, ws, (len1+1)*2);
302         NDR_CHECK(ndr_pull_bytes(ndr, ws, len2*2));
303         SSVAL(ws, len1*2, 0);
304         SSVAL(ws, len2*2, 0);
305         pull_ucs2_talloc(ndr->mem_ctx, &as, (const smb_ucs2_t *)ws);
306         if (!as) {
307                 return NT_STATUS_INVALID_PARAMETER;
308         }
309         *s = as;
310         return NT_STATUS_OK;
311 }
312
313 /*
314   pull a comformant, variable ucs2 string from the wire into a C string
315 */
316 NTSTATUS ndr_pull_unistr_noterm(struct ndr_pull *ndr, const char **s)
317 {
318         return ndr_pull_unistr(ndr, s);
319 }
320
321 /*
322   push a 4 byte offset pointer, remembering where we are so we can later fill
323   in the correct value
324 */
325 NTSTATUS ndr_push_offset(struct ndr_push *ndr, struct ndr_push_save *ofs)
326 {
327         NDR_PUSH_ALIGN(ndr, 4);
328         ndr_push_save(ndr, ofs);
329         return ndr_push_uint32(ndr, 0);
330 }
331
332 /*
333   fill in the correct offset in a saved offset pointer
334   the offset is taken relative to 'save'
335 */
336 NTSTATUS ndr_push_offset_ptr(struct ndr_push *ndr, 
337                              struct ndr_push_save *ofs, 
338                              struct ndr_push_save *save)
339 {
340         struct ndr_push_save save2;
341         ndr_push_save(ndr, &save2);
342         ndr_push_restore(ndr, ofs);
343         NDR_CHECK(ndr_push_uint32(ndr, save2.offset - save->offset));
344         ndr_push_restore(ndr, &save2);
345         return NT_STATUS_OK;
346 }
347
348
349 /*
350   push a GUID
351 */
352 NTSTATUS ndr_push_guid(struct ndr_push *ndr, GUID *guid)
353 {
354         return ndr_push_bytes(ndr, guid->info, GUID_SIZE);
355 }
356
357 /*
358   push a NTTIME
359 */
360 NTSTATUS ndr_push_NTTIME(struct ndr_push *ndr, NTTIME t)
361 {
362         NDR_CHECK(ndr_push_uint32(ndr, t.low));
363         NDR_CHECK(ndr_push_uint32(ndr, t.high));
364         return NT_STATUS_OK;
365 }
366
367 /*
368   pull a NTTIME
369 */
370 NTSTATUS ndr_pull_NTTIME(struct ndr_pull *ndr, NTTIME *t)
371 {
372         NDR_CHECK(ndr_pull_uint32(ndr, &t->low));
373         NDR_CHECK(ndr_pull_uint32(ndr, &t->high));
374         return NT_STATUS_OK;
375 }
376
377
378 void ndr_print_struct(struct ndr_print *ndr, const char *name, const char *type)
379 {
380         ndr->print(ndr, "%s: struct %s", name, type);
381 }
382
383 void ndr_print_uint8(struct ndr_print *ndr, const char *name, uint8 v)
384 {
385         ndr->print(ndr, "%-25s: 0x%02x (%u)", name, v, v);
386 }
387
388 void ndr_print_uint16(struct ndr_print *ndr, const char *name, uint16 v)
389 {
390         ndr->print(ndr, "%-25s: 0x%04x (%u)", name, v, v);
391 }
392
393 void ndr_print_uint32(struct ndr_print *ndr, const char *name, uint32 v)
394 {
395         ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
396 }
397
398 void ndr_print_ptr(struct ndr_print *ndr, const char *name, const void *p)
399 {
400         if (p) {
401                 ndr->print(ndr, "%-25s: *", name);
402         } else {
403                 ndr->print(ndr, "%-25s: NULL", name);
404         }
405 }
406
407 void ndr_print_unistr_noterm(struct ndr_print *ndr, const char *name, const char *s)
408 {
409         ndr->print(ndr, "%-25s: '%s'", name, s);
410 }
411
412 void ndr_print_unistr(struct ndr_print *ndr, const char *name, const char *s)
413 {
414         ndr->print(ndr, "%-25s: '%s'", name, s);
415 }
416
417 void ndr_print_NTTIME(struct ndr_print *ndr, const char *name, NTTIME t)
418 {
419         ndr->print(ndr, "%-25s: %s", name, nt_time_string(ndr->mem_ctx, &t));
420 }
421
422 void ndr_print_union(struct ndr_print *ndr, const char *name, uint16 level, const char *type)
423 {
424         ndr->print(ndr, "%-25s: union %s(case %u)", name, type, level);
425 }
426
427 void ndr_print_bad_level(struct ndr_print *ndr, const char *name, uint16 level)
428 {
429         ndr->print(ndr, "UNKNOWN LEVEL %u", level);
430 }