2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2003
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.
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.
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/>.
23 this provides the core routines for NDR parsing functions
25 see http://www.opengroup.org/onlinepubs/9629399/chap14.htm for details
30 #include "librpc/ndr/libndr.h"
31 #include "lib/util/dlinklist.h"
32 #include "librpc/gen_ndr/dcerpc.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 = {
38 { 0x8a885d04, 0x1ceb, 0x11c9, {0x9f, 0xe8}, {0x08,0x00,0x2b,0x10,0x48,0x60} },
42 const struct ndr_syntax_id ndr64_transfer_syntax = {
43 { 0x71710533, 0xbeba, 0x4937, {0x83, 0x19}, {0xb5,0xdb,0xef,0x9c,0xcc,0x36} },
48 work out the number of bytes needed to align on a n byte boundary
50 _PUBLIC_ size_t ndr_align_size(uint32_t offset, size_t n)
52 if ((offset & (n-1)) == 0) return 0;
53 return n - (offset & (n-1));
57 initialise a ndr parse structure from a data blob
59 _PUBLIC_ struct ndr_pull *ndr_pull_init_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx)
63 ndr = talloc_zero(mem_ctx, struct ndr_pull);
64 if (!ndr) return NULL;
65 ndr->current_mem_ctx = mem_ctx;
67 ndr->data = blob->data;
68 ndr->data_size = blob->length;
74 advance by 'size' bytes
76 _PUBLIC_ enum ndr_err_code ndr_pull_advance(struct ndr_pull *ndr, uint32_t size)
79 if (ndr->offset > ndr->data_size) {
80 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
81 "ndr_pull_advance by %u failed",
84 return NDR_ERR_SUCCESS;
88 set the parse offset to 'ofs'
90 static enum ndr_err_code ndr_pull_set_offset(struct ndr_pull *ndr, uint32_t ofs)
93 if (ndr->offset > ndr->data_size) {
94 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
95 "ndr_pull_set_offset %u failed",
98 return NDR_ERR_SUCCESS;
101 /* create a ndr_push structure, ready for some marshalling */
102 _PUBLIC_ struct ndr_push *ndr_push_init_ctx(TALLOC_CTX *mem_ctx)
104 struct ndr_push *ndr;
106 ndr = talloc_zero(mem_ctx, struct ndr_push);
112 ndr->alloc_size = NDR_BASE_MARSHALL_SIZE;
113 ndr->data = talloc_array(ndr, uint8_t, ndr->alloc_size);
121 /* return a DATA_BLOB structure for the current ndr_push marshalled data */
122 _PUBLIC_ DATA_BLOB ndr_push_blob(struct ndr_push *ndr)
125 blob = data_blob_const(ndr->data, ndr->offset);
126 if (ndr->alloc_size > ndr->offset) {
127 ndr->data[ndr->offset] = 0;
134 expand the available space in the buffer to ndr->offset + extra_size
136 _PUBLIC_ enum ndr_err_code ndr_push_expand(struct ndr_push *ndr, uint32_t extra_size)
138 uint32_t size = extra_size + ndr->offset;
140 if (size < ndr->offset) {
141 /* extra_size overflowed the offset */
142 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, "Overflow in push_expand to %u",
146 if (ndr->alloc_size > size) {
147 return NDR_ERR_SUCCESS;
150 ndr->alloc_size += NDR_BASE_MARSHALL_SIZE;
151 if (size+1 > ndr->alloc_size) {
152 ndr->alloc_size = size+1;
154 ndr->data = talloc_realloc(ndr, ndr->data, uint8_t, ndr->alloc_size);
156 return ndr_push_error(ndr, NDR_ERR_ALLOC, "Failed to push_expand to %u",
160 return NDR_ERR_SUCCESS;
163 _PUBLIC_ void ndr_print_debug_helper(struct ndr_print *ndr, const char *format, ...) _PRINTF_ATTRIBUTE(2,3)
169 va_start(ap, format);
170 vasprintf(&s, format, ap);
173 for (i=0;i<ndr->depth;i++) {
177 DEBUGADD(0,("%s\n", s));
181 _PUBLIC_ void ndr_print_string_helper(struct ndr_print *ndr, const char *format, ...) _PRINTF_ATTRIBUTE(2,3)
186 for (i=0;i<ndr->depth;i++) {
187 ndr->private_data = talloc_asprintf_append_buffer(
188 (char *)ndr->private_data, " ");
191 va_start(ap, format);
192 ndr->private_data = talloc_vasprintf_append_buffer((char *)ndr->private_data,
195 ndr->private_data = talloc_asprintf_append_buffer((char *)ndr->private_data,
200 a useful helper function for printing idl structures via DEBUG()
202 _PUBLIC_ void ndr_print_debug(ndr_print_fn_t fn, const char *name, void *ptr)
204 struct ndr_print *ndr;
206 ndr = talloc_zero(NULL, struct ndr_print);
208 ndr->print = ndr_print_debug_helper;
216 a useful helper function for printing idl unions via DEBUG()
218 _PUBLIC_ void ndr_print_union_debug(ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
220 struct ndr_print *ndr;
222 ndr = talloc_zero(NULL, struct ndr_print);
224 ndr->print = ndr_print_debug_helper;
227 ndr_print_set_switch_value(ndr, ptr, level);
233 a useful helper function for printing idl function calls via DEBUG()
235 _PUBLIC_ void ndr_print_function_debug(ndr_print_function_t fn, const char *name, int flags, void *ptr)
237 struct ndr_print *ndr;
239 ndr = talloc_zero(NULL, struct ndr_print);
241 ndr->print = ndr_print_debug_helper;
244 fn(ndr, name, flags, ptr);
249 a useful helper function for printing idl structures to a string
251 _PUBLIC_ char *ndr_print_struct_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, void *ptr)
253 struct ndr_print *ndr;
256 ndr = talloc_zero(mem_ctx, struct ndr_print);
257 if (!ndr) return NULL;
258 ndr->private_data = talloc_strdup(ndr, "");
259 if (!ndr->private_data) {
262 ndr->print = ndr_print_string_helper;
266 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
273 a useful helper function for printing idl unions to a string
275 _PUBLIC_ char *ndr_print_union_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
277 struct ndr_print *ndr;
280 ndr = talloc_zero(mem_ctx, struct ndr_print);
281 if (!ndr) return NULL;
282 ndr->private_data = talloc_strdup(ndr, "");
283 if (!ndr->private_data) {
286 ndr->print = ndr_print_string_helper;
289 ndr_print_set_switch_value(ndr, ptr, level);
291 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
298 a useful helper function for printing idl function calls to a string
300 _PUBLIC_ char *ndr_print_function_string(TALLOC_CTX *mem_ctx,
301 ndr_print_function_t fn, const char *name,
302 int flags, void *ptr)
304 struct ndr_print *ndr;
307 ndr = talloc_zero(mem_ctx, struct ndr_print);
308 if (!ndr) return NULL;
309 ndr->private_data = talloc_strdup(ndr, "");
310 if (!ndr->private_data) {
313 ndr->print = ndr_print_string_helper;
316 fn(ndr, name, flags, ptr);
317 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
323 _PUBLIC_ void ndr_set_flags(uint32_t *pflags, uint32_t new_flags)
325 /* the big/little endian flags are inter-dependent */
326 if (new_flags & LIBNDR_FLAG_LITTLE_ENDIAN) {
327 (*pflags) &= ~LIBNDR_FLAG_BIGENDIAN;
329 if (new_flags & LIBNDR_FLAG_BIGENDIAN) {
330 (*pflags) &= ~LIBNDR_FLAG_LITTLE_ENDIAN;
332 if (new_flags & LIBNDR_FLAG_REMAINING) {
333 (*pflags) &= ~LIBNDR_ALIGN_FLAGS;
335 if (new_flags & LIBNDR_ALIGN_FLAGS) {
336 (*pflags) &= ~LIBNDR_FLAG_REMAINING;
338 (*pflags) |= new_flags;
342 return and possibly log an NDR error
344 _PUBLIC_ enum ndr_err_code ndr_pull_error(struct ndr_pull *ndr,
345 enum ndr_err_code ndr_err,
346 const char *format, ...) _PRINTF_ATTRIBUTE(3,4)
351 va_start(ap, format);
352 vasprintf(&s, format, ap);
355 DEBUG(3,("ndr_pull_error(%u): %s\n", ndr_err, s));
363 return and possibly log an NDR error
365 _PUBLIC_ enum ndr_err_code ndr_push_error(struct ndr_push *ndr,
366 enum ndr_err_code ndr_err,
367 const char *format, ...) _PRINTF_ATTRIBUTE(3,4)
372 va_start(ap, format);
373 vasprintf(&s, format, ap);
376 DEBUG(3,("ndr_push_error(%u): %s\n", ndr_err, s));
384 handle subcontext buffers, which in midl land are user-marshalled, but
385 we use magic in pidl to make them easier to cope with
387 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_start(struct ndr_pull *ndr,
388 struct ndr_pull **_subndr,
392 struct ndr_pull *subndr;
393 uint32_t r_content_size;
395 switch (header_size) {
397 uint32_t content_size = ndr->data_size - ndr->offset;
399 content_size = size_is;
401 r_content_size = content_size;
406 uint16_t content_size;
407 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &content_size));
408 if (size_is >= 0 && size_is != content_size) {
409 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
410 (int)size_is, (int)content_size);
412 r_content_size = content_size;
417 uint32_t content_size;
418 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
419 if (size_is >= 0 && size_is != content_size) {
420 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
421 (int)size_is, (int)content_size);
423 r_content_size = content_size;
427 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) header_size %d",
431 NDR_PULL_NEED_BYTES(ndr, r_content_size);
433 subndr = talloc_zero(ndr, struct ndr_pull);
434 NDR_ERR_HAVE_NO_MEMORY(subndr);
435 subndr->flags = ndr->flags;
436 subndr->current_mem_ctx = ndr->current_mem_ctx;
438 subndr->data = ndr->data + ndr->offset;
440 subndr->data_size = r_content_size;
443 return NDR_ERR_SUCCESS;
446 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_end(struct ndr_pull *ndr,
447 struct ndr_pull *subndr,
454 } else if (header_size > 0) {
455 advance = subndr->data_size;
457 advance = subndr->offset;
459 NDR_CHECK(ndr_pull_advance(ndr, advance));
460 return NDR_ERR_SUCCESS;
463 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_start(struct ndr_push *ndr,
464 struct ndr_push **_subndr,
468 struct ndr_push *subndr;
470 subndr = ndr_push_init_ctx(ndr);
471 NDR_ERR_HAVE_NO_MEMORY(subndr);
472 subndr->flags = ndr->flags;
475 return NDR_ERR_SUCCESS;
479 push a subcontext header
481 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_end(struct ndr_push *ndr,
482 struct ndr_push *subndr,
487 ssize_t padding_len = size_is - subndr->offset;
488 if (padding_len > 0) {
489 NDR_CHECK(ndr_push_zero(subndr, padding_len));
490 } else if (padding_len < 0) {
491 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
492 (int)subndr->offset, (int)size_is);
496 switch (header_size) {
501 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, subndr->offset));
505 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
509 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext header size %d",
513 NDR_CHECK(ndr_push_bytes(ndr, subndr->data, subndr->offset));
514 return NDR_ERR_SUCCESS;
518 store a token in the ndr context, for later retrieval
520 _PUBLIC_ enum ndr_err_code ndr_token_store(TALLOC_CTX *mem_ctx,
521 struct ndr_token_list **list,
525 struct ndr_token_list *tok;
526 tok = talloc(mem_ctx, struct ndr_token_list);
527 NDR_ERR_HAVE_NO_MEMORY(tok);
530 DLIST_ADD((*list), tok);
531 return NDR_ERR_SUCCESS;
535 retrieve a token from a ndr context, using cmp_fn to match the tokens
537 _PUBLIC_ enum ndr_err_code ndr_token_retrieve_cmp_fn(struct ndr_token_list **list, const void *key, uint32_t *v,
538 comparison_fn_t _cmp_fn, bool _remove_tok)
540 struct ndr_token_list *tok;
541 for (tok=*list;tok;tok=tok->next) {
542 if (_cmp_fn && _cmp_fn(tok->key,key)==0) goto found;
543 else if (!_cmp_fn && tok->key == key) goto found;
545 return NDR_ERR_TOKEN;
549 DLIST_REMOVE((*list), tok);
552 return NDR_ERR_SUCCESS;
556 retrieve a token from a ndr context
558 _PUBLIC_ enum ndr_err_code ndr_token_retrieve(struct ndr_token_list **list, const void *key, uint32_t *v)
560 return ndr_token_retrieve_cmp_fn(list, key, v, NULL, true);
564 peek at but don't removed a token from a ndr context
566 _PUBLIC_ uint32_t ndr_token_peek(struct ndr_token_list **list, const void *key)
568 enum ndr_err_code status;
571 status = ndr_token_retrieve_cmp_fn(list, key, &v, NULL, false);
572 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
580 pull an array size field and add it to the array_size_list token list
582 _PUBLIC_ enum ndr_err_code ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
585 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &size));
586 return ndr_token_store(ndr, &ndr->array_size_list, p, size);
590 get the stored array size field
592 _PUBLIC_ uint32_t ndr_get_array_size(struct ndr_pull *ndr, const void *p)
594 return ndr_token_peek(&ndr->array_size_list, p);
598 check the stored array size field
600 _PUBLIC_ enum ndr_err_code ndr_check_array_size(struct ndr_pull *ndr, void *p, uint32_t size)
603 stored = ndr_token_peek(&ndr->array_size_list, p);
604 if (stored != size) {
605 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
606 "Bad array size - got %u expected %u\n",
609 return NDR_ERR_SUCCESS;
613 pull an array length field and add it to the array_length_list token list
615 _PUBLIC_ enum ndr_err_code ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
617 uint32_t length, offset;
618 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &offset));
620 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
621 "non-zero array offset %u\n", offset);
623 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &length));
624 return ndr_token_store(ndr, &ndr->array_length_list, p, length);
628 get the stored array length field
630 _PUBLIC_ uint32_t ndr_get_array_length(struct ndr_pull *ndr, const void *p)
632 return ndr_token_peek(&ndr->array_length_list, p);
636 check the stored array length field
638 _PUBLIC_ enum ndr_err_code ndr_check_array_length(struct ndr_pull *ndr, void *p, uint32_t length)
641 stored = ndr_token_peek(&ndr->array_length_list, p);
642 if (stored != length) {
643 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
644 "Bad array length - got %u expected %u\n",
647 return NDR_ERR_SUCCESS;
653 _PUBLIC_ enum ndr_err_code ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
655 return ndr_token_store(ndr, &ndr->switch_list, p, val);
658 _PUBLIC_ enum ndr_err_code ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
660 return ndr_token_store(ndr, &ndr->switch_list, p, val);
663 _PUBLIC_ enum ndr_err_code ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
665 return ndr_token_store(ndr, &ndr->switch_list, p, val);
669 retrieve a switch value
671 _PUBLIC_ uint32_t ndr_push_get_switch_value(struct ndr_push *ndr, const void *p)
673 return ndr_token_peek(&ndr->switch_list, p);
676 _PUBLIC_ uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
678 return ndr_token_peek(&ndr->switch_list, p);
681 _PUBLIC_ uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
683 return ndr_token_peek(&ndr->switch_list, p);
687 pull a struct from a blob using NDR
689 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
690 ndr_pull_flags_fn_t fn)
692 struct ndr_pull *ndr;
693 ndr = ndr_pull_init_blob(blob, mem_ctx);
694 NDR_ERR_HAVE_NO_MEMORY(ndr);
695 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
696 return NDR_ERR_SUCCESS;
700 pull a struct from a blob using NDR - failing if all bytes are not consumed
702 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
703 ndr_pull_flags_fn_t fn)
705 struct ndr_pull *ndr;
706 ndr = ndr_pull_init_blob(blob, mem_ctx);
707 NDR_ERR_HAVE_NO_MEMORY(ndr);
708 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
709 if (ndr->offset < ndr->data_size) {
710 return ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
711 "not all bytes consumed ofs[%u] size[%u]",
712 ndr->offset, ndr->data_size);
714 return NDR_ERR_SUCCESS;
718 pull a union from a blob using NDR, given the union discriminator
720 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
721 uint32_t level, ndr_pull_flags_fn_t fn)
723 struct ndr_pull *ndr;
724 ndr = ndr_pull_init_blob(blob, mem_ctx);
725 NDR_ERR_HAVE_NO_MEMORY(ndr);
726 NDR_CHECK(ndr_pull_set_switch_value(ndr, p, level));
727 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
728 return NDR_ERR_SUCCESS;
732 pull a union from a blob using NDR, given the union discriminator,
733 failing if all bytes are not consumed
735 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
736 uint32_t level, ndr_pull_flags_fn_t fn)
738 struct ndr_pull *ndr;
739 ndr = ndr_pull_init_blob(blob, mem_ctx);
740 NDR_ERR_HAVE_NO_MEMORY(ndr);
741 NDR_CHECK(ndr_pull_set_switch_value(ndr, p, level));
742 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
743 if (ndr->offset < ndr->data_size) {
744 return ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
745 "not all bytes consumed ofs[%u] size[%u]",
746 ndr->offset, ndr->data_size);
748 return NDR_ERR_SUCCESS;
752 push a struct to a blob using NDR
754 _PUBLIC_ enum ndr_err_code ndr_push_struct_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, const void *p,
755 ndr_push_flags_fn_t fn)
757 struct ndr_push *ndr;
758 ndr = ndr_push_init_ctx(mem_ctx);
759 NDR_ERR_HAVE_NO_MEMORY(ndr);
761 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
763 *blob = ndr_push_blob(ndr);
764 talloc_steal(mem_ctx, blob->data);
767 return NDR_ERR_SUCCESS;
771 push a union to a blob using NDR
773 _PUBLIC_ enum ndr_err_code ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
774 uint32_t level, ndr_push_flags_fn_t fn)
776 struct ndr_push *ndr;
777 ndr = ndr_push_init_ctx(mem_ctx);
778 NDR_ERR_HAVE_NO_MEMORY(ndr);
780 NDR_CHECK(ndr_push_set_switch_value(ndr, p, level));
781 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
783 *blob = ndr_push_blob(ndr);
784 talloc_steal(mem_ctx, blob->data);
787 return NDR_ERR_SUCCESS;
791 generic ndr_size_*() handler for structures
793 _PUBLIC_ size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push)
795 struct ndr_push *ndr;
796 enum ndr_err_code status;
799 /* avoid recursion */
800 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
802 ndr = ndr_push_init_ctx(NULL);
804 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
805 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
806 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
816 generic ndr_size_*() handler for unions
818 _PUBLIC_ size_t ndr_size_union(const void *p, int flags, uint32_t level, ndr_push_flags_fn_t push)
820 struct ndr_push *ndr;
821 enum ndr_err_code status;
824 /* avoid recursion */
825 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
827 ndr = ndr_push_init_ctx(NULL);
829 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
831 status = ndr_push_set_switch_value(ndr, p, level);
832 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
836 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
837 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
847 get the current base for relative pointers for the push
849 _PUBLIC_ uint32_t ndr_push_get_relative_base_offset(struct ndr_push *ndr)
851 return ndr->relative_base_offset;
855 restore the old base for relative pointers for the push
857 _PUBLIC_ void ndr_push_restore_relative_base_offset(struct ndr_push *ndr, uint32_t offset)
859 ndr->relative_base_offset = offset;
863 setup the current base for relative pointers for the push
864 called in the NDR_SCALAR stage
866 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset1(struct ndr_push *ndr, const void *p, uint32_t offset)
868 ndr->relative_base_offset = offset;
869 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
873 setup the current base for relative pointers for the push
874 called in the NDR_BUFFERS stage
876 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset2(struct ndr_push *ndr, const void *p)
878 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
882 push a relative object - stage1
883 this is called during SCALARS processing
885 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
888 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
889 return NDR_ERR_SUCCESS;
891 NDR_CHECK(ndr_push_align(ndr, 4));
892 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
893 return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
897 push a relative object - stage2
898 this is called during buffers processing
900 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
902 uint32_t save_offset;
903 uint32_t ptr_offset = 0xFFFFFFFF;
905 return NDR_ERR_SUCCESS;
907 save_offset = ndr->offset;
908 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
909 if (ptr_offset > ndr->offset) {
910 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
911 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
912 ptr_offset, ndr->offset);
914 ndr->offset = ptr_offset;
915 if (save_offset < ndr->relative_base_offset) {
916 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
917 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
918 save_offset, ndr->relative_base_offset);
920 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
921 ndr->offset = save_offset;
922 return NDR_ERR_SUCCESS;
926 get the current base for relative pointers for the pull
928 _PUBLIC_ uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull *ndr)
930 return ndr->relative_base_offset;
934 restore the old base for relative pointers for the pull
936 _PUBLIC_ void ndr_pull_restore_relative_base_offset(struct ndr_pull *ndr, uint32_t offset)
938 ndr->relative_base_offset = offset;
942 setup the current base for relative pointers for the pull
943 called in the NDR_SCALAR stage
945 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset1(struct ndr_pull *ndr, const void *p, uint32_t offset)
947 ndr->relative_base_offset = offset;
948 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
952 setup the current base for relative pointers for the pull
953 called in the NDR_BUFFERS stage
955 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset2(struct ndr_pull *ndr, const void *p)
957 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
961 pull a relative object - stage1
962 called during SCALARS processing
964 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
966 rel_offset += ndr->relative_base_offset;
967 if (rel_offset > ndr->data_size) {
968 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
969 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
970 rel_offset, ndr->data_size);
972 return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
976 pull a relative object - stage2
977 called during BUFFERS processing
979 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
982 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
983 return ndr_pull_set_offset(ndr, rel_offset);