libndr: give an error when ndr_push_relative_ptr2_start()/_end() is used with the...
[Samba/nascimento.git] / librpc / ndr / ndr.c
blob0ff43c3eb4595a9a2e8e9d1f40ad6e5dc2489a10
1 /*
2 Unix SMB/CIFS implementation.
4 libndr interface
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
26 of NDR encoding rules
29 #include "includes.h"
30 #include "librpc/ndr/libndr.h"
31 #include "../lib/util/dlinklist.h"
32 #if _SAMBA_BUILD_ == 4
33 #include "param/param.h"
34 #endif
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)
63 struct ndr_pull *ndr;
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);
73 return ndr;
77 advance by 'size' bytes
79 _PUBLIC_ enum ndr_err_code ndr_pull_advance(struct ndr_pull *ndr, uint32_t size)
81 ndr->offset += size;
82 if (ndr->offset > ndr->data_size) {
83 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
84 "ndr_pull_advance by %u failed",
85 size);
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)
95 ndr->offset = ofs;
96 if (ndr->offset > ndr->data_size) {
97 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
98 "ndr_pull_set_offset %u failed",
99 ofs);
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);
110 if (!ndr) {
111 return NULL;
114 ndr->flags = 0;
115 ndr->alloc_size = NDR_BASE_MARSHALL_SIZE;
116 ndr->data = talloc_array(ndr, uint8_t, ndr->alloc_size);
117 if (!ndr->data) {
118 return NULL;
120 ndr->iconv_convenience = talloc_reference(ndr, iconv_convenience);
122 return ndr;
125 /* return a DATA_BLOB structure for the current ndr_push marshalled data */
126 _PUBLIC_ DATA_BLOB ndr_push_blob(struct ndr_push *ndr)
128 DATA_BLOB blob;
129 blob = data_blob_const(ndr->data, ndr->offset);
130 if (ndr->alloc_size > ndr->offset) {
131 ndr->data[ndr->offset] = 0;
133 return blob;
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",
147 size);
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);
159 if (!ndr->data) {
160 return ndr_push_error(ndr, NDR_ERR_ALLOC, "Failed to push_expand to %u",
161 ndr->alloc_size);
164 return NDR_ERR_SUCCESS;
167 _PUBLIC_ void ndr_print_debug_helper(struct ndr_print *ndr, const char *format, ...)
169 va_list ap;
170 char *s = NULL;
171 int i, ret;
173 va_start(ap, format);
174 ret = vasprintf(&s, format, ap);
175 va_end(ap);
177 if (ret == -1) {
178 return;
181 for (i=0;i<ndr->depth;i++) {
182 DEBUGADD(1,(" "));
185 DEBUGADD(1,("%s\n", s));
186 free(s);
189 _PUBLIC_ void ndr_print_string_helper(struct ndr_print *ndr, const char *format, ...)
191 va_list ap;
192 int i;
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,
201 format, ap);
202 va_end(ap);
203 ndr->private_data = talloc_asprintf_append_buffer((char *)ndr->private_data,
204 "\n");
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;
214 DEBUG(1,(" "));
216 ndr = talloc_zero(NULL, struct ndr_print);
217 if (!ndr) return;
218 ndr->print = ndr_print_debug_helper;
219 ndr->depth = 1;
220 ndr->flags = 0;
221 fn(ndr, name, ptr);
222 talloc_free(ndr);
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;
232 DEBUG(1,(" "));
234 ndr = talloc_zero(NULL, struct ndr_print);
235 if (!ndr) return;
236 ndr->print = ndr_print_debug_helper;
237 ndr->depth = 1;
238 ndr->flags = 0;
239 ndr_print_set_switch_value(ndr, ptr, level);
240 fn(ndr, name, ptr);
241 talloc_free(ndr);
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;
251 DEBUG(1,(" "));
253 ndr = talloc_zero(NULL, struct ndr_print);
254 if (!ndr) return;
255 ndr->print = ndr_print_debug_helper;
256 ndr->depth = 1;
257 ndr->flags = 0;
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);
264 #endif
266 fn(ndr, name, flags, ptr);
267 talloc_free(ndr);
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;
276 char *ret = NULL;
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) {
282 goto failed;
284 ndr->print = ndr_print_string_helper;
285 ndr->depth = 1;
286 ndr->flags = 0;
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);
293 #endif
295 fn(ndr, name, ptr);
296 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
297 failed:
298 talloc_free(ndr);
299 return ret;
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;
308 char *ret = NULL;
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) {
314 goto failed;
316 ndr->print = ndr_print_string_helper;
317 ndr->depth = 1;
318 ndr->flags = 0;
319 ndr_print_set_switch_value(ndr, ptr, level);
320 fn(ndr, name, ptr);
321 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
322 failed:
323 talloc_free(ndr);
324 return ret;
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;
335 char *ret = NULL;
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) {
341 goto failed;
343 ndr->print = ndr_print_string_helper;
344 ndr->depth = 1;
345 ndr->flags = 0;
346 fn(ndr, name, flags, ptr);
347 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
348 failed:
349 talloc_free(ndr);
350 return ret;
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;
358 (*pflags) &= ~LIBNDR_FLAG_NDR64;
360 if (new_flags & LIBNDR_FLAG_BIGENDIAN) {
361 (*pflags) &= ~LIBNDR_FLAG_LITTLE_ENDIAN;
362 (*pflags) &= ~LIBNDR_FLAG_NDR64;
364 if (new_flags & LIBNDR_FLAG_REMAINING) {
365 (*pflags) &= ~LIBNDR_ALIGN_FLAGS;
367 if (new_flags & LIBNDR_ALIGN_FLAGS) {
368 (*pflags) &= ~LIBNDR_FLAG_REMAINING;
370 if (new_flags & LIBNDR_FLAG_NO_RELATIVE_REVERSE) {
371 (*pflags) &= ~LIBNDR_FLAG_RELATIVE_REVERSE;
373 (*pflags) |= new_flags;
377 return and possibly log an NDR error
379 _PUBLIC_ enum ndr_err_code ndr_pull_error(struct ndr_pull *ndr,
380 enum ndr_err_code ndr_err,
381 const char *format, ...)
383 char *s=NULL;
384 va_list ap;
385 int ret;
387 va_start(ap, format);
388 ret = vasprintf(&s, format, ap);
389 va_end(ap);
391 if (ret == -1) {
392 return NDR_ERR_ALLOC;
395 DEBUG(1,("ndr_pull_error(%u): %s\n", ndr_err, s));
397 free(s);
399 return ndr_err;
403 return and possibly log an NDR error
405 _PUBLIC_ enum ndr_err_code ndr_push_error(struct ndr_push *ndr,
406 enum ndr_err_code ndr_err,
407 const char *format, ...)
409 char *s=NULL;
410 va_list ap;
411 int ret;
413 va_start(ap, format);
414 ret = vasprintf(&s, format, ap);
415 va_end(ap);
417 if (ret == -1) {
418 return NDR_ERR_ALLOC;
421 DEBUG(1,("ndr_push_error(%u): %s\n", ndr_err, s));
423 free(s);
425 return ndr_err;
429 handle subcontext buffers, which in midl land are user-marshalled, but
430 we use magic in pidl to make them easier to cope with
432 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_start(struct ndr_pull *ndr,
433 struct ndr_pull **_subndr,
434 size_t header_size,
435 ssize_t size_is)
437 struct ndr_pull *subndr;
438 uint32_t r_content_size;
439 bool force_le = false;
440 bool force_be = false;
442 switch (header_size) {
443 case 0: {
444 uint32_t content_size = ndr->data_size - ndr->offset;
445 if (size_is >= 0) {
446 content_size = size_is;
448 r_content_size = content_size;
449 break;
452 case 2: {
453 uint16_t content_size;
454 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &content_size));
455 if (size_is >= 0 && size_is != content_size) {
456 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
457 (int)size_is, (int)content_size);
459 r_content_size = content_size;
460 break;
463 case 4: {
464 uint32_t content_size;
465 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &content_size));
466 if (size_is >= 0 && size_is != content_size) {
467 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
468 (int)size_is, (int)content_size);
470 r_content_size = content_size;
471 break;
473 case 0xFFFFFC01: {
475 * Common Type Header for the Serialization Stream
476 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
478 uint8_t version;
479 uint8_t drep;
480 uint16_t hdrlen;
481 uint32_t filler;
482 uint32_t content_size;
483 uint32_t reserved;
485 /* version */
486 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &version));
488 if (version != 1) {
489 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
490 "Bad subcontext (PULL) Common Type Header version %d != 1",
491 (int)version);
495 * 0x10 little endian
496 * 0x00 big endian
498 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &drep));
499 if (drep == 0x10) {
500 force_le = true;
501 } else if (drep == 0x00) {
502 force_be = true;
503 } else {
504 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
505 "Bad subcontext (PULL) Common Type Header invalid drep 0x%02X",
506 (unsigned int)drep);
509 /* length of the "Private Header for Constructed Type" */
510 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &hdrlen));
511 if (hdrlen != 8) {
512 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
513 "Bad subcontext (PULL) Common Type Header length %d != 8",
514 (int)hdrlen);
517 /* filler should be ignored */
518 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &filler));
521 * Private Header for Constructed Type
523 /* length - will be updated latter */
524 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
525 if (size_is >= 0 && size_is != content_size) {
526 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
527 (int)size_is, (int)content_size);
529 /* the content size must be a multiple of 8 */
530 if ((content_size % 8) != 0) {
531 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
532 "Bad subcontext (PULL) size_is(%d) not padded to 8 content_size %d",
533 (int)size_is, (int)content_size);
535 r_content_size = content_size;
537 /* reserved */
538 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &reserved));
539 break;
541 default:
542 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) header_size %d",
543 (int)header_size);
546 NDR_PULL_NEED_BYTES(ndr, r_content_size);
548 subndr = talloc_zero(ndr, struct ndr_pull);
549 NDR_ERR_HAVE_NO_MEMORY(subndr);
550 subndr->flags = ndr->flags & ~LIBNDR_FLAG_NDR64;
551 subndr->current_mem_ctx = ndr->current_mem_ctx;
553 subndr->data = ndr->data + ndr->offset;
554 subndr->offset = 0;
555 subndr->data_size = r_content_size;
556 subndr->iconv_convenience = talloc_reference(subndr, ndr->iconv_convenience);
558 if (force_le) {
559 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_LITTLE_ENDIAN);
560 } else if (force_be) {
561 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_BIGENDIAN);
564 *_subndr = subndr;
565 return NDR_ERR_SUCCESS;
568 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_end(struct ndr_pull *ndr,
569 struct ndr_pull *subndr,
570 size_t header_size,
571 ssize_t size_is)
573 uint32_t advance;
574 if (size_is >= 0) {
575 advance = size_is;
576 } else if (header_size > 0) {
577 advance = subndr->data_size;
578 } else {
579 advance = subndr->offset;
581 NDR_CHECK(ndr_pull_advance(ndr, advance));
582 return NDR_ERR_SUCCESS;
585 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_start(struct ndr_push *ndr,
586 struct ndr_push **_subndr,
587 size_t header_size,
588 ssize_t size_is)
590 struct ndr_push *subndr;
592 subndr = ndr_push_init_ctx(ndr, ndr->iconv_convenience);
593 NDR_ERR_HAVE_NO_MEMORY(subndr);
594 subndr->flags = ndr->flags & ~LIBNDR_FLAG_NDR64;
596 if (size_is > 0) {
597 NDR_CHECK(ndr_push_zero(subndr, size_is));
598 subndr->offset = 0;
601 *_subndr = subndr;
602 return NDR_ERR_SUCCESS;
606 push a subcontext header
608 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_end(struct ndr_push *ndr,
609 struct ndr_push *subndr,
610 size_t header_size,
611 ssize_t size_is)
613 ssize_t padding_len;
615 if (size_is >= 0) {
616 padding_len = size_is - subndr->offset;
617 if (padding_len < 0) {
618 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
619 (int)subndr->offset, (int)size_is);
621 subndr->offset = size_is;
624 switch (header_size) {
625 case 0:
626 break;
628 case 2:
629 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, subndr->offset));
630 break;
632 case 4:
633 NDR_CHECK(ndr_push_uint3264(ndr, NDR_SCALARS, subndr->offset));
634 break;
636 case 0xFFFFFC01:
638 * Common Type Header for the Serialization Stream
639 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
641 padding_len = NDR_ROUND(subndr->offset, 8) - subndr->offset;
642 if (padding_len > 0) {
643 NDR_CHECK(ndr_push_zero(subndr, padding_len));
646 /* version */
647 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, 1));
650 * 0x10 little endian
651 * 0x00 big endian
653 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, NDR_BE(ndr)?0x00:0x10));
655 /* length of the "Private Header for Constructed Type" */
656 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 8));
658 /* filler */
659 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0xCCCCCCCC));
662 * Private Header for Constructed Type
664 /* length - will be updated latter */
665 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
667 /* reserved */
668 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
669 break;
671 default:
672 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext header size %d",
673 (int)header_size);
676 NDR_CHECK(ndr_push_bytes(ndr, subndr->data, subndr->offset));
677 return NDR_ERR_SUCCESS;
681 store a token in the ndr context, for later retrieval
683 _PUBLIC_ enum ndr_err_code ndr_token_store(TALLOC_CTX *mem_ctx,
684 struct ndr_token_list **list,
685 const void *key,
686 uint32_t value)
688 struct ndr_token_list *tok;
689 tok = talloc(mem_ctx, struct ndr_token_list);
690 NDR_ERR_HAVE_NO_MEMORY(tok);
691 tok->key = key;
692 tok->value = value;
693 DLIST_ADD((*list), tok);
694 return NDR_ERR_SUCCESS;
698 retrieve a token from a ndr context, using cmp_fn to match the tokens
700 _PUBLIC_ enum ndr_err_code ndr_token_retrieve_cmp_fn(struct ndr_token_list **list, const void *key, uint32_t *v,
701 comparison_fn_t _cmp_fn, bool _remove_tok)
703 struct ndr_token_list *tok;
704 for (tok=*list;tok;tok=tok->next) {
705 if (_cmp_fn && _cmp_fn(tok->key,key)==0) goto found;
706 else if (!_cmp_fn && tok->key == key) goto found;
708 return NDR_ERR_TOKEN;
709 found:
710 *v = tok->value;
711 if (_remove_tok) {
712 DLIST_REMOVE((*list), tok);
713 talloc_free(tok);
715 return NDR_ERR_SUCCESS;
719 retrieve a token from a ndr context
721 _PUBLIC_ enum ndr_err_code ndr_token_retrieve(struct ndr_token_list **list, const void *key, uint32_t *v)
723 return ndr_token_retrieve_cmp_fn(list, key, v, NULL, true);
727 peek at but don't removed a token from a ndr context
729 _PUBLIC_ uint32_t ndr_token_peek(struct ndr_token_list **list, const void *key)
731 enum ndr_err_code status;
732 uint32_t v;
734 status = ndr_token_retrieve_cmp_fn(list, key, &v, NULL, false);
735 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
736 return 0;
739 return v;
743 pull an array size field and add it to the array_size_list token list
745 _PUBLIC_ enum ndr_err_code ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
747 uint32_t size;
748 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &size));
749 return ndr_token_store(ndr, &ndr->array_size_list, p, size);
753 get the stored array size field
755 _PUBLIC_ uint32_t ndr_get_array_size(struct ndr_pull *ndr, const void *p)
757 return ndr_token_peek(&ndr->array_size_list, p);
761 check the stored array size field
763 _PUBLIC_ enum ndr_err_code ndr_check_array_size(struct ndr_pull *ndr, void *p, uint32_t size)
765 uint32_t stored;
766 stored = ndr_token_peek(&ndr->array_size_list, p);
767 if (stored != size) {
768 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
769 "Bad array size - got %u expected %u\n",
770 stored, size);
772 return NDR_ERR_SUCCESS;
776 pull an array length field and add it to the array_length_list token list
778 _PUBLIC_ enum ndr_err_code ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
780 uint32_t length, offset;
781 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &offset));
782 if (offset != 0) {
783 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
784 "non-zero array offset %u\n", offset);
786 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &length));
787 return ndr_token_store(ndr, &ndr->array_length_list, p, length);
791 get the stored array length field
793 _PUBLIC_ uint32_t ndr_get_array_length(struct ndr_pull *ndr, const void *p)
795 return ndr_token_peek(&ndr->array_length_list, p);
799 check the stored array length field
801 _PUBLIC_ enum ndr_err_code ndr_check_array_length(struct ndr_pull *ndr, void *p, uint32_t length)
803 uint32_t stored;
804 stored = ndr_token_peek(&ndr->array_length_list, p);
805 if (stored != length) {
806 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
807 "Bad array length - got %u expected %u\n",
808 stored, length);
810 return NDR_ERR_SUCCESS;
814 store a switch value
816 _PUBLIC_ enum ndr_err_code ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
818 return ndr_token_store(ndr, &ndr->switch_list, p, val);
821 _PUBLIC_ enum ndr_err_code ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
823 return ndr_token_store(ndr, &ndr->switch_list, p, val);
826 _PUBLIC_ enum ndr_err_code ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
828 return ndr_token_store(ndr, &ndr->switch_list, p, val);
832 retrieve a switch value
834 _PUBLIC_ uint32_t ndr_push_get_switch_value(struct ndr_push *ndr, const void *p)
836 return ndr_token_peek(&ndr->switch_list, p);
839 _PUBLIC_ uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
841 return ndr_token_peek(&ndr->switch_list, p);
844 _PUBLIC_ uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
846 return ndr_token_peek(&ndr->switch_list, p);
850 pull a struct from a blob using NDR
852 _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,
853 ndr_pull_flags_fn_t fn)
855 struct ndr_pull *ndr;
856 ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
857 NDR_ERR_HAVE_NO_MEMORY(ndr);
858 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
859 talloc_free(ndr);
860 return NDR_ERR_SUCCESS;
864 pull a struct from a blob using NDR - failing if all bytes are not consumed
866 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
867 struct smb_iconv_convenience *iconv_convenience,
868 void *p, ndr_pull_flags_fn_t fn)
870 struct ndr_pull *ndr;
871 uint32_t highest_ofs;
872 ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
873 NDR_ERR_HAVE_NO_MEMORY(ndr);
874 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
875 if (ndr->offset > ndr->relative_highest_offset) {
876 highest_ofs = ndr->offset;
877 } else {
878 highest_ofs = ndr->relative_highest_offset;
880 if (highest_ofs < ndr->data_size) {
881 enum ndr_err_code ret;
882 ret = ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
883 "not all bytes consumed ofs[%u] size[%u]",
884 highest_ofs, ndr->data_size);
885 talloc_free(ndr);
886 return ret;
888 talloc_free(ndr);
889 return NDR_ERR_SUCCESS;
893 pull a union from a blob using NDR, given the union discriminator
895 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
896 struct smb_iconv_convenience *iconv_convenience, void *p,
897 uint32_t level, ndr_pull_flags_fn_t fn)
899 struct ndr_pull *ndr;
900 ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
901 NDR_ERR_HAVE_NO_MEMORY(ndr);
902 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
903 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
904 talloc_free(ndr);
905 return NDR_ERR_SUCCESS;
909 pull a union from a blob using NDR, given the union discriminator,
910 failing if all bytes are not consumed
912 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
913 struct smb_iconv_convenience *iconv_convenience, void *p,
914 uint32_t level, ndr_pull_flags_fn_t fn)
916 struct ndr_pull *ndr;
917 uint32_t highest_ofs;
918 ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
919 NDR_ERR_HAVE_NO_MEMORY(ndr);
920 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
921 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
922 if (ndr->offset > ndr->relative_highest_offset) {
923 highest_ofs = ndr->offset;
924 } else {
925 highest_ofs = ndr->relative_highest_offset;
927 if (highest_ofs < ndr->data_size) {
928 enum ndr_err_code ret;
929 ret = ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
930 "not all bytes consumed ofs[%u] size[%u]",
931 highest_ofs, ndr->data_size);
932 talloc_free(ndr);
933 return ret;
935 talloc_free(ndr);
936 return NDR_ERR_SUCCESS;
940 push a struct to a blob using NDR
942 _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)
944 struct ndr_push *ndr;
945 ndr = ndr_push_init_ctx(mem_ctx, iconv_convenience);
946 NDR_ERR_HAVE_NO_MEMORY(ndr);
948 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
950 *blob = ndr_push_blob(ndr);
951 talloc_steal(mem_ctx, blob->data);
952 talloc_free(ndr);
954 return NDR_ERR_SUCCESS;
958 push a union to a blob using NDR
960 _PUBLIC_ enum ndr_err_code ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience, void *p,
961 uint32_t level, ndr_push_flags_fn_t fn)
963 struct ndr_push *ndr;
964 ndr = ndr_push_init_ctx(mem_ctx, iconv_convenience);
965 NDR_ERR_HAVE_NO_MEMORY(ndr);
967 NDR_CHECK(ndr_push_set_switch_value(ndr, p, level));
968 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
970 *blob = ndr_push_blob(ndr);
971 talloc_steal(mem_ctx, blob->data);
972 talloc_free(ndr);
974 return NDR_ERR_SUCCESS;
978 generic ndr_size_*() handler for structures
980 _PUBLIC_ size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push, struct smb_iconv_convenience *iconv_convenience)
982 struct ndr_push *ndr;
983 enum ndr_err_code status;
984 size_t ret;
986 /* avoid recursion */
987 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
989 ndr = ndr_push_init_ctx(NULL, iconv_convenience);
990 if (!ndr) return 0;
991 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
992 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
993 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
994 talloc_free(ndr);
995 return 0;
997 ret = ndr->offset;
998 talloc_free(ndr);
999 return ret;
1003 generic ndr_size_*() handler for unions
1005 _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)
1007 struct ndr_push *ndr;
1008 enum ndr_err_code status;
1009 size_t ret;
1011 /* avoid recursion */
1012 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
1014 ndr = ndr_push_init_ctx(NULL, iconv_convenience);
1015 if (!ndr) return 0;
1016 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
1018 status = ndr_push_set_switch_value(ndr, p, level);
1019 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1020 talloc_free(ndr);
1021 return 0;
1023 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
1024 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1025 talloc_free(ndr);
1026 return 0;
1028 ret = ndr->offset;
1029 talloc_free(ndr);
1030 return ret;
1034 get the current base for relative pointers for the push
1036 _PUBLIC_ uint32_t ndr_push_get_relative_base_offset(struct ndr_push *ndr)
1038 return ndr->relative_base_offset;
1042 restore the old base for relative pointers for the push
1044 _PUBLIC_ void ndr_push_restore_relative_base_offset(struct ndr_push *ndr, uint32_t offset)
1046 ndr->relative_base_offset = offset;
1050 setup the current base for relative pointers for the push
1051 called in the NDR_SCALAR stage
1053 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset1(struct ndr_push *ndr, const void *p, uint32_t offset)
1055 ndr->relative_base_offset = offset;
1056 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1060 setup the current base for relative pointers for the push
1061 called in the NDR_BUFFERS stage
1063 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset2(struct ndr_push *ndr, const void *p)
1065 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1069 push a relative object - stage1
1070 this is called during SCALARS processing
1072 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
1074 if (p == NULL) {
1075 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
1076 return NDR_ERR_SUCCESS;
1078 NDR_CHECK(ndr_push_align(ndr, 4));
1079 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1080 return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
1084 push a short relative object - stage1
1085 this is called during SCALARS processing
1087 _PUBLIC_ enum ndr_err_code ndr_push_short_relative_ptr1(struct ndr_push *ndr, const void *p)
1089 if (p == NULL) {
1090 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 0));
1091 return NDR_ERR_SUCCESS;
1093 NDR_CHECK(ndr_push_align(ndr, 2));
1094 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1095 return ndr_push_uint16(ndr, NDR_SCALARS, 0xFFFF);
1098 push a relative object - stage2
1099 this is called during buffers processing
1101 static enum ndr_err_code ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
1103 uint32_t save_offset;
1104 uint32_t ptr_offset = 0xFFFFFFFF;
1105 if (p == NULL) {
1106 return NDR_ERR_SUCCESS;
1108 save_offset = ndr->offset;
1109 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1110 if (ptr_offset > ndr->offset) {
1111 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1112 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1113 ptr_offset, ndr->offset);
1115 ndr->offset = ptr_offset;
1116 if (save_offset < ndr->relative_base_offset) {
1117 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1118 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1119 save_offset, ndr->relative_base_offset);
1121 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
1122 ndr->offset = save_offset;
1123 return NDR_ERR_SUCCESS;
1126 push a short relative object - stage2
1127 this is called during buffers processing
1129 _PUBLIC_ enum ndr_err_code ndr_push_short_relative_ptr2(struct ndr_push *ndr, const void *p)
1131 uint32_t save_offset;
1132 uint32_t ptr_offset = 0xFFFF;
1133 if (p == NULL) {
1134 return NDR_ERR_SUCCESS;
1136 save_offset = ndr->offset;
1137 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1138 if (ptr_offset > ndr->offset) {
1139 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1140 "ndr_push_short_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1141 ptr_offset, ndr->offset);
1143 ndr->offset = ptr_offset;
1144 if (save_offset < ndr->relative_base_offset) {
1145 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1146 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1147 save_offset, ndr->relative_base_offset);
1149 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
1150 ndr->offset = save_offset;
1151 return NDR_ERR_SUCCESS;
1155 push a relative object - stage2 start
1156 this is called during buffers processing
1158 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_start(struct ndr_push *ndr, const void *p)
1160 if (p == NULL) {
1161 return NDR_ERR_SUCCESS;
1163 if (!(ndr->flags & LIBNDR_FLAG_RELATIVE_REVERSE)) {
1164 return ndr_push_relative_ptr2(ndr, p);
1167 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1168 "ndr_push_relative_ptr2_start RELATIVE_REVERSE flag set, but not supported");
1172 push a relative object - stage2 end
1173 this is called during buffers processing
1175 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_end(struct ndr_push *ndr, const void *p)
1177 if (p == NULL) {
1178 return NDR_ERR_SUCCESS;
1180 return NDR_ERR_SUCCESS;
1184 get the current base for relative pointers for the pull
1186 _PUBLIC_ uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull *ndr)
1188 return ndr->relative_base_offset;
1192 restore the old base for relative pointers for the pull
1194 _PUBLIC_ void ndr_pull_restore_relative_base_offset(struct ndr_pull *ndr, uint32_t offset)
1196 ndr->relative_base_offset = offset;
1200 setup the current base for relative pointers for the pull
1201 called in the NDR_SCALAR stage
1203 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset1(struct ndr_pull *ndr, const void *p, uint32_t offset)
1205 ndr->relative_base_offset = offset;
1206 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1210 setup the current base for relative pointers for the pull
1211 called in the NDR_BUFFERS stage
1213 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset2(struct ndr_pull *ndr, const void *p)
1215 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1219 pull a relative object - stage1
1220 called during SCALARS processing
1222 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
1224 rel_offset += ndr->relative_base_offset;
1225 if (rel_offset > ndr->data_size) {
1226 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
1227 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1228 rel_offset, ndr->data_size);
1230 return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
1234 pull a relative object - stage2
1235 called during BUFFERS processing
1237 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
1239 uint32_t rel_offset;
1240 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
1241 return ndr_pull_set_offset(ndr, rel_offset);
1244 const static struct {
1245 enum ndr_err_code err;
1246 const char *string;
1247 } ndr_err_code_strings[] = {
1248 { NDR_ERR_SUCCESS, "Success" },
1249 { NDR_ERR_ARRAY_SIZE, "Bad Array Size" },
1250 { NDR_ERR_BAD_SWITCH, "Bad Switch" },
1251 { NDR_ERR_OFFSET, "Offset Error" },
1252 { NDR_ERR_RELATIVE, "Relative Pointer Error" },
1253 { NDR_ERR_CHARCNV, "Character Conversion Error" },
1254 { NDR_ERR_LENGTH, "Length Error" },
1255 { NDR_ERR_SUBCONTEXT, "Subcontext Error" },
1256 { NDR_ERR_COMPRESSION, "Compression Error" },
1257 { NDR_ERR_STRING, "String Error" },
1258 { NDR_ERR_VALIDATE, "Validate Error" },
1259 { NDR_ERR_BUFSIZE, "Buffer Size Error" },
1260 { NDR_ERR_ALLOC, "Allocation Error" },
1261 { NDR_ERR_RANGE, "Range Error" },
1262 { NDR_ERR_TOKEN, "Token Error" },
1263 { NDR_ERR_IPV4ADDRESS, "IPv4 Address Error" },
1264 { NDR_ERR_INVALID_POINTER, "Invalid Pointer" },
1265 { NDR_ERR_UNREAD_BYTES, "Unread Bytes" },
1266 { NDR_ERR_NDR64, "NDR64 assertion error" },
1267 { 0, NULL }
1270 _PUBLIC_ const char *ndr_map_error2string(enum ndr_err_code ndr_err)
1272 int i;
1273 for (i = 0; ndr_err_code_strings[i].string != NULL; i++) {
1274 if (ndr_err_code_strings[i].err == ndr_err)
1275 return ndr_err_code_strings[i].string;
1277 return "Unknown error";