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
;
835 _PUBLIC_
enum ndr_err_code
ndr_push_set_switch_value(struct ndr_push
*ndr
, const void *p
, uint32_t val
)
837 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
840 _PUBLIC_
enum ndr_err_code
ndr_pull_set_switch_value(struct ndr_pull
*ndr
, const void *p
, uint32_t val
)
842 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
845 _PUBLIC_
enum ndr_err_code
ndr_print_set_switch_value(struct ndr_print
*ndr
, const void *p
, uint32_t val
)
847 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
851 retrieve a switch value
853 _PUBLIC_
uint32_t ndr_push_get_switch_value(struct ndr_push
*ndr
, const void *p
)
855 return ndr_token_peek(&ndr
->switch_list
, p
);
858 _PUBLIC_
uint32_t ndr_pull_get_switch_value(struct ndr_pull
*ndr
, const void *p
)
860 return ndr_token_peek(&ndr
->switch_list
, p
);
863 _PUBLIC_
uint32_t ndr_print_get_switch_value(struct ndr_print
*ndr
, const void *p
)
865 return ndr_token_peek(&ndr
->switch_list
, p
);
869 pull a struct from a blob using NDR
871 _PUBLIC_
enum ndr_err_code
ndr_pull_struct_blob(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
, void *p
,
872 ndr_pull_flags_fn_t fn
)
874 struct ndr_pull
*ndr
;
875 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
876 NDR_ERR_HAVE_NO_MEMORY(ndr
);
877 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
879 return NDR_ERR_SUCCESS
;
883 pull a struct from a blob using NDR - failing if all bytes are not consumed
885 _PUBLIC_
enum ndr_err_code
ndr_pull_struct_blob_all(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
886 void *p
, ndr_pull_flags_fn_t fn
)
888 struct ndr_pull
*ndr
;
889 uint32_t highest_ofs
;
890 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
891 NDR_ERR_HAVE_NO_MEMORY(ndr
);
892 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
893 if (ndr
->offset
> ndr
->relative_highest_offset
) {
894 highest_ofs
= ndr
->offset
;
896 highest_ofs
= ndr
->relative_highest_offset
;
898 if (highest_ofs
< ndr
->data_size
) {
899 enum ndr_err_code ret
;
900 ret
= ndr_pull_error(ndr
, NDR_ERR_UNREAD_BYTES
,
901 "not all bytes consumed ofs[%u] size[%u]",
902 highest_ofs
, ndr
->data_size
);
907 return NDR_ERR_SUCCESS
;
911 pull a union from a blob using NDR, given the union discriminator
913 _PUBLIC_
enum ndr_err_code
ndr_pull_union_blob(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
915 uint32_t level
, ndr_pull_flags_fn_t fn
)
917 struct ndr_pull
*ndr
;
918 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
919 NDR_ERR_HAVE_NO_MEMORY(ndr
);
920 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr
, p
, level
));
921 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
923 return NDR_ERR_SUCCESS
;
927 pull a union from a blob using NDR, given the union discriminator,
928 failing if all bytes are not consumed
930 _PUBLIC_
enum ndr_err_code
ndr_pull_union_blob_all(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
932 uint32_t level
, ndr_pull_flags_fn_t fn
)
934 struct ndr_pull
*ndr
;
935 uint32_t highest_ofs
;
936 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
937 NDR_ERR_HAVE_NO_MEMORY(ndr
);
938 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr
, p
, level
));
939 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
940 if (ndr
->offset
> ndr
->relative_highest_offset
) {
941 highest_ofs
= ndr
->offset
;
943 highest_ofs
= ndr
->relative_highest_offset
;
945 if (highest_ofs
< ndr
->data_size
) {
946 enum ndr_err_code ret
;
947 ret
= ndr_pull_error(ndr
, NDR_ERR_UNREAD_BYTES
,
948 "not all bytes consumed ofs[%u] size[%u]",
949 highest_ofs
, ndr
->data_size
);
954 return NDR_ERR_SUCCESS
;
958 push a struct to a blob using NDR
960 _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
)
962 struct ndr_push
*ndr
;
963 ndr
= ndr_push_init_ctx(mem_ctx
);
964 NDR_ERR_HAVE_NO_MEMORY(ndr
);
966 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
968 *blob
= ndr_push_blob(ndr
);
969 talloc_steal(mem_ctx
, blob
->data
);
972 return NDR_ERR_SUCCESS
;
976 push a union to a blob using NDR
978 _PUBLIC_
enum ndr_err_code
ndr_push_union_blob(DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
, void *p
,
979 uint32_t level
, ndr_push_flags_fn_t fn
)
981 struct ndr_push
*ndr
;
982 ndr
= ndr_push_init_ctx(mem_ctx
);
983 NDR_ERR_HAVE_NO_MEMORY(ndr
);
985 NDR_CHECK(ndr_push_set_switch_value(ndr
, p
, level
));
986 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
988 *blob
= ndr_push_blob(ndr
);
989 talloc_steal(mem_ctx
, blob
->data
);
992 return NDR_ERR_SUCCESS
;
996 generic ndr_size_*() handler for structures
998 _PUBLIC_
size_t ndr_size_struct(const void *p
, int flags
, ndr_push_flags_fn_t push
)
1000 struct ndr_push
*ndr
;
1001 enum ndr_err_code status
;
1004 /* avoid recursion */
1005 if (flags
& LIBNDR_FLAG_NO_NDR_SIZE
) return 0;
1007 ndr
= ndr_push_init_ctx(NULL
);
1009 ndr
->flags
|= flags
| LIBNDR_FLAG_NO_NDR_SIZE
;
1010 status
= push(ndr
, NDR_SCALARS
|NDR_BUFFERS
, discard_const(p
));
1011 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
1021 generic ndr_size_*() handler for unions
1023 _PUBLIC_
size_t ndr_size_union(const void *p
, int flags
, uint32_t level
, ndr_push_flags_fn_t push
)
1025 struct ndr_push
*ndr
;
1026 enum ndr_err_code status
;
1029 /* avoid recursion */
1030 if (flags
& LIBNDR_FLAG_NO_NDR_SIZE
) return 0;
1032 ndr
= ndr_push_init_ctx(NULL
);
1034 ndr
->flags
|= flags
| LIBNDR_FLAG_NO_NDR_SIZE
;
1036 status
= ndr_push_set_switch_value(ndr
, p
, level
);
1037 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
1041 status
= push(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
);
1042 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
1052 get the current base for relative pointers for the push
1054 _PUBLIC_
uint32_t ndr_push_get_relative_base_offset(struct ndr_push
*ndr
)
1056 return ndr
->relative_base_offset
;
1060 restore the old base for relative pointers for the push
1062 _PUBLIC_
void ndr_push_restore_relative_base_offset(struct ndr_push
*ndr
, uint32_t offset
)
1064 ndr
->relative_base_offset
= offset
;
1068 setup the current base for relative pointers for the push
1069 called in the NDR_SCALAR stage
1071 _PUBLIC_
enum ndr_err_code
ndr_push_setup_relative_base_offset1(struct ndr_push
*ndr
, const void *p
, uint32_t offset
)
1073 ndr
->relative_base_offset
= offset
;
1074 return ndr_token_store(ndr
, &ndr
->relative_base_list
, p
, offset
);
1078 setup the current base for relative pointers for the push
1079 called in the NDR_BUFFERS stage
1081 _PUBLIC_
enum ndr_err_code
ndr_push_setup_relative_base_offset2(struct ndr_push
*ndr
, const void *p
)
1083 return ndr_token_retrieve(&ndr
->relative_base_list
, p
, &ndr
->relative_base_offset
);
1087 push a relative object - stage1
1088 this is called during SCALARS processing
1090 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr1(struct ndr_push
*ndr
, const void *p
)
1093 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0));
1094 return NDR_ERR_SUCCESS
;
1096 NDR_CHECK(ndr_push_align(ndr
, 4));
1097 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_list
, p
, ndr
->offset
));
1098 return ndr_push_uint32(ndr
, NDR_SCALARS
, 0xFFFFFFFF);
1102 push a short relative object - stage1
1103 this is called during SCALARS processing
1105 _PUBLIC_
enum ndr_err_code
ndr_push_short_relative_ptr1(struct ndr_push
*ndr
, const void *p
)
1108 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, 0));
1109 return NDR_ERR_SUCCESS
;
1111 NDR_CHECK(ndr_push_align(ndr
, 2));
1112 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_list
, p
, ndr
->offset
));
1113 return ndr_push_uint16(ndr
, NDR_SCALARS
, 0xFFFF);
1116 push a relative object - stage2
1117 this is called during buffers processing
1119 static enum ndr_err_code
ndr_push_relative_ptr2(struct ndr_push
*ndr
, const void *p
)
1121 uint32_t save_offset
;
1122 uint32_t ptr_offset
= 0xFFFFFFFF;
1124 return NDR_ERR_SUCCESS
;
1126 save_offset
= ndr
->offset
;
1127 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &ptr_offset
));
1128 if (ptr_offset
> ndr
->offset
) {
1129 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1130 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1131 ptr_offset
, ndr
->offset
);
1133 ndr
->offset
= ptr_offset
;
1134 if (save_offset
< ndr
->relative_base_offset
) {
1135 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1136 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1137 save_offset
, ndr
->relative_base_offset
);
1139 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, save_offset
- ndr
->relative_base_offset
));
1140 ndr
->offset
= save_offset
;
1141 return NDR_ERR_SUCCESS
;
1144 push a short relative object - stage2
1145 this is called during buffers processing
1147 _PUBLIC_
enum ndr_err_code
ndr_push_short_relative_ptr2(struct ndr_push
*ndr
, const void *p
)
1149 uint32_t save_offset
;
1150 uint32_t ptr_offset
= 0xFFFF;
1152 return NDR_ERR_SUCCESS
;
1154 save_offset
= ndr
->offset
;
1155 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &ptr_offset
));
1156 if (ptr_offset
> ndr
->offset
) {
1157 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1158 "ndr_push_short_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1159 ptr_offset
, ndr
->offset
);
1161 ndr
->offset
= ptr_offset
;
1162 if (save_offset
< ndr
->relative_base_offset
) {
1163 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1164 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1165 save_offset
, ndr
->relative_base_offset
);
1167 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, save_offset
- ndr
->relative_base_offset
));
1168 ndr
->offset
= save_offset
;
1169 return NDR_ERR_SUCCESS
;
1173 push a relative object - stage2 start
1174 this is called during buffers processing
1176 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr2_start(struct ndr_push
*ndr
, const void *p
)
1179 return NDR_ERR_SUCCESS
;
1181 if (!(ndr
->flags
& LIBNDR_FLAG_RELATIVE_REVERSE
)) {
1182 return ndr_push_relative_ptr2(ndr
, p
);
1184 if (ndr
->relative_end_offset
== -1) {
1185 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1186 "ndr_push_relative_ptr2_start RELATIVE_REVERSE flag set and relative_end_offset %d",
1187 ndr
->relative_end_offset
);
1189 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_begin_list
, p
, ndr
->offset
));
1190 return NDR_ERR_SUCCESS
;
1194 push a relative object - stage2 end
1195 this is called during buffers processing
1197 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr2_end(struct ndr_push
*ndr
, const void *p
)
1199 uint32_t begin_offset
= 0xFFFFFFFF;
1201 uint32_t correct_offset
= 0;
1206 return NDR_ERR_SUCCESS
;
1209 if (!(ndr
->flags
& LIBNDR_FLAG_RELATIVE_REVERSE
)) {
1210 return NDR_ERR_SUCCESS
;
1213 if (ndr
->flags
& LIBNDR_FLAG_NO_NDR_SIZE
) {
1214 /* better say more than calculation a too small buffer */
1215 NDR_PUSH_ALIGN(ndr
, 8);
1216 return NDR_ERR_SUCCESS
;
1219 if (ndr
->relative_end_offset
< ndr
->offset
) {
1220 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1221 "ndr_push_relative_ptr2_end:"
1222 "relative_end_offset %u < offset %u",
1223 ndr
->relative_end_offset
, ndr
->offset
);
1226 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_begin_list
, p
, &begin_offset
));
1228 /* we have marshalled a buffer, see how long it was */
1229 len
= ndr
->offset
- begin_offset
;
1232 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1233 "ndr_push_relative_ptr2_end:"
1234 "offset %u - begin_offset %u < 0",
1235 ndr
->offset
, begin_offset
);
1238 if (ndr
->relative_end_offset
< len
) {
1239 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1240 "ndr_push_relative_ptr2_end:"
1241 "relative_end_offset %u < len %lld",
1242 ndr
->offset
, (long long)len
);
1245 /* the reversed offset is at the end of the main buffer */
1246 correct_offset
= ndr
->relative_end_offset
- len
;
1248 /* TODO: remove this hack and let the idl use FLAG_ALIGN2 explicit */
1251 if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
1253 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
1255 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
1259 pad
= ndr_align_size(correct_offset
, align
);
1261 correct_offset
+= pad
;
1262 correct_offset
-= align
;
1265 if (correct_offset
< begin_offset
) {
1266 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1267 "ndr_push_relative_ptr2_end: "
1268 "correct_offset %u < begin_offset %u",
1269 correct_offset
, begin_offset
);
1273 uint32_t clear_size
= correct_offset
- begin_offset
;
1275 clear_size
= MIN(clear_size
, len
);
1277 /* now move the marshalled buffer to the end of the main buffer */
1278 memmove(ndr
->data
+ correct_offset
, ndr
->data
+ begin_offset
, len
);
1281 /* and wipe out old buffer within the main buffer */
1282 memset(ndr
->data
+ begin_offset
, '\0', clear_size
);
1286 /* and set the end offset for the next buffer */
1287 ndr
->relative_end_offset
= correct_offset
;
1289 /* finally write the offset to the main buffer */
1290 ndr
->offset
= correct_offset
;
1291 NDR_CHECK(ndr_push_relative_ptr2(ndr
, p
));
1293 /* restore to where we were in the main buffer */
1294 ndr
->offset
= begin_offset
;
1296 return NDR_ERR_SUCCESS
;
1300 get the current base for relative pointers for the pull
1302 _PUBLIC_
uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull
*ndr
)
1304 return ndr
->relative_base_offset
;
1308 restore the old base for relative pointers for the pull
1310 _PUBLIC_
void ndr_pull_restore_relative_base_offset(struct ndr_pull
*ndr
, uint32_t offset
)
1312 ndr
->relative_base_offset
= offset
;
1316 setup the current base for relative pointers for the pull
1317 called in the NDR_SCALAR stage
1319 _PUBLIC_
enum ndr_err_code
ndr_pull_setup_relative_base_offset1(struct ndr_pull
*ndr
, const void *p
, uint32_t offset
)
1321 ndr
->relative_base_offset
= offset
;
1322 return ndr_token_store(ndr
, &ndr
->relative_base_list
, p
, offset
);
1326 setup the current base for relative pointers for the pull
1327 called in the NDR_BUFFERS stage
1329 _PUBLIC_
enum ndr_err_code
ndr_pull_setup_relative_base_offset2(struct ndr_pull
*ndr
, const void *p
)
1331 return ndr_token_retrieve(&ndr
->relative_base_list
, p
, &ndr
->relative_base_offset
);
1335 pull a relative object - stage1
1336 called during SCALARS processing
1338 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr1(struct ndr_pull
*ndr
, const void *p
, uint32_t rel_offset
)
1340 rel_offset
+= ndr
->relative_base_offset
;
1341 if (rel_offset
> ndr
->data_size
) {
1342 return ndr_pull_error(ndr
, NDR_ERR_BUFSIZE
,
1343 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1344 rel_offset
, ndr
->data_size
);
1346 return ndr_token_store(ndr
, &ndr
->relative_list
, p
, rel_offset
);
1350 pull a relative object - stage2
1351 called during BUFFERS processing
1353 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr2(struct ndr_pull
*ndr
, const void *p
)
1355 uint32_t rel_offset
;
1356 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &rel_offset
));
1357 return ndr_pull_set_offset(ndr
, rel_offset
);
1360 const static struct {
1361 enum ndr_err_code err
;
1363 } ndr_err_code_strings
[] = {
1364 { NDR_ERR_SUCCESS
, "Success" },
1365 { NDR_ERR_ARRAY_SIZE
, "Bad Array Size" },
1366 { NDR_ERR_BAD_SWITCH
, "Bad Switch" },
1367 { NDR_ERR_OFFSET
, "Offset Error" },
1368 { NDR_ERR_RELATIVE
, "Relative Pointer Error" },
1369 { NDR_ERR_CHARCNV
, "Character Conversion Error" },
1370 { NDR_ERR_LENGTH
, "Length Error" },
1371 { NDR_ERR_SUBCONTEXT
, "Subcontext Error" },
1372 { NDR_ERR_COMPRESSION
, "Compression Error" },
1373 { NDR_ERR_STRING
, "String Error" },
1374 { NDR_ERR_VALIDATE
, "Validate Error" },
1375 { NDR_ERR_BUFSIZE
, "Buffer Size Error" },
1376 { NDR_ERR_ALLOC
, "Allocation Error" },
1377 { NDR_ERR_RANGE
, "Range Error" },
1378 { NDR_ERR_TOKEN
, "Token Error" },
1379 { NDR_ERR_IPV4ADDRESS
, "IPv4 Address Error" },
1380 { NDR_ERR_INVALID_POINTER
, "Invalid Pointer" },
1381 { NDR_ERR_UNREAD_BYTES
, "Unread Bytes" },
1382 { NDR_ERR_NDR64
, "NDR64 assertion error" },
1386 _PUBLIC_
const char *ndr_map_error2string(enum ndr_err_code ndr_err
)
1389 for (i
= 0; ndr_err_code_strings
[i
].string
!= NULL
; i
++) {
1390 if (ndr_err_code_strings
[i
].err
== ndr_err
)
1391 return ndr_err_code_strings
[i
].string
;
1393 return "Unknown error";