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