VERSION: Bump version up to 4.8.0rc1...
[Samba.git] / librpc / ndr / ndr_basic.c
blobc874f340388e006cf05a773e3d208fb44f65ad72
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 "replace.h"
23 #include "system/network.h"
24 #include "librpc/ndr/libndr.h"
25 #include "lib/util/util_net.h"
26 #include "lib/util/debug.h"
27 #include "lib/util/util.h"
29 #define NDR_SVAL(ndr, ofs) (NDR_BE(ndr)?RSVAL(ndr->data,ofs):SVAL(ndr->data,ofs))
30 #define NDR_IVAL(ndr, ofs) (NDR_BE(ndr)?RIVAL(ndr->data,ofs):IVAL(ndr->data,ofs))
31 #define NDR_IVALS(ndr, ofs) (NDR_BE(ndr)?RIVALS(ndr->data,ofs):IVALS(ndr->data,ofs))
32 #define NDR_SSVAL(ndr, ofs, v) do { if (NDR_BE(ndr)) { RSSVAL(ndr->data,ofs,v); } else SSVAL(ndr->data,ofs,v); } while (0)
33 #define NDR_SIVAL(ndr, ofs, v) do { if (NDR_BE(ndr)) { RSIVAL(ndr->data,ofs,v); } else SIVAL(ndr->data,ofs,v); } while (0)
34 #define NDR_SIVALS(ndr, ofs, v) do { if (NDR_BE(ndr)) { RSIVALS(ndr->data,ofs,v); } else SIVALS(ndr->data,ofs,v); } while (0)
37 static void ndr_dump_data(struct ndr_print *ndr, const uint8_t *buf, int len);
40 check for data leaks from the server by looking for non-zero pad bytes
41 these could also indicate that real structure elements have been
42 mistaken for padding in the IDL
44 _PUBLIC_ void ndr_check_padding(struct ndr_pull *ndr, size_t n)
46 size_t ofs2 = (ndr->offset + (n-1)) & ~(n-1);
47 int i;
48 for (i=ndr->offset;i<ofs2;i++) {
49 if (ndr->data[i] != 0) {
50 break;
53 if (i<ofs2) {
54 DEBUG(0,("WARNING: Non-zero padding to %d: ", (int)n));
55 for (i=ndr->offset;i<ofs2;i++) {
56 DEBUG(0,("%02x ", ndr->data[i]));
58 DEBUG(0,("\n"));
64 parse a int8_t
66 _PUBLIC_ enum ndr_err_code ndr_pull_int8(struct ndr_pull *ndr, int ndr_flags, int8_t *v)
68 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
69 NDR_PULL_NEED_BYTES(ndr, 1);
70 *v = (int8_t)CVAL(ndr->data, ndr->offset);
71 ndr->offset += 1;
72 return NDR_ERR_SUCCESS;
76 parse a uint8_t
78 _PUBLIC_ enum ndr_err_code ndr_pull_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *v)
80 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
81 NDR_PULL_NEED_BYTES(ndr, 1);
82 *v = CVAL(ndr->data, ndr->offset);
83 ndr->offset += 1;
84 return NDR_ERR_SUCCESS;
88 parse a int16_t
90 _PUBLIC_ enum ndr_err_code ndr_pull_int16(struct ndr_pull *ndr, int ndr_flags, int16_t *v)
92 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
93 NDR_PULL_ALIGN(ndr, 2);
94 NDR_PULL_NEED_BYTES(ndr, 2);
95 *v = (uint16_t)NDR_SVAL(ndr, ndr->offset);
96 ndr->offset += 2;
97 return NDR_ERR_SUCCESS;
101 parse a uint16_t
103 _PUBLIC_ enum ndr_err_code ndr_pull_uint16(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
105 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
106 NDR_PULL_ALIGN(ndr, 2);
107 NDR_PULL_NEED_BYTES(ndr, 2);
108 *v = NDR_SVAL(ndr, ndr->offset);
109 ndr->offset += 2;
110 return NDR_ERR_SUCCESS;
114 parse a uint1632_t
116 _PUBLIC_ enum ndr_err_code ndr_pull_uint1632(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
118 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
119 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
120 uint32_t v32 = 0;
121 enum ndr_err_code err = ndr_pull_uint32(ndr, ndr_flags, &v32);
122 *v = v32;
123 if (unlikely(v32 != *v)) {
124 DEBUG(0,(__location__ ": non-zero upper 16 bits 0x%08x\n", (unsigned)v32));
125 return NDR_ERR_NDR64;
127 return err;
129 return ndr_pull_uint16(ndr, ndr_flags, v);
133 parse a int32_t
135 _PUBLIC_ enum ndr_err_code ndr_pull_int32(struct ndr_pull *ndr, int ndr_flags, int32_t *v)
137 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
138 NDR_PULL_ALIGN(ndr, 4);
139 NDR_PULL_NEED_BYTES(ndr, 4);
140 *v = NDR_IVALS(ndr, ndr->offset);
141 ndr->offset += 4;
142 return NDR_ERR_SUCCESS;
146 parse a uint32_t
148 _PUBLIC_ enum ndr_err_code ndr_pull_uint32(struct ndr_pull *ndr, int ndr_flags, uint32_t *v)
150 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
151 NDR_PULL_ALIGN(ndr, 4);
152 NDR_PULL_NEED_BYTES(ndr, 4);
153 *v = NDR_IVAL(ndr, ndr->offset);
154 ndr->offset += 4;
155 return NDR_ERR_SUCCESS;
159 parse a arch dependent uint32/uint64
161 _PUBLIC_ enum ndr_err_code ndr_pull_uint3264(struct ndr_pull *ndr, int ndr_flags, uint32_t *v)
163 uint64_t v64;
164 enum ndr_err_code err;
165 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
166 if (likely(!(ndr->flags & LIBNDR_FLAG_NDR64))) {
167 return ndr_pull_uint32(ndr, ndr_flags, v);
169 err = ndr_pull_hyper(ndr, ndr_flags, &v64);
170 *v = (uint32_t)v64;
171 if (unlikely(v64 != *v)) {
172 DEBUG(0,(__location__ ": non-zero upper 32 bits 0x%016llx\n",
173 (unsigned long long)v64));
174 return ndr_pull_error(ndr, NDR_ERR_NDR64, __location__ ": non-zero upper 32 bits 0x%016llx\n",
175 (unsigned long long)v64);
177 return err;
181 parse a double
183 _PUBLIC_ enum ndr_err_code ndr_pull_double(struct ndr_pull *ndr, int ndr_flags, double *v)
185 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
186 NDR_PULL_ALIGN(ndr, 8);
187 NDR_PULL_NEED_BYTES(ndr, 8);
188 memcpy(v, ndr->data+ndr->offset, 8);
189 ndr->offset += 8;
190 return NDR_ERR_SUCCESS;
194 parse a pointer referent identifier stored in 2 bytes
196 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr_short(struct ndr_pull *ndr, uint16_t *v)
198 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, v));
199 if (*v != 0) {
200 ndr->ptr_count++;
202 *(v) -= ndr->relative_rap_convert;
203 return NDR_ERR_SUCCESS;
207 parse a pointer referent identifier
209 _PUBLIC_ enum ndr_err_code ndr_pull_generic_ptr(struct ndr_pull *ndr, uint32_t *v)
211 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, v));
212 if (*v != 0) {
213 ndr->ptr_count++;
215 return NDR_ERR_SUCCESS;
219 parse a ref pointer referent identifier
221 _PUBLIC_ enum ndr_err_code ndr_pull_ref_ptr(struct ndr_pull *ndr, uint32_t *v)
223 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, v));
224 /* ref pointers always point to data */
225 *v = 1;
226 return NDR_ERR_SUCCESS;
230 parse a udlong
232 _PUBLIC_ enum ndr_err_code ndr_pull_udlong(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
234 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
235 NDR_PULL_ALIGN(ndr, 4);
236 NDR_PULL_NEED_BYTES(ndr, 8);
237 *v = NDR_IVAL(ndr, ndr->offset);
238 *v |= (uint64_t)(NDR_IVAL(ndr, ndr->offset+4)) << 32;
239 ndr->offset += 8;
240 return NDR_ERR_SUCCESS;
244 parse a udlongr
246 _PUBLIC_ enum ndr_err_code ndr_pull_udlongr(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
248 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
249 NDR_PULL_ALIGN(ndr, 4);
250 NDR_PULL_NEED_BYTES(ndr, 8);
251 *v = ((uint64_t)NDR_IVAL(ndr, ndr->offset)) << 32;
252 *v |= NDR_IVAL(ndr, ndr->offset+4);
253 ndr->offset += 8;
254 return NDR_ERR_SUCCESS;
258 parse a dlong
260 _PUBLIC_ enum ndr_err_code ndr_pull_dlong(struct ndr_pull *ndr, int ndr_flags, int64_t *v)
262 return ndr_pull_udlong(ndr, ndr_flags, (uint64_t *)v);
266 parse a hyper
268 _PUBLIC_ enum ndr_err_code ndr_pull_hyper(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
270 NDR_PULL_ALIGN(ndr, 8);
271 if (NDR_BE(ndr)) {
272 return ndr_pull_udlongr(ndr, ndr_flags, v);
274 return ndr_pull_udlong(ndr, ndr_flags, v);
278 parse a pointer
280 _PUBLIC_ enum ndr_err_code ndr_pull_pointer(struct ndr_pull *ndr, int ndr_flags, void* *v)
282 uintptr_t h;
283 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
284 NDR_PULL_ALIGN(ndr, sizeof(h));
285 NDR_PULL_NEED_BYTES(ndr, sizeof(h));
286 memcpy(&h, ndr->data+ndr->offset, sizeof(h));
287 ndr->offset += sizeof(h);
288 *v = (void *)h;
289 return NDR_ERR_SUCCESS;
293 pull a NTSTATUS
295 _PUBLIC_ enum ndr_err_code ndr_pull_NTSTATUS(struct ndr_pull *ndr, int ndr_flags, NTSTATUS *status)
297 uint32_t v;
298 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
299 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
300 *status = NT_STATUS(v);
301 return NDR_ERR_SUCCESS;
305 push a NTSTATUS
307 _PUBLIC_ enum ndr_err_code ndr_push_NTSTATUS(struct ndr_push *ndr, int ndr_flags, NTSTATUS status)
309 return ndr_push_uint32(ndr, ndr_flags, NT_STATUS_V(status));
312 _PUBLIC_ void ndr_print_NTSTATUS(struct ndr_print *ndr, const char *name, NTSTATUS r)
314 ndr->print(ndr, "%-25s: %s", name, nt_errstr(r));
318 pull a WERROR
320 _PUBLIC_ enum ndr_err_code ndr_pull_WERROR(struct ndr_pull *ndr, int ndr_flags, WERROR *status)
322 uint32_t v;
323 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
324 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
325 *status = W_ERROR(v);
326 return NDR_ERR_SUCCESS;
330 pull a HRESULT
332 _PUBLIC_ enum ndr_err_code ndr_pull_HRESULT(struct ndr_pull *ndr, int ndr_flags, HRESULT *status)
334 uint32_t v;
335 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
336 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
337 *status = HRES_ERROR(v);
338 return NDR_ERR_SUCCESS;
342 parse a uint8_t enum
344 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *v)
346 return ndr_pull_uint8(ndr, ndr_flags, v);
350 parse a uint16_t enum
352 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint16(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
354 return ndr_pull_uint16(ndr, ndr_flags, v);
358 parse a uint1632_t enum (uint32_t on NDR64)
360 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint1632(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
362 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
363 uint32_t v32;
364 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &v32));
365 *v = v32;
366 if (v32 != *v) {
367 DEBUG(0,(__location__ ": non-zero upper 16 bits 0x%08x\n", (unsigned)v32));
368 return NDR_ERR_NDR64;
370 return NDR_ERR_SUCCESS;
372 return ndr_pull_uint16(ndr, ndr_flags, v);
376 parse a uint32_t enum
378 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint32(struct ndr_pull *ndr, int ndr_flags, uint32_t *v)
380 return ndr_pull_uint32(ndr, ndr_flags, v);
384 push a uint8_t enum
386 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint8(struct ndr_push *ndr, int ndr_flags, uint8_t v)
388 return ndr_push_uint8(ndr, ndr_flags, v);
392 push a uint16_t enum
394 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint16(struct ndr_push *ndr, int ndr_flags, uint16_t v)
396 return ndr_push_uint16(ndr, ndr_flags, v);
400 push a uint32_t enum
402 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint32(struct ndr_push *ndr, int ndr_flags, uint32_t v)
404 return ndr_push_uint32(ndr, ndr_flags, v);
408 push a uint1632_t enum
410 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint1632(struct ndr_push *ndr, int ndr_flags, uint16_t v)
412 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
413 return ndr_push_uint32(ndr, ndr_flags, v);
415 return ndr_push_uint16(ndr, ndr_flags, v);
419 push a WERROR
421 _PUBLIC_ enum ndr_err_code ndr_push_WERROR(struct ndr_push *ndr, int ndr_flags, WERROR status)
423 return ndr_push_uint32(ndr, NDR_SCALARS, W_ERROR_V(status));
426 _PUBLIC_ void ndr_print_WERROR(struct ndr_print *ndr, const char *name, WERROR r)
428 ndr->print(ndr, "%-25s: %s", name, win_errstr(r));
432 push a HRESULT
434 _PUBLIC_ enum ndr_err_code ndr_push_HRESULT(struct ndr_push *ndr, int ndr_flags, HRESULT status)
436 return ndr_push_uint32(ndr, NDR_SCALARS, HRES_ERROR_V(status));
439 _PUBLIC_ void ndr_print_HRESULT(struct ndr_print *ndr, const char *name, HRESULT r)
441 ndr->print(ndr, "%-25s: %s", name, hresult_errstr(r));
446 parse a set of bytes
448 _PUBLIC_ enum ndr_err_code ndr_pull_bytes(struct ndr_pull *ndr, uint8_t *data, uint32_t n)
450 NDR_PULL_NEED_BYTES(ndr, n);
451 memcpy(data, ndr->data + ndr->offset, n);
452 ndr->offset += n;
453 return NDR_ERR_SUCCESS;
457 pull an array of uint8
459 _PUBLIC_ enum ndr_err_code ndr_pull_array_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *data, uint32_t n)
461 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
462 if (!(ndr_flags & NDR_SCALARS)) {
463 return NDR_ERR_SUCCESS;
465 return ndr_pull_bytes(ndr, data, n);
469 push a int8_t
471 _PUBLIC_ enum ndr_err_code ndr_push_int8(struct ndr_push *ndr, int ndr_flags, int8_t v)
473 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
474 NDR_PUSH_NEED_BYTES(ndr, 1);
475 SCVAL(ndr->data, ndr->offset, (uint8_t)v);
476 ndr->offset += 1;
477 return NDR_ERR_SUCCESS;
481 push a uint8_t
483 _PUBLIC_ enum ndr_err_code ndr_push_uint8(struct ndr_push *ndr, int ndr_flags, uint8_t v)
485 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
486 NDR_PUSH_NEED_BYTES(ndr, 1);
487 SCVAL(ndr->data, ndr->offset, v);
488 ndr->offset += 1;
489 return NDR_ERR_SUCCESS;
493 push a int16_t
495 _PUBLIC_ enum ndr_err_code ndr_push_int16(struct ndr_push *ndr, int ndr_flags, int16_t v)
497 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
498 NDR_PUSH_ALIGN(ndr, 2);
499 NDR_PUSH_NEED_BYTES(ndr, 2);
500 NDR_SSVAL(ndr, ndr->offset, (uint16_t)v);
501 ndr->offset += 2;
502 return NDR_ERR_SUCCESS;
506 push a uint16_t
508 _PUBLIC_ enum ndr_err_code ndr_push_uint16(struct ndr_push *ndr, int ndr_flags, uint16_t v)
510 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
511 NDR_PUSH_ALIGN(ndr, 2);
512 NDR_PUSH_NEED_BYTES(ndr, 2);
513 NDR_SSVAL(ndr, ndr->offset, v);
514 ndr->offset += 2;
515 return NDR_ERR_SUCCESS;
519 push a uint1632
521 _PUBLIC_ enum ndr_err_code ndr_push_uint1632(struct ndr_push *ndr, int ndr_flags, uint16_t v)
523 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
524 return ndr_push_uint32(ndr, ndr_flags, v);
526 return ndr_push_uint16(ndr, ndr_flags, v);
530 push a int32_t
532 _PUBLIC_ enum ndr_err_code ndr_push_int32(struct ndr_push *ndr, int ndr_flags, int32_t v)
534 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
535 NDR_PUSH_ALIGN(ndr, 4);
536 NDR_PUSH_NEED_BYTES(ndr, 4);
537 NDR_SIVALS(ndr, ndr->offset, v);
538 ndr->offset += 4;
539 return NDR_ERR_SUCCESS;
543 push a uint32_t
545 _PUBLIC_ enum ndr_err_code ndr_push_uint32(struct ndr_push *ndr, int ndr_flags, uint32_t v)
547 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
548 NDR_PUSH_ALIGN(ndr, 4);
549 NDR_PUSH_NEED_BYTES(ndr, 4);
550 NDR_SIVAL(ndr, ndr->offset, v);
551 ndr->offset += 4;
552 return NDR_ERR_SUCCESS;
556 push a uint3264
558 _PUBLIC_ enum ndr_err_code ndr_push_uint3264(struct ndr_push *ndr, int ndr_flags, uint32_t v)
560 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
561 return ndr_push_hyper(ndr, ndr_flags, v);
563 return ndr_push_uint32(ndr, ndr_flags, v);
567 push a udlong
569 _PUBLIC_ enum ndr_err_code ndr_push_udlong(struct ndr_push *ndr, int ndr_flags, uint64_t v)
571 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
572 NDR_PUSH_ALIGN(ndr, 4);
573 NDR_PUSH_NEED_BYTES(ndr, 8);
574 NDR_SIVAL(ndr, ndr->offset, (v & 0xFFFFFFFF));
575 NDR_SIVAL(ndr, ndr->offset+4, (v>>32));
576 ndr->offset += 8;
577 return NDR_ERR_SUCCESS;
581 push a udlongr
583 _PUBLIC_ enum ndr_err_code ndr_push_udlongr(struct ndr_push *ndr, int ndr_flags, uint64_t v)
585 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
586 NDR_PUSH_ALIGN(ndr, 4);
587 NDR_PUSH_NEED_BYTES(ndr, 8);
588 NDR_SIVAL(ndr, ndr->offset, (v>>32));
589 NDR_SIVAL(ndr, ndr->offset+4, (v & 0xFFFFFFFF));
590 ndr->offset += 8;
591 return NDR_ERR_SUCCESS;
595 push a dlong
597 _PUBLIC_ enum ndr_err_code ndr_push_dlong(struct ndr_push *ndr, int ndr_flags, int64_t v)
599 return ndr_push_udlong(ndr, NDR_SCALARS, (uint64_t)v);
603 push a hyper
605 _PUBLIC_ enum ndr_err_code ndr_push_hyper(struct ndr_push *ndr, int ndr_flags, uint64_t v)
607 NDR_PUSH_ALIGN(ndr, 8);
608 if (NDR_BE(ndr)) {
609 return ndr_push_udlongr(ndr, NDR_SCALARS, v);
611 return ndr_push_udlong(ndr, NDR_SCALARS, v);
615 push a double
617 _PUBLIC_ enum ndr_err_code ndr_push_double(struct ndr_push *ndr, int ndr_flags, double v)
619 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
620 NDR_PUSH_ALIGN(ndr, 8);
621 NDR_PUSH_NEED_BYTES(ndr, 8);
622 memcpy(ndr->data+ndr->offset, &v, 8);
623 ndr->offset += 8;
624 return NDR_ERR_SUCCESS;
628 push a pointer
630 _PUBLIC_ enum ndr_err_code ndr_push_pointer(struct ndr_push *ndr, int ndr_flags, void* v)
632 uintptr_t h = (intptr_t)v;
633 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
634 NDR_PUSH_ALIGN(ndr, sizeof(h));
635 NDR_PUSH_NEED_BYTES(ndr, sizeof(h));
636 memcpy(ndr->data+ndr->offset, &h, sizeof(h));
637 ndr->offset += sizeof(h);
638 return NDR_ERR_SUCCESS;
641 _PUBLIC_ enum ndr_err_code ndr_push_align(struct ndr_push *ndr, size_t size)
643 /* this is a nasty hack to make pidl work with NDR64 */
644 if (size == 5) {
645 if (ndr->flags & LIBNDR_FLAG_NDR64) {
646 size = 8;
647 } else {
648 size = 4;
650 } else if (size == 3) {
651 if (ndr->flags & LIBNDR_FLAG_NDR64) {
652 size = 4;
653 } else {
654 size = 2;
657 NDR_PUSH_ALIGN(ndr, size);
658 return NDR_ERR_SUCCESS;
661 _PUBLIC_ enum ndr_err_code ndr_pull_align(struct ndr_pull *ndr, size_t size)
663 /* this is a nasty hack to make pidl work with NDR64 */
664 if (size == 5) {
665 if (ndr->flags & LIBNDR_FLAG_NDR64) {
666 size = 8;
667 } else {
668 size = 4;
670 } else if (size == 3) {
671 if (ndr->flags & LIBNDR_FLAG_NDR64) {
672 size = 4;
673 } else {
674 size = 2;
677 NDR_PULL_ALIGN(ndr, size);
678 return NDR_ERR_SUCCESS;
681 _PUBLIC_ enum ndr_err_code ndr_push_union_align(struct ndr_push *ndr, size_t size)
683 /* MS-RPCE section 2.2.5.3.4.4 */
684 if (ndr->flags & LIBNDR_FLAG_NDR64) {
685 return ndr_push_align(ndr, size);
687 return NDR_ERR_SUCCESS;
690 _PUBLIC_ enum ndr_err_code ndr_pull_union_align(struct ndr_pull *ndr, size_t size)
692 /* MS-RPCE section 2.2.5.3.4.4 */
693 if (ndr->flags & LIBNDR_FLAG_NDR64) {
694 return ndr_pull_align(ndr, size);
696 return NDR_ERR_SUCCESS;
699 _PUBLIC_ enum ndr_err_code ndr_push_trailer_align(struct ndr_push *ndr, size_t size)
701 /* MS-RPCE section 2.2.5.3.4.1 */
702 if (ndr->flags & LIBNDR_FLAG_NDR64) {
703 return ndr_push_align(ndr, size);
705 return NDR_ERR_SUCCESS;
708 _PUBLIC_ enum ndr_err_code ndr_pull_trailer_align(struct ndr_pull *ndr, size_t size)
710 /* MS-RPCE section 2.2.5.3.4.1 */
711 if (ndr->flags & LIBNDR_FLAG_NDR64) {
712 return ndr_pull_align(ndr, size);
714 return NDR_ERR_SUCCESS;
718 push some bytes
720 _PUBLIC_ enum ndr_err_code ndr_push_bytes(struct ndr_push *ndr, const uint8_t *data, uint32_t n)
722 NDR_PUSH_NEED_BYTES(ndr, n);
723 memcpy(ndr->data + ndr->offset, data, n);
724 ndr->offset += n;
725 return NDR_ERR_SUCCESS;
729 push some zero bytes
731 _PUBLIC_ enum ndr_err_code ndr_push_zero(struct ndr_push *ndr, uint32_t n)
733 NDR_PUSH_NEED_BYTES(ndr, n);
734 memset(ndr->data + ndr->offset, 0, n);
735 ndr->offset += n;
736 return NDR_ERR_SUCCESS;
740 push an array of uint8
742 _PUBLIC_ enum ndr_err_code ndr_push_array_uint8(struct ndr_push *ndr, int ndr_flags, const uint8_t *data, uint32_t n)
744 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
745 if (!(ndr_flags & NDR_SCALARS)) {
746 return NDR_ERR_SUCCESS;
748 return ndr_push_bytes(ndr, data, n);
752 push a unique non-zero value if a pointer is non-NULL, otherwise 0
754 _PUBLIC_ enum ndr_err_code ndr_push_unique_ptr(struct ndr_push *ndr, const void *p)
756 uint32_t ptr = 0;
757 if (p) {
758 ptr = ndr->ptr_count * 4;
759 ptr |= 0x00020000;
760 ndr->ptr_count++;
762 return ndr_push_uint3264(ndr, NDR_SCALARS, ptr);
766 push a 'simple' full non-zero value if a pointer is non-NULL, otherwise 0
768 _PUBLIC_ enum ndr_err_code ndr_push_full_ptr(struct ndr_push *ndr, const void *p)
770 uint32_t ptr = 0;
771 if (p) {
772 /* Check if the pointer already exists and has an id */
773 ptr = ndr_token_peek(&ndr->full_ptr_list, p);
774 if (ptr == 0) {
775 ndr->ptr_count++;
776 ptr = ndr->ptr_count;
777 ndr_token_store(ndr, &ndr->full_ptr_list, p, ptr);
780 return ndr_push_uint3264(ndr, NDR_SCALARS, ptr);
784 push always a 0, if a pointer is NULL it's a fatal error
786 _PUBLIC_ enum ndr_err_code ndr_push_ref_ptr(struct ndr_push *ndr)
788 return ndr_push_uint3264(ndr, NDR_SCALARS, 0xAEF1AEF1);
793 push a NTTIME
795 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME(struct ndr_push *ndr, int ndr_flags, NTTIME t)
797 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
798 NDR_CHECK(ndr_push_udlong(ndr, ndr_flags, t));
799 return NDR_ERR_SUCCESS;
803 pull a NTTIME
805 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
807 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
808 NDR_CHECK(ndr_pull_udlong(ndr, ndr_flags, t));
809 return NDR_ERR_SUCCESS;
813 push a NTTIME_1sec
815 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME_1sec(struct ndr_push *ndr, int ndr_flags, NTTIME t)
817 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
818 t /= 10000000;
819 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
820 return NDR_ERR_SUCCESS;
824 pull a NTTIME_1sec
826 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME_1sec(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
828 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
829 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
830 (*t) *= 10000000;
831 return NDR_ERR_SUCCESS;
835 pull a NTTIME_hyper
837 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME_hyper(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
839 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
840 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
841 return NDR_ERR_SUCCESS;
845 push a NTTIME_hyper
847 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME_hyper(struct ndr_push *ndr, int ndr_flags, NTTIME t)
849 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
850 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
851 return NDR_ERR_SUCCESS;
855 push a time_t
857 _PUBLIC_ enum ndr_err_code ndr_push_time_t(struct ndr_push *ndr, int ndr_flags, time_t t)
859 return ndr_push_uint32(ndr, ndr_flags, t);
863 pull a time_t
865 _PUBLIC_ enum ndr_err_code ndr_pull_time_t(struct ndr_pull *ndr, int ndr_flags, time_t *t)
867 uint32_t tt;
868 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &tt));
869 *t = tt;
870 return NDR_ERR_SUCCESS;
875 push a uid_t
877 _PUBLIC_ enum ndr_err_code ndr_push_uid_t(struct ndr_push *ndr, int ndr_flags, uid_t u)
879 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
880 return ndr_push_hyper(ndr, NDR_SCALARS, (uint64_t)u);
884 pull a uid_t
886 _PUBLIC_ enum ndr_err_code ndr_pull_uid_t(struct ndr_pull *ndr, int ndr_flags, uid_t *u)
888 uint64_t uu;
889 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, &uu));
890 *u = (uid_t)uu;
891 if (unlikely(uu != *u)) {
892 DEBUG(0,(__location__ ": uid_t pull doesn't fit 0x%016llx\n",
893 (unsigned long long)uu));
894 return NDR_ERR_NDR64;
896 return NDR_ERR_SUCCESS;
901 push a gid_t
903 _PUBLIC_ enum ndr_err_code ndr_push_gid_t(struct ndr_push *ndr, int ndr_flags, gid_t g)
905 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
906 return ndr_push_hyper(ndr, NDR_SCALARS, (uint64_t)g);
910 pull a gid_t
912 _PUBLIC_ enum ndr_err_code ndr_pull_gid_t(struct ndr_pull *ndr, int ndr_flags, gid_t *g)
914 uint64_t gg;
915 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, &gg));
916 *g = (gid_t)gg;
917 if (unlikely(gg != *g)) {
918 DEBUG(0,(__location__ ": gid_t pull doesn't fit 0x%016llx\n",
919 (unsigned long long)gg));
920 return NDR_ERR_NDR64;
922 return NDR_ERR_SUCCESS;
927 pull a ipv4address
929 _PUBLIC_ enum ndr_err_code ndr_pull_ipv4address(struct ndr_pull *ndr, int ndr_flags, const char **address)
931 uint32_t addr;
932 struct in_addr in;
933 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &addr));
934 in.s_addr = htonl(addr);
935 *address = talloc_strdup(ndr->current_mem_ctx, inet_ntoa(in));
936 NDR_ERR_HAVE_NO_MEMORY(*address);
937 return NDR_ERR_SUCCESS;
941 push a ipv4address
943 _PUBLIC_ enum ndr_err_code ndr_push_ipv4address(struct ndr_push *ndr, int ndr_flags, const char *address)
945 uint32_t addr;
946 if (!is_ipaddress(address)) {
947 return ndr_push_error(ndr, NDR_ERR_IPV4ADDRESS,
948 "Invalid IPv4 address: '%s'",
949 address);
951 addr = inet_addr(address);
952 NDR_CHECK(ndr_push_uint32(ndr, ndr_flags, htonl(addr)));
953 return NDR_ERR_SUCCESS;
957 print a ipv4address
959 _PUBLIC_ void ndr_print_ipv4address(struct ndr_print *ndr, const char *name,
960 const char *address)
962 ndr->print(ndr, "%-25s: %s", name, address);
966 pull a ipv6address
968 #define IPV6_BYTES 16
969 #define IPV6_ADDR_STR_LEN 39
970 _PUBLIC_ enum ndr_err_code ndr_pull_ipv6address(struct ndr_pull *ndr, int ndr_flags, const char **address)
972 uint8_t addr[IPV6_BYTES];
973 char *addr_str = talloc_strdup(ndr->current_mem_ctx, "");
974 int i;
975 NDR_CHECK(ndr_pull_array_uint8(ndr, ndr_flags, addr, IPV6_BYTES));
976 for (i = 0; i < IPV6_BYTES; ++i) {
977 addr_str = talloc_asprintf_append(addr_str, "%02x", addr[i]);
978 /* We need a ':' every second byte but the last one */
979 if (i%2 == 1 && i != (IPV6_BYTES - 1)) {
980 addr_str = talloc_strdup_append(addr_str, ":");
983 *address = addr_str;
984 NDR_ERR_HAVE_NO_MEMORY(*address);
985 return NDR_ERR_SUCCESS;
989 push a ipv6address
991 _PUBLIC_ enum ndr_err_code ndr_push_ipv6address(struct ndr_push *ndr, int ndr_flags, const char *address)
993 #ifdef AF_INET6
994 uint8_t addr[IPV6_BYTES];
995 int ret;
997 if (!is_ipaddress(address)) {
998 return ndr_push_error(ndr, NDR_ERR_IPV6ADDRESS,
999 "Invalid IPv6 address: '%s'",
1000 address);
1002 ret = inet_pton(AF_INET6, address, addr);
1003 if (ret <= 0) {
1004 return NDR_ERR_IPV6ADDRESS;
1007 NDR_CHECK(ndr_push_array_uint8(ndr, ndr_flags, addr, IPV6_BYTES));
1009 return NDR_ERR_SUCCESS;
1010 #else
1011 return NDR_ERR_IPV6ADDRESS;
1012 #endif
1016 print a ipv6address
1018 _PUBLIC_ void ndr_print_ipv6address(struct ndr_print *ndr, const char *name,
1019 const char *address)
1021 ndr->print(ndr, "%-25s: %s", name, address);
1023 #undef IPV6_BYTES
1025 _PUBLIC_ void ndr_print_struct(struct ndr_print *ndr, const char *name, const char *type)
1027 ndr->print(ndr, "%s: struct %s", name, type);
1030 _PUBLIC_ void ndr_print_null(struct ndr_print *ndr)
1032 ndr->print(ndr, "UNEXPECTED NULL POINTER");
1035 _PUBLIC_ void ndr_print_enum(struct ndr_print *ndr, const char *name, const char *type,
1036 const char *val, uint32_t value)
1038 if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
1039 ndr->print(ndr, "%-25s: %s (0x%X)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
1040 } else {
1041 ndr->print(ndr, "%-25s: %s (%d)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
1045 _PUBLIC_ void ndr_print_bitmap_flag(struct ndr_print *ndr, size_t size, const char *flag_name, uint32_t flag, uint32_t value)
1047 if (flag == 0) {
1048 return;
1051 /* this is an attempt to support multi-bit bitmap masks */
1052 value &= flag;
1054 while (!(flag & 1)) {
1055 flag >>= 1;
1056 value >>= 1;
1058 if (flag == 1) {
1059 ndr->print(ndr, " %d: %-25s", value, flag_name);
1060 } else {
1061 ndr->print(ndr, "0x%02x: %-25s (%d)", value, flag_name, value);
1065 _PUBLIC_ void ndr_print_int8(struct ndr_print *ndr, const char *name, int8_t v)
1067 if (NDR_HIDE_SECRET(ndr)) {
1068 ndr->print(ndr, "%-25s: <REDACTED SECRET VALUE>", name);
1069 return;
1071 ndr->print(ndr, "%-25s: %d", name, v);
1074 _PUBLIC_ void ndr_print_uint8(struct ndr_print *ndr, const char *name, uint8_t v)
1076 if (NDR_HIDE_SECRET(ndr)) {
1077 ndr->print(ndr, "%-25s: <REDACTED SECRET VALUE>", name);
1078 return;
1080 ndr->print(ndr, "%-25s: 0x%02x (%u)", name, v, v);
1083 _PUBLIC_ void ndr_print_int16(struct ndr_print *ndr, const char *name, int16_t v)
1085 if (NDR_HIDE_SECRET(ndr)) {
1086 ndr->print(ndr, "%-25s: <REDACTED SECRET VALUE>", name);
1087 return;
1089 ndr->print(ndr, "%-25s: %d", name, v);
1092 _PUBLIC_ void ndr_print_uint16(struct ndr_print *ndr, const char *name, uint16_t v)
1094 if (NDR_HIDE_SECRET(ndr)) {
1095 ndr->print(ndr, "%-25s: <REDACTED SECRET VALUE>", name);
1096 return;
1098 ndr->print(ndr, "%-25s: 0x%04x (%u)", name, v, v);
1101 _PUBLIC_ void ndr_print_int32(struct ndr_print *ndr, const char *name, int32_t v)
1103 if (NDR_HIDE_SECRET(ndr)) {
1104 ndr->print(ndr, "%-25s: <REDACTED SECRET VALUE>", name);
1105 return;
1107 ndr->print(ndr, "%-25s: %d", name, v);
1110 _PUBLIC_ void ndr_print_uint32(struct ndr_print *ndr, const char *name, uint32_t v)
1112 if (NDR_HIDE_SECRET(ndr)) {
1113 ndr->print(ndr, "%-25s: <REDACTED SECRET VALUE>", name);
1114 return;
1116 ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
1119 _PUBLIC_ void ndr_print_int3264(struct ndr_print *ndr, const char *name, int32_t v)
1121 if (NDR_HIDE_SECRET(ndr)) {
1122 ndr->print(ndr, "%-25s: <REDACTED SECRET VALUE>", name);
1123 return;
1125 ndr->print(ndr, "%-25s: %d", name, v);
1128 _PUBLIC_ void ndr_print_uint3264(struct ndr_print *ndr, const char *name, uint32_t v)
1130 if (NDR_HIDE_SECRET(ndr)) {
1131 ndr->print(ndr, "%-25s: <REDACTED SECRET VALUE>", name);
1132 return;
1134 ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
1137 _PUBLIC_ void ndr_print_udlong(struct ndr_print *ndr, const char *name, uint64_t v)
1139 ndr->print(ndr, "%-25s: 0x%016llx (%llu)", name, (unsigned long long)v, (unsigned long long)v);
1142 _PUBLIC_ void ndr_print_udlongr(struct ndr_print *ndr, const char *name, uint64_t v)
1144 ndr_print_udlong(ndr, name, v);
1147 _PUBLIC_ void ndr_print_dlong(struct ndr_print *ndr, const char *name, int64_t v)
1149 if (NDR_HIDE_SECRET(ndr)) {
1150 ndr->print(ndr, "%-25s: <REDACTED SECRET VALUE>", name);
1151 return;
1153 ndr->print(ndr, "%-25s: 0x%016llx (%lld)", name, (unsigned long long)v, (long long)v);
1156 _PUBLIC_ void ndr_print_double(struct ndr_print *ndr, const char *name, double v)
1158 ndr->print(ndr, "%-25s: %f", name, v);
1161 _PUBLIC_ void ndr_print_hyper(struct ndr_print *ndr, const char *name, uint64_t v)
1163 ndr_print_dlong(ndr, name, v);
1166 _PUBLIC_ void ndr_print_pointer(struct ndr_print *ndr, const char *name, void *v)
1168 ndr->print(ndr, "%-25s: %p", name, v);
1171 _PUBLIC_ void ndr_print_ptr(struct ndr_print *ndr, const char *name, const void *p)
1173 if (p) {
1174 ndr->print(ndr, "%-25s: *", name);
1175 } else {
1176 ndr->print(ndr, "%-25s: NULL", name);
1180 _PUBLIC_ void ndr_print_NTTIME(struct ndr_print *ndr, const char *name, NTTIME t)
1182 ndr->print(ndr, "%-25s: %s", name, nt_time_string(ndr, t));
1185 _PUBLIC_ void ndr_print_NTTIME_1sec(struct ndr_print *ndr, const char *name, NTTIME t)
1187 /* this is a standard NTTIME here
1188 * as it's already converted in the pull/push code
1190 ndr_print_NTTIME(ndr, name, t);
1193 _PUBLIC_ void ndr_print_NTTIME_hyper(struct ndr_print *ndr, const char *name, NTTIME t)
1195 ndr_print_NTTIME(ndr, name, t);
1198 _PUBLIC_ void ndr_print_time_t(struct ndr_print *ndr, const char *name, time_t t)
1200 if (t == (time_t)-1 || t == 0) {
1201 ndr->print(ndr, "%-25s: (time_t)%d", name, (int)t);
1202 } else {
1203 ndr->print(ndr, "%-25s: %s", name, timestring(ndr, t));
1207 _PUBLIC_ void ndr_print_uid_t(struct ndr_print *ndr, const char *name, uid_t u)
1209 ndr_print_dlong(ndr, name, u);
1212 _PUBLIC_ void ndr_print_gid_t(struct ndr_print *ndr, const char *name, gid_t g)
1214 ndr_print_dlong(ndr, name, g);
1217 _PUBLIC_ void ndr_print_union(struct ndr_print *ndr, const char *name, int level, const char *type)
1219 if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
1220 ndr->print(ndr, "%-25s: union %s(case 0x%X)", name, type, level);
1221 } else {
1222 ndr->print(ndr, "%-25s: union %s(case %d)", name, type, level);
1226 _PUBLIC_ void ndr_print_bad_level(struct ndr_print *ndr, const char *name, uint16_t level)
1228 ndr->print(ndr, "UNKNOWN LEVEL %u", level);
1231 _PUBLIC_ void ndr_print_array_uint8(struct ndr_print *ndr, const char *name,
1232 const uint8_t *data, uint32_t count)
1234 int i;
1235 #define _ONELINE_LIMIT 32
1237 if (data == NULL) {
1238 ndr->print(ndr, "%s: ARRAY(%d) : NULL", name, count);
1239 return;
1242 if (NDR_HIDE_SECRET(ndr)) {
1243 ndr->print(ndr, "%s: ARRAY(%d): <REDACTED SECRET VALUES>", name, count);
1244 return;
1247 if (count <= _ONELINE_LIMIT && (ndr->flags & LIBNDR_PRINT_ARRAY_HEX)) {
1248 char s[(_ONELINE_LIMIT + 1) * 2];
1249 for (i=0;i<count;i++) {
1250 snprintf(&s[i*2], 3, "%02x", data[i]);
1252 s[i*2] = 0;
1253 ndr->print(ndr, "%-25s: %s", name, s);
1254 return;
1257 ndr->print(ndr, "%s: ARRAY(%d)", name, count);
1258 if (count > _ONELINE_LIMIT && (ndr->flags & LIBNDR_PRINT_ARRAY_HEX)) {
1259 ndr_dump_data(ndr, data, count);
1260 return;
1263 ndr->depth++;
1264 for (i=0;i<count;i++) {
1265 char *idx=NULL;
1266 if (asprintf(&idx, "[%d]", i) != -1) {
1267 ndr_print_uint8(ndr, idx, data[i]);
1268 free(idx);
1271 ndr->depth--;
1272 #undef _ONELINE_LIMIT
1275 static void ndr_print_dump_data_cb(const char *buf, void *private_data)
1277 struct ndr_print *ndr = (struct ndr_print *)private_data;
1279 ndr->print(ndr, "%s", buf);
1283 ndr_print version of dump_data()
1285 static void ndr_dump_data(struct ndr_print *ndr, const uint8_t *buf, int len)
1287 if (NDR_HIDE_SECRET(ndr)) {
1288 return;
1290 ndr->no_newline = true;
1291 dump_data_cb(buf, len, true, ndr_print_dump_data_cb, ndr);
1292 ndr->no_newline = false;
1296 _PUBLIC_ void ndr_print_DATA_BLOB(struct ndr_print *ndr, const char *name, DATA_BLOB r)
1298 ndr->print(ndr, "%-25s: DATA_BLOB length=%u", name, (unsigned)r.length);
1299 if (r.length) {
1300 ndr_dump_data(ndr, r.data, r.length);
1306 * Push a DATA_BLOB onto the wire.
1307 * 1) When called with LIBNDR_FLAG_ALIGN* alignment flags set, push padding
1308 * bytes _only_. The length is determined by the alignment required and the
1309 * current ndr offset.
1310 * 2) When called with the LIBNDR_FLAG_REMAINING flag, push the byte array to
1311 * the ndr buffer.
1312 * 3) Otherwise, push a uint3264 length _and_ a corresponding byte array to the
1313 * ndr buffer.
1315 _PUBLIC_ enum ndr_err_code ndr_push_DATA_BLOB(struct ndr_push *ndr, int ndr_flags, DATA_BLOB blob)
1317 if (ndr->flags & LIBNDR_FLAG_REMAINING) {
1318 /* nothing to do */
1319 } else if (ndr->flags & (LIBNDR_ALIGN_FLAGS & ~LIBNDR_FLAG_NOALIGN)) {
1320 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1321 blob.length = NDR_ALIGN(ndr, 2);
1322 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1323 blob.length = NDR_ALIGN(ndr, 4);
1324 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1325 blob.length = NDR_ALIGN(ndr, 8);
1327 NDR_PUSH_ALLOC_SIZE(ndr, blob.data, blob.length);
1328 data_blob_clear(&blob);
1329 } else {
1330 NDR_CHECK(ndr_push_uint3264(ndr, NDR_SCALARS, blob.length));
1332 NDR_CHECK(ndr_push_bytes(ndr, blob.data, blob.length));
1333 return NDR_ERR_SUCCESS;
1337 * Pull a DATA_BLOB from the wire.
1338 * 1) when called with LIBNDR_FLAG_ALIGN* alignment flags set, pull padding
1339 * bytes _only_. The length is determined by the alignment required and the
1340 * current ndr offset.
1341 * 2) When called with the LIBNDR_FLAG_REMAINING flag, pull all remaining bytes
1342 * from the ndr buffer.
1343 * 3) Otherwise, pull a uint3264 length _and_ a corresponding byte array from the
1344 * ndr buffer.
1346 _PUBLIC_ enum ndr_err_code ndr_pull_DATA_BLOB(struct ndr_pull *ndr, int ndr_flags, DATA_BLOB *blob)
1348 uint32_t length = 0;
1350 if (ndr->flags & LIBNDR_FLAG_REMAINING) {
1351 length = ndr->data_size - ndr->offset;
1352 } else if (ndr->flags & (LIBNDR_ALIGN_FLAGS & ~LIBNDR_FLAG_NOALIGN)) {
1353 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1354 length = NDR_ALIGN(ndr, 2);
1355 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1356 length = NDR_ALIGN(ndr, 4);
1357 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1358 length = NDR_ALIGN(ndr, 8);
1360 if (ndr->data_size - ndr->offset < length) {
1361 length = ndr->data_size - ndr->offset;
1363 } else {
1364 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &length));
1366 NDR_PULL_NEED_BYTES(ndr, length);
1367 *blob = data_blob_talloc(ndr->current_mem_ctx, ndr->data+ndr->offset, length);
1368 ndr->offset += length;
1369 return NDR_ERR_SUCCESS;
1372 _PUBLIC_ uint32_t ndr_size_DATA_BLOB(int ret, const DATA_BLOB *data, int flags)
1374 if (!data) return ret;
1375 return ret + data->length;
1378 _PUBLIC_ void ndr_print_bool(struct ndr_print *ndr, const char *name, const bool b)
1380 ndr->print(ndr, "%-25s: %s", name, b?"true":"false");
1383 _PUBLIC_ NTSTATUS ndr_map_error2ntstatus(enum ndr_err_code ndr_err)
1385 switch (ndr_err) {
1386 case NDR_ERR_SUCCESS:
1387 return NT_STATUS_OK;
1388 case NDR_ERR_BUFSIZE:
1389 return NT_STATUS_BUFFER_TOO_SMALL;
1390 case NDR_ERR_TOKEN:
1391 return NT_STATUS_INTERNAL_ERROR;
1392 case NDR_ERR_ALLOC:
1393 return NT_STATUS_NO_MEMORY;
1394 case NDR_ERR_ARRAY_SIZE:
1395 return NT_STATUS_ARRAY_BOUNDS_EXCEEDED;
1396 case NDR_ERR_INVALID_POINTER:
1397 return NT_STATUS_INVALID_PARAMETER_MIX;
1398 case NDR_ERR_UNREAD_BYTES:
1399 return NT_STATUS_PORT_MESSAGE_TOO_LONG;
1400 default:
1401 break;
1404 /* we should map all error codes to different status codes */
1405 return NT_STATUS_INVALID_PARAMETER;
1408 _PUBLIC_ int ndr_map_error2errno(enum ndr_err_code ndr_err)
1410 switch (ndr_err) {
1411 case NDR_ERR_SUCCESS:
1412 return 0;
1413 case NDR_ERR_BUFSIZE:
1414 return ENOSPC;
1415 case NDR_ERR_TOKEN:
1416 return EINVAL;
1417 case NDR_ERR_ALLOC:
1418 return ENOMEM;
1419 case NDR_ERR_ARRAY_SIZE:
1420 return EMSGSIZE;
1421 case NDR_ERR_INVALID_POINTER:
1422 return EINVAL;
1423 case NDR_ERR_UNREAD_BYTES:
1424 return EOVERFLOW;
1425 default:
1426 break;
1429 /* we should map all error codes to different status codes */
1430 return EINVAL;
1433 _PUBLIC_ enum ndr_err_code ndr_push_timespec(struct ndr_push *ndr,
1434 int ndr_flags,
1435 const struct timespec *t)
1437 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
1438 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t->tv_sec));
1439 NDR_CHECK(ndr_push_uint32(ndr, ndr_flags, t->tv_nsec));
1440 return NDR_ERR_SUCCESS;
1443 _PUBLIC_ enum ndr_err_code ndr_pull_timespec(struct ndr_pull *ndr,
1444 int ndr_flags,
1445 struct timespec *t)
1447 uint64_t secs;
1448 uint32_t nsecs;
1449 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
1450 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, &secs));
1451 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &nsecs));
1452 t->tv_sec = secs;
1453 t->tv_nsec = nsecs;
1454 return NDR_ERR_SUCCESS;
1457 _PUBLIC_ void ndr_print_timespec(struct ndr_print *ndr, const char *name,
1458 const struct timespec *t)
1460 ndr->print(ndr, "%-25s: %s.%ld", name, timestring(ndr, t->tv_sec),
1461 (long)t->tv_nsec);
1464 _PUBLIC_ enum ndr_err_code ndr_push_timeval(struct ndr_push *ndr,
1465 int ndr_flags,
1466 const struct timeval *t)
1468 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
1469 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t->tv_sec));
1470 NDR_CHECK(ndr_push_uint32(ndr, ndr_flags, t->tv_usec));
1471 return NDR_ERR_SUCCESS;
1474 _PUBLIC_ enum ndr_err_code ndr_pull_timeval(struct ndr_pull *ndr,
1475 int ndr_flags,
1476 struct timeval *t)
1478 uint64_t secs;
1479 uint32_t usecs;
1480 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
1481 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, &secs));
1482 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &usecs));
1483 t->tv_sec = secs;
1484 t->tv_usec = usecs;
1485 return NDR_ERR_SUCCESS;
1488 _PUBLIC_ void ndr_print_timeval(struct ndr_print *ndr, const char *name,
1489 const struct timeval *t)
1491 ndr->print(ndr, "%-25s: %s.%ld", name, timestring(ndr, t->tv_sec),
1492 (long)t->tv_usec);