librpc/ndr: handle NOALIGN flag for relative pointers and alignment DATA_BLOBs
[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 /*
833   store a switch value
834  */
835 _PUBLIC_ enum ndr_err_code ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
836 {
837         return ndr_token_store(ndr, &ndr->switch_list, p, val);
838 }
839
840 _PUBLIC_ enum ndr_err_code ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
841 {
842         return ndr_token_store(ndr, &ndr->switch_list, p, val);
843 }
844
845 _PUBLIC_ enum ndr_err_code ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
846 {
847         return ndr_token_store(ndr, &ndr->switch_list, p, val);
848 }
849
850 /*
851   retrieve a switch value
852  */
853 _PUBLIC_ uint32_t ndr_push_get_switch_value(struct ndr_push *ndr, const void *p)
854 {
855         return ndr_token_peek(&ndr->switch_list, p);
856 }
857
858 _PUBLIC_ uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
859 {
860         return ndr_token_peek(&ndr->switch_list, p);
861 }
862
863 _PUBLIC_ uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
864 {
865         return ndr_token_peek(&ndr->switch_list, p);
866 }
867
868 /*
869   pull a struct from a blob using NDR
870 */
871 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
872                               ndr_pull_flags_fn_t fn)
873 {
874         struct ndr_pull *ndr;
875         ndr = ndr_pull_init_blob(blob, mem_ctx);
876         NDR_ERR_HAVE_NO_MEMORY(ndr);
877         NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
878         talloc_free(ndr);
879         return NDR_ERR_SUCCESS;
880 }
881
882 /*
883   pull a struct from a blob using NDR - failing if all bytes are not consumed
884 */
885 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
886                                                     void *p, ndr_pull_flags_fn_t fn)
887 {
888         struct ndr_pull *ndr;
889         uint32_t highest_ofs;
890         ndr = ndr_pull_init_blob(blob, mem_ctx);
891         NDR_ERR_HAVE_NO_MEMORY(ndr);
892         NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
893         if (ndr->offset > ndr->relative_highest_offset) {
894                 highest_ofs = ndr->offset;
895         } else {
896                 highest_ofs = ndr->relative_highest_offset;
897         }
898         if (highest_ofs < ndr->data_size) {
899                 enum ndr_err_code ret;
900                 ret = ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
901                                      "not all bytes consumed ofs[%u] size[%u]",
902                                      highest_ofs, ndr->data_size);
903                 talloc_free(ndr);
904                 return ret;
905         }
906         talloc_free(ndr);
907         return NDR_ERR_SUCCESS;
908 }
909
910 /*
911   pull a union from a blob using NDR, given the union discriminator
912 */
913 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
914                                                void *p,
915                              uint32_t level, ndr_pull_flags_fn_t fn)
916 {
917         struct ndr_pull *ndr;
918         ndr = ndr_pull_init_blob(blob, mem_ctx);
919         NDR_ERR_HAVE_NO_MEMORY(ndr);
920         NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
921         NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
922         talloc_free(ndr);
923         return NDR_ERR_SUCCESS;
924 }
925
926 /*
927   pull a union from a blob using NDR, given the union discriminator,
928   failing if all bytes are not consumed
929 */
930 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
931                                                    void *p,
932                              uint32_t level, ndr_pull_flags_fn_t fn)
933 {
934         struct ndr_pull *ndr;
935         uint32_t highest_ofs;
936         ndr = ndr_pull_init_blob(blob, mem_ctx);
937         NDR_ERR_HAVE_NO_MEMORY(ndr);
938         NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
939         NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
940         if (ndr->offset > ndr->relative_highest_offset) {
941                 highest_ofs = ndr->offset;
942         } else {
943                 highest_ofs = ndr->relative_highest_offset;
944         }
945         if (highest_ofs < ndr->data_size) {
946                 enum ndr_err_code ret;
947                 ret = ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
948                                      "not all bytes consumed ofs[%u] size[%u]",
949                                      highest_ofs, ndr->data_size);
950                 talloc_free(ndr);
951                 return ret;
952         }
953         talloc_free(ndr);
954         return NDR_ERR_SUCCESS;
955 }
956
957 /*
958   push a struct to a blob using NDR
959 */
960 _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)
961 {
962         struct ndr_push *ndr;
963         ndr = ndr_push_init_ctx(mem_ctx);
964         NDR_ERR_HAVE_NO_MEMORY(ndr);
965
966         NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
967
968         *blob = ndr_push_blob(ndr);
969         talloc_steal(mem_ctx, blob->data);
970         talloc_free(ndr);
971
972         return NDR_ERR_SUCCESS;
973 }
974
975 /*
976   push a union to a blob using NDR
977 */
978 _PUBLIC_ enum ndr_err_code ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
979                              uint32_t level, ndr_push_flags_fn_t fn)
980 {
981         struct ndr_push *ndr;
982         ndr = ndr_push_init_ctx(mem_ctx);
983         NDR_ERR_HAVE_NO_MEMORY(ndr);
984
985         NDR_CHECK(ndr_push_set_switch_value(ndr, p, level));
986         NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
987
988         *blob = ndr_push_blob(ndr);
989         talloc_steal(mem_ctx, blob->data);
990         talloc_free(ndr);
991
992         return NDR_ERR_SUCCESS;
993 }
994
995 /*
996   generic ndr_size_*() handler for structures
997 */
998 _PUBLIC_ size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push)
999 {
1000         struct ndr_push *ndr;
1001         enum ndr_err_code status;
1002         size_t ret;
1003
1004         /* avoid recursion */
1005         if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
1006
1007         ndr = ndr_push_init_ctx(NULL);
1008         if (!ndr) return 0;
1009         ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
1010         status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
1011         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1012                 talloc_free(ndr);
1013                 return 0;
1014         }
1015         ret = ndr->offset;
1016         talloc_free(ndr);
1017         return ret;
1018 }
1019
1020 /*
1021   generic ndr_size_*() handler for unions
1022 */
1023 _PUBLIC_ size_t ndr_size_union(const void *p, int flags, uint32_t level, ndr_push_flags_fn_t push)
1024 {
1025         struct ndr_push *ndr;
1026         enum ndr_err_code status;
1027         size_t ret;
1028
1029         /* avoid recursion */
1030         if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
1031
1032         ndr = ndr_push_init_ctx(NULL);
1033         if (!ndr) return 0;
1034         ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
1035
1036         status = ndr_push_set_switch_value(ndr, p, level);
1037         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1038                 talloc_free(ndr);
1039                 return 0;
1040         }
1041         status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
1042         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1043                 talloc_free(ndr);
1044                 return 0;
1045         }
1046         ret = ndr->offset;
1047         talloc_free(ndr);
1048         return ret;
1049 }
1050
1051 /*
1052   get the current base for relative pointers for the push
1053 */
1054 _PUBLIC_ uint32_t ndr_push_get_relative_base_offset(struct ndr_push *ndr)
1055 {
1056         return ndr->relative_base_offset;
1057 }
1058
1059 /*
1060   restore the old base for relative pointers for the push
1061 */
1062 _PUBLIC_ void ndr_push_restore_relative_base_offset(struct ndr_push *ndr, uint32_t offset)
1063 {
1064         ndr->relative_base_offset = offset;
1065 }
1066
1067 /*
1068   setup the current base for relative pointers for the push
1069   called in the NDR_SCALAR stage
1070 */
1071 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset1(struct ndr_push *ndr, const void *p, uint32_t offset)
1072 {
1073         ndr->relative_base_offset = offset;
1074         return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1075 }
1076
1077 /*
1078   setup the current base for relative pointers for the push
1079   called in the NDR_BUFFERS stage
1080 */
1081 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset2(struct ndr_push *ndr, const void *p)
1082 {
1083         return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1084 }
1085
1086 /*
1087   push a relative object - stage1
1088   this is called during SCALARS processing
1089 */
1090 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
1091 {
1092         if (p == NULL) {
1093                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
1094                 return NDR_ERR_SUCCESS;
1095         }
1096         NDR_CHECK(ndr_push_align(ndr, 4));
1097         NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1098         return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
1099 }
1100
1101 /*
1102   push a short relative object - stage1
1103   this is called during SCALARS processing
1104 */
1105 _PUBLIC_ enum ndr_err_code ndr_push_short_relative_ptr1(struct ndr_push *ndr, const void *p)
1106 {
1107         if (p == NULL) {
1108                 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 0));
1109                 return NDR_ERR_SUCCESS;
1110         }
1111         NDR_CHECK(ndr_push_align(ndr, 2));
1112         NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1113         return ndr_push_uint16(ndr, NDR_SCALARS, 0xFFFF);
1114 }
1115 /*
1116   push a relative object - stage2
1117   this is called during buffers processing
1118 */
1119 static enum ndr_err_code ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
1120 {
1121         uint32_t save_offset;
1122         uint32_t ptr_offset = 0xFFFFFFFF;
1123         if (p == NULL) {
1124                 return NDR_ERR_SUCCESS;
1125         }
1126         save_offset = ndr->offset;
1127         NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1128         if (ptr_offset > ndr->offset) {
1129                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, 
1130                                       "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1131                                       ptr_offset, ndr->offset);
1132         }
1133         ndr->offset = ptr_offset;
1134         if (save_offset < ndr->relative_base_offset) {
1135                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, 
1136                                       "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1137                                       save_offset, ndr->relative_base_offset);
1138         }       
1139         NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
1140         ndr->offset = save_offset;
1141         return NDR_ERR_SUCCESS;
1142 }
1143 /*
1144   push a short relative object - stage2
1145   this is called during buffers processing
1146 */
1147 _PUBLIC_ enum ndr_err_code ndr_push_short_relative_ptr2(struct ndr_push *ndr, const void *p)
1148 {
1149         uint32_t save_offset;
1150         uint32_t ptr_offset = 0xFFFF;
1151         if (p == NULL) {
1152                 return NDR_ERR_SUCCESS;
1153         }
1154         save_offset = ndr->offset;
1155         NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1156         if (ptr_offset > ndr->offset) {
1157                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1158                                       "ndr_push_short_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1159                                       ptr_offset, ndr->offset);
1160         }
1161         ndr->offset = ptr_offset;
1162         if (save_offset < ndr->relative_base_offset) {
1163                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1164                                       "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1165                                       save_offset, ndr->relative_base_offset);
1166         }
1167         NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
1168         ndr->offset = save_offset;
1169         return NDR_ERR_SUCCESS;
1170 }
1171
1172 /*
1173   push a relative object - stage2 start
1174   this is called during buffers processing
1175 */
1176 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_start(struct ndr_push *ndr, const void *p)
1177 {
1178         if (p == NULL) {
1179                 return NDR_ERR_SUCCESS;
1180         }
1181         if (!(ndr->flags & LIBNDR_FLAG_RELATIVE_REVERSE)) {
1182                 uint32_t relative_offset;
1183                 size_t pad;
1184                 size_t align = 1;
1185
1186                 if (ndr->offset < ndr->relative_base_offset) {
1187                         return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1188                                       "ndr_push_relative_ptr2_start ndr->offset(%u) < ndr->relative_base_offset(%u)",
1189                                       ndr->offset, ndr->relative_base_offset);
1190                 }
1191
1192                 relative_offset = ndr->offset - ndr->relative_base_offset;
1193
1194                 if (ndr->flags & LIBNDR_FLAG_NOALIGN) {
1195                         align = 1;
1196                 } else if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1197                         align = 2;
1198                 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1199                         align = 4;
1200                 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1201                         align = 8;
1202                 }
1203
1204                 pad = ndr_align_size(relative_offset, align);
1205                 if (pad) {
1206                         NDR_CHECK(ndr_push_zero(ndr, pad));
1207                 }
1208
1209                 return ndr_push_relative_ptr2(ndr, p);
1210         }
1211         if (ndr->relative_end_offset == -1) {
1212                 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1213                               "ndr_push_relative_ptr2_start RELATIVE_REVERSE flag set and relative_end_offset %d",
1214                               ndr->relative_end_offset);
1215         }
1216         NDR_CHECK(ndr_token_store(ndr, &ndr->relative_begin_list, p, ndr->offset));
1217         return NDR_ERR_SUCCESS;
1218 }
1219
1220 /*
1221   push a relative object - stage2 end
1222   this is called during buffers processing
1223 */
1224 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_end(struct ndr_push *ndr, const void *p)
1225 {
1226         uint32_t begin_offset = 0xFFFFFFFF;
1227         ssize_t len;
1228         uint32_t correct_offset = 0;
1229         uint32_t align = 1;
1230         uint32_t pad = 0;
1231
1232         if (p == NULL) {
1233                 return NDR_ERR_SUCCESS;
1234         }
1235
1236         if (!(ndr->flags & LIBNDR_FLAG_RELATIVE_REVERSE)) {
1237                 return NDR_ERR_SUCCESS;
1238         }
1239
1240         if (ndr->flags & LIBNDR_FLAG_NO_NDR_SIZE) {
1241                 /* better say more than calculation a too small buffer */
1242                 NDR_PUSH_ALIGN(ndr, 8);
1243                 return NDR_ERR_SUCCESS;
1244         }
1245
1246         if (ndr->relative_end_offset < ndr->offset) {
1247                 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1248                                       "ndr_push_relative_ptr2_end:"
1249                                       "relative_end_offset %u < offset %u",
1250                                       ndr->relative_end_offset, ndr->offset);
1251         }
1252
1253         NDR_CHECK(ndr_token_retrieve(&ndr->relative_begin_list, p, &begin_offset));
1254
1255         /* we have marshalled a buffer, see how long it was */
1256         len = ndr->offset - begin_offset;
1257
1258         if (len < 0) {
1259                 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1260                                       "ndr_push_relative_ptr2_end:"
1261                                       "offset %u - begin_offset %u < 0",
1262                                       ndr->offset, begin_offset);
1263         }
1264
1265         if (ndr->relative_end_offset < len) {
1266                 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1267                                       "ndr_push_relative_ptr2_end:"
1268                                       "relative_end_offset %u < len %lld",
1269                                       ndr->offset, (long long)len);
1270         }
1271
1272         /* the reversed offset is at the end of the main buffer */
1273         correct_offset = ndr->relative_end_offset - len;
1274
1275         if (ndr->flags & LIBNDR_FLAG_NOALIGN) {
1276                 align = 1;
1277         } else if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1278                 align = 2;
1279         } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1280                 align = 4;
1281         } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1282                 align = 8;
1283         }
1284
1285         pad = ndr_align_size(correct_offset, align);
1286         if (pad) {
1287                 correct_offset += pad;
1288                 correct_offset -= align;
1289         }
1290
1291         if (correct_offset < begin_offset) {
1292                 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1293                                       "ndr_push_relative_ptr2_end: "
1294                                       "correct_offset %u < begin_offset %u",
1295                                       correct_offset, begin_offset);
1296         }
1297
1298         if (len > 0) {
1299                 uint32_t clear_size = correct_offset - begin_offset;
1300
1301                 clear_size = MIN(clear_size, len);
1302
1303                 /* now move the marshalled buffer to the end of the main buffer */
1304                 memmove(ndr->data + correct_offset, ndr->data + begin_offset, len);
1305
1306                 if (clear_size) {
1307                         /* and wipe out old buffer within the main buffer */
1308                         memset(ndr->data + begin_offset, '\0', clear_size);
1309                 }
1310         }
1311
1312         /* and set the end offset for the next buffer */
1313         ndr->relative_end_offset = correct_offset;
1314
1315         /* finally write the offset to the main buffer */
1316         ndr->offset = correct_offset;
1317         NDR_CHECK(ndr_push_relative_ptr2(ndr, p));
1318
1319         /* restore to where we were in the main buffer */
1320         ndr->offset = begin_offset;
1321
1322         return NDR_ERR_SUCCESS;
1323 }
1324
1325 /*
1326   get the current base for relative pointers for the pull
1327 */
1328 _PUBLIC_ uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull *ndr)
1329 {
1330         return ndr->relative_base_offset;
1331 }
1332
1333 /*
1334   restore the old base for relative pointers for the pull
1335 */
1336 _PUBLIC_ void ndr_pull_restore_relative_base_offset(struct ndr_pull *ndr, uint32_t offset)
1337 {
1338         ndr->relative_base_offset = offset;
1339 }
1340
1341 /*
1342   setup the current base for relative pointers for the pull
1343   called in the NDR_SCALAR stage
1344 */
1345 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset1(struct ndr_pull *ndr, const void *p, uint32_t offset)
1346 {
1347         ndr->relative_base_offset = offset;
1348         return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1349 }
1350
1351 /*
1352   setup the current base for relative pointers for the pull
1353   called in the NDR_BUFFERS stage
1354 */
1355 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset2(struct ndr_pull *ndr, const void *p)
1356 {
1357         return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1358 }
1359
1360 /*
1361   pull a relative object - stage1
1362   called during SCALARS processing
1363 */
1364 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
1365 {
1366         rel_offset += ndr->relative_base_offset;
1367         if (rel_offset > ndr->data_size) {
1368                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, 
1369                                       "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1370                                       rel_offset, ndr->data_size);
1371         }
1372         return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
1373 }
1374
1375 /*
1376   pull a relative object - stage2
1377   called during BUFFERS processing
1378 */
1379 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
1380 {
1381         uint32_t rel_offset;
1382         NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
1383         return ndr_pull_set_offset(ndr, rel_offset);
1384 }
1385
1386 const static struct {
1387         enum ndr_err_code err;
1388         const char *string;
1389 } ndr_err_code_strings[] = {
1390         { NDR_ERR_SUCCESS, "Success" },
1391         { NDR_ERR_ARRAY_SIZE, "Bad Array Size" },
1392         { NDR_ERR_BAD_SWITCH, "Bad Switch" },
1393         { NDR_ERR_OFFSET, "Offset Error" },
1394         { NDR_ERR_RELATIVE, "Relative Pointer Error" },
1395         { NDR_ERR_CHARCNV, "Character Conversion Error" },
1396         { NDR_ERR_LENGTH, "Length Error" },
1397         { NDR_ERR_SUBCONTEXT, "Subcontext Error" },
1398         { NDR_ERR_COMPRESSION, "Compression Error" },
1399         { NDR_ERR_STRING, "String Error" },
1400         { NDR_ERR_VALIDATE, "Validate Error" },
1401         { NDR_ERR_BUFSIZE, "Buffer Size Error" },
1402         { NDR_ERR_ALLOC, "Allocation Error" },
1403         { NDR_ERR_RANGE, "Range Error" },
1404         { NDR_ERR_TOKEN, "Token Error" },
1405         { NDR_ERR_IPV4ADDRESS, "IPv4 Address Error" },
1406         { NDR_ERR_INVALID_POINTER, "Invalid Pointer" },
1407         { NDR_ERR_UNREAD_BYTES, "Unread Bytes" },
1408         { NDR_ERR_NDR64, "NDR64 assertion error" },
1409         { 0, NULL }
1410 };
1411
1412 _PUBLIC_ const char *ndr_map_error2string(enum ndr_err_code ndr_err)
1413 {
1414         int i;
1415         for (i = 0; ndr_err_code_strings[i].string != NULL; i++) {
1416                 if (ndr_err_code_strings[i].err == ndr_err)
1417                         return ndr_err_code_strings[i].string;
1418         }
1419         return "Unknown error";
1420 }