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
;
79 advance by 'size' bytes
81 _PUBLIC_
enum ndr_err_code
ndr_pull_advance(struct ndr_pull
*ndr
, uint32_t size
)
84 if (ndr
->offset
> ndr
->data_size
) {
85 return ndr_pull_error(ndr
, NDR_ERR_BUFSIZE
,
86 "ndr_pull_advance by %u failed",
89 return NDR_ERR_SUCCESS
;
93 set the parse offset to 'ofs'
95 static enum ndr_err_code
ndr_pull_set_offset(struct ndr_pull
*ndr
, uint32_t ofs
)
98 if (ndr
->offset
> ndr
->data_size
) {
99 return ndr_pull_error(ndr
, NDR_ERR_BUFSIZE
,
100 "ndr_pull_set_offset %u failed",
103 return NDR_ERR_SUCCESS
;
106 /* create a ndr_push structure, ready for some marshalling */
107 _PUBLIC_
struct ndr_push
*ndr_push_init_ctx(TALLOC_CTX
*mem_ctx
)
109 struct ndr_push
*ndr
;
111 ndr
= talloc_zero(mem_ctx
, struct ndr_push
);
117 ndr
->alloc_size
= NDR_BASE_MARSHALL_SIZE
;
118 ndr
->data
= talloc_array(ndr
, uint8_t, ndr
->alloc_size
);
126 /* return a DATA_BLOB structure for the current ndr_push marshalled data */
127 _PUBLIC_ DATA_BLOB
ndr_push_blob(struct ndr_push
*ndr
)
130 blob
= data_blob_const(ndr
->data
, ndr
->offset
);
131 if (ndr
->alloc_size
> ndr
->offset
) {
132 ndr
->data
[ndr
->offset
] = 0;
139 expand the available space in the buffer to ndr->offset + extra_size
141 _PUBLIC_
enum ndr_err_code
ndr_push_expand(struct ndr_push
*ndr
, uint32_t extra_size
)
143 uint32_t size
= extra_size
+ ndr
->offset
;
145 if (size
< ndr
->offset
) {
146 /* extra_size overflowed the offset */
147 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
, "Overflow in push_expand to %u",
151 if (ndr
->alloc_size
> size
) {
152 return NDR_ERR_SUCCESS
;
155 ndr
->alloc_size
+= NDR_BASE_MARSHALL_SIZE
;
156 if (size
+1 > ndr
->alloc_size
) {
157 ndr
->alloc_size
= size
+1;
159 ndr
->data
= talloc_realloc(ndr
, ndr
->data
, uint8_t, ndr
->alloc_size
);
161 return ndr_push_error(ndr
, NDR_ERR_ALLOC
, "Failed to push_expand to %u",
165 return NDR_ERR_SUCCESS
;
168 _PUBLIC_
void ndr_print_debug_helper(struct ndr_print
*ndr
, const char *format
, ...)
175 va_start(ap
, format
);
176 ret
= vasprintf(&s
, format
, ap
);
183 if (ndr
->no_newline
) {
184 DEBUGADD(1,("%s", s
));
189 for (i
=0;i
<ndr
->depth
;i
++) {
193 DEBUGADD(1,("%s\n", s
));
197 _PUBLIC_
void ndr_print_printf_helper(struct ndr_print
*ndr
, const char *format
, ...)
202 if (!ndr
->no_newline
) {
203 for (i
=0;i
<ndr
->depth
;i
++) {
208 va_start(ap
, format
);
211 if (!ndr
->no_newline
) {
216 _PUBLIC_
void ndr_print_string_helper(struct ndr_print
*ndr
, const char *format
, ...)
221 if (!ndr
->no_newline
) {
222 for (i
=0;i
<ndr
->depth
;i
++) {
223 ndr
->private_data
= talloc_asprintf_append_buffer(
224 (char *)ndr
->private_data
, " ");
228 va_start(ap
, format
);
229 ndr
->private_data
= talloc_vasprintf_append_buffer((char *)ndr
->private_data
,
232 if (!ndr
->no_newline
) {
233 ndr
->private_data
= talloc_asprintf_append_buffer((char *)ndr
->private_data
,
239 a useful helper function for printing idl structures via DEBUG()
241 _PUBLIC_
void ndr_print_debug(ndr_print_fn_t fn
, const char *name
, void *ptr
)
243 struct ndr_print
*ndr
;
247 ndr
= talloc_zero(NULL
, struct ndr_print
);
249 ndr
->print
= ndr_print_debug_helper
;
257 a useful helper function for printing idl unions via DEBUG()
259 _PUBLIC_
void ndr_print_union_debug(ndr_print_fn_t fn
, const char *name
, uint32_t level
, void *ptr
)
261 struct ndr_print
*ndr
;
265 ndr
= talloc_zero(NULL
, struct ndr_print
);
267 ndr
->print
= ndr_print_debug_helper
;
270 ndr_print_set_switch_value(ndr
, ptr
, level
);
276 a useful helper function for printing idl function calls via DEBUG()
278 _PUBLIC_
void ndr_print_function_debug(ndr_print_function_t fn
, const char *name
, int flags
, void *ptr
)
280 struct ndr_print
*ndr
;
284 ndr
= talloc_zero(NULL
, struct ndr_print
);
286 ndr
->print
= ndr_print_debug_helper
;
290 fn(ndr
, name
, flags
, ptr
);
295 a useful helper function for printing idl structures to a string
297 _PUBLIC_
char *ndr_print_struct_string(TALLOC_CTX
*mem_ctx
, ndr_print_fn_t fn
, const char *name
, void *ptr
)
299 struct ndr_print
*ndr
;
302 ndr
= talloc_zero(mem_ctx
, struct ndr_print
);
303 if (!ndr
) return NULL
;
304 ndr
->private_data
= talloc_strdup(ndr
, "");
305 if (!ndr
->private_data
) {
308 ndr
->print
= ndr_print_string_helper
;
313 ret
= talloc_steal(mem_ctx
, (char *)ndr
->private_data
);
320 a useful helper function for printing idl unions to a string
322 _PUBLIC_
char *ndr_print_union_string(TALLOC_CTX
*mem_ctx
, ndr_print_fn_t fn
, const char *name
, uint32_t level
, void *ptr
)
324 struct ndr_print
*ndr
;
327 ndr
= talloc_zero(mem_ctx
, struct ndr_print
);
328 if (!ndr
) return NULL
;
329 ndr
->private_data
= talloc_strdup(ndr
, "");
330 if (!ndr
->private_data
) {
333 ndr
->print
= ndr_print_string_helper
;
336 ndr_print_set_switch_value(ndr
, ptr
, level
);
338 ret
= talloc_steal(mem_ctx
, (char *)ndr
->private_data
);
345 a useful helper function for printing idl function calls to a string
347 _PUBLIC_
char *ndr_print_function_string(TALLOC_CTX
*mem_ctx
,
348 ndr_print_function_t fn
, const char *name
,
349 int flags
, void *ptr
)
351 struct ndr_print
*ndr
;
354 ndr
= talloc_zero(mem_ctx
, struct ndr_print
);
355 if (!ndr
) return NULL
;
356 ndr
->private_data
= talloc_strdup(ndr
, "");
357 if (!ndr
->private_data
) {
360 ndr
->print
= ndr_print_string_helper
;
363 fn(ndr
, name
, flags
, ptr
);
364 ret
= talloc_steal(mem_ctx
, (char *)ndr
->private_data
);
370 _PUBLIC_
void ndr_set_flags(uint32_t *pflags
, uint32_t new_flags
)
372 /* the big/little endian flags are inter-dependent */
373 if (new_flags
& LIBNDR_FLAG_LITTLE_ENDIAN
) {
374 (*pflags
) &= ~LIBNDR_FLAG_BIGENDIAN
;
375 (*pflags
) &= ~LIBNDR_FLAG_NDR64
;
377 if (new_flags
& LIBNDR_FLAG_BIGENDIAN
) {
378 (*pflags
) &= ~LIBNDR_FLAG_LITTLE_ENDIAN
;
379 (*pflags
) &= ~LIBNDR_FLAG_NDR64
;
381 if (new_flags
& LIBNDR_ALIGN_FLAGS
) {
382 /* Ensure we only have the passed-in
383 align flag set in the new_flags,
384 remove any old align flag. */
385 (*pflags
) &= ~LIBNDR_ALIGN_FLAGS
;
387 if (new_flags
& LIBNDR_FLAG_NO_RELATIVE_REVERSE
) {
388 (*pflags
) &= ~LIBNDR_FLAG_RELATIVE_REVERSE
;
390 (*pflags
) |= new_flags
;
394 return and possibly log an NDR error
396 _PUBLIC_
enum ndr_err_code
ndr_pull_error(struct ndr_pull
*ndr
,
397 enum ndr_err_code ndr_err
,
398 const char *format
, ...)
404 va_start(ap
, format
);
405 ret
= vasprintf(&s
, format
, ap
);
409 return NDR_ERR_ALLOC
;
412 DEBUG(1,("ndr_pull_error(%u): %s\n", ndr_err
, s
));
420 return and possibly log an NDR error
422 _PUBLIC_
enum ndr_err_code
ndr_push_error(struct ndr_push
*ndr
,
423 enum ndr_err_code ndr_err
,
424 const char *format
, ...)
430 va_start(ap
, format
);
431 ret
= vasprintf(&s
, format
, ap
);
435 return NDR_ERR_ALLOC
;
438 DEBUG(1,("ndr_push_error(%u): %s\n", ndr_err
, s
));
446 handle subcontext buffers, which in midl land are user-marshalled, but
447 we use magic in pidl to make them easier to cope with
449 _PUBLIC_
enum ndr_err_code
ndr_pull_subcontext_start(struct ndr_pull
*ndr
,
450 struct ndr_pull
**_subndr
,
454 struct ndr_pull
*subndr
;
455 uint32_t r_content_size
;
456 bool force_le
= false;
457 bool force_be
= false;
459 switch (header_size
) {
461 uint32_t content_size
= ndr
->data_size
- ndr
->offset
;
463 content_size
= size_is
;
465 r_content_size
= content_size
;
470 uint16_t content_size
;
471 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, &content_size
));
472 if (size_is
>= 0 && size_is
!= content_size
) {
473 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
474 (int)size_is
, (int)content_size
);
476 r_content_size
= content_size
;
481 uint32_t content_size
;
482 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &content_size
));
483 if (size_is
>= 0 && size_is
!= content_size
) {
484 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
485 (int)size_is
, (int)content_size
);
487 r_content_size
= content_size
;
492 * Common Type Header for the Serialization Stream
493 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
499 uint32_t content_size
;
503 NDR_CHECK(ndr_pull_uint8(ndr
, NDR_SCALARS
, &version
));
506 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
507 "Bad subcontext (PULL) Common Type Header version %d != 1",
515 NDR_CHECK(ndr_pull_uint8(ndr
, NDR_SCALARS
, &drep
));
518 } else if (drep
== 0x00) {
521 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
522 "Bad subcontext (PULL) Common Type Header invalid drep 0x%02X",
526 /* length of the "Private Header for Constructed Type" */
527 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, &hdrlen
));
529 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
530 "Bad subcontext (PULL) Common Type Header length %d != 8",
534 /* filler should be ignored */
535 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &filler
));
538 * Private Header for Constructed Type
540 /* length - will be updated latter */
541 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &content_size
));
542 if (size_is
>= 0 && size_is
!= content_size
) {
543 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
544 (int)size_is
, (int)content_size
);
546 /* the content size must be a multiple of 8 */
547 if ((content_size
% 8) != 0) {
548 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
549 "Bad subcontext (PULL) size_is(%d) not padded to 8 content_size %d",
550 (int)size_is
, (int)content_size
);
552 r_content_size
= content_size
;
555 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &reserved
));
559 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) header_size %d",
563 NDR_PULL_NEED_BYTES(ndr
, r_content_size
);
565 subndr
= talloc_zero(ndr
, struct ndr_pull
);
566 NDR_ERR_HAVE_NO_MEMORY(subndr
);
567 subndr
->flags
= ndr
->flags
& ~LIBNDR_FLAG_NDR64
;
568 subndr
->current_mem_ctx
= ndr
->current_mem_ctx
;
570 subndr
->data
= ndr
->data
+ ndr
->offset
;
572 subndr
->data_size
= r_content_size
;
575 ndr_set_flags(&ndr
->flags
, LIBNDR_FLAG_LITTLE_ENDIAN
);
576 } else if (force_be
) {
577 ndr_set_flags(&ndr
->flags
, LIBNDR_FLAG_BIGENDIAN
);
581 return NDR_ERR_SUCCESS
;
584 _PUBLIC_
enum ndr_err_code
ndr_pull_subcontext_end(struct ndr_pull
*ndr
,
585 struct ndr_pull
*subndr
,
592 } else if (header_size
> 0) {
593 advance
= subndr
->data_size
;
595 advance
= subndr
->offset
;
597 NDR_CHECK(ndr_pull_advance(ndr
, advance
));
598 return NDR_ERR_SUCCESS
;
601 _PUBLIC_
enum ndr_err_code
ndr_push_subcontext_start(struct ndr_push
*ndr
,
602 struct ndr_push
**_subndr
,
606 struct ndr_push
*subndr
;
608 subndr
= ndr_push_init_ctx(ndr
);
609 NDR_ERR_HAVE_NO_MEMORY(subndr
);
610 subndr
->flags
= ndr
->flags
& ~LIBNDR_FLAG_NDR64
;
613 NDR_CHECK(ndr_push_zero(subndr
, size_is
));
615 subndr
->relative_end_offset
= size_is
;
619 return NDR_ERR_SUCCESS
;
623 push a subcontext header
625 _PUBLIC_
enum ndr_err_code
ndr_push_subcontext_end(struct ndr_push
*ndr
,
626 struct ndr_push
*subndr
,
633 padding_len
= size_is
- subndr
->offset
;
634 if (padding_len
< 0) {
635 return ndr_push_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
636 (int)subndr
->offset
, (int)size_is
);
638 subndr
->offset
= size_is
;
641 switch (header_size
) {
646 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, subndr
->offset
));
650 NDR_CHECK(ndr_push_uint3264(ndr
, NDR_SCALARS
, subndr
->offset
));
655 * Common Type Header for the Serialization Stream
656 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
658 padding_len
= NDR_ROUND(subndr
->offset
, 8) - subndr
->offset
;
659 if (padding_len
> 0) {
660 NDR_CHECK(ndr_push_zero(subndr
, padding_len
));
664 NDR_CHECK(ndr_push_uint8(ndr
, NDR_SCALARS
, 1));
670 NDR_CHECK(ndr_push_uint8(ndr
, NDR_SCALARS
, NDR_BE(ndr
)?0x00:0x10));
672 /* length of the "Private Header for Constructed Type" */
673 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, 8));
676 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0xCCCCCCCC));
679 * Private Header for Constructed Type
681 /* length - will be updated latter */
682 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, subndr
->offset
));
685 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0));
689 return ndr_push_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext header size %d",
693 NDR_CHECK(ndr_push_bytes(ndr
, subndr
->data
, subndr
->offset
));
694 return NDR_ERR_SUCCESS
;
698 store a token in the ndr context, for later retrieval
700 _PUBLIC_
enum ndr_err_code
ndr_token_store(TALLOC_CTX
*mem_ctx
,
701 struct ndr_token_list
**list
,
705 struct ndr_token_list
*tok
;
706 tok
= talloc(mem_ctx
, struct ndr_token_list
);
707 NDR_ERR_HAVE_NO_MEMORY(tok
);
710 DLIST_ADD((*list
), tok
);
711 return NDR_ERR_SUCCESS
;
715 retrieve a token from a ndr context, using cmp_fn to match the tokens
717 _PUBLIC_
enum ndr_err_code
ndr_token_retrieve_cmp_fn(struct ndr_token_list
**list
, const void *key
, uint32_t *v
,
718 comparison_fn_t _cmp_fn
, bool _remove_tok
)
720 struct ndr_token_list
*tok
;
721 for (tok
=*list
;tok
;tok
=tok
->next
) {
722 if (_cmp_fn
&& _cmp_fn(tok
->key
,key
)==0) goto found
;
723 else if (!_cmp_fn
&& tok
->key
== key
) goto found
;
725 return NDR_ERR_TOKEN
;
729 DLIST_REMOVE((*list
), tok
);
732 return NDR_ERR_SUCCESS
;
736 retrieve a token from a ndr context
738 _PUBLIC_
enum ndr_err_code
ndr_token_retrieve(struct ndr_token_list
**list
, const void *key
, uint32_t *v
)
740 return ndr_token_retrieve_cmp_fn(list
, key
, v
, NULL
, true);
744 peek at but don't removed a token from a ndr context
746 _PUBLIC_
uint32_t ndr_token_peek(struct ndr_token_list
**list
, const void *key
)
748 enum ndr_err_code status
;
751 status
= ndr_token_retrieve_cmp_fn(list
, key
, &v
, NULL
, false);
752 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
760 pull an array size field and add it to the array_size_list token list
762 _PUBLIC_
enum ndr_err_code
ndr_pull_array_size(struct ndr_pull
*ndr
, const void *p
)
765 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &size
));
766 return ndr_token_store(ndr
, &ndr
->array_size_list
, p
, size
);
770 get the stored array size field
772 _PUBLIC_
uint32_t ndr_get_array_size(struct ndr_pull
*ndr
, const void *p
)
774 return ndr_token_peek(&ndr
->array_size_list
, p
);
778 check the stored array size field
780 _PUBLIC_
enum ndr_err_code
ndr_check_array_size(struct ndr_pull
*ndr
, void *p
, uint32_t size
)
783 stored
= ndr_token_peek(&ndr
->array_size_list
, p
);
784 if (stored
!= size
) {
785 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
786 "Bad array size - got %u expected %u\n",
789 return NDR_ERR_SUCCESS
;
793 pull an array length field and add it to the array_length_list token list
795 _PUBLIC_
enum ndr_err_code
ndr_pull_array_length(struct ndr_pull
*ndr
, const void *p
)
797 uint32_t length
, offset
;
798 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &offset
));
800 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
801 "non-zero array offset %u\n", offset
);
803 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &length
));
804 return ndr_token_store(ndr
, &ndr
->array_length_list
, p
, length
);
808 get the stored array length field
810 _PUBLIC_
uint32_t ndr_get_array_length(struct ndr_pull
*ndr
, const void *p
)
812 return ndr_token_peek(&ndr
->array_length_list
, p
);
816 check the stored array length field
818 _PUBLIC_
enum ndr_err_code
ndr_check_array_length(struct ndr_pull
*ndr
, void *p
, uint32_t length
)
821 stored
= ndr_token_peek(&ndr
->array_length_list
, p
);
822 if (stored
!= length
) {
823 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
824 "Bad array length - got %u expected %u\n",
827 return NDR_ERR_SUCCESS
;
830 _PUBLIC_
enum ndr_err_code
ndr_push_pipe_chunk_trailer(struct ndr_push
*ndr
, int ndr_flags
, uint32_t count
)
832 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
833 int64_t tmp
= 0 - (int64_t)count
;
834 uint64_t ncount
= tmp
;
836 NDR_CHECK(ndr_push_hyper(ndr
, ndr_flags
, ncount
));
839 return NDR_ERR_SUCCESS
;
842 _PUBLIC_
enum ndr_err_code
ndr_check_pipe_chunk_trailer(struct ndr_pull
*ndr
, int ndr_flags
, uint32_t count
)
844 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
845 int64_t tmp
= 0 - (int64_t)count
;
846 uint64_t ncount1
= tmp
;
849 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, &ncount2
));
850 if (ncount1
== ncount2
) {
851 return NDR_ERR_SUCCESS
;
854 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
855 "Bad pipe trailer[%lld should be %lld] size was %lu\"",
856 (unsigned long long)ncount2
,
857 (unsigned long long)ncount1
,
858 (unsigned long)count
);
861 return NDR_ERR_SUCCESS
;
867 _PUBLIC_
enum ndr_err_code
ndr_push_set_switch_value(struct ndr_push
*ndr
, const void *p
, uint32_t val
)
869 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
872 _PUBLIC_
enum ndr_err_code
ndr_pull_set_switch_value(struct ndr_pull
*ndr
, const void *p
, uint32_t val
)
874 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
877 _PUBLIC_
enum ndr_err_code
ndr_print_set_switch_value(struct ndr_print
*ndr
, const void *p
, uint32_t val
)
879 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
883 retrieve a switch value
885 _PUBLIC_
uint32_t ndr_push_get_switch_value(struct ndr_push
*ndr
, const void *p
)
887 return ndr_token_peek(&ndr
->switch_list
, p
);
890 _PUBLIC_
uint32_t ndr_pull_get_switch_value(struct ndr_pull
*ndr
, const void *p
)
892 return ndr_token_peek(&ndr
->switch_list
, p
);
895 _PUBLIC_
uint32_t ndr_print_get_switch_value(struct ndr_print
*ndr
, const void *p
)
897 return ndr_token_peek(&ndr
->switch_list
, p
);
901 pull a struct from a blob using NDR
903 _PUBLIC_
enum ndr_err_code
ndr_pull_struct_blob(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
, void *p
,
904 ndr_pull_flags_fn_t fn
)
906 struct ndr_pull
*ndr
;
907 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
908 NDR_ERR_HAVE_NO_MEMORY(ndr
);
909 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
911 return NDR_ERR_SUCCESS
;
915 pull a struct from a blob using NDR - failing if all bytes are not consumed
917 _PUBLIC_
enum ndr_err_code
ndr_pull_struct_blob_all(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
918 void *p
, ndr_pull_flags_fn_t fn
)
920 struct ndr_pull
*ndr
;
921 uint32_t highest_ofs
;
922 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
923 NDR_ERR_HAVE_NO_MEMORY(ndr
);
924 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
925 if (ndr
->offset
> ndr
->relative_highest_offset
) {
926 highest_ofs
= ndr
->offset
;
928 highest_ofs
= ndr
->relative_highest_offset
;
930 if (highest_ofs
< ndr
->data_size
) {
931 enum ndr_err_code ret
;
932 ret
= ndr_pull_error(ndr
, NDR_ERR_UNREAD_BYTES
,
933 "not all bytes consumed ofs[%u] size[%u]",
934 highest_ofs
, ndr
->data_size
);
939 return NDR_ERR_SUCCESS
;
943 pull a union from a blob using NDR, given the union discriminator
945 _PUBLIC_
enum ndr_err_code
ndr_pull_union_blob(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
947 uint32_t level
, ndr_pull_flags_fn_t fn
)
949 struct ndr_pull
*ndr
;
950 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
951 NDR_ERR_HAVE_NO_MEMORY(ndr
);
952 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr
, p
, level
));
953 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
955 return NDR_ERR_SUCCESS
;
959 pull a union from a blob using NDR, given the union discriminator,
960 failing if all bytes are not consumed
962 _PUBLIC_
enum ndr_err_code
ndr_pull_union_blob_all(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
964 uint32_t level
, ndr_pull_flags_fn_t fn
)
966 struct ndr_pull
*ndr
;
967 uint32_t highest_ofs
;
968 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
969 NDR_ERR_HAVE_NO_MEMORY(ndr
);
970 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr
, p
, level
));
971 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
972 if (ndr
->offset
> ndr
->relative_highest_offset
) {
973 highest_ofs
= ndr
->offset
;
975 highest_ofs
= ndr
->relative_highest_offset
;
977 if (highest_ofs
< ndr
->data_size
) {
978 enum ndr_err_code ret
;
979 ret
= ndr_pull_error(ndr
, NDR_ERR_UNREAD_BYTES
,
980 "not all bytes consumed ofs[%u] size[%u]",
981 highest_ofs
, ndr
->data_size
);
986 return NDR_ERR_SUCCESS
;
990 push a struct to a blob using NDR
992 _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
)
994 struct ndr_push
*ndr
;
995 ndr
= ndr_push_init_ctx(mem_ctx
);
996 NDR_ERR_HAVE_NO_MEMORY(ndr
);
998 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
1000 *blob
= ndr_push_blob(ndr
);
1001 talloc_steal(mem_ctx
, blob
->data
);
1004 return NDR_ERR_SUCCESS
;
1008 push a union to a blob using NDR
1010 _PUBLIC_
enum ndr_err_code
ndr_push_union_blob(DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
, void *p
,
1011 uint32_t level
, ndr_push_flags_fn_t fn
)
1013 struct ndr_push
*ndr
;
1014 ndr
= ndr_push_init_ctx(mem_ctx
);
1015 NDR_ERR_HAVE_NO_MEMORY(ndr
);
1017 NDR_CHECK(ndr_push_set_switch_value(ndr
, p
, level
));
1018 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
1020 *blob
= ndr_push_blob(ndr
);
1021 talloc_steal(mem_ctx
, blob
->data
);
1024 return NDR_ERR_SUCCESS
;
1028 generic ndr_size_*() handler for structures
1030 _PUBLIC_
size_t ndr_size_struct(const void *p
, int flags
, ndr_push_flags_fn_t push
)
1032 struct ndr_push
*ndr
;
1033 enum ndr_err_code status
;
1036 /* avoid recursion */
1037 if (flags
& LIBNDR_FLAG_NO_NDR_SIZE
) return 0;
1039 ndr
= ndr_push_init_ctx(NULL
);
1041 ndr
->flags
|= flags
| LIBNDR_FLAG_NO_NDR_SIZE
;
1042 status
= push(ndr
, NDR_SCALARS
|NDR_BUFFERS
, discard_const(p
));
1043 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
1053 generic ndr_size_*() handler for unions
1055 _PUBLIC_
size_t ndr_size_union(const void *p
, int flags
, uint32_t level
, ndr_push_flags_fn_t push
)
1057 struct ndr_push
*ndr
;
1058 enum ndr_err_code status
;
1061 /* avoid recursion */
1062 if (flags
& LIBNDR_FLAG_NO_NDR_SIZE
) return 0;
1064 ndr
= ndr_push_init_ctx(NULL
);
1066 ndr
->flags
|= flags
| LIBNDR_FLAG_NO_NDR_SIZE
;
1068 status
= ndr_push_set_switch_value(ndr
, p
, level
);
1069 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
1073 status
= push(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
);
1074 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
1084 get the current base for relative pointers for the push
1086 _PUBLIC_
uint32_t ndr_push_get_relative_base_offset(struct ndr_push
*ndr
)
1088 return ndr
->relative_base_offset
;
1092 restore the old base for relative pointers for the push
1094 _PUBLIC_
void ndr_push_restore_relative_base_offset(struct ndr_push
*ndr
, uint32_t offset
)
1096 ndr
->relative_base_offset
= offset
;
1100 setup the current base for relative pointers for the push
1101 called in the NDR_SCALAR stage
1103 _PUBLIC_
enum ndr_err_code
ndr_push_setup_relative_base_offset1(struct ndr_push
*ndr
, const void *p
, uint32_t offset
)
1105 ndr
->relative_base_offset
= offset
;
1106 return ndr_token_store(ndr
, &ndr
->relative_base_list
, p
, offset
);
1110 setup the current base for relative pointers for the push
1111 called in the NDR_BUFFERS stage
1113 _PUBLIC_
enum ndr_err_code
ndr_push_setup_relative_base_offset2(struct ndr_push
*ndr
, const void *p
)
1115 return ndr_token_retrieve(&ndr
->relative_base_list
, p
, &ndr
->relative_base_offset
);
1119 push a relative object - stage1
1120 this is called during SCALARS processing
1122 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr1(struct ndr_push
*ndr
, const void *p
)
1125 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0));
1126 return NDR_ERR_SUCCESS
;
1128 NDR_CHECK(ndr_push_align(ndr
, 4));
1129 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_list
, p
, ndr
->offset
));
1130 return ndr_push_uint32(ndr
, NDR_SCALARS
, 0xFFFFFFFF);
1134 push a short relative object - stage1
1135 this is called during SCALARS processing
1137 _PUBLIC_
enum ndr_err_code
ndr_push_short_relative_ptr1(struct ndr_push
*ndr
, const void *p
)
1140 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, 0));
1141 return NDR_ERR_SUCCESS
;
1143 NDR_CHECK(ndr_push_align(ndr
, 2));
1144 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_list
, p
, ndr
->offset
));
1145 return ndr_push_uint16(ndr
, NDR_SCALARS
, 0xFFFF);
1148 push a relative object - stage2
1149 this is called during buffers processing
1151 static enum ndr_err_code
ndr_push_relative_ptr2(struct ndr_push
*ndr
, const void *p
)
1153 uint32_t save_offset
;
1154 uint32_t ptr_offset
= 0xFFFFFFFF;
1156 return NDR_ERR_SUCCESS
;
1158 save_offset
= ndr
->offset
;
1159 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &ptr_offset
));
1160 if (ptr_offset
> ndr
->offset
) {
1161 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1162 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1163 ptr_offset
, ndr
->offset
);
1165 ndr
->offset
= ptr_offset
;
1166 if (save_offset
< ndr
->relative_base_offset
) {
1167 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1168 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1169 save_offset
, ndr
->relative_base_offset
);
1171 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, save_offset
- ndr
->relative_base_offset
));
1172 ndr
->offset
= save_offset
;
1173 return NDR_ERR_SUCCESS
;
1176 push a short relative object - stage2
1177 this is called during buffers processing
1179 _PUBLIC_
enum ndr_err_code
ndr_push_short_relative_ptr2(struct ndr_push
*ndr
, const void *p
)
1181 uint32_t save_offset
;
1182 uint32_t ptr_offset
= 0xFFFF;
1184 return NDR_ERR_SUCCESS
;
1186 save_offset
= ndr
->offset
;
1187 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &ptr_offset
));
1188 if (ptr_offset
> ndr
->offset
) {
1189 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1190 "ndr_push_short_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1191 ptr_offset
, ndr
->offset
);
1193 ndr
->offset
= ptr_offset
;
1194 if (save_offset
< ndr
->relative_base_offset
) {
1195 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1196 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1197 save_offset
, ndr
->relative_base_offset
);
1199 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, save_offset
- ndr
->relative_base_offset
));
1200 ndr
->offset
= save_offset
;
1201 return NDR_ERR_SUCCESS
;
1205 push a relative object - stage2 start
1206 this is called during buffers processing
1208 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr2_start(struct ndr_push
*ndr
, const void *p
)
1211 return NDR_ERR_SUCCESS
;
1213 if (!(ndr
->flags
& LIBNDR_FLAG_RELATIVE_REVERSE
)) {
1214 uint32_t relative_offset
;
1218 if (ndr
->offset
< ndr
->relative_base_offset
) {
1219 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1220 "ndr_push_relative_ptr2_start ndr->offset(%u) < ndr->relative_base_offset(%u)",
1221 ndr
->offset
, ndr
->relative_base_offset
);
1224 relative_offset
= ndr
->offset
- ndr
->relative_base_offset
;
1226 if (ndr
->flags
& LIBNDR_FLAG_NOALIGN
) {
1228 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
1230 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
1232 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
1236 pad
= ndr_align_size(relative_offset
, align
);
1238 NDR_CHECK(ndr_push_zero(ndr
, pad
));
1241 return ndr_push_relative_ptr2(ndr
, p
);
1243 if (ndr
->relative_end_offset
== -1) {
1244 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1245 "ndr_push_relative_ptr2_start RELATIVE_REVERSE flag set and relative_end_offset %d",
1246 ndr
->relative_end_offset
);
1248 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_begin_list
, p
, ndr
->offset
));
1249 return NDR_ERR_SUCCESS
;
1253 push a relative object - stage2 end
1254 this is called during buffers processing
1256 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr2_end(struct ndr_push
*ndr
, const void *p
)
1258 uint32_t begin_offset
= 0xFFFFFFFF;
1260 uint32_t correct_offset
= 0;
1265 return NDR_ERR_SUCCESS
;
1268 if (!(ndr
->flags
& LIBNDR_FLAG_RELATIVE_REVERSE
)) {
1269 return NDR_ERR_SUCCESS
;
1272 if (ndr
->flags
& LIBNDR_FLAG_NO_NDR_SIZE
) {
1273 /* better say more than calculation a too small buffer */
1274 NDR_PUSH_ALIGN(ndr
, 8);
1275 return NDR_ERR_SUCCESS
;
1278 if (ndr
->relative_end_offset
< ndr
->offset
) {
1279 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1280 "ndr_push_relative_ptr2_end:"
1281 "relative_end_offset %u < offset %u",
1282 ndr
->relative_end_offset
, ndr
->offset
);
1285 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_begin_list
, p
, &begin_offset
));
1287 /* we have marshalled a buffer, see how long it was */
1288 len
= ndr
->offset
- begin_offset
;
1291 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1292 "ndr_push_relative_ptr2_end:"
1293 "offset %u - begin_offset %u < 0",
1294 ndr
->offset
, begin_offset
);
1297 if (ndr
->relative_end_offset
< len
) {
1298 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1299 "ndr_push_relative_ptr2_end:"
1300 "relative_end_offset %u < len %lld",
1301 ndr
->offset
, (long long)len
);
1304 /* the reversed offset is at the end of the main buffer */
1305 correct_offset
= ndr
->relative_end_offset
- len
;
1307 if (ndr
->flags
& LIBNDR_FLAG_NOALIGN
) {
1309 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
1311 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
1313 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
1317 pad
= ndr_align_size(correct_offset
, align
);
1319 correct_offset
+= pad
;
1320 correct_offset
-= align
;
1323 if (correct_offset
< begin_offset
) {
1324 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1325 "ndr_push_relative_ptr2_end: "
1326 "correct_offset %u < begin_offset %u",
1327 correct_offset
, begin_offset
);
1331 uint32_t clear_size
= correct_offset
- begin_offset
;
1333 clear_size
= MIN(clear_size
, len
);
1335 /* now move the marshalled buffer to the end of the main buffer */
1336 memmove(ndr
->data
+ correct_offset
, ndr
->data
+ begin_offset
, len
);
1339 /* and wipe out old buffer within the main buffer */
1340 memset(ndr
->data
+ begin_offset
, '\0', clear_size
);
1344 /* and set the end offset for the next buffer */
1345 ndr
->relative_end_offset
= correct_offset
;
1347 /* finally write the offset to the main buffer */
1348 ndr
->offset
= correct_offset
;
1349 NDR_CHECK(ndr_push_relative_ptr2(ndr
, p
));
1351 /* restore to where we were in the main buffer */
1352 ndr
->offset
= begin_offset
;
1354 return NDR_ERR_SUCCESS
;
1358 get the current base for relative pointers for the pull
1360 _PUBLIC_
uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull
*ndr
)
1362 return ndr
->relative_base_offset
;
1366 restore the old base for relative pointers for the pull
1368 _PUBLIC_
void ndr_pull_restore_relative_base_offset(struct ndr_pull
*ndr
, uint32_t offset
)
1370 ndr
->relative_base_offset
= offset
;
1374 setup the current base for relative pointers for the pull
1375 called in the NDR_SCALAR stage
1377 _PUBLIC_
enum ndr_err_code
ndr_pull_setup_relative_base_offset1(struct ndr_pull
*ndr
, const void *p
, uint32_t offset
)
1379 ndr
->relative_base_offset
= offset
;
1380 return ndr_token_store(ndr
, &ndr
->relative_base_list
, p
, offset
);
1384 setup the current base for relative pointers for the pull
1385 called in the NDR_BUFFERS stage
1387 _PUBLIC_
enum ndr_err_code
ndr_pull_setup_relative_base_offset2(struct ndr_pull
*ndr
, const void *p
)
1389 return ndr_token_retrieve(&ndr
->relative_base_list
, p
, &ndr
->relative_base_offset
);
1393 pull a relative object - stage1
1394 called during SCALARS processing
1396 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr1(struct ndr_pull
*ndr
, const void *p
, uint32_t rel_offset
)
1398 rel_offset
+= ndr
->relative_base_offset
;
1399 if (rel_offset
> ndr
->data_size
) {
1400 return ndr_pull_error(ndr
, NDR_ERR_BUFSIZE
,
1401 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1402 rel_offset
, ndr
->data_size
);
1404 return ndr_token_store(ndr
, &ndr
->relative_list
, p
, rel_offset
);
1408 pull a relative object - stage2
1409 called during BUFFERS processing
1411 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr2(struct ndr_pull
*ndr
, const void *p
)
1413 uint32_t rel_offset
;
1414 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &rel_offset
));
1415 return ndr_pull_set_offset(ndr
, rel_offset
);
1418 const static struct {
1419 enum ndr_err_code err
;
1421 } ndr_err_code_strings
[] = {
1422 { NDR_ERR_SUCCESS
, "Success" },
1423 { NDR_ERR_ARRAY_SIZE
, "Bad Array Size" },
1424 { NDR_ERR_BAD_SWITCH
, "Bad Switch" },
1425 { NDR_ERR_OFFSET
, "Offset Error" },
1426 { NDR_ERR_RELATIVE
, "Relative Pointer Error" },
1427 { NDR_ERR_CHARCNV
, "Character Conversion Error" },
1428 { NDR_ERR_LENGTH
, "Length Error" },
1429 { NDR_ERR_SUBCONTEXT
, "Subcontext Error" },
1430 { NDR_ERR_COMPRESSION
, "Compression Error" },
1431 { NDR_ERR_STRING
, "String Error" },
1432 { NDR_ERR_VALIDATE
, "Validate Error" },
1433 { NDR_ERR_BUFSIZE
, "Buffer Size Error" },
1434 { NDR_ERR_ALLOC
, "Allocation Error" },
1435 { NDR_ERR_RANGE
, "Range Error" },
1436 { NDR_ERR_TOKEN
, "Token Error" },
1437 { NDR_ERR_IPV4ADDRESS
, "IPv4 Address Error" },
1438 { NDR_ERR_INVALID_POINTER
, "Invalid Pointer" },
1439 { NDR_ERR_UNREAD_BYTES
, "Unread Bytes" },
1440 { NDR_ERR_NDR64
, "NDR64 assertion error" },
1444 _PUBLIC_
const char *ndr_map_error2string(enum ndr_err_code ndr_err
)
1447 for (i
= 0; ndr_err_code_strings
[i
].string
!= NULL
; i
++) {
1448 if (ndr_err_code_strings
[i
].err
== ndr_err
)
1449 return ndr_err_code_strings
[i
].string
;
1451 return "Unknown error";