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"
25 #include "lib/util/util_net.h"
26 #include "lib/util/debug.h"
27 #include "lib/util/util.h"
28 #include "lib/util/bytearray.h"
30 #define NDR_PULL_U16(ndr, ofs) \
31 (NDR_BE(ndr) ? PULL_BE_U16(ndr->data,ofs) : PULL_LE_U16(ndr->data,ofs))
33 #define NDR_PULL_U32(ndr, ofs) \
34 (NDR_BE(ndr) ? PULL_BE_U32(ndr->data,ofs) : PULL_LE_U32(ndr->data,ofs))
36 #define NDR_PULL_I32(ndr, ofs) \
37 (int32_t)(NDR_BE(ndr) ? PULL_BE_U32(ndr->data,ofs) : PULL_LE_U32(ndr->data,ofs))
39 #define NDR_PUSH_U16(ndr, ofs, v) \
42 PUSH_BE_U16(ndr->data, ofs, v); \
44 PUSH_LE_U16(ndr->data, ofs, v); \
48 #define NDR_PUSH_U32(ndr, ofs, v) \
51 PUSH_BE_U32(ndr->data, ofs, v); \
53 PUSH_LE_U32(ndr->data, ofs, v); \
57 #define NDR_PUSH_I32(ndr, ofs, v) \
60 PUSH_BE_U32(ndr->data, ofs, v); \
62 PUSH_LE_U32(ndr->data, ofs, v); \
66 static void ndr_dump_data(struct ndr_print
*ndr
, const uint8_t *buf
, int len
);
69 check for data leaks from the server by looking for non-zero pad bytes
70 these could also indicate that real structure elements have been
71 mistaken for padding in the IDL
73 _PUBLIC_
void ndr_check_padding(struct ndr_pull
*ndr
, size_t n
)
75 size_t ofs2
= (ndr
->offset
+ (n
-1)) & ~(n
-1);
77 for (i
=ndr
->offset
;i
<ofs2
;i
++) {
78 if (ndr
->data
[i
] != 0) {
83 DEBUG(0,("WARNING: Non-zero padding to %d: ", (int)n
));
84 for (i
=ndr
->offset
;i
<ofs2
;i
++) {
85 DEBUG(0,("%02x ", ndr
->data
[i
]));
95 _PUBLIC_
enum ndr_err_code
ndr_pull_int8(struct ndr_pull
*ndr
, int ndr_flags
, int8_t *v
)
97 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
98 NDR_PULL_NEED_BYTES(ndr
, 1);
99 *v
= (int8_t)PULL_BE_U8(ndr
->data
, ndr
->offset
);
101 return NDR_ERR_SUCCESS
;
107 _PUBLIC_
enum ndr_err_code
ndr_pull_uint8(struct ndr_pull
*ndr
, int ndr_flags
, uint8_t *v
)
109 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
110 NDR_PULL_NEED_BYTES(ndr
, 1);
111 *v
= PULL_BE_U8(ndr
->data
, ndr
->offset
);
113 return NDR_ERR_SUCCESS
;
119 _PUBLIC_
enum ndr_err_code
ndr_pull_int16(struct ndr_pull
*ndr
, int ndr_flags
, int16_t *v
)
121 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
122 NDR_PULL_ALIGN(ndr
, 2);
123 NDR_PULL_NEED_BYTES(ndr
, 2);
124 *v
= (uint16_t)NDR_PULL_U16(ndr
, ndr
->offset
);
126 return NDR_ERR_SUCCESS
;
132 _PUBLIC_
enum ndr_err_code
ndr_pull_uint16(struct ndr_pull
*ndr
, int ndr_flags
, uint16_t *v
)
134 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
135 NDR_PULL_ALIGN(ndr
, 2);
136 NDR_PULL_NEED_BYTES(ndr
, 2);
137 *v
= NDR_PULL_U16(ndr
, ndr
->offset
);
139 return NDR_ERR_SUCCESS
;
145 _PUBLIC_
enum ndr_err_code
ndr_pull_uint1632(struct ndr_pull
*ndr
, int ndr_flags
, uint16_t *v
)
147 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
148 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
150 enum ndr_err_code err
= ndr_pull_uint32(ndr
, ndr_flags
, &v32
);
152 if (unlikely(v32
!= *v
)) {
153 DEBUG(0,(__location__
": non-zero upper 16 bits 0x%08x\n", (unsigned)v32
));
154 return NDR_ERR_NDR64
;
158 return ndr_pull_uint16(ndr
, ndr_flags
, v
);
164 _PUBLIC_
enum ndr_err_code
ndr_pull_int32(struct ndr_pull
*ndr
, int ndr_flags
, int32_t *v
)
166 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
167 NDR_PULL_ALIGN(ndr
, 4);
168 NDR_PULL_NEED_BYTES(ndr
, 4);
169 *v
= NDR_PULL_I32(ndr
, ndr
->offset
);
171 return NDR_ERR_SUCCESS
;
177 _PUBLIC_
enum ndr_err_code
ndr_pull_uint32(struct ndr_pull
*ndr
, int ndr_flags
, uint32_t *v
)
179 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
180 NDR_PULL_ALIGN(ndr
, 4);
181 NDR_PULL_NEED_BYTES(ndr
, 4);
182 *v
= NDR_PULL_U32(ndr
, ndr
->offset
);
184 return NDR_ERR_SUCCESS
;
188 parse a arch dependent uint32/uint64
190 _PUBLIC_
enum ndr_err_code
ndr_pull_uint3264(struct ndr_pull
*ndr
, int ndr_flags
, uint32_t *v
)
193 enum ndr_err_code err
;
194 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
195 if (likely(!(ndr
->flags
& LIBNDR_FLAG_NDR64
))) {
196 return ndr_pull_uint32(ndr
, ndr_flags
, v
);
198 err
= ndr_pull_hyper(ndr
, ndr_flags
, &v64
);
199 if (!NDR_ERR_CODE_IS_SUCCESS(err
)) {
203 if (unlikely(v64
!= *v
)) {
204 DEBUG(0,(__location__
": non-zero upper 32 bits 0x%016llx\n",
205 (unsigned long long)v64
));
206 return ndr_pull_error(ndr
, NDR_ERR_NDR64
, __location__
": non-zero upper 32 bits 0x%016llx\n",
207 (unsigned long long)v64
);
215 _PUBLIC_
enum ndr_err_code
ndr_pull_double(struct ndr_pull
*ndr
, int ndr_flags
, double *v
)
217 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
218 NDR_PULL_ALIGN(ndr
, 8);
219 NDR_PULL_NEED_BYTES(ndr
, 8);
220 memcpy(v
, ndr
->data
+ndr
->offset
, 8);
222 return NDR_ERR_SUCCESS
;
226 parse a pointer referent identifier stored in 2 bytes
228 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr_short(struct ndr_pull
*ndr
, uint16_t *v
)
230 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, v
));
234 *(v
) -= ndr
->relative_rap_convert
;
235 return NDR_ERR_SUCCESS
;
239 parse a pointer referent identifier
241 _PUBLIC_
enum ndr_err_code
ndr_pull_generic_ptr(struct ndr_pull
*ndr
, uint32_t *v
)
243 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, v
));
247 return NDR_ERR_SUCCESS
;
251 parse a ref pointer referent identifier
253 _PUBLIC_
enum ndr_err_code
ndr_pull_ref_ptr(struct ndr_pull
*ndr
, uint32_t *v
)
255 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, v
));
256 /* ref pointers always point to data */
258 return NDR_ERR_SUCCESS
;
264 _PUBLIC_
enum ndr_err_code
ndr_pull_udlong(struct ndr_pull
*ndr
, int ndr_flags
, uint64_t *v
)
266 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
267 NDR_PULL_ALIGN(ndr
, 4);
268 NDR_PULL_NEED_BYTES(ndr
, 8);
269 *v
= NDR_PULL_U32(ndr
, ndr
->offset
);
270 *v
|= (uint64_t)(NDR_PULL_U32(ndr
, ndr
->offset
+4)) << 32;
272 return NDR_ERR_SUCCESS
;
278 _PUBLIC_
enum ndr_err_code
ndr_pull_udlongr(struct ndr_pull
*ndr
, int ndr_flags
, uint64_t *v
)
280 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
281 NDR_PULL_ALIGN(ndr
, 4);
282 NDR_PULL_NEED_BYTES(ndr
, 8);
283 *v
= ((uint64_t)NDR_PULL_U32(ndr
, ndr
->offset
)) << 32;
284 *v
|= NDR_PULL_U32(ndr
, ndr
->offset
+4);
286 return NDR_ERR_SUCCESS
;
292 _PUBLIC_
enum ndr_err_code
ndr_pull_dlong(struct ndr_pull
*ndr
, int ndr_flags
, int64_t *v
)
294 return ndr_pull_udlong(ndr
, ndr_flags
, (uint64_t *)v
);
300 _PUBLIC_
enum ndr_err_code
ndr_pull_hyper(struct ndr_pull
*ndr
, int ndr_flags
, uint64_t *v
)
302 NDR_PULL_ALIGN(ndr
, 8);
304 return ndr_pull_udlongr(ndr
, ndr_flags
, v
);
306 return ndr_pull_udlong(ndr
, ndr_flags
, v
);
312 _PUBLIC_
enum ndr_err_code
ndr_pull_pointer(struct ndr_pull
*ndr
, int ndr_flags
, void* *v
)
315 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
316 NDR_PULL_ALIGN(ndr
, sizeof(h
));
317 NDR_PULL_NEED_BYTES(ndr
, sizeof(h
));
318 memcpy(&h
, ndr
->data
+ndr
->offset
, sizeof(h
));
319 ndr
->offset
+= sizeof(h
);
321 return NDR_ERR_SUCCESS
;
327 _PUBLIC_
enum ndr_err_code
ndr_pull_NTSTATUS(struct ndr_pull
*ndr
, int ndr_flags
, NTSTATUS
*status
)
330 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
331 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &v
));
332 *status
= NT_STATUS(v
);
333 return NDR_ERR_SUCCESS
;
339 _PUBLIC_
enum ndr_err_code
ndr_push_NTSTATUS(struct ndr_push
*ndr
, int ndr_flags
, NTSTATUS status
)
341 return ndr_push_uint32(ndr
, ndr_flags
, NT_STATUS_V(status
));
344 _PUBLIC_
void ndr_print_NTSTATUS(struct ndr_print
*ndr
, const char *name
, NTSTATUS r
)
346 ndr
->print(ndr
, "%-25s: %s", name
, nt_errstr(r
));
352 _PUBLIC_
enum ndr_err_code
ndr_pull_WERROR(struct ndr_pull
*ndr
, int ndr_flags
, WERROR
*status
)
355 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
356 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &v
));
357 *status
= W_ERROR(v
);
358 return NDR_ERR_SUCCESS
;
364 _PUBLIC_
enum ndr_err_code
ndr_pull_HRESULT(struct ndr_pull
*ndr
, int ndr_flags
, HRESULT
*status
)
367 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
368 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &v
));
369 *status
= HRES_ERROR(v
);
370 return NDR_ERR_SUCCESS
;
376 _PUBLIC_
enum ndr_err_code
ndr_pull_enum_uint8(struct ndr_pull
*ndr
, int ndr_flags
, uint8_t *v
)
378 return ndr_pull_uint8(ndr
, ndr_flags
, v
);
382 parse a uint16_t enum
384 _PUBLIC_
enum ndr_err_code
ndr_pull_enum_uint16(struct ndr_pull
*ndr
, int ndr_flags
, uint16_t *v
)
386 return ndr_pull_uint16(ndr
, ndr_flags
, v
);
390 parse a uint1632_t enum (uint32_t on NDR64)
392 _PUBLIC_
enum ndr_err_code
ndr_pull_enum_uint1632(struct ndr_pull
*ndr
, int ndr_flags
, uint16_t *v
)
394 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
396 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &v32
));
399 DEBUG(0,(__location__
": non-zero upper 16 bits 0x%08x\n", (unsigned)v32
));
400 return NDR_ERR_NDR64
;
402 return NDR_ERR_SUCCESS
;
404 return ndr_pull_uint16(ndr
, ndr_flags
, v
);
408 parse a uint32_t enum
410 _PUBLIC_
enum ndr_err_code
ndr_pull_enum_uint32(struct ndr_pull
*ndr
, int ndr_flags
, uint32_t *v
)
412 return ndr_pull_uint32(ndr
, ndr_flags
, v
);
418 _PUBLIC_
enum ndr_err_code
ndr_push_enum_uint8(struct ndr_push
*ndr
, int ndr_flags
, uint8_t v
)
420 return ndr_push_uint8(ndr
, ndr_flags
, v
);
426 _PUBLIC_
enum ndr_err_code
ndr_push_enum_uint16(struct ndr_push
*ndr
, int ndr_flags
, uint16_t v
)
428 return ndr_push_uint16(ndr
, ndr_flags
, v
);
434 _PUBLIC_
enum ndr_err_code
ndr_push_enum_uint32(struct ndr_push
*ndr
, int ndr_flags
, uint32_t v
)
436 return ndr_push_uint32(ndr
, ndr_flags
, v
);
440 push a uint1632_t enum
442 _PUBLIC_
enum ndr_err_code
ndr_push_enum_uint1632(struct ndr_push
*ndr
, int ndr_flags
, uint16_t v
)
444 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
445 return ndr_push_uint32(ndr
, ndr_flags
, v
);
447 return ndr_push_uint16(ndr
, ndr_flags
, v
);
453 _PUBLIC_
enum ndr_err_code
ndr_push_WERROR(struct ndr_push
*ndr
, int ndr_flags
, WERROR status
)
455 return ndr_push_uint32(ndr
, NDR_SCALARS
, W_ERROR_V(status
));
458 _PUBLIC_
void ndr_print_WERROR(struct ndr_print
*ndr
, const char *name
, WERROR r
)
460 ndr
->print(ndr
, "%-25s: %s", name
, win_errstr(r
));
466 _PUBLIC_
enum ndr_err_code
ndr_push_HRESULT(struct ndr_push
*ndr
, int ndr_flags
, HRESULT status
)
468 return ndr_push_uint32(ndr
, NDR_SCALARS
, HRES_ERROR_V(status
));
471 _PUBLIC_
void ndr_print_HRESULT(struct ndr_print
*ndr
, const char *name
, HRESULT r
)
473 ndr
->print(ndr
, "%-25s: %s", name
, hresult_errstr(r
));
480 _PUBLIC_
enum ndr_err_code
ndr_pull_bytes(struct ndr_pull
*ndr
, uint8_t *data
, uint32_t n
)
482 NDR_PULL_NEED_BYTES(ndr
, n
);
483 memcpy(data
, ndr
->data
+ ndr
->offset
, n
);
485 return NDR_ERR_SUCCESS
;
489 pull an array of uint8
491 _PUBLIC_
enum ndr_err_code
ndr_pull_array_uint8(struct ndr_pull
*ndr
, int ndr_flags
, uint8_t *data
, uint32_t n
)
493 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
494 if (!(ndr_flags
& NDR_SCALARS
)) {
495 return NDR_ERR_SUCCESS
;
497 return ndr_pull_bytes(ndr
, data
, n
);
503 _PUBLIC_
enum ndr_err_code
ndr_push_int8(struct ndr_push
*ndr
, int ndr_flags
, int8_t v
)
505 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
506 NDR_PUSH_NEED_BYTES(ndr
, 1);
507 PUSH_BE_U8(ndr
->data
, ndr
->offset
, (uint8_t)v
);
509 return NDR_ERR_SUCCESS
;
515 _PUBLIC_
enum ndr_err_code
ndr_push_uint8(struct ndr_push
*ndr
, int ndr_flags
, uint8_t v
)
517 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
518 NDR_PUSH_NEED_BYTES(ndr
, 1);
519 PUSH_BE_U8(ndr
->data
, ndr
->offset
, v
);
521 return NDR_ERR_SUCCESS
;
527 _PUBLIC_
enum ndr_err_code
ndr_push_int16(struct ndr_push
*ndr
, int ndr_flags
, int16_t v
)
529 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
530 NDR_PUSH_ALIGN(ndr
, 2);
531 NDR_PUSH_NEED_BYTES(ndr
, 2);
532 NDR_PUSH_U16(ndr
, ndr
->offset
, (uint16_t)v
);
534 return NDR_ERR_SUCCESS
;
540 _PUBLIC_
enum ndr_err_code
ndr_push_uint16(struct ndr_push
*ndr
, int ndr_flags
, uint16_t v
)
542 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
543 NDR_PUSH_ALIGN(ndr
, 2);
544 NDR_PUSH_NEED_BYTES(ndr
, 2);
545 NDR_PUSH_U16(ndr
, ndr
->offset
, v
);
547 return NDR_ERR_SUCCESS
;
553 _PUBLIC_
enum ndr_err_code
ndr_push_uint1632(struct ndr_push
*ndr
, int ndr_flags
, uint16_t v
)
555 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
556 return ndr_push_uint32(ndr
, ndr_flags
, v
);
558 return ndr_push_uint16(ndr
, ndr_flags
, v
);
564 _PUBLIC_
enum ndr_err_code
ndr_push_int32(struct ndr_push
*ndr
, int ndr_flags
, int32_t v
)
566 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
567 NDR_PUSH_ALIGN(ndr
, 4);
568 NDR_PUSH_NEED_BYTES(ndr
, 4);
569 NDR_PUSH_I32(ndr
, ndr
->offset
, v
);
571 return NDR_ERR_SUCCESS
;
577 _PUBLIC_
enum ndr_err_code
ndr_push_uint32(struct ndr_push
*ndr
, int ndr_flags
, uint32_t v
)
579 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
580 NDR_PUSH_ALIGN(ndr
, 4);
581 NDR_PUSH_NEED_BYTES(ndr
, 4);
582 NDR_PUSH_U32(ndr
, ndr
->offset
, v
);
584 return NDR_ERR_SUCCESS
;
590 _PUBLIC_
enum ndr_err_code
ndr_push_uint3264(struct ndr_push
*ndr
, int ndr_flags
, uint32_t v
)
592 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
593 return ndr_push_hyper(ndr
, ndr_flags
, v
);
595 return ndr_push_uint32(ndr
, ndr_flags
, v
);
601 _PUBLIC_
enum ndr_err_code
ndr_push_udlong(struct ndr_push
*ndr
, int ndr_flags
, uint64_t v
)
603 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
604 NDR_PUSH_ALIGN(ndr
, 4);
605 NDR_PUSH_NEED_BYTES(ndr
, 8);
606 NDR_PUSH_U32(ndr
, ndr
->offset
, (v
& 0xFFFFFFFF));
607 NDR_PUSH_U32(ndr
, ndr
->offset
+4, (v
>>32));
609 return NDR_ERR_SUCCESS
;
615 _PUBLIC_
enum ndr_err_code
ndr_push_udlongr(struct ndr_push
*ndr
, int ndr_flags
, uint64_t v
)
617 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
618 NDR_PUSH_ALIGN(ndr
, 4);
619 NDR_PUSH_NEED_BYTES(ndr
, 8);
620 NDR_PUSH_U32(ndr
, ndr
->offset
, (v
>>32));
621 NDR_PUSH_U32(ndr
, ndr
->offset
+4, (v
& 0xFFFFFFFF));
623 return NDR_ERR_SUCCESS
;
629 _PUBLIC_
enum ndr_err_code
ndr_push_dlong(struct ndr_push
*ndr
, int ndr_flags
, int64_t v
)
631 return ndr_push_udlong(ndr
, NDR_SCALARS
, (uint64_t)v
);
637 _PUBLIC_
enum ndr_err_code
ndr_push_hyper(struct ndr_push
*ndr
, int ndr_flags
, uint64_t v
)
639 NDR_PUSH_ALIGN(ndr
, 8);
641 return ndr_push_udlongr(ndr
, NDR_SCALARS
, v
);
643 return ndr_push_udlong(ndr
, NDR_SCALARS
, v
);
649 _PUBLIC_
enum ndr_err_code
ndr_push_double(struct ndr_push
*ndr
, int ndr_flags
, double v
)
651 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
652 NDR_PUSH_ALIGN(ndr
, 8);
653 NDR_PUSH_NEED_BYTES(ndr
, 8);
654 memcpy(ndr
->data
+ndr
->offset
, &v
, 8);
656 return NDR_ERR_SUCCESS
;
662 _PUBLIC_
enum ndr_err_code
ndr_push_pointer(struct ndr_push
*ndr
, int ndr_flags
, void* v
)
664 uintptr_t h
= (intptr_t)v
;
665 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
666 NDR_PUSH_ALIGN(ndr
, sizeof(h
));
667 NDR_PUSH_NEED_BYTES(ndr
, sizeof(h
));
668 memcpy(ndr
->data
+ndr
->offset
, &h
, sizeof(h
));
669 ndr
->offset
+= sizeof(h
);
670 return NDR_ERR_SUCCESS
;
673 _PUBLIC_
enum ndr_err_code
ndr_push_align(struct ndr_push
*ndr
, size_t size
)
675 /* this is a nasty hack to make pidl work with NDR64 */
677 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
682 } else if (size
== 3) {
683 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
689 NDR_PUSH_ALIGN(ndr
, size
);
690 return NDR_ERR_SUCCESS
;
693 _PUBLIC_
enum ndr_err_code
ndr_pull_align(struct ndr_pull
*ndr
, size_t size
)
695 /* this is a nasty hack to make pidl work with NDR64 */
697 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
702 } else if (size
== 3) {
703 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
709 NDR_PULL_ALIGN(ndr
, size
);
710 return NDR_ERR_SUCCESS
;
713 _PUBLIC_
enum ndr_err_code
ndr_push_union_align(struct ndr_push
*ndr
, size_t size
)
715 /* MS-RPCE section 2.2.5.3.4.4 */
716 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
717 return ndr_push_align(ndr
, size
);
719 return NDR_ERR_SUCCESS
;
722 _PUBLIC_
enum ndr_err_code
ndr_pull_union_align(struct ndr_pull
*ndr
, size_t size
)
724 /* MS-RPCE section 2.2.5.3.4.4 */
725 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
726 return ndr_pull_align(ndr
, size
);
728 return NDR_ERR_SUCCESS
;
731 _PUBLIC_
enum ndr_err_code
ndr_push_trailer_align(struct ndr_push
*ndr
, size_t size
)
733 /* MS-RPCE section 2.2.5.3.4.1 */
734 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
735 return ndr_push_align(ndr
, size
);
737 return NDR_ERR_SUCCESS
;
740 _PUBLIC_
enum ndr_err_code
ndr_pull_trailer_align(struct ndr_pull
*ndr
, size_t size
)
742 /* MS-RPCE section 2.2.5.3.4.1 */
743 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
744 return ndr_pull_align(ndr
, size
);
746 return NDR_ERR_SUCCESS
;
752 _PUBLIC_
enum ndr_err_code
ndr_push_bytes(struct ndr_push
*ndr
, const uint8_t *data
, uint32_t n
)
754 if (unlikely(n
== 0)) {
755 return NDR_ERR_SUCCESS
;
757 if (unlikely(data
== NULL
)) {
758 return NDR_ERR_INVALID_POINTER
;
760 NDR_PUSH_NEED_BYTES(ndr
, n
);
761 memcpy(ndr
->data
+ ndr
->offset
, data
, n
);
763 return NDR_ERR_SUCCESS
;
769 _PUBLIC_
enum ndr_err_code
ndr_push_zero(struct ndr_push
*ndr
, uint32_t n
)
771 NDR_PUSH_NEED_BYTES(ndr
, n
);
772 memset(ndr
->data
+ ndr
->offset
, 0, n
);
774 return NDR_ERR_SUCCESS
;
778 push an array of uint8
780 _PUBLIC_
enum ndr_err_code
ndr_push_array_uint8(struct ndr_push
*ndr
, int ndr_flags
, const uint8_t *data
, uint32_t n
)
782 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
783 if (!(ndr_flags
& NDR_SCALARS
)) {
784 return NDR_ERR_SUCCESS
;
786 return ndr_push_bytes(ndr
, data
, n
);
790 push a unique non-zero value if a pointer is non-NULL, otherwise 0
792 _PUBLIC_
enum ndr_err_code
ndr_push_unique_ptr(struct ndr_push
*ndr
, const void *p
)
796 ptr
= ndr
->ptr_count
* 4;
800 return ndr_push_uint3264(ndr
, NDR_SCALARS
, ptr
);
804 push a 'simple' full non-zero value if a pointer is non-NULL, otherwise 0
806 _PUBLIC_
enum ndr_err_code
ndr_push_full_ptr(struct ndr_push
*ndr
, const void *p
)
808 enum ndr_err_code ret
= NDR_ERR_SUCCESS
;
811 /* Check if the pointer already exists and has an id */
812 ret
= ndr_token_peek(&ndr
->full_ptr_list
, p
, &ptr
);
813 if (ret
== NDR_ERR_TOKEN
) {
815 ptr
= ndr
->ptr_count
;
816 ret
= ndr_token_store(ndr
, &ndr
->full_ptr_list
, p
, ptr
);
817 if (ret
!= NDR_ERR_SUCCESS
) {
820 } else if (ret
!= NDR_ERR_SUCCESS
) {
824 return ndr_push_uint3264(ndr
, NDR_SCALARS
, ptr
);
828 push always a 0, if a pointer is NULL it's a fatal error
830 _PUBLIC_
enum ndr_err_code
ndr_push_ref_ptr(struct ndr_push
*ndr
)
832 return ndr_push_uint3264(ndr
, NDR_SCALARS
, 0xAEF1AEF1);
839 _PUBLIC_
enum ndr_err_code
ndr_push_NTTIME(struct ndr_push
*ndr
, int ndr_flags
, NTTIME t
)
841 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
842 NDR_CHECK(ndr_push_udlong(ndr
, ndr_flags
, t
));
843 return NDR_ERR_SUCCESS
;
849 _PUBLIC_
enum ndr_err_code
ndr_pull_NTTIME(struct ndr_pull
*ndr
, int ndr_flags
, NTTIME
*t
)
851 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
852 NDR_CHECK(ndr_pull_udlong(ndr
, ndr_flags
, t
));
853 return NDR_ERR_SUCCESS
;
859 _PUBLIC_
enum ndr_err_code
ndr_push_NTTIME_1sec(struct ndr_push
*ndr
, int ndr_flags
, NTTIME t
)
861 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
863 NDR_CHECK(ndr_push_hyper(ndr
, ndr_flags
, t
));
864 return NDR_ERR_SUCCESS
;
870 _PUBLIC_
enum ndr_err_code
ndr_pull_NTTIME_1sec(struct ndr_pull
*ndr
, int ndr_flags
, NTTIME
*t
)
872 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
873 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, t
));
875 return NDR_ERR_SUCCESS
;
881 _PUBLIC_
enum ndr_err_code
ndr_pull_NTTIME_hyper(struct ndr_pull
*ndr
, int ndr_flags
, NTTIME
*t
)
883 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
884 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, t
));
885 return NDR_ERR_SUCCESS
;
891 _PUBLIC_
enum ndr_err_code
ndr_push_NTTIME_hyper(struct ndr_push
*ndr
, int ndr_flags
, NTTIME t
)
893 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
894 NDR_CHECK(ndr_push_hyper(ndr
, ndr_flags
, t
));
895 return NDR_ERR_SUCCESS
;
901 _PUBLIC_
enum ndr_err_code
ndr_push_time_t(struct ndr_push
*ndr
, int ndr_flags
, time_t t
)
903 return ndr_push_uint32(ndr
, ndr_flags
, t
);
909 _PUBLIC_
enum ndr_err_code
ndr_pull_time_t(struct ndr_pull
*ndr
, int ndr_flags
, time_t *t
)
912 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &tt
));
914 return NDR_ERR_SUCCESS
;
921 _PUBLIC_
enum ndr_err_code
ndr_push_uid_t(struct ndr_push
*ndr
, int ndr_flags
, uid_t u
)
923 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
924 return ndr_push_hyper(ndr
, NDR_SCALARS
, (uint64_t)u
);
930 _PUBLIC_
enum ndr_err_code
ndr_pull_uid_t(struct ndr_pull
*ndr
, int ndr_flags
, uid_t
*u
)
933 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, &uu
));
935 if (unlikely(uu
!= *u
)) {
936 DEBUG(0,(__location__
": uid_t pull doesn't fit 0x%016llx\n",
937 (unsigned long long)uu
));
938 return NDR_ERR_NDR64
;
940 return NDR_ERR_SUCCESS
;
947 _PUBLIC_
enum ndr_err_code
ndr_push_gid_t(struct ndr_push
*ndr
, int ndr_flags
, gid_t g
)
949 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
950 return ndr_push_hyper(ndr
, NDR_SCALARS
, (uint64_t)g
);
956 _PUBLIC_
enum ndr_err_code
ndr_pull_gid_t(struct ndr_pull
*ndr
, int ndr_flags
, gid_t
*g
)
959 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, &gg
));
961 if (unlikely(gg
!= *g
)) {
962 DEBUG(0,(__location__
": gid_t pull doesn't fit 0x%016llx\n",
963 (unsigned long long)gg
));
964 return NDR_ERR_NDR64
;
966 return NDR_ERR_SUCCESS
;
973 _PUBLIC_
enum ndr_err_code
ndr_pull_ipv4address(struct ndr_pull
*ndr
, int ndr_flags
, const char **address
)
977 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &addr
));
978 in
.s_addr
= htonl(addr
);
979 *address
= talloc_strdup(ndr
->current_mem_ctx
, inet_ntoa(in
));
980 NDR_ERR_HAVE_NO_MEMORY(*address
);
981 return NDR_ERR_SUCCESS
;
987 _PUBLIC_
enum ndr_err_code
ndr_push_ipv4address(struct ndr_push
*ndr
, int ndr_flags
, const char *address
)
990 if (!is_ipaddress(address
)) {
991 return ndr_push_error(ndr
, NDR_ERR_IPV4ADDRESS
,
992 "Invalid IPv4 address: '%s'",
995 addr
= inet_addr(address
);
996 NDR_CHECK(ndr_push_uint32(ndr
, ndr_flags
, htonl(addr
)));
997 return NDR_ERR_SUCCESS
;
1003 _PUBLIC_
void ndr_print_ipv4address(struct ndr_print
*ndr
, const char *name
,
1004 const char *address
)
1006 ndr
->print(ndr
, "%-25s: %s", name
, address
);
1012 #define IPV6_BYTES 16
1013 #define IPV6_ADDR_STR_LEN 39
1014 _PUBLIC_
enum ndr_err_code
ndr_pull_ipv6address(struct ndr_pull
*ndr
, int ndr_flags
, const char **address
)
1016 uint8_t addr
[IPV6_BYTES
];
1017 char *addr_str
= talloc_strdup(ndr
->current_mem_ctx
, "");
1019 NDR_CHECK(ndr_pull_array_uint8(ndr
, ndr_flags
, addr
, IPV6_BYTES
));
1020 for (i
= 0; i
< IPV6_BYTES
; ++i
) {
1021 addr_str
= talloc_asprintf_append(addr_str
, "%02x", addr
[i
]);
1022 /* We need a ':' every second byte but the last one */
1023 if (i
%2 == 1 && i
!= (IPV6_BYTES
- 1)) {
1024 addr_str
= talloc_strdup_append(addr_str
, ":");
1027 *address
= addr_str
;
1028 NDR_ERR_HAVE_NO_MEMORY(*address
);
1029 return NDR_ERR_SUCCESS
;
1035 _PUBLIC_
enum ndr_err_code
ndr_push_ipv6address(struct ndr_push
*ndr
, int ndr_flags
, const char *address
)
1038 uint8_t addr
[IPV6_BYTES
];
1041 if (!is_ipaddress(address
)) {
1042 return ndr_push_error(ndr
, NDR_ERR_IPV6ADDRESS
,
1043 "Invalid IPv6 address: '%s'",
1046 ret
= inet_pton(AF_INET6
, address
, addr
);
1048 return NDR_ERR_IPV6ADDRESS
;
1051 NDR_CHECK(ndr_push_array_uint8(ndr
, ndr_flags
, addr
, IPV6_BYTES
));
1053 return NDR_ERR_SUCCESS
;
1055 return NDR_ERR_IPV6ADDRESS
;
1062 _PUBLIC_
void ndr_print_ipv6address(struct ndr_print
*ndr
, const char *name
,
1063 const char *address
)
1065 ndr
->print(ndr
, "%-25s: %s", name
, address
);
1069 _PUBLIC_
void ndr_print_struct(struct ndr_print
*ndr
, const char *name
, const char *type
)
1071 ndr
->print(ndr
, "%s: struct %s", name
, type
);
1074 _PUBLIC_
void ndr_print_null(struct ndr_print
*ndr
)
1076 ndr
->print(ndr
, "UNEXPECTED NULL POINTER");
1079 _PUBLIC_
void ndr_print_enum(struct ndr_print
*ndr
, const char *name
, const char *type
,
1080 const char *val
, uint32_t value
)
1082 if (ndr
->flags
& LIBNDR_PRINT_ARRAY_HEX
) {
1083 ndr
->print(ndr
, "%-25s: %s (0x%X)", name
, val
?val
:"UNKNOWN_ENUM_VALUE", value
);
1085 ndr
->print(ndr
, "%-25s: %s (%d)", name
, val
?val
:"UNKNOWN_ENUM_VALUE", value
);
1089 _PUBLIC_
void ndr_print_bitmap_flag(struct ndr_print
*ndr
, size_t size
, const char *flag_name
, uint32_t flag
, uint32_t value
)
1095 /* this is an attempt to support multi-bit bitmap masks */
1098 while (!(flag
& 1)) {
1103 ndr
->print(ndr
, " %d: %-25s", value
, flag_name
);
1105 ndr
->print(ndr
, "0x%02x: %-25s (%d)", value
, flag_name
, value
);
1109 _PUBLIC_
void ndr_print_int8(struct ndr_print
*ndr
, const char *name
, int8_t v
)
1111 if (NDR_HIDE_SECRET(ndr
)) {
1112 ndr
->print(ndr
, "%-25s: <REDACTED SECRET VALUE>", name
);
1115 ndr
->print(ndr
, "%-25s: %d", name
, v
);
1118 _PUBLIC_
void ndr_print_uint8(struct ndr_print
*ndr
, const char *name
, uint8_t v
)
1120 if (NDR_HIDE_SECRET(ndr
)) {
1121 ndr
->print(ndr
, "%-25s: <REDACTED SECRET VALUE>", name
);
1124 ndr
->print(ndr
, "%-25s: 0x%02x (%u)", name
, v
, v
);
1127 _PUBLIC_
void ndr_print_int16(struct ndr_print
*ndr
, const char *name
, int16_t v
)
1129 if (NDR_HIDE_SECRET(ndr
)) {
1130 ndr
->print(ndr
, "%-25s: <REDACTED SECRET VALUE>", name
);
1133 ndr
->print(ndr
, "%-25s: %d", name
, v
);
1136 _PUBLIC_
void ndr_print_uint16(struct ndr_print
*ndr
, const char *name
, uint16_t v
)
1138 if (NDR_HIDE_SECRET(ndr
)) {
1139 ndr
->print(ndr
, "%-25s: <REDACTED SECRET VALUE>", name
);
1142 ndr
->print(ndr
, "%-25s: 0x%04x (%u)", name
, v
, v
);
1145 _PUBLIC_
void ndr_print_int32(struct ndr_print
*ndr
, const char *name
, int32_t v
)
1147 if (NDR_HIDE_SECRET(ndr
)) {
1148 ndr
->print(ndr
, "%-25s: <REDACTED SECRET VALUE>", name
);
1151 ndr
->print(ndr
, "%-25s: %d", name
, v
);
1154 _PUBLIC_
void ndr_print_uint32(struct ndr_print
*ndr
, const char *name
, uint32_t v
)
1156 if (NDR_HIDE_SECRET(ndr
)) {
1157 ndr
->print(ndr
, "%-25s: <REDACTED SECRET VALUE>", name
);
1160 ndr
->print(ndr
, "%-25s: 0x%08x (%u)", name
, v
, v
);
1163 _PUBLIC_
void ndr_print_int3264(struct ndr_print
*ndr
, const char *name
, int32_t v
)
1165 if (NDR_HIDE_SECRET(ndr
)) {
1166 ndr
->print(ndr
, "%-25s: <REDACTED SECRET VALUE>", name
);
1169 ndr
->print(ndr
, "%-25s: %d", name
, v
);
1172 _PUBLIC_
void ndr_print_uint3264(struct ndr_print
*ndr
, const char *name
, uint32_t v
)
1174 if (NDR_HIDE_SECRET(ndr
)) {
1175 ndr
->print(ndr
, "%-25s: <REDACTED SECRET VALUE>", name
);
1178 ndr
->print(ndr
, "%-25s: 0x%08x (%u)", name
, v
, v
);
1181 _PUBLIC_
void ndr_print_udlong(struct ndr_print
*ndr
, const char *name
, uint64_t v
)
1183 ndr
->print(ndr
, "%-25s: 0x%016llx (%llu)", name
, (unsigned long long)v
, (unsigned long long)v
);
1186 _PUBLIC_
void ndr_print_udlongr(struct ndr_print
*ndr
, const char *name
, uint64_t v
)
1188 ndr_print_udlong(ndr
, name
, v
);
1191 _PUBLIC_
void ndr_print_dlong(struct ndr_print
*ndr
, const char *name
, int64_t v
)
1193 if (NDR_HIDE_SECRET(ndr
)) {
1194 ndr
->print(ndr
, "%-25s: <REDACTED SECRET VALUE>", name
);
1197 ndr
->print(ndr
, "%-25s: 0x%016llx (%lld)", name
, (unsigned long long)v
, (long long)v
);
1200 _PUBLIC_
void ndr_print_double(struct ndr_print
*ndr
, const char *name
, double v
)
1202 ndr
->print(ndr
, "%-25s: %f", name
, v
);
1205 _PUBLIC_
void ndr_print_hyper(struct ndr_print
*ndr
, const char *name
, uint64_t v
)
1207 ndr_print_dlong(ndr
, name
, v
);
1210 _PUBLIC_
void ndr_print_pointer(struct ndr_print
*ndr
, const char *name
, void *v
)
1212 ndr
->print(ndr
, "%-25s: %p", name
, v
);
1215 _PUBLIC_
void ndr_print_ptr(struct ndr_print
*ndr
, const char *name
, const void *p
)
1218 ndr
->print(ndr
, "%-25s: *", name
);
1220 ndr
->print(ndr
, "%-25s: NULL", name
);
1224 _PUBLIC_
void ndr_print_NTTIME(struct ndr_print
*ndr
, const char *name
, NTTIME t
)
1226 ndr
->print(ndr
, "%-25s: %s", name
, nt_time_string(ndr
, t
));
1229 _PUBLIC_
void ndr_print_NTTIME_1sec(struct ndr_print
*ndr
, const char *name
, NTTIME t
)
1231 /* this is a standard NTTIME here
1232 * as it's already converted in the pull/push code
1234 ndr_print_NTTIME(ndr
, name
, t
);
1237 _PUBLIC_
void ndr_print_NTTIME_hyper(struct ndr_print
*ndr
, const char *name
, NTTIME t
)
1239 ndr_print_NTTIME(ndr
, name
, t
);
1242 _PUBLIC_
void ndr_print_time_t(struct ndr_print
*ndr
, const char *name
, time_t t
)
1244 if (t
== (time_t)-1 || t
== 0) {
1245 ndr
->print(ndr
, "%-25s: (time_t)%d", name
, (int)t
);
1247 ndr
->print(ndr
, "%-25s: %s", name
, timestring(ndr
, t
));
1251 _PUBLIC_
void ndr_print_uid_t(struct ndr_print
*ndr
, const char *name
, uid_t u
)
1253 ndr_print_dlong(ndr
, name
, u
);
1256 _PUBLIC_
void ndr_print_gid_t(struct ndr_print
*ndr
, const char *name
, gid_t g
)
1258 ndr_print_dlong(ndr
, name
, g
);
1261 _PUBLIC_
void ndr_print_union(struct ndr_print
*ndr
, const char *name
, int level
, const char *type
)
1263 if (ndr
->flags
& LIBNDR_PRINT_ARRAY_HEX
) {
1264 ndr
->print(ndr
, "%-25s: union %s(case 0x%X)", name
, type
, level
);
1266 ndr
->print(ndr
, "%-25s: union %s(case %d)", name
, type
, level
);
1270 _PUBLIC_
void ndr_print_bad_level(struct ndr_print
*ndr
, const char *name
, uint16_t level
)
1272 ndr
->print(ndr
, "UNKNOWN LEVEL %u", level
);
1275 _PUBLIC_
void ndr_print_array_uint8(struct ndr_print
*ndr
, const char *name
,
1276 const uint8_t *data
, uint32_t count
)
1279 #define _ONELINE_LIMIT 32
1282 ndr
->print(ndr
, "%s: ARRAY(%d) : NULL", name
, count
);
1286 if (NDR_HIDE_SECRET(ndr
)) {
1287 ndr
->print(ndr
, "%s: ARRAY(%d): <REDACTED SECRET VALUES>", name
, count
);
1291 if (count
<= _ONELINE_LIMIT
&& (ndr
->flags
& LIBNDR_PRINT_ARRAY_HEX
)) {
1292 char s
[(_ONELINE_LIMIT
+ 1) * 2];
1293 for (i
=0;i
<count
;i
++) {
1294 snprintf(&s
[i
*2], 3, "%02x", data
[i
]);
1297 ndr
->print(ndr
, "%-25s: %s", name
, s
);
1301 ndr
->print(ndr
, "%s: ARRAY(%d)", name
, count
);
1302 if (count
> _ONELINE_LIMIT
&& (ndr
->flags
& LIBNDR_PRINT_ARRAY_HEX
)) {
1303 ndr_dump_data(ndr
, data
, count
);
1308 for (i
=0;i
<count
;i
++) {
1310 if (asprintf(&idx
, "[%d]", i
) != -1) {
1311 ndr_print_uint8(ndr
, idx
, data
[i
]);
1316 #undef _ONELINE_LIMIT
1319 static void ndr_print_dump_data_cb(const char *buf
, void *private_data
)
1321 struct ndr_print
*ndr
= (struct ndr_print
*)private_data
;
1323 ndr
->print(ndr
, "%s", buf
);
1327 ndr_print version of dump_data()
1329 static void ndr_dump_data(struct ndr_print
*ndr
, const uint8_t *buf
, int len
)
1331 if (NDR_HIDE_SECRET(ndr
)) {
1334 ndr
->no_newline
= true;
1335 dump_data_cb(buf
, len
, true, ndr_print_dump_data_cb
, ndr
);
1336 ndr
->no_newline
= false;
1340 _PUBLIC_
void ndr_print_DATA_BLOB(struct ndr_print
*ndr
, const char *name
, DATA_BLOB r
)
1342 ndr
->print(ndr
, "%-25s: DATA_BLOB length=%u", name
, (unsigned)r
.length
);
1344 ndr_dump_data(ndr
, r
.data
, r
.length
);
1350 * Push a DATA_BLOB onto the wire.
1351 * 1) When called with LIBNDR_FLAG_ALIGN* alignment flags set, push padding
1352 * bytes _only_. The length is determined by the alignment required and the
1353 * current ndr offset.
1354 * 2) When called with the LIBNDR_FLAG_REMAINING flag, push the byte array to
1356 * 3) Otherwise, push a uint3264 length _and_ a corresponding byte array to the
1359 _PUBLIC_
enum ndr_err_code
ndr_push_DATA_BLOB(struct ndr_push
*ndr
, int ndr_flags
, DATA_BLOB blob
)
1361 if (ndr
->flags
& LIBNDR_FLAG_REMAINING
) {
1363 } else if (ndr
->flags
& (LIBNDR_ALIGN_FLAGS
& ~LIBNDR_FLAG_NOALIGN
)) {
1364 if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
1365 blob
.length
= NDR_ALIGN(ndr
, 2);
1366 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
1367 blob
.length
= NDR_ALIGN(ndr
, 4);
1368 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
1369 blob
.length
= NDR_ALIGN(ndr
, 8);
1371 NDR_PUSH_ALLOC_SIZE(ndr
, blob
.data
, blob
.length
);
1372 data_blob_clear(&blob
);
1374 NDR_CHECK(ndr_push_uint3264(ndr
, NDR_SCALARS
, blob
.length
));
1376 NDR_CHECK(ndr_push_bytes(ndr
, blob
.data
, blob
.length
));
1377 return NDR_ERR_SUCCESS
;
1381 * Pull a DATA_BLOB from the wire.
1382 * 1) when called with LIBNDR_FLAG_ALIGN* alignment flags set, pull padding
1383 * bytes _only_. The length is determined by the alignment required and the
1384 * current ndr offset.
1385 * 2) When called with the LIBNDR_FLAG_REMAINING flag, pull all remaining bytes
1386 * from the ndr buffer.
1387 * 3) Otherwise, pull a uint3264 length _and_ a corresponding byte array from the
1390 _PUBLIC_
enum ndr_err_code
ndr_pull_DATA_BLOB(struct ndr_pull
*ndr
, int ndr_flags
, DATA_BLOB
*blob
)
1392 uint32_t length
= 0;
1394 if (ndr
->flags
& LIBNDR_FLAG_REMAINING
) {
1395 length
= ndr
->data_size
- ndr
->offset
;
1396 } else if (ndr
->flags
& (LIBNDR_ALIGN_FLAGS
& ~LIBNDR_FLAG_NOALIGN
)) {
1397 if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
1398 length
= NDR_ALIGN(ndr
, 2);
1399 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
1400 length
= NDR_ALIGN(ndr
, 4);
1401 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
1402 length
= NDR_ALIGN(ndr
, 8);
1404 if (ndr
->data_size
- ndr
->offset
< length
) {
1405 length
= ndr
->data_size
- ndr
->offset
;
1408 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &length
));
1410 NDR_PULL_NEED_BYTES(ndr
, length
);
1411 *blob
= data_blob_talloc(ndr
->current_mem_ctx
, ndr
->data
+ndr
->offset
, length
);
1412 ndr
->offset
+= length
;
1413 return NDR_ERR_SUCCESS
;
1416 _PUBLIC_
uint32_t ndr_size_DATA_BLOB(int ret
, const DATA_BLOB
*data
, int flags
)
1418 if (!data
) return ret
;
1419 return ret
+ data
->length
;
1422 _PUBLIC_
void ndr_print_bool(struct ndr_print
*ndr
, const char *name
, const bool b
)
1424 ndr
->print(ndr
, "%-25s: %s", name
, b
?"true":"false");
1427 _PUBLIC_ NTSTATUS
ndr_map_error2ntstatus(enum ndr_err_code ndr_err
)
1430 case NDR_ERR_SUCCESS
:
1431 return NT_STATUS_OK
;
1432 case NDR_ERR_BUFSIZE
:
1433 return NT_STATUS_BUFFER_TOO_SMALL
;
1435 return NT_STATUS_INTERNAL_ERROR
;
1437 return NT_STATUS_NO_MEMORY
;
1438 case NDR_ERR_ARRAY_SIZE
:
1439 return NT_STATUS_ARRAY_BOUNDS_EXCEEDED
;
1440 case NDR_ERR_INVALID_POINTER
:
1441 return NT_STATUS_INVALID_PARAMETER_MIX
;
1442 case NDR_ERR_UNREAD_BYTES
:
1443 return NT_STATUS_PORT_MESSAGE_TOO_LONG
;
1448 /* we should map all error codes to different status codes */
1449 return NT_STATUS_INVALID_PARAMETER
;
1452 _PUBLIC_
int ndr_map_error2errno(enum ndr_err_code ndr_err
)
1455 case NDR_ERR_SUCCESS
:
1457 case NDR_ERR_BUFSIZE
:
1463 case NDR_ERR_ARRAY_SIZE
:
1465 case NDR_ERR_INVALID_POINTER
:
1467 case NDR_ERR_UNREAD_BYTES
:
1473 /* we should map all error codes to different status codes */
1477 _PUBLIC_
enum ndr_err_code
ndr_push_timespec(struct ndr_push
*ndr
,
1479 const struct timespec
*t
)
1481 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
1482 NDR_CHECK(ndr_push_hyper(ndr
, ndr_flags
, t
->tv_sec
));
1483 NDR_CHECK(ndr_push_uint32(ndr
, ndr_flags
, t
->tv_nsec
));
1484 return NDR_ERR_SUCCESS
;
1487 _PUBLIC_
enum ndr_err_code
ndr_pull_timespec(struct ndr_pull
*ndr
,
1493 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
1494 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, &secs
));
1495 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &nsecs
));
1498 return NDR_ERR_SUCCESS
;
1501 _PUBLIC_
void ndr_print_timespec(struct ndr_print
*ndr
, const char *name
,
1502 const struct timespec
*t
)
1504 char *str
= timestring(ndr
, t
->tv_sec
);
1505 ndr
->print(ndr
, "%-25s: %s.%ld", name
, str
, t
->tv_nsec
);
1509 _PUBLIC_
enum ndr_err_code
ndr_push_timeval(struct ndr_push
*ndr
,
1511 const struct timeval
*t
)
1513 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
1514 NDR_CHECK(ndr_push_hyper(ndr
, ndr_flags
, t
->tv_sec
));
1515 NDR_CHECK(ndr_push_uint32(ndr
, ndr_flags
, t
->tv_usec
));
1516 return NDR_ERR_SUCCESS
;
1519 _PUBLIC_
enum ndr_err_code
ndr_pull_timeval(struct ndr_pull
*ndr
,
1525 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
1526 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, &secs
));
1527 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &usecs
));
1530 return NDR_ERR_SUCCESS
;
1533 _PUBLIC_
void ndr_print_timeval(struct ndr_print
*ndr
, const char *name
,
1534 const struct timeval
*t
)
1536 ndr
->print(ndr
, "%-25s: %s.%ld", name
, timestring(ndr
, t
->tv_sec
),