31a1c816b4ac1f4a3f0ae40824f410a694550ced
[samba.git] / source / librpc / ndr / ndr_basic.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    routines for marshalling/unmarshalling basic types
5
6    Copyright (C) Andrew Tridgell 2003
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "system/network.h"
25
26 #define NDR_SVAL(ndr, ofs) (NDR_BE(ndr)?RSVAL(ndr->data,ofs):SVAL(ndr->data,ofs))
27 #define NDR_IVAL(ndr, ofs) (NDR_BE(ndr)?RIVAL(ndr->data,ofs):IVAL(ndr->data,ofs))
28 #define NDR_IVALS(ndr, ofs) (NDR_BE(ndr)?RIVALS(ndr->data,ofs):IVALS(ndr->data,ofs))
29 #define NDR_SSVAL(ndr, ofs, v) do { if (NDR_BE(ndr))  { RSSVAL(ndr->data,ofs,v); } else SSVAL(ndr->data,ofs,v); } while (0)
30 #define NDR_SIVAL(ndr, ofs, v) do { if (NDR_BE(ndr))  { RSIVAL(ndr->data,ofs,v); } else SIVAL(ndr->data,ofs,v); } while (0)
31 #define NDR_SIVALS(ndr, ofs, v) do { if (NDR_BE(ndr))  { RSIVALS(ndr->data,ofs,v); } else SIVALS(ndr->data,ofs,v); } while (0)
32
33
34 /*
35   check for data leaks from the server by looking for non-zero pad bytes
36   these could also indicate that real structure elements have been
37   mistaken for padding in the IDL
38 */
39 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: ", 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 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 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 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 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 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 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 NTSTATUS ndr_pull_unique_ptr(struct ndr_pull *ndr, uint32_t *v)
132 {
133         NTSTATUS status;
134         status = ndr_pull_uint32(ndr, NDR_SCALARS, v);
135         if (*v != 0) {
136                 ndr->ptr_count++;
137         }
138         return status;
139 }
140
141 /*
142   parse a ref pointer referent identifier
143 */
144 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 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 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 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 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   pull a NTSTATUS
197 */
198 NTSTATUS ndr_pull_NTSTATUS(struct ndr_pull *ndr, int ndr_flags, NTSTATUS *status)
199 {
200         uint32_t v;
201         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
202         *status = NT_STATUS(v);
203         return NT_STATUS_OK;
204 }
205
206 /*
207   push a NTSTATUS
208 */
209 NTSTATUS ndr_push_NTSTATUS(struct ndr_push *ndr, int ndr_flags, NTSTATUS status)
210 {
211         return ndr_push_uint32(ndr, ndr_flags, NT_STATUS_V(status));
212 }
213
214 void ndr_print_NTSTATUS(struct ndr_print *ndr, const char *name, NTSTATUS r)
215 {
216         ndr->print(ndr, "%-25s: %s", name, nt_errstr(r));
217 }
218
219 /*
220   pull a WERROR
221 */
222 NTSTATUS ndr_pull_WERROR(struct ndr_pull *ndr, int ndr_flags, WERROR *status)
223 {
224         uint32_t v;
225         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &v));
226         *status = W_ERROR(v);
227         return NT_STATUS_OK;
228 }
229
230 /*
231   push a WERROR
232 */
233 NTSTATUS ndr_push_WERROR(struct ndr_push *ndr, int ndr_flags, WERROR status)
234 {
235         return ndr_push_uint32(ndr, NDR_SCALARS, W_ERROR_V(status));
236 }
237
238 void ndr_print_WERROR(struct ndr_print *ndr, const char *name, WERROR r)
239 {
240         ndr->print(ndr, "%-25s: %s", name, win_errstr(r));
241 }
242
243 /*
244   parse a set of bytes
245 */
246 NTSTATUS ndr_pull_bytes(struct ndr_pull *ndr, uint8_t *data, uint32_t n)
247 {
248         NDR_PULL_NEED_BYTES(ndr, n);
249         memcpy(data, ndr->data + ndr->offset, n);
250         ndr->offset += n;
251         return NT_STATUS_OK;
252 }
253
254 /*
255   pull an array of uint8
256 */
257 NTSTATUS ndr_pull_array_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *data, uint32_t n)
258 {
259         if (!(ndr_flags & NDR_SCALARS)) {
260                 return NT_STATUS_OK;
261         }
262         return ndr_pull_bytes(ndr, data, n);
263 }
264
265 /*
266   pull an array of uint16
267 */
268 NTSTATUS ndr_pull_array_uint16(struct ndr_pull *ndr, int ndr_flags, uint16_t *data, uint32_t n)
269 {
270         uint32_t i;
271         if (!(ndr_flags & NDR_SCALARS)) {
272                 return NT_STATUS_OK;
273         }
274         for (i=0;i<n;i++) {
275                 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &data[i]));
276         }
277         return NT_STATUS_OK;
278 }
279
280 /*
281   pull a const array of uint32_t
282 */
283 NTSTATUS ndr_pull_array_uint32(struct ndr_pull *ndr, int ndr_flags, uint32_t *data, uint32_t n)
284 {
285         uint32_t i;
286         if (!(ndr_flags & NDR_SCALARS)) {
287                 return NT_STATUS_OK;
288         }
289         for (i=0;i<n;i++) {
290                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &data[i]));
291         }
292         return NT_STATUS_OK;
293 }
294
295 /*
296   pull a const array of hyper
297 */
298 NTSTATUS ndr_pull_array_hyper(struct ndr_pull *ndr, int ndr_flags, uint64_t *data, uint32_t n)
299 {
300         uint32_t i;
301         if (!(ndr_flags & NDR_SCALARS)) {
302                 return NT_STATUS_OK;
303         }
304         for (i=0;i<n;i++) {
305                 NDR_CHECK(ndr_pull_hyper(ndr, NDR_SCALARS, &data[i]));
306         }
307         return NT_STATUS_OK;
308 }
309
310 /*
311   pull a const array of WERROR
312 */
313 NTSTATUS ndr_pull_array_WERROR(struct ndr_pull *ndr, int ndr_flags, WERROR *data, uint32_t n)
314 {
315         uint32_t i;
316         if (!(ndr_flags & NDR_SCALARS)) {
317                 return NT_STATUS_OK;
318         }
319         for (i=0;i<n;i++) {
320                 NDR_CHECK(ndr_pull_WERROR(ndr, NDR_SCALARS, &data[i]));
321         }
322         return NT_STATUS_OK;
323 }
324
325 /*
326   push a int8_t
327 */
328 NTSTATUS ndr_push_int8(struct ndr_push *ndr, int ndr_flags, int8_t v)
329 {
330         NDR_PUSH_NEED_BYTES(ndr, 1);
331         SCVAL(ndr->data, ndr->offset, (uint8_t)v);
332         ndr->offset += 1;
333         return NT_STATUS_OK;
334 }
335
336 /*
337   push a uint8_t
338 */
339 NTSTATUS ndr_push_uint8(struct ndr_push *ndr, int ndr_flags, uint8_t v)
340 {
341         NDR_PUSH_NEED_BYTES(ndr, 1);
342         SCVAL(ndr->data, ndr->offset, v);
343         ndr->offset += 1;
344         return NT_STATUS_OK;
345 }
346
347 /*
348   push a int16_t
349 */
350 NTSTATUS ndr_push_int16(struct ndr_push *ndr, int ndr_flags, int16_t v)
351 {
352         NDR_PUSH_ALIGN(ndr, 2);
353         NDR_PUSH_NEED_BYTES(ndr, 2);
354         NDR_SSVAL(ndr, ndr->offset, (uint16_t)v);
355         ndr->offset += 2;
356         return NT_STATUS_OK;
357 }
358
359 /*
360   push a uint16_t
361 */
362 NTSTATUS ndr_push_uint16(struct ndr_push *ndr, int ndr_flags, uint16_t v)
363 {
364         NDR_PUSH_ALIGN(ndr, 2);
365         NDR_PUSH_NEED_BYTES(ndr, 2);
366         NDR_SSVAL(ndr, ndr->offset, v);
367         ndr->offset += 2;
368         return NT_STATUS_OK;
369 }
370
371 /*
372   push a int32_t
373 */
374 NTSTATUS ndr_push_int32(struct ndr_push *ndr, int ndr_flags, int32_t v)
375 {
376         NDR_PUSH_ALIGN(ndr, 4);
377         NDR_PUSH_NEED_BYTES(ndr, 4);
378         NDR_SIVALS(ndr, ndr->offset, v);
379         ndr->offset += 4;
380         return NT_STATUS_OK;
381 }
382
383 /*
384   push a uint32_t
385 */
386 NTSTATUS ndr_push_uint32(struct ndr_push *ndr, int ndr_flags, uint32_t v)
387 {
388         NDR_PUSH_ALIGN(ndr, 4);
389         NDR_PUSH_NEED_BYTES(ndr, 4);
390         NDR_SIVAL(ndr, ndr->offset, v);
391         ndr->offset += 4;
392         return NT_STATUS_OK;
393 }
394
395 /*
396   push a udlong
397 */
398 NTSTATUS ndr_push_udlong(struct ndr_push *ndr, int ndr_flags, uint64_t v)
399 {
400         NDR_PUSH_ALIGN(ndr, 4);
401         NDR_PUSH_NEED_BYTES(ndr, 8);
402         NDR_SIVAL(ndr, ndr->offset, (v & 0xFFFFFFFF));
403         NDR_SIVAL(ndr, ndr->offset+4, (v>>32));
404         ndr->offset += 8;
405         return NT_STATUS_OK;
406 }
407
408 /*
409   push a udlongr
410 */
411 NTSTATUS ndr_push_udlongr(struct ndr_push *ndr, int ndr_flags, uint64_t v)
412 {
413         NDR_PUSH_ALIGN(ndr, 4);
414         NDR_PUSH_NEED_BYTES(ndr, 8);
415         NDR_SIVAL(ndr, ndr->offset+4, (v>>32));
416         NDR_SIVAL(ndr, ndr->offset, (v & 0xFFFFFFFF));
417         ndr->offset += 8;
418         return NT_STATUS_OK;
419 }
420
421 /*
422   push a dlong
423 */
424 NTSTATUS ndr_push_dlong(struct ndr_push *ndr, int ndr_flags, int64_t v)
425 {
426         return ndr_push_udlong(ndr, NDR_SCALARS, (uint64_t)v);
427 }
428
429 /*
430   push a hyper
431 */
432 NTSTATUS ndr_push_hyper(struct ndr_push *ndr, int ndr_flags, uint64_t v)
433 {
434         NDR_PUSH_ALIGN(ndr, 8);
435         return ndr_push_udlong(ndr, NDR_SCALARS, v);
436 }
437
438 NTSTATUS ndr_push_align(struct ndr_push *ndr, size_t size)
439 {
440         NDR_PUSH_ALIGN(ndr, size);
441         return NT_STATUS_OK;
442 }
443
444 NTSTATUS ndr_pull_align(struct ndr_pull *ndr, size_t size)
445 {
446         NDR_PULL_ALIGN(ndr, size);
447         return NT_STATUS_OK;
448 }
449
450 /*
451   push some bytes
452 */
453 NTSTATUS ndr_push_bytes(struct ndr_push *ndr, const uint8_t *data, uint32_t n)
454 {
455         NDR_PUSH_NEED_BYTES(ndr, n);
456         memcpy(ndr->data + ndr->offset, data, n);
457         ndr->offset += n;
458         return NT_STATUS_OK;
459 }
460
461 /*
462   push some zero bytes
463 */
464 NTSTATUS ndr_push_zero(struct ndr_push *ndr, uint32_t n)
465 {
466         NDR_PUSH_NEED_BYTES(ndr, n);
467         memset(ndr->data + ndr->offset, 0, n);
468         ndr->offset += n;
469         return NT_STATUS_OK;
470 }
471
472 /*
473   push an array of uint8
474 */
475 NTSTATUS ndr_push_array_uint8(struct ndr_push *ndr, int ndr_flags, const uint8_t *data, uint32_t n)
476 {
477         if (!(ndr_flags & NDR_SCALARS)) {
478                 return NT_STATUS_OK;
479         }
480         return ndr_push_bytes(ndr, data, n);
481 }
482
483 /*
484   push an array of uint16
485 */
486 NTSTATUS ndr_push_array_uint16(struct ndr_push *ndr, int ndr_flags, const uint16_t *data, uint32_t n)
487 {
488         int i;
489         if (!(ndr_flags & NDR_SCALARS)) {
490                 return NT_STATUS_OK;
491         }
492         for (i=0;i<n;i++) {
493                 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, data[i]));
494         }
495         return NT_STATUS_OK;
496 }
497
498 /*
499   push an array of uint32_t
500 */
501 NTSTATUS ndr_push_array_uint32(struct ndr_push *ndr, int ndr_flags, const uint32_t *data, uint32_t n)
502 {
503         int i;
504         if (!(ndr_flags & NDR_SCALARS)) {
505                 return NT_STATUS_OK;
506         }
507         for (i=0;i<n;i++) {
508                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, data[i]));
509         }
510         return NT_STATUS_OK;
511 }
512
513 /*
514   push an array of hyper
515 */
516 NTSTATUS ndr_push_array_hyper(struct ndr_push *ndr, int ndr_flags, const uint64_t *data, uint32_t n)
517 {
518         int i;
519         if (!(ndr_flags & NDR_SCALARS)) {
520                 return NT_STATUS_OK;
521         }
522         for (i=0;i<n;i++) {
523                 NDR_CHECK(ndr_push_hyper(ndr, NDR_SCALARS, data[i]));
524         }
525         return NT_STATUS_OK;
526 }
527
528 /*
529   push an array of hyper
530 */
531 NTSTATUS ndr_push_array_WERROR(struct ndr_push *ndr, int ndr_flags, const WERROR *data, uint32_t n)
532 {
533         int i;
534         if (!(ndr_flags & NDR_SCALARS)) {
535                 return NT_STATUS_OK;
536         }
537         for (i=0;i<n;i++) {
538                 NDR_CHECK(ndr_push_WERROR(ndr, NDR_SCALARS, data[i]));
539         }
540         return NT_STATUS_OK;
541 }
542
543 /*
544   save the current position
545  */
546 void ndr_push_save(struct ndr_push *ndr, struct ndr_push_save *save)
547 {
548         save->offset = ndr->offset;
549 }
550
551 /*
552   restore the position
553  */
554 void ndr_push_restore(struct ndr_push *ndr, struct ndr_push_save *save)
555 {
556         ndr->offset = save->offset;
557 }
558
559 /*
560   push a unique non-zero value if a pointer is non-NULL, otherwise 0
561 */
562 NTSTATUS ndr_push_unique_ptr(struct ndr_push *ndr, const void *p)
563 {
564         uint32_t ptr = 0;
565         if (p) {
566                 ndr->ptr_count++;
567                 ptr = ndr->ptr_count;
568         }
569         return ndr_push_uint32(ndr, NDR_SCALARS, ptr);
570 }
571
572 /*
573   push always a 0, if a pointer is NULL it's a fatal error
574 */
575 NTSTATUS ndr_push_ref_ptr(struct ndr_push *ndr, const void *p)
576 {
577         if (p == NULL) {
578                 return NT_STATUS_INVALID_PARAMETER_MIX;
579         }
580         return ndr_push_uint32(ndr, NDR_SCALARS, 0);
581 }
582
583
584 /*
585   push a NTTIME
586 */
587 NTSTATUS ndr_push_NTTIME(struct ndr_push *ndr, int ndr_flags, NTTIME t)
588 {
589         NDR_CHECK(ndr_push_udlong(ndr, ndr_flags, t));
590         return NT_STATUS_OK;
591 }
592
593 /*
594   pull a NTTIME
595 */
596 NTSTATUS ndr_pull_NTTIME(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
597 {
598         NDR_CHECK(ndr_pull_udlong(ndr, ndr_flags, t));
599         return NT_STATUS_OK;
600 }
601
602 /*
603   push a NTTIME
604 */
605 NTSTATUS ndr_push_NTTIME_1sec(struct ndr_push *ndr, int ndr_flags, NTTIME t)
606 {
607         t /= 10000000;
608         NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
609         return NT_STATUS_OK;
610 }
611
612 /*
613   pull a NTTIME_1sec
614 */
615 NTSTATUS ndr_pull_NTTIME_1sec(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
616 {
617         NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
618         (*t) *= 10000000;
619         return NT_STATUS_OK;
620 }
621
622 /*
623   pull a NTTIME_hyper
624 */
625 NTSTATUS ndr_pull_NTTIME_hyper(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
626 {
627         NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
628         return NT_STATUS_OK;
629 }
630
631 /*
632   push a NTTIME_hyper
633 */
634 NTSTATUS ndr_push_NTTIME_hyper(struct ndr_push *ndr, int ndr_flags, NTTIME t)
635 {
636         NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
637         return NT_STATUS_OK;
638 }
639
640 /*
641   push a time_t
642 */
643 NTSTATUS ndr_push_time_t(struct ndr_push *ndr, int ndr_flags, time_t t)
644 {
645         return ndr_push_uint32(ndr, ndr_flags, t);
646 }
647
648 /*
649   pull a time_t
650 */
651 NTSTATUS ndr_pull_time_t(struct ndr_pull *ndr, int ndr_flags, time_t *t)
652 {
653         uint32_t tt;
654         NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &tt));
655         *t = tt;
656         return NT_STATUS_OK;
657 }
658
659
660 /*
661   pull a ipv4address
662 */
663 NTSTATUS ndr_pull_ipv4address(struct ndr_pull *ndr, int ndr_flags, const char **address)
664 {
665         struct ipv4_addr in;
666         NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &in.addr));
667         in.addr = htonl(in.addr);
668         *address = talloc_strdup(ndr, sys_inet_ntoa(in));
669         NT_STATUS_HAVE_NO_MEMORY(*address);
670         return NT_STATUS_OK;
671 }
672
673 /*
674   push a ipv4address
675 */
676 NTSTATUS ndr_push_ipv4address(struct ndr_push *ndr, int ndr_flags, const char *address)
677 {
678         uint32_t addr = interpret_addr(address);
679         NDR_CHECK(ndr_push_uint32(ndr, ndr_flags, htonl(addr)));
680         return NT_STATUS_OK;
681 }
682
683 /*
684   print a ipv4address
685 */
686 void ndr_print_ipv4address(struct ndr_print *ndr, const char *name, 
687                            const char *address)
688 {
689         ndr->print(ndr, "%-25s: %s", name, address);
690 }
691
692
693 void ndr_print_struct(struct ndr_print *ndr, const char *name, const char *type)
694 {
695         ndr->print(ndr, "%s: struct %s", name, type);
696 }
697
698 void ndr_print_enum(struct ndr_print *ndr, const char *name, const char *type, 
699                     const char *val, uint_t value)
700 {
701         if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
702                 ndr->print(ndr, "%-25s: %s (0x%X)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
703         } else {
704                 ndr->print(ndr, "%-25s: %s (%d)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
705         }
706 }
707
708 void ndr_print_bitmap_flag(struct ndr_print *ndr, size_t size, const char *flag_name, uint_t flag, uint_t value)
709 {
710         /* this is an attempt to support multi-bit bitmap masks */
711         value &= flag;
712
713         while (!(flag & 1)) {
714                 flag >>= 1;
715                 value >>= 1;
716         }       
717         if (flag == 1) {
718                 ndr->print(ndr, "   %d: %-25s", value, flag_name);
719         } else {
720                 ndr->print(ndr, "0x%02x: %-25s (%d)", value, flag_name, value);
721         }
722 }
723
724 void ndr_print_int8(struct ndr_print *ndr, const char *name, int8_t v)
725 {
726         ndr->print(ndr, "%-25s: %d", name, v);
727 }
728
729 void ndr_print_uint8(struct ndr_print *ndr, const char *name, uint8_t v)
730 {
731         ndr->print(ndr, "%-25s: 0x%02x (%u)", name, v, v);
732 }
733
734 void ndr_print_int16(struct ndr_print *ndr, const char *name, int16_t v)
735 {
736         ndr->print(ndr, "%-25s: %d", name, v);
737 }
738
739 void ndr_print_uint16(struct ndr_print *ndr, const char *name, uint16_t v)
740 {
741         ndr->print(ndr, "%-25s: 0x%04x (%u)", name, v, v);
742 }
743
744 void ndr_print_int32(struct ndr_print *ndr, const char *name, int32_t v)
745 {
746         ndr->print(ndr, "%-25s: %d", name, v);
747 }
748
749 void ndr_print_uint32(struct ndr_print *ndr, const char *name, uint32_t v)
750 {
751         ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
752 }
753
754 void ndr_print_udlong(struct ndr_print *ndr, const char *name, uint64_t v)
755 {
756         ndr->print(ndr, "%-25s: 0x%08x%08x (%llu)", name,
757                    (uint32_t)(v >> 32),
758                    (uint32_t)(v & 0xFFFFFFFF),
759                    v);
760 }
761
762 void ndr_print_udlongr(struct ndr_print *ndr, const char *name, uint64_t v)
763 {
764         ndr_print_udlong(ndr, name, v);
765 }
766
767 void ndr_print_dlong(struct ndr_print *ndr, const char *name, int64_t v)
768 {
769         ndr->print(ndr, "%-25s: 0x%08x%08x (%lld)", name, 
770                    (uint32_t)(v >> 32), 
771                    (uint32_t)(v & 0xFFFFFFFF),
772                    v);
773 }
774
775 void ndr_print_hyper(struct ndr_print *ndr, const char *name, uint64_t v)
776 {
777         ndr_print_dlong(ndr, name, v);
778 }
779
780 void ndr_print_ptr(struct ndr_print *ndr, const char *name, const void *p)
781 {
782         if (p) {
783                 ndr->print(ndr, "%-25s: *", name);
784         } else {
785                 ndr->print(ndr, "%-25s: NULL", name);
786         }
787 }
788
789 void ndr_print_NTTIME(struct ndr_print *ndr, const char *name, NTTIME t)
790 {
791         ndr->print(ndr, "%-25s: %s", name, nt_time_string(ndr, t));
792 }
793
794 void ndr_print_NTTIME_1sec(struct ndr_print *ndr, const char *name, NTTIME t)
795 {
796         /* this is a standard NTTIME here
797          * as it's already converted in the pull/push code
798          */
799         ndr_print_NTTIME(ndr, name, t);
800 }
801
802 void ndr_print_NTTIME_hyper(struct ndr_print *ndr, const char *name, NTTIME t)
803 {
804         ndr_print_NTTIME(ndr, name, t);
805 }
806
807 void ndr_print_time_t(struct ndr_print *ndr, const char *name, time_t t)
808 {
809         if (t == (time_t)-1 || t == 0) {
810                 ndr->print(ndr, "%-25s: (time_t)%d", name, (int)t);
811         } else {
812                 ndr->print(ndr, "%-25s: %s", name, timestring(ndr, t));
813         }
814 }
815
816 void ndr_print_union(struct ndr_print *ndr, const char *name, int level, const char *type)
817 {
818         ndr->print(ndr, "%-25s: union %s(case %d)", name, type, level);
819 }
820
821 void ndr_print_bad_level(struct ndr_print *ndr, const char *name, uint16_t level)
822 {
823         ndr->print(ndr, "UNKNOWN LEVEL %u", level);
824 }
825
826 void ndr_print_array_WERROR(struct ndr_print *ndr, const char *name, 
827                             const WERROR *data, uint32_t count)
828 {
829         int i;
830
831         ndr->print(ndr, "%s: ARRAY(%d)", name, count);
832         ndr->depth++;
833         for (i=0;i<count;i++) {
834                 char *idx=NULL;
835                 asprintf(&idx, "[%d]", i);
836                 if (idx) {
837                         ndr_print_WERROR(ndr, idx, data[i]);
838                         free(idx);
839                 }
840         }
841         ndr->depth--;   
842 }
843
844 void ndr_print_array_hyper(struct ndr_print *ndr, const char *name, 
845                             const uint64_t *data, uint32_t count)
846 {
847         int i;
848
849         ndr->print(ndr, "%s: ARRAY(%d)", name, count);
850         ndr->depth++;
851         for (i=0;i<count;i++) {
852                 char *idx=NULL;
853                 asprintf(&idx, "[%d]", i);
854                 if (idx) {
855                         ndr_print_hyper(ndr, idx, data[i]);
856                         free(idx);
857                 }
858         }
859         ndr->depth--;   
860 }
861
862 void ndr_print_array_uint32(struct ndr_print *ndr, const char *name, 
863                             const uint32_t *data, uint32_t count)
864 {
865         int i;
866
867         ndr->print(ndr, "%s: ARRAY(%d)", name, count);
868         ndr->depth++;
869         for (i=0;i<count;i++) {
870                 char *idx=NULL;
871                 asprintf(&idx, "[%d]", i);
872                 if (idx) {
873                         ndr_print_uint32(ndr, idx, data[i]);
874                         free(idx);
875                 }
876         }
877         ndr->depth--;   
878 }
879
880 void ndr_print_array_uint16(struct ndr_print *ndr, const char *name, 
881                             const uint16_t *data, uint32_t count)
882 {
883         int i;
884
885         ndr->print(ndr, "%s: ARRAY(%d)", name, count);
886         ndr->depth++;
887         for (i=0;i<count;i++) {
888                 char *idx=NULL;
889                 asprintf(&idx, "[%d]", i);
890                 if (idx) {
891                         ndr_print_uint16(ndr, idx, data[i]);
892                         free(idx);
893                 }
894         }
895         ndr->depth--;   
896 }
897
898 void ndr_print_array_uint8(struct ndr_print *ndr, const char *name, 
899                            const uint8_t *data, uint32_t count)
900 {
901         int i;
902
903         if (count <= 600 && (ndr->flags & LIBNDR_PRINT_ARRAY_HEX)) {
904                 char s[1202];
905                 for (i=0;i<count;i++) {
906                         snprintf(&s[i*2], 3, "%02x", data[i]);
907                 }
908                 s[i*2] = 0;
909                 ndr->print(ndr, "%-25s: %s", name, s);
910                 return;
911         }
912
913         ndr->print(ndr, "%s: ARRAY(%d)", name, count);
914         ndr->depth++;
915         for (i=0;i<count;i++) {
916                 char *idx=NULL;
917                 asprintf(&idx, "[%d]", i);
918                 if (idx) {
919                         ndr_print_uint8(ndr, idx, data[i]);
920                         free(idx);
921                 }
922         }
923         ndr->depth--;   
924 }
925
926 void ndr_print_DATA_BLOB(struct ndr_print *ndr, const char *name, DATA_BLOB r)
927 {
928         ndr->print(ndr, "%-25s: DATA_BLOB length=%u", name, r.length);
929         if (r.length) {
930                 dump_data(10, r.data, r.length);
931         }
932 }
933
934
935 /*
936   push a DATA_BLOB onto the wire. 
937 */
938 NTSTATUS ndr_push_DATA_BLOB(struct ndr_push *ndr, int ndr_flags, DATA_BLOB blob)
939 {
940         if (ndr->flags & LIBNDR_ALIGN_FLAGS) {
941                 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
942                         blob.length = NDR_ALIGN(ndr, 2);
943                 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
944                         blob.length = NDR_ALIGN(ndr, 4);
945                 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
946                         blob.length = NDR_ALIGN(ndr, 8);
947                 }
948                 NDR_PUSH_ALLOC_SIZE(ndr, blob.data, blob.length);
949                 data_blob_clear(&blob);
950         } else if (!(ndr->flags & LIBNDR_FLAG_REMAINING)) {
951                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, blob.length));
952         }
953         NDR_CHECK(ndr_push_bytes(ndr, blob.data, blob.length));
954         return NT_STATUS_OK;
955 }
956
957 /*
958   pull a DATA_BLOB from the wire. 
959 */
960 NTSTATUS ndr_pull_DATA_BLOB(struct ndr_pull *ndr, int ndr_flags, DATA_BLOB *blob)
961 {
962         uint32_t length;
963
964         if (ndr->flags & LIBNDR_ALIGN_FLAGS) {
965                 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
966                         length = NDR_ALIGN(ndr, 2);
967                 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
968                         length = NDR_ALIGN(ndr, 4);
969                 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
970                         length = NDR_ALIGN(ndr, 8);
971                 }
972                 if (ndr->data_size - ndr->offset < length) {
973                         length = ndr->data_size - ndr->offset;
974                 }
975         } else if (ndr->flags & LIBNDR_FLAG_REMAINING) {
976                 length = ndr->data_size - ndr->offset;
977         } else {
978                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &length));
979         }
980         NDR_PULL_NEED_BYTES(ndr, length);
981         *blob = data_blob_talloc(ndr, ndr->data+ndr->offset, length);
982         ndr->offset += length;
983         return NT_STATUS_OK;
984 }
985
986 uint32_t ndr_size_DATA_BLOB(int ret, const DATA_BLOB *data, int flags)
987 {
988         return ret + data->length;
989 }