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, ...) _PRINTF_ATTRIBUTE(2,3)
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, ...) _PRINTF_ATTRIBUTE(2,3)
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, ...) _PRINTF_ATTRIBUTE(3,4)
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, ...) _PRINTF_ATTRIBUTE(3,4)
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, void *p,
694 ndr_pull_flags_fn_t fn)
696 struct ndr_pull *ndr;
697 ndr = ndr_pull_init_blob(blob, mem_ctx, lp_iconv_convenience(global_loadparm));
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, void *p,
707 ndr_pull_flags_fn_t fn)
709 struct ndr_pull *ndr;
710 ndr = ndr_pull_init_blob(blob, mem_ctx, lp_iconv_convenience(global_loadparm));
711 NDR_ERR_HAVE_NO_MEMORY(ndr);
712 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
713 if (ndr->offset < ndr->data_size) {
714 return ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
715 "not all bytes consumed ofs[%u] size[%u]",
716 ndr->offset, ndr->data_size);
718 return NDR_ERR_SUCCESS;
722 pull a union from a blob using NDR, given the union discriminator
724 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
725 uint32_t level, ndr_pull_flags_fn_t fn)
727 struct ndr_pull *ndr;
728 ndr = ndr_pull_init_blob(blob, mem_ctx, lp_iconv_convenience(global_loadparm));
729 NDR_ERR_HAVE_NO_MEMORY(ndr);
730 NDR_CHECK(ndr_pull_set_switch_value(ndr, p, level));
731 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
732 return NDR_ERR_SUCCESS;
736 pull a union from a blob using NDR, given the union discriminator,
737 failing if all bytes are not consumed
739 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
740 uint32_t level, ndr_pull_flags_fn_t fn)
742 struct ndr_pull *ndr;
743 ndr = ndr_pull_init_blob(blob, mem_ctx, lp_iconv_convenience(global_loadparm));
744 NDR_ERR_HAVE_NO_MEMORY(ndr);
745 NDR_CHECK(ndr_pull_set_switch_value(ndr, p, level));
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);
752 return NDR_ERR_SUCCESS;
756 push a struct to a blob using NDR
758 _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)
760 struct ndr_push *ndr;
761 ndr = ndr_push_init_ctx(mem_ctx, iconv_convenience);
762 NDR_ERR_HAVE_NO_MEMORY(ndr);
764 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
766 *blob = ndr_push_blob(ndr);
767 talloc_steal(mem_ctx, blob->data);
770 return NDR_ERR_SUCCESS;
774 push a union to a blob using NDR
776 _PUBLIC_ enum ndr_err_code ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
777 uint32_t level, ndr_push_flags_fn_t fn)
779 struct ndr_push *ndr;
780 ndr = ndr_push_init_ctx(mem_ctx, lp_iconv_convenience(global_loadparm));
781 NDR_ERR_HAVE_NO_MEMORY(ndr);
783 NDR_CHECK(ndr_push_set_switch_value(ndr, p, level));
784 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
786 *blob = ndr_push_blob(ndr);
787 talloc_steal(mem_ctx, blob->data);
790 return NDR_ERR_SUCCESS;
794 generic ndr_size_*() handler for structures
796 _PUBLIC_ size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push)
798 struct ndr_push *ndr;
799 enum ndr_err_code status;
802 /* avoid recursion */
803 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
805 ndr = ndr_push_init_ctx(NULL, lp_iconv_convenience(global_loadparm));
807 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
808 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
809 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
819 generic ndr_size_*() handler for unions
821 _PUBLIC_ size_t ndr_size_union(const void *p, int flags, uint32_t level, ndr_push_flags_fn_t push)
823 struct ndr_push *ndr;
824 enum ndr_err_code status;
827 /* avoid recursion */
828 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
830 ndr = ndr_push_init_ctx(NULL, lp_iconv_convenience(global_loadparm));
832 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
834 status = ndr_push_set_switch_value(ndr, p, level);
835 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
839 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
840 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
850 get the current base for relative pointers for the push
852 _PUBLIC_ uint32_t ndr_push_get_relative_base_offset(struct ndr_push *ndr)
854 return ndr->relative_base_offset;
858 restore the old base for relative pointers for the push
860 _PUBLIC_ void ndr_push_restore_relative_base_offset(struct ndr_push *ndr, uint32_t offset)
862 ndr->relative_base_offset = offset;
866 setup the current base for relative pointers for the push
867 called in the NDR_SCALAR stage
869 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset1(struct ndr_push *ndr, const void *p, uint32_t offset)
871 ndr->relative_base_offset = offset;
872 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
876 setup the current base for relative pointers for the push
877 called in the NDR_BUFFERS stage
879 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset2(struct ndr_push *ndr, const void *p)
881 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
885 push a relative object - stage1
886 this is called during SCALARS processing
888 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
891 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
892 return NDR_ERR_SUCCESS;
894 NDR_CHECK(ndr_push_align(ndr, 4));
895 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
896 return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
900 push a relative object - stage2
901 this is called during buffers processing
903 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
905 uint32_t save_offset;
906 uint32_t ptr_offset = 0xFFFFFFFF;
908 return NDR_ERR_SUCCESS;
910 save_offset = ndr->offset;
911 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
912 if (ptr_offset > ndr->offset) {
913 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
914 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
915 ptr_offset, ndr->offset);
917 ndr->offset = ptr_offset;
918 if (save_offset < ndr->relative_base_offset) {
919 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
920 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
921 save_offset, ndr->relative_base_offset);
923 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
924 ndr->offset = save_offset;
925 return NDR_ERR_SUCCESS;
929 get the current base for relative pointers for the pull
931 _PUBLIC_ uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull *ndr)
933 return ndr->relative_base_offset;
937 restore the old base for relative pointers for the pull
939 _PUBLIC_ void ndr_pull_restore_relative_base_offset(struct ndr_pull *ndr, uint32_t offset)
941 ndr->relative_base_offset = offset;
945 setup the current base for relative pointers for the pull
946 called in the NDR_SCALAR stage
948 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset1(struct ndr_pull *ndr, const void *p, uint32_t offset)
950 ndr->relative_base_offset = offset;
951 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
955 setup the current base for relative pointers for the pull
956 called in the NDR_BUFFERS stage
958 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset2(struct ndr_pull *ndr, const void *p)
960 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
964 pull a relative object - stage1
965 called during SCALARS processing
967 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
969 rel_offset += ndr->relative_base_offset;
970 if (rel_offset > ndr->data_size) {
971 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
972 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
973 rel_offset, ndr->data_size);
975 return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
979 pull a relative object - stage2
980 called during BUFFERS processing
982 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
985 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
986 return ndr_pull_set_offset(ndr, rel_offset);