s3: Make name_resolve_bcast return sockaddr_storage
[Samba.git] / librpc / ndr / ndr_basic.c
blobb4443003d0613b7ea273cf756c995ae81c45479f
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"
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);
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_ 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);
66 ndr->offset += 1;
67 return NDR_ERR_SUCCESS;
71 parse a uint8_t
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);
77 ndr->offset += 1;
78 return NDR_ERR_SUCCESS;
82 parse a int16_t
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);
89 ndr->offset += 2;
90 return NDR_ERR_SUCCESS;
94 parse a uint16_t
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);
101 ndr->offset += 2;
102 return NDR_ERR_SUCCESS;
106 parse a uint1632_t
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)) {
111 uint32_t v32 = 0;
112 enum ndr_err_code err = ndr_pull_uint32(ndr, ndr_flags, &v32);
113 *v = 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;
118 return err;
120 return ndr_pull_uint16(ndr, ndr_flags, v);
124 parse a int32_t
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);
131 ndr->offset += 4;
132 return NDR_ERR_SUCCESS;
136 parse a uint32_t
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);
143 ndr->offset += 4;
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)
152 uint64_t v64;
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);
158 *v = (uint32_t)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;
164 return err;
168 parse a double
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);
175 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));
185 if (*v != 0) {
186 ndr->ptr_count++;
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));
198 if (*v != 0) {
199 ndr->ptr_count++;
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 */
211 *v = 1;
212 return NDR_ERR_SUCCESS;
216 parse a udlong
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;
224 ndr->offset += 8;
225 return NDR_ERR_SUCCESS;
229 parse a udlongr
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);
237 ndr->offset += 8;
238 return NDR_ERR_SUCCESS;
242 parse a dlong
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);
250 parse a hyper
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 if (NDR_BE(ndr)) {
256 return ndr_pull_udlongr(ndr, ndr_flags, v);
258 return ndr_pull_udlong(ndr, ndr_flags, v);
262 parse a pointer
264 _PUBLIC_ enum ndr_err_code ndr_pull_pointer(struct ndr_pull *ndr, int ndr_flags, void* *v)
266 uintptr_t h;
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);
271 *v = (void *)h;
272 return NDR_ERR_SUCCESS;
276 pull a NTSTATUS
278 _PUBLIC_ enum ndr_err_code ndr_pull_NTSTATUS(struct ndr_pull *ndr, int ndr_flags, NTSTATUS *status)
280 uint32_t v;
281 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
282 *status = NT_STATUS(v);
283 return NDR_ERR_SUCCESS;
287 push a NTSTATUS
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));
300 pull a WERROR
302 _PUBLIC_ enum ndr_err_code ndr_pull_WERROR(struct ndr_pull *ndr, int ndr_flags, WERROR *status)
304 uint32_t v;
305 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
306 *status = W_ERROR(v);
307 return NDR_ERR_SUCCESS;
312 parse a uint8_t enum
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)) {
333 uint32_t v32;
334 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &v32));
335 *v = v32;
336 if (v32 != *v) {
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);
354 push a uint8_t enum
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);
362 push a uint16_t enum
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);
370 push a uint32_t enum
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);
389 push a WERROR
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));
402 parse a set of bytes
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);
408 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);
424 push a int8_t
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);
430 ndr->offset += 1;
431 return NDR_ERR_SUCCESS;
435 push a uint8_t
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);
441 ndr->offset += 1;
442 return NDR_ERR_SUCCESS;
446 push a int16_t
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);
453 ndr->offset += 2;
454 return NDR_ERR_SUCCESS;
458 push a uint16_t
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);
465 ndr->offset += 2;
466 return NDR_ERR_SUCCESS;
470 push a uint1632
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);
481 push a int32_t
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);
488 ndr->offset += 4;
489 return NDR_ERR_SUCCESS;
493 push a uint32_t
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);
500 ndr->offset += 4;
501 return NDR_ERR_SUCCESS;
505 push a uint3264
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);
516 push a udlong
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));
524 ndr->offset += 8;
525 return NDR_ERR_SUCCESS;
529 push a udlongr
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));
537 ndr->offset += 8;
538 return NDR_ERR_SUCCESS;
542 push a dlong
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);
550 push a hyper
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);
555 if (NDR_BE(ndr)) {
556 return ndr_push_udlongr(ndr, NDR_SCALARS, v);
558 return ndr_push_udlong(ndr, NDR_SCALARS, v);
562 push a double
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);
569 ndr->offset += 8;
570 return NDR_ERR_SUCCESS;
574 push a pointer
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 */
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_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 */
609 if (size == 5) {
610 if (ndr->flags & LIBNDR_FLAG_NDR64) {
611 size = 8;
612 } else {
613 size = 4;
615 } else if (size == 3) {
616 if (ndr->flags & LIBNDR_FLAG_NDR64) {
617 size = 4;
618 } else {
619 size = 2;
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;
663 push some bytes
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);
669 ndr->offset += n;
670 return NDR_ERR_SUCCESS;
674 push some zero bytes
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);
680 ndr->offset += 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)
700 uint32_t ptr = 0;
701 if (p) {
702 ptr = ndr->ptr_count * 4;
703 ptr |= 0x00020000;
704 ndr->ptr_count++;
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)
714 uint32_t ptr = 0;
715 if (p) {
716 /* Check if the pointer already exists and has an id */
717 ptr = ndr_token_peek(&ndr->full_ptr_list, p);
718 if (ptr == 0) {
719 ndr->ptr_count++;
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);
737 push a NTTIME
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;
746 pull a NTTIME
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;
755 push a NTTIME_1sec
757 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME_1sec(struct ndr_push *ndr, int ndr_flags, NTTIME t)
759 t /= 10000000;
760 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
761 return NDR_ERR_SUCCESS;
765 pull a NTTIME_1sec
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));
770 (*t) *= 10000000;
771 return NDR_ERR_SUCCESS;
775 pull a NTTIME_hyper
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;
784 push a NTTIME_hyper
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;
793 push a time_t
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);
801 pull a time_t
803 _PUBLIC_ enum ndr_err_code ndr_pull_time_t(struct ndr_pull *ndr, int ndr_flags, time_t *t)
805 uint32_t tt;
806 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &tt));
807 *t = tt;
808 return NDR_ERR_SUCCESS;
813 push a uid_t
815 _PUBLIC_ enum ndr_err_code ndr_push_uid_t(struct ndr_push *ndr, int ndr_flags, uid_t u)
817 return ndr_push_hyper(ndr, NDR_SCALARS, (uint64_t)u);
821 pull a uid_t
823 _PUBLIC_ enum ndr_err_code ndr_pull_uid_t(struct ndr_pull *ndr, int ndr_flags, uid_t *u)
825 uint64_t uu;
826 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, &uu));
827 *u = (uid_t)uu;
828 if (unlikely(uu != *u)) {
829 DEBUG(0,(__location__ ": uid_t pull doesn't fit 0x%016llx\n",
830 (unsigned long long)uu));
831 return NDR_ERR_NDR64;
833 return NDR_ERR_SUCCESS;
838 push a gid_t
840 _PUBLIC_ enum ndr_err_code ndr_push_gid_t(struct ndr_push *ndr, int ndr_flags, gid_t g)
842 return ndr_push_hyper(ndr, NDR_SCALARS, (uint64_t)g);
846 pull a gid_t
848 _PUBLIC_ enum ndr_err_code ndr_pull_gid_t(struct ndr_pull *ndr, int ndr_flags, gid_t *g)
850 uint64_t gg;
851 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, &gg));
852 *g = (gid_t)gg;
853 if (unlikely(gg != *g)) {
854 DEBUG(0,(__location__ ": gid_t pull doesn't fit 0x%016llx\n",
855 (unsigned long long)gg));
856 return NDR_ERR_NDR64;
858 return NDR_ERR_SUCCESS;
863 pull a ipv4address
865 _PUBLIC_ enum ndr_err_code ndr_pull_ipv4address(struct ndr_pull *ndr, int ndr_flags, const char **address)
867 uint32_t addr;
868 struct in_addr in;
869 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &addr));
870 in.s_addr = htonl(addr);
871 *address = talloc_strdup(ndr->current_mem_ctx, inet_ntoa(in));
872 NDR_ERR_HAVE_NO_MEMORY(*address);
873 return NDR_ERR_SUCCESS;
877 push a ipv4address
879 _PUBLIC_ enum ndr_err_code ndr_push_ipv4address(struct ndr_push *ndr, int ndr_flags, const char *address)
881 uint32_t addr;
882 if (!is_ipaddress(address)) {
883 return ndr_push_error(ndr, NDR_ERR_IPV4ADDRESS,
884 "Invalid IPv4 address: '%s'",
885 address);
887 addr = inet_addr(address);
888 NDR_CHECK(ndr_push_uint32(ndr, ndr_flags, htonl(addr)));
889 return NDR_ERR_SUCCESS;
893 print a ipv4address
895 _PUBLIC_ void ndr_print_ipv4address(struct ndr_print *ndr, const char *name,
896 const char *address)
898 ndr->print(ndr, "%-25s: %s", name, address);
902 pull a ipv6address
904 #define IPV6_BYTES 16
905 #define IPV6_ADDR_STR_LEN 39
906 _PUBLIC_ enum ndr_err_code ndr_pull_ipv6address(struct ndr_pull *ndr, int ndr_flags, const char **address)
908 uint8_t addr[IPV6_BYTES];
909 char *addr_str = talloc_strdup(ndr->current_mem_ctx, "");
910 int i;
911 NDR_CHECK(ndr_pull_array_uint8(ndr, ndr_flags, addr, IPV6_BYTES));
912 for (i = 0; i < IPV6_BYTES; ++i) {
913 addr_str = talloc_asprintf_append(addr_str, "%02x", addr[i]);
914 /* We need a ':' every second byte but the last one */
915 if (i%2 == 1 && i != (IPV6_BYTES - 1)) {
916 addr_str = talloc_strdup_append(addr_str, ":");
919 *address = addr_str;
920 NDR_ERR_HAVE_NO_MEMORY(*address);
921 return NDR_ERR_SUCCESS;
925 push a ipv6address
927 _PUBLIC_ enum ndr_err_code ndr_push_ipv6address(struct ndr_push *ndr, int ndr_flags, const char *address)
929 #ifdef AF_INET6
930 uint8_t addr[IPV6_BYTES];
931 int ret;
933 if (!is_ipaddress(address)) {
934 return ndr_push_error(ndr, NDR_ERR_IPV6ADDRESS,
935 "Invalid IPv6 address: '%s'",
936 address);
938 ret = inet_pton(AF_INET6, address, addr);
939 if (ret <= 0) {
940 return NDR_ERR_IPV6ADDRESS;
943 NDR_CHECK(ndr_push_array_uint8(ndr, ndr_flags, addr, IPV6_BYTES));
945 return NDR_ERR_SUCCESS;
946 #else
947 return NDR_ERR_IPV6ADDRESS;
948 #endif
952 print a ipv6address
954 _PUBLIC_ void ndr_print_ipv6address(struct ndr_print *ndr, const char *name,
955 const char *address)
957 ndr->print(ndr, "%-25s: %s", name, address);
959 #undef IPV6_BYTES
961 _PUBLIC_ void ndr_print_struct(struct ndr_print *ndr, const char *name, const char *type)
963 ndr->print(ndr, "%s: struct %s", name, type);
966 _PUBLIC_ void ndr_print_null(struct ndr_print *ndr)
968 ndr->print(ndr, "UNEXPECTED NULL POINTER");
971 _PUBLIC_ void ndr_print_enum(struct ndr_print *ndr, const char *name, const char *type,
972 const char *val, uint32_t value)
974 if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
975 ndr->print(ndr, "%-25s: %s (0x%X)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
976 } else {
977 ndr->print(ndr, "%-25s: %s (%d)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
981 _PUBLIC_ void ndr_print_bitmap_flag(struct ndr_print *ndr, size_t size, const char *flag_name, uint32_t flag, uint32_t value)
983 /* this is an attempt to support multi-bit bitmap masks */
984 value &= flag;
986 while (!(flag & 1)) {
987 flag >>= 1;
988 value >>= 1;
990 if (flag == 1) {
991 ndr->print(ndr, " %d: %-25s", value, flag_name);
992 } else {
993 ndr->print(ndr, "0x%02x: %-25s (%d)", value, flag_name, value);
997 _PUBLIC_ void ndr_print_int8(struct ndr_print *ndr, const char *name, int8_t v)
999 ndr->print(ndr, "%-25s: %d", name, v);
1002 _PUBLIC_ void ndr_print_uint8(struct ndr_print *ndr, const char *name, uint8_t v)
1004 ndr->print(ndr, "%-25s: 0x%02x (%u)", name, v, v);
1007 _PUBLIC_ void ndr_print_int16(struct ndr_print *ndr, const char *name, int16_t v)
1009 ndr->print(ndr, "%-25s: %d", name, v);
1012 _PUBLIC_ void ndr_print_uint16(struct ndr_print *ndr, const char *name, uint16_t v)
1014 ndr->print(ndr, "%-25s: 0x%04x (%u)", name, v, v);
1017 _PUBLIC_ void ndr_print_int32(struct ndr_print *ndr, const char *name, int32_t v)
1019 ndr->print(ndr, "%-25s: %d", name, v);
1022 _PUBLIC_ void ndr_print_uint32(struct ndr_print *ndr, const char *name, uint32_t v)
1024 ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
1027 _PUBLIC_ void ndr_print_int3264(struct ndr_print *ndr, const char *name, int32_t v)
1029 ndr->print(ndr, "%-25s: %d", name, v);
1032 _PUBLIC_ void ndr_print_uint3264(struct ndr_print *ndr, const char *name, uint32_t v)
1034 ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
1037 _PUBLIC_ void ndr_print_udlong(struct ndr_print *ndr, const char *name, uint64_t v)
1039 ndr->print(ndr, "%-25s: 0x%016llx (%llu)", name, (unsigned long long)v, (unsigned long long)v);
1042 _PUBLIC_ void ndr_print_udlongr(struct ndr_print *ndr, const char *name, uint64_t v)
1044 ndr_print_udlong(ndr, name, v);
1047 _PUBLIC_ void ndr_print_dlong(struct ndr_print *ndr, const char *name, int64_t v)
1049 ndr->print(ndr, "%-25s: 0x%016llx (%lld)", name, (unsigned long long)v, (long long)v);
1052 _PUBLIC_ void ndr_print_double(struct ndr_print *ndr, const char *name, double v)
1054 ndr->print(ndr, "%-25s: %f", name, v);
1057 _PUBLIC_ void ndr_print_hyper(struct ndr_print *ndr, const char *name, uint64_t v)
1059 ndr_print_dlong(ndr, name, v);
1062 _PUBLIC_ void ndr_print_pointer(struct ndr_print *ndr, const char *name, void *v)
1064 ndr->print(ndr, "%-25s: %p", name, v);
1067 _PUBLIC_ void ndr_print_ptr(struct ndr_print *ndr, const char *name, const void *p)
1069 if (p) {
1070 ndr->print(ndr, "%-25s: *", name);
1071 } else {
1072 ndr->print(ndr, "%-25s: NULL", name);
1076 _PUBLIC_ void ndr_print_NTTIME(struct ndr_print *ndr, const char *name, NTTIME t)
1078 ndr->print(ndr, "%-25s: %s", name, nt_time_string(ndr, t));
1081 _PUBLIC_ void ndr_print_NTTIME_1sec(struct ndr_print *ndr, const char *name, NTTIME t)
1083 /* this is a standard NTTIME here
1084 * as it's already converted in the pull/push code
1086 ndr_print_NTTIME(ndr, name, t);
1089 _PUBLIC_ void ndr_print_NTTIME_hyper(struct ndr_print *ndr, const char *name, NTTIME t)
1091 ndr_print_NTTIME(ndr, name, t);
1094 _PUBLIC_ void ndr_print_time_t(struct ndr_print *ndr, const char *name, time_t t)
1096 if (t == (time_t)-1 || t == 0) {
1097 ndr->print(ndr, "%-25s: (time_t)%d", name, (int)t);
1098 } else {
1099 ndr->print(ndr, "%-25s: %s", name, timestring(ndr, t));
1103 _PUBLIC_ void ndr_print_uid_t(struct ndr_print *ndr, const char *name, uid_t u)
1105 ndr_print_dlong(ndr, name, u);
1108 _PUBLIC_ void ndr_print_gid_t(struct ndr_print *ndr, const char *name, gid_t g)
1110 ndr_print_dlong(ndr, name, g);
1113 _PUBLIC_ void ndr_print_union(struct ndr_print *ndr, const char *name, int level, const char *type)
1115 if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
1116 ndr->print(ndr, "%-25s: union %s(case 0x%X)", name, type, level);
1117 } else {
1118 ndr->print(ndr, "%-25s: union %s(case %d)", name, type, level);
1122 _PUBLIC_ void ndr_print_bad_level(struct ndr_print *ndr, const char *name, uint16_t level)
1124 ndr->print(ndr, "UNKNOWN LEVEL %u", level);
1127 _PUBLIC_ void ndr_print_array_uint8(struct ndr_print *ndr, const char *name,
1128 const uint8_t *data, uint32_t count)
1130 int i;
1132 if (data == NULL) {
1133 ndr->print(ndr, "%s: ARRAY(%d) : NULL", name, count);
1134 return;
1137 if (count <= 600 && (ndr->flags & LIBNDR_PRINT_ARRAY_HEX)) {
1138 char s[1202];
1139 for (i=0;i<count;i++) {
1140 snprintf(&s[i*2], 3, "%02x", data[i]);
1142 s[i*2] = 0;
1143 ndr->print(ndr, "%-25s: %s", name, s);
1144 return;
1147 ndr->print(ndr, "%s: ARRAY(%d)", name, count);
1148 ndr->depth++;
1149 for (i=0;i<count;i++) {
1150 char *idx=NULL;
1151 if (asprintf(&idx, "[%d]", i) != -1) {
1152 ndr_print_uint8(ndr, idx, data[i]);
1153 free(idx);
1156 ndr->depth--;
1159 static void ndr_print_asc(struct ndr_print *ndr, const uint8_t *buf, int len)
1161 int i;
1162 for (i=0;i<len;i++)
1163 ndr->print(ndr, "%c", isprint(buf[i])?buf[i]:'.');
1167 ndr_print version of dump_data()
1169 static void ndr_dump_data(struct ndr_print *ndr, const uint8_t *buf, int len)
1171 int i=0;
1173 ndr->no_newline = true;
1175 for (i=0;i<len;) {
1176 if (i%16 == 0 && i<len) {
1177 ndr->print(ndr, "[%04X] ",i);
1180 ndr->print(ndr, "%02X ",(int)buf[i]);
1181 i++;
1182 if (i%8 == 0) ndr->print(ndr," ");
1183 if (i%16 == 0) {
1184 ndr_print_asc(ndr,&buf[i-16],8); ndr->print(ndr," ");
1185 ndr_print_asc(ndr,&buf[i-8],8); ndr->print(ndr, "\n");
1189 if (i%16) {
1190 int n;
1191 n = 16 - (i%16);
1192 ndr->print(ndr, " ");
1193 if (n>8) ndr->print(ndr," ");
1194 while (n--) ndr->print(ndr," ");
1195 n = MIN(8,i%16);
1196 ndr_print_asc(ndr,&buf[i-(i%16)],n); ndr->print(ndr, " ");
1197 n = (i%16) - n;
1198 if (n>0) ndr_print_asc(ndr,&buf[i-n],n);
1199 ndr->print(ndr,"\n");
1202 ndr->no_newline = false;
1206 _PUBLIC_ void ndr_print_DATA_BLOB(struct ndr_print *ndr, const char *name, DATA_BLOB r)
1208 ndr->print(ndr, "%-25s: DATA_BLOB length=%u", name, (unsigned)r.length);
1209 if (r.length) {
1210 ndr_dump_data(ndr, r.data, r.length);
1216 push a DATA_BLOB onto the wire.
1218 _PUBLIC_ enum ndr_err_code ndr_push_DATA_BLOB(struct ndr_push *ndr, int ndr_flags, DATA_BLOB blob)
1220 if (ndr->flags & LIBNDR_FLAG_REMAINING) {
1221 /* nothing to do */
1222 } else if (ndr->flags & LIBNDR_ALIGN_FLAGS) {
1223 if (ndr->flags & LIBNDR_FLAG_NOALIGN) {
1224 blob.length = 0;
1225 } else if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1226 blob.length = NDR_ALIGN(ndr, 2);
1227 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1228 blob.length = NDR_ALIGN(ndr, 4);
1229 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1230 blob.length = NDR_ALIGN(ndr, 8);
1232 NDR_PUSH_ALLOC_SIZE(ndr, blob.data, blob.length);
1233 data_blob_clear(&blob);
1234 } else {
1235 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, blob.length));
1237 NDR_CHECK(ndr_push_bytes(ndr, blob.data, blob.length));
1238 return NDR_ERR_SUCCESS;
1242 pull a DATA_BLOB from the wire.
1244 _PUBLIC_ enum ndr_err_code ndr_pull_DATA_BLOB(struct ndr_pull *ndr, int ndr_flags, DATA_BLOB *blob)
1246 uint32_t length = 0;
1248 if (ndr->flags & LIBNDR_FLAG_REMAINING) {
1249 length = ndr->data_size - ndr->offset;
1250 } else if (ndr->flags & LIBNDR_ALIGN_FLAGS) {
1251 if (ndr->flags & LIBNDR_FLAG_NOALIGN) {
1252 length = 0;
1253 } else if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1254 length = NDR_ALIGN(ndr, 2);
1255 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1256 length = NDR_ALIGN(ndr, 4);
1257 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1258 length = NDR_ALIGN(ndr, 8);
1260 if (ndr->data_size - ndr->offset < length) {
1261 length = ndr->data_size - ndr->offset;
1263 } else {
1264 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &length));
1266 NDR_PULL_NEED_BYTES(ndr, length);
1267 *blob = data_blob_talloc(ndr->current_mem_ctx, ndr->data+ndr->offset, length);
1268 ndr->offset += length;
1269 return NDR_ERR_SUCCESS;
1272 _PUBLIC_ uint32_t ndr_size_DATA_BLOB(int ret, const DATA_BLOB *data, int flags)
1274 if (!data) return ret;
1275 return ret + data->length;
1278 _PUBLIC_ void ndr_print_bool(struct ndr_print *ndr, const char *name, const bool b)
1280 ndr->print(ndr, "%-25s: %s", name, b?"true":"false");
1283 _PUBLIC_ NTSTATUS ndr_map_error2ntstatus(enum ndr_err_code ndr_err)
1285 switch (ndr_err) {
1286 case NDR_ERR_SUCCESS:
1287 return NT_STATUS_OK;
1288 case NDR_ERR_BUFSIZE:
1289 return NT_STATUS_BUFFER_TOO_SMALL;
1290 case NDR_ERR_TOKEN:
1291 return NT_STATUS_INTERNAL_ERROR;
1292 case NDR_ERR_ALLOC:
1293 return NT_STATUS_NO_MEMORY;
1294 case NDR_ERR_ARRAY_SIZE:
1295 return NT_STATUS_ARRAY_BOUNDS_EXCEEDED;
1296 case NDR_ERR_INVALID_POINTER:
1297 return NT_STATUS_INVALID_PARAMETER_MIX;
1298 case NDR_ERR_UNREAD_BYTES:
1299 return NT_STATUS_PORT_MESSAGE_TOO_LONG;
1300 default:
1301 break;
1304 /* we should map all error codes to different status codes */
1305 return NT_STATUS_INVALID_PARAMETER;