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
, struct smb_iconv_convenience
*iconv_convenience
)
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
;
71 ndr
->iconv_convenience
= talloc_reference(ndr
, iconv_convenience
);
77 advance by 'size' bytes
79 _PUBLIC_
enum ndr_err_code
ndr_pull_advance(struct ndr_pull
*ndr
, uint32_t size
)
82 if (ndr
->offset
> ndr
->data_size
) {
83 return ndr_pull_error(ndr
, NDR_ERR_BUFSIZE
,
84 "ndr_pull_advance by %u failed",
87 return NDR_ERR_SUCCESS
;
91 set the parse offset to 'ofs'
93 static enum ndr_err_code
ndr_pull_set_offset(struct ndr_pull
*ndr
, uint32_t ofs
)
96 if (ndr
->offset
> ndr
->data_size
) {
97 return ndr_pull_error(ndr
, NDR_ERR_BUFSIZE
,
98 "ndr_pull_set_offset %u failed",
101 return NDR_ERR_SUCCESS
;
104 /* create a ndr_push structure, ready for some marshalling */
105 _PUBLIC_
struct ndr_push
*ndr_push_init_ctx(TALLOC_CTX
*mem_ctx
, struct smb_iconv_convenience
*iconv_convenience
)
107 struct ndr_push
*ndr
;
109 ndr
= talloc_zero(mem_ctx
, struct ndr_push
);
115 ndr
->alloc_size
= NDR_BASE_MARSHALL_SIZE
;
116 ndr
->data
= talloc_array(ndr
, uint8_t, ndr
->alloc_size
);
120 ndr
->iconv_convenience
= talloc_reference(ndr
, iconv_convenience
);
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
, ...)
173 va_start(ap
, format
);
174 ret
= vasprintf(&s
, format
, ap
);
181 for (i
=0;i
<ndr
->depth
;i
++) {
185 DEBUGADD(1,("%s\n", s
));
189 _PUBLIC_
void ndr_print_string_helper(struct ndr_print
*ndr
, const char *format
, ...)
194 for (i
=0;i
<ndr
->depth
;i
++) {
195 ndr
->private_data
= talloc_asprintf_append_buffer(
196 (char *)ndr
->private_data
, " ");
199 va_start(ap
, format
);
200 ndr
->private_data
= talloc_vasprintf_append_buffer((char *)ndr
->private_data
,
203 ndr
->private_data
= talloc_asprintf_append_buffer((char *)ndr
->private_data
,
208 a useful helper function for printing idl structures via DEBUG()
210 _PUBLIC_
void ndr_print_debug(ndr_print_fn_t fn
, const char *name
, void *ptr
)
212 struct ndr_print
*ndr
;
216 ndr
= talloc_zero(NULL
, struct ndr_print
);
218 ndr
->print
= ndr_print_debug_helper
;
226 a useful helper function for printing idl unions via DEBUG()
228 _PUBLIC_
void ndr_print_union_debug(ndr_print_fn_t fn
, const char *name
, uint32_t level
, void *ptr
)
230 struct ndr_print
*ndr
;
234 ndr
= talloc_zero(NULL
, struct ndr_print
);
236 ndr
->print
= ndr_print_debug_helper
;
239 ndr_print_set_switch_value(ndr
, ptr
, level
);
245 a useful helper function for printing idl function calls via DEBUG()
247 _PUBLIC_
void ndr_print_function_debug(ndr_print_function_t fn
, const char *name
, int flags
, void *ptr
)
249 struct ndr_print
*ndr
;
253 ndr
= talloc_zero(NULL
, struct ndr_print
);
255 ndr
->print
= ndr_print_debug_helper
;
258 fn(ndr
, name
, flags
, ptr
);
263 a useful helper function for printing idl structures to a string
265 _PUBLIC_
char *ndr_print_struct_string(TALLOC_CTX
*mem_ctx
, ndr_print_fn_t fn
, const char *name
, void *ptr
)
267 struct ndr_print
*ndr
;
270 ndr
= talloc_zero(mem_ctx
, struct ndr_print
);
271 if (!ndr
) return NULL
;
272 ndr
->private_data
= talloc_strdup(ndr
, "");
273 if (!ndr
->private_data
) {
276 ndr
->print
= ndr_print_string_helper
;
280 ret
= talloc_steal(mem_ctx
, (char *)ndr
->private_data
);
287 a useful helper function for printing idl unions to a string
289 _PUBLIC_
char *ndr_print_union_string(TALLOC_CTX
*mem_ctx
, ndr_print_fn_t fn
, const char *name
, uint32_t level
, void *ptr
)
291 struct ndr_print
*ndr
;
294 ndr
= talloc_zero(mem_ctx
, struct ndr_print
);
295 if (!ndr
) return NULL
;
296 ndr
->private_data
= talloc_strdup(ndr
, "");
297 if (!ndr
->private_data
) {
300 ndr
->print
= ndr_print_string_helper
;
303 ndr_print_set_switch_value(ndr
, ptr
, level
);
305 ret
= talloc_steal(mem_ctx
, (char *)ndr
->private_data
);
312 a useful helper function for printing idl function calls to a string
314 _PUBLIC_
char *ndr_print_function_string(TALLOC_CTX
*mem_ctx
,
315 ndr_print_function_t fn
, const char *name
,
316 int flags
, void *ptr
)
318 struct ndr_print
*ndr
;
321 ndr
= talloc_zero(mem_ctx
, struct ndr_print
);
322 if (!ndr
) return NULL
;
323 ndr
->private_data
= talloc_strdup(ndr
, "");
324 if (!ndr
->private_data
) {
327 ndr
->print
= ndr_print_string_helper
;
330 fn(ndr
, name
, flags
, ptr
);
331 ret
= talloc_steal(mem_ctx
, (char *)ndr
->private_data
);
337 _PUBLIC_
void ndr_set_flags(uint32_t *pflags
, uint32_t new_flags
)
339 /* the big/little endian flags are inter-dependent */
340 if (new_flags
& LIBNDR_FLAG_LITTLE_ENDIAN
) {
341 (*pflags
) &= ~LIBNDR_FLAG_BIGENDIAN
;
343 if (new_flags
& LIBNDR_FLAG_BIGENDIAN
) {
344 (*pflags
) &= ~LIBNDR_FLAG_LITTLE_ENDIAN
;
346 if (new_flags
& LIBNDR_FLAG_REMAINING
) {
347 (*pflags
) &= ~LIBNDR_ALIGN_FLAGS
;
349 if (new_flags
& LIBNDR_ALIGN_FLAGS
) {
350 (*pflags
) &= ~LIBNDR_FLAG_REMAINING
;
352 if (new_flags
& LIBNDR_FLAG_NO_RELATIVE_REVERSE
) {
353 (*pflags
) &= ~LIBNDR_FLAG_RELATIVE_REVERSE
;
355 (*pflags
) |= new_flags
;
359 return and possibly log an NDR error
361 _PUBLIC_
enum ndr_err_code
ndr_pull_error(struct ndr_pull
*ndr
,
362 enum ndr_err_code ndr_err
,
363 const char *format
, ...)
369 va_start(ap
, format
);
370 ret
= vasprintf(&s
, format
, ap
);
374 return NDR_ERR_ALLOC
;
377 DEBUG(1,("ndr_pull_error(%u): %s\n", ndr_err
, s
));
385 return and possibly log an NDR error
387 _PUBLIC_
enum ndr_err_code
ndr_push_error(struct ndr_push
*ndr
,
388 enum ndr_err_code ndr_err
,
389 const char *format
, ...)
395 va_start(ap
, format
);
396 ret
= vasprintf(&s
, format
, ap
);
400 return NDR_ERR_ALLOC
;
403 DEBUG(1,("ndr_push_error(%u): %s\n", ndr_err
, s
));
411 handle subcontext buffers, which in midl land are user-marshalled, but
412 we use magic in pidl to make them easier to cope with
414 _PUBLIC_
enum ndr_err_code
ndr_pull_subcontext_start(struct ndr_pull
*ndr
,
415 struct ndr_pull
**_subndr
,
419 struct ndr_pull
*subndr
;
420 uint32_t r_content_size
;
421 bool force_le
= false;
422 bool force_be
= false;
424 switch (header_size
) {
426 uint32_t content_size
= ndr
->data_size
- ndr
->offset
;
428 content_size
= size_is
;
430 r_content_size
= content_size
;
435 uint16_t content_size
;
436 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, &content_size
));
437 if (size_is
>= 0 && size_is
!= content_size
) {
438 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
439 (int)size_is
, (int)content_size
);
441 r_content_size
= content_size
;
446 uint32_t content_size
;
447 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &content_size
));
448 if (size_is
>= 0 && size_is
!= content_size
) {
449 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
450 (int)size_is
, (int)content_size
);
452 r_content_size
= content_size
;
457 * Common Type Header for the Serialization Stream
458 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
464 uint32_t content_size
;
468 NDR_CHECK(ndr_pull_uint8(ndr
, NDR_SCALARS
, &version
));
471 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
472 "Bad subcontext (PULL) Common Type Header version %d != 1",
480 NDR_CHECK(ndr_pull_uint8(ndr
, NDR_SCALARS
, &drep
));
483 } else if (drep
== 0x00) {
486 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
487 "Bad subcontext (PULL) Common Type Header invalid drep 0x%02X",
491 /* length of the "Private Header for Constructed Type" */
492 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, &hdrlen
));
494 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
495 "Bad subcontext (PULL) Common Type Header length %d != 8",
499 /* filler should be ignored */
500 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &filler
));
503 * Private Header for Constructed Type
505 /* length - will be updated latter */
506 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &content_size
));
507 if (size_is
>= 0 && size_is
!= content_size
) {
508 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
509 (int)size_is
, (int)content_size
);
511 /* the content size must be a multiple of 8 */
512 if ((content_size
% 8) != 0) {
513 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
514 "Bad subcontext (PULL) size_is(%d) not padded to 8 content_size %d",
515 (int)size_is
, (int)content_size
);
517 r_content_size
= content_size
;
520 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &reserved
));
524 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) header_size %d",
528 NDR_PULL_NEED_BYTES(ndr
, r_content_size
);
530 subndr
= talloc_zero(ndr
, struct ndr_pull
);
531 NDR_ERR_HAVE_NO_MEMORY(subndr
);
532 subndr
->flags
= ndr
->flags
;
533 subndr
->current_mem_ctx
= ndr
->current_mem_ctx
;
535 subndr
->data
= ndr
->data
+ ndr
->offset
;
537 subndr
->data_size
= r_content_size
;
538 subndr
->iconv_convenience
= talloc_reference(subndr
, ndr
->iconv_convenience
);
541 ndr_set_flags(&ndr
->flags
, LIBNDR_FLAG_LITTLE_ENDIAN
);
542 } else if (force_be
) {
543 ndr_set_flags(&ndr
->flags
, LIBNDR_FLAG_BIGENDIAN
);
547 return NDR_ERR_SUCCESS
;
550 _PUBLIC_
enum ndr_err_code
ndr_pull_subcontext_end(struct ndr_pull
*ndr
,
551 struct ndr_pull
*subndr
,
558 } else if (header_size
> 0) {
559 advance
= subndr
->data_size
;
561 advance
= subndr
->offset
;
563 NDR_CHECK(ndr_pull_advance(ndr
, advance
));
564 return NDR_ERR_SUCCESS
;
567 _PUBLIC_
enum ndr_err_code
ndr_push_subcontext_start(struct ndr_push
*ndr
,
568 struct ndr_push
**_subndr
,
572 struct ndr_push
*subndr
;
574 subndr
= ndr_push_init_ctx(ndr
, ndr
->iconv_convenience
);
575 NDR_ERR_HAVE_NO_MEMORY(subndr
);
576 subndr
->flags
= ndr
->flags
;
579 NDR_CHECK(ndr_push_zero(subndr
, size_is
));
581 subndr
->relative_end_offset
= size_is
;
585 return NDR_ERR_SUCCESS
;
589 push a subcontext header
591 _PUBLIC_
enum ndr_err_code
ndr_push_subcontext_end(struct ndr_push
*ndr
,
592 struct ndr_push
*subndr
,
599 padding_len
= size_is
- subndr
->offset
;
600 if (padding_len
< 0) {
601 return ndr_push_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
602 (int)subndr
->offset
, (int)size_is
);
604 subndr
->offset
= size_is
;
607 switch (header_size
) {
612 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, subndr
->offset
));
616 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, subndr
->offset
));
621 * Common Type Header for the Serialization Stream
622 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
624 padding_len
= NDR_ROUND(subndr
->offset
, 8) - subndr
->offset
;
625 if (padding_len
> 0) {
626 NDR_CHECK(ndr_push_zero(subndr
, padding_len
));
630 NDR_CHECK(ndr_push_uint8(ndr
, NDR_SCALARS
, 1));
636 NDR_CHECK(ndr_push_uint8(ndr
, NDR_SCALARS
, NDR_BE(ndr
)?0x00:0x10));
638 /* length of the "Private Header for Constructed Type" */
639 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, 8));
642 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0xCCCCCCCC));
645 * Private Header for Constructed Type
647 /* length - will be updated latter */
648 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, subndr
->offset
));
651 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0));
655 return ndr_push_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext header size %d",
659 NDR_CHECK(ndr_push_bytes(ndr
, subndr
->data
, subndr
->offset
));
660 return NDR_ERR_SUCCESS
;
664 store a token in the ndr context, for later retrieval
666 _PUBLIC_
enum ndr_err_code
ndr_token_store(TALLOC_CTX
*mem_ctx
,
667 struct ndr_token_list
**list
,
671 struct ndr_token_list
*tok
;
672 tok
= talloc(mem_ctx
, struct ndr_token_list
);
673 NDR_ERR_HAVE_NO_MEMORY(tok
);
676 DLIST_ADD((*list
), tok
);
677 return NDR_ERR_SUCCESS
;
681 retrieve a token from a ndr context, using cmp_fn to match the tokens
683 _PUBLIC_
enum ndr_err_code
ndr_token_retrieve_cmp_fn(struct ndr_token_list
**list
, const void *key
, uint32_t *v
,
684 comparison_fn_t _cmp_fn
, bool _remove_tok
)
686 struct ndr_token_list
*tok
;
687 for (tok
=*list
;tok
;tok
=tok
->next
) {
688 if (_cmp_fn
&& _cmp_fn(tok
->key
,key
)==0) goto found
;
689 else if (!_cmp_fn
&& tok
->key
== key
) goto found
;
691 return NDR_ERR_TOKEN
;
695 DLIST_REMOVE((*list
), tok
);
698 return NDR_ERR_SUCCESS
;
702 retrieve a token from a ndr context
704 _PUBLIC_
enum ndr_err_code
ndr_token_retrieve(struct ndr_token_list
**list
, const void *key
, uint32_t *v
)
706 return ndr_token_retrieve_cmp_fn(list
, key
, v
, NULL
, true);
710 peek at but don't removed a token from a ndr context
712 _PUBLIC_
uint32_t ndr_token_peek(struct ndr_token_list
**list
, const void *key
)
714 enum ndr_err_code status
;
717 status
= ndr_token_retrieve_cmp_fn(list
, key
, &v
, NULL
, false);
718 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
726 pull an array size field and add it to the array_size_list token list
728 _PUBLIC_
enum ndr_err_code
ndr_pull_array_size(struct ndr_pull
*ndr
, const void *p
)
731 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &size
));
732 return ndr_token_store(ndr
, &ndr
->array_size_list
, p
, size
);
736 get the stored array size field
738 _PUBLIC_
uint32_t ndr_get_array_size(struct ndr_pull
*ndr
, const void *p
)
740 return ndr_token_peek(&ndr
->array_size_list
, p
);
744 check the stored array size field
746 _PUBLIC_
enum ndr_err_code
ndr_check_array_size(struct ndr_pull
*ndr
, void *p
, uint32_t size
)
749 stored
= ndr_token_peek(&ndr
->array_size_list
, p
);
750 if (stored
!= size
) {
751 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
752 "Bad array size - got %u expected %u\n",
755 return NDR_ERR_SUCCESS
;
759 pull an array length field and add it to the array_length_list token list
761 _PUBLIC_
enum ndr_err_code
ndr_pull_array_length(struct ndr_pull
*ndr
, const void *p
)
763 uint32_t length
, offset
;
764 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &offset
));
766 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
767 "non-zero array offset %u\n", offset
);
769 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &length
));
770 return ndr_token_store(ndr
, &ndr
->array_length_list
, p
, length
);
774 get the stored array length field
776 _PUBLIC_
uint32_t ndr_get_array_length(struct ndr_pull
*ndr
, const void *p
)
778 return ndr_token_peek(&ndr
->array_length_list
, p
);
782 check the stored array length field
784 _PUBLIC_
enum ndr_err_code
ndr_check_array_length(struct ndr_pull
*ndr
, void *p
, uint32_t length
)
787 stored
= ndr_token_peek(&ndr
->array_length_list
, p
);
788 if (stored
!= length
) {
789 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
790 "Bad array length - got %u expected %u\n",
793 return NDR_ERR_SUCCESS
;
799 _PUBLIC_
enum ndr_err_code
ndr_push_set_switch_value(struct ndr_push
*ndr
, const void *p
, uint32_t val
)
801 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
804 _PUBLIC_
enum ndr_err_code
ndr_pull_set_switch_value(struct ndr_pull
*ndr
, const void *p
, uint32_t val
)
806 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
809 _PUBLIC_
enum ndr_err_code
ndr_print_set_switch_value(struct ndr_print
*ndr
, const void *p
, uint32_t val
)
811 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
815 retrieve a switch value
817 _PUBLIC_
uint32_t ndr_push_get_switch_value(struct ndr_push
*ndr
, const void *p
)
819 return ndr_token_peek(&ndr
->switch_list
, p
);
822 _PUBLIC_
uint32_t ndr_pull_get_switch_value(struct ndr_pull
*ndr
, const void *p
)
824 return ndr_token_peek(&ndr
->switch_list
, p
);
827 _PUBLIC_
uint32_t ndr_print_get_switch_value(struct ndr_print
*ndr
, const void *p
)
829 return ndr_token_peek(&ndr
->switch_list
, p
);
833 pull a struct from a blob using NDR
835 _PUBLIC_
enum ndr_err_code
ndr_pull_struct_blob(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
, struct smb_iconv_convenience
*iconv_convenience
, void *p
,
836 ndr_pull_flags_fn_t fn
)
838 struct ndr_pull
*ndr
;
839 ndr
= ndr_pull_init_blob(blob
, mem_ctx
, iconv_convenience
);
840 NDR_ERR_HAVE_NO_MEMORY(ndr
);
841 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
842 return NDR_ERR_SUCCESS
;
846 pull a struct from a blob using NDR - failing if all bytes are not consumed
848 _PUBLIC_
enum ndr_err_code
ndr_pull_struct_blob_all(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
849 struct smb_iconv_convenience
*iconv_convenience
,
850 void *p
, ndr_pull_flags_fn_t fn
)
852 struct ndr_pull
*ndr
;
853 ndr
= ndr_pull_init_blob(blob
, mem_ctx
, iconv_convenience
);
854 NDR_ERR_HAVE_NO_MEMORY(ndr
);
855 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
856 if (ndr
->offset
< ndr
->data_size
) {
857 return ndr_pull_error(ndr
, NDR_ERR_UNREAD_BYTES
,
858 "not all bytes consumed ofs[%u] size[%u]",
859 ndr
->offset
, ndr
->data_size
);
861 return NDR_ERR_SUCCESS
;
865 pull a union from a blob using NDR, given the union discriminator
867 _PUBLIC_
enum ndr_err_code
ndr_pull_union_blob(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
868 struct smb_iconv_convenience
*iconv_convenience
, void *p
,
869 uint32_t level
, ndr_pull_flags_fn_t fn
)
871 struct ndr_pull
*ndr
;
872 ndr
= ndr_pull_init_blob(blob
, mem_ctx
, iconv_convenience
);
873 NDR_ERR_HAVE_NO_MEMORY(ndr
);
874 NDR_CHECK(ndr_pull_set_switch_value(ndr
, p
, level
));
875 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
876 return NDR_ERR_SUCCESS
;
880 pull a union from a blob using NDR, given the union discriminator,
881 failing if all bytes are not consumed
883 _PUBLIC_
enum ndr_err_code
ndr_pull_union_blob_all(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
884 struct smb_iconv_convenience
*iconv_convenience
, void *p
,
885 uint32_t level
, ndr_pull_flags_fn_t fn
)
887 struct ndr_pull
*ndr
;
888 ndr
= ndr_pull_init_blob(blob
, mem_ctx
, iconv_convenience
);
889 NDR_ERR_HAVE_NO_MEMORY(ndr
);
890 NDR_CHECK(ndr_pull_set_switch_value(ndr
, p
, level
));
891 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
892 if (ndr
->offset
< ndr
->data_size
) {
893 return ndr_pull_error(ndr
, NDR_ERR_UNREAD_BYTES
,
894 "not all bytes consumed ofs[%u] size[%u]",
895 ndr
->offset
, ndr
->data_size
);
897 return NDR_ERR_SUCCESS
;
901 push a struct to a blob using NDR
903 _PUBLIC_
enum ndr_err_code
ndr_push_struct_blob(DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
, struct smb_iconv_convenience
*iconv_convenience
, const void *p
, ndr_push_flags_fn_t fn
)
905 struct ndr_push
*ndr
;
906 ndr
= ndr_push_init_ctx(mem_ctx
, iconv_convenience
);
907 NDR_ERR_HAVE_NO_MEMORY(ndr
);
909 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
911 *blob
= ndr_push_blob(ndr
);
912 talloc_steal(mem_ctx
, blob
->data
);
915 return NDR_ERR_SUCCESS
;
919 push a union to a blob using NDR
921 _PUBLIC_
enum ndr_err_code
ndr_push_union_blob(DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
, struct smb_iconv_convenience
*iconv_convenience
, void *p
,
922 uint32_t level
, ndr_push_flags_fn_t fn
)
924 struct ndr_push
*ndr
;
925 ndr
= ndr_push_init_ctx(mem_ctx
, iconv_convenience
);
926 NDR_ERR_HAVE_NO_MEMORY(ndr
);
928 NDR_CHECK(ndr_push_set_switch_value(ndr
, p
, level
));
929 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
931 *blob
= ndr_push_blob(ndr
);
932 talloc_steal(mem_ctx
, blob
->data
);
935 return NDR_ERR_SUCCESS
;
939 generic ndr_size_*() handler for structures
941 _PUBLIC_
size_t ndr_size_struct(const void *p
, int flags
, ndr_push_flags_fn_t push
, struct smb_iconv_convenience
*iconv_convenience
)
943 struct ndr_push
*ndr
;
944 enum ndr_err_code status
;
947 /* avoid recursion */
948 if (flags
& LIBNDR_FLAG_NO_NDR_SIZE
) return 0;
950 ndr
= ndr_push_init_ctx(NULL
, iconv_convenience
);
952 ndr
->flags
|= flags
| LIBNDR_FLAG_NO_NDR_SIZE
;
953 status
= push(ndr
, NDR_SCALARS
|NDR_BUFFERS
, discard_const(p
));
954 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
964 generic ndr_size_*() handler for unions
966 _PUBLIC_
size_t ndr_size_union(const void *p
, int flags
, uint32_t level
, ndr_push_flags_fn_t push
, struct smb_iconv_convenience
*iconv_convenience
)
968 struct ndr_push
*ndr
;
969 enum ndr_err_code status
;
972 /* avoid recursion */
973 if (flags
& LIBNDR_FLAG_NO_NDR_SIZE
) return 0;
975 ndr
= ndr_push_init_ctx(NULL
, iconv_convenience
);
977 ndr
->flags
|= flags
| LIBNDR_FLAG_NO_NDR_SIZE
;
979 status
= ndr_push_set_switch_value(ndr
, p
, level
);
980 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
984 status
= push(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
);
985 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
995 get the current base for relative pointers for the push
997 _PUBLIC_
uint32_t ndr_push_get_relative_base_offset(struct ndr_push
*ndr
)
999 return ndr
->relative_base_offset
;
1003 restore the old base for relative pointers for the push
1005 _PUBLIC_
void ndr_push_restore_relative_base_offset(struct ndr_push
*ndr
, uint32_t offset
)
1007 ndr
->relative_base_offset
= offset
;
1011 setup the current base for relative pointers for the push
1012 called in the NDR_SCALAR stage
1014 _PUBLIC_
enum ndr_err_code
ndr_push_setup_relative_base_offset1(struct ndr_push
*ndr
, const void *p
, uint32_t offset
)
1016 ndr
->relative_base_offset
= offset
;
1017 return ndr_token_store(ndr
, &ndr
->relative_base_list
, p
, offset
);
1021 setup the current base for relative pointers for the push
1022 called in the NDR_BUFFERS stage
1024 _PUBLIC_
enum ndr_err_code
ndr_push_setup_relative_base_offset2(struct ndr_push
*ndr
, const void *p
)
1026 return ndr_token_retrieve(&ndr
->relative_base_list
, p
, &ndr
->relative_base_offset
);
1030 push a relative object - stage1
1031 this is called during SCALARS processing
1033 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr1(struct ndr_push
*ndr
, const void *p
)
1036 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0));
1037 return NDR_ERR_SUCCESS
;
1039 NDR_CHECK(ndr_push_align(ndr
, 4));
1040 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_list
, p
, ndr
->offset
));
1041 return ndr_push_uint32(ndr
, NDR_SCALARS
, 0xFFFFFFFF);
1045 push a relative object - stage2
1046 this is called during buffers processing
1048 static enum ndr_err_code
ndr_push_relative_ptr2(struct ndr_push
*ndr
, const void *p
)
1050 uint32_t save_offset
;
1051 uint32_t ptr_offset
= 0xFFFFFFFF;
1053 return NDR_ERR_SUCCESS
;
1055 save_offset
= ndr
->offset
;
1056 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &ptr_offset
));
1057 if (ptr_offset
> ndr
->offset
) {
1058 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1059 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1060 ptr_offset
, ndr
->offset
);
1062 ndr
->offset
= ptr_offset
;
1063 if (save_offset
< ndr
->relative_base_offset
) {
1064 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1065 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1066 save_offset
, ndr
->relative_base_offset
);
1068 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, save_offset
- ndr
->relative_base_offset
));
1069 ndr
->offset
= save_offset
;
1070 return NDR_ERR_SUCCESS
;
1074 push a relative object - stage2 start
1075 this is called during buffers processing
1077 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr2_start(struct ndr_push
*ndr
, const void *p
)
1080 return NDR_ERR_SUCCESS
;
1082 if (!(ndr
->flags
& LIBNDR_FLAG_RELATIVE_REVERSE
)) {
1083 return ndr_push_relative_ptr2(ndr
, p
);
1085 if (ndr
->relative_end_offset
== -1) {
1086 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1087 "ndr_push_relative_ptr2_start RELATIVE_REVERSE flag set and relative_end_offset %d",
1088 ndr
->relative_end_offset
);
1090 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_begin_list
, p
, ndr
->offset
));
1091 return NDR_ERR_SUCCESS
;
1095 push a relative object - stage2 end
1096 this is called during buffers processing
1098 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr2_end(struct ndr_push
*ndr
, const void *p
)
1100 uint32_t begin_offset
= 0xFFFFFFFF;
1102 uint32_t correct_offset
= 0;
1107 return NDR_ERR_SUCCESS
;
1110 if (!(ndr
->flags
& LIBNDR_FLAG_RELATIVE_REVERSE
)) {
1111 return NDR_ERR_SUCCESS
;
1114 if (ndr
->flags
& LIBNDR_FLAG_NO_NDR_SIZE
) {
1115 /* better say more than calculation a too small buffer */
1116 NDR_PUSH_ALIGN(ndr
, 8);
1117 return NDR_ERR_SUCCESS
;
1120 if (ndr
->relative_end_offset
< ndr
->offset
) {
1121 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1122 "ndr_push_relative_ptr2_end:"
1123 "relative_end_offset %u < offset %u",
1124 ndr
->relative_end_offset
, ndr
->offset
);
1127 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_begin_list
, p
, &begin_offset
));
1129 /* we have marshalled a buffer, see how long it was */
1130 len
= ndr
->offset
- begin_offset
;
1133 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1134 "ndr_push_relative_ptr2_end:"
1135 "offset %u - begin_offset %u < 0",
1136 ndr
->offset
, begin_offset
);
1139 if (ndr
->relative_end_offset
< len
) {
1140 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1141 "ndr_push_relative_ptr2_end:"
1142 "relative_end_offset %u < len %lld",
1143 ndr
->offset
, (long long)len
);
1146 /* the reversed offset is at the end of the main buffer */
1147 correct_offset
= ndr
->relative_end_offset
- len
;
1149 /* TODO: remove this hack and let the idl use FLAG_ALIGN2 explicit */
1152 if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
1154 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
1156 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
1160 pad
= ndr_align_size(correct_offset
, align
);
1162 correct_offset
+= pad
;
1163 correct_offset
-= align
;
1166 if (correct_offset
< begin_offset
) {
1167 return ndr_push_error(ndr
, NDR_ERR_RELATIVE
,
1168 "ndr_push_relative_ptr2_end: "
1169 "correct_offset %u < begin_offset %u",
1170 correct_offset
, begin_offset
);
1174 /* now move the marshalled buffer to the end of the main buffer */
1175 memmove(ndr
->data
+ correct_offset
, ndr
->data
+ begin_offset
, len
);
1177 /* and wipe out old buffer within the main buffer */
1178 memset(ndr
->data
+ begin_offset
, '\0', len
);
1181 /* and set the end offset for the next buffer */
1182 ndr
->relative_end_offset
= correct_offset
;
1184 /* finally write the offset to the main buffer */
1185 ndr
->offset
= correct_offset
;
1186 NDR_CHECK(ndr_push_relative_ptr2(ndr
, p
));
1188 /* restore to where we were in the main buffer */
1189 ndr
->offset
= begin_offset
;
1191 return NDR_ERR_SUCCESS
;
1195 get the current base for relative pointers for the pull
1197 _PUBLIC_
uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull
*ndr
)
1199 return ndr
->relative_base_offset
;
1203 restore the old base for relative pointers for the pull
1205 _PUBLIC_
void ndr_pull_restore_relative_base_offset(struct ndr_pull
*ndr
, uint32_t offset
)
1207 ndr
->relative_base_offset
= offset
;
1211 setup the current base for relative pointers for the pull
1212 called in the NDR_SCALAR stage
1214 _PUBLIC_
enum ndr_err_code
ndr_pull_setup_relative_base_offset1(struct ndr_pull
*ndr
, const void *p
, uint32_t offset
)
1216 ndr
->relative_base_offset
= offset
;
1217 return ndr_token_store(ndr
, &ndr
->relative_base_list
, p
, offset
);
1221 setup the current base for relative pointers for the pull
1222 called in the NDR_BUFFERS stage
1224 _PUBLIC_
enum ndr_err_code
ndr_pull_setup_relative_base_offset2(struct ndr_pull
*ndr
, const void *p
)
1226 return ndr_token_retrieve(&ndr
->relative_base_list
, p
, &ndr
->relative_base_offset
);
1230 pull a relative object - stage1
1231 called during SCALARS processing
1233 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr1(struct ndr_pull
*ndr
, const void *p
, uint32_t rel_offset
)
1235 rel_offset
+= ndr
->relative_base_offset
;
1236 if (rel_offset
> ndr
->data_size
) {
1237 return ndr_pull_error(ndr
, NDR_ERR_BUFSIZE
,
1238 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1239 rel_offset
, ndr
->data_size
);
1241 return ndr_token_store(ndr
, &ndr
->relative_list
, p
, rel_offset
);
1245 pull a relative object - stage2
1246 called during BUFFERS processing
1248 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr2(struct ndr_pull
*ndr
, const void *p
)
1250 uint32_t rel_offset
;
1251 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &rel_offset
));
1252 return ndr_pull_set_offset(ndr
, rel_offset
);
1255 const static struct {
1256 enum ndr_err_code err
;
1258 } ndr_err_code_strings
[] = {
1259 { NDR_ERR_SUCCESS
, "Success" },
1260 { NDR_ERR_ARRAY_SIZE
, "Bad Array Size" },
1261 { NDR_ERR_BAD_SWITCH
, "Bad Switch" },
1262 { NDR_ERR_OFFSET
, "Offset Error" },
1263 { NDR_ERR_RELATIVE
, "Relative Pointer Error" },
1264 { NDR_ERR_CHARCNV
, "Character Conversion Error" },
1265 { NDR_ERR_LENGTH
, "Length Error" },
1266 { NDR_ERR_SUBCONTEXT
, "Subcontext Error" },
1267 { NDR_ERR_COMPRESSION
, "Compression Error" },
1268 { NDR_ERR_STRING
, "String Error" },
1269 { NDR_ERR_VALIDATE
, "Validate Error" },
1270 { NDR_ERR_BUFSIZE
, "Buffer Size Error" },
1271 { NDR_ERR_ALLOC
, "Allocation Error" },
1272 { NDR_ERR_RANGE
, "Range Error" },
1273 { NDR_ERR_TOKEN
, "Token Error" },
1274 { NDR_ERR_IPV4ADDRESS
, "IPv4 Address Error" },
1275 { NDR_ERR_INVALID_POINTER
, "Invalid Pointer" },
1276 { NDR_ERR_UNREAD_BYTES
, "Unread Bytes" },
1280 _PUBLIC_
const char *ndr_map_error2string(enum ndr_err_code ndr_err
)
1283 for (i
= 0; ndr_err_code_strings
[i
].string
!= NULL
; i
++) {
1284 if (ndr_err_code_strings
[i
].err
== ndr_err
)
1285 return ndr_err_code_strings
[i
].string
;
1287 return "Unknown error";