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) (0x%04x) mismatch content_size %d (0x%04x)",
640 (int)size_is
, (int)size_is
,
644 r_content_size
= content_size
;
649 uint32_t content_size
;
650 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &content_size
));
651 if (size_is
>= 0 && size_is
!= content_size
) {
652 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) (0x%08x) mismatch content_size %d (0x%08x)",
653 (int)size_is
, (int)size_is
,
657 r_content_size
= content_size
;
662 * Common Type Header for the Serialization Stream
663 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
669 uint32_t content_size
;
673 NDR_CHECK(ndr_pull_uint8(ndr
, NDR_SCALARS
, &version
));
676 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
677 "Bad subcontext (PULL) Common Type Header version %d != 1",
685 NDR_CHECK(ndr_pull_uint8(ndr
, NDR_SCALARS
, &drep
));
688 } else if (drep
== 0x00) {
691 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
692 "Bad subcontext (PULL) Common Type Header invalid drep 0x%02X",
696 /* length of the "Private Header for Constructed Type" */
697 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, &hdrlen
));
699 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
700 "Bad subcontext (PULL) Common Type Header length %d != 8",
704 /* filler should be ignored */
705 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &filler
));
708 * Private Header for Constructed Type
710 /* length - will be updated latter */
711 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &content_size
));
712 if (size_is
>= 0 && size_is
!= content_size
) {
713 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
714 (int)size_is
, (int)content_size
);
716 /* the content size must be a multiple of 8 */
717 if ((content_size
% 8) != 0) {
718 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
719 "Bad subcontext (PULL) size_is(%d) not padded to 8 content_size %d",
720 (int)size_is
, (int)content_size
);
722 r_content_size
= content_size
;
725 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &reserved
));
730 * a shallow copy like subcontext
731 * useful for DCERPC pipe chunks.
733 subndr
= talloc_zero(ndr
, struct ndr_pull
);
734 NDR_ERR_HAVE_NO_MEMORY(subndr
);
736 subndr
->flags
= ndr
->flags
;
737 subndr
->current_mem_ctx
= ndr
->current_mem_ctx
;
738 subndr
->data
= ndr
->data
;
739 subndr
->offset
= ndr
->offset
;
740 subndr
->data_size
= ndr
->data_size
;
743 return NDR_ERR_SUCCESS
;
746 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) header_size %d",
750 NDR_PULL_NEED_BYTES(ndr
, r_content_size
);
752 subndr
= talloc_zero(ndr
, struct ndr_pull
);
753 NDR_ERR_HAVE_NO_MEMORY(subndr
);
754 subndr
->flags
= ndr
->flags
& ~LIBNDR_FLAG_NDR64
;
755 subndr
->current_mem_ctx
= ndr
->current_mem_ctx
;
757 subndr
->data
= ndr
->data
+ ndr
->offset
;
759 subndr
->data_size
= r_content_size
;
762 ndr_set_flags(&ndr
->flags
, LIBNDR_FLAG_LITTLE_ENDIAN
);
763 } else if (force_be
) {
764 ndr_set_flags(&ndr
->flags
, LIBNDR_FLAG_BIGENDIAN
);
768 return NDR_ERR_SUCCESS
;
771 _PUBLIC_
enum ndr_err_code
ndr_pull_subcontext_end(struct ndr_pull
*ndr
,
772 struct ndr_pull
*subndr
,
777 uint32_t highest_ofs
;
779 if (header_size
== 0xFFFFFFFF) {
780 advance
= subndr
->offset
- ndr
->offset
;
781 } else if (size_is
>= 0) {
783 } else if (header_size
> 0) {
784 advance
= subndr
->data_size
;
786 advance
= subndr
->offset
;
789 if (subndr
->offset
> ndr
->relative_highest_offset
) {
790 highest_ofs
= subndr
->offset
;
792 highest_ofs
= subndr
->relative_highest_offset
;
794 if (!(subndr
->flags
& LIBNDR_FLAG_SUBCONTEXT_NO_UNREAD_BYTES
)) {
796 * avoid an error unless SUBCONTEXT_NO_UNREAD_BYTES is specified
798 highest_ofs
= advance
;
800 if (highest_ofs
< advance
) {
801 return ndr_pull_error(subndr
, NDR_ERR_UNREAD_BYTES
,
802 "not all bytes consumed ofs[%u] advance[%u]",
803 highest_ofs
, advance
);
806 NDR_CHECK(ndr_pull_advance(ndr
, advance
));
807 return NDR_ERR_SUCCESS
;
810 _PUBLIC_
enum ndr_err_code
ndr_push_subcontext_start(struct ndr_push
*ndr
,
811 struct ndr_push
**_subndr
,
815 struct ndr_push
*subndr
;
817 subndr
= ndr_push_init_ctx(ndr
);
818 NDR_ERR_HAVE_NO_MEMORY(subndr
);
819 subndr
->flags
= ndr
->flags
& ~LIBNDR_FLAG_NDR64
;
822 NDR_CHECK(ndr_push_zero(subndr
, size_is
));
824 subndr
->relative_end_offset
= size_is
;
828 return NDR_ERR_SUCCESS
;
832 push a subcontext header
834 _PUBLIC_
enum ndr_err_code
ndr_push_subcontext_end(struct ndr_push
*ndr
,
835 struct ndr_push
*subndr
,
842 padding_len
= size_is
- subndr
->offset
;
843 if (padding_len
< 0) {
844 return ndr_push_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
845 (int)subndr
->offset
, (int)size_is
);
847 subndr
->offset
= size_is
;
850 switch (header_size
) {
855 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, subndr
->offset
));
859 NDR_CHECK(ndr_push_uint3264(ndr
, NDR_SCALARS
, subndr
->offset
));
864 * Common Type Header for the Serialization Stream
865 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
867 padding_len
= NDR_ROUND(subndr
->offset
, 8) - subndr
->offset
;
868 if (padding_len
> 0) {
869 NDR_CHECK(ndr_push_zero(subndr
, padding_len
));
873 NDR_CHECK(ndr_push_uint8(ndr
, NDR_SCALARS
, 1));
879 NDR_CHECK(ndr_push_uint8(ndr
, NDR_SCALARS
, NDR_BE(ndr
)?0x00:0x10));
881 /* length of the "Private Header for Constructed Type" */
882 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, 8));
885 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0xCCCCCCCC));
888 * Private Header for Constructed Type
890 /* length - will be updated latter */
891 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, subndr
->offset
));
894 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0));
898 return ndr_push_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext header size %d",
902 NDR_CHECK(ndr_push_bytes(ndr
, subndr
->data
, subndr
->offset
));
903 return NDR_ERR_SUCCESS
;
907 store a token in the ndr context, for later retrieval
909 _PUBLIC_
enum ndr_err_code
ndr_token_store(TALLOC_CTX
*mem_ctx
,
910 struct ndr_token_list
**list
,
914 struct ndr_token_list
*tok
;
915 tok
= talloc(mem_ctx
, struct ndr_token_list
);
916 NDR_ERR_HAVE_NO_MEMORY(tok
);
919 DLIST_ADD((*list
), tok
);
920 return NDR_ERR_SUCCESS
;
924 retrieve a token from a ndr context, using cmp_fn to match the tokens
926 _PUBLIC_
enum ndr_err_code
ndr_token_retrieve_cmp_fn(struct ndr_token_list
**list
, const void *key
, uint32_t *v
,
927 comparison_fn_t _cmp_fn
, bool _remove_tok
)
929 struct ndr_token_list
*tok
;
930 for (tok
=*list
;tok
;tok
=tok
->next
) {
931 if (_cmp_fn
&& _cmp_fn(tok
->key
,key
)==0) goto found
;
932 else if (!_cmp_fn
&& tok
->key
== key
) goto found
;
934 return NDR_ERR_TOKEN
;
938 DLIST_REMOVE((*list
), tok
);
941 return NDR_ERR_SUCCESS
;
945 retrieve a token from a ndr context
947 _PUBLIC_
enum ndr_err_code
ndr_token_retrieve(struct ndr_token_list
**list
, const void *key
, uint32_t *v
)
949 return ndr_token_retrieve_cmp_fn(list
, key
, v
, NULL
, true);
953 peek at but don't removed a token from a ndr context
955 _PUBLIC_
uint32_t ndr_token_peek(struct ndr_token_list
**list
, const void *key
)
957 enum ndr_err_code status
;
960 status
= ndr_token_retrieve_cmp_fn(list
, key
, &v
, NULL
, false);
961 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
969 pull an array size field and add it to the array_size_list token list
971 _PUBLIC_
enum ndr_err_code
ndr_pull_array_size(struct ndr_pull
*ndr
, const void *p
)
974 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &size
));
975 return ndr_token_store(ndr
, &ndr
->array_size_list
, p
, size
);
979 get the stored array size field
981 _PUBLIC_
uint32_t ndr_get_array_size(struct ndr_pull
*ndr
, const void *p
)
983 return ndr_token_peek(&ndr
->array_size_list
, p
);
987 check the stored array size field
989 _PUBLIC_
enum ndr_err_code
ndr_check_array_size(struct ndr_pull
*ndr
, void *p
, uint32_t size
)
992 stored
= ndr_token_peek(&ndr
->array_size_list
, p
);
993 if (stored
!= size
) {
994 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
995 "Bad array size - got %u expected %u\n",
998 return NDR_ERR_SUCCESS
;
1002 pull an array length field and add it to the array_length_list token list
1004 _PUBLIC_
enum ndr_err_code
ndr_pull_array_length(struct ndr_pull
*ndr
, const void *p
)
1006 uint32_t length
, offset
;
1007 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &offset
));
1009 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
1010 "non-zero array offset %u\n", offset
);
1012 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &length
));
1013 return ndr_token_store(ndr
, &ndr
->array_length_list
, p
, length
);
1017 get the stored array length field
1019 _PUBLIC_
uint32_t ndr_get_array_length(struct ndr_pull
*ndr
, const void *p
)
1021 return ndr_token_peek(&ndr
->array_length_list
, p
);
1025 check the stored array length field
1027 _PUBLIC_
enum ndr_err_code
ndr_check_array_length(struct ndr_pull
*ndr
, void *p
, uint32_t length
)
1030 stored
= ndr_token_peek(&ndr
->array_length_list
, p
);
1031 if (stored
!= length
) {
1032 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
1033 "Bad array length - got %u expected %u\n",
1036 return NDR_ERR_SUCCESS
;
1039 _PUBLIC_
enum ndr_err_code
ndr_push_pipe_chunk_trailer(struct ndr_push
*ndr
, int ndr_flags
, uint32_t count
)
1041 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
1042 int64_t tmp
= 0 - (int64_t)count
;
1043 uint64_t ncount
= tmp
;
1045 NDR_CHECK(ndr_push_hyper(ndr
, ndr_flags
, ncount
));
1048 return NDR_ERR_SUCCESS
;
1051 _PUBLIC_
enum ndr_err_code
ndr_check_pipe_chunk_trailer(struct ndr_pull
*ndr
, int ndr_flags
, uint32_t count
)
1053 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
1054 int64_t tmp
= 0 - (int64_t)count
;
1055 uint64_t ncount1
= tmp
;
1058 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, &ncount2
));
1059 if (ncount1
== ncount2
) {
1060 return NDR_ERR_SUCCESS
;
1063 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
1064 "Bad pipe trailer[%lld should be %lld] size was %lu\"",
1065 (unsigned long long)ncount2
,
1066 (unsigned long long)ncount1
,
1067 (unsigned long)count
);
1070 return NDR_ERR_SUCCESS
;
1074 store a switch value
1076 _PUBLIC_
enum ndr_err_code
ndr_push_set_switch_value(struct ndr_push
*ndr
, const void *p
, uint32_t val
)
1078 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
1081 _PUBLIC_
enum ndr_err_code
ndr_pull_set_switch_value(struct ndr_pull
*ndr
, const void *p
, uint32_t val
)
1083 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
1086 _PUBLIC_
enum ndr_err_code
ndr_print_set_switch_value(struct ndr_print
*ndr
, const void *p
, uint32_t val
)
1088 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
1092 retrieve a switch value
1094 _PUBLIC_
uint32_t ndr_push_get_switch_value(struct ndr_push
*ndr
, const void *p
)
1096 return ndr_token_peek(&ndr
->switch_list
, p
);
1099 _PUBLIC_
uint32_t ndr_pull_get_switch_value(struct ndr_pull
*ndr
, const void *p
)
1101 return ndr_token_peek(&ndr
->switch_list
, p
);
1104 _PUBLIC_
uint32_t ndr_print_get_switch_value(struct ndr_print
*ndr
, const void *p
)
1106 return ndr_token_peek(&ndr
->switch_list
, p
);
1109 /* retrieve a switch value and remove it from the list */
1110 _PUBLIC_
uint32_t ndr_pull_steal_switch_value(struct ndr_pull
*ndr
, const void *p
)
1112 enum ndr_err_code status
;
1115 status
= ndr_token_retrieve(&ndr
->switch_list
, p
, &v
);
1116 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
1124 pull a struct from a blob using NDR
1126 _PUBLIC_
enum ndr_err_code
ndr_pull_struct_blob(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
, void *p
,
1127 ndr_pull_flags_fn_t fn
)
1129 struct ndr_pull
*ndr
;
1130 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
1131 NDR_ERR_HAVE_NO_MEMORY(ndr
);
1132 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
1134 return NDR_ERR_SUCCESS
;
1138 pull a struct from a blob using NDR - failing if all bytes are not consumed
1140 _PUBLIC_
enum ndr_err_code
ndr_pull_struct_blob_all(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
1141 void *p
, ndr_pull_flags_fn_t fn
)
1143 struct ndr_pull
*ndr
;
1144 uint32_t highest_ofs
;
1145 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
1146 NDR_ERR_HAVE_NO_MEMORY(ndr
);
1147 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
1148 if (ndr
->offset
> ndr
->relative_highest_offset
) {
1149 highest_ofs
= ndr
->offset
;
1151 highest_ofs
= ndr
->relative_highest_offset
;
1153 if (highest_ofs
< ndr
->data_size
) {
1154 enum ndr_err_code ret
;
1155 ret
= ndr_pull_error(ndr
, NDR_ERR_UNREAD_BYTES
,
1156 "not all bytes consumed ofs[%u] size[%u]",
1157 highest_ofs
, ndr
->data_size
);
1162 return NDR_ERR_SUCCESS
;
1166 pull a union from a blob using NDR, given the union discriminator
1168 _PUBLIC_
enum ndr_err_code
ndr_pull_union_blob(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
1170 uint32_t level
, ndr_pull_flags_fn_t fn
)
1172 struct ndr_pull
*ndr
;
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
));
1178 return NDR_ERR_SUCCESS
;
1182 pull a union from a blob using NDR, given the union discriminator,
1183 failing if all bytes are not consumed
1185 _PUBLIC_
enum ndr_err_code
ndr_pull_union_blob_all(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
1187 uint32_t level
, ndr_pull_flags_fn_t fn
)
1189 struct ndr_pull
*ndr
;
1190 uint32_t highest_ofs
;
1191 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
1192 NDR_ERR_HAVE_NO_MEMORY(ndr
);
1193 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr
, p
, level
));
1194 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
1195 if (ndr
->offset
> ndr
->relative_highest_offset
) {
1196 highest_ofs
= ndr
->offset
;
1198 highest_ofs
= ndr
->relative_highest_offset
;
1200 if (highest_ofs
< ndr
->data_size
) {
1201 enum ndr_err_code ret
;
1202 ret
= ndr_pull_error(ndr
, NDR_ERR_UNREAD_BYTES
,
1203 "not all bytes consumed ofs[%u] size[%u]",
1204 highest_ofs
, ndr
->data_size
);
1209 return NDR_ERR_SUCCESS
;
1213 push a struct to a blob using NDR
1215 _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
)
1217 struct ndr_push
*ndr
;
1218 ndr
= ndr_push_init_ctx(mem_ctx
);
1219 NDR_ERR_HAVE_NO_MEMORY(ndr
);
1221 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
1223 *blob
= ndr_push_blob(ndr
);
1224 talloc_steal(mem_ctx
, blob
->data
);
1227 return NDR_ERR_SUCCESS
;
1231 push a union to a blob using NDR
1233 _PUBLIC_
enum ndr_err_code
ndr_push_union_blob(DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
, void *p
,
1234 uint32_t level
, ndr_push_flags_fn_t fn
)
1236 struct ndr_push
*ndr
;
1237 ndr
= ndr_push_init_ctx(mem_ctx
);
1238 NDR_ERR_HAVE_NO_MEMORY(ndr
);
1240 NDR_CHECK(ndr_push_set_switch_value(ndr
, p
, level
));
1241 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
1243 *blob
= ndr_push_blob(ndr
);
1244 talloc_steal(mem_ctx
, blob
->data
);
1247 return NDR_ERR_SUCCESS
;
1251 generic ndr_size_*() handler for structures
1253 _PUBLIC_
size_t ndr_size_struct(const void *p
, int flags
, ndr_push_flags_fn_t push
)
1255 struct ndr_push
*ndr
;
1256 enum ndr_err_code status
;
1259 /* avoid recursion */
1260 if (flags
& LIBNDR_FLAG_NO_NDR_SIZE
) return 0;
1262 ndr
= ndr_push_init_ctx(NULL
);
1264 ndr
->flags
|= flags
| LIBNDR_FLAG_NO_NDR_SIZE
;
1265 status
= push(ndr
, NDR_SCALARS
|NDR_BUFFERS
, discard_const(p
));
1266 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
1276 generic ndr_size_*() handler for unions
1278 _PUBLIC_
size_t ndr_size_union(const void *p
, int flags
, uint32_t level
, ndr_push_flags_fn_t push
)
1280 struct ndr_push
*ndr
;
1281 enum ndr_err_code status
;
1284 /* avoid recursion */
1285 if (flags
& LIBNDR_FLAG_NO_NDR_SIZE
) return 0;
1287 ndr
= ndr_push_init_ctx(NULL
);
1289 ndr
->flags
|= flags
| LIBNDR_FLAG_NO_NDR_SIZE
;
1291 status
= ndr_push_set_switch_value(ndr
, p
, level
);
1292 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
1296 status
= push(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
);
1297 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
1307 get the current base for relative pointers for the push
1309 _PUBLIC_
uint32_t ndr_push_get_relative_base_offset(struct ndr_push
*ndr
)
1311 return ndr
->relative_base_offset
;
1315 restore the old base for relative pointers for the push
1317 _PUBLIC_
void ndr_push_restore_relative_base_offset(struct ndr_push
*ndr
, uint32_t offset
)
1319 ndr
->relative_base_offset
= offset
;
1323 setup the current base for relative pointers for the push
1324 called in the NDR_SCALAR stage
1326 _PUBLIC_
enum ndr_err_code
ndr_push_setup_relative_base_offset1(struct ndr_push
*ndr
, const void *p
, uint32_t offset
)
1328 ndr
->relative_base_offset
= offset
;
1329 return ndr_token_store(ndr
, &ndr
->relative_base_list
, p
, offset
);
1333 setup the current base for relative pointers for the push
1334 called in the NDR_BUFFERS stage
1336 _PUBLIC_
enum ndr_err_code
ndr_push_setup_relative_base_offset2(struct ndr_push
*ndr
, const void *p
)
1338 return ndr_token_retrieve(&ndr
->relative_base_list
, p
, &ndr
->relative_base_offset
);
1342 push a relative object - stage1
1343 this is called during SCALARS processing
1345 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr1(struct ndr_push
*ndr
, const void *p
)
1348 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0));
1349 return NDR_ERR_SUCCESS
;
1351 NDR_CHECK(ndr_push_align(ndr
, 4));
1352 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_list
, p
, ndr
->offset
));
1353 return ndr_push_uint32(ndr
, NDR_SCALARS
, 0xFFFFFFFF);
1357 push a short relative object - stage1
1358 this is called during SCALARS processing
1360 _PUBLIC_
enum ndr_err_code
ndr_push_short_relative_ptr1(struct ndr_push
*ndr
, const void *p
)
1363 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, 0));
1364 return NDR_ERR_SUCCESS
;
1366 NDR_CHECK(ndr_push_align(ndr
, 2));
1367 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_list
, p
, ndr
->offset
));
1368 return ndr_push_uint16(ndr
, NDR_SCALARS
, 0xFFFF);
1371 push a relative object - stage2
1372 this is called during buffers processing
1374 static enum ndr_err_code
ndr_push_relative_ptr2(struct ndr_push
*ndr
, const void *p
)
1376 uint32_t save_offset
;
1377 uint32_t ptr_offset
= 0xFFFFFFFF;
1379 return NDR_ERR_SUCCESS
;
1381 save_offset
= ndr
->offset
;
1382 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &ptr_offset
));
1383 if (ptr_offset
> ndr
->offset
) {
1384 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1385 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1386 ptr_offset
, ndr
->offset
);
1388 ndr
->offset
= ptr_offset
;
1389 if (save_offset
< ndr
->relative_base_offset
) {
1390 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1391 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1392 save_offset
, ndr
->relative_base_offset
);
1394 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, save_offset
- ndr
->relative_base_offset
));
1395 ndr
->offset
= save_offset
;
1396 return NDR_ERR_SUCCESS
;
1399 push a short relative object - stage2
1400 this is called during buffers processing
1402 _PUBLIC_
enum ndr_err_code
ndr_push_short_relative_ptr2(struct ndr_push
*ndr
, const void *p
)
1404 uint32_t save_offset
;
1405 uint32_t ptr_offset
= 0xFFFF;
1407 return NDR_ERR_SUCCESS
;
1409 save_offset
= ndr
->offset
;
1410 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &ptr_offset
));
1411 if (ptr_offset
> ndr
->offset
) {
1412 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1413 "ndr_push_short_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1414 ptr_offset
, ndr
->offset
);
1416 ndr
->offset
= ptr_offset
;
1417 if (save_offset
< ndr
->relative_base_offset
) {
1418 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1419 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1420 save_offset
, ndr
->relative_base_offset
);
1422 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, save_offset
- ndr
->relative_base_offset
));
1423 ndr
->offset
= save_offset
;
1424 return NDR_ERR_SUCCESS
;
1428 push a relative object - stage2 start
1429 this is called during buffers processing
1431 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr2_start(struct ndr_push
*ndr
, const void *p
)
1434 return NDR_ERR_SUCCESS
;
1436 if (!(ndr
->flags
& LIBNDR_FLAG_RELATIVE_REVERSE
)) {
1437 uint32_t relative_offset
;
1441 if (ndr
->offset
< ndr
->relative_base_offset
) {
1442 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1443 "ndr_push_relative_ptr2_start ndr->offset(%u) < ndr->relative_base_offset(%u)",
1444 ndr
->offset
, ndr
->relative_base_offset
);
1447 relative_offset
= ndr
->offset
- ndr
->relative_base_offset
;
1449 if (ndr
->flags
& LIBNDR_FLAG_NOALIGN
) {
1451 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
1453 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
1455 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
1459 pad
= ndr_align_size(relative_offset
, align
);
1461 NDR_CHECK(ndr_push_zero(ndr
, pad
));
1464 return ndr_push_relative_ptr2(ndr
, p
);
1466 if (ndr
->relative_end_offset
== -1) {
1467 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1468 "ndr_push_relative_ptr2_start RELATIVE_REVERSE flag set and relative_end_offset %d",
1469 ndr
->relative_end_offset
);
1471 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_begin_list
, p
, ndr
->offset
));
1472 return NDR_ERR_SUCCESS
;
1476 push a relative object - stage2 end
1477 this is called during buffers processing
1479 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr2_end(struct ndr_push
*ndr
, const void *p
)
1481 uint32_t begin_offset
= 0xFFFFFFFF;
1483 uint32_t correct_offset
= 0;
1488 return NDR_ERR_SUCCESS
;
1491 if (!(ndr
->flags
& LIBNDR_FLAG_RELATIVE_REVERSE
)) {
1492 return NDR_ERR_SUCCESS
;
1495 if (ndr
->flags
& LIBNDR_FLAG_NO_NDR_SIZE
) {
1496 /* better say more than calculation a too small buffer */
1497 NDR_PUSH_ALIGN(ndr
, 8);
1498 return NDR_ERR_SUCCESS
;
1501 if (ndr
->relative_end_offset
< ndr
->offset
) {
1502 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1503 "ndr_push_relative_ptr2_end:"
1504 "relative_end_offset %u < offset %u",
1505 ndr
->relative_end_offset
, ndr
->offset
);
1508 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_begin_list
, p
, &begin_offset
));
1510 /* we have marshalled a buffer, see how long it was */
1511 len
= ndr
->offset
- begin_offset
;
1514 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1515 "ndr_push_relative_ptr2_end:"
1516 "offset %u - begin_offset %u < 0",
1517 ndr
->offset
, begin_offset
);
1520 if (ndr
->relative_end_offset
< len
) {
1521 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1522 "ndr_push_relative_ptr2_end:"
1523 "relative_end_offset %u < len %lld",
1524 ndr
->offset
, (long long)len
);
1527 /* the reversed offset is at the end of the main buffer */
1528 correct_offset
= ndr
->relative_end_offset
- len
;
1530 if (ndr
->flags
& LIBNDR_FLAG_NOALIGN
) {
1532 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
1534 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
1536 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
1540 pad
= ndr_align_size(correct_offset
, align
);
1542 correct_offset
+= pad
;
1543 correct_offset
-= align
;
1546 if (correct_offset
< begin_offset
) {
1547 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1548 "ndr_push_relative_ptr2_end: "
1549 "correct_offset %u < begin_offset %u",
1550 correct_offset
, begin_offset
);
1554 uint32_t clear_size
= correct_offset
- begin_offset
;
1556 clear_size
= MIN(clear_size
, len
);
1558 /* now move the marshalled buffer to the end of the main buffer */
1559 memmove(ndr
->data
+ correct_offset
, ndr
->data
+ begin_offset
, len
);
1562 /* and wipe out old buffer within the main buffer */
1563 memset(ndr
->data
+ begin_offset
, '\0', clear_size
);
1567 /* and set the end offset for the next buffer */
1568 ndr
->relative_end_offset
= correct_offset
;
1570 /* finally write the offset to the main buffer */
1571 ndr
->offset
= correct_offset
;
1572 NDR_CHECK(ndr_push_relative_ptr2(ndr
, p
));
1574 /* restore to where we were in the main buffer */
1575 ndr
->offset
= begin_offset
;
1577 return NDR_ERR_SUCCESS
;
1581 get the current base for relative pointers for the pull
1583 _PUBLIC_
uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull
*ndr
)
1585 return ndr
->relative_base_offset
;
1589 restore the old base for relative pointers for the pull
1591 _PUBLIC_
void ndr_pull_restore_relative_base_offset(struct ndr_pull
*ndr
, uint32_t offset
)
1593 ndr
->relative_base_offset
= offset
;
1597 setup the current base for relative pointers for the pull
1598 called in the NDR_SCALAR stage
1600 _PUBLIC_
enum ndr_err_code
ndr_pull_setup_relative_base_offset1(struct ndr_pull
*ndr
, const void *p
, uint32_t offset
)
1602 ndr
->relative_base_offset
= offset
;
1603 return ndr_token_store(ndr
, &ndr
->relative_base_list
, p
, offset
);
1607 setup the current base for relative pointers for the pull
1608 called in the NDR_BUFFERS stage
1610 _PUBLIC_
enum ndr_err_code
ndr_pull_setup_relative_base_offset2(struct ndr_pull
*ndr
, const void *p
)
1612 return ndr_token_retrieve(&ndr
->relative_base_list
, p
, &ndr
->relative_base_offset
);
1616 pull a relative object - stage1
1617 called during SCALARS processing
1619 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr1(struct ndr_pull
*ndr
, const void *p
, uint32_t rel_offset
)
1621 rel_offset
+= ndr
->relative_base_offset
;
1622 if (rel_offset
> ndr
->data_size
) {
1623 return ndr_pull_error(ndr
, NDR_ERR_BUFSIZE
,
1624 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1625 rel_offset
, ndr
->data_size
);
1627 return ndr_token_store(ndr
, &ndr
->relative_list
, p
, rel_offset
);
1631 pull a relative object - stage2
1632 called during BUFFERS processing
1634 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr2(struct ndr_pull
*ndr
, const void *p
)
1636 uint32_t rel_offset
;
1637 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &rel_offset
));
1638 return ndr_pull_set_offset(ndr
, rel_offset
);
1641 const static struct {
1642 enum ndr_err_code err
;
1644 } ndr_err_code_strings
[] = {
1645 { NDR_ERR_SUCCESS
, "Success" },
1646 { NDR_ERR_ARRAY_SIZE
, "Bad Array Size" },
1647 { NDR_ERR_BAD_SWITCH
, "Bad Switch" },
1648 { NDR_ERR_OFFSET
, "Offset Error" },
1649 { NDR_ERR_RELATIVE
, "Relative Pointer Error" },
1650 { NDR_ERR_CHARCNV
, "Character Conversion Error" },
1651 { NDR_ERR_LENGTH
, "Length Error" },
1652 { NDR_ERR_SUBCONTEXT
, "Subcontext Error" },
1653 { NDR_ERR_COMPRESSION
, "Compression Error" },
1654 { NDR_ERR_STRING
, "String Error" },
1655 { NDR_ERR_VALIDATE
, "Validate Error" },
1656 { NDR_ERR_BUFSIZE
, "Buffer Size Error" },
1657 { NDR_ERR_ALLOC
, "Allocation Error" },
1658 { NDR_ERR_RANGE
, "Range Error" },
1659 { NDR_ERR_TOKEN
, "Token Error" },
1660 { NDR_ERR_IPV4ADDRESS
, "IPv4 Address Error" },
1661 { NDR_ERR_INVALID_POINTER
, "Invalid Pointer" },
1662 { NDR_ERR_UNREAD_BYTES
, "Unread Bytes" },
1663 { NDR_ERR_NDR64
, "NDR64 assertion error" },
1664 { NDR_ERR_INCOMPLETE_BUFFER
, "Incomplete Buffer" },
1668 _PUBLIC_
const char *ndr_map_error2string(enum ndr_err_code ndr_err
)
1671 for (i
= 0; ndr_err_code_strings
[i
].string
!= NULL
; i
++) {
1672 if (ndr_err_code_strings
[i
].err
== ndr_err
)
1673 return ndr_err_code_strings
[i
].string
;
1675 return "Unknown error";