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);
256 return ndr_pull_udlongr(ndr, ndr_flags, v);
258 return ndr_pull_udlong(ndr, ndr_flags, v);
264 _PUBLIC_ enum ndr_err_code ndr_pull_pointer(struct ndr_pull *ndr, int ndr_flags, void* *v)
267 NDR_PULL_ALIGN(ndr, sizeof(h));
268 NDR_PULL_NEED_BYTES(ndr, sizeof(h));
269 memcpy(&h, ndr->data+ndr->offset, sizeof(h));
270 ndr->offset += sizeof(h);
272 return NDR_ERR_SUCCESS;
278 _PUBLIC_ enum ndr_err_code ndr_pull_NTSTATUS(struct ndr_pull *ndr, int ndr_flags, NTSTATUS *status)
281 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
282 *status = NT_STATUS(v);
283 return NDR_ERR_SUCCESS;
289 _PUBLIC_ enum ndr_err_code ndr_push_NTSTATUS(struct ndr_push *ndr, int ndr_flags, NTSTATUS status)
291 return ndr_push_uint32(ndr, ndr_flags, NT_STATUS_V(status));
294 _PUBLIC_ void ndr_print_NTSTATUS(struct ndr_print *ndr, const char *name, NTSTATUS r)
296 ndr->print(ndr, "%-25s: %s", name, nt_errstr(r));
302 _PUBLIC_ enum ndr_err_code ndr_pull_WERROR(struct ndr_pull *ndr, int ndr_flags, WERROR *status)
305 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
306 *status = W_ERROR(v);
307 return NDR_ERR_SUCCESS;
314 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *v)
316 return ndr_pull_uint8(ndr, ndr_flags, v);
320 parse a uint16_t enum
322 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint16(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
324 return ndr_pull_uint16(ndr, ndr_flags, v);
328 parse a uint1632_t enum (uint32_t on NDR64)
330 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint1632(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
332 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
334 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &v32));
337 DEBUG(0,(__location__ ": non-zero upper 16 bits 0x%08x\n", (unsigned)v32));
338 return NDR_ERR_NDR64;
340 return NDR_ERR_SUCCESS;
342 return ndr_pull_uint16(ndr, ndr_flags, v);
346 parse a uint32_t enum
348 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint32(struct ndr_pull *ndr, int ndr_flags, uint32_t *v)
350 return ndr_pull_uint32(ndr, ndr_flags, v);
356 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint8(struct ndr_push *ndr, int ndr_flags, uint8_t v)
358 return ndr_push_uint8(ndr, ndr_flags, v);
364 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint16(struct ndr_push *ndr, int ndr_flags, uint16_t v)
366 return ndr_push_uint16(ndr, ndr_flags, v);
372 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint32(struct ndr_push *ndr, int ndr_flags, uint32_t v)
374 return ndr_push_uint32(ndr, ndr_flags, v);
378 push a uint1632_t enum
380 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint1632(struct ndr_push *ndr, int ndr_flags, uint16_t v)
382 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
383 return ndr_push_uint32(ndr, ndr_flags, v);
385 return ndr_push_uint16(ndr, ndr_flags, v);
391 _PUBLIC_ enum ndr_err_code ndr_push_WERROR(struct ndr_push *ndr, int ndr_flags, WERROR status)
393 return ndr_push_uint32(ndr, NDR_SCALARS, W_ERROR_V(status));
396 _PUBLIC_ void ndr_print_WERROR(struct ndr_print *ndr, const char *name, WERROR r)
398 ndr->print(ndr, "%-25s: %s", name, win_errstr(r));
404 _PUBLIC_ enum ndr_err_code ndr_pull_bytes(struct ndr_pull *ndr, uint8_t *data, uint32_t n)
406 NDR_PULL_NEED_BYTES(ndr, n);
407 memcpy(data, ndr->data + ndr->offset, n);
409 return NDR_ERR_SUCCESS;
413 pull an array of uint8
415 _PUBLIC_ enum ndr_err_code ndr_pull_array_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *data, uint32_t n)
417 if (!(ndr_flags & NDR_SCALARS)) {
418 return NDR_ERR_SUCCESS;
420 return ndr_pull_bytes(ndr, data, n);
426 _PUBLIC_ enum ndr_err_code ndr_push_int8(struct ndr_push *ndr, int ndr_flags, int8_t v)
428 NDR_PUSH_NEED_BYTES(ndr, 1);
429 SCVAL(ndr->data, ndr->offset, (uint8_t)v);
431 return NDR_ERR_SUCCESS;
437 _PUBLIC_ enum ndr_err_code ndr_push_uint8(struct ndr_push *ndr, int ndr_flags, uint8_t v)
439 NDR_PUSH_NEED_BYTES(ndr, 1);
440 SCVAL(ndr->data, ndr->offset, v);
442 return NDR_ERR_SUCCESS;
448 _PUBLIC_ enum ndr_err_code ndr_push_int16(struct ndr_push *ndr, int ndr_flags, int16_t v)
450 NDR_PUSH_ALIGN(ndr, 2);
451 NDR_PUSH_NEED_BYTES(ndr, 2);
452 NDR_SSVAL(ndr, ndr->offset, (uint16_t)v);
454 return NDR_ERR_SUCCESS;
460 _PUBLIC_ enum ndr_err_code ndr_push_uint16(struct ndr_push *ndr, int ndr_flags, uint16_t v)
462 NDR_PUSH_ALIGN(ndr, 2);
463 NDR_PUSH_NEED_BYTES(ndr, 2);
464 NDR_SSVAL(ndr, ndr->offset, v);
466 return NDR_ERR_SUCCESS;
472 _PUBLIC_ enum ndr_err_code ndr_push_uint1632(struct ndr_push *ndr, int ndr_flags, uint16_t v)
474 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
475 return ndr_push_uint32(ndr, ndr_flags, v);
477 return ndr_push_uint16(ndr, ndr_flags, v);
483 _PUBLIC_ enum ndr_err_code ndr_push_int32(struct ndr_push *ndr, int ndr_flags, int32_t v)
485 NDR_PUSH_ALIGN(ndr, 4);
486 NDR_PUSH_NEED_BYTES(ndr, 4);
487 NDR_SIVALS(ndr, ndr->offset, v);
489 return NDR_ERR_SUCCESS;
495 _PUBLIC_ enum ndr_err_code ndr_push_uint32(struct ndr_push *ndr, int ndr_flags, uint32_t v)
497 NDR_PUSH_ALIGN(ndr, 4);
498 NDR_PUSH_NEED_BYTES(ndr, 4);
499 NDR_SIVAL(ndr, ndr->offset, v);
501 return NDR_ERR_SUCCESS;
507 _PUBLIC_ enum ndr_err_code ndr_push_uint3264(struct ndr_push *ndr, int ndr_flags, uint32_t v)
509 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
510 return ndr_push_hyper(ndr, ndr_flags, v);
512 return ndr_push_uint32(ndr, ndr_flags, v);
518 _PUBLIC_ enum ndr_err_code ndr_push_udlong(struct ndr_push *ndr, int ndr_flags, uint64_t v)
520 NDR_PUSH_ALIGN(ndr, 4);
521 NDR_PUSH_NEED_BYTES(ndr, 8);
522 NDR_SIVAL(ndr, ndr->offset, (v & 0xFFFFFFFF));
523 NDR_SIVAL(ndr, ndr->offset+4, (v>>32));
525 return NDR_ERR_SUCCESS;
531 _PUBLIC_ enum ndr_err_code ndr_push_udlongr(struct ndr_push *ndr, int ndr_flags, uint64_t v)
533 NDR_PUSH_ALIGN(ndr, 4);
534 NDR_PUSH_NEED_BYTES(ndr, 8);
535 NDR_SIVAL(ndr, ndr->offset, (v>>32));
536 NDR_SIVAL(ndr, ndr->offset+4, (v & 0xFFFFFFFF));
538 return NDR_ERR_SUCCESS;
544 _PUBLIC_ enum ndr_err_code ndr_push_dlong(struct ndr_push *ndr, int ndr_flags, int64_t v)
546 return ndr_push_udlong(ndr, NDR_SCALARS, (uint64_t)v);
552 _PUBLIC_ enum ndr_err_code ndr_push_hyper(struct ndr_push *ndr, int ndr_flags, uint64_t v)
554 NDR_PUSH_ALIGN(ndr, 8);
556 return ndr_push_udlongr(ndr, NDR_SCALARS, v);
558 return ndr_push_udlong(ndr, NDR_SCALARS, v);
564 _PUBLIC_ enum ndr_err_code ndr_push_double(struct ndr_push *ndr, int ndr_flags, double v)
566 NDR_PUSH_ALIGN(ndr, 8);
567 NDR_PUSH_NEED_BYTES(ndr, 8);
568 memcpy(ndr->data+ndr->offset, &v, 8);
570 return NDR_ERR_SUCCESS;
576 _PUBLIC_ enum ndr_err_code ndr_push_pointer(struct ndr_push *ndr, int ndr_flags, void* v)
578 uintptr_t h = (intptr_t)v;
579 NDR_PUSH_ALIGN(ndr, sizeof(h));
580 NDR_PUSH_NEED_BYTES(ndr, sizeof(h));
581 memcpy(ndr->data+ndr->offset, &h, sizeof(h));
582 ndr->offset += sizeof(h);
583 return NDR_ERR_SUCCESS;
586 _PUBLIC_ enum ndr_err_code ndr_push_align(struct ndr_push *ndr, size_t size)
588 /* this is a nasty hack to make pidl work with NDR64 */
590 if (ndr->flags & LIBNDR_FLAG_NDR64) {
595 } else if (size == 3) {
596 if (ndr->flags & LIBNDR_FLAG_NDR64) {
602 NDR_PUSH_ALIGN(ndr, size);
603 return NDR_ERR_SUCCESS;
606 _PUBLIC_ enum ndr_err_code ndr_pull_align(struct ndr_pull *ndr, size_t size)
608 /* this is a nasty hack to make pidl work with NDR64 */
610 if (ndr->flags & LIBNDR_FLAG_NDR64) {
615 } else if (size == 3) {
616 if (ndr->flags & LIBNDR_FLAG_NDR64) {
622 NDR_PULL_ALIGN(ndr, size);
623 return NDR_ERR_SUCCESS;
626 _PUBLIC_ enum ndr_err_code ndr_push_union_align(struct ndr_push *ndr, size_t size)
628 /* MS-RPCE section 2.2.5.3.4.4 */
629 if (ndr->flags & LIBNDR_FLAG_NDR64) {
630 return ndr_push_align(ndr, size);
632 return NDR_ERR_SUCCESS;
635 _PUBLIC_ enum ndr_err_code ndr_pull_union_align(struct ndr_pull *ndr, size_t size)
637 /* MS-RPCE section 2.2.5.3.4.4 */
638 if (ndr->flags & LIBNDR_FLAG_NDR64) {
639 return ndr_pull_align(ndr, size);
641 return NDR_ERR_SUCCESS;
644 _PUBLIC_ enum ndr_err_code ndr_push_trailer_align(struct ndr_push *ndr, size_t size)
646 /* MS-RPCE section 2.2.5.3.4.1 */
647 if (ndr->flags & LIBNDR_FLAG_NDR64) {
648 return ndr_push_align(ndr, size);
650 return NDR_ERR_SUCCESS;
653 _PUBLIC_ enum ndr_err_code ndr_pull_trailer_align(struct ndr_pull *ndr, size_t size)
655 /* MS-RPCE section 2.2.5.3.4.1 */
656 if (ndr->flags & LIBNDR_FLAG_NDR64) {
657 return ndr_pull_align(ndr, size);
659 return NDR_ERR_SUCCESS;
665 _PUBLIC_ enum ndr_err_code ndr_push_bytes(struct ndr_push *ndr, const uint8_t *data, uint32_t n)
667 NDR_PUSH_NEED_BYTES(ndr, n);
668 memcpy(ndr->data + ndr->offset, data, n);
670 return NDR_ERR_SUCCESS;
676 _PUBLIC_ enum ndr_err_code ndr_push_zero(struct ndr_push *ndr, uint32_t n)
678 NDR_PUSH_NEED_BYTES(ndr, n);
679 memset(ndr->data + ndr->offset, 0, n);
681 return NDR_ERR_SUCCESS;
685 push an array of uint8
687 _PUBLIC_ enum ndr_err_code ndr_push_array_uint8(struct ndr_push *ndr, int ndr_flags, const uint8_t *data, uint32_t n)
689 if (!(ndr_flags & NDR_SCALARS)) {
690 return NDR_ERR_SUCCESS;
692 return ndr_push_bytes(ndr, data, n);
696 push a unique non-zero value if a pointer is non-NULL, otherwise 0
698 _PUBLIC_ enum ndr_err_code ndr_push_unique_ptr(struct ndr_push *ndr, const void *p)
702 ptr = ndr->ptr_count * 4;
706 return ndr_push_uint3264(ndr, NDR_SCALARS, ptr);
710 push a 'simple' full non-zero value if a pointer is non-NULL, otherwise 0
712 _PUBLIC_ enum ndr_err_code ndr_push_full_ptr(struct ndr_push *ndr, const void *p)
716 /* Check if the pointer already exists and has an id */
717 ptr = ndr_token_peek(&ndr->full_ptr_list, p);
720 ptr = ndr->ptr_count;
721 ndr_token_store(ndr, &ndr->full_ptr_list, p, ptr);
724 return ndr_push_uint3264(ndr, NDR_SCALARS, ptr);
728 push always a 0, if a pointer is NULL it's a fatal error
730 _PUBLIC_ enum ndr_err_code ndr_push_ref_ptr(struct ndr_push *ndr)
732 return ndr_push_uint3264(ndr, NDR_SCALARS, 0xAEF1AEF1);
739 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME(struct ndr_push *ndr, int ndr_flags, NTTIME t)
741 NDR_CHECK(ndr_push_udlong(ndr, ndr_flags, t));
742 return NDR_ERR_SUCCESS;
748 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
750 NDR_CHECK(ndr_pull_udlong(ndr, ndr_flags, t));
751 return NDR_ERR_SUCCESS;
757 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME_1sec(struct ndr_push *ndr, int ndr_flags, NTTIME t)
760 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
761 return NDR_ERR_SUCCESS;
767 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME_1sec(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
769 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
771 return NDR_ERR_SUCCESS;
777 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME_hyper(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
779 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
780 return NDR_ERR_SUCCESS;
786 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME_hyper(struct ndr_push *ndr, int ndr_flags, NTTIME t)
788 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
789 return NDR_ERR_SUCCESS;
795 _PUBLIC_ enum ndr_err_code ndr_push_time_t(struct ndr_push *ndr, int ndr_flags, time_t t)
797 return ndr_push_uint32(ndr, ndr_flags, t);
803 _PUBLIC_ enum ndr_err_code ndr_pull_time_t(struct ndr_pull *ndr, int ndr_flags, time_t *t)
806 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &tt));
808 return NDR_ERR_SUCCESS;
815 _PUBLIC_ enum ndr_err_code ndr_pull_ipv4address(struct ndr_pull *ndr, int ndr_flags, const char **address)
819 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &addr));
820 in.s_addr = htonl(addr);
821 *address = talloc_strdup(ndr->current_mem_ctx, inet_ntoa(in));
822 NDR_ERR_HAVE_NO_MEMORY(*address);
823 return NDR_ERR_SUCCESS;
829 _PUBLIC_ enum ndr_err_code ndr_push_ipv4address(struct ndr_push *ndr, int ndr_flags, const char *address)
832 if (!is_ipaddress(address)) {
833 return ndr_push_error(ndr, NDR_ERR_IPV4ADDRESS,
834 "Invalid IPv4 address: '%s'",
837 addr = inet_addr(address);
838 NDR_CHECK(ndr_push_uint32(ndr, ndr_flags, htonl(addr)));
839 return NDR_ERR_SUCCESS;
845 _PUBLIC_ void ndr_print_ipv4address(struct ndr_print *ndr, const char *name,
848 ndr->print(ndr, "%-25s: %s", name, address);
854 #define IPV6_BYTES 16
855 #define IPV6_ADDR_STR_LEN 39
856 _PUBLIC_ enum ndr_err_code ndr_pull_ipv6address(struct ndr_pull *ndr, int ndr_flags, const char **address)
858 uint8_t addr[IPV6_BYTES];
859 char *addr_str = talloc_strdup(ndr->current_mem_ctx, "");
861 NDR_CHECK(ndr_pull_array_uint8(ndr, ndr_flags, addr, IPV6_BYTES));
862 for (i = 0; i < IPV6_BYTES; ++i) {
863 addr_str = talloc_asprintf_append(addr_str, "%02x", addr[i]);
864 /* We need a ':' every second byte but the last one */
865 if (i%2 == 1 && i != (IPV6_BYTES - 1)) {
866 addr_str = talloc_strdup_append(addr_str, ":");
870 NDR_ERR_HAVE_NO_MEMORY(*address);
871 return NDR_ERR_SUCCESS;
877 _PUBLIC_ enum ndr_err_code ndr_push_ipv6address(struct ndr_push *ndr, int ndr_flags, const char *address)
880 if (!is_ipaddress(address)) {
881 return ndr_push_error(ndr, NDR_ERR_IPV4ADDRESS,
882 "Invalid IPv6 address: '%s'",
885 addr = inet_addr(address);
886 NDR_CHECK(ndr_push_uint32(ndr, ndr_flags, htonl(addr)));
887 return NDR_ERR_SUCCESS;
893 _PUBLIC_ void ndr_print_ipv6address(struct ndr_print *ndr, const char *name,
896 ndr->print(ndr, "%-25s: %s", name, address);
900 _PUBLIC_ void ndr_print_struct(struct ndr_print *ndr, const char *name, const char *type)
902 ndr->print(ndr, "%s: struct %s", name, type);
905 _PUBLIC_ void ndr_print_null(struct ndr_print *ndr)
907 ndr->print(ndr, "UNEXPECTED NULL POINTER");
910 _PUBLIC_ void ndr_print_enum(struct ndr_print *ndr, const char *name, const char *type,
911 const char *val, uint32_t value)
913 if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
914 ndr->print(ndr, "%-25s: %s (0x%X)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
916 ndr->print(ndr, "%-25s: %s (%d)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
920 _PUBLIC_ void ndr_print_bitmap_flag(struct ndr_print *ndr, size_t size, const char *flag_name, uint32_t flag, uint32_t value)
922 /* this is an attempt to support multi-bit bitmap masks */
925 while (!(flag & 1)) {
930 ndr->print(ndr, " %d: %-25s", value, flag_name);
932 ndr->print(ndr, "0x%02x: %-25s (%d)", value, flag_name, value);
936 _PUBLIC_ void ndr_print_int8(struct ndr_print *ndr, const char *name, int8_t v)
938 ndr->print(ndr, "%-25s: %d", name, v);
941 _PUBLIC_ void ndr_print_uint8(struct ndr_print *ndr, const char *name, uint8_t v)
943 ndr->print(ndr, "%-25s: 0x%02x (%u)", name, v, v);
946 _PUBLIC_ void ndr_print_int16(struct ndr_print *ndr, const char *name, int16_t v)
948 ndr->print(ndr, "%-25s: %d", name, v);
951 _PUBLIC_ void ndr_print_uint16(struct ndr_print *ndr, const char *name, uint16_t v)
953 ndr->print(ndr, "%-25s: 0x%04x (%u)", name, v, v);
956 _PUBLIC_ void ndr_print_int32(struct ndr_print *ndr, const char *name, int32_t v)
958 ndr->print(ndr, "%-25s: %d", name, v);
961 _PUBLIC_ void ndr_print_uint32(struct ndr_print *ndr, const char *name, uint32_t v)
963 ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
966 _PUBLIC_ void ndr_print_int3264(struct ndr_print *ndr, const char *name, int32_t v)
968 ndr->print(ndr, "%-25s: %d", name, v);
971 _PUBLIC_ void ndr_print_uint3264(struct ndr_print *ndr, const char *name, uint32_t v)
973 ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
976 _PUBLIC_ void ndr_print_udlong(struct ndr_print *ndr, const char *name, uint64_t v)
978 ndr->print(ndr, "%-25s: 0x%016llx (%llu)", name, (unsigned long long)v, (unsigned long long)v);
981 _PUBLIC_ void ndr_print_udlongr(struct ndr_print *ndr, const char *name, uint64_t v)
983 ndr_print_udlong(ndr, name, v);
986 _PUBLIC_ void ndr_print_dlong(struct ndr_print *ndr, const char *name, int64_t v)
988 ndr->print(ndr, "%-25s: 0x%016llx (%lld)", name, (unsigned long long)v, (long long)v);
991 _PUBLIC_ void ndr_print_double(struct ndr_print *ndr, const char *name, double v)
993 ndr->print(ndr, "%-25s: %f", name, v);
996 _PUBLIC_ void ndr_print_hyper(struct ndr_print *ndr, const char *name, uint64_t v)
998 ndr_print_dlong(ndr, name, v);
1001 _PUBLIC_ void ndr_print_pointer(struct ndr_print *ndr, const char *name, void *v)
1003 ndr->print(ndr, "%-25s: %p", name, v);
1006 _PUBLIC_ void ndr_print_ptr(struct ndr_print *ndr, const char *name, const void *p)
1009 ndr->print(ndr, "%-25s: *", name);
1011 ndr->print(ndr, "%-25s: NULL", name);
1015 _PUBLIC_ void ndr_print_NTTIME(struct ndr_print *ndr, const char *name, NTTIME t)
1017 ndr->print(ndr, "%-25s: %s", name, nt_time_string(ndr, t));
1020 _PUBLIC_ void ndr_print_NTTIME_1sec(struct ndr_print *ndr, const char *name, NTTIME t)
1022 /* this is a standard NTTIME here
1023 * as it's already converted in the pull/push code
1025 ndr_print_NTTIME(ndr, name, t);
1028 _PUBLIC_ void ndr_print_NTTIME_hyper(struct ndr_print *ndr, const char *name, NTTIME t)
1030 ndr_print_NTTIME(ndr, name, t);
1033 _PUBLIC_ void ndr_print_time_t(struct ndr_print *ndr, const char *name, time_t t)
1035 if (t == (time_t)-1 || t == 0) {
1036 ndr->print(ndr, "%-25s: (time_t)%d", name, (int)t);
1038 ndr->print(ndr, "%-25s: %s", name, timestring(ndr, t));
1042 _PUBLIC_ void ndr_print_union(struct ndr_print *ndr, const char *name, int level, const char *type)
1044 if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
1045 ndr->print(ndr, "%-25s: union %s(case 0x%X)", name, type, level);
1047 ndr->print(ndr, "%-25s: union %s(case %d)", name, type, level);
1051 _PUBLIC_ void ndr_print_bad_level(struct ndr_print *ndr, const char *name, uint16_t level)
1053 ndr->print(ndr, "UNKNOWN LEVEL %u", level);
1056 _PUBLIC_ void ndr_print_array_uint8(struct ndr_print *ndr, const char *name,
1057 const uint8_t *data, uint32_t count)
1062 ndr->print(ndr, "%s: ARRAY(%d) : NULL", name, count);
1066 if (count <= 600 && (ndr->flags & LIBNDR_PRINT_ARRAY_HEX)) {
1068 for (i=0;i<count;i++) {
1069 snprintf(&s[i*2], 3, "%02x", data[i]);
1072 ndr->print(ndr, "%-25s: %s", name, s);
1076 ndr->print(ndr, "%s: ARRAY(%d)", name, count);
1078 for (i=0;i<count;i++) {
1080 if (asprintf(&idx, "[%d]", i) != -1) {
1081 ndr_print_uint8(ndr, idx, data[i]);
1088 static void ndr_print_asc(struct ndr_print *ndr, const uint8_t *buf, int len)
1092 ndr->print(ndr, "%c", isprint(buf[i])?buf[i]:'.');
1096 ndr_print version of dump_data()
1098 static void ndr_dump_data(struct ndr_print *ndr, const uint8_t *buf, int len)
1102 ndr->no_newline = true;
1105 if (i%16 == 0 && i<len) {
1106 ndr->print(ndr, "[%04X] ",i);
1109 ndr->print(ndr, "%02X ",(int)buf[i]);
1111 if (i%8 == 0) ndr->print(ndr," ");
1113 ndr_print_asc(ndr,&buf[i-16],8); ndr->print(ndr," ");
1114 ndr_print_asc(ndr,&buf[i-8],8); ndr->print(ndr, "\n");
1121 ndr->print(ndr, " ");
1122 if (n>8) ndr->print(ndr," ");
1123 while (n--) ndr->print(ndr," ");
1125 ndr_print_asc(ndr,&buf[i-(i%16)],n); ndr->print(ndr, " ");
1127 if (n>0) ndr_print_asc(ndr,&buf[i-n],n);
1128 ndr->print(ndr,"\n");
1131 ndr->no_newline = false;
1135 _PUBLIC_ void ndr_print_DATA_BLOB(struct ndr_print *ndr, const char *name, DATA_BLOB r)
1137 ndr->print(ndr, "%-25s: DATA_BLOB length=%u", name, (unsigned)r.length);
1139 ndr_dump_data(ndr, r.data, r.length);
1145 push a DATA_BLOB onto the wire.
1147 _PUBLIC_ enum ndr_err_code ndr_push_DATA_BLOB(struct ndr_push *ndr, int ndr_flags, DATA_BLOB blob)
1149 if (ndr->flags & LIBNDR_ALIGN_FLAGS) {
1150 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1151 blob.length = NDR_ALIGN(ndr, 2);
1152 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1153 blob.length = NDR_ALIGN(ndr, 4);
1154 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1155 blob.length = NDR_ALIGN(ndr, 8);
1157 NDR_PUSH_ALLOC_SIZE(ndr, blob.data, blob.length);
1158 data_blob_clear(&blob);
1159 } else if (!(ndr->flags & LIBNDR_FLAG_REMAINING)) {
1160 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, blob.length));
1162 NDR_CHECK(ndr_push_bytes(ndr, blob.data, blob.length));
1163 return NDR_ERR_SUCCESS;
1167 pull a DATA_BLOB from the wire.
1169 _PUBLIC_ enum ndr_err_code ndr_pull_DATA_BLOB(struct ndr_pull *ndr, int ndr_flags, DATA_BLOB *blob)
1171 uint32_t length = 0;
1173 if (ndr->flags & LIBNDR_ALIGN_FLAGS) {
1174 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1175 length = NDR_ALIGN(ndr, 2);
1176 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1177 length = NDR_ALIGN(ndr, 4);
1178 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1179 length = NDR_ALIGN(ndr, 8);
1181 if (ndr->data_size - ndr->offset < length) {
1182 length = ndr->data_size - ndr->offset;
1184 } else if (ndr->flags & LIBNDR_FLAG_REMAINING) {
1185 length = ndr->data_size - ndr->offset;
1187 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &length));
1189 NDR_PULL_NEED_BYTES(ndr, length);
1190 *blob = data_blob_talloc(ndr->current_mem_ctx, ndr->data+ndr->offset, length);
1191 ndr->offset += length;
1192 return NDR_ERR_SUCCESS;
1195 _PUBLIC_ uint32_t ndr_size_DATA_BLOB(int ret, const DATA_BLOB *data, int flags)
1197 if (!data) return ret;
1198 return ret + data->length;