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