libndr: Rename ndr64_transfer_syntax and null_ndr_syntax_id so they have a ndr_ prefix.
[kai/samba-autobuild/.git] / librpc / ndr / ndr.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    libndr interface
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Jelmer Vernooij 2005-2008
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
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
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_ndr = {
38   { 0x8a885d04, 0x1ceb, 0x11c9, {0x9f, 0xe8}, {0x08,0x00,0x2b,0x10,0x48,0x60} },
39   2
40 };
41
42 const struct ndr_syntax_id ndr_transfer_syntax_ndr64 = {
43   { 0x71710533, 0xbeba, 0x4937, {0x83, 0x19}, {0xb5,0xdb,0xef,0x9c,0xcc,0x36} },
44   1
45 };
46
47 const struct ndr_syntax_id ndr_syntax_id_null = {
48   { 0, 0, 0, { 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
49   0
50 };
51
52 /*
53   work out the number of bytes needed to align on a n byte boundary
54 */
55 _PUBLIC_ size_t ndr_align_size(uint32_t offset, size_t n)
56 {
57         if ((offset & (n-1)) == 0) return 0;
58         return n - (offset & (n-1));
59 }
60
61 /*
62   initialise a ndr parse structure from a data blob
63 */
64 _PUBLIC_ struct ndr_pull *ndr_pull_init_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx)
65 {
66         struct ndr_pull *ndr;
67
68         ndr = talloc_zero(mem_ctx, struct ndr_pull);
69         if (!ndr) return NULL;
70         ndr->current_mem_ctx = mem_ctx;
71
72         ndr->data = blob->data;
73         ndr->data_size = blob->length;
74
75         return ndr;
76 }
77
78 /*
79   advance by 'size' bytes
80 */
81 _PUBLIC_ enum ndr_err_code ndr_pull_advance(struct ndr_pull *ndr, uint32_t size)
82 {
83         ndr->offset += size;
84         if (ndr->offset > ndr->data_size) {
85                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, 
86                                       "ndr_pull_advance by %u failed",
87                                       size);
88         }
89         return NDR_ERR_SUCCESS;
90 }
91
92 /*
93   set the parse offset to 'ofs'
94 */
95 static enum ndr_err_code ndr_pull_set_offset(struct ndr_pull *ndr, uint32_t ofs)
96 {
97         ndr->offset = ofs;
98         if (ndr->offset > ndr->data_size) {
99                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, 
100                                       "ndr_pull_set_offset %u failed",
101                                       ofs);
102         }
103         return NDR_ERR_SUCCESS;
104 }
105
106 /* create a ndr_push structure, ready for some marshalling */
107 _PUBLIC_ struct ndr_push *ndr_push_init_ctx(TALLOC_CTX *mem_ctx)
108 {
109         struct ndr_push *ndr;
110
111         ndr = talloc_zero(mem_ctx, struct ndr_push);
112         if (!ndr) {
113                 return NULL;
114         }
115
116         ndr->flags = 0;
117         ndr->alloc_size = NDR_BASE_MARSHALL_SIZE;
118         ndr->data = talloc_array(ndr, uint8_t, ndr->alloc_size);
119         if (!ndr->data) {
120                 return NULL;
121         }
122
123         return ndr;
124 }
125
126 /* return a DATA_BLOB structure for the current ndr_push marshalled data */
127 _PUBLIC_ DATA_BLOB ndr_push_blob(struct ndr_push *ndr)
128 {
129         DATA_BLOB blob;
130         blob = data_blob_const(ndr->data, ndr->offset);
131         if (ndr->alloc_size > ndr->offset) {
132                 ndr->data[ndr->offset] = 0;
133         }
134         return blob;
135 }
136
137
138 /*
139   expand the available space in the buffer to ndr->offset + extra_size
140 */
141 _PUBLIC_ enum ndr_err_code ndr_push_expand(struct ndr_push *ndr, uint32_t extra_size)
142 {
143         uint32_t size = extra_size + ndr->offset;
144
145         if (size < ndr->offset) {
146                 /* extra_size overflowed the offset */
147                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, "Overflow in push_expand to %u",
148                                       size);
149         }
150
151         if (ndr->alloc_size > size) {
152                 return NDR_ERR_SUCCESS;
153         }
154
155         ndr->alloc_size += NDR_BASE_MARSHALL_SIZE;
156         if (size+1 > ndr->alloc_size) {
157                 ndr->alloc_size = size+1;
158         }
159         ndr->data = talloc_realloc(ndr, ndr->data, uint8_t, ndr->alloc_size);
160         if (!ndr->data) {
161                 return ndr_push_error(ndr, NDR_ERR_ALLOC, "Failed to push_expand to %u",
162                                       ndr->alloc_size);
163         }
164
165         return NDR_ERR_SUCCESS;
166 }
167
168 _PUBLIC_ void ndr_print_debug_helper(struct ndr_print *ndr, const char *format, ...) 
169 {
170         va_list ap;
171         char *s = NULL;
172         uint32_t i;
173         int ret;
174
175         va_start(ap, format);
176         ret = vasprintf(&s, format, ap);
177         va_end(ap);
178
179         if (ret == -1) {
180                 return;
181         }
182
183         if (ndr->no_newline) {
184                 DEBUGADD(1,("%s", s));
185                 free(s);
186                 return;
187         }
188
189         for (i=0;i<ndr->depth;i++) {
190                 DEBUGADD(1,("    "));
191         }
192
193         DEBUGADD(1,("%s\n", s));
194         free(s);
195 }
196
197 _PUBLIC_ void ndr_print_printf_helper(struct ndr_print *ndr, const char *format, ...) 
198 {
199         va_list ap;
200         uint32_t i;
201
202         if (!ndr->no_newline) {
203                 for (i=0;i<ndr->depth;i++) {
204                         printf("    ");
205                 }
206         }
207
208         va_start(ap, format);
209         vprintf(format, ap);
210         va_end(ap);
211         if (!ndr->no_newline) {
212                 printf("\n");
213         }
214 }
215
216 _PUBLIC_ void ndr_print_string_helper(struct ndr_print *ndr, const char *format, ...)
217 {
218         va_list ap;
219         uint32_t i;
220
221         if (!ndr->no_newline) {
222                 for (i=0;i<ndr->depth;i++) {
223                         ndr->private_data = talloc_asprintf_append_buffer(
224                                 (char *)ndr->private_data, "    ");
225                 }
226         }
227
228         va_start(ap, format);
229         ndr->private_data = talloc_vasprintf_append_buffer((char *)ndr->private_data, 
230                                                     format, ap);
231         va_end(ap);
232         if (!ndr->no_newline) {
233                 ndr->private_data = talloc_asprintf_append_buffer((char *)ndr->private_data,
234                                                                   "\n");
235         }
236 }
237
238 /*
239   a useful helper function for printing idl structures via DEBUG()
240 */
241 _PUBLIC_ void ndr_print_debug(ndr_print_fn_t fn, const char *name, void *ptr)
242 {
243         struct ndr_print *ndr;
244
245         DEBUG(1,(" "));
246
247         ndr = talloc_zero(NULL, struct ndr_print);
248         if (!ndr) return;
249         ndr->print = ndr_print_debug_helper;
250         ndr->depth = 1;
251         ndr->flags = 0;
252         fn(ndr, name, ptr);
253         talloc_free(ndr);
254 }
255
256 /*
257   a useful helper function for printing idl unions via DEBUG()
258 */
259 _PUBLIC_ void ndr_print_union_debug(ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
260 {
261         struct ndr_print *ndr;
262
263         DEBUG(1,(" "));
264
265         ndr = talloc_zero(NULL, struct ndr_print);
266         if (!ndr) return;
267         ndr->print = ndr_print_debug_helper;
268         ndr->depth = 1;
269         ndr->flags = 0;
270         ndr_print_set_switch_value(ndr, ptr, level);
271         fn(ndr, name, ptr);
272         talloc_free(ndr);
273 }
274
275 /*
276   a useful helper function for printing idl function calls via DEBUG()
277 */
278 _PUBLIC_ void ndr_print_function_debug(ndr_print_function_t fn, const char *name, int flags, void *ptr)
279 {
280         struct ndr_print *ndr;
281
282         DEBUG(1,(" "));
283
284         ndr = talloc_zero(NULL, struct ndr_print);
285         if (!ndr) return;
286         ndr->print = ndr_print_debug_helper;
287         ndr->depth = 1;
288         ndr->flags = 0;
289
290         fn(ndr, name, flags, ptr);
291         talloc_free(ndr);
292 }
293
294 /*
295   a useful helper function for printing idl structures to a string
296 */
297 _PUBLIC_ char *ndr_print_struct_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, void *ptr)
298 {
299         struct ndr_print *ndr;
300         char *ret = NULL;
301
302         ndr = talloc_zero(mem_ctx, struct ndr_print);
303         if (!ndr) return NULL;
304         ndr->private_data = talloc_strdup(ndr, "");
305         if (!ndr->private_data) {
306                 goto failed;
307         }
308         ndr->print = ndr_print_string_helper;
309         ndr->depth = 1;
310         ndr->flags = 0;
311
312         fn(ndr, name, ptr);
313         ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
314 failed:
315         talloc_free(ndr);
316         return ret;
317 }
318
319 /*
320   a useful helper function for printing idl unions to a string
321 */
322 _PUBLIC_ char *ndr_print_union_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
323 {
324         struct ndr_print *ndr;
325         char *ret = NULL;
326
327         ndr = talloc_zero(mem_ctx, struct ndr_print);
328         if (!ndr) return NULL;
329         ndr->private_data = talloc_strdup(ndr, "");
330         if (!ndr->private_data) {
331                 goto failed;
332         }
333         ndr->print = ndr_print_string_helper;
334         ndr->depth = 1;
335         ndr->flags = 0;
336         ndr_print_set_switch_value(ndr, ptr, level);
337         fn(ndr, name, ptr);
338         ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
339 failed:
340         talloc_free(ndr);
341         return ret;
342 }
343
344 /*
345   a useful helper function for printing idl function calls to a string
346 */
347 _PUBLIC_ char *ndr_print_function_string(TALLOC_CTX *mem_ctx,
348                                 ndr_print_function_t fn, const char *name, 
349                                 int flags, void *ptr)
350 {
351         struct ndr_print *ndr;
352         char *ret = NULL;
353
354         ndr = talloc_zero(mem_ctx, struct ndr_print);
355         if (!ndr) return NULL;
356         ndr->private_data = talloc_strdup(ndr, "");
357         if (!ndr->private_data) {
358                 goto failed;
359         }
360         ndr->print = ndr_print_string_helper;
361         ndr->depth = 1;
362         ndr->flags = 0;
363         fn(ndr, name, flags, ptr);
364         ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
365 failed:
366         talloc_free(ndr);
367         return ret;
368 }
369
370 _PUBLIC_ void ndr_set_flags(uint32_t *pflags, uint32_t new_flags)
371 {
372         /* the big/little endian flags are inter-dependent */
373         if (new_flags & LIBNDR_FLAG_LITTLE_ENDIAN) {
374                 (*pflags) &= ~LIBNDR_FLAG_BIGENDIAN;
375                 (*pflags) &= ~LIBNDR_FLAG_NDR64;
376         }
377         if (new_flags & LIBNDR_FLAG_BIGENDIAN) {
378                 (*pflags) &= ~LIBNDR_FLAG_LITTLE_ENDIAN;
379                 (*pflags) &= ~LIBNDR_FLAG_NDR64;
380         }
381         if (new_flags & LIBNDR_FLAG_REMAINING) {
382                 (*pflags) &= ~LIBNDR_ALIGN_FLAGS;
383         }
384         if (new_flags & LIBNDR_ALIGN_FLAGS) {
385                 (*pflags) &= ~LIBNDR_FLAG_REMAINING;
386         }
387         if (new_flags & LIBNDR_FLAG_NO_RELATIVE_REVERSE) {
388                 (*pflags) &= ~LIBNDR_FLAG_RELATIVE_REVERSE;
389         }
390         (*pflags) |= new_flags;
391 }
392
393 /*
394   return and possibly log an NDR error
395 */
396 _PUBLIC_ enum ndr_err_code ndr_pull_error(struct ndr_pull *ndr,
397                                  enum ndr_err_code ndr_err,
398                                  const char *format, ...)
399 {
400         char *s=NULL;
401         va_list ap;
402         int ret;
403
404         va_start(ap, format);
405         ret = vasprintf(&s, format, ap);
406         va_end(ap);
407
408         if (ret == -1) {
409                 return NDR_ERR_ALLOC;
410         }
411
412         DEBUG(1,("ndr_pull_error(%u): %s\n", ndr_err, s));
413
414         free(s);
415
416         return ndr_err;
417 }
418
419 /*
420   return and possibly log an NDR error
421 */
422 _PUBLIC_ enum ndr_err_code ndr_push_error(struct ndr_push *ndr,
423                                  enum ndr_err_code ndr_err,
424                                  const char *format, ...) 
425 {
426         char *s=NULL;
427         va_list ap;
428         int ret;
429
430         va_start(ap, format);
431         ret = vasprintf(&s, format, ap);
432         va_end(ap);
433
434         if (ret == -1) {
435                 return NDR_ERR_ALLOC;
436         }
437
438         DEBUG(1,("ndr_push_error(%u): %s\n", ndr_err, s));
439
440         free(s);
441
442         return ndr_err;
443 }
444
445 /*
446   handle subcontext buffers, which in midl land are user-marshalled, but
447   we use magic in pidl to make them easier to cope with
448 */
449 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_start(struct ndr_pull *ndr,
450                                    struct ndr_pull **_subndr,
451                                    size_t header_size,
452                                    ssize_t size_is)
453 {
454         struct ndr_pull *subndr;
455         uint32_t r_content_size;
456         bool force_le = false;
457         bool force_be = false;
458
459         switch (header_size) {
460         case 0: {
461                 uint32_t content_size = ndr->data_size - ndr->offset;
462                 if (size_is >= 0) {
463                         content_size = size_is;
464                 }
465                 r_content_size = content_size;
466                 break;
467         }
468
469         case 2: {
470                 uint16_t content_size;
471                 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &content_size));
472                 if (size_is >= 0 && size_is != content_size) {
473                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d", 
474                                                 (int)size_is, (int)content_size);
475                 }
476                 r_content_size = content_size;
477                 break;
478         }
479
480         case 4: {
481                 uint32_t content_size;
482                 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &content_size));
483                 if (size_is >= 0 && size_is != content_size) {
484                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d", 
485                                                 (int)size_is, (int)content_size);
486                 }
487                 r_content_size = content_size;
488                 break;
489         }
490         case 0xFFFFFC01: {
491                 /*
492                  * Common Type Header for the Serialization Stream
493                  * See [MS-RPCE] 2.2.6 Type Serialization Version 1
494                  */
495                 uint8_t version;
496                 uint8_t drep;
497                 uint16_t hdrlen;
498                 uint32_t filler;
499                 uint32_t content_size;
500                 uint32_t reserved;
501
502                 /* version */
503                 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &version));
504
505                 if (version != 1) {
506                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
507                                               "Bad subcontext (PULL) Common Type Header version %d != 1",
508                                               (int)version);
509                 }
510
511                 /*
512                  * 0x10 little endian
513                  * 0x00 big endian
514                  */
515                 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &drep));
516                 if (drep == 0x10) {
517                         force_le = true;
518                 } else if (drep == 0x00) {
519                         force_be = true;
520                 } else {
521                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
522                                               "Bad subcontext (PULL) Common Type Header invalid drep 0x%02X",
523                                               (unsigned int)drep);
524                 }
525
526                 /* length of the "Private Header for Constructed Type" */
527                 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &hdrlen));
528                 if (hdrlen != 8) {
529                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
530                                               "Bad subcontext (PULL) Common Type Header length %d != 8",
531                                               (int)hdrlen);
532                 }
533
534                 /* filler should be ignored */
535                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &filler));
536
537                 /*
538                  * Private Header for Constructed Type
539                  */
540                 /* length - will be updated latter */
541                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
542                 if (size_is >= 0 && size_is != content_size) {
543                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
544                                               (int)size_is, (int)content_size);
545                 }
546                 /* the content size must be a multiple of 8 */
547                 if ((content_size % 8) != 0) {
548                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
549                                               "Bad subcontext (PULL) size_is(%d) not padded to 8 content_size %d",
550                                               (int)size_is, (int)content_size);
551                 }
552                 r_content_size = content_size;
553
554                 /* reserved */
555                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &reserved));
556                 break;
557         }
558         default:
559                 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) header_size %d", 
560                                       (int)header_size);
561         }
562
563         NDR_PULL_NEED_BYTES(ndr, r_content_size);
564
565         subndr = talloc_zero(ndr, struct ndr_pull);
566         NDR_ERR_HAVE_NO_MEMORY(subndr);
567         subndr->flags           = ndr->flags & ~LIBNDR_FLAG_NDR64;
568         subndr->current_mem_ctx = ndr->current_mem_ctx;
569
570         subndr->data = ndr->data + ndr->offset;
571         subndr->offset = 0;
572         subndr->data_size = r_content_size;
573
574         if (force_le) {
575                 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_LITTLE_ENDIAN);
576         } else if (force_be) {
577                 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_BIGENDIAN);
578         }
579
580         *_subndr = subndr;
581         return NDR_ERR_SUCCESS;
582 }
583
584 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_end(struct ndr_pull *ndr,
585                                  struct ndr_pull *subndr,
586                                  size_t header_size,
587                                  ssize_t size_is)
588 {
589         uint32_t advance;
590         if (size_is >= 0) {
591                 advance = size_is;
592         } else if (header_size > 0) {
593                 advance = subndr->data_size;
594         } else {
595                 advance = subndr->offset;
596         }
597         NDR_CHECK(ndr_pull_advance(ndr, advance));
598         return NDR_ERR_SUCCESS;
599 }
600
601 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_start(struct ndr_push *ndr,
602                                    struct ndr_push **_subndr,
603                                    size_t header_size,
604                                    ssize_t size_is)
605 {
606         struct ndr_push *subndr;
607
608         subndr = ndr_push_init_ctx(ndr);
609         NDR_ERR_HAVE_NO_MEMORY(subndr);
610         subndr->flags   = ndr->flags & ~LIBNDR_FLAG_NDR64;
611
612         if (size_is > 0) {
613                 NDR_CHECK(ndr_push_zero(subndr, size_is));
614                 subndr->offset = 0;
615                 subndr->relative_end_offset = size_is;
616         }
617
618         *_subndr = subndr;
619         return NDR_ERR_SUCCESS;
620 }
621
622 /*
623   push a subcontext header 
624 */
625 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_end(struct ndr_push *ndr,
626                                  struct ndr_push *subndr,
627                                  size_t header_size,
628                                  ssize_t size_is)
629 {
630         ssize_t padding_len;
631
632         if (size_is >= 0) {
633                 padding_len = size_is - subndr->offset;
634                 if (padding_len < 0) {
635                         return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
636                                               (int)subndr->offset, (int)size_is);
637                 }
638                 subndr->offset = size_is;
639         }
640
641         switch (header_size) {
642         case 0: 
643                 break;
644
645         case 2: 
646                 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, subndr->offset));
647                 break;
648
649         case 4: 
650                 NDR_CHECK(ndr_push_uint3264(ndr, NDR_SCALARS, subndr->offset));
651                 break;
652
653         case 0xFFFFFC01:
654                 /*
655                  * Common Type Header for the Serialization Stream
656                  * See [MS-RPCE] 2.2.6 Type Serialization Version 1
657                  */
658                 padding_len = NDR_ROUND(subndr->offset, 8) - subndr->offset;
659                 if (padding_len > 0) {
660                         NDR_CHECK(ndr_push_zero(subndr, padding_len));
661                 }
662
663                 /* version */
664                 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, 1));
665
666                 /*
667                  * 0x10 little endian
668                  * 0x00 big endian
669                  */
670                 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, NDR_BE(ndr)?0x00:0x10));
671
672                 /* length of the "Private Header for Constructed Type" */
673                 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 8));
674
675                 /* filler */
676                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0xCCCCCCCC));
677
678                 /*
679                  * Private Header for Constructed Type
680                  */
681                 /* length - will be updated latter */
682                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
683
684                 /* reserved */
685                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
686                 break;
687
688         default:
689                 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext header size %d", 
690                                       (int)header_size);
691         }
692
693         NDR_CHECK(ndr_push_bytes(ndr, subndr->data, subndr->offset));
694         return NDR_ERR_SUCCESS;
695 }
696
697 /*
698   store a token in the ndr context, for later retrieval
699 */
700 _PUBLIC_ enum ndr_err_code ndr_token_store(TALLOC_CTX *mem_ctx,
701                          struct ndr_token_list **list, 
702                          const void *key, 
703                          uint32_t value)
704 {
705         struct ndr_token_list *tok;
706         tok = talloc(mem_ctx, struct ndr_token_list);
707         NDR_ERR_HAVE_NO_MEMORY(tok);
708         tok->key = key;
709         tok->value = value;
710         DLIST_ADD((*list), tok);
711         return NDR_ERR_SUCCESS;
712 }
713
714 /*
715   retrieve a token from a ndr context, using cmp_fn to match the tokens
716 */
717 _PUBLIC_ enum ndr_err_code ndr_token_retrieve_cmp_fn(struct ndr_token_list **list, const void *key, uint32_t *v,
718                                    comparison_fn_t _cmp_fn, bool _remove_tok)
719 {
720         struct ndr_token_list *tok;
721         for (tok=*list;tok;tok=tok->next) {
722                 if (_cmp_fn && _cmp_fn(tok->key,key)==0) goto found;
723                 else if (!_cmp_fn && tok->key == key) goto found;
724         }
725         return NDR_ERR_TOKEN;
726 found:
727         *v = tok->value;
728         if (_remove_tok) {
729                 DLIST_REMOVE((*list), tok);
730                 talloc_free(tok);
731         }
732         return NDR_ERR_SUCCESS;
733 }
734
735 /*
736   retrieve a token from a ndr context
737 */
738 _PUBLIC_ enum ndr_err_code ndr_token_retrieve(struct ndr_token_list **list, const void *key, uint32_t *v)
739 {
740         return ndr_token_retrieve_cmp_fn(list, key, v, NULL, true);
741 }
742
743 /*
744   peek at but don't removed a token from a ndr context
745 */
746 _PUBLIC_ uint32_t ndr_token_peek(struct ndr_token_list **list, const void *key)
747 {
748         enum ndr_err_code status;
749         uint32_t v;
750
751         status = ndr_token_retrieve_cmp_fn(list, key, &v, NULL, false);
752         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
753                 return 0;
754         }
755
756         return v;
757 }
758
759 /*
760   pull an array size field and add it to the array_size_list token list
761 */
762 _PUBLIC_ enum ndr_err_code ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
763 {
764         uint32_t size;
765         NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &size));
766         return ndr_token_store(ndr, &ndr->array_size_list, p, size);
767 }
768
769 /*
770   get the stored array size field
771 */
772 _PUBLIC_ uint32_t ndr_get_array_size(struct ndr_pull *ndr, const void *p)
773 {
774         return ndr_token_peek(&ndr->array_size_list, p);
775 }
776
777 /*
778   check the stored array size field
779 */
780 _PUBLIC_ enum ndr_err_code ndr_check_array_size(struct ndr_pull *ndr, void *p, uint32_t size)
781 {
782         uint32_t stored;
783         stored = ndr_token_peek(&ndr->array_size_list, p);
784         if (stored != size) {
785                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
786                                       "Bad array size - got %u expected %u\n",
787                                       stored, size);
788         }
789         return NDR_ERR_SUCCESS;
790 }
791
792 /*
793   pull an array length field and add it to the array_length_list token list
794 */
795 _PUBLIC_ enum ndr_err_code ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
796 {
797         uint32_t length, offset;
798         NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &offset));
799         if (offset != 0) {
800                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
801                                       "non-zero array offset %u\n", offset);
802         }
803         NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &length));
804         return ndr_token_store(ndr, &ndr->array_length_list, p, length);
805 }
806
807 /*
808   get the stored array length field
809 */
810 _PUBLIC_ uint32_t ndr_get_array_length(struct ndr_pull *ndr, const void *p)
811 {
812         return ndr_token_peek(&ndr->array_length_list, p);
813 }
814
815 /*
816   check the stored array length field
817 */
818 _PUBLIC_ enum ndr_err_code ndr_check_array_length(struct ndr_pull *ndr, void *p, uint32_t length)
819 {
820         uint32_t stored;
821         stored = ndr_token_peek(&ndr->array_length_list, p);
822         if (stored != length) {
823                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
824                                       "Bad array length - got %u expected %u\n",
825                                       stored, length);
826         }
827         return NDR_ERR_SUCCESS;
828 }
829
830 _PUBLIC_ enum ndr_err_code ndr_push_pipe_chunk_trailer(struct ndr_push *ndr, int ndr_flags, uint32_t count)
831 {
832         if (ndr->flags & LIBNDR_FLAG_NDR64) {
833                 int64_t tmp = 0 - (int64_t)count;
834                 uint64_t ncount = tmp;
835
836                 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, ncount));
837         }
838
839         return NDR_ERR_SUCCESS;
840 }
841
842 _PUBLIC_ enum ndr_err_code ndr_check_pipe_chunk_trailer(struct ndr_pull *ndr, int ndr_flags, uint32_t count)
843 {
844         if (ndr->flags & LIBNDR_FLAG_NDR64) {
845                 int64_t tmp = 0 - (int64_t)count;
846                 uint64_t ncount1 = tmp;
847                 uint64_t ncount2;
848
849                 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, &ncount2));
850                 if (ncount1 == ncount2) {
851                         return NDR_ERR_SUCCESS;
852                 }
853
854                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
855                         "Bad pipe trailer[%lld should be %lld] size was %lu\"",
856                         (unsigned long long)ncount2,
857                         (unsigned long long)ncount1,
858                         (unsigned long)count);
859         }
860
861         return NDR_ERR_SUCCESS;
862 }
863
864 /*
865   store a switch value
866  */
867 _PUBLIC_ enum ndr_err_code ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
868 {
869         return ndr_token_store(ndr, &ndr->switch_list, p, val);
870 }
871
872 _PUBLIC_ enum ndr_err_code ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
873 {
874         return ndr_token_store(ndr, &ndr->switch_list, p, val);
875 }
876
877 _PUBLIC_ enum ndr_err_code ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
878 {
879         return ndr_token_store(ndr, &ndr->switch_list, p, val);
880 }
881
882 /*
883   retrieve a switch value
884  */
885 _PUBLIC_ uint32_t ndr_push_get_switch_value(struct ndr_push *ndr, const void *p)
886 {
887         return ndr_token_peek(&ndr->switch_list, p);
888 }
889
890 _PUBLIC_ uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
891 {
892         return ndr_token_peek(&ndr->switch_list, p);
893 }
894
895 _PUBLIC_ uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
896 {
897         return ndr_token_peek(&ndr->switch_list, p);
898 }
899
900 /*
901   pull a struct from a blob using NDR
902 */
903 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
904                               ndr_pull_flags_fn_t fn)
905 {
906         struct ndr_pull *ndr;
907         ndr = ndr_pull_init_blob(blob, mem_ctx);
908         NDR_ERR_HAVE_NO_MEMORY(ndr);
909         NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
910         talloc_free(ndr);
911         return NDR_ERR_SUCCESS;
912 }
913
914 /*
915   pull a struct from a blob using NDR - failing if all bytes are not consumed
916 */
917 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
918                                                     void *p, ndr_pull_flags_fn_t fn)
919 {
920         struct ndr_pull *ndr;
921         uint32_t highest_ofs;
922         ndr = ndr_pull_init_blob(blob, mem_ctx);
923         NDR_ERR_HAVE_NO_MEMORY(ndr);
924         NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
925         if (ndr->offset > ndr->relative_highest_offset) {
926                 highest_ofs = ndr->offset;
927         } else {
928                 highest_ofs = ndr->relative_highest_offset;
929         }
930         if (highest_ofs < ndr->data_size) {
931                 enum ndr_err_code ret;
932                 ret = ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
933                                      "not all bytes consumed ofs[%u] size[%u]",
934                                      highest_ofs, ndr->data_size);
935                 talloc_free(ndr);
936                 return ret;
937         }
938         talloc_free(ndr);
939         return NDR_ERR_SUCCESS;
940 }
941
942 /*
943   pull a union from a blob using NDR, given the union discriminator
944 */
945 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
946                                                void *p,
947                              uint32_t level, ndr_pull_flags_fn_t fn)
948 {
949         struct ndr_pull *ndr;
950         ndr = ndr_pull_init_blob(blob, mem_ctx);
951         NDR_ERR_HAVE_NO_MEMORY(ndr);
952         NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
953         NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
954         talloc_free(ndr);
955         return NDR_ERR_SUCCESS;
956 }
957
958 /*
959   pull a union from a blob using NDR, given the union discriminator,
960   failing if all bytes are not consumed
961 */
962 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
963                                                    void *p,
964                              uint32_t level, ndr_pull_flags_fn_t fn)
965 {
966         struct ndr_pull *ndr;
967         uint32_t highest_ofs;
968         ndr = ndr_pull_init_blob(blob, mem_ctx);
969         NDR_ERR_HAVE_NO_MEMORY(ndr);
970         NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
971         NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
972         if (ndr->offset > ndr->relative_highest_offset) {
973                 highest_ofs = ndr->offset;
974         } else {
975                 highest_ofs = ndr->relative_highest_offset;
976         }
977         if (highest_ofs < ndr->data_size) {
978                 enum ndr_err_code ret;
979                 ret = ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
980                                      "not all bytes consumed ofs[%u] size[%u]",
981                                      highest_ofs, ndr->data_size);
982                 talloc_free(ndr);
983                 return ret;
984         }
985         talloc_free(ndr);
986         return NDR_ERR_SUCCESS;
987 }
988
989 /*
990   push a struct to a blob using NDR
991 */
992 _PUBLIC_ enum ndr_err_code ndr_push_struct_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, const void *p, ndr_push_flags_fn_t fn)
993 {
994         struct ndr_push *ndr;
995         ndr = ndr_push_init_ctx(mem_ctx);
996         NDR_ERR_HAVE_NO_MEMORY(ndr);
997
998         NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
999
1000         *blob = ndr_push_blob(ndr);
1001         talloc_steal(mem_ctx, blob->data);
1002         talloc_free(ndr);
1003
1004         return NDR_ERR_SUCCESS;
1005 }
1006
1007 /*
1008   push a union to a blob using NDR
1009 */
1010 _PUBLIC_ enum ndr_err_code ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
1011                              uint32_t level, ndr_push_flags_fn_t fn)
1012 {
1013         struct ndr_push *ndr;
1014         ndr = ndr_push_init_ctx(mem_ctx);
1015         NDR_ERR_HAVE_NO_MEMORY(ndr);
1016
1017         NDR_CHECK(ndr_push_set_switch_value(ndr, p, level));
1018         NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
1019
1020         *blob = ndr_push_blob(ndr);
1021         talloc_steal(mem_ctx, blob->data);
1022         talloc_free(ndr);
1023
1024         return NDR_ERR_SUCCESS;
1025 }
1026
1027 /*
1028   generic ndr_size_*() handler for structures
1029 */
1030 _PUBLIC_ size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push)
1031 {
1032         struct ndr_push *ndr;
1033         enum ndr_err_code status;
1034         size_t ret;
1035
1036         /* avoid recursion */
1037         if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
1038
1039         ndr = ndr_push_init_ctx(NULL);
1040         if (!ndr) return 0;
1041         ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
1042         status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
1043         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1044                 talloc_free(ndr);
1045                 return 0;
1046         }
1047         ret = ndr->offset;
1048         talloc_free(ndr);
1049         return ret;
1050 }
1051
1052 /*
1053   generic ndr_size_*() handler for unions
1054 */
1055 _PUBLIC_ size_t ndr_size_union(const void *p, int flags, uint32_t level, ndr_push_flags_fn_t push)
1056 {
1057         struct ndr_push *ndr;
1058         enum ndr_err_code status;
1059         size_t ret;
1060
1061         /* avoid recursion */
1062         if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
1063
1064         ndr = ndr_push_init_ctx(NULL);
1065         if (!ndr) return 0;
1066         ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
1067
1068         status = ndr_push_set_switch_value(ndr, p, level);
1069         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1070                 talloc_free(ndr);
1071                 return 0;
1072         }
1073         status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
1074         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1075                 talloc_free(ndr);
1076                 return 0;
1077         }
1078         ret = ndr->offset;
1079         talloc_free(ndr);
1080         return ret;
1081 }
1082
1083 /*
1084   get the current base for relative pointers for the push
1085 */
1086 _PUBLIC_ uint32_t ndr_push_get_relative_base_offset(struct ndr_push *ndr)
1087 {
1088         return ndr->relative_base_offset;
1089 }
1090
1091 /*
1092   restore the old base for relative pointers for the push
1093 */
1094 _PUBLIC_ void ndr_push_restore_relative_base_offset(struct ndr_push *ndr, uint32_t offset)
1095 {
1096         ndr->relative_base_offset = offset;
1097 }
1098
1099 /*
1100   setup the current base for relative pointers for the push
1101   called in the NDR_SCALAR stage
1102 */
1103 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset1(struct ndr_push *ndr, const void *p, uint32_t offset)
1104 {
1105         ndr->relative_base_offset = offset;
1106         return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1107 }
1108
1109 /*
1110   setup the current base for relative pointers for the push
1111   called in the NDR_BUFFERS stage
1112 */
1113 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset2(struct ndr_push *ndr, const void *p)
1114 {
1115         return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1116 }
1117
1118 /*
1119   push a relative object - stage1
1120   this is called during SCALARS processing
1121 */
1122 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
1123 {
1124         if (p == NULL) {
1125                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
1126                 return NDR_ERR_SUCCESS;
1127         }
1128         NDR_CHECK(ndr_push_align(ndr, 4));
1129         NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1130         return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
1131 }
1132
1133 /*
1134   push a short relative object - stage1
1135   this is called during SCALARS processing
1136 */
1137 _PUBLIC_ enum ndr_err_code ndr_push_short_relative_ptr1(struct ndr_push *ndr, const void *p)
1138 {
1139         if (p == NULL) {
1140                 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 0));
1141                 return NDR_ERR_SUCCESS;
1142         }
1143         NDR_CHECK(ndr_push_align(ndr, 2));
1144         NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1145         return ndr_push_uint16(ndr, NDR_SCALARS, 0xFFFF);
1146 }
1147 /*
1148   push a relative object - stage2
1149   this is called during buffers processing
1150 */
1151 static enum ndr_err_code ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
1152 {
1153         uint32_t save_offset;
1154         uint32_t ptr_offset = 0xFFFFFFFF;
1155         if (p == NULL) {
1156                 return NDR_ERR_SUCCESS;
1157         }
1158         save_offset = ndr->offset;
1159         NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1160         if (ptr_offset > ndr->offset) {
1161                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, 
1162                                       "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1163                                       ptr_offset, ndr->offset);
1164         }
1165         ndr->offset = ptr_offset;
1166         if (save_offset < ndr->relative_base_offset) {
1167                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, 
1168                                       "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1169                                       save_offset, ndr->relative_base_offset);
1170         }       
1171         NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
1172         ndr->offset = save_offset;
1173         return NDR_ERR_SUCCESS;
1174 }
1175 /*
1176   push a short relative object - stage2
1177   this is called during buffers processing
1178 */
1179 _PUBLIC_ enum ndr_err_code ndr_push_short_relative_ptr2(struct ndr_push *ndr, const void *p)
1180 {
1181         uint32_t save_offset;
1182         uint32_t ptr_offset = 0xFFFF;
1183         if (p == NULL) {
1184                 return NDR_ERR_SUCCESS;
1185         }
1186         save_offset = ndr->offset;
1187         NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1188         if (ptr_offset > ndr->offset) {
1189                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1190                                       "ndr_push_short_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1191                                       ptr_offset, ndr->offset);
1192         }
1193         ndr->offset = ptr_offset;
1194         if (save_offset < ndr->relative_base_offset) {
1195                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1196                                       "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1197                                       save_offset, ndr->relative_base_offset);
1198         }
1199         NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
1200         ndr->offset = save_offset;
1201         return NDR_ERR_SUCCESS;
1202 }
1203
1204 /*
1205   push a relative object - stage2 start
1206   this is called during buffers processing
1207 */
1208 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_start(struct ndr_push *ndr, const void *p)
1209 {
1210         if (p == NULL) {
1211                 return NDR_ERR_SUCCESS;
1212         }
1213         if (!(ndr->flags & LIBNDR_FLAG_RELATIVE_REVERSE)) {
1214                 uint32_t relative_offset;
1215                 size_t pad;
1216                 size_t align = 1;
1217
1218                 if (ndr->offset < ndr->relative_base_offset) {
1219                         return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1220                                       "ndr_push_relative_ptr2_start ndr->offset(%u) < ndr->relative_base_offset(%u)",
1221                                       ndr->offset, ndr->relative_base_offset);
1222                 }
1223
1224                 relative_offset = ndr->offset - ndr->relative_base_offset;
1225
1226                 if (ndr->flags & LIBNDR_FLAG_NOALIGN) {
1227                         align = 1;
1228                 } else if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1229                         align = 2;
1230                 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1231                         align = 4;
1232                 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1233                         align = 8;
1234                 }
1235
1236                 pad = ndr_align_size(relative_offset, align);
1237                 if (pad) {
1238                         NDR_CHECK(ndr_push_zero(ndr, pad));
1239                 }
1240
1241                 return ndr_push_relative_ptr2(ndr, p);
1242         }
1243         if (ndr->relative_end_offset == -1) {
1244                 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1245                               "ndr_push_relative_ptr2_start RELATIVE_REVERSE flag set and relative_end_offset %d",
1246                               ndr->relative_end_offset);
1247         }
1248         NDR_CHECK(ndr_token_store(ndr, &ndr->relative_begin_list, p, ndr->offset));
1249         return NDR_ERR_SUCCESS;
1250 }
1251
1252 /*
1253   push a relative object - stage2 end
1254   this is called during buffers processing
1255 */
1256 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_end(struct ndr_push *ndr, const void *p)
1257 {
1258         uint32_t begin_offset = 0xFFFFFFFF;
1259         ssize_t len;
1260         uint32_t correct_offset = 0;
1261         uint32_t align = 1;
1262         uint32_t pad = 0;
1263
1264         if (p == NULL) {
1265                 return NDR_ERR_SUCCESS;
1266         }
1267
1268         if (!(ndr->flags & LIBNDR_FLAG_RELATIVE_REVERSE)) {
1269                 return NDR_ERR_SUCCESS;
1270         }
1271
1272         if (ndr->flags & LIBNDR_FLAG_NO_NDR_SIZE) {
1273                 /* better say more than calculation a too small buffer */
1274                 NDR_PUSH_ALIGN(ndr, 8);
1275                 return NDR_ERR_SUCCESS;
1276         }
1277
1278         if (ndr->relative_end_offset < ndr->offset) {
1279                 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1280                                       "ndr_push_relative_ptr2_end:"
1281                                       "relative_end_offset %u < offset %u",
1282                                       ndr->relative_end_offset, ndr->offset);
1283         }
1284
1285         NDR_CHECK(ndr_token_retrieve(&ndr->relative_begin_list, p, &begin_offset));
1286
1287         /* we have marshalled a buffer, see how long it was */
1288         len = ndr->offset - begin_offset;
1289
1290         if (len < 0) {
1291                 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1292                                       "ndr_push_relative_ptr2_end:"
1293                                       "offset %u - begin_offset %u < 0",
1294                                       ndr->offset, begin_offset);
1295         }
1296
1297         if (ndr->relative_end_offset < len) {
1298                 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1299                                       "ndr_push_relative_ptr2_end:"
1300                                       "relative_end_offset %u < len %lld",
1301                                       ndr->offset, (long long)len);
1302         }
1303
1304         /* the reversed offset is at the end of the main buffer */
1305         correct_offset = ndr->relative_end_offset - len;
1306
1307         if (ndr->flags & LIBNDR_FLAG_NOALIGN) {
1308                 align = 1;
1309         } else if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1310                 align = 2;
1311         } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1312                 align = 4;
1313         } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1314                 align = 8;
1315         }
1316
1317         pad = ndr_align_size(correct_offset, align);
1318         if (pad) {
1319                 correct_offset += pad;
1320                 correct_offset -= align;
1321         }
1322
1323         if (correct_offset < begin_offset) {
1324                 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1325                                       "ndr_push_relative_ptr2_end: "
1326                                       "correct_offset %u < begin_offset %u",
1327                                       correct_offset, begin_offset);
1328         }
1329
1330         if (len > 0) {
1331                 uint32_t clear_size = correct_offset - begin_offset;
1332
1333                 clear_size = MIN(clear_size, len);
1334
1335                 /* now move the marshalled buffer to the end of the main buffer */
1336                 memmove(ndr->data + correct_offset, ndr->data + begin_offset, len);
1337
1338                 if (clear_size) {
1339                         /* and wipe out old buffer within the main buffer */
1340                         memset(ndr->data + begin_offset, '\0', clear_size);
1341                 }
1342         }
1343
1344         /* and set the end offset for the next buffer */
1345         ndr->relative_end_offset = correct_offset;
1346
1347         /* finally write the offset to the main buffer */
1348         ndr->offset = correct_offset;
1349         NDR_CHECK(ndr_push_relative_ptr2(ndr, p));
1350
1351         /* restore to where we were in the main buffer */
1352         ndr->offset = begin_offset;
1353
1354         return NDR_ERR_SUCCESS;
1355 }
1356
1357 /*
1358   get the current base for relative pointers for the pull
1359 */
1360 _PUBLIC_ uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull *ndr)
1361 {
1362         return ndr->relative_base_offset;
1363 }
1364
1365 /*
1366   restore the old base for relative pointers for the pull
1367 */
1368 _PUBLIC_ void ndr_pull_restore_relative_base_offset(struct ndr_pull *ndr, uint32_t offset)
1369 {
1370         ndr->relative_base_offset = offset;
1371 }
1372
1373 /*
1374   setup the current base for relative pointers for the pull
1375   called in the NDR_SCALAR stage
1376 */
1377 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset1(struct ndr_pull *ndr, const void *p, uint32_t offset)
1378 {
1379         ndr->relative_base_offset = offset;
1380         return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1381 }
1382
1383 /*
1384   setup the current base for relative pointers for the pull
1385   called in the NDR_BUFFERS stage
1386 */
1387 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset2(struct ndr_pull *ndr, const void *p)
1388 {
1389         return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1390 }
1391
1392 /*
1393   pull a relative object - stage1
1394   called during SCALARS processing
1395 */
1396 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
1397 {
1398         rel_offset += ndr->relative_base_offset;
1399         if (rel_offset > ndr->data_size) {
1400                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, 
1401                                       "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1402                                       rel_offset, ndr->data_size);
1403         }
1404         return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
1405 }
1406
1407 /*
1408   pull a relative object - stage2
1409   called during BUFFERS processing
1410 */
1411 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
1412 {
1413         uint32_t rel_offset;
1414         NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
1415         return ndr_pull_set_offset(ndr, rel_offset);
1416 }
1417
1418 const static struct {
1419         enum ndr_err_code err;
1420         const char *string;
1421 } ndr_err_code_strings[] = {
1422         { NDR_ERR_SUCCESS, "Success" },
1423         { NDR_ERR_ARRAY_SIZE, "Bad Array Size" },
1424         { NDR_ERR_BAD_SWITCH, "Bad Switch" },
1425         { NDR_ERR_OFFSET, "Offset Error" },
1426         { NDR_ERR_RELATIVE, "Relative Pointer Error" },
1427         { NDR_ERR_CHARCNV, "Character Conversion Error" },
1428         { NDR_ERR_LENGTH, "Length Error" },
1429         { NDR_ERR_SUBCONTEXT, "Subcontext Error" },
1430         { NDR_ERR_COMPRESSION, "Compression Error" },
1431         { NDR_ERR_STRING, "String Error" },
1432         { NDR_ERR_VALIDATE, "Validate Error" },
1433         { NDR_ERR_BUFSIZE, "Buffer Size Error" },
1434         { NDR_ERR_ALLOC, "Allocation Error" },
1435         { NDR_ERR_RANGE, "Range Error" },
1436         { NDR_ERR_TOKEN, "Token Error" },
1437         { NDR_ERR_IPV4ADDRESS, "IPv4 Address Error" },
1438         { NDR_ERR_INVALID_POINTER, "Invalid Pointer" },
1439         { NDR_ERR_UNREAD_BYTES, "Unread Bytes" },
1440         { NDR_ERR_NDR64, "NDR64 assertion error" },
1441         { 0, NULL }
1442 };
1443
1444 _PUBLIC_ const char *ndr_map_error2string(enum ndr_err_code ndr_err)
1445 {
1446         int i;
1447         for (i = 0; ndr_err_code_strings[i].string != NULL; i++) {
1448                 if (ndr_err_code_strings[i].err == ndr_err)
1449                         return ndr_err_code_strings[i].string;
1450         }
1451         return "Unknown error";
1452 }