pidl: added int3264 as a base type
[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         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 NDR_ERR_SUCCESS;
126 }
127
128 /*
129   parse a arch dependent uint32/uint64
130 */
131 _PUBLIC_ enum ndr_err_code ndr_pull_uint3264(struct ndr_pull *ndr, int ndr_flags, uint32_t *v)
132 {
133         uint64_t v64;
134         enum ndr_err_code err;
135         if (likely(!(ndr->flags & LIBNDR_FLAG_NDR64))) {
136                 return ndr_pull_uint32(ndr, ndr_flags, v);
137         }
138         err = ndr_pull_hyper(ndr, ndr_flags, &v64);
139         *v = (uint32_t)v64;
140         if (unlikely(v64 != *v)) {
141                 DEBUG(0,(__location__ ": non-zero upper 32 bits 0x%016llx\n",
142                          (unsigned long long)v64));
143                 return NDR_ERR_NDR64;
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 /*
277   parse a uint8_t enum
278 */
279 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *v)
280 {
281         return ndr_pull_uint8(ndr, ndr_flags, v);
282 }
283
284 /*
285   parse a uint16_t enum (uint32_t on NDR64)
286 */
287 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint16(struct ndr_pull *ndr, int ndr_flags, uint16_t *v)
288 {
289         if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
290                 uint32_t v32;
291                 NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &v32));
292                 *v = v32;
293                 if (v32 != *v) {
294                         DEBUG(0,(__location__ ": non-zero upper 16 bits 0x%08x\n", (unsigned)v32));
295                         return NDR_ERR_NDR64;
296                 }
297                 return NDR_ERR_SUCCESS;
298         }
299         NDR_PULL_ALIGN(ndr, 2);
300         NDR_PULL_NEED_BYTES(ndr, 2);
301         *v = NDR_SVAL(ndr, ndr->offset);
302         ndr->offset += 2;
303         return NDR_ERR_SUCCESS;
304 }
305
306 /*
307   parse a uint32_t enum
308 */
309 _PUBLIC_ enum ndr_err_code ndr_pull_enum_uint32(struct ndr_pull *ndr, int ndr_flags, uint32_t *v)
310 {
311         return ndr_pull_uint32(ndr, ndr_flags, v);
312 }
313
314 /*
315   push a uint8_t enum
316 */
317 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint8(struct ndr_push *ndr, int ndr_flags, uint8_t v)
318 {
319         return ndr_push_uint8(ndr, ndr_flags, v);
320 }
321
322 /*
323   push a uint16_t enum (uint32_t on NDR64)
324 */
325 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint16(struct ndr_push *ndr, int ndr_flags, uint16_t v)
326 {
327         if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
328                 return ndr_push_uint32(ndr, ndr_flags, v);
329         }
330         return ndr_push_uint16(ndr, ndr_flags, v);
331 }
332
333 /*
334   push a uint32_t enum
335 */
336 _PUBLIC_ enum ndr_err_code ndr_push_enum_uint32(struct ndr_push *ndr, int ndr_flags, uint32_t v)
337 {
338         return ndr_push_uint32(ndr, ndr_flags, v);
339 }
340
341
342 /*
343   push a WERROR
344 */
345 _PUBLIC_ enum ndr_err_code ndr_push_WERROR(struct ndr_push *ndr, int ndr_flags, WERROR status)
346 {
347         return ndr_push_uint32(ndr, NDR_SCALARS, W_ERROR_V(status));
348 }
349
350 _PUBLIC_ void ndr_print_WERROR(struct ndr_print *ndr, const char *name, WERROR r)
351 {
352         ndr->print(ndr, "%-25s: %s", name, win_errstr(r));
353 }
354
355 /*
356   parse a set of bytes
357 */
358 _PUBLIC_ enum ndr_err_code ndr_pull_bytes(struct ndr_pull *ndr, uint8_t *data, uint32_t n)
359 {
360         NDR_PULL_NEED_BYTES(ndr, n);
361         memcpy(data, ndr->data + ndr->offset, n);
362         ndr->offset += n;
363         return NDR_ERR_SUCCESS;
364 }
365
366 /*
367   pull an array of uint8
368 */
369 _PUBLIC_ enum ndr_err_code ndr_pull_array_uint8(struct ndr_pull *ndr, int ndr_flags, uint8_t *data, uint32_t n)
370 {
371         if (!(ndr_flags & NDR_SCALARS)) {
372                 return NDR_ERR_SUCCESS;
373         }
374         return ndr_pull_bytes(ndr, data, n);
375 }
376
377 /*
378   push a int8_t
379 */
380 _PUBLIC_ enum ndr_err_code ndr_push_int8(struct ndr_push *ndr, int ndr_flags, int8_t v)
381 {
382         NDR_PUSH_NEED_BYTES(ndr, 1);
383         SCVAL(ndr->data, ndr->offset, (uint8_t)v);
384         ndr->offset += 1;
385         return NDR_ERR_SUCCESS;
386 }
387
388 /*
389   push a uint8_t
390 */
391 _PUBLIC_ enum ndr_err_code ndr_push_uint8(struct ndr_push *ndr, int ndr_flags, uint8_t v)
392 {
393         NDR_PUSH_NEED_BYTES(ndr, 1);
394         SCVAL(ndr->data, ndr->offset, v);
395         ndr->offset += 1;
396         return NDR_ERR_SUCCESS;
397 }
398
399 /*
400   push a int16_t
401 */
402 _PUBLIC_ enum ndr_err_code ndr_push_int16(struct ndr_push *ndr, int ndr_flags, int16_t v)
403 {
404         NDR_PUSH_ALIGN(ndr, 2);
405         NDR_PUSH_NEED_BYTES(ndr, 2);
406         NDR_SSVAL(ndr, ndr->offset, (uint16_t)v);
407         ndr->offset += 2;
408         return NDR_ERR_SUCCESS;
409 }
410
411 /*
412   push a uint16_t
413 */
414 _PUBLIC_ enum ndr_err_code ndr_push_uint16(struct ndr_push *ndr, int ndr_flags, uint16_t v)
415 {
416         NDR_PUSH_ALIGN(ndr, 2);
417         NDR_PUSH_NEED_BYTES(ndr, 2);
418         NDR_SSVAL(ndr, ndr->offset, v);
419         ndr->offset += 2;
420         return NDR_ERR_SUCCESS;
421 }
422
423 /*
424   push a int32_t
425 */
426 _PUBLIC_ enum ndr_err_code ndr_push_int32(struct ndr_push *ndr, int ndr_flags, int32_t v)
427 {
428         NDR_PUSH_ALIGN(ndr, 4);
429         NDR_PUSH_NEED_BYTES(ndr, 4);
430         NDR_SIVALS(ndr, ndr->offset, v);
431         ndr->offset += 4;
432         return NDR_ERR_SUCCESS;
433 }
434
435 /*
436   push a uint32_t
437 */
438 _PUBLIC_ enum ndr_err_code ndr_push_uint32(struct ndr_push *ndr, int ndr_flags, uint32_t v)
439 {
440         NDR_PUSH_ALIGN(ndr, 4);
441         NDR_PUSH_NEED_BYTES(ndr, 4);
442         NDR_SIVAL(ndr, ndr->offset, v);
443         ndr->offset += 4;
444         return NDR_ERR_SUCCESS;
445 }
446
447 /*
448   push a uint3264
449 */
450 _PUBLIC_ enum ndr_err_code ndr_push_uint3264(struct ndr_push *ndr, int ndr_flags, uint32_t v)
451 {
452         if (unlikely(ndr->flags & LIBNDR_FLAG_NDR64)) {
453                 return ndr_push_hyper(ndr, ndr_flags, v);
454         }
455         NDR_PUSH_ALIGN(ndr, 4);
456         NDR_PUSH_NEED_BYTES(ndr, 4);
457         NDR_SIVAL(ndr, ndr->offset, v);
458         ndr->offset += 4;
459         return NDR_ERR_SUCCESS;
460 }
461
462 /*
463   push a udlong
464 */
465 _PUBLIC_ enum ndr_err_code ndr_push_udlong(struct ndr_push *ndr, int ndr_flags, uint64_t v)
466 {
467         NDR_PUSH_ALIGN(ndr, 4);
468         NDR_PUSH_NEED_BYTES(ndr, 8);
469         NDR_SIVAL(ndr, ndr->offset, (v & 0xFFFFFFFF));
470         NDR_SIVAL(ndr, ndr->offset+4, (v>>32));
471         ndr->offset += 8;
472         return NDR_ERR_SUCCESS;
473 }
474
475 /*
476   push a udlongr
477 */
478 _PUBLIC_ enum ndr_err_code ndr_push_udlongr(struct ndr_push *ndr, int ndr_flags, uint64_t v)
479 {
480         NDR_PUSH_ALIGN(ndr, 4);
481         NDR_PUSH_NEED_BYTES(ndr, 8);
482         NDR_SIVAL(ndr, ndr->offset, (v>>32));
483         NDR_SIVAL(ndr, ndr->offset+4, (v & 0xFFFFFFFF));
484         ndr->offset += 8;
485         return NDR_ERR_SUCCESS;
486 }
487
488 /*
489   push a dlong
490 */
491 _PUBLIC_ enum ndr_err_code ndr_push_dlong(struct ndr_push *ndr, int ndr_flags, int64_t v)
492 {
493         return ndr_push_udlong(ndr, NDR_SCALARS, (uint64_t)v);
494 }
495
496 /*
497   push a hyper
498 */
499 _PUBLIC_ enum ndr_err_code ndr_push_hyper(struct ndr_push *ndr, int ndr_flags, uint64_t v)
500 {
501         NDR_PUSH_ALIGN(ndr, 8);
502         return ndr_push_udlong(ndr, NDR_SCALARS, v);
503 }
504
505 /*
506   push a double
507 */
508 _PUBLIC_ enum ndr_err_code ndr_push_double(struct ndr_push *ndr, int ndr_flags, double v)
509 {
510         NDR_PUSH_ALIGN(ndr, 8);
511         NDR_PUSH_NEED_BYTES(ndr, 8);
512         memcpy(ndr->data+ndr->offset, &v, 8);
513         ndr->offset += 8;
514         return NDR_ERR_SUCCESS;
515 }
516
517 /*
518   push a pointer
519 */
520 _PUBLIC_ enum ndr_err_code ndr_push_pointer(struct ndr_push *ndr, int ndr_flags, void* v)
521 {
522         uintptr_t h = (intptr_t)v;
523         NDR_PUSH_ALIGN(ndr, sizeof(h));
524         NDR_PUSH_NEED_BYTES(ndr, sizeof(h));
525         memcpy(ndr->data+ndr->offset, &h, sizeof(h));
526         ndr->offset += sizeof(h);
527         return NDR_ERR_SUCCESS;
528 }
529
530 _PUBLIC_ enum ndr_err_code ndr_push_align(struct ndr_push *ndr, size_t size)
531 {
532         /* this is a nasty hack to make pidl work with NDR64 */
533         if (size == 5) {
534                 if (ndr->flags & LIBNDR_FLAG_NDR64) {
535                         size = 8;
536                 } else {
537                         size = 4;
538                 }
539         }
540         NDR_PUSH_ALIGN(ndr, size);
541         return NDR_ERR_SUCCESS;
542 }
543
544 _PUBLIC_ enum ndr_err_code ndr_pull_align(struct ndr_pull *ndr, size_t size)
545 {
546         /* this is a nasty hack to make pidl work with NDR64 */
547         if (size == 5) {
548                 if (ndr->flags & LIBNDR_FLAG_NDR64) {
549                         size = 8;
550                 } else {
551                         size = 4;
552                 }
553         }
554         NDR_PULL_ALIGN(ndr, size);
555         return NDR_ERR_SUCCESS;
556 }
557
558 _PUBLIC_ enum ndr_err_code ndr_push_union_align(struct ndr_push *ndr, size_t size)
559 {
560         /* MS-RPCE section 2.2.5.3.4.4 */
561         if (ndr->flags & LIBNDR_FLAG_NDR64) {
562                 return ndr_push_align(ndr, size);
563         }
564         return NDR_ERR_SUCCESS;
565 }
566
567 _PUBLIC_ enum ndr_err_code ndr_pull_union_align(struct ndr_pull *ndr, size_t size)
568 {
569         /* MS-RPCE section 2.2.5.3.4.4 */
570         if (ndr->flags & LIBNDR_FLAG_NDR64) {
571                 return ndr_pull_align(ndr, size);
572         }
573         return NDR_ERR_SUCCESS;
574 }
575
576 _PUBLIC_ enum ndr_err_code ndr_push_trailer_align(struct ndr_push *ndr, size_t size)
577 {
578         /* MS-RPCE section 2.2.5.3.4.1 */
579         if (ndr->flags & LIBNDR_FLAG_NDR64) {
580                 return ndr_push_align(ndr, size);
581         }
582         return NDR_ERR_SUCCESS;
583 }
584
585 _PUBLIC_ enum ndr_err_code ndr_pull_trailer_align(struct ndr_pull *ndr, size_t size)
586 {
587         /* MS-RPCE section 2.2.5.3.4.1 */
588         if (ndr->flags & LIBNDR_FLAG_NDR64) {
589                 return ndr_pull_align(ndr, size);
590         }
591         return NDR_ERR_SUCCESS;
592 }
593
594 /*
595   push some bytes
596 */
597 _PUBLIC_ enum ndr_err_code ndr_push_bytes(struct ndr_push *ndr, const uint8_t *data, uint32_t n)
598 {
599         NDR_PUSH_NEED_BYTES(ndr, n);
600         memcpy(ndr->data + ndr->offset, data, n);
601         ndr->offset += n;
602         return NDR_ERR_SUCCESS;
603 }
604
605 /*
606   push some zero bytes
607 */
608 _PUBLIC_ enum ndr_err_code ndr_push_zero(struct ndr_push *ndr, uint32_t n)
609 {
610         NDR_PUSH_NEED_BYTES(ndr, n);
611         memset(ndr->data + ndr->offset, 0, n);
612         ndr->offset += n;
613         return NDR_ERR_SUCCESS;
614 }
615
616 /*
617   push an array of uint8
618 */
619 _PUBLIC_ enum ndr_err_code ndr_push_array_uint8(struct ndr_push *ndr, int ndr_flags, const uint8_t *data, uint32_t n)
620 {
621         if (!(ndr_flags & NDR_SCALARS)) {
622                 return NDR_ERR_SUCCESS;
623         }
624         return ndr_push_bytes(ndr, data, n);
625 }
626
627 /*
628   push a unique non-zero value if a pointer is non-NULL, otherwise 0
629 */
630 _PUBLIC_ enum ndr_err_code ndr_push_unique_ptr(struct ndr_push *ndr, const void *p)
631 {
632         uint32_t ptr = 0;
633         if (p) {
634                 ptr = ndr->ptr_count * 4;
635                 ptr |= 0x00020000;
636                 ndr->ptr_count++;
637         }
638         return ndr_push_uint3264(ndr, NDR_SCALARS, ptr);
639 }
640
641 /*
642   push a 'simple' full non-zero value if a pointer is non-NULL, otherwise 0
643 */
644 _PUBLIC_ enum ndr_err_code ndr_push_full_ptr(struct ndr_push *ndr, const void *p)
645 {
646         uint32_t ptr = 0;
647         if (p) {
648                 /* Check if the pointer already exists and has an id */
649                 ptr = ndr_token_peek(&ndr->full_ptr_list, p);
650                 if (ptr == 0) {
651                         ndr->ptr_count++;
652                         ptr = ndr->ptr_count;
653                         ndr_token_store(ndr, &ndr->full_ptr_list, p, ptr);
654                 }
655         }
656         return ndr_push_uint3264(ndr, NDR_SCALARS, ptr);
657 }
658
659 /*
660   push always a 0, if a pointer is NULL it's a fatal error
661 */
662 _PUBLIC_ enum ndr_err_code ndr_push_ref_ptr(struct ndr_push *ndr)
663 {
664         return ndr_push_uint3264(ndr, NDR_SCALARS, 0xAEF1AEF1);
665 }
666
667
668 /*
669   push a NTTIME
670 */
671 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME(struct ndr_push *ndr, int ndr_flags, NTTIME t)
672 {
673         NDR_CHECK(ndr_push_udlong(ndr, ndr_flags, t));
674         return NDR_ERR_SUCCESS;
675 }
676
677 /*
678   pull a NTTIME
679 */
680 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
681 {
682         NDR_CHECK(ndr_pull_udlong(ndr, ndr_flags, t));
683         return NDR_ERR_SUCCESS;
684 }
685
686 /*
687   push a NTTIME_1sec
688 */
689 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME_1sec(struct ndr_push *ndr, int ndr_flags, NTTIME t)
690 {
691         t /= 10000000;
692         NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
693         return NDR_ERR_SUCCESS;
694 }
695
696 /*
697   pull a NTTIME_1sec
698 */
699 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME_1sec(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
700 {
701         NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
702         (*t) *= 10000000;
703         return NDR_ERR_SUCCESS;
704 }
705
706 /*
707   pull a NTTIME_hyper
708 */
709 _PUBLIC_ enum ndr_err_code ndr_pull_NTTIME_hyper(struct ndr_pull *ndr, int ndr_flags, NTTIME *t)
710 {
711         NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, t));
712         return NDR_ERR_SUCCESS;
713 }
714
715 /*
716   push a NTTIME_hyper
717 */
718 _PUBLIC_ enum ndr_err_code ndr_push_NTTIME_hyper(struct ndr_push *ndr, int ndr_flags, NTTIME t)
719 {
720         NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, t));
721         return NDR_ERR_SUCCESS;
722 }
723
724 /*
725   push a time_t
726 */
727 _PUBLIC_ enum ndr_err_code ndr_push_time_t(struct ndr_push *ndr, int ndr_flags, time_t t)
728 {
729         return ndr_push_uint32(ndr, ndr_flags, t);
730 }
731
732 /*
733   pull a time_t
734 */
735 _PUBLIC_ enum ndr_err_code ndr_pull_time_t(struct ndr_pull *ndr, int ndr_flags, time_t *t)
736 {
737         uint32_t tt;
738         NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &tt));
739         *t = tt;
740         return NDR_ERR_SUCCESS;
741 }
742
743
744 /*
745   pull a ipv4address
746 */
747 _PUBLIC_ enum ndr_err_code ndr_pull_ipv4address(struct ndr_pull *ndr, int ndr_flags, const char **address)
748 {
749         uint32_t addr;
750         struct in_addr in;
751         NDR_CHECK(ndr_pull_uint32(ndr, ndr_flags, &addr));
752         in.s_addr = htonl(addr);
753         *address = talloc_strdup(ndr->current_mem_ctx, inet_ntoa(in));
754         NDR_ERR_HAVE_NO_MEMORY(*address);
755         return NDR_ERR_SUCCESS;
756 }
757
758 /*
759   push a ipv4address
760 */
761 _PUBLIC_ enum ndr_err_code ndr_push_ipv4address(struct ndr_push *ndr, int ndr_flags, const char *address)
762 {
763         uint32_t addr;
764         if (!is_ipaddress(address)) {
765                 return ndr_push_error(ndr, NDR_ERR_IPV4ADDRESS,
766                                       "Invalid IPv4 address: '%s'", 
767                                       address);
768         }
769         addr = inet_addr(address);
770         NDR_CHECK(ndr_push_uint32(ndr, ndr_flags, htonl(addr)));
771         return NDR_ERR_SUCCESS;
772 }
773
774 /*
775   print a ipv4address
776 */
777 _PUBLIC_ void ndr_print_ipv4address(struct ndr_print *ndr, const char *name, 
778                            const char *address)
779 {
780         ndr->print(ndr, "%-25s: %s", name, address);
781 }
782
783
784 _PUBLIC_ void ndr_print_struct(struct ndr_print *ndr, const char *name, const char *type)
785 {
786         ndr->print(ndr, "%s: struct %s", name, type);
787 }
788
789 _PUBLIC_ void ndr_print_enum(struct ndr_print *ndr, const char *name, const char *type, 
790                     const char *val, uint32_t value)
791 {
792         if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
793                 ndr->print(ndr, "%-25s: %s (0x%X)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
794         } else {
795                 ndr->print(ndr, "%-25s: %s (%d)", name, val?val:"UNKNOWN_ENUM_VALUE", value);
796         }
797 }
798
799 _PUBLIC_ void ndr_print_bitmap_flag(struct ndr_print *ndr, size_t size, const char *flag_name, uint32_t flag, uint32_t value)
800 {
801         /* this is an attempt to support multi-bit bitmap masks */
802         value &= flag;
803
804         while (!(flag & 1)) {
805                 flag >>= 1;
806                 value >>= 1;
807         }       
808         if (flag == 1) {
809                 ndr->print(ndr, "   %d: %-25s", value, flag_name);
810         } else {
811                 ndr->print(ndr, "0x%02x: %-25s (%d)", value, flag_name, value);
812         }
813 }
814
815 _PUBLIC_ void ndr_print_int8(struct ndr_print *ndr, const char *name, int8_t v)
816 {
817         ndr->print(ndr, "%-25s: %d", name, v);
818 }
819
820 _PUBLIC_ void ndr_print_uint8(struct ndr_print *ndr, const char *name, uint8_t v)
821 {
822         ndr->print(ndr, "%-25s: 0x%02x (%u)", name, v, v);
823 }
824
825 _PUBLIC_ void ndr_print_int16(struct ndr_print *ndr, const char *name, int16_t v)
826 {
827         ndr->print(ndr, "%-25s: %d", name, v);
828 }
829
830 _PUBLIC_ void ndr_print_uint16(struct ndr_print *ndr, const char *name, uint16_t v)
831 {
832         ndr->print(ndr, "%-25s: 0x%04x (%u)", name, v, v);
833 }
834
835 _PUBLIC_ void ndr_print_int32(struct ndr_print *ndr, const char *name, int32_t v)
836 {
837         ndr->print(ndr, "%-25s: %d", name, v);
838 }
839
840 _PUBLIC_ void ndr_print_uint32(struct ndr_print *ndr, const char *name, uint32_t v)
841 {
842         ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
843 }
844
845 _PUBLIC_ void ndr_print_int3264(struct ndr_print *ndr, const char *name, int32_t v)
846 {
847         ndr->print(ndr, "%-25s: %d", name, v);
848 }
849
850 _PUBLIC_ void ndr_print_uint3264(struct ndr_print *ndr, const char *name, uint32_t v)
851 {
852         ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
853 }
854
855 _PUBLIC_ void ndr_print_udlong(struct ndr_print *ndr, const char *name, uint64_t v)
856 {
857         ndr->print(ndr, "%-25s: 0x%016llx (%llu)", name, (unsigned long long)v, (unsigned long long)v);
858 }
859
860 _PUBLIC_ void ndr_print_udlongr(struct ndr_print *ndr, const char *name, uint64_t v)
861 {
862         ndr_print_udlong(ndr, name, v);
863 }
864
865 _PUBLIC_ void ndr_print_dlong(struct ndr_print *ndr, const char *name, int64_t v)
866 {
867         ndr->print(ndr, "%-25s: 0x%016llx (%lld)", name, (unsigned long long)v, (long long)v);
868 }
869
870 _PUBLIC_ void ndr_print_double(struct ndr_print *ndr, const char *name, double v)
871 {
872         ndr->print(ndr, "%-25s: %f", name, v);
873 }
874
875 _PUBLIC_ void ndr_print_hyper(struct ndr_print *ndr, const char *name, uint64_t v)
876 {
877         ndr_print_dlong(ndr, name, v);
878 }
879
880 _PUBLIC_ void ndr_print_pointer(struct ndr_print *ndr, const char *name, void *v)
881 {
882         ndr->print(ndr, "%-25s: %p", name, v);
883 }
884
885 _PUBLIC_ void ndr_print_ptr(struct ndr_print *ndr, const char *name, const void *p)
886 {
887         if (p) {
888                 ndr->print(ndr, "%-25s: *", name);
889         } else {
890                 ndr->print(ndr, "%-25s: NULL", name);
891         }
892 }
893
894 _PUBLIC_ void ndr_print_NTTIME(struct ndr_print *ndr, const char *name, NTTIME t)
895 {
896         ndr->print(ndr, "%-25s: %s", name, nt_time_string(ndr, t));
897 }
898
899 _PUBLIC_ void ndr_print_NTTIME_1sec(struct ndr_print *ndr, const char *name, NTTIME t)
900 {
901         /* this is a standard NTTIME here
902          * as it's already converted in the pull/push code
903          */
904         ndr_print_NTTIME(ndr, name, t);
905 }
906
907 _PUBLIC_ void ndr_print_NTTIME_hyper(struct ndr_print *ndr, const char *name, NTTIME t)
908 {
909         ndr_print_NTTIME(ndr, name, t);
910 }
911
912 _PUBLIC_ void ndr_print_time_t(struct ndr_print *ndr, const char *name, time_t t)
913 {
914         if (t == (time_t)-1 || t == 0) {
915                 ndr->print(ndr, "%-25s: (time_t)%d", name, (int)t);
916         } else {
917                 ndr->print(ndr, "%-25s: %s", name, timestring(ndr, t));
918         }
919 }
920
921 _PUBLIC_ void ndr_print_union(struct ndr_print *ndr, const char *name, int level, const char *type)
922 {
923         if (ndr->flags & LIBNDR_PRINT_ARRAY_HEX) {
924                 ndr->print(ndr, "%-25s: union %s(case 0x%X)", name, type, level);
925         } else {
926                 ndr->print(ndr, "%-25s: union %s(case %d)", name, type, level);
927         }
928 }
929
930 _PUBLIC_ void ndr_print_bad_level(struct ndr_print *ndr, const char *name, uint16_t level)
931 {
932         ndr->print(ndr, "UNKNOWN LEVEL %u", level);
933 }
934
935 _PUBLIC_ void ndr_print_array_uint8(struct ndr_print *ndr, const char *name, 
936                            const uint8_t *data, uint32_t count)
937 {
938         int i;
939
940         if (count <= 600 && (ndr->flags & LIBNDR_PRINT_ARRAY_HEX)) {
941                 char s[1202];
942                 for (i=0;i<count;i++) {
943                         snprintf(&s[i*2], 3, "%02x", data[i]);
944                 }
945                 s[i*2] = 0;
946                 ndr->print(ndr, "%-25s: %s", name, s);
947                 return;
948         }
949
950         ndr->print(ndr, "%s: ARRAY(%d)", name, count);
951         ndr->depth++;
952         for (i=0;i<count;i++) {
953                 char *idx=NULL;
954                 if (asprintf(&idx, "[%d]", i) != -1) {
955                         ndr_print_uint8(ndr, idx, data[i]);
956                         free(idx);
957                 }
958         }
959         ndr->depth--;   
960 }
961
962 _PUBLIC_ void ndr_print_DATA_BLOB(struct ndr_print *ndr, const char *name, DATA_BLOB r)
963 {
964         ndr->print(ndr, "%-25s: DATA_BLOB length=%u", name, (unsigned)r.length);
965         if (r.length) {
966                 dump_data(10, r.data, r.length);
967         }
968 }
969
970
971 /*
972   push a DATA_BLOB onto the wire. 
973 */
974 _PUBLIC_ enum ndr_err_code ndr_push_DATA_BLOB(struct ndr_push *ndr, int ndr_flags, DATA_BLOB blob)
975 {
976         if (ndr->flags & LIBNDR_ALIGN_FLAGS) {
977                 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
978                         blob.length = NDR_ALIGN(ndr, 2);
979                 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
980                         blob.length = NDR_ALIGN(ndr, 4);
981                 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
982                         blob.length = NDR_ALIGN(ndr, 8);
983                 }
984                 NDR_PUSH_ALLOC_SIZE(ndr, blob.data, blob.length);
985                 data_blob_clear(&blob);
986         } else if (!(ndr->flags & LIBNDR_FLAG_REMAINING)) {
987                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, blob.length));
988         }
989         NDR_CHECK(ndr_push_bytes(ndr, blob.data, blob.length));
990         return NDR_ERR_SUCCESS;
991 }
992
993 /*
994   pull a DATA_BLOB from the wire. 
995 */
996 _PUBLIC_ enum ndr_err_code ndr_pull_DATA_BLOB(struct ndr_pull *ndr, int ndr_flags, DATA_BLOB *blob)
997 {
998         uint32_t length = 0;
999
1000         if (ndr->flags & LIBNDR_ALIGN_FLAGS) {
1001                 if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
1002                         length = NDR_ALIGN(ndr, 2);
1003                 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
1004                         length = NDR_ALIGN(ndr, 4);
1005                 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
1006                         length = NDR_ALIGN(ndr, 8);
1007                 }
1008                 if (ndr->data_size - ndr->offset < length) {
1009                         length = ndr->data_size - ndr->offset;
1010                 }
1011         } else if (ndr->flags & LIBNDR_FLAG_REMAINING) {
1012                 length = ndr->data_size - ndr->offset;
1013         } else {
1014                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &length));
1015         }
1016         NDR_PULL_NEED_BYTES(ndr, length);
1017         *blob = data_blob_talloc(ndr->current_mem_ctx, ndr->data+ndr->offset, length);
1018         ndr->offset += length;
1019         return NDR_ERR_SUCCESS;
1020 }
1021
1022 _PUBLIC_ uint32_t ndr_size_DATA_BLOB(int ret, const DATA_BLOB *data, int flags)
1023 {
1024         if (!data) return ret;
1025         return ret + data->length;
1026 }