WHATSNEW: Update changes.
[Samba/bb.git] / librpc / ndr / ndr_basic.c
blob50a89223089a8e71c45d421bb912477a826676c9
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 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/>.
22 #include "includes.h"
23 #include "system/network.h"
24 #include "librpc/ndr/libndr.h"
26 #define NDR_SVAL(ndr, ofs) (NDR_BE(ndr)?RSVAL(ndr->data,ofs):SVAL(ndr->data,ofs))
27 #define NDR_IVAL(ndr, ofs) (NDR_BE(ndr)?RIVAL(ndr->data,ofs):IVAL(ndr->data,ofs))
28 #define NDR_IVALS(ndr, ofs) (NDR_BE(ndr)?RIVALS(ndr->data,ofs):IVALS(ndr->data,ofs))
29 #define NDR_SSVAL(ndr, ofs, v) do { if (NDR_BE(ndr)) { RSSVAL(ndr->data,ofs,v); } else SSVAL(ndr->data,ofs,v); } while (0)
30 #define NDR_SIVAL(ndr, ofs, v) do { if (NDR_BE(ndr)) { RSIVAL(ndr->data,ofs,v); } else SIVAL(ndr->data,ofs,v); } while (0)
31 #define NDR_SIVALS(ndr, ofs, v) do { if (NDR_BE(ndr)) { RSIVALS(ndr->data,ofs,v); } else SIVALS(ndr->data,ofs,v); } while (0)
35 check for data leaks from the server by looking for non-zero pad bytes
36 these could also indicate that real structure elements have been
37 mistaken for padding in the IDL
39 _PUBLIC_ void ndr_check_padding(struct ndr_pull *ndr, size_t n)
41 size_t ofs2 = (ndr->offset + (n-1)) & ~(n-1);
42 int i;
43 for (i=ndr->offset;i<ofs2;i++) {
44 if (ndr->data[i] != 0) {
45 break;
48 if (i<ofs2) {
49 DEBUG(0,("WARNING: Non-zero padding to %d: ", (int)n));
50 for (i=ndr->offset;i<ofs2;i++) {
51 DEBUG(0,("%02x ", ndr->data[i]));
53 DEBUG(0,("\n"));
59 parse a int8_t
61 _PUBLIC_ enum ndr_err_code ndr_pull_int8(struct ndr_pull *ndr, int ndr_flags, int8_t *v)
63 NDR_PULL_NEED_BYTES(ndr, 1);
64 *v = (int8_t)CVAL(ndr->data, ndr->offset);
65 ndr->offset += 1;
66 return NDR_ERR_SUCCESS;
70 parse a uint8_t
72 _PUBLIC_ enum ndr_err_code ndr_pull_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *v)
74 NDR_PULL_NEED_BYTES(ndr, 1);
75 *v = CVAL(ndr->data, ndr->offset);
76 ndr->offset += 1;
77 return NDR_ERR_SUCCESS;
81 parse a int16_t
83 _PUBLIC_ enum ndr_err_code ndr_pull_int16(struct ndr_pull *ndr, int ndr_flags, int16_t *v)
85 NDR_PULL_ALIGN(ndr, 2);
86 NDR_PULL_NEED_BYTES(ndr, 2);
87 *v = (uint16_t)NDR_SVAL(ndr, ndr->offset);
88 ndr->offset += 2;
89 return NDR_ERR_SUCCESS;
93 parse a uint16_t
95 _PUBLIC_ enum ndr_err_code ndr_pull_uint16(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
97 NDR_PULL_ALIGN(ndr, 2);
98 NDR_PULL_NEED_BYTES(ndr, 2);
99 *v = NDR_SVAL(ndr, ndr->offset);
100 ndr->offset += 2;
101 return NDR_ERR_SUCCESS;
105 parse a uint1632_t
107 _PUBLIC_ enum ndr_err_code ndr_pull_uint1632(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
109 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
110 uint32_t v32 = 0;
111 enum ndr_err_code err = ndr_pull_uint32(ndr, ndr_flags, &v32);
112 *v = v32;
113 if (unlikely(v32 != *v)) {
114 DEBUG(0,(__location__ ": non-zero upper 16 bits 0x%08x\n", (unsigned)v32));
115 return NDR_ERR_NDR64;
117 return err;
119 return ndr_pull_uint16(ndr, ndr_flags, v);
123 parse a int32_t
125 _PUBLIC_ enum ndr_err_code ndr_pull_int32(struct ndr_pull *ndr, int ndr_flags, int32_t *v)
127 NDR_PULL_ALIGN(ndr, 4);
128 NDR_PULL_NEED_BYTES(ndr, 4);
129 *v = NDR_IVALS(ndr, ndr->offset);
130 ndr->offset += 4;
131 return NDR_ERR_SUCCESS;
135 parse a uint32_t
137 _PUBLIC_ enum ndr_err_code ndr_pull_uint32(struct ndr_pull *ndr, int ndr_flags, uint32_t *v)
139 NDR_PULL_ALIGN(ndr, 4);
140 NDR_PULL_NEED_BYTES(ndr, 4);
141 *v = NDR_IVAL(ndr, ndr->offset);
142 ndr->offset += 4;
143 return NDR_ERR_SUCCESS;
147 parse a arch dependent uint32/uint64
149 _PUBLIC_ enum ndr_err_code ndr_pull_uint3264(struct ndr_pull *ndr, int ndr_flags, uint32_t *v)
151 uint64_t v64;
152 enum ndr_err_code err;
153 if (likely(!(ndr->flags & LIBNDR_FLAG_NDR64))) {
154 return ndr_pull_uint32(ndr, ndr_flags, v);
156 err = ndr_pull_hyper(ndr, ndr_flags, &v64);
157 *v = (uint32_t)v64;
158 if (unlikely(v64 != *v)) {
159 DEBUG(0,(__location__ ": non-zero upper 32 bits 0x%016llx\n",
160 (unsigned long long)v64));
161 return NDR_ERR_NDR64;
163 return err;
167 parse a double
169 _PUBLIC_ enum ndr_err_code ndr_pull_double(struct ndr_pull *ndr, int ndr_flags, double *v)
171 NDR_PULL_ALIGN(ndr, 8);
172 NDR_PULL_NEED_BYTES(ndr, 8);
173 memcpy(v, ndr->data+ndr->offset, 8);
174 ndr->offset += 8;
175 return NDR_ERR_SUCCESS;
179 parse a pointer referent identifier
181 _PUBLIC_ enum ndr_err_code ndr_pull_generic_ptr(struct ndr_pull *ndr, uint32_t *v)
183 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, v));
184 if (*v != 0) {
185 ndr->ptr_count++;
187 return NDR_ERR_SUCCESS;
191 parse a ref pointer referent identifier
193 _PUBLIC_ enum ndr_err_code ndr_pull_ref_ptr(struct ndr_pull *ndr, uint32_t *v)
195 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, v));
196 /* ref pointers always point to data */
197 *v = 1;
198 return NDR_ERR_SUCCESS;
202 parse a udlong
204 _PUBLIC_ enum ndr_err_code ndr_pull_udlong(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
206 NDR_PULL_ALIGN(ndr, 4);
207 NDR_PULL_NEED_BYTES(ndr, 8);
208 *v = NDR_IVAL(ndr, ndr->offset);
209 *v |= (uint64_t)(NDR_IVAL(ndr, ndr->offset+4)) << 32;
210 ndr->offset += 8;
211 return NDR_ERR_SUCCESS;
215 parse a udlongr
217 _PUBLIC_ enum ndr_err_code ndr_pull_udlongr(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
219 NDR_PULL_ALIGN(ndr, 4);
220 NDR_PULL_NEED_BYTES(ndr, 8);
221 *v = ((uint64_t)NDR_IVAL(ndr, ndr->offset)) << 32;
222 *v |= NDR_IVAL(ndr, ndr->offset+4);
223 ndr->offset += 8;
224 return NDR_ERR_SUCCESS;
228 parse a dlong
230 _PUBLIC_ enum ndr_err_code ndr_pull_dlong(struct ndr_pull *ndr, int ndr_flags, int64_t *v)
232 return ndr_pull_udlong(ndr, ndr_flags, (uint64_t *)v);
236 parse a hyper
238 _PUBLIC_ enum ndr_err_code ndr_pull_hyper(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
240 NDR_PULL_ALIGN(ndr, 8);
241 return ndr_pull_udlong(ndr, ndr_flags, v);
245 parse a pointer
247 _PUBLIC_ enum ndr_err_code ndr_pull_pointer(struct ndr_pull *ndr, int ndr_flags, void* *v)
249 uintptr_t h;
250 NDR_PULL_ALIGN(ndr, sizeof(h));
251 NDR_PULL_NEED_BYTES(ndr, sizeof(h));
252 memcpy(&h, ndr->data+ndr->offset, sizeof(h));
253 ndr->offset += sizeof(h);
254 *v = (void *)h;
255 return NDR_ERR_SUCCESS;
259 pull a NTSTATUS
261 _PUBLIC_ enum ndr_err_code ndr_pull_NTSTATUS(struct ndr_pull *ndr, int ndr_flags, NTSTATUS *status)
263 uint32_t v;
264 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
265 *status = NT_STATUS(v);
266 return NDR_ERR_SUCCESS;
270 push a NTSTATUS
272 _PUBLIC_ enum ndr_err_code ndr_push_NTSTATUS(struct ndr_push *ndr, int ndr_flags, NTSTATUS status)
274 return ndr_push_uint32(ndr, ndr_flags, NT_STATUS_V(status));
277 _PUBLIC_ void ndr_print_NTSTATUS(struct ndr_print *ndr, const char *name, NTSTATUS r)
279 ndr->print(ndr, "%-25s: %s", name, nt_errstr(r));
283 pull a WERROR
285 _PUBLIC_ enum ndr_err_code ndr_pull_WERROR(struct ndr_pull *ndr, int ndr_flags, WERROR *status)
287 uint32_t v;
288 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
289 *status = W_ERROR(v);
290 return NDR_ERR_SUCCESS;
295 parse a uint8_t enum
297 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *v)
299 return ndr_pull_uint8(ndr, ndr_flags, v);
303 parse a uint16_t enum
305 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint16(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
307 return ndr_pull_uint16(ndr, ndr_flags, v);
311 parse a uint1632_t enum (uint32_t on NDR64)
313 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint1632(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
315 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
316 uint32_t v32;
317 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &v32));
318 *v = v32;
319 if (v32 != *v) {
320 DEBUG(0,(__location__ ": non-zero upper 16 bits 0x%08x\n", (unsigned)v32));
321 return NDR_ERR_NDR64;
323 return NDR_ERR_SUCCESS;
325 return ndr_pull_uint16(ndr, ndr_flags, v);
329 parse a uint32_t enum
331 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint32(struct ndr_pull *ndr, int ndr_flags, uint32_t *v)
333 return ndr_pull_uint32(ndr, ndr_flags, v);
337 push a uint8_t enum
339 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint8(struct ndr_push *ndr, int ndr_flags, uint8_t v)
341 return ndr_push_uint8(ndr, ndr_flags, v);
345 push a uint16_t enum
347 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint16(struct ndr_push *ndr, int ndr_flags, uint16_t v)
349 return ndr_push_uint16(ndr, ndr_flags, v);
353 push a uint32_t enum
355 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint32(struct ndr_push *ndr, int ndr_flags, uint32_t v)
357 return ndr_push_uint32(ndr, ndr_flags, v);
361 push a uint1632_t enum
363 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint1632(struct ndr_push *ndr, int ndr_flags, uint16_t v)
365 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
366 return ndr_push_uint32(ndr, ndr_flags, v);
368 return ndr_push_uint16(ndr, ndr_flags, v);
372 push a WERROR
374 _PUBLIC_ enum ndr_err_code ndr_push_WERROR(struct ndr_push *ndr, int ndr_flags, WERROR status)
376 return ndr_push_uint32(ndr, NDR_SCALARS, W_ERROR_V(status));
379 _PUBLIC_ void ndr_print_WERROR(struct ndr_print *ndr, const char *name, WERROR r)
381 ndr->print(ndr, "%-25s: %s", name, win_errstr(r));
385 parse a set of bytes
387 _PUBLIC_ enum ndr_err_code ndr_pull_bytes(struct ndr_pull *ndr, uint8_t *data, uint32_t n)
389 NDR_PULL_NEED_BYTES(ndr, n);
390 memcpy(data, ndr->data + ndr->offset, n);
391 ndr->offset += n;
392 return NDR_ERR_SUCCESS;
396 pull an array of uint8
398 _PUBLIC_ enum ndr_err_code ndr_pull_array_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *data, uint32_t n)
400 if (!(ndr_flags & NDR_SCALARS)) {
401 return NDR_ERR_SUCCESS;
403 return ndr_pull_bytes(ndr, data, n);
407 push a int8_t
409 _PUBLIC_ enum ndr_err_code ndr_push_int8(struct ndr_push *ndr, int ndr_flags, int8_t v)
411 NDR_PUSH_NEED_BYTES(ndr, 1);
412 SCVAL(ndr->data, ndr->offset, (uint8_t)v);
413 ndr->offset += 1;
414 return NDR_ERR_SUCCESS;
418 push a uint8_t
420 _PUBLIC_ enum ndr_err_code ndr_push_uint8(struct ndr_push *ndr, int ndr_flags, uint8_t v)
422 NDR_PUSH_NEED_BYTES(ndr, 1);
423 SCVAL(ndr->data, ndr->offset, v);
424 ndr->offset += 1;
425 return NDR_ERR_SUCCESS;
429 push a int16_t
431 _PUBLIC_ enum ndr_err_code ndr_push_int16(struct ndr_push *ndr, int ndr_flags, int16_t v)
433 NDR_PUSH_ALIGN(ndr, 2);
434 NDR_PUSH_NEED_BYTES(ndr, 2);
435 NDR_SSVAL(ndr, ndr->offset, (uint16_t)v);
436 ndr->offset += 2;
437 return NDR_ERR_SUCCESS;
441 push a uint16_t
443 _PUBLIC_ enum ndr_err_code ndr_push_uint16(struct ndr_push *ndr, int ndr_flags, uint16_t v)
445 NDR_PUSH_ALIGN(ndr, 2);
446 NDR_PUSH_NEED_BYTES(ndr, 2);
447 NDR_SSVAL(ndr, ndr->offset, v);
448 ndr->offset += 2;
449 return NDR_ERR_SUCCESS;
453 push a uint1632
455 _PUBLIC_ enum ndr_err_code ndr_push_uint1632(struct ndr_push *ndr, int ndr_flags, uint16_t v)
457 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
458 return ndr_push_uint32(ndr, ndr_flags, v);
460 return ndr_push_uint16(ndr, ndr_flags, v);
464 push a int32_t
466 _PUBLIC_ enum ndr_err_code ndr_push_int32(struct ndr_push *ndr, int ndr_flags, int32_t v)
468 NDR_PUSH_ALIGN(ndr, 4);
469 NDR_PUSH_NEED_BYTES(ndr, 4);
470 NDR_SIVALS(ndr, ndr->offset, v);
471 ndr->offset += 4;
472 return NDR_ERR_SUCCESS;
476 push a uint32_t
478 _PUBLIC_ enum ndr_err_code ndr_push_uint32(struct ndr_push *ndr, int ndr_flags, uint32_t v)
480 NDR_PUSH_ALIGN(ndr, 4);
481 NDR_PUSH_NEED_BYTES(ndr, 4);
482 NDR_SIVAL(ndr, ndr->offset, v);
483 ndr->offset += 4;
484 return NDR_ERR_SUCCESS;
488 push a uint3264
490 _PUBLIC_ enum ndr_err_code ndr_push_uint3264(struct ndr_push *ndr, int ndr_flags, uint32_t v)
492 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
493 return ndr_push_hyper(ndr, ndr_flags, v);
495 return ndr_push_uint32(ndr, ndr_flags, v);
499 push a udlong
501 _PUBLIC_ enum ndr_err_code ndr_push_udlong(struct ndr_push *ndr, int ndr_flags, uint64_t v)
503 NDR_PUSH_ALIGN(ndr, 4);
504 NDR_PUSH_NEED_BYTES(ndr, 8);
505 NDR_SIVAL(ndr, ndr->offset, (v & 0xFFFFFFFF));
506 NDR_SIVAL(ndr, ndr->offset+4, (v>>32));
507 ndr->offset += 8;
508 return NDR_ERR_SUCCESS;
512 push a udlongr
514 _PUBLIC_ enum ndr_err_code ndr_push_udlongr(struct ndr_push *ndr, int ndr_flags, uint64_t v)
516 NDR_PUSH_ALIGN(ndr, 4);
517 NDR_PUSH_NEED_BYTES(ndr, 8);
518 NDR_SIVAL(ndr, ndr->offset, (v>>32));
519 NDR_SIVAL(ndr, ndr->offset+4, (v & 0xFFFFFFFF));
520 ndr->offset += 8;
521 return NDR_ERR_SUCCESS;
525 push a dlong
527 _PUBLIC_ enum ndr_err_code ndr_push_dlong(struct ndr_push *ndr, int ndr_flags, int64_t v)
529 return ndr_push_udlong(ndr, NDR_SCALARS, (uint64_t)v);
533 push a hyper
535 _PUBLIC_ enum ndr_err_code ndr_push_hyper(struct ndr_push *ndr, int ndr_flags, uint64_t v)
537 NDR_PUSH_ALIGN(ndr, 8);
538 return ndr_push_udlong(ndr, NDR_SCALARS, v);
542 push a double
544 _PUBLIC_ enum ndr_err_code ndr_push_double(struct ndr_push *ndr, int ndr_flags, double v)
546 NDR_PUSH_ALIGN(ndr, 8);
547 NDR_PUSH_NEED_BYTES(ndr, 8);
548 memcpy(ndr->data+ndr->offset, &v, 8);
549 ndr->offset += 8;
550 return NDR_ERR_SUCCESS;
554 push a pointer
556 _PUBLIC_ enum ndr_err_code ndr_push_pointer(struct ndr_push *ndr, int ndr_flags, void* v)
558 uintptr_t h = (intptr_t)v;
559 NDR_PUSH_ALIGN(ndr, sizeof(h));
560 NDR_PUSH_NEED_BYTES(ndr, sizeof(h));
561 memcpy(ndr->data+ndr->offset, &h, sizeof(h));
562 ndr->offset += sizeof(h);
563 return NDR_ERR_SUCCESS;
566 _PUBLIC_ enum ndr_err_code ndr_push_align(struct ndr_push *ndr, size_t size)
568 /* this is a nasty hack to make pidl work with NDR64 */
569 if (size == 5) {
570 if (ndr->flags & LIBNDR_FLAG_NDR64) {
571 size = 8;
572 } else {
573 size = 4;
575 } else if (size == 3) {
576 if (ndr->flags & LIBNDR_FLAG_NDR64) {
577 size = 4;
578 } else {
579 size = 2;
582 NDR_PUSH_ALIGN(ndr, size);
583 return NDR_ERR_SUCCESS;
586 _PUBLIC_ enum ndr_err_code ndr_pull_align(struct ndr_pull *ndr, size_t size)
588 /* this is a nasty hack to make pidl work with NDR64 */
589 if (size == 5) {
590 if (ndr->flags & LIBNDR_FLAG_NDR64) {
591 size = 8;
592 } else {
593 size = 4;
595 } else if (size == 3) {
596 if (ndr->flags & LIBNDR_FLAG_NDR64) {
597 size = 4;
598 } else {
599 size = 2;
602 NDR_PULL_ALIGN(ndr, size);
603 return NDR_ERR_SUCCESS;
606 _PUBLIC_ enum ndr_err_code ndr_push_union_align(struct ndr_push *ndr, size_t size)
608 /* MS-RPCE section 2.2.5.3.4.4 */
609 if (ndr->flags & LIBNDR_FLAG_NDR64) {
610 return ndr_push_align(ndr, size);
612 return NDR_ERR_SUCCESS;
615 _PUBLIC_ enum ndr_err_code ndr_pull_union_align(struct ndr_pull *ndr, size_t size)
617 /* MS-RPCE section 2.2.5.3.4.4 */
618 if (ndr->flags & LIBNDR_FLAG_NDR64) {
619 return ndr_pull_align(ndr, size);
621 return NDR_ERR_SUCCESS;
624 _PUBLIC_ enum ndr_err_code ndr_push_trailer_align(struct ndr_push *ndr, size_t size)
626 /* MS-RPCE section 2.2.5.3.4.1 */
627 if (ndr->flags & LIBNDR_FLAG_NDR64) {
628 return ndr_push_align(ndr, size);
630 return NDR_ERR_SUCCESS;
633 _PUBLIC_ enum ndr_err_code ndr_pull_trailer_align(struct ndr_pull *ndr, size_t size)
635 /* MS-RPCE section 2.2.5.3.4.1 */
636 if (ndr->flags & LIBNDR_FLAG_NDR64) {
637 return ndr_pull_align(ndr, size);
639 return NDR_ERR_SUCCESS;
643 push some bytes
645 _PUBLIC_ enum ndr_err_code ndr_push_bytes(struct ndr_push *ndr, const uint8_t *data, uint32_t n)
647 NDR_PUSH_NEED_BYTES(ndr, n);
648 memcpy(ndr->data + ndr->offset, data, n);
649 ndr->offset += n;
650 return NDR_ERR_SUCCESS;
654 push some zero bytes
656 _PUBLIC_ enum ndr_err_code ndr_push_zero(struct ndr_push *ndr, uint32_t n)
658 NDR_PUSH_NEED_BYTES(ndr, n);
659 memset(ndr->data + ndr->offset, 0, n);
660 ndr->offset += n;
661 return NDR_ERR_SUCCESS;
665 push an array of uint8
667 _PUBLIC_ enum ndr_err_code ndr_push_array_uint8(struct ndr_push *ndr, int ndr_flags, const uint8_t *data, uint32_t n)
669 if (!(ndr_flags & NDR_SCALARS)) {
670 return NDR_ERR_SUCCESS;
672 return ndr_push_bytes(ndr, data, n);
676 push a unique non-zero value if a pointer is non-NULL, otherwise 0
678 _PUBLIC_ enum ndr_err_code ndr_push_unique_ptr(struct ndr_push *ndr, const void *p)
680 uint32_t ptr = 0;
681 if (p) {
682 ptr = ndr->ptr_count * 4;
683 ptr |= 0x00020000;
684 ndr->ptr_count++;
686 return ndr_push_uint3264(ndr, NDR_SCALARS, ptr);
690 push a 'simple' full non-zero value if a pointer is non-NULL, otherwise 0
692 _PUBLIC_ enum ndr_err_code ndr_push_full_ptr(struct ndr_push *ndr, const void *p)
694 uint32_t ptr = 0;
695 if (p) {
696 /* Check if the pointer already exists and has an id */
697 ptr = ndr_token_peek(&ndr->full_ptr_list, p);
698 if (ptr == 0) {
699 ndr->ptr_count++;
700 ptr = ndr->ptr_count;
701 ndr_token_store(ndr, &ndr->full_ptr_list, p, ptr);
704 return ndr_push_uint3264(ndr, NDR_SCALARS, ptr);
708 push always a 0, if a pointer is NULL it's a fatal error
710 _PUBLIC_ enum ndr_err_code ndr_push_ref_ptr(struct ndr_push *ndr)
712 return ndr_push_uint3264(ndr, NDR_SCALARS, 0xAEF1AEF1);
717 push a NTTIME
719 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME(struct ndr_push *ndr, int ndr_flags, NTTIME t)
721 NDR_CHECK(ndr_push_udlong(ndr, ndr_flags, t));
722 return NDR_ERR_SUCCESS;
726 pull a NTTIME
728 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
730 NDR_CHECK(ndr_pull_udlong(ndr, ndr_flags, t));
731 return NDR_ERR_SUCCESS;
735 push a NTTIME_1sec
737 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME_1sec(struct ndr_push *ndr, int ndr_flags, NTTIME t)
739 t /= 10000000;
740 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
741 return NDR_ERR_SUCCESS;
745 pull a NTTIME_1sec
747 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME_1sec(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
749 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
750 (*t) *= 10000000;
751 return NDR_ERR_SUCCESS;
755 pull a NTTIME_hyper
757 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME_hyper(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
759 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
760 return NDR_ERR_SUCCESS;
764 push a NTTIME_hyper
766 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME_hyper(struct ndr_push *ndr, int ndr_flags, NTTIME t)
768 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
769 return NDR_ERR_SUCCESS;
773 push a time_t
775 _PUBLIC_ enum ndr_err_code ndr_push_time_t(struct ndr_push *ndr, int ndr_flags, time_t t)
777 return ndr_push_uint32(ndr, ndr_flags, t);
781 pull a time_t
783 _PUBLIC_ enum ndr_err_code ndr_pull_time_t(struct ndr_pull *ndr, int ndr_flags, time_t *t)
785 uint32_t tt;
786 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &tt));
787 *t = tt;
788 return NDR_ERR_SUCCESS;
793 pull a ipv4address
795 _PUBLIC_ enum ndr_err_code ndr_pull_ipv4address(struct ndr_pull *ndr, int ndr_flags, const char **address)
797 uint32_t addr;
798 struct in_addr in;
799 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &addr));
800 in.s_addr = htonl(addr);
801 *address = talloc_strdup(ndr->current_mem_ctx, inet_ntoa(in));
802 NDR_ERR_HAVE_NO_MEMORY(*address);
803 return NDR_ERR_SUCCESS;
807 push a ipv4address
809 _PUBLIC_ enum ndr_err_code ndr_push_ipv4address(struct ndr_push *ndr, int ndr_flags, const char *address)
811 uint32_t addr;
812 if (!is_ipaddress(address)) {
813 return ndr_push_error(ndr, NDR_ERR_IPV4ADDRESS,
814 "Invalid IPv4 address: '%s'",
815 address);
817 addr = inet_addr(address);
818 NDR_CHECK(ndr_push_uint32(ndr, ndr_flags, htonl(addr)));
819 return NDR_ERR_SUCCESS;
823 print a ipv4address
825 _PUBLIC_ void ndr_print_ipv4address(struct ndr_print *ndr, const char *name,
826 const char *address)
828 ndr->print(ndr, "%-25s: %s", name, address);
832 _PUBLIC_ void ndr_print_struct(struct ndr_print *ndr, const char *name, const char *type)
834 ndr->print(ndr, "%s: struct %s", name, type);
837 _PUBLIC_ void ndr_print_enum(struct ndr_print *ndr, const char *name, const char *type,
838 const char *val, uint32_t value)
840 if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
841 ndr->print(ndr, "%-25s: %s (0x%X)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
842 } else {
843 ndr->print(ndr, "%-25s: %s (%d)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
847 _PUBLIC_ void ndr_print_bitmap_flag(struct ndr_print *ndr, size_t size, const char *flag_name, uint32_t flag, uint32_t value)
849 /* this is an attempt to support multi-bit bitmap masks */
850 value &= flag;
852 while (!(flag & 1)) {
853 flag >>= 1;
854 value >>= 1;
856 if (flag == 1) {
857 ndr->print(ndr, " %d: %-25s", value, flag_name);
858 } else {
859 ndr->print(ndr, "0x%02x: %-25s (%d)", value, flag_name, value);
863 _PUBLIC_ void ndr_print_int8(struct ndr_print *ndr, const char *name, int8_t v)
865 ndr->print(ndr, "%-25s: %d", name, v);
868 _PUBLIC_ void ndr_print_uint8(struct ndr_print *ndr, const char *name, uint8_t v)
870 ndr->print(ndr, "%-25s: 0x%02x (%u)", name, v, v);
873 _PUBLIC_ void ndr_print_int16(struct ndr_print *ndr, const char *name, int16_t v)
875 ndr->print(ndr, "%-25s: %d", name, v);
878 _PUBLIC_ void ndr_print_uint16(struct ndr_print *ndr, const char *name, uint16_t v)
880 ndr->print(ndr, "%-25s: 0x%04x (%u)", name, v, v);
883 _PUBLIC_ void ndr_print_int32(struct ndr_print *ndr, const char *name, int32_t v)
885 ndr->print(ndr, "%-25s: %d", name, v);
888 _PUBLIC_ void ndr_print_uint32(struct ndr_print *ndr, const char *name, uint32_t v)
890 ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
893 _PUBLIC_ void ndr_print_udlong(struct ndr_print *ndr, const char *name, uint64_t v)
895 ndr->print(ndr, "%-25s: 0x%016llx (%llu)", name, (unsigned long long)v, (unsigned long long)v);
898 _PUBLIC_ void ndr_print_udlongr(struct ndr_print *ndr, const char *name, uint64_t v)
900 ndr_print_udlong(ndr, name, v);
903 _PUBLIC_ void ndr_print_dlong(struct ndr_print *ndr, const char *name, int64_t v)
905 ndr->print(ndr, "%-25s: 0x%016llx (%lld)", name, (unsigned long long)v, (long long)v);
908 _PUBLIC_ void ndr_print_double(struct ndr_print *ndr, const char *name, double v)
910 ndr->print(ndr, "%-25s: %f", name, v);
913 _PUBLIC_ void ndr_print_hyper(struct ndr_print *ndr, const char *name, uint64_t v)
915 ndr_print_dlong(ndr, name, v);
918 _PUBLIC_ void ndr_print_pointer(struct ndr_print *ndr, const char *name, void *v)
920 ndr->print(ndr, "%-25s: %p", name, v);
923 _PUBLIC_ void ndr_print_ptr(struct ndr_print *ndr, const char *name, const void *p)
925 if (p) {
926 ndr->print(ndr, "%-25s: *", name);
927 } else {
928 ndr->print(ndr, "%-25s: NULL", name);
932 _PUBLIC_ void ndr_print_NTTIME(struct ndr_print *ndr, const char *name, NTTIME t)
934 ndr->print(ndr, "%-25s: %s", name, nt_time_string(ndr, t));
937 _PUBLIC_ void ndr_print_NTTIME_1sec(struct ndr_print *ndr, const char *name, NTTIME t)
939 /* this is a standard NTTIME here
940 * as it's already converted in the pull/push code
942 ndr_print_NTTIME(ndr, name, t);
945 _PUBLIC_ void ndr_print_NTTIME_hyper(struct ndr_print *ndr, const char *name, NTTIME t)
947 ndr_print_NTTIME(ndr, name, t);
950 _PUBLIC_ void ndr_print_time_t(struct ndr_print *ndr, const char *name, time_t t)
952 if (t == (time_t)-1 || t == 0) {
953 ndr->print(ndr, "%-25s: (time_t)%d", name, (int)t);
954 } else {
955 ndr->print(ndr, "%-25s: %s", name, timestring(ndr, t));
959 _PUBLIC_ void ndr_print_union(struct ndr_print *ndr, const char *name, int level, const char *type)
961 if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
962 ndr->print(ndr, "%-25s: union %s(case 0x%X)", name, type, level);
963 } else {
964 ndr->print(ndr, "%-25s: union %s(case %d)", name, type, level);
968 _PUBLIC_ void ndr_print_bad_level(struct ndr_print *ndr, const char *name, uint16_t level)
970 ndr->print(ndr, "UNKNOWN LEVEL %u", level);
973 _PUBLIC_ void ndr_print_array_uint8(struct ndr_print *ndr, const char *name,
974 const uint8_t *data, uint32_t count)
976 int i;
978 if (count <= 600 && (ndr->flags & LIBNDR_PRINT_ARRAY_HEX)) {
979 char s[1202];
980 for (i=0;i<count;i++) {
981 snprintf(&s[i*2], 3, "%02x", data[i]);
983 s[i*2] = 0;
984 ndr->print(ndr, "%-25s: %s", name, s);
985 return;
988 ndr->print(ndr, "%s: ARRAY(%d)", name, count);
989 ndr->depth++;
990 for (i=0;i<count;i++) {
991 char *idx=NULL;
992 if (asprintf(&idx, "[%d]", i) != -1) {
993 ndr_print_uint8(ndr, idx, data[i]);
994 free(idx);
997 ndr->depth--;
1000 _PUBLIC_ void ndr_print_DATA_BLOB(struct ndr_print *ndr, const char *name, DATA_BLOB r)
1002 ndr->print(ndr, "%-25s: DATA_BLOB length=%u", name, (unsigned)r.length);
1003 if (r.length) {
1004 dump_data(10, r.data, r.length);
1010 push a DATA_BLOB onto the wire.
1012 _PUBLIC_ enum ndr_err_code ndr_push_DATA_BLOB(struct ndr_push *ndr, int ndr_flags, DATA_BLOB blob)
1014 if (ndr->flags & LIBNDR_ALIGN_FLAGS) {
1015 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1016 blob.length = NDR_ALIGN(ndr, 2);
1017 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1018 blob.length = NDR_ALIGN(ndr, 4);
1019 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1020 blob.length = NDR_ALIGN(ndr, 8);
1022 NDR_PUSH_ALLOC_SIZE(ndr, blob.data, blob.length);
1023 data_blob_clear(&blob);
1024 } else if (!(ndr->flags & LIBNDR_FLAG_REMAINING)) {
1025 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, blob.length));
1027 NDR_CHECK(ndr_push_bytes(ndr, blob.data, blob.length));
1028 return NDR_ERR_SUCCESS;
1032 pull a DATA_BLOB from the wire.
1034 _PUBLIC_ enum ndr_err_code ndr_pull_DATA_BLOB(struct ndr_pull *ndr, int ndr_flags, DATA_BLOB *blob)
1036 uint32_t length = 0;
1038 if (ndr->flags & LIBNDR_ALIGN_FLAGS) {
1039 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1040 length = NDR_ALIGN(ndr, 2);
1041 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1042 length = NDR_ALIGN(ndr, 4);
1043 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1044 length = NDR_ALIGN(ndr, 8);
1046 if (ndr->data_size - ndr->offset < length) {
1047 length = ndr->data_size - ndr->offset;
1049 } else if (ndr->flags & LIBNDR_FLAG_REMAINING) {
1050 length = ndr->data_size - ndr->offset;
1051 } else {
1052 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &length));
1054 NDR_PULL_NEED_BYTES(ndr, length);
1055 *blob = data_blob_talloc(ndr->current_mem_ctx, ndr->data+ndr->offset, length);
1056 ndr->offset += length;
1057 return NDR_ERR_SUCCESS;
1060 _PUBLIC_ uint32_t ndr_size_DATA_BLOB(int ret, const DATA_BLOB *data, int flags)
1062 if (!data) return ret;
1063 return ret + data->length;