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/>.
23 #include "system/network.h"
24 #include "librpc/ndr/libndr.h"
26 #define NDR_SVAL(ndr, ofs) (NDR_BE(ndr)?RSVAL(ndr->data,ofs):SVAL(ndr->data,ofs))
27 #define NDR_IVAL(ndr, ofs) (NDR_BE(ndr)?RIVAL(ndr->data,ofs):IVAL(ndr->data,ofs))
28 #define NDR_IVALS(ndr, ofs) (NDR_BE(ndr)?RIVALS(ndr->data,ofs):IVALS(ndr->data,ofs))
29 #define NDR_SSVAL(ndr, ofs, v) do { if (NDR_BE(ndr)) { RSSVAL(ndr->data,ofs,v); } else SSVAL(ndr->data,ofs,v); } while (0)
30 #define NDR_SIVAL(ndr, ofs, v) do { if (NDR_BE(ndr)) { RSIVAL(ndr->data,ofs,v); } else SIVAL(ndr->data,ofs,v); } while (0)
31 #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 check for data leaks from the server by looking for non-zero pad bytes
36 these could also indicate that real structure elements have been
37 mistaken for padding in the IDL
39 _PUBLIC_
void ndr_check_padding(struct ndr_pull
*ndr
, size_t n
)
41 size_t ofs2
= (ndr
->offset
+ (n
-1)) & ~(n
-1);
43 for (i
=ndr
->offset
;i
<ofs2
;i
++) {
44 if (ndr
->data
[i
] != 0) {
49 DEBUG(0,("WARNING: Non-zero padding to %d: ", (int)n
));
50 for (i
=ndr
->offset
;i
<ofs2
;i
++) {
51 DEBUG(0,("%02x ", ndr
->data
[i
]));
61 _PUBLIC_
enum ndr_err_code
ndr_pull_int8(struct ndr_pull
*ndr
, int ndr_flags
, int8_t *v
)
63 NDR_PULL_NEED_BYTES(ndr
, 1);
64 *v
= (int8_t)CVAL(ndr
->data
, ndr
->offset
);
66 return NDR_ERR_SUCCESS
;
72 _PUBLIC_
enum ndr_err_code
ndr_pull_uint8(struct ndr_pull
*ndr
, int ndr_flags
, uint8_t *v
)
74 NDR_PULL_NEED_BYTES(ndr
, 1);
75 *v
= CVAL(ndr
->data
, ndr
->offset
);
77 return NDR_ERR_SUCCESS
;
83 _PUBLIC_
enum ndr_err_code
ndr_pull_int16(struct ndr_pull
*ndr
, int ndr_flags
, int16_t *v
)
85 NDR_PULL_ALIGN(ndr
, 2);
86 NDR_PULL_NEED_BYTES(ndr
, 2);
87 *v
= (uint16_t)NDR_SVAL(ndr
, ndr
->offset
);
89 return NDR_ERR_SUCCESS
;
95 _PUBLIC_
enum ndr_err_code
ndr_pull_uint16(struct ndr_pull
*ndr
, int ndr_flags
, uint16_t *v
)
97 NDR_PULL_ALIGN(ndr
, 2);
98 NDR_PULL_NEED_BYTES(ndr
, 2);
99 *v
= NDR_SVAL(ndr
, ndr
->offset
);
101 return NDR_ERR_SUCCESS
;
107 _PUBLIC_
enum ndr_err_code
ndr_pull_uint1632(struct ndr_pull
*ndr
, int ndr_flags
, uint16_t *v
)
109 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
111 enum ndr_err_code err
= ndr_pull_uint32(ndr
, ndr_flags
, &v32
);
113 if (unlikely(v32
!= *v
)) {
114 DEBUG(0,(__location__
": non-zero upper 16 bits 0x%08x\n", (unsigned)v32
));
115 return NDR_ERR_NDR64
;
119 return ndr_pull_uint16(ndr
, ndr_flags
, v
);
125 _PUBLIC_
enum ndr_err_code
ndr_pull_int32(struct ndr_pull
*ndr
, int ndr_flags
, int32_t *v
)
127 NDR_PULL_ALIGN(ndr
, 4);
128 NDR_PULL_NEED_BYTES(ndr
, 4);
129 *v
= NDR_IVALS(ndr
, ndr
->offset
);
131 return NDR_ERR_SUCCESS
;
137 _PUBLIC_
enum ndr_err_code
ndr_pull_uint32(struct ndr_pull
*ndr
, int ndr_flags
, uint32_t *v
)
139 NDR_PULL_ALIGN(ndr
, 4);
140 NDR_PULL_NEED_BYTES(ndr
, 4);
141 *v
= NDR_IVAL(ndr
, ndr
->offset
);
143 return NDR_ERR_SUCCESS
;
147 parse a arch dependent uint32/uint64
149 _PUBLIC_
enum ndr_err_code
ndr_pull_uint3264(struct ndr_pull
*ndr
, int ndr_flags
, uint32_t *v
)
152 enum ndr_err_code err
;
153 if (likely(!(ndr
->flags
& LIBNDR_FLAG_NDR64
))) {
154 return ndr_pull_uint32(ndr
, ndr_flags
, v
);
156 err
= ndr_pull_hyper(ndr
, ndr_flags
, &v64
);
158 if (unlikely(v64
!= *v
)) {
159 DEBUG(0,(__location__
": non-zero upper 32 bits 0x%016llx\n",
160 (unsigned long long)v64
));
161 return NDR_ERR_NDR64
;
169 _PUBLIC_
enum ndr_err_code
ndr_pull_double(struct ndr_pull
*ndr
, int ndr_flags
, double *v
)
171 NDR_PULL_ALIGN(ndr
, 8);
172 NDR_PULL_NEED_BYTES(ndr
, 8);
173 memcpy(v
, ndr
->data
+ndr
->offset
, 8);
175 return NDR_ERR_SUCCESS
;
179 parse a pointer referent identifier
181 _PUBLIC_
enum ndr_err_code
ndr_pull_generic_ptr(struct ndr_pull
*ndr
, uint32_t *v
)
183 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, v
));
187 return NDR_ERR_SUCCESS
;
191 parse a ref pointer referent identifier
193 _PUBLIC_
enum ndr_err_code
ndr_pull_ref_ptr(struct ndr_pull
*ndr
, uint32_t *v
)
195 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, v
));
196 /* ref pointers always point to data */
198 return NDR_ERR_SUCCESS
;
204 _PUBLIC_
enum ndr_err_code
ndr_pull_udlong(struct ndr_pull
*ndr
, int ndr_flags
, uint64_t *v
)
206 NDR_PULL_ALIGN(ndr
, 4);
207 NDR_PULL_NEED_BYTES(ndr
, 8);
208 *v
= NDR_IVAL(ndr
, ndr
->offset
);
209 *v
|= (uint64_t)(NDR_IVAL(ndr
, ndr
->offset
+4)) << 32;
211 return NDR_ERR_SUCCESS
;
217 _PUBLIC_
enum ndr_err_code
ndr_pull_udlongr(struct ndr_pull
*ndr
, int ndr_flags
, uint64_t *v
)
219 NDR_PULL_ALIGN(ndr
, 4);
220 NDR_PULL_NEED_BYTES(ndr
, 8);
221 *v
= ((uint64_t)NDR_IVAL(ndr
, ndr
->offset
)) << 32;
222 *v
|= NDR_IVAL(ndr
, ndr
->offset
+4);
224 return NDR_ERR_SUCCESS
;
230 _PUBLIC_
enum ndr_err_code
ndr_pull_dlong(struct ndr_pull
*ndr
, int ndr_flags
, int64_t *v
)
232 return ndr_pull_udlong(ndr
, ndr_flags
, (uint64_t *)v
);
238 _PUBLIC_
enum ndr_err_code
ndr_pull_hyper(struct ndr_pull
*ndr
, int ndr_flags
, uint64_t *v
)
240 NDR_PULL_ALIGN(ndr
, 8);
241 return ndr_pull_udlong(ndr
, ndr_flags
, v
);
247 _PUBLIC_
enum ndr_err_code
ndr_pull_pointer(struct ndr_pull
*ndr
, int ndr_flags
, void* *v
)
250 NDR_PULL_ALIGN(ndr
, sizeof(h
));
251 NDR_PULL_NEED_BYTES(ndr
, sizeof(h
));
252 memcpy(&h
, ndr
->data
+ndr
->offset
, sizeof(h
));
253 ndr
->offset
+= sizeof(h
);
255 return NDR_ERR_SUCCESS
;
261 _PUBLIC_
enum ndr_err_code
ndr_pull_NTSTATUS(struct ndr_pull
*ndr
, int ndr_flags
, NTSTATUS
*status
)
264 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &v
));
265 *status
= NT_STATUS(v
);
266 return NDR_ERR_SUCCESS
;
272 _PUBLIC_
enum ndr_err_code
ndr_push_NTSTATUS(struct ndr_push
*ndr
, int ndr_flags
, NTSTATUS status
)
274 return ndr_push_uint32(ndr
, ndr_flags
, NT_STATUS_V(status
));
277 _PUBLIC_
void ndr_print_NTSTATUS(struct ndr_print
*ndr
, const char *name
, NTSTATUS r
)
279 ndr
->print(ndr
, "%-25s: %s", name
, nt_errstr(r
));
285 _PUBLIC_
enum ndr_err_code
ndr_pull_WERROR(struct ndr_pull
*ndr
, int ndr_flags
, WERROR
*status
)
288 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &v
));
289 *status
= W_ERROR(v
);
290 return NDR_ERR_SUCCESS
;
297 _PUBLIC_
enum ndr_err_code
ndr_pull_enum_uint8(struct ndr_pull
*ndr
, int ndr_flags
, uint8_t *v
)
299 return ndr_pull_uint8(ndr
, ndr_flags
, v
);
303 parse a uint16_t enum
305 _PUBLIC_
enum ndr_err_code
ndr_pull_enum_uint16(struct ndr_pull
*ndr
, int ndr_flags
, uint16_t *v
)
307 return ndr_pull_uint16(ndr
, ndr_flags
, v
);
311 parse a uint1632_t enum (uint32_t on NDR64)
313 _PUBLIC_
enum ndr_err_code
ndr_pull_enum_uint1632(struct ndr_pull
*ndr
, int ndr_flags
, uint16_t *v
)
315 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
317 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &v32
));
320 DEBUG(0,(__location__
": non-zero upper 16 bits 0x%08x\n", (unsigned)v32
));
321 return NDR_ERR_NDR64
;
323 return NDR_ERR_SUCCESS
;
325 return ndr_pull_uint16(ndr
, ndr_flags
, v
);
329 parse a uint32_t enum
331 _PUBLIC_
enum ndr_err_code
ndr_pull_enum_uint32(struct ndr_pull
*ndr
, int ndr_flags
, uint32_t *v
)
333 return ndr_pull_uint32(ndr
, ndr_flags
, v
);
339 _PUBLIC_
enum ndr_err_code
ndr_push_enum_uint8(struct ndr_push
*ndr
, int ndr_flags
, uint8_t v
)
341 return ndr_push_uint8(ndr
, ndr_flags
, v
);
347 _PUBLIC_
enum ndr_err_code
ndr_push_enum_uint16(struct ndr_push
*ndr
, int ndr_flags
, uint16_t v
)
349 return ndr_push_uint16(ndr
, ndr_flags
, v
);
355 _PUBLIC_
enum ndr_err_code
ndr_push_enum_uint32(struct ndr_push
*ndr
, int ndr_flags
, uint32_t v
)
357 return ndr_push_uint32(ndr
, ndr_flags
, v
);
361 push a uint1632_t enum
363 _PUBLIC_
enum ndr_err_code
ndr_push_enum_uint1632(struct ndr_push
*ndr
, int ndr_flags
, uint16_t v
)
365 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
366 return ndr_push_uint32(ndr
, ndr_flags
, v
);
368 return ndr_push_uint16(ndr
, ndr_flags
, v
);
374 _PUBLIC_
enum ndr_err_code
ndr_push_WERROR(struct ndr_push
*ndr
, int ndr_flags
, WERROR status
)
376 return ndr_push_uint32(ndr
, NDR_SCALARS
, W_ERROR_V(status
));
379 _PUBLIC_
void ndr_print_WERROR(struct ndr_print
*ndr
, const char *name
, WERROR r
)
381 ndr
->print(ndr
, "%-25s: %s", name
, win_errstr(r
));
387 _PUBLIC_
enum ndr_err_code
ndr_pull_bytes(struct ndr_pull
*ndr
, uint8_t *data
, uint32_t n
)
389 NDR_PULL_NEED_BYTES(ndr
, n
);
390 memcpy(data
, ndr
->data
+ ndr
->offset
, n
);
392 return NDR_ERR_SUCCESS
;
396 pull an array of uint8
398 _PUBLIC_
enum ndr_err_code
ndr_pull_array_uint8(struct ndr_pull
*ndr
, int ndr_flags
, uint8_t *data
, uint32_t n
)
400 if (!(ndr_flags
& NDR_SCALARS
)) {
401 return NDR_ERR_SUCCESS
;
403 return ndr_pull_bytes(ndr
, data
, n
);
409 _PUBLIC_
enum ndr_err_code
ndr_push_int8(struct ndr_push
*ndr
, int ndr_flags
, int8_t v
)
411 NDR_PUSH_NEED_BYTES(ndr
, 1);
412 SCVAL(ndr
->data
, ndr
->offset
, (uint8_t)v
);
414 return NDR_ERR_SUCCESS
;
420 _PUBLIC_
enum ndr_err_code
ndr_push_uint8(struct ndr_push
*ndr
, int ndr_flags
, uint8_t v
)
422 NDR_PUSH_NEED_BYTES(ndr
, 1);
423 SCVAL(ndr
->data
, ndr
->offset
, v
);
425 return NDR_ERR_SUCCESS
;
431 _PUBLIC_
enum ndr_err_code
ndr_push_int16(struct ndr_push
*ndr
, int ndr_flags
, int16_t v
)
433 NDR_PUSH_ALIGN(ndr
, 2);
434 NDR_PUSH_NEED_BYTES(ndr
, 2);
435 NDR_SSVAL(ndr
, ndr
->offset
, (uint16_t)v
);
437 return NDR_ERR_SUCCESS
;
443 _PUBLIC_
enum ndr_err_code
ndr_push_uint16(struct ndr_push
*ndr
, int ndr_flags
, uint16_t v
)
445 NDR_PUSH_ALIGN(ndr
, 2);
446 NDR_PUSH_NEED_BYTES(ndr
, 2);
447 NDR_SSVAL(ndr
, ndr
->offset
, v
);
449 return NDR_ERR_SUCCESS
;
455 _PUBLIC_
enum ndr_err_code
ndr_push_uint1632(struct ndr_push
*ndr
, int ndr_flags
, uint16_t v
)
457 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
458 return ndr_push_uint32(ndr
, ndr_flags
, v
);
460 return ndr_push_uint16(ndr
, ndr_flags
, v
);
466 _PUBLIC_
enum ndr_err_code
ndr_push_int32(struct ndr_push
*ndr
, int ndr_flags
, int32_t v
)
468 NDR_PUSH_ALIGN(ndr
, 4);
469 NDR_PUSH_NEED_BYTES(ndr
, 4);
470 NDR_SIVALS(ndr
, ndr
->offset
, v
);
472 return NDR_ERR_SUCCESS
;
478 _PUBLIC_
enum ndr_err_code
ndr_push_uint32(struct ndr_push
*ndr
, int ndr_flags
, uint32_t v
)
480 NDR_PUSH_ALIGN(ndr
, 4);
481 NDR_PUSH_NEED_BYTES(ndr
, 4);
482 NDR_SIVAL(ndr
, ndr
->offset
, v
);
484 return NDR_ERR_SUCCESS
;
490 _PUBLIC_
enum ndr_err_code
ndr_push_uint3264(struct ndr_push
*ndr
, int ndr_flags
, uint32_t v
)
492 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
493 return ndr_push_hyper(ndr
, ndr_flags
, v
);
495 return ndr_push_uint32(ndr
, ndr_flags
, v
);
501 _PUBLIC_
enum ndr_err_code
ndr_push_udlong(struct ndr_push
*ndr
, int ndr_flags
, uint64_t v
)
503 NDR_PUSH_ALIGN(ndr
, 4);
504 NDR_PUSH_NEED_BYTES(ndr
, 8);
505 NDR_SIVAL(ndr
, ndr
->offset
, (v
& 0xFFFFFFFF));
506 NDR_SIVAL(ndr
, ndr
->offset
+4, (v
>>32));
508 return NDR_ERR_SUCCESS
;
514 _PUBLIC_
enum ndr_err_code
ndr_push_udlongr(struct ndr_push
*ndr
, int ndr_flags
, uint64_t v
)
516 NDR_PUSH_ALIGN(ndr
, 4);
517 NDR_PUSH_NEED_BYTES(ndr
, 8);
518 NDR_SIVAL(ndr
, ndr
->offset
, (v
>>32));
519 NDR_SIVAL(ndr
, ndr
->offset
+4, (v
& 0xFFFFFFFF));
521 return NDR_ERR_SUCCESS
;
527 _PUBLIC_
enum ndr_err_code
ndr_push_dlong(struct ndr_push
*ndr
, int ndr_flags
, int64_t v
)
529 return ndr_push_udlong(ndr
, NDR_SCALARS
, (uint64_t)v
);
535 _PUBLIC_
enum ndr_err_code
ndr_push_hyper(struct ndr_push
*ndr
, int ndr_flags
, uint64_t v
)
537 NDR_PUSH_ALIGN(ndr
, 8);
538 return ndr_push_udlong(ndr
, NDR_SCALARS
, v
);
544 _PUBLIC_
enum ndr_err_code
ndr_push_double(struct ndr_push
*ndr
, int ndr_flags
, double v
)
546 NDR_PUSH_ALIGN(ndr
, 8);
547 NDR_PUSH_NEED_BYTES(ndr
, 8);
548 memcpy(ndr
->data
+ndr
->offset
, &v
, 8);
550 return NDR_ERR_SUCCESS
;
556 _PUBLIC_
enum ndr_err_code
ndr_push_pointer(struct ndr_push
*ndr
, int ndr_flags
, void* v
)
558 uintptr_t h
= (intptr_t)v
;
559 NDR_PUSH_ALIGN(ndr
, sizeof(h
));
560 NDR_PUSH_NEED_BYTES(ndr
, sizeof(h
));
561 memcpy(ndr
->data
+ndr
->offset
, &h
, sizeof(h
));
562 ndr
->offset
+= sizeof(h
);
563 return NDR_ERR_SUCCESS
;
566 _PUBLIC_
enum ndr_err_code
ndr_push_align(struct ndr_push
*ndr
, size_t size
)
568 /* this is a nasty hack to make pidl work with NDR64 */
570 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
575 } else if (size
== 3) {
576 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
582 NDR_PUSH_ALIGN(ndr
, size
);
583 return NDR_ERR_SUCCESS
;
586 _PUBLIC_
enum ndr_err_code
ndr_pull_align(struct ndr_pull
*ndr
, size_t size
)
588 /* this is a nasty hack to make pidl work with NDR64 */
590 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
595 } else if (size
== 3) {
596 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
602 NDR_PULL_ALIGN(ndr
, size
);
603 return NDR_ERR_SUCCESS
;
606 _PUBLIC_
enum ndr_err_code
ndr_push_union_align(struct ndr_push
*ndr
, size_t size
)
608 /* MS-RPCE section 2.2.5.3.4.4 */
609 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
610 return ndr_push_align(ndr
, size
);
612 return NDR_ERR_SUCCESS
;
615 _PUBLIC_
enum ndr_err_code
ndr_pull_union_align(struct ndr_pull
*ndr
, size_t size
)
617 /* MS-RPCE section 2.2.5.3.4.4 */
618 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
619 return ndr_pull_align(ndr
, size
);
621 return NDR_ERR_SUCCESS
;
624 _PUBLIC_
enum ndr_err_code
ndr_push_trailer_align(struct ndr_push
*ndr
, size_t size
)
626 /* MS-RPCE section 2.2.5.3.4.1 */
627 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
628 return ndr_push_align(ndr
, size
);
630 return NDR_ERR_SUCCESS
;
633 _PUBLIC_
enum ndr_err_code
ndr_pull_trailer_align(struct ndr_pull
*ndr
, size_t size
)
635 /* MS-RPCE section 2.2.5.3.4.1 */
636 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
637 return ndr_pull_align(ndr
, size
);
639 return NDR_ERR_SUCCESS
;
645 _PUBLIC_
enum ndr_err_code
ndr_push_bytes(struct ndr_push
*ndr
, const uint8_t *data
, uint32_t n
)
647 NDR_PUSH_NEED_BYTES(ndr
, n
);
648 memcpy(ndr
->data
+ ndr
->offset
, data
, n
);
650 return NDR_ERR_SUCCESS
;
656 _PUBLIC_
enum ndr_err_code
ndr_push_zero(struct ndr_push
*ndr
, uint32_t n
)
658 NDR_PUSH_NEED_BYTES(ndr
, n
);
659 memset(ndr
->data
+ ndr
->offset
, 0, n
);
661 return NDR_ERR_SUCCESS
;
665 push an array of uint8
667 _PUBLIC_
enum ndr_err_code
ndr_push_array_uint8(struct ndr_push
*ndr
, int ndr_flags
, const uint8_t *data
, uint32_t n
)
669 if (!(ndr_flags
& NDR_SCALARS
)) {
670 return NDR_ERR_SUCCESS
;
672 return ndr_push_bytes(ndr
, data
, n
);
676 push a unique non-zero value if a pointer is non-NULL, otherwise 0
678 _PUBLIC_
enum ndr_err_code
ndr_push_unique_ptr(struct ndr_push
*ndr
, const void *p
)
682 ptr
= ndr
->ptr_count
* 4;
686 return ndr_push_uint3264(ndr
, NDR_SCALARS
, ptr
);
690 push a 'simple' full non-zero value if a pointer is non-NULL, otherwise 0
692 _PUBLIC_
enum ndr_err_code
ndr_push_full_ptr(struct ndr_push
*ndr
, const void *p
)
696 /* Check if the pointer already exists and has an id */
697 ptr
= ndr_token_peek(&ndr
->full_ptr_list
, p
);
700 ptr
= ndr
->ptr_count
;
701 ndr_token_store(ndr
, &ndr
->full_ptr_list
, p
, ptr
);
704 return ndr_push_uint3264(ndr
, NDR_SCALARS
, ptr
);
708 push always a 0, if a pointer is NULL it's a fatal error
710 _PUBLIC_
enum ndr_err_code
ndr_push_ref_ptr(struct ndr_push
*ndr
)
712 return ndr_push_uint3264(ndr
, NDR_SCALARS
, 0xAEF1AEF1);
719 _PUBLIC_
enum ndr_err_code
ndr_push_NTTIME(struct ndr_push
*ndr
, int ndr_flags
, NTTIME t
)
721 NDR_CHECK(ndr_push_udlong(ndr
, ndr_flags
, t
));
722 return NDR_ERR_SUCCESS
;
728 _PUBLIC_
enum ndr_err_code
ndr_pull_NTTIME(struct ndr_pull
*ndr
, int ndr_flags
, NTTIME
*t
)
730 NDR_CHECK(ndr_pull_udlong(ndr
, ndr_flags
, t
));
731 return NDR_ERR_SUCCESS
;
737 _PUBLIC_
enum ndr_err_code
ndr_push_NTTIME_1sec(struct ndr_push
*ndr
, int ndr_flags
, NTTIME t
)
740 NDR_CHECK(ndr_push_hyper(ndr
, ndr_flags
, t
));
741 return NDR_ERR_SUCCESS
;
747 _PUBLIC_
enum ndr_err_code
ndr_pull_NTTIME_1sec(struct ndr_pull
*ndr
, int ndr_flags
, NTTIME
*t
)
749 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, t
));
751 return NDR_ERR_SUCCESS
;
757 _PUBLIC_
enum ndr_err_code
ndr_pull_NTTIME_hyper(struct ndr_pull
*ndr
, int ndr_flags
, NTTIME
*t
)
759 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, t
));
760 return NDR_ERR_SUCCESS
;
766 _PUBLIC_
enum ndr_err_code
ndr_push_NTTIME_hyper(struct ndr_push
*ndr
, int ndr_flags
, NTTIME t
)
768 NDR_CHECK(ndr_push_hyper(ndr
, ndr_flags
, t
));
769 return NDR_ERR_SUCCESS
;
775 _PUBLIC_
enum ndr_err_code
ndr_push_time_t(struct ndr_push
*ndr
, int ndr_flags
, time_t t
)
777 return ndr_push_uint32(ndr
, ndr_flags
, t
);
783 _PUBLIC_
enum ndr_err_code
ndr_pull_time_t(struct ndr_pull
*ndr
, int ndr_flags
, time_t *t
)
786 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &tt
));
788 return NDR_ERR_SUCCESS
;
795 _PUBLIC_
enum ndr_err_code
ndr_pull_ipv4address(struct ndr_pull
*ndr
, int ndr_flags
, const char **address
)
799 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &addr
));
800 in
.s_addr
= htonl(addr
);
801 *address
= talloc_strdup(ndr
->current_mem_ctx
, inet_ntoa(in
));
802 NDR_ERR_HAVE_NO_MEMORY(*address
);
803 return NDR_ERR_SUCCESS
;
809 _PUBLIC_
enum ndr_err_code
ndr_push_ipv4address(struct ndr_push
*ndr
, int ndr_flags
, const char *address
)
812 if (!is_ipaddress(address
)) {
813 return ndr_push_error(ndr
, NDR_ERR_IPV4ADDRESS
,
814 "Invalid IPv4 address: '%s'",
817 addr
= inet_addr(address
);
818 NDR_CHECK(ndr_push_uint32(ndr
, ndr_flags
, htonl(addr
)));
819 return NDR_ERR_SUCCESS
;
825 _PUBLIC_
void ndr_print_ipv4address(struct ndr_print
*ndr
, const char *name
,
828 ndr
->print(ndr
, "%-25s: %s", name
, address
);
832 _PUBLIC_
void ndr_print_struct(struct ndr_print
*ndr
, const char *name
, const char *type
)
834 ndr
->print(ndr
, "%s: struct %s", name
, type
);
837 _PUBLIC_
void ndr_print_enum(struct ndr_print
*ndr
, const char *name
, const char *type
,
838 const char *val
, uint32_t value
)
840 if (ndr
->flags
& LIBNDR_PRINT_ARRAY_HEX
) {
841 ndr
->print(ndr
, "%-25s: %s (0x%X)", name
, val
?val
:"UNKNOWN_ENUM_VALUE", value
);
843 ndr
->print(ndr
, "%-25s: %s (%d)", name
, val
?val
:"UNKNOWN_ENUM_VALUE", value
);
847 _PUBLIC_
void ndr_print_bitmap_flag(struct ndr_print
*ndr
, size_t size
, const char *flag_name
, uint32_t flag
, uint32_t value
)
849 /* this is an attempt to support multi-bit bitmap masks */
852 while (!(flag
& 1)) {
857 ndr
->print(ndr
, " %d: %-25s", value
, flag_name
);
859 ndr
->print(ndr
, "0x%02x: %-25s (%d)", value
, flag_name
, value
);
863 _PUBLIC_
void ndr_print_int8(struct ndr_print
*ndr
, const char *name
, int8_t v
)
865 ndr
->print(ndr
, "%-25s: %d", name
, v
);
868 _PUBLIC_
void ndr_print_uint8(struct ndr_print
*ndr
, const char *name
, uint8_t v
)
870 ndr
->print(ndr
, "%-25s: 0x%02x (%u)", name
, v
, v
);
873 _PUBLIC_
void ndr_print_int16(struct ndr_print
*ndr
, const char *name
, int16_t v
)
875 ndr
->print(ndr
, "%-25s: %d", name
, v
);
878 _PUBLIC_
void ndr_print_uint16(struct ndr_print
*ndr
, const char *name
, uint16_t v
)
880 ndr
->print(ndr
, "%-25s: 0x%04x (%u)", name
, v
, v
);
883 _PUBLIC_
void ndr_print_int32(struct ndr_print
*ndr
, const char *name
, int32_t v
)
885 ndr
->print(ndr
, "%-25s: %d", name
, v
);
888 _PUBLIC_
void ndr_print_uint32(struct ndr_print
*ndr
, const char *name
, uint32_t v
)
890 ndr
->print(ndr
, "%-25s: 0x%08x (%u)", name
, v
, v
);
893 _PUBLIC_
void ndr_print_udlong(struct ndr_print
*ndr
, const char *name
, uint64_t v
)
895 ndr
->print(ndr
, "%-25s: 0x%016llx (%llu)", name
, (unsigned long long)v
, (unsigned long long)v
);
898 _PUBLIC_
void ndr_print_udlongr(struct ndr_print
*ndr
, const char *name
, uint64_t v
)
900 ndr_print_udlong(ndr
, name
, v
);
903 _PUBLIC_
void ndr_print_dlong(struct ndr_print
*ndr
, const char *name
, int64_t v
)
905 ndr
->print(ndr
, "%-25s: 0x%016llx (%lld)", name
, (unsigned long long)v
, (long long)v
);
908 _PUBLIC_
void ndr_print_double(struct ndr_print
*ndr
, const char *name
, double v
)
910 ndr
->print(ndr
, "%-25s: %f", name
, v
);
913 _PUBLIC_
void ndr_print_hyper(struct ndr_print
*ndr
, const char *name
, uint64_t v
)
915 ndr_print_dlong(ndr
, name
, v
);
918 _PUBLIC_
void ndr_print_pointer(struct ndr_print
*ndr
, const char *name
, void *v
)
920 ndr
->print(ndr
, "%-25s: %p", name
, v
);
923 _PUBLIC_
void ndr_print_ptr(struct ndr_print
*ndr
, const char *name
, const void *p
)
926 ndr
->print(ndr
, "%-25s: *", name
);
928 ndr
->print(ndr
, "%-25s: NULL", name
);
932 _PUBLIC_
void ndr_print_NTTIME(struct ndr_print
*ndr
, const char *name
, NTTIME t
)
934 ndr
->print(ndr
, "%-25s: %s", name
, nt_time_string(ndr
, t
));
937 _PUBLIC_
void ndr_print_NTTIME_1sec(struct ndr_print
*ndr
, const char *name
, NTTIME t
)
939 /* this is a standard NTTIME here
940 * as it's already converted in the pull/push code
942 ndr_print_NTTIME(ndr
, name
, t
);
945 _PUBLIC_
void ndr_print_NTTIME_hyper(struct ndr_print
*ndr
, const char *name
, NTTIME t
)
947 ndr_print_NTTIME(ndr
, name
, t
);
950 _PUBLIC_
void ndr_print_time_t(struct ndr_print
*ndr
, const char *name
, time_t t
)
952 if (t
== (time_t)-1 || t
== 0) {
953 ndr
->print(ndr
, "%-25s: (time_t)%d", name
, (int)t
);
955 ndr
->print(ndr
, "%-25s: %s", name
, timestring(ndr
, t
));
959 _PUBLIC_
void ndr_print_union(struct ndr_print
*ndr
, const char *name
, int level
, const char *type
)
961 if (ndr
->flags
& LIBNDR_PRINT_ARRAY_HEX
) {
962 ndr
->print(ndr
, "%-25s: union %s(case 0x%X)", name
, type
, level
);
964 ndr
->print(ndr
, "%-25s: union %s(case %d)", name
, type
, level
);
968 _PUBLIC_
void ndr_print_bad_level(struct ndr_print
*ndr
, const char *name
, uint16_t level
)
970 ndr
->print(ndr
, "UNKNOWN LEVEL %u", level
);
973 _PUBLIC_
void ndr_print_array_uint8(struct ndr_print
*ndr
, const char *name
,
974 const uint8_t *data
, uint32_t count
)
978 if (count
<= 600 && (ndr
->flags
& LIBNDR_PRINT_ARRAY_HEX
)) {
980 for (i
=0;i
<count
;i
++) {
981 snprintf(&s
[i
*2], 3, "%02x", data
[i
]);
984 ndr
->print(ndr
, "%-25s: %s", name
, s
);
988 ndr
->print(ndr
, "%s: ARRAY(%d)", name
, count
);
990 for (i
=0;i
<count
;i
++) {
992 if (asprintf(&idx
, "[%d]", i
) != -1) {
993 ndr_print_uint8(ndr
, idx
, data
[i
]);
1000 _PUBLIC_
void ndr_print_DATA_BLOB(struct ndr_print
*ndr
, const char *name
, DATA_BLOB r
)
1002 ndr
->print(ndr
, "%-25s: DATA_BLOB length=%u", name
, (unsigned)r
.length
);
1004 dump_data(10, r
.data
, r
.length
);
1010 push a DATA_BLOB onto the wire.
1012 _PUBLIC_
enum ndr_err_code
ndr_push_DATA_BLOB(struct ndr_push
*ndr
, int ndr_flags
, DATA_BLOB blob
)
1014 if (ndr
->flags
& LIBNDR_FLAG_REMAINING
) {
1016 } else if (ndr
->flags
& LIBNDR_ALIGN_FLAGS
) {
1017 if (ndr
->flags
& LIBNDR_FLAG_NOALIGN
) {
1019 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
1020 blob
.length
= NDR_ALIGN(ndr
, 2);
1021 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
1022 blob
.length
= NDR_ALIGN(ndr
, 4);
1023 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
1024 blob
.length
= NDR_ALIGN(ndr
, 8);
1026 NDR_PUSH_ALLOC_SIZE(ndr
, blob
.data
, blob
.length
);
1027 data_blob_clear(&blob
);
1029 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, blob
.length
));
1031 NDR_CHECK(ndr_push_bytes(ndr
, blob
.data
, blob
.length
));
1032 return NDR_ERR_SUCCESS
;
1036 pull a DATA_BLOB from the wire.
1038 _PUBLIC_
enum ndr_err_code
ndr_pull_DATA_BLOB(struct ndr_pull
*ndr
, int ndr_flags
, DATA_BLOB
*blob
)
1040 uint32_t length
= 0;
1042 if (ndr
->flags
& LIBNDR_FLAG_REMAINING
) {
1043 length
= ndr
->data_size
- ndr
->offset
;
1044 } else if (ndr
->flags
& LIBNDR_ALIGN_FLAGS
) {
1045 if (ndr
->flags
& LIBNDR_FLAG_NOALIGN
) {
1047 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
1048 length
= NDR_ALIGN(ndr
, 2);
1049 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
1050 length
= NDR_ALIGN(ndr
, 4);
1051 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
1052 length
= NDR_ALIGN(ndr
, 8);
1054 if (ndr
->data_size
- ndr
->offset
< length
) {
1055 length
= ndr
->data_size
- ndr
->offset
;
1058 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &length
));
1060 NDR_PULL_NEED_BYTES(ndr
, length
);
1061 *blob
= data_blob_talloc(ndr
->current_mem_ctx
, ndr
->data
+ndr
->offset
, length
);
1062 ndr
->offset
+= length
;
1063 return NDR_ERR_SUCCESS
;
1066 _PUBLIC_
uint32_t ndr_size_DATA_BLOB(int ret
, const DATA_BLOB
*data
, int flags
)
1068 if (!data
) return ret
;
1069 return ret
+ data
->length
;