41bfd8ae0f2990d98c268156f2ff0bf190a0a61a
[kamenim/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    Copyright (C) Andrew Bartlett              2003.
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 3 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, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "../librpc/gen_ndr/ndr_schannel.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_RPC_PARSE
28
29 /**
30  * Dump a prs to a file: from the current location through to the end.
31  **/
32 void prs_dump(const char *name, int v, prs_struct *ps)
33 {
34         prs_dump_region(name, v, ps, ps->data_offset, ps->buffer_size);
35 }
36
37 /**
38  * Dump from the start of the prs to the current location.
39  **/
40 void prs_dump_before(const char *name, int v, prs_struct *ps)
41 {
42         prs_dump_region(name, v, ps, 0, ps->data_offset);
43 }
44
45 /**
46  * Dump everything from the start of the prs up to the current location.
47  **/
48 void prs_dump_region(const char *name, int v, prs_struct *ps,
49                      int from_off, int to_off)
50 {
51         int fd, i;
52         char *fname = NULL;
53         ssize_t sz;
54         if (DEBUGLEVEL < 50) return;
55         for (i=1;i<100;i++) {
56                 if (v != -1) {
57                         if (asprintf(&fname,"/tmp/%s_%d.%d.prs", name, v, i) < 0) {
58                                 return;
59                         }
60                 } else {
61                         if (asprintf(&fname,"/tmp/%s.%d.prs", name, i) < 0) {
62                                 return;
63                         }
64                 }
65                 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0644);
66                 if (fd != -1 || errno != EEXIST) break;
67         }
68         if (fd != -1) {
69                 sz = write(fd, ps->data_p + from_off, to_off - from_off);
70                 i = close(fd);
71                 if ( (sz != to_off-from_off) || (i != 0) ) {
72                         DEBUG(0,("Error writing/closing %s: %ld!=%ld %d\n", fname, (unsigned long)sz, (unsigned long)to_off-from_off, i ));
73                 } else {
74                         DEBUG(0,("created %s\n", fname));
75                 }
76         }
77         SAFE_FREE(fname);
78 }
79
80 /*******************************************************************
81  Debug output for parsing info
82
83  XXXX side-effect of this function is to increase the debug depth XXXX.
84
85 ********************************************************************/
86
87 void prs_debug(prs_struct *ps, int depth, const char *desc, const char *fn_name)
88 {
89         DEBUG(5+depth, ("%s%06x %s %s\n", tab_depth(5+depth,depth), ps->data_offset, fn_name, desc));
90 }
91
92 /**
93  * Initialise an expandable parse structure.
94  *
95  * @param size Initial buffer size.  If >0, a new buffer will be
96  * created with malloc().
97  *
98  * @return False if allocation fails, otherwise True.
99  **/
100
101 bool prs_init(prs_struct *ps, uint32 size, TALLOC_CTX *ctx, bool io)
102 {
103         ZERO_STRUCTP(ps);
104         ps->io = io;
105         ps->bigendian_data = RPC_LITTLE_ENDIAN;
106         ps->align = RPC_PARSE_ALIGN;
107         ps->is_dynamic = False;
108         ps->data_offset = 0;
109         ps->buffer_size = 0;
110         ps->data_p = NULL;
111         ps->mem_ctx = ctx;
112
113         if (size != 0) {
114                 ps->buffer_size = size;
115                 if((ps->data_p = (char *)SMB_MALLOC((size_t)size)) == NULL) {
116                         DEBUG(0,("prs_init: malloc fail for %u bytes.\n", (unsigned int)size));
117                         return False;
118                 }
119                 memset(ps->data_p, '\0', (size_t)size);
120                 ps->is_dynamic = True; /* We own this memory. */
121         } else if (MARSHALLING(ps)) {
122                 /* If size is zero and we're marshalling we should allocate memory on demand. */
123                 ps->is_dynamic = True;
124         }
125
126         return True;
127 }
128
129 /*******************************************************************
130  Delete the memory in a parse structure - if we own it.
131
132  NOTE: Contrary to the somewhat confusing naming, this function is not
133        intended for freeing memory allocated by prs_alloc_mem().  That memory
134        is attached to the talloc context given by ps->mem_ctx.
135  ********************************************************************/
136
137 void prs_mem_free(prs_struct *ps)
138 {
139         if(ps->is_dynamic)
140                 SAFE_FREE(ps->data_p);
141         ps->is_dynamic = False;
142         ps->buffer_size = 0;
143         ps->data_offset = 0;
144 }
145
146 /*******************************************************************
147  Clear the memory in a parse structure.
148  ********************************************************************/
149
150 void prs_mem_clear(prs_struct *ps)
151 {
152         if (ps->buffer_size)
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 #if defined(PARANOID_MALLOC_CHECKER)
161 char *prs_alloc_mem_(prs_struct *ps, size_t size, unsigned int count)
162 #else
163 char *prs_alloc_mem(prs_struct *ps, size_t size, unsigned int count)
164 #endif
165 {
166         char *ret = NULL;
167
168         if (size && count) {
169                 /* We can't call the type-safe version here. */
170                 ret = (char *)_talloc_zero_array(ps->mem_ctx, size, count,
171                                                  "parse_prs");
172         }
173         return ret;
174 }
175
176 /*******************************************************************
177  Return the current talloc context we're using.
178  ********************************************************************/
179
180 TALLOC_CTX *prs_get_mem_context(prs_struct *ps)
181 {
182         return ps->mem_ctx;
183 }
184
185 /*******************************************************************
186  Hand some already allocated memory to a prs_struct.
187  ********************************************************************/
188
189 void prs_give_memory(prs_struct *ps, char *buf, uint32 size, bool is_dynamic)
190 {
191         ps->is_dynamic = is_dynamic;
192         ps->data_p = buf;
193         ps->buffer_size = size;
194 }
195
196 /*******************************************************************
197  Set a prs_struct to exactly a given size. Will grow or tuncate if neccessary.
198  ********************************************************************/
199
200 bool prs_set_buffer_size(prs_struct *ps, uint32 newsize)
201 {
202         if (newsize > ps->buffer_size)
203                 return prs_force_grow(ps, newsize - ps->buffer_size);
204
205         if (newsize < ps->buffer_size) {
206                 ps->buffer_size = newsize;
207
208                 /* newsize == 0 acts as a free and set pointer to NULL */
209                 if (newsize == 0) {
210                         SAFE_FREE(ps->data_p);
211                 } else {
212                         ps->data_p = (char *)SMB_REALLOC(ps->data_p, newsize);
213
214                         if (ps->data_p == NULL) {
215                                 DEBUG(0,("prs_set_buffer_size: Realloc failure for size %u.\n",
216                                         (unsigned int)newsize));
217                                 DEBUG(0,("prs_set_buffer_size: Reason %s\n",strerror(errno)));
218                                 return False;
219                         }
220                 }
221         }
222
223         return True;
224 }
225
226 /*******************************************************************
227  Attempt, if needed, to grow a data buffer.
228  Also depends on the data stream mode (io).
229  ********************************************************************/
230
231 bool prs_grow(prs_struct *ps, uint32 extra_space)
232 {
233         uint32 new_size;
234
235         ps->grow_size = MAX(ps->grow_size, ps->data_offset + extra_space);
236
237         if(ps->data_offset + extra_space <= ps->buffer_size)
238                 return True;
239
240         /*
241          * We cannot grow the buffer if we're not reading
242          * into the prs_struct, or if we don't own the memory.
243          */
244
245         if(UNMARSHALLING(ps) || !ps->is_dynamic) {
246                 DEBUG(0,("prs_grow: Buffer overflow - unable to expand buffer by %u bytes.\n",
247                                 (unsigned int)extra_space));
248                 return False;
249         }
250         
251         /*
252          * Decide how much extra space we really need.
253          */
254
255         extra_space -= (ps->buffer_size - ps->data_offset);
256         if(ps->buffer_size == 0) {
257
258                 /*
259                  * Start with 128 bytes (arbitrary value), enough for small rpc
260                  * requests
261                  */
262                 new_size = MAX(128, extra_space);
263
264                 if((ps->data_p = (char *)SMB_MALLOC(new_size)) == NULL) {
265                         DEBUG(0,("prs_grow: Malloc failure for size %u.\n", (unsigned int)new_size));
266                         return False;
267                 }
268                 memset(ps->data_p, '\0', (size_t)new_size );
269         } else {
270                 /*
271                  * If the current buffer size is bigger than the space needed,
272                  * just double it, else add extra_space. Always keep 64 bytes
273                  * more, so that after we added a large blob we don't have to
274                  * realloc immediately again.
275                  */
276                 new_size = MAX(ps->buffer_size*2,
277                                ps->buffer_size + extra_space + 64);
278
279                 if ((ps->data_p = (char *)SMB_REALLOC(ps->data_p, new_size)) == NULL) {
280                         DEBUG(0,("prs_grow: Realloc failure for size %u.\n",
281                                 (unsigned int)new_size));
282                         return False;
283                 }
284
285                 memset(&ps->data_p[ps->buffer_size], '\0', (size_t)(new_size - ps->buffer_size));
286         }
287         ps->buffer_size = new_size;
288
289         return True;
290 }
291
292 /*******************************************************************
293  Attempt to force a data buffer to grow by len bytes.
294  This is only used when appending more data onto a prs_struct
295  when reading an rpc reply, before unmarshalling it.
296  ********************************************************************/
297
298 bool prs_force_grow(prs_struct *ps, uint32 extra_space)
299 {
300         uint32 new_size = ps->buffer_size + extra_space;
301
302         if(!UNMARSHALLING(ps) || !ps->is_dynamic) {
303                 DEBUG(0,("prs_force_grow: Buffer overflow - unable to expand buffer by %u bytes.\n",
304                                 (unsigned int)extra_space));
305                 return False;
306         }
307
308         if((ps->data_p = (char *)SMB_REALLOC(ps->data_p, new_size)) == NULL) {
309                 DEBUG(0,("prs_force_grow: Realloc failure for size %u.\n",
310                         (unsigned int)new_size));
311                 return False;
312         }
313
314         memset(&ps->data_p[ps->buffer_size], '\0', (size_t)(new_size - ps->buffer_size));
315
316         ps->buffer_size = new_size;
317
318         return True;
319 }
320
321 /*******************************************************************
322  Get the data pointer (external interface).
323 ********************************************************************/
324
325 char *prs_data_p(prs_struct *ps)
326 {
327         return ps->data_p;
328 }
329
330 /*******************************************************************
331  Get the current data size (external interface).
332  ********************************************************************/
333
334 uint32 prs_data_size(prs_struct *ps)
335 {
336         return ps->buffer_size;
337 }
338
339 /*******************************************************************
340  Fetch the current offset (external interface).
341  ********************************************************************/
342
343 uint32 prs_offset(prs_struct *ps)
344 {
345         return ps->data_offset;
346 }
347
348 /*******************************************************************
349  Set the current offset (external interface).
350  ********************************************************************/
351
352 bool prs_set_offset(prs_struct *ps, uint32 offset)
353 {
354         if ((offset > ps->data_offset)
355             && !prs_grow(ps, offset - ps->data_offset)) {
356                 return False;
357         }
358
359         ps->data_offset = offset;
360         return True;
361 }
362
363 /*******************************************************************
364  Append the data from one parse_struct into another.
365  ********************************************************************/
366
367 bool prs_append_prs_data(prs_struct *dst, prs_struct *src)
368 {
369         if (prs_offset(src) == 0)
370                 return True;
371
372         if(!prs_grow(dst, prs_offset(src)))
373                 return False;
374
375         memcpy(&dst->data_p[dst->data_offset], src->data_p, (size_t)prs_offset(src));
376         dst->data_offset += prs_offset(src);
377
378         return True;
379 }
380
381 /*******************************************************************
382  Append some data from one parse_struct into another.
383  ********************************************************************/
384
385 bool prs_append_some_data(prs_struct *dst, void *src_base, uint32_t start,
386                           uint32_t len)
387 {
388         if (len == 0) {
389                 return true;
390         }
391
392         if(!prs_grow(dst, len)) {
393                 return false;
394         }
395
396         memcpy(&dst->data_p[dst->data_offset], ((char *)src_base) + start, (size_t)len);
397         dst->data_offset += len;
398         return true;
399 }
400
401 bool prs_append_some_prs_data(prs_struct *dst, prs_struct *src, int32 start,
402                               uint32 len)
403 {
404         return prs_append_some_data(dst, src->data_p, start, len);
405 }
406
407 /*******************************************************************
408  Append the data from a buffer into a parse_struct.
409  ********************************************************************/
410
411 bool prs_copy_data_in(prs_struct *dst, const char *src, uint32 len)
412 {
413         if (len == 0)
414                 return True;
415
416         if(!prs_grow(dst, len))
417                 return False;
418
419         memcpy(&dst->data_p[dst->data_offset], src, (size_t)len);
420         dst->data_offset += len;
421
422         return True;
423 }
424
425 /*******************************************************************
426  Copy some data from a parse_struct into a buffer.
427  ********************************************************************/
428
429 bool prs_copy_data_out(char *dst, prs_struct *src, uint32 len)
430 {
431         if (len == 0)
432                 return True;
433
434         if(!prs_mem_get(src, len))
435                 return False;
436
437         memcpy(dst, &src->data_p[src->data_offset], (size_t)len);
438         src->data_offset += len;
439
440         return True;
441 }
442
443 /*******************************************************************
444  Copy all the data from a parse_struct into a buffer.
445  ********************************************************************/
446
447 bool prs_copy_all_data_out(char *dst, prs_struct *src)
448 {
449         uint32 len = prs_offset(src);
450
451         if (!len)
452                 return True;
453
454         prs_set_offset(src, 0);
455         return prs_copy_data_out(dst, src, len);
456 }
457
458 /*******************************************************************
459  Set the data as X-endian (external interface).
460  ********************************************************************/
461
462 void prs_set_endian_data(prs_struct *ps, bool endian)
463 {
464         ps->bigendian_data = endian;
465 }
466
467 /*******************************************************************
468  Align a the data_len to a multiple of align bytes - filling with
469  zeros.
470  ********************************************************************/
471
472 bool prs_align(prs_struct *ps)
473 {
474         uint32 mod = ps->data_offset & (ps->align-1);
475
476         if (ps->align != 0 && mod != 0) {
477                 uint32 extra_space = (ps->align - mod);
478                 if(!prs_grow(ps, extra_space))
479                         return False;
480                 memset(&ps->data_p[ps->data_offset], '\0', (size_t)extra_space);
481                 ps->data_offset += extra_space;
482         }
483
484         return True;
485 }
486
487 /******************************************************************
488  Align on a 2 byte boundary
489  *****************************************************************/
490  
491 bool prs_align_uint16(prs_struct *ps)
492 {
493         bool ret;
494         uint8 old_align = ps->align;
495
496         ps->align = 2;
497         ret = prs_align(ps);
498         ps->align = old_align;
499         
500         return ret;
501 }
502
503 /******************************************************************
504  Align on a 8 byte boundary
505  *****************************************************************/
506  
507 bool prs_align_uint64(prs_struct *ps)
508 {
509         bool ret;
510         uint8 old_align = ps->align;
511
512         ps->align = 8;
513         ret = prs_align(ps);
514         ps->align = old_align;
515         
516         return ret;
517 }
518
519 /******************************************************************
520  Align on a specific byte boundary
521  *****************************************************************/
522  
523 bool prs_align_custom(prs_struct *ps, uint8 boundary)
524 {
525         bool ret;
526         uint8 old_align = ps->align;
527
528         ps->align = boundary;
529         ret = prs_align(ps);
530         ps->align = old_align;
531         
532         return ret;
533 }
534
535
536
537 /*******************************************************************
538  Align only if required (for the unistr2 string mainly)
539  ********************************************************************/
540
541 bool prs_align_needed(prs_struct *ps, uint32 needed)
542 {
543         if (needed==0)
544                 return True;
545         else
546                 return prs_align(ps);
547 }
548
549 /*******************************************************************
550  Ensure we can read/write to a given offset.
551  ********************************************************************/
552
553 char *prs_mem_get(prs_struct *ps, uint32 extra_size)
554 {
555         if(UNMARSHALLING(ps)) {
556                 /*
557                  * If reading, ensure that we can read the requested size item.
558                  */
559                 if (ps->data_offset + extra_size > ps->buffer_size) {
560                         DEBUG(0,("prs_mem_get: reading data of size %u would overrun "
561                                 "buffer by %u bytes.\n",
562                                 (unsigned int)extra_size,
563                                 (unsigned int)(ps->data_offset + extra_size - ps->buffer_size) ));
564                         return NULL;
565                 }
566         } else {
567                 /*
568                  * Writing - grow the buffer if needed.
569                  */
570                 if(!prs_grow(ps, extra_size))
571                         return NULL;
572         }
573         return &ps->data_p[ps->data_offset];
574 }
575
576 /*******************************************************************
577  Change the struct type.
578  ********************************************************************/
579
580 void prs_switch_type(prs_struct *ps, bool io)
581 {
582         if ((ps->io ^ io) == True)
583                 ps->io=io;
584 }
585
586 /*******************************************************************
587  Force a prs_struct to be dynamic even when it's size is 0.
588  ********************************************************************/
589
590 void prs_force_dynamic(prs_struct *ps)
591 {
592         ps->is_dynamic=True;
593 }
594
595 /*******************************************************************
596  Associate a session key with a parse struct.
597  ********************************************************************/
598
599 void prs_set_session_key(prs_struct *ps, const char sess_key[16])
600 {
601         ps->sess_key = sess_key;
602 }
603
604 /*******************************************************************
605  Stream a uint8.
606  ********************************************************************/
607
608 bool prs_uint8(const char *name, prs_struct *ps, int depth, uint8 *data8)
609 {
610         char *q = prs_mem_get(ps, 1);
611         if (q == NULL)
612                 return False;
613
614         if (UNMARSHALLING(ps))
615                 *data8 = CVAL(q,0);
616         else
617                 SCVAL(q,0,*data8);
618
619         DEBUGADD(5,("%s%04x %s: %02x\n", tab_depth(5,depth), ps->data_offset, name, *data8));
620
621         ps->data_offset += 1;
622
623         return True;
624 }
625
626 /*******************************************************************
627  Stream a uint16.
628  ********************************************************************/
629
630 bool prs_uint16(const char *name, prs_struct *ps, int depth, uint16 *data16)
631 {
632         char *q = prs_mem_get(ps, sizeof(uint16));
633         if (q == NULL)
634                 return False;
635
636         if (UNMARSHALLING(ps)) {
637                 if (ps->bigendian_data)
638                         *data16 = RSVAL(q,0);
639                 else
640                         *data16 = SVAL(q,0);
641         } else {
642                 if (ps->bigendian_data)
643                         RSSVAL(q,0,*data16);
644                 else
645                         SSVAL(q,0,*data16);
646         }
647
648         DEBUGADD(5,("%s%04x %s: %04x\n", tab_depth(5,depth), ps->data_offset, name, *data16));
649
650         ps->data_offset += sizeof(uint16);
651
652         return True;
653 }
654
655 /*******************************************************************
656  Stream a uint32.
657  ********************************************************************/
658
659 bool prs_uint32(const char *name, prs_struct *ps, int depth, uint32 *data32)
660 {
661         char *q = prs_mem_get(ps, sizeof(uint32));
662         if (q == NULL)
663                 return False;
664
665         if (UNMARSHALLING(ps)) {
666                 if (ps->bigendian_data)
667                         *data32 = RIVAL(q,0);
668                 else
669                         *data32 = IVAL(q,0);
670         } else {
671                 if (ps->bigendian_data)
672                         RSIVAL(q,0,*data32);
673                 else
674                         SIVAL(q,0,*data32);
675         }
676
677         DEBUGADD(5,("%s%04x %s: %08x\n", tab_depth(5,depth), ps->data_offset, name, *data32));
678
679         ps->data_offset += sizeof(uint32);
680
681         return True;
682 }
683
684 /*******************************************************************
685  Stream an int32.
686  ********************************************************************/
687
688 bool prs_int32(const char *name, prs_struct *ps, int depth, int32 *data32)
689 {
690         char *q = prs_mem_get(ps, sizeof(int32));
691         if (q == NULL)
692                 return False;
693
694         if (UNMARSHALLING(ps)) {
695                 if (ps->bigendian_data)
696                         *data32 = RIVALS(q,0);
697                 else
698                         *data32 = IVALS(q,0);
699         } else {
700                 if (ps->bigendian_data)
701                         RSIVALS(q,0,*data32);
702                 else
703                         SIVALS(q,0,*data32);
704         }
705
706         DEBUGADD(5,("%s%04x %s: %08x\n", tab_depth(5,depth), ps->data_offset, name, *data32));
707
708         ps->data_offset += sizeof(int32);
709
710         return True;
711 }
712
713 /*******************************************************************
714  Stream a uint64_struct
715  ********************************************************************/
716 bool prs_uint64(const char *name, prs_struct *ps, int depth, uint64 *data64)
717 {
718         if (UNMARSHALLING(ps)) {
719                 uint32 high, low;
720
721                 if (!prs_uint32(name, ps, depth+1, &low))
722                         return False;
723
724                 if (!prs_uint32(name, ps, depth+1, &high))
725                         return False;
726
727                 *data64 = ((uint64_t)high << 32) + low;
728
729                 return True;
730         } else {
731                 uint32 high = (*data64) >> 32, low = (*data64) & 0xFFFFFFFF;
732                 return prs_uint32(name, ps, depth+1, &low) &&
733                            prs_uint32(name, ps, depth+1, &high);
734         }
735 }
736
737 /*******************************************************************
738  Stream a DCE error code
739  ********************************************************************/
740
741 bool prs_dcerpc_status(const char *name, prs_struct *ps, int depth, NTSTATUS *status)
742 {
743         char *q = prs_mem_get(ps, sizeof(uint32));
744         if (q == NULL)
745                 return False;
746
747         if (UNMARSHALLING(ps)) {
748                 if (ps->bigendian_data)
749                         *status = NT_STATUS(RIVAL(q,0));
750                 else
751                         *status = NT_STATUS(IVAL(q,0));
752         } else {
753                 if (ps->bigendian_data)
754                         RSIVAL(q,0,NT_STATUS_V(*status));
755                 else
756                         SIVAL(q,0,NT_STATUS_V(*status));
757         }
758
759         DEBUGADD(5,("%s%04x %s: %s\n", tab_depth(5,depth), ps->data_offset, name,
760                  dcerpc_errstr(talloc_tos(), NT_STATUS_V(*status))));
761
762         ps->data_offset += sizeof(uint32);
763
764         return True;
765 }
766
767 /******************************************************************
768  Stream an array of uint8s. Length is number of uint8s.
769  ********************************************************************/
770
771 bool prs_uint8s(bool charmode, const char *name, prs_struct *ps, int depth, uint8 *data8s, int len)
772 {
773         int i;
774         char *q = prs_mem_get(ps, len);
775         if (q == NULL)
776                 return False;
777
778         if (UNMARSHALLING(ps)) {
779                 for (i = 0; i < len; i++)
780                         data8s[i] = CVAL(q,i);
781         } else {
782                 for (i = 0; i < len; i++)
783                         SCVAL(q, i, data8s[i]);
784         }
785
786         DEBUGADD(5,("%s%04x %s: ", tab_depth(5,depth), ps->data_offset ,name));
787         if (charmode)
788                 print_asc(5, (unsigned char*)data8s, len);
789         else {
790                 for (i = 0; i < len; i++)
791                         DEBUGADD(5,("%02x ", data8s[i]));
792         }
793         DEBUGADD(5,("\n"));
794
795         ps->data_offset += len;
796
797         return True;
798 }
799
800 /******************************************************************
801  Stream an array of uint16s. Length is number of uint16s.
802  ********************************************************************/
803
804 bool prs_uint16s(bool charmode, const char *name, prs_struct *ps, int depth, uint16 *data16s, int len)
805 {
806         int i;
807         char *q = prs_mem_get(ps, len * sizeof(uint16));
808         if (q == NULL)
809                 return False;
810
811         if (UNMARSHALLING(ps)) {
812                 if (ps->bigendian_data) {
813                         for (i = 0; i < len; i++)
814                                 data16s[i] = RSVAL(q, 2*i);
815                 } else {
816                         for (i = 0; i < len; i++)
817                                 data16s[i] = SVAL(q, 2*i);
818                 }
819         } else {
820                 if (ps->bigendian_data) {
821                         for (i = 0; i < len; i++)
822                                 RSSVAL(q, 2*i, data16s[i]);
823                 } else {
824                         for (i = 0; i < len; i++)
825                                 SSVAL(q, 2*i, data16s[i]);
826                 }
827         }
828
829         DEBUGADD(5,("%s%04x %s: ", tab_depth(5,depth), ps->data_offset, name));
830         if (charmode)
831                 print_asc(5, (unsigned char*)data16s, 2*len);
832         else {
833                 for (i = 0; i < len; i++)
834                         DEBUGADD(5,("%04x ", data16s[i]));
835         }
836         DEBUGADD(5,("\n"));
837
838         ps->data_offset += (len * sizeof(uint16));
839
840         return True;
841 }
842
843 /******************************************************************
844  Stream an array of uint32s. Length is number of uint32s.
845  ********************************************************************/
846
847 bool prs_uint32s(bool charmode, const char *name, prs_struct *ps, int depth, uint32 *data32s, int len)
848 {
849         int i;
850         char *q = prs_mem_get(ps, len * sizeof(uint32));
851         if (q == NULL)
852                 return False;
853
854         if (UNMARSHALLING(ps)) {
855                 if (ps->bigendian_data) {
856                         for (i = 0; i < len; i++)
857                                 data32s[i] = RIVAL(q, 4*i);
858                 } else {
859                         for (i = 0; i < len; i++)
860                                 data32s[i] = IVAL(q, 4*i);
861                 }
862         } else {
863                 if (ps->bigendian_data) {
864                         for (i = 0; i < len; i++)
865                                 RSIVAL(q, 4*i, data32s[i]);
866                 } else {
867                         for (i = 0; i < len; i++)
868                                 SIVAL(q, 4*i, data32s[i]);
869                 }
870         }
871
872         DEBUGADD(5,("%s%04x %s: ", tab_depth(5,depth), ps->data_offset, name));
873         if (charmode)
874                 print_asc(5, (unsigned char*)data32s, 4*len);
875         else {
876                 for (i = 0; i < len; i++)
877                         DEBUGADD(5,("%08x ", data32s[i]));
878         }
879         DEBUGADD(5,("\n"));
880
881         ps->data_offset += (len * sizeof(uint32));
882
883         return True;
884 }
885
886 /*******************************************************************
887  Stream a unicode  null-terminated string. As the string is already
888  in little-endian format then do it as a stream of bytes.
889  ********************************************************************/
890
891 bool prs_unistr(const char *name, prs_struct *ps, int depth, UNISTR *str)
892 {
893         unsigned int len = 0;
894         unsigned char *p = (unsigned char *)str->buffer;
895         uint8 *start;
896         char *q;
897         uint32 max_len;
898         uint16* ptr;
899
900         if (MARSHALLING(ps)) {
901
902                 for(len = 0; str->buffer[len] != 0; len++)
903                         ;
904
905                 q = prs_mem_get(ps, (len+1)*2);
906                 if (q == NULL)
907                         return False;
908
909                 start = (uint8*)q;
910
911                 for(len = 0; str->buffer[len] != 0; len++) {
912                         if(ps->bigendian_data) {
913                                 /* swap bytes - p is little endian, q is big endian. */
914                                 q[0] = (char)p[1];
915                                 q[1] = (char)p[0];
916                                 p += 2;
917                                 q += 2;
918                         } 
919                         else 
920                         {
921                                 q[0] = (char)p[0];
922                                 q[1] = (char)p[1];
923                                 p += 2;
924                                 q += 2;
925                         }
926                 }
927
928                 /*
929                  * even if the string is 'empty' (only an \0 char)
930                  * at this point the leading \0 hasn't been parsed.
931                  * so parse it now
932                  */
933
934                 q[0] = 0;
935                 q[1] = 0;
936                 q += 2;
937
938                 len++;
939
940                 DEBUGADD(5,("%s%04x %s: ", tab_depth(5,depth), ps->data_offset, name));
941                 print_asc(5, (unsigned char*)start, 2*len);     
942                 DEBUGADD(5, ("\n"));
943         }
944         else { /* unmarshalling */
945         
946                 uint32 alloc_len = 0;
947                 q = ps->data_p + prs_offset(ps);
948
949                 /*
950                  * Work out how much space we need and talloc it.
951                  */
952                 max_len = (ps->buffer_size - ps->data_offset)/sizeof(uint16);
953
954                 /* the test of the value of *ptr helps to catch the circumstance
955                    where we have an emtpty (non-existent) string in the buffer */
956                 for ( ptr = (uint16 *)q; *ptr++ && (alloc_len <= max_len); alloc_len++)
957                         /* do nothing */ 
958                         ;
959
960                 if (alloc_len < max_len)
961                         alloc_len += 1;
962
963                 /* should we allocate anything at all? */
964                 str->buffer = PRS_ALLOC_MEM(ps,uint16,alloc_len);
965                 if ((str->buffer == NULL) && (alloc_len > 0))
966                         return False;
967
968                 p = (unsigned char *)str->buffer;
969
970                 len = 0;
971                 /* the (len < alloc_len) test is to prevent us from overwriting
972                    memory that is not ours...if we get that far, we have a non-null
973                    terminated string in the buffer and have messed up somewhere */
974                 while ((len < alloc_len) && (*(uint16 *)q != 0)) {
975                         if(ps->bigendian_data) 
976                         {
977                                 /* swap bytes - q is big endian, p is little endian. */
978                                 p[0] = (unsigned char)q[1];
979                                 p[1] = (unsigned char)q[0];
980                                 p += 2;
981                                 q += 2;
982                         } else {
983
984                                 p[0] = (unsigned char)q[0];
985                                 p[1] = (unsigned char)q[1];
986                                 p += 2;
987                                 q += 2;
988                         }
989
990                         len++;
991                 } 
992                 if (len < alloc_len) {
993                         /* NULL terminate the UNISTR */
994                         str->buffer[len++] = '\0';
995                 }
996
997                 DEBUGADD(5,("%s%04x %s: ", tab_depth(5,depth), ps->data_offset, name));
998                 print_asc(5, (unsigned char*)str->buffer, 2*len);       
999                 DEBUGADD(5, ("\n"));
1000         }
1001
1002         /* set the offset in the prs_struct; 'len' points to the
1003            terminiating NULL in the UNISTR so we need to go one more
1004            uint16 */
1005         ps->data_offset += (len)*2;
1006         
1007         return True;
1008 }
1009
1010 /*******************************************************************
1011 creates a new prs_struct containing a DATA_BLOB
1012 ********************************************************************/
1013 bool prs_init_data_blob(prs_struct *prs, DATA_BLOB *blob, TALLOC_CTX *mem_ctx)
1014 {
1015         if (!prs_init( prs, RPC_MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL ))
1016                 return False;
1017
1018
1019         if (!prs_copy_data_in(prs, (char *)blob->data, blob->length))
1020                 return False;
1021
1022         return True;
1023 }
1024
1025 /*******************************************************************
1026 return the contents of a prs_struct in a DATA_BLOB
1027 ********************************************************************/
1028 bool prs_data_blob(prs_struct *prs, DATA_BLOB *blob, TALLOC_CTX *mem_ctx)
1029 {
1030         blob->length = prs_data_size(prs);
1031         blob->data = (uint8 *)TALLOC_ZERO_SIZE(mem_ctx, blob->length);
1032         
1033         /* set the pointer at the end of the buffer */
1034         prs_set_offset( prs, prs_data_size(prs) );
1035
1036         if (!prs_copy_all_data_out((char *)blob->data, prs))
1037                 return False;
1038         
1039         return True;
1040 }