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