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