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_ NTSTATUS 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",
88 set the parse offset to 'ofs'
90 static NTSTATUS 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",
101 /* save the offset/size of the current ndr state */
102 _PUBLIC_ void ndr_pull_save(struct ndr_pull *ndr, struct ndr_pull_save *save)
104 save->offset = ndr->offset;
105 save->data_size = ndr->data_size;
108 /* restore the size/offset of a ndr structure */
109 _PUBLIC_ void ndr_pull_restore(struct ndr_pull *ndr, struct ndr_pull_save *save)
111 ndr->offset = save->offset;
112 ndr->data_size = save->data_size;
116 /* create a ndr_push structure, ready for some marshalling */
117 _PUBLIC_ struct ndr_push *ndr_push_init_ctx(TALLOC_CTX *mem_ctx)
119 struct ndr_push *ndr;
121 ndr = talloc_zero(mem_ctx, struct ndr_push);
127 ndr->alloc_size = NDR_BASE_MARSHALL_SIZE;
128 ndr->data = talloc_array(ndr, uint8_t, ndr->alloc_size);
136 /* return a DATA_BLOB structure for the current ndr_push marshalled data */
137 _PUBLIC_ DATA_BLOB ndr_push_blob(struct ndr_push *ndr)
140 blob = data_blob_const(ndr->data, ndr->offset);
141 if (ndr->alloc_size > ndr->offset) {
142 ndr->data[ndr->offset] = 0;
149 expand the available space in the buffer to ndr->offset + extra_size
151 _PUBLIC_ NTSTATUS ndr_push_expand(struct ndr_push *ndr, uint32_t extra_size)
153 uint32_t size = extra_size + ndr->offset;
155 if (size < ndr->offset) {
156 /* extra_size overflowed the offset */
157 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, "Overflow in push_expand to %u",
161 if (ndr->alloc_size > size) {
165 ndr->alloc_size += NDR_BASE_MARSHALL_SIZE;
166 if (size+1 > ndr->alloc_size) {
167 ndr->alloc_size = size+1;
169 ndr->data = talloc_realloc(ndr, ndr->data, uint8_t, ndr->alloc_size);
171 return ndr_push_error(ndr, NDR_ERR_ALLOC, "Failed to push_expand to %u",
178 _PUBLIC_ void ndr_print_debug_helper(struct ndr_print *ndr, const char *format, ...) _PRINTF_ATTRIBUTE(2,3)
184 va_start(ap, format);
185 vasprintf(&s, format, ap);
188 for (i=0;i<ndr->depth;i++) {
192 DEBUGADD(0,("%s\n", s));
196 _PUBLIC_ void ndr_print_string_helper(struct ndr_print *ndr, const char *format, ...) _PRINTF_ATTRIBUTE(2,3)
201 for (i=0;i<ndr->depth;i++) {
202 ndr->private_data = talloc_asprintf_append_buffer(
203 (char *)ndr->private_data, " ");
206 va_start(ap, format);
207 ndr->private_data = talloc_vasprintf_append_buffer((char *)ndr->private_data,
210 ndr->private_data = talloc_asprintf_append_buffer((char *)ndr->private_data,
215 a useful helper function for printing idl structures via DEBUG()
217 _PUBLIC_ void ndr_print_debug(ndr_print_fn_t fn, const char *name, void *ptr)
219 struct ndr_print *ndr;
221 ndr = talloc_zero(NULL, struct ndr_print);
223 ndr->print = ndr_print_debug_helper;
231 a useful helper function for printing idl unions via DEBUG()
233 _PUBLIC_ void ndr_print_union_debug(ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
235 struct ndr_print *ndr;
237 ndr = talloc_zero(NULL, struct ndr_print);
239 ndr->print = ndr_print_debug_helper;
242 ndr_print_set_switch_value(ndr, ptr, level);
248 a useful helper function for printing idl function calls via DEBUG()
250 _PUBLIC_ void ndr_print_function_debug(ndr_print_function_t fn, const char *name, int flags, void *ptr)
252 struct ndr_print *ndr;
254 ndr = talloc_zero(NULL, struct ndr_print);
256 ndr->print = ndr_print_debug_helper;
259 fn(ndr, name, flags, ptr);
264 a useful helper function for printing idl structures to a string
266 _PUBLIC_ char *ndr_print_struct_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, void *ptr)
268 struct ndr_print *ndr;
271 ndr = talloc_zero(mem_ctx, struct ndr_print);
272 if (!ndr) return NULL;
273 ndr->private_data = talloc_strdup(ndr, "");
274 if (!ndr->private_data) {
277 ndr->print = ndr_print_string_helper;
281 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
288 a useful helper function for printing idl unions to a string
290 _PUBLIC_ char *ndr_print_union_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
292 struct ndr_print *ndr;
295 ndr = talloc_zero(mem_ctx, struct ndr_print);
296 if (!ndr) return NULL;
297 ndr->private_data = talloc_strdup(ndr, "");
298 if (!ndr->private_data) {
301 ndr->print = ndr_print_string_helper;
304 ndr_print_set_switch_value(ndr, ptr, level);
306 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
313 a useful helper function for printing idl function calls to a string
315 _PUBLIC_ char *ndr_print_function_string(TALLOC_CTX *mem_ctx,
316 ndr_print_function_t fn, const char *name,
317 int flags, void *ptr)
319 struct ndr_print *ndr;
322 ndr = talloc_zero(mem_ctx, struct ndr_print);
323 if (!ndr) return NULL;
324 ndr->private_data = talloc_strdup(ndr, "");
325 if (!ndr->private_data) {
328 ndr->print = ndr_print_string_helper;
331 fn(ndr, name, flags, ptr);
332 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
338 _PUBLIC_ void ndr_set_flags(uint32_t *pflags, uint32_t new_flags)
340 /* the big/little endian flags are inter-dependent */
341 if (new_flags & LIBNDR_FLAG_LITTLE_ENDIAN) {
342 (*pflags) &= ~LIBNDR_FLAG_BIGENDIAN;
344 if (new_flags & LIBNDR_FLAG_BIGENDIAN) {
345 (*pflags) &= ~LIBNDR_FLAG_LITTLE_ENDIAN;
347 if (new_flags & LIBNDR_FLAG_REMAINING) {
348 (*pflags) &= ~LIBNDR_ALIGN_FLAGS;
350 if (new_flags & LIBNDR_ALIGN_FLAGS) {
351 (*pflags) &= ~LIBNDR_FLAG_REMAINING;
353 (*pflags) |= new_flags;
356 static NTSTATUS ndr_map_error(enum ndr_err_code ndr_err)
359 case NDR_ERR_BUFSIZE:
360 return NT_STATUS_BUFFER_TOO_SMALL;
362 return NT_STATUS_INTERNAL_ERROR;
364 return NT_STATUS_NO_MEMORY;
365 case NDR_ERR_ARRAY_SIZE:
366 return NT_STATUS_ARRAY_BOUNDS_EXCEEDED;
367 case NDR_ERR_INVALID_POINTER:
368 return NT_STATUS_INVALID_PARAMETER_MIX;
369 case NDR_ERR_UNREAD_BYTES:
370 return NT_STATUS_PORT_MESSAGE_TOO_LONG;
375 /* we should map all error codes to different status codes */
376 return NT_STATUS_INVALID_PARAMETER;
380 return and possibly log an NDR error
382 _PUBLIC_ NTSTATUS ndr_pull_error(struct ndr_pull *ndr,
383 enum ndr_err_code ndr_err,
384 const char *format, ...) _PRINTF_ATTRIBUTE(3,4)
389 va_start(ap, format);
390 vasprintf(&s, format, ap);
393 DEBUG(3,("ndr_pull_error(%u): %s\n", ndr_err, s));
397 return ndr_map_error(ndr_err);
401 return and possibly log an NDR error
403 _PUBLIC_ NTSTATUS ndr_push_error(struct ndr_push *ndr,
404 enum ndr_err_code ndr_err,
405 const char *format, ...) _PRINTF_ATTRIBUTE(3,4)
410 va_start(ap, format);
411 vasprintf(&s, format, ap);
414 DEBUG(3,("ndr_push_error(%u): %s\n", ndr_err, s));
418 return ndr_map_error(ndr_err);
422 handle subcontext buffers, which in midl land are user-marshalled, but
423 we use magic in pidl to make them easier to cope with
425 _PUBLIC_ NTSTATUS ndr_pull_subcontext_start(struct ndr_pull *ndr,
426 struct ndr_pull **_subndr,
430 struct ndr_pull *subndr;
431 uint32_t r_content_size;
433 switch (header_size) {
435 uint32_t content_size = ndr->data_size - ndr->offset;
437 content_size = size_is;
439 r_content_size = content_size;
444 uint16_t content_size;
445 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &content_size));
446 if (size_is >= 0 && size_is != content_size) {
447 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
448 (int)size_is, (int)content_size);
450 r_content_size = content_size;
455 uint32_t content_size;
456 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
457 if (size_is >= 0 && size_is != content_size) {
458 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
459 (int)size_is, (int)content_size);
461 r_content_size = content_size;
465 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) header_size %d",
469 NDR_PULL_NEED_BYTES(ndr, r_content_size);
471 subndr = talloc_zero(ndr, struct ndr_pull);
472 NT_STATUS_HAVE_NO_MEMORY(subndr);
473 subndr->flags = ndr->flags;
474 subndr->current_mem_ctx = ndr->current_mem_ctx;
476 subndr->data = ndr->data + ndr->offset;
478 subndr->data_size = r_content_size;
484 _PUBLIC_ NTSTATUS ndr_pull_subcontext_end(struct ndr_pull *ndr,
485 struct ndr_pull *subndr,
492 } else if (header_size > 0) {
493 advance = subndr->data_size;
495 advance = subndr->offset;
497 NDR_CHECK(ndr_pull_advance(ndr, advance));
501 _PUBLIC_ NTSTATUS ndr_push_subcontext_start(struct ndr_push *ndr,
502 struct ndr_push **_subndr,
506 struct ndr_push *subndr;
508 subndr = ndr_push_init_ctx(ndr);
509 NT_STATUS_HAVE_NO_MEMORY(subndr);
510 subndr->flags = ndr->flags;
517 push a subcontext header
519 _PUBLIC_ NTSTATUS ndr_push_subcontext_end(struct ndr_push *ndr,
520 struct ndr_push *subndr,
525 ssize_t padding_len = size_is - subndr->offset;
526 if (padding_len > 0) {
527 NDR_CHECK(ndr_push_zero(subndr, padding_len));
528 } else if (padding_len < 0) {
529 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
530 (int)subndr->offset, (int)size_is);
534 switch (header_size) {
539 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, subndr->offset));
543 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
547 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext header size %d",
551 NDR_CHECK(ndr_push_bytes(ndr, subndr->data, subndr->offset));
556 store a token in the ndr context, for later retrieval
558 _PUBLIC_ NTSTATUS ndr_token_store(TALLOC_CTX *mem_ctx,
559 struct ndr_token_list **list,
563 struct ndr_token_list *tok;
564 tok = talloc(mem_ctx, struct ndr_token_list);
565 NT_STATUS_HAVE_NO_MEMORY(tok);
568 DLIST_ADD((*list), tok);
573 retrieve a token from a ndr context, using cmp_fn to match the tokens
575 _PUBLIC_ NTSTATUS ndr_token_retrieve_cmp_fn(struct ndr_token_list **list, const void *key, uint32_t *v,
576 comparison_fn_t _cmp_fn, bool _remove_tok)
578 struct ndr_token_list *tok;
579 for (tok=*list;tok;tok=tok->next) {
580 if (_cmp_fn && _cmp_fn(tok->key,key)==0) goto found;
581 else if (!_cmp_fn && tok->key == key) goto found;
583 return ndr_map_error(NDR_ERR_TOKEN);
587 DLIST_REMOVE((*list), tok);
594 retrieve a token from a ndr context
596 _PUBLIC_ NTSTATUS ndr_token_retrieve(struct ndr_token_list **list, const void *key, uint32_t *v)
598 return ndr_token_retrieve_cmp_fn(list, key, v, NULL, true);
602 peek at but don't removed a token from a ndr context
604 _PUBLIC_ uint32_t ndr_token_peek(struct ndr_token_list **list, const void *key)
609 status = ndr_token_retrieve_cmp_fn(list, key, &v, NULL, false);
610 if (!NT_STATUS_IS_OK(status)) {
618 pull an array size field and add it to the array_size_list token list
620 _PUBLIC_ NTSTATUS ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
623 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &size));
624 return ndr_token_store(ndr, &ndr->array_size_list, p, size);
628 get the stored array size field
630 _PUBLIC_ uint32_t ndr_get_array_size(struct ndr_pull *ndr, const void *p)
632 return ndr_token_peek(&ndr->array_size_list, p);
636 check the stored array size field
638 _PUBLIC_ NTSTATUS ndr_check_array_size(struct ndr_pull *ndr, void *p, uint32_t size)
641 stored = ndr_token_peek(&ndr->array_size_list, p);
642 if (stored != size) {
643 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
644 "Bad array size - got %u expected %u\n",
651 pull an array length field and add it to the array_length_list token list
653 _PUBLIC_ NTSTATUS ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
655 uint32_t length, offset;
656 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &offset));
658 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
659 "non-zero array offset %u\n", offset);
661 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &length));
662 return ndr_token_store(ndr, &ndr->array_length_list, p, length);
666 get the stored array length field
668 _PUBLIC_ uint32_t ndr_get_array_length(struct ndr_pull *ndr, const void *p)
670 return ndr_token_peek(&ndr->array_length_list, p);
674 check the stored array length field
676 _PUBLIC_ NTSTATUS ndr_check_array_length(struct ndr_pull *ndr, void *p, uint32_t length)
679 stored = ndr_token_peek(&ndr->array_length_list, p);
680 if (stored != length) {
681 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
682 "Bad array length - got %u expected %u\n",
691 _PUBLIC_ NTSTATUS ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
693 return ndr_token_store(ndr, &ndr->switch_list, p, val);
696 _PUBLIC_ NTSTATUS ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
698 return ndr_token_store(ndr, &ndr->switch_list, p, val);
701 _PUBLIC_ NTSTATUS ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
703 return ndr_token_store(ndr, &ndr->switch_list, p, val);
707 retrieve a switch value
709 _PUBLIC_ uint32_t ndr_push_get_switch_value(struct ndr_push *ndr, const void *p)
711 return ndr_token_peek(&ndr->switch_list, p);
714 _PUBLIC_ uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
716 return ndr_token_peek(&ndr->switch_list, p);
719 _PUBLIC_ uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
721 return ndr_token_peek(&ndr->switch_list, p);
725 pull a struct from a blob using NDR
727 _PUBLIC_ NTSTATUS ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
728 ndr_pull_flags_fn_t fn)
730 struct ndr_pull *ndr;
731 ndr = ndr_pull_init_blob(blob, mem_ctx);
732 NT_STATUS_HAVE_NO_MEMORY(ndr);
733 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
738 pull a struct from a blob using NDR - failing if all bytes are not consumed
740 _PUBLIC_ NTSTATUS ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
741 ndr_pull_flags_fn_t fn)
743 struct ndr_pull *ndr;
744 ndr = ndr_pull_init_blob(blob, mem_ctx);
745 NT_STATUS_HAVE_NO_MEMORY(ndr);
746 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
747 if (ndr->offset < ndr->data_size) {
748 return ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
749 "not all bytes consumed ofs[%u] size[%u]",
750 ndr->offset, ndr->data_size);
756 pull a union from a blob using NDR, given the union discriminator
758 _PUBLIC_ NTSTATUS ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
759 uint32_t level, ndr_pull_flags_fn_t fn)
761 struct ndr_pull *ndr;
762 ndr = ndr_pull_init_blob(blob, mem_ctx);
763 NT_STATUS_HAVE_NO_MEMORY(ndr);
764 NDR_CHECK(ndr_pull_set_switch_value(ndr, p, level));
765 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
770 pull a union from a blob using NDR, given the union discriminator,
771 failing if all bytes are not consumed
773 _PUBLIC_ NTSTATUS ndr_pull_union_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
774 uint32_t level, ndr_pull_flags_fn_t fn)
776 struct ndr_pull *ndr;
777 ndr = ndr_pull_init_blob(blob, mem_ctx);
778 NT_STATUS_HAVE_NO_MEMORY(ndr);
779 NDR_CHECK(ndr_pull_set_switch_value(ndr, p, level));
780 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
781 if (ndr->offset < ndr->data_size) {
782 return ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
783 "not all bytes consumed ofs[%u] size[%u]",
784 ndr->offset, ndr->data_size);
790 push a struct to a blob using NDR
792 _PUBLIC_ NTSTATUS ndr_push_struct_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, const void *p,
793 ndr_push_flags_fn_t fn)
795 struct ndr_push *ndr;
796 ndr = ndr_push_init_ctx(mem_ctx);
797 NT_STATUS_HAVE_NO_MEMORY(ndr);
799 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
801 *blob = ndr_push_blob(ndr);
802 talloc_steal(mem_ctx, blob->data);
809 push a union to a blob using NDR
811 _PUBLIC_ NTSTATUS ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
812 uint32_t level, ndr_push_flags_fn_t fn)
814 struct ndr_push *ndr;
815 ndr = ndr_push_init_ctx(mem_ctx);
816 NT_STATUS_HAVE_NO_MEMORY(ndr);
818 NDR_CHECK(ndr_push_set_switch_value(ndr, p, level));
819 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
821 *blob = ndr_push_blob(ndr);
822 talloc_steal(mem_ctx, blob->data);
829 generic ndr_size_*() handler for structures
831 _PUBLIC_ size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push)
833 struct ndr_push *ndr;
837 /* avoid recursion */
838 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
840 ndr = ndr_push_init_ctx(NULL);
842 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
843 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
844 if (!NT_STATUS_IS_OK(status)) {
854 generic ndr_size_*() handler for unions
856 _PUBLIC_ size_t ndr_size_union(const void *p, int flags, uint32_t level, ndr_push_flags_fn_t push)
858 struct ndr_push *ndr;
862 /* avoid recursion */
863 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
865 ndr = ndr_push_init_ctx(NULL);
867 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
869 status = ndr_push_set_switch_value(ndr, p, level);
870 if (!NT_STATUS_IS_OK(status)) {
874 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
875 if (!NT_STATUS_IS_OK(status)) {
885 get the current base for relative pointers for the push
887 _PUBLIC_ uint32_t ndr_push_get_relative_base_offset(struct ndr_push *ndr)
889 return ndr->relative_base_offset;
893 restore the old base for relative pointers for the push
895 _PUBLIC_ void ndr_push_restore_relative_base_offset(struct ndr_push *ndr, uint32_t offset)
897 ndr->relative_base_offset = offset;
901 setup the current base for relative pointers for the push
902 called in the NDR_SCALAR stage
904 _PUBLIC_ NTSTATUS ndr_push_setup_relative_base_offset1(struct ndr_push *ndr, const void *p, uint32_t offset)
906 ndr->relative_base_offset = offset;
907 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
911 setup the current base for relative pointers for the push
912 called in the NDR_BUFFERS stage
914 _PUBLIC_ NTSTATUS ndr_push_setup_relative_base_offset2(struct ndr_push *ndr, const void *p)
916 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
920 push a relative object - stage1
921 this is called during SCALARS processing
923 _PUBLIC_ NTSTATUS ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
926 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
929 NDR_CHECK(ndr_push_align(ndr, 4));
930 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
931 return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
935 push a relative object - stage2
936 this is called during buffers processing
938 _PUBLIC_ NTSTATUS ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
940 struct ndr_push_save save;
941 uint32_t ptr_offset = 0xFFFFFFFF;
945 ndr_push_save(ndr, &save);
946 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
947 if (ptr_offset > ndr->offset) {
948 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
949 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
950 ptr_offset, ndr->offset);
952 ndr->offset = ptr_offset;
953 if (save.offset < ndr->relative_base_offset) {
954 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
955 "ndr_push_relative_ptr2 save.offset(%u) < ndr->relative_base_offset(%u)",
956 save.offset, ndr->relative_base_offset);
958 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save.offset - ndr->relative_base_offset));
959 ndr_push_restore(ndr, &save);
964 get the current base for relative pointers for the pull
966 _PUBLIC_ uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull *ndr)
968 return ndr->relative_base_offset;
972 restore the old base for relative pointers for the pull
974 _PUBLIC_ void ndr_pull_restore_relative_base_offset(struct ndr_pull *ndr, uint32_t offset)
976 ndr->relative_base_offset = offset;
980 setup the current base for relative pointers for the pull
981 called in the NDR_SCALAR stage
983 _PUBLIC_ NTSTATUS ndr_pull_setup_relative_base_offset1(struct ndr_pull *ndr, const void *p, uint32_t offset)
985 ndr->relative_base_offset = offset;
986 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
990 setup the current base for relative pointers for the pull
991 called in the NDR_BUFFERS stage
993 _PUBLIC_ NTSTATUS ndr_pull_setup_relative_base_offset2(struct ndr_pull *ndr, const void *p)
995 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
999 pull a relative object - stage1
1000 called during SCALARS processing
1002 _PUBLIC_ NTSTATUS ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
1004 rel_offset += ndr->relative_base_offset;
1005 if (rel_offset > ndr->data_size) {
1006 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
1007 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1008 rel_offset, ndr->data_size);
1010 return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
1014 pull a relative object - stage2
1015 called during BUFFERS processing
1017 _PUBLIC_ NTSTATUS ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
1019 uint32_t rel_offset;
1020 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
1021 return ndr_pull_set_offset(ndr, rel_offset);