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;
397 bool force_le = false;
398 bool force_be = false;
400 switch (header_size) {
402 uint32_t content_size = ndr->data_size - ndr->offset;
404 content_size = size_is;
406 r_content_size = content_size;
411 uint16_t content_size;
412 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &content_size));
413 if (size_is >= 0 && size_is != content_size) {
414 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
415 (int)size_is, (int)content_size);
417 r_content_size = content_size;
422 uint32_t content_size;
423 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
424 if (size_is >= 0 && size_is != content_size) {
425 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
426 (int)size_is, (int)content_size);
428 r_content_size = content_size;
433 * Common Type Header for the Serialization Stream
434 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
440 uint32_t content_size;
444 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &version));
447 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
448 "Bad subcontext (PULL) Common Type Header version %d != 1",
456 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &drep));
459 } else if (drep == 0x00) {
462 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
463 "Bad subcontext (PULL) Common Type Header invalid drep 0x%02X",
467 /* length of the "Private Header for Constructed Type" */
468 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &hdrlen));
470 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
471 "Bad subcontext (PULL) Common Type Header length %d != 8",
475 /* filler should be ignored */
476 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &filler));
479 * Private Header for Constructed Type
481 /* length - will be updated latter */
482 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
483 if (size_is >= 0 && size_is != content_size) {
484 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
485 (int)size_is, (int)content_size);
487 /* the content size must be a multiple of 8 */
488 if ((content_size % 8) != 0) {
489 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
490 "Bad subcontext (PULL) size_is(%d) not padded to 8 content_size %d",
491 (int)size_is, (int)content_size);
493 r_content_size = content_size;
496 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &reserved));
500 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) header_size %d",
504 NDR_PULL_NEED_BYTES(ndr, r_content_size);
506 subndr = talloc_zero(ndr, struct ndr_pull);
507 NDR_ERR_HAVE_NO_MEMORY(subndr);
508 subndr->flags = ndr->flags;
509 subndr->current_mem_ctx = ndr->current_mem_ctx;
511 subndr->data = ndr->data + ndr->offset;
513 subndr->data_size = r_content_size;
514 subndr->iconv_convenience = talloc_reference(subndr, ndr->iconv_convenience);
517 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_LITTLE_ENDIAN);
518 } else if (force_be) {
519 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_BIGENDIAN);
523 return NDR_ERR_SUCCESS;
526 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_end(struct ndr_pull *ndr,
527 struct ndr_pull *subndr,
534 } else if (header_size > 0) {
535 advance = subndr->data_size;
537 advance = subndr->offset;
539 NDR_CHECK(ndr_pull_advance(ndr, advance));
540 return NDR_ERR_SUCCESS;
543 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_start(struct ndr_push *ndr,
544 struct ndr_push **_subndr,
548 struct ndr_push *subndr;
550 subndr = ndr_push_init_ctx(ndr, ndr->iconv_convenience);
551 NDR_ERR_HAVE_NO_MEMORY(subndr);
552 subndr->flags = ndr->flags;
555 return NDR_ERR_SUCCESS;
559 push a subcontext header
561 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_end(struct ndr_push *ndr,
562 struct ndr_push *subndr,
569 padding_len = size_is - subndr->offset;
570 if (padding_len > 0) {
571 NDR_CHECK(ndr_push_zero(subndr, padding_len));
572 } else if (padding_len < 0) {
573 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
574 (int)subndr->offset, (int)size_is);
578 switch (header_size) {
583 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, subndr->offset));
587 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
592 * Common Type Header for the Serialization Stream
593 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
595 padding_len = NDR_ROUND(subndr->offset, 8) - subndr->offset;
596 if (padding_len > 0) {
597 NDR_CHECK(ndr_push_zero(subndr, padding_len));
601 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, 1));
607 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, NDR_BE(ndr)?0x00:0x10));
609 /* length of the "Private Header for Constructed Type" */
610 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 8));
613 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0xCCCCCCCC));
616 * Private Header for Constructed Type
618 /* length - will be updated latter */
619 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
622 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
626 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext header size %d",
630 NDR_CHECK(ndr_push_bytes(ndr, subndr->data, subndr->offset));
631 return NDR_ERR_SUCCESS;
635 store a token in the ndr context, for later retrieval
637 _PUBLIC_ enum ndr_err_code ndr_token_store(TALLOC_CTX *mem_ctx,
638 struct ndr_token_list **list,
642 struct ndr_token_list *tok;
643 tok = talloc(mem_ctx, struct ndr_token_list);
644 NDR_ERR_HAVE_NO_MEMORY(tok);
647 DLIST_ADD((*list), tok);
648 return NDR_ERR_SUCCESS;
652 retrieve a token from a ndr context, using cmp_fn to match the tokens
654 _PUBLIC_ enum ndr_err_code ndr_token_retrieve_cmp_fn(struct ndr_token_list **list, const void *key, uint32_t *v,
655 comparison_fn_t _cmp_fn, bool _remove_tok)
657 struct ndr_token_list *tok;
658 for (tok=*list;tok;tok=tok->next) {
659 if (_cmp_fn && _cmp_fn(tok->key,key)==0) goto found;
660 else if (!_cmp_fn && tok->key == key) goto found;
662 return NDR_ERR_TOKEN;
666 DLIST_REMOVE((*list), tok);
669 return NDR_ERR_SUCCESS;
673 retrieve a token from a ndr context
675 _PUBLIC_ enum ndr_err_code ndr_token_retrieve(struct ndr_token_list **list, const void *key, uint32_t *v)
677 return ndr_token_retrieve_cmp_fn(list, key, v, NULL, true);
681 peek at but don't removed a token from a ndr context
683 _PUBLIC_ uint32_t ndr_token_peek(struct ndr_token_list **list, const void *key)
685 enum ndr_err_code status;
688 status = ndr_token_retrieve_cmp_fn(list, key, &v, NULL, false);
689 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
697 pull an array size field and add it to the array_size_list token list
699 _PUBLIC_ enum ndr_err_code ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
702 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &size));
703 return ndr_token_store(ndr, &ndr->array_size_list, p, size);
707 get the stored array size field
709 _PUBLIC_ uint32_t ndr_get_array_size(struct ndr_pull *ndr, const void *p)
711 return ndr_token_peek(&ndr->array_size_list, p);
715 check the stored array size field
717 _PUBLIC_ enum ndr_err_code ndr_check_array_size(struct ndr_pull *ndr, void *p, uint32_t size)
720 stored = ndr_token_peek(&ndr->array_size_list, p);
721 if (stored != size) {
722 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
723 "Bad array size - got %u expected %u\n",
726 return NDR_ERR_SUCCESS;
730 pull an array length field and add it to the array_length_list token list
732 _PUBLIC_ enum ndr_err_code ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
734 uint32_t length, offset;
735 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &offset));
737 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
738 "non-zero array offset %u\n", offset);
740 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &length));
741 return ndr_token_store(ndr, &ndr->array_length_list, p, length);
745 get the stored array length field
747 _PUBLIC_ uint32_t ndr_get_array_length(struct ndr_pull *ndr, const void *p)
749 return ndr_token_peek(&ndr->array_length_list, p);
753 check the stored array length field
755 _PUBLIC_ enum ndr_err_code ndr_check_array_length(struct ndr_pull *ndr, void *p, uint32_t length)
758 stored = ndr_token_peek(&ndr->array_length_list, p);
759 if (stored != length) {
760 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
761 "Bad array length - got %u expected %u\n",
764 return NDR_ERR_SUCCESS;
770 _PUBLIC_ enum ndr_err_code ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
772 return ndr_token_store(ndr, &ndr->switch_list, p, val);
775 _PUBLIC_ enum ndr_err_code ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
777 return ndr_token_store(ndr, &ndr->switch_list, p, val);
780 _PUBLIC_ enum ndr_err_code ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
782 return ndr_token_store(ndr, &ndr->switch_list, p, val);
786 retrieve a switch value
788 _PUBLIC_ uint32_t ndr_push_get_switch_value(struct ndr_push *ndr, const void *p)
790 return ndr_token_peek(&ndr->switch_list, p);
793 _PUBLIC_ uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
795 return ndr_token_peek(&ndr->switch_list, p);
798 _PUBLIC_ uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
800 return ndr_token_peek(&ndr->switch_list, p);
804 pull a struct from a blob using NDR
806 _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,
807 ndr_pull_flags_fn_t fn)
809 struct ndr_pull *ndr;
810 ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
811 NDR_ERR_HAVE_NO_MEMORY(ndr);
812 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
813 return NDR_ERR_SUCCESS;
817 pull a struct from a blob using NDR - failing if all bytes are not consumed
819 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
820 struct smb_iconv_convenience *iconv_convenience,
821 void *p, ndr_pull_flags_fn_t fn)
823 struct ndr_pull *ndr;
824 ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
825 NDR_ERR_HAVE_NO_MEMORY(ndr);
826 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
827 if (ndr->offset < ndr->data_size) {
828 return ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
829 "not all bytes consumed ofs[%u] size[%u]",
830 ndr->offset, ndr->data_size);
832 return NDR_ERR_SUCCESS;
836 pull a union from a blob using NDR, given the union discriminator
838 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
839 struct smb_iconv_convenience *iconv_convenience, void *p,
840 uint32_t level, ndr_pull_flags_fn_t fn)
842 struct ndr_pull *ndr;
843 ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
844 NDR_ERR_HAVE_NO_MEMORY(ndr);
845 NDR_CHECK(ndr_pull_set_switch_value(ndr, p, level));
846 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
847 return NDR_ERR_SUCCESS;
851 pull a union from a blob using NDR, given the union discriminator,
852 failing if all bytes are not consumed
854 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
855 struct smb_iconv_convenience *iconv_convenience, void *p,
856 uint32_t level, ndr_pull_flags_fn_t fn)
858 struct ndr_pull *ndr;
859 ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
860 NDR_ERR_HAVE_NO_MEMORY(ndr);
861 NDR_CHECK(ndr_pull_set_switch_value(ndr, p, level));
862 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
863 if (ndr->offset < ndr->data_size) {
864 return ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
865 "not all bytes consumed ofs[%u] size[%u]",
866 ndr->offset, ndr->data_size);
868 return NDR_ERR_SUCCESS;
872 push a struct to a blob using NDR
874 _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)
876 struct ndr_push *ndr;
877 ndr = ndr_push_init_ctx(mem_ctx, iconv_convenience);
878 NDR_ERR_HAVE_NO_MEMORY(ndr);
880 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
882 *blob = ndr_push_blob(ndr);
883 talloc_steal(mem_ctx, blob->data);
886 return NDR_ERR_SUCCESS;
890 push a union to a blob using NDR
892 _PUBLIC_ enum ndr_err_code ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience, void *p,
893 uint32_t level, ndr_push_flags_fn_t fn)
895 struct ndr_push *ndr;
896 ndr = ndr_push_init_ctx(mem_ctx, iconv_convenience);
897 NDR_ERR_HAVE_NO_MEMORY(ndr);
899 NDR_CHECK(ndr_push_set_switch_value(ndr, p, level));
900 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
902 *blob = ndr_push_blob(ndr);
903 talloc_steal(mem_ctx, blob->data);
906 return NDR_ERR_SUCCESS;
910 generic ndr_size_*() handler for structures
912 _PUBLIC_ size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push)
914 struct ndr_push *ndr;
915 enum ndr_err_code status;
918 /* avoid recursion */
919 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
921 ndr = ndr_push_init_ctx(NULL, lp_iconv_convenience(global_loadparm));
923 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
924 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
925 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
935 generic ndr_size_*() handler for unions
937 _PUBLIC_ size_t ndr_size_union(const void *p, int flags, uint32_t level, ndr_push_flags_fn_t push)
939 struct ndr_push *ndr;
940 enum ndr_err_code status;
943 /* avoid recursion */
944 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
946 ndr = ndr_push_init_ctx(NULL, lp_iconv_convenience(global_loadparm));
948 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
950 status = ndr_push_set_switch_value(ndr, p, level);
951 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
955 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
956 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
966 get the current base for relative pointers for the push
968 _PUBLIC_ uint32_t ndr_push_get_relative_base_offset(struct ndr_push *ndr)
970 return ndr->relative_base_offset;
974 restore the old base for relative pointers for the push
976 _PUBLIC_ void ndr_push_restore_relative_base_offset(struct ndr_push *ndr, uint32_t offset)
978 ndr->relative_base_offset = offset;
982 setup the current base for relative pointers for the push
983 called in the NDR_SCALAR stage
985 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset1(struct ndr_push *ndr, const void *p, uint32_t offset)
987 ndr->relative_base_offset = offset;
988 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
992 setup the current base for relative pointers for the push
993 called in the NDR_BUFFERS stage
995 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset2(struct ndr_push *ndr, const void *p)
997 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1001 push a relative object - stage1
1002 this is called during SCALARS processing
1004 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
1007 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
1008 return NDR_ERR_SUCCESS;
1010 NDR_CHECK(ndr_push_align(ndr, 4));
1011 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1012 return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
1016 push a relative object - stage2
1017 this is called during buffers processing
1019 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
1021 uint32_t save_offset;
1022 uint32_t ptr_offset = 0xFFFFFFFF;
1024 return NDR_ERR_SUCCESS;
1026 save_offset = ndr->offset;
1027 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1028 if (ptr_offset > ndr->offset) {
1029 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1030 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1031 ptr_offset, ndr->offset);
1033 ndr->offset = ptr_offset;
1034 if (save_offset < ndr->relative_base_offset) {
1035 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1036 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1037 save_offset, ndr->relative_base_offset);
1039 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
1040 ndr->offset = save_offset;
1041 return NDR_ERR_SUCCESS;
1045 get the current base for relative pointers for the pull
1047 _PUBLIC_ uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull *ndr)
1049 return ndr->relative_base_offset;
1053 restore the old base for relative pointers for the pull
1055 _PUBLIC_ void ndr_pull_restore_relative_base_offset(struct ndr_pull *ndr, uint32_t offset)
1057 ndr->relative_base_offset = offset;
1061 setup the current base for relative pointers for the pull
1062 called in the NDR_SCALAR stage
1064 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset1(struct ndr_pull *ndr, const void *p, uint32_t offset)
1066 ndr->relative_base_offset = offset;
1067 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1071 setup the current base for relative pointers for the pull
1072 called in the NDR_BUFFERS stage
1074 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset2(struct ndr_pull *ndr, const void *p)
1076 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1080 pull a relative object - stage1
1081 called during SCALARS processing
1083 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
1085 rel_offset += ndr->relative_base_offset;
1086 if (rel_offset > ndr->data_size) {
1087 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
1088 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1089 rel_offset, ndr->data_size);
1091 return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
1095 pull a relative object - stage2
1096 called during BUFFERS processing
1098 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
1100 uint32_t rel_offset;
1101 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
1102 return ndr_pull_set_offset(ndr, rel_offset);