2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2003
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 this provides the core routines for NDR parsing functions
25 see http://www.opengroup.org/onlinepubs/9629399/chap14.htm for details
30 #include "librpc/ndr/libndr.h"
31 #include "../lib/util/dlinklist.h"
32 #if _SAMBA_BUILD_ == 4
33 #include "param/param.h"
36 #define NDR_BASE_MARSHALL_SIZE 1024
38 /* this guid indicates NDR encoding in a protocol tower */
39 const struct ndr_syntax_id ndr_transfer_syntax
= {
40 { 0x8a885d04, 0x1ceb, 0x11c9, {0x9f, 0xe8}, {0x08,0x00,0x2b,0x10,0x48,0x60} },
44 const struct ndr_syntax_id ndr64_transfer_syntax
= {
45 { 0x71710533, 0xbeba, 0x4937, {0x83, 0x19}, {0xb5,0xdb,0xef,0x9c,0xcc,0x36} },
49 const struct ndr_syntax_id null_ndr_syntax_id
= {
50 { 0, 0, 0, { 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
55 work out the number of bytes needed to align on a n byte boundary
57 _PUBLIC_
size_t ndr_align_size(uint32_t offset
, size_t n
)
59 if ((offset
& (n
-1)) == 0) return 0;
60 return n
- (offset
& (n
-1));
64 initialise a ndr parse structure from a data blob
66 _PUBLIC_
struct ndr_pull
*ndr_pull_init_blob(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
)
70 ndr
= talloc_zero(mem_ctx
, struct ndr_pull
);
71 if (!ndr
) return NULL
;
72 ndr
->current_mem_ctx
= mem_ctx
;
74 ndr
->data
= blob
->data
;
75 ndr
->data_size
= blob
->length
;
81 advance by 'size' bytes
83 _PUBLIC_
enum ndr_err_code
ndr_pull_advance(struct ndr_pull
*ndr
, uint32_t size
)
86 if (ndr
->offset
> ndr
->data_size
) {
87 return ndr_pull_error(ndr
, NDR_ERR_BUFSIZE
,
88 "ndr_pull_advance by %u failed",
91 return NDR_ERR_SUCCESS
;
95 set the parse offset to 'ofs'
97 static enum ndr_err_code
ndr_pull_set_offset(struct ndr_pull
*ndr
, uint32_t ofs
)
100 if (ndr
->offset
> ndr
->data_size
) {
101 return ndr_pull_error(ndr
, NDR_ERR_BUFSIZE
,
102 "ndr_pull_set_offset %u failed",
105 return NDR_ERR_SUCCESS
;
108 /* create a ndr_push structure, ready for some marshalling */
109 _PUBLIC_
struct ndr_push
*ndr_push_init_ctx(TALLOC_CTX
*mem_ctx
)
111 struct ndr_push
*ndr
;
113 ndr
= talloc_zero(mem_ctx
, struct ndr_push
);
119 ndr
->alloc_size
= NDR_BASE_MARSHALL_SIZE
;
120 ndr
->data
= talloc_array(ndr
, uint8_t, ndr
->alloc_size
);
128 /* return a DATA_BLOB structure for the current ndr_push marshalled data */
129 _PUBLIC_ DATA_BLOB
ndr_push_blob(struct ndr_push
*ndr
)
132 blob
= data_blob_const(ndr
->data
, ndr
->offset
);
133 if (ndr
->alloc_size
> ndr
->offset
) {
134 ndr
->data
[ndr
->offset
] = 0;
141 expand the available space in the buffer to ndr->offset + extra_size
143 _PUBLIC_
enum ndr_err_code
ndr_push_expand(struct ndr_push
*ndr
, uint32_t extra_size
)
145 uint32_t size
= extra_size
+ ndr
->offset
;
147 if (size
< ndr
->offset
) {
148 /* extra_size overflowed the offset */
149 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
, "Overflow in push_expand to %u",
153 if (ndr
->alloc_size
> size
) {
154 return NDR_ERR_SUCCESS
;
157 ndr
->alloc_size
+= NDR_BASE_MARSHALL_SIZE
;
158 if (size
+1 > ndr
->alloc_size
) {
159 ndr
->alloc_size
= size
+1;
161 ndr
->data
= talloc_realloc(ndr
, ndr
->data
, uint8_t, ndr
->alloc_size
);
163 return ndr_push_error(ndr
, NDR_ERR_ALLOC
, "Failed to push_expand to %u",
167 return NDR_ERR_SUCCESS
;
170 _PUBLIC_
void ndr_print_debug_helper(struct ndr_print
*ndr
, const char *format
, ...)
177 va_start(ap
, format
);
178 ret
= vasprintf(&s
, format
, ap
);
185 if (ndr
->no_newline
) {
186 DEBUGADD(1,("%s", s
));
191 for (i
=0;i
<ndr
->depth
;i
++) {
195 DEBUGADD(1,("%s\n", s
));
199 _PUBLIC_
void ndr_print_printf_helper(struct ndr_print
*ndr
, const char *format
, ...)
204 if (!ndr
->no_newline
) {
205 for (i
=0;i
<ndr
->depth
;i
++) {
210 va_start(ap
, format
);
213 if (!ndr
->no_newline
) {
218 _PUBLIC_
void ndr_print_string_helper(struct ndr_print
*ndr
, const char *format
, ...)
223 if (!ndr
->no_newline
) {
224 for (i
=0;i
<ndr
->depth
;i
++) {
225 ndr
->private_data
= talloc_asprintf_append_buffer(
226 (char *)ndr
->private_data
, " ");
230 va_start(ap
, format
);
231 ndr
->private_data
= talloc_vasprintf_append_buffer((char *)ndr
->private_data
,
234 if (!ndr
->no_newline
) {
235 ndr
->private_data
= talloc_asprintf_append_buffer((char *)ndr
->private_data
,
241 a useful helper function for printing idl structures via DEBUG()
243 _PUBLIC_
void ndr_print_debug(ndr_print_fn_t fn
, const char *name
, void *ptr
)
245 struct ndr_print
*ndr
;
249 ndr
= talloc_zero(NULL
, struct ndr_print
);
251 ndr
->print
= ndr_print_debug_helper
;
259 a useful helper function for printing idl unions via DEBUG()
261 _PUBLIC_
void ndr_print_union_debug(ndr_print_fn_t fn
, const char *name
, uint32_t level
, void *ptr
)
263 struct ndr_print
*ndr
;
267 ndr
= talloc_zero(NULL
, struct ndr_print
);
269 ndr
->print
= ndr_print_debug_helper
;
272 ndr_print_set_switch_value(ndr
, ptr
, level
);
278 a useful helper function for printing idl function calls via DEBUG()
280 _PUBLIC_
void ndr_print_function_debug(ndr_print_function_t fn
, const char *name
, int flags
, void *ptr
)
282 struct ndr_print
*ndr
;
286 ndr
= talloc_zero(NULL
, struct ndr_print
);
288 ndr
->print
= ndr_print_debug_helper
;
292 fn(ndr
, name
, flags
, ptr
);
297 a useful helper function for printing idl structures to a string
299 _PUBLIC_
char *ndr_print_struct_string(TALLOC_CTX
*mem_ctx
, ndr_print_fn_t fn
, const char *name
, void *ptr
)
301 struct ndr_print
*ndr
;
304 ndr
= talloc_zero(mem_ctx
, struct ndr_print
);
305 if (!ndr
) return NULL
;
306 ndr
->private_data
= talloc_strdup(ndr
, "");
307 if (!ndr
->private_data
) {
310 ndr
->print
= ndr_print_string_helper
;
315 ret
= talloc_steal(mem_ctx
, (char *)ndr
->private_data
);
322 a useful helper function for printing idl unions to a string
324 _PUBLIC_
char *ndr_print_union_string(TALLOC_CTX
*mem_ctx
, ndr_print_fn_t fn
, const char *name
, uint32_t level
, void *ptr
)
326 struct ndr_print
*ndr
;
329 ndr
= talloc_zero(mem_ctx
, struct ndr_print
);
330 if (!ndr
) return NULL
;
331 ndr
->private_data
= talloc_strdup(ndr
, "");
332 if (!ndr
->private_data
) {
335 ndr
->print
= ndr_print_string_helper
;
338 ndr_print_set_switch_value(ndr
, ptr
, level
);
340 ret
= talloc_steal(mem_ctx
, (char *)ndr
->private_data
);
347 a useful helper function for printing idl function calls to a string
349 _PUBLIC_
char *ndr_print_function_string(TALLOC_CTX
*mem_ctx
,
350 ndr_print_function_t fn
, const char *name
,
351 int flags
, void *ptr
)
353 struct ndr_print
*ndr
;
356 ndr
= talloc_zero(mem_ctx
, struct ndr_print
);
357 if (!ndr
) return NULL
;
358 ndr
->private_data
= talloc_strdup(ndr
, "");
359 if (!ndr
->private_data
) {
362 ndr
->print
= ndr_print_string_helper
;
365 fn(ndr
, name
, flags
, ptr
);
366 ret
= talloc_steal(mem_ctx
, (char *)ndr
->private_data
);
372 _PUBLIC_
void ndr_set_flags(uint32_t *pflags
, uint32_t new_flags
)
374 /* the big/little endian flags are inter-dependent */
375 if (new_flags
& LIBNDR_FLAG_LITTLE_ENDIAN
) {
376 (*pflags
) &= ~LIBNDR_FLAG_BIGENDIAN
;
377 (*pflags
) &= ~LIBNDR_FLAG_NDR64
;
379 if (new_flags
& LIBNDR_FLAG_BIGENDIAN
) {
380 (*pflags
) &= ~LIBNDR_FLAG_LITTLE_ENDIAN
;
381 (*pflags
) &= ~LIBNDR_FLAG_NDR64
;
383 if (new_flags
& LIBNDR_FLAG_REMAINING
) {
384 (*pflags
) &= ~LIBNDR_ALIGN_FLAGS
;
386 if (new_flags
& LIBNDR_ALIGN_FLAGS
) {
387 (*pflags
) &= ~LIBNDR_FLAG_REMAINING
;
389 if (new_flags
& LIBNDR_FLAG_NO_RELATIVE_REVERSE
) {
390 (*pflags
) &= ~LIBNDR_FLAG_RELATIVE_REVERSE
;
392 (*pflags
) |= new_flags
;
396 return and possibly log an NDR error
398 _PUBLIC_
enum ndr_err_code
ndr_pull_error(struct ndr_pull
*ndr
,
399 enum ndr_err_code ndr_err
,
400 const char *format
, ...)
406 va_start(ap
, format
);
407 ret
= vasprintf(&s
, format
, ap
);
411 return NDR_ERR_ALLOC
;
414 DEBUG(1,("ndr_pull_error(%u): %s\n", ndr_err
, s
));
422 return and possibly log an NDR error
424 _PUBLIC_
enum ndr_err_code
ndr_push_error(struct ndr_push
*ndr
,
425 enum ndr_err_code ndr_err
,
426 const char *format
, ...)
432 va_start(ap
, format
);
433 ret
= vasprintf(&s
, format
, ap
);
437 return NDR_ERR_ALLOC
;
440 DEBUG(1,("ndr_push_error(%u): %s\n", ndr_err
, s
));
448 handle subcontext buffers, which in midl land are user-marshalled, but
449 we use magic in pidl to make them easier to cope with
451 _PUBLIC_
enum ndr_err_code
ndr_pull_subcontext_start(struct ndr_pull
*ndr
,
452 struct ndr_pull
**_subndr
,
456 struct ndr_pull
*subndr
;
457 uint32_t r_content_size
;
458 bool force_le
= false;
459 bool force_be
= false;
461 switch (header_size
) {
463 uint32_t content_size
= ndr
->data_size
- ndr
->offset
;
465 content_size
= size_is
;
467 r_content_size
= content_size
;
472 uint16_t content_size
;
473 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, &content_size
));
474 if (size_is
>= 0 && size_is
!= content_size
) {
475 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
476 (int)size_is
, (int)content_size
);
478 r_content_size
= content_size
;
483 uint32_t content_size
;
484 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &content_size
));
485 if (size_is
>= 0 && size_is
!= content_size
) {
486 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
487 (int)size_is
, (int)content_size
);
489 r_content_size
= content_size
;
494 * Common Type Header for the Serialization Stream
495 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
501 uint32_t content_size
;
505 NDR_CHECK(ndr_pull_uint8(ndr
, NDR_SCALARS
, &version
));
508 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
509 "Bad subcontext (PULL) Common Type Header version %d != 1",
517 NDR_CHECK(ndr_pull_uint8(ndr
, NDR_SCALARS
, &drep
));
520 } else if (drep
== 0x00) {
523 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
524 "Bad subcontext (PULL) Common Type Header invalid drep 0x%02X",
528 /* length of the "Private Header for Constructed Type" */
529 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, &hdrlen
));
531 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
532 "Bad subcontext (PULL) Common Type Header length %d != 8",
536 /* filler should be ignored */
537 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &filler
));
540 * Private Header for Constructed Type
542 /* length - will be updated latter */
543 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &content_size
));
544 if (size_is
>= 0 && size_is
!= content_size
) {
545 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
546 (int)size_is
, (int)content_size
);
548 /* the content size must be a multiple of 8 */
549 if ((content_size
% 8) != 0) {
550 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
551 "Bad subcontext (PULL) size_is(%d) not padded to 8 content_size %d",
552 (int)size_is
, (int)content_size
);
554 r_content_size
= content_size
;
557 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &reserved
));
561 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) header_size %d",
565 NDR_PULL_NEED_BYTES(ndr
, r_content_size
);
567 subndr
= talloc_zero(ndr
, struct ndr_pull
);
568 NDR_ERR_HAVE_NO_MEMORY(subndr
);
569 subndr
->flags
= ndr
->flags
& ~LIBNDR_FLAG_NDR64
;
570 subndr
->current_mem_ctx
= ndr
->current_mem_ctx
;
572 subndr
->data
= ndr
->data
+ ndr
->offset
;
574 subndr
->data_size
= r_content_size
;
577 ndr_set_flags(&ndr
->flags
, LIBNDR_FLAG_LITTLE_ENDIAN
);
578 } else if (force_be
) {
579 ndr_set_flags(&ndr
->flags
, LIBNDR_FLAG_BIGENDIAN
);
583 return NDR_ERR_SUCCESS
;
586 _PUBLIC_
enum ndr_err_code
ndr_pull_subcontext_end(struct ndr_pull
*ndr
,
587 struct ndr_pull
*subndr
,
594 } else if (header_size
> 0) {
595 advance
= subndr
->data_size
;
597 advance
= subndr
->offset
;
599 NDR_CHECK(ndr_pull_advance(ndr
, advance
));
600 return NDR_ERR_SUCCESS
;
603 _PUBLIC_
enum ndr_err_code
ndr_push_subcontext_start(struct ndr_push
*ndr
,
604 struct ndr_push
**_subndr
,
608 struct ndr_push
*subndr
;
610 subndr
= ndr_push_init_ctx(ndr
);
611 NDR_ERR_HAVE_NO_MEMORY(subndr
);
612 subndr
->flags
= ndr
->flags
& ~LIBNDR_FLAG_NDR64
;
615 NDR_CHECK(ndr_push_zero(subndr
, size_is
));
617 subndr
->relative_end_offset
= size_is
;
621 return NDR_ERR_SUCCESS
;
625 push a subcontext header
627 _PUBLIC_
enum ndr_err_code
ndr_push_subcontext_end(struct ndr_push
*ndr
,
628 struct ndr_push
*subndr
,
635 padding_len
= size_is
- subndr
->offset
;
636 if (padding_len
< 0) {
637 return ndr_push_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
638 (int)subndr
->offset
, (int)size_is
);
640 subndr
->offset
= size_is
;
643 switch (header_size
) {
648 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, subndr
->offset
));
652 NDR_CHECK(ndr_push_uint3264(ndr
, NDR_SCALARS
, subndr
->offset
));
657 * Common Type Header for the Serialization Stream
658 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
660 padding_len
= NDR_ROUND(subndr
->offset
, 8) - subndr
->offset
;
661 if (padding_len
> 0) {
662 NDR_CHECK(ndr_push_zero(subndr
, padding_len
));
666 NDR_CHECK(ndr_push_uint8(ndr
, NDR_SCALARS
, 1));
672 NDR_CHECK(ndr_push_uint8(ndr
, NDR_SCALARS
, NDR_BE(ndr
)?0x00:0x10));
674 /* length of the "Private Header for Constructed Type" */
675 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, 8));
678 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0xCCCCCCCC));
681 * Private Header for Constructed Type
683 /* length - will be updated latter */
684 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, subndr
->offset
));
687 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0));
691 return ndr_push_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext header size %d",
695 NDR_CHECK(ndr_push_bytes(ndr
, subndr
->data
, subndr
->offset
));
696 return NDR_ERR_SUCCESS
;
700 store a token in the ndr context, for later retrieval
702 _PUBLIC_
enum ndr_err_code
ndr_token_store(TALLOC_CTX
*mem_ctx
,
703 struct ndr_token_list
**list
,
707 struct ndr_token_list
*tok
;
708 tok
= talloc(mem_ctx
, struct ndr_token_list
);
709 NDR_ERR_HAVE_NO_MEMORY(tok
);
712 DLIST_ADD((*list
), tok
);
713 return NDR_ERR_SUCCESS
;
717 retrieve a token from a ndr context, using cmp_fn to match the tokens
719 _PUBLIC_
enum ndr_err_code
ndr_token_retrieve_cmp_fn(struct ndr_token_list
**list
, const void *key
, uint32_t *v
,
720 comparison_fn_t _cmp_fn
, bool _remove_tok
)
722 struct ndr_token_list
*tok
;
723 for (tok
=*list
;tok
;tok
=tok
->next
) {
724 if (_cmp_fn
&& _cmp_fn(tok
->key
,key
)==0) goto found
;
725 else if (!_cmp_fn
&& tok
->key
== key
) goto found
;
727 return NDR_ERR_TOKEN
;
731 DLIST_REMOVE((*list
), tok
);
734 return NDR_ERR_SUCCESS
;
738 retrieve a token from a ndr context
740 _PUBLIC_
enum ndr_err_code
ndr_token_retrieve(struct ndr_token_list
**list
, const void *key
, uint32_t *v
)
742 return ndr_token_retrieve_cmp_fn(list
, key
, v
, NULL
, true);
746 peek at but don't removed a token from a ndr context
748 _PUBLIC_
uint32_t ndr_token_peek(struct ndr_token_list
**list
, const void *key
)
750 enum ndr_err_code status
;
753 status
= ndr_token_retrieve_cmp_fn(list
, key
, &v
, NULL
, false);
754 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
762 pull an array size field and add it to the array_size_list token list
764 _PUBLIC_
enum ndr_err_code
ndr_pull_array_size(struct ndr_pull
*ndr
, const void *p
)
767 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &size
));
768 return ndr_token_store(ndr
, &ndr
->array_size_list
, p
, size
);
772 get the stored array size field
774 _PUBLIC_
uint32_t ndr_get_array_size(struct ndr_pull
*ndr
, const void *p
)
776 return ndr_token_peek(&ndr
->array_size_list
, p
);
780 check the stored array size field
782 _PUBLIC_
enum ndr_err_code
ndr_check_array_size(struct ndr_pull
*ndr
, void *p
, uint32_t size
)
785 stored
= ndr_token_peek(&ndr
->array_size_list
, p
);
786 if (stored
!= size
) {
787 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
788 "Bad array size - got %u expected %u\n",
791 return NDR_ERR_SUCCESS
;
795 pull an array length field and add it to the array_length_list token list
797 _PUBLIC_
enum ndr_err_code
ndr_pull_array_length(struct ndr_pull
*ndr
, const void *p
)
799 uint32_t length
, offset
;
800 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &offset
));
802 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
803 "non-zero array offset %u\n", offset
);
805 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &length
));
806 return ndr_token_store(ndr
, &ndr
->array_length_list
, p
, length
);
810 get the stored array length field
812 _PUBLIC_
uint32_t ndr_get_array_length(struct ndr_pull
*ndr
, const void *p
)
814 return ndr_token_peek(&ndr
->array_length_list
, p
);
818 check the stored array length field
820 _PUBLIC_
enum ndr_err_code
ndr_check_array_length(struct ndr_pull
*ndr
, void *p
, uint32_t length
)
823 stored
= ndr_token_peek(&ndr
->array_length_list
, p
);
824 if (stored
!= length
) {
825 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
826 "Bad array length - got %u expected %u\n",
829 return NDR_ERR_SUCCESS
;
832 _PUBLIC_
enum ndr_err_code
ndr_push_pipe_chunk_trailer(struct ndr_push
*ndr
, int ndr_flags
, uint32_t count
)
834 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
835 int64_t tmp
= 0 - (int64_t)count
;
836 uint64_t ncount
= tmp
;
838 NDR_CHECK(ndr_push_hyper(ndr
, ndr_flags
, ncount
));
841 return NDR_ERR_SUCCESS
;
844 _PUBLIC_
enum ndr_err_code
ndr_check_pipe_chunk_trailer(struct ndr_pull
*ndr
, int ndr_flags
, uint32_t count
)
846 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
847 int64_t tmp
= 0 - (int64_t)count
;
848 uint64_t ncount1
= tmp
;
851 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, &ncount2
));
852 if (ncount1
== ncount2
) {
853 return NDR_ERR_SUCCESS
;
856 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
857 "Bad pipe trailer[%lld should be %lld] size was %lu\"",
858 (unsigned long long)ncount2
,
859 (unsigned long long)ncount1
,
860 (unsigned long)count
);
863 return NDR_ERR_SUCCESS
;
869 _PUBLIC_
enum ndr_err_code
ndr_push_set_switch_value(struct ndr_push
*ndr
, const void *p
, uint32_t val
)
871 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
874 _PUBLIC_
enum ndr_err_code
ndr_pull_set_switch_value(struct ndr_pull
*ndr
, const void *p
, uint32_t val
)
876 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
879 _PUBLIC_
enum ndr_err_code
ndr_print_set_switch_value(struct ndr_print
*ndr
, const void *p
, uint32_t val
)
881 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
885 retrieve a switch value
887 _PUBLIC_
uint32_t ndr_push_get_switch_value(struct ndr_push
*ndr
, const void *p
)
889 return ndr_token_peek(&ndr
->switch_list
, p
);
892 _PUBLIC_
uint32_t ndr_pull_get_switch_value(struct ndr_pull
*ndr
, const void *p
)
894 return ndr_token_peek(&ndr
->switch_list
, p
);
897 _PUBLIC_
uint32_t ndr_print_get_switch_value(struct ndr_print
*ndr
, const void *p
)
899 return ndr_token_peek(&ndr
->switch_list
, p
);
903 pull a struct from a blob using NDR
905 _PUBLIC_
enum ndr_err_code
ndr_pull_struct_blob(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
, void *p
,
906 ndr_pull_flags_fn_t fn
)
908 struct ndr_pull
*ndr
;
909 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
910 NDR_ERR_HAVE_NO_MEMORY(ndr
);
911 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
913 return NDR_ERR_SUCCESS
;
917 pull a struct from a blob using NDR - failing if all bytes are not consumed
919 _PUBLIC_
enum ndr_err_code
ndr_pull_struct_blob_all(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
920 void *p
, ndr_pull_flags_fn_t fn
)
922 struct ndr_pull
*ndr
;
923 uint32_t highest_ofs
;
924 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
925 NDR_ERR_HAVE_NO_MEMORY(ndr
);
926 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
927 if (ndr
->offset
> ndr
->relative_highest_offset
) {
928 highest_ofs
= ndr
->offset
;
930 highest_ofs
= ndr
->relative_highest_offset
;
932 if (highest_ofs
< ndr
->data_size
) {
933 enum ndr_err_code ret
;
934 ret
= ndr_pull_error(ndr
, NDR_ERR_UNREAD_BYTES
,
935 "not all bytes consumed ofs[%u] size[%u]",
936 highest_ofs
, ndr
->data_size
);
941 return NDR_ERR_SUCCESS
;
945 pull a union from a blob using NDR, given the union discriminator
947 _PUBLIC_
enum ndr_err_code
ndr_pull_union_blob(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
949 uint32_t level
, ndr_pull_flags_fn_t fn
)
951 struct ndr_pull
*ndr
;
952 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
953 NDR_ERR_HAVE_NO_MEMORY(ndr
);
954 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr
, p
, level
));
955 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
957 return NDR_ERR_SUCCESS
;
961 pull a union from a blob using NDR, given the union discriminator,
962 failing if all bytes are not consumed
964 _PUBLIC_
enum ndr_err_code
ndr_pull_union_blob_all(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
966 uint32_t level
, ndr_pull_flags_fn_t fn
)
968 struct ndr_pull
*ndr
;
969 uint32_t highest_ofs
;
970 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
971 NDR_ERR_HAVE_NO_MEMORY(ndr
);
972 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr
, p
, level
));
973 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
974 if (ndr
->offset
> ndr
->relative_highest_offset
) {
975 highest_ofs
= ndr
->offset
;
977 highest_ofs
= ndr
->relative_highest_offset
;
979 if (highest_ofs
< ndr
->data_size
) {
980 enum ndr_err_code ret
;
981 ret
= ndr_pull_error(ndr
, NDR_ERR_UNREAD_BYTES
,
982 "not all bytes consumed ofs[%u] size[%u]",
983 highest_ofs
, ndr
->data_size
);
988 return NDR_ERR_SUCCESS
;
992 push a struct to a blob using NDR
994 _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
)
996 struct ndr_push
*ndr
;
997 ndr
= ndr_push_init_ctx(mem_ctx
);
998 NDR_ERR_HAVE_NO_MEMORY(ndr
);
1000 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
1002 *blob
= ndr_push_blob(ndr
);
1003 talloc_steal(mem_ctx
, blob
->data
);
1006 return NDR_ERR_SUCCESS
;
1010 push a union to a blob using NDR
1012 _PUBLIC_
enum ndr_err_code
ndr_push_union_blob(DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
, void *p
,
1013 uint32_t level
, ndr_push_flags_fn_t fn
)
1015 struct ndr_push
*ndr
;
1016 ndr
= ndr_push_init_ctx(mem_ctx
);
1017 NDR_ERR_HAVE_NO_MEMORY(ndr
);
1019 NDR_CHECK(ndr_push_set_switch_value(ndr
, p
, level
));
1020 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
1022 *blob
= ndr_push_blob(ndr
);
1023 talloc_steal(mem_ctx
, blob
->data
);
1026 return NDR_ERR_SUCCESS
;
1030 generic ndr_size_*() handler for structures
1032 _PUBLIC_
size_t ndr_size_struct(const void *p
, int flags
, ndr_push_flags_fn_t push
)
1034 struct ndr_push
*ndr
;
1035 enum ndr_err_code status
;
1038 /* avoid recursion */
1039 if (flags
& LIBNDR_FLAG_NO_NDR_SIZE
) return 0;
1041 ndr
= ndr_push_init_ctx(NULL
);
1043 ndr
->flags
|= flags
| LIBNDR_FLAG_NO_NDR_SIZE
;
1044 status
= push(ndr
, NDR_SCALARS
|NDR_BUFFERS
, discard_const(p
));
1045 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
1055 generic ndr_size_*() handler for unions
1057 _PUBLIC_
size_t ndr_size_union(const void *p
, int flags
, uint32_t level
, ndr_push_flags_fn_t push
)
1059 struct ndr_push
*ndr
;
1060 enum ndr_err_code status
;
1063 /* avoid recursion */
1064 if (flags
& LIBNDR_FLAG_NO_NDR_SIZE
) return 0;
1066 ndr
= ndr_push_init_ctx(NULL
);
1068 ndr
->flags
|= flags
| LIBNDR_FLAG_NO_NDR_SIZE
;
1070 status
= ndr_push_set_switch_value(ndr
, p
, level
);
1071 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
1075 status
= push(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
);
1076 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
1086 get the current base for relative pointers for the push
1088 _PUBLIC_
uint32_t ndr_push_get_relative_base_offset(struct ndr_push
*ndr
)
1090 return ndr
->relative_base_offset
;
1094 restore the old base for relative pointers for the push
1096 _PUBLIC_
void ndr_push_restore_relative_base_offset(struct ndr_push
*ndr
, uint32_t offset
)
1098 ndr
->relative_base_offset
= offset
;
1102 setup the current base for relative pointers for the push
1103 called in the NDR_SCALAR stage
1105 _PUBLIC_
enum ndr_err_code
ndr_push_setup_relative_base_offset1(struct ndr_push
*ndr
, const void *p
, uint32_t offset
)
1107 ndr
->relative_base_offset
= offset
;
1108 return ndr_token_store(ndr
, &ndr
->relative_base_list
, p
, offset
);
1112 setup the current base for relative pointers for the push
1113 called in the NDR_BUFFERS stage
1115 _PUBLIC_
enum ndr_err_code
ndr_push_setup_relative_base_offset2(struct ndr_push
*ndr
, const void *p
)
1117 return ndr_token_retrieve(&ndr
->relative_base_list
, p
, &ndr
->relative_base_offset
);
1121 push a relative object - stage1
1122 this is called during SCALARS processing
1124 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr1(struct ndr_push
*ndr
, const void *p
)
1127 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0));
1128 return NDR_ERR_SUCCESS
;
1130 NDR_CHECK(ndr_push_align(ndr
, 4));
1131 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_list
, p
, ndr
->offset
));
1132 return ndr_push_uint32(ndr
, NDR_SCALARS
, 0xFFFFFFFF);
1136 push a short relative object - stage1
1137 this is called during SCALARS processing
1139 _PUBLIC_
enum ndr_err_code
ndr_push_short_relative_ptr1(struct ndr_push
*ndr
, const void *p
)
1142 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, 0));
1143 return NDR_ERR_SUCCESS
;
1145 NDR_CHECK(ndr_push_align(ndr
, 2));
1146 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_list
, p
, ndr
->offset
));
1147 return ndr_push_uint16(ndr
, NDR_SCALARS
, 0xFFFF);
1150 push a relative object - stage2
1151 this is called during buffers processing
1153 static enum ndr_err_code
ndr_push_relative_ptr2(struct ndr_push
*ndr
, const void *p
)
1155 uint32_t save_offset
;
1156 uint32_t ptr_offset
= 0xFFFFFFFF;
1158 return NDR_ERR_SUCCESS
;
1160 save_offset
= ndr
->offset
;
1161 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &ptr_offset
));
1162 if (ptr_offset
> ndr
->offset
) {
1163 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1164 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1165 ptr_offset
, ndr
->offset
);
1167 ndr
->offset
= ptr_offset
;
1168 if (save_offset
< ndr
->relative_base_offset
) {
1169 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1170 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1171 save_offset
, ndr
->relative_base_offset
);
1173 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, save_offset
- ndr
->relative_base_offset
));
1174 ndr
->offset
= save_offset
;
1175 return NDR_ERR_SUCCESS
;
1178 push a short relative object - stage2
1179 this is called during buffers processing
1181 _PUBLIC_
enum ndr_err_code
ndr_push_short_relative_ptr2(struct ndr_push
*ndr
, const void *p
)
1183 uint32_t save_offset
;
1184 uint32_t ptr_offset
= 0xFFFF;
1186 return NDR_ERR_SUCCESS
;
1188 save_offset
= ndr
->offset
;
1189 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &ptr_offset
));
1190 if (ptr_offset
> ndr
->offset
) {
1191 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1192 "ndr_push_short_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1193 ptr_offset
, ndr
->offset
);
1195 ndr
->offset
= ptr_offset
;
1196 if (save_offset
< ndr
->relative_base_offset
) {
1197 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1198 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1199 save_offset
, ndr
->relative_base_offset
);
1201 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, save_offset
- ndr
->relative_base_offset
));
1202 ndr
->offset
= save_offset
;
1203 return NDR_ERR_SUCCESS
;
1207 push a relative object - stage2 start
1208 this is called during buffers processing
1210 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr2_start(struct ndr_push
*ndr
, const void *p
)
1213 return NDR_ERR_SUCCESS
;
1215 if (!(ndr
->flags
& LIBNDR_FLAG_RELATIVE_REVERSE
)) {
1216 uint32_t relative_offset
;
1220 if (ndr
->offset
< ndr
->relative_base_offset
) {
1221 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1222 "ndr_push_relative_ptr2_start ndr->offset(%u) < ndr->relative_base_offset(%u)",
1223 ndr
->offset
, ndr
->relative_base_offset
);
1226 relative_offset
= ndr
->offset
- ndr
->relative_base_offset
;
1228 if (ndr
->flags
& LIBNDR_FLAG_NOALIGN
) {
1230 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
1232 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
1234 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
1238 pad
= ndr_align_size(relative_offset
, align
);
1240 NDR_CHECK(ndr_push_zero(ndr
, pad
));
1243 return ndr_push_relative_ptr2(ndr
, p
);
1245 if (ndr
->relative_end_offset
== -1) {
1246 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1247 "ndr_push_relative_ptr2_start RELATIVE_REVERSE flag set and relative_end_offset %d",
1248 ndr
->relative_end_offset
);
1250 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_begin_list
, p
, ndr
->offset
));
1251 return NDR_ERR_SUCCESS
;
1255 push a relative object - stage2 end
1256 this is called during buffers processing
1258 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr2_end(struct ndr_push
*ndr
, const void *p
)
1260 uint32_t begin_offset
= 0xFFFFFFFF;
1262 uint32_t correct_offset
= 0;
1267 return NDR_ERR_SUCCESS
;
1270 if (!(ndr
->flags
& LIBNDR_FLAG_RELATIVE_REVERSE
)) {
1271 return NDR_ERR_SUCCESS
;
1274 if (ndr
->flags
& LIBNDR_FLAG_NO_NDR_SIZE
) {
1275 /* better say more than calculation a too small buffer */
1276 NDR_PUSH_ALIGN(ndr
, 8);
1277 return NDR_ERR_SUCCESS
;
1280 if (ndr
->relative_end_offset
< ndr
->offset
) {
1281 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1282 "ndr_push_relative_ptr2_end:"
1283 "relative_end_offset %u < offset %u",
1284 ndr
->relative_end_offset
, ndr
->offset
);
1287 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_begin_list
, p
, &begin_offset
));
1289 /* we have marshalled a buffer, see how long it was */
1290 len
= ndr
->offset
- begin_offset
;
1293 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1294 "ndr_push_relative_ptr2_end:"
1295 "offset %u - begin_offset %u < 0",
1296 ndr
->offset
, begin_offset
);
1299 if (ndr
->relative_end_offset
< len
) {
1300 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1301 "ndr_push_relative_ptr2_end:"
1302 "relative_end_offset %u < len %lld",
1303 ndr
->offset
, (long long)len
);
1306 /* the reversed offset is at the end of the main buffer */
1307 correct_offset
= ndr
->relative_end_offset
- len
;
1309 if (ndr
->flags
& LIBNDR_FLAG_NOALIGN
) {
1311 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
1313 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
1315 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
1319 pad
= ndr_align_size(correct_offset
, align
);
1321 correct_offset
+= pad
;
1322 correct_offset
-= align
;
1325 if (correct_offset
< begin_offset
) {
1326 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1327 "ndr_push_relative_ptr2_end: "
1328 "correct_offset %u < begin_offset %u",
1329 correct_offset
, begin_offset
);
1333 uint32_t clear_size
= correct_offset
- begin_offset
;
1335 clear_size
= MIN(clear_size
, len
);
1337 /* now move the marshalled buffer to the end of the main buffer */
1338 memmove(ndr
->data
+ correct_offset
, ndr
->data
+ begin_offset
, len
);
1341 /* and wipe out old buffer within the main buffer */
1342 memset(ndr
->data
+ begin_offset
, '\0', clear_size
);
1346 /* and set the end offset for the next buffer */
1347 ndr
->relative_end_offset
= correct_offset
;
1349 /* finally write the offset to the main buffer */
1350 ndr
->offset
= correct_offset
;
1351 NDR_CHECK(ndr_push_relative_ptr2(ndr
, p
));
1353 /* restore to where we were in the main buffer */
1354 ndr
->offset
= begin_offset
;
1356 return NDR_ERR_SUCCESS
;
1360 get the current base for relative pointers for the pull
1362 _PUBLIC_
uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull
*ndr
)
1364 return ndr
->relative_base_offset
;
1368 restore the old base for relative pointers for the pull
1370 _PUBLIC_
void ndr_pull_restore_relative_base_offset(struct ndr_pull
*ndr
, uint32_t offset
)
1372 ndr
->relative_base_offset
= offset
;
1376 setup the current base for relative pointers for the pull
1377 called in the NDR_SCALAR stage
1379 _PUBLIC_
enum ndr_err_code
ndr_pull_setup_relative_base_offset1(struct ndr_pull
*ndr
, const void *p
, uint32_t offset
)
1381 ndr
->relative_base_offset
= offset
;
1382 return ndr_token_store(ndr
, &ndr
->relative_base_list
, p
, offset
);
1386 setup the current base for relative pointers for the pull
1387 called in the NDR_BUFFERS stage
1389 _PUBLIC_
enum ndr_err_code
ndr_pull_setup_relative_base_offset2(struct ndr_pull
*ndr
, const void *p
)
1391 return ndr_token_retrieve(&ndr
->relative_base_list
, p
, &ndr
->relative_base_offset
);
1395 pull a relative object - stage1
1396 called during SCALARS processing
1398 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr1(struct ndr_pull
*ndr
, const void *p
, uint32_t rel_offset
)
1400 rel_offset
+= ndr
->relative_base_offset
;
1401 if (rel_offset
> ndr
->data_size
) {
1402 return ndr_pull_error(ndr
, NDR_ERR_BUFSIZE
,
1403 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1404 rel_offset
, ndr
->data_size
);
1406 return ndr_token_store(ndr
, &ndr
->relative_list
, p
, rel_offset
);
1410 pull a relative object - stage2
1411 called during BUFFERS processing
1413 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr2(struct ndr_pull
*ndr
, const void *p
)
1415 uint32_t rel_offset
;
1416 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &rel_offset
));
1417 return ndr_pull_set_offset(ndr
, rel_offset
);
1420 const static struct {
1421 enum ndr_err_code err
;
1423 } ndr_err_code_strings
[] = {
1424 { NDR_ERR_SUCCESS
, "Success" },
1425 { NDR_ERR_ARRAY_SIZE
, "Bad Array Size" },
1426 { NDR_ERR_BAD_SWITCH
, "Bad Switch" },
1427 { NDR_ERR_OFFSET
, "Offset Error" },
1428 { NDR_ERR_RELATIVE
, "Relative Pointer Error" },
1429 { NDR_ERR_CHARCNV
, "Character Conversion Error" },
1430 { NDR_ERR_LENGTH
, "Length Error" },
1431 { NDR_ERR_SUBCONTEXT
, "Subcontext Error" },
1432 { NDR_ERR_COMPRESSION
, "Compression Error" },
1433 { NDR_ERR_STRING
, "String Error" },
1434 { NDR_ERR_VALIDATE
, "Validate Error" },
1435 { NDR_ERR_BUFSIZE
, "Buffer Size Error" },
1436 { NDR_ERR_ALLOC
, "Allocation Error" },
1437 { NDR_ERR_RANGE
, "Range Error" },
1438 { NDR_ERR_TOKEN
, "Token Error" },
1439 { NDR_ERR_IPV4ADDRESS
, "IPv4 Address Error" },
1440 { NDR_ERR_INVALID_POINTER
, "Invalid Pointer" },
1441 { NDR_ERR_UNREAD_BYTES
, "Unread Bytes" },
1442 { NDR_ERR_NDR64
, "NDR64 assertion error" },
1446 _PUBLIC_
const char *ndr_map_error2string(enum ndr_err_code ndr_err
)
1449 for (i
= 0; ndr_err_code_strings
[i
].string
!= NULL
; i
++) {
1450 if (ndr_err_code_strings
[i
].err
== ndr_err
)
1451 return ndr_err_code_strings
[i
].string
;
1453 return "Unknown error";