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 pointer referent identifier
131 _PUBLIC_
enum ndr_err_code
ndr_pull_generic_ptr(struct ndr_pull
*ndr
, uint32_t *v
)
133 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, v
));
137 return NDR_ERR_SUCCESS
;
141 parse a ref pointer referent identifier
143 _PUBLIC_
enum ndr_err_code
ndr_pull_ref_ptr(struct ndr_pull
*ndr
, uint32_t *v
)
145 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, v
));
146 /* ref pointers always point to data */
148 return NDR_ERR_SUCCESS
;
154 _PUBLIC_
enum ndr_err_code
ndr_pull_udlong(struct ndr_pull
*ndr
, int ndr_flags
, uint64_t *v
)
156 NDR_PULL_ALIGN(ndr
, 4);
157 NDR_PULL_NEED_BYTES(ndr
, 8);
158 *v
= NDR_IVAL(ndr
, ndr
->offset
);
159 *v
|= (uint64_t)(NDR_IVAL(ndr
, ndr
->offset
+4)) << 32;
161 return NDR_ERR_SUCCESS
;
167 _PUBLIC_
enum ndr_err_code
ndr_pull_udlongr(struct ndr_pull
*ndr
, int ndr_flags
, uint64_t *v
)
169 NDR_PULL_ALIGN(ndr
, 4);
170 NDR_PULL_NEED_BYTES(ndr
, 8);
171 *v
= ((uint64_t)NDR_IVAL(ndr
, ndr
->offset
)) << 32;
172 *v
|= NDR_IVAL(ndr
, ndr
->offset
+4);
174 return NDR_ERR_SUCCESS
;
180 _PUBLIC_
enum ndr_err_code
ndr_pull_dlong(struct ndr_pull
*ndr
, int ndr_flags
, int64_t *v
)
182 return ndr_pull_udlong(ndr
, ndr_flags
, (uint64_t *)v
);
188 _PUBLIC_
enum ndr_err_code
ndr_pull_hyper(struct ndr_pull
*ndr
, int ndr_flags
, uint64_t *v
)
190 NDR_PULL_ALIGN(ndr
, 8);
191 return ndr_pull_udlong(ndr
, ndr_flags
, v
);
197 _PUBLIC_
enum ndr_err_code
ndr_pull_pointer(struct ndr_pull
*ndr
, int ndr_flags
, void* *v
)
200 NDR_PULL_ALIGN(ndr
, sizeof(h
));
201 NDR_PULL_NEED_BYTES(ndr
, sizeof(h
));
202 memcpy(&h
, ndr
->data
+ndr
->offset
, sizeof(h
));
203 ndr
->offset
+= sizeof(h
);
205 return NDR_ERR_SUCCESS
;
211 _PUBLIC_
enum ndr_err_code
ndr_pull_NTSTATUS(struct ndr_pull
*ndr
, int ndr_flags
, NTSTATUS
*status
)
214 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &v
));
215 *status
= NT_STATUS(v
);
216 return NDR_ERR_SUCCESS
;
222 _PUBLIC_
enum ndr_err_code
ndr_push_NTSTATUS(struct ndr_push
*ndr
, int ndr_flags
, NTSTATUS status
)
224 return ndr_push_uint32(ndr
, ndr_flags
, NT_STATUS_V(status
));
227 _PUBLIC_
void ndr_print_NTSTATUS(struct ndr_print
*ndr
, const char *name
, NTSTATUS r
)
229 ndr
->print(ndr
, "%-25s: %s", name
, nt_errstr(r
));
235 _PUBLIC_
enum ndr_err_code
ndr_pull_WERROR(struct ndr_pull
*ndr
, int ndr_flags
, WERROR
*status
)
238 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &v
));
239 *status
= W_ERROR(v
);
240 return NDR_ERR_SUCCESS
;
246 _PUBLIC_
enum ndr_err_code
ndr_push_WERROR(struct ndr_push
*ndr
, int ndr_flags
, WERROR status
)
248 return ndr_push_uint32(ndr
, NDR_SCALARS
, W_ERROR_V(status
));
251 _PUBLIC_
void ndr_print_WERROR(struct ndr_print
*ndr
, const char *name
, WERROR r
)
253 ndr
->print(ndr
, "%-25s: %s", name
, win_errstr(r
));
259 _PUBLIC_
enum ndr_err_code
ndr_pull_bytes(struct ndr_pull
*ndr
, uint8_t *data
, uint32_t n
)
261 NDR_PULL_NEED_BYTES(ndr
, n
);
262 memcpy(data
, ndr
->data
+ ndr
->offset
, n
);
264 return NDR_ERR_SUCCESS
;
268 pull an array of uint8
270 _PUBLIC_
enum ndr_err_code
ndr_pull_array_uint8(struct ndr_pull
*ndr
, int ndr_flags
, uint8_t *data
, uint32_t n
)
272 if (!(ndr_flags
& NDR_SCALARS
)) {
273 return NDR_ERR_SUCCESS
;
275 return ndr_pull_bytes(ndr
, data
, n
);
281 _PUBLIC_
enum ndr_err_code
ndr_push_int8(struct ndr_push
*ndr
, int ndr_flags
, int8_t v
)
283 NDR_PUSH_NEED_BYTES(ndr
, 1);
284 SCVAL(ndr
->data
, ndr
->offset
, (uint8_t)v
);
286 return NDR_ERR_SUCCESS
;
292 _PUBLIC_
enum ndr_err_code
ndr_push_uint8(struct ndr_push
*ndr
, int ndr_flags
, uint8_t v
)
294 NDR_PUSH_NEED_BYTES(ndr
, 1);
295 SCVAL(ndr
->data
, ndr
->offset
, v
);
297 return NDR_ERR_SUCCESS
;
303 _PUBLIC_
enum ndr_err_code
ndr_push_int16(struct ndr_push
*ndr
, int ndr_flags
, int16_t v
)
305 NDR_PUSH_ALIGN(ndr
, 2);
306 NDR_PUSH_NEED_BYTES(ndr
, 2);
307 NDR_SSVAL(ndr
, ndr
->offset
, (uint16_t)v
);
309 return NDR_ERR_SUCCESS
;
315 _PUBLIC_
enum ndr_err_code
ndr_push_uint16(struct ndr_push
*ndr
, int ndr_flags
, uint16_t v
)
317 NDR_PUSH_ALIGN(ndr
, 2);
318 NDR_PUSH_NEED_BYTES(ndr
, 2);
319 NDR_SSVAL(ndr
, ndr
->offset
, v
);
321 return NDR_ERR_SUCCESS
;
327 _PUBLIC_
enum ndr_err_code
ndr_push_int32(struct ndr_push
*ndr
, int ndr_flags
, int32_t v
)
329 NDR_PUSH_ALIGN(ndr
, 4);
330 NDR_PUSH_NEED_BYTES(ndr
, 4);
331 NDR_SIVALS(ndr
, ndr
->offset
, v
);
333 return NDR_ERR_SUCCESS
;
339 _PUBLIC_
enum ndr_err_code
ndr_push_uint32(struct ndr_push
*ndr
, int ndr_flags
, uint32_t v
)
341 NDR_PUSH_ALIGN(ndr
, 4);
342 NDR_PUSH_NEED_BYTES(ndr
, 4);
343 NDR_SIVAL(ndr
, ndr
->offset
, v
);
345 return NDR_ERR_SUCCESS
;
351 _PUBLIC_
enum ndr_err_code
ndr_push_udlong(struct ndr_push
*ndr
, int ndr_flags
, uint64_t v
)
353 NDR_PUSH_ALIGN(ndr
, 4);
354 NDR_PUSH_NEED_BYTES(ndr
, 8);
355 NDR_SIVAL(ndr
, ndr
->offset
, (v
& 0xFFFFFFFF));
356 NDR_SIVAL(ndr
, ndr
->offset
+4, (v
>>32));
358 return NDR_ERR_SUCCESS
;
364 _PUBLIC_
enum ndr_err_code
ndr_push_udlongr(struct ndr_push
*ndr
, int ndr_flags
, uint64_t v
)
366 NDR_PUSH_ALIGN(ndr
, 4);
367 NDR_PUSH_NEED_BYTES(ndr
, 8);
368 NDR_SIVAL(ndr
, ndr
->offset
, (v
>>32));
369 NDR_SIVAL(ndr
, ndr
->offset
+4, (v
& 0xFFFFFFFF));
371 return NDR_ERR_SUCCESS
;
377 _PUBLIC_
enum ndr_err_code
ndr_push_dlong(struct ndr_push
*ndr
, int ndr_flags
, int64_t v
)
379 return ndr_push_udlong(ndr
, NDR_SCALARS
, (uint64_t)v
);
385 _PUBLIC_
enum ndr_err_code
ndr_push_hyper(struct ndr_push
*ndr
, int ndr_flags
, uint64_t v
)
387 NDR_PUSH_ALIGN(ndr
, 8);
388 return ndr_push_udlong(ndr
, NDR_SCALARS
, v
);
394 _PUBLIC_
enum ndr_err_code
ndr_push_pointer(struct ndr_push
*ndr
, int ndr_flags
, void* v
)
396 uintptr_t h
= (intptr_t)v
;
397 NDR_PUSH_ALIGN(ndr
, sizeof(h
));
398 NDR_PUSH_NEED_BYTES(ndr
, sizeof(h
));
399 memcpy(ndr
->data
+ndr
->offset
, &h
, sizeof(h
));
400 ndr
->offset
+= sizeof(h
);
401 return NDR_ERR_SUCCESS
;
404 _PUBLIC_
enum ndr_err_code
ndr_push_align(struct ndr_push
*ndr
, size_t size
)
406 NDR_PUSH_ALIGN(ndr
, size
);
407 return NDR_ERR_SUCCESS
;
410 _PUBLIC_
enum ndr_err_code
ndr_pull_align(struct ndr_pull
*ndr
, size_t size
)
412 NDR_PULL_ALIGN(ndr
, size
);
413 return NDR_ERR_SUCCESS
;
419 _PUBLIC_
enum ndr_err_code
ndr_push_bytes(struct ndr_push
*ndr
, const uint8_t *data
, uint32_t n
)
421 NDR_PUSH_NEED_BYTES(ndr
, n
);
422 memcpy(ndr
->data
+ ndr
->offset
, data
, n
);
424 return NDR_ERR_SUCCESS
;
430 _PUBLIC_
enum ndr_err_code
ndr_push_zero(struct ndr_push
*ndr
, uint32_t n
)
432 NDR_PUSH_NEED_BYTES(ndr
, n
);
433 memset(ndr
->data
+ ndr
->offset
, 0, n
);
435 return NDR_ERR_SUCCESS
;
439 push an array of uint8
441 _PUBLIC_
enum ndr_err_code
ndr_push_array_uint8(struct ndr_push
*ndr
, int ndr_flags
, const uint8_t *data
, uint32_t n
)
443 if (!(ndr_flags
& NDR_SCALARS
)) {
444 return NDR_ERR_SUCCESS
;
446 return ndr_push_bytes(ndr
, data
, n
);
450 save the current position
452 _PUBLIC_
void ndr_push_save(struct ndr_push
*ndr
, struct ndr_push_save
*save
)
454 save
->offset
= ndr
->offset
;
460 _PUBLIC_
void ndr_push_restore(struct ndr_push
*ndr
, struct ndr_push_save
*save
)
462 ndr
->offset
= save
->offset
;
466 push a unique non-zero value if a pointer is non-NULL, otherwise 0
468 _PUBLIC_
enum ndr_err_code
ndr_push_unique_ptr(struct ndr_push
*ndr
, const void *p
)
472 ptr
= ndr
->ptr_count
* 4;
476 return ndr_push_uint32(ndr
, NDR_SCALARS
, ptr
);
480 push a 'simple' full non-zero value if a pointer is non-NULL, otherwise 0
482 _PUBLIC_
enum ndr_err_code
ndr_push_full_ptr(struct ndr_push
*ndr
, const void *p
)
486 /* Check if the pointer already exists and has an id */
487 ptr
= ndr_token_peek(&ndr
->full_ptr_list
, p
);
490 ptr
= ndr
->ptr_count
;
491 ndr_token_store(ndr
, &ndr
->full_ptr_list
, p
, ptr
);
494 return ndr_push_uint32(ndr
, NDR_SCALARS
, ptr
);
498 push always a 0, if a pointer is NULL it's a fatal error
500 _PUBLIC_
enum ndr_err_code
ndr_push_ref_ptr(struct ndr_push
*ndr
)
502 return ndr_push_uint32(ndr
, NDR_SCALARS
, 0xAEF1AEF1);
509 _PUBLIC_
enum ndr_err_code
ndr_push_NTTIME(struct ndr_push
*ndr
, int ndr_flags
, NTTIME t
)
511 NDR_CHECK(ndr_push_udlong(ndr
, ndr_flags
, t
));
512 return NDR_ERR_SUCCESS
;
518 _PUBLIC_
enum ndr_err_code
ndr_pull_NTTIME(struct ndr_pull
*ndr
, int ndr_flags
, NTTIME
*t
)
520 NDR_CHECK(ndr_pull_udlong(ndr
, ndr_flags
, t
));
521 return NDR_ERR_SUCCESS
;
527 _PUBLIC_
enum ndr_err_code
ndr_push_NTTIME_1sec(struct ndr_push
*ndr
, int ndr_flags
, NTTIME t
)
530 NDR_CHECK(ndr_push_hyper(ndr
, ndr_flags
, t
));
531 return NDR_ERR_SUCCESS
;
537 _PUBLIC_
enum ndr_err_code
ndr_pull_NTTIME_1sec(struct ndr_pull
*ndr
, int ndr_flags
, NTTIME
*t
)
539 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, t
));
541 return NDR_ERR_SUCCESS
;
547 _PUBLIC_
enum ndr_err_code
ndr_pull_NTTIME_hyper(struct ndr_pull
*ndr
, int ndr_flags
, NTTIME
*t
)
549 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, t
));
550 return NDR_ERR_SUCCESS
;
556 _PUBLIC_
enum ndr_err_code
ndr_push_NTTIME_hyper(struct ndr_push
*ndr
, int ndr_flags
, NTTIME t
)
558 NDR_CHECK(ndr_push_hyper(ndr
, ndr_flags
, t
));
559 return NDR_ERR_SUCCESS
;
565 _PUBLIC_
enum ndr_err_code
ndr_push_time_t(struct ndr_push
*ndr
, int ndr_flags
, time_t t
)
567 return ndr_push_uint32(ndr
, ndr_flags
, t
);
573 _PUBLIC_
enum ndr_err_code
ndr_pull_time_t(struct ndr_pull
*ndr
, int ndr_flags
, time_t *t
)
576 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &tt
));
578 return NDR_ERR_SUCCESS
;
585 _PUBLIC_
enum ndr_err_code
ndr_pull_ipv4address(struct ndr_pull
*ndr
, int ndr_flags
, const char **address
)
588 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &in
.s_addr
));
589 in
.s_addr
= htonl(in
.s_addr
);
590 *address
= talloc_strdup(ndr
->current_mem_ctx
, inet_ntoa(in
));
591 NDR_ERR_HAVE_NO_MEMORY(*address
);
592 return NDR_ERR_SUCCESS
;
598 _PUBLIC_
enum ndr_err_code
ndr_push_ipv4address(struct ndr_push
*ndr
, int ndr_flags
, const char *address
)
601 if (!is_ipaddress(address
)) {
602 return ndr_push_error(ndr
, NDR_ERR_IPV4ADDRESS
,
603 "Invalid IPv4 address: '%s'",
606 addr
= inet_addr(address
);
607 NDR_CHECK(ndr_push_uint32(ndr
, ndr_flags
, htonl(addr
)));
608 return NDR_ERR_SUCCESS
;
614 _PUBLIC_
void ndr_print_ipv4address(struct ndr_print
*ndr
, const char *name
,
617 ndr
->print(ndr
, "%-25s: %s", name
, address
);
621 _PUBLIC_
void ndr_print_struct(struct ndr_print
*ndr
, const char *name
, const char *type
)
623 ndr
->print(ndr
, "%s: struct %s", name
, type
);
626 _PUBLIC_
void ndr_print_enum(struct ndr_print
*ndr
, const char *name
, const char *type
,
627 const char *val
, uint32_t value
)
629 if (ndr
->flags
& LIBNDR_PRINT_ARRAY_HEX
) {
630 ndr
->print(ndr
, "%-25s: %s (0x%X)", name
, val
?val
:"UNKNOWN_ENUM_VALUE", value
);
632 ndr
->print(ndr
, "%-25s: %s (%d)", name
, val
?val
:"UNKNOWN_ENUM_VALUE", value
);
636 _PUBLIC_
void ndr_print_bitmap_flag(struct ndr_print
*ndr
, size_t size
, const char *flag_name
, uint32_t flag
, uint32_t value
)
638 /* this is an attempt to support multi-bit bitmap masks */
641 while (!(flag
& 1)) {
646 ndr
->print(ndr
, " %d: %-25s", value
, flag_name
);
648 ndr
->print(ndr
, "0x%02x: %-25s (%d)", value
, flag_name
, value
);
652 _PUBLIC_
void ndr_print_int8(struct ndr_print
*ndr
, const char *name
, int8_t v
)
654 ndr
->print(ndr
, "%-25s: %d", name
, v
);
657 _PUBLIC_
void ndr_print_uint8(struct ndr_print
*ndr
, const char *name
, uint8_t v
)
659 ndr
->print(ndr
, "%-25s: 0x%02x (%u)", name
, v
, v
);
662 _PUBLIC_
void ndr_print_int16(struct ndr_print
*ndr
, const char *name
, int16_t v
)
664 ndr
->print(ndr
, "%-25s: %d", name
, v
);
667 _PUBLIC_
void ndr_print_uint16(struct ndr_print
*ndr
, const char *name
, uint16_t v
)
669 ndr
->print(ndr
, "%-25s: 0x%04x (%u)", name
, v
, v
);
672 _PUBLIC_
void ndr_print_int32(struct ndr_print
*ndr
, const char *name
, int32_t v
)
674 ndr
->print(ndr
, "%-25s: %d", name
, v
);
677 _PUBLIC_
void ndr_print_uint32(struct ndr_print
*ndr
, const char *name
, uint32_t v
)
679 ndr
->print(ndr
, "%-25s: 0x%08x (%u)", name
, v
, v
);
682 _PUBLIC_
void ndr_print_udlong(struct ndr_print
*ndr
, const char *name
, uint64_t v
)
684 ndr
->print(ndr
, "%-25s: 0x%016llx (%llu)", name
, (unsigned long long)v
, (unsigned long long)v
);
687 _PUBLIC_
void ndr_print_udlongr(struct ndr_print
*ndr
, const char *name
, uint64_t v
)
689 ndr_print_udlong(ndr
, name
, v
);
692 _PUBLIC_
void ndr_print_dlong(struct ndr_print
*ndr
, const char *name
, int64_t v
)
694 ndr
->print(ndr
, "%-25s: 0x%016llx (%lld)", name
, (unsigned long long)v
, (long long)v
);
697 _PUBLIC_
void ndr_print_hyper(struct ndr_print
*ndr
, const char *name
, uint64_t v
)
699 ndr_print_dlong(ndr
, name
, v
);
702 _PUBLIC_
void ndr_print_pointer(struct ndr_print
*ndr
, const char *name
, void *v
)
704 ndr
->print(ndr
, "%-25s: %p", name
, v
);
707 _PUBLIC_
void ndr_print_ptr(struct ndr_print
*ndr
, const char *name
, const void *p
)
710 ndr
->print(ndr
, "%-25s: *", name
);
712 ndr
->print(ndr
, "%-25s: NULL", name
);
716 _PUBLIC_
void ndr_print_NTTIME(struct ndr_print
*ndr
, const char *name
, NTTIME t
)
718 ndr
->print(ndr
, "%-25s: %s", name
, nt_time_string(ndr
, t
));
721 _PUBLIC_
void ndr_print_NTTIME_1sec(struct ndr_print
*ndr
, const char *name
, NTTIME t
)
723 /* this is a standard NTTIME here
724 * as it's already converted in the pull/push code
726 ndr_print_NTTIME(ndr
, name
, t
);
729 _PUBLIC_
void ndr_print_NTTIME_hyper(struct ndr_print
*ndr
, const char *name
, NTTIME t
)
731 ndr_print_NTTIME(ndr
, name
, t
);
734 _PUBLIC_
void ndr_print_time_t(struct ndr_print
*ndr
, const char *name
, time_t t
)
736 if (t
== (time_t)-1 || t
== 0) {
737 ndr
->print(ndr
, "%-25s: (time_t)%d", name
, (int)t
);
739 ndr
->print(ndr
, "%-25s: %s", name
, timestring(ndr
, t
));
743 _PUBLIC_
void ndr_print_union(struct ndr_print
*ndr
, const char *name
, int level
, const char *type
)
745 if (ndr
->flags
& LIBNDR_PRINT_ARRAY_HEX
) {
746 ndr
->print(ndr
, "%-25s: union %s(case 0x%X)", name
, type
, level
);
748 ndr
->print(ndr
, "%-25s: union %s(case %d)", name
, type
, level
);
752 _PUBLIC_
void ndr_print_bad_level(struct ndr_print
*ndr
, const char *name
, uint16_t level
)
754 ndr
->print(ndr
, "UNKNOWN LEVEL %u", level
);
757 _PUBLIC_
void ndr_print_array_uint8(struct ndr_print
*ndr
, const char *name
,
758 const uint8_t *data
, uint32_t count
)
762 if (count
<= 600 && (ndr
->flags
& LIBNDR_PRINT_ARRAY_HEX
)) {
764 for (i
=0;i
<count
;i
++) {
765 snprintf(&s
[i
*2], 3, "%02x", data
[i
]);
768 ndr
->print(ndr
, "%-25s: %s", name
, s
);
772 ndr
->print(ndr
, "%s: ARRAY(%d)", name
, count
);
774 for (i
=0;i
<count
;i
++) {
776 if (asprintf(&idx
, "[%d]", i
) != -1) {
777 ndr_print_uint8(ndr
, idx
, data
[i
]);
784 _PUBLIC_
void ndr_print_DATA_BLOB(struct ndr_print
*ndr
, const char *name
, DATA_BLOB r
)
786 ndr
->print(ndr
, "%-25s: DATA_BLOB length=%u", name
, (unsigned)r
.length
);
788 dump_data(10, r
.data
, r
.length
);
794 push a DATA_BLOB onto the wire.
796 _PUBLIC_
enum ndr_err_code
ndr_push_DATA_BLOB(struct ndr_push
*ndr
, int ndr_flags
, DATA_BLOB blob
)
798 if (ndr
->flags
& LIBNDR_ALIGN_FLAGS
) {
799 if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
800 blob
.length
= NDR_ALIGN(ndr
, 2);
801 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
802 blob
.length
= NDR_ALIGN(ndr
, 4);
803 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
804 blob
.length
= NDR_ALIGN(ndr
, 8);
806 NDR_PUSH_ALLOC_SIZE(ndr
, blob
.data
, blob
.length
);
807 data_blob_clear(&blob
);
808 } else if (!(ndr
->flags
& LIBNDR_FLAG_REMAINING
)) {
809 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, blob
.length
));
811 NDR_CHECK(ndr_push_bytes(ndr
, blob
.data
, blob
.length
));
812 return NDR_ERR_SUCCESS
;
816 pull a DATA_BLOB from the wire.
818 _PUBLIC_
enum ndr_err_code
ndr_pull_DATA_BLOB(struct ndr_pull
*ndr
, int ndr_flags
, DATA_BLOB
*blob
)
822 if (ndr
->flags
& LIBNDR_ALIGN_FLAGS
) {
823 if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
824 length
= NDR_ALIGN(ndr
, 2);
825 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
826 length
= NDR_ALIGN(ndr
, 4);
827 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
828 length
= NDR_ALIGN(ndr
, 8);
830 if (ndr
->data_size
- ndr
->offset
< length
) {
831 length
= ndr
->data_size
- ndr
->offset
;
833 } else if (ndr
->flags
& LIBNDR_FLAG_REMAINING
) {
834 length
= ndr
->data_size
- ndr
->offset
;
836 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &length
));
838 NDR_PULL_NEED_BYTES(ndr
, length
);
839 *blob
= data_blob_talloc(ndr
->current_mem_ctx
, ndr
->data
+ndr
->offset
, length
);
840 ndr
->offset
+= length
;
841 return NDR_ERR_SUCCESS
;
844 _PUBLIC_
uint32_t ndr_size_DATA_BLOB(int ret
, const DATA_BLOB
*data
, int flags
)
846 if (!data
) return ret
;
847 return ret
+ data
->length
;
850 _PUBLIC_
void ndr_print_bool(struct ndr_print
*ndr
, const char *name
, const bool b
)
852 ndr
->print(ndr
, "%-25s: %s", name
, b
?"true":"false");
855 _PUBLIC_
void ndr_print_sockaddr_storage(struct ndr_print
*ndr
, const char *name
, const struct sockaddr_storage
*ss
)
857 char addr
[INET6_ADDRSTRLEN
];
858 ndr
->print(ndr
, "%-25s: %s", name
, print_sockaddr(addr
, sizeof(addr
), ss
));