Removed version number from file header.
[samba.git] / source3 / rpc_parse / parse_prs.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba memory buffer functions
4    Copyright (C) Andrew Tridgell              1992-1997
5    Copyright (C) Luke Kenneth Casson Leighton 1996-1997
6    Copyright (C) Jeremy Allison 1999.
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  * Dump a prs to a file: from the current location through to the end.
27  **/
28 void prs_dump(char *name, int v, prs_struct *ps)
29 {
30         prs_dump_region(name, v, ps, ps->data_offset, ps->buffer_size);
31 }
32
33
34 /**
35  * Dump from the start of the prs to the current location.
36  **/
37 void prs_dump_before(char *name, int v, prs_struct *ps)
38 {
39         prs_dump_region(name, v, ps, 0, ps->data_offset);
40 }
41
42
43 /**
44  * Dump everything from the start of the prs up to the current location.
45  **/
46 void prs_dump_region(char *name, int v, prs_struct *ps,
47                      int from_off, int to_off)
48 {
49         int fd, i;
50         pstring fname;
51         if (DEBUGLEVEL < 50) return;
52         for (i=1;i<100;i++) {
53                 if (v != -1) {
54                         slprintf(fname,sizeof(fname)-1, "/tmp/%s_%d.%d.prs", name, v, i);
55                 } else {
56                         slprintf(fname,sizeof(fname)-1, "/tmp/%s.%d.prs", name, i);
57                 }
58                 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0644);
59                 if (fd != -1 || errno != EEXIST) break;
60         }
61         if (fd != -1) {
62                 write(fd, ps->data_p + from_off, to_off - from_off);
63                 close(fd);
64                 DEBUG(0,("created %s\n", fname));
65         }
66 }
67
68
69
70 /*******************************************************************
71  debug output for parsing info.
72
73  XXXX side-effect of this function is to increase the debug depth XXXX
74
75  ********************************************************************/
76 void prs_debug(prs_struct *ps, int depth, char *desc, char *fn_name)
77 {
78         DEBUG(5+depth, ("%s%06x %s %s\n", tab_depth(depth), ps->data_offset, fn_name, desc));
79 }
80
81
82 /**
83  * Initialise an expandable parse structure.
84  *
85  * @param size Initial buffer size.  If >0, a new buffer will be
86  * created with malloc().
87  *
88  * @return False if allocation fails, otherwise True.
89  **/
90 BOOL prs_init(prs_struct *ps, uint32 size, TALLOC_CTX *ctx, BOOL io)
91 {
92         ZERO_STRUCTP(ps);
93         ps->io = io;
94         ps->bigendian_data = RPC_LITTLE_ENDIAN;
95         ps->align = RPC_PARSE_ALIGN;
96         ps->is_dynamic = False;
97         ps->data_offset = 0;
98         ps->buffer_size = 0;
99         ps->data_p = NULL;
100         ps->mem_ctx = ctx;
101
102         if (size != 0) {
103                 ps->buffer_size = size;
104                 if((ps->data_p = (char *)malloc((size_t)size)) == NULL) {
105                         DEBUG(0,("prs_init: malloc fail for %u bytes.\n", (unsigned int)size));
106                         return False;
107                 }
108                 memset(ps->data_p, '\0', (size_t)size);
109                 ps->is_dynamic = True; /* We own this memory. */
110         }
111
112         return True;
113 }
114
115 /*******************************************************************
116  read from a socket into memory.
117  ********************************************************************/
118 BOOL prs_read(prs_struct *ps, int fd, size_t len, int timeout)
119 {
120         BOOL ok;
121         size_t prev_size = ps->buffer_size;
122         if (!prs_grow(ps, len))
123                 return False;
124
125         if (timeout > 0) {
126                 ok = (read_with_timeout(fd, &ps->data_p[prev_size],
127                                             len, len,timeout) == len);
128         } else {
129                 ok = (read_data(fd, &ps->data_p[prev_size], len) == len);
130         }
131         return ok;
132 }
133
134 /*******************************************************************
135  Delete the memory in a parse structure - if we own it.
136  ********************************************************************/
137
138 void prs_mem_free(prs_struct *ps)
139 {
140         if(ps->is_dynamic)
141                 SAFE_FREE(ps->data_p);
142         ps->is_dynamic = False;
143         ps->buffer_size = 0;
144         ps->data_offset = 0;
145 }
146
147 /*******************************************************************
148  Clear the memory in a parse structure.
149  ********************************************************************/
150
151 void prs_mem_clear(prs_struct *ps)
152 {
153         memset(ps->data_p, '\0', (size_t)ps->buffer_size);
154 }
155
156 /*******************************************************************
157  Allocate memory when unmarshalling... Always zero clears.
158  ********************************************************************/
159
160 char *prs_alloc_mem(prs_struct *ps, size_t size)
161 {
162         char *ret = talloc(ps->mem_ctx, size);
163
164         if (ret)
165                 memset(ret, '\0', size);
166
167         return ret;
168 }
169
170 /*******************************************************************
171  Return the current talloc context we're using.
172  ********************************************************************/
173
174 TALLOC_CTX *prs_get_mem_context(prs_struct *ps)
175 {
176         return ps->mem_ctx;
177 }
178
179 /*******************************************************************
180  Hand some already allocated memory to a prs_struct.
181  ********************************************************************/
182
183 void prs_give_memory(prs_struct *ps, char *buf, uint32 size, BOOL is_dynamic)
184 {
185         ps->is_dynamic = is_dynamic;
186         ps->data_p = buf;
187         ps->buffer_size = size;
188 }
189
190 /*******************************************************************
191  Take some memory back from a prs_struct.
192  ********************************************************************/
193
194 char *prs_take_memory(prs_struct *ps, uint32 *psize)
195 {
196         char *ret = ps->data_p;
197         if(psize)
198                 *psize = ps->buffer_size;
199         ps->is_dynamic = False;
200         prs_mem_free(ps);
201         return ret;
202 }
203
204 /*******************************************************************
205  Set a prs_struct to exactly a given size. Will grow or tuncate if neccessary.
206  ********************************************************************/
207
208 BOOL prs_set_buffer_size(prs_struct *ps, uint32 newsize)
209 {
210         if (newsize > ps->buffer_size)
211                 return prs_force_grow(ps, newsize - ps->buffer_size);
212
213         if (newsize < ps->buffer_size) {
214                 char *new_data_p = Realloc(ps->data_p, newsize);
215                 /* if newsize is zero, Realloc acts like free() & returns NULL*/
216                 if (new_data_p == NULL && newsize != 0) {
217                         DEBUG(0,("prs_set_buffer_size: Realloc failure for size %u.\n",
218                                 (unsigned int)newsize));
219                         DEBUG(0,("prs_set_buffer_size: Reason %s\n",strerror(errno)));
220                         return False;
221                 }
222                 ps->data_p = new_data_p;
223                 ps->buffer_size = newsize;
224         }
225
226         return True;
227 }
228
229 /*******************************************************************
230  Attempt, if needed, to grow a data buffer.
231  Also depends on the data stream mode (io).
232  ********************************************************************/
233
234 BOOL prs_grow(prs_struct *ps, uint32 extra_space)
235 {
236         uint32 new_size;
237         char *new_data;
238
239         ps->grow_size = MAX(ps->grow_size, ps->data_offset + extra_space);
240
241         if(ps->data_offset + extra_space <= ps->buffer_size)
242                 return True;
243
244         /*
245          * We cannot grow the buffer if we're not reading
246          * into the prs_struct, or if we don't own the memory.
247          */
248
249         if(UNMARSHALLING(ps) || !ps->is_dynamic) {
250                 DEBUG(0,("prs_grow: Buffer overflow - unable to expand buffer by %u bytes.\n",
251                                 (unsigned int)extra_space));
252                 return False;
253         }
254         
255         /*
256          * Decide how much extra space we really need.
257          */
258
259         extra_space -= (ps->buffer_size - ps->data_offset);
260         if(ps->buffer_size == 0) {
261                 /*
262                  * Ensure we have at least a PDU's length, or extra_space, whichever
263                  * is greater.
264                  */
265
266                 new_size = MAX(MAX_PDU_FRAG_LEN,extra_space);
267
268                 if((new_data = malloc(new_size)) == NULL) {
269                         DEBUG(0,("prs_grow: Malloc failure for size %u.\n", (unsigned int)new_size));
270                         return False;
271                 }
272                 memset(new_data, '\0', (size_t)new_size );
273         } else {
274                 /*
275                  * If the current buffer size is bigger than the space needed, just 
276                  * double it, else add extra_space.
277                  */
278                 new_size = MAX(ps->buffer_size*2, ps->buffer_size + extra_space);               
279
280                 if ((new_data = Realloc(ps->data_p, new_size)) == NULL) {
281                         DEBUG(0,("prs_grow: Realloc failure for size %u.\n",
282                                 (unsigned int)new_size));
283                         return False;
284                 }
285
286                 memset(&new_data[ps->buffer_size], '\0', (size_t)(new_size - ps->buffer_size));
287         }
288         ps->buffer_size = new_size;
289         ps->data_p = new_data;
290
291         return True;
292 }
293
294 /*******************************************************************
295  Attempt to force a data buffer to grow by len bytes.
296  This is only used when appending more data onto a prs_struct
297  when reading an rpc reply, before unmarshalling it.
298  ********************************************************************/
299
300 BOOL prs_force_grow(prs_struct *ps, uint32 extra_space)
301 {
302         uint32 new_size = ps->buffer_size + extra_space;
303         char *new_data;
304
305         if(!UNMARSHALLING(ps) || !ps->is_dynamic) {
306                 DEBUG(0,("prs_force_grow: Buffer overflow - unable to expand buffer by %u bytes.\n",
307                                 (unsigned int)extra_space));
308                 return False;
309         }
310
311         if((new_data = Realloc(ps->data_p, new_size)) == NULL) {
312                 DEBUG(0,("prs_force_grow: Realloc failure for size %u.\n",
313                         (unsigned int)new_size));
314                 return False;
315         }
316
317         memset(&new_data[ps->buffer_size], '\0', (size_t)(new_size - ps->buffer_size));
318
319         ps->buffer_size = new_size;
320         ps->data_p = new_data;
321
322         return True;
323 }
324
325 /*******************************************************************
326  Get the data pointer (external interface).
327  ********************************************************************/
328
329 char *prs_data_p(prs_struct *ps)
330 {
331         return ps->data_p;
332 }
333
334 /*******************************************************************
335  Get the current data size (external interface).
336  ********************************************************************/
337
338 uint32 prs_data_size(prs_struct *ps)
339 {
340         return ps->buffer_size;
341 }
342
343 /*******************************************************************
344  Fetch the current offset (external interface).
345  ********************************************************************/
346
347 uint32 prs_offset(prs_struct *ps)
348 {
349         return ps->data_offset;
350 }
351
352 /*******************************************************************
353  Set the current offset (external interface).
354  ********************************************************************/
355
356 BOOL prs_set_offset(prs_struct *ps, uint32 offset)
357 {
358         if(offset <= ps->data_offset) {
359                 ps->data_offset = offset;
360                 return True;
361         }
362
363         if(!prs_grow(ps, offset - ps->data_offset))
364                 return False;
365
366         ps->data_offset = offset;
367         return True;
368 }
369
370 /*******************************************************************
371  Append the data from one parse_struct into another.
372  ********************************************************************/
373
374 BOOL prs_append_prs_data(prs_struct *dst, prs_struct *src)
375 {
376         if(!prs_grow(dst, prs_offset(src)))
377                 return False;
378
379         memcpy(&dst->data_p[dst->data_offset], prs_data_p(src), (size_t)prs_offset(src));
380         dst->data_offset += prs_offset(src);
381
382         return True;
383 }
384
385 /*******************************************************************
386  Append some data from one parse_struct into another.
387  ********************************************************************/
388
389 BOOL prs_append_some_prs_data(prs_struct *dst, prs_struct *src, int32 start, uint32 len)
390 {       
391         if (len == 0)
392                 return True;
393
394         if(!prs_grow(dst, len))
395                 return False;
396         
397         memcpy(&dst->data_p[dst->data_offset], prs_data_p(src)+start, (size_t)len);
398         dst->data_offset += len;
399
400         return True;
401 }
402
403 /*******************************************************************
404  Append the data from a buffer into a parse_struct.
405  ********************************************************************/
406
407 BOOL prs_append_data(prs_struct *dst, char *src, uint32 len)
408 {
409         if(!prs_grow(dst, len))
410                 return False;
411
412         memcpy(&dst->data_p[dst->data_offset], src, (size_t)len);
413         dst->data_offset += len;
414
415         return True;
416 }
417
418 /*******************************************************************
419  Set the data as X-endian (external interface).
420  ********************************************************************/
421
422 void prs_set_endian_data(prs_struct *ps, BOOL endian)
423 {
424         ps->bigendian_data = endian;
425 }
426
427 /*******************************************************************
428  Align a the data_len to a multiple of align bytes - filling with
429  zeros.
430  ********************************************************************/
431
432 BOOL prs_align(prs_struct *ps)
433 {
434         uint32 mod = ps->data_offset & (ps->align-1);
435
436         if (ps->align != 0 && mod != 0) {
437                 uint32 extra_space = (ps->align - mod);
438                 if(!prs_grow(ps, extra_space))
439                         return False;
440                 memset(&ps->data_p[ps->data_offset], '\0', (size_t)extra_space);
441                 ps->data_offset += extra_space;
442         }
443
444         return True;
445 }
446
447 /*******************************************************************
448  Align only if required (for the unistr2 string mainly)
449  ********************************************************************/
450
451 BOOL prs_align_needed(prs_struct *ps, uint32 needed)
452 {
453         if (needed==0)
454                 return True;
455         else
456                 return prs_align(ps);
457 }
458
459 /*******************************************************************
460  Ensure we can read/write to a given offset.
461  ********************************************************************/
462
463 char *prs_mem_get(prs_struct *ps, uint32 extra_size)
464 {
465         if(UNMARSHALLING(ps)) {
466                 /*
467                  * If reading, ensure that we can read the requested size item.
468                  */
469                 if (ps->data_offset + extra_size > ps->buffer_size) {
470                         DEBUG(0,("prs_mem_get: reading data of size %u would overrun buffer.\n",
471                                         (unsigned int)extra_size ));
472                         return NULL;
473                 }
474         } else {
475                 /*
476                  * Writing - grow the buffer if needed.
477                  */
478                 if(!prs_grow(ps, extra_size))
479                         return NULL;
480         }
481         return &ps->data_p[ps->data_offset];
482 }
483
484 /*******************************************************************
485  Change the struct type.
486  ********************************************************************/
487
488 void prs_switch_type(prs_struct *ps, BOOL io)
489 {
490         if ((ps->io ^ io) == True)
491                 ps->io=io;
492 }
493
494 /*******************************************************************
495  Force a prs_struct to be dynamic even when it's size is 0.
496  ********************************************************************/
497
498 void prs_force_dynamic(prs_struct *ps)
499 {
500         ps->is_dynamic=True;
501 }
502
503 /*******************************************************************
504  Stream a uint8.
505  ********************************************************************/
506
507 BOOL prs_uint8(char *name, prs_struct *ps, int depth, uint8 *data8)
508 {
509         char *q = prs_mem_get(ps, 1);
510         if (q == NULL)
511                 return False;
512
513     if (UNMARSHALLING(ps))
514                 *data8 = CVAL(q,0);
515         else
516                 SCVAL(q,0,*data8);
517
518     DEBUG(5,("%s%04x %s: %02x\n", tab_depth(depth), ps->data_offset, name, *data8));
519
520         ps->data_offset += 1;
521
522         return True;
523 }
524
525 /*******************************************************************
526  Stream a uint16.
527  ********************************************************************/
528
529 BOOL prs_uint16(char *name, prs_struct *ps, int depth, uint16 *data16)
530 {
531         char *q = prs_mem_get(ps, sizeof(uint16));
532         if (q == NULL)
533                 return False;
534
535     if (UNMARSHALLING(ps)) {
536                 if (ps->bigendian_data)
537                         *data16 = RSVAL(q,0);
538                 else
539                         *data16 = SVAL(q,0);
540     } else {
541                 if (ps->bigendian_data)
542                         RSSVAL(q,0,*data16);
543                 else
544                         SSVAL(q,0,*data16);
545         }
546
547         DEBUG(5,("%s%04x %s: %04x\n", tab_depth(depth), ps->data_offset, name, *data16));
548
549         ps->data_offset += sizeof(uint16);
550
551         return True;
552 }
553
554 /*******************************************************************
555  Stream a uint32.
556  ********************************************************************/
557
558 BOOL prs_uint32(char *name, prs_struct *ps, int depth, uint32 *data32)
559 {
560         char *q = prs_mem_get(ps, sizeof(uint32));
561         if (q == NULL)
562                 return False;
563
564         if (UNMARSHALLING(ps)) {
565                 if (ps->bigendian_data)
566                         *data32 = RIVAL(q,0);
567                 else
568                         *data32 = IVAL(q,0);
569         } else {
570                 if (ps->bigendian_data)
571                         RSIVAL(q,0,*data32);
572                 else
573                         SIVAL(q,0,*data32);
574         }
575
576         DEBUG(5,("%s%04x %s: %08x\n", tab_depth(depth), ps->data_offset, name, *data32));
577
578         ps->data_offset += sizeof(uint32);
579
580         return True;
581 }
582
583 /*******************************************************************
584  Stream a NTSTATUS
585  ********************************************************************/
586
587 BOOL prs_ntstatus(char *name, prs_struct *ps, int depth, NTSTATUS *status)
588 {
589         char *q = prs_mem_get(ps, sizeof(uint32));
590         if (q == NULL)
591                 return False;
592
593         if (UNMARSHALLING(ps)) {
594                 if (ps->bigendian_data)
595                         *status = NT_STATUS(RIVAL(q,0));
596                 else
597                         *status = NT_STATUS(IVAL(q,0));
598         } else {
599                 if (ps->bigendian_data)
600                         RSIVAL(q,0,NT_STATUS_V(*status));
601                 else
602                         SIVAL(q,0,NT_STATUS_V(*status));
603         }
604
605         DEBUG(5,("%s%04x %s: %s\n", tab_depth(depth), ps->data_offset, name, 
606                  get_nt_error_msg(*status)));
607
608         ps->data_offset += sizeof(uint32);
609
610         return True;
611 }
612
613 /*******************************************************************
614  Stream a WERROR
615  ********************************************************************/
616
617 BOOL prs_werror(char *name, prs_struct *ps, int depth, WERROR *status)
618 {
619         char *q = prs_mem_get(ps, sizeof(uint32));
620         if (q == NULL)
621                 return False;
622
623         if (UNMARSHALLING(ps)) {
624                 if (ps->bigendian_data)
625                         *status = W_ERROR(RIVAL(q,0));
626                 else
627                         *status = W_ERROR(IVAL(q,0));
628         } else {
629                 if (ps->bigendian_data)
630                         RSIVAL(q,0,W_ERROR_V(*status));
631                 else
632                         SIVAL(q,0,W_ERROR_V(*status));
633         }
634
635         DEBUG(5,("%s%04x %s: %s\n", tab_depth(depth), ps->data_offset, name, 
636                  werror_str(*status)));
637
638         ps->data_offset += sizeof(uint32);
639
640         return True;
641 }
642
643
644 /******************************************************************
645  Stream an array of uint8s. Length is number of uint8s.
646  ********************************************************************/
647
648 BOOL prs_uint8s(BOOL charmode, char *name, prs_struct *ps, int depth, uint8 *data8s, int len)
649 {
650         int i;
651         char *q = prs_mem_get(ps, len);
652         if (q == NULL)
653                 return False;
654
655         if (UNMARSHALLING(ps)) {
656                 for (i = 0; i < len; i++)
657                         data8s[i] = CVAL(q,i);
658         } else {
659                 for (i = 0; i < len; i++)
660                         SCVAL(q, i, data8s[i]);
661         }
662
663     DEBUG(5,("%s%04x %s: ", tab_depth(depth), ps->data_offset ,name));
664     if (charmode)
665                 print_asc(5, (unsigned char*)data8s, len);
666         else {
667         for (i = 0; i < len; i++)
668                         DEBUG(5,("%02x ", data8s[i]));
669         }
670     DEBUG(5,("\n"));
671
672         ps->data_offset += len;
673
674         return True;
675 }
676
677 /******************************************************************
678  Stream an array of uint16s. Length is number of uint16s.
679  ********************************************************************/
680
681 BOOL prs_uint16s(BOOL charmode, char *name, prs_struct *ps, int depth, uint16 *data16s, int len)
682 {
683         int i;
684         char *q = prs_mem_get(ps, len * sizeof(uint16));
685         if (q == NULL)
686                 return False;
687
688         if (UNMARSHALLING(ps)) {
689                 if (ps->bigendian_data) {
690                         for (i = 0; i < len; i++)
691                                 data16s[i] = RSVAL(q, 2*i);
692                 } else {
693                         for (i = 0; i < len; i++)
694                                 data16s[i] = SVAL(q, 2*i);
695                 }
696         } else {
697                 if (ps->bigendian_data) {
698                         for (i = 0; i < len; i++)
699                                 RSSVAL(q, 2*i, data16s[i]);
700                 } else {
701                         for (i = 0; i < len; i++)
702                                 SSVAL(q, 2*i, data16s[i]);
703                 }
704         }
705
706         DEBUG(5,("%s%04x %s: ", tab_depth(depth), ps->data_offset, name));
707         if (charmode)
708                 print_asc(5, (unsigned char*)data16s, 2*len);
709         else {
710                 for (i = 0; i < len; i++)
711                         DEBUG(5,("%04x ", data16s[i]));
712         }
713     DEBUG(5,("\n"));
714
715         ps->data_offset += (len * sizeof(uint16));
716
717         return True;
718 }
719
720 /******************************************************************
721  Start using a function for streaming unicode chars. If unmarshalling,
722  output must be little-endian, if marshalling, input must be little-endian.
723  ********************************************************************/
724
725 static void dbg_rw_punival(BOOL charmode, char *name, int depth, prs_struct *ps,
726                                                         char *in_buf, char *out_buf, int len)
727 {
728         int i;
729
730         if (UNMARSHALLING(ps)) {
731                 if (ps->bigendian_data) {
732                         for (i = 0; i < len; i++)
733                                 SSVAL(out_buf,2*i,RSVAL(in_buf, 2*i));
734                 } else {
735                         for (i = 0; i < len; i++)
736                                 SSVAL(out_buf, 2*i, SVAL(in_buf, 2*i));
737                 }
738         } else {
739                 if (ps->bigendian_data) {
740                         for (i = 0; i < len; i++)
741                                 RSSVAL(in_buf, 2*i, SVAL(out_buf,2*i));
742                 } else {
743                         for (i = 0; i < len; i++)
744                                 SSVAL(in_buf, 2*i, SVAL(out_buf,2*i));
745                 }
746         }
747
748         DEBUG(5,("%s%04x %s: ", tab_depth(depth), ps->data_offset, name));
749         if (charmode)
750                 print_asc(5, (unsigned char*)out_buf, 2*len);
751         else {
752                 for (i = 0; i < len; i++)
753                         DEBUG(5,("%04x ", out_buf[i]));
754         }
755     DEBUG(5,("\n"));
756 }
757
758 /******************************************************************
759  Stream a unistr. Always little endian.
760  ********************************************************************/
761
762 BOOL prs_uint16uni(BOOL charmode, char *name, prs_struct *ps, int depth, uint16 *data16s, int len)
763 {
764         char *q = prs_mem_get(ps, len * sizeof(uint16));
765         if (q == NULL)
766                 return False;
767
768         dbg_rw_punival(charmode, name, depth, ps, q, (char *)data16s, len);
769         ps->data_offset += (len * sizeof(uint16));
770
771         return True;
772 }
773
774 /******************************************************************
775  Stream an array of uint32s. Length is number of uint32s.
776  ********************************************************************/
777
778 BOOL prs_uint32s(BOOL charmode, char *name, prs_struct *ps, int depth, uint32 *data32s, int len)
779 {
780         int i;
781         char *q = prs_mem_get(ps, len * sizeof(uint32));
782         if (q == NULL)
783                 return False;
784
785         if (UNMARSHALLING(ps)) {
786                 if (ps->bigendian_data) {
787                         for (i = 0; i < len; i++)
788                                 data32s[i] = RIVAL(q, 4*i);
789                 } else {
790                         for (i = 0; i < len; i++)
791                                 data32s[i] = IVAL(q, 4*i);
792                 }
793         } else {
794                 if (ps->bigendian_data) {
795                         for (i = 0; i < len; i++)
796                                 RSIVAL(q, 4*i, data32s[i]);
797                 } else {
798                         for (i = 0; i < len; i++)
799                                 SIVAL(q, 4*i, data32s[i]);
800                 }
801         }
802
803         DEBUG(5,("%s%04x %s: ", tab_depth(depth), ps->data_offset, name));
804         if (charmode)
805                 print_asc(5, (unsigned char*)data32s, 4*len);
806         else {
807                 for (i = 0; i < len; i++)
808                         DEBUG(5,("%08x ", data32s[i]));
809         }
810     DEBUG(5,("\n"));
811
812         ps->data_offset += (len * sizeof(uint32));
813
814         return True;
815 }
816
817 /******************************************************************
818  Stream an array of unicode string, length/buffer specified separately,
819  in uint16 chars. The unicode string is already in little-endian format.
820  ********************************************************************/
821
822 BOOL prs_buffer5(BOOL charmode, char *name, prs_struct *ps, int depth, BUFFER5 *str)
823 {
824         char *p;
825         char *q = prs_mem_get(ps, str->buf_len * sizeof(uint16));
826         if (q == NULL)
827                 return False;
828
829         if (UNMARSHALLING(ps)) {
830                 str->buffer = (uint16 *)prs_alloc_mem(ps,str->buf_len * sizeof(uint16));
831                 if (str->buffer == NULL)
832                         return False;
833         }
834
835         /* If the string is empty, we don't have anything to stream */
836         if (str->buf_len==0)
837                 return True;
838
839         p = (char *)str->buffer;
840
841         dbg_rw_punival(charmode, name, depth, ps, q, p, str->buf_len);
842         
843         ps->data_offset += (str->buf_len * sizeof(uint16));
844
845         return True;
846 }
847
848 /******************************************************************
849  Stream a "not" unicode string, length/buffer specified separately,
850  in byte chars. String is in little-endian format.
851  ********************************************************************/
852
853 BOOL prs_buffer2(BOOL charmode, char *name, prs_struct *ps, int depth, BUFFER2 *str)
854 {
855         char *p;
856         char *q = prs_mem_get(ps, str->buf_len);
857         if (q == NULL)
858                 return False;
859
860         if (UNMARSHALLING(ps)) {
861                 str->buffer = (uint16 *)prs_alloc_mem(ps,str->buf_len);
862                 if (str->buffer == NULL)
863                         return False;
864         }
865
866         p = (char *)str->buffer;
867
868         dbg_rw_punival(charmode, name, depth, ps, q, p, str->buf_len/2);
869         ps->data_offset += str->buf_len;
870
871         return True;
872 }
873
874 /******************************************************************
875  Stream a string, length/buffer specified separately,
876  in uint8 chars.
877  ********************************************************************/
878
879 BOOL prs_string2(BOOL charmode, char *name, prs_struct *ps, int depth, STRING2 *str)
880 {
881         int i;
882         char *q = prs_mem_get(ps, str->str_max_len);
883         if (q == NULL)
884                 return False;
885
886         if (UNMARSHALLING(ps)) {
887                 str->buffer = (unsigned char *)prs_alloc_mem(ps,str->str_max_len);
888                 if (str->buffer == NULL)
889                         return False;
890         }
891
892         if (UNMARSHALLING(ps)) {
893                 for (i = 0; i < str->str_str_len; i++)
894                         str->buffer[i] = CVAL(q,i);
895         } else {
896                 for (i = 0; i < str->str_str_len; i++)
897                         SCVAL(q, i, str->buffer[i]);
898         }
899
900     DEBUG(5,("%s%04x %s: ", tab_depth(depth), ps->data_offset, name));
901     if (charmode)
902                 print_asc(5, (unsigned char*)str->buffer, str->str_str_len);
903         else {
904         for (i = 0; i < str->str_str_len; i++)
905                         DEBUG(5,("%02x ", str->buffer[i]));
906         }
907     DEBUG(5,("\n"));
908
909         ps->data_offset += str->str_str_len;
910
911         return True;
912 }
913
914 /******************************************************************
915  Stream a unicode string, length/buffer specified separately,
916  in uint16 chars. The unicode string is already in little-endian format.
917  ********************************************************************/
918
919 BOOL prs_unistr2(BOOL charmode, char *name, prs_struct *ps, int depth, UNISTR2 *str)
920 {
921         char *p;
922         char *q = prs_mem_get(ps, str->uni_str_len * sizeof(uint16));
923         if (q == NULL)
924                 return False;
925
926         /* If the string is empty, we don't have anything to stream */
927         if (str->uni_str_len==0)
928                 return True;
929
930         if (UNMARSHALLING(ps)) {
931                 str->buffer = (uint16 *)prs_alloc_mem(ps,str->uni_max_len * sizeof(uint16));
932                 if (str->buffer == NULL)
933                         return False;
934         }
935
936         p = (char *)str->buffer;
937
938         dbg_rw_punival(charmode, name, depth, ps, q, p, str->uni_str_len);
939         
940         ps->data_offset += (str->uni_str_len * sizeof(uint16));
941
942         return True;
943 }
944
945 /******************************************************************
946  Stream a unicode string, length/buffer specified separately,
947  in uint16 chars. The unicode string is already in little-endian format.
948  ********************************************************************/
949
950 BOOL prs_unistr3(BOOL charmode, char *name, UNISTR3 *str, prs_struct *ps, int depth)
951 {
952         char *p;
953         char *q = prs_mem_get(ps, str->uni_str_len * sizeof(uint16));
954         if (q == NULL)
955                 return False;
956
957         if (UNMARSHALLING(ps)) {
958                 str->str.buffer = (uint16 *)prs_alloc_mem(ps,str->uni_str_len * sizeof(uint16));
959                 if (str->str.buffer == NULL)
960                         return False;
961         }
962
963         p = (char *)str->str.buffer;
964
965         dbg_rw_punival(charmode, name, depth, ps, q, p, str->uni_str_len);
966         ps->data_offset += (str->uni_str_len * sizeof(uint16));
967
968         return True;
969 }
970
971 /*******************************************************************
972  Stream a unicode  null-terminated string. As the string is already
973  in little-endian format then do it as a stream of bytes.
974  ********************************************************************/
975
976 BOOL prs_unistr(char *name, prs_struct *ps, int depth, UNISTR *str)
977 {
978         int len = 0;
979         unsigned char *p = (unsigned char *)str->buffer;
980         uint8 *start;
981         char *q;
982         uint32 max_len;
983         uint16* ptr;
984
985         if (MARSHALLING(ps)) {
986
987                 for(len = 0; str->buffer[len] != 0; len++)
988                         ;
989
990                 q = prs_mem_get(ps, (len+1)*2);
991                 if (q == NULL)
992                         return False;
993
994                 start = (uint8*)q;
995
996                 for(len = 0; str->buffer[len] != 0; len++) 
997                 {
998                         if(ps->bigendian_data) 
999                         {
1000                                 /* swap bytes - p is little endian, q is big endian. */
1001                                 q[0] = (char)p[1];
1002                                 q[1] = (char)p[0];
1003                                 p += 2;
1004                                 q += 2;
1005                         } 
1006                         else 
1007                         {
1008                                 q[0] = (char)p[0];
1009                                 q[1] = (char)p[1];
1010                                 p += 2;
1011                                 q += 2;
1012                         }
1013                 }
1014
1015                 /*
1016                  * even if the string is 'empty' (only an \0 char)
1017                  * at this point the leading \0 hasn't been parsed.
1018                  * so parse it now
1019                  */
1020
1021                 q[0] = 0;
1022                 q[1] = 0;
1023                 q += 2;
1024
1025                 len++;
1026
1027                 dump_data(5+depth, (char *)start, len * 2);
1028         }
1029         else { /* unmarshalling */
1030         
1031                 uint32 alloc_len = 0;
1032                 q = prs_data_p(ps) + prs_offset(ps);
1033
1034                 /*
1035                  * Work out how much space we need and talloc it.
1036                  */
1037                 max_len = (ps->buffer_size - ps->data_offset)/sizeof(uint16);
1038
1039                 /* the test of the value of *ptr helps to catch the circumstance
1040                    where we have an emtpty (non-existent) string in the buffer */
1041                 for ( ptr = (uint16 *)q; *ptr && (alloc_len <= max_len); alloc_len++)
1042                         /* do nothing */ 
1043                         ;
1044
1045                 /* should we allocate anything at all? */
1046                 str->buffer = (uint16 *)prs_alloc_mem(ps,alloc_len * sizeof(uint16));
1047                 if ((str->buffer == NULL) && (alloc_len > 0))
1048                         return False;
1049
1050                 p = (unsigned char *)str->buffer;
1051
1052                 len = 0;
1053                 /* the (len < alloc_len) test is to prevent us from overwriting
1054                    memory that is not ours...if we get that far, we have a non-null
1055                    terminated string in the buffer and have messed up somewhere */
1056                 while ((len < alloc_len) && (*(uint16 *)q != 0))
1057                 {
1058                         if(ps->bigendian_data) 
1059                         {
1060                                 /* swap bytes - q is big endian, p is little endian. */
1061                                 p[0] = (unsigned char)q[1];
1062                                 p[1] = (unsigned char)q[0];
1063                                 p += 2;
1064                                 q += 2;
1065                         } else {
1066
1067                                 p[0] = (unsigned char)q[0];
1068                                 p[1] = (unsigned char)q[1];
1069                                 p += 2;
1070                                 q += 2;
1071                         }
1072
1073                         len++;
1074                 } 
1075                 if (len < alloc_len)
1076                 {
1077                         /* NULL terminate the UNISTR */
1078                         str->buffer[len++] = '\0';
1079                 }
1080         }
1081
1082         /* set the offset in the prs_struct; 'len' points to the
1083            terminiating NULL in the UNISTR so we need to go one more
1084            uint16 */
1085         ps->data_offset += (len)*2;
1086         
1087         return True;
1088 }
1089
1090
1091 /*******************************************************************
1092  Stream a null-terminated string.  len is strlen, and therefore does
1093  not include the null-termination character.
1094  ********************************************************************/
1095
1096 BOOL prs_string(char *name, prs_struct *ps, int depth, char *str, int len, int max_buf_size)
1097 {
1098         char *q;
1099         int i;
1100
1101         len = MIN(len, (max_buf_size-1));
1102
1103         q = prs_mem_get(ps, len+1);
1104         if (q == NULL)
1105                 return False;
1106
1107         for(i = 0; i < len; i++) {
1108                 if (UNMARSHALLING(ps))
1109                         str[i] = q[i];
1110                 else
1111                         q[i] = str[i];
1112         }
1113
1114         /* The terminating null. */
1115         str[i] = '\0';
1116
1117         if (MARSHALLING(ps)) {
1118                 q[i] = '\0';
1119         }
1120
1121         ps->data_offset += len+1;
1122
1123         dump_data(5+depth, q, len);
1124
1125         return True;
1126 }
1127
1128 /*******************************************************************
1129  prs_uint16 wrapper. Call this and it sets up a pointer to where the
1130  uint16 should be stored, or gets the size if reading.
1131  ********************************************************************/
1132
1133 BOOL prs_uint16_pre(char *name, prs_struct *ps, int depth, uint16 *data16, uint32 *offset)
1134 {
1135         *offset = ps->data_offset;
1136         if (UNMARSHALLING(ps)) {
1137                 /* reading. */
1138                 return prs_uint16(name, ps, depth, data16);
1139         } else {
1140                 char *q = prs_mem_get(ps, sizeof(uint16));
1141                 if(q ==NULL)
1142                         return False;
1143                 ps->data_offset += sizeof(uint16);
1144         }
1145         return True;
1146 }
1147
1148 /*******************************************************************
1149  prs_uint16 wrapper.  call this and it retrospectively stores the size.
1150  does nothing on reading, as that is already handled by ...._pre()
1151  ********************************************************************/
1152
1153 BOOL prs_uint16_post(char *name, prs_struct *ps, int depth, uint16 *data16,
1154                                 uint32 ptr_uint16, uint32 start_offset)
1155 {
1156         if (MARSHALLING(ps)) {
1157                 /* 
1158                  * Writing - temporarily move the offset pointer.
1159                  */
1160                 uint16 data_size = ps->data_offset - start_offset;
1161                 uint32 old_offset = ps->data_offset;
1162
1163                 ps->data_offset = ptr_uint16;
1164                 if(!prs_uint16(name, ps, depth, &data_size)) {
1165                         ps->data_offset = old_offset;
1166                         return False;
1167                 }
1168                 ps->data_offset = old_offset;
1169         } else {
1170                 ps->data_offset = start_offset + (uint32)(*data16);
1171         }
1172         return True;
1173 }
1174
1175 /*******************************************************************
1176  prs_uint32 wrapper. Call this and it sets up a pointer to where the
1177  uint32 should be stored, or gets the size if reading.
1178  ********************************************************************/
1179
1180 BOOL prs_uint32_pre(char *name, prs_struct *ps, int depth, uint32 *data32, uint32 *offset)
1181 {
1182         *offset = ps->data_offset;
1183         if (UNMARSHALLING(ps) && (data32 != NULL)) {
1184                 /* reading. */
1185                 return prs_uint32(name, ps, depth, data32);
1186         } else {
1187                 ps->data_offset += sizeof(uint32);
1188         }
1189         return True;
1190 }
1191
1192 /*******************************************************************
1193  prs_uint32 wrapper.  call this and it retrospectively stores the size.
1194  does nothing on reading, as that is already handled by ...._pre()
1195  ********************************************************************/
1196
1197 BOOL prs_uint32_post(char *name, prs_struct *ps, int depth, uint32 *data32,
1198                                 uint32 ptr_uint32, uint32 data_size)
1199 {
1200         if (MARSHALLING(ps)) {
1201                 /* 
1202                  * Writing - temporarily move the offset pointer.
1203                  */
1204                 uint32 old_offset = ps->data_offset;
1205                 ps->data_offset = ptr_uint32;
1206                 if(!prs_uint32(name, ps, depth, &data_size)) {
1207                         ps->data_offset = old_offset;
1208                         return False;
1209                 }
1210                 ps->data_offset = old_offset;
1211         }
1212         return True;
1213 }
1214
1215 /* useful function to store a structure in rpc wire format */
1216 int tdb_prs_store(TDB_CONTEXT *tdb, char *keystr, prs_struct *ps)
1217 {
1218     TDB_DATA kbuf, dbuf;
1219     kbuf.dptr = keystr;
1220     kbuf.dsize = strlen(keystr)+1;
1221     dbuf.dptr = prs_data_p(ps);
1222     dbuf.dsize = prs_offset(ps);
1223     return tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
1224 }
1225
1226 /* useful function to fetch a structure into rpc wire format */
1227 int tdb_prs_fetch(TDB_CONTEXT *tdb, char *keystr, prs_struct *ps, TALLOC_CTX *mem_ctx)
1228 {
1229     TDB_DATA kbuf, dbuf;
1230     kbuf.dptr = keystr;
1231     kbuf.dsize = strlen(keystr)+1;
1232
1233     dbuf = tdb_fetch(tdb, kbuf);
1234     if (!dbuf.dptr) return -1;
1235
1236     ZERO_STRUCTP(ps);
1237     prs_init(ps, 0, mem_ctx, UNMARSHALL);
1238     prs_give_memory(ps, dbuf.dptr, dbuf.dsize, True);
1239
1240     return 0;
1241
1242
1243 /*******************************************************************
1244  hash a stream.
1245  ********************************************************************/
1246 BOOL prs_hash1(prs_struct *ps, uint32 offset, uint8 sess_key[16])
1247 {
1248         char *q;
1249
1250         q = prs_data_p(ps);
1251         q = &q[offset];
1252
1253 #ifdef DEBUG_PASSWORD
1254         DEBUG(100, ("prs_hash1\n"));
1255         dump_data(100, sess_key, 16);
1256         dump_data(100, q, 68);
1257 #endif
1258         SamOEMhash((uchar *) q, sess_key, 68);
1259
1260 #ifdef DEBUG_PASSWORD
1261         dump_data(100, q, 68);
1262 #endif
1263
1264         return True;
1265 }