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"
29 #define NDR_SVAL(ndr, ofs) (NDR_BE(ndr)?RSVAL(ndr->data,ofs):SVAL(ndr->data,ofs))
30 #define NDR_IVAL(ndr, ofs) (NDR_BE(ndr)?RIVAL(ndr->data,ofs):IVAL(ndr->data,ofs))
31 #define NDR_IVALS(ndr, ofs) (NDR_BE(ndr)?RIVALS(ndr->data,ofs):IVALS(ndr->data,ofs))
32 #define NDR_SSVAL(ndr, ofs, v) do { if (NDR_BE(ndr)) { RSSVAL(ndr->data,ofs,v); } else SSVAL(ndr->data,ofs,v); } while (0)
33 #define NDR_SIVAL(ndr, ofs, v) do { if (NDR_BE(ndr)) { RSIVAL(ndr->data,ofs,v); } else SIVAL(ndr->data,ofs,v); } while (0)
34 #define NDR_SIVALS(ndr, ofs, v) do { if (NDR_BE(ndr)) { RSIVALS(ndr->data,ofs,v); } else SIVALS(ndr->data,ofs,v); } while (0)
37 static void ndr_dump_data(struct ndr_print
*ndr
, const uint8_t *buf
, int len
);
40 check for data leaks from the server by looking for non-zero pad bytes
41 these could also indicate that real structure elements have been
42 mistaken for padding in the IDL
44 _PUBLIC_
void ndr_check_padding(struct ndr_pull
*ndr
, size_t n
)
46 size_t ofs2
= (ndr
->offset
+ (n
-1)) & ~(n
-1);
48 for (i
=ndr
->offset
;i
<ofs2
;i
++) {
49 if (ndr
->data
[i
] != 0) {
54 DEBUG(0,("WARNING: Non-zero padding to %d: ", (int)n
));
55 for (i
=ndr
->offset
;i
<ofs2
;i
++) {
56 DEBUG(0,("%02x ", ndr
->data
[i
]));
66 _PUBLIC_
enum ndr_err_code
ndr_pull_int8(struct ndr_pull
*ndr
, int ndr_flags
, int8_t *v
)
68 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
69 NDR_PULL_NEED_BYTES(ndr
, 1);
70 *v
= (int8_t)CVAL(ndr
->data
, ndr
->offset
);
72 return NDR_ERR_SUCCESS
;
78 _PUBLIC_
enum ndr_err_code
ndr_pull_uint8(struct ndr_pull
*ndr
, int ndr_flags
, uint8_t *v
)
80 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
81 NDR_PULL_NEED_BYTES(ndr
, 1);
82 *v
= CVAL(ndr
->data
, ndr
->offset
);
84 return NDR_ERR_SUCCESS
;
90 _PUBLIC_
enum ndr_err_code
ndr_pull_int16(struct ndr_pull
*ndr
, int ndr_flags
, int16_t *v
)
92 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
93 NDR_PULL_ALIGN(ndr
, 2);
94 NDR_PULL_NEED_BYTES(ndr
, 2);
95 *v
= (uint16_t)NDR_SVAL(ndr
, ndr
->offset
);
97 return NDR_ERR_SUCCESS
;
103 _PUBLIC_
enum ndr_err_code
ndr_pull_uint16(struct ndr_pull
*ndr
, int ndr_flags
, uint16_t *v
)
105 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
106 NDR_PULL_ALIGN(ndr
, 2);
107 NDR_PULL_NEED_BYTES(ndr
, 2);
108 *v
= NDR_SVAL(ndr
, ndr
->offset
);
110 return NDR_ERR_SUCCESS
;
116 _PUBLIC_
enum ndr_err_code
ndr_pull_uint1632(struct ndr_pull
*ndr
, int ndr_flags
, uint16_t *v
)
118 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
119 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
121 enum ndr_err_code err
= ndr_pull_uint32(ndr
, ndr_flags
, &v32
);
123 if (unlikely(v32
!= *v
)) {
124 DEBUG(0,(__location__
": non-zero upper 16 bits 0x%08x\n", (unsigned)v32
));
125 return NDR_ERR_NDR64
;
129 return ndr_pull_uint16(ndr
, ndr_flags
, v
);
135 _PUBLIC_
enum ndr_err_code
ndr_pull_int32(struct ndr_pull
*ndr
, int ndr_flags
, int32_t *v
)
137 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
138 NDR_PULL_ALIGN(ndr
, 4);
139 NDR_PULL_NEED_BYTES(ndr
, 4);
140 *v
= NDR_IVALS(ndr
, ndr
->offset
);
142 return NDR_ERR_SUCCESS
;
148 _PUBLIC_
enum ndr_err_code
ndr_pull_uint32(struct ndr_pull
*ndr
, int ndr_flags
, uint32_t *v
)
150 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
151 NDR_PULL_ALIGN(ndr
, 4);
152 NDR_PULL_NEED_BYTES(ndr
, 4);
153 *v
= NDR_IVAL(ndr
, ndr
->offset
);
155 return NDR_ERR_SUCCESS
;
159 parse a arch dependent uint32/uint64
161 _PUBLIC_
enum ndr_err_code
ndr_pull_uint3264(struct ndr_pull
*ndr
, int ndr_flags
, uint32_t *v
)
164 enum ndr_err_code err
;
165 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
166 if (likely(!(ndr
->flags
& LIBNDR_FLAG_NDR64
))) {
167 return ndr_pull_uint32(ndr
, ndr_flags
, v
);
169 err
= ndr_pull_hyper(ndr
, ndr_flags
, &v64
);
170 if (!NDR_ERR_CODE_IS_SUCCESS(err
)) {
174 if (unlikely(v64
!= *v
)) {
175 DEBUG(0,(__location__
": non-zero upper 32 bits 0x%016llx\n",
176 (unsigned long long)v64
));
177 return ndr_pull_error(ndr
, NDR_ERR_NDR64
, __location__
": non-zero upper 32 bits 0x%016llx\n",
178 (unsigned long long)v64
);
186 _PUBLIC_
enum ndr_err_code
ndr_pull_double(struct ndr_pull
*ndr
, int ndr_flags
, double *v
)
188 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
189 NDR_PULL_ALIGN(ndr
, 8);
190 NDR_PULL_NEED_BYTES(ndr
, 8);
191 memcpy(v
, ndr
->data
+ndr
->offset
, 8);
193 return NDR_ERR_SUCCESS
;
197 parse a pointer referent identifier stored in 2 bytes
199 _PUBLIC_
enum ndr_err_code
ndr_pull_relative_ptr_short(struct ndr_pull
*ndr
, uint16_t *v
)
201 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, v
));
205 *(v
) -= ndr
->relative_rap_convert
;
206 return NDR_ERR_SUCCESS
;
210 parse a pointer referent identifier
212 _PUBLIC_
enum ndr_err_code
ndr_pull_generic_ptr(struct ndr_pull
*ndr
, uint32_t *v
)
214 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, v
));
218 return NDR_ERR_SUCCESS
;
222 parse a ref pointer referent identifier
224 _PUBLIC_
enum ndr_err_code
ndr_pull_ref_ptr(struct ndr_pull
*ndr
, uint32_t *v
)
226 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, v
));
227 /* ref pointers always point to data */
229 return NDR_ERR_SUCCESS
;
235 _PUBLIC_
enum ndr_err_code
ndr_pull_udlong(struct ndr_pull
*ndr
, int ndr_flags
, uint64_t *v
)
237 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
238 NDR_PULL_ALIGN(ndr
, 4);
239 NDR_PULL_NEED_BYTES(ndr
, 8);
240 *v
= NDR_IVAL(ndr
, ndr
->offset
);
241 *v
|= (uint64_t)(NDR_IVAL(ndr
, ndr
->offset
+4)) << 32;
243 return NDR_ERR_SUCCESS
;
249 _PUBLIC_
enum ndr_err_code
ndr_pull_udlongr(struct ndr_pull
*ndr
, int ndr_flags
, uint64_t *v
)
251 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
252 NDR_PULL_ALIGN(ndr
, 4);
253 NDR_PULL_NEED_BYTES(ndr
, 8);
254 *v
= ((uint64_t)NDR_IVAL(ndr
, ndr
->offset
)) << 32;
255 *v
|= NDR_IVAL(ndr
, ndr
->offset
+4);
257 return NDR_ERR_SUCCESS
;
263 _PUBLIC_
enum ndr_err_code
ndr_pull_dlong(struct ndr_pull
*ndr
, int ndr_flags
, int64_t *v
)
265 return ndr_pull_udlong(ndr
, ndr_flags
, (uint64_t *)v
);
271 _PUBLIC_
enum ndr_err_code
ndr_pull_hyper(struct ndr_pull
*ndr
, int ndr_flags
, uint64_t *v
)
273 NDR_PULL_ALIGN(ndr
, 8);
275 return ndr_pull_udlongr(ndr
, ndr_flags
, v
);
277 return ndr_pull_udlong(ndr
, ndr_flags
, v
);
283 _PUBLIC_
enum ndr_err_code
ndr_pull_pointer(struct ndr_pull
*ndr
, int ndr_flags
, void* *v
)
286 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
287 NDR_PULL_ALIGN(ndr
, sizeof(h
));
288 NDR_PULL_NEED_BYTES(ndr
, sizeof(h
));
289 memcpy(&h
, ndr
->data
+ndr
->offset
, sizeof(h
));
290 ndr
->offset
+= sizeof(h
);
292 return NDR_ERR_SUCCESS
;
298 _PUBLIC_
enum ndr_err_code
ndr_pull_NTSTATUS(struct ndr_pull
*ndr
, int ndr_flags
, NTSTATUS
*status
)
301 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
302 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &v
));
303 *status
= NT_STATUS(v
);
304 return NDR_ERR_SUCCESS
;
310 _PUBLIC_
enum ndr_err_code
ndr_push_NTSTATUS(struct ndr_push
*ndr
, int ndr_flags
, NTSTATUS status
)
312 return ndr_push_uint32(ndr
, ndr_flags
, NT_STATUS_V(status
));
315 _PUBLIC_
void ndr_print_NTSTATUS(struct ndr_print
*ndr
, const char *name
, NTSTATUS r
)
317 ndr
->print(ndr
, "%-25s: %s", name
, nt_errstr(r
));
323 _PUBLIC_
enum ndr_err_code
ndr_pull_WERROR(struct ndr_pull
*ndr
, int ndr_flags
, WERROR
*status
)
326 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
327 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &v
));
328 *status
= W_ERROR(v
);
329 return NDR_ERR_SUCCESS
;
335 _PUBLIC_
enum ndr_err_code
ndr_pull_HRESULT(struct ndr_pull
*ndr
, int ndr_flags
, HRESULT
*status
)
338 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
339 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &v
));
340 *status
= HRES_ERROR(v
);
341 return NDR_ERR_SUCCESS
;
347 _PUBLIC_
enum ndr_err_code
ndr_pull_enum_uint8(struct ndr_pull
*ndr
, int ndr_flags
, uint8_t *v
)
349 return ndr_pull_uint8(ndr
, ndr_flags
, v
);
353 parse a uint16_t enum
355 _PUBLIC_
enum ndr_err_code
ndr_pull_enum_uint16(struct ndr_pull
*ndr
, int ndr_flags
, uint16_t *v
)
357 return ndr_pull_uint16(ndr
, ndr_flags
, v
);
361 parse a uint1632_t enum (uint32_t on NDR64)
363 _PUBLIC_
enum ndr_err_code
ndr_pull_enum_uint1632(struct ndr_pull
*ndr
, int ndr_flags
, uint16_t *v
)
365 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
367 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &v32
));
370 DEBUG(0,(__location__
": non-zero upper 16 bits 0x%08x\n", (unsigned)v32
));
371 return NDR_ERR_NDR64
;
373 return NDR_ERR_SUCCESS
;
375 return ndr_pull_uint16(ndr
, ndr_flags
, v
);
379 parse a uint32_t enum
381 _PUBLIC_
enum ndr_err_code
ndr_pull_enum_uint32(struct ndr_pull
*ndr
, int ndr_flags
, uint32_t *v
)
383 return ndr_pull_uint32(ndr
, ndr_flags
, v
);
389 _PUBLIC_
enum ndr_err_code
ndr_push_enum_uint8(struct ndr_push
*ndr
, int ndr_flags
, uint8_t v
)
391 return ndr_push_uint8(ndr
, ndr_flags
, v
);
397 _PUBLIC_
enum ndr_err_code
ndr_push_enum_uint16(struct ndr_push
*ndr
, int ndr_flags
, uint16_t v
)
399 return ndr_push_uint16(ndr
, ndr_flags
, v
);
405 _PUBLIC_
enum ndr_err_code
ndr_push_enum_uint32(struct ndr_push
*ndr
, int ndr_flags
, uint32_t v
)
407 return ndr_push_uint32(ndr
, ndr_flags
, v
);
411 push a uint1632_t enum
413 _PUBLIC_
enum ndr_err_code
ndr_push_enum_uint1632(struct ndr_push
*ndr
, int ndr_flags
, uint16_t v
)
415 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
416 return ndr_push_uint32(ndr
, ndr_flags
, v
);
418 return ndr_push_uint16(ndr
, ndr_flags
, v
);
424 _PUBLIC_
enum ndr_err_code
ndr_push_WERROR(struct ndr_push
*ndr
, int ndr_flags
, WERROR status
)
426 return ndr_push_uint32(ndr
, NDR_SCALARS
, W_ERROR_V(status
));
429 _PUBLIC_
void ndr_print_WERROR(struct ndr_print
*ndr
, const char *name
, WERROR r
)
431 ndr
->print(ndr
, "%-25s: %s", name
, win_errstr(r
));
437 _PUBLIC_
enum ndr_err_code
ndr_push_HRESULT(struct ndr_push
*ndr
, int ndr_flags
, HRESULT status
)
439 return ndr_push_uint32(ndr
, NDR_SCALARS
, HRES_ERROR_V(status
));
442 _PUBLIC_
void ndr_print_HRESULT(struct ndr_print
*ndr
, const char *name
, HRESULT r
)
444 ndr
->print(ndr
, "%-25s: %s", name
, hresult_errstr(r
));
451 _PUBLIC_
enum ndr_err_code
ndr_pull_bytes(struct ndr_pull
*ndr
, uint8_t *data
, uint32_t n
)
453 NDR_PULL_NEED_BYTES(ndr
, n
);
454 memcpy(data
, ndr
->data
+ ndr
->offset
, n
);
456 return NDR_ERR_SUCCESS
;
460 pull an array of uint8
462 _PUBLIC_
enum ndr_err_code
ndr_pull_array_uint8(struct ndr_pull
*ndr
, int ndr_flags
, uint8_t *data
, uint32_t n
)
464 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
465 if (!(ndr_flags
& NDR_SCALARS
)) {
466 return NDR_ERR_SUCCESS
;
468 return ndr_pull_bytes(ndr
, data
, n
);
474 _PUBLIC_
enum ndr_err_code
ndr_push_int8(struct ndr_push
*ndr
, int ndr_flags
, int8_t v
)
476 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
477 NDR_PUSH_NEED_BYTES(ndr
, 1);
478 SCVAL(ndr
->data
, ndr
->offset
, (uint8_t)v
);
480 return NDR_ERR_SUCCESS
;
486 _PUBLIC_
enum ndr_err_code
ndr_push_uint8(struct ndr_push
*ndr
, int ndr_flags
, uint8_t v
)
488 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
489 NDR_PUSH_NEED_BYTES(ndr
, 1);
490 SCVAL(ndr
->data
, ndr
->offset
, v
);
492 return NDR_ERR_SUCCESS
;
498 _PUBLIC_
enum ndr_err_code
ndr_push_int16(struct ndr_push
*ndr
, int ndr_flags
, int16_t v
)
500 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
501 NDR_PUSH_ALIGN(ndr
, 2);
502 NDR_PUSH_NEED_BYTES(ndr
, 2);
503 NDR_SSVAL(ndr
, ndr
->offset
, (uint16_t)v
);
505 return NDR_ERR_SUCCESS
;
511 _PUBLIC_
enum ndr_err_code
ndr_push_uint16(struct ndr_push
*ndr
, int ndr_flags
, uint16_t v
)
513 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
514 NDR_PUSH_ALIGN(ndr
, 2);
515 NDR_PUSH_NEED_BYTES(ndr
, 2);
516 NDR_SSVAL(ndr
, ndr
->offset
, v
);
518 return NDR_ERR_SUCCESS
;
524 _PUBLIC_
enum ndr_err_code
ndr_push_uint1632(struct ndr_push
*ndr
, int ndr_flags
, uint16_t v
)
526 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
527 return ndr_push_uint32(ndr
, ndr_flags
, v
);
529 return ndr_push_uint16(ndr
, ndr_flags
, v
);
535 _PUBLIC_
enum ndr_err_code
ndr_push_int32(struct ndr_push
*ndr
, int ndr_flags
, int32_t v
)
537 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
538 NDR_PUSH_ALIGN(ndr
, 4);
539 NDR_PUSH_NEED_BYTES(ndr
, 4);
540 NDR_SIVALS(ndr
, ndr
->offset
, v
);
542 return NDR_ERR_SUCCESS
;
548 _PUBLIC_
enum ndr_err_code
ndr_push_uint32(struct ndr_push
*ndr
, int ndr_flags
, uint32_t v
)
550 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
551 NDR_PUSH_ALIGN(ndr
, 4);
552 NDR_PUSH_NEED_BYTES(ndr
, 4);
553 NDR_SIVAL(ndr
, ndr
->offset
, v
);
555 return NDR_ERR_SUCCESS
;
561 _PUBLIC_
enum ndr_err_code
ndr_push_uint3264(struct ndr_push
*ndr
, int ndr_flags
, uint32_t v
)
563 if (unlikely(ndr
->flags
& LIBNDR_FLAG_NDR64
)) {
564 return ndr_push_hyper(ndr
, ndr_flags
, v
);
566 return ndr_push_uint32(ndr
, ndr_flags
, v
);
572 _PUBLIC_
enum ndr_err_code
ndr_push_udlong(struct ndr_push
*ndr
, int ndr_flags
, uint64_t v
)
574 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
575 NDR_PUSH_ALIGN(ndr
, 4);
576 NDR_PUSH_NEED_BYTES(ndr
, 8);
577 NDR_SIVAL(ndr
, ndr
->offset
, (v
& 0xFFFFFFFF));
578 NDR_SIVAL(ndr
, ndr
->offset
+4, (v
>>32));
580 return NDR_ERR_SUCCESS
;
586 _PUBLIC_
enum ndr_err_code
ndr_push_udlongr(struct ndr_push
*ndr
, int ndr_flags
, uint64_t v
)
588 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
589 NDR_PUSH_ALIGN(ndr
, 4);
590 NDR_PUSH_NEED_BYTES(ndr
, 8);
591 NDR_SIVAL(ndr
, ndr
->offset
, (v
>>32));
592 NDR_SIVAL(ndr
, ndr
->offset
+4, (v
& 0xFFFFFFFF));
594 return NDR_ERR_SUCCESS
;
600 _PUBLIC_
enum ndr_err_code
ndr_push_dlong(struct ndr_push
*ndr
, int ndr_flags
, int64_t v
)
602 return ndr_push_udlong(ndr
, NDR_SCALARS
, (uint64_t)v
);
608 _PUBLIC_
enum ndr_err_code
ndr_push_hyper(struct ndr_push
*ndr
, int ndr_flags
, uint64_t v
)
610 NDR_PUSH_ALIGN(ndr
, 8);
612 return ndr_push_udlongr(ndr
, NDR_SCALARS
, v
);
614 return ndr_push_udlong(ndr
, NDR_SCALARS
, v
);
620 _PUBLIC_
enum ndr_err_code
ndr_push_double(struct ndr_push
*ndr
, int ndr_flags
, double v
)
622 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
623 NDR_PUSH_ALIGN(ndr
, 8);
624 NDR_PUSH_NEED_BYTES(ndr
, 8);
625 memcpy(ndr
->data
+ndr
->offset
, &v
, 8);
627 return NDR_ERR_SUCCESS
;
633 _PUBLIC_
enum ndr_err_code
ndr_push_pointer(struct ndr_push
*ndr
, int ndr_flags
, void* v
)
635 uintptr_t h
= (intptr_t)v
;
636 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
637 NDR_PUSH_ALIGN(ndr
, sizeof(h
));
638 NDR_PUSH_NEED_BYTES(ndr
, sizeof(h
));
639 memcpy(ndr
->data
+ndr
->offset
, &h
, sizeof(h
));
640 ndr
->offset
+= sizeof(h
);
641 return NDR_ERR_SUCCESS
;
644 _PUBLIC_
enum ndr_err_code
ndr_push_align(struct ndr_push
*ndr
, size_t size
)
646 /* this is a nasty hack to make pidl work with NDR64 */
648 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
653 } else if (size
== 3) {
654 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
660 NDR_PUSH_ALIGN(ndr
, size
);
661 return NDR_ERR_SUCCESS
;
664 _PUBLIC_
enum ndr_err_code
ndr_pull_align(struct ndr_pull
*ndr
, size_t size
)
666 /* this is a nasty hack to make pidl work with NDR64 */
668 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
673 } else if (size
== 3) {
674 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
680 NDR_PULL_ALIGN(ndr
, size
);
681 return NDR_ERR_SUCCESS
;
684 _PUBLIC_
enum ndr_err_code
ndr_push_union_align(struct ndr_push
*ndr
, size_t size
)
686 /* MS-RPCE section 2.2.5.3.4.4 */
687 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
688 return ndr_push_align(ndr
, size
);
690 return NDR_ERR_SUCCESS
;
693 _PUBLIC_
enum ndr_err_code
ndr_pull_union_align(struct ndr_pull
*ndr
, size_t size
)
695 /* MS-RPCE section 2.2.5.3.4.4 */
696 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
697 return ndr_pull_align(ndr
, size
);
699 return NDR_ERR_SUCCESS
;
702 _PUBLIC_
enum ndr_err_code
ndr_push_trailer_align(struct ndr_push
*ndr
, size_t size
)
704 /* MS-RPCE section 2.2.5.3.4.1 */
705 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
706 return ndr_push_align(ndr
, size
);
708 return NDR_ERR_SUCCESS
;
711 _PUBLIC_
enum ndr_err_code
ndr_pull_trailer_align(struct ndr_pull
*ndr
, size_t size
)
713 /* MS-RPCE section 2.2.5.3.4.1 */
714 if (ndr
->flags
& LIBNDR_FLAG_NDR64
) {
715 return ndr_pull_align(ndr
, size
);
717 return NDR_ERR_SUCCESS
;
723 _PUBLIC_
enum ndr_err_code
ndr_push_bytes(struct ndr_push
*ndr
, const uint8_t *data
, uint32_t n
)
725 if (unlikely(n
== 0)) {
726 return NDR_ERR_SUCCESS
;
728 if (unlikely(data
== NULL
)) {
729 return NDR_ERR_INVALID_POINTER
;
731 NDR_PUSH_NEED_BYTES(ndr
, n
);
732 memcpy(ndr
->data
+ ndr
->offset
, data
, n
);
734 return NDR_ERR_SUCCESS
;
740 _PUBLIC_
enum ndr_err_code
ndr_push_zero(struct ndr_push
*ndr
, uint32_t n
)
742 NDR_PUSH_NEED_BYTES(ndr
, n
);
743 memset(ndr
->data
+ ndr
->offset
, 0, n
);
745 return NDR_ERR_SUCCESS
;
749 push an array of uint8
751 _PUBLIC_
enum ndr_err_code
ndr_push_array_uint8(struct ndr_push
*ndr
, int ndr_flags
, const uint8_t *data
, uint32_t n
)
753 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
754 if (!(ndr_flags
& NDR_SCALARS
)) {
755 return NDR_ERR_SUCCESS
;
757 return ndr_push_bytes(ndr
, data
, n
);
761 push a unique non-zero value if a pointer is non-NULL, otherwise 0
763 _PUBLIC_
enum ndr_err_code
ndr_push_unique_ptr(struct ndr_push
*ndr
, const void *p
)
767 ptr
= ndr
->ptr_count
* 4;
771 return ndr_push_uint3264(ndr
, NDR_SCALARS
, ptr
);
775 push a 'simple' full non-zero value if a pointer is non-NULL, otherwise 0
777 _PUBLIC_
enum ndr_err_code
ndr_push_full_ptr(struct ndr_push
*ndr
, const void *p
)
781 /* Check if the pointer already exists and has an id */
782 ptr
= ndr_token_peek(&ndr
->full_ptr_list
, p
);
784 enum ndr_err_code ret
= NDR_ERR_SUCCESS
;
786 ptr
= ndr
->ptr_count
;
787 ret
= ndr_token_store(ndr
, &ndr
->full_ptr_list
, p
, ptr
);
788 if (ret
!= NDR_ERR_SUCCESS
) {
793 return ndr_push_uint3264(ndr
, NDR_SCALARS
, ptr
);
797 push always a 0, if a pointer is NULL it's a fatal error
799 _PUBLIC_
enum ndr_err_code
ndr_push_ref_ptr(struct ndr_push
*ndr
)
801 return ndr_push_uint3264(ndr
, NDR_SCALARS
, 0xAEF1AEF1);
808 _PUBLIC_
enum ndr_err_code
ndr_push_NTTIME(struct ndr_push
*ndr
, int ndr_flags
, NTTIME t
)
810 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
811 NDR_CHECK(ndr_push_udlong(ndr
, ndr_flags
, t
));
812 return NDR_ERR_SUCCESS
;
818 _PUBLIC_
enum ndr_err_code
ndr_pull_NTTIME(struct ndr_pull
*ndr
, int ndr_flags
, NTTIME
*t
)
820 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
821 NDR_CHECK(ndr_pull_udlong(ndr
, ndr_flags
, t
));
822 return NDR_ERR_SUCCESS
;
828 _PUBLIC_
enum ndr_err_code
ndr_push_NTTIME_1sec(struct ndr_push
*ndr
, int ndr_flags
, NTTIME t
)
830 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
832 NDR_CHECK(ndr_push_hyper(ndr
, ndr_flags
, t
));
833 return NDR_ERR_SUCCESS
;
839 _PUBLIC_
enum ndr_err_code
ndr_pull_NTTIME_1sec(struct ndr_pull
*ndr
, int ndr_flags
, NTTIME
*t
)
841 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
842 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, t
));
844 return NDR_ERR_SUCCESS
;
850 _PUBLIC_
enum ndr_err_code
ndr_pull_NTTIME_hyper(struct ndr_pull
*ndr
, int ndr_flags
, NTTIME
*t
)
852 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
853 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, t
));
854 return NDR_ERR_SUCCESS
;
860 _PUBLIC_
enum ndr_err_code
ndr_push_NTTIME_hyper(struct ndr_push
*ndr
, int ndr_flags
, NTTIME t
)
862 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_push_time_t(struct ndr_push
*ndr
, int ndr_flags
, time_t t
)
872 return ndr_push_uint32(ndr
, ndr_flags
, t
);
878 _PUBLIC_
enum ndr_err_code
ndr_pull_time_t(struct ndr_pull
*ndr
, int ndr_flags
, time_t *t
)
881 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &tt
));
883 return NDR_ERR_SUCCESS
;
890 _PUBLIC_
enum ndr_err_code
ndr_push_uid_t(struct ndr_push
*ndr
, int ndr_flags
, uid_t u
)
892 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
893 return ndr_push_hyper(ndr
, NDR_SCALARS
, (uint64_t)u
);
899 _PUBLIC_
enum ndr_err_code
ndr_pull_uid_t(struct ndr_pull
*ndr
, int ndr_flags
, uid_t
*u
)
902 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, &uu
));
904 if (unlikely(uu
!= *u
)) {
905 DEBUG(0,(__location__
": uid_t pull doesn't fit 0x%016llx\n",
906 (unsigned long long)uu
));
907 return NDR_ERR_NDR64
;
909 return NDR_ERR_SUCCESS
;
916 _PUBLIC_
enum ndr_err_code
ndr_push_gid_t(struct ndr_push
*ndr
, int ndr_flags
, gid_t g
)
918 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
919 return ndr_push_hyper(ndr
, NDR_SCALARS
, (uint64_t)g
);
925 _PUBLIC_
enum ndr_err_code
ndr_pull_gid_t(struct ndr_pull
*ndr
, int ndr_flags
, gid_t
*g
)
928 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, &gg
));
930 if (unlikely(gg
!= *g
)) {
931 DEBUG(0,(__location__
": gid_t pull doesn't fit 0x%016llx\n",
932 (unsigned long long)gg
));
933 return NDR_ERR_NDR64
;
935 return NDR_ERR_SUCCESS
;
942 _PUBLIC_
enum ndr_err_code
ndr_pull_ipv4address(struct ndr_pull
*ndr
, int ndr_flags
, const char **address
)
946 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &addr
));
947 in
.s_addr
= htonl(addr
);
948 *address
= talloc_strdup(ndr
->current_mem_ctx
, inet_ntoa(in
));
949 NDR_ERR_HAVE_NO_MEMORY(*address
);
950 return NDR_ERR_SUCCESS
;
956 _PUBLIC_
enum ndr_err_code
ndr_push_ipv4address(struct ndr_push
*ndr
, int ndr_flags
, const char *address
)
959 if (!is_ipaddress(address
)) {
960 return ndr_push_error(ndr
, NDR_ERR_IPV4ADDRESS
,
961 "Invalid IPv4 address: '%s'",
964 addr
= inet_addr(address
);
965 NDR_CHECK(ndr_push_uint32(ndr
, ndr_flags
, htonl(addr
)));
966 return NDR_ERR_SUCCESS
;
972 _PUBLIC_
void ndr_print_ipv4address(struct ndr_print
*ndr
, const char *name
,
975 ndr
->print(ndr
, "%-25s: %s", name
, address
);
981 #define IPV6_BYTES 16
982 #define IPV6_ADDR_STR_LEN 39
983 _PUBLIC_
enum ndr_err_code
ndr_pull_ipv6address(struct ndr_pull
*ndr
, int ndr_flags
, const char **address
)
985 uint8_t addr
[IPV6_BYTES
];
986 char *addr_str
= talloc_strdup(ndr
->current_mem_ctx
, "");
988 NDR_CHECK(ndr_pull_array_uint8(ndr
, ndr_flags
, addr
, IPV6_BYTES
));
989 for (i
= 0; i
< IPV6_BYTES
; ++i
) {
990 addr_str
= talloc_asprintf_append(addr_str
, "%02x", addr
[i
]);
991 /* We need a ':' every second byte but the last one */
992 if (i
%2 == 1 && i
!= (IPV6_BYTES
- 1)) {
993 addr_str
= talloc_strdup_append(addr_str
, ":");
997 NDR_ERR_HAVE_NO_MEMORY(*address
);
998 return NDR_ERR_SUCCESS
;
1004 _PUBLIC_
enum ndr_err_code
ndr_push_ipv6address(struct ndr_push
*ndr
, int ndr_flags
, const char *address
)
1007 uint8_t addr
[IPV6_BYTES
];
1010 if (!is_ipaddress(address
)) {
1011 return ndr_push_error(ndr
, NDR_ERR_IPV6ADDRESS
,
1012 "Invalid IPv6 address: '%s'",
1015 ret
= inet_pton(AF_INET6
, address
, addr
);
1017 return NDR_ERR_IPV6ADDRESS
;
1020 NDR_CHECK(ndr_push_array_uint8(ndr
, ndr_flags
, addr
, IPV6_BYTES
));
1022 return NDR_ERR_SUCCESS
;
1024 return NDR_ERR_IPV6ADDRESS
;
1031 _PUBLIC_
void ndr_print_ipv6address(struct ndr_print
*ndr
, const char *name
,
1032 const char *address
)
1034 ndr
->print(ndr
, "%-25s: %s", name
, address
);
1038 _PUBLIC_
void ndr_print_struct(struct ndr_print
*ndr
, const char *name
, const char *type
)
1040 ndr
->print(ndr
, "%s: struct %s", name
, type
);
1043 _PUBLIC_
void ndr_print_null(struct ndr_print
*ndr
)
1045 ndr
->print(ndr
, "UNEXPECTED NULL POINTER");
1048 _PUBLIC_
void ndr_print_enum(struct ndr_print
*ndr
, const char *name
, const char *type
,
1049 const char *val
, uint32_t value
)
1051 if (ndr
->flags
& LIBNDR_PRINT_ARRAY_HEX
) {
1052 ndr
->print(ndr
, "%-25s: %s (0x%X)", name
, val
?val
:"UNKNOWN_ENUM_VALUE", value
);
1054 ndr
->print(ndr
, "%-25s: %s (%d)", name
, val
?val
:"UNKNOWN_ENUM_VALUE", value
);
1058 _PUBLIC_
void ndr_print_bitmap_flag(struct ndr_print
*ndr
, size_t size
, const char *flag_name
, uint32_t flag
, uint32_t value
)
1064 /* this is an attempt to support multi-bit bitmap masks */
1067 while (!(flag
& 1)) {
1072 ndr
->print(ndr
, " %d: %-25s", value
, flag_name
);
1074 ndr
->print(ndr
, "0x%02x: %-25s (%d)", value
, flag_name
, value
);
1078 _PUBLIC_
void ndr_print_int8(struct ndr_print
*ndr
, const char *name
, int8_t v
)
1080 if (NDR_HIDE_SECRET(ndr
)) {
1081 ndr
->print(ndr
, "%-25s: <REDACTED SECRET VALUE>", name
);
1084 ndr
->print(ndr
, "%-25s: %d", name
, v
);
1087 _PUBLIC_
void ndr_print_uint8(struct ndr_print
*ndr
, const char *name
, uint8_t v
)
1089 if (NDR_HIDE_SECRET(ndr
)) {
1090 ndr
->print(ndr
, "%-25s: <REDACTED SECRET VALUE>", name
);
1093 ndr
->print(ndr
, "%-25s: 0x%02x (%u)", name
, v
, v
);
1096 _PUBLIC_
void ndr_print_int16(struct ndr_print
*ndr
, const char *name
, int16_t v
)
1098 if (NDR_HIDE_SECRET(ndr
)) {
1099 ndr
->print(ndr
, "%-25s: <REDACTED SECRET VALUE>", name
);
1102 ndr
->print(ndr
, "%-25s: %d", name
, v
);
1105 _PUBLIC_
void ndr_print_uint16(struct ndr_print
*ndr
, const char *name
, uint16_t v
)
1107 if (NDR_HIDE_SECRET(ndr
)) {
1108 ndr
->print(ndr
, "%-25s: <REDACTED SECRET VALUE>", name
);
1111 ndr
->print(ndr
, "%-25s: 0x%04x (%u)", name
, v
, v
);
1114 _PUBLIC_
void ndr_print_int32(struct ndr_print
*ndr
, const char *name
, int32_t v
)
1116 if (NDR_HIDE_SECRET(ndr
)) {
1117 ndr
->print(ndr
, "%-25s: <REDACTED SECRET VALUE>", name
);
1120 ndr
->print(ndr
, "%-25s: %d", name
, v
);
1123 _PUBLIC_
void ndr_print_uint32(struct ndr_print
*ndr
, const char *name
, uint32_t v
)
1125 if (NDR_HIDE_SECRET(ndr
)) {
1126 ndr
->print(ndr
, "%-25s: <REDACTED SECRET VALUE>", name
);
1129 ndr
->print(ndr
, "%-25s: 0x%08x (%u)", name
, v
, v
);
1132 _PUBLIC_
void ndr_print_int3264(struct ndr_print
*ndr
, const char *name
, int32_t v
)
1134 if (NDR_HIDE_SECRET(ndr
)) {
1135 ndr
->print(ndr
, "%-25s: <REDACTED SECRET VALUE>", name
);
1138 ndr
->print(ndr
, "%-25s: %d", name
, v
);
1141 _PUBLIC_
void ndr_print_uint3264(struct ndr_print
*ndr
, const char *name
, uint32_t v
)
1143 if (NDR_HIDE_SECRET(ndr
)) {
1144 ndr
->print(ndr
, "%-25s: <REDACTED SECRET VALUE>", name
);
1147 ndr
->print(ndr
, "%-25s: 0x%08x (%u)", name
, v
, v
);
1150 _PUBLIC_
void ndr_print_udlong(struct ndr_print
*ndr
, const char *name
, uint64_t v
)
1152 ndr
->print(ndr
, "%-25s: 0x%016llx (%llu)", name
, (unsigned long long)v
, (unsigned long long)v
);
1155 _PUBLIC_
void ndr_print_udlongr(struct ndr_print
*ndr
, const char *name
, uint64_t v
)
1157 ndr_print_udlong(ndr
, name
, v
);
1160 _PUBLIC_
void ndr_print_dlong(struct ndr_print
*ndr
, const char *name
, int64_t v
)
1162 if (NDR_HIDE_SECRET(ndr
)) {
1163 ndr
->print(ndr
, "%-25s: <REDACTED SECRET VALUE>", name
);
1166 ndr
->print(ndr
, "%-25s: 0x%016llx (%lld)", name
, (unsigned long long)v
, (long long)v
);
1169 _PUBLIC_
void ndr_print_double(struct ndr_print
*ndr
, const char *name
, double v
)
1171 ndr
->print(ndr
, "%-25s: %f", name
, v
);
1174 _PUBLIC_
void ndr_print_hyper(struct ndr_print
*ndr
, const char *name
, uint64_t v
)
1176 ndr_print_dlong(ndr
, name
, v
);
1179 _PUBLIC_
void ndr_print_pointer(struct ndr_print
*ndr
, const char *name
, void *v
)
1181 ndr
->print(ndr
, "%-25s: %p", name
, v
);
1184 _PUBLIC_
void ndr_print_ptr(struct ndr_print
*ndr
, const char *name
, const void *p
)
1187 ndr
->print(ndr
, "%-25s: *", name
);
1189 ndr
->print(ndr
, "%-25s: NULL", name
);
1193 _PUBLIC_
void ndr_print_NTTIME(struct ndr_print
*ndr
, const char *name
, NTTIME t
)
1195 ndr
->print(ndr
, "%-25s: %s", name
, nt_time_string(ndr
, t
));
1198 _PUBLIC_
void ndr_print_NTTIME_1sec(struct ndr_print
*ndr
, const char *name
, NTTIME t
)
1200 /* this is a standard NTTIME here
1201 * as it's already converted in the pull/push code
1203 ndr_print_NTTIME(ndr
, name
, t
);
1206 _PUBLIC_
void ndr_print_NTTIME_hyper(struct ndr_print
*ndr
, const char *name
, NTTIME t
)
1208 ndr_print_NTTIME(ndr
, name
, t
);
1211 _PUBLIC_
void ndr_print_time_t(struct ndr_print
*ndr
, const char *name
, time_t t
)
1213 if (t
== (time_t)-1 || t
== 0) {
1214 ndr
->print(ndr
, "%-25s: (time_t)%d", name
, (int)t
);
1216 ndr
->print(ndr
, "%-25s: %s", name
, timestring(ndr
, t
));
1220 _PUBLIC_
void ndr_print_uid_t(struct ndr_print
*ndr
, const char *name
, uid_t u
)
1222 ndr_print_dlong(ndr
, name
, u
);
1225 _PUBLIC_
void ndr_print_gid_t(struct ndr_print
*ndr
, const char *name
, gid_t g
)
1227 ndr_print_dlong(ndr
, name
, g
);
1230 _PUBLIC_
void ndr_print_union(struct ndr_print
*ndr
, const char *name
, int level
, const char *type
)
1232 if (ndr
->flags
& LIBNDR_PRINT_ARRAY_HEX
) {
1233 ndr
->print(ndr
, "%-25s: union %s(case 0x%X)", name
, type
, level
);
1235 ndr
->print(ndr
, "%-25s: union %s(case %d)", name
, type
, level
);
1239 _PUBLIC_
void ndr_print_bad_level(struct ndr_print
*ndr
, const char *name
, uint16_t level
)
1241 ndr
->print(ndr
, "UNKNOWN LEVEL %u", level
);
1244 _PUBLIC_
void ndr_print_array_uint8(struct ndr_print
*ndr
, const char *name
,
1245 const uint8_t *data
, uint32_t count
)
1248 #define _ONELINE_LIMIT 32
1251 ndr
->print(ndr
, "%s: ARRAY(%d) : NULL", name
, count
);
1255 if (NDR_HIDE_SECRET(ndr
)) {
1256 ndr
->print(ndr
, "%s: ARRAY(%d): <REDACTED SECRET VALUES>", name
, count
);
1260 if (count
<= _ONELINE_LIMIT
&& (ndr
->flags
& LIBNDR_PRINT_ARRAY_HEX
)) {
1261 char s
[(_ONELINE_LIMIT
+ 1) * 2];
1262 for (i
=0;i
<count
;i
++) {
1263 snprintf(&s
[i
*2], 3, "%02x", data
[i
]);
1266 ndr
->print(ndr
, "%-25s: %s", name
, s
);
1270 ndr
->print(ndr
, "%s: ARRAY(%d)", name
, count
);
1271 if (count
> _ONELINE_LIMIT
&& (ndr
->flags
& LIBNDR_PRINT_ARRAY_HEX
)) {
1272 ndr_dump_data(ndr
, data
, count
);
1277 for (i
=0;i
<count
;i
++) {
1279 if (asprintf(&idx
, "[%d]", i
) != -1) {
1280 ndr_print_uint8(ndr
, idx
, data
[i
]);
1285 #undef _ONELINE_LIMIT
1288 static void ndr_print_dump_data_cb(const char *buf
, void *private_data
)
1290 struct ndr_print
*ndr
= (struct ndr_print
*)private_data
;
1292 ndr
->print(ndr
, "%s", buf
);
1296 ndr_print version of dump_data()
1298 static void ndr_dump_data(struct ndr_print
*ndr
, const uint8_t *buf
, int len
)
1300 if (NDR_HIDE_SECRET(ndr
)) {
1303 ndr
->no_newline
= true;
1304 dump_data_cb(buf
, len
, true, ndr_print_dump_data_cb
, ndr
);
1305 ndr
->no_newline
= false;
1309 _PUBLIC_
void ndr_print_DATA_BLOB(struct ndr_print
*ndr
, const char *name
, DATA_BLOB r
)
1311 ndr
->print(ndr
, "%-25s: DATA_BLOB length=%u", name
, (unsigned)r
.length
);
1313 ndr_dump_data(ndr
, r
.data
, r
.length
);
1319 * Push a DATA_BLOB onto the wire.
1320 * 1) When called with LIBNDR_FLAG_ALIGN* alignment flags set, push padding
1321 * bytes _only_. The length is determined by the alignment required and the
1322 * current ndr offset.
1323 * 2) When called with the LIBNDR_FLAG_REMAINING flag, push the byte array to
1325 * 3) Otherwise, push a uint3264 length _and_ a corresponding byte array to the
1328 _PUBLIC_
enum ndr_err_code
ndr_push_DATA_BLOB(struct ndr_push
*ndr
, int ndr_flags
, DATA_BLOB blob
)
1330 if (ndr
->flags
& LIBNDR_FLAG_REMAINING
) {
1332 } else if (ndr
->flags
& (LIBNDR_ALIGN_FLAGS
& ~LIBNDR_FLAG_NOALIGN
)) {
1333 if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
1334 blob
.length
= NDR_ALIGN(ndr
, 2);
1335 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
1336 blob
.length
= NDR_ALIGN(ndr
, 4);
1337 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
1338 blob
.length
= NDR_ALIGN(ndr
, 8);
1340 NDR_PUSH_ALLOC_SIZE(ndr
, blob
.data
, blob
.length
);
1341 data_blob_clear(&blob
);
1343 NDR_CHECK(ndr_push_uint3264(ndr
, NDR_SCALARS
, blob
.length
));
1345 NDR_CHECK(ndr_push_bytes(ndr
, blob
.data
, blob
.length
));
1346 return NDR_ERR_SUCCESS
;
1350 * Pull a DATA_BLOB from the wire.
1351 * 1) when called with LIBNDR_FLAG_ALIGN* alignment flags set, pull 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, pull all remaining bytes
1355 * from the ndr buffer.
1356 * 3) Otherwise, pull a uint3264 length _and_ a corresponding byte array from the
1359 _PUBLIC_
enum ndr_err_code
ndr_pull_DATA_BLOB(struct ndr_pull
*ndr
, int ndr_flags
, DATA_BLOB
*blob
)
1361 uint32_t length
= 0;
1363 if (ndr
->flags
& LIBNDR_FLAG_REMAINING
) {
1364 length
= ndr
->data_size
- ndr
->offset
;
1365 } else if (ndr
->flags
& (LIBNDR_ALIGN_FLAGS
& ~LIBNDR_FLAG_NOALIGN
)) {
1366 if (ndr
->flags
& LIBNDR_FLAG_ALIGN2
) {
1367 length
= NDR_ALIGN(ndr
, 2);
1368 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN4
) {
1369 length
= NDR_ALIGN(ndr
, 4);
1370 } else if (ndr
->flags
& LIBNDR_FLAG_ALIGN8
) {
1371 length
= NDR_ALIGN(ndr
, 8);
1373 if (ndr
->data_size
- ndr
->offset
< length
) {
1374 length
= ndr
->data_size
- ndr
->offset
;
1377 NDR_CHECK(ndr_pull_uint3264(ndr
, NDR_SCALARS
, &length
));
1379 NDR_PULL_NEED_BYTES(ndr
, length
);
1380 *blob
= data_blob_talloc(ndr
->current_mem_ctx
, ndr
->data
+ndr
->offset
, length
);
1381 ndr
->offset
+= length
;
1382 return NDR_ERR_SUCCESS
;
1385 _PUBLIC_
uint32_t ndr_size_DATA_BLOB(int ret
, const DATA_BLOB
*data
, int flags
)
1387 if (!data
) return ret
;
1388 return ret
+ data
->length
;
1391 _PUBLIC_
void ndr_print_bool(struct ndr_print
*ndr
, const char *name
, const bool b
)
1393 ndr
->print(ndr
, "%-25s: %s", name
, b
?"true":"false");
1396 _PUBLIC_ NTSTATUS
ndr_map_error2ntstatus(enum ndr_err_code ndr_err
)
1399 case NDR_ERR_SUCCESS
:
1400 return NT_STATUS_OK
;
1401 case NDR_ERR_BUFSIZE
:
1402 return NT_STATUS_BUFFER_TOO_SMALL
;
1404 return NT_STATUS_INTERNAL_ERROR
;
1406 return NT_STATUS_NO_MEMORY
;
1407 case NDR_ERR_ARRAY_SIZE
:
1408 return NT_STATUS_ARRAY_BOUNDS_EXCEEDED
;
1409 case NDR_ERR_INVALID_POINTER
:
1410 return NT_STATUS_INVALID_PARAMETER_MIX
;
1411 case NDR_ERR_UNREAD_BYTES
:
1412 return NT_STATUS_PORT_MESSAGE_TOO_LONG
;
1417 /* we should map all error codes to different status codes */
1418 return NT_STATUS_INVALID_PARAMETER
;
1421 _PUBLIC_
int ndr_map_error2errno(enum ndr_err_code ndr_err
)
1424 case NDR_ERR_SUCCESS
:
1426 case NDR_ERR_BUFSIZE
:
1432 case NDR_ERR_ARRAY_SIZE
:
1434 case NDR_ERR_INVALID_POINTER
:
1436 case NDR_ERR_UNREAD_BYTES
:
1442 /* we should map all error codes to different status codes */
1446 _PUBLIC_
enum ndr_err_code
ndr_push_timespec(struct ndr_push
*ndr
,
1448 const struct timespec
*t
)
1450 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
1451 NDR_CHECK(ndr_push_hyper(ndr
, ndr_flags
, t
->tv_sec
));
1452 NDR_CHECK(ndr_push_uint32(ndr
, ndr_flags
, t
->tv_nsec
));
1453 return NDR_ERR_SUCCESS
;
1456 _PUBLIC_
enum ndr_err_code
ndr_pull_timespec(struct ndr_pull
*ndr
,
1462 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
1463 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, &secs
));
1464 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &nsecs
));
1467 return NDR_ERR_SUCCESS
;
1470 _PUBLIC_
void ndr_print_timespec(struct ndr_print
*ndr
, const char *name
,
1471 const struct timespec
*t
)
1473 char *str
= timestring(ndr
, t
->tv_sec
);
1474 ndr
->print(ndr
, "%-25s: %s.%ld", name
, str
, t
->tv_nsec
);
1478 _PUBLIC_
enum ndr_err_code
ndr_push_timeval(struct ndr_push
*ndr
,
1480 const struct timeval
*t
)
1482 NDR_PUSH_CHECK_FLAGS(ndr
, ndr_flags
);
1483 NDR_CHECK(ndr_push_hyper(ndr
, ndr_flags
, t
->tv_sec
));
1484 NDR_CHECK(ndr_push_uint32(ndr
, ndr_flags
, t
->tv_usec
));
1485 return NDR_ERR_SUCCESS
;
1488 _PUBLIC_
enum ndr_err_code
ndr_pull_timeval(struct ndr_pull
*ndr
,
1494 NDR_PULL_CHECK_FLAGS(ndr
, ndr_flags
);
1495 NDR_CHECK(ndr_pull_hyper(ndr
, ndr_flags
, &secs
));
1496 NDR_CHECK(ndr_pull_uint32(ndr
, ndr_flags
, &usecs
));
1499 return NDR_ERR_SUCCESS
;
1502 _PUBLIC_
void ndr_print_timeval(struct ndr_print
*ndr
, const char *name
,
1503 const struct timeval
*t
)
1505 ndr
->print(ndr
, "%-25s: %s.%ld", name
, timestring(ndr
, t
->tv_sec
),