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_int32(struct ndr_pull
*ndr
, int ndr_flags
, int32_t *v
)
109 NDR_PULL_ALIGN(ndr
, 4);
110 NDR_PULL_NEED_BYTES(ndr
, 4);
111 *v
= NDR_IVALS(ndr
, ndr
->offset
);
113 return NDR_ERR_SUCCESS
;
119 _PUBLIC_
enum ndr_err_code
ndr_pull_uint32(struct ndr_pull
*ndr
, int ndr_flags
, uint32_t *v
)
121 NDR_PULL_ALIGN(ndr
, 4);
122 NDR_PULL_NEED_BYTES(ndr
, 4);
123 *v
= NDR_IVAL(ndr
, ndr
->offset
);
125 return NDR_ERR_SUCCESS
;
129 parse a arch dependent uint32/uint64
131 _PUBLIC_
enum ndr_err_code
ndr_pull_uint3264(struct ndr_pull
*ndr
, int ndr_flags
, uint32_t *v
)
134 enum ndr_err_code err
;
135 if (likely(!(ndr
->flags
& LIBNDR_FLAG_NDR64
))) {
136 return ndr_pull_uint32(ndr
, ndr_flags
, v
);
138 err
= ndr_pull_hyper(ndr
, ndr_flags
, &v64
);
140 if (unlikely(v64
!= *v
)) {
141 DEBUG(0,(__location__
": non-zero upper 32 bits 0x%016llx\n",
142 (unsigned long long)v64
));
143 return NDR_ERR_NDR64
;
151 _PUBLIC_
enum ndr_err_code
ndr_pull_double(struct ndr_pull
*ndr
, int ndr_flags
, double *v
)
153 NDR_PULL_ALIGN(ndr
, 8);
154 NDR_PULL_NEED_BYTES(ndr
, 8);
155 memcpy(v
, ndr
->data
+ndr
->offset
, 8);
157 return NDR_ERR_SUCCESS
;
161 parse a pointer referent identifier
163 _PUBLIC_
enum ndr_err_code
ndr_pull_generic_ptr(struct ndr_pull
*ndr
, uint32_t *v
)
165 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, v
));
169 return NDR_ERR_SUCCESS
;
173 parse a ref pointer referent identifier
175 _PUBLIC_
enum ndr_err_code
ndr_pull_ref_ptr(struct ndr_pull
*ndr
, uint32_t *v
)
177 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, v
));
178 /* ref pointers always point to data */
180 return NDR_ERR_SUCCESS
;
186 _PUBLIC_
enum ndr_err_code
ndr_pull_udlong(struct ndr_pull
*ndr
, int ndr_flags
, uint64_t *v
)
188 NDR_PULL_ALIGN(ndr
, 4);
189 NDR_PULL_NEED_BYTES(ndr
, 8);
190 *v
= NDR_IVAL(ndr
, ndr
->offset
);
191 *v
|= (uint64_t)(NDR_IVAL(ndr
, ndr
->offset
+4)) << 32;
193 return NDR_ERR_SUCCESS
;
199 _PUBLIC_
enum ndr_err_code
ndr_pull_udlongr(struct ndr_pull
*ndr
, int ndr_flags
, uint64_t *v
)
201 NDR_PULL_ALIGN(ndr
, 4);
202 NDR_PULL_NEED_BYTES(ndr
, 8);
203 *v
= ((uint64_t)NDR_IVAL(ndr
, ndr
->offset
)) << 32;
204 *v
|= NDR_IVAL(ndr
, ndr
->offset
+4);
206 return NDR_ERR_SUCCESS
;
212 _PUBLIC_
enum ndr_err_code
ndr_pull_dlong(struct ndr_pull
*ndr
, int ndr_flags
, int64_t *v
)
214 return ndr_pull_udlong(ndr
, ndr_flags
, (uint64_t *)v
);
220 _PUBLIC_
enum ndr_err_code
ndr_pull_hyper(struct ndr_pull
*ndr
, int ndr_flags
, uint64_t *v
)
222 NDR_PULL_ALIGN(ndr
, 8);
223 return ndr_pull_udlong(ndr
, ndr_flags
, v
);
229 _PUBLIC_
enum ndr_err_code
ndr_pull_pointer(struct ndr_pull
*ndr
, int ndr_flags
, void* *v
)
232 NDR_PULL_ALIGN(ndr
, sizeof(h
));
233 NDR_PULL_NEED_BYTES(ndr
, sizeof(h
));
234 memcpy(&h
, ndr
->data
+ndr
->offset
, sizeof(h
));
235 ndr
->offset
+= sizeof(h
);
237 return NDR_ERR_SUCCESS
;
243 _PUBLIC_
enum ndr_err_code
ndr_pull_NTSTATUS(struct ndr_pull
*ndr
, int ndr_flags
, NTSTATUS
*status
)
246 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &v
));
247 *status
= NT_STATUS(v
);
248 return NDR_ERR_SUCCESS
;
254 _PUBLIC_
enum ndr_err_code
ndr_push_NTSTATUS(struct ndr_push
*ndr
, int ndr_flags
, NTSTATUS status
)
256 return ndr_push_uint32(ndr
, ndr_flags
, NT_STATUS_V(status
));
259 _PUBLIC_
void ndr_print_NTSTATUS(struct ndr_print
*ndr
, const char *name
, NTSTATUS r
)
261 ndr
->print(ndr
, "%-25s: %s", name
, nt_errstr(r
));
267 _PUBLIC_
enum ndr_err_code
ndr_pull_WERROR(struct ndr_pull
*ndr
, int ndr_flags
, WERROR
*status
)
270 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &v
));
271 *status
= W_ERROR(v
);
272 return NDR_ERR_SUCCESS
;
279 _PUBLIC_
enum ndr_err_code
ndr_pull_enum_uint8(struct ndr_pull
*ndr
, int ndr_flags
, uint8_t *v
)
281 return ndr_pull_uint8(ndr
, ndr_flags
, v
);
285 parse a uint16_t enum (uint32_t on NDR64)
287 _PUBLIC_
enum ndr_err_code
ndr_pull_enum_uint16(struct ndr_pull
*ndr
, int ndr_flags
, uint16_t *v
)
289 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
291 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &v32
));
294 DEBUG(0,(__location__
": non-zero upper 16 bits 0x%08x\n", (unsigned)v32
));
295 return NDR_ERR_NDR64
;
297 return NDR_ERR_SUCCESS
;
299 NDR_PULL_ALIGN(ndr
, 2);
300 NDR_PULL_NEED_BYTES(ndr
, 2);
301 *v
= NDR_SVAL(ndr
, ndr
->offset
);
303 return NDR_ERR_SUCCESS
;
307 parse a uint32_t enum
309 _PUBLIC_
enum ndr_err_code
ndr_pull_enum_uint32(struct ndr_pull
*ndr
, int ndr_flags
, uint32_t *v
)
311 return ndr_pull_uint32(ndr
, ndr_flags
, v
);
317 _PUBLIC_
enum ndr_err_code
ndr_push_enum_uint8(struct ndr_push
*ndr
, int ndr_flags
, uint8_t v
)
319 return ndr_push_uint8(ndr
, ndr_flags
, v
);
323 push a uint16_t enum (uint32_t on NDR64)
325 _PUBLIC_
enum ndr_err_code
ndr_push_enum_uint16(struct ndr_push
*ndr
, int ndr_flags
, uint16_t v
)
327 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
328 return ndr_push_uint32(ndr
, ndr_flags
, v
);
330 return ndr_push_uint16(ndr
, ndr_flags
, v
);
336 _PUBLIC_
enum ndr_err_code
ndr_push_enum_uint32(struct ndr_push
*ndr
, int ndr_flags
, uint32_t v
)
338 return ndr_push_uint32(ndr
, ndr_flags
, v
);
345 _PUBLIC_
enum ndr_err_code
ndr_push_WERROR(struct ndr_push
*ndr
, int ndr_flags
, WERROR status
)
347 return ndr_push_uint32(ndr
, NDR_SCALARS
, W_ERROR_V(status
));
350 _PUBLIC_
void ndr_print_WERROR(struct ndr_print
*ndr
, const char *name
, WERROR r
)
352 ndr
->print(ndr
, "%-25s: %s", name
, win_errstr(r
));
358 _PUBLIC_
enum ndr_err_code
ndr_pull_bytes(struct ndr_pull
*ndr
, uint8_t *data
, uint32_t n
)
360 NDR_PULL_NEED_BYTES(ndr
, n
);
361 memcpy(data
, ndr
->data
+ ndr
->offset
, n
);
363 return NDR_ERR_SUCCESS
;
367 pull an array of uint8
369 _PUBLIC_
enum ndr_err_code
ndr_pull_array_uint8(struct ndr_pull
*ndr
, int ndr_flags
, uint8_t *data
, uint32_t n
)
371 if (!(ndr_flags
& NDR_SCALARS
)) {
372 return NDR_ERR_SUCCESS
;
374 return ndr_pull_bytes(ndr
, data
, n
);
380 _PUBLIC_
enum ndr_err_code
ndr_push_int8(struct ndr_push
*ndr
, int ndr_flags
, int8_t v
)
382 NDR_PUSH_NEED_BYTES(ndr
, 1);
383 SCVAL(ndr
->data
, ndr
->offset
, (uint8_t)v
);
385 return NDR_ERR_SUCCESS
;
391 _PUBLIC_
enum ndr_err_code
ndr_push_uint8(struct ndr_push
*ndr
, int ndr_flags
, uint8_t v
)
393 NDR_PUSH_NEED_BYTES(ndr
, 1);
394 SCVAL(ndr
->data
, ndr
->offset
, v
);
396 return NDR_ERR_SUCCESS
;
402 _PUBLIC_
enum ndr_err_code
ndr_push_int16(struct ndr_push
*ndr
, int ndr_flags
, int16_t v
)
404 NDR_PUSH_ALIGN(ndr
, 2);
405 NDR_PUSH_NEED_BYTES(ndr
, 2);
406 NDR_SSVAL(ndr
, ndr
->offset
, (uint16_t)v
);
408 return NDR_ERR_SUCCESS
;
414 _PUBLIC_
enum ndr_err_code
ndr_push_uint16(struct ndr_push
*ndr
, int ndr_flags
, uint16_t v
)
416 NDR_PUSH_ALIGN(ndr
, 2);
417 NDR_PUSH_NEED_BYTES(ndr
, 2);
418 NDR_SSVAL(ndr
, ndr
->offset
, v
);
420 return NDR_ERR_SUCCESS
;
426 _PUBLIC_
enum ndr_err_code
ndr_push_int32(struct ndr_push
*ndr
, int ndr_flags
, int32_t v
)
428 NDR_PUSH_ALIGN(ndr
, 4);
429 NDR_PUSH_NEED_BYTES(ndr
, 4);
430 NDR_SIVALS(ndr
, ndr
->offset
, v
);
432 return NDR_ERR_SUCCESS
;
438 _PUBLIC_
enum ndr_err_code
ndr_push_uint32(struct ndr_push
*ndr
, int ndr_flags
, uint32_t v
)
440 NDR_PUSH_ALIGN(ndr
, 4);
441 NDR_PUSH_NEED_BYTES(ndr
, 4);
442 NDR_SIVAL(ndr
, ndr
->offset
, v
);
444 return NDR_ERR_SUCCESS
;
450 _PUBLIC_
enum ndr_err_code
ndr_push_uint3264(struct ndr_push
*ndr
, int ndr_flags
, uint32_t v
)
452 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
453 return ndr_push_hyper(ndr
, ndr_flags
, v
);
455 NDR_PUSH_ALIGN(ndr
, 4);
456 NDR_PUSH_NEED_BYTES(ndr
, 4);
457 NDR_SIVAL(ndr
, ndr
->offset
, v
);
459 return NDR_ERR_SUCCESS
;
465 _PUBLIC_
enum ndr_err_code
ndr_push_udlong(struct ndr_push
*ndr
, int ndr_flags
, uint64_t v
)
467 NDR_PUSH_ALIGN(ndr
, 4);
468 NDR_PUSH_NEED_BYTES(ndr
, 8);
469 NDR_SIVAL(ndr
, ndr
->offset
, (v
& 0xFFFFFFFF));
470 NDR_SIVAL(ndr
, ndr
->offset
+4, (v
>>32));
472 return NDR_ERR_SUCCESS
;
478 _PUBLIC_
enum ndr_err_code
ndr_push_udlongr(struct ndr_push
*ndr
, int ndr_flags
, uint64_t v
)
480 NDR_PUSH_ALIGN(ndr
, 4);
481 NDR_PUSH_NEED_BYTES(ndr
, 8);
482 NDR_SIVAL(ndr
, ndr
->offset
, (v
>>32));
483 NDR_SIVAL(ndr
, ndr
->offset
+4, (v
& 0xFFFFFFFF));
485 return NDR_ERR_SUCCESS
;
491 _PUBLIC_
enum ndr_err_code
ndr_push_dlong(struct ndr_push
*ndr
, int ndr_flags
, int64_t v
)
493 return ndr_push_udlong(ndr
, NDR_SCALARS
, (uint64_t)v
);
499 _PUBLIC_
enum ndr_err_code
ndr_push_hyper(struct ndr_push
*ndr
, int ndr_flags
, uint64_t v
)
501 NDR_PUSH_ALIGN(ndr
, 8);
502 return ndr_push_udlong(ndr
, NDR_SCALARS
, v
);
508 _PUBLIC_
enum ndr_err_code
ndr_push_double(struct ndr_push
*ndr
, int ndr_flags
, double v
)
510 NDR_PUSH_ALIGN(ndr
, 8);
511 NDR_PUSH_NEED_BYTES(ndr
, 8);
512 memcpy(ndr
->data
+ndr
->offset
, &v
, 8);
514 return NDR_ERR_SUCCESS
;
520 _PUBLIC_
enum ndr_err_code
ndr_push_pointer(struct ndr_push
*ndr
, int ndr_flags
, void* v
)
522 uintptr_t h
= (intptr_t)v
;
523 NDR_PUSH_ALIGN(ndr
, sizeof(h
));
524 NDR_PUSH_NEED_BYTES(ndr
, sizeof(h
));
525 memcpy(ndr
->data
+ndr
->offset
, &h
, sizeof(h
));
526 ndr
->offset
+= sizeof(h
);
527 return NDR_ERR_SUCCESS
;
530 _PUBLIC_
enum ndr_err_code
ndr_push_align(struct ndr_push
*ndr
, size_t size
)
532 /* this is a nasty hack to make pidl work with NDR64 */
534 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
540 NDR_PUSH_ALIGN(ndr
, size
);
541 return NDR_ERR_SUCCESS
;
544 _PUBLIC_
enum ndr_err_code
ndr_pull_align(struct ndr_pull
*ndr
, size_t size
)
546 /* this is a nasty hack to make pidl work with NDR64 */
548 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
554 NDR_PULL_ALIGN(ndr
, size
);
555 return NDR_ERR_SUCCESS
;
558 _PUBLIC_
enum ndr_err_code
ndr_push_union_align(struct ndr_push
*ndr
, size_t size
)
560 /* MS-RPCE section 2.2.5.3.4.4 */
561 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
562 return ndr_push_align(ndr
, size
);
564 return NDR_ERR_SUCCESS
;
567 _PUBLIC_
enum ndr_err_code
ndr_pull_union_align(struct ndr_pull
*ndr
, size_t size
)
569 /* MS-RPCE section 2.2.5.3.4.4 */
570 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
571 return ndr_pull_align(ndr
, size
);
573 return NDR_ERR_SUCCESS
;
576 _PUBLIC_
enum ndr_err_code
ndr_push_trailer_align(struct ndr_push
*ndr
, size_t size
)
578 /* MS-RPCE section 2.2.5.3.4.1 */
579 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
580 return ndr_push_align(ndr
, size
);
582 return NDR_ERR_SUCCESS
;
585 _PUBLIC_
enum ndr_err_code
ndr_pull_trailer_align(struct ndr_pull
*ndr
, size_t size
)
587 /* MS-RPCE section 2.2.5.3.4.1 */
588 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
589 return ndr_pull_align(ndr
, size
);
591 return NDR_ERR_SUCCESS
;
597 _PUBLIC_
enum ndr_err_code
ndr_push_bytes(struct ndr_push
*ndr
, const uint8_t *data
, uint32_t n
)
599 NDR_PUSH_NEED_BYTES(ndr
, n
);
600 memcpy(ndr
->data
+ ndr
->offset
, data
, n
);
602 return NDR_ERR_SUCCESS
;
608 _PUBLIC_
enum ndr_err_code
ndr_push_zero(struct ndr_push
*ndr
, uint32_t n
)
610 NDR_PUSH_NEED_BYTES(ndr
, n
);
611 memset(ndr
->data
+ ndr
->offset
, 0, n
);
613 return NDR_ERR_SUCCESS
;
617 push an array of uint8
619 _PUBLIC_
enum ndr_err_code
ndr_push_array_uint8(struct ndr_push
*ndr
, int ndr_flags
, const uint8_t *data
, uint32_t n
)
621 if (!(ndr_flags
& NDR_SCALARS
)) {
622 return NDR_ERR_SUCCESS
;
624 return ndr_push_bytes(ndr
, data
, n
);
628 push a unique non-zero value if a pointer is non-NULL, otherwise 0
630 _PUBLIC_
enum ndr_err_code
ndr_push_unique_ptr(struct ndr_push
*ndr
, const void *p
)
634 ptr
= ndr
->ptr_count
* 4;
638 return ndr_push_uint3264(ndr
, NDR_SCALARS
, ptr
);
642 push a 'simple' full non-zero value if a pointer is non-NULL, otherwise 0
644 _PUBLIC_
enum ndr_err_code
ndr_push_full_ptr(struct ndr_push
*ndr
, const void *p
)
648 /* Check if the pointer already exists and has an id */
649 ptr
= ndr_token_peek(&ndr
->full_ptr_list
, p
);
652 ptr
= ndr
->ptr_count
;
653 ndr_token_store(ndr
, &ndr
->full_ptr_list
, p
, ptr
);
656 return ndr_push_uint3264(ndr
, NDR_SCALARS
, ptr
);
660 push always a 0, if a pointer is NULL it's a fatal error
662 _PUBLIC_
enum ndr_err_code
ndr_push_ref_ptr(struct ndr_push
*ndr
)
664 return ndr_push_uint3264(ndr
, NDR_SCALARS
, 0xAEF1AEF1);
671 _PUBLIC_
enum ndr_err_code
ndr_push_NTTIME(struct ndr_push
*ndr
, int ndr_flags
, NTTIME t
)
673 NDR_CHECK(ndr_push_udlong(ndr
, ndr_flags
, t
));
674 return NDR_ERR_SUCCESS
;
680 _PUBLIC_
enum ndr_err_code
ndr_pull_NTTIME(struct ndr_pull
*ndr
, int ndr_flags
, NTTIME
*t
)
682 NDR_CHECK(ndr_pull_udlong(ndr
, ndr_flags
, t
));
683 return NDR_ERR_SUCCESS
;
689 _PUBLIC_
enum ndr_err_code
ndr_push_NTTIME_1sec(struct ndr_push
*ndr
, int ndr_flags
, NTTIME t
)
692 NDR_CHECK(ndr_push_hyper(ndr
, ndr_flags
, t
));
693 return NDR_ERR_SUCCESS
;
699 _PUBLIC_
enum ndr_err_code
ndr_pull_NTTIME_1sec(struct ndr_pull
*ndr
, int ndr_flags
, NTTIME
*t
)
701 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, t
));
703 return NDR_ERR_SUCCESS
;
709 _PUBLIC_
enum ndr_err_code
ndr_pull_NTTIME_hyper(struct ndr_pull
*ndr
, int ndr_flags
, NTTIME
*t
)
711 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, t
));
712 return NDR_ERR_SUCCESS
;
718 _PUBLIC_
enum ndr_err_code
ndr_push_NTTIME_hyper(struct ndr_push
*ndr
, int ndr_flags
, NTTIME t
)
720 NDR_CHECK(ndr_push_hyper(ndr
, ndr_flags
, t
));
721 return NDR_ERR_SUCCESS
;
727 _PUBLIC_
enum ndr_err_code
ndr_push_time_t(struct ndr_push
*ndr
, int ndr_flags
, time_t t
)
729 return ndr_push_uint32(ndr
, ndr_flags
, t
);
735 _PUBLIC_
enum ndr_err_code
ndr_pull_time_t(struct ndr_pull
*ndr
, int ndr_flags
, time_t *t
)
738 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &tt
));
740 return NDR_ERR_SUCCESS
;
747 _PUBLIC_
enum ndr_err_code
ndr_pull_ipv4address(struct ndr_pull
*ndr
, int ndr_flags
, const char **address
)
751 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &addr
));
752 in
.s_addr
= htonl(addr
);
753 *address
= talloc_strdup(ndr
->current_mem_ctx
, inet_ntoa(in
));
754 NDR_ERR_HAVE_NO_MEMORY(*address
);
755 return NDR_ERR_SUCCESS
;
761 _PUBLIC_
enum ndr_err_code
ndr_push_ipv4address(struct ndr_push
*ndr
, int ndr_flags
, const char *address
)
764 if (!is_ipaddress(address
)) {
765 return ndr_push_error(ndr
, NDR_ERR_IPV4ADDRESS
,
766 "Invalid IPv4 address: '%s'",
769 addr
= inet_addr(address
);
770 NDR_CHECK(ndr_push_uint32(ndr
, ndr_flags
, htonl(addr
)));
771 return NDR_ERR_SUCCESS
;
777 _PUBLIC_
void ndr_print_ipv4address(struct ndr_print
*ndr
, const char *name
,
780 ndr
->print(ndr
, "%-25s: %s", name
, address
);
784 _PUBLIC_
void ndr_print_struct(struct ndr_print
*ndr
, const char *name
, const char *type
)
786 ndr
->print(ndr
, "%s: struct %s", name
, type
);
789 _PUBLIC_
void ndr_print_enum(struct ndr_print
*ndr
, const char *name
, const char *type
,
790 const char *val
, uint32_t value
)
792 if (ndr
->flags
& LIBNDR_PRINT_ARRAY_HEX
) {
793 ndr
->print(ndr
, "%-25s: %s (0x%X)", name
, val
?val
:"UNKNOWN_ENUM_VALUE", value
);
795 ndr
->print(ndr
, "%-25s: %s (%d)", name
, val
?val
:"UNKNOWN_ENUM_VALUE", value
);
799 _PUBLIC_
void ndr_print_bitmap_flag(struct ndr_print
*ndr
, size_t size
, const char *flag_name
, uint32_t flag
, uint32_t value
)
801 /* this is an attempt to support multi-bit bitmap masks */
804 while (!(flag
& 1)) {
809 ndr
->print(ndr
, " %d: %-25s", value
, flag_name
);
811 ndr
->print(ndr
, "0x%02x: %-25s (%d)", value
, flag_name
, value
);
815 _PUBLIC_
void ndr_print_int8(struct ndr_print
*ndr
, const char *name
, int8_t v
)
817 ndr
->print(ndr
, "%-25s: %d", name
, v
);
820 _PUBLIC_
void ndr_print_uint8(struct ndr_print
*ndr
, const char *name
, uint8_t v
)
822 ndr
->print(ndr
, "%-25s: 0x%02x (%u)", name
, v
, v
);
825 _PUBLIC_
void ndr_print_int16(struct ndr_print
*ndr
, const char *name
, int16_t v
)
827 ndr
->print(ndr
, "%-25s: %d", name
, v
);
830 _PUBLIC_
void ndr_print_uint16(struct ndr_print
*ndr
, const char *name
, uint16_t v
)
832 ndr
->print(ndr
, "%-25s: 0x%04x (%u)", name
, v
, v
);
835 _PUBLIC_
void ndr_print_int32(struct ndr_print
*ndr
, const char *name
, int32_t v
)
837 ndr
->print(ndr
, "%-25s: %d", name
, v
);
840 _PUBLIC_
void ndr_print_uint32(struct ndr_print
*ndr
, const char *name
, uint32_t v
)
842 ndr
->print(ndr
, "%-25s: 0x%08x (%u)", name
, v
, v
);
845 _PUBLIC_
void ndr_print_udlong(struct ndr_print
*ndr
, const char *name
, uint64_t v
)
847 ndr
->print(ndr
, "%-25s: 0x%016llx (%llu)", name
, (unsigned long long)v
, (unsigned long long)v
);
850 _PUBLIC_
void ndr_print_udlongr(struct ndr_print
*ndr
, const char *name
, uint64_t v
)
852 ndr_print_udlong(ndr
, name
, v
);
855 _PUBLIC_
void ndr_print_dlong(struct ndr_print
*ndr
, const char *name
, int64_t v
)
857 ndr
->print(ndr
, "%-25s: 0x%016llx (%lld)", name
, (unsigned long long)v
, (long long)v
);
860 _PUBLIC_
void ndr_print_double(struct ndr_print
*ndr
, const char *name
, double v
)
862 ndr
->print(ndr
, "%-25s: %f", name
, v
);
865 _PUBLIC_
void ndr_print_hyper(struct ndr_print
*ndr
, const char *name
, uint64_t v
)
867 ndr_print_dlong(ndr
, name
, v
);
870 _PUBLIC_
void ndr_print_pointer(struct ndr_print
*ndr
, const char *name
, void *v
)
872 ndr
->print(ndr
, "%-25s: %p", name
, v
);
875 _PUBLIC_
void ndr_print_ptr(struct ndr_print
*ndr
, const char *name
, const void *p
)
878 ndr
->print(ndr
, "%-25s: *", name
);
880 ndr
->print(ndr
, "%-25s: NULL", name
);
884 _PUBLIC_
void ndr_print_NTTIME(struct ndr_print
*ndr
, const char *name
, NTTIME t
)
886 ndr
->print(ndr
, "%-25s: %s", name
, nt_time_string(ndr
, t
));
889 _PUBLIC_
void ndr_print_NTTIME_1sec(struct ndr_print
*ndr
, const char *name
, NTTIME t
)
891 /* this is a standard NTTIME here
892 * as it's already converted in the pull/push code
894 ndr_print_NTTIME(ndr
, name
, t
);
897 _PUBLIC_
void ndr_print_NTTIME_hyper(struct ndr_print
*ndr
, const char *name
, NTTIME t
)
899 ndr_print_NTTIME(ndr
, name
, t
);
902 _PUBLIC_
void ndr_print_time_t(struct ndr_print
*ndr
, const char *name
, time_t t
)
904 if (t
== (time_t)-1 || t
== 0) {
905 ndr
->print(ndr
, "%-25s: (time_t)%d", name
, (int)t
);
907 ndr
->print(ndr
, "%-25s: %s", name
, timestring(ndr
, t
));
911 _PUBLIC_
void ndr_print_union(struct ndr_print
*ndr
, const char *name
, int level
, const char *type
)
913 if (ndr
->flags
& LIBNDR_PRINT_ARRAY_HEX
) {
914 ndr
->print(ndr
, "%-25s: union %s(case 0x%X)", name
, type
, level
);
916 ndr
->print(ndr
, "%-25s: union %s(case %d)", name
, type
, level
);
920 _PUBLIC_
void ndr_print_bad_level(struct ndr_print
*ndr
, const char *name
, uint16_t level
)
922 ndr
->print(ndr
, "UNKNOWN LEVEL %u", level
);
925 _PUBLIC_
void ndr_print_array_uint8(struct ndr_print
*ndr
, const char *name
,
926 const uint8_t *data
, uint32_t count
)
930 if (count
<= 600 && (ndr
->flags
& LIBNDR_PRINT_ARRAY_HEX
)) {
932 for (i
=0;i
<count
;i
++) {
933 snprintf(&s
[i
*2], 3, "%02x", data
[i
]);
936 ndr
->print(ndr
, "%-25s: %s", name
, s
);
940 ndr
->print(ndr
, "%s: ARRAY(%d)", name
, count
);
942 for (i
=0;i
<count
;i
++) {
944 if (asprintf(&idx
, "[%d]", i
) != -1) {
945 ndr_print_uint8(ndr
, idx
, data
[i
]);
952 _PUBLIC_
void ndr_print_DATA_BLOB(struct ndr_print
*ndr
, const char *name
, DATA_BLOB r
)
954 ndr
->print(ndr
, "%-25s: DATA_BLOB length=%u", name
, (unsigned)r
.length
);
956 dump_data(10, r
.data
, r
.length
);
962 push a DATA_BLOB onto the wire.
964 _PUBLIC_
enum ndr_err_code
ndr_push_DATA_BLOB(struct ndr_push
*ndr
, int ndr_flags
, DATA_BLOB blob
)
966 if (ndr
->flags
& LIBNDR_ALIGN_FLAGS
) {
967 if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
968 blob
.length
= NDR_ALIGN(ndr
, 2);
969 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
970 blob
.length
= NDR_ALIGN(ndr
, 4);
971 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
972 blob
.length
= NDR_ALIGN(ndr
, 8);
974 NDR_PUSH_ALLOC_SIZE(ndr
, blob
.data
, blob
.length
);
975 data_blob_clear(&blob
);
976 } else if (!(ndr
->flags
& LIBNDR_FLAG_REMAINING
)) {
977 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, blob
.length
));
979 NDR_CHECK(ndr_push_bytes(ndr
, blob
.data
, blob
.length
));
980 return NDR_ERR_SUCCESS
;
984 pull a DATA_BLOB from the wire.
986 _PUBLIC_
enum ndr_err_code
ndr_pull_DATA_BLOB(struct ndr_pull
*ndr
, int ndr_flags
, DATA_BLOB
*blob
)
990 if (ndr
->flags
& LIBNDR_ALIGN_FLAGS
) {
991 if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
992 length
= NDR_ALIGN(ndr
, 2);
993 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
994 length
= NDR_ALIGN(ndr
, 4);
995 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
996 length
= NDR_ALIGN(ndr
, 8);
998 if (ndr
->data_size
- ndr
->offset
< length
) {
999 length
= ndr
->data_size
- ndr
->offset
;
1001 } else if (ndr
->flags
& LIBNDR_FLAG_REMAINING
) {
1002 length
= ndr
->data_size
- ndr
->offset
;
1004 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &length
));
1006 NDR_PULL_NEED_BYTES(ndr
, length
);
1007 *blob
= data_blob_talloc(ndr
->current_mem_ctx
, ndr
->data
+ndr
->offset
, length
);
1008 ndr
->offset
+= length
;
1009 return NDR_ERR_SUCCESS
;
1012 _PUBLIC_
uint32_t ndr_size_DATA_BLOB(int ret
, const DATA_BLOB
*data
, int flags
)
1014 if (!data
) return ret
;
1015 return ret
+ data
->length
;