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