2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Jelmer Vernooij 2005-2008
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 this provides the core routines for NDR parsing functions
26 see http://www.opengroup.org/onlinepubs/9629399/chap14.htm for details
31 #include "librpc/ndr/libndr.h"
32 #include "../lib/util/dlinklist.h"
34 #define NDR_BASE_MARSHALL_SIZE 1024
36 /* this guid indicates NDR encoding in a protocol tower */
37 const struct ndr_syntax_id ndr_transfer_syntax_ndr
= {
38 { 0x8a885d04, 0x1ceb, 0x11c9, {0x9f, 0xe8}, {0x08,0x00,0x2b,0x10,0x48,0x60} },
42 const struct ndr_syntax_id ndr_transfer_syntax_ndr64
= {
43 { 0x71710533, 0xbeba, 0x4937, {0x83, 0x19}, {0xb5,0xdb,0xef,0x9c,0xcc,0x36} },
47 const struct ndr_syntax_id ndr_syntax_id_null
= {
48 { 0, 0, 0, { 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
53 work out the number of bytes needed to align on a n byte boundary
55 _PUBLIC_
size_t ndr_align_size(uint32_t offset
, size_t n
)
57 if ((offset
& (n
-1)) == 0) return 0;
58 return n
- (offset
& (n
-1));
62 initialise a ndr parse structure from a data blob
64 _PUBLIC_
struct ndr_pull
*ndr_pull_init_blob(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
)
68 ndr
= talloc_zero(mem_ctx
, struct ndr_pull
);
69 if (!ndr
) return NULL
;
70 ndr
->current_mem_ctx
= mem_ctx
;
72 ndr
->data
= blob
->data
;
73 ndr
->data_size
= blob
->length
;
78 _PUBLIC_
enum ndr_err_code
ndr_pull_append(struct ndr_pull
*ndr
, DATA_BLOB
*blob
)
80 enum ndr_err_code ndr_err
;
85 if (blob
->length
== 0) {
86 return NDR_ERR_SUCCESS
;
89 ndr_err
= ndr_token_retrieve(&ndr
->array_size_list
, ndr
, &append
);
90 if (ndr_err
== NDR_ERR_TOKEN
) {
92 ndr_err
= NDR_ERR_SUCCESS
;
96 if (ndr
->data_size
== 0) {
101 if (append
== UINT32_MAX
) {
103 * append == UINT32_MAX means that
104 * ndr->data is either NULL or a valid
105 * talloc child of ndr, which means
106 * we can use data_blob_append() without
107 * data_blob_talloc() of the existing callers data
109 b
= data_blob_const(ndr
->data
, ndr
->data_size
);
111 b
= data_blob_talloc(ndr
, ndr
->data
, ndr
->data_size
);
112 if (b
.data
== NULL
) {
113 return ndr_pull_error(ndr
, NDR_ERR_ALLOC
, "%s", __location__
);
117 ok
= data_blob_append(ndr
, &b
, blob
->data
, blob
->length
);
119 return ndr_pull_error(ndr
, NDR_ERR_ALLOC
, "%s", __location__
);
123 ndr
->data_size
= b
.length
;
125 return ndr_token_store(ndr
, &ndr
->array_size_list
, ndr
, UINT32_MAX
);
128 _PUBLIC_
enum ndr_err_code
ndr_pull_pop(struct ndr_pull
*ndr
)
133 if (ndr
->relative_base_offset
!= 0) {
134 return ndr_pull_error(ndr
, NDR_ERR_RELATIVE
,
137 if (ndr
->relative_highest_offset
!= 0) {
138 return ndr_pull_error(ndr
, NDR_ERR_RELATIVE
,
141 if (ndr
->relative_list
!= NULL
) {
142 return ndr_pull_error(ndr
, NDR_ERR_RELATIVE
,
145 if (ndr
->relative_base_list
!= NULL
) {
146 return ndr_pull_error(ndr
, NDR_ERR_RELATIVE
,
151 * we need to keep up to 7 bytes
152 * in order to get the aligment right.
154 skip
= ndr
->offset
& 0xFFFFFFF8;
157 return NDR_ERR_SUCCESS
;
161 ndr
->data_size
-= skip
;
163 append
= ndr_token_peek(&ndr
->array_size_list
, ndr
);
164 if (append
!= UINT32_MAX
) {
166 * here we assume, that ndr->data is not a
167 * talloc child of ndr.
170 return NDR_ERR_SUCCESS
;
173 memmove(ndr
->data
, ndr
->data
+ skip
, ndr
->data_size
);
175 ndr
->data
= talloc_realloc(ndr
, ndr
->data
, uint8_t, ndr
->data_size
);
176 if (ndr
->data_size
!= 0 && ndr
->data
== NULL
) {
177 return ndr_pull_error(ndr
, NDR_ERR_ALLOC
, "%s", __location__
);
180 return NDR_ERR_SUCCESS
;
184 advance by 'size' bytes
186 _PUBLIC_
enum ndr_err_code
ndr_pull_advance(struct ndr_pull
*ndr
, uint32_t size
)
189 if (ndr
->offset
> ndr
->data_size
) {
190 return ndr_pull_error(ndr
, NDR_ERR_BUFSIZE
,
191 "ndr_pull_advance by %u failed",
194 return NDR_ERR_SUCCESS
;
198 set the parse offset to 'ofs'
200 static enum ndr_err_code
ndr_pull_set_offset(struct ndr_pull
*ndr
, uint32_t ofs
)
203 if (ndr
->offset
> ndr
->data_size
) {
204 return ndr_pull_error(ndr
, NDR_ERR_BUFSIZE
,
205 "ndr_pull_set_offset %u failed",
208 return NDR_ERR_SUCCESS
;
211 /* create a ndr_push structure, ready for some marshalling */
212 _PUBLIC_
struct ndr_push
*ndr_push_init_ctx(TALLOC_CTX
*mem_ctx
)
214 struct ndr_push
*ndr
;
216 ndr
= talloc_zero(mem_ctx
, struct ndr_push
);
222 ndr
->alloc_size
= NDR_BASE_MARSHALL_SIZE
;
223 ndr
->data
= talloc_array(ndr
, uint8_t, ndr
->alloc_size
);
232 /* return a DATA_BLOB structure for the current ndr_push marshalled data */
233 _PUBLIC_ DATA_BLOB
ndr_push_blob(struct ndr_push
*ndr
)
236 blob
= data_blob_const(ndr
->data
, ndr
->offset
);
237 if (ndr
->alloc_size
> ndr
->offset
) {
238 ndr
->data
[ndr
->offset
] = 0;
245 expand the available space in the buffer to ndr->offset + extra_size
247 _PUBLIC_
enum ndr_err_code
ndr_push_expand(struct ndr_push
*ndr
, uint32_t extra_size
)
249 uint32_t size
= extra_size
+ ndr
->offset
;
251 if (size
< ndr
->offset
) {
252 /* extra_size overflowed the offset */
253 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
, "Overflow in push_expand to %u",
257 if (ndr
->alloc_size
> size
) {
258 return NDR_ERR_SUCCESS
;
261 ndr
->alloc_size
+= NDR_BASE_MARSHALL_SIZE
;
262 if (size
+1 > ndr
->alloc_size
) {
263 ndr
->alloc_size
= size
+1;
265 ndr
->data
= talloc_realloc(ndr
, ndr
->data
, uint8_t, ndr
->alloc_size
);
267 return ndr_push_error(ndr
, NDR_ERR_ALLOC
, "Failed to push_expand to %u",
271 return NDR_ERR_SUCCESS
;
274 _PUBLIC_
void ndr_print_debugc_helper(struct ndr_print
*ndr
, const char *format
, ...)
282 va_start(ap
, format
);
283 ret
= vasprintf(&s
, format
, ap
);
290 dbgc_class
= *(int *)ndr
->private_data
;
292 if (ndr
->no_newline
) {
293 DEBUGADDC(dbgc_class
, 1,("%s", s
));
298 for (i
=0;i
<ndr
->depth
;i
++) {
299 DEBUGADDC(dbgc_class
, 1,(" "));
302 DEBUGADDC(dbgc_class
, 1,("%s\n", s
));
306 _PUBLIC_
void ndr_print_debug_helper(struct ndr_print
*ndr
, const char *format
, ...)
313 va_start(ap
, format
);
314 ret
= vasprintf(&s
, format
, ap
);
321 if (ndr
->no_newline
) {
322 DEBUGADD(1,("%s", s
));
327 for (i
=0;i
<ndr
->depth
;i
++) {
331 DEBUGADD(1,("%s\n", s
));
335 _PUBLIC_
void ndr_print_printf_helper(struct ndr_print
*ndr
, const char *format
, ...)
340 if (!ndr
->no_newline
) {
341 for (i
=0;i
<ndr
->depth
;i
++) {
346 va_start(ap
, format
);
349 if (!ndr
->no_newline
) {
354 _PUBLIC_
void ndr_print_string_helper(struct ndr_print
*ndr
, const char *format
, ...)
359 if (!ndr
->no_newline
) {
360 for (i
=0;i
<ndr
->depth
;i
++) {
361 ndr
->private_data
= talloc_asprintf_append_buffer(
362 (char *)ndr
->private_data
, " ");
366 va_start(ap
, format
);
367 ndr
->private_data
= talloc_vasprintf_append_buffer((char *)ndr
->private_data
,
370 if (!ndr
->no_newline
) {
371 ndr
->private_data
= talloc_asprintf_append_buffer((char *)ndr
->private_data
,
377 a useful helper function for printing idl structures via DEBUGC()
379 _PUBLIC_
void ndr_print_debugc(int dbgc_class
, ndr_print_fn_t fn
, const char *name
, void *ptr
)
381 struct ndr_print
*ndr
;
383 DEBUGC(dbgc_class
, 1,(" "));
385 ndr
= talloc_zero(NULL
, struct ndr_print
);
387 ndr
->private_data
= &dbgc_class
;
388 ndr
->print
= ndr_print_debugc_helper
;
396 a useful helper function for printing idl structures via DEBUG()
398 _PUBLIC_
void ndr_print_debug(ndr_print_fn_t fn
, const char *name
, void *ptr
)
400 struct ndr_print
*ndr
;
404 ndr
= talloc_zero(NULL
, struct ndr_print
);
406 ndr
->print
= ndr_print_debug_helper
;
414 a useful helper function for printing idl unions via DEBUG()
416 _PUBLIC_
void ndr_print_union_debug(ndr_print_fn_t fn
, const char *name
, uint32_t level
, void *ptr
)
418 struct ndr_print
*ndr
;
422 ndr
= talloc_zero(NULL
, struct ndr_print
);
424 ndr
->print
= ndr_print_debug_helper
;
427 ndr_print_set_switch_value(ndr
, ptr
, level
);
433 a useful helper function for printing idl function calls via DEBUG()
435 _PUBLIC_
void ndr_print_function_debug(ndr_print_function_t fn
, const char *name
, int flags
, void *ptr
)
437 struct ndr_print
*ndr
;
441 ndr
= talloc_zero(NULL
, struct ndr_print
);
443 ndr
->print
= ndr_print_debug_helper
;
447 fn(ndr
, name
, flags
, ptr
);
452 a useful helper function for printing idl structures to a string
454 _PUBLIC_
char *ndr_print_struct_string(TALLOC_CTX
*mem_ctx
, ndr_print_fn_t fn
, const char *name
, void *ptr
)
456 struct ndr_print
*ndr
;
459 ndr
= talloc_zero(mem_ctx
, struct ndr_print
);
460 if (!ndr
) return NULL
;
461 ndr
->private_data
= talloc_strdup(ndr
, "");
462 if (!ndr
->private_data
) {
465 ndr
->print
= ndr_print_string_helper
;
470 ret
= talloc_steal(mem_ctx
, (char *)ndr
->private_data
);
477 a useful helper function for printing idl unions to a string
479 _PUBLIC_
char *ndr_print_union_string(TALLOC_CTX
*mem_ctx
, ndr_print_fn_t fn
, const char *name
, uint32_t level
, void *ptr
)
481 struct ndr_print
*ndr
;
484 ndr
= talloc_zero(mem_ctx
, struct ndr_print
);
485 if (!ndr
) return NULL
;
486 ndr
->private_data
= talloc_strdup(ndr
, "");
487 if (!ndr
->private_data
) {
490 ndr
->print
= ndr_print_string_helper
;
493 ndr_print_set_switch_value(ndr
, ptr
, level
);
495 ret
= talloc_steal(mem_ctx
, (char *)ndr
->private_data
);
502 a useful helper function for printing idl function calls to a string
504 _PUBLIC_
char *ndr_print_function_string(TALLOC_CTX
*mem_ctx
,
505 ndr_print_function_t fn
, const char *name
,
506 int flags
, void *ptr
)
508 struct ndr_print
*ndr
;
511 ndr
= talloc_zero(mem_ctx
, struct ndr_print
);
512 if (!ndr
) return NULL
;
513 ndr
->private_data
= talloc_strdup(ndr
, "");
514 if (!ndr
->private_data
) {
517 ndr
->print
= ndr_print_string_helper
;
520 fn(ndr
, name
, flags
, ptr
);
521 ret
= talloc_steal(mem_ctx
, (char *)ndr
->private_data
);
527 _PUBLIC_
void ndr_set_flags(uint32_t *pflags
, uint32_t new_flags
)
529 /* the big/little endian flags are inter-dependent */
530 if (new_flags
& LIBNDR_FLAG_LITTLE_ENDIAN
) {
531 (*pflags
) &= ~LIBNDR_FLAG_BIGENDIAN
;
532 (*pflags
) &= ~LIBNDR_FLAG_NDR64
;
534 if (new_flags
& LIBNDR_FLAG_BIGENDIAN
) {
535 (*pflags
) &= ~LIBNDR_FLAG_LITTLE_ENDIAN
;
536 (*pflags
) &= ~LIBNDR_FLAG_NDR64
;
538 if (new_flags
& LIBNDR_ALIGN_FLAGS
) {
539 /* Ensure we only have the passed-in
540 align flag set in the new_flags,
541 remove any old align flag. */
542 (*pflags
) &= ~LIBNDR_ALIGN_FLAGS
;
544 if (new_flags
& LIBNDR_FLAG_NO_RELATIVE_REVERSE
) {
545 (*pflags
) &= ~LIBNDR_FLAG_RELATIVE_REVERSE
;
547 (*pflags
) |= new_flags
;
551 return and possibly log an NDR error
553 _PUBLIC_
enum ndr_err_code
ndr_pull_error(struct ndr_pull
*ndr
,
554 enum ndr_err_code ndr_err
,
555 const char *format
, ...)
561 if (ndr
->flags
& LIBNDR_FLAG_INCOMPLETE_BUFFER
) {
563 case NDR_ERR_BUFSIZE
:
564 return NDR_ERR_INCOMPLETE_BUFFER
;
570 va_start(ap
, format
);
571 ret
= vasprintf(&s
, format
, ap
);
575 return NDR_ERR_ALLOC
;
578 DEBUG(1,("ndr_pull_error(%u): %s\n", ndr_err
, s
));
586 return and possibly log an NDR error
588 _PUBLIC_
enum ndr_err_code
ndr_push_error(struct ndr_push
*ndr
,
589 enum ndr_err_code ndr_err
,
590 const char *format
, ...)
596 va_start(ap
, format
);
597 ret
= vasprintf(&s
, format
, ap
);
601 return NDR_ERR_ALLOC
;
604 DEBUG(1,("ndr_push_error(%u): %s\n", ndr_err
, s
));
612 handle subcontext buffers, which in midl land are user-marshalled, but
613 we use magic in pidl to make them easier to cope with
615 _PUBLIC_
enum ndr_err_code
ndr_pull_subcontext_start(struct ndr_pull
*ndr
,
616 struct ndr_pull
**_subndr
,
620 struct ndr_pull
*subndr
;
621 uint32_t r_content_size
;
622 bool force_le
= false;
623 bool force_be
= false;
625 switch (header_size
) {
627 uint32_t content_size
= ndr
->data_size
- ndr
->offset
;
629 content_size
= size_is
;
631 r_content_size
= content_size
;
636 uint16_t content_size
;
637 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, &content_size
));
638 if (size_is
>= 0 && size_is
!= content_size
) {
639 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
640 (int)size_is
, (int)content_size
);
642 r_content_size
= content_size
;
647 uint32_t content_size
;
648 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &content_size
));
649 if (size_is
>= 0 && size_is
!= content_size
) {
650 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
651 (int)size_is
, (int)content_size
);
653 r_content_size
= content_size
;
658 * Common Type Header for the Serialization Stream
659 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
665 uint32_t content_size
;
669 NDR_CHECK(ndr_pull_uint8(ndr
, NDR_SCALARS
, &version
));
672 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
673 "Bad subcontext (PULL) Common Type Header version %d != 1",
681 NDR_CHECK(ndr_pull_uint8(ndr
, NDR_SCALARS
, &drep
));
684 } else if (drep
== 0x00) {
687 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
688 "Bad subcontext (PULL) Common Type Header invalid drep 0x%02X",
692 /* length of the "Private Header for Constructed Type" */
693 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, &hdrlen
));
695 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
696 "Bad subcontext (PULL) Common Type Header length %d != 8",
700 /* filler should be ignored */
701 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &filler
));
704 * Private Header for Constructed Type
706 /* length - will be updated latter */
707 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &content_size
));
708 if (size_is
>= 0 && size_is
!= content_size
) {
709 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
710 (int)size_is
, (int)content_size
);
712 /* the content size must be a multiple of 8 */
713 if ((content_size
% 8) != 0) {
714 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
715 "Bad subcontext (PULL) size_is(%d) not padded to 8 content_size %d",
716 (int)size_is
, (int)content_size
);
718 r_content_size
= content_size
;
721 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &reserved
));
726 * a shallow copy like subcontext
727 * useful for DCERPC pipe chunks.
729 subndr
= talloc_zero(ndr
, struct ndr_pull
);
730 NDR_ERR_HAVE_NO_MEMORY(subndr
);
732 subndr
->flags
= ndr
->flags
;
733 subndr
->current_mem_ctx
= ndr
->current_mem_ctx
;
734 subndr
->data
= ndr
->data
;
735 subndr
->offset
= ndr
->offset
;
736 subndr
->data_size
= ndr
->data_size
;
739 return NDR_ERR_SUCCESS
;
742 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) header_size %d",
746 NDR_PULL_NEED_BYTES(ndr
, r_content_size
);
748 subndr
= talloc_zero(ndr
, struct ndr_pull
);
749 NDR_ERR_HAVE_NO_MEMORY(subndr
);
750 subndr
->flags
= ndr
->flags
& ~LIBNDR_FLAG_NDR64
;
751 subndr
->current_mem_ctx
= ndr
->current_mem_ctx
;
753 subndr
->data
= ndr
->data
+ ndr
->offset
;
755 subndr
->data_size
= r_content_size
;
758 ndr_set_flags(&ndr
->flags
, LIBNDR_FLAG_LITTLE_ENDIAN
);
759 } else if (force_be
) {
760 ndr_set_flags(&ndr
->flags
, LIBNDR_FLAG_BIGENDIAN
);
764 return NDR_ERR_SUCCESS
;
767 _PUBLIC_
enum ndr_err_code
ndr_pull_subcontext_end(struct ndr_pull
*ndr
,
768 struct ndr_pull
*subndr
,
773 uint32_t highest_ofs
;
775 if (header_size
== 0xFFFFFFFF) {
776 advance
= subndr
->offset
- ndr
->offset
;
777 } else if (size_is
>= 0) {
779 } else if (header_size
> 0) {
780 advance
= subndr
->data_size
;
782 advance
= subndr
->offset
;
785 if (subndr
->offset
> ndr
->relative_highest_offset
) {
786 highest_ofs
= subndr
->offset
;
788 highest_ofs
= subndr
->relative_highest_offset
;
790 if (!(subndr
->flags
& LIBNDR_FLAG_SUBCONTEXT_NO_UNREAD_BYTES
)) {
792 * avoid an error unless SUBCONTEXT_NO_UNREAD_BYTES is specified
794 highest_ofs
= advance
;
796 if (highest_ofs
< advance
) {
797 return ndr_pull_error(subndr
, NDR_ERR_UNREAD_BYTES
,
798 "not all bytes consumed ofs[%u] advance[%u]",
799 highest_ofs
, advance
);
802 NDR_CHECK(ndr_pull_advance(ndr
, advance
));
803 return NDR_ERR_SUCCESS
;
806 _PUBLIC_
enum ndr_err_code
ndr_push_subcontext_start(struct ndr_push
*ndr
,
807 struct ndr_push
**_subndr
,
811 struct ndr_push
*subndr
;
813 subndr
= ndr_push_init_ctx(ndr
);
814 NDR_ERR_HAVE_NO_MEMORY(subndr
);
815 subndr
->flags
= ndr
->flags
& ~LIBNDR_FLAG_NDR64
;
818 NDR_CHECK(ndr_push_zero(subndr
, size_is
));
820 subndr
->relative_end_offset
= size_is
;
824 return NDR_ERR_SUCCESS
;
828 push a subcontext header
830 _PUBLIC_
enum ndr_err_code
ndr_push_subcontext_end(struct ndr_push
*ndr
,
831 struct ndr_push
*subndr
,
838 padding_len
= size_is
- subndr
->offset
;
839 if (padding_len
< 0) {
840 return ndr_push_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
841 (int)subndr
->offset
, (int)size_is
);
843 subndr
->offset
= size_is
;
846 switch (header_size
) {
851 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, subndr
->offset
));
855 NDR_CHECK(ndr_push_uint3264(ndr
, NDR_SCALARS
, subndr
->offset
));
860 * Common Type Header for the Serialization Stream
861 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
863 padding_len
= NDR_ROUND(subndr
->offset
, 8) - subndr
->offset
;
864 if (padding_len
> 0) {
865 NDR_CHECK(ndr_push_zero(subndr
, padding_len
));
869 NDR_CHECK(ndr_push_uint8(ndr
, NDR_SCALARS
, 1));
875 NDR_CHECK(ndr_push_uint8(ndr
, NDR_SCALARS
, NDR_BE(ndr
)?0x00:0x10));
877 /* length of the "Private Header for Constructed Type" */
878 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, 8));
881 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0xCCCCCCCC));
884 * Private Header for Constructed Type
886 /* length - will be updated latter */
887 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, subndr
->offset
));
890 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0));
894 return ndr_push_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext header size %d",
898 NDR_CHECK(ndr_push_bytes(ndr
, subndr
->data
, subndr
->offset
));
899 return NDR_ERR_SUCCESS
;
903 store a token in the ndr context, for later retrieval
905 _PUBLIC_
enum ndr_err_code
ndr_token_store(TALLOC_CTX
*mem_ctx
,
906 struct ndr_token_list
**list
,
910 struct ndr_token_list
*tok
;
911 tok
= talloc(mem_ctx
, struct ndr_token_list
);
912 NDR_ERR_HAVE_NO_MEMORY(tok
);
915 DLIST_ADD((*list
), tok
);
916 return NDR_ERR_SUCCESS
;
920 retrieve a token from a ndr context, using cmp_fn to match the tokens
922 _PUBLIC_
enum ndr_err_code
ndr_token_retrieve_cmp_fn(struct ndr_token_list
**list
, const void *key
, uint32_t *v
,
923 comparison_fn_t _cmp_fn
, bool _remove_tok
)
925 struct ndr_token_list
*tok
;
926 for (tok
=*list
;tok
;tok
=tok
->next
) {
927 if (_cmp_fn
&& _cmp_fn(tok
->key
,key
)==0) goto found
;
928 else if (!_cmp_fn
&& tok
->key
== key
) goto found
;
930 return NDR_ERR_TOKEN
;
934 DLIST_REMOVE((*list
), tok
);
937 return NDR_ERR_SUCCESS
;
941 retrieve a token from a ndr context
943 _PUBLIC_
enum ndr_err_code
ndr_token_retrieve(struct ndr_token_list
**list
, const void *key
, uint32_t *v
)
945 return ndr_token_retrieve_cmp_fn(list
, key
, v
, NULL
, true);
949 peek at but don't removed a token from a ndr context
951 _PUBLIC_
uint32_t ndr_token_peek(struct ndr_token_list
**list
, const void *key
)
953 enum ndr_err_code status
;
956 status
= ndr_token_retrieve_cmp_fn(list
, key
, &v
, NULL
, false);
957 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
965 pull an array size field and add it to the array_size_list token list
967 _PUBLIC_
enum ndr_err_code
ndr_pull_array_size(struct ndr_pull
*ndr
, const void *p
)
970 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &size
));
971 return ndr_token_store(ndr
, &ndr
->array_size_list
, p
, size
);
975 get the stored array size field
977 _PUBLIC_
uint32_t ndr_get_array_size(struct ndr_pull
*ndr
, const void *p
)
979 return ndr_token_peek(&ndr
->array_size_list
, p
);
983 check the stored array size field
985 _PUBLIC_
enum ndr_err_code
ndr_check_array_size(struct ndr_pull
*ndr
, void *p
, uint32_t size
)
988 stored
= ndr_token_peek(&ndr
->array_size_list
, p
);
989 if (stored
!= size
) {
990 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
991 "Bad array size - got %u expected %u\n",
994 return NDR_ERR_SUCCESS
;
998 pull an array length field and add it to the array_length_list token list
1000 _PUBLIC_
enum ndr_err_code
ndr_pull_array_length(struct ndr_pull
*ndr
, const void *p
)
1002 uint32_t length
, offset
;
1003 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &offset
));
1005 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
1006 "non-zero array offset %u\n", offset
);
1008 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &length
));
1009 return ndr_token_store(ndr
, &ndr
->array_length_list
, p
, length
);
1013 get the stored array length field
1015 _PUBLIC_
uint32_t ndr_get_array_length(struct ndr_pull
*ndr
, const void *p
)
1017 return ndr_token_peek(&ndr
->array_length_list
, p
);
1021 check the stored array length field
1023 _PUBLIC_
enum ndr_err_code
ndr_check_array_length(struct ndr_pull
*ndr
, void *p
, uint32_t length
)
1026 stored
= ndr_token_peek(&ndr
->array_length_list
, p
);
1027 if (stored
!= length
) {
1028 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
1029 "Bad array length - got %u expected %u\n",
1032 return NDR_ERR_SUCCESS
;
1035 _PUBLIC_
enum ndr_err_code
ndr_push_pipe_chunk_trailer(struct ndr_push
*ndr
, int ndr_flags
, uint32_t count
)
1037 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
1038 int64_t tmp
= 0 - (int64_t)count
;
1039 uint64_t ncount
= tmp
;
1041 NDR_CHECK(ndr_push_hyper(ndr
, ndr_flags
, ncount
));
1044 return NDR_ERR_SUCCESS
;
1047 _PUBLIC_
enum ndr_err_code
ndr_check_pipe_chunk_trailer(struct ndr_pull
*ndr
, int ndr_flags
, uint32_t count
)
1049 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
1050 int64_t tmp
= 0 - (int64_t)count
;
1051 uint64_t ncount1
= tmp
;
1054 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, &ncount2
));
1055 if (ncount1
== ncount2
) {
1056 return NDR_ERR_SUCCESS
;
1059 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
1060 "Bad pipe trailer[%lld should be %lld] size was %lu\"",
1061 (unsigned long long)ncount2
,
1062 (unsigned long long)ncount1
,
1063 (unsigned long)count
);
1066 return NDR_ERR_SUCCESS
;
1070 store a switch value
1072 _PUBLIC_
enum ndr_err_code
ndr_push_set_switch_value(struct ndr_push
*ndr
, const void *p
, uint32_t val
)
1074 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
1077 _PUBLIC_
enum ndr_err_code
ndr_pull_set_switch_value(struct ndr_pull
*ndr
, const void *p
, uint32_t val
)
1079 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
1082 _PUBLIC_
enum ndr_err_code
ndr_print_set_switch_value(struct ndr_print
*ndr
, const void *p
, uint32_t val
)
1084 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
1088 retrieve a switch value
1090 _PUBLIC_
uint32_t ndr_push_get_switch_value(struct ndr_push
*ndr
, const void *p
)
1092 return ndr_token_peek(&ndr
->switch_list
, p
);
1095 _PUBLIC_
uint32_t ndr_pull_get_switch_value(struct ndr_pull
*ndr
, const void *p
)
1097 return ndr_token_peek(&ndr
->switch_list
, p
);
1100 _PUBLIC_
uint32_t ndr_print_get_switch_value(struct ndr_print
*ndr
, const void *p
)
1102 return ndr_token_peek(&ndr
->switch_list
, p
);
1106 pull a struct from a blob using NDR
1108 _PUBLIC_
enum ndr_err_code
ndr_pull_struct_blob(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
, void *p
,
1109 ndr_pull_flags_fn_t fn
)
1111 struct ndr_pull
*ndr
;
1112 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
1113 NDR_ERR_HAVE_NO_MEMORY(ndr
);
1114 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
1116 return NDR_ERR_SUCCESS
;
1120 pull a struct from a blob using NDR - failing if all bytes are not consumed
1122 _PUBLIC_
enum ndr_err_code
ndr_pull_struct_blob_all(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
1123 void *p
, ndr_pull_flags_fn_t fn
)
1125 struct ndr_pull
*ndr
;
1126 uint32_t highest_ofs
;
1127 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
1128 NDR_ERR_HAVE_NO_MEMORY(ndr
);
1129 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
1130 if (ndr
->offset
> ndr
->relative_highest_offset
) {
1131 highest_ofs
= ndr
->offset
;
1133 highest_ofs
= ndr
->relative_highest_offset
;
1135 if (highest_ofs
< ndr
->data_size
) {
1136 enum ndr_err_code ret
;
1137 ret
= ndr_pull_error(ndr
, NDR_ERR_UNREAD_BYTES
,
1138 "not all bytes consumed ofs[%u] size[%u]",
1139 highest_ofs
, ndr
->data_size
);
1144 return NDR_ERR_SUCCESS
;
1148 pull a union from a blob using NDR, given the union discriminator
1150 _PUBLIC_
enum ndr_err_code
ndr_pull_union_blob(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
1152 uint32_t level
, ndr_pull_flags_fn_t fn
)
1154 struct ndr_pull
*ndr
;
1155 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
1156 NDR_ERR_HAVE_NO_MEMORY(ndr
);
1157 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr
, p
, level
));
1158 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
1160 return NDR_ERR_SUCCESS
;
1164 pull a union from a blob using NDR, given the union discriminator,
1165 failing if all bytes are not consumed
1167 _PUBLIC_
enum ndr_err_code
ndr_pull_union_blob_all(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
1169 uint32_t level
, ndr_pull_flags_fn_t fn
)
1171 struct ndr_pull
*ndr
;
1172 uint32_t highest_ofs
;
1173 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
1174 NDR_ERR_HAVE_NO_MEMORY(ndr
);
1175 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr
, p
, level
));
1176 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
1177 if (ndr
->offset
> ndr
->relative_highest_offset
) {
1178 highest_ofs
= ndr
->offset
;
1180 highest_ofs
= ndr
->relative_highest_offset
;
1182 if (highest_ofs
< ndr
->data_size
) {
1183 enum ndr_err_code ret
;
1184 ret
= ndr_pull_error(ndr
, NDR_ERR_UNREAD_BYTES
,
1185 "not all bytes consumed ofs[%u] size[%u]",
1186 highest_ofs
, ndr
->data_size
);
1191 return NDR_ERR_SUCCESS
;
1195 push a struct to a blob using NDR
1197 _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
)
1199 struct ndr_push
*ndr
;
1200 ndr
= ndr_push_init_ctx(mem_ctx
);
1201 NDR_ERR_HAVE_NO_MEMORY(ndr
);
1203 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
1205 *blob
= ndr_push_blob(ndr
);
1206 talloc_steal(mem_ctx
, blob
->data
);
1209 return NDR_ERR_SUCCESS
;
1213 push a union to a blob using NDR
1215 _PUBLIC_
enum ndr_err_code
ndr_push_union_blob(DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
, void *p
,
1216 uint32_t level
, ndr_push_flags_fn_t fn
)
1218 struct ndr_push
*ndr
;
1219 ndr
= ndr_push_init_ctx(mem_ctx
);
1220 NDR_ERR_HAVE_NO_MEMORY(ndr
);
1222 NDR_CHECK(ndr_push_set_switch_value(ndr
, p
, level
));
1223 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
1225 *blob
= ndr_push_blob(ndr
);
1226 talloc_steal(mem_ctx
, blob
->data
);
1229 return NDR_ERR_SUCCESS
;
1233 generic ndr_size_*() handler for structures
1235 _PUBLIC_
size_t ndr_size_struct(const void *p
, int flags
, ndr_push_flags_fn_t push
)
1237 struct ndr_push
*ndr
;
1238 enum ndr_err_code status
;
1241 /* avoid recursion */
1242 if (flags
& LIBNDR_FLAG_NO_NDR_SIZE
) return 0;
1244 ndr
= ndr_push_init_ctx(NULL
);
1246 ndr
->flags
|= flags
| LIBNDR_FLAG_NO_NDR_SIZE
;
1247 status
= push(ndr
, NDR_SCALARS
|NDR_BUFFERS
, discard_const(p
));
1248 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
1258 generic ndr_size_*() handler for unions
1260 _PUBLIC_
size_t ndr_size_union(const void *p
, int flags
, uint32_t level
, ndr_push_flags_fn_t push
)
1262 struct ndr_push
*ndr
;
1263 enum ndr_err_code status
;
1266 /* avoid recursion */
1267 if (flags
& LIBNDR_FLAG_NO_NDR_SIZE
) return 0;
1269 ndr
= ndr_push_init_ctx(NULL
);
1271 ndr
->flags
|= flags
| LIBNDR_FLAG_NO_NDR_SIZE
;
1273 status
= ndr_push_set_switch_value(ndr
, p
, level
);
1274 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
1278 status
= push(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
);
1279 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
1289 get the current base for relative pointers for the push
1291 _PUBLIC_
uint32_t ndr_push_get_relative_base_offset(struct ndr_push
*ndr
)
1293 return ndr
->relative_base_offset
;
1297 restore the old base for relative pointers for the push
1299 _PUBLIC_
void ndr_push_restore_relative_base_offset(struct ndr_push
*ndr
, uint32_t offset
)
1301 ndr
->relative_base_offset
= offset
;
1305 setup the current base for relative pointers for the push
1306 called in the NDR_SCALAR stage
1308 _PUBLIC_
enum ndr_err_code
ndr_push_setup_relative_base_offset1(struct ndr_push
*ndr
, const void *p
, uint32_t offset
)
1310 ndr
->relative_base_offset
= offset
;
1311 return ndr_token_store(ndr
, &ndr
->relative_base_list
, p
, offset
);
1315 setup the current base for relative pointers for the push
1316 called in the NDR_BUFFERS stage
1318 _PUBLIC_
enum ndr_err_code
ndr_push_setup_relative_base_offset2(struct ndr_push
*ndr
, const void *p
)
1320 return ndr_token_retrieve(&ndr
->relative_base_list
, p
, &ndr
->relative_base_offset
);
1324 push a relative object - stage1
1325 this is called during SCALARS processing
1327 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr1(struct ndr_push
*ndr
, const void *p
)
1330 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0));
1331 return NDR_ERR_SUCCESS
;
1333 NDR_CHECK(ndr_push_align(ndr
, 4));
1334 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_list
, p
, ndr
->offset
));
1335 return ndr_push_uint32(ndr
, NDR_SCALARS
, 0xFFFFFFFF);
1339 push a short relative object - stage1
1340 this is called during SCALARS processing
1342 _PUBLIC_
enum ndr_err_code
ndr_push_short_relative_ptr1(struct ndr_push
*ndr
, const void *p
)
1345 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, 0));
1346 return NDR_ERR_SUCCESS
;
1348 NDR_CHECK(ndr_push_align(ndr
, 2));
1349 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_list
, p
, ndr
->offset
));
1350 return ndr_push_uint16(ndr
, NDR_SCALARS
, 0xFFFF);
1353 push a relative object - stage2
1354 this is called during buffers processing
1356 static enum ndr_err_code
ndr_push_relative_ptr2(struct ndr_push
*ndr
, const void *p
)
1358 uint32_t save_offset
;
1359 uint32_t ptr_offset
= 0xFFFFFFFF;
1361 return NDR_ERR_SUCCESS
;
1363 save_offset
= ndr
->offset
;
1364 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &ptr_offset
));
1365 if (ptr_offset
> ndr
->offset
) {
1366 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1367 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1368 ptr_offset
, ndr
->offset
);
1370 ndr
->offset
= ptr_offset
;
1371 if (save_offset
< ndr
->relative_base_offset
) {
1372 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1373 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1374 save_offset
, ndr
->relative_base_offset
);
1376 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, save_offset
- ndr
->relative_base_offset
));
1377 ndr
->offset
= save_offset
;
1378 return NDR_ERR_SUCCESS
;
1381 push a short relative object - stage2
1382 this is called during buffers processing
1384 _PUBLIC_
enum ndr_err_code
ndr_push_short_relative_ptr2(struct ndr_push
*ndr
, const void *p
)
1386 uint32_t save_offset
;
1387 uint32_t ptr_offset
= 0xFFFF;
1389 return NDR_ERR_SUCCESS
;
1391 save_offset
= ndr
->offset
;
1392 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &ptr_offset
));
1393 if (ptr_offset
> ndr
->offset
) {
1394 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1395 "ndr_push_short_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1396 ptr_offset
, ndr
->offset
);
1398 ndr
->offset
= ptr_offset
;
1399 if (save_offset
< ndr
->relative_base_offset
) {
1400 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1401 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1402 save_offset
, ndr
->relative_base_offset
);
1404 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, save_offset
- ndr
->relative_base_offset
));
1405 ndr
->offset
= save_offset
;
1406 return NDR_ERR_SUCCESS
;
1410 push a relative object - stage2 start
1411 this is called during buffers processing
1413 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr2_start(struct ndr_push
*ndr
, const void *p
)
1416 return NDR_ERR_SUCCESS
;
1418 if (!(ndr
->flags
& LIBNDR_FLAG_RELATIVE_REVERSE
)) {
1419 uint32_t relative_offset
;
1423 if (ndr
->offset
< ndr
->relative_base_offset
) {
1424 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1425 "ndr_push_relative_ptr2_start ndr->offset(%u) < ndr->relative_base_offset(%u)",
1426 ndr
->offset
, ndr
->relative_base_offset
);
1429 relative_offset
= ndr
->offset
- ndr
->relative_base_offset
;
1431 if (ndr
->flags
& LIBNDR_FLAG_NOALIGN
) {
1433 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
1435 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
1437 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
1441 pad
= ndr_align_size(relative_offset
, align
);
1443 NDR_CHECK(ndr_push_zero(ndr
, pad
));
1446 return ndr_push_relative_ptr2(ndr
, p
);
1448 if (ndr
->relative_end_offset
== -1) {
1449 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1450 "ndr_push_relative_ptr2_start RELATIVE_REVERSE flag set and relative_end_offset %d",
1451 ndr
->relative_end_offset
);
1453 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_begin_list
, p
, ndr
->offset
));
1454 return NDR_ERR_SUCCESS
;
1458 push a relative object - stage2 end
1459 this is called during buffers processing
1461 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr2_end(struct ndr_push
*ndr
, const void *p
)
1463 uint32_t begin_offset
= 0xFFFFFFFF;
1465 uint32_t correct_offset
= 0;
1470 return NDR_ERR_SUCCESS
;
1473 if (!(ndr
->flags
& LIBNDR_FLAG_RELATIVE_REVERSE
)) {
1474 return NDR_ERR_SUCCESS
;
1477 if (ndr
->flags
& LIBNDR_FLAG_NO_NDR_SIZE
) {
1478 /* better say more than calculation a too small buffer */
1479 NDR_PUSH_ALIGN(ndr
, 8);
1480 return NDR_ERR_SUCCESS
;
1483 if (ndr
->relative_end_offset
< ndr
->offset
) {
1484 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1485 "ndr_push_relative_ptr2_end:"
1486 "relative_end_offset %u < offset %u",
1487 ndr
->relative_end_offset
, ndr
->offset
);
1490 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_begin_list
, p
, &begin_offset
));
1492 /* we have marshalled a buffer, see how long it was */
1493 len
= ndr
->offset
- begin_offset
;
1496 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1497 "ndr_push_relative_ptr2_end:"
1498 "offset %u - begin_offset %u < 0",
1499 ndr
->offset
, begin_offset
);
1502 if (ndr
->relative_end_offset
< len
) {
1503 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1504 "ndr_push_relative_ptr2_end:"
1505 "relative_end_offset %u < len %lld",
1506 ndr
->offset
, (long long)len
);
1509 /* the reversed offset is at the end of the main buffer */
1510 correct_offset
= ndr
->relative_end_offset
- len
;
1512 if (ndr
->flags
& LIBNDR_FLAG_NOALIGN
) {
1514 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
1516 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
1518 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
1522 pad
= ndr_align_size(correct_offset
, align
);
1524 correct_offset
+= pad
;
1525 correct_offset
-= align
;
1528 if (correct_offset
< begin_offset
) {
1529 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1530 "ndr_push_relative_ptr2_end: "
1531 "correct_offset %u < begin_offset %u",
1532 correct_offset
, begin_offset
);
1536 uint32_t clear_size
= correct_offset
- begin_offset
;
1538 clear_size
= MIN(clear_size
, len
);
1540 /* now move the marshalled buffer to the end of the main buffer */
1541 memmove(ndr
->data
+ correct_offset
, ndr
->data
+ begin_offset
, len
);
1544 /* and wipe out old buffer within the main buffer */
1545 memset(ndr
->data
+ begin_offset
, '\0', clear_size
);
1549 /* and set the end offset for the next buffer */
1550 ndr
->relative_end_offset
= correct_offset
;
1552 /* finally write the offset to the main buffer */
1553 ndr
->offset
= correct_offset
;
1554 NDR_CHECK(ndr_push_relative_ptr2(ndr
, p
));
1556 /* restore to where we were in the main buffer */
1557 ndr
->offset
= begin_offset
;
1559 return NDR_ERR_SUCCESS
;
1563 get the current base for relative pointers for the pull
1565 _PUBLIC_
uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull
*ndr
)
1567 return ndr
->relative_base_offset
;
1571 restore the old base for relative pointers for the pull
1573 _PUBLIC_
void ndr_pull_restore_relative_base_offset(struct ndr_pull
*ndr
, uint32_t offset
)
1575 ndr
->relative_base_offset
= offset
;
1579 setup the current base for relative pointers for the pull
1580 called in the NDR_SCALAR stage
1582 _PUBLIC_
enum ndr_err_code
ndr_pull_setup_relative_base_offset1(struct ndr_pull
*ndr
, const void *p
, uint32_t offset
)
1584 ndr
->relative_base_offset
= offset
;
1585 return ndr_token_store(ndr
, &ndr
->relative_base_list
, p
, offset
);
1589 setup the current base for relative pointers for the pull
1590 called in the NDR_BUFFERS stage
1592 _PUBLIC_
enum ndr_err_code
ndr_pull_setup_relative_base_offset2(struct ndr_pull
*ndr
, const void *p
)
1594 return ndr_token_retrieve(&ndr
->relative_base_list
, p
, &ndr
->relative_base_offset
);
1598 pull a relative object - stage1
1599 called during SCALARS processing
1601 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr1(struct ndr_pull
*ndr
, const void *p
, uint32_t rel_offset
)
1603 rel_offset
+= ndr
->relative_base_offset
;
1604 if (rel_offset
> ndr
->data_size
) {
1605 return ndr_pull_error(ndr
, NDR_ERR_BUFSIZE
,
1606 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1607 rel_offset
, ndr
->data_size
);
1609 return ndr_token_store(ndr
, &ndr
->relative_list
, p
, rel_offset
);
1613 pull a relative object - stage2
1614 called during BUFFERS processing
1616 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr2(struct ndr_pull
*ndr
, const void *p
)
1618 uint32_t rel_offset
;
1619 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &rel_offset
));
1620 return ndr_pull_set_offset(ndr
, rel_offset
);
1623 const static struct {
1624 enum ndr_err_code err
;
1626 } ndr_err_code_strings
[] = {
1627 { NDR_ERR_SUCCESS
, "Success" },
1628 { NDR_ERR_ARRAY_SIZE
, "Bad Array Size" },
1629 { NDR_ERR_BAD_SWITCH
, "Bad Switch" },
1630 { NDR_ERR_OFFSET
, "Offset Error" },
1631 { NDR_ERR_RELATIVE
, "Relative Pointer Error" },
1632 { NDR_ERR_CHARCNV
, "Character Conversion Error" },
1633 { NDR_ERR_LENGTH
, "Length Error" },
1634 { NDR_ERR_SUBCONTEXT
, "Subcontext Error" },
1635 { NDR_ERR_COMPRESSION
, "Compression Error" },
1636 { NDR_ERR_STRING
, "String Error" },
1637 { NDR_ERR_VALIDATE
, "Validate Error" },
1638 { NDR_ERR_BUFSIZE
, "Buffer Size Error" },
1639 { NDR_ERR_ALLOC
, "Allocation Error" },
1640 { NDR_ERR_RANGE
, "Range Error" },
1641 { NDR_ERR_TOKEN
, "Token Error" },
1642 { NDR_ERR_IPV4ADDRESS
, "IPv4 Address Error" },
1643 { NDR_ERR_INVALID_POINTER
, "Invalid Pointer" },
1644 { NDR_ERR_UNREAD_BYTES
, "Unread Bytes" },
1645 { NDR_ERR_NDR64
, "NDR64 assertion error" },
1646 { NDR_ERR_INCOMPLETE_BUFFER
, "Incomplete Buffer" },
1650 _PUBLIC_
const char *ndr_map_error2string(enum ndr_err_code ndr_err
)
1653 for (i
= 0; ndr_err_code_strings
[i
].string
!= NULL
; i
++) {
1654 if (ndr_err_code_strings
[i
].err
== ndr_err
)
1655 return ndr_err_code_strings
[i
].string
;
1657 return "Unknown error";