s4:rootdse LDB module - remove unused variable
[Samba.git] / librpc / ndr / ndr.c
blob63b2cc323c6c2df2e89d3d7ba1f521588bf1bcb8
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)
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;
72 return ndr;
76 advance by 'size' bytes
78 _PUBLIC_ enum ndr_err_code ndr_pull_advance(struct ndr_pull *ndr, uint32_t size)
80 ndr->offset += size;
81 if (ndr->offset > ndr->data_size) {
82 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
83 "ndr_pull_advance by %u failed",
84 size);
86 return NDR_ERR_SUCCESS;
90 set the parse offset to 'ofs'
92 static enum ndr_err_code ndr_pull_set_offset(struct ndr_pull *ndr, uint32_t ofs)
94 ndr->offset = ofs;
95 if (ndr->offset > ndr->data_size) {
96 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
97 "ndr_pull_set_offset %u failed",
98 ofs);
100 return NDR_ERR_SUCCESS;
103 /* create a ndr_push structure, ready for some marshalling */
104 _PUBLIC_ struct ndr_push *ndr_push_init_ctx(TALLOC_CTX *mem_ctx)
106 struct ndr_push *ndr;
108 ndr = talloc_zero(mem_ctx, struct ndr_push);
109 if (!ndr) {
110 return NULL;
113 ndr->flags = 0;
114 ndr->alloc_size = NDR_BASE_MARSHALL_SIZE;
115 ndr->data = talloc_array(ndr, uint8_t, ndr->alloc_size);
116 if (!ndr->data) {
117 return NULL;
120 return ndr;
123 /* return a DATA_BLOB structure for the current ndr_push marshalled data */
124 _PUBLIC_ DATA_BLOB ndr_push_blob(struct ndr_push *ndr)
126 DATA_BLOB blob;
127 blob = data_blob_const(ndr->data, ndr->offset);
128 if (ndr->alloc_size > ndr->offset) {
129 ndr->data[ndr->offset] = 0;
131 return blob;
136 expand the available space in the buffer to ndr->offset + extra_size
138 _PUBLIC_ enum ndr_err_code ndr_push_expand(struct ndr_push *ndr, uint32_t extra_size)
140 uint32_t size = extra_size + ndr->offset;
142 if (size < ndr->offset) {
143 /* extra_size overflowed the offset */
144 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, "Overflow in push_expand to %u",
145 size);
148 if (ndr->alloc_size > size) {
149 return NDR_ERR_SUCCESS;
152 ndr->alloc_size += NDR_BASE_MARSHALL_SIZE;
153 if (size+1 > ndr->alloc_size) {
154 ndr->alloc_size = size+1;
156 ndr->data = talloc_realloc(ndr, ndr->data, uint8_t, ndr->alloc_size);
157 if (!ndr->data) {
158 return ndr_push_error(ndr, NDR_ERR_ALLOC, "Failed to push_expand to %u",
159 ndr->alloc_size);
162 return NDR_ERR_SUCCESS;
165 _PUBLIC_ void ndr_print_debug_helper(struct ndr_print *ndr, const char *format, ...)
167 va_list ap;
168 char *s = NULL;
169 uint32_t i;
170 int ret;
172 va_start(ap, format);
173 ret = vasprintf(&s, format, ap);
174 va_end(ap);
176 if (ret == -1) {
177 return;
180 if (ndr->no_newline) {
181 DEBUGADD(1,("%s", s));
182 free(s);
183 return;
186 for (i=0;i<ndr->depth;i++) {
187 DEBUGADD(1,(" "));
190 DEBUGADD(1,("%s\n", s));
191 free(s);
194 _PUBLIC_ void ndr_print_printf_helper(struct ndr_print *ndr, const char *format, ...)
196 va_list ap;
197 uint32_t i;
199 if (!ndr->no_newline) {
200 for (i=0;i<ndr->depth;i++) {
201 printf(" ");
205 va_start(ap, format);
206 vprintf(format, ap);
207 va_end(ap);
208 if (!ndr->no_newline) {
209 printf("\n");
213 _PUBLIC_ void ndr_print_string_helper(struct ndr_print *ndr, const char *format, ...)
215 va_list ap;
216 uint32_t i;
218 if (!ndr->no_newline) {
219 for (i=0;i<ndr->depth;i++) {
220 ndr->private_data = talloc_asprintf_append_buffer(
221 (char *)ndr->private_data, " ");
225 va_start(ap, format);
226 ndr->private_data = talloc_vasprintf_append_buffer((char *)ndr->private_data,
227 format, ap);
228 va_end(ap);
229 if (!ndr->no_newline) {
230 ndr->private_data = talloc_asprintf_append_buffer((char *)ndr->private_data,
231 "\n");
236 a useful helper function for printing idl structures via DEBUG()
238 _PUBLIC_ void ndr_print_debug(ndr_print_fn_t fn, const char *name, void *ptr)
240 struct ndr_print *ndr;
242 DEBUG(1,(" "));
244 ndr = talloc_zero(NULL, struct ndr_print);
245 if (!ndr) return;
246 ndr->print = ndr_print_debug_helper;
247 ndr->depth = 1;
248 ndr->flags = 0;
249 fn(ndr, name, ptr);
250 talloc_free(ndr);
254 a useful helper function for printing idl unions via DEBUG()
256 _PUBLIC_ void ndr_print_union_debug(ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
258 struct ndr_print *ndr;
260 DEBUG(1,(" "));
262 ndr = talloc_zero(NULL, struct ndr_print);
263 if (!ndr) return;
264 ndr->print = ndr_print_debug_helper;
265 ndr->depth = 1;
266 ndr->flags = 0;
267 ndr_print_set_switch_value(ndr, ptr, level);
268 fn(ndr, name, ptr);
269 talloc_free(ndr);
273 a useful helper function for printing idl function calls via DEBUG()
275 _PUBLIC_ void ndr_print_function_debug(ndr_print_function_t fn, const char *name, int flags, void *ptr)
277 struct ndr_print *ndr;
279 DEBUG(1,(" "));
281 ndr = talloc_zero(NULL, struct ndr_print);
282 if (!ndr) return;
283 ndr->print = ndr_print_debug_helper;
284 ndr->depth = 1;
285 ndr->flags = 0;
287 fn(ndr, name, flags, ptr);
288 talloc_free(ndr);
292 a useful helper function for printing idl structures to a string
294 _PUBLIC_ char *ndr_print_struct_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, void *ptr)
296 struct ndr_print *ndr;
297 char *ret = NULL;
299 ndr = talloc_zero(mem_ctx, struct ndr_print);
300 if (!ndr) return NULL;
301 ndr->private_data = talloc_strdup(ndr, "");
302 if (!ndr->private_data) {
303 goto failed;
305 ndr->print = ndr_print_string_helper;
306 ndr->depth = 1;
307 ndr->flags = 0;
309 fn(ndr, name, ptr);
310 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
311 failed:
312 talloc_free(ndr);
313 return ret;
317 a useful helper function for printing idl unions to a string
319 _PUBLIC_ char *ndr_print_union_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
321 struct ndr_print *ndr;
322 char *ret = NULL;
324 ndr = talloc_zero(mem_ctx, struct ndr_print);
325 if (!ndr) return NULL;
326 ndr->private_data = talloc_strdup(ndr, "");
327 if (!ndr->private_data) {
328 goto failed;
330 ndr->print = ndr_print_string_helper;
331 ndr->depth = 1;
332 ndr->flags = 0;
333 ndr_print_set_switch_value(ndr, ptr, level);
334 fn(ndr, name, ptr);
335 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
336 failed:
337 talloc_free(ndr);
338 return ret;
342 a useful helper function for printing idl function calls to a string
344 _PUBLIC_ char *ndr_print_function_string(TALLOC_CTX *mem_ctx,
345 ndr_print_function_t fn, const char *name,
346 int flags, void *ptr)
348 struct ndr_print *ndr;
349 char *ret = NULL;
351 ndr = talloc_zero(mem_ctx, struct ndr_print);
352 if (!ndr) return NULL;
353 ndr->private_data = talloc_strdup(ndr, "");
354 if (!ndr->private_data) {
355 goto failed;
357 ndr->print = ndr_print_string_helper;
358 ndr->depth = 1;
359 ndr->flags = 0;
360 fn(ndr, name, flags, ptr);
361 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
362 failed:
363 talloc_free(ndr);
364 return ret;
367 _PUBLIC_ void ndr_set_flags(uint32_t *pflags, uint32_t new_flags)
369 /* the big/little endian flags are inter-dependent */
370 if (new_flags & LIBNDR_FLAG_LITTLE_ENDIAN) {
371 (*pflags) &= ~LIBNDR_FLAG_BIGENDIAN;
372 (*pflags) &= ~LIBNDR_FLAG_NDR64;
374 if (new_flags & LIBNDR_FLAG_BIGENDIAN) {
375 (*pflags) &= ~LIBNDR_FLAG_LITTLE_ENDIAN;
376 (*pflags) &= ~LIBNDR_FLAG_NDR64;
378 if (new_flags & LIBNDR_FLAG_REMAINING) {
379 (*pflags) &= ~LIBNDR_ALIGN_FLAGS;
381 if (new_flags & LIBNDR_ALIGN_FLAGS) {
382 (*pflags) &= ~LIBNDR_FLAG_REMAINING;
384 if (new_flags & LIBNDR_FLAG_NO_RELATIVE_REVERSE) {
385 (*pflags) &= ~LIBNDR_FLAG_RELATIVE_REVERSE;
387 (*pflags) |= new_flags;
391 return and possibly log an NDR error
393 _PUBLIC_ enum ndr_err_code ndr_pull_error(struct ndr_pull *ndr,
394 enum ndr_err_code ndr_err,
395 const char *format, ...)
397 char *s=NULL;
398 va_list ap;
399 int ret;
401 va_start(ap, format);
402 ret = vasprintf(&s, format, ap);
403 va_end(ap);
405 if (ret == -1) {
406 return NDR_ERR_ALLOC;
409 DEBUG(1,("ndr_pull_error(%u): %s\n", ndr_err, s));
411 free(s);
413 return ndr_err;
417 return and possibly log an NDR error
419 _PUBLIC_ enum ndr_err_code ndr_push_error(struct ndr_push *ndr,
420 enum ndr_err_code ndr_err,
421 const char *format, ...)
423 char *s=NULL;
424 va_list ap;
425 int ret;
427 va_start(ap, format);
428 ret = vasprintf(&s, format, ap);
429 va_end(ap);
431 if (ret == -1) {
432 return NDR_ERR_ALLOC;
435 DEBUG(1,("ndr_push_error(%u): %s\n", ndr_err, s));
437 free(s);
439 return ndr_err;
443 handle subcontext buffers, which in midl land are user-marshalled, but
444 we use magic in pidl to make them easier to cope with
446 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_start(struct ndr_pull *ndr,
447 struct ndr_pull **_subndr,
448 size_t header_size,
449 ssize_t size_is)
451 struct ndr_pull *subndr;
452 uint32_t r_content_size;
453 bool force_le = false;
454 bool force_be = false;
456 switch (header_size) {
457 case 0: {
458 uint32_t content_size = ndr->data_size - ndr->offset;
459 if (size_is >= 0) {
460 content_size = size_is;
462 r_content_size = content_size;
463 break;
466 case 2: {
467 uint16_t content_size;
468 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &content_size));
469 if (size_is >= 0 && size_is != content_size) {
470 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
471 (int)size_is, (int)content_size);
473 r_content_size = content_size;
474 break;
477 case 4: {
478 uint32_t content_size;
479 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &content_size));
480 if (size_is >= 0 && size_is != content_size) {
481 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
482 (int)size_is, (int)content_size);
484 r_content_size = content_size;
485 break;
487 case 0xFFFFFC01: {
489 * Common Type Header for the Serialization Stream
490 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
492 uint8_t version;
493 uint8_t drep;
494 uint16_t hdrlen;
495 uint32_t filler;
496 uint32_t content_size;
497 uint32_t reserved;
499 /* version */
500 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &version));
502 if (version != 1) {
503 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
504 "Bad subcontext (PULL) Common Type Header version %d != 1",
505 (int)version);
509 * 0x10 little endian
510 * 0x00 big endian
512 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &drep));
513 if (drep == 0x10) {
514 force_le = true;
515 } else if (drep == 0x00) {
516 force_be = true;
517 } else {
518 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
519 "Bad subcontext (PULL) Common Type Header invalid drep 0x%02X",
520 (unsigned int)drep);
523 /* length of the "Private Header for Constructed Type" */
524 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &hdrlen));
525 if (hdrlen != 8) {
526 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
527 "Bad subcontext (PULL) Common Type Header length %d != 8",
528 (int)hdrlen);
531 /* filler should be ignored */
532 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &filler));
535 * Private Header for Constructed Type
537 /* length - will be updated latter */
538 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
539 if (size_is >= 0 && size_is != content_size) {
540 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
541 (int)size_is, (int)content_size);
543 /* the content size must be a multiple of 8 */
544 if ((content_size % 8) != 0) {
545 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
546 "Bad subcontext (PULL) size_is(%d) not padded to 8 content_size %d",
547 (int)size_is, (int)content_size);
549 r_content_size = content_size;
551 /* reserved */
552 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &reserved));
553 break;
555 default:
556 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) header_size %d",
557 (int)header_size);
560 NDR_PULL_NEED_BYTES(ndr, r_content_size);
562 subndr = talloc_zero(ndr, struct ndr_pull);
563 NDR_ERR_HAVE_NO_MEMORY(subndr);
564 subndr->flags = ndr->flags & ~LIBNDR_FLAG_NDR64;
565 subndr->current_mem_ctx = ndr->current_mem_ctx;
567 subndr->data = ndr->data + ndr->offset;
568 subndr->offset = 0;
569 subndr->data_size = r_content_size;
571 if (force_le) {
572 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_LITTLE_ENDIAN);
573 } else if (force_be) {
574 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_BIGENDIAN);
577 *_subndr = subndr;
578 return NDR_ERR_SUCCESS;
581 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_end(struct ndr_pull *ndr,
582 struct ndr_pull *subndr,
583 size_t header_size,
584 ssize_t size_is)
586 uint32_t advance;
587 if (size_is >= 0) {
588 advance = size_is;
589 } else if (header_size > 0) {
590 advance = subndr->data_size;
591 } else {
592 advance = subndr->offset;
594 NDR_CHECK(ndr_pull_advance(ndr, advance));
595 return NDR_ERR_SUCCESS;
598 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_start(struct ndr_push *ndr,
599 struct ndr_push **_subndr,
600 size_t header_size,
601 ssize_t size_is)
603 struct ndr_push *subndr;
605 subndr = ndr_push_init_ctx(ndr);
606 NDR_ERR_HAVE_NO_MEMORY(subndr);
607 subndr->flags = ndr->flags & ~LIBNDR_FLAG_NDR64;
609 if (size_is > 0) {
610 NDR_CHECK(ndr_push_zero(subndr, size_is));
611 subndr->offset = 0;
612 subndr->relative_end_offset = size_is;
615 *_subndr = subndr;
616 return NDR_ERR_SUCCESS;
620 push a subcontext header
622 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_end(struct ndr_push *ndr,
623 struct ndr_push *subndr,
624 size_t header_size,
625 ssize_t size_is)
627 ssize_t padding_len;
629 if (size_is >= 0) {
630 padding_len = size_is - subndr->offset;
631 if (padding_len < 0) {
632 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
633 (int)subndr->offset, (int)size_is);
635 subndr->offset = size_is;
638 switch (header_size) {
639 case 0:
640 break;
642 case 2:
643 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, subndr->offset));
644 break;
646 case 4:
647 NDR_CHECK(ndr_push_uint3264(ndr, NDR_SCALARS, subndr->offset));
648 break;
650 case 0xFFFFFC01:
652 * Common Type Header for the Serialization Stream
653 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
655 padding_len = NDR_ROUND(subndr->offset, 8) - subndr->offset;
656 if (padding_len > 0) {
657 NDR_CHECK(ndr_push_zero(subndr, padding_len));
660 /* version */
661 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, 1));
664 * 0x10 little endian
665 * 0x00 big endian
667 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, NDR_BE(ndr)?0x00:0x10));
669 /* length of the "Private Header for Constructed Type" */
670 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 8));
672 /* filler */
673 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0xCCCCCCCC));
676 * Private Header for Constructed Type
678 /* length - will be updated latter */
679 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
681 /* reserved */
682 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
683 break;
685 default:
686 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext header size %d",
687 (int)header_size);
690 NDR_CHECK(ndr_push_bytes(ndr, subndr->data, subndr->offset));
691 return NDR_ERR_SUCCESS;
695 store a token in the ndr context, for later retrieval
697 _PUBLIC_ enum ndr_err_code ndr_token_store(TALLOC_CTX *mem_ctx,
698 struct ndr_token_list **list,
699 const void *key,
700 uint32_t value)
702 struct ndr_token_list *tok;
703 tok = talloc(mem_ctx, struct ndr_token_list);
704 NDR_ERR_HAVE_NO_MEMORY(tok);
705 tok->key = key;
706 tok->value = value;
707 DLIST_ADD((*list), tok);
708 return NDR_ERR_SUCCESS;
712 retrieve a token from a ndr context, using cmp_fn to match the tokens
714 _PUBLIC_ enum ndr_err_code ndr_token_retrieve_cmp_fn(struct ndr_token_list **list, const void *key, uint32_t *v,
715 comparison_fn_t _cmp_fn, bool _remove_tok)
717 struct ndr_token_list *tok;
718 for (tok=*list;tok;tok=tok->next) {
719 if (_cmp_fn && _cmp_fn(tok->key,key)==0) goto found;
720 else if (!_cmp_fn && tok->key == key) goto found;
722 return NDR_ERR_TOKEN;
723 found:
724 *v = tok->value;
725 if (_remove_tok) {
726 DLIST_REMOVE((*list), tok);
727 talloc_free(tok);
729 return NDR_ERR_SUCCESS;
733 retrieve a token from a ndr context
735 _PUBLIC_ enum ndr_err_code ndr_token_retrieve(struct ndr_token_list **list, const void *key, uint32_t *v)
737 return ndr_token_retrieve_cmp_fn(list, key, v, NULL, true);
741 peek at but don't removed a token from a ndr context
743 _PUBLIC_ uint32_t ndr_token_peek(struct ndr_token_list **list, const void *key)
745 enum ndr_err_code status;
746 uint32_t v;
748 status = ndr_token_retrieve_cmp_fn(list, key, &v, NULL, false);
749 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
750 return 0;
753 return v;
757 pull an array size field and add it to the array_size_list token list
759 _PUBLIC_ enum ndr_err_code ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
761 uint32_t size;
762 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &size));
763 return ndr_token_store(ndr, &ndr->array_size_list, p, size);
767 get the stored array size field
769 _PUBLIC_ uint32_t ndr_get_array_size(struct ndr_pull *ndr, const void *p)
771 return ndr_token_peek(&ndr->array_size_list, p);
775 check the stored array size field
777 _PUBLIC_ enum ndr_err_code ndr_check_array_size(struct ndr_pull *ndr, void *p, uint32_t size)
779 uint32_t stored;
780 stored = ndr_token_peek(&ndr->array_size_list, p);
781 if (stored != size) {
782 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
783 "Bad array size - got %u expected %u\n",
784 stored, size);
786 return NDR_ERR_SUCCESS;
790 pull an array length field and add it to the array_length_list token list
792 _PUBLIC_ enum ndr_err_code ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
794 uint32_t length, offset;
795 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &offset));
796 if (offset != 0) {
797 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
798 "non-zero array offset %u\n", offset);
800 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &length));
801 return ndr_token_store(ndr, &ndr->array_length_list, p, length);
805 get the stored array length field
807 _PUBLIC_ uint32_t ndr_get_array_length(struct ndr_pull *ndr, const void *p)
809 return ndr_token_peek(&ndr->array_length_list, p);
813 check the stored array length field
815 _PUBLIC_ enum ndr_err_code ndr_check_array_length(struct ndr_pull *ndr, void *p, uint32_t length)
817 uint32_t stored;
818 stored = ndr_token_peek(&ndr->array_length_list, p);
819 if (stored != length) {
820 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
821 "Bad array length - got %u expected %u\n",
822 stored, length);
824 return NDR_ERR_SUCCESS;
828 store a switch value
830 _PUBLIC_ enum ndr_err_code ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
832 return ndr_token_store(ndr, &ndr->switch_list, p, val);
835 _PUBLIC_ enum ndr_err_code ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
837 return ndr_token_store(ndr, &ndr->switch_list, p, val);
840 _PUBLIC_ enum ndr_err_code ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
842 return ndr_token_store(ndr, &ndr->switch_list, p, val);
846 retrieve a switch value
848 _PUBLIC_ uint32_t ndr_push_get_switch_value(struct ndr_push *ndr, const void *p)
850 return ndr_token_peek(&ndr->switch_list, p);
853 _PUBLIC_ uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
855 return ndr_token_peek(&ndr->switch_list, p);
858 _PUBLIC_ uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
860 return ndr_token_peek(&ndr->switch_list, p);
864 pull a struct from a blob using NDR
866 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
867 ndr_pull_flags_fn_t fn)
869 struct ndr_pull *ndr;
870 ndr = ndr_pull_init_blob(blob, mem_ctx);
871 NDR_ERR_HAVE_NO_MEMORY(ndr);
872 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
873 talloc_free(ndr);
874 return NDR_ERR_SUCCESS;
878 pull a struct from a blob using NDR - failing if all bytes are not consumed
880 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
881 void *p, ndr_pull_flags_fn_t fn)
883 struct ndr_pull *ndr;
884 uint32_t highest_ofs;
885 ndr = ndr_pull_init_blob(blob, mem_ctx);
886 NDR_ERR_HAVE_NO_MEMORY(ndr);
887 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
888 if (ndr->offset > ndr->relative_highest_offset) {
889 highest_ofs = ndr->offset;
890 } else {
891 highest_ofs = ndr->relative_highest_offset;
893 if (highest_ofs < ndr->data_size) {
894 enum ndr_err_code ret;
895 ret = ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
896 "not all bytes consumed ofs[%u] size[%u]",
897 highest_ofs, ndr->data_size);
898 talloc_free(ndr);
899 return ret;
901 talloc_free(ndr);
902 return NDR_ERR_SUCCESS;
906 pull a union from a blob using NDR, given the union discriminator
908 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
909 void *p,
910 uint32_t level, ndr_pull_flags_fn_t fn)
912 struct ndr_pull *ndr;
913 ndr = ndr_pull_init_blob(blob, mem_ctx);
914 NDR_ERR_HAVE_NO_MEMORY(ndr);
915 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
916 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
917 talloc_free(ndr);
918 return NDR_ERR_SUCCESS;
922 pull a union from a blob using NDR, given the union discriminator,
923 failing if all bytes are not consumed
925 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
926 void *p,
927 uint32_t level, ndr_pull_flags_fn_t fn)
929 struct ndr_pull *ndr;
930 uint32_t highest_ofs;
931 ndr = ndr_pull_init_blob(blob, mem_ctx);
932 NDR_ERR_HAVE_NO_MEMORY(ndr);
933 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
934 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
935 if (ndr->offset > ndr->relative_highest_offset) {
936 highest_ofs = ndr->offset;
937 } else {
938 highest_ofs = ndr->relative_highest_offset;
940 if (highest_ofs < ndr->data_size) {
941 enum ndr_err_code ret;
942 ret = ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
943 "not all bytes consumed ofs[%u] size[%u]",
944 highest_ofs, ndr->data_size);
945 talloc_free(ndr);
946 return ret;
948 talloc_free(ndr);
949 return NDR_ERR_SUCCESS;
953 push a struct to a blob using NDR
955 _PUBLIC_ enum ndr_err_code ndr_push_struct_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, const void *p, ndr_push_flags_fn_t fn)
957 struct ndr_push *ndr;
958 ndr = ndr_push_init_ctx(mem_ctx);
959 NDR_ERR_HAVE_NO_MEMORY(ndr);
961 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
963 *blob = ndr_push_blob(ndr);
964 talloc_steal(mem_ctx, blob->data);
965 talloc_free(ndr);
967 return NDR_ERR_SUCCESS;
971 push a union to a blob using NDR
973 _PUBLIC_ enum ndr_err_code ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
974 uint32_t level, ndr_push_flags_fn_t fn)
976 struct ndr_push *ndr;
977 ndr = ndr_push_init_ctx(mem_ctx);
978 NDR_ERR_HAVE_NO_MEMORY(ndr);
980 NDR_CHECK(ndr_push_set_switch_value(ndr, p, level));
981 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
983 *blob = ndr_push_blob(ndr);
984 talloc_steal(mem_ctx, blob->data);
985 talloc_free(ndr);
987 return NDR_ERR_SUCCESS;
991 generic ndr_size_*() handler for structures
993 _PUBLIC_ size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push)
995 struct ndr_push *ndr;
996 enum ndr_err_code status;
997 size_t ret;
999 /* avoid recursion */
1000 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
1002 ndr = ndr_push_init_ctx(NULL);
1003 if (!ndr) return 0;
1004 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
1005 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
1006 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1007 talloc_free(ndr);
1008 return 0;
1010 ret = ndr->offset;
1011 talloc_free(ndr);
1012 return ret;
1016 generic ndr_size_*() handler for unions
1018 _PUBLIC_ size_t ndr_size_union(const void *p, int flags, uint32_t level, ndr_push_flags_fn_t push)
1020 struct ndr_push *ndr;
1021 enum ndr_err_code status;
1022 size_t ret;
1024 /* avoid recursion */
1025 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
1027 ndr = ndr_push_init_ctx(NULL);
1028 if (!ndr) return 0;
1029 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
1031 status = ndr_push_set_switch_value(ndr, p, level);
1032 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1033 talloc_free(ndr);
1034 return 0;
1036 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
1037 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1038 talloc_free(ndr);
1039 return 0;
1041 ret = ndr->offset;
1042 talloc_free(ndr);
1043 return ret;
1047 get the current base for relative pointers for the push
1049 _PUBLIC_ uint32_t ndr_push_get_relative_base_offset(struct ndr_push *ndr)
1051 return ndr->relative_base_offset;
1055 restore the old base for relative pointers for the push
1057 _PUBLIC_ void ndr_push_restore_relative_base_offset(struct ndr_push *ndr, uint32_t offset)
1059 ndr->relative_base_offset = offset;
1063 setup the current base for relative pointers for the push
1064 called in the NDR_SCALAR stage
1066 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset1(struct ndr_push *ndr, const void *p, uint32_t offset)
1068 ndr->relative_base_offset = offset;
1069 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1073 setup the current base for relative pointers for the push
1074 called in the NDR_BUFFERS stage
1076 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset2(struct ndr_push *ndr, const void *p)
1078 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1082 push a relative object - stage1
1083 this is called during SCALARS processing
1085 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
1087 if (p == NULL) {
1088 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
1089 return NDR_ERR_SUCCESS;
1091 NDR_CHECK(ndr_push_align(ndr, 4));
1092 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1093 return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
1097 push a short relative object - stage1
1098 this is called during SCALARS processing
1100 _PUBLIC_ enum ndr_err_code ndr_push_short_relative_ptr1(struct ndr_push *ndr, const void *p)
1102 if (p == NULL) {
1103 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 0));
1104 return NDR_ERR_SUCCESS;
1106 NDR_CHECK(ndr_push_align(ndr, 2));
1107 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1108 return ndr_push_uint16(ndr, NDR_SCALARS, 0xFFFF);
1111 push a relative object - stage2
1112 this is called during buffers processing
1114 static enum ndr_err_code ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
1116 uint32_t save_offset;
1117 uint32_t ptr_offset = 0xFFFFFFFF;
1118 if (p == NULL) {
1119 return NDR_ERR_SUCCESS;
1121 save_offset = ndr->offset;
1122 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1123 if (ptr_offset > ndr->offset) {
1124 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1125 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1126 ptr_offset, ndr->offset);
1128 ndr->offset = ptr_offset;
1129 if (save_offset < ndr->relative_base_offset) {
1130 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1131 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1132 save_offset, ndr->relative_base_offset);
1134 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
1135 ndr->offset = save_offset;
1136 return NDR_ERR_SUCCESS;
1139 push a short relative object - stage2
1140 this is called during buffers processing
1142 _PUBLIC_ enum ndr_err_code ndr_push_short_relative_ptr2(struct ndr_push *ndr, const void *p)
1144 uint32_t save_offset;
1145 uint32_t ptr_offset = 0xFFFF;
1146 if (p == NULL) {
1147 return NDR_ERR_SUCCESS;
1149 save_offset = ndr->offset;
1150 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1151 if (ptr_offset > ndr->offset) {
1152 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1153 "ndr_push_short_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1154 ptr_offset, ndr->offset);
1156 ndr->offset = ptr_offset;
1157 if (save_offset < ndr->relative_base_offset) {
1158 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1159 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1160 save_offset, ndr->relative_base_offset);
1162 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
1163 ndr->offset = save_offset;
1164 return NDR_ERR_SUCCESS;
1168 push a relative object - stage2 start
1169 this is called during buffers processing
1171 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_start(struct ndr_push *ndr, const void *p)
1173 if (p == NULL) {
1174 return NDR_ERR_SUCCESS;
1176 if (!(ndr->flags & LIBNDR_FLAG_RELATIVE_REVERSE)) {
1177 return ndr_push_relative_ptr2(ndr, p);
1179 if (ndr->relative_end_offset == -1) {
1180 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1181 "ndr_push_relative_ptr2_start RELATIVE_REVERSE flag set and relative_end_offset %d",
1182 ndr->relative_end_offset);
1184 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_begin_list, p, ndr->offset));
1185 return NDR_ERR_SUCCESS;
1189 push a relative object - stage2 end
1190 this is called during buffers processing
1192 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_end(struct ndr_push *ndr, const void *p)
1194 uint32_t begin_offset = 0xFFFFFFFF;
1195 ssize_t len;
1196 uint32_t correct_offset = 0;
1197 uint32_t align = 1;
1198 uint32_t pad = 0;
1200 if (p == NULL) {
1201 return NDR_ERR_SUCCESS;
1204 if (!(ndr->flags & LIBNDR_FLAG_RELATIVE_REVERSE)) {
1205 return NDR_ERR_SUCCESS;
1208 if (ndr->flags & LIBNDR_FLAG_NO_NDR_SIZE) {
1209 /* better say more than calculation a too small buffer */
1210 NDR_PUSH_ALIGN(ndr, 8);
1211 return NDR_ERR_SUCCESS;
1214 if (ndr->relative_end_offset < ndr->offset) {
1215 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1216 "ndr_push_relative_ptr2_end:"
1217 "relative_end_offset %u < offset %u",
1218 ndr->relative_end_offset, ndr->offset);
1221 NDR_CHECK(ndr_token_retrieve(&ndr->relative_begin_list, p, &begin_offset));
1223 /* we have marshalled a buffer, see how long it was */
1224 len = ndr->offset - begin_offset;
1226 if (len < 0) {
1227 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1228 "ndr_push_relative_ptr2_end:"
1229 "offset %u - begin_offset %u < 0",
1230 ndr->offset, begin_offset);
1233 if (ndr->relative_end_offset < len) {
1234 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1235 "ndr_push_relative_ptr2_end:"
1236 "relative_end_offset %u < len %lld",
1237 ndr->offset, (long long)len);
1240 /* the reversed offset is at the end of the main buffer */
1241 correct_offset = ndr->relative_end_offset - len;
1243 /* TODO: remove this hack and let the idl use FLAG_ALIGN2 explicit */
1244 align = 2;
1246 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1247 align = 2;
1248 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1249 align = 4;
1250 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1251 align = 8;
1254 pad = ndr_align_size(correct_offset, align);
1255 if (pad) {
1256 correct_offset += pad;
1257 correct_offset -= align;
1260 if (correct_offset < begin_offset) {
1261 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1262 "ndr_push_relative_ptr2_end: "
1263 "correct_offset %u < begin_offset %u",
1264 correct_offset, begin_offset);
1267 if (len > 0) {
1268 uint32_t clear_size = correct_offset - begin_offset;
1270 clear_size = MIN(clear_size, len);
1272 /* now move the marshalled buffer to the end of the main buffer */
1273 memmove(ndr->data + correct_offset, ndr->data + begin_offset, len);
1275 if (clear_size) {
1276 /* and wipe out old buffer within the main buffer */
1277 memset(ndr->data + begin_offset, '\0', clear_size);
1281 /* and set the end offset for the next buffer */
1282 ndr->relative_end_offset = correct_offset;
1284 /* finally write the offset to the main buffer */
1285 ndr->offset = correct_offset;
1286 NDR_CHECK(ndr_push_relative_ptr2(ndr, p));
1288 /* restore to where we were in the main buffer */
1289 ndr->offset = begin_offset;
1291 return NDR_ERR_SUCCESS;
1295 get the current base for relative pointers for the pull
1297 _PUBLIC_ uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull *ndr)
1299 return ndr->relative_base_offset;
1303 restore the old base for relative pointers for the pull
1305 _PUBLIC_ void ndr_pull_restore_relative_base_offset(struct ndr_pull *ndr, uint32_t offset)
1307 ndr->relative_base_offset = offset;
1311 setup the current base for relative pointers for the pull
1312 called in the NDR_SCALAR stage
1314 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset1(struct ndr_pull *ndr, const void *p, uint32_t offset)
1316 ndr->relative_base_offset = offset;
1317 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1321 setup the current base for relative pointers for the pull
1322 called in the NDR_BUFFERS stage
1324 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset2(struct ndr_pull *ndr, const void *p)
1326 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1330 pull a relative object - stage1
1331 called during SCALARS processing
1333 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
1335 rel_offset += ndr->relative_base_offset;
1336 if (rel_offset > ndr->data_size) {
1337 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
1338 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1339 rel_offset, ndr->data_size);
1341 return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
1345 pull a relative object - stage2
1346 called during BUFFERS processing
1348 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
1350 uint32_t rel_offset;
1351 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
1352 return ndr_pull_set_offset(ndr, rel_offset);
1355 const static struct {
1356 enum ndr_err_code err;
1357 const char *string;
1358 } ndr_err_code_strings[] = {
1359 { NDR_ERR_SUCCESS, "Success" },
1360 { NDR_ERR_ARRAY_SIZE, "Bad Array Size" },
1361 { NDR_ERR_BAD_SWITCH, "Bad Switch" },
1362 { NDR_ERR_OFFSET, "Offset Error" },
1363 { NDR_ERR_RELATIVE, "Relative Pointer Error" },
1364 { NDR_ERR_CHARCNV, "Character Conversion Error" },
1365 { NDR_ERR_LENGTH, "Length Error" },
1366 { NDR_ERR_SUBCONTEXT, "Subcontext Error" },
1367 { NDR_ERR_COMPRESSION, "Compression Error" },
1368 { NDR_ERR_STRING, "String Error" },
1369 { NDR_ERR_VALIDATE, "Validate Error" },
1370 { NDR_ERR_BUFSIZE, "Buffer Size Error" },
1371 { NDR_ERR_ALLOC, "Allocation Error" },
1372 { NDR_ERR_RANGE, "Range Error" },
1373 { NDR_ERR_TOKEN, "Token Error" },
1374 { NDR_ERR_IPV4ADDRESS, "IPv4 Address Error" },
1375 { NDR_ERR_INVALID_POINTER, "Invalid Pointer" },
1376 { NDR_ERR_UNREAD_BYTES, "Unread Bytes" },
1377 { NDR_ERR_NDR64, "NDR64 assertion error" },
1378 { 0, NULL }
1381 _PUBLIC_ const char *ndr_map_error2string(enum ndr_err_code ndr_err)
1383 int i;
1384 for (i = 0; ndr_err_code_strings[i].string != NULL; i++) {
1385 if (ndr_err_code_strings[i].err == ndr_err)
1386 return ndr_err_code_strings[i].string;
1388 return "Unknown error";