r19856: Use sptr as basis for full ptr implementation. Will add checks for duplicates...
[Samba.git] / source4 / librpc / ndr / ndr_basic.c
blobb8f2a8115fa75c80aa976dda971faa456646abbb
1 /*
2 Unix SMB/CIFS implementation.
4 routines for marshalling/unmarshalling basic types
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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
24 #include "system/network.h"
25 #include "librpc/ndr/libndr.h"
27 #define NDR_SVAL(ndr, ofs) (NDR_BE(ndr)?RSVAL(ndr->data,ofs):SVAL(ndr->data,ofs))
28 #define NDR_IVAL(ndr, ofs) (NDR_BE(ndr)?RIVAL(ndr->data,ofs):IVAL(ndr->data,ofs))
29 #define NDR_IVALS(ndr, ofs) (NDR_BE(ndr)?RIVALS(ndr->data,ofs):IVALS(ndr->data,ofs))
30 #define NDR_SSVAL(ndr, ofs, v) do { if (NDR_BE(ndr)) { RSSVAL(ndr->data,ofs,v); } else SSVAL(ndr->data,ofs,v); } while (0)
31 #define NDR_SIVAL(ndr, ofs, v) do { if (NDR_BE(ndr)) { RSIVAL(ndr->data,ofs,v); } else SIVAL(ndr->data,ofs,v); } while (0)
32 #define NDR_SIVALS(ndr, ofs, v) do { if (NDR_BE(ndr)) { RSIVALS(ndr->data,ofs,v); } else SIVALS(ndr->data,ofs,v); } while (0)
36 check for data leaks from the server by looking for non-zero pad bytes
37 these could also indicate that real structure elements have been
38 mistaken for padding in the IDL
40 _PUBLIC_ void ndr_check_padding(struct ndr_pull *ndr, size_t n)
42 size_t ofs2 = (ndr->offset + (n-1)) & ~(n-1);
43 int i;
44 for (i=ndr->offset;i<ofs2;i++) {
45 if (ndr->data[i] != 0) {
46 break;
49 if (i<ofs2) {
50 DEBUG(0,("WARNING: Non-zero padding to %d: ", (int)n));
51 for (i=ndr->offset;i<ofs2;i++) {
52 DEBUG(0,("%02x ", ndr->data[i]));
54 DEBUG(0,("\n"));
60 parse a int8_t
62 _PUBLIC_ NTSTATUS ndr_pull_int8(struct ndr_pull *ndr, int ndr_flags, int8_t *v)
64 NDR_PULL_NEED_BYTES(ndr, 1);
65 *v = (int8_t)CVAL(ndr->data, ndr->offset);
66 ndr->offset += 1;
67 return NT_STATUS_OK;
71 parse a uint8_t
73 _PUBLIC_ NTSTATUS ndr_pull_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *v)
75 NDR_PULL_NEED_BYTES(ndr, 1);
76 *v = CVAL(ndr->data, ndr->offset);
77 ndr->offset += 1;
78 return NT_STATUS_OK;
82 parse a int16_t
84 _PUBLIC_ NTSTATUS ndr_pull_int16(struct ndr_pull *ndr, int ndr_flags, int16_t *v)
86 NDR_PULL_ALIGN(ndr, 2);
87 NDR_PULL_NEED_BYTES(ndr, 2);
88 *v = (uint16_t)NDR_SVAL(ndr, ndr->offset);
89 ndr->offset += 2;
90 return NT_STATUS_OK;
94 parse a uint16_t
96 _PUBLIC_ NTSTATUS ndr_pull_uint16(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
98 NDR_PULL_ALIGN(ndr, 2);
99 NDR_PULL_NEED_BYTES(ndr, 2);
100 *v = NDR_SVAL(ndr, ndr->offset);
101 ndr->offset += 2;
102 return NT_STATUS_OK;
106 parse a int32_t
108 _PUBLIC_ NTSTATUS ndr_pull_int32(struct ndr_pull *ndr, int ndr_flags, int32_t *v)
110 NDR_PULL_ALIGN(ndr, 4);
111 NDR_PULL_NEED_BYTES(ndr, 4);
112 *v = NDR_IVALS(ndr, ndr->offset);
113 ndr->offset += 4;
114 return NT_STATUS_OK;
118 parse a uint32_t
120 _PUBLIC_ NTSTATUS ndr_pull_uint32(struct ndr_pull *ndr, int ndr_flags, uint32_t *v)
122 NDR_PULL_ALIGN(ndr, 4);
123 NDR_PULL_NEED_BYTES(ndr, 4);
124 *v = NDR_IVAL(ndr, ndr->offset);
125 ndr->offset += 4;
126 return NT_STATUS_OK;
130 parse a pointer referent identifier
132 _PUBLIC_ NTSTATUS ndr_pull_generic_ptr(struct ndr_pull *ndr, uint32_t *v)
134 NTSTATUS status;
135 status = ndr_pull_uint32(ndr, NDR_SCALARS, v);
136 if (NT_STATUS_IS_OK(status) && *v != 0) {
137 ndr->ptr_count++;
139 return status;
143 parse a ref pointer referent identifier
145 _PUBLIC_ NTSTATUS ndr_pull_ref_ptr(struct ndr_pull *ndr, uint32_t *v)
147 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, v));
148 /* ref pointers always point to data */
149 *v = 1;
150 return NT_STATUS_OK;
154 parse a udlong
156 _PUBLIC_ NTSTATUS ndr_pull_udlong(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
158 NDR_PULL_ALIGN(ndr, 4);
159 NDR_PULL_NEED_BYTES(ndr, 8);
160 *v = NDR_IVAL(ndr, ndr->offset);
161 *v |= (uint64_t)(NDR_IVAL(ndr, ndr->offset+4)) << 32;
162 ndr->offset += 8;
163 return NT_STATUS_OK;
167 parse a udlongr
169 _PUBLIC_ NTSTATUS ndr_pull_udlongr(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
171 NDR_PULL_ALIGN(ndr, 4);
172 NDR_PULL_NEED_BYTES(ndr, 8);
173 *v = ((uint64_t)NDR_IVAL(ndr, ndr->offset)) << 32;
174 *v |= NDR_IVAL(ndr, ndr->offset+4);
175 ndr->offset += 8;
176 return NT_STATUS_OK;
180 parse a dlong
182 _PUBLIC_ NTSTATUS ndr_pull_dlong(struct ndr_pull *ndr, int ndr_flags, int64_t *v)
184 return ndr_pull_udlong(ndr, ndr_flags, (uint64_t *)v);
188 parse a hyper
190 _PUBLIC_ NTSTATUS ndr_pull_hyper(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
192 NDR_PULL_ALIGN(ndr, 8);
193 return ndr_pull_udlong(ndr, ndr_flags, v);
197 parse a pointer
199 _PUBLIC_ NTSTATUS ndr_pull_pointer(struct ndr_pull *ndr, int ndr_flags, void* *v)
201 intptr_t h;
202 NDR_PULL_ALIGN(ndr, sizeof(h));
203 NDR_PULL_NEED_BYTES(ndr, sizeof(h));
204 memcpy(&h, ndr->data+ndr->offset, sizeof(h));
205 ndr->offset += sizeof(h);
206 *v = (void *)h;
207 return NT_STATUS_OK;
211 pull a NTSTATUS
213 _PUBLIC_ NTSTATUS ndr_pull_NTSTATUS(struct ndr_pull *ndr, int ndr_flags, NTSTATUS *status)
215 uint32_t v;
216 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
217 *status = NT_STATUS(v);
218 return NT_STATUS_OK;
222 push a NTSTATUS
224 _PUBLIC_ NTSTATUS ndr_push_NTSTATUS(struct ndr_push *ndr, int ndr_flags, NTSTATUS status)
226 return ndr_push_uint32(ndr, ndr_flags, NT_STATUS_V(status));
229 _PUBLIC_ void ndr_print_NTSTATUS(struct ndr_print *ndr, const char *name, NTSTATUS r)
231 ndr->print(ndr, "%-25s: %s", name, nt_errstr(r));
235 pull a WERROR
237 _PUBLIC_ NTSTATUS ndr_pull_WERROR(struct ndr_pull *ndr, int ndr_flags, WERROR *status)
239 uint32_t v;
240 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
241 *status = W_ERROR(v);
242 return NT_STATUS_OK;
246 push a WERROR
248 _PUBLIC_ NTSTATUS ndr_push_WERROR(struct ndr_push *ndr, int ndr_flags, WERROR status)
250 return ndr_push_uint32(ndr, NDR_SCALARS, W_ERROR_V(status));
253 _PUBLIC_ void ndr_print_WERROR(struct ndr_print *ndr, const char *name, WERROR r)
255 ndr->print(ndr, "%-25s: %s", name, win_errstr(r));
259 parse a set of bytes
261 _PUBLIC_ NTSTATUS ndr_pull_bytes(struct ndr_pull *ndr, uint8_t *data, uint32_t n)
263 NDR_PULL_NEED_BYTES(ndr, n);
264 memcpy(data, ndr->data + ndr->offset, n);
265 ndr->offset += n;
266 return NT_STATUS_OK;
270 pull an array of uint8
272 _PUBLIC_ NTSTATUS ndr_pull_array_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *data, uint32_t n)
274 if (!(ndr_flags & NDR_SCALARS)) {
275 return NT_STATUS_OK;
277 return ndr_pull_bytes(ndr, data, n);
281 push a int8_t
283 _PUBLIC_ NTSTATUS ndr_push_int8(struct ndr_push *ndr, int ndr_flags, int8_t v)
285 NDR_PUSH_NEED_BYTES(ndr, 1);
286 SCVAL(ndr->data, ndr->offset, (uint8_t)v);
287 ndr->offset += 1;
288 return NT_STATUS_OK;
292 push a uint8_t
294 _PUBLIC_ NTSTATUS ndr_push_uint8(struct ndr_push *ndr, int ndr_flags, uint8_t v)
296 NDR_PUSH_NEED_BYTES(ndr, 1);
297 SCVAL(ndr->data, ndr->offset, v);
298 ndr->offset += 1;
299 return NT_STATUS_OK;
303 push a int16_t
305 _PUBLIC_ NTSTATUS ndr_push_int16(struct ndr_push *ndr, int ndr_flags, int16_t v)
307 NDR_PUSH_ALIGN(ndr, 2);
308 NDR_PUSH_NEED_BYTES(ndr, 2);
309 NDR_SSVAL(ndr, ndr->offset, (uint16_t)v);
310 ndr->offset += 2;
311 return NT_STATUS_OK;
315 push a uint16_t
317 _PUBLIC_ NTSTATUS ndr_push_uint16(struct ndr_push *ndr, int ndr_flags, uint16_t v)
319 NDR_PUSH_ALIGN(ndr, 2);
320 NDR_PUSH_NEED_BYTES(ndr, 2);
321 NDR_SSVAL(ndr, ndr->offset, v);
322 ndr->offset += 2;
323 return NT_STATUS_OK;
327 push a int32_t
329 _PUBLIC_ NTSTATUS ndr_push_int32(struct ndr_push *ndr, int ndr_flags, int32_t v)
331 NDR_PUSH_ALIGN(ndr, 4);
332 NDR_PUSH_NEED_BYTES(ndr, 4);
333 NDR_SIVALS(ndr, ndr->offset, v);
334 ndr->offset += 4;
335 return NT_STATUS_OK;
339 push a uint32_t
341 _PUBLIC_ NTSTATUS ndr_push_uint32(struct ndr_push *ndr, int ndr_flags, uint32_t v)
343 NDR_PUSH_ALIGN(ndr, 4);
344 NDR_PUSH_NEED_BYTES(ndr, 4);
345 NDR_SIVAL(ndr, ndr->offset, v);
346 ndr->offset += 4;
347 return NT_STATUS_OK;
351 push a udlong
353 _PUBLIC_ NTSTATUS ndr_push_udlong(struct ndr_push *ndr, int ndr_flags, uint64_t v)
355 NDR_PUSH_ALIGN(ndr, 4);
356 NDR_PUSH_NEED_BYTES(ndr, 8);
357 NDR_SIVAL(ndr, ndr->offset, (v & 0xFFFFFFFF));
358 NDR_SIVAL(ndr, ndr->offset+4, (v>>32));
359 ndr->offset += 8;
360 return NT_STATUS_OK;
364 push a udlongr
366 _PUBLIC_ NTSTATUS ndr_push_udlongr(struct ndr_push *ndr, int ndr_flags, uint64_t v)
368 NDR_PUSH_ALIGN(ndr, 4);
369 NDR_PUSH_NEED_BYTES(ndr, 8);
370 NDR_SIVAL(ndr, ndr->offset, (v>>32));
371 NDR_SIVAL(ndr, ndr->offset+4, (v & 0xFFFFFFFF));
372 ndr->offset += 8;
373 return NT_STATUS_OK;
377 push a dlong
379 _PUBLIC_ NTSTATUS ndr_push_dlong(struct ndr_push *ndr, int ndr_flags, int64_t v)
381 return ndr_push_udlong(ndr, NDR_SCALARS, (uint64_t)v);
385 push a hyper
387 _PUBLIC_ NTSTATUS ndr_push_hyper(struct ndr_push *ndr, int ndr_flags, uint64_t v)
389 NDR_PUSH_ALIGN(ndr, 8);
390 return ndr_push_udlong(ndr, NDR_SCALARS, v);
394 push a pointer
396 _PUBLIC_ NTSTATUS ndr_push_pointer(struct ndr_push *ndr, int ndr_flags, void* v)
398 intptr_t h = (intptr_t)v;
399 NDR_PUSH_ALIGN(ndr, sizeof(h));
400 NDR_PUSH_NEED_BYTES(ndr, sizeof(h));
401 memcpy(ndr->data+ndr->offset, &h, sizeof(h));
402 ndr->offset += sizeof(h);
403 return NT_STATUS_OK;
406 _PUBLIC_ NTSTATUS ndr_push_align(struct ndr_push *ndr, size_t size)
408 NDR_PUSH_ALIGN(ndr, size);
409 return NT_STATUS_OK;
412 _PUBLIC_ NTSTATUS ndr_pull_align(struct ndr_pull *ndr, size_t size)
414 NDR_PULL_ALIGN(ndr, size);
415 return NT_STATUS_OK;
419 push some bytes
421 _PUBLIC_ NTSTATUS ndr_push_bytes(struct ndr_push *ndr, const uint8_t *data, uint32_t n)
423 NDR_PUSH_NEED_BYTES(ndr, n);
424 memcpy(ndr->data + ndr->offset, data, n);
425 ndr->offset += n;
426 return NT_STATUS_OK;
430 push some zero bytes
432 _PUBLIC_ NTSTATUS ndr_push_zero(struct ndr_push *ndr, uint32_t n)
434 NDR_PUSH_NEED_BYTES(ndr, n);
435 memset(ndr->data + ndr->offset, 0, n);
436 ndr->offset += n;
437 return NT_STATUS_OK;
441 push an array of uint8
443 _PUBLIC_ NTSTATUS ndr_push_array_uint8(struct ndr_push *ndr, int ndr_flags, const uint8_t *data, uint32_t n)
445 if (!(ndr_flags & NDR_SCALARS)) {
446 return NT_STATUS_OK;
448 return ndr_push_bytes(ndr, data, n);
452 save the current position
454 _PUBLIC_ void ndr_push_save(struct ndr_push *ndr, struct ndr_push_save *save)
456 save->offset = ndr->offset;
460 restore the position
462 _PUBLIC_ void ndr_push_restore(struct ndr_push *ndr, struct ndr_push_save *save)
464 ndr->offset = save->offset;
468 push a unique non-zero value if a pointer is non-NULL, otherwise 0
470 _PUBLIC_ NTSTATUS ndr_push_unique_ptr(struct ndr_push *ndr, const void *p)
472 uint32_t ptr = 0;
473 if (p) {
474 ptr = ndr->ptr_count * 4;
475 ptr |= 0x00020000;
476 ndr->ptr_count++;
478 return ndr_push_uint32(ndr, NDR_SCALARS, ptr);
482 push a 'simple' full non-zero value if a pointer is non-NULL, otherwise 0
484 _PUBLIC_ NTSTATUS ndr_push_full_ptr(struct ndr_push *ndr, const void *p)
486 uint32_t ptr = 0;
487 if (p) {
488 ndr->ptr_count++;
489 ptr = ndr->ptr_count;
491 return ndr_push_uint32(ndr, NDR_SCALARS, ptr);
495 push always a 0, if a pointer is NULL it's a fatal error
497 _PUBLIC_ NTSTATUS ndr_push_ref_ptr(struct ndr_push *ndr)
499 return ndr_push_uint32(ndr, NDR_SCALARS, 0xAEF1AEF1);
504 push a NTTIME
506 _PUBLIC_ NTSTATUS ndr_push_NTTIME(struct ndr_push *ndr, int ndr_flags, NTTIME t)
508 NDR_CHECK(ndr_push_udlong(ndr, ndr_flags, t));
509 return NT_STATUS_OK;
513 pull a NTTIME
515 _PUBLIC_ NTSTATUS ndr_pull_NTTIME(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
517 NDR_CHECK(ndr_pull_udlong(ndr, ndr_flags, t));
518 return NT_STATUS_OK;
522 push a NTTIME
524 _PUBLIC_ NTSTATUS ndr_push_NTTIME_1sec(struct ndr_push *ndr, int ndr_flags, NTTIME t)
526 t /= 10000000;
527 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
528 return NT_STATUS_OK;
532 pull a NTTIME_1sec
534 _PUBLIC_ NTSTATUS ndr_pull_NTTIME_1sec(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
536 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
537 (*t) *= 10000000;
538 return NT_STATUS_OK;
542 pull a NTTIME_hyper
544 _PUBLIC_ NTSTATUS ndr_pull_NTTIME_hyper(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
546 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
547 return NT_STATUS_OK;
551 push a NTTIME_hyper
553 _PUBLIC_ NTSTATUS ndr_push_NTTIME_hyper(struct ndr_push *ndr, int ndr_flags, NTTIME t)
555 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
556 return NT_STATUS_OK;
560 push a time_t
562 _PUBLIC_ NTSTATUS ndr_push_time_t(struct ndr_push *ndr, int ndr_flags, time_t t)
564 return ndr_push_uint32(ndr, ndr_flags, t);
568 pull a time_t
570 _PUBLIC_ NTSTATUS ndr_pull_time_t(struct ndr_pull *ndr, int ndr_flags, time_t *t)
572 uint32_t tt;
573 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &tt));
574 *t = tt;
575 return NT_STATUS_OK;
580 pull a ipv4address
582 _PUBLIC_ NTSTATUS ndr_pull_ipv4address(struct ndr_pull *ndr, int ndr_flags, const char **address)
584 struct ipv4_addr in;
585 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &in.addr));
586 in.addr = htonl(in.addr);
587 *address = talloc_strdup(ndr->current_mem_ctx, sys_inet_ntoa(in));
588 NT_STATUS_HAVE_NO_MEMORY(*address);
589 return NT_STATUS_OK;
593 push a ipv4address
595 _PUBLIC_ NTSTATUS ndr_push_ipv4address(struct ndr_push *ndr, int ndr_flags, const char *address)
597 uint32_t addr;
598 if (!is_ipaddress(address)) {
599 return ndr_push_error(ndr, NDR_ERR_IPV4ADDRESS,
600 "Invalid IPv4 address: '%s'",
601 address);
603 addr = inet_addr(address);
604 NDR_CHECK(ndr_push_uint32(ndr, ndr_flags, htonl(addr)));
605 return NT_STATUS_OK;
609 print a ipv4address
611 _PUBLIC_ void ndr_print_ipv4address(struct ndr_print *ndr, const char *name,
612 const char *address)
614 ndr->print(ndr, "%-25s: %s", name, address);
618 _PUBLIC_ void ndr_print_struct(struct ndr_print *ndr, const char *name, const char *type)
620 ndr->print(ndr, "%s: struct %s", name, type);
623 _PUBLIC_ void ndr_print_enum(struct ndr_print *ndr, const char *name, const char *type,
624 const char *val, uint32_t value)
626 if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
627 ndr->print(ndr, "%-25s: %s (0x%X)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
628 } else {
629 ndr->print(ndr, "%-25s: %s (%d)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
633 _PUBLIC_ void ndr_print_bitmap_flag(struct ndr_print *ndr, size_t size, const char *flag_name, uint32_t flag, uint32_t value)
635 /* this is an attempt to support multi-bit bitmap masks */
636 value &= flag;
638 while (!(flag & 1)) {
639 flag >>= 1;
640 value >>= 1;
642 if (flag == 1) {
643 ndr->print(ndr, " %d: %-25s", value, flag_name);
644 } else {
645 ndr->print(ndr, "0x%02x: %-25s (%d)", value, flag_name, value);
649 _PUBLIC_ void ndr_print_int8(struct ndr_print *ndr, const char *name, int8_t v)
651 ndr->print(ndr, "%-25s: %d", name, v);
654 _PUBLIC_ void ndr_print_uint8(struct ndr_print *ndr, const char *name, uint8_t v)
656 ndr->print(ndr, "%-25s: 0x%02x (%u)", name, v, v);
659 _PUBLIC_ void ndr_print_int16(struct ndr_print *ndr, const char *name, int16_t v)
661 ndr->print(ndr, "%-25s: %d", name, v);
664 _PUBLIC_ void ndr_print_uint16(struct ndr_print *ndr, const char *name, uint16_t v)
666 ndr->print(ndr, "%-25s: 0x%04x (%u)", name, v, v);
669 _PUBLIC_ void ndr_print_int32(struct ndr_print *ndr, const char *name, int32_t v)
671 ndr->print(ndr, "%-25s: %d", name, v);
674 _PUBLIC_ void ndr_print_uint32(struct ndr_print *ndr, const char *name, uint32_t v)
676 ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
679 _PUBLIC_ void ndr_print_udlong(struct ndr_print *ndr, const char *name, uint64_t v)
681 ndr->print(ndr, "%-25s: 0x%016llx (%llu)", name, (unsigned long long)v, (unsigned long long)v);
684 _PUBLIC_ void ndr_print_udlongr(struct ndr_print *ndr, const char *name, uint64_t v)
686 ndr_print_udlong(ndr, name, v);
689 _PUBLIC_ void ndr_print_dlong(struct ndr_print *ndr, const char *name, int64_t v)
691 ndr->print(ndr, "%-25s: 0x%016llx (%lld)", name, (unsigned long long)v, (long long)v);
694 _PUBLIC_ void ndr_print_hyper(struct ndr_print *ndr, const char *name, uint64_t v)
696 ndr_print_dlong(ndr, name, v);
699 _PUBLIC_ void ndr_print_pointer(struct ndr_print *ndr, const char *name, void *v)
701 ndr->print(ndr, "%-25s: %p", name, v);
704 _PUBLIC_ void ndr_print_ptr(struct ndr_print *ndr, const char *name, const void *p)
706 if (p) {
707 ndr->print(ndr, "%-25s: *", name);
708 } else {
709 ndr->print(ndr, "%-25s: NULL", name);
713 _PUBLIC_ void ndr_print_NTTIME(struct ndr_print *ndr, const char *name, NTTIME t)
715 ndr->print(ndr, "%-25s: %s", name, nt_time_string(ndr, t));
718 _PUBLIC_ void ndr_print_NTTIME_1sec(struct ndr_print *ndr, const char *name, NTTIME t)
720 /* this is a standard NTTIME here
721 * as it's already converted in the pull/push code
723 ndr_print_NTTIME(ndr, name, t);
726 _PUBLIC_ void ndr_print_NTTIME_hyper(struct ndr_print *ndr, const char *name, NTTIME t)
728 ndr_print_NTTIME(ndr, name, t);
731 _PUBLIC_ void ndr_print_time_t(struct ndr_print *ndr, const char *name, time_t t)
733 if (t == (time_t)-1 || t == 0) {
734 ndr->print(ndr, "%-25s: (time_t)%d", name, (int)t);
735 } else {
736 ndr->print(ndr, "%-25s: %s", name, timestring(ndr, t));
740 _PUBLIC_ void ndr_print_union(struct ndr_print *ndr, const char *name, int level, const char *type)
742 if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
743 ndr->print(ndr, "%-25s: union %s(case 0x%X)", name, type, level);
744 } else {
745 ndr->print(ndr, "%-25s: union %s(case %d)", name, type, level);
749 _PUBLIC_ void ndr_print_bad_level(struct ndr_print *ndr, const char *name, uint16_t level)
751 ndr->print(ndr, "UNKNOWN LEVEL %u", level);
754 _PUBLIC_ void ndr_print_array_uint8(struct ndr_print *ndr, const char *name,
755 const uint8_t *data, uint32_t count)
757 int i;
759 if (count <= 600 && (ndr->flags & LIBNDR_PRINT_ARRAY_HEX)) {
760 char s[1202];
761 for (i=0;i<count;i++) {
762 snprintf(&s[i*2], 3, "%02x", data[i]);
764 s[i*2] = 0;
765 ndr->print(ndr, "%-25s: %s", name, s);
766 return;
769 ndr->print(ndr, "%s: ARRAY(%d)", name, count);
770 ndr->depth++;
771 for (i=0;i<count;i++) {
772 char *idx=NULL;
773 asprintf(&idx, "[%d]", i);
774 if (idx) {
775 ndr_print_uint8(ndr, idx, data[i]);
776 free(idx);
779 ndr->depth--;
782 _PUBLIC_ void ndr_print_DATA_BLOB(struct ndr_print *ndr, const char *name, DATA_BLOB r)
784 ndr->print(ndr, "%-25s: DATA_BLOB length=%u", name, (unsigned)r.length);
785 if (r.length) {
786 dump_data(10, r.data, r.length);
792 push a DATA_BLOB onto the wire.
794 _PUBLIC_ NTSTATUS ndr_push_DATA_BLOB(struct ndr_push *ndr, int ndr_flags, DATA_BLOB blob)
796 if (ndr->flags & LIBNDR_ALIGN_FLAGS) {
797 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
798 blob.length = NDR_ALIGN(ndr, 2);
799 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
800 blob.length = NDR_ALIGN(ndr, 4);
801 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
802 blob.length = NDR_ALIGN(ndr, 8);
804 NDR_PUSH_ALLOC_SIZE(ndr, blob.data, blob.length);
805 data_blob_clear(&blob);
806 } else if (!(ndr->flags & LIBNDR_FLAG_REMAINING)) {
807 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, blob.length));
809 NDR_CHECK(ndr_push_bytes(ndr, blob.data, blob.length));
810 return NT_STATUS_OK;
814 pull a DATA_BLOB from the wire.
816 _PUBLIC_ NTSTATUS ndr_pull_DATA_BLOB(struct ndr_pull *ndr, int ndr_flags, DATA_BLOB *blob)
818 uint32_t length = 0;
820 if (ndr->flags & LIBNDR_ALIGN_FLAGS) {
821 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
822 length = NDR_ALIGN(ndr, 2);
823 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
824 length = NDR_ALIGN(ndr, 4);
825 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
826 length = NDR_ALIGN(ndr, 8);
828 if (ndr->data_size - ndr->offset < length) {
829 length = ndr->data_size - ndr->offset;
831 } else if (ndr->flags & LIBNDR_FLAG_REMAINING) {
832 length = ndr->data_size - ndr->offset;
833 } else {
834 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &length));
836 NDR_PULL_NEED_BYTES(ndr, length);
837 *blob = data_blob_talloc(ndr->current_mem_ctx, ndr->data+ndr->offset, length);
838 ndr->offset += length;
839 return NT_STATUS_OK;
842 _PUBLIC_ uint32_t ndr_size_DATA_BLOB(int ret, const DATA_BLOB *data, int flags)
844 return ret + data->length;