torture: Run new testsuite for krb5 and KDC behaviour with machine account also
[Samba.git] / librpc / ndr / ndr_basic.c
bloba6cf607f3eb89ae4f37f4e9a00e9bfb472e3a73e
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)
35 static void ndr_dump_data(struct ndr_print *ndr, const uint8_t *buf, int len);
38 check for data leaks from the server by looking for non-zero pad bytes
39 these could also indicate that real structure elements have been
40 mistaken for padding in the IDL
42 _PUBLIC_ void ndr_check_padding(struct ndr_pull *ndr, size_t n)
44 size_t ofs2 = (ndr->offset + (n-1)) & ~(n-1);
45 int i;
46 for (i=ndr->offset;i<ofs2;i++) {
47 if (ndr->data[i] != 0) {
48 break;
51 if (i<ofs2) {
52 DEBUG(0,("WARNING: Non-zero padding to %d: ", (int)n));
53 for (i=ndr->offset;i<ofs2;i++) {
54 DEBUG(0,("%02x ", ndr->data[i]));
56 DEBUG(0,("\n"));
62 parse a int8_t
64 _PUBLIC_ enum ndr_err_code ndr_pull_int8(struct ndr_pull *ndr, int ndr_flags, int8_t *v)
66 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
67 NDR_PULL_NEED_BYTES(ndr, 1);
68 *v = (int8_t)CVAL(ndr->data, ndr->offset);
69 ndr->offset += 1;
70 return NDR_ERR_SUCCESS;
74 parse a uint8_t
76 _PUBLIC_ enum ndr_err_code ndr_pull_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *v)
78 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
79 NDR_PULL_NEED_BYTES(ndr, 1);
80 *v = CVAL(ndr->data, ndr->offset);
81 ndr->offset += 1;
82 return NDR_ERR_SUCCESS;
86 parse a int16_t
88 _PUBLIC_ enum ndr_err_code ndr_pull_int16(struct ndr_pull *ndr, int ndr_flags, int16_t *v)
90 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
91 NDR_PULL_ALIGN(ndr, 2);
92 NDR_PULL_NEED_BYTES(ndr, 2);
93 *v = (uint16_t)NDR_SVAL(ndr, ndr->offset);
94 ndr->offset += 2;
95 return NDR_ERR_SUCCESS;
99 parse a uint16_t
101 _PUBLIC_ enum ndr_err_code ndr_pull_uint16(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
103 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
104 NDR_PULL_ALIGN(ndr, 2);
105 NDR_PULL_NEED_BYTES(ndr, 2);
106 *v = NDR_SVAL(ndr, ndr->offset);
107 ndr->offset += 2;
108 return NDR_ERR_SUCCESS;
112 parse a uint1632_t
114 _PUBLIC_ enum ndr_err_code ndr_pull_uint1632(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
116 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
117 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
118 uint32_t v32 = 0;
119 enum ndr_err_code err = ndr_pull_uint32(ndr, ndr_flags, &v32);
120 *v = v32;
121 if (unlikely(v32 != *v)) {
122 DEBUG(0,(__location__ ": non-zero upper 16 bits 0x%08x\n", (unsigned)v32));
123 return NDR_ERR_NDR64;
125 return err;
127 return ndr_pull_uint16(ndr, ndr_flags, v);
131 parse a int32_t
133 _PUBLIC_ enum ndr_err_code ndr_pull_int32(struct ndr_pull *ndr, int ndr_flags, int32_t *v)
135 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
136 NDR_PULL_ALIGN(ndr, 4);
137 NDR_PULL_NEED_BYTES(ndr, 4);
138 *v = NDR_IVALS(ndr, ndr->offset);
139 ndr->offset += 4;
140 return NDR_ERR_SUCCESS;
144 parse a uint32_t
146 _PUBLIC_ enum ndr_err_code ndr_pull_uint32(struct ndr_pull *ndr, int ndr_flags, uint32_t *v)
148 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
149 NDR_PULL_ALIGN(ndr, 4);
150 NDR_PULL_NEED_BYTES(ndr, 4);
151 *v = NDR_IVAL(ndr, ndr->offset);
152 ndr->offset += 4;
153 return NDR_ERR_SUCCESS;
157 parse a arch dependent uint32/uint64
159 _PUBLIC_ enum ndr_err_code ndr_pull_uint3264(struct ndr_pull *ndr, int ndr_flags, uint32_t *v)
161 uint64_t v64;
162 enum ndr_err_code err;
163 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
164 if (likely(!(ndr->flags & LIBNDR_FLAG_NDR64))) {
165 return ndr_pull_uint32(ndr, ndr_flags, v);
167 err = ndr_pull_hyper(ndr, ndr_flags, &v64);
168 *v = (uint32_t)v64;
169 if (unlikely(v64 != *v)) {
170 DEBUG(0,(__location__ ": non-zero upper 32 bits 0x%016llx\n",
171 (unsigned long long)v64));
172 return ndr_pull_error(ndr, NDR_ERR_NDR64, __location__ ": non-zero upper 32 bits 0x%016llx\n",
173 (unsigned long long)v64);
175 return err;
179 parse a double
181 _PUBLIC_ enum ndr_err_code ndr_pull_double(struct ndr_pull *ndr, int ndr_flags, double *v)
183 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
184 NDR_PULL_ALIGN(ndr, 8);
185 NDR_PULL_NEED_BYTES(ndr, 8);
186 memcpy(v, ndr->data+ndr->offset, 8);
187 ndr->offset += 8;
188 return NDR_ERR_SUCCESS;
192 parse a pointer referent identifier stored in 2 bytes
194 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr_short(struct ndr_pull *ndr, uint16_t *v)
196 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, v));
197 if (*v != 0) {
198 ndr->ptr_count++;
200 *(v) -= ndr->relative_rap_convert;
201 return NDR_ERR_SUCCESS;
205 parse a pointer referent identifier
207 _PUBLIC_ enum ndr_err_code ndr_pull_generic_ptr(struct ndr_pull *ndr, uint32_t *v)
209 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, v));
210 if (*v != 0) {
211 ndr->ptr_count++;
213 return NDR_ERR_SUCCESS;
217 parse a ref pointer referent identifier
219 _PUBLIC_ enum ndr_err_code ndr_pull_ref_ptr(struct ndr_pull *ndr, uint32_t *v)
221 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, v));
222 /* ref pointers always point to data */
223 *v = 1;
224 return NDR_ERR_SUCCESS;
228 parse a udlong
230 _PUBLIC_ enum ndr_err_code ndr_pull_udlong(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
232 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
233 NDR_PULL_ALIGN(ndr, 4);
234 NDR_PULL_NEED_BYTES(ndr, 8);
235 *v = NDR_IVAL(ndr, ndr->offset);
236 *v |= (uint64_t)(NDR_IVAL(ndr, ndr->offset+4)) << 32;
237 ndr->offset += 8;
238 return NDR_ERR_SUCCESS;
242 parse a udlongr
244 _PUBLIC_ enum ndr_err_code ndr_pull_udlongr(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
246 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
247 NDR_PULL_ALIGN(ndr, 4);
248 NDR_PULL_NEED_BYTES(ndr, 8);
249 *v = ((uint64_t)NDR_IVAL(ndr, ndr->offset)) << 32;
250 *v |= NDR_IVAL(ndr, ndr->offset+4);
251 ndr->offset += 8;
252 return NDR_ERR_SUCCESS;
256 parse a dlong
258 _PUBLIC_ enum ndr_err_code ndr_pull_dlong(struct ndr_pull *ndr, int ndr_flags, int64_t *v)
260 return ndr_pull_udlong(ndr, ndr_flags, (uint64_t *)v);
264 parse a hyper
266 _PUBLIC_ enum ndr_err_code ndr_pull_hyper(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
268 NDR_PULL_ALIGN(ndr, 8);
269 if (NDR_BE(ndr)) {
270 return ndr_pull_udlongr(ndr, ndr_flags, v);
272 return ndr_pull_udlong(ndr, ndr_flags, v);
276 parse a pointer
278 _PUBLIC_ enum ndr_err_code ndr_pull_pointer(struct ndr_pull *ndr, int ndr_flags, void* *v)
280 uintptr_t h;
281 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
282 NDR_PULL_ALIGN(ndr, sizeof(h));
283 NDR_PULL_NEED_BYTES(ndr, sizeof(h));
284 memcpy(&h, ndr->data+ndr->offset, sizeof(h));
285 ndr->offset += sizeof(h);
286 *v = (void *)h;
287 return NDR_ERR_SUCCESS;
291 pull a NTSTATUS
293 _PUBLIC_ enum ndr_err_code ndr_pull_NTSTATUS(struct ndr_pull *ndr, int ndr_flags, NTSTATUS *status)
295 uint32_t v;
296 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
297 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
298 *status = NT_STATUS(v);
299 return NDR_ERR_SUCCESS;
303 push a NTSTATUS
305 _PUBLIC_ enum ndr_err_code ndr_push_NTSTATUS(struct ndr_push *ndr, int ndr_flags, NTSTATUS status)
307 return ndr_push_uint32(ndr, ndr_flags, NT_STATUS_V(status));
310 _PUBLIC_ void ndr_print_NTSTATUS(struct ndr_print *ndr, const char *name, NTSTATUS r)
312 ndr->print(ndr, "%-25s: %s", name, nt_errstr(r));
316 pull a WERROR
318 _PUBLIC_ enum ndr_err_code ndr_pull_WERROR(struct ndr_pull *ndr, int ndr_flags, WERROR *status)
320 uint32_t v;
321 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
322 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
323 *status = W_ERROR(v);
324 return NDR_ERR_SUCCESS;
329 parse a uint8_t enum
331 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *v)
333 return ndr_pull_uint8(ndr, ndr_flags, v);
337 parse a uint16_t enum
339 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint16(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
341 return ndr_pull_uint16(ndr, ndr_flags, v);
345 parse a uint1632_t enum (uint32_t on NDR64)
347 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint1632(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
349 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
350 uint32_t v32;
351 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &v32));
352 *v = v32;
353 if (v32 != *v) {
354 DEBUG(0,(__location__ ": non-zero upper 16 bits 0x%08x\n", (unsigned)v32));
355 return NDR_ERR_NDR64;
357 return NDR_ERR_SUCCESS;
359 return ndr_pull_uint16(ndr, ndr_flags, v);
363 parse a uint32_t enum
365 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint32(struct ndr_pull *ndr, int ndr_flags, uint32_t *v)
367 return ndr_pull_uint32(ndr, ndr_flags, v);
371 push a uint8_t enum
373 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint8(struct ndr_push *ndr, int ndr_flags, uint8_t v)
375 return ndr_push_uint8(ndr, ndr_flags, v);
379 push a uint16_t enum
381 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint16(struct ndr_push *ndr, int ndr_flags, uint16_t v)
383 return ndr_push_uint16(ndr, ndr_flags, v);
387 push a uint32_t enum
389 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint32(struct ndr_push *ndr, int ndr_flags, uint32_t v)
391 return ndr_push_uint32(ndr, ndr_flags, v);
395 push a uint1632_t enum
397 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint1632(struct ndr_push *ndr, int ndr_flags, uint16_t v)
399 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
400 return ndr_push_uint32(ndr, ndr_flags, v);
402 return ndr_push_uint16(ndr, ndr_flags, v);
406 push a WERROR
408 _PUBLIC_ enum ndr_err_code ndr_push_WERROR(struct ndr_push *ndr, int ndr_flags, WERROR status)
410 return ndr_push_uint32(ndr, NDR_SCALARS, W_ERROR_V(status));
413 _PUBLIC_ void ndr_print_WERROR(struct ndr_print *ndr, const char *name, WERROR r)
415 ndr->print(ndr, "%-25s: %s", name, win_errstr(r));
419 parse a set of bytes
421 _PUBLIC_ enum ndr_err_code ndr_pull_bytes(struct ndr_pull *ndr, uint8_t *data, uint32_t n)
423 NDR_PULL_NEED_BYTES(ndr, n);
424 memcpy(data, ndr->data + ndr->offset, n);
425 ndr->offset += n;
426 return NDR_ERR_SUCCESS;
430 pull an array of uint8
432 _PUBLIC_ enum ndr_err_code ndr_pull_array_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *data, uint32_t n)
434 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
435 if (!(ndr_flags & NDR_SCALARS)) {
436 return NDR_ERR_SUCCESS;
438 return ndr_pull_bytes(ndr, data, n);
442 push a int8_t
444 _PUBLIC_ enum ndr_err_code ndr_push_int8(struct ndr_push *ndr, int ndr_flags, int8_t v)
446 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
447 NDR_PUSH_NEED_BYTES(ndr, 1);
448 SCVAL(ndr->data, ndr->offset, (uint8_t)v);
449 ndr->offset += 1;
450 return NDR_ERR_SUCCESS;
454 push a uint8_t
456 _PUBLIC_ enum ndr_err_code ndr_push_uint8(struct ndr_push *ndr, int ndr_flags, uint8_t v)
458 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
459 NDR_PUSH_NEED_BYTES(ndr, 1);
460 SCVAL(ndr->data, ndr->offset, v);
461 ndr->offset += 1;
462 return NDR_ERR_SUCCESS;
466 push a int16_t
468 _PUBLIC_ enum ndr_err_code ndr_push_int16(struct ndr_push *ndr, int ndr_flags, int16_t v)
470 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
471 NDR_PUSH_ALIGN(ndr, 2);
472 NDR_PUSH_NEED_BYTES(ndr, 2);
473 NDR_SSVAL(ndr, ndr->offset, (uint16_t)v);
474 ndr->offset += 2;
475 return NDR_ERR_SUCCESS;
479 push a uint16_t
481 _PUBLIC_ enum ndr_err_code ndr_push_uint16(struct ndr_push *ndr, int ndr_flags, uint16_t v)
483 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
484 NDR_PUSH_ALIGN(ndr, 2);
485 NDR_PUSH_NEED_BYTES(ndr, 2);
486 NDR_SSVAL(ndr, ndr->offset, v);
487 ndr->offset += 2;
488 return NDR_ERR_SUCCESS;
492 push a uint1632
494 _PUBLIC_ enum ndr_err_code ndr_push_uint1632(struct ndr_push *ndr, int ndr_flags, uint16_t v)
496 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
497 return ndr_push_uint32(ndr, ndr_flags, v);
499 return ndr_push_uint16(ndr, ndr_flags, v);
503 push a int32_t
505 _PUBLIC_ enum ndr_err_code ndr_push_int32(struct ndr_push *ndr, int ndr_flags, int32_t v)
507 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
508 NDR_PUSH_ALIGN(ndr, 4);
509 NDR_PUSH_NEED_BYTES(ndr, 4);
510 NDR_SIVALS(ndr, ndr->offset, v);
511 ndr->offset += 4;
512 return NDR_ERR_SUCCESS;
516 push a uint32_t
518 _PUBLIC_ enum ndr_err_code ndr_push_uint32(struct ndr_push *ndr, int ndr_flags, uint32_t v)
520 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
521 NDR_PUSH_ALIGN(ndr, 4);
522 NDR_PUSH_NEED_BYTES(ndr, 4);
523 NDR_SIVAL(ndr, ndr->offset, v);
524 ndr->offset += 4;
525 return NDR_ERR_SUCCESS;
529 push a uint3264
531 _PUBLIC_ enum ndr_err_code ndr_push_uint3264(struct ndr_push *ndr, int ndr_flags, uint32_t v)
533 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
534 return ndr_push_hyper(ndr, ndr_flags, v);
536 return ndr_push_uint32(ndr, ndr_flags, v);
540 push a udlong
542 _PUBLIC_ enum ndr_err_code ndr_push_udlong(struct ndr_push *ndr, int ndr_flags, uint64_t v)
544 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
545 NDR_PUSH_ALIGN(ndr, 4);
546 NDR_PUSH_NEED_BYTES(ndr, 8);
547 NDR_SIVAL(ndr, ndr->offset, (v & 0xFFFFFFFF));
548 NDR_SIVAL(ndr, ndr->offset+4, (v>>32));
549 ndr->offset += 8;
550 return NDR_ERR_SUCCESS;
554 push a udlongr
556 _PUBLIC_ enum ndr_err_code ndr_push_udlongr(struct ndr_push *ndr, int ndr_flags, uint64_t v)
558 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
559 NDR_PUSH_ALIGN(ndr, 4);
560 NDR_PUSH_NEED_BYTES(ndr, 8);
561 NDR_SIVAL(ndr, ndr->offset, (v>>32));
562 NDR_SIVAL(ndr, ndr->offset+4, (v & 0xFFFFFFFF));
563 ndr->offset += 8;
564 return NDR_ERR_SUCCESS;
568 push a dlong
570 _PUBLIC_ enum ndr_err_code ndr_push_dlong(struct ndr_push *ndr, int ndr_flags, int64_t v)
572 return ndr_push_udlong(ndr, NDR_SCALARS, (uint64_t)v);
576 push a hyper
578 _PUBLIC_ enum ndr_err_code ndr_push_hyper(struct ndr_push *ndr, int ndr_flags, uint64_t v)
580 NDR_PUSH_ALIGN(ndr, 8);
581 if (NDR_BE(ndr)) {
582 return ndr_push_udlongr(ndr, NDR_SCALARS, v);
584 return ndr_push_udlong(ndr, NDR_SCALARS, v);
588 push a double
590 _PUBLIC_ enum ndr_err_code ndr_push_double(struct ndr_push *ndr, int ndr_flags, double v)
592 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
593 NDR_PUSH_ALIGN(ndr, 8);
594 NDR_PUSH_NEED_BYTES(ndr, 8);
595 memcpy(ndr->data+ndr->offset, &v, 8);
596 ndr->offset += 8;
597 return NDR_ERR_SUCCESS;
601 push a pointer
603 _PUBLIC_ enum ndr_err_code ndr_push_pointer(struct ndr_push *ndr, int ndr_flags, void* v)
605 uintptr_t h = (intptr_t)v;
606 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
607 NDR_PUSH_ALIGN(ndr, sizeof(h));
608 NDR_PUSH_NEED_BYTES(ndr, sizeof(h));
609 memcpy(ndr->data+ndr->offset, &h, sizeof(h));
610 ndr->offset += sizeof(h);
611 return NDR_ERR_SUCCESS;
614 _PUBLIC_ enum ndr_err_code ndr_push_align(struct ndr_push *ndr, size_t size)
616 /* this is a nasty hack to make pidl work with NDR64 */
617 if (size == 5) {
618 if (ndr->flags & LIBNDR_FLAG_NDR64) {
619 size = 8;
620 } else {
621 size = 4;
623 } else if (size == 3) {
624 if (ndr->flags & LIBNDR_FLAG_NDR64) {
625 size = 4;
626 } else {
627 size = 2;
630 NDR_PUSH_ALIGN(ndr, size);
631 return NDR_ERR_SUCCESS;
634 _PUBLIC_ enum ndr_err_code ndr_pull_align(struct ndr_pull *ndr, size_t size)
636 /* this is a nasty hack to make pidl work with NDR64 */
637 if (size == 5) {
638 if (ndr->flags & LIBNDR_FLAG_NDR64) {
639 size = 8;
640 } else {
641 size = 4;
643 } else if (size == 3) {
644 if (ndr->flags & LIBNDR_FLAG_NDR64) {
645 size = 4;
646 } else {
647 size = 2;
650 NDR_PULL_ALIGN(ndr, size);
651 return NDR_ERR_SUCCESS;
654 _PUBLIC_ enum ndr_err_code ndr_push_union_align(struct ndr_push *ndr, size_t size)
656 /* MS-RPCE section 2.2.5.3.4.4 */
657 if (ndr->flags & LIBNDR_FLAG_NDR64) {
658 return ndr_push_align(ndr, size);
660 return NDR_ERR_SUCCESS;
663 _PUBLIC_ enum ndr_err_code ndr_pull_union_align(struct ndr_pull *ndr, size_t size)
665 /* MS-RPCE section 2.2.5.3.4.4 */
666 if (ndr->flags & LIBNDR_FLAG_NDR64) {
667 return ndr_pull_align(ndr, size);
669 return NDR_ERR_SUCCESS;
672 _PUBLIC_ enum ndr_err_code ndr_push_trailer_align(struct ndr_push *ndr, size_t size)
674 /* MS-RPCE section 2.2.5.3.4.1 */
675 if (ndr->flags & LIBNDR_FLAG_NDR64) {
676 return ndr_push_align(ndr, size);
678 return NDR_ERR_SUCCESS;
681 _PUBLIC_ enum ndr_err_code ndr_pull_trailer_align(struct ndr_pull *ndr, size_t size)
683 /* MS-RPCE section 2.2.5.3.4.1 */
684 if (ndr->flags & LIBNDR_FLAG_NDR64) {
685 return ndr_pull_align(ndr, size);
687 return NDR_ERR_SUCCESS;
691 push some bytes
693 _PUBLIC_ enum ndr_err_code ndr_push_bytes(struct ndr_push *ndr, const uint8_t *data, uint32_t n)
695 NDR_PUSH_NEED_BYTES(ndr, n);
696 memcpy(ndr->data + ndr->offset, data, n);
697 ndr->offset += n;
698 return NDR_ERR_SUCCESS;
702 push some zero bytes
704 _PUBLIC_ enum ndr_err_code ndr_push_zero(struct ndr_push *ndr, uint32_t n)
706 NDR_PUSH_NEED_BYTES(ndr, n);
707 memset(ndr->data + ndr->offset, 0, n);
708 ndr->offset += n;
709 return NDR_ERR_SUCCESS;
713 push an array of uint8
715 _PUBLIC_ enum ndr_err_code ndr_push_array_uint8(struct ndr_push *ndr, int ndr_flags, const uint8_t *data, uint32_t n)
717 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
718 if (!(ndr_flags & NDR_SCALARS)) {
719 return NDR_ERR_SUCCESS;
721 return ndr_push_bytes(ndr, data, n);
725 push a unique non-zero value if a pointer is non-NULL, otherwise 0
727 _PUBLIC_ enum ndr_err_code ndr_push_unique_ptr(struct ndr_push *ndr, const void *p)
729 uint32_t ptr = 0;
730 if (p) {
731 ptr = ndr->ptr_count * 4;
732 ptr |= 0x00020000;
733 ndr->ptr_count++;
735 return ndr_push_uint3264(ndr, NDR_SCALARS, ptr);
739 push a 'simple' full non-zero value if a pointer is non-NULL, otherwise 0
741 _PUBLIC_ enum ndr_err_code ndr_push_full_ptr(struct ndr_push *ndr, const void *p)
743 uint32_t ptr = 0;
744 if (p) {
745 /* Check if the pointer already exists and has an id */
746 ptr = ndr_token_peek(&ndr->full_ptr_list, p);
747 if (ptr == 0) {
748 ndr->ptr_count++;
749 ptr = ndr->ptr_count;
750 ndr_token_store(ndr, &ndr->full_ptr_list, p, ptr);
753 return ndr_push_uint3264(ndr, NDR_SCALARS, ptr);
757 push always a 0, if a pointer is NULL it's a fatal error
759 _PUBLIC_ enum ndr_err_code ndr_push_ref_ptr(struct ndr_push *ndr)
761 return ndr_push_uint3264(ndr, NDR_SCALARS, 0xAEF1AEF1);
766 push a NTTIME
768 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME(struct ndr_push *ndr, int ndr_flags, NTTIME t)
770 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
771 NDR_CHECK(ndr_push_udlong(ndr, ndr_flags, t));
772 return NDR_ERR_SUCCESS;
776 pull a NTTIME
778 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
780 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
781 NDR_CHECK(ndr_pull_udlong(ndr, ndr_flags, t));
782 return NDR_ERR_SUCCESS;
786 push a NTTIME_1sec
788 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME_1sec(struct ndr_push *ndr, int ndr_flags, NTTIME t)
790 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
791 t /= 10000000;
792 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
793 return NDR_ERR_SUCCESS;
797 pull a NTTIME_1sec
799 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME_1sec(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
801 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
802 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
803 (*t) *= 10000000;
804 return NDR_ERR_SUCCESS;
808 pull a NTTIME_hyper
810 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME_hyper(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
812 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
813 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
814 return NDR_ERR_SUCCESS;
818 push a NTTIME_hyper
820 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME_hyper(struct ndr_push *ndr, int ndr_flags, NTTIME t)
822 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
823 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
824 return NDR_ERR_SUCCESS;
828 push a time_t
830 _PUBLIC_ enum ndr_err_code ndr_push_time_t(struct ndr_push *ndr, int ndr_flags, time_t t)
832 return ndr_push_uint32(ndr, ndr_flags, t);
836 pull a time_t
838 _PUBLIC_ enum ndr_err_code ndr_pull_time_t(struct ndr_pull *ndr, int ndr_flags, time_t *t)
840 uint32_t tt;
841 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &tt));
842 *t = tt;
843 return NDR_ERR_SUCCESS;
848 push a uid_t
850 _PUBLIC_ enum ndr_err_code ndr_push_uid_t(struct ndr_push *ndr, int ndr_flags, uid_t u)
852 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
853 return ndr_push_hyper(ndr, NDR_SCALARS, (uint64_t)u);
857 pull a uid_t
859 _PUBLIC_ enum ndr_err_code ndr_pull_uid_t(struct ndr_pull *ndr, int ndr_flags, uid_t *u)
861 uint64_t uu;
862 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, &uu));
863 *u = (uid_t)uu;
864 if (unlikely(uu != *u)) {
865 DEBUG(0,(__location__ ": uid_t pull doesn't fit 0x%016llx\n",
866 (unsigned long long)uu));
867 return NDR_ERR_NDR64;
869 return NDR_ERR_SUCCESS;
874 push a gid_t
876 _PUBLIC_ enum ndr_err_code ndr_push_gid_t(struct ndr_push *ndr, int ndr_flags, gid_t g)
878 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
879 return ndr_push_hyper(ndr, NDR_SCALARS, (uint64_t)g);
883 pull a gid_t
885 _PUBLIC_ enum ndr_err_code ndr_pull_gid_t(struct ndr_pull *ndr, int ndr_flags, gid_t *g)
887 uint64_t gg;
888 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, &gg));
889 *g = (gid_t)gg;
890 if (unlikely(gg != *g)) {
891 DEBUG(0,(__location__ ": gid_t pull doesn't fit 0x%016llx\n",
892 (unsigned long long)gg));
893 return NDR_ERR_NDR64;
895 return NDR_ERR_SUCCESS;
900 pull a ipv4address
902 _PUBLIC_ enum ndr_err_code ndr_pull_ipv4address(struct ndr_pull *ndr, int ndr_flags, const char **address)
904 uint32_t addr;
905 struct in_addr in;
906 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &addr));
907 in.s_addr = htonl(addr);
908 *address = talloc_strdup(ndr->current_mem_ctx, inet_ntoa(in));
909 NDR_ERR_HAVE_NO_MEMORY(*address);
910 return NDR_ERR_SUCCESS;
914 push a ipv4address
916 _PUBLIC_ enum ndr_err_code ndr_push_ipv4address(struct ndr_push *ndr, int ndr_flags, const char *address)
918 uint32_t addr;
919 if (!is_ipaddress(address)) {
920 return ndr_push_error(ndr, NDR_ERR_IPV4ADDRESS,
921 "Invalid IPv4 address: '%s'",
922 address);
924 addr = inet_addr(address);
925 NDR_CHECK(ndr_push_uint32(ndr, ndr_flags, htonl(addr)));
926 return NDR_ERR_SUCCESS;
930 print a ipv4address
932 _PUBLIC_ void ndr_print_ipv4address(struct ndr_print *ndr, const char *name,
933 const char *address)
935 ndr->print(ndr, "%-25s: %s", name, address);
939 pull a ipv6address
941 #define IPV6_BYTES 16
942 #define IPV6_ADDR_STR_LEN 39
943 _PUBLIC_ enum ndr_err_code ndr_pull_ipv6address(struct ndr_pull *ndr, int ndr_flags, const char **address)
945 uint8_t addr[IPV6_BYTES];
946 char *addr_str = talloc_strdup(ndr->current_mem_ctx, "");
947 int i;
948 NDR_CHECK(ndr_pull_array_uint8(ndr, ndr_flags, addr, IPV6_BYTES));
949 for (i = 0; i < IPV6_BYTES; ++i) {
950 addr_str = talloc_asprintf_append(addr_str, "%02x", addr[i]);
951 /* We need a ':' every second byte but the last one */
952 if (i%2 == 1 && i != (IPV6_BYTES - 1)) {
953 addr_str = talloc_strdup_append(addr_str, ":");
956 *address = addr_str;
957 NDR_ERR_HAVE_NO_MEMORY(*address);
958 return NDR_ERR_SUCCESS;
962 push a ipv6address
964 _PUBLIC_ enum ndr_err_code ndr_push_ipv6address(struct ndr_push *ndr, int ndr_flags, const char *address)
966 #ifdef AF_INET6
967 uint8_t addr[IPV6_BYTES];
968 int ret;
970 if (!is_ipaddress(address)) {
971 return ndr_push_error(ndr, NDR_ERR_IPV6ADDRESS,
972 "Invalid IPv6 address: '%s'",
973 address);
975 ret = inet_pton(AF_INET6, address, addr);
976 if (ret <= 0) {
977 return NDR_ERR_IPV6ADDRESS;
980 NDR_CHECK(ndr_push_array_uint8(ndr, ndr_flags, addr, IPV6_BYTES));
982 return NDR_ERR_SUCCESS;
983 #else
984 return NDR_ERR_IPV6ADDRESS;
985 #endif
989 print a ipv6address
991 _PUBLIC_ void ndr_print_ipv6address(struct ndr_print *ndr, const char *name,
992 const char *address)
994 ndr->print(ndr, "%-25s: %s", name, address);
996 #undef IPV6_BYTES
998 _PUBLIC_ void ndr_print_struct(struct ndr_print *ndr, const char *name, const char *type)
1000 ndr->print(ndr, "%s: struct %s", name, type);
1003 _PUBLIC_ void ndr_print_null(struct ndr_print *ndr)
1005 ndr->print(ndr, "UNEXPECTED NULL POINTER");
1008 _PUBLIC_ void ndr_print_enum(struct ndr_print *ndr, const char *name, const char *type,
1009 const char *val, uint32_t value)
1011 if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
1012 ndr->print(ndr, "%-25s: %s (0x%X)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
1013 } else {
1014 ndr->print(ndr, "%-25s: %s (%d)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
1018 _PUBLIC_ void ndr_print_bitmap_flag(struct ndr_print *ndr, size_t size, const char *flag_name, uint32_t flag, uint32_t value)
1020 if (flag == 0) {
1021 return;
1024 /* this is an attempt to support multi-bit bitmap masks */
1025 value &= flag;
1027 while (!(flag & 1)) {
1028 flag >>= 1;
1029 value >>= 1;
1031 if (flag == 1) {
1032 ndr->print(ndr, " %d: %-25s", value, flag_name);
1033 } else {
1034 ndr->print(ndr, "0x%02x: %-25s (%d)", value, flag_name, value);
1038 _PUBLIC_ void ndr_print_int8(struct ndr_print *ndr, const char *name, int8_t v)
1040 ndr->print(ndr, "%-25s: %d", name, v);
1043 _PUBLIC_ void ndr_print_uint8(struct ndr_print *ndr, const char *name, uint8_t v)
1045 ndr->print(ndr, "%-25s: 0x%02x (%u)", name, v, v);
1048 _PUBLIC_ void ndr_print_int16(struct ndr_print *ndr, const char *name, int16_t v)
1050 ndr->print(ndr, "%-25s: %d", name, v);
1053 _PUBLIC_ void ndr_print_uint16(struct ndr_print *ndr, const char *name, uint16_t v)
1055 ndr->print(ndr, "%-25s: 0x%04x (%u)", name, v, v);
1058 _PUBLIC_ void ndr_print_int32(struct ndr_print *ndr, const char *name, int32_t v)
1060 ndr->print(ndr, "%-25s: %d", name, v);
1063 _PUBLIC_ void ndr_print_uint32(struct ndr_print *ndr, const char *name, uint32_t v)
1065 ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
1068 _PUBLIC_ void ndr_print_int3264(struct ndr_print *ndr, const char *name, int32_t v)
1070 ndr->print(ndr, "%-25s: %d", name, v);
1073 _PUBLIC_ void ndr_print_uint3264(struct ndr_print *ndr, const char *name, uint32_t v)
1075 ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
1078 _PUBLIC_ void ndr_print_udlong(struct ndr_print *ndr, const char *name, uint64_t v)
1080 ndr->print(ndr, "%-25s: 0x%016llx (%llu)", name, (unsigned long long)v, (unsigned long long)v);
1083 _PUBLIC_ void ndr_print_udlongr(struct ndr_print *ndr, const char *name, uint64_t v)
1085 ndr_print_udlong(ndr, name, v);
1088 _PUBLIC_ void ndr_print_dlong(struct ndr_print *ndr, const char *name, int64_t v)
1090 ndr->print(ndr, "%-25s: 0x%016llx (%lld)", name, (unsigned long long)v, (long long)v);
1093 _PUBLIC_ void ndr_print_double(struct ndr_print *ndr, const char *name, double v)
1095 ndr->print(ndr, "%-25s: %f", name, v);
1098 _PUBLIC_ void ndr_print_hyper(struct ndr_print *ndr, const char *name, uint64_t v)
1100 ndr_print_dlong(ndr, name, v);
1103 _PUBLIC_ void ndr_print_pointer(struct ndr_print *ndr, const char *name, void *v)
1105 ndr->print(ndr, "%-25s: %p", name, v);
1108 _PUBLIC_ void ndr_print_ptr(struct ndr_print *ndr, const char *name, const void *p)
1110 if (p) {
1111 ndr->print(ndr, "%-25s: *", name);
1112 } else {
1113 ndr->print(ndr, "%-25s: NULL", name);
1117 _PUBLIC_ void ndr_print_NTTIME(struct ndr_print *ndr, const char *name, NTTIME t)
1119 ndr->print(ndr, "%-25s: %s", name, nt_time_string(ndr, t));
1122 _PUBLIC_ void ndr_print_NTTIME_1sec(struct ndr_print *ndr, const char *name, NTTIME t)
1124 /* this is a standard NTTIME here
1125 * as it's already converted in the pull/push code
1127 ndr_print_NTTIME(ndr, name, t);
1130 _PUBLIC_ void ndr_print_NTTIME_hyper(struct ndr_print *ndr, const char *name, NTTIME t)
1132 ndr_print_NTTIME(ndr, name, t);
1135 _PUBLIC_ void ndr_print_time_t(struct ndr_print *ndr, const char *name, time_t t)
1137 if (t == (time_t)-1 || t == 0) {
1138 ndr->print(ndr, "%-25s: (time_t)%d", name, (int)t);
1139 } else {
1140 ndr->print(ndr, "%-25s: %s", name, timestring(ndr, t));
1144 _PUBLIC_ void ndr_print_uid_t(struct ndr_print *ndr, const char *name, uid_t u)
1146 ndr_print_dlong(ndr, name, u);
1149 _PUBLIC_ void ndr_print_gid_t(struct ndr_print *ndr, const char *name, gid_t g)
1151 ndr_print_dlong(ndr, name, g);
1154 _PUBLIC_ void ndr_print_union(struct ndr_print *ndr, const char *name, int level, const char *type)
1156 if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
1157 ndr->print(ndr, "%-25s: union %s(case 0x%X)", name, type, level);
1158 } else {
1159 ndr->print(ndr, "%-25s: union %s(case %d)", name, type, level);
1163 _PUBLIC_ void ndr_print_bad_level(struct ndr_print *ndr, const char *name, uint16_t level)
1165 ndr->print(ndr, "UNKNOWN LEVEL %u", level);
1168 _PUBLIC_ void ndr_print_array_uint8(struct ndr_print *ndr, const char *name,
1169 const uint8_t *data, uint32_t count)
1171 int i;
1172 #define _ONELINE_LIMIT 32
1174 if (data == NULL) {
1175 ndr->print(ndr, "%s: ARRAY(%d) : NULL", name, count);
1176 return;
1179 if (count <= _ONELINE_LIMIT && (ndr->flags & LIBNDR_PRINT_ARRAY_HEX)) {
1180 char s[(_ONELINE_LIMIT + 1) * 2];
1181 for (i=0;i<count;i++) {
1182 snprintf(&s[i*2], 3, "%02x", data[i]);
1184 s[i*2] = 0;
1185 ndr->print(ndr, "%-25s: %s", name, s);
1186 return;
1189 ndr->print(ndr, "%s: ARRAY(%d)", name, count);
1190 if (count > _ONELINE_LIMIT && (ndr->flags & LIBNDR_PRINT_ARRAY_HEX)) {
1191 ndr_dump_data(ndr, data, count);
1192 return;
1195 ndr->depth++;
1196 for (i=0;i<count;i++) {
1197 char *idx=NULL;
1198 if (asprintf(&idx, "[%d]", i) != -1) {
1199 ndr_print_uint8(ndr, idx, data[i]);
1200 free(idx);
1203 ndr->depth--;
1204 #undef _ONELINE_LIMIT
1207 static void ndr_print_asc(struct ndr_print *ndr, const uint8_t *buf, int len)
1209 int i;
1210 for (i=0;i<len;i++)
1211 ndr->print(ndr, "%c", isprint(buf[i])?buf[i]:'.');
1215 ndr_print version of dump_data()
1217 static void ndr_dump_data(struct ndr_print *ndr, const uint8_t *buf, int len)
1219 int i=0;
1221 ndr->no_newline = true;
1223 for (i=0;i<len;) {
1224 if (i%16 == 0 && i<len) {
1225 ndr->print(ndr, "[%04X] ",i);
1228 ndr->print(ndr, "%02X ",(int)buf[i]);
1229 i++;
1230 if (i%8 == 0) ndr->print(ndr," ");
1231 if (i%16 == 0) {
1232 ndr_print_asc(ndr,&buf[i-16],8); ndr->print(ndr," ");
1233 ndr_print_asc(ndr,&buf[i-8],8); ndr->print(ndr, "\n");
1237 if (i%16) {
1238 int n;
1239 n = 16 - (i%16);
1240 ndr->print(ndr, " ");
1241 if (n>8) ndr->print(ndr," ");
1242 while (n--) ndr->print(ndr," ");
1243 n = MIN(8,i%16);
1244 ndr_print_asc(ndr,&buf[i-(i%16)],n); ndr->print(ndr, " ");
1245 n = (i%16) - n;
1246 if (n>0) ndr_print_asc(ndr,&buf[i-n],n);
1247 ndr->print(ndr,"\n");
1250 ndr->no_newline = false;
1254 _PUBLIC_ void ndr_print_DATA_BLOB(struct ndr_print *ndr, const char *name, DATA_BLOB r)
1256 ndr->print(ndr, "%-25s: DATA_BLOB length=%u", name, (unsigned)r.length);
1257 if (r.length) {
1258 ndr_dump_data(ndr, r.data, r.length);
1264 * Push a DATA_BLOB onto the wire.
1265 * 1) When called with LIBNDR_FLAG_ALIGN* alignment flags set, push padding
1266 * bytes _only_. The length is determined by the alignment required and the
1267 * current ndr offset.
1268 * 2) When called with the LIBNDR_FLAG_REMAINING flag, push the byte array to
1269 * the ndr buffer.
1270 * 3) Otherwise, push a uint3264 length _and_ a corresponding byte array to the
1271 * ndr buffer.
1273 _PUBLIC_ enum ndr_err_code ndr_push_DATA_BLOB(struct ndr_push *ndr, int ndr_flags, DATA_BLOB blob)
1275 if (ndr->flags & LIBNDR_FLAG_REMAINING) {
1276 /* nothing to do */
1277 } else if (ndr->flags & (LIBNDR_ALIGN_FLAGS & ~LIBNDR_FLAG_NOALIGN)) {
1278 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1279 blob.length = NDR_ALIGN(ndr, 2);
1280 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1281 blob.length = NDR_ALIGN(ndr, 4);
1282 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1283 blob.length = NDR_ALIGN(ndr, 8);
1285 NDR_PUSH_ALLOC_SIZE(ndr, blob.data, blob.length);
1286 data_blob_clear(&blob);
1287 } else {
1288 NDR_CHECK(ndr_push_uint3264(ndr, NDR_SCALARS, blob.length));
1290 NDR_CHECK(ndr_push_bytes(ndr, blob.data, blob.length));
1291 return NDR_ERR_SUCCESS;
1295 * Pull a DATA_BLOB from the wire.
1296 * 1) when called with LIBNDR_FLAG_ALIGN* alignment flags set, pull padding
1297 * bytes _only_. The length is determined by the alignment required and the
1298 * current ndr offset.
1299 * 2) When called with the LIBNDR_FLAG_REMAINING flag, pull all remaining bytes
1300 * from the ndr buffer.
1301 * 3) Otherwise, pull a uint3264 length _and_ a corresponding byte array from the
1302 * ndr buffer.
1304 _PUBLIC_ enum ndr_err_code ndr_pull_DATA_BLOB(struct ndr_pull *ndr, int ndr_flags, DATA_BLOB *blob)
1306 uint32_t length = 0;
1308 if (ndr->flags & LIBNDR_FLAG_REMAINING) {
1309 length = ndr->data_size - ndr->offset;
1310 } else if (ndr->flags & (LIBNDR_ALIGN_FLAGS & ~LIBNDR_FLAG_NOALIGN)) {
1311 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1312 length = NDR_ALIGN(ndr, 2);
1313 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1314 length = NDR_ALIGN(ndr, 4);
1315 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1316 length = NDR_ALIGN(ndr, 8);
1318 if (ndr->data_size - ndr->offset < length) {
1319 length = ndr->data_size - ndr->offset;
1321 } else {
1322 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &length));
1324 NDR_PULL_NEED_BYTES(ndr, length);
1325 *blob = data_blob_talloc(ndr->current_mem_ctx, ndr->data+ndr->offset, length);
1326 ndr->offset += length;
1327 return NDR_ERR_SUCCESS;
1330 _PUBLIC_ uint32_t ndr_size_DATA_BLOB(int ret, const DATA_BLOB *data, int flags)
1332 if (!data) return ret;
1333 return ret + data->length;
1336 _PUBLIC_ void ndr_print_bool(struct ndr_print *ndr, const char *name, const bool b)
1338 ndr->print(ndr, "%-25s: %s", name, b?"true":"false");
1341 _PUBLIC_ NTSTATUS ndr_map_error2ntstatus(enum ndr_err_code ndr_err)
1343 switch (ndr_err) {
1344 case NDR_ERR_SUCCESS:
1345 return NT_STATUS_OK;
1346 case NDR_ERR_BUFSIZE:
1347 return NT_STATUS_BUFFER_TOO_SMALL;
1348 case NDR_ERR_TOKEN:
1349 return NT_STATUS_INTERNAL_ERROR;
1350 case NDR_ERR_ALLOC:
1351 return NT_STATUS_NO_MEMORY;
1352 case NDR_ERR_ARRAY_SIZE:
1353 return NT_STATUS_ARRAY_BOUNDS_EXCEEDED;
1354 case NDR_ERR_INVALID_POINTER:
1355 return NT_STATUS_INVALID_PARAMETER_MIX;
1356 case NDR_ERR_UNREAD_BYTES:
1357 return NT_STATUS_PORT_MESSAGE_TOO_LONG;
1358 default:
1359 break;
1362 /* we should map all error codes to different status codes */
1363 return NT_STATUS_INVALID_PARAMETER;
1366 _PUBLIC_ int ndr_map_error2errno(enum ndr_err_code ndr_err)
1368 switch (ndr_err) {
1369 case NDR_ERR_SUCCESS:
1370 return 0;
1371 case NDR_ERR_BUFSIZE:
1372 return ENOSPC;
1373 case NDR_ERR_TOKEN:
1374 return EINVAL;
1375 case NDR_ERR_ALLOC:
1376 return ENOMEM;
1377 case NDR_ERR_ARRAY_SIZE:
1378 return EMSGSIZE;
1379 case NDR_ERR_INVALID_POINTER:
1380 return EINVAL;
1381 case NDR_ERR_UNREAD_BYTES:
1382 return EOVERFLOW;
1383 default:
1384 break;
1387 /* we should map all error codes to different status codes */
1388 return EINVAL;
1391 _PUBLIC_ enum ndr_err_code ndr_push_timespec(struct ndr_push *ndr,
1392 int ndr_flags,
1393 const struct timespec *t)
1395 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
1396 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t->tv_sec));
1397 NDR_CHECK(ndr_push_uint32(ndr, ndr_flags, t->tv_nsec));
1398 return NDR_ERR_SUCCESS;
1401 _PUBLIC_ enum ndr_err_code ndr_pull_timespec(struct ndr_pull *ndr,
1402 int ndr_flags,
1403 struct timespec *t)
1405 uint64_t secs;
1406 uint32_t nsecs;
1407 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
1408 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, &secs));
1409 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &nsecs));
1410 t->tv_sec = secs;
1411 t->tv_nsec = nsecs;
1412 return NDR_ERR_SUCCESS;
1415 _PUBLIC_ void ndr_print_timespec(struct ndr_print *ndr, const char *name,
1416 const struct timespec *t)
1418 ndr->print(ndr, "%-25s: %s.%ld", name, timestring(ndr, t->tv_sec),
1419 (long)t->tv_nsec);
1422 _PUBLIC_ enum ndr_err_code ndr_push_timeval(struct ndr_push *ndr,
1423 int ndr_flags,
1424 const struct timeval *t)
1426 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
1427 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t->tv_sec));
1428 NDR_CHECK(ndr_push_uint32(ndr, ndr_flags, t->tv_usec));
1429 return NDR_ERR_SUCCESS;
1432 _PUBLIC_ enum ndr_err_code ndr_pull_timeval(struct ndr_pull *ndr,
1433 int ndr_flags,
1434 struct timeval *t)
1436 uint64_t secs;
1437 uint32_t usecs;
1438 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
1439 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, &secs));
1440 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &usecs));
1441 t->tv_sec = secs;
1442 t->tv_usec = usecs;
1443 return NDR_ERR_SUCCESS;
1446 _PUBLIC_ void ndr_print_timeval(struct ndr_print *ndr, const char *name,
1447 const struct timeval *t)
1449 ndr->print(ndr, "%-25s: %s.%ld", name, timestring(ndr, t->tv_sec),
1450 (long)t->tv_usec);