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