added support for level1 of EnumPrinters in spoolss. This uses a
[gd/samba-autobuild/.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
25 /*
26   parse a uint8
27 */
28 NTSTATUS ndr_pull_uint8(struct ndr_pull *ndr, uint8 *v)
29 {
30         NDR_PULL_NEED_BYTES(ndr, 1);
31         *v = CVAL(ndr->data, ndr->offset);
32         ndr->offset += 1;
33         return NT_STATUS_OK;
34 }
35
36
37 /*
38   parse a uint16
39 */
40 NTSTATUS ndr_pull_uint16(struct ndr_pull *ndr, uint16 *v)
41 {
42         NDR_PULL_ALIGN(ndr, 2);
43         NDR_PULL_NEED_BYTES(ndr, 2);
44         *v = SVAL(ndr->data, ndr->offset);
45         ndr->offset += 2;
46         return NT_STATUS_OK;
47 }
48
49
50 /*
51   parse a uint32
52 */
53 NTSTATUS ndr_pull_uint32(struct ndr_pull *ndr, uint32 *v)
54 {
55         NDR_PULL_ALIGN(ndr, 4);
56         NDR_PULL_NEED_BYTES(ndr, 4);
57         *v = IVAL(ndr->data, ndr->offset);
58         ndr->offset += 4;
59         return NT_STATUS_OK;
60 }
61
62 /*
63   parse a HYPER_T
64 */
65 NTSTATUS ndr_pull_HYPER_T(struct ndr_pull *ndr, HYPER_T *v)
66 {
67         NDR_PULL_ALIGN(ndr, 8);
68         NDR_PULL_NEED_BYTES(ndr, 8);
69         v->low = IVAL(ndr->data, ndr->offset);
70         v->high = IVAL(ndr->data, ndr->offset+4);
71         ndr->offset += 8;
72         return NT_STATUS_OK;
73 }
74
75 /*
76   pull a NTSTATUS
77 */
78 NTSTATUS ndr_pull_NTSTATUS(struct ndr_pull *ndr, NTSTATUS *status)
79 {
80         uint32 v;
81         NDR_CHECK(ndr_pull_uint32(ndr, &v));
82         *status = NT_STATUS(v);
83         return NT_STATUS_OK;
84 }
85
86 /*
87   parse a set of bytes
88 */
89 NTSTATUS ndr_pull_bytes(struct ndr_pull *ndr, char *data, uint32 n)
90 {
91         NDR_PULL_NEED_BYTES(ndr, n);
92         memcpy(data, ndr->data + ndr->offset, n);
93         ndr->offset += n;
94         return NT_STATUS_OK;
95 }
96
97 /*
98   pull an array of uint8
99 */
100 NTSTATUS ndr_pull_array_uint8(struct ndr_pull *ndr, char *data, uint32 n)
101 {
102         return ndr_pull_bytes(ndr, data, n);
103 }
104
105
106 /*
107   pull an array of uint16
108 */
109 NTSTATUS ndr_pull_array_uint16(struct ndr_pull *ndr, uint16 *data, uint32 n)
110 {
111         uint32 i;
112         for (i=0;i<n;i++) {
113                 NDR_CHECK(ndr_pull_uint16(ndr, &data[i]));
114         }
115         return NT_STATUS_OK;
116 }
117
118 /*
119   pull a const array of uint32
120 */
121 NTSTATUS ndr_pull_array_uint32(struct ndr_pull *ndr, uint32 *data, uint32 n)
122 {
123         uint32 i;
124         for (i=0;i<n;i++) {
125                 NDR_CHECK(ndr_pull_uint32(ndr, &data[i]));
126         }
127         return NT_STATUS_OK;
128 }
129
130 /*
131   parse a GUID
132 */
133 NTSTATUS ndr_pull_GUID(struct ndr_pull *ndr, int ndr_flags, GUID *guid)
134 {
135         if (ndr_flags & NDR_SCALARS) {
136                 return ndr_pull_bytes(ndr, guid->info, GUID_SIZE);
137         }
138         return NT_STATUS_OK;
139 }
140
141
142 /*
143   push a uint8
144 */
145 NTSTATUS ndr_push_uint8(struct ndr_push *ndr, uint8 v)
146 {
147         NDR_PUSH_NEED_BYTES(ndr, 1);
148         SCVAL(ndr->data, ndr->offset, v);
149         ndr->offset += 1;
150         return NT_STATUS_OK;
151 }
152
153 /*
154   push a uint16
155 */
156 NTSTATUS ndr_push_uint16(struct ndr_push *ndr, uint16 v)
157 {
158         NDR_PUSH_ALIGN(ndr, 2);
159         NDR_PUSH_NEED_BYTES(ndr, 2);
160         SSVAL(ndr->data, ndr->offset, v);
161         ndr->offset += 2;
162         return NT_STATUS_OK;
163 }
164
165 /*
166   push a uint32
167 */
168 NTSTATUS ndr_push_uint32(struct ndr_push *ndr, uint32 v)
169 {
170         NDR_PUSH_ALIGN(ndr, 4);
171         NDR_PUSH_NEED_BYTES(ndr, 4);
172         SIVAL(ndr->data, ndr->offset, v);
173         ndr->offset += 4;
174         return NT_STATUS_OK;
175 }
176
177 /*
178   push a HYPER_T
179 */
180 NTSTATUS ndr_push_HYPER_T(struct ndr_push *ndr, HYPER_T v)
181 {
182         NDR_PUSH_ALIGN(ndr, 8);
183         NDR_PUSH_NEED_BYTES(ndr, 8);
184         SIVAL(ndr->data, ndr->offset, v.low);
185         SIVAL(ndr->data, ndr->offset+4, v.high);
186         ndr->offset += 8;
187         return NT_STATUS_OK;
188 }
189
190 NTSTATUS ndr_push_align(struct ndr_push *ndr, size_t size)
191 {
192         NDR_PUSH_ALIGN(ndr, size);
193         return NT_STATUS_OK;
194 }
195
196 NTSTATUS ndr_pull_align(struct ndr_pull *ndr, size_t size)
197 {
198         NDR_PULL_ALIGN(ndr, size);
199         return NT_STATUS_OK;
200 }
201
202 /*
203   push some bytes
204 */
205 NTSTATUS ndr_push_bytes(struct ndr_push *ndr, const char *data, uint32 n)
206 {
207         NDR_PUSH_NEED_BYTES(ndr, n);
208         memcpy(ndr->data + ndr->offset, data, n);
209         ndr->offset += n;
210         return NT_STATUS_OK;
211 }
212
213 /*
214   push an array of uint8
215 */
216 NTSTATUS ndr_push_array_uint8(struct ndr_push *ndr, const char *data, uint32 n)
217 {
218         return ndr_push_bytes(ndr, data, n);
219 }
220
221 /*
222   push an array of uint32
223 */
224 NTSTATUS ndr_push_array_uint32(struct ndr_push *ndr, const uint32 *data, uint32 n)
225 {
226         int i;
227         for (i=0;i<n;i++) {
228                 NDR_CHECK(ndr_push_uint32(ndr, data[i]));
229         }
230         return NT_STATUS_OK;
231 }
232
233 /*
234   save the current position
235  */
236 void ndr_push_save(struct ndr_push *ndr, struct ndr_push_save *save)
237 {
238         save->offset = ndr->offset;
239 }
240
241 /*
242   restore the position
243  */
244 void ndr_push_restore(struct ndr_push *ndr, struct ndr_push_save *save)
245 {
246         ndr->offset = save->offset;
247 }
248
249 /*
250   this is used when a packet has a 4 byte length field. We remember the start position
251   and come back to it later to fill in the size
252 */
253 NTSTATUS ndr_push_length4_start(struct ndr_push *ndr, struct ndr_push_save *save)
254 {
255         NDR_PUSH_ALIGN(ndr, 4);
256         ndr_push_save(ndr, save);
257         return ndr_push_uint32(ndr, 0);
258 }
259
260 NTSTATUS ndr_push_length4_end(struct ndr_push *ndr, struct ndr_push_save *save)
261 {
262         struct ndr_push_save save2;
263         ndr_push_save(ndr, &save2);
264         ndr_push_restore(ndr, save);
265         NDR_CHECK(ndr_push_uint32(ndr, save2.offset - ndr->offset));
266         ndr_push_restore(ndr, &save2);
267         return NT_STATUS_OK;
268 }
269
270 /*
271   push a 1 if a pointer is non-NULL, otherwise 0
272 */
273 NTSTATUS ndr_push_ptr(struct ndr_push *ndr, const void *p)
274 {
275         return ndr_push_uint32(ndr, p?0xaabbccdd:0);
276 }
277
278 /*
279   push a comformant, variable ucs2 string onto the wire from a C string
280 */
281 NTSTATUS ndr_push_unistr(struct ndr_push *ndr, const char *s)
282 {
283         char *ws;
284         ssize_t len;
285         len = push_ucs2_talloc(ndr->mem_ctx, (smb_ucs2_t **)&ws, s);
286         if (len == -1) {
287                 return NT_STATUS_INVALID_PARAMETER;
288         }
289         NDR_CHECK(ndr_push_uint32(ndr, len/2));
290         NDR_CHECK(ndr_push_uint32(ndr, 0));
291         NDR_CHECK(ndr_push_uint32(ndr, len/2));
292         NDR_CHECK(ndr_push_bytes(ndr, ws, len));
293         return NT_STATUS_OK;
294 }
295
296 /*
297   push a comformant, variable ucs2 string onto the wire from a C string
298   don't send the null
299 */
300 NTSTATUS ndr_push_unistr_noterm(struct ndr_push *ndr, const char *s)
301 {
302         char *ws;
303         ssize_t len;
304         len = push_ucs2_talloc(ndr->mem_ctx, (smb_ucs2_t **)&ws, s);
305         if (len == -1) {
306                 return NT_STATUS_INVALID_PARAMETER;
307         }
308         NDR_CHECK(ndr_push_uint32(ndr, len/2 - 1));
309         NDR_CHECK(ndr_push_uint32(ndr, 0));
310         NDR_CHECK(ndr_push_uint32(ndr, len/2 - 1));
311         NDR_CHECK(ndr_push_bytes(ndr, ws, len - 2));
312         return NT_STATUS_OK;
313 }
314
315 /*
316   pull a comformant, variable ucs2 string from the wire into a C string
317 */
318 NTSTATUS ndr_pull_unistr(struct ndr_pull *ndr, const char **s)
319 {
320         char *ws, *as=NULL;
321         uint32 len1, ofs, len2;
322
323         NDR_CHECK(ndr_pull_uint32(ndr, &len1));
324         NDR_CHECK(ndr_pull_uint32(ndr, &ofs));
325         NDR_CHECK(ndr_pull_uint32(ndr, &len2));
326         if (len2 > len1) {
327                 return NT_STATUS_INVALID_PARAMETER;
328         }
329         NDR_ALLOC_N(ndr, ws, (len1+1)*2);
330         NDR_CHECK(ndr_pull_bytes(ndr, ws, len2*2));
331         SSVAL(ws, len1*2, 0);
332         SSVAL(ws, len2*2, 0);
333         pull_ucs2_talloc(ndr->mem_ctx, &as, (const smb_ucs2_t *)ws);
334         if (!as) {
335                 return NT_STATUS_INVALID_PARAMETER;
336         }
337         *s = as;
338         return NT_STATUS_OK;
339 }
340
341 /*
342   pull a comformant, variable ucs2 string from the wire into a C string
343 */
344 NTSTATUS ndr_pull_unistr_noterm(struct ndr_pull *ndr, const char **s)
345 {
346         return ndr_pull_unistr(ndr, s);
347 }
348
349 /*
350   push a 4 byte offset pointer, remembering where we are so we can later fill
351   in the correct value
352 */
353 NTSTATUS ndr_push_offset(struct ndr_push *ndr, struct ndr_push_save *ofs)
354 {
355         NDR_PUSH_ALIGN(ndr, 4);
356         ndr_push_save(ndr, ofs);
357         return ndr_push_uint32(ndr, 0);
358 }
359
360 /*
361   fill in the correct offset in a saved offset pointer
362   the offset is taken relative to 'save'
363 */
364 NTSTATUS ndr_push_offset_ptr(struct ndr_push *ndr, 
365                              struct ndr_push_save *ofs, 
366                              struct ndr_push_save *save)
367 {
368         struct ndr_push_save save2;
369         ndr_push_save(ndr, &save2);
370         ndr_push_restore(ndr, ofs);
371         NDR_CHECK(ndr_push_uint32(ndr, save2.offset - save->offset));
372         ndr_push_restore(ndr, &save2);
373         return NT_STATUS_OK;
374 }
375
376
377 /*
378   push a GUID
379 */
380 NTSTATUS ndr_push_GUID(struct ndr_push *ndr, int ndr_flags, GUID *guid)
381 {
382         if (ndr_flags & NDR_SCALARS) {
383                 return ndr_push_bytes(ndr, guid->info, GUID_SIZE);
384         }
385         return NT_STATUS_OK;
386 }
387
388 /*
389   push a NTTIME
390 */
391 NTSTATUS ndr_push_NTTIME(struct ndr_push *ndr, NTTIME t)
392 {
393         NDR_CHECK(ndr_push_uint32(ndr, t.low));
394         NDR_CHECK(ndr_push_uint32(ndr, t.high));
395         return NT_STATUS_OK;
396 }
397
398 /*
399   pull a NTTIME
400 */
401 NTSTATUS ndr_pull_NTTIME(struct ndr_pull *ndr, NTTIME *t)
402 {
403         NDR_CHECK(ndr_pull_uint32(ndr, &t->low));
404         NDR_CHECK(ndr_pull_uint32(ndr, &t->high));
405         return NT_STATUS_OK;
406 }
407
408
409 void ndr_print_struct(struct ndr_print *ndr, const char *name, const char *type)
410 {
411         ndr->print(ndr, "%s: struct %s", name, type);
412 }
413
414 void ndr_print_uint8(struct ndr_print *ndr, const char *name, uint8 v)
415 {
416         ndr->print(ndr, "%-25s: 0x%02x (%u)", name, v, v);
417 }
418
419 void ndr_print_uint16(struct ndr_print *ndr, const char *name, uint16 v)
420 {
421         ndr->print(ndr, "%-25s: 0x%04x (%u)", name, v, v);
422 }
423
424 void ndr_print_uint32(struct ndr_print *ndr, const char *name, uint32 v)
425 {
426         ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
427 }
428
429 void ndr_print_HYPER_T(struct ndr_print *ndr, const char *name, HYPER_T v)
430 {
431         ndr->print(ndr, "%-25s: 0x%08x%08x", name, v.high, v.low);
432 }
433
434 void ndr_print_ptr(struct ndr_print *ndr, const char *name, const void *p)
435 {
436         if (p) {
437                 ndr->print(ndr, "%-25s: *", name);
438         } else {
439                 ndr->print(ndr, "%-25s: NULL", name);
440         }
441 }
442
443 void ndr_print_unistr_noterm(struct ndr_print *ndr, const char *name, const char *s)
444 {
445         ndr->print(ndr, "%-25s: '%s'", name, s);
446 }
447
448 void ndr_print_unistr(struct ndr_print *ndr, const char *name, const char *s)
449 {
450         ndr->print(ndr, "%-25s: '%s'", name, s);
451 }
452
453 void ndr_print_NTTIME(struct ndr_print *ndr, const char *name, NTTIME t)
454 {
455         ndr->print(ndr, "%-25s: %s", name, nt_time_string(ndr->mem_ctx, &t));
456 }
457
458 void ndr_print_union(struct ndr_print *ndr, const char *name, uint16 level, const char *type)
459 {
460         ndr->print(ndr, "%-25s: union %s(case %u)", name, type, level);
461 }
462
463 void ndr_print_bad_level(struct ndr_print *ndr, const char *name, uint16 level)
464 {
465         ndr->print(ndr, "UNKNOWN LEVEL %u", level);
466 }
467
468 void ndr_print_array_uint32(struct ndr_print *ndr, const char *name, 
469                             uint32 *data, uint32 count)
470 {
471         int i;
472
473         ndr->print(ndr, "%s: ARRAY(%d)", name, count);
474         ndr->depth++;
475         for (i=0;i<count;i++) {
476                 char *idx=NULL;
477                 asprintf(&idx, "[%d]", i);
478                 if (idx) {
479                         ndr_print_uint32(ndr, idx, data[i]);
480                         free(idx);
481                 }
482         }
483         ndr->depth--;   
484 }
485
486 void ndr_print_array_uint8(struct ndr_print *ndr, const char *name, 
487                            uint8 *data, uint32 count)
488 {
489         int i;
490
491         ndr->print(ndr, "%s: ARRAY(%d)", name, count);
492         ndr->depth++;
493         for (i=0;i<count;i++) {
494                 char *idx=NULL;
495                 asprintf(&idx, "[%d]", i);
496                 if (idx) {
497                         ndr_print_uint8(ndr, idx, data[i]);
498                         free(idx);
499                 }
500         }
501         ndr->depth--;   
502 }
503
504 void ndr_print_GUID(struct ndr_print *ndr, const char *name, struct GUID *guid)
505 {
506         ndr->print(ndr, "%-25s: %08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", 
507                    name,
508                    IVAL(guid->info, 0), SVAL(guid->info, 4), SVAL(guid->info, 6),
509                    guid->info[8], guid->info[9],
510                    guid->info[10], guid->info[11], guid->info[12], guid->info[13], 
511                    guid->info[14], guid->info[15]);
512 }
513
514
515 /*
516   pull a spoolss style "relative string"
517 */
518 NTSTATUS ndr_pull_relstr(struct ndr_pull *ndr, int ndr_flags, const char **s)
519 {
520         uint32 ofs;
521         int ret;
522         struct ndr_pull_save save;
523
524         if (!(ndr_flags & NDR_SCALARS)) {
525                 return NT_STATUS_OK;
526         }
527
528         NDR_CHECK(ndr_pull_uint32(ndr, &ofs));
529         ndr_pull_save(ndr, &save);
530         NDR_CHECK(ndr_pull_set_offset(ndr, ofs));
531         ret = convert_string_talloc(ndr->mem_ctx, CH_UCS2, CH_UNIX, 
532                                     ndr->data+ndr->offset, 
533                                     ndr->data_size - ndr->offset,
534                                     (const void **)s);
535         if (ret == -1) {
536                 return ndr_pull_error(ndr, NDR_ERR_RELSTR, "Bad relative string");
537         }
538         ndr_pull_restore(ndr, &save);
539         return NT_STATUS_OK;
540 }
541
542 /*
543   push a spoolss style "relative string"
544 */
545 NTSTATUS ndr_push_relstr(struct ndr_push *ndr, int ndr_flags, const char **s)
546 {
547         struct ndr_push_save *save;
548         if (ndr_flags & NDR_SCALARS) {
549                 save = talloc(ndr->mem_ctx, sizeof(*save));
550                 if (!save) return NT_STATUS_NO_MEMORY;
551                 NDR_CHECK(ndr_push_align(ndr, 4));
552                 ndr_push_save(ndr, save);
553                 NDR_CHECK(ndr_push_uint32(ndr, 0xFFFFFFFF));
554                 save->next = ndr->relstr_list;
555                 ndr->relstr_list = save;
556         }
557         if (ndr_flags & NDR_BUFFERS) {
558                 struct ndr_push_save save2;
559                 uint32 len;
560                 int ret;
561                 save = ndr->relstr_list;
562                 if (!save) {
563                         return ndr_push_error(ndr, NDR_ERR_RELSTR, "Empty relstr stack");
564                 }
565                 ndr->relstr_list = save->next;
566                 NDR_CHECK(ndr_push_align(ndr, 2));
567                 ndr_push_save(ndr, &save2);
568                 ndr_push_restore(ndr, save);
569                 NDR_CHECK(ndr_push_uint32(ndr, save2.offset));
570                 ndr_push_restore(ndr, &save2);
571                 len = 2*(strlen_m(*s)+1);
572                 NDR_PUSH_NEED_BYTES(ndr, len);
573                 ret = push_ucs2(NULL, ndr->data + ndr->offset, *s, len, STR_TERMINATE);
574                 if (ret == -1) {
575                         return ndr_push_error(ndr, NDR_ERR_CHARCNV, "Bad string conversion");
576                 }
577                 ndr->offset += len;
578         }
579         return NT_STATUS_OK;
580 }
581
582 void ndr_print_relstr(struct ndr_print *ndr, const char *name, const char **s)
583 {
584         ndr_print_unistr(ndr, name, *s);
585 }