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
;
259 /* this is a s4 hack until we build up the courage to pass
260 * this all the way down
262 #if _SAMBA_BUILD_ == 4
263 ndr
->iconv_convenience
= smb_iconv_convenience_init(talloc_autofree_context(), "ASCII", "UTF-8", true);
266 fn(ndr
, name
, flags
, ptr
);
271 a useful helper function for printing idl structures to a string
273 _PUBLIC_
char *ndr_print_struct_string(TALLOC_CTX
*mem_ctx
, ndr_print_fn_t fn
, const char *name
, void *ptr
)
275 struct ndr_print
*ndr
;
278 ndr
= talloc_zero(mem_ctx
, struct ndr_print
);
279 if (!ndr
) return NULL
;
280 ndr
->private_data
= talloc_strdup(ndr
, "");
281 if (!ndr
->private_data
) {
284 ndr
->print
= ndr_print_string_helper
;
288 /* this is a s4 hack until we build up the courage to pass
289 * this all the way down
291 #if _SAMBA_BUILD_ == 4
292 ndr
->iconv_convenience
= smb_iconv_convenience_init(talloc_autofree_context(), "ASCII", "UTF-8", true);
296 ret
= talloc_steal(mem_ctx
, (char *)ndr
->private_data
);
303 a useful helper function for printing idl unions to a string
305 _PUBLIC_
char *ndr_print_union_string(TALLOC_CTX
*mem_ctx
, ndr_print_fn_t fn
, const char *name
, uint32_t level
, void *ptr
)
307 struct ndr_print
*ndr
;
310 ndr
= talloc_zero(mem_ctx
, struct ndr_print
);
311 if (!ndr
) return NULL
;
312 ndr
->private_data
= talloc_strdup(ndr
, "");
313 if (!ndr
->private_data
) {
316 ndr
->print
= ndr_print_string_helper
;
319 ndr_print_set_switch_value(ndr
, ptr
, level
);
321 ret
= talloc_steal(mem_ctx
, (char *)ndr
->private_data
);
328 a useful helper function for printing idl function calls to a string
330 _PUBLIC_
char *ndr_print_function_string(TALLOC_CTX
*mem_ctx
,
331 ndr_print_function_t fn
, const char *name
,
332 int flags
, void *ptr
)
334 struct ndr_print
*ndr
;
337 ndr
= talloc_zero(mem_ctx
, struct ndr_print
);
338 if (!ndr
) return NULL
;
339 ndr
->private_data
= talloc_strdup(ndr
, "");
340 if (!ndr
->private_data
) {
343 ndr
->print
= ndr_print_string_helper
;
346 fn(ndr
, name
, flags
, ptr
);
347 ret
= talloc_steal(mem_ctx
, (char *)ndr
->private_data
);
353 _PUBLIC_
void ndr_set_flags(uint32_t *pflags
, uint32_t new_flags
)
355 /* the big/little endian flags are inter-dependent */
356 if (new_flags
& LIBNDR_FLAG_LITTLE_ENDIAN
) {
357 (*pflags
) &= ~LIBNDR_FLAG_BIGENDIAN
;
359 if (new_flags
& LIBNDR_FLAG_BIGENDIAN
) {
360 (*pflags
) &= ~LIBNDR_FLAG_LITTLE_ENDIAN
;
362 if (new_flags
& LIBNDR_FLAG_REMAINING
) {
363 (*pflags
) &= ~LIBNDR_ALIGN_FLAGS
;
365 if (new_flags
& LIBNDR_ALIGN_FLAGS
) {
366 (*pflags
) &= ~LIBNDR_FLAG_REMAINING
;
368 (*pflags
) |= new_flags
;
372 return and possibly log an NDR error
374 _PUBLIC_
enum ndr_err_code
ndr_pull_error(struct ndr_pull
*ndr
,
375 enum ndr_err_code ndr_err
,
376 const char *format
, ...)
382 va_start(ap
, format
);
383 ret
= vasprintf(&s
, format
, ap
);
387 return NDR_ERR_ALLOC
;
390 DEBUG(1,("ndr_pull_error(%u): %s\n", ndr_err
, s
));
398 return and possibly log an NDR error
400 _PUBLIC_
enum ndr_err_code
ndr_push_error(struct ndr_push
*ndr
,
401 enum ndr_err_code ndr_err
,
402 const char *format
, ...)
408 va_start(ap
, format
);
409 ret
= vasprintf(&s
, format
, ap
);
413 return NDR_ERR_ALLOC
;
416 DEBUG(1,("ndr_push_error(%u): %s\n", ndr_err
, s
));
424 handle subcontext buffers, which in midl land are user-marshalled, but
425 we use magic in pidl to make them easier to cope with
427 _PUBLIC_
enum ndr_err_code
ndr_pull_subcontext_start(struct ndr_pull
*ndr
,
428 struct ndr_pull
**_subndr
,
432 struct ndr_pull
*subndr
;
433 uint32_t r_content_size
;
434 bool force_le
= false;
435 bool force_be
= false;
437 switch (header_size
) {
439 uint32_t content_size
= ndr
->data_size
- ndr
->offset
;
441 content_size
= size_is
;
443 r_content_size
= content_size
;
448 uint16_t content_size
;
449 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, &content_size
));
450 if (size_is
>= 0 && size_is
!= content_size
) {
451 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
452 (int)size_is
, (int)content_size
);
454 r_content_size
= content_size
;
459 uint32_t content_size
;
460 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &content_size
));
461 if (size_is
>= 0 && size_is
!= content_size
) {
462 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
463 (int)size_is
, (int)content_size
);
465 r_content_size
= content_size
;
470 * Common Type Header for the Serialization Stream
471 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
477 uint32_t content_size
;
481 NDR_CHECK(ndr_pull_uint8(ndr
, NDR_SCALARS
, &version
));
484 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
485 "Bad subcontext (PULL) Common Type Header version %d != 1",
493 NDR_CHECK(ndr_pull_uint8(ndr
, NDR_SCALARS
, &drep
));
496 } else if (drep
== 0x00) {
499 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
500 "Bad subcontext (PULL) Common Type Header invalid drep 0x%02X",
504 /* length of the "Private Header for Constructed Type" */
505 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, &hdrlen
));
507 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
508 "Bad subcontext (PULL) Common Type Header length %d != 8",
512 /* filler should be ignored */
513 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &filler
));
516 * Private Header for Constructed Type
518 /* length - will be updated latter */
519 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &content_size
));
520 if (size_is
>= 0 && size_is
!= content_size
) {
521 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
522 (int)size_is
, (int)content_size
);
524 /* the content size must be a multiple of 8 */
525 if ((content_size
% 8) != 0) {
526 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
527 "Bad subcontext (PULL) size_is(%d) not padded to 8 content_size %d",
528 (int)size_is
, (int)content_size
);
530 r_content_size
= content_size
;
533 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &reserved
));
537 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) header_size %d",
541 NDR_PULL_NEED_BYTES(ndr
, r_content_size
);
543 subndr
= talloc_zero(ndr
, struct ndr_pull
);
544 NDR_ERR_HAVE_NO_MEMORY(subndr
);
545 subndr
->flags
= ndr
->flags
;
546 subndr
->current_mem_ctx
= ndr
->current_mem_ctx
;
548 subndr
->data
= ndr
->data
+ ndr
->offset
;
550 subndr
->data_size
= r_content_size
;
551 subndr
->iconv_convenience
= talloc_reference(subndr
, ndr
->iconv_convenience
);
554 ndr_set_flags(&ndr
->flags
, LIBNDR_FLAG_LITTLE_ENDIAN
);
555 } else if (force_be
) {
556 ndr_set_flags(&ndr
->flags
, LIBNDR_FLAG_BIGENDIAN
);
560 return NDR_ERR_SUCCESS
;
563 _PUBLIC_
enum ndr_err_code
ndr_pull_subcontext_end(struct ndr_pull
*ndr
,
564 struct ndr_pull
*subndr
,
571 } else if (header_size
> 0) {
572 advance
= subndr
->data_size
;
574 advance
= subndr
->offset
;
576 NDR_CHECK(ndr_pull_advance(ndr
, advance
));
577 return NDR_ERR_SUCCESS
;
580 _PUBLIC_
enum ndr_err_code
ndr_push_subcontext_start(struct ndr_push
*ndr
,
581 struct ndr_push
**_subndr
,
585 struct ndr_push
*subndr
;
587 subndr
= ndr_push_init_ctx(ndr
, ndr
->iconv_convenience
);
588 NDR_ERR_HAVE_NO_MEMORY(subndr
);
589 subndr
->flags
= ndr
->flags
;
592 return NDR_ERR_SUCCESS
;
596 push a subcontext header
598 _PUBLIC_
enum ndr_err_code
ndr_push_subcontext_end(struct ndr_push
*ndr
,
599 struct ndr_push
*subndr
,
606 padding_len
= size_is
- subndr
->offset
;
607 if (padding_len
> 0) {
608 NDR_CHECK(ndr_push_zero(subndr
, padding_len
));
609 } else if (padding_len
< 0) {
610 return ndr_push_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
611 (int)subndr
->offset
, (int)size_is
);
615 switch (header_size
) {
620 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, subndr
->offset
));
624 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, subndr
->offset
));
629 * Common Type Header for the Serialization Stream
630 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
632 padding_len
= NDR_ROUND(subndr
->offset
, 8) - subndr
->offset
;
633 if (padding_len
> 0) {
634 NDR_CHECK(ndr_push_zero(subndr
, padding_len
));
638 NDR_CHECK(ndr_push_uint8(ndr
, NDR_SCALARS
, 1));
644 NDR_CHECK(ndr_push_uint8(ndr
, NDR_SCALARS
, NDR_BE(ndr
)?0x00:0x10));
646 /* length of the "Private Header for Constructed Type" */
647 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, 8));
650 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0xCCCCCCCC));
653 * Private Header for Constructed Type
655 /* length - will be updated latter */
656 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, subndr
->offset
));
659 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0));
663 return ndr_push_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext header size %d",
667 NDR_CHECK(ndr_push_bytes(ndr
, subndr
->data
, subndr
->offset
));
668 return NDR_ERR_SUCCESS
;
672 store a token in the ndr context, for later retrieval
674 _PUBLIC_
enum ndr_err_code
ndr_token_store(TALLOC_CTX
*mem_ctx
,
675 struct ndr_token_list
**list
,
679 struct ndr_token_list
*tok
;
680 tok
= talloc(mem_ctx
, struct ndr_token_list
);
681 NDR_ERR_HAVE_NO_MEMORY(tok
);
684 DLIST_ADD((*list
), tok
);
685 return NDR_ERR_SUCCESS
;
689 retrieve a token from a ndr context, using cmp_fn to match the tokens
691 _PUBLIC_
enum ndr_err_code
ndr_token_retrieve_cmp_fn(struct ndr_token_list
**list
, const void *key
, uint32_t *v
,
692 comparison_fn_t _cmp_fn
, bool _remove_tok
)
694 struct ndr_token_list
*tok
;
695 for (tok
=*list
;tok
;tok
=tok
->next
) {
696 if (_cmp_fn
&& _cmp_fn(tok
->key
,key
)==0) goto found
;
697 else if (!_cmp_fn
&& tok
->key
== key
) goto found
;
699 return NDR_ERR_TOKEN
;
703 DLIST_REMOVE((*list
), tok
);
706 return NDR_ERR_SUCCESS
;
710 retrieve a token from a ndr context
712 _PUBLIC_
enum ndr_err_code
ndr_token_retrieve(struct ndr_token_list
**list
, const void *key
, uint32_t *v
)
714 return ndr_token_retrieve_cmp_fn(list
, key
, v
, NULL
, true);
718 peek at but don't removed a token from a ndr context
720 _PUBLIC_
uint32_t ndr_token_peek(struct ndr_token_list
**list
, const void *key
)
722 enum ndr_err_code status
;
725 status
= ndr_token_retrieve_cmp_fn(list
, key
, &v
, NULL
, false);
726 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
734 pull an array size field and add it to the array_size_list token list
736 _PUBLIC_
enum ndr_err_code
ndr_pull_array_size(struct ndr_pull
*ndr
, const void *p
)
739 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &size
));
740 return ndr_token_store(ndr
, &ndr
->array_size_list
, p
, size
);
744 get the stored array size field
746 _PUBLIC_
uint32_t ndr_get_array_size(struct ndr_pull
*ndr
, const void *p
)
748 return ndr_token_peek(&ndr
->array_size_list
, p
);
752 check the stored array size field
754 _PUBLIC_
enum ndr_err_code
ndr_check_array_size(struct ndr_pull
*ndr
, void *p
, uint32_t size
)
757 stored
= ndr_token_peek(&ndr
->array_size_list
, p
);
758 if (stored
!= size
) {
759 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
760 "Bad array size - got %u expected %u\n",
763 return NDR_ERR_SUCCESS
;
767 pull an array length field and add it to the array_length_list token list
769 _PUBLIC_
enum ndr_err_code
ndr_pull_array_length(struct ndr_pull
*ndr
, const void *p
)
771 uint32_t length
, offset
;
772 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &offset
));
774 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
775 "non-zero array offset %u\n", offset
);
777 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &length
));
778 return ndr_token_store(ndr
, &ndr
->array_length_list
, p
, length
);
782 get the stored array length field
784 _PUBLIC_
uint32_t ndr_get_array_length(struct ndr_pull
*ndr
, const void *p
)
786 return ndr_token_peek(&ndr
->array_length_list
, p
);
790 check the stored array length field
792 _PUBLIC_
enum ndr_err_code
ndr_check_array_length(struct ndr_pull
*ndr
, void *p
, uint32_t length
)
795 stored
= ndr_token_peek(&ndr
->array_length_list
, p
);
796 if (stored
!= length
) {
797 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
798 "Bad array length - got %u expected %u\n",
801 return NDR_ERR_SUCCESS
;
807 _PUBLIC_
enum ndr_err_code
ndr_push_set_switch_value(struct ndr_push
*ndr
, const void *p
, uint32_t val
)
809 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
812 _PUBLIC_
enum ndr_err_code
ndr_pull_set_switch_value(struct ndr_pull
*ndr
, const void *p
, uint32_t val
)
814 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
817 _PUBLIC_
enum ndr_err_code
ndr_print_set_switch_value(struct ndr_print
*ndr
, const void *p
, uint32_t val
)
819 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
823 retrieve a switch value
825 _PUBLIC_
uint32_t ndr_push_get_switch_value(struct ndr_push
*ndr
, const void *p
)
827 return ndr_token_peek(&ndr
->switch_list
, p
);
830 _PUBLIC_
uint32_t ndr_pull_get_switch_value(struct ndr_pull
*ndr
, const void *p
)
832 return ndr_token_peek(&ndr
->switch_list
, p
);
835 _PUBLIC_
uint32_t ndr_print_get_switch_value(struct ndr_print
*ndr
, const void *p
)
837 return ndr_token_peek(&ndr
->switch_list
, p
);
841 pull a struct from a blob using NDR
843 _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
,
844 ndr_pull_flags_fn_t fn
)
846 struct ndr_pull
*ndr
;
847 ndr
= ndr_pull_init_blob(blob
, mem_ctx
, iconv_convenience
);
848 NDR_ERR_HAVE_NO_MEMORY(ndr
);
849 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
850 return NDR_ERR_SUCCESS
;
854 pull a struct from a blob using NDR - failing if all bytes are not consumed
856 _PUBLIC_
enum ndr_err_code
ndr_pull_struct_blob_all(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
857 struct smb_iconv_convenience
*iconv_convenience
,
858 void *p
, ndr_pull_flags_fn_t fn
)
860 struct ndr_pull
*ndr
;
861 ndr
= ndr_pull_init_blob(blob
, mem_ctx
, iconv_convenience
);
862 NDR_ERR_HAVE_NO_MEMORY(ndr
);
863 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
864 if (ndr
->offset
< ndr
->data_size
) {
865 return ndr_pull_error(ndr
, NDR_ERR_UNREAD_BYTES
,
866 "not all bytes consumed ofs[%u] size[%u]",
867 ndr
->offset
, ndr
->data_size
);
869 return NDR_ERR_SUCCESS
;
873 pull a union from a blob using NDR, given the union discriminator
875 _PUBLIC_
enum ndr_err_code
ndr_pull_union_blob(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
876 struct smb_iconv_convenience
*iconv_convenience
, void *p
,
877 uint32_t level
, ndr_pull_flags_fn_t fn
)
879 struct ndr_pull
*ndr
;
880 ndr
= ndr_pull_init_blob(blob
, mem_ctx
, iconv_convenience
);
881 NDR_ERR_HAVE_NO_MEMORY(ndr
);
882 NDR_CHECK(ndr_pull_set_switch_value(ndr
, p
, level
));
883 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
884 return NDR_ERR_SUCCESS
;
888 pull a union from a blob using NDR, given the union discriminator,
889 failing if all bytes are not consumed
891 _PUBLIC_
enum ndr_err_code
ndr_pull_union_blob_all(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
892 struct smb_iconv_convenience
*iconv_convenience
, void *p
,
893 uint32_t level
, ndr_pull_flags_fn_t fn
)
895 struct ndr_pull
*ndr
;
896 ndr
= ndr_pull_init_blob(blob
, mem_ctx
, iconv_convenience
);
897 NDR_ERR_HAVE_NO_MEMORY(ndr
);
898 NDR_CHECK(ndr_pull_set_switch_value(ndr
, p
, level
));
899 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
900 if (ndr
->offset
< ndr
->data_size
) {
901 return ndr_pull_error(ndr
, NDR_ERR_UNREAD_BYTES
,
902 "not all bytes consumed ofs[%u] size[%u]",
903 ndr
->offset
, ndr
->data_size
);
905 return NDR_ERR_SUCCESS
;
909 push a struct to a blob using NDR
911 _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
)
913 struct ndr_push
*ndr
;
914 ndr
= ndr_push_init_ctx(mem_ctx
, iconv_convenience
);
915 NDR_ERR_HAVE_NO_MEMORY(ndr
);
917 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
919 *blob
= ndr_push_blob(ndr
);
920 talloc_steal(mem_ctx
, blob
->data
);
923 return NDR_ERR_SUCCESS
;
927 push a union to a blob using NDR
929 _PUBLIC_
enum ndr_err_code
ndr_push_union_blob(DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
, struct smb_iconv_convenience
*iconv_convenience
, void *p
,
930 uint32_t level
, ndr_push_flags_fn_t fn
)
932 struct ndr_push
*ndr
;
933 ndr
= ndr_push_init_ctx(mem_ctx
, iconv_convenience
);
934 NDR_ERR_HAVE_NO_MEMORY(ndr
);
936 NDR_CHECK(ndr_push_set_switch_value(ndr
, p
, level
));
937 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
939 *blob
= ndr_push_blob(ndr
);
940 talloc_steal(mem_ctx
, blob
->data
);
943 return NDR_ERR_SUCCESS
;
947 generic ndr_size_*() handler for structures
949 _PUBLIC_
size_t ndr_size_struct(const void *p
, int flags
, ndr_push_flags_fn_t push
, struct smb_iconv_convenience
*iconv_convenience
)
951 struct ndr_push
*ndr
;
952 enum ndr_err_code status
;
955 /* avoid recursion */
956 if (flags
& LIBNDR_FLAG_NO_NDR_SIZE
) return 0;
958 ndr
= ndr_push_init_ctx(NULL
, iconv_convenience
);
960 ndr
->flags
|= flags
| LIBNDR_FLAG_NO_NDR_SIZE
;
961 status
= push(ndr
, NDR_SCALARS
|NDR_BUFFERS
, discard_const(p
));
962 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
972 generic ndr_size_*() handler for unions
974 _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
)
976 struct ndr_push
*ndr
;
977 enum ndr_err_code status
;
980 /* avoid recursion */
981 if (flags
& LIBNDR_FLAG_NO_NDR_SIZE
) return 0;
983 ndr
= ndr_push_init_ctx(NULL
, iconv_convenience
);
985 ndr
->flags
|= flags
| LIBNDR_FLAG_NO_NDR_SIZE
;
987 status
= ndr_push_set_switch_value(ndr
, p
, level
);
988 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
992 status
= push(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
);
993 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
1003 get the current base for relative pointers for the push
1005 _PUBLIC_
uint32_t ndr_push_get_relative_base_offset(struct ndr_push
*ndr
)
1007 return ndr
->relative_base_offset
;
1011 restore the old base for relative pointers for the push
1013 _PUBLIC_
void ndr_push_restore_relative_base_offset(struct ndr_push
*ndr
, uint32_t offset
)
1015 ndr
->relative_base_offset
= offset
;
1019 setup the current base for relative pointers for the push
1020 called in the NDR_SCALAR stage
1022 _PUBLIC_
enum ndr_err_code
ndr_push_setup_relative_base_offset1(struct ndr_push
*ndr
, const void *p
, uint32_t offset
)
1024 ndr
->relative_base_offset
= offset
;
1025 return ndr_token_store(ndr
, &ndr
->relative_base_list
, p
, offset
);
1029 setup the current base for relative pointers for the push
1030 called in the NDR_BUFFERS stage
1032 _PUBLIC_
enum ndr_err_code
ndr_push_setup_relative_base_offset2(struct ndr_push
*ndr
, const void *p
)
1034 return ndr_token_retrieve(&ndr
->relative_base_list
, p
, &ndr
->relative_base_offset
);
1038 push a relative object - stage1
1039 this is called during SCALARS processing
1041 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr1(struct ndr_push
*ndr
, const void *p
)
1044 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0));
1045 return NDR_ERR_SUCCESS
;
1047 NDR_CHECK(ndr_push_align(ndr
, 4));
1048 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_list
, p
, ndr
->offset
));
1049 return ndr_push_uint32(ndr
, NDR_SCALARS
, 0xFFFFFFFF);
1053 push a relative object - stage2
1054 this is called during buffers processing
1056 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr2(struct ndr_push
*ndr
, const void *p
)
1058 uint32_t save_offset
;
1059 uint32_t ptr_offset
= 0xFFFFFFFF;
1061 return NDR_ERR_SUCCESS
;
1063 save_offset
= ndr
->offset
;
1064 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &ptr_offset
));
1065 if (ptr_offset
> ndr
->offset
) {
1066 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1067 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1068 ptr_offset
, ndr
->offset
);
1070 ndr
->offset
= ptr_offset
;
1071 if (save_offset
< ndr
->relative_base_offset
) {
1072 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1073 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1074 save_offset
, ndr
->relative_base_offset
);
1076 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, save_offset
- ndr
->relative_base_offset
));
1077 ndr
->offset
= save_offset
;
1078 return NDR_ERR_SUCCESS
;
1082 get the current base for relative pointers for the pull
1084 _PUBLIC_
uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull
*ndr
)
1086 return ndr
->relative_base_offset
;
1090 restore the old base for relative pointers for the pull
1092 _PUBLIC_
void ndr_pull_restore_relative_base_offset(struct ndr_pull
*ndr
, uint32_t offset
)
1094 ndr
->relative_base_offset
= offset
;
1098 setup the current base for relative pointers for the pull
1099 called in the NDR_SCALAR stage
1101 _PUBLIC_
enum ndr_err_code
ndr_pull_setup_relative_base_offset1(struct ndr_pull
*ndr
, const void *p
, uint32_t offset
)
1103 ndr
->relative_base_offset
= offset
;
1104 return ndr_token_store(ndr
, &ndr
->relative_base_list
, p
, offset
);
1108 setup the current base for relative pointers for the pull
1109 called in the NDR_BUFFERS stage
1111 _PUBLIC_
enum ndr_err_code
ndr_pull_setup_relative_base_offset2(struct ndr_pull
*ndr
, const void *p
)
1113 return ndr_token_retrieve(&ndr
->relative_base_list
, p
, &ndr
->relative_base_offset
);
1117 pull a relative object - stage1
1118 called during SCALARS processing
1120 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr1(struct ndr_pull
*ndr
, const void *p
, uint32_t rel_offset
)
1122 rel_offset
+= ndr
->relative_base_offset
;
1123 if (rel_offset
> ndr
->data_size
) {
1124 return ndr_pull_error(ndr
, NDR_ERR_BUFSIZE
,
1125 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1126 rel_offset
, ndr
->data_size
);
1128 return ndr_token_store(ndr
, &ndr
->relative_list
, p
, rel_offset
);
1132 pull a relative object - stage2
1133 called during BUFFERS processing
1135 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr2(struct ndr_pull
*ndr
, const void *p
)
1137 uint32_t rel_offset
;
1138 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &rel_offset
));
1139 return ndr_pull_set_offset(ndr
, rel_offset
);
1142 const static struct {
1143 enum ndr_err_code err
;
1145 } ndr_err_code_strings
[] = {
1146 { NDR_ERR_SUCCESS
, "Success" },
1147 { NDR_ERR_ARRAY_SIZE
, "Bad Array Size" },
1148 { NDR_ERR_BAD_SWITCH
, "Bad Switch" },
1149 { NDR_ERR_OFFSET
, "Offset Error" },
1150 { NDR_ERR_RELATIVE
, "Relative Pointer Error" },
1151 { NDR_ERR_CHARCNV
, "Character Conversion Error" },
1152 { NDR_ERR_LENGTH
, "Length Error" },
1153 { NDR_ERR_SUBCONTEXT
, "Subcontext Error" },
1154 { NDR_ERR_COMPRESSION
, "Compression Error" },
1155 { NDR_ERR_STRING
, "String Error" },
1156 { NDR_ERR_VALIDATE
, "Validate Error" },
1157 { NDR_ERR_BUFSIZE
, "Buffer Size Error" },
1158 { NDR_ERR_ALLOC
, "Allocation Error" },
1159 { NDR_ERR_RANGE
, "Range Error" },
1160 { NDR_ERR_TOKEN
, "Token Error" },
1161 { NDR_ERR_IPV4ADDRESS
, "IPv4 Address Error" },
1162 { NDR_ERR_INVALID_POINTER
, "Invalid Pointer" },
1163 { NDR_ERR_UNREAD_BYTES
, "Unread Bytes" },
1167 _PUBLIC_
const char *ndr_map_error2string(enum ndr_err_code ndr_err
)
1170 for (i
= 0; ndr_err_code_strings
[i
].string
!= NULL
; i
++) {
1171 if (ndr_err_code_strings
[i
].err
== ndr_err
)
1172 return ndr_err_code_strings
[i
].string
;
1174 return "Unknown error";