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"
27 #define NDR_SVAL(ndr, ofs) (NDR_BE(ndr)?RSVAL(ndr->data,ofs):SVAL(ndr->data,ofs))
28 #define NDR_IVAL(ndr, ofs) (NDR_BE(ndr)?RIVAL(ndr->data,ofs):IVAL(ndr->data,ofs))
29 #define NDR_IVALS(ndr, ofs) (NDR_BE(ndr)?RIVALS(ndr->data,ofs):IVALS(ndr->data,ofs))
30 #define NDR_SSVAL(ndr, ofs, v) do { if (NDR_BE(ndr)) { RSSVAL(ndr->data,ofs,v); } else SSVAL(ndr->data,ofs,v); } while (0)
31 #define NDR_SIVAL(ndr, ofs, v) do { if (NDR_BE(ndr)) { RSIVAL(ndr->data,ofs,v); } else SIVAL(ndr->data,ofs,v); } while (0)
32 #define NDR_SIVALS(ndr, ofs, v) do { if (NDR_BE(ndr)) { RSIVALS(ndr->data,ofs,v); } else SIVALS(ndr->data,ofs,v); } while (0)
36 check for data leaks from the server by looking for non-zero pad bytes
37 these could also indicate that real structure elements have been
38 mistaken for padding in the IDL
40 _PUBLIC_
void ndr_check_padding(struct ndr_pull
*ndr
, size_t n
)
42 size_t ofs2
= (ndr
->offset
+ (n
-1)) & ~(n
-1);
44 for (i
=ndr
->offset
;i
<ofs2
;i
++) {
45 if (ndr
->data
[i
] != 0) {
50 DEBUG(0,("WARNING: Non-zero padding to %d: ", (int)n
));
51 for (i
=ndr
->offset
;i
<ofs2
;i
++) {
52 DEBUG(0,("%02x ", ndr
->data
[i
]));
62 _PUBLIC_
enum ndr_err_code
ndr_pull_int8(struct ndr_pull
*ndr
, int ndr_flags
, int8_t *v
)
64 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
65 NDR_PULL_NEED_BYTES(ndr
, 1);
66 *v
= (int8_t)CVAL(ndr
->data
, ndr
->offset
);
68 return NDR_ERR_SUCCESS
;
74 _PUBLIC_
enum ndr_err_code
ndr_pull_uint8(struct ndr_pull
*ndr
, int ndr_flags
, uint8_t *v
)
76 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
77 NDR_PULL_NEED_BYTES(ndr
, 1);
78 *v
= CVAL(ndr
->data
, ndr
->offset
);
80 return NDR_ERR_SUCCESS
;
86 _PUBLIC_
enum ndr_err_code
ndr_pull_int16(struct ndr_pull
*ndr
, int ndr_flags
, int16_t *v
)
88 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
89 NDR_PULL_ALIGN(ndr
, 2);
90 NDR_PULL_NEED_BYTES(ndr
, 2);
91 *v
= (uint16_t)NDR_SVAL(ndr
, ndr
->offset
);
93 return NDR_ERR_SUCCESS
;
99 _PUBLIC_
enum ndr_err_code
ndr_pull_uint16(struct ndr_pull
*ndr
, int ndr_flags
, uint16_t *v
)
101 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
102 NDR_PULL_ALIGN(ndr
, 2);
103 NDR_PULL_NEED_BYTES(ndr
, 2);
104 *v
= NDR_SVAL(ndr
, ndr
->offset
);
106 return NDR_ERR_SUCCESS
;
112 _PUBLIC_
enum ndr_err_code
ndr_pull_uint1632(struct ndr_pull
*ndr
, int ndr_flags
, uint16_t *v
)
114 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
115 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
117 enum ndr_err_code err
= ndr_pull_uint32(ndr
, ndr_flags
, &v32
);
119 if (unlikely(v32
!= *v
)) {
120 DEBUG(0,(__location__
": non-zero upper 16 bits 0x%08x\n", (unsigned)v32
));
121 return NDR_ERR_NDR64
;
125 return ndr_pull_uint16(ndr
, ndr_flags
, v
);
131 _PUBLIC_
enum ndr_err_code
ndr_pull_int32(struct ndr_pull
*ndr
, int ndr_flags
, int32_t *v
)
133 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
134 NDR_PULL_ALIGN(ndr
, 4);
135 NDR_PULL_NEED_BYTES(ndr
, 4);
136 *v
= NDR_IVALS(ndr
, ndr
->offset
);
138 return NDR_ERR_SUCCESS
;
144 _PUBLIC_
enum ndr_err_code
ndr_pull_uint32(struct ndr_pull
*ndr
, int ndr_flags
, uint32_t *v
)
146 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
147 NDR_PULL_ALIGN(ndr
, 4);
148 NDR_PULL_NEED_BYTES(ndr
, 4);
149 *v
= NDR_IVAL(ndr
, ndr
->offset
);
151 return NDR_ERR_SUCCESS
;
155 parse a arch dependent uint32/uint64
157 _PUBLIC_
enum ndr_err_code
ndr_pull_uint3264(struct ndr_pull
*ndr
, int ndr_flags
, uint32_t *v
)
160 enum ndr_err_code err
;
161 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
162 if (likely(!(ndr
->flags
& LIBNDR_FLAG_NDR64
))) {
163 return ndr_pull_uint32(ndr
, ndr_flags
, v
);
165 err
= ndr_pull_hyper(ndr
, ndr_flags
, &v64
);
167 if (unlikely(v64
!= *v
)) {
168 DEBUG(0,(__location__
": non-zero upper 32 bits 0x%016llx\n",
169 (unsigned long long)v64
));
170 return NDR_ERR_NDR64
;
178 _PUBLIC_
enum ndr_err_code
ndr_pull_double(struct ndr_pull
*ndr
, int ndr_flags
, double *v
)
180 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
181 NDR_PULL_ALIGN(ndr
, 8);
182 NDR_PULL_NEED_BYTES(ndr
, 8);
183 memcpy(v
, ndr
->data
+ndr
->offset
, 8);
185 return NDR_ERR_SUCCESS
;
189 parse a pointer referent identifier stored in 2 bytes
191 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr_short(struct ndr_pull
*ndr
, uint16_t *v
)
193 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, v
));
197 *(v
) -= ndr
->relative_rap_convert
;
198 return NDR_ERR_SUCCESS
;
202 parse a pointer referent identifier
204 _PUBLIC_
enum ndr_err_code
ndr_pull_generic_ptr(struct ndr_pull
*ndr
, uint32_t *v
)
206 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, v
));
210 return NDR_ERR_SUCCESS
;
214 parse a ref pointer referent identifier
216 _PUBLIC_
enum ndr_err_code
ndr_pull_ref_ptr(struct ndr_pull
*ndr
, uint32_t *v
)
218 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, v
));
219 /* ref pointers always point to data */
221 return NDR_ERR_SUCCESS
;
227 _PUBLIC_
enum ndr_err_code
ndr_pull_udlong(struct ndr_pull
*ndr
, int ndr_flags
, uint64_t *v
)
229 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
230 NDR_PULL_ALIGN(ndr
, 4);
231 NDR_PULL_NEED_BYTES(ndr
, 8);
232 *v
= NDR_IVAL(ndr
, ndr
->offset
);
233 *v
|= (uint64_t)(NDR_IVAL(ndr
, ndr
->offset
+4)) << 32;
235 return NDR_ERR_SUCCESS
;
241 _PUBLIC_
enum ndr_err_code
ndr_pull_udlongr(struct ndr_pull
*ndr
, int ndr_flags
, uint64_t *v
)
243 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
244 NDR_PULL_ALIGN(ndr
, 4);
245 NDR_PULL_NEED_BYTES(ndr
, 8);
246 *v
= ((uint64_t)NDR_IVAL(ndr
, ndr
->offset
)) << 32;
247 *v
|= NDR_IVAL(ndr
, ndr
->offset
+4);
249 return NDR_ERR_SUCCESS
;
255 _PUBLIC_
enum ndr_err_code
ndr_pull_dlong(struct ndr_pull
*ndr
, int ndr_flags
, int64_t *v
)
257 return ndr_pull_udlong(ndr
, ndr_flags
, (uint64_t *)v
);
263 _PUBLIC_
enum ndr_err_code
ndr_pull_hyper(struct ndr_pull
*ndr
, int ndr_flags
, uint64_t *v
)
265 NDR_PULL_ALIGN(ndr
, 8);
267 return ndr_pull_udlongr(ndr
, ndr_flags
, v
);
269 return ndr_pull_udlong(ndr
, ndr_flags
, v
);
275 _PUBLIC_
enum ndr_err_code
ndr_pull_pointer(struct ndr_pull
*ndr
, int ndr_flags
, void* *v
)
278 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
279 NDR_PULL_ALIGN(ndr
, sizeof(h
));
280 NDR_PULL_NEED_BYTES(ndr
, sizeof(h
));
281 memcpy(&h
, ndr
->data
+ndr
->offset
, sizeof(h
));
282 ndr
->offset
+= sizeof(h
);
284 return NDR_ERR_SUCCESS
;
290 _PUBLIC_
enum ndr_err_code
ndr_pull_NTSTATUS(struct ndr_pull
*ndr
, int ndr_flags
, NTSTATUS
*status
)
293 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
294 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &v
));
295 *status
= NT_STATUS(v
);
296 return NDR_ERR_SUCCESS
;
302 _PUBLIC_
enum ndr_err_code
ndr_push_NTSTATUS(struct ndr_push
*ndr
, int ndr_flags
, NTSTATUS status
)
304 return ndr_push_uint32(ndr
, ndr_flags
, NT_STATUS_V(status
));
307 _PUBLIC_
void ndr_print_NTSTATUS(struct ndr_print
*ndr
, const char *name
, NTSTATUS r
)
309 ndr
->print(ndr
, "%-25s: %s", name
, nt_errstr(r
));
315 _PUBLIC_
enum ndr_err_code
ndr_pull_WERROR(struct ndr_pull
*ndr
, int ndr_flags
, WERROR
*status
)
318 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
319 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &v
));
320 *status
= W_ERROR(v
);
321 return NDR_ERR_SUCCESS
;
328 _PUBLIC_
enum ndr_err_code
ndr_pull_enum_uint8(struct ndr_pull
*ndr
, int ndr_flags
, uint8_t *v
)
330 return ndr_pull_uint8(ndr
, ndr_flags
, v
);
334 parse a uint16_t enum
336 _PUBLIC_
enum ndr_err_code
ndr_pull_enum_uint16(struct ndr_pull
*ndr
, int ndr_flags
, uint16_t *v
)
338 return ndr_pull_uint16(ndr
, ndr_flags
, v
);
342 parse a uint1632_t enum (uint32_t on NDR64)
344 _PUBLIC_
enum ndr_err_code
ndr_pull_enum_uint1632(struct ndr_pull
*ndr
, int ndr_flags
, uint16_t *v
)
346 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
348 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &v32
));
351 DEBUG(0,(__location__
": non-zero upper 16 bits 0x%08x\n", (unsigned)v32
));
352 return NDR_ERR_NDR64
;
354 return NDR_ERR_SUCCESS
;
356 return ndr_pull_uint16(ndr
, ndr_flags
, v
);
360 parse a uint32_t enum
362 _PUBLIC_
enum ndr_err_code
ndr_pull_enum_uint32(struct ndr_pull
*ndr
, int ndr_flags
, uint32_t *v
)
364 return ndr_pull_uint32(ndr
, ndr_flags
, v
);
370 _PUBLIC_
enum ndr_err_code
ndr_push_enum_uint8(struct ndr_push
*ndr
, int ndr_flags
, uint8_t v
)
372 return ndr_push_uint8(ndr
, ndr_flags
, v
);
378 _PUBLIC_
enum ndr_err_code
ndr_push_enum_uint16(struct ndr_push
*ndr
, int ndr_flags
, uint16_t v
)
380 return ndr_push_uint16(ndr
, ndr_flags
, v
);
386 _PUBLIC_
enum ndr_err_code
ndr_push_enum_uint32(struct ndr_push
*ndr
, int ndr_flags
, uint32_t v
)
388 return ndr_push_uint32(ndr
, ndr_flags
, v
);
392 push a uint1632_t enum
394 _PUBLIC_
enum ndr_err_code
ndr_push_enum_uint1632(struct ndr_push
*ndr
, int ndr_flags
, uint16_t v
)
396 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
397 return ndr_push_uint32(ndr
, ndr_flags
, v
);
399 return ndr_push_uint16(ndr
, ndr_flags
, v
);
405 _PUBLIC_
enum ndr_err_code
ndr_push_WERROR(struct ndr_push
*ndr
, int ndr_flags
, WERROR status
)
407 return ndr_push_uint32(ndr
, NDR_SCALARS
, W_ERROR_V(status
));
410 _PUBLIC_
void ndr_print_WERROR(struct ndr_print
*ndr
, const char *name
, WERROR r
)
412 ndr
->print(ndr
, "%-25s: %s", name
, win_errstr(r
));
418 _PUBLIC_
enum ndr_err_code
ndr_pull_bytes(struct ndr_pull
*ndr
, uint8_t *data
, uint32_t n
)
420 NDR_PULL_NEED_BYTES(ndr
, n
);
421 memcpy(data
, ndr
->data
+ ndr
->offset
, n
);
423 return NDR_ERR_SUCCESS
;
427 pull an array of uint8
429 _PUBLIC_
enum ndr_err_code
ndr_pull_array_uint8(struct ndr_pull
*ndr
, int ndr_flags
, uint8_t *data
, uint32_t n
)
431 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
432 if (!(ndr_flags
& NDR_SCALARS
)) {
433 return NDR_ERR_SUCCESS
;
435 return ndr_pull_bytes(ndr
, data
, n
);
441 _PUBLIC_
enum ndr_err_code
ndr_push_int8(struct ndr_push
*ndr
, int ndr_flags
, int8_t v
)
443 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
444 NDR_PUSH_NEED_BYTES(ndr
, 1);
445 SCVAL(ndr
->data
, ndr
->offset
, (uint8_t)v
);
447 return NDR_ERR_SUCCESS
;
453 _PUBLIC_
enum ndr_err_code
ndr_push_uint8(struct ndr_push
*ndr
, int ndr_flags
, uint8_t v
)
455 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
456 NDR_PUSH_NEED_BYTES(ndr
, 1);
457 SCVAL(ndr
->data
, ndr
->offset
, v
);
459 return NDR_ERR_SUCCESS
;
465 _PUBLIC_
enum ndr_err_code
ndr_push_int16(struct ndr_push
*ndr
, int ndr_flags
, int16_t v
)
467 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
468 NDR_PUSH_ALIGN(ndr
, 2);
469 NDR_PUSH_NEED_BYTES(ndr
, 2);
470 NDR_SSVAL(ndr
, ndr
->offset
, (uint16_t)v
);
472 return NDR_ERR_SUCCESS
;
478 _PUBLIC_
enum ndr_err_code
ndr_push_uint16(struct ndr_push
*ndr
, int ndr_flags
, uint16_t v
)
480 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
481 NDR_PUSH_ALIGN(ndr
, 2);
482 NDR_PUSH_NEED_BYTES(ndr
, 2);
483 NDR_SSVAL(ndr
, ndr
->offset
, v
);
485 return NDR_ERR_SUCCESS
;
491 _PUBLIC_
enum ndr_err_code
ndr_push_uint1632(struct ndr_push
*ndr
, int ndr_flags
, uint16_t v
)
493 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
494 return ndr_push_uint32(ndr
, ndr_flags
, v
);
496 return ndr_push_uint16(ndr
, ndr_flags
, v
);
502 _PUBLIC_
enum ndr_err_code
ndr_push_int32(struct ndr_push
*ndr
, int ndr_flags
, int32_t v
)
504 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
505 NDR_PUSH_ALIGN(ndr
, 4);
506 NDR_PUSH_NEED_BYTES(ndr
, 4);
507 NDR_SIVALS(ndr
, ndr
->offset
, v
);
509 return NDR_ERR_SUCCESS
;
515 _PUBLIC_
enum ndr_err_code
ndr_push_uint32(struct ndr_push
*ndr
, int ndr_flags
, uint32_t v
)
517 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
518 NDR_PUSH_ALIGN(ndr
, 4);
519 NDR_PUSH_NEED_BYTES(ndr
, 4);
520 NDR_SIVAL(ndr
, ndr
->offset
, v
);
522 return NDR_ERR_SUCCESS
;
528 _PUBLIC_
enum ndr_err_code
ndr_push_uint3264(struct ndr_push
*ndr
, int ndr_flags
, uint32_t v
)
530 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
531 return ndr_push_hyper(ndr
, ndr_flags
, v
);
533 return ndr_push_uint32(ndr
, ndr_flags
, v
);
539 _PUBLIC_
enum ndr_err_code
ndr_push_udlong(struct ndr_push
*ndr
, int ndr_flags
, uint64_t v
)
541 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
542 NDR_PUSH_ALIGN(ndr
, 4);
543 NDR_PUSH_NEED_BYTES(ndr
, 8);
544 NDR_SIVAL(ndr
, ndr
->offset
, (v
& 0xFFFFFFFF));
545 NDR_SIVAL(ndr
, ndr
->offset
+4, (v
>>32));
547 return NDR_ERR_SUCCESS
;
553 _PUBLIC_
enum ndr_err_code
ndr_push_udlongr(struct ndr_push
*ndr
, int ndr_flags
, uint64_t v
)
555 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
556 NDR_PUSH_ALIGN(ndr
, 4);
557 NDR_PUSH_NEED_BYTES(ndr
, 8);
558 NDR_SIVAL(ndr
, ndr
->offset
, (v
>>32));
559 NDR_SIVAL(ndr
, ndr
->offset
+4, (v
& 0xFFFFFFFF));
561 return NDR_ERR_SUCCESS
;
567 _PUBLIC_
enum ndr_err_code
ndr_push_dlong(struct ndr_push
*ndr
, int ndr_flags
, int64_t v
)
569 return ndr_push_udlong(ndr
, NDR_SCALARS
, (uint64_t)v
);
575 _PUBLIC_
enum ndr_err_code
ndr_push_hyper(struct ndr_push
*ndr
, int ndr_flags
, uint64_t v
)
577 NDR_PUSH_ALIGN(ndr
, 8);
579 return ndr_push_udlongr(ndr
, NDR_SCALARS
, v
);
581 return ndr_push_udlong(ndr
, NDR_SCALARS
, v
);
587 _PUBLIC_
enum ndr_err_code
ndr_push_double(struct ndr_push
*ndr
, int ndr_flags
, double v
)
589 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
590 NDR_PUSH_ALIGN(ndr
, 8);
591 NDR_PUSH_NEED_BYTES(ndr
, 8);
592 memcpy(ndr
->data
+ndr
->offset
, &v
, 8);
594 return NDR_ERR_SUCCESS
;
600 _PUBLIC_
enum ndr_err_code
ndr_push_pointer(struct ndr_push
*ndr
, int ndr_flags
, void* v
)
602 uintptr_t h
= (intptr_t)v
;
603 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
604 NDR_PUSH_ALIGN(ndr
, sizeof(h
));
605 NDR_PUSH_NEED_BYTES(ndr
, sizeof(h
));
606 memcpy(ndr
->data
+ndr
->offset
, &h
, sizeof(h
));
607 ndr
->offset
+= sizeof(h
);
608 return NDR_ERR_SUCCESS
;
611 _PUBLIC_
enum ndr_err_code
ndr_push_align(struct ndr_push
*ndr
, size_t size
)
613 /* this is a nasty hack to make pidl work with NDR64 */
615 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
620 } else if (size
== 3) {
621 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
627 NDR_PUSH_ALIGN(ndr
, size
);
628 return NDR_ERR_SUCCESS
;
631 _PUBLIC_
enum ndr_err_code
ndr_pull_align(struct ndr_pull
*ndr
, size_t size
)
633 /* this is a nasty hack to make pidl work with NDR64 */
635 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
640 } else if (size
== 3) {
641 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
647 NDR_PULL_ALIGN(ndr
, size
);
648 return NDR_ERR_SUCCESS
;
651 _PUBLIC_
enum ndr_err_code
ndr_push_union_align(struct ndr_push
*ndr
, size_t size
)
653 /* MS-RPCE section 2.2.5.3.4.4 */
654 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
655 return ndr_push_align(ndr
, size
);
657 return NDR_ERR_SUCCESS
;
660 _PUBLIC_
enum ndr_err_code
ndr_pull_union_align(struct ndr_pull
*ndr
, size_t size
)
662 /* MS-RPCE section 2.2.5.3.4.4 */
663 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
664 return ndr_pull_align(ndr
, size
);
666 return NDR_ERR_SUCCESS
;
669 _PUBLIC_
enum ndr_err_code
ndr_push_trailer_align(struct ndr_push
*ndr
, size_t size
)
671 /* MS-RPCE section 2.2.5.3.4.1 */
672 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
673 return ndr_push_align(ndr
, size
);
675 return NDR_ERR_SUCCESS
;
678 _PUBLIC_
enum ndr_err_code
ndr_pull_trailer_align(struct ndr_pull
*ndr
, size_t size
)
680 /* MS-RPCE section 2.2.5.3.4.1 */
681 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
682 return ndr_pull_align(ndr
, size
);
684 return NDR_ERR_SUCCESS
;
690 _PUBLIC_
enum ndr_err_code
ndr_push_bytes(struct ndr_push
*ndr
, const uint8_t *data
, uint32_t n
)
692 NDR_PUSH_NEED_BYTES(ndr
, n
);
693 memcpy(ndr
->data
+ ndr
->offset
, data
, n
);
695 return NDR_ERR_SUCCESS
;
701 _PUBLIC_
enum ndr_err_code
ndr_push_zero(struct ndr_push
*ndr
, uint32_t n
)
703 NDR_PUSH_NEED_BYTES(ndr
, n
);
704 memset(ndr
->data
+ ndr
->offset
, 0, n
);
706 return NDR_ERR_SUCCESS
;
710 push an array of uint8
712 _PUBLIC_
enum ndr_err_code
ndr_push_array_uint8(struct ndr_push
*ndr
, int ndr_flags
, const uint8_t *data
, uint32_t n
)
714 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
715 if (!(ndr_flags
& NDR_SCALARS
)) {
716 return NDR_ERR_SUCCESS
;
718 return ndr_push_bytes(ndr
, data
, n
);
722 push a unique non-zero value if a pointer is non-NULL, otherwise 0
724 _PUBLIC_
enum ndr_err_code
ndr_push_unique_ptr(struct ndr_push
*ndr
, const void *p
)
728 ptr
= ndr
->ptr_count
* 4;
732 return ndr_push_uint3264(ndr
, NDR_SCALARS
, ptr
);
736 push a 'simple' full non-zero value if a pointer is non-NULL, otherwise 0
738 _PUBLIC_
enum ndr_err_code
ndr_push_full_ptr(struct ndr_push
*ndr
, const void *p
)
742 /* Check if the pointer already exists and has an id */
743 ptr
= ndr_token_peek(&ndr
->full_ptr_list
, p
);
746 ptr
= ndr
->ptr_count
;
747 ndr_token_store(ndr
, &ndr
->full_ptr_list
, p
, ptr
);
750 return ndr_push_uint3264(ndr
, NDR_SCALARS
, ptr
);
754 push always a 0, if a pointer is NULL it's a fatal error
756 _PUBLIC_
enum ndr_err_code
ndr_push_ref_ptr(struct ndr_push
*ndr
)
758 return ndr_push_uint3264(ndr
, NDR_SCALARS
, 0xAEF1AEF1);
765 _PUBLIC_
enum ndr_err_code
ndr_push_NTTIME(struct ndr_push
*ndr
, int ndr_flags
, NTTIME t
)
767 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
768 NDR_CHECK(ndr_push_udlong(ndr
, ndr_flags
, t
));
769 return NDR_ERR_SUCCESS
;
775 _PUBLIC_
enum ndr_err_code
ndr_pull_NTTIME(struct ndr_pull
*ndr
, int ndr_flags
, NTTIME
*t
)
777 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
778 NDR_CHECK(ndr_pull_udlong(ndr
, ndr_flags
, t
));
779 return NDR_ERR_SUCCESS
;
785 _PUBLIC_
enum ndr_err_code
ndr_push_NTTIME_1sec(struct ndr_push
*ndr
, int ndr_flags
, NTTIME t
)
787 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
789 NDR_CHECK(ndr_push_hyper(ndr
, ndr_flags
, t
));
790 return NDR_ERR_SUCCESS
;
796 _PUBLIC_
enum ndr_err_code
ndr_pull_NTTIME_1sec(struct ndr_pull
*ndr
, int ndr_flags
, NTTIME
*t
)
798 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
799 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, t
));
801 return NDR_ERR_SUCCESS
;
807 _PUBLIC_
enum ndr_err_code
ndr_pull_NTTIME_hyper(struct ndr_pull
*ndr
, int ndr_flags
, NTTIME
*t
)
809 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
810 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, t
));
811 return NDR_ERR_SUCCESS
;
817 _PUBLIC_
enum ndr_err_code
ndr_push_NTTIME_hyper(struct ndr_push
*ndr
, int ndr_flags
, NTTIME t
)
819 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
820 NDR_CHECK(ndr_push_hyper(ndr
, ndr_flags
, t
));
821 return NDR_ERR_SUCCESS
;
827 _PUBLIC_
enum ndr_err_code
ndr_push_time_t(struct ndr_push
*ndr
, int ndr_flags
, time_t t
)
829 return ndr_push_uint32(ndr
, ndr_flags
, t
);
835 _PUBLIC_
enum ndr_err_code
ndr_pull_time_t(struct ndr_pull
*ndr
, int ndr_flags
, time_t *t
)
838 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &tt
));
840 return NDR_ERR_SUCCESS
;
847 _PUBLIC_
enum ndr_err_code
ndr_push_uid_t(struct ndr_push
*ndr
, int ndr_flags
, uid_t u
)
849 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
850 return ndr_push_hyper(ndr
, NDR_SCALARS
, (uint64_t)u
);
856 _PUBLIC_
enum ndr_err_code
ndr_pull_uid_t(struct ndr_pull
*ndr
, int ndr_flags
, uid_t
*u
)
859 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, &uu
));
861 if (unlikely(uu
!= *u
)) {
862 DEBUG(0,(__location__
": uid_t pull doesn't fit 0x%016llx\n",
863 (unsigned long long)uu
));
864 return NDR_ERR_NDR64
;
866 return NDR_ERR_SUCCESS
;
873 _PUBLIC_
enum ndr_err_code
ndr_push_gid_t(struct ndr_push
*ndr
, int ndr_flags
, gid_t g
)
875 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
876 return ndr_push_hyper(ndr
, NDR_SCALARS
, (uint64_t)g
);
882 _PUBLIC_
enum ndr_err_code
ndr_pull_gid_t(struct ndr_pull
*ndr
, int ndr_flags
, gid_t
*g
)
885 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, &gg
));
887 if (unlikely(gg
!= *g
)) {
888 DEBUG(0,(__location__
": gid_t pull doesn't fit 0x%016llx\n",
889 (unsigned long long)gg
));
890 return NDR_ERR_NDR64
;
892 return NDR_ERR_SUCCESS
;
899 _PUBLIC_
enum ndr_err_code
ndr_pull_ipv4address(struct ndr_pull
*ndr
, int ndr_flags
, const char **address
)
903 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &addr
));
904 in
.s_addr
= htonl(addr
);
905 *address
= talloc_strdup(ndr
->current_mem_ctx
, inet_ntoa(in
));
906 NDR_ERR_HAVE_NO_MEMORY(*address
);
907 return NDR_ERR_SUCCESS
;
913 _PUBLIC_
enum ndr_err_code
ndr_push_ipv4address(struct ndr_push
*ndr
, int ndr_flags
, const char *address
)
916 if (!is_ipaddress(address
)) {
917 return ndr_push_error(ndr
, NDR_ERR_IPV4ADDRESS
,
918 "Invalid IPv4 address: '%s'",
921 addr
= inet_addr(address
);
922 NDR_CHECK(ndr_push_uint32(ndr
, ndr_flags
, htonl(addr
)));
923 return NDR_ERR_SUCCESS
;
929 _PUBLIC_
void ndr_print_ipv4address(struct ndr_print
*ndr
, const char *name
,
932 ndr
->print(ndr
, "%-25s: %s", name
, address
);
938 #define IPV6_BYTES 16
939 #define IPV6_ADDR_STR_LEN 39
940 _PUBLIC_
enum ndr_err_code
ndr_pull_ipv6address(struct ndr_pull
*ndr
, int ndr_flags
, const char **address
)
942 uint8_t addr
[IPV6_BYTES
];
943 char *addr_str
= talloc_strdup(ndr
->current_mem_ctx
, "");
945 NDR_CHECK(ndr_pull_array_uint8(ndr
, ndr_flags
, addr
, IPV6_BYTES
));
946 for (i
= 0; i
< IPV6_BYTES
; ++i
) {
947 addr_str
= talloc_asprintf_append(addr_str
, "%02x", addr
[i
]);
948 /* We need a ':' every second byte but the last one */
949 if (i
%2 == 1 && i
!= (IPV6_BYTES
- 1)) {
950 addr_str
= talloc_strdup_append(addr_str
, ":");
954 NDR_ERR_HAVE_NO_MEMORY(*address
);
955 return NDR_ERR_SUCCESS
;
961 _PUBLIC_
enum ndr_err_code
ndr_push_ipv6address(struct ndr_push
*ndr
, int ndr_flags
, const char *address
)
964 uint8_t addr
[IPV6_BYTES
];
967 if (!is_ipaddress(address
)) {
968 return ndr_push_error(ndr
, NDR_ERR_IPV6ADDRESS
,
969 "Invalid IPv6 address: '%s'",
972 ret
= inet_pton(AF_INET6
, address
, addr
);
974 return NDR_ERR_IPV6ADDRESS
;
977 NDR_CHECK(ndr_push_array_uint8(ndr
, ndr_flags
, addr
, IPV6_BYTES
));
979 return NDR_ERR_SUCCESS
;
981 return NDR_ERR_IPV6ADDRESS
;
988 _PUBLIC_
void ndr_print_ipv6address(struct ndr_print
*ndr
, const char *name
,
991 ndr
->print(ndr
, "%-25s: %s", name
, address
);
995 _PUBLIC_
void ndr_print_struct(struct ndr_print
*ndr
, const char *name
, const char *type
)
997 ndr
->print(ndr
, "%s: struct %s", name
, type
);
1000 _PUBLIC_
void ndr_print_null(struct ndr_print
*ndr
)
1002 ndr
->print(ndr
, "UNEXPECTED NULL POINTER");
1005 _PUBLIC_
void ndr_print_enum(struct ndr_print
*ndr
, const char *name
, const char *type
,
1006 const char *val
, uint32_t value
)
1008 if (ndr
->flags
& LIBNDR_PRINT_ARRAY_HEX
) {
1009 ndr
->print(ndr
, "%-25s: %s (0x%X)", name
, val
?val
:"UNKNOWN_ENUM_VALUE", value
);
1011 ndr
->print(ndr
, "%-25s: %s (%d)", name
, val
?val
:"UNKNOWN_ENUM_VALUE", value
);
1015 _PUBLIC_
void ndr_print_bitmap_flag(struct ndr_print
*ndr
, size_t size
, const char *flag_name
, uint32_t flag
, uint32_t value
)
1017 /* this is an attempt to support multi-bit bitmap masks */
1020 while (!(flag
& 1)) {
1025 ndr
->print(ndr
, " %d: %-25s", value
, flag_name
);
1027 ndr
->print(ndr
, "0x%02x: %-25s (%d)", value
, flag_name
, value
);
1031 _PUBLIC_
void ndr_print_int8(struct ndr_print
*ndr
, const char *name
, int8_t v
)
1033 ndr
->print(ndr
, "%-25s: %d", name
, v
);
1036 _PUBLIC_
void ndr_print_uint8(struct ndr_print
*ndr
, const char *name
, uint8_t v
)
1038 ndr
->print(ndr
, "%-25s: 0x%02x (%u)", name
, v
, v
);
1041 _PUBLIC_
void ndr_print_int16(struct ndr_print
*ndr
, const char *name
, int16_t v
)
1043 ndr
->print(ndr
, "%-25s: %d", name
, v
);
1046 _PUBLIC_
void ndr_print_uint16(struct ndr_print
*ndr
, const char *name
, uint16_t v
)
1048 ndr
->print(ndr
, "%-25s: 0x%04x (%u)", name
, v
, v
);
1051 _PUBLIC_
void ndr_print_int32(struct ndr_print
*ndr
, const char *name
, int32_t v
)
1053 ndr
->print(ndr
, "%-25s: %d", name
, v
);
1056 _PUBLIC_
void ndr_print_uint32(struct ndr_print
*ndr
, const char *name
, uint32_t v
)
1058 ndr
->print(ndr
, "%-25s: 0x%08x (%u)", name
, v
, v
);
1061 _PUBLIC_
void ndr_print_int3264(struct ndr_print
*ndr
, const char *name
, int32_t v
)
1063 ndr
->print(ndr
, "%-25s: %d", name
, v
);
1066 _PUBLIC_
void ndr_print_uint3264(struct ndr_print
*ndr
, const char *name
, uint32_t v
)
1068 ndr
->print(ndr
, "%-25s: 0x%08x (%u)", name
, v
, v
);
1071 _PUBLIC_
void ndr_print_udlong(struct ndr_print
*ndr
, const char *name
, uint64_t v
)
1073 ndr
->print(ndr
, "%-25s: 0x%016llx (%llu)", name
, (unsigned long long)v
, (unsigned long long)v
);
1076 _PUBLIC_
void ndr_print_udlongr(struct ndr_print
*ndr
, const char *name
, uint64_t v
)
1078 ndr_print_udlong(ndr
, name
, v
);
1081 _PUBLIC_
void ndr_print_dlong(struct ndr_print
*ndr
, const char *name
, int64_t v
)
1083 ndr
->print(ndr
, "%-25s: 0x%016llx (%lld)", name
, (unsigned long long)v
, (long long)v
);
1086 _PUBLIC_
void ndr_print_double(struct ndr_print
*ndr
, const char *name
, double v
)
1088 ndr
->print(ndr
, "%-25s: %f", name
, v
);
1091 _PUBLIC_
void ndr_print_hyper(struct ndr_print
*ndr
, const char *name
, uint64_t v
)
1093 ndr_print_dlong(ndr
, name
, v
);
1096 _PUBLIC_
void ndr_print_pointer(struct ndr_print
*ndr
, const char *name
, void *v
)
1098 ndr
->print(ndr
, "%-25s: %p", name
, v
);
1101 _PUBLIC_
void ndr_print_ptr(struct ndr_print
*ndr
, const char *name
, const void *p
)
1104 ndr
->print(ndr
, "%-25s: *", name
);
1106 ndr
->print(ndr
, "%-25s: NULL", name
);
1110 _PUBLIC_
void ndr_print_NTTIME(struct ndr_print
*ndr
, const char *name
, NTTIME t
)
1112 ndr
->print(ndr
, "%-25s: %s", name
, nt_time_string(ndr
, t
));
1115 _PUBLIC_
void ndr_print_NTTIME_1sec(struct ndr_print
*ndr
, const char *name
, NTTIME t
)
1117 /* this is a standard NTTIME here
1118 * as it's already converted in the pull/push code
1120 ndr_print_NTTIME(ndr
, name
, t
);
1123 _PUBLIC_
void ndr_print_NTTIME_hyper(struct ndr_print
*ndr
, const char *name
, NTTIME t
)
1125 ndr_print_NTTIME(ndr
, name
, t
);
1128 _PUBLIC_
void ndr_print_time_t(struct ndr_print
*ndr
, const char *name
, time_t t
)
1130 if (t
== (time_t)-1 || t
== 0) {
1131 ndr
->print(ndr
, "%-25s: (time_t)%d", name
, (int)t
);
1133 ndr
->print(ndr
, "%-25s: %s", name
, timestring(ndr
, t
));
1137 _PUBLIC_
void ndr_print_uid_t(struct ndr_print
*ndr
, const char *name
, uid_t u
)
1139 ndr_print_dlong(ndr
, name
, u
);
1142 _PUBLIC_
void ndr_print_gid_t(struct ndr_print
*ndr
, const char *name
, gid_t g
)
1144 ndr_print_dlong(ndr
, name
, g
);
1147 _PUBLIC_
void ndr_print_union(struct ndr_print
*ndr
, const char *name
, int level
, const char *type
)
1149 if (ndr
->flags
& LIBNDR_PRINT_ARRAY_HEX
) {
1150 ndr
->print(ndr
, "%-25s: union %s(case 0x%X)", name
, type
, level
);
1152 ndr
->print(ndr
, "%-25s: union %s(case %d)", name
, type
, level
);
1156 _PUBLIC_
void ndr_print_bad_level(struct ndr_print
*ndr
, const char *name
, uint16_t level
)
1158 ndr
->print(ndr
, "UNKNOWN LEVEL %u", level
);
1161 _PUBLIC_
void ndr_print_array_uint8(struct ndr_print
*ndr
, const char *name
,
1162 const uint8_t *data
, uint32_t count
)
1167 ndr
->print(ndr
, "%s: ARRAY(%d) : NULL", name
, count
);
1171 if (count
<= 600 && (ndr
->flags
& LIBNDR_PRINT_ARRAY_HEX
)) {
1173 for (i
=0;i
<count
;i
++) {
1174 snprintf(&s
[i
*2], 3, "%02x", data
[i
]);
1177 ndr
->print(ndr
, "%-25s: %s", name
, s
);
1181 ndr
->print(ndr
, "%s: ARRAY(%d)", name
, count
);
1183 for (i
=0;i
<count
;i
++) {
1185 if (asprintf(&idx
, "[%d]", i
) != -1) {
1186 ndr_print_uint8(ndr
, idx
, data
[i
]);
1193 static void ndr_print_asc(struct ndr_print
*ndr
, const uint8_t *buf
, int len
)
1197 ndr
->print(ndr
, "%c", isprint(buf
[i
])?buf
[i
]:'.');
1201 ndr_print version of dump_data()
1203 static void ndr_dump_data(struct ndr_print
*ndr
, const uint8_t *buf
, int len
)
1207 ndr
->no_newline
= true;
1210 if (i
%16 == 0 && i
<len
) {
1211 ndr
->print(ndr
, "[%04X] ",i
);
1214 ndr
->print(ndr
, "%02X ",(int)buf
[i
]);
1216 if (i
%8 == 0) ndr
->print(ndr
," ");
1218 ndr_print_asc(ndr
,&buf
[i
-16],8); ndr
->print(ndr
," ");
1219 ndr_print_asc(ndr
,&buf
[i
-8],8); ndr
->print(ndr
, "\n");
1226 ndr
->print(ndr
, " ");
1227 if (n
>8) ndr
->print(ndr
," ");
1228 while (n
--) ndr
->print(ndr
," ");
1230 ndr_print_asc(ndr
,&buf
[i
-(i
%16)],n
); ndr
->print(ndr
, " ");
1232 if (n
>0) ndr_print_asc(ndr
,&buf
[i
-n
],n
);
1233 ndr
->print(ndr
,"\n");
1236 ndr
->no_newline
= false;
1240 _PUBLIC_
void ndr_print_DATA_BLOB(struct ndr_print
*ndr
, const char *name
, DATA_BLOB r
)
1242 ndr
->print(ndr
, "%-25s: DATA_BLOB length=%u", name
, (unsigned)r
.length
);
1244 ndr_dump_data(ndr
, r
.data
, r
.length
);
1250 push a DATA_BLOB onto the wire.
1252 _PUBLIC_
enum ndr_err_code
ndr_push_DATA_BLOB(struct ndr_push
*ndr
, int ndr_flags
, DATA_BLOB blob
)
1254 if (ndr
->flags
& LIBNDR_FLAG_REMAINING
) {
1256 } else if (ndr
->flags
& LIBNDR_ALIGN_FLAGS
) {
1257 if (ndr
->flags
& LIBNDR_FLAG_NOALIGN
) {
1259 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
1260 blob
.length
= NDR_ALIGN(ndr
, 2);
1261 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
1262 blob
.length
= NDR_ALIGN(ndr
, 4);
1263 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
1264 blob
.length
= NDR_ALIGN(ndr
, 8);
1266 NDR_PUSH_ALLOC_SIZE(ndr
, blob
.data
, blob
.length
);
1267 data_blob_clear(&blob
);
1269 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, blob
.length
));
1271 NDR_CHECK(ndr_push_bytes(ndr
, blob
.data
, blob
.length
));
1272 return NDR_ERR_SUCCESS
;
1276 pull a DATA_BLOB from the wire.
1278 _PUBLIC_
enum ndr_err_code
ndr_pull_DATA_BLOB(struct ndr_pull
*ndr
, int ndr_flags
, DATA_BLOB
*blob
)
1280 uint32_t length
= 0;
1282 if (ndr
->flags
& LIBNDR_FLAG_REMAINING
) {
1283 length
= ndr
->data_size
- ndr
->offset
;
1284 } else if (ndr
->flags
& LIBNDR_ALIGN_FLAGS
) {
1285 if (ndr
->flags
& LIBNDR_FLAG_NOALIGN
) {
1287 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
1288 length
= NDR_ALIGN(ndr
, 2);
1289 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
1290 length
= NDR_ALIGN(ndr
, 4);
1291 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
1292 length
= NDR_ALIGN(ndr
, 8);
1294 if (ndr
->data_size
- ndr
->offset
< length
) {
1295 length
= ndr
->data_size
- ndr
->offset
;
1298 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &length
));
1300 NDR_PULL_NEED_BYTES(ndr
, length
);
1301 *blob
= data_blob_talloc(ndr
->current_mem_ctx
, ndr
->data
+ndr
->offset
, length
);
1302 ndr
->offset
+= length
;
1303 return NDR_ERR_SUCCESS
;
1306 _PUBLIC_
uint32_t ndr_size_DATA_BLOB(int ret
, const DATA_BLOB
*data
, int flags
)
1308 if (!data
) return ret
;
1309 return ret
+ data
->length
;
1312 _PUBLIC_
void ndr_print_bool(struct ndr_print
*ndr
, const char *name
, const bool b
)
1314 ndr
->print(ndr
, "%-25s: %s", name
, b
?"true":"false");
1317 _PUBLIC_ NTSTATUS
ndr_map_error2ntstatus(enum ndr_err_code ndr_err
)
1320 case NDR_ERR_SUCCESS
:
1321 return NT_STATUS_OK
;
1322 case NDR_ERR_BUFSIZE
:
1323 return NT_STATUS_BUFFER_TOO_SMALL
;
1325 return NT_STATUS_INTERNAL_ERROR
;
1327 return NT_STATUS_NO_MEMORY
;
1328 case NDR_ERR_ARRAY_SIZE
:
1329 return NT_STATUS_ARRAY_BOUNDS_EXCEEDED
;
1330 case NDR_ERR_INVALID_POINTER
:
1331 return NT_STATUS_INVALID_PARAMETER_MIX
;
1332 case NDR_ERR_UNREAD_BYTES
:
1333 return NT_STATUS_PORT_MESSAGE_TOO_LONG
;
1338 /* we should map all error codes to different status codes */
1339 return NT_STATUS_INVALID_PARAMETER
;