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