r14542: Remove librpc, libndr and libnbt from includes.h
[samba.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, const void *p)
498 {
499         if (p == NULL) {
500                 return NT_STATUS_INVALID_PARAMETER_MIX;
501         }
502         return ndr_push_uint32(ndr, NDR_SCALARS, 0xAEF1AEF1);
503 }
504
505
506 /*
507   push a NTTIME
508 */
509 _PUBLIC_ NTSTATUS ndr_push_NTTIME(struct ndr_push *ndr, int ndr_flags, NTTIME t)
510 {
511         NDR_CHECK(ndr_push_udlong(ndr, ndr_flags, t));
512         return NT_STATUS_OK;
513 }
514
515 /*
516   pull a NTTIME
517 */
518 _PUBLIC_ NTSTATUS ndr_pull_NTTIME(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
519 {
520         NDR_CHECK(ndr_pull_udlong(ndr, ndr_flags, t));
521         return NT_STATUS_OK;
522 }
523
524 /*
525   push a NTTIME
526 */
527 _PUBLIC_ NTSTATUS ndr_push_NTTIME_1sec(struct ndr_push *ndr, int ndr_flags, NTTIME t)
528 {
529         t /= 10000000;
530         NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
531         return NT_STATUS_OK;
532 }
533
534 /*
535   pull a NTTIME_1sec
536 */
537 _PUBLIC_ NTSTATUS ndr_pull_NTTIME_1sec(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
538 {
539         NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
540         (*t) *= 10000000;
541         return NT_STATUS_OK;
542 }
543
544 /*
545   pull a NTTIME_hyper
546 */
547 _PUBLIC_ NTSTATUS ndr_pull_NTTIME_hyper(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
548 {
549         NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
550         return NT_STATUS_OK;
551 }
552
553 /*
554   push a NTTIME_hyper
555 */
556 _PUBLIC_ NTSTATUS ndr_push_NTTIME_hyper(struct ndr_push *ndr, int ndr_flags, NTTIME t)
557 {
558         NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
559         return NT_STATUS_OK;
560 }
561
562 /*
563   push a time_t
564 */
565 _PUBLIC_ NTSTATUS ndr_push_time_t(struct ndr_push *ndr, int ndr_flags, time_t t)
566 {
567         return ndr_push_uint32(ndr, ndr_flags, t);
568 }
569
570 /*
571   pull a time_t
572 */
573 _PUBLIC_ NTSTATUS ndr_pull_time_t(struct ndr_pull *ndr, int ndr_flags, time_t *t)
574 {
575         uint32_t tt;
576         NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &tt));
577         *t = tt;
578         return NT_STATUS_OK;
579 }
580
581
582 /*
583   pull a ipv4address
584 */
585 _PUBLIC_ NTSTATUS ndr_pull_ipv4address(struct ndr_pull *ndr, int ndr_flags, const char **address)
586 {
587         struct ipv4_addr in;
588         NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &in.addr));
589         in.addr = htonl(in.addr);
590         *address = talloc_strdup(ndr->current_mem_ctx, sys_inet_ntoa(in));
591         NT_STATUS_HAVE_NO_MEMORY(*address);
592         return NT_STATUS_OK;
593 }
594
595 /*
596   push a ipv4address
597 */
598 _PUBLIC_ NTSTATUS ndr_push_ipv4address(struct ndr_push *ndr, int ndr_flags, const char *address)
599 {
600         uint32_t addr;
601         if (!is_ipaddress(address)) {
602                 return ndr_push_error(ndr, NDR_ERR_IPV4ADDRESS,
603                                       "Invalid IPv4 address: '%s'", 
604                                       address);
605         }
606         addr = inet_addr(address);
607         NDR_CHECK(ndr_push_uint32(ndr, ndr_flags, htonl(addr)));
608         return NT_STATUS_OK;
609 }
610
611 /*
612   print a ipv4address
613 */
614 _PUBLIC_ void ndr_print_ipv4address(struct ndr_print *ndr, const char *name, 
615                            const char *address)
616 {
617         ndr->print(ndr, "%-25s: %s", name, address);
618 }
619
620
621 _PUBLIC_ void ndr_print_struct(struct ndr_print *ndr, const char *name, const char *type)
622 {
623         ndr->print(ndr, "%s: struct %s", name, type);
624 }
625
626 _PUBLIC_ void ndr_print_enum(struct ndr_print *ndr, const char *name, const char *type, 
627                     const char *val, uint_t value)
628 {
629         if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
630                 ndr->print(ndr, "%-25s: %s (0x%X)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
631         } else {
632                 ndr->print(ndr, "%-25s: %s (%d)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
633         }
634 }
635
636 _PUBLIC_ void ndr_print_bitmap_flag(struct ndr_print *ndr, size_t size, const char *flag_name, uint_t flag, uint_t value)
637 {
638         /* this is an attempt to support multi-bit bitmap masks */
639         value &= flag;
640
641         while (!(flag & 1)) {
642                 flag >>= 1;
643                 value >>= 1;
644         }       
645         if (flag == 1) {
646                 ndr->print(ndr, "   %d: %-25s", value, flag_name);
647         } else {
648                 ndr->print(ndr, "0x%02x: %-25s (%d)", value, flag_name, value);
649         }
650 }
651
652 _PUBLIC_ void ndr_print_int8(struct ndr_print *ndr, const char *name, int8_t v)
653 {
654         ndr->print(ndr, "%-25s: %d", name, v);
655 }
656
657 _PUBLIC_ void ndr_print_uint8(struct ndr_print *ndr, const char *name, uint8_t v)
658 {
659         ndr->print(ndr, "%-25s: 0x%02x (%u)", name, v, v);
660 }
661
662 _PUBLIC_ void ndr_print_int16(struct ndr_print *ndr, const char *name, int16_t v)
663 {
664         ndr->print(ndr, "%-25s: %d", name, v);
665 }
666
667 _PUBLIC_ void ndr_print_uint16(struct ndr_print *ndr, const char *name, uint16_t v)
668 {
669         ndr->print(ndr, "%-25s: 0x%04x (%u)", name, v, v);
670 }
671
672 _PUBLIC_ void ndr_print_int32(struct ndr_print *ndr, const char *name, int32_t v)
673 {
674         ndr->print(ndr, "%-25s: %d", name, v);
675 }
676
677 _PUBLIC_ void ndr_print_uint32(struct ndr_print *ndr, const char *name, uint32_t v)
678 {
679         ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
680 }
681
682 _PUBLIC_ void ndr_print_udlong(struct ndr_print *ndr, const char *name, uint64_t v)
683 {
684         ndr->print(ndr, "%-25s: 0x%016llx (%llu)", name, v, v);
685 }
686
687 _PUBLIC_ void ndr_print_udlongr(struct ndr_print *ndr, const char *name, uint64_t v)
688 {
689         ndr_print_udlong(ndr, name, v);
690 }
691
692 _PUBLIC_ void ndr_print_dlong(struct ndr_print *ndr, const char *name, int64_t v)
693 {
694         ndr->print(ndr, "%-25s: 0x%016llx (%lld)", name, v, v);
695 }
696
697 _PUBLIC_ void ndr_print_hyper(struct ndr_print *ndr, const char *name, uint64_t v)
698 {
699         ndr_print_dlong(ndr, name, v);
700 }
701
702 _PUBLIC_ void ndr_print_pointer(struct ndr_print *ndr, const char *name, void *v)
703 {
704         ndr->print(ndr, "%-25s: %p", name, v);
705 }
706
707 _PUBLIC_ void ndr_print_ptr(struct ndr_print *ndr, const char *name, const void *p)
708 {
709         if (p) {
710                 ndr->print(ndr, "%-25s: *", name);
711         } else {
712                 ndr->print(ndr, "%-25s: NULL", name);
713         }
714 }
715
716 _PUBLIC_ void ndr_print_NTTIME(struct ndr_print *ndr, const char *name, NTTIME t)
717 {
718         ndr->print(ndr, "%-25s: %s", name, nt_time_string(ndr, t));
719 }
720
721 _PUBLIC_ void ndr_print_NTTIME_1sec(struct ndr_print *ndr, const char *name, NTTIME t)
722 {
723         /* this is a standard NTTIME here
724          * as it's already converted in the pull/push code
725          */
726         ndr_print_NTTIME(ndr, name, t);
727 }
728
729 _PUBLIC_ void ndr_print_NTTIME_hyper(struct ndr_print *ndr, const char *name, NTTIME t)
730 {
731         ndr_print_NTTIME(ndr, name, t);
732 }
733
734 _PUBLIC_ void ndr_print_time_t(struct ndr_print *ndr, const char *name, time_t t)
735 {
736         if (t == (time_t)-1 || t == 0) {
737                 ndr->print(ndr, "%-25s: (time_t)%d", name, (int)t);
738         } else {
739                 ndr->print(ndr, "%-25s: %s", name, timestring(ndr, t));
740         }
741 }
742
743 _PUBLIC_ void ndr_print_union(struct ndr_print *ndr, const char *name, int level, const char *type)
744 {
745         if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
746                 ndr->print(ndr, "%-25s: union %s(case 0x%X)", name, type, level);
747         } else {
748                 ndr->print(ndr, "%-25s: union %s(case %d)", name, type, level);
749         }
750 }
751
752 _PUBLIC_ void ndr_print_bad_level(struct ndr_print *ndr, const char *name, uint16_t level)
753 {
754         ndr->print(ndr, "UNKNOWN LEVEL %u", level);
755 }
756
757 _PUBLIC_ void ndr_print_array_uint8(struct ndr_print *ndr, const char *name, 
758                            const uint8_t *data, uint32_t count)
759 {
760         int i;
761
762         if (count <= 600 && (ndr->flags & LIBNDR_PRINT_ARRAY_HEX)) {
763                 char s[1202];
764                 for (i=0;i<count;i++) {
765                         snprintf(&s[i*2], 3, "%02x", data[i]);
766                 }
767                 s[i*2] = 0;
768                 ndr->print(ndr, "%-25s: %s", name, s);
769                 return;
770         }
771
772         ndr->print(ndr, "%s: ARRAY(%d)", name, count);
773         ndr->depth++;
774         for (i=0;i<count;i++) {
775                 char *idx=NULL;
776                 asprintf(&idx, "[%d]", i);
777                 if (idx) {
778                         ndr_print_uint8(ndr, idx, data[i]);
779                         free(idx);
780                 }
781         }
782         ndr->depth--;   
783 }
784
785 _PUBLIC_ void ndr_print_DATA_BLOB(struct ndr_print *ndr, const char *name, DATA_BLOB r)
786 {
787         ndr->print(ndr, "%-25s: DATA_BLOB length=%u", name, r.length);
788         if (r.length) {
789                 dump_data(10, r.data, r.length);
790         }
791 }
792
793
794 /*
795   push a DATA_BLOB onto the wire. 
796 */
797 _PUBLIC_ NTSTATUS ndr_push_DATA_BLOB(struct ndr_push *ndr, int ndr_flags, DATA_BLOB blob)
798 {
799         if (ndr->flags & LIBNDR_ALIGN_FLAGS) {
800                 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
801                         blob.length = NDR_ALIGN(ndr, 2);
802                 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
803                         blob.length = NDR_ALIGN(ndr, 4);
804                 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
805                         blob.length = NDR_ALIGN(ndr, 8);
806                 }
807                 NDR_PUSH_ALLOC_SIZE(ndr, blob.data, blob.length);
808                 data_blob_clear(&blob);
809         } else if (!(ndr->flags & LIBNDR_FLAG_REMAINING)) {
810                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, blob.length));
811         }
812         NDR_CHECK(ndr_push_bytes(ndr, blob.data, blob.length));
813         return NT_STATUS_OK;
814 }
815
816 /*
817   pull a DATA_BLOB from the wire. 
818 */
819 _PUBLIC_ NTSTATUS ndr_pull_DATA_BLOB(struct ndr_pull *ndr, int ndr_flags, DATA_BLOB *blob)
820 {
821         uint32_t length = 0;
822
823         if (ndr->flags & LIBNDR_ALIGN_FLAGS) {
824                 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
825                         length = NDR_ALIGN(ndr, 2);
826                 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
827                         length = NDR_ALIGN(ndr, 4);
828                 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
829                         length = NDR_ALIGN(ndr, 8);
830                 }
831                 if (ndr->data_size - ndr->offset < length) {
832                         length = ndr->data_size - ndr->offset;
833                 }
834         } else if (ndr->flags & LIBNDR_FLAG_REMAINING) {
835                 length = ndr->data_size - ndr->offset;
836         } else {
837                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &length));
838         }
839         NDR_PULL_NEED_BYTES(ndr, length);
840         *blob = data_blob_talloc(ndr->current_mem_ctx, ndr->data+ndr->offset, length);
841         ndr->offset += length;
842         return NT_STATUS_OK;
843 }
844
845 _PUBLIC_ uint32_t ndr_size_DATA_BLOB(int ret, const DATA_BLOB *data, int flags)
846 {
847         return ret + data->length;
848 }