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