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