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} },
50 work out the number of bytes needed to align on a n byte boundary
52 _PUBLIC_
size_t ndr_align_size(uint32_t offset
, size_t n
)
54 if ((offset
& (n
-1)) == 0) return 0;
55 return n
- (offset
& (n
-1));
59 initialise a ndr parse structure from a data blob
61 _PUBLIC_
struct ndr_pull
*ndr_pull_init_blob(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
)
65 ndr
= talloc_zero(mem_ctx
, struct ndr_pull
);
66 if (!ndr
) return NULL
;
67 ndr
->current_mem_ctx
= mem_ctx
;
69 ndr
->data
= blob
->data
;
70 ndr
->data_size
= blob
->length
;
76 advance by 'size' bytes
78 _PUBLIC_
enum ndr_err_code
ndr_pull_advance(struct ndr_pull
*ndr
, uint32_t size
)
81 if (ndr
->offset
> ndr
->data_size
) {
82 return ndr_pull_error(ndr
, NDR_ERR_BUFSIZE
,
83 "ndr_pull_advance by %u failed",
86 return NDR_ERR_SUCCESS
;
90 set the parse offset to 'ofs'
92 static enum ndr_err_code
ndr_pull_set_offset(struct ndr_pull
*ndr
, uint32_t ofs
)
95 if (ndr
->offset
> ndr
->data_size
) {
96 return ndr_pull_error(ndr
, NDR_ERR_BUFSIZE
,
97 "ndr_pull_set_offset %u failed",
100 return NDR_ERR_SUCCESS
;
103 /* create a ndr_push structure, ready for some marshalling */
104 _PUBLIC_
struct ndr_push
*ndr_push_init_ctx(TALLOC_CTX
*mem_ctx
)
106 struct ndr_push
*ndr
;
108 ndr
= talloc_zero(mem_ctx
, struct ndr_push
);
114 ndr
->alloc_size
= NDR_BASE_MARSHALL_SIZE
;
115 ndr
->data
= talloc_array(ndr
, uint8_t, ndr
->alloc_size
);
123 /* return a DATA_BLOB structure for the current ndr_push marshalled data */
124 _PUBLIC_ DATA_BLOB
ndr_push_blob(struct ndr_push
*ndr
)
127 blob
= data_blob_const(ndr
->data
, ndr
->offset
);
128 if (ndr
->alloc_size
> ndr
->offset
) {
129 ndr
->data
[ndr
->offset
] = 0;
136 expand the available space in the buffer to ndr->offset + extra_size
138 _PUBLIC_
enum ndr_err_code
ndr_push_expand(struct ndr_push
*ndr
, uint32_t extra_size
)
140 uint32_t size
= extra_size
+ ndr
->offset
;
142 if (size
< ndr
->offset
) {
143 /* extra_size overflowed the offset */
144 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
, "Overflow in push_expand to %u",
148 if (ndr
->alloc_size
> size
) {
149 return NDR_ERR_SUCCESS
;
152 ndr
->alloc_size
+= NDR_BASE_MARSHALL_SIZE
;
153 if (size
+1 > ndr
->alloc_size
) {
154 ndr
->alloc_size
= size
+1;
156 ndr
->data
= talloc_realloc(ndr
, ndr
->data
, uint8_t, ndr
->alloc_size
);
158 return ndr_push_error(ndr
, NDR_ERR_ALLOC
, "Failed to push_expand to %u",
162 return NDR_ERR_SUCCESS
;
165 _PUBLIC_
void ndr_print_debug_helper(struct ndr_print
*ndr
, const char *format
, ...)
172 va_start(ap
, format
);
173 ret
= vasprintf(&s
, format
, ap
);
180 if (ndr
->no_newline
) {
181 DEBUGADD(1,("%s", s
));
186 for (i
=0;i
<ndr
->depth
;i
++) {
190 DEBUGADD(1,("%s\n", s
));
194 _PUBLIC_
void ndr_print_printf_helper(struct ndr_print
*ndr
, const char *format
, ...)
199 if (!ndr
->no_newline
) {
200 for (i
=0;i
<ndr
->depth
;i
++) {
205 va_start(ap
, format
);
208 if (!ndr
->no_newline
) {
213 _PUBLIC_
void ndr_print_string_helper(struct ndr_print
*ndr
, const char *format
, ...)
218 if (!ndr
->no_newline
) {
219 for (i
=0;i
<ndr
->depth
;i
++) {
220 ndr
->private_data
= talloc_asprintf_append_buffer(
221 (char *)ndr
->private_data
, " ");
225 va_start(ap
, format
);
226 ndr
->private_data
= talloc_vasprintf_append_buffer((char *)ndr
->private_data
,
229 if (!ndr
->no_newline
) {
230 ndr
->private_data
= talloc_asprintf_append_buffer((char *)ndr
->private_data
,
236 a useful helper function for printing idl structures via DEBUG()
238 _PUBLIC_
void ndr_print_debug(ndr_print_fn_t fn
, const char *name
, void *ptr
)
240 struct ndr_print
*ndr
;
244 ndr
= talloc_zero(NULL
, struct ndr_print
);
246 ndr
->print
= ndr_print_debug_helper
;
254 a useful helper function for printing idl unions via DEBUG()
256 _PUBLIC_
void ndr_print_union_debug(ndr_print_fn_t fn
, const char *name
, uint32_t level
, void *ptr
)
258 struct ndr_print
*ndr
;
262 ndr
= talloc_zero(NULL
, struct ndr_print
);
264 ndr
->print
= ndr_print_debug_helper
;
267 ndr_print_set_switch_value(ndr
, ptr
, level
);
273 a useful helper function for printing idl function calls via DEBUG()
275 _PUBLIC_
void ndr_print_function_debug(ndr_print_function_t fn
, const char *name
, int flags
, void *ptr
)
277 struct ndr_print
*ndr
;
281 ndr
= talloc_zero(NULL
, struct ndr_print
);
283 ndr
->print
= ndr_print_debug_helper
;
287 fn(ndr
, name
, flags
, ptr
);
292 a useful helper function for printing idl structures to a string
294 _PUBLIC_
char *ndr_print_struct_string(TALLOC_CTX
*mem_ctx
, ndr_print_fn_t fn
, const char *name
, void *ptr
)
296 struct ndr_print
*ndr
;
299 ndr
= talloc_zero(mem_ctx
, struct ndr_print
);
300 if (!ndr
) return NULL
;
301 ndr
->private_data
= talloc_strdup(ndr
, "");
302 if (!ndr
->private_data
) {
305 ndr
->print
= ndr_print_string_helper
;
310 ret
= talloc_steal(mem_ctx
, (char *)ndr
->private_data
);
317 a useful helper function for printing idl unions to a string
319 _PUBLIC_
char *ndr_print_union_string(TALLOC_CTX
*mem_ctx
, ndr_print_fn_t fn
, const char *name
, uint32_t level
, void *ptr
)
321 struct ndr_print
*ndr
;
324 ndr
= talloc_zero(mem_ctx
, struct ndr_print
);
325 if (!ndr
) return NULL
;
326 ndr
->private_data
= talloc_strdup(ndr
, "");
327 if (!ndr
->private_data
) {
330 ndr
->print
= ndr_print_string_helper
;
333 ndr_print_set_switch_value(ndr
, ptr
, level
);
335 ret
= talloc_steal(mem_ctx
, (char *)ndr
->private_data
);
342 a useful helper function for printing idl function calls to a string
344 _PUBLIC_
char *ndr_print_function_string(TALLOC_CTX
*mem_ctx
,
345 ndr_print_function_t fn
, const char *name
,
346 int flags
, void *ptr
)
348 struct ndr_print
*ndr
;
351 ndr
= talloc_zero(mem_ctx
, struct ndr_print
);
352 if (!ndr
) return NULL
;
353 ndr
->private_data
= talloc_strdup(ndr
, "");
354 if (!ndr
->private_data
) {
357 ndr
->print
= ndr_print_string_helper
;
360 fn(ndr
, name
, flags
, ptr
);
361 ret
= talloc_steal(mem_ctx
, (char *)ndr
->private_data
);
367 _PUBLIC_
void ndr_set_flags(uint32_t *pflags
, uint32_t new_flags
)
369 /* the big/little endian flags are inter-dependent */
370 if (new_flags
& LIBNDR_FLAG_LITTLE_ENDIAN
) {
371 (*pflags
) &= ~LIBNDR_FLAG_BIGENDIAN
;
372 (*pflags
) &= ~LIBNDR_FLAG_NDR64
;
374 if (new_flags
& LIBNDR_FLAG_BIGENDIAN
) {
375 (*pflags
) &= ~LIBNDR_FLAG_LITTLE_ENDIAN
;
376 (*pflags
) &= ~LIBNDR_FLAG_NDR64
;
378 if (new_flags
& LIBNDR_FLAG_REMAINING
) {
379 (*pflags
) &= ~LIBNDR_ALIGN_FLAGS
;
381 if (new_flags
& LIBNDR_ALIGN_FLAGS
) {
382 (*pflags
) &= ~LIBNDR_FLAG_REMAINING
;
384 if (new_flags
& LIBNDR_FLAG_NO_RELATIVE_REVERSE
) {
385 (*pflags
) &= ~LIBNDR_FLAG_RELATIVE_REVERSE
;
387 (*pflags
) |= new_flags
;
391 return and possibly log an NDR error
393 _PUBLIC_
enum ndr_err_code
ndr_pull_error(struct ndr_pull
*ndr
,
394 enum ndr_err_code ndr_err
,
395 const char *format
, ...)
401 va_start(ap
, format
);
402 ret
= vasprintf(&s
, format
, ap
);
406 return NDR_ERR_ALLOC
;
409 DEBUG(1,("ndr_pull_error(%u): %s\n", ndr_err
, s
));
417 return and possibly log an NDR error
419 _PUBLIC_
enum ndr_err_code
ndr_push_error(struct ndr_push
*ndr
,
420 enum ndr_err_code ndr_err
,
421 const char *format
, ...)
427 va_start(ap
, format
);
428 ret
= vasprintf(&s
, format
, ap
);
432 return NDR_ERR_ALLOC
;
435 DEBUG(1,("ndr_push_error(%u): %s\n", ndr_err
, s
));
443 handle subcontext buffers, which in midl land are user-marshalled, but
444 we use magic in pidl to make them easier to cope with
446 _PUBLIC_
enum ndr_err_code
ndr_pull_subcontext_start(struct ndr_pull
*ndr
,
447 struct ndr_pull
**_subndr
,
451 struct ndr_pull
*subndr
;
452 uint32_t r_content_size
;
453 bool force_le
= false;
454 bool force_be
= false;
456 switch (header_size
) {
458 uint32_t content_size
= ndr
->data_size
- ndr
->offset
;
460 content_size
= size_is
;
462 r_content_size
= content_size
;
467 uint16_t content_size
;
468 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, &content_size
));
469 if (size_is
>= 0 && size_is
!= content_size
) {
470 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
471 (int)size_is
, (int)content_size
);
473 r_content_size
= content_size
;
478 uint32_t content_size
;
479 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &content_size
));
480 if (size_is
>= 0 && size_is
!= content_size
) {
481 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
482 (int)size_is
, (int)content_size
);
484 r_content_size
= content_size
;
489 * Common Type Header for the Serialization Stream
490 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
496 uint32_t content_size
;
500 NDR_CHECK(ndr_pull_uint8(ndr
, NDR_SCALARS
, &version
));
503 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
504 "Bad subcontext (PULL) Common Type Header version %d != 1",
512 NDR_CHECK(ndr_pull_uint8(ndr
, NDR_SCALARS
, &drep
));
515 } else if (drep
== 0x00) {
518 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
519 "Bad subcontext (PULL) Common Type Header invalid drep 0x%02X",
523 /* length of the "Private Header for Constructed Type" */
524 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, &hdrlen
));
526 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
527 "Bad subcontext (PULL) Common Type Header length %d != 8",
531 /* filler should be ignored */
532 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &filler
));
535 * Private Header for Constructed Type
537 /* length - will be updated latter */
538 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &content_size
));
539 if (size_is
>= 0 && size_is
!= content_size
) {
540 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
541 (int)size_is
, (int)content_size
);
543 /* the content size must be a multiple of 8 */
544 if ((content_size
% 8) != 0) {
545 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
546 "Bad subcontext (PULL) size_is(%d) not padded to 8 content_size %d",
547 (int)size_is
, (int)content_size
);
549 r_content_size
= content_size
;
552 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &reserved
));
556 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) header_size %d",
560 NDR_PULL_NEED_BYTES(ndr
, r_content_size
);
562 subndr
= talloc_zero(ndr
, struct ndr_pull
);
563 NDR_ERR_HAVE_NO_MEMORY(subndr
);
564 subndr
->flags
= ndr
->flags
& ~LIBNDR_FLAG_NDR64
;
565 subndr
->current_mem_ctx
= ndr
->current_mem_ctx
;
567 subndr
->data
= ndr
->data
+ ndr
->offset
;
569 subndr
->data_size
= r_content_size
;
572 ndr_set_flags(&ndr
->flags
, LIBNDR_FLAG_LITTLE_ENDIAN
);
573 } else if (force_be
) {
574 ndr_set_flags(&ndr
->flags
, LIBNDR_FLAG_BIGENDIAN
);
578 return NDR_ERR_SUCCESS
;
581 _PUBLIC_
enum ndr_err_code
ndr_pull_subcontext_end(struct ndr_pull
*ndr
,
582 struct ndr_pull
*subndr
,
589 } else if (header_size
> 0) {
590 advance
= subndr
->data_size
;
592 advance
= subndr
->offset
;
594 NDR_CHECK(ndr_pull_advance(ndr
, advance
));
595 return NDR_ERR_SUCCESS
;
598 _PUBLIC_
enum ndr_err_code
ndr_push_subcontext_start(struct ndr_push
*ndr
,
599 struct ndr_push
**_subndr
,
603 struct ndr_push
*subndr
;
605 subndr
= ndr_push_init_ctx(ndr
);
606 NDR_ERR_HAVE_NO_MEMORY(subndr
);
607 subndr
->flags
= ndr
->flags
& ~LIBNDR_FLAG_NDR64
;
610 NDR_CHECK(ndr_push_zero(subndr
, size_is
));
612 subndr
->relative_end_offset
= size_is
;
616 return NDR_ERR_SUCCESS
;
620 push a subcontext header
622 _PUBLIC_
enum ndr_err_code
ndr_push_subcontext_end(struct ndr_push
*ndr
,
623 struct ndr_push
*subndr
,
630 padding_len
= size_is
- subndr
->offset
;
631 if (padding_len
< 0) {
632 return ndr_push_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
633 (int)subndr
->offset
, (int)size_is
);
635 subndr
->offset
= size_is
;
638 switch (header_size
) {
643 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, subndr
->offset
));
647 NDR_CHECK(ndr_push_uint3264(ndr
, NDR_SCALARS
, subndr
->offset
));
652 * Common Type Header for the Serialization Stream
653 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
655 padding_len
= NDR_ROUND(subndr
->offset
, 8) - subndr
->offset
;
656 if (padding_len
> 0) {
657 NDR_CHECK(ndr_push_zero(subndr
, padding_len
));
661 NDR_CHECK(ndr_push_uint8(ndr
, NDR_SCALARS
, 1));
667 NDR_CHECK(ndr_push_uint8(ndr
, NDR_SCALARS
, NDR_BE(ndr
)?0x00:0x10));
669 /* length of the "Private Header for Constructed Type" */
670 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, 8));
673 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0xCCCCCCCC));
676 * Private Header for Constructed Type
678 /* length - will be updated latter */
679 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, subndr
->offset
));
682 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0));
686 return ndr_push_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext header size %d",
690 NDR_CHECK(ndr_push_bytes(ndr
, subndr
->data
, subndr
->offset
));
691 return NDR_ERR_SUCCESS
;
695 store a token in the ndr context, for later retrieval
697 _PUBLIC_
enum ndr_err_code
ndr_token_store(TALLOC_CTX
*mem_ctx
,
698 struct ndr_token_list
**list
,
702 struct ndr_token_list
*tok
;
703 tok
= talloc(mem_ctx
, struct ndr_token_list
);
704 NDR_ERR_HAVE_NO_MEMORY(tok
);
707 DLIST_ADD((*list
), tok
);
708 return NDR_ERR_SUCCESS
;
712 retrieve a token from a ndr context, using cmp_fn to match the tokens
714 _PUBLIC_
enum ndr_err_code
ndr_token_retrieve_cmp_fn(struct ndr_token_list
**list
, const void *key
, uint32_t *v
,
715 comparison_fn_t _cmp_fn
, bool _remove_tok
)
717 struct ndr_token_list
*tok
;
718 for (tok
=*list
;tok
;tok
=tok
->next
) {
719 if (_cmp_fn
&& _cmp_fn(tok
->key
,key
)==0) goto found
;
720 else if (!_cmp_fn
&& tok
->key
== key
) goto found
;
722 return NDR_ERR_TOKEN
;
726 DLIST_REMOVE((*list
), tok
);
729 return NDR_ERR_SUCCESS
;
733 retrieve a token from a ndr context
735 _PUBLIC_
enum ndr_err_code
ndr_token_retrieve(struct ndr_token_list
**list
, const void *key
, uint32_t *v
)
737 return ndr_token_retrieve_cmp_fn(list
, key
, v
, NULL
, true);
741 peek at but don't removed a token from a ndr context
743 _PUBLIC_
uint32_t ndr_token_peek(struct ndr_token_list
**list
, const void *key
)
745 enum ndr_err_code status
;
748 status
= ndr_token_retrieve_cmp_fn(list
, key
, &v
, NULL
, false);
749 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
757 pull an array size field and add it to the array_size_list token list
759 _PUBLIC_
enum ndr_err_code
ndr_pull_array_size(struct ndr_pull
*ndr
, const void *p
)
762 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &size
));
763 return ndr_token_store(ndr
, &ndr
->array_size_list
, p
, size
);
767 get the stored array size field
769 _PUBLIC_
uint32_t ndr_get_array_size(struct ndr_pull
*ndr
, const void *p
)
771 return ndr_token_peek(&ndr
->array_size_list
, p
);
775 check the stored array size field
777 _PUBLIC_
enum ndr_err_code
ndr_check_array_size(struct ndr_pull
*ndr
, void *p
, uint32_t size
)
780 stored
= ndr_token_peek(&ndr
->array_size_list
, p
);
781 if (stored
!= size
) {
782 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
783 "Bad array size - got %u expected %u\n",
786 return NDR_ERR_SUCCESS
;
790 pull an array length field and add it to the array_length_list token list
792 _PUBLIC_
enum ndr_err_code
ndr_pull_array_length(struct ndr_pull
*ndr
, const void *p
)
794 uint32_t length
, offset
;
795 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &offset
));
797 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
798 "non-zero array offset %u\n", offset
);
800 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &length
));
801 return ndr_token_store(ndr
, &ndr
->array_length_list
, p
, length
);
805 get the stored array length field
807 _PUBLIC_
uint32_t ndr_get_array_length(struct ndr_pull
*ndr
, const void *p
)
809 return ndr_token_peek(&ndr
->array_length_list
, p
);
813 check the stored array length field
815 _PUBLIC_
enum ndr_err_code
ndr_check_array_length(struct ndr_pull
*ndr
, void *p
, uint32_t length
)
818 stored
= ndr_token_peek(&ndr
->array_length_list
, p
);
819 if (stored
!= length
) {
820 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
821 "Bad array length - got %u expected %u\n",
824 return NDR_ERR_SUCCESS
;
830 _PUBLIC_
enum ndr_err_code
ndr_push_set_switch_value(struct ndr_push
*ndr
, const void *p
, uint32_t val
)
832 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
835 _PUBLIC_
enum ndr_err_code
ndr_pull_set_switch_value(struct ndr_pull
*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_print_set_switch_value(struct ndr_print
*ndr
, const void *p
, uint32_t val
)
842 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
846 retrieve a switch value
848 _PUBLIC_
uint32_t ndr_push_get_switch_value(struct ndr_push
*ndr
, const void *p
)
850 return ndr_token_peek(&ndr
->switch_list
, p
);
853 _PUBLIC_
uint32_t ndr_pull_get_switch_value(struct ndr_pull
*ndr
, const void *p
)
855 return ndr_token_peek(&ndr
->switch_list
, p
);
858 _PUBLIC_
uint32_t ndr_print_get_switch_value(struct ndr_print
*ndr
, const void *p
)
860 return ndr_token_peek(&ndr
->switch_list
, p
);
864 pull a struct from a blob using NDR
866 _PUBLIC_
enum ndr_err_code
ndr_pull_struct_blob(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
, void *p
,
867 ndr_pull_flags_fn_t fn
)
869 struct ndr_pull
*ndr
;
870 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
871 NDR_ERR_HAVE_NO_MEMORY(ndr
);
872 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
874 return NDR_ERR_SUCCESS
;
878 pull a struct from a blob using NDR - failing if all bytes are not consumed
880 _PUBLIC_
enum ndr_err_code
ndr_pull_struct_blob_all(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
881 void *p
, ndr_pull_flags_fn_t fn
)
883 struct ndr_pull
*ndr
;
884 uint32_t highest_ofs
;
885 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
886 NDR_ERR_HAVE_NO_MEMORY(ndr
);
887 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
888 if (ndr
->offset
> ndr
->relative_highest_offset
) {
889 highest_ofs
= ndr
->offset
;
891 highest_ofs
= ndr
->relative_highest_offset
;
893 if (highest_ofs
< ndr
->data_size
) {
894 enum ndr_err_code ret
;
895 ret
= ndr_pull_error(ndr
, NDR_ERR_UNREAD_BYTES
,
896 "not all bytes consumed ofs[%u] size[%u]",
897 highest_ofs
, ndr
->data_size
);
902 return NDR_ERR_SUCCESS
;
906 pull a union from a blob using NDR, given the union discriminator
908 _PUBLIC_
enum ndr_err_code
ndr_pull_union_blob(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
910 uint32_t level
, ndr_pull_flags_fn_t fn
)
912 struct ndr_pull
*ndr
;
913 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
914 NDR_ERR_HAVE_NO_MEMORY(ndr
);
915 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr
, p
, level
));
916 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
918 return NDR_ERR_SUCCESS
;
922 pull a union from a blob using NDR, given the union discriminator,
923 failing if all bytes are not consumed
925 _PUBLIC_
enum ndr_err_code
ndr_pull_union_blob_all(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
927 uint32_t level
, ndr_pull_flags_fn_t fn
)
929 struct ndr_pull
*ndr
;
930 uint32_t highest_ofs
;
931 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
932 NDR_ERR_HAVE_NO_MEMORY(ndr
);
933 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr
, p
, level
));
934 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
935 if (ndr
->offset
> ndr
->relative_highest_offset
) {
936 highest_ofs
= ndr
->offset
;
938 highest_ofs
= ndr
->relative_highest_offset
;
940 if (highest_ofs
< ndr
->data_size
) {
941 enum ndr_err_code ret
;
942 ret
= ndr_pull_error(ndr
, NDR_ERR_UNREAD_BYTES
,
943 "not all bytes consumed ofs[%u] size[%u]",
944 highest_ofs
, ndr
->data_size
);
949 return NDR_ERR_SUCCESS
;
953 push a struct to a blob using NDR
955 _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
)
957 struct ndr_push
*ndr
;
958 ndr
= ndr_push_init_ctx(mem_ctx
);
959 NDR_ERR_HAVE_NO_MEMORY(ndr
);
961 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
963 *blob
= ndr_push_blob(ndr
);
964 talloc_steal(mem_ctx
, blob
->data
);
967 return NDR_ERR_SUCCESS
;
971 push a union to a blob using NDR
973 _PUBLIC_
enum ndr_err_code
ndr_push_union_blob(DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
, void *p
,
974 uint32_t level
, ndr_push_flags_fn_t fn
)
976 struct ndr_push
*ndr
;
977 ndr
= ndr_push_init_ctx(mem_ctx
);
978 NDR_ERR_HAVE_NO_MEMORY(ndr
);
980 NDR_CHECK(ndr_push_set_switch_value(ndr
, p
, level
));
981 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
983 *blob
= ndr_push_blob(ndr
);
984 talloc_steal(mem_ctx
, blob
->data
);
987 return NDR_ERR_SUCCESS
;
991 generic ndr_size_*() handler for structures
993 _PUBLIC_
size_t ndr_size_struct(const void *p
, int flags
, ndr_push_flags_fn_t push
)
995 struct ndr_push
*ndr
;
996 enum ndr_err_code status
;
999 /* avoid recursion */
1000 if (flags
& LIBNDR_FLAG_NO_NDR_SIZE
) return 0;
1002 ndr
= ndr_push_init_ctx(NULL
);
1004 ndr
->flags
|= flags
| LIBNDR_FLAG_NO_NDR_SIZE
;
1005 status
= push(ndr
, NDR_SCALARS
|NDR_BUFFERS
, discard_const(p
));
1006 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
1016 generic ndr_size_*() handler for unions
1018 _PUBLIC_
size_t ndr_size_union(const void *p
, int flags
, uint32_t level
, ndr_push_flags_fn_t push
)
1020 struct ndr_push
*ndr
;
1021 enum ndr_err_code status
;
1024 /* avoid recursion */
1025 if (flags
& LIBNDR_FLAG_NO_NDR_SIZE
) return 0;
1027 ndr
= ndr_push_init_ctx(NULL
);
1029 ndr
->flags
|= flags
| LIBNDR_FLAG_NO_NDR_SIZE
;
1031 status
= ndr_push_set_switch_value(ndr
, p
, level
);
1032 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
1036 status
= push(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
);
1037 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
1047 get the current base for relative pointers for the push
1049 _PUBLIC_
uint32_t ndr_push_get_relative_base_offset(struct ndr_push
*ndr
)
1051 return ndr
->relative_base_offset
;
1055 restore the old base for relative pointers for the push
1057 _PUBLIC_
void ndr_push_restore_relative_base_offset(struct ndr_push
*ndr
, uint32_t offset
)
1059 ndr
->relative_base_offset
= offset
;
1063 setup the current base for relative pointers for the push
1064 called in the NDR_SCALAR stage
1066 _PUBLIC_
enum ndr_err_code
ndr_push_setup_relative_base_offset1(struct ndr_push
*ndr
, const void *p
, uint32_t offset
)
1068 ndr
->relative_base_offset
= offset
;
1069 return ndr_token_store(ndr
, &ndr
->relative_base_list
, p
, offset
);
1073 setup the current base for relative pointers for the push
1074 called in the NDR_BUFFERS stage
1076 _PUBLIC_
enum ndr_err_code
ndr_push_setup_relative_base_offset2(struct ndr_push
*ndr
, const void *p
)
1078 return ndr_token_retrieve(&ndr
->relative_base_list
, p
, &ndr
->relative_base_offset
);
1082 push a relative object - stage1
1083 this is called during SCALARS processing
1085 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr1(struct ndr_push
*ndr
, const void *p
)
1088 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0));
1089 return NDR_ERR_SUCCESS
;
1091 NDR_CHECK(ndr_push_align(ndr
, 4));
1092 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_list
, p
, ndr
->offset
));
1093 return ndr_push_uint32(ndr
, NDR_SCALARS
, 0xFFFFFFFF);
1097 push a short relative object - stage1
1098 this is called during SCALARS processing
1100 _PUBLIC_
enum ndr_err_code
ndr_push_short_relative_ptr1(struct ndr_push
*ndr
, const void *p
)
1103 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, 0));
1104 return NDR_ERR_SUCCESS
;
1106 NDR_CHECK(ndr_push_align(ndr
, 2));
1107 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_list
, p
, ndr
->offset
));
1108 return ndr_push_uint16(ndr
, NDR_SCALARS
, 0xFFFF);
1111 push a relative object - stage2
1112 this is called during buffers processing
1114 static enum ndr_err_code
ndr_push_relative_ptr2(struct ndr_push
*ndr
, const void *p
)
1116 uint32_t save_offset
;
1117 uint32_t ptr_offset
= 0xFFFFFFFF;
1119 return NDR_ERR_SUCCESS
;
1121 save_offset
= ndr
->offset
;
1122 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &ptr_offset
));
1123 if (ptr_offset
> ndr
->offset
) {
1124 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1125 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1126 ptr_offset
, ndr
->offset
);
1128 ndr
->offset
= ptr_offset
;
1129 if (save_offset
< ndr
->relative_base_offset
) {
1130 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1131 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1132 save_offset
, ndr
->relative_base_offset
);
1134 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, save_offset
- ndr
->relative_base_offset
));
1135 ndr
->offset
= save_offset
;
1136 return NDR_ERR_SUCCESS
;
1139 push a short relative object - stage2
1140 this is called during buffers processing
1142 _PUBLIC_
enum ndr_err_code
ndr_push_short_relative_ptr2(struct ndr_push
*ndr
, const void *p
)
1144 uint32_t save_offset
;
1145 uint32_t ptr_offset
= 0xFFFF;
1147 return NDR_ERR_SUCCESS
;
1149 save_offset
= ndr
->offset
;
1150 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &ptr_offset
));
1151 if (ptr_offset
> ndr
->offset
) {
1152 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1153 "ndr_push_short_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1154 ptr_offset
, ndr
->offset
);
1156 ndr
->offset
= ptr_offset
;
1157 if (save_offset
< ndr
->relative_base_offset
) {
1158 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1159 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1160 save_offset
, ndr
->relative_base_offset
);
1162 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, save_offset
- ndr
->relative_base_offset
));
1163 ndr
->offset
= save_offset
;
1164 return NDR_ERR_SUCCESS
;
1168 push a relative object - stage2 start
1169 this is called during buffers processing
1171 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr2_start(struct ndr_push
*ndr
, const void *p
)
1174 return NDR_ERR_SUCCESS
;
1176 if (!(ndr
->flags
& LIBNDR_FLAG_RELATIVE_REVERSE
)) {
1177 return ndr_push_relative_ptr2(ndr
, p
);
1179 if (ndr
->relative_end_offset
== -1) {
1180 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1181 "ndr_push_relative_ptr2_start RELATIVE_REVERSE flag set and relative_end_offset %d",
1182 ndr
->relative_end_offset
);
1184 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_begin_list
, p
, ndr
->offset
));
1185 return NDR_ERR_SUCCESS
;
1189 push a relative object - stage2 end
1190 this is called during buffers processing
1192 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr2_end(struct ndr_push
*ndr
, const void *p
)
1194 uint32_t begin_offset
= 0xFFFFFFFF;
1196 uint32_t correct_offset
= 0;
1201 return NDR_ERR_SUCCESS
;
1204 if (!(ndr
->flags
& LIBNDR_FLAG_RELATIVE_REVERSE
)) {
1205 return NDR_ERR_SUCCESS
;
1208 if (ndr
->flags
& LIBNDR_FLAG_NO_NDR_SIZE
) {
1209 /* better say more than calculation a too small buffer */
1210 NDR_PUSH_ALIGN(ndr
, 8);
1211 return NDR_ERR_SUCCESS
;
1214 if (ndr
->relative_end_offset
< ndr
->offset
) {
1215 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1216 "ndr_push_relative_ptr2_end:"
1217 "relative_end_offset %u < offset %u",
1218 ndr
->relative_end_offset
, ndr
->offset
);
1221 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_begin_list
, p
, &begin_offset
));
1223 /* we have marshalled a buffer, see how long it was */
1224 len
= ndr
->offset
- begin_offset
;
1227 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1228 "ndr_push_relative_ptr2_end:"
1229 "offset %u - begin_offset %u < 0",
1230 ndr
->offset
, begin_offset
);
1233 if (ndr
->relative_end_offset
< len
) {
1234 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1235 "ndr_push_relative_ptr2_end:"
1236 "relative_end_offset %u < len %lld",
1237 ndr
->offset
, (long long)len
);
1240 /* the reversed offset is at the end of the main buffer */
1241 correct_offset
= ndr
->relative_end_offset
- len
;
1243 /* TODO: remove this hack and let the idl use FLAG_ALIGN2 explicit */
1246 if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
1248 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
1250 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
1254 pad
= ndr_align_size(correct_offset
, align
);
1256 correct_offset
+= pad
;
1257 correct_offset
-= align
;
1260 if (correct_offset
< begin_offset
) {
1261 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1262 "ndr_push_relative_ptr2_end: "
1263 "correct_offset %u < begin_offset %u",
1264 correct_offset
, begin_offset
);
1268 uint32_t clear_size
= correct_offset
- begin_offset
;
1270 clear_size
= MIN(clear_size
, len
);
1272 /* now move the marshalled buffer to the end of the main buffer */
1273 memmove(ndr
->data
+ correct_offset
, ndr
->data
+ begin_offset
, len
);
1276 /* and wipe out old buffer within the main buffer */
1277 memset(ndr
->data
+ begin_offset
, '\0', clear_size
);
1281 /* and set the end offset for the next buffer */
1282 ndr
->relative_end_offset
= correct_offset
;
1284 /* finally write the offset to the main buffer */
1285 ndr
->offset
= correct_offset
;
1286 NDR_CHECK(ndr_push_relative_ptr2(ndr
, p
));
1288 /* restore to where we were in the main buffer */
1289 ndr
->offset
= begin_offset
;
1291 return NDR_ERR_SUCCESS
;
1295 get the current base for relative pointers for the pull
1297 _PUBLIC_
uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull
*ndr
)
1299 return ndr
->relative_base_offset
;
1303 restore the old base for relative pointers for the pull
1305 _PUBLIC_
void ndr_pull_restore_relative_base_offset(struct ndr_pull
*ndr
, uint32_t offset
)
1307 ndr
->relative_base_offset
= offset
;
1311 setup the current base for relative pointers for the pull
1312 called in the NDR_SCALAR stage
1314 _PUBLIC_
enum ndr_err_code
ndr_pull_setup_relative_base_offset1(struct ndr_pull
*ndr
, const void *p
, uint32_t offset
)
1316 ndr
->relative_base_offset
= offset
;
1317 return ndr_token_store(ndr
, &ndr
->relative_base_list
, p
, offset
);
1321 setup the current base for relative pointers for the pull
1322 called in the NDR_BUFFERS stage
1324 _PUBLIC_
enum ndr_err_code
ndr_pull_setup_relative_base_offset2(struct ndr_pull
*ndr
, const void *p
)
1326 return ndr_token_retrieve(&ndr
->relative_base_list
, p
, &ndr
->relative_base_offset
);
1330 pull a relative object - stage1
1331 called during SCALARS processing
1333 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr1(struct ndr_pull
*ndr
, const void *p
, uint32_t rel_offset
)
1335 rel_offset
+= ndr
->relative_base_offset
;
1336 if (rel_offset
> ndr
->data_size
) {
1337 return ndr_pull_error(ndr
, NDR_ERR_BUFSIZE
,
1338 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1339 rel_offset
, ndr
->data_size
);
1341 return ndr_token_store(ndr
, &ndr
->relative_list
, p
, rel_offset
);
1345 pull a relative object - stage2
1346 called during BUFFERS processing
1348 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr2(struct ndr_pull
*ndr
, const void *p
)
1350 uint32_t rel_offset
;
1351 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &rel_offset
));
1352 return ndr_pull_set_offset(ndr
, rel_offset
);
1355 const static struct {
1356 enum ndr_err_code err
;
1358 } ndr_err_code_strings
[] = {
1359 { NDR_ERR_SUCCESS
, "Success" },
1360 { NDR_ERR_ARRAY_SIZE
, "Bad Array Size" },
1361 { NDR_ERR_BAD_SWITCH
, "Bad Switch" },
1362 { NDR_ERR_OFFSET
, "Offset Error" },
1363 { NDR_ERR_RELATIVE
, "Relative Pointer Error" },
1364 { NDR_ERR_CHARCNV
, "Character Conversion Error" },
1365 { NDR_ERR_LENGTH
, "Length Error" },
1366 { NDR_ERR_SUBCONTEXT
, "Subcontext Error" },
1367 { NDR_ERR_COMPRESSION
, "Compression Error" },
1368 { NDR_ERR_STRING
, "String Error" },
1369 { NDR_ERR_VALIDATE
, "Validate Error" },
1370 { NDR_ERR_BUFSIZE
, "Buffer Size Error" },
1371 { NDR_ERR_ALLOC
, "Allocation Error" },
1372 { NDR_ERR_RANGE
, "Range Error" },
1373 { NDR_ERR_TOKEN
, "Token Error" },
1374 { NDR_ERR_IPV4ADDRESS
, "IPv4 Address Error" },
1375 { NDR_ERR_INVALID_POINTER
, "Invalid Pointer" },
1376 { NDR_ERR_UNREAD_BYTES
, "Unread Bytes" },
1377 { NDR_ERR_NDR64
, "NDR64 assertion error" },
1381 _PUBLIC_
const char *ndr_map_error2string(enum ndr_err_code ndr_err
)
1384 for (i
= 0; ndr_err_code_strings
[i
].string
!= NULL
; i
++) {
1385 if (ndr_err_code_strings
[i
].err
== ndr_err
)
1386 return ndr_err_code_strings
[i
].string
;
1388 return "Unknown error";