libndr: give an error when ndr_push_relative_ptr2_start()/_end() is used with the...
[Samba/gbeck.git] / librpc / ndr / ndr.c
blob15510d62162678bae46b372613e1a0a766b965c1
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;
258 fn(ndr, name, flags, ptr);
259 talloc_free(ndr);
263 a useful helper function for printing idl structures to a string
265 _PUBLIC_ char *ndr_print_struct_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, void *ptr)
267 struct ndr_print *ndr;
268 char *ret = NULL;
270 ndr = talloc_zero(mem_ctx, struct ndr_print);
271 if (!ndr) return NULL;
272 ndr->private_data = talloc_strdup(ndr, "");
273 if (!ndr->private_data) {
274 goto failed;
276 ndr->print = ndr_print_string_helper;
277 ndr->depth = 1;
278 ndr->flags = 0;
279 fn(ndr, name, ptr);
280 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
281 failed:
282 talloc_free(ndr);
283 return ret;
287 a useful helper function for printing idl unions to a string
289 _PUBLIC_ char *ndr_print_union_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
291 struct ndr_print *ndr;
292 char *ret = NULL;
294 ndr = talloc_zero(mem_ctx, struct ndr_print);
295 if (!ndr) return NULL;
296 ndr->private_data = talloc_strdup(ndr, "");
297 if (!ndr->private_data) {
298 goto failed;
300 ndr->print = ndr_print_string_helper;
301 ndr->depth = 1;
302 ndr->flags = 0;
303 ndr_print_set_switch_value(ndr, ptr, level);
304 fn(ndr, name, ptr);
305 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
306 failed:
307 talloc_free(ndr);
308 return ret;
312 a useful helper function for printing idl function calls to a string
314 _PUBLIC_ char *ndr_print_function_string(TALLOC_CTX *mem_ctx,
315 ndr_print_function_t fn, const char *name,
316 int flags, void *ptr)
318 struct ndr_print *ndr;
319 char *ret = NULL;
321 ndr = talloc_zero(mem_ctx, struct ndr_print);
322 if (!ndr) return NULL;
323 ndr->private_data = talloc_strdup(ndr, "");
324 if (!ndr->private_data) {
325 goto failed;
327 ndr->print = ndr_print_string_helper;
328 ndr->depth = 1;
329 ndr->flags = 0;
330 fn(ndr, name, flags, ptr);
331 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
332 failed:
333 talloc_free(ndr);
334 return ret;
337 _PUBLIC_ void ndr_set_flags(uint32_t *pflags, uint32_t new_flags)
339 /* the big/little endian flags are inter-dependent */
340 if (new_flags & LIBNDR_FLAG_LITTLE_ENDIAN) {
341 (*pflags) &= ~LIBNDR_FLAG_BIGENDIAN;
343 if (new_flags & LIBNDR_FLAG_BIGENDIAN) {
344 (*pflags) &= ~LIBNDR_FLAG_LITTLE_ENDIAN;
346 if (new_flags & LIBNDR_FLAG_REMAINING) {
347 (*pflags) &= ~LIBNDR_ALIGN_FLAGS;
349 if (new_flags & LIBNDR_ALIGN_FLAGS) {
350 (*pflags) &= ~LIBNDR_FLAG_REMAINING;
352 if (new_flags & LIBNDR_FLAG_NO_RELATIVE_REVERSE) {
353 (*pflags) &= ~LIBNDR_FLAG_RELATIVE_REVERSE;
355 (*pflags) |= new_flags;
359 return and possibly log an NDR error
361 _PUBLIC_ enum ndr_err_code ndr_pull_error(struct ndr_pull *ndr,
362 enum ndr_err_code ndr_err,
363 const char *format, ...)
365 char *s=NULL;
366 va_list ap;
367 int ret;
369 va_start(ap, format);
370 ret = vasprintf(&s, format, ap);
371 va_end(ap);
373 if (ret == -1) {
374 return NDR_ERR_ALLOC;
377 DEBUG(1,("ndr_pull_error(%u): %s\n", ndr_err, s));
379 free(s);
381 return ndr_err;
385 return and possibly log an NDR error
387 _PUBLIC_ enum ndr_err_code ndr_push_error(struct ndr_push *ndr,
388 enum ndr_err_code ndr_err,
389 const char *format, ...)
391 char *s=NULL;
392 va_list ap;
393 int ret;
395 va_start(ap, format);
396 ret = vasprintf(&s, format, ap);
397 va_end(ap);
399 if (ret == -1) {
400 return NDR_ERR_ALLOC;
403 DEBUG(1,("ndr_push_error(%u): %s\n", ndr_err, s));
405 free(s);
407 return ndr_err;
411 handle subcontext buffers, which in midl land are user-marshalled, but
412 we use magic in pidl to make them easier to cope with
414 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_start(struct ndr_pull *ndr,
415 struct ndr_pull **_subndr,
416 size_t header_size,
417 ssize_t size_is)
419 struct ndr_pull *subndr;
420 uint32_t r_content_size;
421 bool force_le = false;
422 bool force_be = false;
424 switch (header_size) {
425 case 0: {
426 uint32_t content_size = ndr->data_size - ndr->offset;
427 if (size_is >= 0) {
428 content_size = size_is;
430 r_content_size = content_size;
431 break;
434 case 2: {
435 uint16_t content_size;
436 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &content_size));
437 if (size_is >= 0 && size_is != content_size) {
438 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
439 (int)size_is, (int)content_size);
441 r_content_size = content_size;
442 break;
445 case 4: {
446 uint32_t content_size;
447 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
448 if (size_is >= 0 && size_is != content_size) {
449 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
450 (int)size_is, (int)content_size);
452 r_content_size = content_size;
453 break;
455 case 0xFFFFFC01: {
457 * Common Type Header for the Serialization Stream
458 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
460 uint8_t version;
461 uint8_t drep;
462 uint16_t hdrlen;
463 uint32_t filler;
464 uint32_t content_size;
465 uint32_t reserved;
467 /* version */
468 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &version));
470 if (version != 1) {
471 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
472 "Bad subcontext (PULL) Common Type Header version %d != 1",
473 (int)version);
477 * 0x10 little endian
478 * 0x00 big endian
480 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &drep));
481 if (drep == 0x10) {
482 force_le = true;
483 } else if (drep == 0x00) {
484 force_be = true;
485 } else {
486 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
487 "Bad subcontext (PULL) Common Type Header invalid drep 0x%02X",
488 (unsigned int)drep);
491 /* length of the "Private Header for Constructed Type" */
492 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &hdrlen));
493 if (hdrlen != 8) {
494 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
495 "Bad subcontext (PULL) Common Type Header length %d != 8",
496 (int)hdrlen);
499 /* filler should be ignored */
500 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &filler));
503 * Private Header for Constructed Type
505 /* length - will be updated latter */
506 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
507 if (size_is >= 0 && size_is != content_size) {
508 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
509 (int)size_is, (int)content_size);
511 /* the content size must be a multiple of 8 */
512 if ((content_size % 8) != 0) {
513 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
514 "Bad subcontext (PULL) size_is(%d) not padded to 8 content_size %d",
515 (int)size_is, (int)content_size);
517 r_content_size = content_size;
519 /* reserved */
520 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &reserved));
521 break;
523 default:
524 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) header_size %d",
525 (int)header_size);
528 NDR_PULL_NEED_BYTES(ndr, r_content_size);
530 subndr = talloc_zero(ndr, struct ndr_pull);
531 NDR_ERR_HAVE_NO_MEMORY(subndr);
532 subndr->flags = ndr->flags;
533 subndr->current_mem_ctx = ndr->current_mem_ctx;
535 subndr->data = ndr->data + ndr->offset;
536 subndr->offset = 0;
537 subndr->data_size = r_content_size;
538 subndr->iconv_convenience = talloc_reference(subndr, ndr->iconv_convenience);
540 if (force_le) {
541 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_LITTLE_ENDIAN);
542 } else if (force_be) {
543 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_BIGENDIAN);
546 *_subndr = subndr;
547 return NDR_ERR_SUCCESS;
550 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_end(struct ndr_pull *ndr,
551 struct ndr_pull *subndr,
552 size_t header_size,
553 ssize_t size_is)
555 uint32_t advance;
556 if (size_is >= 0) {
557 advance = size_is;
558 } else if (header_size > 0) {
559 advance = subndr->data_size;
560 } else {
561 advance = subndr->offset;
563 NDR_CHECK(ndr_pull_advance(ndr, advance));
564 return NDR_ERR_SUCCESS;
567 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_start(struct ndr_push *ndr,
568 struct ndr_push **_subndr,
569 size_t header_size,
570 ssize_t size_is)
572 struct ndr_push *subndr;
574 subndr = ndr_push_init_ctx(ndr, ndr->iconv_convenience);
575 NDR_ERR_HAVE_NO_MEMORY(subndr);
576 subndr->flags = ndr->flags;
578 if (size_is > 0) {
579 NDR_CHECK(ndr_push_zero(subndr, size_is));
580 subndr->offset = 0;
583 *_subndr = subndr;
584 return NDR_ERR_SUCCESS;
588 push a subcontext header
590 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_end(struct ndr_push *ndr,
591 struct ndr_push *subndr,
592 size_t header_size,
593 ssize_t size_is)
595 ssize_t padding_len;
597 if (size_is >= 0) {
598 padding_len = size_is - subndr->offset;
599 if (padding_len < 0) {
600 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
601 (int)subndr->offset, (int)size_is);
603 subndr->offset = size_is;
606 switch (header_size) {
607 case 0:
608 break;
610 case 2:
611 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, subndr->offset));
612 break;
614 case 4:
615 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
616 break;
618 case 0xFFFFFC01:
620 * Common Type Header for the Serialization Stream
621 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
623 padding_len = NDR_ROUND(subndr->offset, 8) - subndr->offset;
624 if (padding_len > 0) {
625 NDR_CHECK(ndr_push_zero(subndr, padding_len));
628 /* version */
629 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, 1));
632 * 0x10 little endian
633 * 0x00 big endian
635 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, NDR_BE(ndr)?0x00:0x10));
637 /* length of the "Private Header for Constructed Type" */
638 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 8));
640 /* filler */
641 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0xCCCCCCCC));
644 * Private Header for Constructed Type
646 /* length - will be updated latter */
647 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
649 /* reserved */
650 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
651 break;
653 default:
654 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext header size %d",
655 (int)header_size);
658 NDR_CHECK(ndr_push_bytes(ndr, subndr->data, subndr->offset));
659 return NDR_ERR_SUCCESS;
663 store a token in the ndr context, for later retrieval
665 _PUBLIC_ enum ndr_err_code ndr_token_store(TALLOC_CTX *mem_ctx,
666 struct ndr_token_list **list,
667 const void *key,
668 uint32_t value)
670 struct ndr_token_list *tok;
671 tok = talloc(mem_ctx, struct ndr_token_list);
672 NDR_ERR_HAVE_NO_MEMORY(tok);
673 tok->key = key;
674 tok->value = value;
675 DLIST_ADD((*list), tok);
676 return NDR_ERR_SUCCESS;
680 retrieve a token from a ndr context, using cmp_fn to match the tokens
682 _PUBLIC_ enum ndr_err_code ndr_token_retrieve_cmp_fn(struct ndr_token_list **list, const void *key, uint32_t *v,
683 comparison_fn_t _cmp_fn, bool _remove_tok)
685 struct ndr_token_list *tok;
686 for (tok=*list;tok;tok=tok->next) {
687 if (_cmp_fn && _cmp_fn(tok->key,key)==0) goto found;
688 else if (!_cmp_fn && tok->key == key) goto found;
690 return NDR_ERR_TOKEN;
691 found:
692 *v = tok->value;
693 if (_remove_tok) {
694 DLIST_REMOVE((*list), tok);
695 talloc_free(tok);
697 return NDR_ERR_SUCCESS;
701 retrieve a token from a ndr context
703 _PUBLIC_ enum ndr_err_code ndr_token_retrieve(struct ndr_token_list **list, const void *key, uint32_t *v)
705 return ndr_token_retrieve_cmp_fn(list, key, v, NULL, true);
709 peek at but don't removed a token from a ndr context
711 _PUBLIC_ uint32_t ndr_token_peek(struct ndr_token_list **list, const void *key)
713 enum ndr_err_code status;
714 uint32_t v;
716 status = ndr_token_retrieve_cmp_fn(list, key, &v, NULL, false);
717 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
718 return 0;
721 return v;
725 pull an array size field and add it to the array_size_list token list
727 _PUBLIC_ enum ndr_err_code ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
729 uint32_t size;
730 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &size));
731 return ndr_token_store(ndr, &ndr->array_size_list, p, size);
735 get the stored array size field
737 _PUBLIC_ uint32_t ndr_get_array_size(struct ndr_pull *ndr, const void *p)
739 return ndr_token_peek(&ndr->array_size_list, p);
743 check the stored array size field
745 _PUBLIC_ enum ndr_err_code ndr_check_array_size(struct ndr_pull *ndr, void *p, uint32_t size)
747 uint32_t stored;
748 stored = ndr_token_peek(&ndr->array_size_list, p);
749 if (stored != size) {
750 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
751 "Bad array size - got %u expected %u\n",
752 stored, size);
754 return NDR_ERR_SUCCESS;
758 pull an array length field and add it to the array_length_list token list
760 _PUBLIC_ enum ndr_err_code ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
762 uint32_t length, offset;
763 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &offset));
764 if (offset != 0) {
765 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
766 "non-zero array offset %u\n", offset);
768 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &length));
769 return ndr_token_store(ndr, &ndr->array_length_list, p, length);
773 get the stored array length field
775 _PUBLIC_ uint32_t ndr_get_array_length(struct ndr_pull *ndr, const void *p)
777 return ndr_token_peek(&ndr->array_length_list, p);
781 check the stored array length field
783 _PUBLIC_ enum ndr_err_code ndr_check_array_length(struct ndr_pull *ndr, void *p, uint32_t length)
785 uint32_t stored;
786 stored = ndr_token_peek(&ndr->array_length_list, p);
787 if (stored != length) {
788 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
789 "Bad array length - got %u expected %u\n",
790 stored, length);
792 return NDR_ERR_SUCCESS;
796 store a switch value
798 _PUBLIC_ enum ndr_err_code ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
800 return ndr_token_store(ndr, &ndr->switch_list, p, val);
803 _PUBLIC_ enum ndr_err_code ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
805 return ndr_token_store(ndr, &ndr->switch_list, p, val);
808 _PUBLIC_ enum ndr_err_code ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
810 return ndr_token_store(ndr, &ndr->switch_list, p, val);
814 retrieve a switch value
816 _PUBLIC_ uint32_t ndr_push_get_switch_value(struct ndr_push *ndr, const void *p)
818 return ndr_token_peek(&ndr->switch_list, p);
821 _PUBLIC_ uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
823 return ndr_token_peek(&ndr->switch_list, p);
826 _PUBLIC_ uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
828 return ndr_token_peek(&ndr->switch_list, p);
832 pull a struct from a blob using NDR
834 _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,
835 ndr_pull_flags_fn_t fn)
837 struct ndr_pull *ndr;
838 ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
839 NDR_ERR_HAVE_NO_MEMORY(ndr);
840 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
841 return NDR_ERR_SUCCESS;
845 pull a struct from a blob using NDR - failing if all bytes are not consumed
847 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
848 struct smb_iconv_convenience *iconv_convenience,
849 void *p, ndr_pull_flags_fn_t fn)
851 struct ndr_pull *ndr;
852 ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
853 NDR_ERR_HAVE_NO_MEMORY(ndr);
854 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
855 if (ndr->offset < ndr->data_size) {
856 return ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
857 "not all bytes consumed ofs[%u] size[%u]",
858 ndr->offset, ndr->data_size);
860 return NDR_ERR_SUCCESS;
864 pull a union from a blob using NDR, given the union discriminator
866 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
867 struct smb_iconv_convenience *iconv_convenience, void *p,
868 uint32_t level, ndr_pull_flags_fn_t fn)
870 struct ndr_pull *ndr;
871 ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
872 NDR_ERR_HAVE_NO_MEMORY(ndr);
873 NDR_CHECK(ndr_pull_set_switch_value(ndr, p, level));
874 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
875 return NDR_ERR_SUCCESS;
879 pull a union from a blob using NDR, given the union discriminator,
880 failing if all bytes are not consumed
882 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
883 struct smb_iconv_convenience *iconv_convenience, void *p,
884 uint32_t level, ndr_pull_flags_fn_t fn)
886 struct ndr_pull *ndr;
887 ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
888 NDR_ERR_HAVE_NO_MEMORY(ndr);
889 NDR_CHECK(ndr_pull_set_switch_value(ndr, p, level));
890 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
891 if (ndr->offset < ndr->data_size) {
892 return ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
893 "not all bytes consumed ofs[%u] size[%u]",
894 ndr->offset, ndr->data_size);
896 return NDR_ERR_SUCCESS;
900 push a struct to a blob using NDR
902 _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)
904 struct ndr_push *ndr;
905 ndr = ndr_push_init_ctx(mem_ctx, iconv_convenience);
906 NDR_ERR_HAVE_NO_MEMORY(ndr);
908 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
910 *blob = ndr_push_blob(ndr);
911 talloc_steal(mem_ctx, blob->data);
912 talloc_free(ndr);
914 return NDR_ERR_SUCCESS;
918 push a union to a blob using NDR
920 _PUBLIC_ enum ndr_err_code ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience, void *p,
921 uint32_t level, ndr_push_flags_fn_t fn)
923 struct ndr_push *ndr;
924 ndr = ndr_push_init_ctx(mem_ctx, iconv_convenience);
925 NDR_ERR_HAVE_NO_MEMORY(ndr);
927 NDR_CHECK(ndr_push_set_switch_value(ndr, p, level));
928 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
930 *blob = ndr_push_blob(ndr);
931 talloc_steal(mem_ctx, blob->data);
932 talloc_free(ndr);
934 return NDR_ERR_SUCCESS;
938 generic ndr_size_*() handler for structures
940 _PUBLIC_ size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push, struct smb_iconv_convenience *iconv_convenience)
942 struct ndr_push *ndr;
943 enum ndr_err_code status;
944 size_t ret;
946 /* avoid recursion */
947 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
949 ndr = ndr_push_init_ctx(NULL, iconv_convenience);
950 if (!ndr) return 0;
951 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
952 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
953 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
954 talloc_free(ndr);
955 return 0;
957 ret = ndr->offset;
958 talloc_free(ndr);
959 return ret;
963 generic ndr_size_*() handler for unions
965 _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)
967 struct ndr_push *ndr;
968 enum ndr_err_code status;
969 size_t ret;
971 /* avoid recursion */
972 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
974 ndr = ndr_push_init_ctx(NULL, iconv_convenience);
975 if (!ndr) return 0;
976 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
978 status = ndr_push_set_switch_value(ndr, p, level);
979 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
980 talloc_free(ndr);
981 return 0;
983 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
984 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
985 talloc_free(ndr);
986 return 0;
988 ret = ndr->offset;
989 talloc_free(ndr);
990 return ret;
994 get the current base for relative pointers for the push
996 _PUBLIC_ uint32_t ndr_push_get_relative_base_offset(struct ndr_push *ndr)
998 return ndr->relative_base_offset;
1002 restore the old base for relative pointers for the push
1004 _PUBLIC_ void ndr_push_restore_relative_base_offset(struct ndr_push *ndr, uint32_t offset)
1006 ndr->relative_base_offset = offset;
1010 setup the current base for relative pointers for the push
1011 called in the NDR_SCALAR stage
1013 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset1(struct ndr_push *ndr, const void *p, uint32_t offset)
1015 ndr->relative_base_offset = offset;
1016 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1020 setup the current base for relative pointers for the push
1021 called in the NDR_BUFFERS stage
1023 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset2(struct ndr_push *ndr, const void *p)
1025 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1029 push a relative object - stage1
1030 this is called during SCALARS processing
1032 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
1034 if (p == NULL) {
1035 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
1036 return NDR_ERR_SUCCESS;
1038 NDR_CHECK(ndr_push_align(ndr, 4));
1039 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1040 return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
1044 push a relative object - stage2
1045 this is called during buffers processing
1047 static enum ndr_err_code ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
1049 uint32_t save_offset;
1050 uint32_t ptr_offset = 0xFFFFFFFF;
1051 if (p == NULL) {
1052 return NDR_ERR_SUCCESS;
1054 save_offset = ndr->offset;
1055 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1056 if (ptr_offset > ndr->offset) {
1057 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1058 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1059 ptr_offset, ndr->offset);
1061 ndr->offset = ptr_offset;
1062 if (save_offset < ndr->relative_base_offset) {
1063 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1064 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1065 save_offset, ndr->relative_base_offset);
1067 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
1068 ndr->offset = save_offset;
1069 return NDR_ERR_SUCCESS;
1073 push a relative object - stage2 start
1074 this is called during buffers processing
1076 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_start(struct ndr_push *ndr, const void *p)
1078 if (p == NULL) {
1079 return NDR_ERR_SUCCESS;
1081 if (!(ndr->flags & LIBNDR_FLAG_RELATIVE_REVERSE)) {
1082 return ndr_push_relative_ptr2(ndr, p);
1085 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1086 "ndr_push_relative_ptr2_start RELATIVE_REVERSE flag set, but not supported");
1090 push a relative object - stage2 end
1091 this is called during buffers processing
1093 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_end(struct ndr_push *ndr, const void *p)
1095 if (p == NULL) {
1096 return NDR_ERR_SUCCESS;
1098 return NDR_ERR_SUCCESS;
1102 get the current base for relative pointers for the pull
1104 _PUBLIC_ uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull *ndr)
1106 return ndr->relative_base_offset;
1110 restore the old base for relative pointers for the pull
1112 _PUBLIC_ void ndr_pull_restore_relative_base_offset(struct ndr_pull *ndr, uint32_t offset)
1114 ndr->relative_base_offset = offset;
1118 setup the current base for relative pointers for the pull
1119 called in the NDR_SCALAR stage
1121 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset1(struct ndr_pull *ndr, const void *p, uint32_t offset)
1123 ndr->relative_base_offset = offset;
1124 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1128 setup the current base for relative pointers for the pull
1129 called in the NDR_BUFFERS stage
1131 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset2(struct ndr_pull *ndr, const void *p)
1133 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1137 pull a relative object - stage1
1138 called during SCALARS processing
1140 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
1142 rel_offset += ndr->relative_base_offset;
1143 if (rel_offset > ndr->data_size) {
1144 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
1145 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1146 rel_offset, ndr->data_size);
1148 return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
1152 pull a relative object - stage2
1153 called during BUFFERS processing
1155 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
1157 uint32_t rel_offset;
1158 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
1159 return ndr_pull_set_offset(ndr, rel_offset);
1162 const static struct {
1163 enum ndr_err_code err;
1164 const char *string;
1165 } ndr_err_code_strings[] = {
1166 { NDR_ERR_SUCCESS, "Success" },
1167 { NDR_ERR_ARRAY_SIZE, "Bad Array Size" },
1168 { NDR_ERR_BAD_SWITCH, "Bad Switch" },
1169 { NDR_ERR_OFFSET, "Offset Error" },
1170 { NDR_ERR_RELATIVE, "Relative Pointer Error" },
1171 { NDR_ERR_CHARCNV, "Character Conversion Error" },
1172 { NDR_ERR_LENGTH, "Length Error" },
1173 { NDR_ERR_SUBCONTEXT, "Subcontext Error" },
1174 { NDR_ERR_COMPRESSION, "Compression Error" },
1175 { NDR_ERR_STRING, "String Error" },
1176 { NDR_ERR_VALIDATE, "Validate Error" },
1177 { NDR_ERR_BUFSIZE, "Buffer Size Error" },
1178 { NDR_ERR_ALLOC, "Allocation Error" },
1179 { NDR_ERR_RANGE, "Range Error" },
1180 { NDR_ERR_TOKEN, "Token Error" },
1181 { NDR_ERR_IPV4ADDRESS, "IPv4 Address Error" },
1182 { NDR_ERR_INVALID_POINTER, "Invalid Pointer" },
1183 { NDR_ERR_UNREAD_BYTES, "Unread Bytes" },
1184 { 0, NULL }
1187 _PUBLIC_ const char *ndr_map_error2string(enum ndr_err_code ndr_err)
1189 int i;
1190 for (i = 0; ndr_err_code_strings[i].string != NULL; i++) {
1191 if (ndr_err_code_strings[i].err == ndr_err)
1192 return ndr_err_code_strings[i].string;
1194 return "Unknown error";