ndr: added --ndr64 flag to ndrdump
[bbaumbach/samba-autobuild/.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(1,("    "));
183         }
184
185         DEBUGADD(1,("%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(1,(" "));
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(1,(" "));
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(1,(" "));
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
259         /* this is a s4 hack until we build up the courage to pass
260          * this all the way down 
261          */
262 #if _SAMBA_BUILD_ == 4
263         ndr->iconv_convenience = smb_iconv_convenience_init(talloc_autofree_context(), "ASCII", "UTF-8", true);
264 #endif
265
266         fn(ndr, name, flags, ptr);
267         talloc_free(ndr);
268 }
269
270 /*
271   a useful helper function for printing idl structures to a string
272 */
273 _PUBLIC_ char *ndr_print_struct_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, void *ptr)
274 {
275         struct ndr_print *ndr;
276         char *ret = NULL;
277
278         ndr = talloc_zero(mem_ctx, struct ndr_print);
279         if (!ndr) return NULL;
280         ndr->private_data = talloc_strdup(ndr, "");
281         if (!ndr->private_data) {
282                 goto failed;
283         }
284         ndr->print = ndr_print_string_helper;
285         ndr->depth = 1;
286         ndr->flags = 0;
287
288         /* this is a s4 hack until we build up the courage to pass
289          * this all the way down 
290          */
291 #if _SAMBA_BUILD_ == 4
292         ndr->iconv_convenience = smb_iconv_convenience_init(talloc_autofree_context(), "ASCII", "UTF-8", true);
293 #endif
294
295         fn(ndr, name, ptr);
296         ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
297 failed:
298         talloc_free(ndr);
299         return ret;
300 }
301
302 /*
303   a useful helper function for printing idl unions to a string
304 */
305 _PUBLIC_ char *ndr_print_union_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, uint32_t level, 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         ndr_print_set_switch_value(ndr, ptr, level);
320         fn(ndr, name, ptr);
321         ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
322 failed:
323         talloc_free(ndr);
324         return ret;
325 }
326
327 /*
328   a useful helper function for printing idl function calls to a string
329 */
330 _PUBLIC_ char *ndr_print_function_string(TALLOC_CTX *mem_ctx,
331                                 ndr_print_function_t fn, const char *name, 
332                                 int flags, void *ptr)
333 {
334         struct ndr_print *ndr;
335         char *ret = NULL;
336
337         ndr = talloc_zero(mem_ctx, struct ndr_print);
338         if (!ndr) return NULL;
339         ndr->private_data = talloc_strdup(ndr, "");
340         if (!ndr->private_data) {
341                 goto failed;
342         }
343         ndr->print = ndr_print_string_helper;
344         ndr->depth = 1;
345         ndr->flags = 0;
346         fn(ndr, name, flags, ptr);
347         ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
348 failed:
349         talloc_free(ndr);
350         return ret;
351 }
352
353 _PUBLIC_ void ndr_set_flags(uint32_t *pflags, uint32_t new_flags)
354 {
355         /* the big/little endian flags are inter-dependent */
356         if (new_flags & LIBNDR_FLAG_LITTLE_ENDIAN) {
357                 (*pflags) &= ~LIBNDR_FLAG_BIGENDIAN;
358         }
359         if (new_flags & LIBNDR_FLAG_BIGENDIAN) {
360                 (*pflags) &= ~LIBNDR_FLAG_LITTLE_ENDIAN;
361         }
362         if (new_flags & LIBNDR_FLAG_REMAINING) {
363                 (*pflags) &= ~LIBNDR_ALIGN_FLAGS;
364         }
365         if (new_flags & LIBNDR_ALIGN_FLAGS) {
366                 (*pflags) &= ~LIBNDR_FLAG_REMAINING;
367         }
368         (*pflags) |= new_flags;
369 }
370
371 /*
372   return and possibly log an NDR error
373 */
374 _PUBLIC_ enum ndr_err_code ndr_pull_error(struct ndr_pull *ndr,
375                                  enum ndr_err_code ndr_err,
376                                  const char *format, ...)
377 {
378         char *s=NULL;
379         va_list ap;
380         int ret;
381
382         va_start(ap, format);
383         ret = vasprintf(&s, format, ap);
384         va_end(ap);
385
386         if (ret == -1) {
387                 return NDR_ERR_ALLOC;
388         }
389
390         DEBUG(1,("ndr_pull_error(%u): %s\n", ndr_err, s));
391
392         free(s);
393
394         return ndr_err;
395 }
396
397 /*
398   return and possibly log an NDR error
399 */
400 _PUBLIC_ enum ndr_err_code ndr_push_error(struct ndr_push *ndr,
401                                  enum ndr_err_code ndr_err,
402                                  const char *format, ...) 
403 {
404         char *s=NULL;
405         va_list ap;
406         int ret;
407
408         va_start(ap, format);
409         ret = vasprintf(&s, format, ap);
410         va_end(ap);
411
412         if (ret == -1) {
413                 return NDR_ERR_ALLOC;
414         }
415
416         DEBUG(1,("ndr_push_error(%u): %s\n", ndr_err, s));
417
418         free(s);
419
420         return ndr_err;
421 }
422
423 /*
424   handle subcontext buffers, which in midl land are user-marshalled, but
425   we use magic in pidl to make them easier to cope with
426 */
427 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_start(struct ndr_pull *ndr,
428                                    struct ndr_pull **_subndr,
429                                    size_t header_size,
430                                    ssize_t size_is)
431 {
432         struct ndr_pull *subndr;
433         uint32_t r_content_size;
434         bool force_le = false;
435         bool force_be = false;
436
437         switch (header_size) {
438         case 0: {
439                 uint32_t content_size = ndr->data_size - ndr->offset;
440                 if (size_is >= 0) {
441                         content_size = size_is;
442                 }
443                 r_content_size = content_size;
444                 break;
445         }
446
447         case 2: {
448                 uint16_t content_size;
449                 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &content_size));
450                 if (size_is >= 0 && size_is != content_size) {
451                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d", 
452                                                 (int)size_is, (int)content_size);
453                 }
454                 r_content_size = content_size;
455                 break;
456         }
457
458         case 4: {
459                 uint32_t content_size;
460                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
461                 if (size_is >= 0 && size_is != content_size) {
462                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d", 
463                                                 (int)size_is, (int)content_size);
464                 }
465                 r_content_size = content_size;
466                 break;
467         }
468         case 0xFFFFFC01: {
469                 /*
470                  * Common Type Header for the Serialization Stream
471                  * See [MS-RPCE] 2.2.6 Type Serialization Version 1
472                  */
473                 uint8_t version;
474                 uint8_t drep;
475                 uint16_t hdrlen;
476                 uint32_t filler;
477                 uint32_t content_size;
478                 uint32_t reserved;
479
480                 /* version */
481                 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &version));
482
483                 if (version != 1) {
484                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
485                                               "Bad subcontext (PULL) Common Type Header version %d != 1",
486                                               (int)version);
487                 }
488
489                 /*
490                  * 0x10 little endian
491                  * 0x00 big endian
492                  */
493                 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &drep));
494                 if (drep == 0x10) {
495                         force_le = true;
496                 } else if (drep == 0x00) {
497                         force_be = true;
498                 } else {
499                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
500                                               "Bad subcontext (PULL) Common Type Header invalid drep 0x%02X",
501                                               (unsigned int)drep);
502                 }
503
504                 /* length of the "Private Header for Constructed Type" */
505                 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &hdrlen));
506                 if (hdrlen != 8) {
507                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
508                                               "Bad subcontext (PULL) Common Type Header length %d != 8",
509                                               (int)hdrlen);
510                 }
511
512                 /* filler should be ignored */
513                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &filler));
514
515                 /*
516                  * Private Header for Constructed Type
517                  */
518                 /* length - will be updated latter */
519                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
520                 if (size_is >= 0 && size_is != content_size) {
521                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
522                                               (int)size_is, (int)content_size);
523                 }
524                 /* the content size must be a multiple of 8 */
525                 if ((content_size % 8) != 0) {
526                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
527                                               "Bad subcontext (PULL) size_is(%d) not padded to 8 content_size %d",
528                                               (int)size_is, (int)content_size);
529                 }
530                 r_content_size = content_size;
531
532                 /* reserved */
533                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &reserved));
534                 break;
535         }
536         default:
537                 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) header_size %d", 
538                                       (int)header_size);
539         }
540
541         NDR_PULL_NEED_BYTES(ndr, r_content_size);
542
543         subndr = talloc_zero(ndr, struct ndr_pull);
544         NDR_ERR_HAVE_NO_MEMORY(subndr);
545         subndr->flags           = ndr->flags;
546         subndr->current_mem_ctx = ndr->current_mem_ctx;
547
548         subndr->data = ndr->data + ndr->offset;
549         subndr->offset = 0;
550         subndr->data_size = r_content_size;
551         subndr->iconv_convenience = talloc_reference(subndr, ndr->iconv_convenience);
552
553         if (force_le) {
554                 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_LITTLE_ENDIAN);
555         } else if (force_be) {
556                 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_BIGENDIAN);
557         }
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, ndr->iconv_convenience);
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         ssize_t padding_len;
604
605         if (size_is >= 0) {
606                 padding_len = size_is - subndr->offset;
607                 if (padding_len > 0) {
608                         NDR_CHECK(ndr_push_zero(subndr, padding_len));
609                 } else if (padding_len < 0) {
610                         return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
611                                               (int)subndr->offset, (int)size_is);
612                 }
613         }
614
615         switch (header_size) {
616         case 0: 
617                 break;
618
619         case 2: 
620                 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, subndr->offset));
621                 break;
622
623         case 4: 
624                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
625                 break;
626
627         case 0xFFFFFC01:
628                 /*
629                  * Common Type Header for the Serialization Stream
630                  * See [MS-RPCE] 2.2.6 Type Serialization Version 1
631                  */
632                 padding_len = NDR_ROUND(subndr->offset, 8) - subndr->offset;
633                 if (padding_len > 0) {
634                         NDR_CHECK(ndr_push_zero(subndr, padding_len));
635                 }
636
637                 /* version */
638                 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, 1));
639
640                 /*
641                  * 0x10 little endian
642                  * 0x00 big endian
643                  */
644                 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, NDR_BE(ndr)?0x00:0x10));
645
646                 /* length of the "Private Header for Constructed Type" */
647                 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 8));
648
649                 /* filler */
650                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0xCCCCCCCC));
651
652                 /*
653                  * Private Header for Constructed Type
654                  */
655                 /* length - will be updated latter */
656                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
657
658                 /* reserved */
659                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
660                 break;
661
662         default:
663                 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext header size %d", 
664                                       (int)header_size);
665         }
666
667         NDR_CHECK(ndr_push_bytes(ndr, subndr->data, subndr->offset));
668         return NDR_ERR_SUCCESS;
669 }
670
671 /*
672   store a token in the ndr context, for later retrieval
673 */
674 _PUBLIC_ enum ndr_err_code ndr_token_store(TALLOC_CTX *mem_ctx,
675                          struct ndr_token_list **list, 
676                          const void *key, 
677                          uint32_t value)
678 {
679         struct ndr_token_list *tok;
680         tok = talloc(mem_ctx, struct ndr_token_list);
681         NDR_ERR_HAVE_NO_MEMORY(tok);
682         tok->key = key;
683         tok->value = value;
684         DLIST_ADD((*list), tok);
685         return NDR_ERR_SUCCESS;
686 }
687
688 /*
689   retrieve a token from a ndr context, using cmp_fn to match the tokens
690 */
691 _PUBLIC_ enum ndr_err_code ndr_token_retrieve_cmp_fn(struct ndr_token_list **list, const void *key, uint32_t *v,
692                                    comparison_fn_t _cmp_fn, bool _remove_tok)
693 {
694         struct ndr_token_list *tok;
695         for (tok=*list;tok;tok=tok->next) {
696                 if (_cmp_fn && _cmp_fn(tok->key,key)==0) goto found;
697                 else if (!_cmp_fn && tok->key == key) goto found;
698         }
699         return NDR_ERR_TOKEN;
700 found:
701         *v = tok->value;
702         if (_remove_tok) {
703                 DLIST_REMOVE((*list), tok);
704                 talloc_free(tok);
705         }
706         return NDR_ERR_SUCCESS;
707 }
708
709 /*
710   retrieve a token from a ndr context
711 */
712 _PUBLIC_ enum ndr_err_code ndr_token_retrieve(struct ndr_token_list **list, const void *key, uint32_t *v)
713 {
714         return ndr_token_retrieve_cmp_fn(list, key, v, NULL, true);
715 }
716
717 /*
718   peek at but don't removed a token from a ndr context
719 */
720 _PUBLIC_ uint32_t ndr_token_peek(struct ndr_token_list **list, const void *key)
721 {
722         enum ndr_err_code status;
723         uint32_t v;
724
725         status = ndr_token_retrieve_cmp_fn(list, key, &v, NULL, false);
726         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
727                 return 0;
728         }
729
730         return v;
731 }
732
733 /*
734   pull an array size field and add it to the array_size_list token list
735 */
736 _PUBLIC_ enum ndr_err_code ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
737 {
738         uint32_t size;
739         NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &size));
740         return ndr_token_store(ndr, &ndr->array_size_list, p, size);
741 }
742
743 /*
744   get the stored array size field
745 */
746 _PUBLIC_ uint32_t ndr_get_array_size(struct ndr_pull *ndr, const void *p)
747 {
748         return ndr_token_peek(&ndr->array_size_list, p);
749 }
750
751 /*
752   check the stored array size field
753 */
754 _PUBLIC_ enum ndr_err_code ndr_check_array_size(struct ndr_pull *ndr, void *p, uint32_t size)
755 {
756         uint32_t stored;
757         stored = ndr_token_peek(&ndr->array_size_list, p);
758         if (stored != size) {
759                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
760                                       "Bad array size - got %u expected %u\n",
761                                       stored, size);
762         }
763         return NDR_ERR_SUCCESS;
764 }
765
766 /*
767   pull an array length field and add it to the array_length_list token list
768 */
769 _PUBLIC_ enum ndr_err_code ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
770 {
771         uint32_t length, offset;
772         NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &offset));
773         if (offset != 0) {
774                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
775                                       "non-zero array offset %u\n", offset);
776         }
777         NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &length));
778         return ndr_token_store(ndr, &ndr->array_length_list, p, length);
779 }
780
781 /*
782   get the stored array length field
783 */
784 _PUBLIC_ uint32_t ndr_get_array_length(struct ndr_pull *ndr, const void *p)
785 {
786         return ndr_token_peek(&ndr->array_length_list, p);
787 }
788
789 /*
790   check the stored array length field
791 */
792 _PUBLIC_ enum ndr_err_code ndr_check_array_length(struct ndr_pull *ndr, void *p, uint32_t length)
793 {
794         uint32_t stored;
795         stored = ndr_token_peek(&ndr->array_length_list, p);
796         if (stored != length) {
797                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
798                                       "Bad array length - got %u expected %u\n",
799                                       stored, length);
800         }
801         return NDR_ERR_SUCCESS;
802 }
803
804 /*
805   store a switch value
806  */
807 _PUBLIC_ enum ndr_err_code ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
808 {
809         return ndr_token_store(ndr, &ndr->switch_list, p, val);
810 }
811
812 _PUBLIC_ enum ndr_err_code ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
813 {
814         return ndr_token_store(ndr, &ndr->switch_list, p, val);
815 }
816
817 _PUBLIC_ enum ndr_err_code ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
818 {
819         return ndr_token_store(ndr, &ndr->switch_list, p, val);
820 }
821
822 /*
823   retrieve a switch value
824  */
825 _PUBLIC_ uint32_t ndr_push_get_switch_value(struct ndr_push *ndr, const void *p)
826 {
827         return ndr_token_peek(&ndr->switch_list, p);
828 }
829
830 _PUBLIC_ uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
831 {
832         return ndr_token_peek(&ndr->switch_list, p);
833 }
834
835 _PUBLIC_ uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
836 {
837         return ndr_token_peek(&ndr->switch_list, p);
838 }
839
840 /*
841   pull a struct from a blob using NDR
842 */
843 _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,
844                               ndr_pull_flags_fn_t fn)
845 {
846         struct ndr_pull *ndr;
847         ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
848         NDR_ERR_HAVE_NO_MEMORY(ndr);
849         NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
850         talloc_free(ndr);
851         return NDR_ERR_SUCCESS;
852 }
853
854 /*
855   pull a struct from a blob using NDR - failing if all bytes are not consumed
856 */
857 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
858                                                     struct smb_iconv_convenience *iconv_convenience,
859                                                     void *p, ndr_pull_flags_fn_t fn)
860 {
861         struct ndr_pull *ndr;
862         ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
863         NDR_ERR_HAVE_NO_MEMORY(ndr);
864         NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
865         if (ndr->offset < ndr->data_size) {
866                 return ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
867                                       "not all bytes consumed ofs[%u] size[%u]",
868                                       ndr->offset, ndr->data_size);
869         }
870         talloc_free(ndr);
871         return NDR_ERR_SUCCESS;
872 }
873
874 /*
875   pull a union from a blob using NDR, given the union discriminator
876 */
877 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
878                                                struct smb_iconv_convenience *iconv_convenience, void *p,
879                              uint32_t level, ndr_pull_flags_fn_t fn)
880 {
881         struct ndr_pull *ndr;
882         ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
883         NDR_ERR_HAVE_NO_MEMORY(ndr);
884         NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
885         NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
886         talloc_free(ndr);
887         return NDR_ERR_SUCCESS;
888 }
889
890 /*
891   pull a union from a blob using NDR, given the union discriminator,
892   failing if all bytes are not consumed
893 */
894 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
895                                                    struct smb_iconv_convenience *iconv_convenience, void *p,
896                              uint32_t level, ndr_pull_flags_fn_t fn)
897 {
898         struct ndr_pull *ndr;
899         ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
900         NDR_ERR_HAVE_NO_MEMORY(ndr);
901         NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
902         NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
903         if (ndr->offset < ndr->data_size) {
904                 enum ndr_err_code ret;
905                 ret = ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
906                                      "not all bytes consumed ofs[%u] size[%u]",
907                                      ndr->offset, ndr->data_size);
908                 talloc_free(ndr);
909                 return ret;
910         }
911         talloc_free(ndr);
912         return NDR_ERR_SUCCESS;
913 }
914
915 /*
916   push a struct to a blob using NDR
917 */
918 _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)
919 {
920         struct ndr_push *ndr;
921         ndr = ndr_push_init_ctx(mem_ctx, iconv_convenience);
922         NDR_ERR_HAVE_NO_MEMORY(ndr);
923
924         NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
925
926         *blob = ndr_push_blob(ndr);
927         talloc_steal(mem_ctx, blob->data);
928         talloc_free(ndr);
929
930         return NDR_ERR_SUCCESS;
931 }
932
933 /*
934   push a union to a blob using NDR
935 */
936 _PUBLIC_ enum ndr_err_code ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience, void *p,
937                              uint32_t level, ndr_push_flags_fn_t fn)
938 {
939         struct ndr_push *ndr;
940         ndr = ndr_push_init_ctx(mem_ctx, iconv_convenience);
941         NDR_ERR_HAVE_NO_MEMORY(ndr);
942
943         NDR_CHECK(ndr_push_set_switch_value(ndr, p, level));
944         NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
945
946         *blob = ndr_push_blob(ndr);
947         talloc_steal(mem_ctx, blob->data);
948         talloc_free(ndr);
949
950         return NDR_ERR_SUCCESS;
951 }
952
953 /*
954   generic ndr_size_*() handler for structures
955 */
956 _PUBLIC_ size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push, struct smb_iconv_convenience *iconv_convenience)
957 {
958         struct ndr_push *ndr;
959         enum ndr_err_code status;
960         size_t ret;
961
962         /* avoid recursion */
963         if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
964
965         ndr = ndr_push_init_ctx(NULL, iconv_convenience);
966         if (!ndr) return 0;
967         ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
968         status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
969         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
970                 talloc_free(ndr);
971                 return 0;
972         }
973         ret = ndr->offset;
974         talloc_free(ndr);
975         return ret;
976 }
977
978 /*
979   generic ndr_size_*() handler for unions
980 */
981 _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)
982 {
983         struct ndr_push *ndr;
984         enum ndr_err_code status;
985         size_t ret;
986
987         /* avoid recursion */
988         if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
989
990         ndr = ndr_push_init_ctx(NULL, iconv_convenience);
991         if (!ndr) return 0;
992         ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
993
994         status = ndr_push_set_switch_value(ndr, p, level);
995         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
996                 talloc_free(ndr);
997                 return 0;
998         }
999         status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
1000         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1001                 talloc_free(ndr);
1002                 return 0;
1003         }
1004         ret = ndr->offset;
1005         talloc_free(ndr);
1006         return ret;
1007 }
1008
1009 /*
1010   get the current base for relative pointers for the push
1011 */
1012 _PUBLIC_ uint32_t ndr_push_get_relative_base_offset(struct ndr_push *ndr)
1013 {
1014         return ndr->relative_base_offset;
1015 }
1016
1017 /*
1018   restore the old base for relative pointers for the push
1019 */
1020 _PUBLIC_ void ndr_push_restore_relative_base_offset(struct ndr_push *ndr, uint32_t offset)
1021 {
1022         ndr->relative_base_offset = offset;
1023 }
1024
1025 /*
1026   setup the current base for relative pointers for the push
1027   called in the NDR_SCALAR stage
1028 */
1029 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset1(struct ndr_push *ndr, const void *p, uint32_t offset)
1030 {
1031         ndr->relative_base_offset = offset;
1032         return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1033 }
1034
1035 /*
1036   setup the current base for relative pointers for the push
1037   called in the NDR_BUFFERS stage
1038 */
1039 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset2(struct ndr_push *ndr, const void *p)
1040 {
1041         return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1042 }
1043
1044 /*
1045   push a relative object - stage1
1046   this is called during SCALARS processing
1047 */
1048 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
1049 {
1050         if (p == NULL) {
1051                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
1052                 return NDR_ERR_SUCCESS;
1053         }
1054         NDR_CHECK(ndr_push_align(ndr, 4));
1055         NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1056         return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
1057 }
1058
1059 /*
1060   push a relative object - stage2
1061   this is called during buffers processing
1062 */
1063 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
1064 {
1065         uint32_t save_offset;
1066         uint32_t ptr_offset = 0xFFFFFFFF;
1067         if (p == NULL) {
1068                 return NDR_ERR_SUCCESS;
1069         }
1070         save_offset = ndr->offset;
1071         NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1072         if (ptr_offset > ndr->offset) {
1073                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, 
1074                                       "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1075                                       ptr_offset, ndr->offset);
1076         }
1077         ndr->offset = ptr_offset;
1078         if (save_offset < ndr->relative_base_offset) {
1079                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, 
1080                                       "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1081                                       save_offset, ndr->relative_base_offset);
1082         }       
1083         NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
1084         ndr->offset = save_offset;
1085         return NDR_ERR_SUCCESS;
1086 }
1087
1088 /*
1089   get the current base for relative pointers for the pull
1090 */
1091 _PUBLIC_ uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull *ndr)
1092 {
1093         return ndr->relative_base_offset;
1094 }
1095
1096 /*
1097   restore the old base for relative pointers for the pull
1098 */
1099 _PUBLIC_ void ndr_pull_restore_relative_base_offset(struct ndr_pull *ndr, uint32_t offset)
1100 {
1101         ndr->relative_base_offset = offset;
1102 }
1103
1104 /*
1105   setup the current base for relative pointers for the pull
1106   called in the NDR_SCALAR stage
1107 */
1108 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset1(struct ndr_pull *ndr, const void *p, uint32_t offset)
1109 {
1110         ndr->relative_base_offset = offset;
1111         return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1112 }
1113
1114 /*
1115   setup the current base for relative pointers for the pull
1116   called in the NDR_BUFFERS stage
1117 */
1118 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset2(struct ndr_pull *ndr, const void *p)
1119 {
1120         return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1121 }
1122
1123 /*
1124   pull a relative object - stage1
1125   called during SCALARS processing
1126 */
1127 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
1128 {
1129         rel_offset += ndr->relative_base_offset;
1130         if (rel_offset > ndr->data_size) {
1131                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, 
1132                                       "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1133                                       rel_offset, ndr->data_size);
1134         }
1135         return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
1136 }
1137
1138 /*
1139   pull a relative object - stage2
1140   called during BUFFERS processing
1141 */
1142 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
1143 {
1144         uint32_t rel_offset;
1145         NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
1146         return ndr_pull_set_offset(ndr, rel_offset);
1147 }
1148
1149 const static struct {
1150         enum ndr_err_code err;
1151         const char *string;
1152 } ndr_err_code_strings[] = {
1153         { NDR_ERR_SUCCESS, "Success" },
1154         { NDR_ERR_ARRAY_SIZE, "Bad Array Size" },
1155         { NDR_ERR_BAD_SWITCH, "Bad Switch" },
1156         { NDR_ERR_OFFSET, "Offset Error" },
1157         { NDR_ERR_RELATIVE, "Relative Pointer Error" },
1158         { NDR_ERR_CHARCNV, "Character Conversion Error" },
1159         { NDR_ERR_LENGTH, "Length Error" },
1160         { NDR_ERR_SUBCONTEXT, "Subcontext Error" },
1161         { NDR_ERR_COMPRESSION, "Compression Error" },
1162         { NDR_ERR_STRING, "String Error" },
1163         { NDR_ERR_VALIDATE, "Validate Error" },
1164         { NDR_ERR_BUFSIZE, "Buffer Size Error" },
1165         { NDR_ERR_ALLOC, "Allocation Error" },
1166         { NDR_ERR_RANGE, "Range Error" },
1167         { NDR_ERR_TOKEN, "Token Error" },
1168         { NDR_ERR_IPV4ADDRESS, "IPv4 Address Error" },
1169         { NDR_ERR_INVALID_POINTER, "Invalid Pointer" },
1170         { NDR_ERR_UNREAD_BYTES, "Unread Bytes" },
1171         { 0, NULL }
1172 };
1173
1174 _PUBLIC_ const char *ndr_map_error2string(enum ndr_err_code ndr_err)
1175 {
1176         int i;
1177         for (i = 0; ndr_err_code_strings[i].string != NULL; i++) {
1178                 if (ndr_err_code_strings[i].err == ndr_err)
1179                         return ndr_err_code_strings[i].string;
1180         }
1181         return "Unknown error";
1182 }