s3-passdb: Silence scary DEBUG(0) message on first use of secrets.tdb databases
[Samba/gebeck_regimport.git] / librpc / ndr / ndr_basic.c
blob1887838779d409941ec336c6ffb9a66682c3ad64
1 /*
2 Unix SMB/CIFS implementation.
4 routines for marshalling/unmarshalling basic types
6 Copyright (C) Andrew Tridgell 2003
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "system/network.h"
24 #include "librpc/ndr/libndr.h"
25 #include "lib/util/util_net.h"
27 #define NDR_SVAL(ndr, ofs) (NDR_BE(ndr)?RSVAL(ndr->data,ofs):SVAL(ndr->data,ofs))
28 #define NDR_IVAL(ndr, ofs) (NDR_BE(ndr)?RIVAL(ndr->data,ofs):IVAL(ndr->data,ofs))
29 #define NDR_IVALS(ndr, ofs) (NDR_BE(ndr)?RIVALS(ndr->data,ofs):IVALS(ndr->data,ofs))
30 #define NDR_SSVAL(ndr, ofs, v) do { if (NDR_BE(ndr)) { RSSVAL(ndr->data,ofs,v); } else SSVAL(ndr->data,ofs,v); } while (0)
31 #define NDR_SIVAL(ndr, ofs, v) do { if (NDR_BE(ndr)) { RSIVAL(ndr->data,ofs,v); } else SIVAL(ndr->data,ofs,v); } while (0)
32 #define NDR_SIVALS(ndr, ofs, v) do { if (NDR_BE(ndr)) { RSIVALS(ndr->data,ofs,v); } else SIVALS(ndr->data,ofs,v); } while (0)
36 check for data leaks from the server by looking for non-zero pad bytes
37 these could also indicate that real structure elements have been
38 mistaken for padding in the IDL
40 _PUBLIC_ void ndr_check_padding(struct ndr_pull *ndr, size_t n)
42 size_t ofs2 = (ndr->offset + (n-1)) & ~(n-1);
43 int i;
44 for (i=ndr->offset;i<ofs2;i++) {
45 if (ndr->data[i] != 0) {
46 break;
49 if (i<ofs2) {
50 DEBUG(0,("WARNING: Non-zero padding to %d: ", (int)n));
51 for (i=ndr->offset;i<ofs2;i++) {
52 DEBUG(0,("%02x ", ndr->data[i]));
54 DEBUG(0,("\n"));
60 parse a int8_t
62 _PUBLIC_ enum ndr_err_code ndr_pull_int8(struct ndr_pull *ndr, int ndr_flags, int8_t *v)
64 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
65 NDR_PULL_NEED_BYTES(ndr, 1);
66 *v = (int8_t)CVAL(ndr->data, ndr->offset);
67 ndr->offset += 1;
68 return NDR_ERR_SUCCESS;
72 parse a uint8_t
74 _PUBLIC_ enum ndr_err_code ndr_pull_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *v)
76 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
77 NDR_PULL_NEED_BYTES(ndr, 1);
78 *v = CVAL(ndr->data, ndr->offset);
79 ndr->offset += 1;
80 return NDR_ERR_SUCCESS;
84 parse a int16_t
86 _PUBLIC_ enum ndr_err_code ndr_pull_int16(struct ndr_pull *ndr, int ndr_flags, int16_t *v)
88 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
89 NDR_PULL_ALIGN(ndr, 2);
90 NDR_PULL_NEED_BYTES(ndr, 2);
91 *v = (uint16_t)NDR_SVAL(ndr, ndr->offset);
92 ndr->offset += 2;
93 return NDR_ERR_SUCCESS;
97 parse a uint16_t
99 _PUBLIC_ enum ndr_err_code ndr_pull_uint16(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
101 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
102 NDR_PULL_ALIGN(ndr, 2);
103 NDR_PULL_NEED_BYTES(ndr, 2);
104 *v = NDR_SVAL(ndr, ndr->offset);
105 ndr->offset += 2;
106 return NDR_ERR_SUCCESS;
110 parse a uint1632_t
112 _PUBLIC_ enum ndr_err_code ndr_pull_uint1632(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
114 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
115 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
116 uint32_t v32 = 0;
117 enum ndr_err_code err = ndr_pull_uint32(ndr, ndr_flags, &v32);
118 *v = v32;
119 if (unlikely(v32 != *v)) {
120 DEBUG(0,(__location__ ": non-zero upper 16 bits 0x%08x\n", (unsigned)v32));
121 return NDR_ERR_NDR64;
123 return err;
125 return ndr_pull_uint16(ndr, ndr_flags, v);
129 parse a int32_t
131 _PUBLIC_ enum ndr_err_code ndr_pull_int32(struct ndr_pull *ndr, int ndr_flags, int32_t *v)
133 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
134 NDR_PULL_ALIGN(ndr, 4);
135 NDR_PULL_NEED_BYTES(ndr, 4);
136 *v = NDR_IVALS(ndr, ndr->offset);
137 ndr->offset += 4;
138 return NDR_ERR_SUCCESS;
142 parse a uint32_t
144 _PUBLIC_ enum ndr_err_code ndr_pull_uint32(struct ndr_pull *ndr, int ndr_flags, uint32_t *v)
146 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
147 NDR_PULL_ALIGN(ndr, 4);
148 NDR_PULL_NEED_BYTES(ndr, 4);
149 *v = NDR_IVAL(ndr, ndr->offset);
150 ndr->offset += 4;
151 return NDR_ERR_SUCCESS;
155 parse a arch dependent uint32/uint64
157 _PUBLIC_ enum ndr_err_code ndr_pull_uint3264(struct ndr_pull *ndr, int ndr_flags, uint32_t *v)
159 uint64_t v64;
160 enum ndr_err_code err;
161 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
162 if (likely(!(ndr->flags & LIBNDR_FLAG_NDR64))) {
163 return ndr_pull_uint32(ndr, ndr_flags, v);
165 err = ndr_pull_hyper(ndr, ndr_flags, &v64);
166 *v = (uint32_t)v64;
167 if (unlikely(v64 != *v)) {
168 DEBUG(0,(__location__ ": non-zero upper 32 bits 0x%016llx\n",
169 (unsigned long long)v64));
170 return NDR_ERR_NDR64;
172 return err;
176 parse a double
178 _PUBLIC_ enum ndr_err_code ndr_pull_double(struct ndr_pull *ndr, int ndr_flags, double *v)
180 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
181 NDR_PULL_ALIGN(ndr, 8);
182 NDR_PULL_NEED_BYTES(ndr, 8);
183 memcpy(v, ndr->data+ndr->offset, 8);
184 ndr->offset += 8;
185 return NDR_ERR_SUCCESS;
189 parse a pointer referent identifier stored in 2 bytes
191 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr_short(struct ndr_pull *ndr, uint16_t *v)
193 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, v));
194 if (*v != 0) {
195 ndr->ptr_count++;
197 *(v) -= ndr->relative_rap_convert;
198 return NDR_ERR_SUCCESS;
202 parse a pointer referent identifier
204 _PUBLIC_ enum ndr_err_code ndr_pull_generic_ptr(struct ndr_pull *ndr, uint32_t *v)
206 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, v));
207 if (*v != 0) {
208 ndr->ptr_count++;
210 return NDR_ERR_SUCCESS;
214 parse a ref pointer referent identifier
216 _PUBLIC_ enum ndr_err_code ndr_pull_ref_ptr(struct ndr_pull *ndr, uint32_t *v)
218 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, v));
219 /* ref pointers always point to data */
220 *v = 1;
221 return NDR_ERR_SUCCESS;
225 parse a udlong
227 _PUBLIC_ enum ndr_err_code ndr_pull_udlong(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
229 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
230 NDR_PULL_ALIGN(ndr, 4);
231 NDR_PULL_NEED_BYTES(ndr, 8);
232 *v = NDR_IVAL(ndr, ndr->offset);
233 *v |= (uint64_t)(NDR_IVAL(ndr, ndr->offset+4)) << 32;
234 ndr->offset += 8;
235 return NDR_ERR_SUCCESS;
239 parse a udlongr
241 _PUBLIC_ enum ndr_err_code ndr_pull_udlongr(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
243 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
244 NDR_PULL_ALIGN(ndr, 4);
245 NDR_PULL_NEED_BYTES(ndr, 8);
246 *v = ((uint64_t)NDR_IVAL(ndr, ndr->offset)) << 32;
247 *v |= NDR_IVAL(ndr, ndr->offset+4);
248 ndr->offset += 8;
249 return NDR_ERR_SUCCESS;
253 parse a dlong
255 _PUBLIC_ enum ndr_err_code ndr_pull_dlong(struct ndr_pull *ndr, int ndr_flags, int64_t *v)
257 return ndr_pull_udlong(ndr, ndr_flags, (uint64_t *)v);
261 parse a hyper
263 _PUBLIC_ enum ndr_err_code ndr_pull_hyper(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
265 NDR_PULL_ALIGN(ndr, 8);
266 if (NDR_BE(ndr)) {
267 return ndr_pull_udlongr(ndr, ndr_flags, v);
269 return ndr_pull_udlong(ndr, ndr_flags, v);
273 parse a pointer
275 _PUBLIC_ enum ndr_err_code ndr_pull_pointer(struct ndr_pull *ndr, int ndr_flags, void* *v)
277 uintptr_t h;
278 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
279 NDR_PULL_ALIGN(ndr, sizeof(h));
280 NDR_PULL_NEED_BYTES(ndr, sizeof(h));
281 memcpy(&h, ndr->data+ndr->offset, sizeof(h));
282 ndr->offset += sizeof(h);
283 *v = (void *)h;
284 return NDR_ERR_SUCCESS;
288 pull a NTSTATUS
290 _PUBLIC_ enum ndr_err_code ndr_pull_NTSTATUS(struct ndr_pull *ndr, int ndr_flags, NTSTATUS *status)
292 uint32_t v;
293 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
294 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
295 *status = NT_STATUS(v);
296 return NDR_ERR_SUCCESS;
300 push a NTSTATUS
302 _PUBLIC_ enum ndr_err_code ndr_push_NTSTATUS(struct ndr_push *ndr, int ndr_flags, NTSTATUS status)
304 return ndr_push_uint32(ndr, ndr_flags, NT_STATUS_V(status));
307 _PUBLIC_ void ndr_print_NTSTATUS(struct ndr_print *ndr, const char *name, NTSTATUS r)
309 ndr->print(ndr, "%-25s: %s", name, nt_errstr(r));
313 pull a WERROR
315 _PUBLIC_ enum ndr_err_code ndr_pull_WERROR(struct ndr_pull *ndr, int ndr_flags, WERROR *status)
317 uint32_t v;
318 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
319 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
320 *status = W_ERROR(v);
321 return NDR_ERR_SUCCESS;
326 parse a uint8_t enum
328 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *v)
330 return ndr_pull_uint8(ndr, ndr_flags, v);
334 parse a uint16_t enum
336 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint16(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
338 return ndr_pull_uint16(ndr, ndr_flags, v);
342 parse a uint1632_t enum (uint32_t on NDR64)
344 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint1632(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
346 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
347 uint32_t v32;
348 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &v32));
349 *v = v32;
350 if (v32 != *v) {
351 DEBUG(0,(__location__ ": non-zero upper 16 bits 0x%08x\n", (unsigned)v32));
352 return NDR_ERR_NDR64;
354 return NDR_ERR_SUCCESS;
356 return ndr_pull_uint16(ndr, ndr_flags, v);
360 parse a uint32_t enum
362 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint32(struct ndr_pull *ndr, int ndr_flags, uint32_t *v)
364 return ndr_pull_uint32(ndr, ndr_flags, v);
368 push a uint8_t enum
370 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint8(struct ndr_push *ndr, int ndr_flags, uint8_t v)
372 return ndr_push_uint8(ndr, ndr_flags, v);
376 push a uint16_t enum
378 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint16(struct ndr_push *ndr, int ndr_flags, uint16_t v)
380 return ndr_push_uint16(ndr, ndr_flags, v);
384 push a uint32_t enum
386 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint32(struct ndr_push *ndr, int ndr_flags, uint32_t v)
388 return ndr_push_uint32(ndr, ndr_flags, v);
392 push a uint1632_t enum
394 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint1632(struct ndr_push *ndr, int ndr_flags, uint16_t v)
396 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
397 return ndr_push_uint32(ndr, ndr_flags, v);
399 return ndr_push_uint16(ndr, ndr_flags, v);
403 push a WERROR
405 _PUBLIC_ enum ndr_err_code ndr_push_WERROR(struct ndr_push *ndr, int ndr_flags, WERROR status)
407 return ndr_push_uint32(ndr, NDR_SCALARS, W_ERROR_V(status));
410 _PUBLIC_ void ndr_print_WERROR(struct ndr_print *ndr, const char *name, WERROR r)
412 ndr->print(ndr, "%-25s: %s", name, win_errstr(r));
416 parse a set of bytes
418 _PUBLIC_ enum ndr_err_code ndr_pull_bytes(struct ndr_pull *ndr, uint8_t *data, uint32_t n)
420 NDR_PULL_NEED_BYTES(ndr, n);
421 memcpy(data, ndr->data + ndr->offset, n);
422 ndr->offset += n;
423 return NDR_ERR_SUCCESS;
427 pull an array of uint8
429 _PUBLIC_ enum ndr_err_code ndr_pull_array_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *data, uint32_t n)
431 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
432 if (!(ndr_flags & NDR_SCALARS)) {
433 return NDR_ERR_SUCCESS;
435 return ndr_pull_bytes(ndr, data, n);
439 push a int8_t
441 _PUBLIC_ enum ndr_err_code ndr_push_int8(struct ndr_push *ndr, int ndr_flags, int8_t v)
443 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
444 NDR_PUSH_NEED_BYTES(ndr, 1);
445 SCVAL(ndr->data, ndr->offset, (uint8_t)v);
446 ndr->offset += 1;
447 return NDR_ERR_SUCCESS;
451 push a uint8_t
453 _PUBLIC_ enum ndr_err_code ndr_push_uint8(struct ndr_push *ndr, int ndr_flags, uint8_t v)
455 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
456 NDR_PUSH_NEED_BYTES(ndr, 1);
457 SCVAL(ndr->data, ndr->offset, v);
458 ndr->offset += 1;
459 return NDR_ERR_SUCCESS;
463 push a int16_t
465 _PUBLIC_ enum ndr_err_code ndr_push_int16(struct ndr_push *ndr, int ndr_flags, int16_t v)
467 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
468 NDR_PUSH_ALIGN(ndr, 2);
469 NDR_PUSH_NEED_BYTES(ndr, 2);
470 NDR_SSVAL(ndr, ndr->offset, (uint16_t)v);
471 ndr->offset += 2;
472 return NDR_ERR_SUCCESS;
476 push a uint16_t
478 _PUBLIC_ enum ndr_err_code ndr_push_uint16(struct ndr_push *ndr, int ndr_flags, uint16_t v)
480 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
481 NDR_PUSH_ALIGN(ndr, 2);
482 NDR_PUSH_NEED_BYTES(ndr, 2);
483 NDR_SSVAL(ndr, ndr->offset, v);
484 ndr->offset += 2;
485 return NDR_ERR_SUCCESS;
489 push a uint1632
491 _PUBLIC_ enum ndr_err_code ndr_push_uint1632(struct ndr_push *ndr, int ndr_flags, uint16_t v)
493 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
494 return ndr_push_uint32(ndr, ndr_flags, v);
496 return ndr_push_uint16(ndr, ndr_flags, v);
500 push a int32_t
502 _PUBLIC_ enum ndr_err_code ndr_push_int32(struct ndr_push *ndr, int ndr_flags, int32_t v)
504 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
505 NDR_PUSH_ALIGN(ndr, 4);
506 NDR_PUSH_NEED_BYTES(ndr, 4);
507 NDR_SIVALS(ndr, ndr->offset, v);
508 ndr->offset += 4;
509 return NDR_ERR_SUCCESS;
513 push a uint32_t
515 _PUBLIC_ enum ndr_err_code ndr_push_uint32(struct ndr_push *ndr, int ndr_flags, uint32_t v)
517 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
518 NDR_PUSH_ALIGN(ndr, 4);
519 NDR_PUSH_NEED_BYTES(ndr, 4);
520 NDR_SIVAL(ndr, ndr->offset, v);
521 ndr->offset += 4;
522 return NDR_ERR_SUCCESS;
526 push a uint3264
528 _PUBLIC_ enum ndr_err_code ndr_push_uint3264(struct ndr_push *ndr, int ndr_flags, uint32_t v)
530 if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
531 return ndr_push_hyper(ndr, ndr_flags, v);
533 return ndr_push_uint32(ndr, ndr_flags, v);
537 push a udlong
539 _PUBLIC_ enum ndr_err_code ndr_push_udlong(struct ndr_push *ndr, int ndr_flags, uint64_t v)
541 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
542 NDR_PUSH_ALIGN(ndr, 4);
543 NDR_PUSH_NEED_BYTES(ndr, 8);
544 NDR_SIVAL(ndr, ndr->offset, (v & 0xFFFFFFFF));
545 NDR_SIVAL(ndr, ndr->offset+4, (v>>32));
546 ndr->offset += 8;
547 return NDR_ERR_SUCCESS;
551 push a udlongr
553 _PUBLIC_ enum ndr_err_code ndr_push_udlongr(struct ndr_push *ndr, int ndr_flags, uint64_t v)
555 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
556 NDR_PUSH_ALIGN(ndr, 4);
557 NDR_PUSH_NEED_BYTES(ndr, 8);
558 NDR_SIVAL(ndr, ndr->offset, (v>>32));
559 NDR_SIVAL(ndr, ndr->offset+4, (v & 0xFFFFFFFF));
560 ndr->offset += 8;
561 return NDR_ERR_SUCCESS;
565 push a dlong
567 _PUBLIC_ enum ndr_err_code ndr_push_dlong(struct ndr_push *ndr, int ndr_flags, int64_t v)
569 return ndr_push_udlong(ndr, NDR_SCALARS, (uint64_t)v);
573 push a hyper
575 _PUBLIC_ enum ndr_err_code ndr_push_hyper(struct ndr_push *ndr, int ndr_flags, uint64_t v)
577 NDR_PUSH_ALIGN(ndr, 8);
578 if (NDR_BE(ndr)) {
579 return ndr_push_udlongr(ndr, NDR_SCALARS, v);
581 return ndr_push_udlong(ndr, NDR_SCALARS, v);
585 push a double
587 _PUBLIC_ enum ndr_err_code ndr_push_double(struct ndr_push *ndr, int ndr_flags, double v)
589 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
590 NDR_PUSH_ALIGN(ndr, 8);
591 NDR_PUSH_NEED_BYTES(ndr, 8);
592 memcpy(ndr->data+ndr->offset, &v, 8);
593 ndr->offset += 8;
594 return NDR_ERR_SUCCESS;
598 push a pointer
600 _PUBLIC_ enum ndr_err_code ndr_push_pointer(struct ndr_push *ndr, int ndr_flags, void* v)
602 uintptr_t h = (intptr_t)v;
603 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
604 NDR_PUSH_ALIGN(ndr, sizeof(h));
605 NDR_PUSH_NEED_BYTES(ndr, sizeof(h));
606 memcpy(ndr->data+ndr->offset, &h, sizeof(h));
607 ndr->offset += sizeof(h);
608 return NDR_ERR_SUCCESS;
611 _PUBLIC_ enum ndr_err_code ndr_push_align(struct ndr_push *ndr, size_t size)
613 /* this is a nasty hack to make pidl work with NDR64 */
614 if (size == 5) {
615 if (ndr->flags & LIBNDR_FLAG_NDR64) {
616 size = 8;
617 } else {
618 size = 4;
620 } else if (size == 3) {
621 if (ndr->flags & LIBNDR_FLAG_NDR64) {
622 size = 4;
623 } else {
624 size = 2;
627 NDR_PUSH_ALIGN(ndr, size);
628 return NDR_ERR_SUCCESS;
631 _PUBLIC_ enum ndr_err_code ndr_pull_align(struct ndr_pull *ndr, size_t size)
633 /* this is a nasty hack to make pidl work with NDR64 */
634 if (size == 5) {
635 if (ndr->flags & LIBNDR_FLAG_NDR64) {
636 size = 8;
637 } else {
638 size = 4;
640 } else if (size == 3) {
641 if (ndr->flags & LIBNDR_FLAG_NDR64) {
642 size = 4;
643 } else {
644 size = 2;
647 NDR_PULL_ALIGN(ndr, size);
648 return NDR_ERR_SUCCESS;
651 _PUBLIC_ enum ndr_err_code ndr_push_union_align(struct ndr_push *ndr, size_t size)
653 /* MS-RPCE section 2.2.5.3.4.4 */
654 if (ndr->flags & LIBNDR_FLAG_NDR64) {
655 return ndr_push_align(ndr, size);
657 return NDR_ERR_SUCCESS;
660 _PUBLIC_ enum ndr_err_code ndr_pull_union_align(struct ndr_pull *ndr, size_t size)
662 /* MS-RPCE section 2.2.5.3.4.4 */
663 if (ndr->flags & LIBNDR_FLAG_NDR64) {
664 return ndr_pull_align(ndr, size);
666 return NDR_ERR_SUCCESS;
669 _PUBLIC_ enum ndr_err_code ndr_push_trailer_align(struct ndr_push *ndr, size_t size)
671 /* MS-RPCE section 2.2.5.3.4.1 */
672 if (ndr->flags & LIBNDR_FLAG_NDR64) {
673 return ndr_push_align(ndr, size);
675 return NDR_ERR_SUCCESS;
678 _PUBLIC_ enum ndr_err_code ndr_pull_trailer_align(struct ndr_pull *ndr, size_t size)
680 /* MS-RPCE section 2.2.5.3.4.1 */
681 if (ndr->flags & LIBNDR_FLAG_NDR64) {
682 return ndr_pull_align(ndr, size);
684 return NDR_ERR_SUCCESS;
688 push some bytes
690 _PUBLIC_ enum ndr_err_code ndr_push_bytes(struct ndr_push *ndr, const uint8_t *data, uint32_t n)
692 NDR_PUSH_NEED_BYTES(ndr, n);
693 memcpy(ndr->data + ndr->offset, data, n);
694 ndr->offset += n;
695 return NDR_ERR_SUCCESS;
699 push some zero bytes
701 _PUBLIC_ enum ndr_err_code ndr_push_zero(struct ndr_push *ndr, uint32_t n)
703 NDR_PUSH_NEED_BYTES(ndr, n);
704 memset(ndr->data + ndr->offset, 0, n);
705 ndr->offset += n;
706 return NDR_ERR_SUCCESS;
710 push an array of uint8
712 _PUBLIC_ enum ndr_err_code ndr_push_array_uint8(struct ndr_push *ndr, int ndr_flags, const uint8_t *data, uint32_t n)
714 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
715 if (!(ndr_flags & NDR_SCALARS)) {
716 return NDR_ERR_SUCCESS;
718 return ndr_push_bytes(ndr, data, n);
722 push a unique non-zero value if a pointer is non-NULL, otherwise 0
724 _PUBLIC_ enum ndr_err_code ndr_push_unique_ptr(struct ndr_push *ndr, const void *p)
726 uint32_t ptr = 0;
727 if (p) {
728 ptr = ndr->ptr_count * 4;
729 ptr |= 0x00020000;
730 ndr->ptr_count++;
732 return ndr_push_uint3264(ndr, NDR_SCALARS, ptr);
736 push a 'simple' full non-zero value if a pointer is non-NULL, otherwise 0
738 _PUBLIC_ enum ndr_err_code ndr_push_full_ptr(struct ndr_push *ndr, const void *p)
740 uint32_t ptr = 0;
741 if (p) {
742 /* Check if the pointer already exists and has an id */
743 ptr = ndr_token_peek(&ndr->full_ptr_list, p);
744 if (ptr == 0) {
745 ndr->ptr_count++;
746 ptr = ndr->ptr_count;
747 ndr_token_store(ndr, &ndr->full_ptr_list, p, ptr);
750 return ndr_push_uint3264(ndr, NDR_SCALARS, ptr);
754 push always a 0, if a pointer is NULL it's a fatal error
756 _PUBLIC_ enum ndr_err_code ndr_push_ref_ptr(struct ndr_push *ndr)
758 return ndr_push_uint3264(ndr, NDR_SCALARS, 0xAEF1AEF1);
763 push a NTTIME
765 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME(struct ndr_push *ndr, int ndr_flags, NTTIME t)
767 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
768 NDR_CHECK(ndr_push_udlong(ndr, ndr_flags, t));
769 return NDR_ERR_SUCCESS;
773 pull a NTTIME
775 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
777 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
778 NDR_CHECK(ndr_pull_udlong(ndr, ndr_flags, t));
779 return NDR_ERR_SUCCESS;
783 push a NTTIME_1sec
785 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME_1sec(struct ndr_push *ndr, int ndr_flags, NTTIME t)
787 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
788 t /= 10000000;
789 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
790 return NDR_ERR_SUCCESS;
794 pull a NTTIME_1sec
796 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME_1sec(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
798 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
799 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
800 (*t) *= 10000000;
801 return NDR_ERR_SUCCESS;
805 pull a NTTIME_hyper
807 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME_hyper(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
809 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
810 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
811 return NDR_ERR_SUCCESS;
815 push a NTTIME_hyper
817 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME_hyper(struct ndr_push *ndr, int ndr_flags, NTTIME t)
819 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
820 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
821 return NDR_ERR_SUCCESS;
825 push a time_t
827 _PUBLIC_ enum ndr_err_code ndr_push_time_t(struct ndr_push *ndr, int ndr_flags, time_t t)
829 return ndr_push_uint32(ndr, ndr_flags, t);
833 pull a time_t
835 _PUBLIC_ enum ndr_err_code ndr_pull_time_t(struct ndr_pull *ndr, int ndr_flags, time_t *t)
837 uint32_t tt;
838 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &tt));
839 *t = tt;
840 return NDR_ERR_SUCCESS;
845 push a uid_t
847 _PUBLIC_ enum ndr_err_code ndr_push_uid_t(struct ndr_push *ndr, int ndr_flags, uid_t u)
849 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
850 return ndr_push_hyper(ndr, NDR_SCALARS, (uint64_t)u);
854 pull a uid_t
856 _PUBLIC_ enum ndr_err_code ndr_pull_uid_t(struct ndr_pull *ndr, int ndr_flags, uid_t *u)
858 uint64_t uu;
859 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, &uu));
860 *u = (uid_t)uu;
861 if (unlikely(uu != *u)) {
862 DEBUG(0,(__location__ ": uid_t pull doesn't fit 0x%016llx\n",
863 (unsigned long long)uu));
864 return NDR_ERR_NDR64;
866 return NDR_ERR_SUCCESS;
871 push a gid_t
873 _PUBLIC_ enum ndr_err_code ndr_push_gid_t(struct ndr_push *ndr, int ndr_flags, gid_t g)
875 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
876 return ndr_push_hyper(ndr, NDR_SCALARS, (uint64_t)g);
880 pull a gid_t
882 _PUBLIC_ enum ndr_err_code ndr_pull_gid_t(struct ndr_pull *ndr, int ndr_flags, gid_t *g)
884 uint64_t gg;
885 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, &gg));
886 *g = (gid_t)gg;
887 if (unlikely(gg != *g)) {
888 DEBUG(0,(__location__ ": gid_t pull doesn't fit 0x%016llx\n",
889 (unsigned long long)gg));
890 return NDR_ERR_NDR64;
892 return NDR_ERR_SUCCESS;
897 pull a ipv4address
899 _PUBLIC_ enum ndr_err_code ndr_pull_ipv4address(struct ndr_pull *ndr, int ndr_flags, const char **address)
901 uint32_t addr;
902 struct in_addr in;
903 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &addr));
904 in.s_addr = htonl(addr);
905 *address = talloc_strdup(ndr->current_mem_ctx, inet_ntoa(in));
906 NDR_ERR_HAVE_NO_MEMORY(*address);
907 return NDR_ERR_SUCCESS;
911 push a ipv4address
913 _PUBLIC_ enum ndr_err_code ndr_push_ipv4address(struct ndr_push *ndr, int ndr_flags, const char *address)
915 uint32_t addr;
916 if (!is_ipaddress(address)) {
917 return ndr_push_error(ndr, NDR_ERR_IPV4ADDRESS,
918 "Invalid IPv4 address: '%s'",
919 address);
921 addr = inet_addr(address);
922 NDR_CHECK(ndr_push_uint32(ndr, ndr_flags, htonl(addr)));
923 return NDR_ERR_SUCCESS;
927 print a ipv4address
929 _PUBLIC_ void ndr_print_ipv4address(struct ndr_print *ndr, const char *name,
930 const char *address)
932 ndr->print(ndr, "%-25s: %s", name, address);
936 pull a ipv6address
938 #define IPV6_BYTES 16
939 #define IPV6_ADDR_STR_LEN 39
940 _PUBLIC_ enum ndr_err_code ndr_pull_ipv6address(struct ndr_pull *ndr, int ndr_flags, const char **address)
942 uint8_t addr[IPV6_BYTES];
943 char *addr_str = talloc_strdup(ndr->current_mem_ctx, "");
944 int i;
945 NDR_CHECK(ndr_pull_array_uint8(ndr, ndr_flags, addr, IPV6_BYTES));
946 for (i = 0; i < IPV6_BYTES; ++i) {
947 addr_str = talloc_asprintf_append(addr_str, "%02x", addr[i]);
948 /* We need a ':' every second byte but the last one */
949 if (i%2 == 1 && i != (IPV6_BYTES - 1)) {
950 addr_str = talloc_strdup_append(addr_str, ":");
953 *address = addr_str;
954 NDR_ERR_HAVE_NO_MEMORY(*address);
955 return NDR_ERR_SUCCESS;
959 push a ipv6address
961 _PUBLIC_ enum ndr_err_code ndr_push_ipv6address(struct ndr_push *ndr, int ndr_flags, const char *address)
963 #ifdef AF_INET6
964 uint8_t addr[IPV6_BYTES];
965 int ret;
967 if (!is_ipaddress(address)) {
968 return ndr_push_error(ndr, NDR_ERR_IPV6ADDRESS,
969 "Invalid IPv6 address: '%s'",
970 address);
972 ret = inet_pton(AF_INET6, address, addr);
973 if (ret <= 0) {
974 return NDR_ERR_IPV6ADDRESS;
977 NDR_CHECK(ndr_push_array_uint8(ndr, ndr_flags, addr, IPV6_BYTES));
979 return NDR_ERR_SUCCESS;
980 #else
981 return NDR_ERR_IPV6ADDRESS;
982 #endif
986 print a ipv6address
988 _PUBLIC_ void ndr_print_ipv6address(struct ndr_print *ndr, const char *name,
989 const char *address)
991 ndr->print(ndr, "%-25s: %s", name, address);
993 #undef IPV6_BYTES
995 _PUBLIC_ void ndr_print_struct(struct ndr_print *ndr, const char *name, const char *type)
997 ndr->print(ndr, "%s: struct %s", name, type);
1000 _PUBLIC_ void ndr_print_null(struct ndr_print *ndr)
1002 ndr->print(ndr, "UNEXPECTED NULL POINTER");
1005 _PUBLIC_ void ndr_print_enum(struct ndr_print *ndr, const char *name, const char *type,
1006 const char *val, uint32_t value)
1008 if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
1009 ndr->print(ndr, "%-25s: %s (0x%X)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
1010 } else {
1011 ndr->print(ndr, "%-25s: %s (%d)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
1015 _PUBLIC_ void ndr_print_bitmap_flag(struct ndr_print *ndr, size_t size, const char *flag_name, uint32_t flag, uint32_t value)
1017 /* this is an attempt to support multi-bit bitmap masks */
1018 value &= flag;
1020 while (!(flag & 1)) {
1021 flag >>= 1;
1022 value >>= 1;
1024 if (flag == 1) {
1025 ndr->print(ndr, " %d: %-25s", value, flag_name);
1026 } else {
1027 ndr->print(ndr, "0x%02x: %-25s (%d)", value, flag_name, value);
1031 _PUBLIC_ void ndr_print_int8(struct ndr_print *ndr, const char *name, int8_t v)
1033 ndr->print(ndr, "%-25s: %d", name, v);
1036 _PUBLIC_ void ndr_print_uint8(struct ndr_print *ndr, const char *name, uint8_t v)
1038 ndr->print(ndr, "%-25s: 0x%02x (%u)", name, v, v);
1041 _PUBLIC_ void ndr_print_int16(struct ndr_print *ndr, const char *name, int16_t v)
1043 ndr->print(ndr, "%-25s: %d", name, v);
1046 _PUBLIC_ void ndr_print_uint16(struct ndr_print *ndr, const char *name, uint16_t v)
1048 ndr->print(ndr, "%-25s: 0x%04x (%u)", name, v, v);
1051 _PUBLIC_ void ndr_print_int32(struct ndr_print *ndr, const char *name, int32_t v)
1053 ndr->print(ndr, "%-25s: %d", name, v);
1056 _PUBLIC_ void ndr_print_uint32(struct ndr_print *ndr, const char *name, uint32_t v)
1058 ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
1061 _PUBLIC_ void ndr_print_int3264(struct ndr_print *ndr, const char *name, int32_t v)
1063 ndr->print(ndr, "%-25s: %d", name, v);
1066 _PUBLIC_ void ndr_print_uint3264(struct ndr_print *ndr, const char *name, uint32_t v)
1068 ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
1071 _PUBLIC_ void ndr_print_udlong(struct ndr_print *ndr, const char *name, uint64_t v)
1073 ndr->print(ndr, "%-25s: 0x%016llx (%llu)", name, (unsigned long long)v, (unsigned long long)v);
1076 _PUBLIC_ void ndr_print_udlongr(struct ndr_print *ndr, const char *name, uint64_t v)
1078 ndr_print_udlong(ndr, name, v);
1081 _PUBLIC_ void ndr_print_dlong(struct ndr_print *ndr, const char *name, int64_t v)
1083 ndr->print(ndr, "%-25s: 0x%016llx (%lld)", name, (unsigned long long)v, (long long)v);
1086 _PUBLIC_ void ndr_print_double(struct ndr_print *ndr, const char *name, double v)
1088 ndr->print(ndr, "%-25s: %f", name, v);
1091 _PUBLIC_ void ndr_print_hyper(struct ndr_print *ndr, const char *name, uint64_t v)
1093 ndr_print_dlong(ndr, name, v);
1096 _PUBLIC_ void ndr_print_pointer(struct ndr_print *ndr, const char *name, void *v)
1098 ndr->print(ndr, "%-25s: %p", name, v);
1101 _PUBLIC_ void ndr_print_ptr(struct ndr_print *ndr, const char *name, const void *p)
1103 if (p) {
1104 ndr->print(ndr, "%-25s: *", name);
1105 } else {
1106 ndr->print(ndr, "%-25s: NULL", name);
1110 _PUBLIC_ void ndr_print_NTTIME(struct ndr_print *ndr, const char *name, NTTIME t)
1112 ndr->print(ndr, "%-25s: %s", name, nt_time_string(ndr, t));
1115 _PUBLIC_ void ndr_print_NTTIME_1sec(struct ndr_print *ndr, const char *name, NTTIME t)
1117 /* this is a standard NTTIME here
1118 * as it's already converted in the pull/push code
1120 ndr_print_NTTIME(ndr, name, t);
1123 _PUBLIC_ void ndr_print_NTTIME_hyper(struct ndr_print *ndr, const char *name, NTTIME t)
1125 ndr_print_NTTIME(ndr, name, t);
1128 _PUBLIC_ void ndr_print_time_t(struct ndr_print *ndr, const char *name, time_t t)
1130 if (t == (time_t)-1 || t == 0) {
1131 ndr->print(ndr, "%-25s: (time_t)%d", name, (int)t);
1132 } else {
1133 ndr->print(ndr, "%-25s: %s", name, timestring(ndr, t));
1137 _PUBLIC_ void ndr_print_uid_t(struct ndr_print *ndr, const char *name, uid_t u)
1139 ndr_print_dlong(ndr, name, u);
1142 _PUBLIC_ void ndr_print_gid_t(struct ndr_print *ndr, const char *name, gid_t g)
1144 ndr_print_dlong(ndr, name, g);
1147 _PUBLIC_ void ndr_print_union(struct ndr_print *ndr, const char *name, int level, const char *type)
1149 if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
1150 ndr->print(ndr, "%-25s: union %s(case 0x%X)", name, type, level);
1151 } else {
1152 ndr->print(ndr, "%-25s: union %s(case %d)", name, type, level);
1156 _PUBLIC_ void ndr_print_bad_level(struct ndr_print *ndr, const char *name, uint16_t level)
1158 ndr->print(ndr, "UNKNOWN LEVEL %u", level);
1161 _PUBLIC_ void ndr_print_array_uint8(struct ndr_print *ndr, const char *name,
1162 const uint8_t *data, uint32_t count)
1164 int i;
1166 if (data == NULL) {
1167 ndr->print(ndr, "%s: ARRAY(%d) : NULL", name, count);
1168 return;
1171 if (count <= 600 && (ndr->flags & LIBNDR_PRINT_ARRAY_HEX)) {
1172 char s[1202];
1173 for (i=0;i<count;i++) {
1174 snprintf(&s[i*2], 3, "%02x", data[i]);
1176 s[i*2] = 0;
1177 ndr->print(ndr, "%-25s: %s", name, s);
1178 return;
1181 ndr->print(ndr, "%s: ARRAY(%d)", name, count);
1182 ndr->depth++;
1183 for (i=0;i<count;i++) {
1184 char *idx=NULL;
1185 if (asprintf(&idx, "[%d]", i) != -1) {
1186 ndr_print_uint8(ndr, idx, data[i]);
1187 free(idx);
1190 ndr->depth--;
1193 static void ndr_print_asc(struct ndr_print *ndr, const uint8_t *buf, int len)
1195 int i;
1196 for (i=0;i<len;i++)
1197 ndr->print(ndr, "%c", isprint(buf[i])?buf[i]:'.');
1201 ndr_print version of dump_data()
1203 static void ndr_dump_data(struct ndr_print *ndr, const uint8_t *buf, int len)
1205 int i=0;
1207 ndr->no_newline = true;
1209 for (i=0;i<len;) {
1210 if (i%16 == 0 && i<len) {
1211 ndr->print(ndr, "[%04X] ",i);
1214 ndr->print(ndr, "%02X ",(int)buf[i]);
1215 i++;
1216 if (i%8 == 0) ndr->print(ndr," ");
1217 if (i%16 == 0) {
1218 ndr_print_asc(ndr,&buf[i-16],8); ndr->print(ndr," ");
1219 ndr_print_asc(ndr,&buf[i-8],8); ndr->print(ndr, "\n");
1223 if (i%16) {
1224 int n;
1225 n = 16 - (i%16);
1226 ndr->print(ndr, " ");
1227 if (n>8) ndr->print(ndr," ");
1228 while (n--) ndr->print(ndr," ");
1229 n = MIN(8,i%16);
1230 ndr_print_asc(ndr,&buf[i-(i%16)],n); ndr->print(ndr, " ");
1231 n = (i%16) - n;
1232 if (n>0) ndr_print_asc(ndr,&buf[i-n],n);
1233 ndr->print(ndr,"\n");
1236 ndr->no_newline = false;
1240 _PUBLIC_ void ndr_print_DATA_BLOB(struct ndr_print *ndr, const char *name, DATA_BLOB r)
1242 ndr->print(ndr, "%-25s: DATA_BLOB length=%u", name, (unsigned)r.length);
1243 if (r.length) {
1244 ndr_dump_data(ndr, r.data, r.length);
1250 * Push a DATA_BLOB onto the wire.
1251 * 1) When called with LIBNDR_FLAG_ALIGN* alignment flags set, push padding
1252 * bytes _only_. The length is determined by the alignment required and the
1253 * current ndr offset.
1254 * 2) When called with the LIBNDR_FLAG_REMAINING flag, push the byte array to
1255 * the ndr buffer.
1256 * 3) Otherwise, push a uint32 length _and_ a corresponding byte array to the
1257 * ndr buffer.
1259 _PUBLIC_ enum ndr_err_code ndr_push_DATA_BLOB(struct ndr_push *ndr, int ndr_flags, DATA_BLOB blob)
1261 if (ndr->flags & LIBNDR_FLAG_REMAINING) {
1262 /* nothing to do */
1263 } else if (ndr->flags & (LIBNDR_ALIGN_FLAGS & ~LIBNDR_FLAG_NOALIGN)) {
1264 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1265 blob.length = NDR_ALIGN(ndr, 2);
1266 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1267 blob.length = NDR_ALIGN(ndr, 4);
1268 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1269 blob.length = NDR_ALIGN(ndr, 8);
1271 NDR_PUSH_ALLOC_SIZE(ndr, blob.data, blob.length);
1272 data_blob_clear(&blob);
1273 } else {
1274 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, blob.length));
1276 NDR_CHECK(ndr_push_bytes(ndr, blob.data, blob.length));
1277 return NDR_ERR_SUCCESS;
1281 * Pull a DATA_BLOB from the wire.
1282 * 1) when called with LIBNDR_FLAG_ALIGN* alignment flags set, pull padding
1283 * bytes _only_. The length is determined by the alignment required and the
1284 * current ndr offset.
1285 * 2) When called with the LIBNDR_FLAG_REMAINING flag, pull all remaining bytes
1286 * from the ndr buffer.
1287 * 3) Otherwise, pull a uint32 length _and_ a corresponding byte array from the
1288 * ndr buffer.
1290 _PUBLIC_ enum ndr_err_code ndr_pull_DATA_BLOB(struct ndr_pull *ndr, int ndr_flags, DATA_BLOB *blob)
1292 uint32_t length = 0;
1294 if (ndr->flags & LIBNDR_FLAG_REMAINING) {
1295 length = ndr->data_size - ndr->offset;
1296 } else if (ndr->flags & (LIBNDR_ALIGN_FLAGS & ~LIBNDR_FLAG_NOALIGN)) {
1297 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1298 length = NDR_ALIGN(ndr, 2);
1299 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1300 length = NDR_ALIGN(ndr, 4);
1301 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1302 length = NDR_ALIGN(ndr, 8);
1304 if (ndr->data_size - ndr->offset < length) {
1305 length = ndr->data_size - ndr->offset;
1307 } else {
1308 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &length));
1310 NDR_PULL_NEED_BYTES(ndr, length);
1311 *blob = data_blob_talloc(ndr->current_mem_ctx, ndr->data+ndr->offset, length);
1312 ndr->offset += length;
1313 return NDR_ERR_SUCCESS;
1316 _PUBLIC_ uint32_t ndr_size_DATA_BLOB(int ret, const DATA_BLOB *data, int flags)
1318 if (!data) return ret;
1319 return ret + data->length;
1322 _PUBLIC_ void ndr_print_bool(struct ndr_print *ndr, const char *name, const bool b)
1324 ndr->print(ndr, "%-25s: %s", name, b?"true":"false");
1327 _PUBLIC_ NTSTATUS ndr_map_error2ntstatus(enum ndr_err_code ndr_err)
1329 switch (ndr_err) {
1330 case NDR_ERR_SUCCESS:
1331 return NT_STATUS_OK;
1332 case NDR_ERR_BUFSIZE:
1333 return NT_STATUS_BUFFER_TOO_SMALL;
1334 case NDR_ERR_TOKEN:
1335 return NT_STATUS_INTERNAL_ERROR;
1336 case NDR_ERR_ALLOC:
1337 return NT_STATUS_NO_MEMORY;
1338 case NDR_ERR_ARRAY_SIZE:
1339 return NT_STATUS_ARRAY_BOUNDS_EXCEEDED;
1340 case NDR_ERR_INVALID_POINTER:
1341 return NT_STATUS_INVALID_PARAMETER_MIX;
1342 case NDR_ERR_UNREAD_BYTES:
1343 return NT_STATUS_PORT_MESSAGE_TOO_LONG;
1344 default:
1345 break;
1348 /* we should map all error codes to different status codes */
1349 return NT_STATUS_INVALID_PARAMETER;
1352 _PUBLIC_ int ndr_map_error2errno(enum ndr_err_code ndr_err)
1354 switch (ndr_err) {
1355 case NDR_ERR_SUCCESS:
1356 return 0;
1357 case NDR_ERR_BUFSIZE:
1358 return ENOSPC;
1359 case NDR_ERR_TOKEN:
1360 return EINVAL;
1361 case NDR_ERR_ALLOC:
1362 return ENOMEM;
1363 case NDR_ERR_ARRAY_SIZE:
1364 return EMSGSIZE;
1365 case NDR_ERR_INVALID_POINTER:
1366 return EINVAL;
1367 case NDR_ERR_UNREAD_BYTES:
1368 return EOVERFLOW;
1369 default:
1370 break;
1373 /* we should map all error codes to different status codes */
1374 return EINVAL;
1377 _PUBLIC_ enum ndr_err_code ndr_push_timespec(struct ndr_push *ndr,
1378 int ndr_flags,
1379 const struct timespec *t)
1381 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
1382 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t->tv_sec));
1383 NDR_CHECK(ndr_push_uint32(ndr, ndr_flags, t->tv_nsec));
1384 return NDR_ERR_SUCCESS;
1387 _PUBLIC_ enum ndr_err_code ndr_pull_timespec(struct ndr_pull *ndr,
1388 int ndr_flags,
1389 struct timespec *t)
1391 uint64_t secs;
1392 uint32_t nsecs;
1393 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
1394 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, &secs));
1395 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &nsecs));
1396 t->tv_sec = secs;
1397 t->tv_nsec = nsecs;
1398 return NDR_ERR_SUCCESS;
1401 _PUBLIC_ void ndr_print_timespec(struct ndr_print *ndr, const char *name,
1402 const struct timespec *t)
1404 ndr->print(ndr, "%-25s: %s.%ld", name, timestring(ndr, t->tv_sec),
1405 (long)t->tv_nsec);
1408 _PUBLIC_ enum ndr_err_code ndr_push_timeval(struct ndr_push *ndr,
1409 int ndr_flags,
1410 const struct timeval *t)
1412 NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags);
1413 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t->tv_sec));
1414 NDR_CHECK(ndr_push_uint32(ndr, ndr_flags, t->tv_usec));
1415 return NDR_ERR_SUCCESS;
1418 _PUBLIC_ enum ndr_err_code ndr_pull_timeval(struct ndr_pull *ndr,
1419 int ndr_flags,
1420 struct timeval *t)
1422 uint64_t secs;
1423 uint32_t usecs;
1424 NDR_PULL_CHECK_FLAGS(ndr, ndr_flags);
1425 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, &secs));
1426 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &usecs));
1427 t->tv_sec = secs;
1428 t->tv_usec = usecs;
1429 return NDR_ERR_SUCCESS;
1432 _PUBLIC_ void ndr_print_timeval(struct ndr_print *ndr, const char *name,
1433 const struct timeval *t)
1435 ndr->print(ndr, "%-25s: %s.%ld", name, timestring(ndr, t->tv_sec),
1436 (long)t->tv_usec);