ec5bc9cf6a9f9a86f3044ef94b756a0543980fe0
[idra/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 talloc().
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                 ps->data_p = (char *)talloc_zero_size(ps->mem_ctx, size);
116                 if(ps->data_p == NULL) {
117                         DEBUG(0,("prs_init: talloc fail for %u bytes.\n", (unsigned int)size));
118                         return False;
119                 }
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().
134        That memory is also attached to the talloc context given by
135        ps->mem_ctx, but is only freed when that talloc context is
136        freed. prs_mem_free() is used to delete "dynamic" memory
137        allocated in marshalling/unmarshalling.
138  ********************************************************************/
139
140 void prs_mem_free(prs_struct *ps)
141 {
142         if(ps->is_dynamic) {
143                 TALLOC_FREE(ps->data_p);
144         }
145         ps->is_dynamic = False;
146         ps->buffer_size = 0;
147         ps->data_offset = 0;
148 }
149
150 /*******************************************************************
151  Clear the memory in a parse structure.
152  ********************************************************************/
153
154 void prs_mem_clear(prs_struct *ps)
155 {
156         if (ps->buffer_size)
157                 memset(ps->data_p, '\0', (size_t)ps->buffer_size);
158 }
159
160 /*******************************************************************
161  Allocate memory when unmarshalling... Always zero clears.
162  ********************************************************************/
163
164 #if defined(PARANOID_MALLOC_CHECKER)
165 char *prs_alloc_mem_(prs_struct *ps, size_t size, unsigned int count)
166 #else
167 char *prs_alloc_mem(prs_struct *ps, size_t size, unsigned int count)
168 #endif
169 {
170         char *ret = NULL;
171
172         if (size && count) {
173                 /* We can't call the type-safe version here. */
174                 ret = (char *)_talloc_zero_array(ps->mem_ctx, size, count,
175                                                  "parse_prs");
176         }
177         return ret;
178 }
179
180 /*******************************************************************
181  Return the current talloc context we're using.
182  ********************************************************************/
183
184 TALLOC_CTX *prs_get_mem_context(prs_struct *ps)
185 {
186         return ps->mem_ctx;
187 }
188
189 /*******************************************************************
190  Hand some already allocated memory to a prs_struct.
191  If "is_dynamic" is true, this is a talloc_steal.
192  If "is_dynamic" is false, just make ps->data_p a pointer to
193  the unwritable memory.
194  ********************************************************************/
195
196 void prs_give_memory(prs_struct *ps, char *buf, uint32 size, bool is_dynamic)
197 {
198         ps->is_dynamic = is_dynamic;
199         if (ps->is_dynamic && buf) {
200                 talloc_steal(ps->mem_ctx, buf);
201         }
202         ps->data_p = buf;
203         ps->buffer_size = size;
204 }
205
206 /*******************************************************************
207  Set a prs_struct to exactly a given size. Will grow or tuncate if neccessary.
208  ********************************************************************/
209
210 bool prs_set_buffer_size(prs_struct *ps, uint32 newsize)
211 {
212         if (newsize > ps->buffer_size)
213                 return prs_force_grow(ps, newsize - ps->buffer_size);
214
215         if (newsize < ps->buffer_size) {
216                 ps->buffer_size = newsize;
217
218                 /* newsize == 0 acts as a free and set pointer to NULL */
219                 if (newsize == 0) {
220                         TALLOC_FREE(ps->data_p);
221                 } else {
222                         ps->data_p = talloc_realloc(ps->mem_ctx,
223                                                 ps->data_p,
224                                                 char,
225                                                 newsize);
226
227                         if (ps->data_p == NULL) {
228                                 DEBUG(0,("prs_set_buffer_size: Realloc failure for size %u.\n",
229                                         (unsigned int)newsize));
230                                 return False;
231                         }
232                 }
233         }
234
235         return True;
236 }
237
238 /*******************************************************************
239  Attempt, if needed, to grow a data buffer.
240  Also depends on the data stream mode (io).
241  ********************************************************************/
242
243 bool prs_grow(prs_struct *ps, uint32 extra_space)
244 {
245         uint32 new_size;
246
247         ps->grow_size = MAX(ps->grow_size, ps->data_offset + extra_space);
248
249         if(ps->data_offset + extra_space <= ps->buffer_size)
250                 return True;
251
252         /*
253          * We cannot grow the buffer if we're not reading
254          * into the prs_struct, or if we don't own the memory.
255          */
256
257         if(UNMARSHALLING(ps) || !ps->is_dynamic) {
258                 DEBUG(0,("prs_grow: Buffer overflow - unable to expand buffer by %u bytes.\n",
259                                 (unsigned int)extra_space));
260                 return False;
261         }
262         
263         /*
264          * Decide how much extra space we really need.
265          */
266
267         extra_space -= (ps->buffer_size - ps->data_offset);
268         if(ps->buffer_size == 0) {
269
270                 /*
271                  * Start with 128 bytes (arbitrary value), enough for small rpc
272                  * requests
273                  */
274                 new_size = MAX(128, extra_space);
275
276                 ps->data_p = (char *)talloc_zero_size(ps->mem_ctx, new_size);
277                 if(ps->data_p == NULL) {
278                         DEBUG(0,("prs_grow: talloc failure for size %u.\n", (unsigned int)new_size));
279                         return False;
280                 }
281         } else {
282                 /*
283                  * If the current buffer size is bigger than the space needed,
284                  * just double it, else add extra_space. Always keep 64 bytes
285                  * more, so that after we added a large blob we don't have to
286                  * realloc immediately again.
287                  */
288                 new_size = MAX(ps->buffer_size*2,
289                                ps->buffer_size + extra_space + 64);
290
291                 ps->data_p = talloc_realloc(ps->mem_ctx,
292                                                 ps->data_p,
293                                                 char,
294                                                 new_size);
295                 if (ps->data_p == NULL) {
296                         DEBUG(0,("prs_grow: Realloc failure for size %u.\n",
297                                 (unsigned int)new_size));
298                         return False;
299                 }
300
301                 memset(&ps->data_p[ps->buffer_size], '\0', (size_t)(new_size - ps->buffer_size));
302         }
303         ps->buffer_size = new_size;
304
305         return True;
306 }
307
308 /*******************************************************************
309  Attempt to force a data buffer to grow by len bytes.
310  This is only used when appending more data onto a prs_struct
311  when reading an rpc reply, before unmarshalling it.
312  ********************************************************************/
313
314 bool prs_force_grow(prs_struct *ps, uint32 extra_space)
315 {
316         uint32 new_size = ps->buffer_size + extra_space;
317
318         if(!UNMARSHALLING(ps) || !ps->is_dynamic) {
319                 DEBUG(0,("prs_force_grow: Buffer overflow - unable to expand buffer by %u bytes.\n",
320                                 (unsigned int)extra_space));
321                 return False;
322         }
323
324         ps->data_p = (char *)talloc_realloc(ps->mem_ctx,
325                                         ps->data_p,
326                                         char,
327                                         new_size);
328         if(ps->data_p == NULL) {
329                 DEBUG(0,("prs_force_grow: Realloc failure for size %u.\n",
330                         (unsigned int)new_size));
331                 return False;
332         }
333
334         memset(&ps->data_p[ps->buffer_size], '\0', (size_t)(new_size - ps->buffer_size));
335
336         ps->buffer_size = new_size;
337
338         return True;
339 }
340
341 /*******************************************************************
342  Get the data pointer (external interface).
343 ********************************************************************/
344
345 char *prs_data_p(prs_struct *ps)
346 {
347         return ps->data_p;
348 }
349
350 /*******************************************************************
351  Get the current data size (external interface).
352  ********************************************************************/
353
354 uint32 prs_data_size(prs_struct *ps)
355 {
356         return ps->buffer_size;
357 }
358
359 /*******************************************************************
360  Fetch the current offset (external interface).
361  ********************************************************************/
362
363 uint32 prs_offset(prs_struct *ps)
364 {
365         return ps->data_offset;
366 }
367
368 /*******************************************************************
369  Set the current offset (external interface).
370  ********************************************************************/
371
372 bool prs_set_offset(prs_struct *ps, uint32 offset)
373 {
374         if ((offset > ps->data_offset)
375             && !prs_grow(ps, offset - ps->data_offset)) {
376                 return False;
377         }
378
379         ps->data_offset = offset;
380         return True;
381 }
382
383 /*******************************************************************
384  Append the data from one parse_struct into another.
385  ********************************************************************/
386
387 bool prs_append_prs_data(prs_struct *dst, prs_struct *src)
388 {
389         if (prs_offset(src) == 0)
390                 return True;
391
392         if(!prs_grow(dst, prs_offset(src)))
393                 return False;
394
395         memcpy(&dst->data_p[dst->data_offset], src->data_p, (size_t)prs_offset(src));
396         dst->data_offset += prs_offset(src);
397
398         return True;
399 }
400
401 /*******************************************************************
402  Append some data from one parse_struct into another.
403  ********************************************************************/
404
405 bool prs_append_some_data(prs_struct *dst, void *src_base, uint32_t start,
406                           uint32_t len)
407 {
408         if (len == 0) {
409                 return true;
410         }
411
412         if(!prs_grow(dst, len)) {
413                 return false;
414         }
415
416         memcpy(&dst->data_p[dst->data_offset], ((char *)src_base) + start, (size_t)len);
417         dst->data_offset += len;
418         return true;
419 }
420
421 bool prs_append_some_prs_data(prs_struct *dst, prs_struct *src, int32 start,
422                               uint32 len)
423 {
424         return prs_append_some_data(dst, src->data_p, start, len);
425 }
426
427 /*******************************************************************
428  Append the data from a buffer into a parse_struct.
429  ********************************************************************/
430
431 bool prs_copy_data_in(prs_struct *dst, const char *src, uint32 len)
432 {
433         if (len == 0)
434                 return True;
435
436         if(!prs_grow(dst, len))
437                 return False;
438
439         memcpy(&dst->data_p[dst->data_offset], src, (size_t)len);
440         dst->data_offset += len;
441
442         return True;
443 }
444
445 /*******************************************************************
446  Copy some data from a parse_struct into a buffer.
447  ********************************************************************/
448
449 bool prs_copy_data_out(char *dst, prs_struct *src, uint32 len)
450 {
451         if (len == 0)
452                 return True;
453
454         if(!prs_mem_get(src, len))
455                 return False;
456
457         memcpy(dst, &src->data_p[src->data_offset], (size_t)len);
458         src->data_offset += len;
459
460         return True;
461 }
462
463 /*******************************************************************
464  Copy all the data from a parse_struct into a buffer.
465  ********************************************************************/
466
467 bool prs_copy_all_data_out(char *dst, prs_struct *src)
468 {
469         uint32 len = prs_offset(src);
470
471         if (!len)
472                 return True;
473
474         prs_set_offset(src, 0);
475         return prs_copy_data_out(dst, src, len);
476 }
477
478 /*******************************************************************
479  Set the data as X-endian (external interface).
480  ********************************************************************/
481
482 void prs_set_endian_data(prs_struct *ps, bool endian)
483 {
484         ps->bigendian_data = endian;
485 }
486
487 /*******************************************************************
488  Align a the data_len to a multiple of align bytes - filling with
489  zeros.
490  ********************************************************************/
491
492 bool prs_align(prs_struct *ps)
493 {
494         uint32 mod = ps->data_offset & (ps->align-1);
495
496         if (ps->align != 0 && mod != 0) {
497                 uint32 extra_space = (ps->align - mod);
498                 if(!prs_grow(ps, extra_space))
499                         return False;
500                 memset(&ps->data_p[ps->data_offset], '\0', (size_t)extra_space);
501                 ps->data_offset += extra_space;
502         }
503
504         return True;
505 }
506
507 /******************************************************************
508  Align on a 2 byte boundary
509  *****************************************************************/
510  
511 bool prs_align_uint16(prs_struct *ps)
512 {
513         bool ret;
514         uint8 old_align = ps->align;
515
516         ps->align = 2;
517         ret = prs_align(ps);
518         ps->align = old_align;
519         
520         return ret;
521 }
522
523 /******************************************************************
524  Align on a 8 byte boundary
525  *****************************************************************/
526  
527 bool prs_align_uint64(prs_struct *ps)
528 {
529         bool ret;
530         uint8 old_align = ps->align;
531
532         ps->align = 8;
533         ret = prs_align(ps);
534         ps->align = old_align;
535         
536         return ret;
537 }
538
539 /******************************************************************
540  Align on a specific byte boundary
541  *****************************************************************/
542  
543 bool prs_align_custom(prs_struct *ps, uint8 boundary)
544 {
545         bool ret;
546         uint8 old_align = ps->align;
547
548         ps->align = boundary;
549         ret = prs_align(ps);
550         ps->align = old_align;
551         
552         return ret;
553 }
554
555
556
557 /*******************************************************************
558  Align only if required (for the unistr2 string mainly)
559  ********************************************************************/
560
561 bool prs_align_needed(prs_struct *ps, uint32 needed)
562 {
563         if (needed==0)
564                 return True;
565         else
566                 return prs_align(ps);
567 }
568
569 /*******************************************************************
570  Ensure we can read/write to a given offset.
571  ********************************************************************/
572
573 char *prs_mem_get(prs_struct *ps, uint32 extra_size)
574 {
575         if(UNMARSHALLING(ps)) {
576                 /*
577                  * If reading, ensure that we can read the requested size item.
578                  */
579                 if (ps->data_offset + extra_size > ps->buffer_size) {
580                         DEBUG(0,("prs_mem_get: reading data of size %u would overrun "
581                                 "buffer by %u bytes.\n",
582                                 (unsigned int)extra_size,
583                                 (unsigned int)(ps->data_offset + extra_size - ps->buffer_size) ));
584                         return NULL;
585                 }
586         } else {
587                 /*
588                  * Writing - grow the buffer if needed.
589                  */
590                 if(!prs_grow(ps, extra_size))
591                         return NULL;
592         }
593         return &ps->data_p[ps->data_offset];
594 }
595
596 /*******************************************************************
597  Change the struct type.
598  ********************************************************************/
599
600 void prs_switch_type(prs_struct *ps, bool io)
601 {
602         if ((ps->io ^ io) == True)
603                 ps->io=io;
604 }
605
606 /*******************************************************************
607  Stream a uint8.
608  ********************************************************************/
609
610 bool prs_uint8(const char *name, prs_struct *ps, int depth, uint8 *data8)
611 {
612         char *q = prs_mem_get(ps, 1);
613         if (q == NULL)
614                 return False;
615
616         if (UNMARSHALLING(ps))
617                 *data8 = CVAL(q,0);
618         else
619                 SCVAL(q,0,*data8);
620
621         DEBUGADD(5,("%s%04x %s: %02x\n", tab_depth(5,depth), ps->data_offset, name, *data8));
622
623         ps->data_offset += 1;
624
625         return True;
626 }
627
628 /*******************************************************************
629  Stream a uint16.
630  ********************************************************************/
631
632 bool prs_uint16(const char *name, prs_struct *ps, int depth, uint16 *data16)
633 {
634         char *q = prs_mem_get(ps, sizeof(uint16));
635         if (q == NULL)
636                 return False;
637
638         if (UNMARSHALLING(ps)) {
639                 if (ps->bigendian_data)
640                         *data16 = RSVAL(q,0);
641                 else
642                         *data16 = SVAL(q,0);
643         } else {
644                 if (ps->bigendian_data)
645                         RSSVAL(q,0,*data16);
646                 else
647                         SSVAL(q,0,*data16);
648         }
649
650         DEBUGADD(5,("%s%04x %s: %04x\n", tab_depth(5,depth), ps->data_offset, name, *data16));
651
652         ps->data_offset += sizeof(uint16);
653
654         return True;
655 }
656
657 /*******************************************************************
658  Stream a uint32.
659  ********************************************************************/
660
661 bool prs_uint32(const char *name, prs_struct *ps, int depth, uint32 *data32)
662 {
663         char *q = prs_mem_get(ps, sizeof(uint32));
664         if (q == NULL)
665                 return False;
666
667         if (UNMARSHALLING(ps)) {
668                 if (ps->bigendian_data)
669                         *data32 = RIVAL(q,0);
670                 else
671                         *data32 = IVAL(q,0);
672         } else {
673                 if (ps->bigendian_data)
674                         RSIVAL(q,0,*data32);
675                 else
676                         SIVAL(q,0,*data32);
677         }
678
679         DEBUGADD(5,("%s%04x %s: %08x\n", tab_depth(5,depth), ps->data_offset, name, *data32));
680
681         ps->data_offset += sizeof(uint32);
682
683         return True;
684 }
685
686 /*******************************************************************
687  Stream an int32.
688  ********************************************************************/
689
690 bool prs_int32(const char *name, prs_struct *ps, int depth, int32 *data32)
691 {
692         char *q = prs_mem_get(ps, sizeof(int32));
693         if (q == NULL)
694                 return False;
695
696         if (UNMARSHALLING(ps)) {
697                 if (ps->bigendian_data)
698                         *data32 = RIVALS(q,0);
699                 else
700                         *data32 = IVALS(q,0);
701         } else {
702                 if (ps->bigendian_data)
703                         RSIVALS(q,0,*data32);
704                 else
705                         SIVALS(q,0,*data32);
706         }
707
708         DEBUGADD(5,("%s%04x %s: %08x\n", tab_depth(5,depth), ps->data_offset, name, *data32));
709
710         ps->data_offset += sizeof(int32);
711
712         return True;
713 }
714
715 /*******************************************************************
716  Stream a uint64_struct
717  ********************************************************************/
718 bool prs_uint64(const char *name, prs_struct *ps, int depth, uint64 *data64)
719 {
720         if (UNMARSHALLING(ps)) {
721                 uint32 high, low;
722
723                 if (!prs_uint32(name, ps, depth+1, &low))
724                         return False;
725
726                 if (!prs_uint32(name, ps, depth+1, &high))
727                         return False;
728
729                 *data64 = ((uint64_t)high << 32) + low;
730
731                 return True;
732         } else {
733                 uint32 high = (*data64) >> 32, low = (*data64) & 0xFFFFFFFF;
734                 return prs_uint32(name, ps, depth+1, &low) &&
735                            prs_uint32(name, ps, depth+1, &high);
736         }
737 }
738
739 /*******************************************************************
740  Stream a DCE error code
741  ********************************************************************/
742
743 bool prs_dcerpc_status(const char *name, prs_struct *ps, int depth, NTSTATUS *status)
744 {
745         char *q = prs_mem_get(ps, sizeof(uint32));
746         if (q == NULL)
747                 return False;
748
749         if (UNMARSHALLING(ps)) {
750                 if (ps->bigendian_data)
751                         *status = NT_STATUS(RIVAL(q,0));
752                 else
753                         *status = NT_STATUS(IVAL(q,0));
754         } else {
755                 if (ps->bigendian_data)
756                         RSIVAL(q,0,NT_STATUS_V(*status));
757                 else
758                         SIVAL(q,0,NT_STATUS_V(*status));
759         }
760
761         DEBUGADD(5,("%s%04x %s: %s\n", tab_depth(5,depth), ps->data_offset, name,
762                  dcerpc_errstr(talloc_tos(), NT_STATUS_V(*status))));
763
764         ps->data_offset += sizeof(uint32);
765
766         return True;
767 }
768
769 /******************************************************************
770  Stream an array of uint8s. Length is number of uint8s.
771  ********************************************************************/
772
773 bool prs_uint8s(bool charmode, const char *name, prs_struct *ps, int depth, uint8 *data8s, int len)
774 {
775         int i;
776         char *q = prs_mem_get(ps, len);
777         if (q == NULL)
778                 return False;
779
780         if (UNMARSHALLING(ps)) {
781                 for (i = 0; i < len; i++)
782                         data8s[i] = CVAL(q,i);
783         } else {
784                 for (i = 0; i < len; i++)
785                         SCVAL(q, i, data8s[i]);
786         }
787
788         DEBUGADD(5,("%s%04x %s: ", tab_depth(5,depth), ps->data_offset ,name));
789         if (charmode)
790                 print_asc(5, (unsigned char*)data8s, len);
791         else {
792                 for (i = 0; i < len; i++)
793                         DEBUGADD(5,("%02x ", data8s[i]));
794         }
795         DEBUGADD(5,("\n"));
796
797         ps->data_offset += len;
798
799         return True;
800 }
801
802 /******************************************************************
803  Stream an array of uint16s. Length is number of uint16s.
804  ********************************************************************/
805
806 bool prs_uint16s(bool charmode, const char *name, prs_struct *ps, int depth, uint16 *data16s, int len)
807 {
808         int i;
809         char *q = prs_mem_get(ps, len * sizeof(uint16));
810         if (q == NULL)
811                 return False;
812
813         if (UNMARSHALLING(ps)) {
814                 if (ps->bigendian_data) {
815                         for (i = 0; i < len; i++)
816                                 data16s[i] = RSVAL(q, 2*i);
817                 } else {
818                         for (i = 0; i < len; i++)
819                                 data16s[i] = SVAL(q, 2*i);
820                 }
821         } else {
822                 if (ps->bigendian_data) {
823                         for (i = 0; i < len; i++)
824                                 RSSVAL(q, 2*i, data16s[i]);
825                 } else {
826                         for (i = 0; i < len; i++)
827                                 SSVAL(q, 2*i, data16s[i]);
828                 }
829         }
830
831         DEBUGADD(5,("%s%04x %s: ", tab_depth(5,depth), ps->data_offset, name));
832         if (charmode)
833                 print_asc(5, (unsigned char*)data16s, 2*len);
834         else {
835                 for (i = 0; i < len; i++)
836                         DEBUGADD(5,("%04x ", data16s[i]));
837         }
838         DEBUGADD(5,("\n"));
839
840         ps->data_offset += (len * sizeof(uint16));
841
842         return True;
843 }
844
845 /******************************************************************
846  Stream an array of uint32s. Length is number of uint32s.
847  ********************************************************************/
848
849 bool prs_uint32s(bool charmode, const char *name, prs_struct *ps, int depth, uint32 *data32s, int len)
850 {
851         int i;
852         char *q = prs_mem_get(ps, len * sizeof(uint32));
853         if (q == NULL)
854                 return False;
855
856         if (UNMARSHALLING(ps)) {
857                 if (ps->bigendian_data) {
858                         for (i = 0; i < len; i++)
859                                 data32s[i] = RIVAL(q, 4*i);
860                 } else {
861                         for (i = 0; i < len; i++)
862                                 data32s[i] = IVAL(q, 4*i);
863                 }
864         } else {
865                 if (ps->bigendian_data) {
866                         for (i = 0; i < len; i++)
867                                 RSIVAL(q, 4*i, data32s[i]);
868                 } else {
869                         for (i = 0; i < len; i++)
870                                 SIVAL(q, 4*i, data32s[i]);
871                 }
872         }
873
874         DEBUGADD(5,("%s%04x %s: ", tab_depth(5,depth), ps->data_offset, name));
875         if (charmode)
876                 print_asc(5, (unsigned char*)data32s, 4*len);
877         else {
878                 for (i = 0; i < len; i++)
879                         DEBUGADD(5,("%08x ", data32s[i]));
880         }
881         DEBUGADD(5,("\n"));
882
883         ps->data_offset += (len * sizeof(uint32));
884
885         return True;
886 }
887
888 /*******************************************************************
889 creates a new prs_struct containing a DATA_BLOB
890 ********************************************************************/
891 bool prs_init_data_blob(prs_struct *prs, DATA_BLOB *blob, TALLOC_CTX *mem_ctx)
892 {
893         if (!prs_init( prs, RPC_MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL ))
894                 return False;
895
896
897         if (!prs_copy_data_in(prs, (char *)blob->data, blob->length))
898                 return False;
899
900         return True;
901 }
902
903 /*******************************************************************
904 return the contents of a prs_struct in a DATA_BLOB
905 ********************************************************************/
906 bool prs_data_blob(prs_struct *prs, DATA_BLOB *blob, TALLOC_CTX *mem_ctx)
907 {
908         blob->length = prs_data_size(prs);
909         blob->data = (uint8 *)TALLOC_ZERO_SIZE(mem_ctx, blob->length);
910         
911         /* set the pointer at the end of the buffer */
912         prs_set_offset( prs, prs_data_size(prs) );
913
914         if (!prs_copy_all_data_out((char *)blob->data, prs))
915                 return False;
916         
917         return True;
918 }