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