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_NEED_BYTES(ndr
, 1);
65 *v
= (int8_t)CVAL(ndr
->data
, ndr
->offset
);
67 return NDR_ERR_SUCCESS
;
73 _PUBLIC_
enum ndr_err_code
ndr_pull_uint8(struct ndr_pull
*ndr
, int ndr_flags
, uint8_t *v
)
75 NDR_PULL_NEED_BYTES(ndr
, 1);
76 *v
= CVAL(ndr
->data
, ndr
->offset
);
78 return NDR_ERR_SUCCESS
;
84 _PUBLIC_
enum ndr_err_code
ndr_pull_int16(struct ndr_pull
*ndr
, int ndr_flags
, int16_t *v
)
86 NDR_PULL_ALIGN(ndr
, 2);
87 NDR_PULL_NEED_BYTES(ndr
, 2);
88 *v
= (uint16_t)NDR_SVAL(ndr
, ndr
->offset
);
90 return NDR_ERR_SUCCESS
;
96 _PUBLIC_
enum ndr_err_code
ndr_pull_uint16(struct ndr_pull
*ndr
, int ndr_flags
, uint16_t *v
)
98 NDR_PULL_ALIGN(ndr
, 2);
99 NDR_PULL_NEED_BYTES(ndr
, 2);
100 *v
= NDR_SVAL(ndr
, ndr
->offset
);
102 return NDR_ERR_SUCCESS
;
108 _PUBLIC_
enum ndr_err_code
ndr_pull_uint1632(struct ndr_pull
*ndr
, int ndr_flags
, uint16_t *v
)
110 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
112 enum ndr_err_code err
= ndr_pull_uint32(ndr
, ndr_flags
, &v32
);
114 if (unlikely(v32
!= *v
)) {
115 DEBUG(0,(__location__
": non-zero upper 16 bits 0x%08x\n", (unsigned)v32
));
116 return NDR_ERR_NDR64
;
120 return ndr_pull_uint16(ndr
, ndr_flags
, v
);
126 _PUBLIC_
enum ndr_err_code
ndr_pull_int32(struct ndr_pull
*ndr
, int ndr_flags
, int32_t *v
)
128 NDR_PULL_ALIGN(ndr
, 4);
129 NDR_PULL_NEED_BYTES(ndr
, 4);
130 *v
= NDR_IVALS(ndr
, ndr
->offset
);
132 return NDR_ERR_SUCCESS
;
138 _PUBLIC_
enum ndr_err_code
ndr_pull_uint32(struct ndr_pull
*ndr
, int ndr_flags
, uint32_t *v
)
140 NDR_PULL_ALIGN(ndr
, 4);
141 NDR_PULL_NEED_BYTES(ndr
, 4);
142 *v
= NDR_IVAL(ndr
, ndr
->offset
);
144 return NDR_ERR_SUCCESS
;
148 parse a arch dependent uint32/uint64
150 _PUBLIC_
enum ndr_err_code
ndr_pull_uint3264(struct ndr_pull
*ndr
, int ndr_flags
, uint32_t *v
)
153 enum ndr_err_code err
;
154 if (likely(!(ndr
->flags
& LIBNDR_FLAG_NDR64
))) {
155 return ndr_pull_uint32(ndr
, ndr_flags
, v
);
157 err
= ndr_pull_hyper(ndr
, ndr_flags
, &v64
);
159 if (unlikely(v64
!= *v
)) {
160 DEBUG(0,(__location__
": non-zero upper 32 bits 0x%016llx\n",
161 (unsigned long long)v64
));
162 return NDR_ERR_NDR64
;
170 _PUBLIC_
enum ndr_err_code
ndr_pull_double(struct ndr_pull
*ndr
, int ndr_flags
, double *v
)
172 NDR_PULL_ALIGN(ndr
, 8);
173 NDR_PULL_NEED_BYTES(ndr
, 8);
174 memcpy(v
, ndr
->data
+ndr
->offset
, 8);
176 return NDR_ERR_SUCCESS
;
180 parse a pointer referent identifier stored in 2 bytes
182 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr_short(struct ndr_pull
*ndr
, uint16_t *v
)
184 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, v
));
188 *(v
) -= ndr
->relative_rap_convert
;
189 return NDR_ERR_SUCCESS
;
193 parse a pointer referent identifier
195 _PUBLIC_
enum ndr_err_code
ndr_pull_generic_ptr(struct ndr_pull
*ndr
, uint32_t *v
)
197 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, v
));
201 return NDR_ERR_SUCCESS
;
205 parse a ref pointer referent identifier
207 _PUBLIC_
enum ndr_err_code
ndr_pull_ref_ptr(struct ndr_pull
*ndr
, uint32_t *v
)
209 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, v
));
210 /* ref pointers always point to data */
212 return NDR_ERR_SUCCESS
;
218 _PUBLIC_
enum ndr_err_code
ndr_pull_udlong(struct ndr_pull
*ndr
, int ndr_flags
, uint64_t *v
)
220 NDR_PULL_ALIGN(ndr
, 4);
221 NDR_PULL_NEED_BYTES(ndr
, 8);
222 *v
= NDR_IVAL(ndr
, ndr
->offset
);
223 *v
|= (uint64_t)(NDR_IVAL(ndr
, ndr
->offset
+4)) << 32;
225 return NDR_ERR_SUCCESS
;
231 _PUBLIC_
enum ndr_err_code
ndr_pull_udlongr(struct ndr_pull
*ndr
, int ndr_flags
, uint64_t *v
)
233 NDR_PULL_ALIGN(ndr
, 4);
234 NDR_PULL_NEED_BYTES(ndr
, 8);
235 *v
= ((uint64_t)NDR_IVAL(ndr
, ndr
->offset
)) << 32;
236 *v
|= NDR_IVAL(ndr
, ndr
->offset
+4);
238 return NDR_ERR_SUCCESS
;
244 _PUBLIC_
enum ndr_err_code
ndr_pull_dlong(struct ndr_pull
*ndr
, int ndr_flags
, int64_t *v
)
246 return ndr_pull_udlong(ndr
, ndr_flags
, (uint64_t *)v
);
252 _PUBLIC_
enum ndr_err_code
ndr_pull_hyper(struct ndr_pull
*ndr
, int ndr_flags
, uint64_t *v
)
254 NDR_PULL_ALIGN(ndr
, 8);
256 return ndr_pull_udlongr(ndr
, ndr_flags
, v
);
258 return ndr_pull_udlong(ndr
, ndr_flags
, v
);
264 _PUBLIC_
enum ndr_err_code
ndr_pull_pointer(struct ndr_pull
*ndr
, int ndr_flags
, void* *v
)
267 NDR_PULL_ALIGN(ndr
, sizeof(h
));
268 NDR_PULL_NEED_BYTES(ndr
, sizeof(h
));
269 memcpy(&h
, ndr
->data
+ndr
->offset
, sizeof(h
));
270 ndr
->offset
+= sizeof(h
);
272 return NDR_ERR_SUCCESS
;
278 _PUBLIC_
enum ndr_err_code
ndr_pull_NTSTATUS(struct ndr_pull
*ndr
, int ndr_flags
, NTSTATUS
*status
)
281 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &v
));
282 *status
= NT_STATUS(v
);
283 return NDR_ERR_SUCCESS
;
289 _PUBLIC_
enum ndr_err_code
ndr_push_NTSTATUS(struct ndr_push
*ndr
, int ndr_flags
, NTSTATUS status
)
291 return ndr_push_uint32(ndr
, ndr_flags
, NT_STATUS_V(status
));
294 _PUBLIC_
void ndr_print_NTSTATUS(struct ndr_print
*ndr
, const char *name
, NTSTATUS r
)
296 ndr
->print(ndr
, "%-25s: %s", name
, nt_errstr(r
));
302 _PUBLIC_
enum ndr_err_code
ndr_pull_WERROR(struct ndr_pull
*ndr
, int ndr_flags
, WERROR
*status
)
305 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &v
));
306 *status
= W_ERROR(v
);
307 return NDR_ERR_SUCCESS
;
314 _PUBLIC_
enum ndr_err_code
ndr_pull_enum_uint8(struct ndr_pull
*ndr
, int ndr_flags
, uint8_t *v
)
316 return ndr_pull_uint8(ndr
, ndr_flags
, v
);
320 parse a uint16_t enum
322 _PUBLIC_
enum ndr_err_code
ndr_pull_enum_uint16(struct ndr_pull
*ndr
, int ndr_flags
, uint16_t *v
)
324 return ndr_pull_uint16(ndr
, ndr_flags
, v
);
328 parse a uint1632_t enum (uint32_t on NDR64)
330 _PUBLIC_
enum ndr_err_code
ndr_pull_enum_uint1632(struct ndr_pull
*ndr
, int ndr_flags
, uint16_t *v
)
332 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
334 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &v32
));
337 DEBUG(0,(__location__
": non-zero upper 16 bits 0x%08x\n", (unsigned)v32
));
338 return NDR_ERR_NDR64
;
340 return NDR_ERR_SUCCESS
;
342 return ndr_pull_uint16(ndr
, ndr_flags
, v
);
346 parse a uint32_t enum
348 _PUBLIC_
enum ndr_err_code
ndr_pull_enum_uint32(struct ndr_pull
*ndr
, int ndr_flags
, uint32_t *v
)
350 return ndr_pull_uint32(ndr
, ndr_flags
, v
);
356 _PUBLIC_
enum ndr_err_code
ndr_push_enum_uint8(struct ndr_push
*ndr
, int ndr_flags
, uint8_t v
)
358 return ndr_push_uint8(ndr
, ndr_flags
, v
);
364 _PUBLIC_
enum ndr_err_code
ndr_push_enum_uint16(struct ndr_push
*ndr
, int ndr_flags
, uint16_t v
)
366 return ndr_push_uint16(ndr
, ndr_flags
, v
);
372 _PUBLIC_
enum ndr_err_code
ndr_push_enum_uint32(struct ndr_push
*ndr
, int ndr_flags
, uint32_t v
)
374 return ndr_push_uint32(ndr
, ndr_flags
, v
);
378 push a uint1632_t enum
380 _PUBLIC_
enum ndr_err_code
ndr_push_enum_uint1632(struct ndr_push
*ndr
, int ndr_flags
, uint16_t v
)
382 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
383 return ndr_push_uint32(ndr
, ndr_flags
, v
);
385 return ndr_push_uint16(ndr
, ndr_flags
, v
);
391 _PUBLIC_
enum ndr_err_code
ndr_push_WERROR(struct ndr_push
*ndr
, int ndr_flags
, WERROR status
)
393 return ndr_push_uint32(ndr
, NDR_SCALARS
, W_ERROR_V(status
));
396 _PUBLIC_
void ndr_print_WERROR(struct ndr_print
*ndr
, const char *name
, WERROR r
)
398 ndr
->print(ndr
, "%-25s: %s", name
, win_errstr(r
));
404 _PUBLIC_
enum ndr_err_code
ndr_pull_bytes(struct ndr_pull
*ndr
, uint8_t *data
, uint32_t n
)
406 NDR_PULL_NEED_BYTES(ndr
, n
);
407 memcpy(data
, ndr
->data
+ ndr
->offset
, n
);
409 return NDR_ERR_SUCCESS
;
413 pull an array of uint8
415 _PUBLIC_
enum ndr_err_code
ndr_pull_array_uint8(struct ndr_pull
*ndr
, int ndr_flags
, uint8_t *data
, uint32_t n
)
417 if (!(ndr_flags
& NDR_SCALARS
)) {
418 return NDR_ERR_SUCCESS
;
420 return ndr_pull_bytes(ndr
, data
, n
);
426 _PUBLIC_
enum ndr_err_code
ndr_push_int8(struct ndr_push
*ndr
, int ndr_flags
, int8_t v
)
428 NDR_PUSH_NEED_BYTES(ndr
, 1);
429 SCVAL(ndr
->data
, ndr
->offset
, (uint8_t)v
);
431 return NDR_ERR_SUCCESS
;
437 _PUBLIC_
enum ndr_err_code
ndr_push_uint8(struct ndr_push
*ndr
, int ndr_flags
, uint8_t v
)
439 NDR_PUSH_NEED_BYTES(ndr
, 1);
440 SCVAL(ndr
->data
, ndr
->offset
, v
);
442 return NDR_ERR_SUCCESS
;
448 _PUBLIC_
enum ndr_err_code
ndr_push_int16(struct ndr_push
*ndr
, int ndr_flags
, int16_t v
)
450 NDR_PUSH_ALIGN(ndr
, 2);
451 NDR_PUSH_NEED_BYTES(ndr
, 2);
452 NDR_SSVAL(ndr
, ndr
->offset
, (uint16_t)v
);
454 return NDR_ERR_SUCCESS
;
460 _PUBLIC_
enum ndr_err_code
ndr_push_uint16(struct ndr_push
*ndr
, int ndr_flags
, uint16_t v
)
462 NDR_PUSH_ALIGN(ndr
, 2);
463 NDR_PUSH_NEED_BYTES(ndr
, 2);
464 NDR_SSVAL(ndr
, ndr
->offset
, v
);
466 return NDR_ERR_SUCCESS
;
472 _PUBLIC_
enum ndr_err_code
ndr_push_uint1632(struct ndr_push
*ndr
, int ndr_flags
, uint16_t v
)
474 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
475 return ndr_push_uint32(ndr
, ndr_flags
, v
);
477 return ndr_push_uint16(ndr
, ndr_flags
, v
);
483 _PUBLIC_
enum ndr_err_code
ndr_push_int32(struct ndr_push
*ndr
, int ndr_flags
, int32_t v
)
485 NDR_PUSH_ALIGN(ndr
, 4);
486 NDR_PUSH_NEED_BYTES(ndr
, 4);
487 NDR_SIVALS(ndr
, ndr
->offset
, v
);
489 return NDR_ERR_SUCCESS
;
495 _PUBLIC_
enum ndr_err_code
ndr_push_uint32(struct ndr_push
*ndr
, int ndr_flags
, uint32_t v
)
497 NDR_PUSH_ALIGN(ndr
, 4);
498 NDR_PUSH_NEED_BYTES(ndr
, 4);
499 NDR_SIVAL(ndr
, ndr
->offset
, v
);
501 return NDR_ERR_SUCCESS
;
507 _PUBLIC_
enum ndr_err_code
ndr_push_uint3264(struct ndr_push
*ndr
, int ndr_flags
, uint32_t v
)
509 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
510 return ndr_push_hyper(ndr
, ndr_flags
, v
);
512 return ndr_push_uint32(ndr
, ndr_flags
, v
);
518 _PUBLIC_
enum ndr_err_code
ndr_push_udlong(struct ndr_push
*ndr
, int ndr_flags
, uint64_t v
)
520 NDR_PUSH_ALIGN(ndr
, 4);
521 NDR_PUSH_NEED_BYTES(ndr
, 8);
522 NDR_SIVAL(ndr
, ndr
->offset
, (v
& 0xFFFFFFFF));
523 NDR_SIVAL(ndr
, ndr
->offset
+4, (v
>>32));
525 return NDR_ERR_SUCCESS
;
531 _PUBLIC_
enum ndr_err_code
ndr_push_udlongr(struct ndr_push
*ndr
, int ndr_flags
, uint64_t v
)
533 NDR_PUSH_ALIGN(ndr
, 4);
534 NDR_PUSH_NEED_BYTES(ndr
, 8);
535 NDR_SIVAL(ndr
, ndr
->offset
, (v
>>32));
536 NDR_SIVAL(ndr
, ndr
->offset
+4, (v
& 0xFFFFFFFF));
538 return NDR_ERR_SUCCESS
;
544 _PUBLIC_
enum ndr_err_code
ndr_push_dlong(struct ndr_push
*ndr
, int ndr_flags
, int64_t v
)
546 return ndr_push_udlong(ndr
, NDR_SCALARS
, (uint64_t)v
);
552 _PUBLIC_
enum ndr_err_code
ndr_push_hyper(struct ndr_push
*ndr
, int ndr_flags
, uint64_t v
)
554 NDR_PUSH_ALIGN(ndr
, 8);
556 return ndr_push_udlongr(ndr
, NDR_SCALARS
, v
);
558 return ndr_push_udlong(ndr
, NDR_SCALARS
, v
);
564 _PUBLIC_
enum ndr_err_code
ndr_push_double(struct ndr_push
*ndr
, int ndr_flags
, double v
)
566 NDR_PUSH_ALIGN(ndr
, 8);
567 NDR_PUSH_NEED_BYTES(ndr
, 8);
568 memcpy(ndr
->data
+ndr
->offset
, &v
, 8);
570 return NDR_ERR_SUCCESS
;
576 _PUBLIC_
enum ndr_err_code
ndr_push_pointer(struct ndr_push
*ndr
, int ndr_flags
, void* v
)
578 uintptr_t h
= (intptr_t)v
;
579 NDR_PUSH_ALIGN(ndr
, sizeof(h
));
580 NDR_PUSH_NEED_BYTES(ndr
, sizeof(h
));
581 memcpy(ndr
->data
+ndr
->offset
, &h
, sizeof(h
));
582 ndr
->offset
+= sizeof(h
);
583 return NDR_ERR_SUCCESS
;
586 _PUBLIC_
enum ndr_err_code
ndr_push_align(struct ndr_push
*ndr
, size_t size
)
588 /* this is a nasty hack to make pidl work with NDR64 */
590 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
595 } else if (size
== 3) {
596 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
602 NDR_PUSH_ALIGN(ndr
, size
);
603 return NDR_ERR_SUCCESS
;
606 _PUBLIC_
enum ndr_err_code
ndr_pull_align(struct ndr_pull
*ndr
, size_t size
)
608 /* this is a nasty hack to make pidl work with NDR64 */
610 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
615 } else if (size
== 3) {
616 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
622 NDR_PULL_ALIGN(ndr
, size
);
623 return NDR_ERR_SUCCESS
;
626 _PUBLIC_
enum ndr_err_code
ndr_push_union_align(struct ndr_push
*ndr
, size_t size
)
628 /* MS-RPCE section 2.2.5.3.4.4 */
629 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
630 return ndr_push_align(ndr
, size
);
632 return NDR_ERR_SUCCESS
;
635 _PUBLIC_
enum ndr_err_code
ndr_pull_union_align(struct ndr_pull
*ndr
, size_t size
)
637 /* MS-RPCE section 2.2.5.3.4.4 */
638 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
639 return ndr_pull_align(ndr
, size
);
641 return NDR_ERR_SUCCESS
;
644 _PUBLIC_
enum ndr_err_code
ndr_push_trailer_align(struct ndr_push
*ndr
, size_t size
)
646 /* MS-RPCE section 2.2.5.3.4.1 */
647 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
648 return ndr_push_align(ndr
, size
);
650 return NDR_ERR_SUCCESS
;
653 _PUBLIC_
enum ndr_err_code
ndr_pull_trailer_align(struct ndr_pull
*ndr
, size_t size
)
655 /* MS-RPCE section 2.2.5.3.4.1 */
656 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
657 return ndr_pull_align(ndr
, size
);
659 return NDR_ERR_SUCCESS
;
665 _PUBLIC_
enum ndr_err_code
ndr_push_bytes(struct ndr_push
*ndr
, const uint8_t *data
, uint32_t n
)
667 NDR_PUSH_NEED_BYTES(ndr
, n
);
668 memcpy(ndr
->data
+ ndr
->offset
, data
, n
);
670 return NDR_ERR_SUCCESS
;
676 _PUBLIC_
enum ndr_err_code
ndr_push_zero(struct ndr_push
*ndr
, uint32_t n
)
678 NDR_PUSH_NEED_BYTES(ndr
, n
);
679 memset(ndr
->data
+ ndr
->offset
, 0, n
);
681 return NDR_ERR_SUCCESS
;
685 push an array of uint8
687 _PUBLIC_
enum ndr_err_code
ndr_push_array_uint8(struct ndr_push
*ndr
, int ndr_flags
, const uint8_t *data
, uint32_t n
)
689 if (!(ndr_flags
& NDR_SCALARS
)) {
690 return NDR_ERR_SUCCESS
;
692 return ndr_push_bytes(ndr
, data
, n
);
696 push a unique non-zero value if a pointer is non-NULL, otherwise 0
698 _PUBLIC_
enum ndr_err_code
ndr_push_unique_ptr(struct ndr_push
*ndr
, const void *p
)
702 ptr
= ndr
->ptr_count
* 4;
706 return ndr_push_uint3264(ndr
, NDR_SCALARS
, ptr
);
710 push a 'simple' full non-zero value if a pointer is non-NULL, otherwise 0
712 _PUBLIC_
enum ndr_err_code
ndr_push_full_ptr(struct ndr_push
*ndr
, const void *p
)
716 /* Check if the pointer already exists and has an id */
717 ptr
= ndr_token_peek(&ndr
->full_ptr_list
, p
);
720 ptr
= ndr
->ptr_count
;
721 ndr_token_store(ndr
, &ndr
->full_ptr_list
, p
, ptr
);
724 return ndr_push_uint3264(ndr
, NDR_SCALARS
, ptr
);
728 push always a 0, if a pointer is NULL it's a fatal error
730 _PUBLIC_
enum ndr_err_code
ndr_push_ref_ptr(struct ndr_push
*ndr
)
732 return ndr_push_uint3264(ndr
, NDR_SCALARS
, 0xAEF1AEF1);
739 _PUBLIC_
enum ndr_err_code
ndr_push_NTTIME(struct ndr_push
*ndr
, int ndr_flags
, NTTIME t
)
741 NDR_CHECK(ndr_push_udlong(ndr
, ndr_flags
, t
));
742 return NDR_ERR_SUCCESS
;
748 _PUBLIC_
enum ndr_err_code
ndr_pull_NTTIME(struct ndr_pull
*ndr
, int ndr_flags
, NTTIME
*t
)
750 NDR_CHECK(ndr_pull_udlong(ndr
, ndr_flags
, t
));
751 return NDR_ERR_SUCCESS
;
757 _PUBLIC_
enum ndr_err_code
ndr_push_NTTIME_1sec(struct ndr_push
*ndr
, int ndr_flags
, NTTIME t
)
760 NDR_CHECK(ndr_push_hyper(ndr
, ndr_flags
, t
));
761 return NDR_ERR_SUCCESS
;
767 _PUBLIC_
enum ndr_err_code
ndr_pull_NTTIME_1sec(struct ndr_pull
*ndr
, int ndr_flags
, NTTIME
*t
)
769 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, t
));
771 return NDR_ERR_SUCCESS
;
777 _PUBLIC_
enum ndr_err_code
ndr_pull_NTTIME_hyper(struct ndr_pull
*ndr
, int ndr_flags
, NTTIME
*t
)
779 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, t
));
780 return NDR_ERR_SUCCESS
;
786 _PUBLIC_
enum ndr_err_code
ndr_push_NTTIME_hyper(struct ndr_push
*ndr
, int ndr_flags
, NTTIME t
)
788 NDR_CHECK(ndr_push_hyper(ndr
, ndr_flags
, t
));
789 return NDR_ERR_SUCCESS
;
795 _PUBLIC_
enum ndr_err_code
ndr_push_time_t(struct ndr_push
*ndr
, int ndr_flags
, time_t t
)
797 return ndr_push_uint32(ndr
, ndr_flags
, t
);
803 _PUBLIC_
enum ndr_err_code
ndr_pull_time_t(struct ndr_pull
*ndr
, int ndr_flags
, time_t *t
)
806 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &tt
));
808 return NDR_ERR_SUCCESS
;
815 _PUBLIC_
enum ndr_err_code
ndr_push_uid_t(struct ndr_push
*ndr
, int ndr_flags
, uid_t u
)
817 return ndr_push_hyper(ndr
, NDR_SCALARS
, (uint64_t)u
);
823 _PUBLIC_
enum ndr_err_code
ndr_pull_uid_t(struct ndr_pull
*ndr
, int ndr_flags
, uid_t
*u
)
826 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, &uu
));
828 if (unlikely(uu
!= *u
)) {
829 DEBUG(0,(__location__
": uid_t pull doesn't fit 0x%016llx\n",
830 (unsigned long long)uu
));
831 return NDR_ERR_NDR64
;
833 return NDR_ERR_SUCCESS
;
840 _PUBLIC_
enum ndr_err_code
ndr_push_gid_t(struct ndr_push
*ndr
, int ndr_flags
, gid_t g
)
842 return ndr_push_hyper(ndr
, NDR_SCALARS
, (uint64_t)g
);
848 _PUBLIC_
enum ndr_err_code
ndr_pull_gid_t(struct ndr_pull
*ndr
, int ndr_flags
, gid_t
*g
)
851 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, &gg
));
853 if (unlikely(gg
!= *g
)) {
854 DEBUG(0,(__location__
": gid_t pull doesn't fit 0x%016llx\n",
855 (unsigned long long)gg
));
856 return NDR_ERR_NDR64
;
858 return NDR_ERR_SUCCESS
;
865 _PUBLIC_
enum ndr_err_code
ndr_pull_ipv4address(struct ndr_pull
*ndr
, int ndr_flags
, const char **address
)
869 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &addr
));
870 in
.s_addr
= htonl(addr
);
871 *address
= talloc_strdup(ndr
->current_mem_ctx
, inet_ntoa(in
));
872 NDR_ERR_HAVE_NO_MEMORY(*address
);
873 return NDR_ERR_SUCCESS
;
879 _PUBLIC_
enum ndr_err_code
ndr_push_ipv4address(struct ndr_push
*ndr
, int ndr_flags
, const char *address
)
882 if (!is_ipaddress(address
)) {
883 return ndr_push_error(ndr
, NDR_ERR_IPV4ADDRESS
,
884 "Invalid IPv4 address: '%s'",
887 addr
= inet_addr(address
);
888 NDR_CHECK(ndr_push_uint32(ndr
, ndr_flags
, htonl(addr
)));
889 return NDR_ERR_SUCCESS
;
895 _PUBLIC_
void ndr_print_ipv4address(struct ndr_print
*ndr
, const char *name
,
898 ndr
->print(ndr
, "%-25s: %s", name
, address
);
904 #define IPV6_BYTES 16
905 #define IPV6_ADDR_STR_LEN 39
906 _PUBLIC_
enum ndr_err_code
ndr_pull_ipv6address(struct ndr_pull
*ndr
, int ndr_flags
, const char **address
)
908 uint8_t addr
[IPV6_BYTES
];
909 char *addr_str
= talloc_strdup(ndr
->current_mem_ctx
, "");
911 NDR_CHECK(ndr_pull_array_uint8(ndr
, ndr_flags
, addr
, IPV6_BYTES
));
912 for (i
= 0; i
< IPV6_BYTES
; ++i
) {
913 addr_str
= talloc_asprintf_append(addr_str
, "%02x", addr
[i
]);
914 /* We need a ':' every second byte but the last one */
915 if (i
%2 == 1 && i
!= (IPV6_BYTES
- 1)) {
916 addr_str
= talloc_strdup_append(addr_str
, ":");
920 NDR_ERR_HAVE_NO_MEMORY(*address
);
921 return NDR_ERR_SUCCESS
;
927 _PUBLIC_
enum ndr_err_code
ndr_push_ipv6address(struct ndr_push
*ndr
, int ndr_flags
, const char *address
)
930 uint8_t addr
[IPV6_BYTES
];
933 if (!is_ipaddress(address
)) {
934 return ndr_push_error(ndr
, NDR_ERR_IPV6ADDRESS
,
935 "Invalid IPv6 address: '%s'",
938 ret
= inet_pton(AF_INET6
, address
, addr
);
940 return NDR_ERR_IPV6ADDRESS
;
943 NDR_CHECK(ndr_push_array_uint8(ndr
, ndr_flags
, addr
, IPV6_BYTES
));
945 return NDR_ERR_SUCCESS
;
947 return NDR_ERR_IPV6ADDRESS
;
954 _PUBLIC_
void ndr_print_ipv6address(struct ndr_print
*ndr
, const char *name
,
957 ndr
->print(ndr
, "%-25s: %s", name
, address
);
961 _PUBLIC_
void ndr_print_struct(struct ndr_print
*ndr
, const char *name
, const char *type
)
963 ndr
->print(ndr
, "%s: struct %s", name
, type
);
966 _PUBLIC_
void ndr_print_null(struct ndr_print
*ndr
)
968 ndr
->print(ndr
, "UNEXPECTED NULL POINTER");
971 _PUBLIC_
void ndr_print_enum(struct ndr_print
*ndr
, const char *name
, const char *type
,
972 const char *val
, uint32_t value
)
974 if (ndr
->flags
& LIBNDR_PRINT_ARRAY_HEX
) {
975 ndr
->print(ndr
, "%-25s: %s (0x%X)", name
, val
?val
:"UNKNOWN_ENUM_VALUE", value
);
977 ndr
->print(ndr
, "%-25s: %s (%d)", name
, val
?val
:"UNKNOWN_ENUM_VALUE", value
);
981 _PUBLIC_
void ndr_print_bitmap_flag(struct ndr_print
*ndr
, size_t size
, const char *flag_name
, uint32_t flag
, uint32_t value
)
983 /* this is an attempt to support multi-bit bitmap masks */
986 while (!(flag
& 1)) {
991 ndr
->print(ndr
, " %d: %-25s", value
, flag_name
);
993 ndr
->print(ndr
, "0x%02x: %-25s (%d)", value
, flag_name
, value
);
997 _PUBLIC_
void ndr_print_int8(struct ndr_print
*ndr
, const char *name
, int8_t v
)
999 ndr
->print(ndr
, "%-25s: %d", name
, v
);
1002 _PUBLIC_
void ndr_print_uint8(struct ndr_print
*ndr
, const char *name
, uint8_t v
)
1004 ndr
->print(ndr
, "%-25s: 0x%02x (%u)", name
, v
, v
);
1007 _PUBLIC_
void ndr_print_int16(struct ndr_print
*ndr
, const char *name
, int16_t v
)
1009 ndr
->print(ndr
, "%-25s: %d", name
, v
);
1012 _PUBLIC_
void ndr_print_uint16(struct ndr_print
*ndr
, const char *name
, uint16_t v
)
1014 ndr
->print(ndr
, "%-25s: 0x%04x (%u)", name
, v
, v
);
1017 _PUBLIC_
void ndr_print_int32(struct ndr_print
*ndr
, const char *name
, int32_t v
)
1019 ndr
->print(ndr
, "%-25s: %d", name
, v
);
1022 _PUBLIC_
void ndr_print_uint32(struct ndr_print
*ndr
, const char *name
, uint32_t v
)
1024 ndr
->print(ndr
, "%-25s: 0x%08x (%u)", name
, v
, v
);
1027 _PUBLIC_
void ndr_print_int3264(struct ndr_print
*ndr
, const char *name
, int32_t v
)
1029 ndr
->print(ndr
, "%-25s: %d", name
, v
);
1032 _PUBLIC_
void ndr_print_uint3264(struct ndr_print
*ndr
, const char *name
, uint32_t v
)
1034 ndr
->print(ndr
, "%-25s: 0x%08x (%u)", name
, v
, v
);
1037 _PUBLIC_
void ndr_print_udlong(struct ndr_print
*ndr
, const char *name
, uint64_t v
)
1039 ndr
->print(ndr
, "%-25s: 0x%016llx (%llu)", name
, (unsigned long long)v
, (unsigned long long)v
);
1042 _PUBLIC_
void ndr_print_udlongr(struct ndr_print
*ndr
, const char *name
, uint64_t v
)
1044 ndr_print_udlong(ndr
, name
, v
);
1047 _PUBLIC_
void ndr_print_dlong(struct ndr_print
*ndr
, const char *name
, int64_t v
)
1049 ndr
->print(ndr
, "%-25s: 0x%016llx (%lld)", name
, (unsigned long long)v
, (long long)v
);
1052 _PUBLIC_
void ndr_print_double(struct ndr_print
*ndr
, const char *name
, double v
)
1054 ndr
->print(ndr
, "%-25s: %f", name
, v
);
1057 _PUBLIC_
void ndr_print_hyper(struct ndr_print
*ndr
, const char *name
, uint64_t v
)
1059 ndr_print_dlong(ndr
, name
, v
);
1062 _PUBLIC_
void ndr_print_pointer(struct ndr_print
*ndr
, const char *name
, void *v
)
1064 ndr
->print(ndr
, "%-25s: %p", name
, v
);
1067 _PUBLIC_
void ndr_print_ptr(struct ndr_print
*ndr
, const char *name
, const void *p
)
1070 ndr
->print(ndr
, "%-25s: *", name
);
1072 ndr
->print(ndr
, "%-25s: NULL", name
);
1076 _PUBLIC_
void ndr_print_NTTIME(struct ndr_print
*ndr
, const char *name
, NTTIME t
)
1078 ndr
->print(ndr
, "%-25s: %s", name
, nt_time_string(ndr
, t
));
1081 _PUBLIC_
void ndr_print_NTTIME_1sec(struct ndr_print
*ndr
, const char *name
, NTTIME t
)
1083 /* this is a standard NTTIME here
1084 * as it's already converted in the pull/push code
1086 ndr_print_NTTIME(ndr
, name
, t
);
1089 _PUBLIC_
void ndr_print_NTTIME_hyper(struct ndr_print
*ndr
, const char *name
, NTTIME t
)
1091 ndr_print_NTTIME(ndr
, name
, t
);
1094 _PUBLIC_
void ndr_print_time_t(struct ndr_print
*ndr
, const char *name
, time_t t
)
1096 if (t
== (time_t)-1 || t
== 0) {
1097 ndr
->print(ndr
, "%-25s: (time_t)%d", name
, (int)t
);
1099 ndr
->print(ndr
, "%-25s: %s", name
, timestring(ndr
, t
));
1103 _PUBLIC_
void ndr_print_uid_t(struct ndr_print
*ndr
, const char *name
, uid_t u
)
1105 ndr_print_dlong(ndr
, name
, u
);
1108 _PUBLIC_
void ndr_print_gid_t(struct ndr_print
*ndr
, const char *name
, gid_t g
)
1110 ndr_print_dlong(ndr
, name
, g
);
1113 _PUBLIC_
void ndr_print_union(struct ndr_print
*ndr
, const char *name
, int level
, const char *type
)
1115 if (ndr
->flags
& LIBNDR_PRINT_ARRAY_HEX
) {
1116 ndr
->print(ndr
, "%-25s: union %s(case 0x%X)", name
, type
, level
);
1118 ndr
->print(ndr
, "%-25s: union %s(case %d)", name
, type
, level
);
1122 _PUBLIC_
void ndr_print_bad_level(struct ndr_print
*ndr
, const char *name
, uint16_t level
)
1124 ndr
->print(ndr
, "UNKNOWN LEVEL %u", level
);
1127 _PUBLIC_
void ndr_print_array_uint8(struct ndr_print
*ndr
, const char *name
,
1128 const uint8_t *data
, uint32_t count
)
1133 ndr
->print(ndr
, "%s: ARRAY(%d) : NULL", name
, count
);
1137 if (count
<= 600 && (ndr
->flags
& LIBNDR_PRINT_ARRAY_HEX
)) {
1139 for (i
=0;i
<count
;i
++) {
1140 snprintf(&s
[i
*2], 3, "%02x", data
[i
]);
1143 ndr
->print(ndr
, "%-25s: %s", name
, s
);
1147 ndr
->print(ndr
, "%s: ARRAY(%d)", name
, count
);
1149 for (i
=0;i
<count
;i
++) {
1151 if (asprintf(&idx
, "[%d]", i
) != -1) {
1152 ndr_print_uint8(ndr
, idx
, data
[i
]);
1159 static void ndr_print_asc(struct ndr_print
*ndr
, const uint8_t *buf
, int len
)
1163 ndr
->print(ndr
, "%c", isprint(buf
[i
])?buf
[i
]:'.');
1167 ndr_print version of dump_data()
1169 static void ndr_dump_data(struct ndr_print
*ndr
, const uint8_t *buf
, int len
)
1173 ndr
->no_newline
= true;
1176 if (i
%16 == 0 && i
<len
) {
1177 ndr
->print(ndr
, "[%04X] ",i
);
1180 ndr
->print(ndr
, "%02X ",(int)buf
[i
]);
1182 if (i
%8 == 0) ndr
->print(ndr
," ");
1184 ndr_print_asc(ndr
,&buf
[i
-16],8); ndr
->print(ndr
," ");
1185 ndr_print_asc(ndr
,&buf
[i
-8],8); ndr
->print(ndr
, "\n");
1192 ndr
->print(ndr
, " ");
1193 if (n
>8) ndr
->print(ndr
," ");
1194 while (n
--) ndr
->print(ndr
," ");
1196 ndr_print_asc(ndr
,&buf
[i
-(i
%16)],n
); ndr
->print(ndr
, " ");
1198 if (n
>0) ndr_print_asc(ndr
,&buf
[i
-n
],n
);
1199 ndr
->print(ndr
,"\n");
1202 ndr
->no_newline
= false;
1206 _PUBLIC_
void ndr_print_DATA_BLOB(struct ndr_print
*ndr
, const char *name
, DATA_BLOB r
)
1208 ndr
->print(ndr
, "%-25s: DATA_BLOB length=%u", name
, (unsigned)r
.length
);
1210 ndr_dump_data(ndr
, r
.data
, r
.length
);
1216 push a DATA_BLOB onto the wire.
1218 _PUBLIC_
enum ndr_err_code
ndr_push_DATA_BLOB(struct ndr_push
*ndr
, int ndr_flags
, DATA_BLOB blob
)
1220 if (ndr
->flags
& LIBNDR_FLAG_REMAINING
) {
1222 } else if (ndr
->flags
& LIBNDR_ALIGN_FLAGS
) {
1223 if (ndr
->flags
& LIBNDR_FLAG_NOALIGN
) {
1225 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
1226 blob
.length
= NDR_ALIGN(ndr
, 2);
1227 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
1228 blob
.length
= NDR_ALIGN(ndr
, 4);
1229 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
1230 blob
.length
= NDR_ALIGN(ndr
, 8);
1232 NDR_PUSH_ALLOC_SIZE(ndr
, blob
.data
, blob
.length
);
1233 data_blob_clear(&blob
);
1235 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, blob
.length
));
1237 NDR_CHECK(ndr_push_bytes(ndr
, blob
.data
, blob
.length
));
1238 return NDR_ERR_SUCCESS
;
1242 pull a DATA_BLOB from the wire.
1244 _PUBLIC_
enum ndr_err_code
ndr_pull_DATA_BLOB(struct ndr_pull
*ndr
, int ndr_flags
, DATA_BLOB
*blob
)
1246 uint32_t length
= 0;
1248 if (ndr
->flags
& LIBNDR_FLAG_REMAINING
) {
1249 length
= ndr
->data_size
- ndr
->offset
;
1250 } else if (ndr
->flags
& LIBNDR_ALIGN_FLAGS
) {
1251 if (ndr
->flags
& LIBNDR_FLAG_NOALIGN
) {
1253 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
1254 length
= NDR_ALIGN(ndr
, 2);
1255 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
1256 length
= NDR_ALIGN(ndr
, 4);
1257 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
1258 length
= NDR_ALIGN(ndr
, 8);
1260 if (ndr
->data_size
- ndr
->offset
< length
) {
1261 length
= ndr
->data_size
- ndr
->offset
;
1264 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &length
));
1266 NDR_PULL_NEED_BYTES(ndr
, length
);
1267 *blob
= data_blob_talloc(ndr
->current_mem_ctx
, ndr
->data
+ndr
->offset
, length
);
1268 ndr
->offset
+= length
;
1269 return NDR_ERR_SUCCESS
;
1272 _PUBLIC_
uint32_t ndr_size_DATA_BLOB(int ret
, const DATA_BLOB
*data
, int flags
)
1274 if (!data
) return ret
;
1275 return ret
+ data
->length
;
1278 _PUBLIC_
void ndr_print_bool(struct ndr_print
*ndr
, const char *name
, const bool b
)
1280 ndr
->print(ndr
, "%-25s: %s", name
, b
?"true":"false");
1283 _PUBLIC_ NTSTATUS
ndr_map_error2ntstatus(enum ndr_err_code ndr_err
)
1286 case NDR_ERR_SUCCESS
:
1287 return NT_STATUS_OK
;
1288 case NDR_ERR_BUFSIZE
:
1289 return NT_STATUS_BUFFER_TOO_SMALL
;
1291 return NT_STATUS_INTERNAL_ERROR
;
1293 return NT_STATUS_NO_MEMORY
;
1294 case NDR_ERR_ARRAY_SIZE
:
1295 return NT_STATUS_ARRAY_BOUNDS_EXCEEDED
;
1296 case NDR_ERR_INVALID_POINTER
:
1297 return NT_STATUS_INVALID_PARAMETER_MIX
;
1298 case NDR_ERR_UNREAD_BYTES
:
1299 return NT_STATUS_PORT_MESSAGE_TOO_LONG
;
1304 /* we should map all error codes to different status codes */
1305 return NT_STATUS_INVALID_PARAMETER
;