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 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 #include "system/network.h"
24 #include "librpc/ndr/libndr.h"
25 #include "lib/util/util_net.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);
44 for (i=ndr->offset;i<ofs2;i++) {
45 if (ndr->data[i] != 0) {
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]));
62 _PUBLIC_ enum ndr_err_code 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);
67 return NDR_ERR_SUCCESS;
73 _PUBLIC_ enum ndr_err_code 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);
78 return NDR_ERR_SUCCESS;
84 _PUBLIC_ enum ndr_err_code 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);
90 return NDR_ERR_SUCCESS;
96 _PUBLIC_ enum ndr_err_code 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);
102 return NDR_ERR_SUCCESS;
108 _PUBLIC_ enum ndr_err_code ndr_pull_uint1632(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
110 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
112 enum ndr_err_code err = ndr_pull_uint32(ndr, ndr_flags, &v32);
114 if (unlikely(v32 != *v)) {
115 DEBUG(0,(__location__ ": non-zero upper 16 bits 0x%08x\n", (unsigned)v32));
116 return NDR_ERR_NDR64;
120 return ndr_pull_uint16(ndr, ndr_flags, v);
126 _PUBLIC_ enum ndr_err_code ndr_pull_int32(struct ndr_pull *ndr, int ndr_flags, int32_t *v)
128 NDR_PULL_ALIGN(ndr, 4);
129 NDR_PULL_NEED_BYTES(ndr, 4);
130 *v = NDR_IVALS(ndr, ndr->offset);
132 return NDR_ERR_SUCCESS;
138 _PUBLIC_ enum ndr_err_code ndr_pull_uint32(struct ndr_pull *ndr, int ndr_flags, uint32_t *v)
140 NDR_PULL_ALIGN(ndr, 4);
141 NDR_PULL_NEED_BYTES(ndr, 4);
142 *v = NDR_IVAL(ndr, ndr->offset);
144 return NDR_ERR_SUCCESS;
148 parse a arch dependent uint32/uint64
150 _PUBLIC_ enum ndr_err_code ndr_pull_uint3264(struct ndr_pull *ndr, int ndr_flags, uint32_t *v)
153 enum ndr_err_code err;
154 if (likely(!(ndr->flags & LIBNDR_FLAG_NDR64))) {
155 return ndr_pull_uint32(ndr, ndr_flags, v);
157 err = ndr_pull_hyper(ndr, ndr_flags, &v64);
159 if (unlikely(v64 != *v)) {
160 DEBUG(0,(__location__ ": non-zero upper 32 bits 0x%016llx\n",
161 (unsigned long long)v64));
162 return NDR_ERR_NDR64;
170 _PUBLIC_ enum ndr_err_code ndr_pull_double(struct ndr_pull *ndr, int ndr_flags, double *v)
172 NDR_PULL_ALIGN(ndr, 8);
173 NDR_PULL_NEED_BYTES(ndr, 8);
174 memcpy(v, ndr->data+ndr->offset, 8);
176 return NDR_ERR_SUCCESS;
180 parse a pointer referent identifier stored in 2 bytes
182 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr_short(struct ndr_pull *ndr, uint16_t *v)
184 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, v));
188 *(v) -= ndr->relative_rap_convert;
189 return NDR_ERR_SUCCESS;
193 parse a pointer referent identifier
195 _PUBLIC_ enum ndr_err_code ndr_pull_generic_ptr(struct ndr_pull *ndr, uint32_t *v)
197 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, v));
201 return NDR_ERR_SUCCESS;
205 parse a ref pointer referent identifier
207 _PUBLIC_ enum ndr_err_code ndr_pull_ref_ptr(struct ndr_pull *ndr, uint32_t *v)
209 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, v));
210 /* ref pointers always point to data */
212 return NDR_ERR_SUCCESS;
218 _PUBLIC_ enum ndr_err_code ndr_pull_udlong(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
220 NDR_PULL_ALIGN(ndr, 4);
221 NDR_PULL_NEED_BYTES(ndr, 8);
222 *v = NDR_IVAL(ndr, ndr->offset);
223 *v |= (uint64_t)(NDR_IVAL(ndr, ndr->offset+4)) << 32;
225 return NDR_ERR_SUCCESS;
231 _PUBLIC_ enum ndr_err_code ndr_pull_udlongr(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
233 NDR_PULL_ALIGN(ndr, 4);
234 NDR_PULL_NEED_BYTES(ndr, 8);
235 *v = ((uint64_t)NDR_IVAL(ndr, ndr->offset)) << 32;
236 *v |= NDR_IVAL(ndr, ndr->offset+4);
238 return NDR_ERR_SUCCESS;
244 _PUBLIC_ enum ndr_err_code ndr_pull_dlong(struct ndr_pull *ndr, int ndr_flags, int64_t *v)
246 return ndr_pull_udlong(ndr, ndr_flags, (uint64_t *)v);
252 _PUBLIC_ enum ndr_err_code ndr_pull_hyper(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
254 NDR_PULL_ALIGN(ndr, 8);
255 return ndr_pull_udlong(ndr, ndr_flags, v);
261 _PUBLIC_ enum ndr_err_code ndr_pull_pointer(struct ndr_pull *ndr, int ndr_flags, void* *v)
264 NDR_PULL_ALIGN(ndr, sizeof(h));
265 NDR_PULL_NEED_BYTES(ndr, sizeof(h));
266 memcpy(&h, ndr->data+ndr->offset, sizeof(h));
267 ndr->offset += sizeof(h);
269 return NDR_ERR_SUCCESS;
275 _PUBLIC_ enum ndr_err_code ndr_pull_NTSTATUS(struct ndr_pull *ndr, int ndr_flags, NTSTATUS *status)
278 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
279 *status = NT_STATUS(v);
280 return NDR_ERR_SUCCESS;
286 _PUBLIC_ enum ndr_err_code ndr_push_NTSTATUS(struct ndr_push *ndr, int ndr_flags, NTSTATUS status)
288 return ndr_push_uint32(ndr, ndr_flags, NT_STATUS_V(status));
291 _PUBLIC_ void ndr_print_NTSTATUS(struct ndr_print *ndr, const char *name, NTSTATUS r)
293 ndr->print(ndr, "%-25s: %s", name, nt_errstr(r));
299 _PUBLIC_ enum ndr_err_code ndr_pull_WERROR(struct ndr_pull *ndr, int ndr_flags, WERROR *status)
302 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
303 *status = W_ERROR(v);
304 return NDR_ERR_SUCCESS;
311 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *v)
313 return ndr_pull_uint8(ndr, ndr_flags, v);
317 parse a uint16_t enum
319 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint16(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
321 return ndr_pull_uint16(ndr, ndr_flags, v);
325 parse a uint1632_t enum (uint32_t on NDR64)
327 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint1632(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
329 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
331 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &v32));
334 DEBUG(0,(__location__ ": non-zero upper 16 bits 0x%08x\n", (unsigned)v32));
335 return NDR_ERR_NDR64;
337 return NDR_ERR_SUCCESS;
339 return ndr_pull_uint16(ndr, ndr_flags, v);
343 parse a uint32_t enum
345 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint32(struct ndr_pull *ndr, int ndr_flags, uint32_t *v)
347 return ndr_pull_uint32(ndr, ndr_flags, v);
353 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint8(struct ndr_push *ndr, int ndr_flags, uint8_t v)
355 return ndr_push_uint8(ndr, ndr_flags, v);
361 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint16(struct ndr_push *ndr, int ndr_flags, uint16_t v)
363 return ndr_push_uint16(ndr, ndr_flags, v);
369 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint32(struct ndr_push *ndr, int ndr_flags, uint32_t v)
371 return ndr_push_uint32(ndr, ndr_flags, v);
375 push a uint1632_t enum
377 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint1632(struct ndr_push *ndr, int ndr_flags, uint16_t v)
379 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
380 return ndr_push_uint32(ndr, ndr_flags, v);
382 return ndr_push_uint16(ndr, ndr_flags, v);
388 _PUBLIC_ enum ndr_err_code ndr_push_WERROR(struct ndr_push *ndr, int ndr_flags, WERROR status)
390 return ndr_push_uint32(ndr, NDR_SCALARS, W_ERROR_V(status));
393 _PUBLIC_ void ndr_print_WERROR(struct ndr_print *ndr, const char *name, WERROR r)
395 ndr->print(ndr, "%-25s: %s", name, win_errstr(r));
401 _PUBLIC_ enum ndr_err_code ndr_pull_bytes(struct ndr_pull *ndr, uint8_t *data, uint32_t n)
403 NDR_PULL_NEED_BYTES(ndr, n);
404 memcpy(data, ndr->data + ndr->offset, n);
406 return NDR_ERR_SUCCESS;
410 pull an array of uint8
412 _PUBLIC_ enum ndr_err_code ndr_pull_array_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *data, uint32_t n)
414 if (!(ndr_flags & NDR_SCALARS)) {
415 return NDR_ERR_SUCCESS;
417 return ndr_pull_bytes(ndr, data, n);
423 _PUBLIC_ enum ndr_err_code ndr_push_int8(struct ndr_push *ndr, int ndr_flags, int8_t v)
425 NDR_PUSH_NEED_BYTES(ndr, 1);
426 SCVAL(ndr->data, ndr->offset, (uint8_t)v);
428 return NDR_ERR_SUCCESS;
434 _PUBLIC_ enum ndr_err_code ndr_push_uint8(struct ndr_push *ndr, int ndr_flags, uint8_t v)
436 NDR_PUSH_NEED_BYTES(ndr, 1);
437 SCVAL(ndr->data, ndr->offset, v);
439 return NDR_ERR_SUCCESS;
445 _PUBLIC_ enum ndr_err_code ndr_push_int16(struct ndr_push *ndr, int ndr_flags, int16_t v)
447 NDR_PUSH_ALIGN(ndr, 2);
448 NDR_PUSH_NEED_BYTES(ndr, 2);
449 NDR_SSVAL(ndr, ndr->offset, (uint16_t)v);
451 return NDR_ERR_SUCCESS;
457 _PUBLIC_ enum ndr_err_code ndr_push_uint16(struct ndr_push *ndr, int ndr_flags, uint16_t v)
459 NDR_PUSH_ALIGN(ndr, 2);
460 NDR_PUSH_NEED_BYTES(ndr, 2);
461 NDR_SSVAL(ndr, ndr->offset, v);
463 return NDR_ERR_SUCCESS;
469 _PUBLIC_ enum ndr_err_code ndr_push_uint1632(struct ndr_push *ndr, int ndr_flags, uint16_t v)
471 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
472 return ndr_push_uint32(ndr, ndr_flags, v);
474 return ndr_push_uint16(ndr, ndr_flags, v);
480 _PUBLIC_ enum ndr_err_code ndr_push_int32(struct ndr_push *ndr, int ndr_flags, int32_t v)
482 NDR_PUSH_ALIGN(ndr, 4);
483 NDR_PUSH_NEED_BYTES(ndr, 4);
484 NDR_SIVALS(ndr, ndr->offset, v);
486 return NDR_ERR_SUCCESS;
492 _PUBLIC_ enum ndr_err_code ndr_push_uint32(struct ndr_push *ndr, int ndr_flags, uint32_t v)
494 NDR_PUSH_ALIGN(ndr, 4);
495 NDR_PUSH_NEED_BYTES(ndr, 4);
496 NDR_SIVAL(ndr, ndr->offset, v);
498 return NDR_ERR_SUCCESS;
504 _PUBLIC_ enum ndr_err_code ndr_push_uint3264(struct ndr_push *ndr, int ndr_flags, uint32_t v)
506 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
507 return ndr_push_hyper(ndr, ndr_flags, v);
509 return ndr_push_uint32(ndr, ndr_flags, v);
515 _PUBLIC_ enum ndr_err_code ndr_push_udlong(struct ndr_push *ndr, int ndr_flags, uint64_t v)
517 NDR_PUSH_ALIGN(ndr, 4);
518 NDR_PUSH_NEED_BYTES(ndr, 8);
519 NDR_SIVAL(ndr, ndr->offset, (v & 0xFFFFFFFF));
520 NDR_SIVAL(ndr, ndr->offset+4, (v>>32));
522 return NDR_ERR_SUCCESS;
528 _PUBLIC_ enum ndr_err_code ndr_push_udlongr(struct ndr_push *ndr, int ndr_flags, uint64_t v)
530 NDR_PUSH_ALIGN(ndr, 4);
531 NDR_PUSH_NEED_BYTES(ndr, 8);
532 NDR_SIVAL(ndr, ndr->offset, (v>>32));
533 NDR_SIVAL(ndr, ndr->offset+4, (v & 0xFFFFFFFF));
535 return NDR_ERR_SUCCESS;
541 _PUBLIC_ enum ndr_err_code ndr_push_dlong(struct ndr_push *ndr, int ndr_flags, int64_t v)
543 return ndr_push_udlong(ndr, NDR_SCALARS, (uint64_t)v);
549 _PUBLIC_ enum ndr_err_code ndr_push_hyper(struct ndr_push *ndr, int ndr_flags, uint64_t v)
551 NDR_PUSH_ALIGN(ndr, 8);
552 return ndr_push_udlong(ndr, NDR_SCALARS, v);
558 _PUBLIC_ enum ndr_err_code ndr_push_double(struct ndr_push *ndr, int ndr_flags, double v)
560 NDR_PUSH_ALIGN(ndr, 8);
561 NDR_PUSH_NEED_BYTES(ndr, 8);
562 memcpy(ndr->data+ndr->offset, &v, 8);
564 return NDR_ERR_SUCCESS;
570 _PUBLIC_ enum ndr_err_code ndr_push_pointer(struct ndr_push *ndr, int ndr_flags, void* v)
572 uintptr_t h = (intptr_t)v;
573 NDR_PUSH_ALIGN(ndr, sizeof(h));
574 NDR_PUSH_NEED_BYTES(ndr, sizeof(h));
575 memcpy(ndr->data+ndr->offset, &h, sizeof(h));
576 ndr->offset += sizeof(h);
577 return NDR_ERR_SUCCESS;
580 _PUBLIC_ enum ndr_err_code ndr_push_align(struct ndr_push *ndr, size_t size)
582 /* this is a nasty hack to make pidl work with NDR64 */
584 if (ndr->flags & LIBNDR_FLAG_NDR64) {
589 } else if (size == 3) {
590 if (ndr->flags & LIBNDR_FLAG_NDR64) {
596 NDR_PUSH_ALIGN(ndr, size);
597 return NDR_ERR_SUCCESS;
600 _PUBLIC_ enum ndr_err_code ndr_pull_align(struct ndr_pull *ndr, size_t size)
602 /* this is a nasty hack to make pidl work with NDR64 */
604 if (ndr->flags & LIBNDR_FLAG_NDR64) {
609 } else if (size == 3) {
610 if (ndr->flags & LIBNDR_FLAG_NDR64) {
616 NDR_PULL_ALIGN(ndr, size);
617 return NDR_ERR_SUCCESS;
620 _PUBLIC_ enum ndr_err_code ndr_push_union_align(struct ndr_push *ndr, size_t size)
622 /* MS-RPCE section 2.2.5.3.4.4 */
623 if (ndr->flags & LIBNDR_FLAG_NDR64) {
624 return ndr_push_align(ndr, size);
626 return NDR_ERR_SUCCESS;
629 _PUBLIC_ enum ndr_err_code ndr_pull_union_align(struct ndr_pull *ndr, size_t size)
631 /* MS-RPCE section 2.2.5.3.4.4 */
632 if (ndr->flags & LIBNDR_FLAG_NDR64) {
633 return ndr_pull_align(ndr, size);
635 return NDR_ERR_SUCCESS;
638 _PUBLIC_ enum ndr_err_code ndr_push_trailer_align(struct ndr_push *ndr, size_t size)
640 /* MS-RPCE section 2.2.5.3.4.1 */
641 if (ndr->flags & LIBNDR_FLAG_NDR64) {
642 return ndr_push_align(ndr, size);
644 return NDR_ERR_SUCCESS;
647 _PUBLIC_ enum ndr_err_code ndr_pull_trailer_align(struct ndr_pull *ndr, size_t size)
649 /* MS-RPCE section 2.2.5.3.4.1 */
650 if (ndr->flags & LIBNDR_FLAG_NDR64) {
651 return ndr_pull_align(ndr, size);
653 return NDR_ERR_SUCCESS;
659 _PUBLIC_ enum ndr_err_code ndr_push_bytes(struct ndr_push *ndr, const uint8_t *data, uint32_t n)
661 NDR_PUSH_NEED_BYTES(ndr, n);
662 memcpy(ndr->data + ndr->offset, data, n);
664 return NDR_ERR_SUCCESS;
670 _PUBLIC_ enum ndr_err_code ndr_push_zero(struct ndr_push *ndr, uint32_t n)
672 NDR_PUSH_NEED_BYTES(ndr, n);
673 memset(ndr->data + ndr->offset, 0, n);
675 return NDR_ERR_SUCCESS;
679 push an array of uint8
681 _PUBLIC_ enum ndr_err_code ndr_push_array_uint8(struct ndr_push *ndr, int ndr_flags, const uint8_t *data, uint32_t n)
683 if (!(ndr_flags & NDR_SCALARS)) {
684 return NDR_ERR_SUCCESS;
686 return ndr_push_bytes(ndr, data, n);
690 push a unique non-zero value if a pointer is non-NULL, otherwise 0
692 _PUBLIC_ enum ndr_err_code ndr_push_unique_ptr(struct ndr_push *ndr, const void *p)
696 ptr = ndr->ptr_count * 4;
700 return ndr_push_uint3264(ndr, NDR_SCALARS, ptr);
704 push a 'simple' full non-zero value if a pointer is non-NULL, otherwise 0
706 _PUBLIC_ enum ndr_err_code ndr_push_full_ptr(struct ndr_push *ndr, const void *p)
710 /* Check if the pointer already exists and has an id */
711 ptr = ndr_token_peek(&ndr->full_ptr_list, p);
714 ptr = ndr->ptr_count;
715 ndr_token_store(ndr, &ndr->full_ptr_list, p, ptr);
718 return ndr_push_uint3264(ndr, NDR_SCALARS, ptr);
722 push always a 0, if a pointer is NULL it's a fatal error
724 _PUBLIC_ enum ndr_err_code ndr_push_ref_ptr(struct ndr_push *ndr)
726 return ndr_push_uint3264(ndr, NDR_SCALARS, 0xAEF1AEF1);
733 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME(struct ndr_push *ndr, int ndr_flags, NTTIME t)
735 NDR_CHECK(ndr_push_udlong(ndr, ndr_flags, t));
736 return NDR_ERR_SUCCESS;
742 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
744 NDR_CHECK(ndr_pull_udlong(ndr, ndr_flags, t));
745 return NDR_ERR_SUCCESS;
751 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME_1sec(struct ndr_push *ndr, int ndr_flags, NTTIME t)
754 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
755 return NDR_ERR_SUCCESS;
761 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME_1sec(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
763 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
765 return NDR_ERR_SUCCESS;
771 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME_hyper(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
773 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
774 return NDR_ERR_SUCCESS;
780 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME_hyper(struct ndr_push *ndr, int ndr_flags, NTTIME t)
782 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
783 return NDR_ERR_SUCCESS;
789 _PUBLIC_ enum ndr_err_code ndr_push_time_t(struct ndr_push *ndr, int ndr_flags, time_t t)
791 return ndr_push_uint32(ndr, ndr_flags, t);
797 _PUBLIC_ enum ndr_err_code ndr_pull_time_t(struct ndr_pull *ndr, int ndr_flags, time_t *t)
800 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &tt));
802 return NDR_ERR_SUCCESS;
809 _PUBLIC_ enum ndr_err_code ndr_pull_ipv4address(struct ndr_pull *ndr, int ndr_flags, const char **address)
813 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &addr));
814 in.s_addr = htonl(addr);
815 *address = talloc_strdup(ndr->current_mem_ctx, inet_ntoa(in));
816 NDR_ERR_HAVE_NO_MEMORY(*address);
817 return NDR_ERR_SUCCESS;
823 _PUBLIC_ enum ndr_err_code ndr_push_ipv4address(struct ndr_push *ndr, int ndr_flags, const char *address)
826 if (!is_ipaddress(address)) {
827 return ndr_push_error(ndr, NDR_ERR_IPV4ADDRESS,
828 "Invalid IPv4 address: '%s'",
831 addr = inet_addr(address);
832 NDR_CHECK(ndr_push_uint32(ndr, ndr_flags, htonl(addr)));
833 return NDR_ERR_SUCCESS;
839 _PUBLIC_ void ndr_print_ipv4address(struct ndr_print *ndr, const char *name,
842 ndr->print(ndr, "%-25s: %s", name, address);
846 _PUBLIC_ void ndr_print_struct(struct ndr_print *ndr, const char *name, const char *type)
848 ndr->print(ndr, "%s: struct %s", name, type);
851 _PUBLIC_ void ndr_print_enum(struct ndr_print *ndr, const char *name, const char *type,
852 const char *val, uint32_t value)
854 if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
855 ndr->print(ndr, "%-25s: %s (0x%X)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
857 ndr->print(ndr, "%-25s: %s (%d)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
861 _PUBLIC_ void ndr_print_bitmap_flag(struct ndr_print *ndr, size_t size, const char *flag_name, uint32_t flag, uint32_t value)
863 /* this is an attempt to support multi-bit bitmap masks */
866 while (!(flag & 1)) {
871 ndr->print(ndr, " %d: %-25s", value, flag_name);
873 ndr->print(ndr, "0x%02x: %-25s (%d)", value, flag_name, value);
877 _PUBLIC_ void ndr_print_int8(struct ndr_print *ndr, const char *name, int8_t v)
879 ndr->print(ndr, "%-25s: %d", name, v);
882 _PUBLIC_ void ndr_print_uint8(struct ndr_print *ndr, const char *name, uint8_t v)
884 ndr->print(ndr, "%-25s: 0x%02x (%u)", name, v, v);
887 _PUBLIC_ void ndr_print_int16(struct ndr_print *ndr, const char *name, int16_t v)
889 ndr->print(ndr, "%-25s: %d", name, v);
892 _PUBLIC_ void ndr_print_uint16(struct ndr_print *ndr, const char *name, uint16_t v)
894 ndr->print(ndr, "%-25s: 0x%04x (%u)", name, v, v);
897 _PUBLIC_ void ndr_print_int32(struct ndr_print *ndr, const char *name, int32_t v)
899 ndr->print(ndr, "%-25s: %d", name, v);
902 _PUBLIC_ void ndr_print_uint32(struct ndr_print *ndr, const char *name, uint32_t v)
904 ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
907 _PUBLIC_ void ndr_print_int3264(struct ndr_print *ndr, const char *name, int32_t v)
909 ndr->print(ndr, "%-25s: %d", name, v);
912 _PUBLIC_ void ndr_print_uint3264(struct ndr_print *ndr, const char *name, uint32_t v)
914 ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
917 _PUBLIC_ void ndr_print_udlong(struct ndr_print *ndr, const char *name, uint64_t v)
919 ndr->print(ndr, "%-25s: 0x%016llx (%llu)", name, (unsigned long long)v, (unsigned long long)v);
922 _PUBLIC_ void ndr_print_udlongr(struct ndr_print *ndr, const char *name, uint64_t v)
924 ndr_print_udlong(ndr, name, v);
927 _PUBLIC_ void ndr_print_dlong(struct ndr_print *ndr, const char *name, int64_t v)
929 ndr->print(ndr, "%-25s: 0x%016llx (%lld)", name, (unsigned long long)v, (long long)v);
932 _PUBLIC_ void ndr_print_double(struct ndr_print *ndr, const char *name, double v)
934 ndr->print(ndr, "%-25s: %f", name, v);
937 _PUBLIC_ void ndr_print_hyper(struct ndr_print *ndr, const char *name, uint64_t v)
939 ndr_print_dlong(ndr, name, v);
942 _PUBLIC_ void ndr_print_pointer(struct ndr_print *ndr, const char *name, void *v)
944 ndr->print(ndr, "%-25s: %p", name, v);
947 _PUBLIC_ void ndr_print_ptr(struct ndr_print *ndr, const char *name, const void *p)
950 ndr->print(ndr, "%-25s: *", name);
952 ndr->print(ndr, "%-25s: NULL", name);
956 _PUBLIC_ void ndr_print_NTTIME(struct ndr_print *ndr, const char *name, NTTIME t)
958 ndr->print(ndr, "%-25s: %s", name, nt_time_string(ndr, t));
961 _PUBLIC_ void ndr_print_NTTIME_1sec(struct ndr_print *ndr, const char *name, NTTIME t)
963 /* this is a standard NTTIME here
964 * as it's already converted in the pull/push code
966 ndr_print_NTTIME(ndr, name, t);
969 _PUBLIC_ void ndr_print_NTTIME_hyper(struct ndr_print *ndr, const char *name, NTTIME t)
971 ndr_print_NTTIME(ndr, name, t);
974 _PUBLIC_ void ndr_print_time_t(struct ndr_print *ndr, const char *name, time_t t)
976 if (t == (time_t)-1 || t == 0) {
977 ndr->print(ndr, "%-25s: (time_t)%d", name, (int)t);
979 ndr->print(ndr, "%-25s: %s", name, timestring(ndr, t));
983 _PUBLIC_ void ndr_print_union(struct ndr_print *ndr, const char *name, int level, const char *type)
985 if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
986 ndr->print(ndr, "%-25s: union %s(case 0x%X)", name, type, level);
988 ndr->print(ndr, "%-25s: union %s(case %d)", name, type, level);
992 _PUBLIC_ void ndr_print_bad_level(struct ndr_print *ndr, const char *name, uint16_t level)
994 ndr->print(ndr, "UNKNOWN LEVEL %u", level);
997 _PUBLIC_ void ndr_print_array_uint8(struct ndr_print *ndr, const char *name,
998 const uint8_t *data, uint32_t count)
1002 if (count <= 600 && (ndr->flags & LIBNDR_PRINT_ARRAY_HEX)) {
1004 for (i=0;i<count;i++) {
1005 snprintf(&s[i*2], 3, "%02x", data[i]);
1008 ndr->print(ndr, "%-25s: %s", name, s);
1012 ndr->print(ndr, "%s: ARRAY(%d)", name, count);
1014 for (i=0;i<count;i++) {
1016 if (asprintf(&idx, "[%d]", i) != -1) {
1017 ndr_print_uint8(ndr, idx, data[i]);
1024 static void ndr_print_asc(struct ndr_print *ndr, const uint8_t *buf, int len)
1028 ndr->print(ndr, "%c", isprint(buf[i])?buf[i]:'.');
1032 ndr_print version of dump_data()
1034 static void ndr_dump_data(struct ndr_print *ndr, const uint8_t *buf, int len)
1038 ndr->no_newline = true;
1041 if (i%16 == 0 && i<len) {
1042 ndr->print(ndr, "[%04X] ",i);
1045 ndr->print(ndr, "%02X ",(int)buf[i]);
1047 if (i%8 == 0) ndr->print(ndr," ");
1049 ndr_print_asc(ndr,&buf[i-16],8); ndr->print(ndr," ");
1050 ndr_print_asc(ndr,&buf[i-8],8); ndr->print(ndr, "\n");
1057 ndr->print(ndr, " ");
1058 if (n>8) ndr->print(ndr," ");
1059 while (n--) ndr->print(ndr," ");
1061 ndr_print_asc(ndr,&buf[i-(i%16)],n); ndr->print(ndr, " ");
1063 if (n>0) ndr_print_asc(ndr,&buf[i-n],n);
1064 ndr->print(ndr,"\n");
1067 ndr->no_newline = false;
1071 _PUBLIC_ void ndr_print_DATA_BLOB(struct ndr_print *ndr, const char *name, DATA_BLOB r)
1073 ndr->print(ndr, "%-25s: DATA_BLOB length=%u", name, (unsigned)r.length);
1075 ndr_dump_data(ndr, r.data, r.length);
1081 push a DATA_BLOB onto the wire.
1083 _PUBLIC_ enum ndr_err_code ndr_push_DATA_BLOB(struct ndr_push *ndr, int ndr_flags, DATA_BLOB blob)
1085 if (ndr->flags & LIBNDR_ALIGN_FLAGS) {
1086 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1087 blob.length = NDR_ALIGN(ndr, 2);
1088 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1089 blob.length = NDR_ALIGN(ndr, 4);
1090 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1091 blob.length = NDR_ALIGN(ndr, 8);
1093 NDR_PUSH_ALLOC_SIZE(ndr, blob.data, blob.length);
1094 data_blob_clear(&blob);
1095 } else if (!(ndr->flags & LIBNDR_FLAG_REMAINING)) {
1096 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, blob.length));
1098 NDR_CHECK(ndr_push_bytes(ndr, blob.data, blob.length));
1099 return NDR_ERR_SUCCESS;
1103 pull a DATA_BLOB from the wire.
1105 _PUBLIC_ enum ndr_err_code ndr_pull_DATA_BLOB(struct ndr_pull *ndr, int ndr_flags, DATA_BLOB *blob)
1107 uint32_t length = 0;
1109 if (ndr->flags & LIBNDR_ALIGN_FLAGS) {
1110 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1111 length = NDR_ALIGN(ndr, 2);
1112 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1113 length = NDR_ALIGN(ndr, 4);
1114 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1115 length = NDR_ALIGN(ndr, 8);
1117 if (ndr->data_size - ndr->offset < length) {
1118 length = ndr->data_size - ndr->offset;
1120 } else if (ndr->flags & LIBNDR_FLAG_REMAINING) {
1121 length = ndr->data_size - ndr->offset;
1123 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &length));
1125 NDR_PULL_NEED_BYTES(ndr, length);
1126 *blob = data_blob_talloc(ndr->current_mem_ctx, ndr->data+ndr->offset, length);
1127 ndr->offset += length;
1128 return NDR_ERR_SUCCESS;
1131 _PUBLIC_ uint32_t ndr_size_DATA_BLOB(int ret, const DATA_BLOB *data, int flags)
1133 if (!data) return ret;
1134 return ret + data->length;