librpc/ndr: make ndr_push_relative_ptr2() static
[Samba.git] / librpc / ndr / ndr.c
blob400045f5d04d86283aece837f46052affc2738a6
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 (*pflags) |= new_flags;
374 return and possibly log an NDR error
376 _PUBLIC_ enum ndr_err_code ndr_pull_error(struct ndr_pull *ndr,
377 enum ndr_err_code ndr_err,
378 const char *format, ...)
380 char *s=NULL;
381 va_list ap;
382 int ret;
384 va_start(ap, format);
385 ret = vasprintf(&s, format, ap);
386 va_end(ap);
388 if (ret == -1) {
389 return NDR_ERR_ALLOC;
392 DEBUG(1,("ndr_pull_error(%u): %s\n", ndr_err, s));
394 free(s);
396 return ndr_err;
400 return and possibly log an NDR error
402 _PUBLIC_ enum ndr_err_code ndr_push_error(struct ndr_push *ndr,
403 enum ndr_err_code ndr_err,
404 const char *format, ...)
406 char *s=NULL;
407 va_list ap;
408 int ret;
410 va_start(ap, format);
411 ret = vasprintf(&s, format, ap);
412 va_end(ap);
414 if (ret == -1) {
415 return NDR_ERR_ALLOC;
418 DEBUG(1,("ndr_push_error(%u): %s\n", ndr_err, s));
420 free(s);
422 return ndr_err;
426 handle subcontext buffers, which in midl land are user-marshalled, but
427 we use magic in pidl to make them easier to cope with
429 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_start(struct ndr_pull *ndr,
430 struct ndr_pull **_subndr,
431 size_t header_size,
432 ssize_t size_is)
434 struct ndr_pull *subndr;
435 uint32_t r_content_size;
436 bool force_le = false;
437 bool force_be = false;
439 switch (header_size) {
440 case 0: {
441 uint32_t content_size = ndr->data_size - ndr->offset;
442 if (size_is >= 0) {
443 content_size = size_is;
445 r_content_size = content_size;
446 break;
449 case 2: {
450 uint16_t content_size;
451 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &content_size));
452 if (size_is >= 0 && size_is != content_size) {
453 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
454 (int)size_is, (int)content_size);
456 r_content_size = content_size;
457 break;
460 case 4: {
461 uint32_t content_size;
462 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &content_size));
463 if (size_is >= 0 && size_is != content_size) {
464 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
465 (int)size_is, (int)content_size);
467 r_content_size = content_size;
468 break;
470 case 0xFFFFFC01: {
472 * Common Type Header for the Serialization Stream
473 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
475 uint8_t version;
476 uint8_t drep;
477 uint16_t hdrlen;
478 uint32_t filler;
479 uint32_t content_size;
480 uint32_t reserved;
482 /* version */
483 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &version));
485 if (version != 1) {
486 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
487 "Bad subcontext (PULL) Common Type Header version %d != 1",
488 (int)version);
492 * 0x10 little endian
493 * 0x00 big endian
495 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &drep));
496 if (drep == 0x10) {
497 force_le = true;
498 } else if (drep == 0x00) {
499 force_be = true;
500 } else {
501 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
502 "Bad subcontext (PULL) Common Type Header invalid drep 0x%02X",
503 (unsigned int)drep);
506 /* length of the "Private Header for Constructed Type" */
507 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &hdrlen));
508 if (hdrlen != 8) {
509 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
510 "Bad subcontext (PULL) Common Type Header length %d != 8",
511 (int)hdrlen);
514 /* filler should be ignored */
515 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &filler));
518 * Private Header for Constructed Type
520 /* length - will be updated latter */
521 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
522 if (size_is >= 0 && size_is != content_size) {
523 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
524 (int)size_is, (int)content_size);
526 /* the content size must be a multiple of 8 */
527 if ((content_size % 8) != 0) {
528 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
529 "Bad subcontext (PULL) size_is(%d) not padded to 8 content_size %d",
530 (int)size_is, (int)content_size);
532 r_content_size = content_size;
534 /* reserved */
535 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &reserved));
536 break;
538 default:
539 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) header_size %d",
540 (int)header_size);
543 NDR_PULL_NEED_BYTES(ndr, r_content_size);
545 subndr = talloc_zero(ndr, struct ndr_pull);
546 NDR_ERR_HAVE_NO_MEMORY(subndr);
547 subndr->flags = ndr->flags & ~LIBNDR_FLAG_NDR64;
548 subndr->current_mem_ctx = ndr->current_mem_ctx;
550 subndr->data = ndr->data + ndr->offset;
551 subndr->offset = 0;
552 subndr->data_size = r_content_size;
553 subndr->iconv_convenience = talloc_reference(subndr, ndr->iconv_convenience);
555 if (force_le) {
556 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_LITTLE_ENDIAN);
557 } else if (force_be) {
558 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_BIGENDIAN);
561 *_subndr = subndr;
562 return NDR_ERR_SUCCESS;
565 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_end(struct ndr_pull *ndr,
566 struct ndr_pull *subndr,
567 size_t header_size,
568 ssize_t size_is)
570 uint32_t advance;
571 if (size_is >= 0) {
572 advance = size_is;
573 } else if (header_size > 0) {
574 advance = subndr->data_size;
575 } else {
576 advance = subndr->offset;
578 NDR_CHECK(ndr_pull_advance(ndr, advance));
579 return NDR_ERR_SUCCESS;
582 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_start(struct ndr_push *ndr,
583 struct ndr_push **_subndr,
584 size_t header_size,
585 ssize_t size_is)
587 struct ndr_push *subndr;
589 subndr = ndr_push_init_ctx(ndr, ndr->iconv_convenience);
590 NDR_ERR_HAVE_NO_MEMORY(subndr);
591 subndr->flags = ndr->flags & ~LIBNDR_FLAG_NDR64;
593 *_subndr = subndr;
594 return NDR_ERR_SUCCESS;
598 push a subcontext header
600 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_end(struct ndr_push *ndr,
601 struct ndr_push *subndr,
602 size_t header_size,
603 ssize_t size_is)
605 ssize_t padding_len;
607 if (size_is >= 0) {
608 padding_len = size_is - subndr->offset;
609 if (padding_len > 0) {
610 NDR_CHECK(ndr_push_zero(subndr, padding_len));
611 } else if (padding_len < 0) {
612 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
613 (int)subndr->offset, (int)size_is);
617 switch (header_size) {
618 case 0:
619 break;
621 case 2:
622 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, subndr->offset));
623 break;
625 case 4:
626 NDR_CHECK(ndr_push_uint3264(ndr, NDR_SCALARS, subndr->offset));
627 break;
629 case 0xFFFFFC01:
631 * Common Type Header for the Serialization Stream
632 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
634 padding_len = NDR_ROUND(subndr->offset, 8) - subndr->offset;
635 if (padding_len > 0) {
636 NDR_CHECK(ndr_push_zero(subndr, padding_len));
639 /* version */
640 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, 1));
643 * 0x10 little endian
644 * 0x00 big endian
646 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, NDR_BE(ndr)?0x00:0x10));
648 /* length of the "Private Header for Constructed Type" */
649 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 8));
651 /* filler */
652 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0xCCCCCCCC));
655 * Private Header for Constructed Type
657 /* length - will be updated latter */
658 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
660 /* reserved */
661 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
662 break;
664 default:
665 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext header size %d",
666 (int)header_size);
669 NDR_CHECK(ndr_push_bytes(ndr, subndr->data, subndr->offset));
670 return NDR_ERR_SUCCESS;
674 store a token in the ndr context, for later retrieval
676 _PUBLIC_ enum ndr_err_code ndr_token_store(TALLOC_CTX *mem_ctx,
677 struct ndr_token_list **list,
678 const void *key,
679 uint32_t value)
681 struct ndr_token_list *tok;
682 tok = talloc(mem_ctx, struct ndr_token_list);
683 NDR_ERR_HAVE_NO_MEMORY(tok);
684 tok->key = key;
685 tok->value = value;
686 DLIST_ADD((*list), tok);
687 return NDR_ERR_SUCCESS;
691 retrieve a token from a ndr context, using cmp_fn to match the tokens
693 _PUBLIC_ enum ndr_err_code ndr_token_retrieve_cmp_fn(struct ndr_token_list **list, const void *key, uint32_t *v,
694 comparison_fn_t _cmp_fn, bool _remove_tok)
696 struct ndr_token_list *tok;
697 for (tok=*list;tok;tok=tok->next) {
698 if (_cmp_fn && _cmp_fn(tok->key,key)==0) goto found;
699 else if (!_cmp_fn && tok->key == key) goto found;
701 return NDR_ERR_TOKEN;
702 found:
703 *v = tok->value;
704 if (_remove_tok) {
705 DLIST_REMOVE((*list), tok);
706 talloc_free(tok);
708 return NDR_ERR_SUCCESS;
712 retrieve a token from a ndr context
714 _PUBLIC_ enum ndr_err_code ndr_token_retrieve(struct ndr_token_list **list, const void *key, uint32_t *v)
716 return ndr_token_retrieve_cmp_fn(list, key, v, NULL, true);
720 peek at but don't removed a token from a ndr context
722 _PUBLIC_ uint32_t ndr_token_peek(struct ndr_token_list **list, const void *key)
724 enum ndr_err_code status;
725 uint32_t v;
727 status = ndr_token_retrieve_cmp_fn(list, key, &v, NULL, false);
728 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
729 return 0;
732 return v;
736 pull an array size field and add it to the array_size_list token list
738 _PUBLIC_ enum ndr_err_code ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
740 uint32_t size;
741 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &size));
742 return ndr_token_store(ndr, &ndr->array_size_list, p, size);
746 get the stored array size field
748 _PUBLIC_ uint32_t ndr_get_array_size(struct ndr_pull *ndr, const void *p)
750 return ndr_token_peek(&ndr->array_size_list, p);
754 check the stored array size field
756 _PUBLIC_ enum ndr_err_code ndr_check_array_size(struct ndr_pull *ndr, void *p, uint32_t size)
758 uint32_t stored;
759 stored = ndr_token_peek(&ndr->array_size_list, p);
760 if (stored != size) {
761 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
762 "Bad array size - got %u expected %u\n",
763 stored, size);
765 return NDR_ERR_SUCCESS;
769 pull an array length field and add it to the array_length_list token list
771 _PUBLIC_ enum ndr_err_code ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
773 uint32_t length, offset;
774 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &offset));
775 if (offset != 0) {
776 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
777 "non-zero array offset %u\n", offset);
779 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &length));
780 return ndr_token_store(ndr, &ndr->array_length_list, p, length);
784 get the stored array length field
786 _PUBLIC_ uint32_t ndr_get_array_length(struct ndr_pull *ndr, const void *p)
788 return ndr_token_peek(&ndr->array_length_list, p);
792 check the stored array length field
794 _PUBLIC_ enum ndr_err_code ndr_check_array_length(struct ndr_pull *ndr, void *p, uint32_t length)
796 uint32_t stored;
797 stored = ndr_token_peek(&ndr->array_length_list, p);
798 if (stored != length) {
799 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
800 "Bad array length - got %u expected %u\n",
801 stored, length);
803 return NDR_ERR_SUCCESS;
807 store a switch value
809 _PUBLIC_ enum ndr_err_code ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
811 return ndr_token_store(ndr, &ndr->switch_list, p, val);
814 _PUBLIC_ enum ndr_err_code ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
816 return ndr_token_store(ndr, &ndr->switch_list, p, val);
819 _PUBLIC_ enum ndr_err_code ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
821 return ndr_token_store(ndr, &ndr->switch_list, p, val);
825 retrieve a switch value
827 _PUBLIC_ uint32_t ndr_push_get_switch_value(struct ndr_push *ndr, const void *p)
829 return ndr_token_peek(&ndr->switch_list, p);
832 _PUBLIC_ uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
834 return ndr_token_peek(&ndr->switch_list, p);
837 _PUBLIC_ uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
839 return ndr_token_peek(&ndr->switch_list, p);
843 pull a struct from a blob using NDR
845 _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,
846 ndr_pull_flags_fn_t fn)
848 struct ndr_pull *ndr;
849 ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
850 NDR_ERR_HAVE_NO_MEMORY(ndr);
851 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
852 talloc_free(ndr);
853 return NDR_ERR_SUCCESS;
857 pull a struct from a blob using NDR - failing if all bytes are not consumed
859 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
860 struct smb_iconv_convenience *iconv_convenience,
861 void *p, 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_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
867 if (ndr->offset < ndr->data_size) {
868 return ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
869 "not all bytes consumed ofs[%u] size[%u]",
870 ndr->offset, ndr->data_size);
872 talloc_free(ndr);
873 return NDR_ERR_SUCCESS;
877 pull a union from a blob using NDR, given the union discriminator
879 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
880 struct smb_iconv_convenience *iconv_convenience, void *p,
881 uint32_t level, ndr_pull_flags_fn_t fn)
883 struct ndr_pull *ndr;
884 ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
885 NDR_ERR_HAVE_NO_MEMORY(ndr);
886 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
887 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
888 talloc_free(ndr);
889 return NDR_ERR_SUCCESS;
893 pull a union from a blob using NDR, given the union discriminator,
894 failing if all bytes are not consumed
896 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
897 struct smb_iconv_convenience *iconv_convenience, void *p,
898 uint32_t level, ndr_pull_flags_fn_t fn)
900 struct ndr_pull *ndr;
901 ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
902 NDR_ERR_HAVE_NO_MEMORY(ndr);
903 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
904 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
905 if (ndr->offset < ndr->data_size) {
906 enum ndr_err_code ret;
907 ret = ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
908 "not all bytes consumed ofs[%u] size[%u]",
909 ndr->offset, ndr->data_size);
910 talloc_free(ndr);
911 return ret;
913 talloc_free(ndr);
914 return NDR_ERR_SUCCESS;
918 push a struct to a blob using NDR
920 _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)
922 struct ndr_push *ndr;
923 ndr = ndr_push_init_ctx(mem_ctx, iconv_convenience);
924 NDR_ERR_HAVE_NO_MEMORY(ndr);
926 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
928 *blob = ndr_push_blob(ndr);
929 talloc_steal(mem_ctx, blob->data);
930 talloc_free(ndr);
932 return NDR_ERR_SUCCESS;
936 push a union to a blob using NDR
938 _PUBLIC_ enum ndr_err_code ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience, void *p,
939 uint32_t level, ndr_push_flags_fn_t fn)
941 struct ndr_push *ndr;
942 ndr = ndr_push_init_ctx(mem_ctx, iconv_convenience);
943 NDR_ERR_HAVE_NO_MEMORY(ndr);
945 NDR_CHECK(ndr_push_set_switch_value(ndr, p, level));
946 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
948 *blob = ndr_push_blob(ndr);
949 talloc_steal(mem_ctx, blob->data);
950 talloc_free(ndr);
952 return NDR_ERR_SUCCESS;
956 generic ndr_size_*() handler for structures
958 _PUBLIC_ size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push, struct smb_iconv_convenience *iconv_convenience)
960 struct ndr_push *ndr;
961 enum ndr_err_code status;
962 size_t ret;
964 /* avoid recursion */
965 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
967 ndr = ndr_push_init_ctx(NULL, iconv_convenience);
968 if (!ndr) return 0;
969 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
970 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
971 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
972 talloc_free(ndr);
973 return 0;
975 ret = ndr->offset;
976 talloc_free(ndr);
977 return ret;
981 generic ndr_size_*() handler for unions
983 _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)
985 struct ndr_push *ndr;
986 enum ndr_err_code status;
987 size_t ret;
989 /* avoid recursion */
990 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
992 ndr = ndr_push_init_ctx(NULL, iconv_convenience);
993 if (!ndr) return 0;
994 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
996 status = ndr_push_set_switch_value(ndr, p, level);
997 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
998 talloc_free(ndr);
999 return 0;
1001 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
1002 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1003 talloc_free(ndr);
1004 return 0;
1006 ret = ndr->offset;
1007 talloc_free(ndr);
1008 return ret;
1012 get the current base for relative pointers for the push
1014 _PUBLIC_ uint32_t ndr_push_get_relative_base_offset(struct ndr_push *ndr)
1016 return ndr->relative_base_offset;
1020 restore the old base for relative pointers for the push
1022 _PUBLIC_ void ndr_push_restore_relative_base_offset(struct ndr_push *ndr, uint32_t offset)
1024 ndr->relative_base_offset = offset;
1028 setup the current base for relative pointers for the push
1029 called in the NDR_SCALAR stage
1031 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset1(struct ndr_push *ndr, const void *p, uint32_t offset)
1033 ndr->relative_base_offset = offset;
1034 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1038 setup the current base for relative pointers for the push
1039 called in the NDR_BUFFERS stage
1041 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset2(struct ndr_push *ndr, const void *p)
1043 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1047 push a relative object - stage1
1048 this is called during SCALARS processing
1050 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
1052 if (p == NULL) {
1053 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
1054 return NDR_ERR_SUCCESS;
1056 NDR_CHECK(ndr_push_align(ndr, 4));
1057 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1058 return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
1062 push a relative object - stage2
1063 this is called during buffers processing
1065 static enum ndr_err_code ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
1067 uint32_t save_offset;
1068 uint32_t ptr_offset = 0xFFFFFFFF;
1069 if (p == NULL) {
1070 return NDR_ERR_SUCCESS;
1072 save_offset = ndr->offset;
1073 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1074 if (ptr_offset > ndr->offset) {
1075 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1076 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1077 ptr_offset, ndr->offset);
1079 ndr->offset = ptr_offset;
1080 if (save_offset < ndr->relative_base_offset) {
1081 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1082 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1083 save_offset, ndr->relative_base_offset);
1085 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
1086 ndr->offset = save_offset;
1087 return NDR_ERR_SUCCESS;
1091 push a relative object - stage2 start
1092 this is called during buffers processing
1094 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_start(struct ndr_push *ndr, const void *p)
1096 if (p == NULL) {
1097 return NDR_ERR_SUCCESS;
1099 return ndr_push_relative_ptr2(ndr, p);
1103 push a relative object - stage2 end
1104 this is called during buffers processing
1106 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_end(struct ndr_push *ndr, const void *p)
1108 if (p == NULL) {
1109 return NDR_ERR_SUCCESS;
1111 return NDR_ERR_SUCCESS;
1115 get the current base for relative pointers for the pull
1117 _PUBLIC_ uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull *ndr)
1119 return ndr->relative_base_offset;
1123 restore the old base for relative pointers for the pull
1125 _PUBLIC_ void ndr_pull_restore_relative_base_offset(struct ndr_pull *ndr, uint32_t offset)
1127 ndr->relative_base_offset = offset;
1131 setup the current base for relative pointers for the pull
1132 called in the NDR_SCALAR stage
1134 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset1(struct ndr_pull *ndr, const void *p, uint32_t offset)
1136 ndr->relative_base_offset = offset;
1137 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1141 setup the current base for relative pointers for the pull
1142 called in the NDR_BUFFERS stage
1144 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset2(struct ndr_pull *ndr, const void *p)
1146 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1150 pull a relative object - stage1
1151 called during SCALARS processing
1153 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
1155 rel_offset += ndr->relative_base_offset;
1156 if (rel_offset > ndr->data_size) {
1157 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
1158 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1159 rel_offset, ndr->data_size);
1161 return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
1165 pull a relative object - stage2
1166 called during BUFFERS processing
1168 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
1170 uint32_t rel_offset;
1171 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
1172 return ndr_pull_set_offset(ndr, rel_offset);
1175 const static struct {
1176 enum ndr_err_code err;
1177 const char *string;
1178 } ndr_err_code_strings[] = {
1179 { NDR_ERR_SUCCESS, "Success" },
1180 { NDR_ERR_ARRAY_SIZE, "Bad Array Size" },
1181 { NDR_ERR_BAD_SWITCH, "Bad Switch" },
1182 { NDR_ERR_OFFSET, "Offset Error" },
1183 { NDR_ERR_RELATIVE, "Relative Pointer Error" },
1184 { NDR_ERR_CHARCNV, "Character Conversion Error" },
1185 { NDR_ERR_LENGTH, "Length Error" },
1186 { NDR_ERR_SUBCONTEXT, "Subcontext Error" },
1187 { NDR_ERR_COMPRESSION, "Compression Error" },
1188 { NDR_ERR_STRING, "String Error" },
1189 { NDR_ERR_VALIDATE, "Validate Error" },
1190 { NDR_ERR_BUFSIZE, "Buffer Size Error" },
1191 { NDR_ERR_ALLOC, "Allocation Error" },
1192 { NDR_ERR_RANGE, "Range Error" },
1193 { NDR_ERR_TOKEN, "Token Error" },
1194 { NDR_ERR_IPV4ADDRESS, "IPv4 Address Error" },
1195 { NDR_ERR_INVALID_POINTER, "Invalid Pointer" },
1196 { NDR_ERR_UNREAD_BYTES, "Unread Bytes" },
1197 { NDR_ERR_NDR64, "NDR64 assertion error" },
1198 { 0, NULL }
1201 _PUBLIC_ const char *ndr_map_error2string(enum ndr_err_code ndr_err)
1203 int i;
1204 for (i = 0; ndr_err_code_strings[i].string != NULL; i++) {
1205 if (ndr_err_code_strings[i].err == ndr_err)
1206 return ndr_err_code_strings[i].string;
1208 return "Unknown error";