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