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