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"
33 #include "param/param.h"
35 #define NDR_BASE_MARSHALL_SIZE 1024
37 /* this guid indicates NDR encoding in a protocol tower */
38 const struct ndr_syntax_id ndr_transfer_syntax = {
39 { 0x8a885d04, 0x1ceb, 0x11c9, {0x9f, 0xe8}, {0x08,0x00,0x2b,0x10,0x48,0x60} },
43 const struct ndr_syntax_id ndr64_transfer_syntax = {
44 { 0x71710533, 0xbeba, 0x4937, {0x83, 0x19}, {0xb5,0xdb,0xef,0x9c,0xcc,0x36} },
49 work out the number of bytes needed to align on a n byte boundary
51 _PUBLIC_ size_t ndr_align_size(uint32_t offset, size_t n)
53 if ((offset & (n-1)) == 0) return 0;
54 return n - (offset & (n-1));
58 initialise a ndr parse structure from a data blob
60 _PUBLIC_ struct ndr_pull *ndr_pull_init_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience)
64 ndr = talloc_zero(mem_ctx, struct ndr_pull);
65 if (!ndr) return NULL;
66 ndr->current_mem_ctx = mem_ctx;
68 ndr->data = blob->data;
69 ndr->data_size = blob->length;
70 ndr->iconv_convenience = talloc_reference(ndr, iconv_convenience);
76 advance by 'size' bytes
78 _PUBLIC_ enum ndr_err_code ndr_pull_advance(struct ndr_pull *ndr, uint32_t size)
81 if (ndr->offset > ndr->data_size) {
82 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
83 "ndr_pull_advance by %u failed",
86 return NDR_ERR_SUCCESS;
90 set the parse offset to 'ofs'
92 static enum ndr_err_code ndr_pull_set_offset(struct ndr_pull *ndr, uint32_t ofs)
95 if (ndr->offset > ndr->data_size) {
96 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
97 "ndr_pull_set_offset %u failed",
100 return NDR_ERR_SUCCESS;
103 /* create a ndr_push structure, ready for some marshalling */
104 _PUBLIC_ struct ndr_push *ndr_push_init_ctx(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience)
106 struct ndr_push *ndr;
108 ndr = talloc_zero(mem_ctx, struct ndr_push);
114 ndr->alloc_size = NDR_BASE_MARSHALL_SIZE;
115 ndr->data = talloc_array(ndr, uint8_t, ndr->alloc_size);
119 ndr->iconv_convenience = talloc_reference(ndr, iconv_convenience);
124 /* return a DATA_BLOB structure for the current ndr_push marshalled data */
125 _PUBLIC_ DATA_BLOB ndr_push_blob(struct ndr_push *ndr)
128 blob = data_blob_const(ndr->data, ndr->offset);
129 if (ndr->alloc_size > ndr->offset) {
130 ndr->data[ndr->offset] = 0;
137 expand the available space in the buffer to ndr->offset + extra_size
139 _PUBLIC_ enum ndr_err_code ndr_push_expand(struct ndr_push *ndr, uint32_t extra_size)
141 uint32_t size = extra_size + ndr->offset;
143 if (size < ndr->offset) {
144 /* extra_size overflowed the offset */
145 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, "Overflow in push_expand to %u",
149 if (ndr->alloc_size > size) {
150 return NDR_ERR_SUCCESS;
153 ndr->alloc_size += NDR_BASE_MARSHALL_SIZE;
154 if (size+1 > ndr->alloc_size) {
155 ndr->alloc_size = size+1;
157 ndr->data = talloc_realloc(ndr, ndr->data, uint8_t, ndr->alloc_size);
159 return ndr_push_error(ndr, NDR_ERR_ALLOC, "Failed to push_expand to %u",
163 return NDR_ERR_SUCCESS;
166 _PUBLIC_ void ndr_print_debug_helper(struct ndr_print *ndr, const char *format, ...)
172 va_start(ap, format);
173 vasprintf(&s, format, ap);
176 for (i=0;i<ndr->depth;i++) {
180 DEBUGADD(0,("%s\n", s));
184 _PUBLIC_ void ndr_print_string_helper(struct ndr_print *ndr, const char *format, ...)
189 for (i=0;i<ndr->depth;i++) {
190 ndr->private_data = talloc_asprintf_append_buffer(
191 (char *)ndr->private_data, " ");
194 va_start(ap, format);
195 ndr->private_data = talloc_vasprintf_append_buffer((char *)ndr->private_data,
198 ndr->private_data = talloc_asprintf_append_buffer((char *)ndr->private_data,
203 a useful helper function for printing idl structures via DEBUG()
205 _PUBLIC_ void ndr_print_debug(ndr_print_fn_t fn, const char *name, void *ptr)
207 struct ndr_print *ndr;
209 ndr = talloc_zero(NULL, struct ndr_print);
211 ndr->print = ndr_print_debug_helper;
219 a useful helper function for printing idl unions via DEBUG()
221 _PUBLIC_ void ndr_print_union_debug(ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
223 struct ndr_print *ndr;
225 ndr = talloc_zero(NULL, struct ndr_print);
227 ndr->print = ndr_print_debug_helper;
230 ndr_print_set_switch_value(ndr, ptr, level);
236 a useful helper function for printing idl function calls via DEBUG()
238 _PUBLIC_ void ndr_print_function_debug(ndr_print_function_t fn, const char *name, int flags, void *ptr)
240 struct ndr_print *ndr;
242 ndr = talloc_zero(NULL, struct ndr_print);
244 ndr->print = ndr_print_debug_helper;
247 fn(ndr, name, flags, ptr);
252 a useful helper function for printing idl structures to a string
254 _PUBLIC_ char *ndr_print_struct_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, void *ptr)
256 struct ndr_print *ndr;
259 ndr = talloc_zero(mem_ctx, struct ndr_print);
260 if (!ndr) return NULL;
261 ndr->private_data = talloc_strdup(ndr, "");
262 if (!ndr->private_data) {
265 ndr->print = ndr_print_string_helper;
269 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
276 a useful helper function for printing idl unions to a string
278 _PUBLIC_ char *ndr_print_union_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
280 struct ndr_print *ndr;
283 ndr = talloc_zero(mem_ctx, struct ndr_print);
284 if (!ndr) return NULL;
285 ndr->private_data = talloc_strdup(ndr, "");
286 if (!ndr->private_data) {
289 ndr->print = ndr_print_string_helper;
292 ndr_print_set_switch_value(ndr, ptr, level);
294 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
301 a useful helper function for printing idl function calls to a string
303 _PUBLIC_ char *ndr_print_function_string(TALLOC_CTX *mem_ctx,
304 ndr_print_function_t fn, const char *name,
305 int flags, void *ptr)
307 struct ndr_print *ndr;
310 ndr = talloc_zero(mem_ctx, struct ndr_print);
311 if (!ndr) return NULL;
312 ndr->private_data = talloc_strdup(ndr, "");
313 if (!ndr->private_data) {
316 ndr->print = ndr_print_string_helper;
319 fn(ndr, name, flags, ptr);
320 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
326 _PUBLIC_ void ndr_set_flags(uint32_t *pflags, uint32_t new_flags)
328 /* the big/little endian flags are inter-dependent */
329 if (new_flags & LIBNDR_FLAG_LITTLE_ENDIAN) {
330 (*pflags) &= ~LIBNDR_FLAG_BIGENDIAN;
332 if (new_flags & LIBNDR_FLAG_BIGENDIAN) {
333 (*pflags) &= ~LIBNDR_FLAG_LITTLE_ENDIAN;
335 if (new_flags & LIBNDR_FLAG_REMAINING) {
336 (*pflags) &= ~LIBNDR_ALIGN_FLAGS;
338 if (new_flags & LIBNDR_ALIGN_FLAGS) {
339 (*pflags) &= ~LIBNDR_FLAG_REMAINING;
341 (*pflags) |= new_flags;
345 return and possibly log an NDR error
347 _PUBLIC_ enum ndr_err_code ndr_pull_error(struct ndr_pull *ndr,
348 enum ndr_err_code ndr_err,
349 const char *format, ...)
354 va_start(ap, format);
355 vasprintf(&s, format, ap);
358 DEBUG(3,("ndr_pull_error(%u): %s\n", ndr_err, s));
366 return and possibly log an NDR error
368 _PUBLIC_ enum ndr_err_code ndr_push_error(struct ndr_push *ndr,
369 enum ndr_err_code ndr_err,
370 const char *format, ...)
375 va_start(ap, format);
376 vasprintf(&s, format, ap);
379 DEBUG(3,("ndr_push_error(%u): %s\n", ndr_err, s));
387 handle subcontext buffers, which in midl land are user-marshalled, but
388 we use magic in pidl to make them easier to cope with
390 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_start(struct ndr_pull *ndr,
391 struct ndr_pull **_subndr,
395 struct ndr_pull *subndr;
396 uint32_t r_content_size;
398 switch (header_size) {
400 uint32_t content_size = ndr->data_size - ndr->offset;
402 content_size = size_is;
404 r_content_size = content_size;
409 uint16_t content_size;
410 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &content_size));
411 if (size_is >= 0 && size_is != content_size) {
412 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
413 (int)size_is, (int)content_size);
415 r_content_size = content_size;
420 uint32_t content_size;
421 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
422 if (size_is >= 0 && size_is != content_size) {
423 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
424 (int)size_is, (int)content_size);
426 r_content_size = content_size;
430 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) header_size %d",
434 NDR_PULL_NEED_BYTES(ndr, r_content_size);
436 subndr = talloc_zero(ndr, struct ndr_pull);
437 NDR_ERR_HAVE_NO_MEMORY(subndr);
438 subndr->flags = ndr->flags;
439 subndr->current_mem_ctx = ndr->current_mem_ctx;
441 subndr->data = ndr->data + ndr->offset;
443 subndr->data_size = r_content_size;
444 subndr->iconv_convenience = talloc_reference(subndr, ndr->iconv_convenience);
447 return NDR_ERR_SUCCESS;
450 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_end(struct ndr_pull *ndr,
451 struct ndr_pull *subndr,
458 } else if (header_size > 0) {
459 advance = subndr->data_size;
461 advance = subndr->offset;
463 NDR_CHECK(ndr_pull_advance(ndr, advance));
464 return NDR_ERR_SUCCESS;
467 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_start(struct ndr_push *ndr,
468 struct ndr_push **_subndr,
472 struct ndr_push *subndr;
474 subndr = ndr_push_init_ctx(ndr, ndr->iconv_convenience);
475 NDR_ERR_HAVE_NO_MEMORY(subndr);
476 subndr->flags = ndr->flags;
479 return NDR_ERR_SUCCESS;
483 push a subcontext header
485 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_end(struct ndr_push *ndr,
486 struct ndr_push *subndr,
491 ssize_t padding_len = size_is - subndr->offset;
492 if (padding_len > 0) {
493 NDR_CHECK(ndr_push_zero(subndr, padding_len));
494 } else if (padding_len < 0) {
495 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
496 (int)subndr->offset, (int)size_is);
500 switch (header_size) {
505 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, subndr->offset));
509 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
513 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext header size %d",
517 NDR_CHECK(ndr_push_bytes(ndr, subndr->data, subndr->offset));
518 return NDR_ERR_SUCCESS;
522 store a token in the ndr context, for later retrieval
524 _PUBLIC_ enum ndr_err_code ndr_token_store(TALLOC_CTX *mem_ctx,
525 struct ndr_token_list **list,
529 struct ndr_token_list *tok;
530 tok = talloc(mem_ctx, struct ndr_token_list);
531 NDR_ERR_HAVE_NO_MEMORY(tok);
534 DLIST_ADD((*list), tok);
535 return NDR_ERR_SUCCESS;
539 retrieve a token from a ndr context, using cmp_fn to match the tokens
541 _PUBLIC_ enum ndr_err_code ndr_token_retrieve_cmp_fn(struct ndr_token_list **list, const void *key, uint32_t *v,
542 comparison_fn_t _cmp_fn, bool _remove_tok)
544 struct ndr_token_list *tok;
545 for (tok=*list;tok;tok=tok->next) {
546 if (_cmp_fn && _cmp_fn(tok->key,key)==0) goto found;
547 else if (!_cmp_fn && tok->key == key) goto found;
549 return NDR_ERR_TOKEN;
553 DLIST_REMOVE((*list), tok);
556 return NDR_ERR_SUCCESS;
560 retrieve a token from a ndr context
562 _PUBLIC_ enum ndr_err_code ndr_token_retrieve(struct ndr_token_list **list, const void *key, uint32_t *v)
564 return ndr_token_retrieve_cmp_fn(list, key, v, NULL, true);
568 peek at but don't removed a token from a ndr context
570 _PUBLIC_ uint32_t ndr_token_peek(struct ndr_token_list **list, const void *key)
572 enum ndr_err_code status;
575 status = ndr_token_retrieve_cmp_fn(list, key, &v, NULL, false);
576 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
584 pull an array size field and add it to the array_size_list token list
586 _PUBLIC_ enum ndr_err_code ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
589 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &size));
590 return ndr_token_store(ndr, &ndr->array_size_list, p, size);
594 get the stored array size field
596 _PUBLIC_ uint32_t ndr_get_array_size(struct ndr_pull *ndr, const void *p)
598 return ndr_token_peek(&ndr->array_size_list, p);
602 check the stored array size field
604 _PUBLIC_ enum ndr_err_code ndr_check_array_size(struct ndr_pull *ndr, void *p, uint32_t size)
607 stored = ndr_token_peek(&ndr->array_size_list, p);
608 if (stored != size) {
609 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
610 "Bad array size - got %u expected %u\n",
613 return NDR_ERR_SUCCESS;
617 pull an array length field and add it to the array_length_list token list
619 _PUBLIC_ enum ndr_err_code ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
621 uint32_t length, offset;
622 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &offset));
624 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
625 "non-zero array offset %u\n", offset);
627 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &length));
628 return ndr_token_store(ndr, &ndr->array_length_list, p, length);
632 get the stored array length field
634 _PUBLIC_ uint32_t ndr_get_array_length(struct ndr_pull *ndr, const void *p)
636 return ndr_token_peek(&ndr->array_length_list, p);
640 check the stored array length field
642 _PUBLIC_ enum ndr_err_code ndr_check_array_length(struct ndr_pull *ndr, void *p, uint32_t length)
645 stored = ndr_token_peek(&ndr->array_length_list, p);
646 if (stored != length) {
647 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
648 "Bad array length - got %u expected %u\n",
651 return NDR_ERR_SUCCESS;
657 _PUBLIC_ enum ndr_err_code ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
659 return ndr_token_store(ndr, &ndr->switch_list, p, val);
662 _PUBLIC_ enum ndr_err_code ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
664 return ndr_token_store(ndr, &ndr->switch_list, p, val);
667 _PUBLIC_ enum ndr_err_code ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
669 return ndr_token_store(ndr, &ndr->switch_list, p, val);
673 retrieve a switch value
675 _PUBLIC_ uint32_t ndr_push_get_switch_value(struct ndr_push *ndr, const void *p)
677 return ndr_token_peek(&ndr->switch_list, p);
680 _PUBLIC_ uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
682 return ndr_token_peek(&ndr->switch_list, p);
685 _PUBLIC_ uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
687 return ndr_token_peek(&ndr->switch_list, p);
691 pull a struct from a blob using NDR
693 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience, void *p,
694 ndr_pull_flags_fn_t fn)
696 struct ndr_pull *ndr;
697 ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
698 NDR_ERR_HAVE_NO_MEMORY(ndr);
699 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
700 return NDR_ERR_SUCCESS;
704 pull a struct from a blob using NDR - failing if all bytes are not consumed
706 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
707 struct smb_iconv_convenience *iconv_convenience,
708 void *p, ndr_pull_flags_fn_t fn)
710 struct ndr_pull *ndr;
711 ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
712 NDR_ERR_HAVE_NO_MEMORY(ndr);
713 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
714 if (ndr->offset < ndr->data_size) {
715 return ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
716 "not all bytes consumed ofs[%u] size[%u]",
717 ndr->offset, ndr->data_size);
719 return NDR_ERR_SUCCESS;
723 pull a union from a blob using NDR, given the union discriminator
725 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
726 struct smb_iconv_convenience *iconv_convenience, void *p,
727 uint32_t level, ndr_pull_flags_fn_t fn)
729 struct ndr_pull *ndr;
730 ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
731 NDR_ERR_HAVE_NO_MEMORY(ndr);
732 NDR_CHECK(ndr_pull_set_switch_value(ndr, p, level));
733 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
734 return NDR_ERR_SUCCESS;
738 pull a union from a blob using NDR, given the union discriminator,
739 failing if all bytes are not consumed
741 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
742 struct smb_iconv_convenience *iconv_convenience, void *p,
743 uint32_t level, ndr_pull_flags_fn_t fn)
745 struct ndr_pull *ndr;
746 ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
747 NDR_ERR_HAVE_NO_MEMORY(ndr);
748 NDR_CHECK(ndr_pull_set_switch_value(ndr, p, level));
749 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
750 if (ndr->offset < ndr->data_size) {
751 return ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
752 "not all bytes consumed ofs[%u] size[%u]",
753 ndr->offset, ndr->data_size);
755 return NDR_ERR_SUCCESS;
759 push a struct to a blob using NDR
761 _PUBLIC_ enum ndr_err_code ndr_push_struct_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience, const void *p, ndr_push_flags_fn_t fn)
763 struct ndr_push *ndr;
764 ndr = ndr_push_init_ctx(mem_ctx, iconv_convenience);
765 NDR_ERR_HAVE_NO_MEMORY(ndr);
767 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
769 *blob = ndr_push_blob(ndr);
770 talloc_steal(mem_ctx, blob->data);
773 return NDR_ERR_SUCCESS;
777 push a union to a blob using NDR
779 _PUBLIC_ enum ndr_err_code ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience, void *p,
780 uint32_t level, ndr_push_flags_fn_t fn)
782 struct ndr_push *ndr;
783 ndr = ndr_push_init_ctx(mem_ctx, iconv_convenience);
784 NDR_ERR_HAVE_NO_MEMORY(ndr);
786 NDR_CHECK(ndr_push_set_switch_value(ndr, p, level));
787 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
789 *blob = ndr_push_blob(ndr);
790 talloc_steal(mem_ctx, blob->data);
793 return NDR_ERR_SUCCESS;
797 generic ndr_size_*() handler for structures
799 _PUBLIC_ size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push)
801 struct ndr_push *ndr;
802 enum ndr_err_code status;
805 /* avoid recursion */
806 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
808 ndr = ndr_push_init_ctx(NULL, lp_iconv_convenience(global_loadparm));
810 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
811 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
812 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
822 generic ndr_size_*() handler for unions
824 _PUBLIC_ size_t ndr_size_union(const void *p, int flags, uint32_t level, ndr_push_flags_fn_t push)
826 struct ndr_push *ndr;
827 enum ndr_err_code status;
830 /* avoid recursion */
831 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
833 ndr = ndr_push_init_ctx(NULL, lp_iconv_convenience(global_loadparm));
835 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
837 status = ndr_push_set_switch_value(ndr, p, level);
838 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
842 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
843 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
853 get the current base for relative pointers for the push
855 _PUBLIC_ uint32_t ndr_push_get_relative_base_offset(struct ndr_push *ndr)
857 return ndr->relative_base_offset;
861 restore the old base for relative pointers for the push
863 _PUBLIC_ void ndr_push_restore_relative_base_offset(struct ndr_push *ndr, uint32_t offset)
865 ndr->relative_base_offset = offset;
869 setup the current base for relative pointers for the push
870 called in the NDR_SCALAR stage
872 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset1(struct ndr_push *ndr, const void *p, uint32_t offset)
874 ndr->relative_base_offset = offset;
875 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
879 setup the current base for relative pointers for the push
880 called in the NDR_BUFFERS stage
882 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset2(struct ndr_push *ndr, const void *p)
884 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
888 push a relative object - stage1
889 this is called during SCALARS processing
891 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
894 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
895 return NDR_ERR_SUCCESS;
897 NDR_CHECK(ndr_push_align(ndr, 4));
898 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
899 return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
903 push a relative object - stage2
904 this is called during buffers processing
906 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
908 uint32_t save_offset;
909 uint32_t ptr_offset = 0xFFFFFFFF;
911 return NDR_ERR_SUCCESS;
913 save_offset = ndr->offset;
914 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
915 if (ptr_offset > ndr->offset) {
916 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
917 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
918 ptr_offset, ndr->offset);
920 ndr->offset = ptr_offset;
921 if (save_offset < ndr->relative_base_offset) {
922 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
923 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
924 save_offset, ndr->relative_base_offset);
926 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
927 ndr->offset = save_offset;
928 return NDR_ERR_SUCCESS;
932 get the current base for relative pointers for the pull
934 _PUBLIC_ uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull *ndr)
936 return ndr->relative_base_offset;
940 restore the old base for relative pointers for the pull
942 _PUBLIC_ void ndr_pull_restore_relative_base_offset(struct ndr_pull *ndr, uint32_t offset)
944 ndr->relative_base_offset = offset;
948 setup the current base for relative pointers for the pull
949 called in the NDR_SCALAR stage
951 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset1(struct ndr_pull *ndr, const void *p, uint32_t offset)
953 ndr->relative_base_offset = offset;
954 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
958 setup the current base for relative pointers for the pull
959 called in the NDR_BUFFERS stage
961 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset2(struct ndr_pull *ndr, const void *p)
963 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
967 pull a relative object - stage1
968 called during SCALARS processing
970 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
972 rel_offset += ndr->relative_base_offset;
973 if (rel_offset > ndr->data_size) {
974 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
975 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
976 rel_offset, ndr->data_size);
978 return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
982 pull a relative object - stage2
983 called during BUFFERS processing
985 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
988 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
989 return ndr_pull_set_offset(ndr, rel_offset);