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 (*pflags
) |= new_flags
;
356 return and possibly log an NDR error
358 _PUBLIC_
enum ndr_err_code
ndr_pull_error(struct ndr_pull
*ndr
,
359 enum ndr_err_code ndr_err
,
360 const char *format
, ...)
366 va_start(ap
, format
);
367 ret
= vasprintf(&s
, format
, ap
);
371 return NDR_ERR_ALLOC
;
374 DEBUG(1,("ndr_pull_error(%u): %s\n", ndr_err
, s
));
382 return and possibly log an NDR error
384 _PUBLIC_
enum ndr_err_code
ndr_push_error(struct ndr_push
*ndr
,
385 enum ndr_err_code ndr_err
,
386 const char *format
, ...)
392 va_start(ap
, format
);
393 ret
= vasprintf(&s
, format
, ap
);
397 return NDR_ERR_ALLOC
;
400 DEBUG(1,("ndr_push_error(%u): %s\n", ndr_err
, s
));
408 handle subcontext buffers, which in midl land are user-marshalled, but
409 we use magic in pidl to make them easier to cope with
411 _PUBLIC_
enum ndr_err_code
ndr_pull_subcontext_start(struct ndr_pull
*ndr
,
412 struct ndr_pull
**_subndr
,
416 struct ndr_pull
*subndr
;
417 uint32_t r_content_size
;
418 bool force_le
= false;
419 bool force_be
= false;
421 switch (header_size
) {
423 uint32_t content_size
= ndr
->data_size
- ndr
->offset
;
425 content_size
= size_is
;
427 r_content_size
= content_size
;
432 uint16_t content_size
;
433 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, &content_size
));
434 if (size_is
>= 0 && size_is
!= content_size
) {
435 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
436 (int)size_is
, (int)content_size
);
438 r_content_size
= content_size
;
443 uint32_t content_size
;
444 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &content_size
));
445 if (size_is
>= 0 && size_is
!= content_size
) {
446 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
447 (int)size_is
, (int)content_size
);
449 r_content_size
= content_size
;
454 * Common Type Header for the Serialization Stream
455 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
461 uint32_t content_size
;
465 NDR_CHECK(ndr_pull_uint8(ndr
, NDR_SCALARS
, &version
));
468 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
469 "Bad subcontext (PULL) Common Type Header version %d != 1",
477 NDR_CHECK(ndr_pull_uint8(ndr
, NDR_SCALARS
, &drep
));
480 } else if (drep
== 0x00) {
483 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
484 "Bad subcontext (PULL) Common Type Header invalid drep 0x%02X",
488 /* length of the "Private Header for Constructed Type" */
489 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, &hdrlen
));
491 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
492 "Bad subcontext (PULL) Common Type Header length %d != 8",
496 /* filler should be ignored */
497 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &filler
));
500 * Private Header for Constructed Type
502 /* length - will be updated latter */
503 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &content_size
));
504 if (size_is
>= 0 && size_is
!= content_size
) {
505 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
506 (int)size_is
, (int)content_size
);
508 /* the content size must be a multiple of 8 */
509 if ((content_size
% 8) != 0) {
510 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
,
511 "Bad subcontext (PULL) size_is(%d) not padded to 8 content_size %d",
512 (int)size_is
, (int)content_size
);
514 r_content_size
= content_size
;
517 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &reserved
));
521 return ndr_pull_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PULL) header_size %d",
525 NDR_PULL_NEED_BYTES(ndr
, r_content_size
);
527 subndr
= talloc_zero(ndr
, struct ndr_pull
);
528 NDR_ERR_HAVE_NO_MEMORY(subndr
);
529 subndr
->flags
= ndr
->flags
;
530 subndr
->current_mem_ctx
= ndr
->current_mem_ctx
;
532 subndr
->data
= ndr
->data
+ ndr
->offset
;
534 subndr
->data_size
= r_content_size
;
535 subndr
->iconv_convenience
= talloc_reference(subndr
, ndr
->iconv_convenience
);
538 ndr_set_flags(&ndr
->flags
, LIBNDR_FLAG_LITTLE_ENDIAN
);
539 } else if (force_be
) {
540 ndr_set_flags(&ndr
->flags
, LIBNDR_FLAG_BIGENDIAN
);
544 return NDR_ERR_SUCCESS
;
547 _PUBLIC_
enum ndr_err_code
ndr_pull_subcontext_end(struct ndr_pull
*ndr
,
548 struct ndr_pull
*subndr
,
555 } else if (header_size
> 0) {
556 advance
= subndr
->data_size
;
558 advance
= subndr
->offset
;
560 NDR_CHECK(ndr_pull_advance(ndr
, advance
));
561 return NDR_ERR_SUCCESS
;
564 _PUBLIC_
enum ndr_err_code
ndr_push_subcontext_start(struct ndr_push
*ndr
,
565 struct ndr_push
**_subndr
,
569 struct ndr_push
*subndr
;
571 subndr
= ndr_push_init_ctx(ndr
, ndr
->iconv_convenience
);
572 NDR_ERR_HAVE_NO_MEMORY(subndr
);
573 subndr
->flags
= ndr
->flags
;
576 return NDR_ERR_SUCCESS
;
580 push a subcontext header
582 _PUBLIC_
enum ndr_err_code
ndr_push_subcontext_end(struct ndr_push
*ndr
,
583 struct ndr_push
*subndr
,
590 padding_len
= size_is
- subndr
->offset
;
591 if (padding_len
> 0) {
592 NDR_CHECK(ndr_push_zero(subndr
, padding_len
));
593 } else if (padding_len
< 0) {
594 return ndr_push_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
595 (int)subndr
->offset
, (int)size_is
);
599 switch (header_size
) {
604 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, subndr
->offset
));
608 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, subndr
->offset
));
613 * Common Type Header for the Serialization Stream
614 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
616 padding_len
= NDR_ROUND(subndr
->offset
, 8) - subndr
->offset
;
617 if (padding_len
> 0) {
618 NDR_CHECK(ndr_push_zero(subndr
, padding_len
));
622 NDR_CHECK(ndr_push_uint8(ndr
, NDR_SCALARS
, 1));
628 NDR_CHECK(ndr_push_uint8(ndr
, NDR_SCALARS
, NDR_BE(ndr
)?0x00:0x10));
630 /* length of the "Private Header for Constructed Type" */
631 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, 8));
634 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0xCCCCCCCC));
637 * Private Header for Constructed Type
639 /* length - will be updated latter */
640 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, subndr
->offset
));
643 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0));
647 return ndr_push_error(ndr
, NDR_ERR_SUBCONTEXT
, "Bad subcontext header size %d",
651 NDR_CHECK(ndr_push_bytes(ndr
, subndr
->data
, subndr
->offset
));
652 return NDR_ERR_SUCCESS
;
656 store a token in the ndr context, for later retrieval
658 _PUBLIC_
enum ndr_err_code
ndr_token_store(TALLOC_CTX
*mem_ctx
,
659 struct ndr_token_list
**list
,
663 struct ndr_token_list
*tok
;
664 tok
= talloc(mem_ctx
, struct ndr_token_list
);
665 NDR_ERR_HAVE_NO_MEMORY(tok
);
668 DLIST_ADD((*list
), tok
);
669 return NDR_ERR_SUCCESS
;
673 retrieve a token from a ndr context, using cmp_fn to match the tokens
675 _PUBLIC_
enum ndr_err_code
ndr_token_retrieve_cmp_fn(struct ndr_token_list
**list
, const void *key
, uint32_t *v
,
676 comparison_fn_t _cmp_fn
, bool _remove_tok
)
678 struct ndr_token_list
*tok
;
679 for (tok
=*list
;tok
;tok
=tok
->next
) {
680 if (_cmp_fn
&& _cmp_fn(tok
->key
,key
)==0) goto found
;
681 else if (!_cmp_fn
&& tok
->key
== key
) goto found
;
683 return NDR_ERR_TOKEN
;
687 DLIST_REMOVE((*list
), tok
);
690 return NDR_ERR_SUCCESS
;
694 retrieve a token from a ndr context
696 _PUBLIC_
enum ndr_err_code
ndr_token_retrieve(struct ndr_token_list
**list
, const void *key
, uint32_t *v
)
698 return ndr_token_retrieve_cmp_fn(list
, key
, v
, NULL
, true);
702 peek at but don't removed a token from a ndr context
704 _PUBLIC_
uint32_t ndr_token_peek(struct ndr_token_list
**list
, const void *key
)
706 enum ndr_err_code status
;
709 status
= ndr_token_retrieve_cmp_fn(list
, key
, &v
, NULL
, false);
710 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
718 pull an array size field and add it to the array_size_list token list
720 _PUBLIC_
enum ndr_err_code
ndr_pull_array_size(struct ndr_pull
*ndr
, const void *p
)
723 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &size
));
724 return ndr_token_store(ndr
, &ndr
->array_size_list
, p
, size
);
728 get the stored array size field
730 _PUBLIC_
uint32_t ndr_get_array_size(struct ndr_pull
*ndr
, const void *p
)
732 return ndr_token_peek(&ndr
->array_size_list
, p
);
736 check the stored array size field
738 _PUBLIC_
enum ndr_err_code
ndr_check_array_size(struct ndr_pull
*ndr
, void *p
, uint32_t size
)
741 stored
= ndr_token_peek(&ndr
->array_size_list
, p
);
742 if (stored
!= size
) {
743 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
744 "Bad array size - got %u expected %u\n",
747 return NDR_ERR_SUCCESS
;
751 pull an array length field and add it to the array_length_list token list
753 _PUBLIC_
enum ndr_err_code
ndr_pull_array_length(struct ndr_pull
*ndr
, const void *p
)
755 uint32_t length
, offset
;
756 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &offset
));
758 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
759 "non-zero array offset %u\n", offset
);
761 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &length
));
762 return ndr_token_store(ndr
, &ndr
->array_length_list
, p
, length
);
766 get the stored array length field
768 _PUBLIC_
uint32_t ndr_get_array_length(struct ndr_pull
*ndr
, const void *p
)
770 return ndr_token_peek(&ndr
->array_length_list
, p
);
774 check the stored array length field
776 _PUBLIC_
enum ndr_err_code
ndr_check_array_length(struct ndr_pull
*ndr
, void *p
, uint32_t length
)
779 stored
= ndr_token_peek(&ndr
->array_length_list
, p
);
780 if (stored
!= length
) {
781 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
,
782 "Bad array length - got %u expected %u\n",
785 return NDR_ERR_SUCCESS
;
791 _PUBLIC_
enum ndr_err_code
ndr_push_set_switch_value(struct ndr_push
*ndr
, const void *p
, uint32_t val
)
793 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
796 _PUBLIC_
enum ndr_err_code
ndr_pull_set_switch_value(struct ndr_pull
*ndr
, const void *p
, uint32_t val
)
798 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
801 _PUBLIC_
enum ndr_err_code
ndr_print_set_switch_value(struct ndr_print
*ndr
, const void *p
, uint32_t val
)
803 return ndr_token_store(ndr
, &ndr
->switch_list
, p
, val
);
807 retrieve a switch value
809 _PUBLIC_
uint32_t ndr_push_get_switch_value(struct ndr_push
*ndr
, const void *p
)
811 return ndr_token_peek(&ndr
->switch_list
, p
);
814 _PUBLIC_
uint32_t ndr_pull_get_switch_value(struct ndr_pull
*ndr
, const void *p
)
816 return ndr_token_peek(&ndr
->switch_list
, p
);
819 _PUBLIC_
uint32_t ndr_print_get_switch_value(struct ndr_print
*ndr
, const void *p
)
821 return ndr_token_peek(&ndr
->switch_list
, p
);
825 pull a struct from a blob using NDR
827 _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
,
828 ndr_pull_flags_fn_t fn
)
830 struct ndr_pull
*ndr
;
831 ndr
= ndr_pull_init_blob(blob
, mem_ctx
, iconv_convenience
);
832 NDR_ERR_HAVE_NO_MEMORY(ndr
);
833 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
834 return NDR_ERR_SUCCESS
;
838 pull a struct from a blob using NDR - failing if all bytes are not consumed
840 _PUBLIC_
enum ndr_err_code
ndr_pull_struct_blob_all(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
841 struct smb_iconv_convenience
*iconv_convenience
,
842 void *p
, ndr_pull_flags_fn_t fn
)
844 struct ndr_pull
*ndr
;
845 ndr
= ndr_pull_init_blob(blob
, mem_ctx
, iconv_convenience
);
846 NDR_ERR_HAVE_NO_MEMORY(ndr
);
847 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
848 if (ndr
->offset
< ndr
->data_size
) {
849 return ndr_pull_error(ndr
, NDR_ERR_UNREAD_BYTES
,
850 "not all bytes consumed ofs[%u] size[%u]",
851 ndr
->offset
, ndr
->data_size
);
853 return NDR_ERR_SUCCESS
;
857 pull a union from a blob using NDR, given the union discriminator
859 _PUBLIC_
enum ndr_err_code
ndr_pull_union_blob(const DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
,
860 struct smb_iconv_convenience
*iconv_convenience
, void *p
,
861 uint32_t level
, ndr_pull_flags_fn_t fn
)
863 struct ndr_pull
*ndr
;
864 ndr
= ndr_pull_init_blob(blob
, mem_ctx
, iconv_convenience
);
865 NDR_ERR_HAVE_NO_MEMORY(ndr
);
866 NDR_CHECK(ndr_pull_set_switch_value(ndr
, p
, level
));
867 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
868 return NDR_ERR_SUCCESS
;
872 pull a union from a blob using NDR, given the union discriminator,
873 failing if all bytes are not consumed
875 _PUBLIC_
enum ndr_err_code
ndr_pull_union_blob_all(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 if (ndr
->offset
< ndr
->data_size
) {
885 return ndr_pull_error(ndr
, NDR_ERR_UNREAD_BYTES
,
886 "not all bytes consumed ofs[%u] size[%u]",
887 ndr
->offset
, ndr
->data_size
);
889 return NDR_ERR_SUCCESS
;
893 push a struct to a blob using NDR
895 _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
)
897 struct ndr_push
*ndr
;
898 ndr
= ndr_push_init_ctx(mem_ctx
, iconv_convenience
);
899 NDR_ERR_HAVE_NO_MEMORY(ndr
);
901 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
903 *blob
= ndr_push_blob(ndr
);
904 talloc_steal(mem_ctx
, blob
->data
);
907 return NDR_ERR_SUCCESS
;
911 push a union to a blob using NDR
913 _PUBLIC_
enum ndr_err_code
ndr_push_union_blob(DATA_BLOB
*blob
, TALLOC_CTX
*mem_ctx
, struct smb_iconv_convenience
*iconv_convenience
, void *p
,
914 uint32_t level
, ndr_push_flags_fn_t fn
)
916 struct ndr_push
*ndr
;
917 ndr
= ndr_push_init_ctx(mem_ctx
, iconv_convenience
);
918 NDR_ERR_HAVE_NO_MEMORY(ndr
);
920 NDR_CHECK(ndr_push_set_switch_value(ndr
, p
, level
));
921 NDR_CHECK(fn(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
));
923 *blob
= ndr_push_blob(ndr
);
924 talloc_steal(mem_ctx
, blob
->data
);
927 return NDR_ERR_SUCCESS
;
931 generic ndr_size_*() handler for structures
933 _PUBLIC_
size_t ndr_size_struct(const void *p
, int flags
, ndr_push_flags_fn_t push
, struct smb_iconv_convenience
*iconv_convenience
)
935 struct ndr_push
*ndr
;
936 enum ndr_err_code status
;
939 /* avoid recursion */
940 if (flags
& LIBNDR_FLAG_NO_NDR_SIZE
) return 0;
942 ndr
= ndr_push_init_ctx(NULL
, iconv_convenience
);
944 ndr
->flags
|= flags
| LIBNDR_FLAG_NO_NDR_SIZE
;
945 status
= push(ndr
, NDR_SCALARS
|NDR_BUFFERS
, discard_const(p
));
946 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
956 generic ndr_size_*() handler for unions
958 _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
)
960 struct ndr_push
*ndr
;
961 enum ndr_err_code status
;
964 /* avoid recursion */
965 if (flags
& LIBNDR_FLAG_NO_NDR_SIZE
) return 0;
967 ndr
= ndr_push_init_ctx(NULL
, iconv_convenience
);
969 ndr
->flags
|= flags
| LIBNDR_FLAG_NO_NDR_SIZE
;
971 status
= ndr_push_set_switch_value(ndr
, p
, level
);
972 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
976 status
= push(ndr
, NDR_SCALARS
|NDR_BUFFERS
, p
);
977 if (!NDR_ERR_CODE_IS_SUCCESS(status
)) {
987 get the current base for relative pointers for the push
989 _PUBLIC_
uint32_t ndr_push_get_relative_base_offset(struct ndr_push
*ndr
)
991 return ndr
->relative_base_offset
;
995 restore the old base for relative pointers for the push
997 _PUBLIC_
void ndr_push_restore_relative_base_offset(struct ndr_push
*ndr
, uint32_t offset
)
999 ndr
->relative_base_offset
= offset
;
1003 setup the current base for relative pointers for the push
1004 called in the NDR_SCALAR stage
1006 _PUBLIC_
enum ndr_err_code
ndr_push_setup_relative_base_offset1(struct ndr_push
*ndr
, const void *p
, uint32_t offset
)
1008 ndr
->relative_base_offset
= offset
;
1009 return ndr_token_store(ndr
, &ndr
->relative_base_list
, p
, offset
);
1013 setup the current base for relative pointers for the push
1014 called in the NDR_BUFFERS stage
1016 _PUBLIC_
enum ndr_err_code
ndr_push_setup_relative_base_offset2(struct ndr_push
*ndr
, const void *p
)
1018 return ndr_token_retrieve(&ndr
->relative_base_list
, p
, &ndr
->relative_base_offset
);
1022 push a relative object - stage1
1023 this is called during SCALARS processing
1025 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr1(struct ndr_push
*ndr
, const void *p
)
1028 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0));
1029 return NDR_ERR_SUCCESS
;
1031 NDR_CHECK(ndr_push_align(ndr
, 4));
1032 NDR_CHECK(ndr_token_store(ndr
, &ndr
->relative_list
, p
, ndr
->offset
));
1033 return ndr_push_uint32(ndr
, NDR_SCALARS
, 0xFFFFFFFF);
1037 push a relative object - stage2
1038 this is called during buffers processing
1040 _PUBLIC_
enum ndr_err_code
ndr_push_relative_ptr2(struct ndr_push
*ndr
, const void *p
)
1042 uint32_t save_offset
;
1043 uint32_t ptr_offset
= 0xFFFFFFFF;
1045 return NDR_ERR_SUCCESS
;
1047 save_offset
= ndr
->offset
;
1048 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &ptr_offset
));
1049 if (ptr_offset
> ndr
->offset
) {
1050 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1051 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1052 ptr_offset
, ndr
->offset
);
1054 ndr
->offset
= ptr_offset
;
1055 if (save_offset
< ndr
->relative_base_offset
) {
1056 return ndr_push_error(ndr
, NDR_ERR_BUFSIZE
,
1057 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1058 save_offset
, ndr
->relative_base_offset
);
1060 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, save_offset
- ndr
->relative_base_offset
));
1061 ndr
->offset
= save_offset
;
1062 return NDR_ERR_SUCCESS
;
1066 get the current base for relative pointers for the pull
1068 _PUBLIC_
uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull
*ndr
)
1070 return ndr
->relative_base_offset
;
1074 restore the old base for relative pointers for the pull
1076 _PUBLIC_
void ndr_pull_restore_relative_base_offset(struct ndr_pull
*ndr
, uint32_t offset
)
1078 ndr
->relative_base_offset
= offset
;
1082 setup the current base for relative pointers for the pull
1083 called in the NDR_SCALAR stage
1085 _PUBLIC_
enum ndr_err_code
ndr_pull_setup_relative_base_offset1(struct ndr_pull
*ndr
, const void *p
, uint32_t offset
)
1087 ndr
->relative_base_offset
= offset
;
1088 return ndr_token_store(ndr
, &ndr
->relative_base_list
, p
, offset
);
1092 setup the current base for relative pointers for the pull
1093 called in the NDR_BUFFERS stage
1095 _PUBLIC_
enum ndr_err_code
ndr_pull_setup_relative_base_offset2(struct ndr_pull
*ndr
, const void *p
)
1097 return ndr_token_retrieve(&ndr
->relative_base_list
, p
, &ndr
->relative_base_offset
);
1101 pull a relative object - stage1
1102 called during SCALARS processing
1104 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr1(struct ndr_pull
*ndr
, const void *p
, uint32_t rel_offset
)
1106 rel_offset
+= ndr
->relative_base_offset
;
1107 if (rel_offset
> ndr
->data_size
) {
1108 return ndr_pull_error(ndr
, NDR_ERR_BUFSIZE
,
1109 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1110 rel_offset
, ndr
->data_size
);
1112 return ndr_token_store(ndr
, &ndr
->relative_list
, p
, rel_offset
);
1116 pull a relative object - stage2
1117 called during BUFFERS processing
1119 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr2(struct ndr_pull
*ndr
, const void *p
)
1121 uint32_t rel_offset
;
1122 NDR_CHECK(ndr_token_retrieve(&ndr
->relative_list
, p
, &rel_offset
));
1123 return ndr_pull_set_offset(ndr
, rel_offset
);
1126 const static struct {
1127 enum ndr_err_code err
;
1129 } ndr_err_code_strings
[] = {
1130 { NDR_ERR_SUCCESS
, "Success" },
1131 { NDR_ERR_ARRAY_SIZE
, "Bad Array Size" },
1132 { NDR_ERR_BAD_SWITCH
, "Bad Switch" },
1133 { NDR_ERR_OFFSET
, "Offset Error" },
1134 { NDR_ERR_RELATIVE
, "Relative Pointer Error" },
1135 { NDR_ERR_CHARCNV
, "Character Conversion Error" },
1136 { NDR_ERR_LENGTH
, "Length Error" },
1137 { NDR_ERR_SUBCONTEXT
, "Subcontext Error" },
1138 { NDR_ERR_COMPRESSION
, "Compression Error" },
1139 { NDR_ERR_STRING
, "String Error" },
1140 { NDR_ERR_VALIDATE
, "Validate Error" },
1141 { NDR_ERR_BUFSIZE
, "Buffer Size Error" },
1142 { NDR_ERR_ALLOC
, "Allocation Error" },
1143 { NDR_ERR_RANGE
, "Range Error" },
1144 { NDR_ERR_TOKEN
, "Token Error" },
1145 { NDR_ERR_IPV4ADDRESS
, "IPv4 Address Error" },
1146 { NDR_ERR_INVALID_POINTER
, "Invalid Pointer" },
1147 { NDR_ERR_UNREAD_BYTES
, "Unread Bytes" },
1151 _PUBLIC_
const char *ndr_map_error2string(enum ndr_err_code ndr_err
)
1154 for (i
= 0; ndr_err_code_strings
[i
].string
!= NULL
; i
++) {
1155 if (ndr_err_code_strings
[i
].err
== ndr_err
)
1156 return ndr_err_code_strings
[i
].string
;
1158 return "Unknown error";