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