s3: added support for fake oplocks in SMB2.
[Samba/gbeck.git] / librpc / ndr / ndr.c
blob1600d51c1c0791b07be13d536fbd493192e22eb5
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 int i, ret;
171 va_start(ap, format);
172 ret = vasprintf(&s, format, ap);
173 va_end(ap);
175 if (ret == -1) {
176 return;
179 for (i=0;i<ndr->depth;i++) {
180 DEBUGADD(1,(" "));
183 DEBUGADD(1,("%s\n", s));
184 free(s);
187 _PUBLIC_ void ndr_print_string_helper(struct ndr_print *ndr, const char *format, ...)
189 va_list ap;
190 int i;
192 for (i=0;i<ndr->depth;i++) {
193 ndr->private_data = talloc_asprintf_append_buffer(
194 (char *)ndr->private_data, " ");
197 va_start(ap, format);
198 ndr->private_data = talloc_vasprintf_append_buffer((char *)ndr->private_data,
199 format, ap);
200 va_end(ap);
201 ndr->private_data = talloc_asprintf_append_buffer((char *)ndr->private_data,
202 "\n");
206 a useful helper function for printing idl structures via DEBUG()
208 _PUBLIC_ void ndr_print_debug(ndr_print_fn_t fn, const char *name, void *ptr)
210 struct ndr_print *ndr;
212 DEBUG(1,(" "));
214 ndr = talloc_zero(NULL, struct ndr_print);
215 if (!ndr) return;
216 ndr->print = ndr_print_debug_helper;
217 ndr->depth = 1;
218 ndr->flags = 0;
219 fn(ndr, name, ptr);
220 talloc_free(ndr);
224 a useful helper function for printing idl unions via DEBUG()
226 _PUBLIC_ void ndr_print_union_debug(ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
228 struct ndr_print *ndr;
230 DEBUG(1,(" "));
232 ndr = talloc_zero(NULL, struct ndr_print);
233 if (!ndr) return;
234 ndr->print = ndr_print_debug_helper;
235 ndr->depth = 1;
236 ndr->flags = 0;
237 ndr_print_set_switch_value(ndr, ptr, level);
238 fn(ndr, name, ptr);
239 talloc_free(ndr);
243 a useful helper function for printing idl function calls via DEBUG()
245 _PUBLIC_ void ndr_print_function_debug(ndr_print_function_t fn, const char *name, int flags, void *ptr)
247 struct ndr_print *ndr;
249 DEBUG(1,(" "));
251 ndr = talloc_zero(NULL, struct ndr_print);
252 if (!ndr) return;
253 ndr->print = ndr_print_debug_helper;
254 ndr->depth = 1;
255 ndr->flags = 0;
257 fn(ndr, name, flags, ptr);
258 talloc_free(ndr);
262 a useful helper function for printing idl structures to a string
264 _PUBLIC_ char *ndr_print_struct_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, void *ptr)
266 struct ndr_print *ndr;
267 char *ret = NULL;
269 ndr = talloc_zero(mem_ctx, struct ndr_print);
270 if (!ndr) return NULL;
271 ndr->private_data = talloc_strdup(ndr, "");
272 if (!ndr->private_data) {
273 goto failed;
275 ndr->print = ndr_print_string_helper;
276 ndr->depth = 1;
277 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;
342 (*pflags) &= ~LIBNDR_FLAG_NDR64;
344 if (new_flags & LIBNDR_FLAG_BIGENDIAN) {
345 (*pflags) &= ~LIBNDR_FLAG_LITTLE_ENDIAN;
346 (*pflags) &= ~LIBNDR_FLAG_NDR64;
348 if (new_flags & LIBNDR_FLAG_REMAINING) {
349 (*pflags) &= ~LIBNDR_ALIGN_FLAGS;
351 if (new_flags & LIBNDR_ALIGN_FLAGS) {
352 (*pflags) &= ~LIBNDR_FLAG_REMAINING;
354 if (new_flags & LIBNDR_FLAG_NO_RELATIVE_REVERSE) {
355 (*pflags) &= ~LIBNDR_FLAG_RELATIVE_REVERSE;
357 (*pflags) |= new_flags;
361 return and possibly log an NDR error
363 _PUBLIC_ enum ndr_err_code ndr_pull_error(struct ndr_pull *ndr,
364 enum ndr_err_code ndr_err,
365 const char *format, ...)
367 char *s=NULL;
368 va_list ap;
369 int ret;
371 va_start(ap, format);
372 ret = vasprintf(&s, format, ap);
373 va_end(ap);
375 if (ret == -1) {
376 return NDR_ERR_ALLOC;
379 DEBUG(1,("ndr_pull_error(%u): %s\n", ndr_err, s));
381 free(s);
383 return ndr_err;
387 return and possibly log an NDR error
389 _PUBLIC_ enum ndr_err_code ndr_push_error(struct ndr_push *ndr,
390 enum ndr_err_code ndr_err,
391 const char *format, ...)
393 char *s=NULL;
394 va_list ap;
395 int ret;
397 va_start(ap, format);
398 ret = vasprintf(&s, format, ap);
399 va_end(ap);
401 if (ret == -1) {
402 return NDR_ERR_ALLOC;
405 DEBUG(1,("ndr_push_error(%u): %s\n", ndr_err, s));
407 free(s);
409 return ndr_err;
413 handle subcontext buffers, which in midl land are user-marshalled, but
414 we use magic in pidl to make them easier to cope with
416 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_start(struct ndr_pull *ndr,
417 struct ndr_pull **_subndr,
418 size_t header_size,
419 ssize_t size_is)
421 struct ndr_pull *subndr;
422 uint32_t r_content_size;
423 bool force_le = false;
424 bool force_be = false;
426 switch (header_size) {
427 case 0: {
428 uint32_t content_size = ndr->data_size - ndr->offset;
429 if (size_is >= 0) {
430 content_size = size_is;
432 r_content_size = content_size;
433 break;
436 case 2: {
437 uint16_t content_size;
438 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &content_size));
439 if (size_is >= 0 && size_is != content_size) {
440 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
441 (int)size_is, (int)content_size);
443 r_content_size = content_size;
444 break;
447 case 4: {
448 uint32_t content_size;
449 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &content_size));
450 if (size_is >= 0 && size_is != content_size) {
451 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
452 (int)size_is, (int)content_size);
454 r_content_size = content_size;
455 break;
457 case 0xFFFFFC01: {
459 * Common Type Header for the Serialization Stream
460 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
462 uint8_t version;
463 uint8_t drep;
464 uint16_t hdrlen;
465 uint32_t filler;
466 uint32_t content_size;
467 uint32_t reserved;
469 /* version */
470 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &version));
472 if (version != 1) {
473 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
474 "Bad subcontext (PULL) Common Type Header version %d != 1",
475 (int)version);
479 * 0x10 little endian
480 * 0x00 big endian
482 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &drep));
483 if (drep == 0x10) {
484 force_le = true;
485 } else if (drep == 0x00) {
486 force_be = true;
487 } else {
488 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
489 "Bad subcontext (PULL) Common Type Header invalid drep 0x%02X",
490 (unsigned int)drep);
493 /* length of the "Private Header for Constructed Type" */
494 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &hdrlen));
495 if (hdrlen != 8) {
496 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
497 "Bad subcontext (PULL) Common Type Header length %d != 8",
498 (int)hdrlen);
501 /* filler should be ignored */
502 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &filler));
505 * Private Header for Constructed Type
507 /* length - will be updated latter */
508 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
509 if (size_is >= 0 && size_is != content_size) {
510 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
511 (int)size_is, (int)content_size);
513 /* the content size must be a multiple of 8 */
514 if ((content_size % 8) != 0) {
515 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
516 "Bad subcontext (PULL) size_is(%d) not padded to 8 content_size %d",
517 (int)size_is, (int)content_size);
519 r_content_size = content_size;
521 /* reserved */
522 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &reserved));
523 break;
525 default:
526 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) header_size %d",
527 (int)header_size);
530 NDR_PULL_NEED_BYTES(ndr, r_content_size);
532 subndr = talloc_zero(ndr, struct ndr_pull);
533 NDR_ERR_HAVE_NO_MEMORY(subndr);
534 subndr->flags = ndr->flags & ~LIBNDR_FLAG_NDR64;
535 subndr->current_mem_ctx = ndr->current_mem_ctx;
537 subndr->data = ndr->data + ndr->offset;
538 subndr->offset = 0;
539 subndr->data_size = r_content_size;
541 if (force_le) {
542 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_LITTLE_ENDIAN);
543 } else if (force_be) {
544 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_BIGENDIAN);
547 *_subndr = subndr;
548 return NDR_ERR_SUCCESS;
551 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_end(struct ndr_pull *ndr,
552 struct ndr_pull *subndr,
553 size_t header_size,
554 ssize_t size_is)
556 uint32_t advance;
557 if (size_is >= 0) {
558 advance = size_is;
559 } else if (header_size > 0) {
560 advance = subndr->data_size;
561 } else {
562 advance = subndr->offset;
564 NDR_CHECK(ndr_pull_advance(ndr, advance));
565 return NDR_ERR_SUCCESS;
568 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_start(struct ndr_push *ndr,
569 struct ndr_push **_subndr,
570 size_t header_size,
571 ssize_t size_is)
573 struct ndr_push *subndr;
575 subndr = ndr_push_init_ctx(ndr);
576 NDR_ERR_HAVE_NO_MEMORY(subndr);
577 subndr->flags = ndr->flags & ~LIBNDR_FLAG_NDR64;
579 if (size_is > 0) {
580 NDR_CHECK(ndr_push_zero(subndr, size_is));
581 subndr->offset = 0;
582 subndr->relative_end_offset = size_is;
585 *_subndr = subndr;
586 return NDR_ERR_SUCCESS;
590 push a subcontext header
592 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_end(struct ndr_push *ndr,
593 struct ndr_push *subndr,
594 size_t header_size,
595 ssize_t size_is)
597 ssize_t padding_len;
599 if (size_is >= 0) {
600 padding_len = size_is - subndr->offset;
601 if (padding_len < 0) {
602 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
603 (int)subndr->offset, (int)size_is);
605 subndr->offset = size_is;
608 switch (header_size) {
609 case 0:
610 break;
612 case 2:
613 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, subndr->offset));
614 break;
616 case 4:
617 NDR_CHECK(ndr_push_uint3264(ndr, NDR_SCALARS, subndr->offset));
618 break;
620 case 0xFFFFFC01:
622 * Common Type Header for the Serialization Stream
623 * See [MS-RPCE] 2.2.6 Type Serialization Version 1
625 padding_len = NDR_ROUND(subndr->offset, 8) - subndr->offset;
626 if (padding_len > 0) {
627 NDR_CHECK(ndr_push_zero(subndr, padding_len));
630 /* version */
631 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, 1));
634 * 0x10 little endian
635 * 0x00 big endian
637 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, NDR_BE(ndr)?0x00:0x10));
639 /* length of the "Private Header for Constructed Type" */
640 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 8));
642 /* filler */
643 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0xCCCCCCCC));
646 * Private Header for Constructed Type
648 /* length - will be updated latter */
649 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
651 /* reserved */
652 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
653 break;
655 default:
656 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext header size %d",
657 (int)header_size);
660 NDR_CHECK(ndr_push_bytes(ndr, subndr->data, subndr->offset));
661 return NDR_ERR_SUCCESS;
665 store a token in the ndr context, for later retrieval
667 _PUBLIC_ enum ndr_err_code ndr_token_store(TALLOC_CTX *mem_ctx,
668 struct ndr_token_list **list,
669 const void *key,
670 uint32_t value)
672 struct ndr_token_list *tok;
673 tok = talloc(mem_ctx, struct ndr_token_list);
674 NDR_ERR_HAVE_NO_MEMORY(tok);
675 tok->key = key;
676 tok->value = value;
677 DLIST_ADD((*list), tok);
678 return NDR_ERR_SUCCESS;
682 retrieve a token from a ndr context, using cmp_fn to match the tokens
684 _PUBLIC_ enum ndr_err_code ndr_token_retrieve_cmp_fn(struct ndr_token_list **list, const void *key, uint32_t *v,
685 comparison_fn_t _cmp_fn, bool _remove_tok)
687 struct ndr_token_list *tok;
688 for (tok=*list;tok;tok=tok->next) {
689 if (_cmp_fn && _cmp_fn(tok->key,key)==0) goto found;
690 else if (!_cmp_fn && tok->key == key) goto found;
692 return NDR_ERR_TOKEN;
693 found:
694 *v = tok->value;
695 if (_remove_tok) {
696 DLIST_REMOVE((*list), tok);
697 talloc_free(tok);
699 return NDR_ERR_SUCCESS;
703 retrieve a token from a ndr context
705 _PUBLIC_ enum ndr_err_code ndr_token_retrieve(struct ndr_token_list **list, const void *key, uint32_t *v)
707 return ndr_token_retrieve_cmp_fn(list, key, v, NULL, true);
711 peek at but don't removed a token from a ndr context
713 _PUBLIC_ uint32_t ndr_token_peek(struct ndr_token_list **list, const void *key)
715 enum ndr_err_code status;
716 uint32_t v;
718 status = ndr_token_retrieve_cmp_fn(list, key, &v, NULL, false);
719 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
720 return 0;
723 return v;
727 pull an array size field and add it to the array_size_list token list
729 _PUBLIC_ enum ndr_err_code ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
731 uint32_t size;
732 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &size));
733 return ndr_token_store(ndr, &ndr->array_size_list, p, size);
737 get the stored array size field
739 _PUBLIC_ uint32_t ndr_get_array_size(struct ndr_pull *ndr, const void *p)
741 return ndr_token_peek(&ndr->array_size_list, p);
745 check the stored array size field
747 _PUBLIC_ enum ndr_err_code ndr_check_array_size(struct ndr_pull *ndr, void *p, uint32_t size)
749 uint32_t stored;
750 stored = ndr_token_peek(&ndr->array_size_list, p);
751 if (stored != size) {
752 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
753 "Bad array size - got %u expected %u\n",
754 stored, size);
756 return NDR_ERR_SUCCESS;
760 pull an array length field and add it to the array_length_list token list
762 _PUBLIC_ enum ndr_err_code ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
764 uint32_t length, offset;
765 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &offset));
766 if (offset != 0) {
767 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
768 "non-zero array offset %u\n", offset);
770 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &length));
771 return ndr_token_store(ndr, &ndr->array_length_list, p, length);
775 get the stored array length field
777 _PUBLIC_ uint32_t ndr_get_array_length(struct ndr_pull *ndr, const void *p)
779 return ndr_token_peek(&ndr->array_length_list, p);
783 check the stored array length field
785 _PUBLIC_ enum ndr_err_code ndr_check_array_length(struct ndr_pull *ndr, void *p, uint32_t length)
787 uint32_t stored;
788 stored = ndr_token_peek(&ndr->array_length_list, p);
789 if (stored != length) {
790 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
791 "Bad array length - got %u expected %u\n",
792 stored, length);
794 return NDR_ERR_SUCCESS;
798 store a switch value
800 _PUBLIC_ enum ndr_err_code ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
802 return ndr_token_store(ndr, &ndr->switch_list, p, val);
805 _PUBLIC_ enum ndr_err_code ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
807 return ndr_token_store(ndr, &ndr->switch_list, p, val);
810 _PUBLIC_ enum ndr_err_code ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
812 return ndr_token_store(ndr, &ndr->switch_list, p, val);
816 retrieve a switch value
818 _PUBLIC_ uint32_t ndr_push_get_switch_value(struct ndr_push *ndr, const void *p)
820 return ndr_token_peek(&ndr->switch_list, p);
823 _PUBLIC_ uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
825 return ndr_token_peek(&ndr->switch_list, p);
828 _PUBLIC_ uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
830 return ndr_token_peek(&ndr->switch_list, p);
834 pull a struct from a blob using NDR
836 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
837 ndr_pull_flags_fn_t fn)
839 struct ndr_pull *ndr;
840 ndr = ndr_pull_init_blob(blob, mem_ctx);
841 NDR_ERR_HAVE_NO_MEMORY(ndr);
842 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
843 talloc_free(ndr);
844 return NDR_ERR_SUCCESS;
848 pull a struct from a blob using NDR - failing if all bytes are not consumed
850 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
851 void *p, ndr_pull_flags_fn_t fn)
853 struct ndr_pull *ndr;
854 uint32_t highest_ofs;
855 ndr = ndr_pull_init_blob(blob, mem_ctx);
856 NDR_ERR_HAVE_NO_MEMORY(ndr);
857 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
858 if (ndr->offset > ndr->relative_highest_offset) {
859 highest_ofs = ndr->offset;
860 } else {
861 highest_ofs = ndr->relative_highest_offset;
863 if (highest_ofs < ndr->data_size) {
864 enum ndr_err_code ret;
865 ret = ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
866 "not all bytes consumed ofs[%u] size[%u]",
867 highest_ofs, ndr->data_size);
868 talloc_free(ndr);
869 return ret;
871 talloc_free(ndr);
872 return NDR_ERR_SUCCESS;
876 pull a union from a blob using NDR, given the union discriminator
878 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
879 void *p,
880 uint32_t level, ndr_pull_flags_fn_t fn)
882 struct ndr_pull *ndr;
883 ndr = ndr_pull_init_blob(blob, mem_ctx);
884 NDR_ERR_HAVE_NO_MEMORY(ndr);
885 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
886 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
887 talloc_free(ndr);
888 return NDR_ERR_SUCCESS;
892 pull a union from a blob using NDR, given the union discriminator,
893 failing if all bytes are not consumed
895 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
896 void *p,
897 uint32_t level, ndr_pull_flags_fn_t fn)
899 struct ndr_pull *ndr;
900 uint32_t highest_ofs;
901 ndr = ndr_pull_init_blob(blob, mem_ctx);
902 NDR_ERR_HAVE_NO_MEMORY(ndr);
903 NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
904 NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
905 if (ndr->offset > ndr->relative_highest_offset) {
906 highest_ofs = ndr->offset;
907 } else {
908 highest_ofs = ndr->relative_highest_offset;
910 if (highest_ofs < ndr->data_size) {
911 enum ndr_err_code ret;
912 ret = ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
913 "not all bytes consumed ofs[%u] size[%u]",
914 highest_ofs, ndr->data_size);
915 talloc_free(ndr);
916 return ret;
918 talloc_free(ndr);
919 return NDR_ERR_SUCCESS;
923 push a struct to a blob using NDR
925 _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)
927 struct ndr_push *ndr;
928 ndr = ndr_push_init_ctx(mem_ctx);
929 NDR_ERR_HAVE_NO_MEMORY(ndr);
931 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
933 *blob = ndr_push_blob(ndr);
934 talloc_steal(mem_ctx, blob->data);
935 talloc_free(ndr);
937 return NDR_ERR_SUCCESS;
941 push a union to a blob using NDR
943 _PUBLIC_ enum ndr_err_code ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
944 uint32_t level, ndr_push_flags_fn_t fn)
946 struct ndr_push *ndr;
947 ndr = ndr_push_init_ctx(mem_ctx);
948 NDR_ERR_HAVE_NO_MEMORY(ndr);
950 NDR_CHECK(ndr_push_set_switch_value(ndr, p, level));
951 NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
953 *blob = ndr_push_blob(ndr);
954 talloc_steal(mem_ctx, blob->data);
955 talloc_free(ndr);
957 return NDR_ERR_SUCCESS;
961 generic ndr_size_*() handler for structures
963 _PUBLIC_ size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push)
965 struct ndr_push *ndr;
966 enum ndr_err_code status;
967 size_t ret;
969 /* avoid recursion */
970 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
972 ndr = ndr_push_init_ctx(NULL);
973 if (!ndr) return 0;
974 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
975 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
976 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
977 talloc_free(ndr);
978 return 0;
980 ret = ndr->offset;
981 talloc_free(ndr);
982 return ret;
986 generic ndr_size_*() handler for unions
988 _PUBLIC_ size_t ndr_size_union(const void *p, int flags, uint32_t level, ndr_push_flags_fn_t push)
990 struct ndr_push *ndr;
991 enum ndr_err_code status;
992 size_t ret;
994 /* avoid recursion */
995 if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
997 ndr = ndr_push_init_ctx(NULL);
998 if (!ndr) return 0;
999 ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
1001 status = ndr_push_set_switch_value(ndr, p, level);
1002 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1003 talloc_free(ndr);
1004 return 0;
1006 status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
1007 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1008 talloc_free(ndr);
1009 return 0;
1011 ret = ndr->offset;
1012 talloc_free(ndr);
1013 return ret;
1017 get the current base for relative pointers for the push
1019 _PUBLIC_ uint32_t ndr_push_get_relative_base_offset(struct ndr_push *ndr)
1021 return ndr->relative_base_offset;
1025 restore the old base for relative pointers for the push
1027 _PUBLIC_ void ndr_push_restore_relative_base_offset(struct ndr_push *ndr, uint32_t offset)
1029 ndr->relative_base_offset = offset;
1033 setup the current base for relative pointers for the push
1034 called in the NDR_SCALAR stage
1036 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset1(struct ndr_push *ndr, const void *p, uint32_t offset)
1038 ndr->relative_base_offset = offset;
1039 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1043 setup the current base for relative pointers for the push
1044 called in the NDR_BUFFERS stage
1046 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset2(struct ndr_push *ndr, const void *p)
1048 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1052 push a relative object - stage1
1053 this is called during SCALARS processing
1055 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
1057 if (p == NULL) {
1058 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
1059 return NDR_ERR_SUCCESS;
1061 NDR_CHECK(ndr_push_align(ndr, 4));
1062 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1063 return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
1067 push a short relative object - stage1
1068 this is called during SCALARS processing
1070 _PUBLIC_ enum ndr_err_code ndr_push_short_relative_ptr1(struct ndr_push *ndr, const void *p)
1072 if (p == NULL) {
1073 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 0));
1074 return NDR_ERR_SUCCESS;
1076 NDR_CHECK(ndr_push_align(ndr, 2));
1077 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1078 return ndr_push_uint16(ndr, NDR_SCALARS, 0xFFFF);
1081 push a relative object - stage2
1082 this is called during buffers processing
1084 static enum ndr_err_code ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
1086 uint32_t save_offset;
1087 uint32_t ptr_offset = 0xFFFFFFFF;
1088 if (p == NULL) {
1089 return NDR_ERR_SUCCESS;
1091 save_offset = ndr->offset;
1092 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1093 if (ptr_offset > ndr->offset) {
1094 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1095 "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1096 ptr_offset, ndr->offset);
1098 ndr->offset = ptr_offset;
1099 if (save_offset < ndr->relative_base_offset) {
1100 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1101 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1102 save_offset, ndr->relative_base_offset);
1104 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
1105 ndr->offset = save_offset;
1106 return NDR_ERR_SUCCESS;
1109 push a short relative object - stage2
1110 this is called during buffers processing
1112 _PUBLIC_ enum ndr_err_code ndr_push_short_relative_ptr2(struct ndr_push *ndr, const void *p)
1114 uint32_t save_offset;
1115 uint32_t ptr_offset = 0xFFFF;
1116 if (p == NULL) {
1117 return NDR_ERR_SUCCESS;
1119 save_offset = ndr->offset;
1120 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1121 if (ptr_offset > ndr->offset) {
1122 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1123 "ndr_push_short_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1124 ptr_offset, ndr->offset);
1126 ndr->offset = ptr_offset;
1127 if (save_offset < ndr->relative_base_offset) {
1128 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1129 "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1130 save_offset, ndr->relative_base_offset);
1132 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
1133 ndr->offset = save_offset;
1134 return NDR_ERR_SUCCESS;
1138 push a relative object - stage2 start
1139 this is called during buffers processing
1141 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_start(struct ndr_push *ndr, const void *p)
1143 if (p == NULL) {
1144 return NDR_ERR_SUCCESS;
1146 if (!(ndr->flags & LIBNDR_FLAG_RELATIVE_REVERSE)) {
1147 return ndr_push_relative_ptr2(ndr, p);
1149 if (ndr->relative_end_offset == -1) {
1150 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1151 "ndr_push_relative_ptr2_start RELATIVE_REVERSE flag set and relative_end_offset %d",
1152 ndr->relative_end_offset);
1154 NDR_CHECK(ndr_token_store(ndr, &ndr->relative_begin_list, p, ndr->offset));
1155 return NDR_ERR_SUCCESS;
1159 push a relative object - stage2 end
1160 this is called during buffers processing
1162 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_end(struct ndr_push *ndr, const void *p)
1164 uint32_t begin_offset = 0xFFFFFFFF;
1165 ssize_t len;
1166 uint32_t correct_offset = 0;
1167 uint32_t align = 1;
1168 uint32_t pad = 0;
1170 if (p == NULL) {
1171 return NDR_ERR_SUCCESS;
1174 if (!(ndr->flags & LIBNDR_FLAG_RELATIVE_REVERSE)) {
1175 return NDR_ERR_SUCCESS;
1178 if (ndr->flags & LIBNDR_FLAG_NO_NDR_SIZE) {
1179 /* better say more than calculation a too small buffer */
1180 NDR_PUSH_ALIGN(ndr, 8);
1181 return NDR_ERR_SUCCESS;
1184 if (ndr->relative_end_offset < ndr->offset) {
1185 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1186 "ndr_push_relative_ptr2_end:"
1187 "relative_end_offset %u < offset %u",
1188 ndr->relative_end_offset, ndr->offset);
1191 NDR_CHECK(ndr_token_retrieve(&ndr->relative_begin_list, p, &begin_offset));
1193 /* we have marshalled a buffer, see how long it was */
1194 len = ndr->offset - begin_offset;
1196 if (len < 0) {
1197 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1198 "ndr_push_relative_ptr2_end:"
1199 "offset %u - begin_offset %u < 0",
1200 ndr->offset, begin_offset);
1203 if (ndr->relative_end_offset < len) {
1204 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1205 "ndr_push_relative_ptr2_end:"
1206 "relative_end_offset %u < len %lld",
1207 ndr->offset, (long long)len);
1210 /* the reversed offset is at the end of the main buffer */
1211 correct_offset = ndr->relative_end_offset - len;
1213 /* TODO: remove this hack and let the idl use FLAG_ALIGN2 explicit */
1214 align = 2;
1216 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1217 align = 2;
1218 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1219 align = 4;
1220 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1221 align = 8;
1224 pad = ndr_align_size(correct_offset, align);
1225 if (pad) {
1226 correct_offset += pad;
1227 correct_offset -= align;
1230 if (correct_offset < begin_offset) {
1231 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
1232 "ndr_push_relative_ptr2_end: "
1233 "correct_offset %u < begin_offset %u",
1234 correct_offset, begin_offset);
1237 if (len > 0) {
1238 uint32_t clear_size = correct_offset - begin_offset;
1240 clear_size = MIN(clear_size, len);
1242 /* now move the marshalled buffer to the end of the main buffer */
1243 memmove(ndr->data + correct_offset, ndr->data + begin_offset, len);
1245 if (clear_size) {
1246 /* and wipe out old buffer within the main buffer */
1247 memset(ndr->data + begin_offset, '\0', clear_size);
1251 /* and set the end offset for the next buffer */
1252 ndr->relative_end_offset = correct_offset;
1254 /* finally write the offset to the main buffer */
1255 ndr->offset = correct_offset;
1256 NDR_CHECK(ndr_push_relative_ptr2(ndr, p));
1258 /* restore to where we were in the main buffer */
1259 ndr->offset = begin_offset;
1261 return NDR_ERR_SUCCESS;
1265 get the current base for relative pointers for the pull
1267 _PUBLIC_ uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull *ndr)
1269 return ndr->relative_base_offset;
1273 restore the old base for relative pointers for the pull
1275 _PUBLIC_ void ndr_pull_restore_relative_base_offset(struct ndr_pull *ndr, uint32_t offset)
1277 ndr->relative_base_offset = offset;
1281 setup the current base for relative pointers for the pull
1282 called in the NDR_SCALAR stage
1284 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset1(struct ndr_pull *ndr, const void *p, uint32_t offset)
1286 ndr->relative_base_offset = offset;
1287 return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1291 setup the current base for relative pointers for the pull
1292 called in the NDR_BUFFERS stage
1294 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset2(struct ndr_pull *ndr, const void *p)
1296 return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1300 pull a relative object - stage1
1301 called during SCALARS processing
1303 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
1305 rel_offset += ndr->relative_base_offset;
1306 if (rel_offset > ndr->data_size) {
1307 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
1308 "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1309 rel_offset, ndr->data_size);
1311 return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
1315 pull a relative object - stage2
1316 called during BUFFERS processing
1318 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
1320 uint32_t rel_offset;
1321 NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
1322 return ndr_pull_set_offset(ndr, rel_offset);
1325 const static struct {
1326 enum ndr_err_code err;
1327 const char *string;
1328 } ndr_err_code_strings[] = {
1329 { NDR_ERR_SUCCESS, "Success" },
1330 { NDR_ERR_ARRAY_SIZE, "Bad Array Size" },
1331 { NDR_ERR_BAD_SWITCH, "Bad Switch" },
1332 { NDR_ERR_OFFSET, "Offset Error" },
1333 { NDR_ERR_RELATIVE, "Relative Pointer Error" },
1334 { NDR_ERR_CHARCNV, "Character Conversion Error" },
1335 { NDR_ERR_LENGTH, "Length Error" },
1336 { NDR_ERR_SUBCONTEXT, "Subcontext Error" },
1337 { NDR_ERR_COMPRESSION, "Compression Error" },
1338 { NDR_ERR_STRING, "String Error" },
1339 { NDR_ERR_VALIDATE, "Validate Error" },
1340 { NDR_ERR_BUFSIZE, "Buffer Size Error" },
1341 { NDR_ERR_ALLOC, "Allocation Error" },
1342 { NDR_ERR_RANGE, "Range Error" },
1343 { NDR_ERR_TOKEN, "Token Error" },
1344 { NDR_ERR_IPV4ADDRESS, "IPv4 Address Error" },
1345 { NDR_ERR_INVALID_POINTER, "Invalid Pointer" },
1346 { NDR_ERR_UNREAD_BYTES, "Unread Bytes" },
1347 { NDR_ERR_NDR64, "NDR64 assertion error" },
1348 { 0, NULL }
1351 _PUBLIC_ const char *ndr_map_error2string(enum ndr_err_code ndr_err)
1353 int i;
1354 for (i = 0; ndr_err_code_strings[i].string != NULL; i++) {
1355 if (ndr_err_code_strings[i].err == ndr_err)
1356 return ndr_err_code_strings[i].string;
1358 return "Unknown error";