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