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 #if _SAMBA_BUILD_ == 4
33 #include "param/param.h"
36 #define NDR_BASE_MARSHALL_SIZE 1024
38 /* this guid indicates NDR encoding in a protocol tower */
39 const struct ndr_syntax_id ndr_transfer_syntax = {
40 { 0x8a885d04, 0x1ceb, 0x11c9, {0x9f, 0xe8}, {0x08,0x00,0x2b,0x10,0x48,0x60} },
44 const struct ndr_syntax_id ndr64_transfer_syntax = {
45 { 0x71710533, 0xbeba, 0x4937, {0x83, 0x19}, {0xb5,0xdb,0xef,0x9c,0xcc,0x36} },
50 work out the number of bytes needed to align on a n byte boundary
52 _PUBLIC_ size_t ndr_align_size(uint32_t offset, size_t n)
54 if ((offset & (n-1)) == 0) return 0;
55 return n - (offset & (n-1));
59 initialise a ndr parse structure from a data blob
61 _PUBLIC_ struct ndr_pull *ndr_pull_init_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx)
65 ndr = talloc_zero(mem_ctx, struct ndr_pull);
66 if (!ndr) return NULL;
67 ndr->current_mem_ctx = mem_ctx;
69 ndr->data = blob->data;
70 ndr->data_size = blob->length;
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)
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);
123 /* return a DATA_BLOB structure for the current ndr_push marshalled data */
124 _PUBLIC_ DATA_BLOB ndr_push_blob(struct ndr_push *ndr)
127 blob = data_blob_const(ndr->data, ndr->offset);
128 if (ndr->alloc_size > ndr->offset) {
129 ndr->data[ndr->offset] = 0;
136 expand the available space in the buffer to ndr->offset + extra_size
138 _PUBLIC_ enum ndr_err_code ndr_push_expand(struct ndr_push *ndr, uint32_t extra_size)
140 uint32_t size = extra_size + ndr->offset;
142 if (size < ndr->offset) {
143 /* extra_size overflowed the offset */
144 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, "Overflow in push_expand to %u",
148 if (ndr->alloc_size > size) {
149 return NDR_ERR_SUCCESS;
152 ndr->alloc_size += NDR_BASE_MARSHALL_SIZE;
153 if (size+1 > ndr->alloc_size) {
154 ndr->alloc_size = size+1;
156 ndr->data = talloc_realloc(ndr, ndr->data, uint8_t, ndr->alloc_size);
158 return ndr_push_error(ndr, NDR_ERR_ALLOC, "Failed to push_expand to %u",
162 return NDR_ERR_SUCCESS;
165 _PUBLIC_ void ndr_print_debug_helper(struct ndr_print *ndr, const char *format, ...)
171 va_start(ap, format);
172 ret = vasprintf(&s, format, ap);
179 if (ndr->no_newline) {
180 DEBUGADD(1,("%s", s));
185 for (i=0;i<ndr->depth;i++) {
189 DEBUGADD(1,("%s\n", s));
193 _PUBLIC_ void ndr_print_string_helper(struct ndr_print *ndr, const char *format, ...)
198 if (!ndr->no_newline) {
199 for (i=0;i<ndr->depth;i++) {
200 ndr->private_data = talloc_asprintf_append_buffer(
201 (char *)ndr->private_data, " ");
205 va_start(ap, format);
206 ndr->private_data = talloc_vasprintf_append_buffer((char *)ndr->private_data,
209 if (!ndr->no_newline) {
210 ndr->private_data = talloc_asprintf_append_buffer((char *)ndr->private_data,
216 a useful helper function for printing idl structures via DEBUG()
218 _PUBLIC_ void ndr_print_debug(ndr_print_fn_t fn, const char *name, void *ptr)
220 struct ndr_print *ndr;
224 ndr = talloc_zero(NULL, struct ndr_print);
226 ndr->print = ndr_print_debug_helper;
234 a useful helper function for printing idl unions via DEBUG()
236 _PUBLIC_ void ndr_print_union_debug(ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
238 struct ndr_print *ndr;
242 ndr = talloc_zero(NULL, struct ndr_print);
244 ndr->print = ndr_print_debug_helper;
247 ndr_print_set_switch_value(ndr, ptr, level);
253 a useful helper function for printing idl function calls via DEBUG()
255 _PUBLIC_ void ndr_print_function_debug(ndr_print_function_t fn, const char *name, int flags, void *ptr)
257 struct ndr_print *ndr;
261 ndr = talloc_zero(NULL, struct ndr_print);
263 ndr->print = ndr_print_debug_helper;
267 fn(ndr, name, flags, ptr);
272 a useful helper function for printing idl structures to a string
274 _PUBLIC_ char *ndr_print_struct_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, void *ptr)
276 struct ndr_print *ndr;
279 ndr = talloc_zero(mem_ctx, struct ndr_print);
280 if (!ndr) return NULL;
281 ndr->private_data = talloc_strdup(ndr, "");
282 if (!ndr->private_data) {
285 ndr->print = ndr_print_string_helper;
290 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
297 a useful helper function for printing idl unions to a string
299 _PUBLIC_ char *ndr_print_union_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
301 struct ndr_print *ndr;
304 ndr = talloc_zero(mem_ctx, struct ndr_print);
305 if (!ndr) return NULL;
306 ndr->private_data = talloc_strdup(ndr, "");
307 if (!ndr->private_data) {
310 ndr->print = ndr_print_string_helper;
313 ndr_print_set_switch_value(ndr, ptr, level);
315 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
322 a useful helper function for printing idl function calls to a string
324 _PUBLIC_ char *ndr_print_function_string(TALLOC_CTX *mem_ctx,
325 ndr_print_function_t fn, const char *name,
326 int flags, void *ptr)
328 struct ndr_print *ndr;
331 ndr = talloc_zero(mem_ctx, struct ndr_print);
332 if (!ndr) return NULL;
333 ndr->private_data = talloc_strdup(ndr, "");
334 if (!ndr->private_data) {
337 ndr->print = ndr_print_string_helper;
340 fn(ndr, name, flags, ptr);
341 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
347 _PUBLIC_ void ndr_set_flags(uint32_t *pflags, uint32_t new_flags)
349 /* the big/little endian flags are inter-dependent */
350 if (new_flags & LIBNDR_FLAG_LITTLE_ENDIAN) {
351 (*pflags) &= ~LIBNDR_FLAG_BIGENDIAN;
352 (*pflags) &= ~LIBNDR_FLAG_NDR64;
354 if (new_flags & LIBNDR_FLAG_BIGENDIAN) {
355 (*pflags) &= ~LIBNDR_FLAG_LITTLE_ENDIAN;
356 (*pflags) &= ~LIBNDR_FLAG_NDR64;
358 if (new_flags & LIBNDR_FLAG_REMAINING) {
359 (*pflags) &= ~LIBNDR_ALIGN_FLAGS;
361 if (new_flags & LIBNDR_ALIGN_FLAGS) {
362 (*pflags) &= ~LIBNDR_FLAG_REMAINING;
364 if (new_flags & LIBNDR_FLAG_NO_RELATIVE_REVERSE) {
365 (*pflags) &= ~LIBNDR_FLAG_RELATIVE_REVERSE;
367 (*pflags) |= new_flags;
371 return and possibly log an NDR error
373 _PUBLIC_ enum ndr_err_code ndr_pull_error(struct ndr_pull *ndr,
374 enum ndr_err_code ndr_err,
375 const char *format, ...)
381 va_start(ap, format);
382 ret = vasprintf(&s, format, ap);
386 return NDR_ERR_ALLOC;
389 DEBUG(1,("ndr_pull_error(%u): %s\n", ndr_err, s));
397 return and possibly log an NDR error
399 _PUBLIC_ enum ndr_err_code ndr_push_error(struct ndr_push *ndr,
400 enum ndr_err_code ndr_err,
401 const char *format, ...)
407 va_start(ap, format);
408 ret = vasprintf(&s, format, ap);
412 return NDR_ERR_ALLOC;
415 DEBUG(1,("ndr_push_error(%u): %s\n", ndr_err, s));
423 handle subcontext buffers, which in midl land are user-marshalled, but
424 we use magic in pidl to make them easier to cope with
426 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_start(struct ndr_pull *ndr,
427 struct ndr_pull **_subndr,
431 struct ndr_pull *subndr;
432 uint32_t r_content_size;
433 bool force_le = false;
434 bool force_be = false;
436 switch (header_size) {
438 uint32_t content_size = ndr->data_size - ndr->offset;
440 content_size = size_is;
442 r_content_size = content_size;
447 uint16_t content_size;
448 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &content_size));
449 if (size_is >= 0 && size_is != content_size) {
450 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
451 (int)size_is, (int)content_size);
453 r_content_size = content_size;
458 uint32_t content_size;
459 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &content_size));
460 if (size_is >= 0 && size_is != content_size) {
461 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
462 (int)size_is, (int)content_size);
464 r_content_size = content_size;
469 * Common Type Header for the Serialization Stream
470 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
476 uint32_t content_size;
480 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &version));
483 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
484 "Bad subcontext (PULL) Common Type Header version %d != 1",
492 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &drep));
495 } else if (drep == 0x00) {
498 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
499 "Bad subcontext (PULL) Common Type Header invalid drep 0x%02X",
503 /* length of the "Private Header for Constructed Type" */
504 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &hdrlen));
506 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
507 "Bad subcontext (PULL) Common Type Header length %d != 8",
511 /* filler should be ignored */
512 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &filler));
515 * Private Header for Constructed Type
517 /* length - will be updated latter */
518 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
519 if (size_is >= 0 && size_is != content_size) {
520 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
521 (int)size_is, (int)content_size);
523 /* the content size must be a multiple of 8 */
524 if ((content_size % 8) != 0) {
525 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
526 "Bad subcontext (PULL) size_is(%d) not padded to 8 content_size %d",
527 (int)size_is, (int)content_size);
529 r_content_size = content_size;
532 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &reserved));
536 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) header_size %d",
540 NDR_PULL_NEED_BYTES(ndr, r_content_size);
542 subndr = talloc_zero(ndr, struct ndr_pull);
543 NDR_ERR_HAVE_NO_MEMORY(subndr);
544 subndr->flags = ndr->flags & ~LIBNDR_FLAG_NDR64;
545 subndr->current_mem_ctx = ndr->current_mem_ctx;
547 subndr->data = ndr->data + ndr->offset;
549 subndr->data_size = r_content_size;
552 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_LITTLE_ENDIAN);
553 } else if (force_be) {
554 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_BIGENDIAN);
558 return NDR_ERR_SUCCESS;
561 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_end(struct ndr_pull *ndr,
562 struct ndr_pull *subndr,
569 } else if (header_size > 0) {
570 advance = subndr->data_size;
572 advance = subndr->offset;
574 NDR_CHECK(ndr_pull_advance(ndr, advance));
575 return NDR_ERR_SUCCESS;
578 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_start(struct ndr_push *ndr,
579 struct ndr_push **_subndr,
583 struct ndr_push *subndr;
585 subndr = ndr_push_init_ctx(ndr);
586 NDR_ERR_HAVE_NO_MEMORY(subndr);
587 subndr->flags = ndr->flags & ~LIBNDR_FLAG_NDR64;
590 NDR_CHECK(ndr_push_zero(subndr, size_is));
592 subndr->relative_end_offset = size_is;
596 return NDR_ERR_SUCCESS;
600 push a subcontext header
602 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_end(struct ndr_push *ndr,
603 struct ndr_push *subndr,
610 padding_len = size_is - subndr->offset;
611 if (padding_len < 0) {
612 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
613 (int)subndr->offset, (int)size_is);
615 subndr->offset = size_is;
618 switch (header_size) {
623 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, subndr->offset));
627 NDR_CHECK(ndr_push_uint3264(ndr, NDR_SCALARS, subndr->offset));
632 * Common Type Header for the Serialization Stream
633 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
635 padding_len = NDR_ROUND(subndr->offset, 8) - subndr->offset;
636 if (padding_len > 0) {
637 NDR_CHECK(ndr_push_zero(subndr, padding_len));
641 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, 1));
647 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, NDR_BE(ndr)?0x00:0x10));
649 /* length of the "Private Header for Constructed Type" */
650 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 8));
653 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0xCCCCCCCC));
656 * Private Header for Constructed Type
658 /* length - will be updated latter */
659 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
662 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
666 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext header size %d",
670 NDR_CHECK(ndr_push_bytes(ndr, subndr->data, subndr->offset));
671 return NDR_ERR_SUCCESS;
675 store a token in the ndr context, for later retrieval
677 _PUBLIC_ enum ndr_err_code ndr_token_store(TALLOC_CTX *mem_ctx,
678 struct ndr_token_list **list,
682 struct ndr_token_list *tok;
683 tok = talloc(mem_ctx, struct ndr_token_list);
684 NDR_ERR_HAVE_NO_MEMORY(tok);
687 DLIST_ADD((*list), tok);
688 return NDR_ERR_SUCCESS;
692 retrieve a token from a ndr context, using cmp_fn to match the tokens
694 _PUBLIC_ enum ndr_err_code ndr_token_retrieve_cmp_fn(struct ndr_token_list **list, const void *key, uint32_t *v,
695 comparison_fn_t _cmp_fn, bool _remove_tok)
697 struct ndr_token_list *tok;
698 for (tok=*list;tok;tok=tok->next) {
699 if (_cmp_fn && _cmp_fn(tok->key,key)==0) goto found;
700 else if (!_cmp_fn && tok->key == key) goto found;
702 return NDR_ERR_TOKEN;
706 DLIST_REMOVE((*list), tok);
709 return NDR_ERR_SUCCESS;
713 retrieve a token from a ndr context
715 _PUBLIC_ enum ndr_err_code ndr_token_retrieve(struct ndr_token_list **list, const void *key, uint32_t *v)
717 return ndr_token_retrieve_cmp_fn(list, key, v, NULL, true);
721 peek at but don't removed a token from a ndr context
723 _PUBLIC_ uint32_t ndr_token_peek(struct ndr_token_list **list, const void *key)
725 enum ndr_err_code status;
728 status = ndr_token_retrieve_cmp_fn(list, key, &v, NULL, false);
729 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
737 pull an array size field and add it to the array_size_list token list
739 _PUBLIC_ enum ndr_err_code ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
742 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &size));
743 return ndr_token_store(ndr, &ndr->array_size_list, p, size);
747 get the stored array size field
749 _PUBLIC_ uint32_t ndr_get_array_size(struct ndr_pull *ndr, const void *p)
751 return ndr_token_peek(&ndr->array_size_list, p);
755 check the stored array size field
757 _PUBLIC_ enum ndr_err_code ndr_check_array_size(struct ndr_pull *ndr, void *p, uint32_t size)
760 stored = ndr_token_peek(&ndr->array_size_list, p);
761 if (stored != size) {
762 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
763 "Bad array size - got %u expected %u\n",
766 return NDR_ERR_SUCCESS;
770 pull an array length field and add it to the array_length_list token list
772 _PUBLIC_ enum ndr_err_code ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
774 uint32_t length, offset;
775 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &offset));
777 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
778 "non-zero array offset %u\n", offset);
780 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &length));
781 return ndr_token_store(ndr, &ndr->array_length_list, p, length);
785 get the stored array length field
787 _PUBLIC_ uint32_t ndr_get_array_length(struct ndr_pull *ndr, const void *p)
789 return ndr_token_peek(&ndr->array_length_list, p);
793 check the stored array length field
795 _PUBLIC_ enum ndr_err_code ndr_check_array_length(struct ndr_pull *ndr, void *p, uint32_t length)
798 stored = ndr_token_peek(&ndr->array_length_list, p);
799 if (stored != length) {
800 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
801 "Bad array length - got %u expected %u\n",
804 return NDR_ERR_SUCCESS;
810 _PUBLIC_ enum ndr_err_code ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
812 return ndr_token_store(ndr, &ndr->switch_list, p, val);
815 _PUBLIC_ enum ndr_err_code ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
817 return ndr_token_store(ndr, &ndr->switch_list, p, val);
820 _PUBLIC_ enum ndr_err_code ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
822 return ndr_token_store(ndr, &ndr->switch_list, p, val);
826 retrieve a switch value
828 _PUBLIC_ uint32_t ndr_push_get_switch_value(struct ndr_push *ndr, const void *p)
830 return ndr_token_peek(&ndr->switch_list, p);
833 _PUBLIC_ uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
835 return ndr_token_peek(&ndr->switch_list, p);
838 _PUBLIC_ uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
840 return ndr_token_peek(&ndr->switch_list, p);
844 pull a struct from a blob using NDR
846 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
847 ndr_pull_flags_fn_t fn)
849 struct ndr_pull *ndr;
850 ndr = ndr_pull_init_blob(blob, mem_ctx);
851 NDR_ERR_HAVE_NO_MEMORY(ndr);
852 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
854 return NDR_ERR_SUCCESS;
858 pull a struct from a blob using NDR - failing if all bytes are not consumed
860 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
861 void *p, ndr_pull_flags_fn_t fn)
863 struct ndr_pull *ndr;
864 uint32_t highest_ofs;
865 ndr = ndr_pull_init_blob(blob, mem_ctx);
866 NDR_ERR_HAVE_NO_MEMORY(ndr);
867 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
868 if (ndr->offset > ndr->relative_highest_offset) {
869 highest_ofs = ndr->offset;
871 highest_ofs = ndr->relative_highest_offset;
873 if (highest_ofs < ndr->data_size) {
874 enum ndr_err_code ret;
875 ret = ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
876 "not all bytes consumed ofs[%u] size[%u]",
877 highest_ofs, ndr->data_size);
882 return NDR_ERR_SUCCESS;
886 pull a union from a blob using NDR, given the union discriminator
888 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
890 uint32_t level, ndr_pull_flags_fn_t fn)
892 struct ndr_pull *ndr;
893 ndr = ndr_pull_init_blob(blob, mem_ctx);
894 NDR_ERR_HAVE_NO_MEMORY(ndr);
895 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
896 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
898 return NDR_ERR_SUCCESS;
902 pull a union from a blob using NDR, given the union discriminator,
903 failing if all bytes are not consumed
905 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
907 uint32_t level, ndr_pull_flags_fn_t fn)
909 struct ndr_pull *ndr;
910 uint32_t highest_ofs;
911 ndr = ndr_pull_init_blob(blob, mem_ctx);
912 NDR_ERR_HAVE_NO_MEMORY(ndr);
913 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
914 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
915 if (ndr->offset > ndr->relative_highest_offset) {
916 highest_ofs = ndr->offset;
918 highest_ofs = ndr->relative_highest_offset;
920 if (highest_ofs < ndr->data_size) {
921 enum ndr_err_code ret;
922 ret = ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
923 "not all bytes consumed ofs[%u] size[%u]",
924 highest_ofs, ndr->data_size);
929 return NDR_ERR_SUCCESS;
933 push a struct to a blob using NDR
935 _PUBLIC_ enum ndr_err_code ndr_push_struct_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, const void *p, ndr_push_flags_fn_t fn)
937 struct ndr_push *ndr;
938 ndr = ndr_push_init_ctx(mem_ctx);
939 NDR_ERR_HAVE_NO_MEMORY(ndr);
941 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
943 *blob = ndr_push_blob(ndr);
944 talloc_steal(mem_ctx, blob->data);
947 return NDR_ERR_SUCCESS;
951 push a union to a blob using NDR
953 _PUBLIC_ enum ndr_err_code ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
954 uint32_t level, ndr_push_flags_fn_t fn)
956 struct ndr_push *ndr;
957 ndr = ndr_push_init_ctx(mem_ctx);
958 NDR_ERR_HAVE_NO_MEMORY(ndr);
960 NDR_CHECK(ndr_push_set_switch_value(ndr, p, level));
961 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
963 *blob = ndr_push_blob(ndr);
964 talloc_steal(mem_ctx, blob->data);
967 return NDR_ERR_SUCCESS;
971 generic ndr_size_*() handler for structures
973 _PUBLIC_ size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push)
975 struct ndr_push *ndr;
976 enum ndr_err_code status;
979 /* avoid recursion */
980 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
982 ndr = ndr_push_init_ctx(NULL);
984 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
985 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
986 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
996 generic ndr_size_*() handler for unions
998 _PUBLIC_ size_t ndr_size_union(const void *p, int flags, uint32_t level, ndr_push_flags_fn_t push)
1000 struct ndr_push *ndr;
1001 enum ndr_err_code status;
1004 /* avoid recursion */
1005 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
1007 ndr = ndr_push_init_ctx(NULL);
1009 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
1011 status = ndr_push_set_switch_value(ndr, p, level);
1012 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1016 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
1017 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1027 get the current base for relative pointers for the push
1029 _PUBLIC_ uint32_t ndr_push_get_relative_base_offset(struct ndr_push *ndr)
1031 return ndr->relative_base_offset;
1035 restore the old base for relative pointers for the push
1037 _PUBLIC_ void ndr_push_restore_relative_base_offset(struct ndr_push *ndr, uint32_t offset)
1039 ndr->relative_base_offset = offset;
1043 setup the current base for relative pointers for the push
1044 called in the NDR_SCALAR stage
1046 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset1(struct ndr_push *ndr, const void *p, uint32_t offset)
1048 ndr->relative_base_offset = offset;
1049 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1053 setup the current base for relative pointers for the push
1054 called in the NDR_BUFFERS stage
1056 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset2(struct ndr_push *ndr, const void *p)
1058 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1062 push a relative object - stage1
1063 this is called during SCALARS processing
1065 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
1068 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
1069 return NDR_ERR_SUCCESS;
1071 NDR_CHECK(ndr_push_align(ndr, 4));
1072 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1073 return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
1077 push a short relative object - stage1
1078 this is called during SCALARS processing
1080 _PUBLIC_ enum ndr_err_code ndr_push_short_relative_ptr1(struct ndr_push *ndr, const void *p)
1083 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 0));
1084 return NDR_ERR_SUCCESS;
1086 NDR_CHECK(ndr_push_align(ndr, 2));
1087 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1088 return ndr_push_uint16(ndr, NDR_SCALARS, 0xFFFF);
1091 push a relative object - stage2
1092 this is called during buffers processing
1094 static enum ndr_err_code ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
1096 uint32_t save_offset;
1097 uint32_t ptr_offset = 0xFFFFFFFF;
1099 return NDR_ERR_SUCCESS;
1101 save_offset = ndr->offset;
1102 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1103 if (ptr_offset > ndr->offset) {
1104 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1105 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1106 ptr_offset, ndr->offset);
1108 ndr->offset = ptr_offset;
1109 if (save_offset < ndr->relative_base_offset) {
1110 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1111 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1112 save_offset, ndr->relative_base_offset);
1114 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
1115 ndr->offset = save_offset;
1116 return NDR_ERR_SUCCESS;
1119 push a short relative object - stage2
1120 this is called during buffers processing
1122 _PUBLIC_ enum ndr_err_code ndr_push_short_relative_ptr2(struct ndr_push *ndr, const void *p)
1124 uint32_t save_offset;
1125 uint32_t ptr_offset = 0xFFFF;
1127 return NDR_ERR_SUCCESS;
1129 save_offset = ndr->offset;
1130 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1131 if (ptr_offset > ndr->offset) {
1132 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1133 "ndr_push_short_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1134 ptr_offset, ndr->offset);
1136 ndr->offset = ptr_offset;
1137 if (save_offset < ndr->relative_base_offset) {
1138 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1139 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1140 save_offset, ndr->relative_base_offset);
1142 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
1143 ndr->offset = save_offset;
1144 return NDR_ERR_SUCCESS;
1148 push a relative object - stage2 start
1149 this is called during buffers processing
1151 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_start(struct ndr_push *ndr, const void *p)
1154 return NDR_ERR_SUCCESS;
1156 if (!(ndr->flags & LIBNDR_FLAG_RELATIVE_REVERSE)) {
1157 return ndr_push_relative_ptr2(ndr, p);
1159 if (ndr->relative_end_offset == -1) {
1160 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1161 "ndr_push_relative_ptr2_start RELATIVE_REVERSE flag set and relative_end_offset %d",
1162 ndr->relative_end_offset);
1164 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_begin_list, p, ndr->offset));
1165 return NDR_ERR_SUCCESS;
1169 push a relative object - stage2 end
1170 this is called during buffers processing
1172 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_end(struct ndr_push *ndr, const void *p)
1174 uint32_t begin_offset = 0xFFFFFFFF;
1176 uint32_t correct_offset = 0;
1181 return NDR_ERR_SUCCESS;
1184 if (!(ndr->flags & LIBNDR_FLAG_RELATIVE_REVERSE)) {
1185 return NDR_ERR_SUCCESS;
1188 if (ndr->flags & LIBNDR_FLAG_NO_NDR_SIZE) {
1189 /* better say more than calculation a too small buffer */
1190 NDR_PUSH_ALIGN(ndr, 8);
1191 return NDR_ERR_SUCCESS;
1194 if (ndr->relative_end_offset < ndr->offset) {
1195 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1196 "ndr_push_relative_ptr2_end:"
1197 "relative_end_offset %u < offset %u",
1198 ndr->relative_end_offset, ndr->offset);
1201 NDR_CHECK(ndr_token_retrieve(&ndr->relative_begin_list, p, &begin_offset));
1203 /* we have marshalled a buffer, see how long it was */
1204 len = ndr->offset - begin_offset;
1207 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1208 "ndr_push_relative_ptr2_end:"
1209 "offset %u - begin_offset %u < 0",
1210 ndr->offset, begin_offset);
1213 if (ndr->relative_end_offset < len) {
1214 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1215 "ndr_push_relative_ptr2_end:"
1216 "relative_end_offset %u < len %lld",
1217 ndr->offset, (long long)len);
1220 /* the reversed offset is at the end of the main buffer */
1221 correct_offset = ndr->relative_end_offset - len;
1223 /* TODO: remove this hack and let the idl use FLAG_ALIGN2 explicit */
1226 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1228 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1230 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1234 pad = ndr_align_size(correct_offset, align);
1236 correct_offset += pad;
1237 correct_offset -= align;
1240 if (correct_offset < begin_offset) {
1241 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1242 "ndr_push_relative_ptr2_end: "
1243 "correct_offset %u < begin_offset %u",
1244 correct_offset, begin_offset);
1248 uint32_t clear_size = correct_offset - begin_offset;
1250 clear_size = MIN(clear_size, len);
1252 /* now move the marshalled buffer to the end of the main buffer */
1253 memmove(ndr->data + correct_offset, ndr->data + begin_offset, len);
1256 /* and wipe out old buffer within the main buffer */
1257 memset(ndr->data + begin_offset, '\0', clear_size);
1261 /* and set the end offset for the next buffer */
1262 ndr->relative_end_offset = correct_offset;
1264 /* finally write the offset to the main buffer */
1265 ndr->offset = correct_offset;
1266 NDR_CHECK(ndr_push_relative_ptr2(ndr, p));
1268 /* restore to where we were in the main buffer */
1269 ndr->offset = begin_offset;
1271 return NDR_ERR_SUCCESS;
1275 get the current base for relative pointers for the pull
1277 _PUBLIC_ uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull *ndr)
1279 return ndr->relative_base_offset;
1283 restore the old base for relative pointers for the pull
1285 _PUBLIC_ void ndr_pull_restore_relative_base_offset(struct ndr_pull *ndr, uint32_t offset)
1287 ndr->relative_base_offset = offset;
1291 setup the current base for relative pointers for the pull
1292 called in the NDR_SCALAR stage
1294 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset1(struct ndr_pull *ndr, const void *p, uint32_t offset)
1296 ndr->relative_base_offset = offset;
1297 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1301 setup the current base for relative pointers for the pull
1302 called in the NDR_BUFFERS stage
1304 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset2(struct ndr_pull *ndr, const void *p)
1306 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1310 pull a relative object - stage1
1311 called during SCALARS processing
1313 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
1315 rel_offset += ndr->relative_base_offset;
1316 if (rel_offset > ndr->data_size) {
1317 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
1318 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1319 rel_offset, ndr->data_size);
1321 return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
1325 pull a relative object - stage2
1326 called during BUFFERS processing
1328 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
1330 uint32_t rel_offset;
1331 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
1332 return ndr_pull_set_offset(ndr, rel_offset);
1335 const static struct {
1336 enum ndr_err_code err;
1338 } ndr_err_code_strings[] = {
1339 { NDR_ERR_SUCCESS, "Success" },
1340 { NDR_ERR_ARRAY_SIZE, "Bad Array Size" },
1341 { NDR_ERR_BAD_SWITCH, "Bad Switch" },
1342 { NDR_ERR_OFFSET, "Offset Error" },
1343 { NDR_ERR_RELATIVE, "Relative Pointer Error" },
1344 { NDR_ERR_CHARCNV, "Character Conversion Error" },
1345 { NDR_ERR_LENGTH, "Length Error" },
1346 { NDR_ERR_SUBCONTEXT, "Subcontext Error" },
1347 { NDR_ERR_COMPRESSION, "Compression Error" },
1348 { NDR_ERR_STRING, "String Error" },
1349 { NDR_ERR_VALIDATE, "Validate Error" },
1350 { NDR_ERR_BUFSIZE, "Buffer Size Error" },
1351 { NDR_ERR_ALLOC, "Allocation Error" },
1352 { NDR_ERR_RANGE, "Range Error" },
1353 { NDR_ERR_TOKEN, "Token Error" },
1354 { NDR_ERR_IPV4ADDRESS, "IPv4 Address Error" },
1355 { NDR_ERR_INVALID_POINTER, "Invalid Pointer" },
1356 { NDR_ERR_UNREAD_BYTES, "Unread Bytes" },
1357 { NDR_ERR_NDR64, "NDR64 assertion error" },
1361 _PUBLIC_ const char *ndr_map_error2string(enum ndr_err_code ndr_err)
1364 for (i = 0; ndr_err_code_strings[i].string != NULL; i++) {
1365 if (ndr_err_code_strings[i].err == ndr_err)
1366 return ndr_err_code_strings[i].string;
1368 return "Unknown error";