s3-utils: Add cast to fix error found by -Werror=format
[Samba.git] / librpc / ndr / ndr.c
blobb77bfae36b76a84fbbbc31c316d7ad201f8b0609
1 /*
2 Unix SMB/CIFS implementation.
4 libndr interface
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Jelmer Vernooij 2005-2008
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 this provides the core routines for NDR parsing functions
26 see http://www.opengroup.org/onlinepubs/9629399/chap14.htm for details
27 of NDR encoding rules
30 #include "includes.h"
31 #include "librpc/ndr/libndr.h"
32 #include "../lib/util/dlinklist.h"
34 #define NDR_BASE_MARSHALL_SIZE 1024
36 /* this guid indicates NDR encoding in a protocol tower */
37 const struct ndr_syntax_id ndr_transfer_syntax_ndr = {
38 { 0x8a885d04, 0x1ceb, 0x11c9, {0x9f, 0xe8}, {0x08,0x00,0x2b,0x10,0x48,0x60} },
42 const struct ndr_syntax_id ndr_transfer_syntax_ndr64 = {
43 { 0x71710533, 0xbeba, 0x4937, {0x83, 0x19}, {0xb5,0xdb,0xef,0x9c,0xcc,0x36} },
47 const struct ndr_syntax_id ndr_syntax_id_null = {
48 { 0, 0, 0, { 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
53 work out the number of bytes needed to align on a n byte boundary
55 _PUBLIC_ size_t ndr_align_size(uint32_t offset, size_t n)
57 if ((offset & (n-1)) == 0) return 0;
58 return n - (offset & (n-1));
62 initialise a ndr parse structure from a data blob
64 _PUBLIC_ struct ndr_pull *ndr_pull_init_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx)
66 struct ndr_pull *ndr;
68 ndr = talloc_zero(mem_ctx, struct ndr_pull);
69 if (!ndr) return NULL;
70 ndr->current_mem_ctx = mem_ctx;
72 ndr->data = blob->data;
73 ndr->data_size = blob->length;
75 return ndr;
79 advance by 'size' bytes
81 _PUBLIC_ enum ndr_err_code ndr_pull_advance(struct ndr_pull *ndr, uint32_t size)
83 ndr->offset += size;
84 if (ndr->offset > ndr->data_size) {
85 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
86 "ndr_pull_advance by %u failed",
87 size);
89 return NDR_ERR_SUCCESS;
93 set the parse offset to 'ofs'
95 static enum ndr_err_code ndr_pull_set_offset(struct ndr_pull *ndr, uint32_t ofs)
97 ndr->offset = ofs;
98 if (ndr->offset > ndr->data_size) {
99 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
100 "ndr_pull_set_offset %u failed",
101 ofs);
103 return NDR_ERR_SUCCESS;
106 /* create a ndr_push structure, ready for some marshalling */
107 _PUBLIC_ struct ndr_push *ndr_push_init_ctx(TALLOC_CTX *mem_ctx)
109 struct ndr_push *ndr;
111 ndr = talloc_zero(mem_ctx, struct ndr_push);
112 if (!ndr) {
113 return NULL;
116 ndr->flags = 0;
117 ndr->alloc_size = NDR_BASE_MARSHALL_SIZE;
118 ndr->data = talloc_array(ndr, uint8_t, ndr->alloc_size);
119 if (!ndr->data) {
120 return NULL;
123 return ndr;
126 /* return a DATA_BLOB structure for the current ndr_push marshalled data */
127 _PUBLIC_ DATA_BLOB ndr_push_blob(struct ndr_push *ndr)
129 DATA_BLOB blob;
130 blob = data_blob_const(ndr->data, ndr->offset);
131 if (ndr->alloc_size > ndr->offset) {
132 ndr->data[ndr->offset] = 0;
134 return blob;
139 expand the available space in the buffer to ndr->offset + extra_size
141 _PUBLIC_ enum ndr_err_code ndr_push_expand(struct ndr_push *ndr, uint32_t extra_size)
143 uint32_t size = extra_size + ndr->offset;
145 if (size < ndr->offset) {
146 /* extra_size overflowed the offset */
147 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, "Overflow in push_expand to %u",
148 size);
151 if (ndr->alloc_size > size) {
152 return NDR_ERR_SUCCESS;
155 ndr->alloc_size += NDR_BASE_MARSHALL_SIZE;
156 if (size+1 > ndr->alloc_size) {
157 ndr->alloc_size = size+1;
159 ndr->data = talloc_realloc(ndr, ndr->data, uint8_t, ndr->alloc_size);
160 if (!ndr->data) {
161 return ndr_push_error(ndr, NDR_ERR_ALLOC, "Failed to push_expand to %u",
162 ndr->alloc_size);
165 return NDR_ERR_SUCCESS;
168 _PUBLIC_ void ndr_print_debug_helper(struct ndr_print *ndr, const char *format, ...)
170 va_list ap;
171 char *s = NULL;
172 uint32_t i;
173 int ret;
175 va_start(ap, format);
176 ret = vasprintf(&s, format, ap);
177 va_end(ap);
179 if (ret == -1) {
180 return;
183 if (ndr->no_newline) {
184 DEBUGADD(1,("%s", s));
185 free(s);
186 return;
189 for (i=0;i<ndr->depth;i++) {
190 DEBUGADD(1,(" "));
193 DEBUGADD(1,("%s\n", s));
194 free(s);
197 _PUBLIC_ void ndr_print_printf_helper(struct ndr_print *ndr, const char *format, ...)
199 va_list ap;
200 uint32_t i;
202 if (!ndr->no_newline) {
203 for (i=0;i<ndr->depth;i++) {
204 printf(" ");
208 va_start(ap, format);
209 vprintf(format, ap);
210 va_end(ap);
211 if (!ndr->no_newline) {
212 printf("\n");
216 _PUBLIC_ void ndr_print_string_helper(struct ndr_print *ndr, const char *format, ...)
218 va_list ap;
219 uint32_t i;
221 if (!ndr->no_newline) {
222 for (i=0;i<ndr->depth;i++) {
223 ndr->private_data = talloc_asprintf_append_buffer(
224 (char *)ndr->private_data, " ");
228 va_start(ap, format);
229 ndr->private_data = talloc_vasprintf_append_buffer((char *)ndr->private_data,
230 format, ap);
231 va_end(ap);
232 if (!ndr->no_newline) {
233 ndr->private_data = talloc_asprintf_append_buffer((char *)ndr->private_data,
234 "\n");
239 a useful helper function for printing idl structures via DEBUG()
241 _PUBLIC_ void ndr_print_debug(ndr_print_fn_t fn, const char *name, void *ptr)
243 struct ndr_print *ndr;
245 DEBUG(1,(" "));
247 ndr = talloc_zero(NULL, struct ndr_print);
248 if (!ndr) return;
249 ndr->print = ndr_print_debug_helper;
250 ndr->depth = 1;
251 ndr->flags = 0;
252 fn(ndr, name, ptr);
253 talloc_free(ndr);
257 a useful helper function for printing idl unions via DEBUG()
259 _PUBLIC_ void ndr_print_union_debug(ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
261 struct ndr_print *ndr;
263 DEBUG(1,(" "));
265 ndr = talloc_zero(NULL, struct ndr_print);
266 if (!ndr) return;
267 ndr->print = ndr_print_debug_helper;
268 ndr->depth = 1;
269 ndr->flags = 0;
270 ndr_print_set_switch_value(ndr, ptr, level);
271 fn(ndr, name, ptr);
272 talloc_free(ndr);
276 a useful helper function for printing idl function calls via DEBUG()
278 _PUBLIC_ void ndr_print_function_debug(ndr_print_function_t fn, const char *name, int flags, void *ptr)
280 struct ndr_print *ndr;
282 DEBUG(1,(" "));
284 ndr = talloc_zero(NULL, struct ndr_print);
285 if (!ndr) return;
286 ndr->print = ndr_print_debug_helper;
287 ndr->depth = 1;
288 ndr->flags = 0;
290 fn(ndr, name, flags, ptr);
291 talloc_free(ndr);
295 a useful helper function for printing idl structures to a string
297 _PUBLIC_ char *ndr_print_struct_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, void *ptr)
299 struct ndr_print *ndr;
300 char *ret = NULL;
302 ndr = talloc_zero(mem_ctx, struct ndr_print);
303 if (!ndr) return NULL;
304 ndr->private_data = talloc_strdup(ndr, "");
305 if (!ndr->private_data) {
306 goto failed;
308 ndr->print = ndr_print_string_helper;
309 ndr->depth = 1;
310 ndr->flags = 0;
312 fn(ndr, name, ptr);
313 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
314 failed:
315 talloc_free(ndr);
316 return ret;
320 a useful helper function for printing idl unions to a string
322 _PUBLIC_ char *ndr_print_union_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
324 struct ndr_print *ndr;
325 char *ret = NULL;
327 ndr = talloc_zero(mem_ctx, struct ndr_print);
328 if (!ndr) return NULL;
329 ndr->private_data = talloc_strdup(ndr, "");
330 if (!ndr->private_data) {
331 goto failed;
333 ndr->print = ndr_print_string_helper;
334 ndr->depth = 1;
335 ndr->flags = 0;
336 ndr_print_set_switch_value(ndr, ptr, level);
337 fn(ndr, name, ptr);
338 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
339 failed:
340 talloc_free(ndr);
341 return ret;
345 a useful helper function for printing idl function calls to a string
347 _PUBLIC_ char *ndr_print_function_string(TALLOC_CTX *mem_ctx,
348 ndr_print_function_t fn, const char *name,
349 int flags, void *ptr)
351 struct ndr_print *ndr;
352 char *ret = NULL;
354 ndr = talloc_zero(mem_ctx, struct ndr_print);
355 if (!ndr) return NULL;
356 ndr->private_data = talloc_strdup(ndr, "");
357 if (!ndr->private_data) {
358 goto failed;
360 ndr->print = ndr_print_string_helper;
361 ndr->depth = 1;
362 ndr->flags = 0;
363 fn(ndr, name, flags, ptr);
364 ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
365 failed:
366 talloc_free(ndr);
367 return ret;
370 _PUBLIC_ void ndr_set_flags(uint32_t *pflags, uint32_t new_flags)
372 /* the big/little endian flags are inter-dependent */
373 if (new_flags & LIBNDR_FLAG_LITTLE_ENDIAN) {
374 (*pflags) &= ~LIBNDR_FLAG_BIGENDIAN;
375 (*pflags) &= ~LIBNDR_FLAG_NDR64;
377 if (new_flags & LIBNDR_FLAG_BIGENDIAN) {
378 (*pflags) &= ~LIBNDR_FLAG_LITTLE_ENDIAN;
379 (*pflags) &= ~LIBNDR_FLAG_NDR64;
381 if (new_flags & LIBNDR_ALIGN_FLAGS) {
382 /* Ensure we only have the passed-in
383 align flag set in the new_flags,
384 remove any old align flag. */
385 (*pflags) &= ~LIBNDR_ALIGN_FLAGS;
387 if (new_flags & LIBNDR_FLAG_NO_RELATIVE_REVERSE) {
388 (*pflags) &= ~LIBNDR_FLAG_RELATIVE_REVERSE;
390 (*pflags) |= new_flags;
394 return and possibly log an NDR error
396 _PUBLIC_ enum ndr_err_code ndr_pull_error(struct ndr_pull *ndr,
397 enum ndr_err_code ndr_err,
398 const char *format, ...)
400 char *s=NULL;
401 va_list ap;
402 int ret;
404 va_start(ap, format);
405 ret = vasprintf(&s, format, ap);
406 va_end(ap);
408 if (ret == -1) {
409 return NDR_ERR_ALLOC;
412 DEBUG(1,("ndr_pull_error(%u): %s\n", ndr_err, s));
414 free(s);
416 return ndr_err;
420 return and possibly log an NDR error
422 _PUBLIC_ enum ndr_err_code ndr_push_error(struct ndr_push *ndr,
423 enum ndr_err_code ndr_err,
424 const char *format, ...)
426 char *s=NULL;
427 va_list ap;
428 int ret;
430 va_start(ap, format);
431 ret = vasprintf(&s, format, ap);
432 va_end(ap);
434 if (ret == -1) {
435 return NDR_ERR_ALLOC;
438 DEBUG(1,("ndr_push_error(%u): %s\n", ndr_err, s));
440 free(s);
442 return ndr_err;
446 handle subcontext buffers, which in midl land are user-marshalled, but
447 we use magic in pidl to make them easier to cope with
449 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_start(struct ndr_pull *ndr,
450 struct ndr_pull **_subndr,
451 size_t header_size,
452 ssize_t size_is)
454 struct ndr_pull *subndr;
455 uint32_t r_content_size;
456 bool force_le = false;
457 bool force_be = false;
459 switch (header_size) {
460 case 0: {
461 uint32_t content_size = ndr->data_size - ndr->offset;
462 if (size_is >= 0) {
463 content_size = size_is;
465 r_content_size = content_size;
466 break;
469 case 2: {
470 uint16_t content_size;
471 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &content_size));
472 if (size_is >= 0 && size_is != content_size) {
473 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
474 (int)size_is, (int)content_size);
476 r_content_size = content_size;
477 break;
480 case 4: {
481 uint32_t content_size;
482 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &content_size));
483 if (size_is >= 0 && size_is != content_size) {
484 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
485 (int)size_is, (int)content_size);
487 r_content_size = content_size;
488 break;
490 case 0xFFFFFC01: {
492 * Common Type Header for the Serialization Stream
493 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
495 uint8_t version;
496 uint8_t drep;
497 uint16_t hdrlen;
498 uint32_t filler;
499 uint32_t content_size;
500 uint32_t reserved;
502 /* version */
503 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &version));
505 if (version != 1) {
506 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
507 "Bad subcontext (PULL) Common Type Header version %d != 1",
508 (int)version);
512 * 0x10 little endian
513 * 0x00 big endian
515 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &drep));
516 if (drep == 0x10) {
517 force_le = true;
518 } else if (drep == 0x00) {
519 force_be = true;
520 } else {
521 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
522 "Bad subcontext (PULL) Common Type Header invalid drep 0x%02X",
523 (unsigned int)drep);
526 /* length of the "Private Header for Constructed Type" */
527 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &hdrlen));
528 if (hdrlen != 8) {
529 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
530 "Bad subcontext (PULL) Common Type Header length %d != 8",
531 (int)hdrlen);
534 /* filler should be ignored */
535 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &filler));
538 * Private Header for Constructed Type
540 /* length - will be updated latter */
541 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
542 if (size_is >= 0 && size_is != content_size) {
543 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
544 (int)size_is, (int)content_size);
546 /* the content size must be a multiple of 8 */
547 if ((content_size % 8) != 0) {
548 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
549 "Bad subcontext (PULL) size_is(%d) not padded to 8 content_size %d",
550 (int)size_is, (int)content_size);
552 r_content_size = content_size;
554 /* reserved */
555 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &reserved));
556 break;
558 default:
559 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) header_size %d",
560 (int)header_size);
563 NDR_PULL_NEED_BYTES(ndr, r_content_size);
565 subndr = talloc_zero(ndr, struct ndr_pull);
566 NDR_ERR_HAVE_NO_MEMORY(subndr);
567 subndr->flags = ndr->flags & ~LIBNDR_FLAG_NDR64;
568 subndr->current_mem_ctx = ndr->current_mem_ctx;
570 subndr->data = ndr->data + ndr->offset;
571 subndr->offset = 0;
572 subndr->data_size = r_content_size;
574 if (force_le) {
575 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_LITTLE_ENDIAN);
576 } else if (force_be) {
577 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_BIGENDIAN);
580 *_subndr = subndr;
581 return NDR_ERR_SUCCESS;
584 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_end(struct ndr_pull *ndr,
585 struct ndr_pull *subndr,
586 size_t header_size,
587 ssize_t size_is)
589 uint32_t advance;
590 if (size_is >= 0) {
591 advance = size_is;
592 } else if (header_size > 0) {
593 advance = subndr->data_size;
594 } else {
595 advance = subndr->offset;
597 NDR_CHECK(ndr_pull_advance(ndr, advance));
598 return NDR_ERR_SUCCESS;
601 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_start(struct ndr_push *ndr,
602 struct ndr_push **_subndr,
603 size_t header_size,
604 ssize_t size_is)
606 struct ndr_push *subndr;
608 subndr = ndr_push_init_ctx(ndr);
609 NDR_ERR_HAVE_NO_MEMORY(subndr);
610 subndr->flags = ndr->flags & ~LIBNDR_FLAG_NDR64;
612 if (size_is > 0) {
613 NDR_CHECK(ndr_push_zero(subndr, size_is));
614 subndr->offset = 0;
615 subndr->relative_end_offset = size_is;
618 *_subndr = subndr;
619 return NDR_ERR_SUCCESS;
623 push a subcontext header
625 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_end(struct ndr_push *ndr,
626 struct ndr_push *subndr,
627 size_t header_size,
628 ssize_t size_is)
630 ssize_t padding_len;
632 if (size_is >= 0) {
633 padding_len = size_is - subndr->offset;
634 if (padding_len < 0) {
635 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
636 (int)subndr->offset, (int)size_is);
638 subndr->offset = size_is;
641 switch (header_size) {
642 case 0:
643 break;
645 case 2:
646 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, subndr->offset));
647 break;
649 case 4:
650 NDR_CHECK(ndr_push_uint3264(ndr, NDR_SCALARS, subndr->offset));
651 break;
653 case 0xFFFFFC01:
655 * Common Type Header for the Serialization Stream
656 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
658 padding_len = NDR_ROUND(subndr->offset, 8) - subndr->offset;
659 if (padding_len > 0) {
660 NDR_CHECK(ndr_push_zero(subndr, padding_len));
663 /* version */
664 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, 1));
667 * 0x10 little endian
668 * 0x00 big endian
670 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, NDR_BE(ndr)?0x00:0x10));
672 /* length of the "Private Header for Constructed Type" */
673 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 8));
675 /* filler */
676 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0xCCCCCCCC));
679 * Private Header for Constructed Type
681 /* length - will be updated latter */
682 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
684 /* reserved */
685 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
686 break;
688 default:
689 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext header size %d",
690 (int)header_size);
693 NDR_CHECK(ndr_push_bytes(ndr, subndr->data, subndr->offset));
694 return NDR_ERR_SUCCESS;
698 store a token in the ndr context, for later retrieval
700 _PUBLIC_ enum ndr_err_code ndr_token_store(TALLOC_CTX *mem_ctx,
701 struct ndr_token_list **list,
702 const void *key,
703 uint32_t value)
705 struct ndr_token_list *tok;
706 tok = talloc(mem_ctx, struct ndr_token_list);
707 NDR_ERR_HAVE_NO_MEMORY(tok);
708 tok->key = key;
709 tok->value = value;
710 DLIST_ADD((*list), tok);
711 return NDR_ERR_SUCCESS;
715 retrieve a token from a ndr context, using cmp_fn to match the tokens
717 _PUBLIC_ enum ndr_err_code ndr_token_retrieve_cmp_fn(struct ndr_token_list **list, const void *key, uint32_t *v,
718 comparison_fn_t _cmp_fn, bool _remove_tok)
720 struct ndr_token_list *tok;
721 for (tok=*list;tok;tok=tok->next) {
722 if (_cmp_fn && _cmp_fn(tok->key,key)==0) goto found;
723 else if (!_cmp_fn && tok->key == key) goto found;
725 return NDR_ERR_TOKEN;
726 found:
727 *v = tok->value;
728 if (_remove_tok) {
729 DLIST_REMOVE((*list), tok);
730 talloc_free(tok);
732 return NDR_ERR_SUCCESS;
736 retrieve a token from a ndr context
738 _PUBLIC_ enum ndr_err_code ndr_token_retrieve(struct ndr_token_list **list, const void *key, uint32_t *v)
740 return ndr_token_retrieve_cmp_fn(list, key, v, NULL, true);
744 peek at but don't removed a token from a ndr context
746 _PUBLIC_ uint32_t ndr_token_peek(struct ndr_token_list **list, const void *key)
748 enum ndr_err_code status;
749 uint32_t v;
751 status = ndr_token_retrieve_cmp_fn(list, key, &v, NULL, false);
752 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
753 return 0;
756 return v;
760 pull an array size field and add it to the array_size_list token list
762 _PUBLIC_ enum ndr_err_code ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
764 uint32_t size;
765 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &size));
766 return ndr_token_store(ndr, &ndr->array_size_list, p, size);
770 get the stored array size field
772 _PUBLIC_ uint32_t ndr_get_array_size(struct ndr_pull *ndr, const void *p)
774 return ndr_token_peek(&ndr->array_size_list, p);
778 check the stored array size field
780 _PUBLIC_ enum ndr_err_code ndr_check_array_size(struct ndr_pull *ndr, void *p, uint32_t size)
782 uint32_t stored;
783 stored = ndr_token_peek(&ndr->array_size_list, p);
784 if (stored != size) {
785 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
786 "Bad array size - got %u expected %u\n",
787 stored, size);
789 return NDR_ERR_SUCCESS;
793 pull an array length field and add it to the array_length_list token list
795 _PUBLIC_ enum ndr_err_code ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
797 uint32_t length, offset;
798 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &offset));
799 if (offset != 0) {
800 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
801 "non-zero array offset %u\n", offset);
803 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &length));
804 return ndr_token_store(ndr, &ndr->array_length_list, p, length);
808 get the stored array length field
810 _PUBLIC_ uint32_t ndr_get_array_length(struct ndr_pull *ndr, const void *p)
812 return ndr_token_peek(&ndr->array_length_list, p);
816 check the stored array length field
818 _PUBLIC_ enum ndr_err_code ndr_check_array_length(struct ndr_pull *ndr, void *p, uint32_t length)
820 uint32_t stored;
821 stored = ndr_token_peek(&ndr->array_length_list, p);
822 if (stored != length) {
823 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
824 "Bad array length - got %u expected %u\n",
825 stored, length);
827 return NDR_ERR_SUCCESS;
830 _PUBLIC_ enum ndr_err_code ndr_push_pipe_chunk_trailer(struct ndr_push *ndr, int ndr_flags, uint32_t count)
832 if (ndr->flags & LIBNDR_FLAG_NDR64) {
833 int64_t tmp = 0 - (int64_t)count;
834 uint64_t ncount = tmp;
836 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, ncount));
839 return NDR_ERR_SUCCESS;
842 _PUBLIC_ enum ndr_err_code ndr_check_pipe_chunk_trailer(struct ndr_pull *ndr, int ndr_flags, uint32_t count)
844 if (ndr->flags & LIBNDR_FLAG_NDR64) {
845 int64_t tmp = 0 - (int64_t)count;
846 uint64_t ncount1 = tmp;
847 uint64_t ncount2;
849 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, &ncount2));
850 if (ncount1 == ncount2) {
851 return NDR_ERR_SUCCESS;
854 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
855 "Bad pipe trailer[%lld should be %lld] size was %lu\"",
856 (unsigned long long)ncount2,
857 (unsigned long long)ncount1,
858 (unsigned long)count);
861 return NDR_ERR_SUCCESS;
865 store a switch value
867 _PUBLIC_ enum ndr_err_code ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
869 return ndr_token_store(ndr, &ndr->switch_list, p, val);
872 _PUBLIC_ enum ndr_err_code ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
874 return ndr_token_store(ndr, &ndr->switch_list, p, val);
877 _PUBLIC_ enum ndr_err_code ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
879 return ndr_token_store(ndr, &ndr->switch_list, p, val);
883 retrieve a switch value
885 _PUBLIC_ uint32_t ndr_push_get_switch_value(struct ndr_push *ndr, const void *p)
887 return ndr_token_peek(&ndr->switch_list, p);
890 _PUBLIC_ uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
892 return ndr_token_peek(&ndr->switch_list, p);
895 _PUBLIC_ uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
897 return ndr_token_peek(&ndr->switch_list, p);
901 pull a struct from a blob using NDR
903 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
904 ndr_pull_flags_fn_t fn)
906 struct ndr_pull *ndr;
907 ndr = ndr_pull_init_blob(blob, mem_ctx);
908 NDR_ERR_HAVE_NO_MEMORY(ndr);
909 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
910 talloc_free(ndr);
911 return NDR_ERR_SUCCESS;
915 pull a struct from a blob using NDR - failing if all bytes are not consumed
917 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
918 void *p, ndr_pull_flags_fn_t fn)
920 struct ndr_pull *ndr;
921 uint32_t highest_ofs;
922 ndr = ndr_pull_init_blob(blob, mem_ctx);
923 NDR_ERR_HAVE_NO_MEMORY(ndr);
924 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
925 if (ndr->offset > ndr->relative_highest_offset) {
926 highest_ofs = ndr->offset;
927 } else {
928 highest_ofs = ndr->relative_highest_offset;
930 if (highest_ofs < ndr->data_size) {
931 enum ndr_err_code ret;
932 ret = ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
933 "not all bytes consumed ofs[%u] size[%u]",
934 highest_ofs, ndr->data_size);
935 talloc_free(ndr);
936 return ret;
938 talloc_free(ndr);
939 return NDR_ERR_SUCCESS;
943 pull a union from a blob using NDR, given the union discriminator
945 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
946 void *p,
947 uint32_t level, ndr_pull_flags_fn_t fn)
949 struct ndr_pull *ndr;
950 ndr = ndr_pull_init_blob(blob, mem_ctx);
951 NDR_ERR_HAVE_NO_MEMORY(ndr);
952 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
953 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
954 talloc_free(ndr);
955 return NDR_ERR_SUCCESS;
959 pull a union from a blob using NDR, given the union discriminator,
960 failing if all bytes are not consumed
962 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
963 void *p,
964 uint32_t level, ndr_pull_flags_fn_t fn)
966 struct ndr_pull *ndr;
967 uint32_t highest_ofs;
968 ndr = ndr_pull_init_blob(blob, mem_ctx);
969 NDR_ERR_HAVE_NO_MEMORY(ndr);
970 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
971 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
972 if (ndr->offset > ndr->relative_highest_offset) {
973 highest_ofs = ndr->offset;
974 } else {
975 highest_ofs = ndr->relative_highest_offset;
977 if (highest_ofs < ndr->data_size) {
978 enum ndr_err_code ret;
979 ret = ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
980 "not all bytes consumed ofs[%u] size[%u]",
981 highest_ofs, ndr->data_size);
982 talloc_free(ndr);
983 return ret;
985 talloc_free(ndr);
986 return NDR_ERR_SUCCESS;
990 push a struct to a blob using NDR
992 _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)
994 struct ndr_push *ndr;
995 ndr = ndr_push_init_ctx(mem_ctx);
996 NDR_ERR_HAVE_NO_MEMORY(ndr);
998 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
1000 *blob = ndr_push_blob(ndr);
1001 talloc_steal(mem_ctx, blob->data);
1002 talloc_free(ndr);
1004 return NDR_ERR_SUCCESS;
1008 push a union to a blob using NDR
1010 _PUBLIC_ enum ndr_err_code ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
1011 uint32_t level, ndr_push_flags_fn_t fn)
1013 struct ndr_push *ndr;
1014 ndr = ndr_push_init_ctx(mem_ctx);
1015 NDR_ERR_HAVE_NO_MEMORY(ndr);
1017 NDR_CHECK(ndr_push_set_switch_value(ndr, p, level));
1018 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
1020 *blob = ndr_push_blob(ndr);
1021 talloc_steal(mem_ctx, blob->data);
1022 talloc_free(ndr);
1024 return NDR_ERR_SUCCESS;
1028 generic ndr_size_*() handler for structures
1030 _PUBLIC_ size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push)
1032 struct ndr_push *ndr;
1033 enum ndr_err_code status;
1034 size_t ret;
1036 /* avoid recursion */
1037 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
1039 ndr = ndr_push_init_ctx(NULL);
1040 if (!ndr) return 0;
1041 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
1042 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
1043 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1044 talloc_free(ndr);
1045 return 0;
1047 ret = ndr->offset;
1048 talloc_free(ndr);
1049 return ret;
1053 generic ndr_size_*() handler for unions
1055 _PUBLIC_ size_t ndr_size_union(const void *p, int flags, uint32_t level, ndr_push_flags_fn_t push)
1057 struct ndr_push *ndr;
1058 enum ndr_err_code status;
1059 size_t ret;
1061 /* avoid recursion */
1062 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
1064 ndr = ndr_push_init_ctx(NULL);
1065 if (!ndr) return 0;
1066 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
1068 status = ndr_push_set_switch_value(ndr, p, level);
1069 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1070 talloc_free(ndr);
1071 return 0;
1073 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
1074 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1075 talloc_free(ndr);
1076 return 0;
1078 ret = ndr->offset;
1079 talloc_free(ndr);
1080 return ret;
1084 get the current base for relative pointers for the push
1086 _PUBLIC_ uint32_t ndr_push_get_relative_base_offset(struct ndr_push *ndr)
1088 return ndr->relative_base_offset;
1092 restore the old base for relative pointers for the push
1094 _PUBLIC_ void ndr_push_restore_relative_base_offset(struct ndr_push *ndr, uint32_t offset)
1096 ndr->relative_base_offset = offset;
1100 setup the current base for relative pointers for the push
1101 called in the NDR_SCALAR stage
1103 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset1(struct ndr_push *ndr, const void *p, uint32_t offset)
1105 ndr->relative_base_offset = offset;
1106 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1110 setup the current base for relative pointers for the push
1111 called in the NDR_BUFFERS stage
1113 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset2(struct ndr_push *ndr, const void *p)
1115 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1119 push a relative object - stage1
1120 this is called during SCALARS processing
1122 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
1124 if (p == NULL) {
1125 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
1126 return NDR_ERR_SUCCESS;
1128 NDR_CHECK(ndr_push_align(ndr, 4));
1129 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1130 return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
1134 push a short relative object - stage1
1135 this is called during SCALARS processing
1137 _PUBLIC_ enum ndr_err_code ndr_push_short_relative_ptr1(struct ndr_push *ndr, const void *p)
1139 if (p == NULL) {
1140 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 0));
1141 return NDR_ERR_SUCCESS;
1143 NDR_CHECK(ndr_push_align(ndr, 2));
1144 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1145 return ndr_push_uint16(ndr, NDR_SCALARS, 0xFFFF);
1148 push a relative object - stage2
1149 this is called during buffers processing
1151 static enum ndr_err_code ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
1153 uint32_t save_offset;
1154 uint32_t ptr_offset = 0xFFFFFFFF;
1155 if (p == NULL) {
1156 return NDR_ERR_SUCCESS;
1158 save_offset = ndr->offset;
1159 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1160 if (ptr_offset > ndr->offset) {
1161 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1162 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1163 ptr_offset, ndr->offset);
1165 ndr->offset = ptr_offset;
1166 if (save_offset < ndr->relative_base_offset) {
1167 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1168 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1169 save_offset, ndr->relative_base_offset);
1171 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
1172 ndr->offset = save_offset;
1173 return NDR_ERR_SUCCESS;
1176 push a short relative object - stage2
1177 this is called during buffers processing
1179 _PUBLIC_ enum ndr_err_code ndr_push_short_relative_ptr2(struct ndr_push *ndr, const void *p)
1181 uint32_t save_offset;
1182 uint32_t ptr_offset = 0xFFFF;
1183 if (p == NULL) {
1184 return NDR_ERR_SUCCESS;
1186 save_offset = ndr->offset;
1187 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1188 if (ptr_offset > ndr->offset) {
1189 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1190 "ndr_push_short_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1191 ptr_offset, ndr->offset);
1193 ndr->offset = ptr_offset;
1194 if (save_offset < ndr->relative_base_offset) {
1195 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1196 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1197 save_offset, ndr->relative_base_offset);
1199 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
1200 ndr->offset = save_offset;
1201 return NDR_ERR_SUCCESS;
1205 push a relative object - stage2 start
1206 this is called during buffers processing
1208 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_start(struct ndr_push *ndr, const void *p)
1210 if (p == NULL) {
1211 return NDR_ERR_SUCCESS;
1213 if (!(ndr->flags & LIBNDR_FLAG_RELATIVE_REVERSE)) {
1214 uint32_t relative_offset;
1215 size_t pad;
1216 size_t align = 1;
1218 if (ndr->offset < ndr->relative_base_offset) {
1219 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1220 "ndr_push_relative_ptr2_start ndr->offset(%u) < ndr->relative_base_offset(%u)",
1221 ndr->offset, ndr->relative_base_offset);
1224 relative_offset = ndr->offset - ndr->relative_base_offset;
1226 if (ndr->flags & LIBNDR_FLAG_NOALIGN) {
1227 align = 1;
1228 } else if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1229 align = 2;
1230 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1231 align = 4;
1232 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1233 align = 8;
1236 pad = ndr_align_size(relative_offset, align);
1237 if (pad) {
1238 NDR_CHECK(ndr_push_zero(ndr, pad));
1241 return ndr_push_relative_ptr2(ndr, p);
1243 if (ndr->relative_end_offset == -1) {
1244 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1245 "ndr_push_relative_ptr2_start RELATIVE_REVERSE flag set and relative_end_offset %d",
1246 ndr->relative_end_offset);
1248 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_begin_list, p, ndr->offset));
1249 return NDR_ERR_SUCCESS;
1253 push a relative object - stage2 end
1254 this is called during buffers processing
1256 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_end(struct ndr_push *ndr, const void *p)
1258 uint32_t begin_offset = 0xFFFFFFFF;
1259 ssize_t len;
1260 uint32_t correct_offset = 0;
1261 uint32_t align = 1;
1262 uint32_t pad = 0;
1264 if (p == NULL) {
1265 return NDR_ERR_SUCCESS;
1268 if (!(ndr->flags & LIBNDR_FLAG_RELATIVE_REVERSE)) {
1269 return NDR_ERR_SUCCESS;
1272 if (ndr->flags & LIBNDR_FLAG_NO_NDR_SIZE) {
1273 /* better say more than calculation a too small buffer */
1274 NDR_PUSH_ALIGN(ndr, 8);
1275 return NDR_ERR_SUCCESS;
1278 if (ndr->relative_end_offset < ndr->offset) {
1279 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1280 "ndr_push_relative_ptr2_end:"
1281 "relative_end_offset %u < offset %u",
1282 ndr->relative_end_offset, ndr->offset);
1285 NDR_CHECK(ndr_token_retrieve(&ndr->relative_begin_list, p, &begin_offset));
1287 /* we have marshalled a buffer, see how long it was */
1288 len = ndr->offset - begin_offset;
1290 if (len < 0) {
1291 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1292 "ndr_push_relative_ptr2_end:"
1293 "offset %u - begin_offset %u < 0",
1294 ndr->offset, begin_offset);
1297 if (ndr->relative_end_offset < len) {
1298 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1299 "ndr_push_relative_ptr2_end:"
1300 "relative_end_offset %u < len %lld",
1301 ndr->offset, (long long)len);
1304 /* the reversed offset is at the end of the main buffer */
1305 correct_offset = ndr->relative_end_offset - len;
1307 if (ndr->flags & LIBNDR_FLAG_NOALIGN) {
1308 align = 1;
1309 } else if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1310 align = 2;
1311 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1312 align = 4;
1313 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1314 align = 8;
1317 pad = ndr_align_size(correct_offset, align);
1318 if (pad) {
1319 correct_offset += pad;
1320 correct_offset -= align;
1323 if (correct_offset < begin_offset) {
1324 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1325 "ndr_push_relative_ptr2_end: "
1326 "correct_offset %u < begin_offset %u",
1327 correct_offset, begin_offset);
1330 if (len > 0) {
1331 uint32_t clear_size = correct_offset - begin_offset;
1333 clear_size = MIN(clear_size, len);
1335 /* now move the marshalled buffer to the end of the main buffer */
1336 memmove(ndr->data + correct_offset, ndr->data + begin_offset, len);
1338 if (clear_size) {
1339 /* and wipe out old buffer within the main buffer */
1340 memset(ndr->data + begin_offset, '\0', clear_size);
1344 /* and set the end offset for the next buffer */
1345 ndr->relative_end_offset = correct_offset;
1347 /* finally write the offset to the main buffer */
1348 ndr->offset = correct_offset;
1349 NDR_CHECK(ndr_push_relative_ptr2(ndr, p));
1351 /* restore to where we were in the main buffer */
1352 ndr->offset = begin_offset;
1354 return NDR_ERR_SUCCESS;
1358 get the current base for relative pointers for the pull
1360 _PUBLIC_ uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull *ndr)
1362 return ndr->relative_base_offset;
1366 restore the old base for relative pointers for the pull
1368 _PUBLIC_ void ndr_pull_restore_relative_base_offset(struct ndr_pull *ndr, uint32_t offset)
1370 ndr->relative_base_offset = offset;
1374 setup the current base for relative pointers for the pull
1375 called in the NDR_SCALAR stage
1377 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset1(struct ndr_pull *ndr, const void *p, uint32_t offset)
1379 ndr->relative_base_offset = offset;
1380 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1384 setup the current base for relative pointers for the pull
1385 called in the NDR_BUFFERS stage
1387 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset2(struct ndr_pull *ndr, const void *p)
1389 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1393 pull a relative object - stage1
1394 called during SCALARS processing
1396 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
1398 rel_offset += ndr->relative_base_offset;
1399 if (rel_offset > ndr->data_size) {
1400 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
1401 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1402 rel_offset, ndr->data_size);
1404 return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
1408 pull a relative object - stage2
1409 called during BUFFERS processing
1411 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
1413 uint32_t rel_offset;
1414 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
1415 return ndr_pull_set_offset(ndr, rel_offset);
1418 const static struct {
1419 enum ndr_err_code err;
1420 const char *string;
1421 } ndr_err_code_strings[] = {
1422 { NDR_ERR_SUCCESS, "Success" },
1423 { NDR_ERR_ARRAY_SIZE, "Bad Array Size" },
1424 { NDR_ERR_BAD_SWITCH, "Bad Switch" },
1425 { NDR_ERR_OFFSET, "Offset Error" },
1426 { NDR_ERR_RELATIVE, "Relative Pointer Error" },
1427 { NDR_ERR_CHARCNV, "Character Conversion Error" },
1428 { NDR_ERR_LENGTH, "Length Error" },
1429 { NDR_ERR_SUBCONTEXT, "Subcontext Error" },
1430 { NDR_ERR_COMPRESSION, "Compression Error" },
1431 { NDR_ERR_STRING, "String Error" },
1432 { NDR_ERR_VALIDATE, "Validate Error" },
1433 { NDR_ERR_BUFSIZE, "Buffer Size Error" },
1434 { NDR_ERR_ALLOC, "Allocation Error" },
1435 { NDR_ERR_RANGE, "Range Error" },
1436 { NDR_ERR_TOKEN, "Token Error" },
1437 { NDR_ERR_IPV4ADDRESS, "IPv4 Address Error" },
1438 { NDR_ERR_INVALID_POINTER, "Invalid Pointer" },
1439 { NDR_ERR_UNREAD_BYTES, "Unread Bytes" },
1440 { NDR_ERR_NDR64, "NDR64 assertion error" },
1441 { 0, NULL }
1444 _PUBLIC_ const char *ndr_map_error2string(enum ndr_err_code ndr_err)
1446 int i;
1447 for (i = 0; ndr_err_code_strings[i].string != NULL; i++) {
1448 if (ndr_err_code_strings[i].err == ndr_err)
1449 return ndr_err_code_strings[i].string;
1451 return "Unknown error";