source: branches/samba-3.0/source/librpc/ndr/ndr_basic.c

Last change on this file was 1, checked in by Paul Smedley, 18 years ago

Initial code import

File size: 18.0 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 routines for marshalling/unmarshalling basic types
5
6 Copyright (C) Andrew Tridgell 2003
7
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 2 of the License, or
11 (at your option) any later version.
12
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.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23#include "includes.h"
24
25#define NDR_SVAL(ndr, ofs) (NDR_BE(ndr)?RSVAL(ndr->data,ofs):SVAL(ndr->data,ofs))
26#define NDR_IVAL(ndr, ofs) (NDR_BE(ndr)?RIVAL(ndr->data,ofs):IVAL(ndr->data,ofs))
27#define NDR_IVALS(ndr, ofs) (NDR_BE(ndr)?RIVALS(ndr->data,ofs):IVALS(ndr->data,ofs))
28#define NDR_SSVAL(ndr, ofs, v) do { if (NDR_BE(ndr)) { RSSVAL(ndr->data,ofs,v); } else SSVAL(ndr->data,ofs,v); } while (0)
29#define NDR_SIVAL(ndr, ofs, v) do { if (NDR_BE(ndr)) { RSIVAL(ndr->data,ofs,v); } else SIVAL(ndr->data,ofs,v); } while (0)
30#define NDR_SIVALS(ndr, ofs, v) do { if (NDR_BE(ndr)) { RSIVALS(ndr->data,ofs,v); } else SIVALS(ndr->data,ofs,v); } while (0)
31
32
33/*
34 check for data leaks from the server by looking for non-zero pad bytes
35 these could also indicate that real structure elements have been
36 mistaken for padding in the IDL
37*/
38void ndr_check_padding(struct ndr_pull *ndr, size_t n)
39{
40 size_t ofs2 = (ndr->offset + (n-1)) & ~(n-1);
41 int i;
42 for (i=ndr->offset;i<ofs2;i++) {
43 if (ndr->data[i] != 0) {
44 break;
45 }
46 }
47 if (i<ofs2) {
48 DEBUG(0,("WARNING: Non-zero padding to %d: ", (int)n));
49 for (i=ndr->offset;i<ofs2;i++) {
50 DEBUG(0,("%02x ", ndr->data[i]));
51 }
52 DEBUG(0,("\n"));
53 }
54
55}
56
57/*
58 parse a int8_t
59*/
60NTSTATUS ndr_pull_int8(struct ndr_pull *ndr, int ndr_flags, int8_t *v)
61{
62 NDR_PULL_NEED_BYTES(ndr, 1);
63 *v = (int8_t)CVAL(ndr->data, ndr->offset);
64 ndr->offset += 1;
65 return NT_STATUS_OK;
66}
67
68/*
69 parse a uint8_t
70*/
71NTSTATUS ndr_pull_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *v)
72{
73 NDR_PULL_NEED_BYTES(ndr, 1);
74 *v = CVAL(ndr->data, ndr->offset);
75 ndr->offset += 1;
76 return NT_STATUS_OK;
77}
78
79/*
80 parse a int16_t
81*/
82NTSTATUS ndr_pull_int16(struct ndr_pull *ndr, int ndr_flags, int16_t *v)
83{
84 NDR_PULL_ALIGN(ndr, 2);
85 NDR_PULL_NEED_BYTES(ndr, 2);
86 *v = (uint16_t)NDR_SVAL(ndr, ndr->offset);
87 ndr->offset += 2;
88 return NT_STATUS_OK;
89}
90
91/*
92 parse a uint16_t
93*/
94NTSTATUS ndr_pull_uint16(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
95{
96 NDR_PULL_ALIGN(ndr, 2);
97 NDR_PULL_NEED_BYTES(ndr, 2);
98 *v = NDR_SVAL(ndr, ndr->offset);
99 ndr->offset += 2;
100 return NT_STATUS_OK;
101}
102
103/*
104 parse a int32_t
105*/
106NTSTATUS ndr_pull_int32(struct ndr_pull *ndr, int ndr_flags, int32_t *v)
107{
108 NDR_PULL_ALIGN(ndr, 4);
109 NDR_PULL_NEED_BYTES(ndr, 4);
110 *v = NDR_IVALS(ndr, ndr->offset);
111 ndr->offset += 4;
112 return NT_STATUS_OK;
113}
114
115/*
116 parse a uint32_t
117*/
118NTSTATUS ndr_pull_uint32(struct ndr_pull *ndr, int ndr_flags, uint32_t *v)
119{
120 NDR_PULL_ALIGN(ndr, 4);
121 NDR_PULL_NEED_BYTES(ndr, 4);
122 *v = NDR_IVAL(ndr, ndr->offset);
123 ndr->offset += 4;
124 return NT_STATUS_OK;
125}
126
127/*
128 parse a pointer referent identifier
129*/
130NTSTATUS ndr_pull_generic_ptr(struct ndr_pull *ndr, uint32_t *v)
131{
132 NTSTATUS status;
133 status = ndr_pull_uint32(ndr, NDR_SCALARS, v);
134 if (NT_STATUS_IS_OK(status) && *v != 0) {
135 ndr->ptr_count++;
136 }
137 return status;
138}
139
140/*
141 parse a ref pointer referent identifier
142*/
143NTSTATUS ndr_pull_ref_ptr(struct ndr_pull *ndr, uint32_t *v)
144{
145 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, v));
146 /* ref pointers always point to data */
147 *v = 1;
148 return NT_STATUS_OK;
149}
150
151/*
152 parse a udlong
153*/
154NTSTATUS ndr_pull_udlong(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
155{
156 NDR_PULL_ALIGN(ndr, 4);
157 NDR_PULL_NEED_BYTES(ndr, 8);
158 *v = NDR_IVAL(ndr, ndr->offset);
159 *v |= (uint64_t)(NDR_IVAL(ndr, ndr->offset+4)) << 32;
160 ndr->offset += 8;
161 return NT_STATUS_OK;
162}
163
164/*
165 parse a udlongr
166*/
167NTSTATUS ndr_pull_udlongr(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
168{
169 NDR_PULL_ALIGN(ndr, 4);
170 NDR_PULL_NEED_BYTES(ndr, 8);
171 *v = ((uint64_t)NDR_IVAL(ndr, ndr->offset)) << 32;
172 *v |= NDR_IVAL(ndr, ndr->offset+4);
173 ndr->offset += 8;
174 return NT_STATUS_OK;
175}
176
177/*
178 parse a dlong
179*/
180NTSTATUS ndr_pull_dlong(struct ndr_pull *ndr, int ndr_flags, int64_t *v)
181{
182 return ndr_pull_udlong(ndr, ndr_flags, (uint64_t *)v);
183}
184
185/*
186 parse a hyper
187*/
188NTSTATUS ndr_pull_hyper(struct ndr_pull *ndr, int ndr_flags, uint64_t *v)
189{
190 NDR_PULL_ALIGN(ndr, 8);
191 return ndr_pull_udlong(ndr, ndr_flags, v);
192}
193
194/*
195 parse a pointer
196*/
197NTSTATUS ndr_pull_pointer(struct ndr_pull *ndr, int ndr_flags, void* *v)
198{
199 intptr_t h;
200 NDR_PULL_ALIGN(ndr, sizeof(h));
201 NDR_PULL_NEED_BYTES(ndr, sizeof(h));
202 memcpy(&h, ndr->data+ndr->offset, sizeof(h));
203 ndr->offset += sizeof(h);
204 *v = (void *)h;
205 return NT_STATUS_OK;
206}
207
208/*
209 pull a NTSTATUS
210*/
211NTSTATUS ndr_pull_NTSTATUS(struct ndr_pull *ndr, int ndr_flags, NTSTATUS *status)
212{
213 uint32_t v;
214 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
215 *status = NT_STATUS(v);
216 return NT_STATUS_OK;
217}
218
219/*
220 push a NTSTATUS
221*/
222NTSTATUS ndr_push_NTSTATUS(struct ndr_push *ndr, int ndr_flags, NTSTATUS status)
223{
224 return ndr_push_uint32(ndr, ndr_flags, NT_STATUS_V(status));
225}
226
227void ndr_print_NTSTATUS(struct ndr_print *ndr, const char *name, NTSTATUS r)
228{
229 ndr->print(ndr, "%-25s: %s", name, nt_errstr(r));
230}
231
232/*
233 pull a WERROR
234*/
235NTSTATUS ndr_pull_WERROR(struct ndr_pull *ndr, int ndr_flags, WERROR *status)
236{
237 uint32_t v;
238 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
239 *status = W_ERROR(v);
240 return NT_STATUS_OK;
241}
242
243/*
244 push a WERROR
245*/
246NTSTATUS ndr_push_WERROR(struct ndr_push *ndr, int ndr_flags, WERROR status)
247{
248 return ndr_push_uint32(ndr, NDR_SCALARS, W_ERROR_V(status));
249}
250
251void ndr_print_WERROR(struct ndr_print *ndr, const char *name, WERROR r)
252{
253 ndr->print(ndr, "%-25s: %s", name, dos_errstr(r));
254}
255
256/*
257 parse a set of bytes
258*/
259NTSTATUS ndr_pull_bytes(struct ndr_pull *ndr, uint8_t *data, uint32_t n)
260{
261 NDR_PULL_NEED_BYTES(ndr, n);
262 memcpy(data, ndr->data + ndr->offset, n);
263 ndr->offset += n;
264 return NT_STATUS_OK;
265}
266
267/*
268 pull an array of uint8
269*/
270NTSTATUS ndr_pull_array_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *data, uint32_t n)
271{
272 if (!(ndr_flags & NDR_SCALARS)) {
273 return NT_STATUS_OK;
274 }
275 return ndr_pull_bytes(ndr, data, n);
276}
277
278/*
279 push a int8_t
280*/
281NTSTATUS ndr_push_int8(struct ndr_push *ndr, int ndr_flags, int8_t v)
282{
283 NDR_PUSH_NEED_BYTES(ndr, 1);
284 SCVAL(ndr->data, ndr->offset, (uint8_t)v);
285 ndr->offset += 1;
286 return NT_STATUS_OK;
287}
288
289/*
290 push a uint8_t
291*/
292NTSTATUS ndr_push_uint8(struct ndr_push *ndr, int ndr_flags, uint8_t v)
293{
294 NDR_PUSH_NEED_BYTES(ndr, 1);
295 SCVAL(ndr->data, ndr->offset, v);
296 ndr->offset += 1;
297 return NT_STATUS_OK;
298}
299
300/*
301 push a int16_t
302*/
303NTSTATUS ndr_push_int16(struct ndr_push *ndr, int ndr_flags, int16_t v)
304{
305 NDR_PUSH_ALIGN(ndr, 2);
306 NDR_PUSH_NEED_BYTES(ndr, 2);
307 NDR_SSVAL(ndr, ndr->offset, (uint16_t)v);
308 ndr->offset += 2;
309 return NT_STATUS_OK;
310}
311
312/*
313 push a uint16_t
314*/
315NTSTATUS ndr_push_uint16(struct ndr_push *ndr, int ndr_flags, uint16_t v)
316{
317 NDR_PUSH_ALIGN(ndr, 2);
318 NDR_PUSH_NEED_BYTES(ndr, 2);
319 NDR_SSVAL(ndr, ndr->offset, v);
320 ndr->offset += 2;
321 return NT_STATUS_OK;
322}
323
324/*
325 push a int32_t
326*/
327NTSTATUS ndr_push_int32(struct ndr_push *ndr, int ndr_flags, int32_t v)
328{
329 NDR_PUSH_ALIGN(ndr, 4);
330 NDR_PUSH_NEED_BYTES(ndr, 4);
331 NDR_SIVALS(ndr, ndr->offset, v);
332 ndr->offset += 4;
333 return NT_STATUS_OK;
334}
335
336/*
337 push a uint32_t
338*/
339NTSTATUS ndr_push_uint32(struct ndr_push *ndr, int ndr_flags, uint32_t v)
340{
341 NDR_PUSH_ALIGN(ndr, 4);
342 NDR_PUSH_NEED_BYTES(ndr, 4);
343 NDR_SIVAL(ndr, ndr->offset, v);
344 ndr->offset += 4;
345 return NT_STATUS_OK;
346}
347
348/*
349 push a udlong
350*/
351NTSTATUS ndr_push_udlong(struct ndr_push *ndr, int ndr_flags, uint64_t v)
352{
353 NDR_PUSH_ALIGN(ndr, 4);
354 NDR_PUSH_NEED_BYTES(ndr, 8);
355 NDR_SIVAL(ndr, ndr->offset, (v & 0xFFFFFFFF));
356 NDR_SIVAL(ndr, ndr->offset+4, (v>>32));
357 ndr->offset += 8;
358 return NT_STATUS_OK;
359}
360
361/*
362 push a udlongr
363*/
364NTSTATUS ndr_push_udlongr(struct ndr_push *ndr, int ndr_flags, uint64_t v)
365{
366 NDR_PUSH_ALIGN(ndr, 4);
367 NDR_PUSH_NEED_BYTES(ndr, 8);
368 NDR_SIVAL(ndr, ndr->offset, (v>>32));
369 NDR_SIVAL(ndr, ndr->offset+4, (v & 0xFFFFFFFF));
370 ndr->offset += 8;
371 return NT_STATUS_OK;
372}
373
374/*
375 push a dlong
376*/
377NTSTATUS ndr_push_dlong(struct ndr_push *ndr, int ndr_flags, int64_t v)
378{
379 return ndr_push_udlong(ndr, NDR_SCALARS, (uint64_t)v);
380}
381
382/*
383 push a hyper
384*/
385NTSTATUS ndr_push_hyper(struct ndr_push *ndr, int ndr_flags, uint64_t v)
386{
387 NDR_PUSH_ALIGN(ndr, 8);
388 return ndr_push_udlong(ndr, NDR_SCALARS, v);
389}
390
391/*
392 push a pointer
393*/
394NTSTATUS ndr_push_pointer(struct ndr_push *ndr, int ndr_flags, void* v)
395{
396 intptr_t h = (intptr_t)v;
397 NDR_PUSH_ALIGN(ndr, sizeof(h));
398 NDR_PUSH_NEED_BYTES(ndr, sizeof(h));
399 memcpy(ndr->data+ndr->offset, &h, sizeof(h));
400 ndr->offset += sizeof(h);
401 return NT_STATUS_OK;
402}
403
404NTSTATUS ndr_push_align(struct ndr_push *ndr, size_t size)
405{
406 NDR_PUSH_ALIGN(ndr, size);
407 return NT_STATUS_OK;
408}
409
410NTSTATUS ndr_pull_align(struct ndr_pull *ndr, size_t size)
411{
412 NDR_PULL_ALIGN(ndr, size);
413 return NT_STATUS_OK;
414}
415
416/*
417 push some bytes
418*/
419NTSTATUS ndr_push_bytes(struct ndr_push *ndr, const uint8_t *data, uint32_t n)
420{
421 NDR_PUSH_NEED_BYTES(ndr, n);
422 memcpy(ndr->data + ndr->offset, data, n);
423 ndr->offset += n;
424 return NT_STATUS_OK;
425}
426
427/*
428 push some zero bytes
429*/
430NTSTATUS ndr_push_zero(struct ndr_push *ndr, uint32_t n)
431{
432 NDR_PUSH_NEED_BYTES(ndr, n);
433 memset(ndr->data + ndr->offset, 0, n);
434 ndr->offset += n;
435 return NT_STATUS_OK;
436}
437
438/*
439 push an array of uint8
440*/
441NTSTATUS ndr_push_array_uint8(struct ndr_push *ndr, int ndr_flags, const uint8_t *data, uint32_t n)
442{
443 if (!(ndr_flags & NDR_SCALARS)) {
444 return NT_STATUS_OK;
445 }
446 return ndr_push_bytes(ndr, data, n);
447}
448
449/*
450 save the current position
451 */
452void ndr_push_save(struct ndr_push *ndr, struct ndr_push_save *save)
453{
454 save->offset = ndr->offset;
455}
456
457/*
458 restore the position
459 */
460void ndr_push_restore(struct ndr_push *ndr, struct ndr_push_save *save)
461{
462 ndr->offset = save->offset;
463}
464
465/*
466 push a unique non-zero value if a pointer is non-NULL, otherwise 0
467*/
468NTSTATUS ndr_push_unique_ptr(struct ndr_push *ndr, const void *p)
469{
470 uint32_t ptr = 0;
471 if (p) {
472 ptr = ndr->ptr_count * 4;
473 ptr |= 0x00020000;
474 ndr->ptr_count++;
475 }
476 return ndr_push_uint32(ndr, NDR_SCALARS, ptr);
477}
478
479/*
480 push a 'simple' full non-zero value if a pointer is non-NULL, otherwise 0
481*/
482NTSTATUS ndr_push_sptr_ptr(struct ndr_push *ndr, const void *p)
483{
484 uint32_t ptr = 0;
485 if (p) {
486 ndr->ptr_count++;
487 ptr = ndr->ptr_count;
488 }
489 return ndr_push_uint32(ndr, NDR_SCALARS, ptr);
490}
491
492/*
493 push always a 0, if a pointer is NULL it's a fatal error
494*/
495NTSTATUS ndr_push_ref_ptr(struct ndr_push *ndr)
496{
497 return ndr_push_uint32(ndr, NDR_SCALARS, 0xAEF1AEF1);
498}
499
500/*
501 push a NTTIME
502*/
503NTSTATUS ndr_push_NTTIME(struct ndr_push *ndr, int ndr_flags, NTTIME t)
504{
505 NDR_CHECK(ndr_push_udlong(ndr, ndr_flags, t));
506 return NT_STATUS_OK;
507}
508
509/*
510 pull a NTTIME
511*/
512NTSTATUS ndr_pull_NTTIME(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
513{
514 NDR_CHECK(ndr_pull_udlong(ndr, ndr_flags, t));
515 return NT_STATUS_OK;
516}
517
518/*
519 push a NTTIME
520*/
521NTSTATUS ndr_push_NTTIME_1sec(struct ndr_push *ndr, int ndr_flags, NTTIME t)
522{
523 t /= 10000000;
524 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
525 return NT_STATUS_OK;
526}
527
528/*
529 pull a NTTIME_1sec
530*/
531NTSTATUS ndr_pull_NTTIME_1sec(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
532{
533 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
534 (*t) *= 10000000;
535 return NT_STATUS_OK;
536}
537
538/*
539 pull a NTTIME_hyper
540*/
541NTSTATUS ndr_pull_NTTIME_hyper(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
542{
543 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
544 return NT_STATUS_OK;
545}
546
547/*
548 push a NTTIME_hyper
549*/
550NTSTATUS ndr_push_NTTIME_hyper(struct ndr_push *ndr, int ndr_flags, NTTIME t)
551{
552 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
553 return NT_STATUS_OK;
554}
555
556/*
557 push a time_t
558*/
559NTSTATUS ndr_push_time_t(struct ndr_push *ndr, int ndr_flags, time_t t)
560{
561 return ndr_push_uint32(ndr, ndr_flags, t);
562}
563
564/*
565 pull a time_t
566*/
567NTSTATUS ndr_pull_time_t(struct ndr_pull *ndr, int ndr_flags, time_t *t)
568{
569 uint32_t tt;
570 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &tt));
571 *t = tt;
572 return NT_STATUS_OK;
573}
574
575
576void ndr_print_struct(struct ndr_print *ndr, const char *name, const char *type)
577{
578 ndr->print(ndr, "%s: struct %s", name, type);
579}
580
581void ndr_print_enum(struct ndr_print *ndr, const char *name, const char *type,
582 const char *val, uint32_t value)
583{
584 if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
585 ndr->print(ndr, "%-25s: %s (0x%X)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
586 } else {
587 ndr->print(ndr, "%-25s: %s (%d)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
588 }
589}
590
591void ndr_print_bitmap_flag(struct ndr_print *ndr, size_t size, const char *flag_name, uint32_t flag, uint32_t value)
592{
593 /* this is an attempt to support multi-bit bitmap masks */
594 value &= flag;
595
596 while (!(flag & 1)) {
597 flag >>= 1;
598 value >>= 1;
599 }
600 if (flag == 1) {
601 ndr->print(ndr, " %d: %-25s", value, flag_name);
602 } else {
603 ndr->print(ndr, "0x%02x: %-25s (%d)", value, flag_name, value);
604 }
605}
606
607void ndr_print_int8(struct ndr_print *ndr, const char *name, int8_t v)
608{
609 ndr->print(ndr, "%-25s: %d", name, v);
610}
611
612void ndr_print_uint8(struct ndr_print *ndr, const char *name, uint8_t v)
613{
614 ndr->print(ndr, "%-25s: 0x%02x (%u)", name, v, v);
615}
616
617void ndr_print_int16(struct ndr_print *ndr, const char *name, int16_t v)
618{
619 ndr->print(ndr, "%-25s: %d", name, v);
620}
621
622void ndr_print_uint16(struct ndr_print *ndr, const char *name, uint16_t v)
623{
624 ndr->print(ndr, "%-25s: 0x%04x (%u)", name, v, v);
625}
626
627void ndr_print_int32(struct ndr_print *ndr, const char *name, int32_t v)
628{
629 ndr->print(ndr, "%-25s: %d", name, v);
630}
631
632void ndr_print_uint32(struct ndr_print *ndr, const char *name, uint32_t v)
633{
634 ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
635}
636
637void ndr_print_udlong(struct ndr_print *ndr, const char *name, uint64_t v)
638{
639 ndr->print(ndr, "%-25s: 0x%016llx (%llu)", name, v, v);
640}
641
642void ndr_print_udlongr(struct ndr_print *ndr, const char *name, uint64_t v)
643{
644 ndr_print_udlong(ndr, name, v);
645}
646
647void ndr_print_dlong(struct ndr_print *ndr, const char *name, int64_t v)
648{
649 ndr->print(ndr, "%-25s: 0x%016llx (%lld)", name, v, v);
650}
651
652void ndr_print_hyper(struct ndr_print *ndr, const char *name, uint64_t v)
653{
654 ndr_print_dlong(ndr, name, v);
655}
656
657void ndr_print_pointer(struct ndr_print *ndr, const char *name, void *v)
658{
659 ndr->print(ndr, "%-25s: %p", name, v);
660}
661
662void ndr_print_ptr(struct ndr_print *ndr, const char *name, const void *p)
663{
664 if (p) {
665 ndr->print(ndr, "%-25s: *", name);
666 } else {
667 ndr->print(ndr, "%-25s: NULL", name);
668 }
669}
670
671void ndr_print_NTTIME(struct ndr_print *ndr, const char *name, NTTIME t)
672{
673 ndr->print(ndr, "%-25s: %s", name, nt_time_string(ndr, t));
674}
675
676void ndr_print_NTTIME_1sec(struct ndr_print *ndr, const char *name, NTTIME t)
677{
678 /* this is a standard NTTIME here
679 * as it's already converted in the pull/push code
680 */
681 ndr_print_NTTIME(ndr, name, t);
682}
683
684void ndr_print_NTTIME_hyper(struct ndr_print *ndr, const char *name, NTTIME t)
685{
686 ndr_print_NTTIME(ndr, name, t);
687}
688
689void ndr_print_time_t(struct ndr_print *ndr, const char *name, time_t t)
690{
691 if (t == (time_t)-1 || t == 0) {
692 ndr->print(ndr, "%-25s: (time_t)%d", name, (int)t);
693 } else {
694 ndr->print(ndr, "%-25s: %s", name, timestring(ndr, t));
695 }
696}
697
698void ndr_print_union(struct ndr_print *ndr, const char *name, int level, const char *type)
699{
700 if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
701 ndr->print(ndr, "%-25s: union %s(case 0x%X)", name, type, level);
702 } else {
703 ndr->print(ndr, "%-25s: union %s(case %d)", name, type, level);
704 }
705}
706
707void ndr_print_bad_level(struct ndr_print *ndr, const char *name, uint16_t level)
708{
709 ndr->print(ndr, "UNKNOWN LEVEL %u", level);
710}
711
712void ndr_print_array_uint8(struct ndr_print *ndr, const char *name,
713 const uint8_t *data, uint32_t count)
714{
715 int i;
716
717 if (count <= 600 && (ndr->flags & LIBNDR_PRINT_ARRAY_HEX)) {
718 char s[1202];
719 for (i=0;i<count;i++) {
720 snprintf(&s[i*2], 3, "%02x", data[i]);
721 }
722 s[i*2] = 0;
723 ndr->print(ndr, "%-25s: %s", name, s);
724 return;
725 }
726
727 ndr->print(ndr, "%s: ARRAY(%d)", name, count);
728 ndr->depth++;
729 for (i=0;i<count;i++) {
730 char *idx=NULL;
731 asprintf(&idx, "[%d]", i);
732 if (idx) {
733 ndr_print_uint8(ndr, idx, data[i]);
734 free(idx);
735 }
736 }
737 ndr->depth--;
738}
739
740void ndr_print_DATA_BLOB(struct ndr_print *ndr, const char *name, DATA_BLOB r)
741{
742 ndr->print(ndr, "%-25s: DATA_BLOB length=%u", name, r.length);
743 if (r.length) {
744 dump_data(10, (const char *)r.data, r.length);
745 }
746}
747
748
749/*
750 push a DATA_BLOB onto the wire.
751*/
752NTSTATUS ndr_push_DATA_BLOB(struct ndr_push *ndr, int ndr_flags, DATA_BLOB blob)
753{
754 if (ndr->flags & LIBNDR_ALIGN_FLAGS) {
755 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
756 blob.length = NDR_ALIGN(ndr, 2);
757 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
758 blob.length = NDR_ALIGN(ndr, 4);
759 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
760 blob.length = NDR_ALIGN(ndr, 8);
761 }
762 NDR_PUSH_ALLOC_SIZE(ndr, blob.data, blob.length);
763 data_blob_clear(&blob);
764 } else if (!(ndr->flags & LIBNDR_FLAG_REMAINING)) {
765 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, blob.length));
766 }
767 NDR_CHECK(ndr_push_bytes(ndr, blob.data, blob.length));
768 return NT_STATUS_OK;
769}
770
771/*
772 pull a DATA_BLOB from the wire.
773*/
774NTSTATUS ndr_pull_DATA_BLOB(struct ndr_pull *ndr, int ndr_flags, DATA_BLOB *blob)
775{
776 uint32_t length = 0;
777
778 if (ndr->flags & LIBNDR_ALIGN_FLAGS) {
779 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
780 length = NDR_ALIGN(ndr, 2);
781 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
782 length = NDR_ALIGN(ndr, 4);
783 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
784 length = NDR_ALIGN(ndr, 8);
785 }
786 if (ndr->data_size - ndr->offset < length) {
787 length = ndr->data_size - ndr->offset;
788 }
789 } else if (ndr->flags & LIBNDR_FLAG_REMAINING) {
790 length = ndr->data_size - ndr->offset;
791 } else {
792 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &length));
793 }
794 NDR_PULL_NEED_BYTES(ndr, length);
795 *blob = data_blob_talloc(ndr->current_mem_ctx, ndr->data+ndr->offset, length);
796 ndr->offset += length;
797 return NT_STATUS_OK;
798}
799
800uint32_t ndr_size_DATA_BLOB(int ret, const DATA_BLOB *data, int flags)
801{
802 return ret + data->length;
803}
Note: See TracBrowser for help on using the repository browser.