rpcclient: Set internal log level to 0
[Samba.git] / librpc / ndr / ndr_basic.c
blob12e39422a03262ca8e954fefca9f7c69dd44e1bf
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;
328 pull a HRESULT
330 _PUBLIC_ enum ndr_err_code ndr_pull_HRESULT(struct ndr_pull *ndr, int ndr_flags, HRESULT *status)
332 uint32_t v;
333 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
334 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
335 *status = HRES_ERROR(v);
336 return NDR_ERR_SUCCESS;
340 parse a uint8_t enum
342 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *v)
344 return ndr_pull_uint8(ndr, ndr_flags, v);
348 parse a uint16_t enum
350 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint16(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
352 return ndr_pull_uint16(ndr, ndr_flags, v);
356 parse a uint1632_t enum (uint32_t on NDR64)
358 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint1632(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
360 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
361 uint32_t v32;
362 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &v32));
363 *v = v32;
364 if (v32 != *v) {
365 DEBUG(0,(__location__ ": non-zero upper 16 bits 0x%08x\n", (unsigned)v32));
366 return NDR_ERR_NDR64;
368 return NDR_ERR_SUCCESS;
370 return ndr_pull_uint16(ndr, ndr_flags, v);
374 parse a uint32_t enum
376 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint32(struct ndr_pull *ndr, int ndr_flags, uint32_t *v)
378 return ndr_pull_uint32(ndr, ndr_flags, v);
382 push a uint8_t enum
384 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint8(struct ndr_push *ndr, int ndr_flags, uint8_t v)
386 return ndr_push_uint8(ndr, ndr_flags, v);
390 push a uint16_t enum
392 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint16(struct ndr_push *ndr, int ndr_flags, uint16_t v)
394 return ndr_push_uint16(ndr, ndr_flags, v);
398 push a uint32_t enum
400 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint32(struct ndr_push *ndr, int ndr_flags, uint32_t v)
402 return ndr_push_uint32(ndr, ndr_flags, v);
406 push a uint1632_t enum
408 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint1632(struct ndr_push *ndr, int ndr_flags, uint16_t v)
410 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
411 return ndr_push_uint32(ndr, ndr_flags, v);
413 return ndr_push_uint16(ndr, ndr_flags, v);
417 push a WERROR
419 _PUBLIC_ enum ndr_err_code ndr_push_WERROR(struct ndr_push *ndr, int ndr_flags, WERROR status)
421 return ndr_push_uint32(ndr, NDR_SCALARS, W_ERROR_V(status));
424 _PUBLIC_ void ndr_print_WERROR(struct ndr_print *ndr, const char *name, WERROR r)
426 ndr->print(ndr, "%-25s: %s", name, win_errstr(r));
430 push a HRESULT
432 _PUBLIC_ enum ndr_err_code ndr_push_HRESULT(struct ndr_push *ndr, int ndr_flags, HRESULT status)
434 return ndr_push_uint32(ndr, NDR_SCALARS, HRES_ERROR_V(status));
437 _PUBLIC_ void ndr_print_HRESULT(struct ndr_print *ndr, const char *name, HRESULT r)
439 ndr->print(ndr, "%-25s: %s", name, hresult_errstr(r));
444 parse a set of bytes
446 _PUBLIC_ enum ndr_err_code ndr_pull_bytes(struct ndr_pull *ndr, uint8_t *data, uint32_t n)
448 NDR_PULL_NEED_BYTES(ndr, n);
449 memcpy(data, ndr->data + ndr->offset, n);
450 ndr->offset += n;
451 return NDR_ERR_SUCCESS;
455 pull an array of uint8
457 _PUBLIC_ enum ndr_err_code ndr_pull_array_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *data, uint32_t n)
459 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
460 if (!(ndr_flags & NDR_SCALARS)) {
461 return NDR_ERR_SUCCESS;
463 return ndr_pull_bytes(ndr, data, n);
467 push a int8_t
469 _PUBLIC_ enum ndr_err_code ndr_push_int8(struct ndr_push *ndr, int ndr_flags, int8_t v)
471 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
472 NDR_PUSH_NEED_BYTES(ndr, 1);
473 SCVAL(ndr->data, ndr->offset, (uint8_t)v);
474 ndr->offset += 1;
475 return NDR_ERR_SUCCESS;
479 push a uint8_t
481 _PUBLIC_ enum ndr_err_code ndr_push_uint8(struct ndr_push *ndr, int ndr_flags, uint8_t v)
483 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
484 NDR_PUSH_NEED_BYTES(ndr, 1);
485 SCVAL(ndr->data, ndr->offset, v);
486 ndr->offset += 1;
487 return NDR_ERR_SUCCESS;
491 push a int16_t
493 _PUBLIC_ enum ndr_err_code ndr_push_int16(struct ndr_push *ndr, int ndr_flags, int16_t v)
495 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
496 NDR_PUSH_ALIGN(ndr, 2);
497 NDR_PUSH_NEED_BYTES(ndr, 2);
498 NDR_SSVAL(ndr, ndr->offset, (uint16_t)v);
499 ndr->offset += 2;
500 return NDR_ERR_SUCCESS;
504 push a uint16_t
506 _PUBLIC_ enum ndr_err_code ndr_push_uint16(struct ndr_push *ndr, int ndr_flags, uint16_t v)
508 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
509 NDR_PUSH_ALIGN(ndr, 2);
510 NDR_PUSH_NEED_BYTES(ndr, 2);
511 NDR_SSVAL(ndr, ndr->offset, v);
512 ndr->offset += 2;
513 return NDR_ERR_SUCCESS;
517 push a uint1632
519 _PUBLIC_ enum ndr_err_code ndr_push_uint1632(struct ndr_push *ndr, int ndr_flags, uint16_t v)
521 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
522 return ndr_push_uint32(ndr, ndr_flags, v);
524 return ndr_push_uint16(ndr, ndr_flags, v);
528 push a int32_t
530 _PUBLIC_ enum ndr_err_code ndr_push_int32(struct ndr_push *ndr, int ndr_flags, int32_t v)
532 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
533 NDR_PUSH_ALIGN(ndr, 4);
534 NDR_PUSH_NEED_BYTES(ndr, 4);
535 NDR_SIVALS(ndr, ndr->offset, v);
536 ndr->offset += 4;
537 return NDR_ERR_SUCCESS;
541 push a uint32_t
543 _PUBLIC_ enum ndr_err_code ndr_push_uint32(struct ndr_push *ndr, int ndr_flags, uint32_t v)
545 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
546 NDR_PUSH_ALIGN(ndr, 4);
547 NDR_PUSH_NEED_BYTES(ndr, 4);
548 NDR_SIVAL(ndr, ndr->offset, v);
549 ndr->offset += 4;
550 return NDR_ERR_SUCCESS;
554 push a uint3264
556 _PUBLIC_ enum ndr_err_code ndr_push_uint3264(struct ndr_push *ndr, int ndr_flags, uint32_t v)
558 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
559 return ndr_push_hyper(ndr, ndr_flags, v);
561 return ndr_push_uint32(ndr, ndr_flags, v);
565 push a udlong
567 _PUBLIC_ enum ndr_err_code ndr_push_udlong(struct ndr_push *ndr, int ndr_flags, uint64_t v)
569 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
570 NDR_PUSH_ALIGN(ndr, 4);
571 NDR_PUSH_NEED_BYTES(ndr, 8);
572 NDR_SIVAL(ndr, ndr->offset, (v & 0xFFFFFFFF));
573 NDR_SIVAL(ndr, ndr->offset+4, (v>>32));
574 ndr->offset += 8;
575 return NDR_ERR_SUCCESS;
579 push a udlongr
581 _PUBLIC_ enum ndr_err_code ndr_push_udlongr(struct ndr_push *ndr, int ndr_flags, uint64_t v)
583 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
584 NDR_PUSH_ALIGN(ndr, 4);
585 NDR_PUSH_NEED_BYTES(ndr, 8);
586 NDR_SIVAL(ndr, ndr->offset, (v>>32));
587 NDR_SIVAL(ndr, ndr->offset+4, (v & 0xFFFFFFFF));
588 ndr->offset += 8;
589 return NDR_ERR_SUCCESS;
593 push a dlong
595 _PUBLIC_ enum ndr_err_code ndr_push_dlong(struct ndr_push *ndr, int ndr_flags, int64_t v)
597 return ndr_push_udlong(ndr, NDR_SCALARS, (uint64_t)v);
601 push a hyper
603 _PUBLIC_ enum ndr_err_code ndr_push_hyper(struct ndr_push *ndr, int ndr_flags, uint64_t v)
605 NDR_PUSH_ALIGN(ndr, 8);
606 if (NDR_BE(ndr)) {
607 return ndr_push_udlongr(ndr, NDR_SCALARS, v);
609 return ndr_push_udlong(ndr, NDR_SCALARS, v);
613 push a double
615 _PUBLIC_ enum ndr_err_code ndr_push_double(struct ndr_push *ndr, int ndr_flags, double v)
617 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
618 NDR_PUSH_ALIGN(ndr, 8);
619 NDR_PUSH_NEED_BYTES(ndr, 8);
620 memcpy(ndr->data+ndr->offset, &v, 8);
621 ndr->offset += 8;
622 return NDR_ERR_SUCCESS;
626 push a pointer
628 _PUBLIC_ enum ndr_err_code ndr_push_pointer(struct ndr_push *ndr, int ndr_flags, void* v)
630 uintptr_t h = (intptr_t)v;
631 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
632 NDR_PUSH_ALIGN(ndr, sizeof(h));
633 NDR_PUSH_NEED_BYTES(ndr, sizeof(h));
634 memcpy(ndr->data+ndr->offset, &h, sizeof(h));
635 ndr->offset += sizeof(h);
636 return NDR_ERR_SUCCESS;
639 _PUBLIC_ enum ndr_err_code ndr_push_align(struct ndr_push *ndr, size_t size)
641 /* this is a nasty hack to make pidl work with NDR64 */
642 if (size == 5) {
643 if (ndr->flags & LIBNDR_FLAG_NDR64) {
644 size = 8;
645 } else {
646 size = 4;
648 } else if (size == 3) {
649 if (ndr->flags & LIBNDR_FLAG_NDR64) {
650 size = 4;
651 } else {
652 size = 2;
655 NDR_PUSH_ALIGN(ndr, size);
656 return NDR_ERR_SUCCESS;
659 _PUBLIC_ enum ndr_err_code ndr_pull_align(struct ndr_pull *ndr, size_t size)
661 /* this is a nasty hack to make pidl work with NDR64 */
662 if (size == 5) {
663 if (ndr->flags & LIBNDR_FLAG_NDR64) {
664 size = 8;
665 } else {
666 size = 4;
668 } else if (size == 3) {
669 if (ndr->flags & LIBNDR_FLAG_NDR64) {
670 size = 4;
671 } else {
672 size = 2;
675 NDR_PULL_ALIGN(ndr, size);
676 return NDR_ERR_SUCCESS;
679 _PUBLIC_ enum ndr_err_code ndr_push_union_align(struct ndr_push *ndr, size_t size)
681 /* MS-RPCE section 2.2.5.3.4.4 */
682 if (ndr->flags & LIBNDR_FLAG_NDR64) {
683 return ndr_push_align(ndr, size);
685 return NDR_ERR_SUCCESS;
688 _PUBLIC_ enum ndr_err_code ndr_pull_union_align(struct ndr_pull *ndr, size_t size)
690 /* MS-RPCE section 2.2.5.3.4.4 */
691 if (ndr->flags & LIBNDR_FLAG_NDR64) {
692 return ndr_pull_align(ndr, size);
694 return NDR_ERR_SUCCESS;
697 _PUBLIC_ enum ndr_err_code ndr_push_trailer_align(struct ndr_push *ndr, size_t size)
699 /* MS-RPCE section 2.2.5.3.4.1 */
700 if (ndr->flags & LIBNDR_FLAG_NDR64) {
701 return ndr_push_align(ndr, size);
703 return NDR_ERR_SUCCESS;
706 _PUBLIC_ enum ndr_err_code ndr_pull_trailer_align(struct ndr_pull *ndr, size_t size)
708 /* MS-RPCE section 2.2.5.3.4.1 */
709 if (ndr->flags & LIBNDR_FLAG_NDR64) {
710 return ndr_pull_align(ndr, size);
712 return NDR_ERR_SUCCESS;
716 push some bytes
718 _PUBLIC_ enum ndr_err_code ndr_push_bytes(struct ndr_push *ndr, const uint8_t *data, uint32_t n)
720 NDR_PUSH_NEED_BYTES(ndr, n);
721 memcpy(ndr->data + ndr->offset, data, n);
722 ndr->offset += n;
723 return NDR_ERR_SUCCESS;
727 push some zero bytes
729 _PUBLIC_ enum ndr_err_code ndr_push_zero(struct ndr_push *ndr, uint32_t n)
731 NDR_PUSH_NEED_BYTES(ndr, n);
732 memset(ndr->data + ndr->offset, 0, n);
733 ndr->offset += n;
734 return NDR_ERR_SUCCESS;
738 push an array of uint8
740 _PUBLIC_ enum ndr_err_code ndr_push_array_uint8(struct ndr_push *ndr, int ndr_flags, const uint8_t *data, uint32_t n)
742 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
743 if (!(ndr_flags & NDR_SCALARS)) {
744 return NDR_ERR_SUCCESS;
746 return ndr_push_bytes(ndr, data, n);
750 push a unique non-zero value if a pointer is non-NULL, otherwise 0
752 _PUBLIC_ enum ndr_err_code ndr_push_unique_ptr(struct ndr_push *ndr, const void *p)
754 uint32_t ptr = 0;
755 if (p) {
756 ptr = ndr->ptr_count * 4;
757 ptr |= 0x00020000;
758 ndr->ptr_count++;
760 return ndr_push_uint3264(ndr, NDR_SCALARS, ptr);
764 push a 'simple' full non-zero value if a pointer is non-NULL, otherwise 0
766 _PUBLIC_ enum ndr_err_code ndr_push_full_ptr(struct ndr_push *ndr, const void *p)
768 uint32_t ptr = 0;
769 if (p) {
770 /* Check if the pointer already exists and has an id */
771 ptr = ndr_token_peek(&ndr->full_ptr_list, p);
772 if (ptr == 0) {
773 ndr->ptr_count++;
774 ptr = ndr->ptr_count;
775 ndr_token_store(ndr, &ndr->full_ptr_list, p, ptr);
778 return ndr_push_uint3264(ndr, NDR_SCALARS, ptr);
782 push always a 0, if a pointer is NULL it's a fatal error
784 _PUBLIC_ enum ndr_err_code ndr_push_ref_ptr(struct ndr_push *ndr)
786 return ndr_push_uint3264(ndr, NDR_SCALARS, 0xAEF1AEF1);
791 push a NTTIME
793 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME(struct ndr_push *ndr, int ndr_flags, NTTIME t)
795 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
796 NDR_CHECK(ndr_push_udlong(ndr, ndr_flags, t));
797 return NDR_ERR_SUCCESS;
801 pull a NTTIME
803 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
805 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
806 NDR_CHECK(ndr_pull_udlong(ndr, ndr_flags, t));
807 return NDR_ERR_SUCCESS;
811 push a NTTIME_1sec
813 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME_1sec(struct ndr_push *ndr, int ndr_flags, NTTIME t)
815 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
816 t /= 10000000;
817 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
818 return NDR_ERR_SUCCESS;
822 pull a NTTIME_1sec
824 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME_1sec(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
826 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
827 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
828 (*t) *= 10000000;
829 return NDR_ERR_SUCCESS;
833 pull a NTTIME_hyper
835 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME_hyper(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
837 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
838 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
839 return NDR_ERR_SUCCESS;
843 push a NTTIME_hyper
845 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME_hyper(struct ndr_push *ndr, int ndr_flags, NTTIME t)
847 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
848 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
849 return NDR_ERR_SUCCESS;
853 push a time_t
855 _PUBLIC_ enum ndr_err_code ndr_push_time_t(struct ndr_push *ndr, int ndr_flags, time_t t)
857 return ndr_push_uint32(ndr, ndr_flags, t);
861 pull a time_t
863 _PUBLIC_ enum ndr_err_code ndr_pull_time_t(struct ndr_pull *ndr, int ndr_flags, time_t *t)
865 uint32_t tt;
866 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &tt));
867 *t = tt;
868 return NDR_ERR_SUCCESS;
873 push a uid_t
875 _PUBLIC_ enum ndr_err_code ndr_push_uid_t(struct ndr_push *ndr, int ndr_flags, uid_t u)
877 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
878 return ndr_push_hyper(ndr, NDR_SCALARS, (uint64_t)u);
882 pull a uid_t
884 _PUBLIC_ enum ndr_err_code ndr_pull_uid_t(struct ndr_pull *ndr, int ndr_flags, uid_t *u)
886 uint64_t uu;
887 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, &uu));
888 *u = (uid_t)uu;
889 if (unlikely(uu != *u)) {
890 DEBUG(0,(__location__ ": uid_t pull doesn't fit 0x%016llx\n",
891 (unsigned long long)uu));
892 return NDR_ERR_NDR64;
894 return NDR_ERR_SUCCESS;
899 push a gid_t
901 _PUBLIC_ enum ndr_err_code ndr_push_gid_t(struct ndr_push *ndr, int ndr_flags, gid_t g)
903 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
904 return ndr_push_hyper(ndr, NDR_SCALARS, (uint64_t)g);
908 pull a gid_t
910 _PUBLIC_ enum ndr_err_code ndr_pull_gid_t(struct ndr_pull *ndr, int ndr_flags, gid_t *g)
912 uint64_t gg;
913 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, &gg));
914 *g = (gid_t)gg;
915 if (unlikely(gg != *g)) {
916 DEBUG(0,(__location__ ": gid_t pull doesn't fit 0x%016llx\n",
917 (unsigned long long)gg));
918 return NDR_ERR_NDR64;
920 return NDR_ERR_SUCCESS;
925 pull a ipv4address
927 _PUBLIC_ enum ndr_err_code ndr_pull_ipv4address(struct ndr_pull *ndr, int ndr_flags, const char **address)
929 uint32_t addr;
930 struct in_addr in;
931 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &addr));
932 in.s_addr = htonl(addr);
933 *address = talloc_strdup(ndr->current_mem_ctx, inet_ntoa(in));
934 NDR_ERR_HAVE_NO_MEMORY(*address);
935 return NDR_ERR_SUCCESS;
939 push a ipv4address
941 _PUBLIC_ enum ndr_err_code ndr_push_ipv4address(struct ndr_push *ndr, int ndr_flags, const char *address)
943 uint32_t addr;
944 if (!is_ipaddress(address)) {
945 return ndr_push_error(ndr, NDR_ERR_IPV4ADDRESS,
946 "Invalid IPv4 address: '%s'",
947 address);
949 addr = inet_addr(address);
950 NDR_CHECK(ndr_push_uint32(ndr, ndr_flags, htonl(addr)));
951 return NDR_ERR_SUCCESS;
955 print a ipv4address
957 _PUBLIC_ void ndr_print_ipv4address(struct ndr_print *ndr, const char *name,
958 const char *address)
960 ndr->print(ndr, "%-25s: %s", name, address);
964 pull a ipv6address
966 #define IPV6_BYTES 16
967 #define IPV6_ADDR_STR_LEN 39
968 _PUBLIC_ enum ndr_err_code ndr_pull_ipv6address(struct ndr_pull *ndr, int ndr_flags, const char **address)
970 uint8_t addr[IPV6_BYTES];
971 char *addr_str = talloc_strdup(ndr->current_mem_ctx, "");
972 int i;
973 NDR_CHECK(ndr_pull_array_uint8(ndr, ndr_flags, addr, IPV6_BYTES));
974 for (i = 0; i < IPV6_BYTES; ++i) {
975 addr_str = talloc_asprintf_append(addr_str, "%02x", addr[i]);
976 /* We need a ':' every second byte but the last one */
977 if (i%2 == 1 && i != (IPV6_BYTES - 1)) {
978 addr_str = talloc_strdup_append(addr_str, ":");
981 *address = addr_str;
982 NDR_ERR_HAVE_NO_MEMORY(*address);
983 return NDR_ERR_SUCCESS;
987 push a ipv6address
989 _PUBLIC_ enum ndr_err_code ndr_push_ipv6address(struct ndr_push *ndr, int ndr_flags, const char *address)
991 #ifdef AF_INET6
992 uint8_t addr[IPV6_BYTES];
993 int ret;
995 if (!is_ipaddress(address)) {
996 return ndr_push_error(ndr, NDR_ERR_IPV6ADDRESS,
997 "Invalid IPv6 address: '%s'",
998 address);
1000 ret = inet_pton(AF_INET6, address, addr);
1001 if (ret <= 0) {
1002 return NDR_ERR_IPV6ADDRESS;
1005 NDR_CHECK(ndr_push_array_uint8(ndr, ndr_flags, addr, IPV6_BYTES));
1007 return NDR_ERR_SUCCESS;
1008 #else
1009 return NDR_ERR_IPV6ADDRESS;
1010 #endif
1014 print a ipv6address
1016 _PUBLIC_ void ndr_print_ipv6address(struct ndr_print *ndr, const char *name,
1017 const char *address)
1019 ndr->print(ndr, "%-25s: %s", name, address);
1021 #undef IPV6_BYTES
1023 _PUBLIC_ void ndr_print_struct(struct ndr_print *ndr, const char *name, const char *type)
1025 ndr->print(ndr, "%s: struct %s", name, type);
1028 _PUBLIC_ void ndr_print_null(struct ndr_print *ndr)
1030 ndr->print(ndr, "UNEXPECTED NULL POINTER");
1033 _PUBLIC_ void ndr_print_enum(struct ndr_print *ndr, const char *name, const char *type,
1034 const char *val, uint32_t value)
1036 if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
1037 ndr->print(ndr, "%-25s: %s (0x%X)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
1038 } else {
1039 ndr->print(ndr, "%-25s: %s (%d)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
1043 _PUBLIC_ void ndr_print_bitmap_flag(struct ndr_print *ndr, size_t size, const char *flag_name, uint32_t flag, uint32_t value)
1045 if (flag == 0) {
1046 return;
1049 /* this is an attempt to support multi-bit bitmap masks */
1050 value &= flag;
1052 while (!(flag & 1)) {
1053 flag >>= 1;
1054 value >>= 1;
1056 if (flag == 1) {
1057 ndr->print(ndr, " %d: %-25s", value, flag_name);
1058 } else {
1059 ndr->print(ndr, "0x%02x: %-25s (%d)", value, flag_name, value);
1063 _PUBLIC_ void ndr_print_int8(struct ndr_print *ndr, const char *name, int8_t v)
1065 ndr->print(ndr, "%-25s: %d", name, v);
1068 _PUBLIC_ void ndr_print_uint8(struct ndr_print *ndr, const char *name, uint8_t v)
1070 ndr->print(ndr, "%-25s: 0x%02x (%u)", name, v, v);
1073 _PUBLIC_ void ndr_print_int16(struct ndr_print *ndr, const char *name, int16_t v)
1075 ndr->print(ndr, "%-25s: %d", name, v);
1078 _PUBLIC_ void ndr_print_uint16(struct ndr_print *ndr, const char *name, uint16_t v)
1080 ndr->print(ndr, "%-25s: 0x%04x (%u)", name, v, v);
1083 _PUBLIC_ void ndr_print_int32(struct ndr_print *ndr, const char *name, int32_t v)
1085 ndr->print(ndr, "%-25s: %d", name, v);
1088 _PUBLIC_ void ndr_print_uint32(struct ndr_print *ndr, const char *name, uint32_t v)
1090 ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
1093 _PUBLIC_ void ndr_print_int3264(struct ndr_print *ndr, const char *name, int32_t v)
1095 ndr->print(ndr, "%-25s: %d", name, v);
1098 _PUBLIC_ void ndr_print_uint3264(struct ndr_print *ndr, const char *name, uint32_t v)
1100 ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
1103 _PUBLIC_ void ndr_print_udlong(struct ndr_print *ndr, const char *name, uint64_t v)
1105 ndr->print(ndr, "%-25s: 0x%016llx (%llu)", name, (unsigned long long)v, (unsigned long long)v);
1108 _PUBLIC_ void ndr_print_udlongr(struct ndr_print *ndr, const char *name, uint64_t v)
1110 ndr_print_udlong(ndr, name, v);
1113 _PUBLIC_ void ndr_print_dlong(struct ndr_print *ndr, const char *name, int64_t v)
1115 ndr->print(ndr, "%-25s: 0x%016llx (%lld)", name, (unsigned long long)v, (long long)v);
1118 _PUBLIC_ void ndr_print_double(struct ndr_print *ndr, const char *name, double v)
1120 ndr->print(ndr, "%-25s: %f", name, v);
1123 _PUBLIC_ void ndr_print_hyper(struct ndr_print *ndr, const char *name, uint64_t v)
1125 ndr_print_dlong(ndr, name, v);
1128 _PUBLIC_ void ndr_print_pointer(struct ndr_print *ndr, const char *name, void *v)
1130 ndr->print(ndr, "%-25s: %p", name, v);
1133 _PUBLIC_ void ndr_print_ptr(struct ndr_print *ndr, const char *name, const void *p)
1135 if (p) {
1136 ndr->print(ndr, "%-25s: *", name);
1137 } else {
1138 ndr->print(ndr, "%-25s: NULL", name);
1142 _PUBLIC_ void ndr_print_NTTIME(struct ndr_print *ndr, const char *name, NTTIME t)
1144 ndr->print(ndr, "%-25s: %s", name, nt_time_string(ndr, t));
1147 _PUBLIC_ void ndr_print_NTTIME_1sec(struct ndr_print *ndr, const char *name, NTTIME t)
1149 /* this is a standard NTTIME here
1150 * as it's already converted in the pull/push code
1152 ndr_print_NTTIME(ndr, name, t);
1155 _PUBLIC_ void ndr_print_NTTIME_hyper(struct ndr_print *ndr, const char *name, NTTIME t)
1157 ndr_print_NTTIME(ndr, name, t);
1160 _PUBLIC_ void ndr_print_time_t(struct ndr_print *ndr, const char *name, time_t t)
1162 if (t == (time_t)-1 || t == 0) {
1163 ndr->print(ndr, "%-25s: (time_t)%d", name, (int)t);
1164 } else {
1165 ndr->print(ndr, "%-25s: %s", name, timestring(ndr, t));
1169 _PUBLIC_ void ndr_print_uid_t(struct ndr_print *ndr, const char *name, uid_t u)
1171 ndr_print_dlong(ndr, name, u);
1174 _PUBLIC_ void ndr_print_gid_t(struct ndr_print *ndr, const char *name, gid_t g)
1176 ndr_print_dlong(ndr, name, g);
1179 _PUBLIC_ void ndr_print_union(struct ndr_print *ndr, const char *name, int level, const char *type)
1181 if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
1182 ndr->print(ndr, "%-25s: union %s(case 0x%X)", name, type, level);
1183 } else {
1184 ndr->print(ndr, "%-25s: union %s(case %d)", name, type, level);
1188 _PUBLIC_ void ndr_print_bad_level(struct ndr_print *ndr, const char *name, uint16_t level)
1190 ndr->print(ndr, "UNKNOWN LEVEL %u", level);
1193 _PUBLIC_ void ndr_print_array_uint8(struct ndr_print *ndr, const char *name,
1194 const uint8_t *data, uint32_t count)
1196 int i;
1197 #define _ONELINE_LIMIT 32
1199 if (data == NULL) {
1200 ndr->print(ndr, "%s: ARRAY(%d) : NULL", name, count);
1201 return;
1204 if (count <= _ONELINE_LIMIT && (ndr->flags & LIBNDR_PRINT_ARRAY_HEX)) {
1205 char s[(_ONELINE_LIMIT + 1) * 2];
1206 for (i=0;i<count;i++) {
1207 snprintf(&s[i*2], 3, "%02x", data[i]);
1209 s[i*2] = 0;
1210 ndr->print(ndr, "%-25s: %s", name, s);
1211 return;
1214 ndr->print(ndr, "%s: ARRAY(%d)", name, count);
1215 if (count > _ONELINE_LIMIT && (ndr->flags & LIBNDR_PRINT_ARRAY_HEX)) {
1216 ndr_dump_data(ndr, data, count);
1217 return;
1220 ndr->depth++;
1221 for (i=0;i<count;i++) {
1222 char *idx=NULL;
1223 if (asprintf(&idx, "[%d]", i) != -1) {
1224 ndr_print_uint8(ndr, idx, data[i]);
1225 free(idx);
1228 ndr->depth--;
1229 #undef _ONELINE_LIMIT
1232 static void ndr_print_dump_data_cb(const char *buf, void *private_data)
1234 struct ndr_print *ndr = (struct ndr_print *)private_data;
1236 ndr->print(ndr, "%s", buf);
1240 ndr_print version of dump_data()
1242 static void ndr_dump_data(struct ndr_print *ndr, const uint8_t *buf, int len)
1244 ndr->no_newline = true;
1245 dump_data_cb(buf, len, true, ndr_print_dump_data_cb, ndr);
1246 ndr->no_newline = false;
1250 _PUBLIC_ void ndr_print_DATA_BLOB(struct ndr_print *ndr, const char *name, DATA_BLOB r)
1252 ndr->print(ndr, "%-25s: DATA_BLOB length=%u", name, (unsigned)r.length);
1253 if (r.length) {
1254 ndr_dump_data(ndr, r.data, r.length);
1260 * Push a DATA_BLOB onto the wire.
1261 * 1) When called with LIBNDR_FLAG_ALIGN* alignment flags set, push padding
1262 * bytes _only_. The length is determined by the alignment required and the
1263 * current ndr offset.
1264 * 2) When called with the LIBNDR_FLAG_REMAINING flag, push the byte array to
1265 * the ndr buffer.
1266 * 3) Otherwise, push a uint3264 length _and_ a corresponding byte array to the
1267 * ndr buffer.
1269 _PUBLIC_ enum ndr_err_code ndr_push_DATA_BLOB(struct ndr_push *ndr, int ndr_flags, DATA_BLOB blob)
1271 if (ndr->flags & LIBNDR_FLAG_REMAINING) {
1272 /* nothing to do */
1273 } else if (ndr->flags & (LIBNDR_ALIGN_FLAGS & ~LIBNDR_FLAG_NOALIGN)) {
1274 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1275 blob.length = NDR_ALIGN(ndr, 2);
1276 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1277 blob.length = NDR_ALIGN(ndr, 4);
1278 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1279 blob.length = NDR_ALIGN(ndr, 8);
1281 NDR_PUSH_ALLOC_SIZE(ndr, blob.data, blob.length);
1282 data_blob_clear(&blob);
1283 } else {
1284 NDR_CHECK(ndr_push_uint3264(ndr, NDR_SCALARS, blob.length));
1286 NDR_CHECK(ndr_push_bytes(ndr, blob.data, blob.length));
1287 return NDR_ERR_SUCCESS;
1291 * Pull a DATA_BLOB from the wire.
1292 * 1) when called with LIBNDR_FLAG_ALIGN* alignment flags set, pull padding
1293 * bytes _only_. The length is determined by the alignment required and the
1294 * current ndr offset.
1295 * 2) When called with the LIBNDR_FLAG_REMAINING flag, pull all remaining bytes
1296 * from the ndr buffer.
1297 * 3) Otherwise, pull a uint3264 length _and_ a corresponding byte array from the
1298 * ndr buffer.
1300 _PUBLIC_ enum ndr_err_code ndr_pull_DATA_BLOB(struct ndr_pull *ndr, int ndr_flags, DATA_BLOB *blob)
1302 uint32_t length = 0;
1304 if (ndr->flags & LIBNDR_FLAG_REMAINING) {
1305 length = ndr->data_size - ndr->offset;
1306 } else if (ndr->flags & (LIBNDR_ALIGN_FLAGS & ~LIBNDR_FLAG_NOALIGN)) {
1307 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1308 length = NDR_ALIGN(ndr, 2);
1309 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1310 length = NDR_ALIGN(ndr, 4);
1311 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1312 length = NDR_ALIGN(ndr, 8);
1314 if (ndr->data_size - ndr->offset < length) {
1315 length = ndr->data_size - ndr->offset;
1317 } else {
1318 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &length));
1320 NDR_PULL_NEED_BYTES(ndr, length);
1321 *blob = data_blob_talloc(ndr->current_mem_ctx, ndr->data+ndr->offset, length);
1322 ndr->offset += length;
1323 return NDR_ERR_SUCCESS;
1326 _PUBLIC_ uint32_t ndr_size_DATA_BLOB(int ret, const DATA_BLOB *data, int flags)
1328 if (!data) return ret;
1329 return ret + data->length;
1332 _PUBLIC_ void ndr_print_bool(struct ndr_print *ndr, const char *name, const bool b)
1334 ndr->print(ndr, "%-25s: %s", name, b?"true":"false");
1337 _PUBLIC_ NTSTATUS ndr_map_error2ntstatus(enum ndr_err_code ndr_err)
1339 switch (ndr_err) {
1340 case NDR_ERR_SUCCESS:
1341 return NT_STATUS_OK;
1342 case NDR_ERR_BUFSIZE:
1343 return NT_STATUS_BUFFER_TOO_SMALL;
1344 case NDR_ERR_TOKEN:
1345 return NT_STATUS_INTERNAL_ERROR;
1346 case NDR_ERR_ALLOC:
1347 return NT_STATUS_NO_MEMORY;
1348 case NDR_ERR_ARRAY_SIZE:
1349 return NT_STATUS_ARRAY_BOUNDS_EXCEEDED;
1350 case NDR_ERR_INVALID_POINTER:
1351 return NT_STATUS_INVALID_PARAMETER_MIX;
1352 case NDR_ERR_UNREAD_BYTES:
1353 return NT_STATUS_PORT_MESSAGE_TOO_LONG;
1354 default:
1355 break;
1358 /* we should map all error codes to different status codes */
1359 return NT_STATUS_INVALID_PARAMETER;
1362 _PUBLIC_ int ndr_map_error2errno(enum ndr_err_code ndr_err)
1364 switch (ndr_err) {
1365 case NDR_ERR_SUCCESS:
1366 return 0;
1367 case NDR_ERR_BUFSIZE:
1368 return ENOSPC;
1369 case NDR_ERR_TOKEN:
1370 return EINVAL;
1371 case NDR_ERR_ALLOC:
1372 return ENOMEM;
1373 case NDR_ERR_ARRAY_SIZE:
1374 return EMSGSIZE;
1375 case NDR_ERR_INVALID_POINTER:
1376 return EINVAL;
1377 case NDR_ERR_UNREAD_BYTES:
1378 return EOVERFLOW;
1379 default:
1380 break;
1383 /* we should map all error codes to different status codes */
1384 return EINVAL;
1387 _PUBLIC_ enum ndr_err_code ndr_push_timespec(struct ndr_push *ndr,
1388 int ndr_flags,
1389 const struct timespec *t)
1391 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
1392 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t->tv_sec));
1393 NDR_CHECK(ndr_push_uint32(ndr, ndr_flags, t->tv_nsec));
1394 return NDR_ERR_SUCCESS;
1397 _PUBLIC_ enum ndr_err_code ndr_pull_timespec(struct ndr_pull *ndr,
1398 int ndr_flags,
1399 struct timespec *t)
1401 uint64_t secs;
1402 uint32_t nsecs;
1403 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
1404 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, &secs));
1405 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &nsecs));
1406 t->tv_sec = secs;
1407 t->tv_nsec = nsecs;
1408 return NDR_ERR_SUCCESS;
1411 _PUBLIC_ void ndr_print_timespec(struct ndr_print *ndr, const char *name,
1412 const struct timespec *t)
1414 ndr->print(ndr, "%-25s: %s.%ld", name, timestring(ndr, t->tv_sec),
1415 (long)t->tv_nsec);
1418 _PUBLIC_ enum ndr_err_code ndr_push_timeval(struct ndr_push *ndr,
1419 int ndr_flags,
1420 const struct timeval *t)
1422 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
1423 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t->tv_sec));
1424 NDR_CHECK(ndr_push_uint32(ndr, ndr_flags, t->tv_usec));
1425 return NDR_ERR_SUCCESS;
1428 _PUBLIC_ enum ndr_err_code ndr_pull_timeval(struct ndr_pull *ndr,
1429 int ndr_flags,
1430 struct timeval *t)
1432 uint64_t secs;
1433 uint32_t usecs;
1434 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
1435 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, &secs));
1436 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &usecs));
1437 t->tv_sec = secs;
1438 t->tv_usec = usecs;
1439 return NDR_ERR_SUCCESS;
1442 _PUBLIC_ void ndr_print_timeval(struct ndr_print *ndr, const char *name,
1443 const struct timeval *t)
1445 ndr->print(ndr, "%-25s: %s.%ld", name, timestring(ndr, t->tv_sec),
1446 (long)t->tv_usec);