s4-auth: Rework memory handling to use a tmp_ctx
[Samba.git] / librpc / ndr / ndr.c
blob76073ed0b72b5b8c2684b2eaeac4593c1c5e2f3a
1 /*
2 Unix SMB/CIFS implementation.
4 libndr interface
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Jelmer Vernooij 2005-2008
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 this provides the core routines for NDR parsing functions
26 see http://www.opengroup.org/onlinepubs/9629399/chap14.htm for details
27 of NDR encoding rules
30 #include "includes.h"
31 #include "librpc/ndr/libndr.h"
32 #include "../lib/util/dlinklist.h"
34 #define NDR_BASE_MARSHALL_SIZE 1024
36 /* this guid indicates NDR encoding in a protocol tower */
37 const struct ndr_syntax_id ndr_transfer_syntax_ndr = {
38 { 0x8a885d04, 0x1ceb, 0x11c9, {0x9f, 0xe8}, {0x08,0x00,0x2b,0x10,0x48,0x60} },
42 const struct ndr_syntax_id ndr_transfer_syntax_ndr64 = {
43 { 0x71710533, 0xbeba, 0x4937, {0x83, 0x19}, {0xb5,0xdb,0xef,0x9c,0xcc,0x36} },
47 const struct ndr_syntax_id ndr_syntax_id_null = {
48 { 0, 0, 0, { 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
53 work out the number of bytes needed to align on a n byte boundary
55 _PUBLIC_ size_t ndr_align_size(uint32_t offset, size_t n)
57 if ((offset & (n-1)) == 0) return 0;
58 return n - (offset & (n-1));
62 initialise a ndr parse structure from a data blob
64 _PUBLIC_ struct ndr_pull *ndr_pull_init_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx)
66 struct ndr_pull *ndr;
68 ndr = talloc_zero(mem_ctx, struct ndr_pull);
69 if (!ndr) return NULL;
70 ndr->current_mem_ctx = mem_ctx;
72 ndr->data = blob->data;
73 ndr->data_size = blob->length;
75 return ndr;
78 _PUBLIC_ enum ndr_err_code ndr_pull_append(struct ndr_pull *ndr, DATA_BLOB *blob)
80 enum ndr_err_code ndr_err;
81 DATA_BLOB b;
82 uint32_t append = 0;
83 bool ok;
85 if (blob->length == 0) {
86 return NDR_ERR_SUCCESS;
89 ndr_err = ndr_token_retrieve(&ndr->array_size_list, ndr, &append);
90 if (ndr_err == NDR_ERR_TOKEN) {
91 append = 0;
92 ndr_err = NDR_ERR_SUCCESS;
94 NDR_CHECK(ndr_err);
96 if (ndr->data_size == 0) {
97 ndr->data = NULL;
98 append = UINT32_MAX;
101 if (append == UINT32_MAX) {
103 * append == UINT32_MAX means that
104 * ndr->data is either NULL or a valid
105 * talloc child of ndr, which means
106 * we can use data_blob_append() without
107 * data_blob_talloc() of the existing callers data
109 b = data_blob_const(ndr->data, ndr->data_size);
110 } else {
111 b = data_blob_talloc(ndr, ndr->data, ndr->data_size);
112 if (b.data == NULL) {
113 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "%s", __location__);
117 ok = data_blob_append(ndr, &b, blob->data, blob->length);
118 if (!ok) {
119 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "%s", __location__);
122 ndr->data = b.data;
123 ndr->data_size = b.length;
125 return ndr_token_store(ndr, &ndr->array_size_list, ndr, UINT32_MAX);
128 _PUBLIC_ enum ndr_err_code ndr_pull_pop(struct ndr_pull *ndr)
130 uint32_t skip = 0;
131 uint32_t append = 0;
133 if (ndr->relative_base_offset != 0) {
134 return ndr_pull_error(ndr, NDR_ERR_RELATIVE,
135 "%s", __location__);
137 if (ndr->relative_highest_offset != 0) {
138 return ndr_pull_error(ndr, NDR_ERR_RELATIVE,
139 "%s", __location__);
141 if (ndr->relative_list != NULL) {
142 return ndr_pull_error(ndr, NDR_ERR_RELATIVE,
143 "%s", __location__);
145 if (ndr->relative_base_list != NULL) {
146 return ndr_pull_error(ndr, NDR_ERR_RELATIVE,
147 "%s", __location__);
151 * we need to keep up to 7 bytes
152 * in order to get the aligment right.
154 skip = ndr->offset & 0xFFFFFFF8;
156 if (skip == 0) {
157 return NDR_ERR_SUCCESS;
160 ndr->offset -= skip;
161 ndr->data_size -= skip;
163 append = ndr_token_peek(&ndr->array_size_list, ndr);
164 if (append != UINT32_MAX) {
166 * here we assume, that ndr->data is not a
167 * talloc child of ndr.
169 ndr->data += skip;
170 return NDR_ERR_SUCCESS;
173 memmove(ndr->data, ndr->data + skip, ndr->data_size);
175 ndr->data = talloc_realloc(ndr, ndr->data, uint8_t, ndr->data_size);
176 if (ndr->data_size != 0 && ndr->data == NULL) {
177 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "%s", __location__);
180 return NDR_ERR_SUCCESS;
184 advance by 'size' bytes
186 _PUBLIC_ enum ndr_err_code ndr_pull_advance(struct ndr_pull *ndr, uint32_t size)
188 ndr->offset += size;
189 if (ndr->offset > ndr->data_size) {
190 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
191 "ndr_pull_advance by %u failed",
192 size);
194 return NDR_ERR_SUCCESS;
198 set the parse offset to 'ofs'
200 static enum ndr_err_code ndr_pull_set_offset(struct ndr_pull *ndr, uint32_t ofs)
202 ndr->offset = ofs;
203 if (ndr->offset > ndr->data_size) {
204 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
205 "ndr_pull_set_offset %u failed",
206 ofs);
208 return NDR_ERR_SUCCESS;
211 /* create a ndr_push structure, ready for some marshalling */
212 _PUBLIC_ struct ndr_push *ndr_push_init_ctx(TALLOC_CTX *mem_ctx)
214 struct ndr_push *ndr;
216 ndr = talloc_zero(mem_ctx, struct ndr_push);
217 if (!ndr) {
218 return NULL;
221 ndr->flags = 0;
222 ndr->alloc_size = NDR_BASE_MARSHALL_SIZE;
223 ndr->data = talloc_array(ndr, uint8_t, ndr->alloc_size);
224 if (!ndr->data) {
225 talloc_free(ndr);
226 return NULL;
229 return ndr;
232 /* return a DATA_BLOB structure for the current ndr_push marshalled data */
233 _PUBLIC_ DATA_BLOB ndr_push_blob(struct ndr_push *ndr)
235 DATA_BLOB blob;
236 blob = data_blob_const(ndr->data, ndr->offset);
237 if (ndr->alloc_size > ndr->offset) {
238 ndr->data[ndr->offset] = 0;
240 return blob;
245 expand the available space in the buffer to ndr->offset + extra_size
247 _PUBLIC_ enum ndr_err_code ndr_push_expand(struct ndr_push *ndr, uint32_t extra_size)
249 uint32_t size = extra_size + ndr->offset;
251 if (size < ndr->offset) {
252 /* extra_size overflowed the offset */
253 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, "Overflow in push_expand to %u",
254 size);
257 if (ndr->alloc_size > size) {
258 return NDR_ERR_SUCCESS;
261 ndr->alloc_size += NDR_BASE_MARSHALL_SIZE;
262 if (size+1 > ndr->alloc_size) {
263 ndr->alloc_size = size+1;
265 ndr->data = talloc_realloc(ndr, ndr->data, uint8_t, ndr->alloc_size);
266 if (!ndr->data) {
267 return ndr_push_error(ndr, NDR_ERR_ALLOC, "Failed to push_expand to %u",
268 ndr->alloc_size);
271 return NDR_ERR_SUCCESS;
274 _PUBLIC_ void ndr_print_debugc_helper(struct ndr_print *ndr, const char *format, ...)
276 va_list ap;
277 char *s = NULL;
278 uint32_t i;
279 int ret;
280 int dbgc_class;
282 va_start(ap, format);
283 ret = vasprintf(&s, format, ap);
284 va_end(ap);
286 if (ret == -1) {
287 return;
290 dbgc_class = *(int *)ndr->private_data;
292 if (ndr->no_newline) {
293 DEBUGADDC(dbgc_class, 1,("%s", s));
294 free(s);
295 return;
298 for (i=0;i<ndr->depth;i++) {
299 DEBUGADDC(dbgc_class, 1,(" "));
302 DEBUGADDC(dbgc_class, 1,("%s\n", s));
303 free(s);
306 _PUBLIC_ void ndr_print_debug_helper(struct ndr_print *ndr, const char *format, ...)
308 va_list ap;
309 char *s = NULL;
310 uint32_t i;
311 int ret;
313 va_start(ap, format);
314 ret = vasprintf(&s, format, ap);
315 va_end(ap);
317 if (ret == -1) {
318 return;
321 if (ndr->no_newline) {
322 DEBUGADD(1,("%s", s));
323 free(s);
324 return;
327 for (i=0;i<ndr->depth;i++) {
328 DEBUGADD(1,(" "));
331 DEBUGADD(1,("%s\n", s));
332 free(s);
335 _PUBLIC_ void ndr_print_printf_helper(struct ndr_print *ndr, const char *format, ...)
337 va_list ap;
338 uint32_t i;
340 if (!ndr->no_newline) {
341 for (i=0;i<ndr->depth;i++) {
342 printf(" ");
346 va_start(ap, format);
347 vprintf(format, ap);
348 va_end(ap);
349 if (!ndr->no_newline) {
350 printf("\n");
354 _PUBLIC_ void ndr_print_string_helper(struct ndr_print *ndr, const char *format, ...)
356 va_list ap;
357 uint32_t i;
359 if (!ndr->no_newline) {
360 for (i=0;i<ndr->depth;i++) {
361 ndr->private_data = talloc_asprintf_append_buffer(
362 (char *)ndr->private_data, " ");
366 va_start(ap, format);
367 ndr->private_data = talloc_vasprintf_append_buffer((char *)ndr->private_data,
368 format, ap);
369 va_end(ap);
370 if (!ndr->no_newline) {
371 ndr->private_data = talloc_asprintf_append_buffer((char *)ndr->private_data,
372 "\n");
377 a useful helper function for printing idl structures via DEBUGC()
379 _PUBLIC_ void ndr_print_debugc(int dbgc_class, ndr_print_fn_t fn, const char *name, void *ptr)
381 struct ndr_print *ndr;
383 DEBUGC(dbgc_class, 1,(" "));
385 ndr = talloc_zero(NULL, struct ndr_print);
386 if (!ndr) return;
387 ndr->private_data = &dbgc_class;
388 ndr->print = ndr_print_debugc_helper;
389 ndr->depth = 1;
390 ndr->flags = 0;
391 fn(ndr, name, ptr);
392 talloc_free(ndr);
396 a useful helper function for printing idl structures via DEBUG()
398 _PUBLIC_ void ndr_print_debug(ndr_print_fn_t fn, const char *name, void *ptr)
400 struct ndr_print *ndr;
402 DEBUG(1,(" "));
404 ndr = talloc_zero(NULL, struct ndr_print);
405 if (!ndr) return;
406 ndr->print = ndr_print_debug_helper;
407 ndr->depth = 1;
408 ndr->flags = 0;
409 fn(ndr, name, ptr);
410 talloc_free(ndr);
414 a useful helper function for printing idl unions via DEBUG()
416 _PUBLIC_ void ndr_print_union_debug(ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
418 struct ndr_print *ndr;
420 DEBUG(1,(" "));
422 ndr = talloc_zero(NULL, struct ndr_print);
423 if (!ndr) return;
424 ndr->print = ndr_print_debug_helper;
425 ndr->depth = 1;
426 ndr->flags = 0;
427 ndr_print_set_switch_value(ndr, ptr, level);
428 fn(ndr, name, ptr);
429 talloc_free(ndr);
433 a useful helper function for printing idl function calls via DEBUG()
435 _PUBLIC_ void ndr_print_function_debug(ndr_print_function_t fn, const char *name, int flags, void *ptr)
437 struct ndr_print *ndr;
439 DEBUG(1,(" "));
441 ndr = talloc_zero(NULL, struct ndr_print);
442 if (!ndr) return;
443 ndr->print = ndr_print_debug_helper;
444 ndr->depth = 1;
445 ndr->flags = 0;
447 fn(ndr, name, flags, ptr);
448 talloc_free(ndr);
452 a useful helper function for printing idl structures to a string
454 _PUBLIC_ char *ndr_print_struct_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, void *ptr)
456 struct ndr_print *ndr;
457 char *ret = NULL;
459 ndr = talloc_zero(mem_ctx, struct ndr_print);
460 if (!ndr) return NULL;
461 ndr->private_data = talloc_strdup(ndr, "");
462 if (!ndr->private_data) {
463 goto failed;
465 ndr->print = ndr_print_string_helper;
466 ndr->depth = 1;
467 ndr->flags = 0;
469 fn(ndr, name, ptr);
470 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
471 failed:
472 talloc_free(ndr);
473 return ret;
477 a useful helper function for printing idl unions to a string
479 _PUBLIC_ char *ndr_print_union_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
481 struct ndr_print *ndr;
482 char *ret = NULL;
484 ndr = talloc_zero(mem_ctx, struct ndr_print);
485 if (!ndr) return NULL;
486 ndr->private_data = talloc_strdup(ndr, "");
487 if (!ndr->private_data) {
488 goto failed;
490 ndr->print = ndr_print_string_helper;
491 ndr->depth = 1;
492 ndr->flags = 0;
493 ndr_print_set_switch_value(ndr, ptr, level);
494 fn(ndr, name, ptr);
495 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
496 failed:
497 talloc_free(ndr);
498 return ret;
502 a useful helper function for printing idl function calls to a string
504 _PUBLIC_ char *ndr_print_function_string(TALLOC_CTX *mem_ctx,
505 ndr_print_function_t fn, const char *name,
506 int flags, void *ptr)
508 struct ndr_print *ndr;
509 char *ret = NULL;
511 ndr = talloc_zero(mem_ctx, struct ndr_print);
512 if (!ndr) return NULL;
513 ndr->private_data = talloc_strdup(ndr, "");
514 if (!ndr->private_data) {
515 goto failed;
517 ndr->print = ndr_print_string_helper;
518 ndr->depth = 1;
519 ndr->flags = 0;
520 fn(ndr, name, flags, ptr);
521 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
522 failed:
523 talloc_free(ndr);
524 return ret;
527 _PUBLIC_ void ndr_set_flags(uint32_t *pflags, uint32_t new_flags)
529 /* the big/little endian flags are inter-dependent */
530 if (new_flags & LIBNDR_FLAG_LITTLE_ENDIAN) {
531 (*pflags) &= ~LIBNDR_FLAG_BIGENDIAN;
532 (*pflags) &= ~LIBNDR_FLAG_NDR64;
534 if (new_flags & LIBNDR_FLAG_BIGENDIAN) {
535 (*pflags) &= ~LIBNDR_FLAG_LITTLE_ENDIAN;
536 (*pflags) &= ~LIBNDR_FLAG_NDR64;
538 if (new_flags & LIBNDR_ALIGN_FLAGS) {
539 /* Ensure we only have the passed-in
540 align flag set in the new_flags,
541 remove any old align flag. */
542 (*pflags) &= ~LIBNDR_ALIGN_FLAGS;
544 if (new_flags & LIBNDR_FLAG_NO_RELATIVE_REVERSE) {
545 (*pflags) &= ~LIBNDR_FLAG_RELATIVE_REVERSE;
547 (*pflags) |= new_flags;
551 return and possibly log an NDR error
553 _PUBLIC_ enum ndr_err_code ndr_pull_error(struct ndr_pull *ndr,
554 enum ndr_err_code ndr_err,
555 const char *format, ...)
557 char *s=NULL;
558 va_list ap;
559 int ret;
561 if (ndr->flags & LIBNDR_FLAG_INCOMPLETE_BUFFER) {
562 switch (ndr_err) {
563 case NDR_ERR_BUFSIZE:
564 return NDR_ERR_INCOMPLETE_BUFFER;
565 default:
566 break;
570 va_start(ap, format);
571 ret = vasprintf(&s, format, ap);
572 va_end(ap);
574 if (ret == -1) {
575 return NDR_ERR_ALLOC;
578 DEBUG(1,("ndr_pull_error(%u): %s\n", ndr_err, s));
580 free(s);
582 return ndr_err;
586 return and possibly log an NDR error
588 _PUBLIC_ enum ndr_err_code ndr_push_error(struct ndr_push *ndr,
589 enum ndr_err_code ndr_err,
590 const char *format, ...)
592 char *s=NULL;
593 va_list ap;
594 int ret;
596 va_start(ap, format);
597 ret = vasprintf(&s, format, ap);
598 va_end(ap);
600 if (ret == -1) {
601 return NDR_ERR_ALLOC;
604 DEBUG(1,("ndr_push_error(%u): %s\n", ndr_err, s));
606 free(s);
608 return ndr_err;
612 handle subcontext buffers, which in midl land are user-marshalled, but
613 we use magic in pidl to make them easier to cope with
615 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_start(struct ndr_pull *ndr,
616 struct ndr_pull **_subndr,
617 size_t header_size,
618 ssize_t size_is)
620 struct ndr_pull *subndr;
621 uint32_t r_content_size;
622 bool force_le = false;
623 bool force_be = false;
625 switch (header_size) {
626 case 0: {
627 uint32_t content_size = ndr->data_size - ndr->offset;
628 if (size_is >= 0) {
629 content_size = size_is;
631 r_content_size = content_size;
632 break;
635 case 2: {
636 uint16_t content_size;
637 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &content_size));
638 if (size_is >= 0 && size_is != content_size) {
639 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
640 (int)size_is, (int)content_size);
642 r_content_size = content_size;
643 break;
646 case 4: {
647 uint32_t content_size;
648 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &content_size));
649 if (size_is >= 0 && size_is != content_size) {
650 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
651 (int)size_is, (int)content_size);
653 r_content_size = content_size;
654 break;
656 case 0xFFFFFC01: {
658 * Common Type Header for the Serialization Stream
659 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
661 uint8_t version;
662 uint8_t drep;
663 uint16_t hdrlen;
664 uint32_t filler;
665 uint32_t content_size;
666 uint32_t reserved;
668 /* version */
669 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &version));
671 if (version != 1) {
672 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
673 "Bad subcontext (PULL) Common Type Header version %d != 1",
674 (int)version);
678 * 0x10 little endian
679 * 0x00 big endian
681 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &drep));
682 if (drep == 0x10) {
683 force_le = true;
684 } else if (drep == 0x00) {
685 force_be = true;
686 } else {
687 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
688 "Bad subcontext (PULL) Common Type Header invalid drep 0x%02X",
689 (unsigned int)drep);
692 /* length of the "Private Header for Constructed Type" */
693 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &hdrlen));
694 if (hdrlen != 8) {
695 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
696 "Bad subcontext (PULL) Common Type Header length %d != 8",
697 (int)hdrlen);
700 /* filler should be ignored */
701 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &filler));
704 * Private Header for Constructed Type
706 /* length - will be updated latter */
707 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
708 if (size_is >= 0 && size_is != content_size) {
709 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
710 (int)size_is, (int)content_size);
712 /* the content size must be a multiple of 8 */
713 if ((content_size % 8) != 0) {
714 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
715 "Bad subcontext (PULL) size_is(%d) not padded to 8 content_size %d",
716 (int)size_is, (int)content_size);
718 r_content_size = content_size;
720 /* reserved */
721 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &reserved));
722 break;
724 case 0xFFFFFFFF:
726 * a shallow copy like subcontext
727 * useful for DCERPC pipe chunks.
729 subndr = talloc_zero(ndr, struct ndr_pull);
730 NDR_ERR_HAVE_NO_MEMORY(subndr);
732 subndr->flags = ndr->flags;
733 subndr->current_mem_ctx = ndr->current_mem_ctx;
734 subndr->data = ndr->data;
735 subndr->offset = ndr->offset;
736 subndr->data_size = ndr->data_size;
738 *_subndr = subndr;
739 return NDR_ERR_SUCCESS;
741 default:
742 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) header_size %d",
743 (int)header_size);
746 NDR_PULL_NEED_BYTES(ndr, r_content_size);
748 subndr = talloc_zero(ndr, struct ndr_pull);
749 NDR_ERR_HAVE_NO_MEMORY(subndr);
750 subndr->flags = ndr->flags & ~LIBNDR_FLAG_NDR64;
751 subndr->current_mem_ctx = ndr->current_mem_ctx;
753 subndr->data = ndr->data + ndr->offset;
754 subndr->offset = 0;
755 subndr->data_size = r_content_size;
757 if (force_le) {
758 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_LITTLE_ENDIAN);
759 } else if (force_be) {
760 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_BIGENDIAN);
763 *_subndr = subndr;
764 return NDR_ERR_SUCCESS;
767 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_end(struct ndr_pull *ndr,
768 struct ndr_pull *subndr,
769 size_t header_size,
770 ssize_t size_is)
772 uint32_t advance;
773 uint32_t highest_ofs;
775 if (header_size == 0xFFFFFFFF) {
776 advance = subndr->offset - ndr->offset;
777 } else if (size_is >= 0) {
778 advance = size_is;
779 } else if (header_size > 0) {
780 advance = subndr->data_size;
781 } else {
782 advance = subndr->offset;
785 if (subndr->offset > ndr->relative_highest_offset) {
786 highest_ofs = subndr->offset;
787 } else {
788 highest_ofs = subndr->relative_highest_offset;
790 if (!(subndr->flags & LIBNDR_FLAG_SUBCONTEXT_NO_UNREAD_BYTES)) {
792 * avoid an error unless SUBCONTEXT_NO_UNREAD_BYTES is specified
794 highest_ofs = advance;
796 if (highest_ofs < advance) {
797 return ndr_pull_error(subndr, NDR_ERR_UNREAD_BYTES,
798 "not all bytes consumed ofs[%u] advance[%u]",
799 highest_ofs, advance);
802 NDR_CHECK(ndr_pull_advance(ndr, advance));
803 return NDR_ERR_SUCCESS;
806 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_start(struct ndr_push *ndr,
807 struct ndr_push **_subndr,
808 size_t header_size,
809 ssize_t size_is)
811 struct ndr_push *subndr;
813 subndr = ndr_push_init_ctx(ndr);
814 NDR_ERR_HAVE_NO_MEMORY(subndr);
815 subndr->flags = ndr->flags & ~LIBNDR_FLAG_NDR64;
817 if (size_is > 0) {
818 NDR_CHECK(ndr_push_zero(subndr, size_is));
819 subndr->offset = 0;
820 subndr->relative_end_offset = size_is;
823 *_subndr = subndr;
824 return NDR_ERR_SUCCESS;
828 push a subcontext header
830 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_end(struct ndr_push *ndr,
831 struct ndr_push *subndr,
832 size_t header_size,
833 ssize_t size_is)
835 ssize_t padding_len;
837 if (size_is >= 0) {
838 padding_len = size_is - subndr->offset;
839 if (padding_len < 0) {
840 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
841 (int)subndr->offset, (int)size_is);
843 subndr->offset = size_is;
846 switch (header_size) {
847 case 0:
848 break;
850 case 2:
851 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, subndr->offset));
852 break;
854 case 4:
855 NDR_CHECK(ndr_push_uint3264(ndr, NDR_SCALARS, subndr->offset));
856 break;
858 case 0xFFFFFC01:
860 * Common Type Header for the Serialization Stream
861 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
863 padding_len = NDR_ROUND(subndr->offset, 8) - subndr->offset;
864 if (padding_len > 0) {
865 NDR_CHECK(ndr_push_zero(subndr, padding_len));
868 /* version */
869 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, 1));
872 * 0x10 little endian
873 * 0x00 big endian
875 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, NDR_BE(ndr)?0x00:0x10));
877 /* length of the "Private Header for Constructed Type" */
878 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 8));
880 /* filler */
881 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0xCCCCCCCC));
884 * Private Header for Constructed Type
886 /* length - will be updated latter */
887 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
889 /* reserved */
890 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
891 break;
893 default:
894 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext header size %d",
895 (int)header_size);
898 NDR_CHECK(ndr_push_bytes(ndr, subndr->data, subndr->offset));
899 return NDR_ERR_SUCCESS;
903 store a token in the ndr context, for later retrieval
905 _PUBLIC_ enum ndr_err_code ndr_token_store(TALLOC_CTX *mem_ctx,
906 struct ndr_token_list **list,
907 const void *key,
908 uint32_t value)
910 struct ndr_token_list *tok;
911 tok = talloc(mem_ctx, struct ndr_token_list);
912 NDR_ERR_HAVE_NO_MEMORY(tok);
913 tok->key = key;
914 tok->value = value;
915 DLIST_ADD((*list), tok);
916 return NDR_ERR_SUCCESS;
920 retrieve a token from a ndr context, using cmp_fn to match the tokens
922 _PUBLIC_ enum ndr_err_code ndr_token_retrieve_cmp_fn(struct ndr_token_list **list, const void *key, uint32_t *v,
923 comparison_fn_t _cmp_fn, bool _remove_tok)
925 struct ndr_token_list *tok;
926 for (tok=*list;tok;tok=tok->next) {
927 if (_cmp_fn && _cmp_fn(tok->key,key)==0) goto found;
928 else if (!_cmp_fn && tok->key == key) goto found;
930 return NDR_ERR_TOKEN;
931 found:
932 *v = tok->value;
933 if (_remove_tok) {
934 DLIST_REMOVE((*list), tok);
935 talloc_free(tok);
937 return NDR_ERR_SUCCESS;
941 retrieve a token from a ndr context
943 _PUBLIC_ enum ndr_err_code ndr_token_retrieve(struct ndr_token_list **list, const void *key, uint32_t *v)
945 return ndr_token_retrieve_cmp_fn(list, key, v, NULL, true);
949 peek at but don't removed a token from a ndr context
951 _PUBLIC_ uint32_t ndr_token_peek(struct ndr_token_list **list, const void *key)
953 enum ndr_err_code status;
954 uint32_t v;
956 status = ndr_token_retrieve_cmp_fn(list, key, &v, NULL, false);
957 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
958 return 0;
961 return v;
965 pull an array size field and add it to the array_size_list token list
967 _PUBLIC_ enum ndr_err_code ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
969 uint32_t size;
970 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &size));
971 return ndr_token_store(ndr, &ndr->array_size_list, p, size);
975 get the stored array size field
977 _PUBLIC_ uint32_t ndr_get_array_size(struct ndr_pull *ndr, const void *p)
979 return ndr_token_peek(&ndr->array_size_list, p);
983 check the stored array size field
985 _PUBLIC_ enum ndr_err_code ndr_check_array_size(struct ndr_pull *ndr, void *p, uint32_t size)
987 uint32_t stored;
988 stored = ndr_token_peek(&ndr->array_size_list, p);
989 if (stored != size) {
990 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
991 "Bad array size - got %u expected %u\n",
992 stored, size);
994 return NDR_ERR_SUCCESS;
998 pull an array length field and add it to the array_length_list token list
1000 _PUBLIC_ enum ndr_err_code ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
1002 uint32_t length, offset;
1003 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &offset));
1004 if (offset != 0) {
1005 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
1006 "non-zero array offset %u\n", offset);
1008 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &length));
1009 return ndr_token_store(ndr, &ndr->array_length_list, p, length);
1013 get the stored array length field
1015 _PUBLIC_ uint32_t ndr_get_array_length(struct ndr_pull *ndr, const void *p)
1017 return ndr_token_peek(&ndr->array_length_list, p);
1021 check the stored array length field
1023 _PUBLIC_ enum ndr_err_code ndr_check_array_length(struct ndr_pull *ndr, void *p, uint32_t length)
1025 uint32_t stored;
1026 stored = ndr_token_peek(&ndr->array_length_list, p);
1027 if (stored != length) {
1028 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
1029 "Bad array length - got %u expected %u\n",
1030 stored, length);
1032 return NDR_ERR_SUCCESS;
1035 _PUBLIC_ enum ndr_err_code ndr_push_pipe_chunk_trailer(struct ndr_push *ndr, int ndr_flags, uint32_t count)
1037 if (ndr->flags & LIBNDR_FLAG_NDR64) {
1038 int64_t tmp = 0 - (int64_t)count;
1039 uint64_t ncount = tmp;
1041 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, ncount));
1044 return NDR_ERR_SUCCESS;
1047 _PUBLIC_ enum ndr_err_code ndr_check_pipe_chunk_trailer(struct ndr_pull *ndr, int ndr_flags, uint32_t count)
1049 if (ndr->flags & LIBNDR_FLAG_NDR64) {
1050 int64_t tmp = 0 - (int64_t)count;
1051 uint64_t ncount1 = tmp;
1052 uint64_t ncount2;
1054 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, &ncount2));
1055 if (ncount1 == ncount2) {
1056 return NDR_ERR_SUCCESS;
1059 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
1060 "Bad pipe trailer[%lld should be %lld] size was %lu\"",
1061 (unsigned long long)ncount2,
1062 (unsigned long long)ncount1,
1063 (unsigned long)count);
1066 return NDR_ERR_SUCCESS;
1070 store a switch value
1072 _PUBLIC_ enum ndr_err_code ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
1074 return ndr_token_store(ndr, &ndr->switch_list, p, val);
1077 _PUBLIC_ enum ndr_err_code ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
1079 return ndr_token_store(ndr, &ndr->switch_list, p, val);
1082 _PUBLIC_ enum ndr_err_code ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
1084 return ndr_token_store(ndr, &ndr->switch_list, p, val);
1088 retrieve a switch value
1090 _PUBLIC_ uint32_t ndr_push_get_switch_value(struct ndr_push *ndr, const void *p)
1092 return ndr_token_peek(&ndr->switch_list, p);
1095 _PUBLIC_ uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
1097 return ndr_token_peek(&ndr->switch_list, p);
1100 _PUBLIC_ uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
1102 return ndr_token_peek(&ndr->switch_list, p);
1106 pull a struct from a blob using NDR
1108 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
1109 ndr_pull_flags_fn_t fn)
1111 struct ndr_pull *ndr;
1112 ndr = ndr_pull_init_blob(blob, mem_ctx);
1113 NDR_ERR_HAVE_NO_MEMORY(ndr);
1114 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
1115 talloc_free(ndr);
1116 return NDR_ERR_SUCCESS;
1120 pull a struct from a blob using NDR - failing if all bytes are not consumed
1122 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
1123 void *p, ndr_pull_flags_fn_t fn)
1125 struct ndr_pull *ndr;
1126 uint32_t highest_ofs;
1127 ndr = ndr_pull_init_blob(blob, mem_ctx);
1128 NDR_ERR_HAVE_NO_MEMORY(ndr);
1129 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
1130 if (ndr->offset > ndr->relative_highest_offset) {
1131 highest_ofs = ndr->offset;
1132 } else {
1133 highest_ofs = ndr->relative_highest_offset;
1135 if (highest_ofs < ndr->data_size) {
1136 enum ndr_err_code ret;
1137 ret = ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
1138 "not all bytes consumed ofs[%u] size[%u]",
1139 highest_ofs, ndr->data_size);
1140 talloc_free(ndr);
1141 return ret;
1143 talloc_free(ndr);
1144 return NDR_ERR_SUCCESS;
1148 pull a union from a blob using NDR, given the union discriminator
1150 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
1151 void *p,
1152 uint32_t level, ndr_pull_flags_fn_t fn)
1154 struct ndr_pull *ndr;
1155 ndr = ndr_pull_init_blob(blob, mem_ctx);
1156 NDR_ERR_HAVE_NO_MEMORY(ndr);
1157 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
1158 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
1159 talloc_free(ndr);
1160 return NDR_ERR_SUCCESS;
1164 pull a union from a blob using NDR, given the union discriminator,
1165 failing if all bytes are not consumed
1167 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
1168 void *p,
1169 uint32_t level, ndr_pull_flags_fn_t fn)
1171 struct ndr_pull *ndr;
1172 uint32_t highest_ofs;
1173 ndr = ndr_pull_init_blob(blob, mem_ctx);
1174 NDR_ERR_HAVE_NO_MEMORY(ndr);
1175 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
1176 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
1177 if (ndr->offset > ndr->relative_highest_offset) {
1178 highest_ofs = ndr->offset;
1179 } else {
1180 highest_ofs = ndr->relative_highest_offset;
1182 if (highest_ofs < ndr->data_size) {
1183 enum ndr_err_code ret;
1184 ret = ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
1185 "not all bytes consumed ofs[%u] size[%u]",
1186 highest_ofs, ndr->data_size);
1187 talloc_free(ndr);
1188 return ret;
1190 talloc_free(ndr);
1191 return NDR_ERR_SUCCESS;
1195 push a struct to a blob using NDR
1197 _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)
1199 struct ndr_push *ndr;
1200 ndr = ndr_push_init_ctx(mem_ctx);
1201 NDR_ERR_HAVE_NO_MEMORY(ndr);
1203 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
1205 *blob = ndr_push_blob(ndr);
1206 talloc_steal(mem_ctx, blob->data);
1207 talloc_free(ndr);
1209 return NDR_ERR_SUCCESS;
1213 push a union to a blob using NDR
1215 _PUBLIC_ enum ndr_err_code ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
1216 uint32_t level, ndr_push_flags_fn_t fn)
1218 struct ndr_push *ndr;
1219 ndr = ndr_push_init_ctx(mem_ctx);
1220 NDR_ERR_HAVE_NO_MEMORY(ndr);
1222 NDR_CHECK(ndr_push_set_switch_value(ndr, p, level));
1223 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
1225 *blob = ndr_push_blob(ndr);
1226 talloc_steal(mem_ctx, blob->data);
1227 talloc_free(ndr);
1229 return NDR_ERR_SUCCESS;
1233 generic ndr_size_*() handler for structures
1235 _PUBLIC_ size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push)
1237 struct ndr_push *ndr;
1238 enum ndr_err_code status;
1239 size_t ret;
1241 /* avoid recursion */
1242 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
1244 ndr = ndr_push_init_ctx(NULL);
1245 if (!ndr) return 0;
1246 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
1247 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
1248 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1249 talloc_free(ndr);
1250 return 0;
1252 ret = ndr->offset;
1253 talloc_free(ndr);
1254 return ret;
1258 generic ndr_size_*() handler for unions
1260 _PUBLIC_ size_t ndr_size_union(const void *p, int flags, uint32_t level, ndr_push_flags_fn_t push)
1262 struct ndr_push *ndr;
1263 enum ndr_err_code status;
1264 size_t ret;
1266 /* avoid recursion */
1267 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
1269 ndr = ndr_push_init_ctx(NULL);
1270 if (!ndr) return 0;
1271 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
1273 status = ndr_push_set_switch_value(ndr, p, level);
1274 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1275 talloc_free(ndr);
1276 return 0;
1278 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
1279 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1280 talloc_free(ndr);
1281 return 0;
1283 ret = ndr->offset;
1284 talloc_free(ndr);
1285 return ret;
1289 get the current base for relative pointers for the push
1291 _PUBLIC_ uint32_t ndr_push_get_relative_base_offset(struct ndr_push *ndr)
1293 return ndr->relative_base_offset;
1297 restore the old base for relative pointers for the push
1299 _PUBLIC_ void ndr_push_restore_relative_base_offset(struct ndr_push *ndr, uint32_t offset)
1301 ndr->relative_base_offset = offset;
1305 setup the current base for relative pointers for the push
1306 called in the NDR_SCALAR stage
1308 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset1(struct ndr_push *ndr, const void *p, uint32_t offset)
1310 ndr->relative_base_offset = offset;
1311 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1315 setup the current base for relative pointers for the push
1316 called in the NDR_BUFFERS stage
1318 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset2(struct ndr_push *ndr, const void *p)
1320 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1324 push a relative object - stage1
1325 this is called during SCALARS processing
1327 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
1329 if (p == NULL) {
1330 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
1331 return NDR_ERR_SUCCESS;
1333 NDR_CHECK(ndr_push_align(ndr, 4));
1334 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1335 return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
1339 push a short relative object - stage1
1340 this is called during SCALARS processing
1342 _PUBLIC_ enum ndr_err_code ndr_push_short_relative_ptr1(struct ndr_push *ndr, const void *p)
1344 if (p == NULL) {
1345 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 0));
1346 return NDR_ERR_SUCCESS;
1348 NDR_CHECK(ndr_push_align(ndr, 2));
1349 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1350 return ndr_push_uint16(ndr, NDR_SCALARS, 0xFFFF);
1353 push a relative object - stage2
1354 this is called during buffers processing
1356 static enum ndr_err_code ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
1358 uint32_t save_offset;
1359 uint32_t ptr_offset = 0xFFFFFFFF;
1360 if (p == NULL) {
1361 return NDR_ERR_SUCCESS;
1363 save_offset = ndr->offset;
1364 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1365 if (ptr_offset > ndr->offset) {
1366 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1367 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1368 ptr_offset, ndr->offset);
1370 ndr->offset = ptr_offset;
1371 if (save_offset < ndr->relative_base_offset) {
1372 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1373 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1374 save_offset, ndr->relative_base_offset);
1376 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
1377 ndr->offset = save_offset;
1378 return NDR_ERR_SUCCESS;
1381 push a short relative object - stage2
1382 this is called during buffers processing
1384 _PUBLIC_ enum ndr_err_code ndr_push_short_relative_ptr2(struct ndr_push *ndr, const void *p)
1386 uint32_t save_offset;
1387 uint32_t ptr_offset = 0xFFFF;
1388 if (p == NULL) {
1389 return NDR_ERR_SUCCESS;
1391 save_offset = ndr->offset;
1392 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1393 if (ptr_offset > ndr->offset) {
1394 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1395 "ndr_push_short_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1396 ptr_offset, ndr->offset);
1398 ndr->offset = ptr_offset;
1399 if (save_offset < ndr->relative_base_offset) {
1400 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1401 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1402 save_offset, ndr->relative_base_offset);
1404 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
1405 ndr->offset = save_offset;
1406 return NDR_ERR_SUCCESS;
1410 push a relative object - stage2 start
1411 this is called during buffers processing
1413 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_start(struct ndr_push *ndr, const void *p)
1415 if (p == NULL) {
1416 return NDR_ERR_SUCCESS;
1418 if (!(ndr->flags & LIBNDR_FLAG_RELATIVE_REVERSE)) {
1419 uint32_t relative_offset;
1420 size_t pad;
1421 size_t align = 1;
1423 if (ndr->offset < ndr->relative_base_offset) {
1424 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1425 "ndr_push_relative_ptr2_start ndr->offset(%u) < ndr->relative_base_offset(%u)",
1426 ndr->offset, ndr->relative_base_offset);
1429 relative_offset = ndr->offset - ndr->relative_base_offset;
1431 if (ndr->flags & LIBNDR_FLAG_NOALIGN) {
1432 align = 1;
1433 } else if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1434 align = 2;
1435 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1436 align = 4;
1437 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1438 align = 8;
1441 pad = ndr_align_size(relative_offset, align);
1442 if (pad) {
1443 NDR_CHECK(ndr_push_zero(ndr, pad));
1446 return ndr_push_relative_ptr2(ndr, p);
1448 if (ndr->relative_end_offset == -1) {
1449 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1450 "ndr_push_relative_ptr2_start RELATIVE_REVERSE flag set and relative_end_offset %d",
1451 ndr->relative_end_offset);
1453 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_begin_list, p, ndr->offset));
1454 return NDR_ERR_SUCCESS;
1458 push a relative object - stage2 end
1459 this is called during buffers processing
1461 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_end(struct ndr_push *ndr, const void *p)
1463 uint32_t begin_offset = 0xFFFFFFFF;
1464 ssize_t len;
1465 uint32_t correct_offset = 0;
1466 uint32_t align = 1;
1467 uint32_t pad = 0;
1469 if (p == NULL) {
1470 return NDR_ERR_SUCCESS;
1473 if (!(ndr->flags & LIBNDR_FLAG_RELATIVE_REVERSE)) {
1474 return NDR_ERR_SUCCESS;
1477 if (ndr->flags & LIBNDR_FLAG_NO_NDR_SIZE) {
1478 /* better say more than calculation a too small buffer */
1479 NDR_PUSH_ALIGN(ndr, 8);
1480 return NDR_ERR_SUCCESS;
1483 if (ndr->relative_end_offset < ndr->offset) {
1484 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1485 "ndr_push_relative_ptr2_end:"
1486 "relative_end_offset %u < offset %u",
1487 ndr->relative_end_offset, ndr->offset);
1490 NDR_CHECK(ndr_token_retrieve(&ndr->relative_begin_list, p, &begin_offset));
1492 /* we have marshalled a buffer, see how long it was */
1493 len = ndr->offset - begin_offset;
1495 if (len < 0) {
1496 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1497 "ndr_push_relative_ptr2_end:"
1498 "offset %u - begin_offset %u < 0",
1499 ndr->offset, begin_offset);
1502 if (ndr->relative_end_offset < len) {
1503 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1504 "ndr_push_relative_ptr2_end:"
1505 "relative_end_offset %u < len %lld",
1506 ndr->offset, (long long)len);
1509 /* the reversed offset is at the end of the main buffer */
1510 correct_offset = ndr->relative_end_offset - len;
1512 if (ndr->flags & LIBNDR_FLAG_NOALIGN) {
1513 align = 1;
1514 } else if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1515 align = 2;
1516 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1517 align = 4;
1518 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1519 align = 8;
1522 pad = ndr_align_size(correct_offset, align);
1523 if (pad) {
1524 correct_offset += pad;
1525 correct_offset -= align;
1528 if (correct_offset < begin_offset) {
1529 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1530 "ndr_push_relative_ptr2_end: "
1531 "correct_offset %u < begin_offset %u",
1532 correct_offset, begin_offset);
1535 if (len > 0) {
1536 uint32_t clear_size = correct_offset - begin_offset;
1538 clear_size = MIN(clear_size, len);
1540 /* now move the marshalled buffer to the end of the main buffer */
1541 memmove(ndr->data + correct_offset, ndr->data + begin_offset, len);
1543 if (clear_size) {
1544 /* and wipe out old buffer within the main buffer */
1545 memset(ndr->data + begin_offset, '\0', clear_size);
1549 /* and set the end offset for the next buffer */
1550 ndr->relative_end_offset = correct_offset;
1552 /* finally write the offset to the main buffer */
1553 ndr->offset = correct_offset;
1554 NDR_CHECK(ndr_push_relative_ptr2(ndr, p));
1556 /* restore to where we were in the main buffer */
1557 ndr->offset = begin_offset;
1559 return NDR_ERR_SUCCESS;
1563 get the current base for relative pointers for the pull
1565 _PUBLIC_ uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull *ndr)
1567 return ndr->relative_base_offset;
1571 restore the old base for relative pointers for the pull
1573 _PUBLIC_ void ndr_pull_restore_relative_base_offset(struct ndr_pull *ndr, uint32_t offset)
1575 ndr->relative_base_offset = offset;
1579 setup the current base for relative pointers for the pull
1580 called in the NDR_SCALAR stage
1582 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset1(struct ndr_pull *ndr, const void *p, uint32_t offset)
1584 ndr->relative_base_offset = offset;
1585 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1589 setup the current base for relative pointers for the pull
1590 called in the NDR_BUFFERS stage
1592 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset2(struct ndr_pull *ndr, const void *p)
1594 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1598 pull a relative object - stage1
1599 called during SCALARS processing
1601 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
1603 rel_offset += ndr->relative_base_offset;
1604 if (rel_offset > ndr->data_size) {
1605 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
1606 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1607 rel_offset, ndr->data_size);
1609 return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
1613 pull a relative object - stage2
1614 called during BUFFERS processing
1616 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
1618 uint32_t rel_offset;
1619 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
1620 return ndr_pull_set_offset(ndr, rel_offset);
1623 const static struct {
1624 enum ndr_err_code err;
1625 const char *string;
1626 } ndr_err_code_strings[] = {
1627 { NDR_ERR_SUCCESS, "Success" },
1628 { NDR_ERR_ARRAY_SIZE, "Bad Array Size" },
1629 { NDR_ERR_BAD_SWITCH, "Bad Switch" },
1630 { NDR_ERR_OFFSET, "Offset Error" },
1631 { NDR_ERR_RELATIVE, "Relative Pointer Error" },
1632 { NDR_ERR_CHARCNV, "Character Conversion Error" },
1633 { NDR_ERR_LENGTH, "Length Error" },
1634 { NDR_ERR_SUBCONTEXT, "Subcontext Error" },
1635 { NDR_ERR_COMPRESSION, "Compression Error" },
1636 { NDR_ERR_STRING, "String Error" },
1637 { NDR_ERR_VALIDATE, "Validate Error" },
1638 { NDR_ERR_BUFSIZE, "Buffer Size Error" },
1639 { NDR_ERR_ALLOC, "Allocation Error" },
1640 { NDR_ERR_RANGE, "Range Error" },
1641 { NDR_ERR_TOKEN, "Token Error" },
1642 { NDR_ERR_IPV4ADDRESS, "IPv4 Address Error" },
1643 { NDR_ERR_INVALID_POINTER, "Invalid Pointer" },
1644 { NDR_ERR_UNREAD_BYTES, "Unread Bytes" },
1645 { NDR_ERR_NDR64, "NDR64 assertion error" },
1646 { NDR_ERR_INCOMPLETE_BUFFER, "Incomplete Buffer" },
1647 { 0, NULL }
1650 _PUBLIC_ const char *ndr_map_error2string(enum ndr_err_code ndr_err)
1652 int i;
1653 for (i = 0; ndr_err_code_strings[i].string != NULL; i++) {
1654 if (ndr_err_code_strings[i].err == ndr_err)
1655 return ndr_err_code_strings[i].string;
1657 return "Unknown error";