r25000: Fix some more C++ compatibility warnings.
[gd/samba-autobuild/.git] / source4 / librpc / ndr / ndr.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    libndr interface
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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 /*
23   this provides the core routines for NDR parsing functions
24
25   see http://www.opengroup.org/onlinepubs/9629399/chap14.htm for details
26   of NDR encoding rules
27 */
28
29 #include "includes.h"
30 #include "librpc/ndr/libndr.h"
31 #include "lib/util/dlinklist.h"
32 #include "librpc/gen_ndr/dcerpc.h"
33
34 #define NDR_BASE_MARSHALL_SIZE 1024
35
36 /* this guid indicates NDR encoding in a protocol tower */
37 const struct ndr_syntax_id ndr_transfer_syntax = {
38   { 0x8a885d04, 0x1ceb, 0x11c9, {0x9f, 0xe8}, {0x08,0x00,0x2b,0x10,0x48,0x60} },
39   2
40 };
41
42 const struct ndr_syntax_id ndr64_transfer_syntax = {
43   { 0x71710533, 0xbeba, 0x4937, {0x83, 0x19}, {0xb5,0xdb,0xef,0x9c,0xcc,0x36} },
44   1
45 };
46
47 /*
48   work out the number of bytes needed to align on a n byte boundary
49 */
50 _PUBLIC_ size_t ndr_align_size(uint32_t offset, size_t n)
51 {
52         if ((offset & (n-1)) == 0) return 0;
53         return n - (offset & (n-1));
54 }
55
56 /*
57   initialise a ndr parse structure from a data blob
58 */
59 _PUBLIC_ struct ndr_pull *ndr_pull_init_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx)
60 {
61         struct ndr_pull *ndr;
62
63         ndr = talloc_zero(mem_ctx, struct ndr_pull);
64         if (!ndr) return NULL;
65         ndr->current_mem_ctx = mem_ctx;
66
67         ndr->data = blob->data;
68         ndr->data_size = blob->length;
69
70         return ndr;
71 }
72
73 /*
74   advance by 'size' bytes
75 */
76 _PUBLIC_ NTSTATUS ndr_pull_advance(struct ndr_pull *ndr, uint32_t size)
77 {
78         ndr->offset += size;
79         if (ndr->offset > ndr->data_size) {
80                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, 
81                                       "ndr_pull_advance by %u failed",
82                                       size);
83         }
84         return NT_STATUS_OK;
85 }
86
87 /*
88   set the parse offset to 'ofs'
89 */
90 static NTSTATUS ndr_pull_set_offset(struct ndr_pull *ndr, uint32_t ofs)
91 {
92         ndr->offset = ofs;
93         if (ndr->offset > ndr->data_size) {
94                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, 
95                                       "ndr_pull_set_offset %u failed",
96                                       ofs);
97         }
98         return NT_STATUS_OK;
99 }
100
101 /* save the offset/size of the current ndr state */
102 _PUBLIC_ void ndr_pull_save(struct ndr_pull *ndr, struct ndr_pull_save *save)
103 {
104         save->offset = ndr->offset;
105         save->data_size = ndr->data_size;
106 }
107
108 /* restore the size/offset of a ndr structure */
109 _PUBLIC_ void ndr_pull_restore(struct ndr_pull *ndr, struct ndr_pull_save *save)
110 {
111         ndr->offset = save->offset;
112         ndr->data_size = save->data_size;
113 }
114
115
116 /* create a ndr_push structure, ready for some marshalling */
117 _PUBLIC_ struct ndr_push *ndr_push_init_ctx(TALLOC_CTX *mem_ctx)
118 {
119         struct ndr_push *ndr;
120
121         ndr = talloc_zero(mem_ctx, struct ndr_push);
122         if (!ndr) {
123                 return NULL;
124         }
125
126         ndr->flags = 0;
127         ndr->alloc_size = NDR_BASE_MARSHALL_SIZE;
128         ndr->data = talloc_array(ndr, uint8_t, ndr->alloc_size);
129         if (!ndr->data) {
130                 return NULL;
131         }
132
133         return ndr;
134 }
135
136 /* return a DATA_BLOB structure for the current ndr_push marshalled data */
137 _PUBLIC_ DATA_BLOB ndr_push_blob(struct ndr_push *ndr)
138 {
139         DATA_BLOB blob;
140         blob.data = ndr->data;
141         blob.length = ndr->offset;
142         if (ndr->alloc_size > ndr->offset) {
143                 ndr->data[ndr->offset] = 0;
144         }
145         return blob;
146 }
147
148
149 /*
150   expand the available space in the buffer to ndr->offset + extra_size
151 */
152 _PUBLIC_ NTSTATUS ndr_push_expand(struct ndr_push *ndr, uint32_t extra_size)
153 {
154         uint32_t size = extra_size + ndr->offset;
155
156         if (size < ndr->offset) {
157                 /* extra_size overflowed the offset */
158                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, "Overflow in push_expand to %u",
159                                       size);
160         }
161
162         if (ndr->alloc_size > size) {
163                 return NT_STATUS_OK;
164         }
165
166         ndr->alloc_size += NDR_BASE_MARSHALL_SIZE;
167         if (size+1 > ndr->alloc_size) {
168                 ndr->alloc_size = size+1;
169         }
170         ndr->data = talloc_realloc(ndr, ndr->data, uint8_t, ndr->alloc_size);
171         if (!ndr->data) {
172                 return ndr_push_error(ndr, NDR_ERR_ALLOC, "Failed to push_expand to %u",
173                                       ndr->alloc_size);
174         }
175
176         return NT_STATUS_OK;
177 }
178
179 _PUBLIC_ void ndr_print_debug_helper(struct ndr_print *ndr, const char *format, ...) _PRINTF_ATTRIBUTE(2,3)
180 {
181         va_list ap;
182         char *s = NULL;
183         int i;
184
185         va_start(ap, format);
186         vasprintf(&s, format, ap);
187         va_end(ap);
188
189         for (i=0;i<ndr->depth;i++) {
190                 DEBUG(0,("    "));
191         }
192
193         DEBUG(0,("%s\n", s));
194         free(s);
195 }
196
197 _PUBLIC_ void ndr_print_string_helper(struct ndr_print *ndr, const char *format, ...) _PRINTF_ATTRIBUTE(2,3)
198 {
199         va_list ap;
200         int i;
201
202         for (i=0;i<ndr->depth;i++) {
203                 ndr->private_data = talloc_asprintf_append(
204                                         (char *)ndr->private_data, "    ");
205         }
206
207         va_start(ap, format);
208         ndr->private_data = talloc_vasprintf_append((char *)ndr->private_data, 
209                                                     format, ap);
210         va_end(ap);
211         ndr->private_data = talloc_asprintf_append((char *)ndr->private_data, 
212                                                    "\n");
213 }
214
215 /*
216   a useful helper function for printing idl structures via DEBUG()
217 */
218 _PUBLIC_ void ndr_print_debug(ndr_print_fn_t fn, const char *name, void *ptr)
219 {
220         struct ndr_print *ndr;
221
222         ndr = talloc_zero(NULL, struct ndr_print);
223         if (!ndr) return;
224         ndr->print = ndr_print_debug_helper;
225         ndr->depth = 1;
226         ndr->flags = 0;
227         fn(ndr, name, ptr);
228         talloc_free(ndr);
229 }
230
231 /*
232   a useful helper function for printing idl unions via DEBUG()
233 */
234 _PUBLIC_ void ndr_print_union_debug(ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
235 {
236         struct ndr_print *ndr;
237
238         ndr = talloc_zero(NULL, struct ndr_print);
239         if (!ndr) return;
240         ndr->print = ndr_print_debug_helper;
241         ndr->depth = 1;
242         ndr->flags = 0;
243         ndr_print_set_switch_value(ndr, ptr, level);
244         fn(ndr, name, ptr);
245         talloc_free(ndr);
246 }
247
248 /*
249   a useful helper function for printing idl function calls via DEBUG()
250 */
251 _PUBLIC_ void ndr_print_function_debug(ndr_print_function_t fn, const char *name, int flags, void *ptr)
252 {
253         struct ndr_print *ndr;
254
255         ndr = talloc_zero(NULL, struct ndr_print);
256         if (!ndr) return;
257         ndr->print = ndr_print_debug_helper;
258         ndr->depth = 1;
259         ndr->flags = 0;
260         fn(ndr, name, flags, ptr);
261         talloc_free(ndr);
262 }
263
264 /*
265   a useful helper function for printing idl structures to a string
266 */
267 _PUBLIC_ char *ndr_print_struct_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, void *ptr)
268 {
269         struct ndr_print *ndr;
270         char *ret = NULL;
271
272         ndr = talloc_zero(mem_ctx, struct ndr_print);
273         if (!ndr) return NULL;
274         ndr->private_data = talloc_strdup(ndr, "");
275         if (!ndr->private_data) {
276                 goto failed;
277         }
278         ndr->print = ndr_print_string_helper;
279         ndr->depth = 1;
280         ndr->flags = 0;
281         fn(ndr, name, ptr);
282         ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
283 failed:
284         talloc_free(ndr);
285         return ret;
286 }
287
288 /*
289   a useful helper function for printing idl unions to a string
290 */
291 _PUBLIC_ char *ndr_print_union_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
292 {
293         struct ndr_print *ndr;
294         char *ret = NULL;
295
296         ndr = talloc_zero(mem_ctx, struct ndr_print);
297         if (!ndr) return NULL;
298         ndr->private_data = talloc_strdup(ndr, "");
299         if (!ndr->private_data) {
300                 goto failed;
301         }
302         ndr->print = ndr_print_string_helper;
303         ndr->depth = 1;
304         ndr->flags = 0;
305         ndr_print_set_switch_value(ndr, ptr, level);
306         fn(ndr, name, ptr);
307         ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
308 failed:
309         talloc_free(ndr);
310         return ret;
311 }
312
313 /*
314   a useful helper function for printing idl function calls to a string
315 */
316 _PUBLIC_ char *ndr_print_function_string(TALLOC_CTX *mem_ctx,
317                                 ndr_print_function_t fn, const char *name, 
318                                 int flags, void *ptr)
319 {
320         struct ndr_print *ndr;
321         char *ret = NULL;
322
323         ndr = talloc_zero(mem_ctx, struct ndr_print);
324         if (!ndr) return NULL;
325         ndr->private_data = talloc_strdup(ndr, "");
326         if (!ndr->private_data) {
327                 goto failed;
328         }
329         ndr->print = ndr_print_string_helper;
330         ndr->depth = 1;
331         ndr->flags = 0;
332         fn(ndr, name, flags, ptr);
333         ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
334 failed:
335         talloc_free(ndr);
336         return ret;
337 }
338
339 _PUBLIC_ void ndr_set_flags(uint32_t *pflags, uint32_t new_flags)
340 {
341         /* the big/little endian flags are inter-dependent */
342         if (new_flags & LIBNDR_FLAG_LITTLE_ENDIAN) {
343                 (*pflags) &= ~LIBNDR_FLAG_BIGENDIAN;
344         }
345         if (new_flags & LIBNDR_FLAG_BIGENDIAN) {
346                 (*pflags) &= ~LIBNDR_FLAG_LITTLE_ENDIAN;
347         }
348         if (new_flags & LIBNDR_FLAG_REMAINING) {
349                 (*pflags) &= ~LIBNDR_ALIGN_FLAGS;
350         }
351         if (new_flags & LIBNDR_ALIGN_FLAGS) {
352                 (*pflags) &= ~LIBNDR_FLAG_REMAINING;
353         }
354         (*pflags) |= new_flags;
355 }
356
357 static NTSTATUS ndr_map_error(enum ndr_err_code ndr_err)
358 {
359         switch (ndr_err) {
360         case NDR_ERR_BUFSIZE:
361                 return NT_STATUS_BUFFER_TOO_SMALL;
362         case NDR_ERR_TOKEN:
363                 return NT_STATUS_INTERNAL_ERROR;
364         case NDR_ERR_ALLOC:
365                 return NT_STATUS_NO_MEMORY;
366         case NDR_ERR_ARRAY_SIZE:
367                 return NT_STATUS_ARRAY_BOUNDS_EXCEEDED;
368         default:
369                 break;
370         }
371
372         /* we should map all error codes to different status codes */
373         return NT_STATUS_INVALID_PARAMETER;
374 }
375
376 /*
377   return and possibly log an NDR error
378 */
379 _PUBLIC_ NTSTATUS ndr_pull_error(struct ndr_pull *ndr,
380                                  enum ndr_err_code ndr_err,
381                                  const char *format, ...) _PRINTF_ATTRIBUTE(3,4)
382 {
383         char *s=NULL;
384         va_list ap;
385
386         va_start(ap, format);
387         vasprintf(&s, format, ap);
388         va_end(ap);
389
390         DEBUG(3,("ndr_pull_error(%u): %s\n", ndr_err, s));
391
392         free(s);
393
394         return ndr_map_error(ndr_err);
395 }
396
397 /*
398   return and possibly log an NDR error
399 */
400 _PUBLIC_ NTSTATUS ndr_push_error(struct ndr_push *ndr,
401                                  enum ndr_err_code ndr_err,
402                                  const char *format, ...)  _PRINTF_ATTRIBUTE(3,4)
403 {
404         char *s=NULL;
405         va_list ap;
406
407         va_start(ap, format);
408         vasprintf(&s, format, ap);
409         va_end(ap);
410
411         DEBUG(3,("ndr_push_error(%u): %s\n", ndr_err, s));
412
413         free(s);
414
415         return ndr_map_error(ndr_err);
416 }
417
418 /*
419   handle subcontext buffers, which in midl land are user-marshalled, but
420   we use magic in pidl to make them easier to cope with
421 */
422 _PUBLIC_ NTSTATUS ndr_pull_subcontext_start(struct ndr_pull *ndr, 
423                                    struct ndr_pull **_subndr,
424                                    size_t header_size,
425                                    ssize_t size_is)
426 {
427         struct ndr_pull *subndr;
428         uint32_t r_content_size;
429
430         switch (header_size) {
431         case 0: {
432                 uint32_t content_size = ndr->data_size - ndr->offset;
433                 if (size_is >= 0) {
434                         content_size = size_is;
435                 }
436                 r_content_size = content_size;
437                 break;
438         }
439
440         case 2: {
441                 uint16_t content_size;
442                 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &content_size));
443                 if (size_is >= 0 && size_is != content_size) {
444                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d", 
445                                                 (int)size_is, (int)content_size);
446                 }
447                 r_content_size = content_size;
448                 break;
449         }
450
451         case 4: {
452                 uint32_t content_size;
453                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
454                 if (size_is >= 0 && size_is != content_size) {
455                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d", 
456                                                 (int)size_is, (int)content_size);
457                 }
458                 r_content_size = content_size;
459                 break;
460         }
461         default:
462                 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) header_size %d", 
463                                       (int)header_size);
464         }
465
466         NDR_PULL_NEED_BYTES(ndr, r_content_size);
467
468         subndr = talloc_zero(ndr, struct ndr_pull);
469         NT_STATUS_HAVE_NO_MEMORY(subndr);
470         subndr->flags           = ndr->flags;
471         subndr->current_mem_ctx = ndr->current_mem_ctx;
472
473         subndr->data = ndr->data + ndr->offset;
474         subndr->offset = 0;
475         subndr->data_size = r_content_size;
476
477         *_subndr = subndr;
478         return NT_STATUS_OK;
479 }
480
481 _PUBLIC_ NTSTATUS ndr_pull_subcontext_end(struct ndr_pull *ndr, 
482                                  struct ndr_pull *subndr,
483                                  size_t header_size,
484                                  ssize_t size_is)
485 {
486         uint32_t advance;
487         if (size_is >= 0) {
488                 advance = size_is;
489         } else if (header_size > 0) {
490                 advance = subndr->data_size;
491         } else {
492                 advance = subndr->offset;
493         }
494         NDR_CHECK(ndr_pull_advance(ndr, advance));
495         return NT_STATUS_OK;
496 }
497
498 _PUBLIC_ NTSTATUS ndr_push_subcontext_start(struct ndr_push *ndr,
499                                    struct ndr_push **_subndr,
500                                    size_t header_size,
501                                    ssize_t size_is)
502 {
503         struct ndr_push *subndr;
504
505         subndr = ndr_push_init_ctx(ndr);
506         NT_STATUS_HAVE_NO_MEMORY(subndr);
507         subndr->flags   = ndr->flags;
508
509         *_subndr = subndr;
510         return NT_STATUS_OK;
511 }
512
513 /*
514   push a subcontext header 
515 */
516 _PUBLIC_ NTSTATUS ndr_push_subcontext_end(struct ndr_push *ndr,
517                                  struct ndr_push *subndr,
518                                  size_t header_size,
519                                  ssize_t size_is)
520 {
521         if (size_is >= 0) {
522                 ssize_t padding_len = size_is - subndr->offset;
523                 if (padding_len > 0) {
524                         NDR_CHECK(ndr_push_zero(subndr, padding_len));
525                 } else if (padding_len < 0) {
526                         return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
527                                               (int)subndr->offset, (int)size_is);
528                 }
529         }
530
531         switch (header_size) {
532         case 0: 
533                 break;
534
535         case 2: 
536                 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, subndr->offset));
537                 break;
538
539         case 4: 
540                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
541                 break;
542
543         default:
544                 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext header size %d", 
545                                       (int)header_size);
546         }
547
548         NDR_CHECK(ndr_push_bytes(ndr, subndr->data, subndr->offset));
549         return NT_STATUS_OK;
550 }
551
552 /*
553   store a token in the ndr context, for later retrieval
554 */
555 _PUBLIC_ NTSTATUS ndr_token_store(TALLOC_CTX *mem_ctx, 
556                          struct ndr_token_list **list, 
557                          const void *key, 
558                          uint32_t value)
559 {
560         struct ndr_token_list *tok;
561         tok = talloc(mem_ctx, struct ndr_token_list);
562         if (tok == NULL) {
563                 return NT_STATUS_NO_MEMORY;
564         }
565         tok->key = key;
566         tok->value = value;
567         DLIST_ADD((*list), tok);
568         return NT_STATUS_OK;
569 }
570
571 /*
572   retrieve a token from a ndr context, using cmp_fn to match the tokens
573 */
574 _PUBLIC_ NTSTATUS ndr_token_retrieve_cmp_fn(struct ndr_token_list **list, const void *key, uint32_t *v,
575                                    comparison_fn_t _cmp_fn, BOOL _remove_tok)
576 {
577         struct ndr_token_list *tok;
578         for (tok=*list;tok;tok=tok->next) {
579                 if (_cmp_fn && _cmp_fn(tok->key,key)==0) goto found;
580                 else if (!_cmp_fn && tok->key == key) goto found;
581         }
582         return ndr_map_error(NDR_ERR_TOKEN);
583 found:
584         *v = tok->value;
585         if (_remove_tok) {
586                 DLIST_REMOVE((*list), tok);
587                 talloc_free(tok);
588         }
589         return NT_STATUS_OK;            
590 }
591
592 /*
593   retrieve a token from a ndr context
594 */
595 _PUBLIC_ NTSTATUS ndr_token_retrieve(struct ndr_token_list **list, const void *key, uint32_t *v)
596 {
597         return ndr_token_retrieve_cmp_fn(list, key, v, NULL, True);
598 }
599
600 /*
601   peek at but don't removed a token from a ndr context
602 */
603 _PUBLIC_ uint32_t ndr_token_peek(struct ndr_token_list **list, const void *key)
604 {
605         NTSTATUS status;
606         uint32_t v;
607         status = ndr_token_retrieve_cmp_fn(list, key, &v, NULL, False);
608         if (NT_STATUS_IS_OK(status)) return v;
609         return 0;
610 }
611
612 /*
613   pull an array size field and add it to the array_size_list token list
614 */
615 _PUBLIC_ NTSTATUS ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
616 {
617         uint32_t size;
618         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &size));
619         return ndr_token_store(ndr, &ndr->array_size_list, p, size);
620 }
621
622 /*
623   get the stored array size field
624 */
625 _PUBLIC_ uint32_t ndr_get_array_size(struct ndr_pull *ndr, const void *p)
626 {
627         return ndr_token_peek(&ndr->array_size_list, p);
628 }
629
630 /*
631   check the stored array size field
632 */
633 _PUBLIC_ NTSTATUS ndr_check_array_size(struct ndr_pull *ndr, void *p, uint32_t size)
634 {
635         uint32_t stored;
636         stored = ndr_token_peek(&ndr->array_size_list, p);
637         if (stored != size) {
638                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
639                                       "Bad array size - got %u expected %u\n",
640                                       stored, size);
641         }
642         return NT_STATUS_OK;
643 }
644
645 /*
646   pull an array length field and add it to the array_length_list token list
647 */
648 _PUBLIC_ NTSTATUS ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
649 {
650         uint32_t length, offset;
651         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &offset));
652         if (offset != 0) {
653                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
654                                       "non-zero array offset %u\n", offset);
655         }
656         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &length));
657         return ndr_token_store(ndr, &ndr->array_length_list, p, length);
658 }
659
660 /*
661   get the stored array length field
662 */
663 _PUBLIC_ uint32_t ndr_get_array_length(struct ndr_pull *ndr, const void *p)
664 {
665         return ndr_token_peek(&ndr->array_length_list, p);
666 }
667
668 /*
669   check the stored array length field
670 */
671 _PUBLIC_ NTSTATUS ndr_check_array_length(struct ndr_pull *ndr, void *p, uint32_t length)
672 {
673         uint32_t stored;
674         stored = ndr_token_peek(&ndr->array_length_list, p);
675         if (stored != length) {
676                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
677                                       "Bad array length - got %u expected %u\n",
678                                       stored, length);
679         }
680         return NT_STATUS_OK;
681 }
682
683 /*
684   store a switch value
685  */
686 _PUBLIC_ NTSTATUS ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
687 {
688         return ndr_token_store(ndr, &ndr->switch_list, p, val);
689 }
690
691 _PUBLIC_ NTSTATUS ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
692 {
693         return ndr_token_store(ndr, &ndr->switch_list, p, val);
694 }
695
696 _PUBLIC_ NTSTATUS ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
697 {
698         return ndr_token_store(ndr, &ndr->switch_list, p, val);
699 }
700
701 /*
702   retrieve a switch value
703  */
704 _PUBLIC_ uint32_t ndr_push_get_switch_value(struct ndr_push *ndr, const void *p)
705 {
706         return ndr_token_peek(&ndr->switch_list, p);
707 }
708
709 _PUBLIC_ uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
710 {
711         return ndr_token_peek(&ndr->switch_list, p);
712 }
713
714 _PUBLIC_ uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
715 {
716         return ndr_token_peek(&ndr->switch_list, p);
717 }
718
719 /*
720   pull a struct from a blob using NDR
721 */
722 _PUBLIC_ NTSTATUS ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
723                               ndr_pull_flags_fn_t fn)
724 {
725         struct ndr_pull *ndr;
726         ndr = ndr_pull_init_blob(blob, mem_ctx);
727         if (!ndr) {
728                 return NT_STATUS_NO_MEMORY;
729         }
730         return fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
731 }
732
733 /*
734   pull a struct from a blob using NDR - failing if all bytes are not consumed
735 */
736 _PUBLIC_ NTSTATUS ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
737                                   ndr_pull_flags_fn_t fn)
738 {
739         struct ndr_pull *ndr;
740         NTSTATUS status;
741
742         ndr = ndr_pull_init_blob(blob, mem_ctx);
743         if (!ndr) {
744                 return NT_STATUS_NO_MEMORY;
745         }
746         status = fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
747         if (!NT_STATUS_IS_OK(status)) return status;
748         if (ndr->offset < ndr->data_size) {
749                 return NT_STATUS_PORT_MESSAGE_TOO_LONG;
750         }
751         return status;
752 }
753
754 /*
755   pull a union from a blob using NDR, given the union discriminator
756 */
757 _PUBLIC_ NTSTATUS ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
758                              uint32_t level, ndr_pull_flags_fn_t fn)
759 {
760         struct ndr_pull *ndr;
761         NTSTATUS status;
762
763         ndr = ndr_pull_init_blob(blob, mem_ctx);
764         if (!ndr) {
765                 return NT_STATUS_NO_MEMORY;
766         }
767         ndr_pull_set_switch_value(ndr, p, level);
768         status = fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
769         if (!NT_STATUS_IS_OK(status)) return status;
770         if (ndr->offset != ndr->data_size) {
771                 return NT_STATUS_BUFFER_TOO_SMALL;
772         }
773         return status;
774 }
775
776 /*
777   push a struct to a blob using NDR
778 */
779 _PUBLIC_ NTSTATUS ndr_push_struct_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, const void *p,
780                               ndr_push_flags_fn_t fn)
781 {
782         NTSTATUS status;
783         struct ndr_push *ndr;
784         ndr = ndr_push_init_ctx(mem_ctx);
785         if (!ndr) {
786                 return NT_STATUS_NO_MEMORY;
787         }
788         status = fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
789         if (!NT_STATUS_IS_OK(status)) {
790                 return status;
791         }
792
793         *blob = ndr_push_blob(ndr);
794         talloc_steal(mem_ctx, blob->data);
795         talloc_free(ndr);
796
797         return NT_STATUS_OK;
798 }
799
800 /*
801   push a union to a blob using NDR
802 */
803 _PUBLIC_ NTSTATUS ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
804                              uint32_t level, ndr_push_flags_fn_t fn)
805 {
806         NTSTATUS status;
807         struct ndr_push *ndr;
808         ndr = ndr_push_init_ctx(mem_ctx);
809         if (!ndr) {
810                 return NT_STATUS_NO_MEMORY;
811         }
812         ndr_push_set_switch_value(ndr, p, level);
813         status = fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
814         if (!NT_STATUS_IS_OK(status)) {
815                 return status;
816         }
817
818         *blob = ndr_push_blob(ndr);
819         talloc_steal(mem_ctx, blob->data);
820         talloc_free(ndr);
821
822         return NT_STATUS_OK;
823 }
824
825 /*
826   generic ndr_size_*() handler for structures
827 */
828 _PUBLIC_ size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push)
829 {
830         struct ndr_push *ndr;
831         NTSTATUS status;
832         size_t ret;
833
834         /* avoid recursion */
835         if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
836
837         ndr = ndr_push_init_ctx(NULL);
838         if (!ndr) return 0;
839         ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
840         status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
841         if (!NT_STATUS_IS_OK(status)) {
842                 return 0;
843         }
844         ret = ndr->offset;
845         talloc_free(ndr);
846         return ret;
847 }
848
849 /*
850   generic ndr_size_*() handler for unions
851 */
852 _PUBLIC_ size_t ndr_size_union(const void *p, int flags, uint32_t level, ndr_push_flags_fn_t push)
853 {
854         struct ndr_push *ndr;
855         NTSTATUS status;
856         size_t ret;
857
858         /* avoid recursion */
859         if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
860
861         ndr = ndr_push_init_ctx(NULL);
862         if (!ndr) return 0;
863         ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
864         ndr_push_set_switch_value(ndr, p, level);
865         status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
866         if (!NT_STATUS_IS_OK(status)) {
867                 return 0;
868         }
869         ret = ndr->offset;
870         talloc_free(ndr);
871         return ret;
872 }
873
874 /*
875   get the current base for relative pointers for the push
876 */
877 _PUBLIC_ uint32_t ndr_push_get_relative_base_offset(struct ndr_push *ndr)
878 {
879         return ndr->relative_base_offset;
880 }
881
882 /*
883   restore the old base for relative pointers for the push
884 */
885 _PUBLIC_ void ndr_push_restore_relative_base_offset(struct ndr_push *ndr, uint32_t offset)
886 {
887         ndr->relative_base_offset = offset;
888 }
889
890 /*
891   setup the current base for relative pointers for the push
892   called in the NDR_SCALAR stage
893 */
894 _PUBLIC_ NTSTATUS ndr_push_setup_relative_base_offset1(struct ndr_push *ndr, const void *p, uint32_t offset)
895 {
896         ndr->relative_base_offset = offset;
897         return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
898 }
899
900 /*
901   setup the current base for relative pointers for the push
902   called in the NDR_BUFFERS stage
903 */
904 _PUBLIC_ NTSTATUS ndr_push_setup_relative_base_offset2(struct ndr_push *ndr, const void *p)
905 {
906         return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
907 }
908
909 /*
910   push a relative object - stage1
911   this is called during SCALARS processing
912 */
913 _PUBLIC_ NTSTATUS ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
914 {
915         if (p == NULL) {
916                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
917                 return NT_STATUS_OK;
918         }
919         NDR_CHECK(ndr_push_align(ndr, 4));
920         NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
921         return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
922 }
923
924 /*
925   push a relative object - stage2
926   this is called during buffers processing
927 */
928 _PUBLIC_ NTSTATUS ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
929 {
930         struct ndr_push_save save;
931         uint32_t ptr_offset = 0xFFFFFFFF;
932         if (p == NULL) {
933                 return NT_STATUS_OK;
934         }
935         ndr_push_save(ndr, &save);
936         NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
937         if (ptr_offset > ndr->offset) {
938                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, 
939                                       "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
940                                       ptr_offset, ndr->offset);
941         }
942         ndr->offset = ptr_offset;
943         if (save.offset < ndr->relative_base_offset) {
944                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, 
945                                       "ndr_push_relative_ptr2 save.offset(%u) < ndr->relative_base_offset(%u)",
946                                       save.offset, ndr->relative_base_offset);
947         }       
948         NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save.offset - ndr->relative_base_offset));
949         ndr_push_restore(ndr, &save);
950         return NT_STATUS_OK;
951 }
952
953 /*
954   get the current base for relative pointers for the pull
955 */
956 _PUBLIC_ uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull *ndr)
957 {
958         return ndr->relative_base_offset;
959 }
960
961 /*
962   restore the old base for relative pointers for the pull
963 */
964 _PUBLIC_ void ndr_pull_restore_relative_base_offset(struct ndr_pull *ndr, uint32_t offset)
965 {
966         ndr->relative_base_offset = offset;
967 }
968
969 /*
970   setup the current base for relative pointers for the pull
971   called in the NDR_SCALAR stage
972 */
973 _PUBLIC_ NTSTATUS ndr_pull_setup_relative_base_offset1(struct ndr_pull *ndr, const void *p, uint32_t offset)
974 {
975         ndr->relative_base_offset = offset;
976         return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
977 }
978
979 /*
980   setup the current base for relative pointers for the pull
981   called in the NDR_BUFFERS stage
982 */
983 _PUBLIC_ NTSTATUS ndr_pull_setup_relative_base_offset2(struct ndr_pull *ndr, const void *p)
984 {
985         return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
986 }
987
988 /*
989   pull a relative object - stage1
990   called during SCALARS processing
991 */
992 _PUBLIC_ NTSTATUS ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
993 {
994         rel_offset += ndr->relative_base_offset;
995         if (rel_offset > ndr->data_size) {
996                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, 
997                                       "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
998                                       rel_offset, ndr->data_size);
999         }
1000         return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
1001 }
1002
1003 /*
1004   pull a relative object - stage2
1005   called during BUFFERS processing
1006 */
1007 _PUBLIC_ NTSTATUS ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
1008 {
1009         uint32_t rel_offset;
1010         NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
1011         return ndr_pull_set_offset(ndr, rel_offset);
1012 }