test setinfo FULL_EA_INFORMATION in gentest
[gd/samba/.git] / source3 / 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
31 #define NDR_BASE_MARSHALL_SIZE 1024
32
33 /* this guid indicates NDR encoding in a protocol tower */
34 const struct ndr_syntax_id ndr_transfer_syntax = {
35   { 0x8a885d04, 0x1ceb, 0x11c9, {0x9f, 0xe8}, {0x08,0x00,0x2b,0x10,0x48,0x60} },
36   2
37 };
38
39 const struct ndr_syntax_id ndr64_transfer_syntax = {
40   { 0x71710533, 0xbeba, 0x4937, {0x83, 0x19}, {0xb5,0xdb,0xef,0x9c,0xcc,0x36} },
41   1
42 };
43
44 /*
45   work out the number of bytes needed to align on a n byte boundary
46 */
47 _PUBLIC_ size_t ndr_align_size(uint32_t offset, size_t n)
48 {
49         if ((offset & (n-1)) == 0) return 0;
50         return n - (offset & (n-1));
51 }
52
53 /*
54   initialise a ndr parse structure from a data blob
55 */
56 _PUBLIC_ struct ndr_pull *ndr_pull_init_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx)
57 {
58         struct ndr_pull *ndr;
59
60         ndr = talloc_zero(mem_ctx, struct ndr_pull);
61         if (!ndr) return NULL;
62         ndr->current_mem_ctx = mem_ctx;
63
64         ndr->data = blob->data;
65         ndr->data_size = blob->length;
66
67         return ndr;
68 }
69
70 /*
71   advance by 'size' bytes
72 */
73 _PUBLIC_ enum ndr_err_code ndr_pull_advance(struct ndr_pull *ndr, uint32_t size)
74 {
75         ndr->offset += size;
76         if (ndr->offset > ndr->data_size) {
77                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, 
78                                       "ndr_pull_advance by %u failed",
79                                       size);
80         }
81         return NDR_ERR_SUCCESS;
82 }
83
84 /*
85   set the parse offset to 'ofs'
86 */
87 static enum ndr_err_code ndr_pull_set_offset(struct ndr_pull *ndr, uint32_t ofs)
88 {
89         ndr->offset = ofs;
90         if (ndr->offset > ndr->data_size) {
91                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, 
92                                       "ndr_pull_set_offset %u failed",
93                                       ofs);
94         }
95         return NDR_ERR_SUCCESS;
96 }
97
98 /* save the offset/size of the current ndr state */
99 _PUBLIC_ void ndr_pull_save(struct ndr_pull *ndr, struct ndr_pull_save *save)
100 {
101         save->offset = ndr->offset;
102         save->data_size = ndr->data_size;
103 }
104
105 /* restore the size/offset of a ndr structure */
106 _PUBLIC_ void ndr_pull_restore(struct ndr_pull *ndr, struct ndr_pull_save *save)
107 {
108         ndr->offset = save->offset;
109         ndr->data_size = save->data_size;
110 }
111
112
113 /* create a ndr_push structure, ready for some marshalling */
114 _PUBLIC_ struct ndr_push *ndr_push_init_ctx(TALLOC_CTX *mem_ctx)
115 {
116         struct ndr_push *ndr;
117
118         ndr = talloc_zero(mem_ctx, struct ndr_push);
119         if (!ndr) {
120                 return NULL;
121         }
122
123         ndr->flags = 0;
124         ndr->alloc_size = NDR_BASE_MARSHALL_SIZE;
125         ndr->data = talloc_array(ndr, uint8_t, ndr->alloc_size);
126         if (!ndr->data) {
127                 return NULL;
128         }
129
130         return ndr;
131 }
132
133 /* return a DATA_BLOB structure for the current ndr_push marshalled data */
134 _PUBLIC_ DATA_BLOB ndr_push_blob(struct ndr_push *ndr)
135 {
136         DATA_BLOB blob;
137         blob = data_blob_const(ndr->data, ndr->offset);
138         if (ndr->alloc_size > ndr->offset) {
139                 ndr->data[ndr->offset] = 0;
140         }
141         return blob;
142 }
143
144
145 /*
146   expand the available space in the buffer to ndr->offset + extra_size
147 */
148 _PUBLIC_ enum ndr_err_code ndr_push_expand(struct ndr_push *ndr, uint32_t extra_size)
149 {
150         uint32_t size = extra_size + ndr->offset;
151
152         if (size < ndr->offset) {
153                 /* extra_size overflowed the offset */
154                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, "Overflow in push_expand to %u",
155                                       size);
156         }
157
158         if (ndr->alloc_size > size) {
159                 return NDR_ERR_SUCCESS;
160         }
161
162         ndr->alloc_size += NDR_BASE_MARSHALL_SIZE;
163         if (size+1 > ndr->alloc_size) {
164                 ndr->alloc_size = size+1;
165         }
166         ndr->data = talloc_realloc(ndr, ndr->data, uint8_t, ndr->alloc_size);
167         if (!ndr->data) {
168                 return ndr_push_error(ndr, NDR_ERR_ALLOC, "Failed to push_expand to %u",
169                                       ndr->alloc_size);
170         }
171
172         return NDR_ERR_SUCCESS;
173 }
174
175 _PUBLIC_ void ndr_print_debug_helper(struct ndr_print *ndr, const char *format, ...) _PRINTF_ATTRIBUTE(2,3)
176 {
177         va_list ap;
178         char *s = NULL;
179         int i, ret;
180
181         va_start(ap, format);
182         ret = vasprintf(&s, format, ap);
183         va_end(ap);
184
185         if (ret == -1) {
186                 return;
187         }
188
189         for (i=0;i<ndr->depth;i++) {
190                 DEBUGADD(0,("    "));
191         }
192
193         DEBUGADD(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_buffer(
204                                         (char *)ndr->private_data, "    ");
205         }
206
207         va_start(ap, format);
208         ndr->private_data = talloc_vasprintf_append_buffer((char *)ndr->private_data, 
209                                                     format, ap);
210         va_end(ap);
211         ndr->private_data = talloc_asprintf_append_buffer((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 NTSTATUS ndr_map_error2ntstatus(enum ndr_err_code ndr_err)
358 {
359         switch (ndr_err) {
360         case NDR_ERR_SUCCESS:
361                 return NT_STATUS_OK;
362         case NDR_ERR_BUFSIZE:
363                 return NT_STATUS_BUFFER_TOO_SMALL;
364         case NDR_ERR_TOKEN:
365                 return NT_STATUS_INTERNAL_ERROR;
366         case NDR_ERR_ALLOC:
367                 return NT_STATUS_NO_MEMORY;
368         case NDR_ERR_ARRAY_SIZE:
369                 return NT_STATUS_ARRAY_BOUNDS_EXCEEDED;
370         case NDR_ERR_INVALID_POINTER:
371                 return NT_STATUS_INVALID_PARAMETER_MIX;
372         case NDR_ERR_UNREAD_BYTES:
373                 return NT_STATUS_PORT_MESSAGE_TOO_LONG;
374         default:
375                 break;
376         }
377
378         /* we should map all error codes to different status codes */
379         return NT_STATUS_INVALID_PARAMETER;
380 }
381
382 /*
383  * Convert an ndr error to string
384  */
385
386 const char *ndr_errstr(enum ndr_err_code err)
387 {
388         switch (err) {
389         case NDR_ERR_SUCCESS:
390                 return "NDR_ERR_SUCCESS";
391                 break;
392         case NDR_ERR_ARRAY_SIZE:
393                 return "NDR_ERR_ARRAY_SIZE";
394                 break;
395         case NDR_ERR_BAD_SWITCH:
396                 return "NDR_ERR_BAD_SWITCH";
397                 break;
398         case NDR_ERR_OFFSET:
399                 return "NDR_ERR_OFFSET";
400                 break;
401         case NDR_ERR_RELATIVE:
402                 return "NDR_ERR_RELATIVE";
403                 break;
404         case NDR_ERR_CHARCNV:
405                 return "NDR_ERR_CHARCNV";
406                 break;
407         case NDR_ERR_LENGTH:
408                 return "NDR_ERR_LENGTH";
409                 break;
410         case NDR_ERR_SUBCONTEXT:
411                 return "NDR_ERR_SUBCONTEXT";
412                 break;
413         case NDR_ERR_COMPRESSION:
414                 return "NDR_ERR_COMPRESSION";
415                 break;
416         case NDR_ERR_STRING:
417                 return "NDR_ERR_STRING";
418                 break;
419         case NDR_ERR_VALIDATE:
420                 return "NDR_ERR_VALIDATE";
421                 break;
422         case NDR_ERR_BUFSIZE:
423                 return "NDR_ERR_BUFSIZE";
424                 break;
425         case NDR_ERR_ALLOC:
426                 return "NDR_ERR_ALLOC";
427                 break;
428         case NDR_ERR_RANGE:
429                 return "NDR_ERR_RANGE";
430                 break;
431         case NDR_ERR_TOKEN:
432                 return "NDR_ERR_TOKEN";
433                 break;
434         case NDR_ERR_IPV4ADDRESS:
435                 return "NDR_ERR_IPV4ADDRESS";
436                 break;
437         case NDR_ERR_INVALID_POINTER:
438                 return "NDR_ERR_INVALID_POINTER";
439                 break;
440         case NDR_ERR_UNREAD_BYTES:
441                 return "NDR_ERR_UNREAD_BYTES";
442                 break;
443         }
444
445         return talloc_asprintf(talloc_tos(), "Unknown NDR error: %d", err);
446 }
447
448 /*
449   return and possibly log an NDR error
450 */
451 _PUBLIC_ enum ndr_err_code ndr_pull_error(struct ndr_pull *ndr,
452                                  enum ndr_err_code ndr_err,
453                                  const char *format, ...) _PRINTF_ATTRIBUTE(3,4)
454 {
455         char *s=NULL;
456         va_list ap;
457         int ret;
458
459         va_start(ap, format);
460         ret = vasprintf(&s, format, ap);
461         va_end(ap);
462
463         if (ret == -1) {
464                 return NDR_ERR_ALLOC;
465         }
466
467         DEBUG(1,("ndr_pull_error(%u): %s\n", ndr_err, s));
468
469         free(s);
470
471         return ndr_err;
472 }
473
474 /*
475   return and possibly log an NDR error
476 */
477 _PUBLIC_ enum ndr_err_code ndr_push_error(struct ndr_push *ndr,
478                                  enum ndr_err_code ndr_err,
479                                  const char *format, ...)  _PRINTF_ATTRIBUTE(3,4)
480 {
481         char *s=NULL;
482         va_list ap;
483         int ret;
484
485         va_start(ap, format);
486         ret = vasprintf(&s, format, ap);
487         va_end(ap);
488
489         if (ret == -1) {
490                 return NDR_ERR_ALLOC;
491         }
492
493         DEBUG(1,("ndr_push_error(%u): %s\n", ndr_err, s));
494
495         free(s);
496
497         return ndr_err;
498 }
499
500 /*
501   handle subcontext buffers, which in midl land are user-marshalled, but
502   we use magic in pidl to make them easier to cope with
503 */
504 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_start(struct ndr_pull *ndr,
505                                    struct ndr_pull **_subndr,
506                                    size_t header_size,
507                                    ssize_t size_is)
508 {
509         struct ndr_pull *subndr;
510         uint32_t r_content_size;
511
512         switch (header_size) {
513         case 0: {
514                 uint32_t content_size = ndr->data_size - ndr->offset;
515                 if (size_is >= 0) {
516                         content_size = size_is;
517                 }
518                 r_content_size = content_size;
519                 break;
520         }
521
522         case 2: {
523                 uint16_t content_size;
524                 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &content_size));
525                 if (size_is >= 0 && size_is != content_size) {
526                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d", 
527                                                 (int)size_is, (int)content_size);
528                 }
529                 r_content_size = content_size;
530                 break;
531         }
532
533         case 4: {
534                 uint32_t content_size;
535                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
536                 if (size_is >= 0 && size_is != content_size) {
537                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d", 
538                                                 (int)size_is, (int)content_size);
539                 }
540                 r_content_size = content_size;
541                 break;
542         }
543         default:
544                 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) header_size %d", 
545                                       (int)header_size);
546         }
547
548         NDR_PULL_NEED_BYTES(ndr, r_content_size);
549
550         subndr = talloc_zero(ndr, struct ndr_pull);
551         NDR_ERR_HAVE_NO_MEMORY(subndr);
552         subndr->flags           = ndr->flags;
553         subndr->current_mem_ctx = ndr->current_mem_ctx;
554
555         subndr->data = ndr->data + ndr->offset;
556         subndr->offset = 0;
557         subndr->data_size = r_content_size;
558
559         *_subndr = subndr;
560         return NDR_ERR_SUCCESS;
561 }
562
563 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_end(struct ndr_pull *ndr,
564                                  struct ndr_pull *subndr,
565                                  size_t header_size,
566                                  ssize_t size_is)
567 {
568         uint32_t advance;
569         if (size_is >= 0) {
570                 advance = size_is;
571         } else if (header_size > 0) {
572                 advance = subndr->data_size;
573         } else {
574                 advance = subndr->offset;
575         }
576         NDR_CHECK(ndr_pull_advance(ndr, advance));
577         return NDR_ERR_SUCCESS;
578 }
579
580 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_start(struct ndr_push *ndr,
581                                    struct ndr_push **_subndr,
582                                    size_t header_size,
583                                    ssize_t size_is)
584 {
585         struct ndr_push *subndr;
586
587         subndr = ndr_push_init_ctx(ndr);
588         NDR_ERR_HAVE_NO_MEMORY(subndr);
589         subndr->flags   = ndr->flags;
590
591         *_subndr = subndr;
592         return NDR_ERR_SUCCESS;
593 }
594
595 /*
596   push a subcontext header 
597 */
598 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_end(struct ndr_push *ndr,
599                                  struct ndr_push *subndr,
600                                  size_t header_size,
601                                  ssize_t size_is)
602 {
603         if (size_is >= 0) {
604                 ssize_t padding_len = size_is - subndr->offset;
605                 if (padding_len > 0) {
606                         NDR_CHECK(ndr_push_zero(subndr, padding_len));
607                 } else if (padding_len < 0) {
608                         return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
609                                               (int)subndr->offset, (int)size_is);
610                 }
611         }
612
613         switch (header_size) {
614         case 0: 
615                 break;
616
617         case 2: 
618                 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, subndr->offset));
619                 break;
620
621         case 4: 
622                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
623                 break;
624
625         default:
626                 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext header size %d", 
627                                       (int)header_size);
628         }
629
630         NDR_CHECK(ndr_push_bytes(ndr, subndr->data, subndr->offset));
631         return NDR_ERR_SUCCESS;
632 }
633
634 /*
635   store a token in the ndr context, for later retrieval
636 */
637 _PUBLIC_ enum ndr_err_code ndr_token_store(TALLOC_CTX *mem_ctx,
638                          struct ndr_token_list **list, 
639                          const void *key, 
640                          uint32_t value)
641 {
642         struct ndr_token_list *tok;
643         tok = talloc(mem_ctx, struct ndr_token_list);
644         NDR_ERR_HAVE_NO_MEMORY(tok);
645         tok->key = key;
646         tok->value = value;
647         DLIST_ADD((*list), tok);
648         return NDR_ERR_SUCCESS;
649 }
650
651 /*
652   retrieve a token from a ndr context, using cmp_fn to match the tokens
653 */
654 _PUBLIC_ enum ndr_err_code ndr_token_retrieve_cmp_fn(struct ndr_token_list **list, const void *key, uint32_t *v,
655                                    comparison_fn_t _cmp_fn, bool _remove_tok)
656 {
657         struct ndr_token_list *tok;
658         for (tok=*list;tok;tok=tok->next) {
659                 if (_cmp_fn && _cmp_fn(tok->key,key)==0) goto found;
660                 else if (!_cmp_fn && tok->key == key) goto found;
661         }
662         return NDR_ERR_TOKEN;
663 found:
664         *v = tok->value;
665         if (_remove_tok) {
666                 DLIST_REMOVE((*list), tok);
667                 talloc_free(tok);
668         }
669         return NDR_ERR_SUCCESS;
670 }
671
672 /*
673   retrieve a token from a ndr context
674 */
675 _PUBLIC_ enum ndr_err_code ndr_token_retrieve(struct ndr_token_list **list, const void *key, uint32_t *v)
676 {
677         return ndr_token_retrieve_cmp_fn(list, key, v, NULL, true);
678 }
679
680 /*
681   peek at but don't removed a token from a ndr context
682 */
683 _PUBLIC_ uint32_t ndr_token_peek(struct ndr_token_list **list, const void *key)
684 {
685         enum ndr_err_code status;
686         uint32_t v;
687
688         status = ndr_token_retrieve_cmp_fn(list, key, &v, NULL, false);
689         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
690                 return 0;
691         }
692
693         return v;
694 }
695
696 /*
697   pull an array size field and add it to the array_size_list token list
698 */
699 _PUBLIC_ enum ndr_err_code ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
700 {
701         uint32_t size;
702         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &size));
703         return ndr_token_store(ndr, &ndr->array_size_list, p, size);
704 }
705
706 /*
707   get the stored array size field
708 */
709 _PUBLIC_ uint32_t ndr_get_array_size(struct ndr_pull *ndr, const void *p)
710 {
711         return ndr_token_peek(&ndr->array_size_list, p);
712 }
713
714 /*
715   check the stored array size field
716 */
717 _PUBLIC_ enum ndr_err_code ndr_check_array_size(struct ndr_pull *ndr, void *p, uint32_t size)
718 {
719         uint32_t stored;
720         stored = ndr_token_peek(&ndr->array_size_list, p);
721         if (stored != size) {
722                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
723                                       "Bad array size - got %u expected %u\n",
724                                       stored, size);
725         }
726         return NDR_ERR_SUCCESS;
727 }
728
729 /*
730   pull an array length field and add it to the array_length_list token list
731 */
732 _PUBLIC_ enum ndr_err_code ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
733 {
734         uint32_t length, offset;
735         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &offset));
736         if (offset != 0) {
737                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
738                                       "non-zero array offset %u\n", offset);
739         }
740         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &length));
741         return ndr_token_store(ndr, &ndr->array_length_list, p, length);
742 }
743
744 /*
745   get the stored array length field
746 */
747 _PUBLIC_ uint32_t ndr_get_array_length(struct ndr_pull *ndr, const void *p)
748 {
749         return ndr_token_peek(&ndr->array_length_list, p);
750 }
751
752 /*
753   check the stored array length field
754 */
755 _PUBLIC_ enum ndr_err_code ndr_check_array_length(struct ndr_pull *ndr, void *p, uint32_t length)
756 {
757         uint32_t stored;
758         stored = ndr_token_peek(&ndr->array_length_list, p);
759         if (stored != length) {
760                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
761                                       "Bad array length - got %u expected %u\n",
762                                       stored, length);
763         }
764         return NDR_ERR_SUCCESS;
765 }
766
767 /*
768   store a switch value
769  */
770 _PUBLIC_ enum ndr_err_code ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
771 {
772         return ndr_token_store(ndr, &ndr->switch_list, p, val);
773 }
774
775 _PUBLIC_ enum ndr_err_code ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
776 {
777         return ndr_token_store(ndr, &ndr->switch_list, p, val);
778 }
779
780 _PUBLIC_ enum ndr_err_code ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
781 {
782         return ndr_token_store(ndr, &ndr->switch_list, p, val);
783 }
784
785 /*
786   retrieve a switch value
787  */
788 _PUBLIC_ uint32_t ndr_push_get_switch_value(struct ndr_push *ndr, const void *p)
789 {
790         return ndr_token_peek(&ndr->switch_list, p);
791 }
792
793 _PUBLIC_ uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
794 {
795         return ndr_token_peek(&ndr->switch_list, p);
796 }
797
798 _PUBLIC_ uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
799 {
800         return ndr_token_peek(&ndr->switch_list, p);
801 }
802
803 /*
804   pull a struct from a blob using NDR
805 */
806 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
807                               ndr_pull_flags_fn_t fn)
808 {
809         struct ndr_pull *ndr;
810         ndr = ndr_pull_init_blob(blob, mem_ctx);
811         NDR_ERR_HAVE_NO_MEMORY(ndr);
812         NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
813         return NDR_ERR_SUCCESS;
814 }
815
816 /*
817   pull a struct from a blob using NDR - failing if all bytes are not consumed
818 */
819 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
820                                   ndr_pull_flags_fn_t fn)
821 {
822         struct ndr_pull *ndr;
823         ndr = ndr_pull_init_blob(blob, mem_ctx);
824         NDR_ERR_HAVE_NO_MEMORY(ndr);
825         NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
826         if (ndr->offset < ndr->data_size) {
827                 return ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
828                                       "not all bytes consumed ofs[%u] size[%u]",
829                                       ndr->offset, ndr->data_size);
830         }
831         return NDR_ERR_SUCCESS;
832 }
833
834 /*
835   pull a union from a blob using NDR, given the union discriminator
836 */
837 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
838                              uint32_t level, ndr_pull_flags_fn_t fn)
839 {
840         struct ndr_pull *ndr;
841         ndr = ndr_pull_init_blob(blob, mem_ctx);
842         NDR_ERR_HAVE_NO_MEMORY(ndr);
843         NDR_CHECK(ndr_pull_set_switch_value(ndr, p, level));
844         NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
845         return NDR_ERR_SUCCESS;
846 }
847
848 /*
849   pull a union from a blob using NDR, given the union discriminator,
850   failing if all bytes are not consumed
851 */
852 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
853                              uint32_t level, ndr_pull_flags_fn_t fn)
854 {
855         struct ndr_pull *ndr;
856         ndr = ndr_pull_init_blob(blob, mem_ctx);
857         NDR_ERR_HAVE_NO_MEMORY(ndr);
858         NDR_CHECK(ndr_pull_set_switch_value(ndr, p, level));
859         NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
860         if (ndr->offset < ndr->data_size) {
861                 return ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
862                                       "not all bytes consumed ofs[%u] size[%u]",
863                                       ndr->offset, ndr->data_size);
864         }
865         return NDR_ERR_SUCCESS;
866 }
867
868 /*
869   push a struct to a blob using NDR
870 */
871 _PUBLIC_ enum ndr_err_code ndr_push_struct_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, const void *p,
872                               ndr_push_flags_fn_t fn)
873 {
874         struct ndr_push *ndr;
875         ndr = ndr_push_init_ctx(mem_ctx);
876         NDR_ERR_HAVE_NO_MEMORY(ndr);
877
878         NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
879
880         *blob = ndr_push_blob(ndr);
881         talloc_steal(mem_ctx, blob->data);
882         talloc_free(ndr);
883
884         return NDR_ERR_SUCCESS;
885 }
886
887 /*
888   push a union to a blob using NDR
889 */
890 _PUBLIC_ enum ndr_err_code ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
891                              uint32_t level, ndr_push_flags_fn_t fn)
892 {
893         struct ndr_push *ndr;
894         ndr = ndr_push_init_ctx(mem_ctx);
895         NDR_ERR_HAVE_NO_MEMORY(ndr);
896
897         NDR_CHECK(ndr_push_set_switch_value(ndr, p, level));
898         NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
899
900         *blob = ndr_push_blob(ndr);
901         talloc_steal(mem_ctx, blob->data);
902         talloc_free(ndr);
903
904         return NDR_ERR_SUCCESS;
905 }
906
907 /*
908   generic ndr_size_*() handler for structures
909 */
910 _PUBLIC_ size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push)
911 {
912         struct ndr_push *ndr;
913         enum ndr_err_code status;
914         size_t ret;
915
916         /* avoid recursion */
917         if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
918
919         ndr = ndr_push_init_ctx(NULL);
920         if (!ndr) return 0;
921         ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
922         status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
923         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
924                 talloc_free(ndr);
925                 return 0;
926         }
927         ret = ndr->offset;
928         talloc_free(ndr);
929         return ret;
930 }
931
932 /*
933   generic ndr_size_*() handler for unions
934 */
935 _PUBLIC_ size_t ndr_size_union(const void *p, int flags, uint32_t level, ndr_push_flags_fn_t push)
936 {
937         struct ndr_push *ndr;
938         enum ndr_err_code status;
939         size_t ret;
940
941         /* avoid recursion */
942         if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
943
944         ndr = ndr_push_init_ctx(NULL);
945         if (!ndr) return 0;
946         ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
947
948         status = ndr_push_set_switch_value(ndr, p, level);
949         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
950                 talloc_free(ndr);
951                 return 0;
952         }
953         status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
954         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
955                 talloc_free(ndr);
956                 return 0;
957         }
958         ret = ndr->offset;
959         talloc_free(ndr);
960         return ret;
961 }
962
963 /*
964   get the current base for relative pointers for the push
965 */
966 _PUBLIC_ uint32_t ndr_push_get_relative_base_offset(struct ndr_push *ndr)
967 {
968         return ndr->relative_base_offset;
969 }
970
971 /*
972   restore the old base for relative pointers for the push
973 */
974 _PUBLIC_ void ndr_push_restore_relative_base_offset(struct ndr_push *ndr, uint32_t offset)
975 {
976         ndr->relative_base_offset = offset;
977 }
978
979 /*
980   setup the current base for relative pointers for the push
981   called in the NDR_SCALAR stage
982 */
983 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset1(struct ndr_push *ndr, const void *p, uint32_t offset)
984 {
985         ndr->relative_base_offset = offset;
986         return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
987 }
988
989 /*
990   setup the current base for relative pointers for the push
991   called in the NDR_BUFFERS stage
992 */
993 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset2(struct ndr_push *ndr, const void *p)
994 {
995         return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
996 }
997
998 /*
999   push a relative object - stage1
1000   this is called during SCALARS processing
1001 */
1002 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
1003 {
1004         if (p == NULL) {
1005                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
1006                 return NDR_ERR_SUCCESS;
1007         }
1008         NDR_CHECK(ndr_push_align(ndr, 4));
1009         NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1010         return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
1011 }
1012
1013 /*
1014   push a relative object - stage2
1015   this is called during buffers processing
1016 */
1017 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
1018 {
1019         struct ndr_push_save save;
1020         uint32_t ptr_offset = 0xFFFFFFFF;
1021         if (p == NULL) {
1022                 return NDR_ERR_SUCCESS;
1023         }
1024         ndr_push_save(ndr, &save);
1025         NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1026         if (ptr_offset > ndr->offset) {
1027                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, 
1028                                       "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1029                                       ptr_offset, ndr->offset);
1030         }
1031         ndr->offset = ptr_offset;
1032         if (save.offset < ndr->relative_base_offset) {
1033                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, 
1034                                       "ndr_push_relative_ptr2 save.offset(%u) < ndr->relative_base_offset(%u)",
1035                                       save.offset, ndr->relative_base_offset);
1036         }       
1037         NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save.offset - ndr->relative_base_offset));
1038         ndr_push_restore(ndr, &save);
1039         return NDR_ERR_SUCCESS;
1040 }
1041
1042 /*
1043   get the current base for relative pointers for the pull
1044 */
1045 _PUBLIC_ uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull *ndr)
1046 {
1047         return ndr->relative_base_offset;
1048 }
1049
1050 /*
1051   restore the old base for relative pointers for the pull
1052 */
1053 _PUBLIC_ void ndr_pull_restore_relative_base_offset(struct ndr_pull *ndr, uint32_t offset)
1054 {
1055         ndr->relative_base_offset = offset;
1056 }
1057
1058 /*
1059   setup the current base for relative pointers for the pull
1060   called in the NDR_SCALAR stage
1061 */
1062 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset1(struct ndr_pull *ndr, const void *p, uint32_t offset)
1063 {
1064         ndr->relative_base_offset = offset;
1065         return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1066 }
1067
1068 /*
1069   setup the current base for relative pointers for the pull
1070   called in the NDR_BUFFERS stage
1071 */
1072 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset2(struct ndr_pull *ndr, const void *p)
1073 {
1074         return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1075 }
1076
1077 /*
1078   pull a relative object - stage1
1079   called during SCALARS processing
1080 */
1081 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
1082 {
1083         rel_offset += ndr->relative_base_offset;
1084         if (rel_offset > ndr->data_size) {
1085                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, 
1086                                       "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1087                                       rel_offset, ndr->data_size);
1088         }
1089         return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
1090 }
1091
1092 /*
1093   pull a relative object - stage2
1094   called during BUFFERS processing
1095 */
1096 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
1097 {
1098         uint32_t rel_offset;
1099         NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
1100         return ndr_pull_set_offset(ndr, rel_offset);
1101 }