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"
33 #define NDR_BASE_MARSHALL_SIZE 1024
35 /* this guid indicates NDR encoding in a protocol tower */
36 const struct ndr_syntax_id ndr_transfer_syntax
= {
37 { 0x8a885d04, 0x1ceb, 0x11c9, {0x9f, 0xe8}, {0x08,0x00,0x2b,0x10,0x48,0x60} },
41 const struct ndr_syntax_id ndr64_transfer_syntax
= {
42 { 0x71710533, 0xbeba, 0x4937, {0x83, 0x19}, {0xb5,0xdb,0xef,0x9c,0xcc,0x36} },
46 const struct ndr_syntax_id null_ndr_syntax_id
= {
47 { 0, 0, 0, { 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
52 work out the number of bytes needed to align on a n byte boundary
54 _PUBLIC_
size_t ndr_align_size(uint32_t offset
, size_t n
)
56 if ((offset
& (n
-1)) == 0) return 0;
57 return n
- (offset
& (n
-1));
61 initialise a ndr parse structure from a data blob
63 _PUBLIC_
struct ndr_pull
*ndr_pull_init_blob(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
)
67 ndr
= talloc_zero(mem_ctx
, struct ndr_pull
);
68 if (!ndr
) return NULL
;
69 ndr
->current_mem_ctx
= mem_ctx
;
71 ndr
->data
= blob
->data
;
72 ndr
->data_size
= blob
->length
;
78 advance by 'size' bytes
80 _PUBLIC_
enum ndr_err_code
ndr_pull_advance(struct ndr_pull
*ndr
, uint32_t size
)
83 if (ndr
->offset
> ndr
->data_size
) {
84 return ndr_pull_error(ndr
, NDR_ERR_BUFSIZE
,
85 "ndr_pull_advance by %u failed",
88 return NDR_ERR_SUCCESS
;
92 set the parse offset to 'ofs'
94 static enum ndr_err_code
ndr_pull_set_offset(struct ndr_pull
*ndr
, uint32_t ofs
)
97 if (ndr
->offset
> ndr
->data_size
) {
98 return ndr_pull_error(ndr
, NDR_ERR_BUFSIZE
,
99 "ndr_pull_set_offset %u failed",
102 return NDR_ERR_SUCCESS
;
105 /* create a ndr_push structure, ready for some marshalling */
106 _PUBLIC_
struct ndr_push
*ndr_push_init_ctx(TALLOC_CTX
*mem_ctx
)
108 struct ndr_push
*ndr
;
110 ndr
= talloc_zero(mem_ctx
, struct ndr_push
);
116 ndr
->alloc_size
= NDR_BASE_MARSHALL_SIZE
;
117 ndr
->data
= talloc_array(ndr
, uint8_t, ndr
->alloc_size
);
125 /* return a DATA_BLOB structure for the current ndr_push marshalled data */
126 _PUBLIC_ DATA_BLOB
ndr_push_blob(struct ndr_push
*ndr
)
129 blob
= data_blob_const(ndr
->data
, ndr
->offset
);
130 if (ndr
->alloc_size
> ndr
->offset
) {
131 ndr
->data
[ndr
->offset
] = 0;
138 expand the available space in the buffer to ndr->offset + extra_size
140 _PUBLIC_
enum ndr_err_code
ndr_push_expand(struct ndr_push
*ndr
, uint32_t extra_size
)
142 uint32_t size
= extra_size
+ ndr
->offset
;
144 if (size
< ndr
->offset
) {
145 /* extra_size overflowed the offset */
146 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
, "Overflow in push_expand to %u",
150 if (ndr
->alloc_size
> size
) {
151 return NDR_ERR_SUCCESS
;
154 ndr
->alloc_size
+= NDR_BASE_MARSHALL_SIZE
;
155 if (size
+1 > ndr
->alloc_size
) {
156 ndr
->alloc_size
= size
+1;
158 ndr
->data
= talloc_realloc(ndr
, ndr
->data
, uint8_t, ndr
->alloc_size
);
160 return ndr_push_error(ndr
, NDR_ERR_ALLOC
, "Failed to push_expand to %u",
164 return NDR_ERR_SUCCESS
;
167 _PUBLIC_
void ndr_print_debug_helper(struct ndr_print
*ndr
, const char *format
, ...)
174 va_start(ap
, format
);
175 ret
= vasprintf(&s
, format
, ap
);
182 if (ndr
->no_newline
) {
183 DEBUGADD(1,("%s", s
));
188 for (i
=0;i
<ndr
->depth
;i
++) {
192 DEBUGADD(1,("%s\n", s
));
196 _PUBLIC_
void ndr_print_printf_helper(struct ndr_print
*ndr
, const char *format
, ...)
201 if (!ndr
->no_newline
) {
202 for (i
=0;i
<ndr
->depth
;i
++) {
207 va_start(ap
, format
);
210 if (!ndr
->no_newline
) {
215 _PUBLIC_
void ndr_print_string_helper(struct ndr_print
*ndr
, const char *format
, ...)
220 if (!ndr
->no_newline
) {
221 for (i
=0;i
<ndr
->depth
;i
++) {
222 ndr
->private_data
= talloc_asprintf_append_buffer(
223 (char *)ndr
->private_data
, " ");
227 va_start(ap
, format
);
228 ndr
->private_data
= talloc_vasprintf_append_buffer((char *)ndr
->private_data
,
231 if (!ndr
->no_newline
) {
232 ndr
->private_data
= talloc_asprintf_append_buffer((char *)ndr
->private_data
,
238 a useful helper function for printing idl structures via DEBUG()
240 _PUBLIC_
void ndr_print_debug(ndr_print_fn_t fn
, const char *name
, void *ptr
)
242 struct ndr_print
*ndr
;
246 ndr
= talloc_zero(NULL
, struct ndr_print
);
248 ndr
->print
= ndr_print_debug_helper
;
256 a useful helper function for printing idl unions via DEBUG()
258 _PUBLIC_
void ndr_print_union_debug(ndr_print_fn_t fn
, const char *name
, uint32_t level
, void *ptr
)
260 struct ndr_print
*ndr
;
264 ndr
= talloc_zero(NULL
, struct ndr_print
);
266 ndr
->print
= ndr_print_debug_helper
;
269 ndr_print_set_switch_value(ndr
, ptr
, level
);
275 a useful helper function for printing idl function calls via DEBUG()
277 _PUBLIC_
void ndr_print_function_debug(ndr_print_function_t fn
, const char *name
, int flags
, void *ptr
)
279 struct ndr_print
*ndr
;
283 ndr
= talloc_zero(NULL
, struct ndr_print
);
285 ndr
->print
= ndr_print_debug_helper
;
289 fn(ndr
, name
, flags
, ptr
);
294 a useful helper function for printing idl structures to a string
296 _PUBLIC_
char *ndr_print_struct_string(TALLOC_CTX
*mem_ctx
, ndr_print_fn_t fn
, const char *name
, void *ptr
)
298 struct ndr_print
*ndr
;
301 ndr
= talloc_zero(mem_ctx
, struct ndr_print
);
302 if (!ndr
) return NULL
;
303 ndr
->private_data
= talloc_strdup(ndr
, "");
304 if (!ndr
->private_data
) {
307 ndr
->print
= ndr_print_string_helper
;
312 ret
= talloc_steal(mem_ctx
, (char *)ndr
->private_data
);
319 a useful helper function for printing idl unions to a string
321 _PUBLIC_
char *ndr_print_union_string(TALLOC_CTX
*mem_ctx
, ndr_print_fn_t fn
, const char *name
, uint32_t level
, void *ptr
)
323 struct ndr_print
*ndr
;
326 ndr
= talloc_zero(mem_ctx
, struct ndr_print
);
327 if (!ndr
) return NULL
;
328 ndr
->private_data
= talloc_strdup(ndr
, "");
329 if (!ndr
->private_data
) {
332 ndr
->print
= ndr_print_string_helper
;
335 ndr_print_set_switch_value(ndr
, ptr
, level
);
337 ret
= talloc_steal(mem_ctx
, (char *)ndr
->private_data
);
344 a useful helper function for printing idl function calls to a string
346 _PUBLIC_
char *ndr_print_function_string(TALLOC_CTX
*mem_ctx
,
347 ndr_print_function_t fn
, const char *name
,
348 int flags
, void *ptr
)
350 struct ndr_print
*ndr
;
353 ndr
= talloc_zero(mem_ctx
, struct ndr_print
);
354 if (!ndr
) return NULL
;
355 ndr
->private_data
= talloc_strdup(ndr
, "");
356 if (!ndr
->private_data
) {
359 ndr
->print
= ndr_print_string_helper
;
362 fn(ndr
, name
, flags
, ptr
);
363 ret
= talloc_steal(mem_ctx
, (char *)ndr
->private_data
);
369 _PUBLIC_
void ndr_set_flags(uint32_t *pflags
, uint32_t new_flags
)
371 /* the big/little endian flags are inter-dependent */
372 if (new_flags
& LIBNDR_FLAG_LITTLE_ENDIAN
) {
373 (*pflags
) &= ~LIBNDR_FLAG_BIGENDIAN
;
374 (*pflags
) &= ~LIBNDR_FLAG_NDR64
;
376 if (new_flags
& LIBNDR_FLAG_BIGENDIAN
) {
377 (*pflags
) &= ~LIBNDR_FLAG_LITTLE_ENDIAN
;
378 (*pflags
) &= ~LIBNDR_FLAG_NDR64
;
380 if (new_flags
& LIBNDR_FLAG_REMAINING
) {
381 (*pflags
) &= ~LIBNDR_ALIGN_FLAGS
;
383 if (new_flags
& LIBNDR_ALIGN_FLAGS
) {
384 (*pflags
) &= ~LIBNDR_FLAG_REMAINING
;
386 if (new_flags
& LIBNDR_FLAG_NO_RELATIVE_REVERSE
) {
387 (*pflags
) &= ~LIBNDR_FLAG_RELATIVE_REVERSE
;
389 (*pflags
) |= new_flags
;
393 return and possibly log an NDR error
395 _PUBLIC_
enum ndr_err_code
ndr_pull_error(struct ndr_pull
*ndr
,
396 enum ndr_err_code ndr_err
,
397 const char *format
, ...)
403 va_start(ap
, format
);
404 ret
= vasprintf(&s
, format
, ap
);
408 return NDR_ERR_ALLOC
;
411 DEBUG(1,("ndr_pull_error(%u): %s\n", ndr_err
, s
));
419 return and possibly log an NDR error
421 _PUBLIC_
enum ndr_err_code
ndr_push_error(struct ndr_push
*ndr
,
422 enum ndr_err_code ndr_err
,
423 const char *format
, ...)
429 va_start(ap
, format
);
430 ret
= vasprintf(&s
, format
, ap
);
434 return NDR_ERR_ALLOC
;
437 DEBUG(1,("ndr_push_error(%u): %s\n", ndr_err
, s
));
445 handle subcontext buffers, which in midl land are user-marshalled, but
446 we use magic in pidl to make them easier to cope with
448 _PUBLIC_
enum ndr_err_code
ndr_pull_subcontext_start(struct ndr_pull
*ndr
,
449 struct ndr_pull
**_subndr
,
453 struct ndr_pull
*subndr
;
454 uint32_t r_content_size
;
455 bool force_le
= false;
456 bool force_be
= false;
458 switch (header_size
) {
460 uint32_t content_size
= ndr
->data_size
- ndr
->offset
;
462 content_size
= size_is
;
464 r_content_size
= content_size
;
469 uint16_t content_size
;
470 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, &content_size
));
471 if (size_is
>= 0 && size_is
!= content_size
) {
472 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
473 (int)size_is
, (int)content_size
);
475 r_content_size
= content_size
;
480 uint32_t content_size
;
481 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &content_size
));
482 if (size_is
>= 0 && size_is
!= content_size
) {
483 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
484 (int)size_is
, (int)content_size
);
486 r_content_size
= content_size
;
491 * Common Type Header for the Serialization Stream
492 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
498 uint32_t content_size
;
502 NDR_CHECK(ndr_pull_uint8(ndr
, NDR_SCALARS
, &version
));
505 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
506 "Bad subcontext (PULL) Common Type Header version %d != 1",
514 NDR_CHECK(ndr_pull_uint8(ndr
, NDR_SCALARS
, &drep
));
517 } else if (drep
== 0x00) {
520 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
521 "Bad subcontext (PULL) Common Type Header invalid drep 0x%02X",
525 /* length of the "Private Header for Constructed Type" */
526 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, &hdrlen
));
528 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
529 "Bad subcontext (PULL) Common Type Header length %d != 8",
533 /* filler should be ignored */
534 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &filler
));
537 * Private Header for Constructed Type
539 /* length - will be updated latter */
540 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &content_size
));
541 if (size_is
>= 0 && size_is
!= content_size
) {
542 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
543 (int)size_is
, (int)content_size
);
545 /* the content size must be a multiple of 8 */
546 if ((content_size
% 8) != 0) {
547 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
548 "Bad subcontext (PULL) size_is(%d) not padded to 8 content_size %d",
549 (int)size_is
, (int)content_size
);
551 r_content_size
= content_size
;
554 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &reserved
));
558 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) header_size %d",
562 NDR_PULL_NEED_BYTES(ndr
, r_content_size
);
564 subndr
= talloc_zero(ndr
, struct ndr_pull
);
565 NDR_ERR_HAVE_NO_MEMORY(subndr
);
566 subndr
->flags
= ndr
->flags
& ~LIBNDR_FLAG_NDR64
;
567 subndr
->current_mem_ctx
= ndr
->current_mem_ctx
;
569 subndr
->data
= ndr
->data
+ ndr
->offset
;
571 subndr
->data_size
= r_content_size
;
574 ndr_set_flags(&ndr
->flags
, LIBNDR_FLAG_LITTLE_ENDIAN
);
575 } else if (force_be
) {
576 ndr_set_flags(&ndr
->flags
, LIBNDR_FLAG_BIGENDIAN
);
580 return NDR_ERR_SUCCESS
;
583 _PUBLIC_
enum ndr_err_code
ndr_pull_subcontext_end(struct ndr_pull
*ndr
,
584 struct ndr_pull
*subndr
,
591 } else if (header_size
> 0) {
592 advance
= subndr
->data_size
;
594 advance
= subndr
->offset
;
596 NDR_CHECK(ndr_pull_advance(ndr
, advance
));
597 return NDR_ERR_SUCCESS
;
600 _PUBLIC_
enum ndr_err_code
ndr_push_subcontext_start(struct ndr_push
*ndr
,
601 struct ndr_push
**_subndr
,
605 struct ndr_push
*subndr
;
607 subndr
= ndr_push_init_ctx(ndr
);
608 NDR_ERR_HAVE_NO_MEMORY(subndr
);
609 subndr
->flags
= ndr
->flags
& ~LIBNDR_FLAG_NDR64
;
612 NDR_CHECK(ndr_push_zero(subndr
, size_is
));
614 subndr
->relative_end_offset
= size_is
;
618 return NDR_ERR_SUCCESS
;
622 push a subcontext header
624 _PUBLIC_
enum ndr_err_code
ndr_push_subcontext_end(struct ndr_push
*ndr
,
625 struct ndr_push
*subndr
,
632 padding_len
= size_is
- subndr
->offset
;
633 if (padding_len
< 0) {
634 return ndr_push_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
635 (int)subndr
->offset
, (int)size_is
);
637 subndr
->offset
= size_is
;
640 switch (header_size
) {
645 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, subndr
->offset
));
649 NDR_CHECK(ndr_push_uint3264(ndr
, NDR_SCALARS
, subndr
->offset
));
654 * Common Type Header for the Serialization Stream
655 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
657 padding_len
= NDR_ROUND(subndr
->offset
, 8) - subndr
->offset
;
658 if (padding_len
> 0) {
659 NDR_CHECK(ndr_push_zero(subndr
, padding_len
));
663 NDR_CHECK(ndr_push_uint8(ndr
, NDR_SCALARS
, 1));
669 NDR_CHECK(ndr_push_uint8(ndr
, NDR_SCALARS
, NDR_BE(ndr
)?0x00:0x10));
671 /* length of the "Private Header for Constructed Type" */
672 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, 8));
675 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0xCCCCCCCC));
678 * Private Header for Constructed Type
680 /* length - will be updated latter */
681 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, subndr
->offset
));
684 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0));
688 return ndr_push_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext header size %d",
692 NDR_CHECK(ndr_push_bytes(ndr
, subndr
->data
, subndr
->offset
));
693 return NDR_ERR_SUCCESS
;
697 store a token in the ndr context, for later retrieval
699 _PUBLIC_
enum ndr_err_code
ndr_token_store(TALLOC_CTX
*mem_ctx
,
700 struct ndr_token_list
**list
,
704 struct ndr_token_list
*tok
;
705 tok
= talloc(mem_ctx
, struct ndr_token_list
);
706 NDR_ERR_HAVE_NO_MEMORY(tok
);
709 DLIST_ADD((*list
), tok
);
710 return NDR_ERR_SUCCESS
;
714 retrieve a token from a ndr context, using cmp_fn to match the tokens
716 _PUBLIC_
enum ndr_err_code
ndr_token_retrieve_cmp_fn(struct ndr_token_list
**list
, const void *key
, uint32_t *v
,
717 comparison_fn_t _cmp_fn
, bool _remove_tok
)
719 struct ndr_token_list
*tok
;
720 for (tok
=*list
;tok
;tok
=tok
->next
) {
721 if (_cmp_fn
&& _cmp_fn(tok
->key
,key
)==0) goto found
;
722 else if (!_cmp_fn
&& tok
->key
== key
) goto found
;
724 return NDR_ERR_TOKEN
;
728 DLIST_REMOVE((*list
), tok
);
731 return NDR_ERR_SUCCESS
;
735 retrieve a token from a ndr context
737 _PUBLIC_
enum ndr_err_code
ndr_token_retrieve(struct ndr_token_list
**list
, const void *key
, uint32_t *v
)
739 return ndr_token_retrieve_cmp_fn(list
, key
, v
, NULL
, true);
743 peek at but don't removed a token from a ndr context
745 _PUBLIC_
uint32_t ndr_token_peek(struct ndr_token_list
**list
, const void *key
)
747 enum ndr_err_code status
;
750 status
= ndr_token_retrieve_cmp_fn(list
, key
, &v
, NULL
, false);
751 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
759 pull an array size field and add it to the array_size_list token list
761 _PUBLIC_
enum ndr_err_code
ndr_pull_array_size(struct ndr_pull
*ndr
, const void *p
)
764 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &size
));
765 return ndr_token_store(ndr
, &ndr
->array_size_list
, p
, size
);
769 get the stored array size field
771 _PUBLIC_
uint32_t ndr_get_array_size(struct ndr_pull
*ndr
, const void *p
)
773 return ndr_token_peek(&ndr
->array_size_list
, p
);
777 check the stored array size field
779 _PUBLIC_
enum ndr_err_code
ndr_check_array_size(struct ndr_pull
*ndr
, void *p
, uint32_t size
)
782 stored
= ndr_token_peek(&ndr
->array_size_list
, p
);
783 if (stored
!= size
) {
784 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
785 "Bad array size - got %u expected %u\n",
788 return NDR_ERR_SUCCESS
;
792 pull an array length field and add it to the array_length_list token list
794 _PUBLIC_
enum ndr_err_code
ndr_pull_array_length(struct ndr_pull
*ndr
, const void *p
)
796 uint32_t length
, offset
;
797 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &offset
));
799 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
800 "non-zero array offset %u\n", offset
);
802 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &length
));
803 return ndr_token_store(ndr
, &ndr
->array_length_list
, p
, length
);
807 get the stored array length field
809 _PUBLIC_
uint32_t ndr_get_array_length(struct ndr_pull
*ndr
, const void *p
)
811 return ndr_token_peek(&ndr
->array_length_list
, p
);
815 check the stored array length field
817 _PUBLIC_
enum ndr_err_code
ndr_check_array_length(struct ndr_pull
*ndr
, void *p
, uint32_t length
)
820 stored
= ndr_token_peek(&ndr
->array_length_list
, p
);
821 if (stored
!= length
) {
822 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
823 "Bad array length - got %u expected %u\n",
826 return NDR_ERR_SUCCESS
;
829 _PUBLIC_
enum ndr_err_code
ndr_push_pipe_chunk_trailer(struct ndr_push
*ndr
, int ndr_flags
, uint32_t count
)
831 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
832 int64_t tmp
= 0 - (int64_t)count
;
833 uint64_t ncount
= tmp
;
835 NDR_CHECK(ndr_push_hyper(ndr
, ndr_flags
, ncount
));
838 return NDR_ERR_SUCCESS
;
841 _PUBLIC_
enum ndr_err_code
ndr_check_pipe_chunk_trailer(struct ndr_pull
*ndr
, int ndr_flags
, uint32_t count
)
843 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
844 int64_t tmp
= 0 - (int64_t)count
;
845 uint64_t ncount1
= tmp
;
848 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, &ncount2
));
849 if (ncount1
== ncount2
) {
850 return NDR_ERR_SUCCESS
;
853 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
854 "Bad pipe trailer[%lld should be %lld] size was %lu\"",
855 (unsigned long long)ncount2
,
856 (unsigned long long)ncount1
,
857 (unsigned long)count
);
860 return NDR_ERR_SUCCESS
;
866 _PUBLIC_
enum ndr_err_code
ndr_push_set_switch_value(struct ndr_push
*ndr
, const void *p
, uint32_t val
)
868 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
871 _PUBLIC_
enum ndr_err_code
ndr_pull_set_switch_value(struct ndr_pull
*ndr
, const void *p
, uint32_t val
)
873 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
876 _PUBLIC_
enum ndr_err_code
ndr_print_set_switch_value(struct ndr_print
*ndr
, const void *p
, uint32_t val
)
878 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
882 retrieve a switch value
884 _PUBLIC_
uint32_t ndr_push_get_switch_value(struct ndr_push
*ndr
, const void *p
)
886 return ndr_token_peek(&ndr
->switch_list
, p
);
889 _PUBLIC_
uint32_t ndr_pull_get_switch_value(struct ndr_pull
*ndr
, const void *p
)
891 return ndr_token_peek(&ndr
->switch_list
, p
);
894 _PUBLIC_
uint32_t ndr_print_get_switch_value(struct ndr_print
*ndr
, const void *p
)
896 return ndr_token_peek(&ndr
->switch_list
, p
);
900 pull a struct from a blob using NDR
902 _PUBLIC_
enum ndr_err_code
ndr_pull_struct_blob(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
, void *p
,
903 ndr_pull_flags_fn_t fn
)
905 struct ndr_pull
*ndr
;
906 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
907 NDR_ERR_HAVE_NO_MEMORY(ndr
);
908 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
910 return NDR_ERR_SUCCESS
;
914 pull a struct from a blob using NDR - failing if all bytes are not consumed
916 _PUBLIC_
enum ndr_err_code
ndr_pull_struct_blob_all(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
917 void *p
, ndr_pull_flags_fn_t fn
)
919 struct ndr_pull
*ndr
;
920 uint32_t highest_ofs
;
921 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
922 NDR_ERR_HAVE_NO_MEMORY(ndr
);
923 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
924 if (ndr
->offset
> ndr
->relative_highest_offset
) {
925 highest_ofs
= ndr
->offset
;
927 highest_ofs
= ndr
->relative_highest_offset
;
929 if (highest_ofs
< ndr
->data_size
) {
930 enum ndr_err_code ret
;
931 ret
= ndr_pull_error(ndr
, NDR_ERR_UNREAD_BYTES
,
932 "not all bytes consumed ofs[%u] size[%u]",
933 highest_ofs
, ndr
->data_size
);
938 return NDR_ERR_SUCCESS
;
942 pull a union from a blob using NDR, given the union discriminator
944 _PUBLIC_
enum ndr_err_code
ndr_pull_union_blob(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
946 uint32_t level
, ndr_pull_flags_fn_t fn
)
948 struct ndr_pull
*ndr
;
949 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
950 NDR_ERR_HAVE_NO_MEMORY(ndr
);
951 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr
, p
, level
));
952 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
954 return NDR_ERR_SUCCESS
;
958 pull a union from a blob using NDR, given the union discriminator,
959 failing if all bytes are not consumed
961 _PUBLIC_
enum ndr_err_code
ndr_pull_union_blob_all(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
963 uint32_t level
, ndr_pull_flags_fn_t fn
)
965 struct ndr_pull
*ndr
;
966 uint32_t highest_ofs
;
967 ndr
= ndr_pull_init_blob(blob
, mem_ctx
);
968 NDR_ERR_HAVE_NO_MEMORY(ndr
);
969 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr
, p
, level
));
970 NDR_CHECK_FREE(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
971 if (ndr
->offset
> ndr
->relative_highest_offset
) {
972 highest_ofs
= ndr
->offset
;
974 highest_ofs
= ndr
->relative_highest_offset
;
976 if (highest_ofs
< ndr
->data_size
) {
977 enum ndr_err_code ret
;
978 ret
= ndr_pull_error(ndr
, NDR_ERR_UNREAD_BYTES
,
979 "not all bytes consumed ofs[%u] size[%u]",
980 highest_ofs
, ndr
->data_size
);
985 return NDR_ERR_SUCCESS
;
989 push a struct to a blob using NDR
991 _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
)
993 struct ndr_push
*ndr
;
994 ndr
= ndr_push_init_ctx(mem_ctx
);
995 NDR_ERR_HAVE_NO_MEMORY(ndr
);
997 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
999 *blob
= ndr_push_blob(ndr
);
1000 talloc_steal(mem_ctx
, blob
->data
);
1003 return NDR_ERR_SUCCESS
;
1007 push a union to a blob using NDR
1009 _PUBLIC_
enum ndr_err_code
ndr_push_union_blob(DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
, void *p
,
1010 uint32_t level
, ndr_push_flags_fn_t fn
)
1012 struct ndr_push
*ndr
;
1013 ndr
= ndr_push_init_ctx(mem_ctx
);
1014 NDR_ERR_HAVE_NO_MEMORY(ndr
);
1016 NDR_CHECK(ndr_push_set_switch_value(ndr
, p
, level
));
1017 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
1019 *blob
= ndr_push_blob(ndr
);
1020 talloc_steal(mem_ctx
, blob
->data
);
1023 return NDR_ERR_SUCCESS
;
1027 generic ndr_size_*() handler for structures
1029 _PUBLIC_
size_t ndr_size_struct(const void *p
, int flags
, ndr_push_flags_fn_t push
)
1031 struct ndr_push
*ndr
;
1032 enum ndr_err_code status
;
1035 /* avoid recursion */
1036 if (flags
& LIBNDR_FLAG_NO_NDR_SIZE
) return 0;
1038 ndr
= ndr_push_init_ctx(NULL
);
1040 ndr
->flags
|= flags
| LIBNDR_FLAG_NO_NDR_SIZE
;
1041 status
= push(ndr
, NDR_SCALARS
|NDR_BUFFERS
, discard_const(p
));
1042 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
1052 generic ndr_size_*() handler for unions
1054 _PUBLIC_
size_t ndr_size_union(const void *p
, int flags
, uint32_t level
, ndr_push_flags_fn_t push
)
1056 struct ndr_push
*ndr
;
1057 enum ndr_err_code status
;
1060 /* avoid recursion */
1061 if (flags
& LIBNDR_FLAG_NO_NDR_SIZE
) return 0;
1063 ndr
= ndr_push_init_ctx(NULL
);
1065 ndr
->flags
|= flags
| LIBNDR_FLAG_NO_NDR_SIZE
;
1067 status
= ndr_push_set_switch_value(ndr
, p
, level
);
1068 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
1072 status
= push(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
);
1073 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
1083 get the current base for relative pointers for the push
1085 _PUBLIC_
uint32_t ndr_push_get_relative_base_offset(struct ndr_push
*ndr
)
1087 return ndr
->relative_base_offset
;
1091 restore the old base for relative pointers for the push
1093 _PUBLIC_
void ndr_push_restore_relative_base_offset(struct ndr_push
*ndr
, uint32_t offset
)
1095 ndr
->relative_base_offset
= offset
;
1099 setup the current base for relative pointers for the push
1100 called in the NDR_SCALAR stage
1102 _PUBLIC_
enum ndr_err_code
ndr_push_setup_relative_base_offset1(struct ndr_push
*ndr
, const void *p
, uint32_t offset
)
1104 ndr
->relative_base_offset
= offset
;
1105 return ndr_token_store(ndr
, &ndr
->relative_base_list
, p
, offset
);
1109 setup the current base for relative pointers for the push
1110 called in the NDR_BUFFERS stage
1112 _PUBLIC_
enum ndr_err_code
ndr_push_setup_relative_base_offset2(struct ndr_push
*ndr
, const void *p
)
1114 return ndr_token_retrieve(&ndr
->relative_base_list
, p
, &ndr
->relative_base_offset
);
1118 push a relative object - stage1
1119 this is called during SCALARS processing
1121 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr1(struct ndr_push
*ndr
, const void *p
)
1124 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0));
1125 return NDR_ERR_SUCCESS
;
1127 NDR_CHECK(ndr_push_align(ndr
, 4));
1128 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_list
, p
, ndr
->offset
));
1129 return ndr_push_uint32(ndr
, NDR_SCALARS
, 0xFFFFFFFF);
1133 push a short relative object - stage1
1134 this is called during SCALARS processing
1136 _PUBLIC_
enum ndr_err_code
ndr_push_short_relative_ptr1(struct ndr_push
*ndr
, const void *p
)
1139 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, 0));
1140 return NDR_ERR_SUCCESS
;
1142 NDR_CHECK(ndr_push_align(ndr
, 2));
1143 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_list
, p
, ndr
->offset
));
1144 return ndr_push_uint16(ndr
, NDR_SCALARS
, 0xFFFF);
1147 push a relative object - stage2
1148 this is called during buffers processing
1150 static enum ndr_err_code
ndr_push_relative_ptr2(struct ndr_push
*ndr
, const void *p
)
1152 uint32_t save_offset
;
1153 uint32_t ptr_offset
= 0xFFFFFFFF;
1155 return NDR_ERR_SUCCESS
;
1157 save_offset
= ndr
->offset
;
1158 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &ptr_offset
));
1159 if (ptr_offset
> ndr
->offset
) {
1160 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1161 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1162 ptr_offset
, ndr
->offset
);
1164 ndr
->offset
= ptr_offset
;
1165 if (save_offset
< ndr
->relative_base_offset
) {
1166 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1167 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1168 save_offset
, ndr
->relative_base_offset
);
1170 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, save_offset
- ndr
->relative_base_offset
));
1171 ndr
->offset
= save_offset
;
1172 return NDR_ERR_SUCCESS
;
1175 push a short relative object - stage2
1176 this is called during buffers processing
1178 _PUBLIC_
enum ndr_err_code
ndr_push_short_relative_ptr2(struct ndr_push
*ndr
, const void *p
)
1180 uint32_t save_offset
;
1181 uint32_t ptr_offset
= 0xFFFF;
1183 return NDR_ERR_SUCCESS
;
1185 save_offset
= ndr
->offset
;
1186 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &ptr_offset
));
1187 if (ptr_offset
> ndr
->offset
) {
1188 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1189 "ndr_push_short_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1190 ptr_offset
, ndr
->offset
);
1192 ndr
->offset
= ptr_offset
;
1193 if (save_offset
< ndr
->relative_base_offset
) {
1194 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1195 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1196 save_offset
, ndr
->relative_base_offset
);
1198 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, save_offset
- ndr
->relative_base_offset
));
1199 ndr
->offset
= save_offset
;
1200 return NDR_ERR_SUCCESS
;
1204 push a relative object - stage2 start
1205 this is called during buffers processing
1207 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr2_start(struct ndr_push
*ndr
, const void *p
)
1210 return NDR_ERR_SUCCESS
;
1212 if (!(ndr
->flags
& LIBNDR_FLAG_RELATIVE_REVERSE
)) {
1213 uint32_t relative_offset
;
1217 if (ndr
->offset
< ndr
->relative_base_offset
) {
1218 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1219 "ndr_push_relative_ptr2_start ndr->offset(%u) < ndr->relative_base_offset(%u)",
1220 ndr
->offset
, ndr
->relative_base_offset
);
1223 relative_offset
= ndr
->offset
- ndr
->relative_base_offset
;
1225 if (ndr
->flags
& LIBNDR_FLAG_NOALIGN
) {
1227 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
1229 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
1231 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
1235 pad
= ndr_align_size(relative_offset
, align
);
1237 NDR_CHECK(ndr_push_zero(ndr
, pad
));
1240 return ndr_push_relative_ptr2(ndr
, p
);
1242 if (ndr
->relative_end_offset
== -1) {
1243 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1244 "ndr_push_relative_ptr2_start RELATIVE_REVERSE flag set and relative_end_offset %d",
1245 ndr
->relative_end_offset
);
1247 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_begin_list
, p
, ndr
->offset
));
1248 return NDR_ERR_SUCCESS
;
1252 push a relative object - stage2 end
1253 this is called during buffers processing
1255 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr2_end(struct ndr_push
*ndr
, const void *p
)
1257 uint32_t begin_offset
= 0xFFFFFFFF;
1259 uint32_t correct_offset
= 0;
1264 return NDR_ERR_SUCCESS
;
1267 if (!(ndr
->flags
& LIBNDR_FLAG_RELATIVE_REVERSE
)) {
1268 return NDR_ERR_SUCCESS
;
1271 if (ndr
->flags
& LIBNDR_FLAG_NO_NDR_SIZE
) {
1272 /* better say more than calculation a too small buffer */
1273 NDR_PUSH_ALIGN(ndr
, 8);
1274 return NDR_ERR_SUCCESS
;
1277 if (ndr
->relative_end_offset
< ndr
->offset
) {
1278 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1279 "ndr_push_relative_ptr2_end:"
1280 "relative_end_offset %u < offset %u",
1281 ndr
->relative_end_offset
, ndr
->offset
);
1284 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_begin_list
, p
, &begin_offset
));
1286 /* we have marshalled a buffer, see how long it was */
1287 len
= ndr
->offset
- begin_offset
;
1290 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1291 "ndr_push_relative_ptr2_end:"
1292 "offset %u - begin_offset %u < 0",
1293 ndr
->offset
, begin_offset
);
1296 if (ndr
->relative_end_offset
< len
) {
1297 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1298 "ndr_push_relative_ptr2_end:"
1299 "relative_end_offset %u < len %lld",
1300 ndr
->offset
, (long long)len
);
1303 /* the reversed offset is at the end of the main buffer */
1304 correct_offset
= ndr
->relative_end_offset
- len
;
1306 if (ndr
->flags
& LIBNDR_FLAG_NOALIGN
) {
1308 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
1310 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
1312 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
1316 pad
= ndr_align_size(correct_offset
, align
);
1318 correct_offset
+= pad
;
1319 correct_offset
-= align
;
1322 if (correct_offset
< begin_offset
) {
1323 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1324 "ndr_push_relative_ptr2_end: "
1325 "correct_offset %u < begin_offset %u",
1326 correct_offset
, begin_offset
);
1330 uint32_t clear_size
= correct_offset
- begin_offset
;
1332 clear_size
= MIN(clear_size
, len
);
1334 /* now move the marshalled buffer to the end of the main buffer */
1335 memmove(ndr
->data
+ correct_offset
, ndr
->data
+ begin_offset
, len
);
1338 /* and wipe out old buffer within the main buffer */
1339 memset(ndr
->data
+ begin_offset
, '\0', clear_size
);
1343 /* and set the end offset for the next buffer */
1344 ndr
->relative_end_offset
= correct_offset
;
1346 /* finally write the offset to the main buffer */
1347 ndr
->offset
= correct_offset
;
1348 NDR_CHECK(ndr_push_relative_ptr2(ndr
, p
));
1350 /* restore to where we were in the main buffer */
1351 ndr
->offset
= begin_offset
;
1353 return NDR_ERR_SUCCESS
;
1357 get the current base for relative pointers for the pull
1359 _PUBLIC_
uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull
*ndr
)
1361 return ndr
->relative_base_offset
;
1365 restore the old base for relative pointers for the pull
1367 _PUBLIC_
void ndr_pull_restore_relative_base_offset(struct ndr_pull
*ndr
, uint32_t offset
)
1369 ndr
->relative_base_offset
= offset
;
1373 setup the current base for relative pointers for the pull
1374 called in the NDR_SCALAR stage
1376 _PUBLIC_
enum ndr_err_code
ndr_pull_setup_relative_base_offset1(struct ndr_pull
*ndr
, const void *p
, uint32_t offset
)
1378 ndr
->relative_base_offset
= offset
;
1379 return ndr_token_store(ndr
, &ndr
->relative_base_list
, p
, offset
);
1383 setup the current base for relative pointers for the pull
1384 called in the NDR_BUFFERS stage
1386 _PUBLIC_
enum ndr_err_code
ndr_pull_setup_relative_base_offset2(struct ndr_pull
*ndr
, const void *p
)
1388 return ndr_token_retrieve(&ndr
->relative_base_list
, p
, &ndr
->relative_base_offset
);
1392 pull a relative object - stage1
1393 called during SCALARS processing
1395 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr1(struct ndr_pull
*ndr
, const void *p
, uint32_t rel_offset
)
1397 rel_offset
+= ndr
->relative_base_offset
;
1398 if (rel_offset
> ndr
->data_size
) {
1399 return ndr_pull_error(ndr
, NDR_ERR_BUFSIZE
,
1400 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1401 rel_offset
, ndr
->data_size
);
1403 return ndr_token_store(ndr
, &ndr
->relative_list
, p
, rel_offset
);
1407 pull a relative object - stage2
1408 called during BUFFERS processing
1410 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr2(struct ndr_pull
*ndr
, const void *p
)
1412 uint32_t rel_offset
;
1413 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &rel_offset
));
1414 return ndr_pull_set_offset(ndr
, rel_offset
);
1417 const static struct {
1418 enum ndr_err_code err
;
1420 } ndr_err_code_strings
[] = {
1421 { NDR_ERR_SUCCESS
, "Success" },
1422 { NDR_ERR_ARRAY_SIZE
, "Bad Array Size" },
1423 { NDR_ERR_BAD_SWITCH
, "Bad Switch" },
1424 { NDR_ERR_OFFSET
, "Offset Error" },
1425 { NDR_ERR_RELATIVE
, "Relative Pointer Error" },
1426 { NDR_ERR_CHARCNV
, "Character Conversion Error" },
1427 { NDR_ERR_LENGTH
, "Length Error" },
1428 { NDR_ERR_SUBCONTEXT
, "Subcontext Error" },
1429 { NDR_ERR_COMPRESSION
, "Compression Error" },
1430 { NDR_ERR_STRING
, "String Error" },
1431 { NDR_ERR_VALIDATE
, "Validate Error" },
1432 { NDR_ERR_BUFSIZE
, "Buffer Size Error" },
1433 { NDR_ERR_ALLOC
, "Allocation Error" },
1434 { NDR_ERR_RANGE
, "Range Error" },
1435 { NDR_ERR_TOKEN
, "Token Error" },
1436 { NDR_ERR_IPV4ADDRESS
, "IPv4 Address Error" },
1437 { NDR_ERR_INVALID_POINTER
, "Invalid Pointer" },
1438 { NDR_ERR_UNREAD_BYTES
, "Unread Bytes" },
1439 { NDR_ERR_NDR64
, "NDR64 assertion error" },
1443 _PUBLIC_
const char *ndr_map_error2string(enum ndr_err_code ndr_err
)
1446 for (i
= 0; ndr_err_code_strings
[i
].string
!= NULL
; i
++) {
1447 if (ndr_err_code_strings
[i
].err
== ndr_err
)
1448 return ndr_err_code_strings
[i
].string
;
1450 return "Unknown error";