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