2 Unix SMB/Netbios implementation.
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.
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.
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.
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.
26 /*******************************************************************
28 ********************************************************************/
29 void prs_dump(char *name, int v, prs_struct *ps)
33 if (DEBUGLEVEL < 50) return;
36 slprintf(fname,sizeof(fname)-1, "/tmp/%s_%d.%d.prs", name, v, i);
38 slprintf(fname,sizeof(fname)-1, "/tmp/%s.%d.prs", name, i);
40 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0644);
41 if (fd != -1 || errno != EEXIST) break;
44 write(fd, ps->data_p + ps->data_offset, ps->buffer_size - ps->data_offset);
46 DEBUG(0,("created %s\n", fname));
52 /*******************************************************************
53 debug output for parsing info.
55 XXXX side-effect of this function is to increase the debug depth XXXX
57 ********************************************************************/
58 void prs_debug(prs_struct *ps, int depth, char *desc, char *fn_name)
60 DEBUG(5+depth, ("%s%06x %s %s\n", tab_depth(depth), ps->data_offset, fn_name, desc));
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)
71 ps->bigendian_data = RPC_LITTLE_ENDIAN;
72 ps->align = RPC_PARSE_ALIGN;
73 ps->is_dynamic = False;
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));
85 ps->is_dynamic = True; /* We own this memory. */
91 /*******************************************************************
92 read from a socket into memory.
93 ********************************************************************/
94 BOOL prs_read(prs_struct *ps, int fd, size_t len, int timeout)
97 size_t prev_size = ps->buffer_size;
98 if (!prs_grow(ps, len))
102 ok = (read_with_timeout(fd, &ps->data_p[prev_size],
103 len, len,timeout) == len);
105 ok = (read_data(fd, &ps->data_p[prev_size], len) == len);
110 /*******************************************************************
111 Delete the memory in a parse structure - if we own it.
112 ********************************************************************/
114 void prs_mem_free(prs_struct *ps)
117 SAFE_FREE(ps->data_p);
118 ps->is_dynamic = False;
123 /*******************************************************************
124 Allocate memory when unmarshalling... Always zero clears.
125 ********************************************************************/
127 char *prs_alloc_mem(prs_struct *ps, size_t size)
129 char *ret = talloc(ps->mem_ctx, size);
132 memset(ret, '\0', size);
137 /*******************************************************************
138 Return the current talloc context we're using.
139 ********************************************************************/
141 TALLOC_CTX *prs_get_mem_context(prs_struct *ps)
146 /*******************************************************************
147 Hand some already allocated memory to a prs_struct.
148 ********************************************************************/
150 void prs_give_memory(prs_struct *ps, char *buf, uint32 size, BOOL is_dynamic)
152 ps->is_dynamic = is_dynamic;
154 ps->buffer_size = size;
157 /*******************************************************************
158 Take some memory back from a prs_struct.
159 ********************************************************************/
161 char *prs_take_memory(prs_struct *ps, uint32 *psize)
163 char *ret = ps->data_p;
165 *psize = ps->buffer_size;
166 ps->is_dynamic = False;
171 /*******************************************************************
172 Set a prs_struct to exactly a given size. Will grow or tuncate if neccessary.
173 ********************************************************************/
175 BOOL prs_set_buffer_size(prs_struct *ps, uint32 newsize)
177 if (newsize > ps->buffer_size)
178 return prs_force_grow(ps, newsize - ps->buffer_size);
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)));
189 ps->data_p = new_data_p;
190 ps->buffer_size = newsize;
196 /*******************************************************************
197 Attempt, if needed, to grow a data buffer.
198 Also depends on the data stream mode (io).
199 ********************************************************************/
201 BOOL prs_grow(prs_struct *ps, uint32 extra_space)
206 ps->grow_size = MAX(ps->grow_size, ps->data_offset + extra_space);
208 if(ps->data_offset + extra_space <= ps->buffer_size)
212 * We cannot grow the buffer if we're not reading
213 * into the prs_struct, or if we don't own the memory.
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));
223 * Decide how much extra space we really need.
226 extra_space -= (ps->buffer_size - ps->data_offset);
227 if(ps->buffer_size == 0) {
229 * Ensure we have at least a PDU's length, or extra_space, whichever
233 new_size = MAX(MAX_PDU_FRAG_LEN,extra_space);
235 if((new_data = malloc(new_size)) == NULL) {
236 DEBUG(0,("prs_grow: Malloc failure for size %u.\n", (unsigned int)new_size));
239 memset(new_data, '\0', new_size );
242 * If the current buffer size is bigger than the space needed, just
243 * double it, else add extra_space.
245 new_size = MAX(ps->buffer_size*2, ps->buffer_size + extra_space);
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));
253 memset(&new_data[ps->buffer_size], '\0', new_size - ps->buffer_size);
255 ps->buffer_size = new_size;
256 ps->data_p = new_data;
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 ********************************************************************/
267 BOOL prs_force_grow(prs_struct *ps, uint32 extra_space)
269 uint32 new_size = ps->buffer_size + extra_space;
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));
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));
284 memset(&new_data[ps->buffer_size], '\0', new_size - ps->buffer_size);
286 ps->buffer_size = new_size;
287 ps->data_p = new_data;
292 /*******************************************************************
293 Get the data pointer (external interface).
294 ********************************************************************/
296 char *prs_data_p(prs_struct *ps)
301 /*******************************************************************
302 Get the current data size (external interface).
303 ********************************************************************/
305 uint32 prs_data_size(prs_struct *ps)
307 return ps->buffer_size;
310 /*******************************************************************
311 Fetch the current offset (external interface).
312 ********************************************************************/
314 uint32 prs_offset(prs_struct *ps)
316 return ps->data_offset;
319 /*******************************************************************
320 Set the current offset (external interface).
321 ********************************************************************/
323 BOOL prs_set_offset(prs_struct *ps, uint32 offset)
325 if(offset <= ps->data_offset) {
326 ps->data_offset = offset;
330 if(!prs_grow(ps, offset - ps->data_offset))
333 ps->data_offset = offset;
337 /*******************************************************************
338 Append the data from one parse_struct into another.
339 ********************************************************************/
341 BOOL prs_append_prs_data(prs_struct *dst, prs_struct *src)
343 if(!prs_grow(dst, prs_offset(src)))
346 memcpy(&dst->data_p[dst->data_offset], prs_data_p(src), (size_t)prs_offset(src));
347 dst->data_offset += prs_offset(src);
352 /*******************************************************************
353 Append some data from one parse_struct into another.
354 ********************************************************************/
356 BOOL prs_append_some_prs_data(prs_struct *dst, prs_struct *src, int32 start, uint32 len)
361 if(!prs_grow(dst, len))
364 memcpy(&dst->data_p[dst->data_offset], prs_data_p(src)+start, (size_t)len);
365 dst->data_offset += len;
370 /*******************************************************************
371 Append the data from a buffer into a parse_struct.
372 ********************************************************************/
374 BOOL prs_append_data(prs_struct *dst, char *src, uint32 len)
376 if(!prs_grow(dst, len))
379 memcpy(&dst->data_p[dst->data_offset], src, (size_t)len);
380 dst->data_offset += len;
385 /*******************************************************************
386 Set the data as X-endian (external interface).
387 ********************************************************************/
389 void prs_set_endian_data(prs_struct *ps, BOOL endian)
391 ps->bigendian_data = endian;
394 /*******************************************************************
395 Align a the data_len to a multiple of align bytes - filling with
397 ********************************************************************/
399 BOOL prs_align(prs_struct *ps)
401 uint32 mod = ps->data_offset & (ps->align-1);
403 if (ps->align != 0 && mod != 0) {
404 uint32 extra_space = (ps->align - mod);
405 if(!prs_grow(ps, extra_space))
407 memset(&ps->data_p[ps->data_offset], '\0', (size_t)extra_space);
408 ps->data_offset += extra_space;
414 /*******************************************************************
415 Align only if required (for the unistr2 string mainly)
416 ********************************************************************/
418 BOOL prs_align_needed(prs_struct *ps, uint32 needed)
423 return prs_align(ps);
426 /*******************************************************************
427 Ensure we can read/write to a given offset.
428 ********************************************************************/
430 char *prs_mem_get(prs_struct *ps, uint32 extra_size)
432 if(UNMARSHALLING(ps)) {
434 * If reading, ensure that we can read the requested size item.
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 ));
443 * Writing - grow the buffer if needed.
445 if(!prs_grow(ps, extra_size))
448 return &ps->data_p[ps->data_offset];
451 /*******************************************************************
452 Change the struct type.
453 ********************************************************************/
455 void prs_switch_type(prs_struct *ps, BOOL io)
457 if ((ps->io ^ io) == True)
461 /*******************************************************************
462 Force a prs_struct to be dynamic even when it's size is 0.
463 ********************************************************************/
465 void prs_force_dynamic(prs_struct *ps)
470 /*******************************************************************
472 ********************************************************************/
474 BOOL prs_uint8(char *name, prs_struct *ps, int depth, uint8 *data8)
476 char *q = prs_mem_get(ps, 1);
480 if (UNMARSHALLING(ps))
485 DEBUG(5,("%s%04x %s: %02x\n", tab_depth(depth), ps->data_offset, name, *data8));
487 ps->data_offset += 1;
492 /*******************************************************************
494 ********************************************************************/
496 BOOL prs_uint16(char *name, prs_struct *ps, int depth, uint16 *data16)
498 char *q = prs_mem_get(ps, sizeof(uint16));
502 if (UNMARSHALLING(ps)) {
503 if (ps->bigendian_data)
504 *data16 = RSVAL(q,0);
508 if (ps->bigendian_data)
514 DEBUG(5,("%s%04x %s: %04x\n", tab_depth(depth), ps->data_offset, name, *data16));
516 ps->data_offset += sizeof(uint16);
521 /*******************************************************************
523 ********************************************************************/
525 BOOL prs_uint32(char *name, prs_struct *ps, int depth, uint32 *data32)
527 char *q = prs_mem_get(ps, sizeof(uint32));
531 if (UNMARSHALLING(ps)) {
532 if (ps->bigendian_data)
533 *data32 = RIVAL(q,0);
537 if (ps->bigendian_data)
543 DEBUG(5,("%s%04x %s: %08x\n", tab_depth(depth), ps->data_offset, name, *data32));
545 ps->data_offset += sizeof(uint32);
550 /*******************************************************************
552 ********************************************************************/
554 BOOL prs_ntstatus(char *name, prs_struct *ps, int depth, NTSTATUS *status)
556 char *q = prs_mem_get(ps, sizeof(uint32));
560 if (UNMARSHALLING(ps)) {
561 if (ps->bigendian_data)
562 *status = NT_STATUS(RIVAL(q,0));
564 *status = NT_STATUS(IVAL(q,0));
566 if (ps->bigendian_data)
567 RSIVAL(q,0,NT_STATUS_V(*status));
569 SIVAL(q,0,NT_STATUS_V(*status));
572 DEBUG(5,("%s%04x %s: %s\n", tab_depth(depth), ps->data_offset, name,
573 get_nt_error_msg(*status)));
575 ps->data_offset += sizeof(uint32);
580 /*******************************************************************
582 ********************************************************************/
584 BOOL prs_werror(char *name, prs_struct *ps, int depth, WERROR *status)
586 char *q = prs_mem_get(ps, sizeof(uint32));
590 if (UNMARSHALLING(ps)) {
591 if (ps->bigendian_data)
592 *status = W_ERROR(RIVAL(q,0));
594 *status = W_ERROR(IVAL(q,0));
596 if (ps->bigendian_data)
597 RSIVAL(q,0,W_ERROR_V(*status));
599 SIVAL(q,0,W_ERROR_V(*status));
602 DEBUG(5,("%s%04x %s: %s\n", tab_depth(depth), ps->data_offset, name,
603 werror_str(*status)));
605 ps->data_offset += sizeof(uint32);
611 /******************************************************************
612 Stream an array of uint8s. Length is number of uint8s.
613 ********************************************************************/
615 BOOL prs_uint8s(BOOL charmode, char *name, prs_struct *ps, int depth, uint8 *data8s, int len)
618 char *q = prs_mem_get(ps, len);
622 if (UNMARSHALLING(ps)) {
623 for (i = 0; i < len; i++)
624 data8s[i] = CVAL(q,i);
626 for (i = 0; i < len; i++)
627 SCVAL(q, i, data8s[i]);
630 DEBUG(5,("%s%04x %s: ", tab_depth(depth), ps->data_offset ,name));
632 print_asc(5, (unsigned char*)data8s, len);
634 for (i = 0; i < len; i++)
635 DEBUG(5,("%02x ", data8s[i]));
639 ps->data_offset += len;
644 /******************************************************************
645 Stream an array of uint16s. Length is number of uint16s.
646 ********************************************************************/
648 BOOL prs_uint16s(BOOL charmode, char *name, prs_struct *ps, int depth, uint16 *data16s, int len)
651 char *q = prs_mem_get(ps, len * sizeof(uint16));
655 if (UNMARSHALLING(ps)) {
656 if (ps->bigendian_data) {
657 for (i = 0; i < len; i++)
658 data16s[i] = RSVAL(q, 2*i);
660 for (i = 0; i < len; i++)
661 data16s[i] = SVAL(q, 2*i);
664 if (ps->bigendian_data) {
665 for (i = 0; i < len; i++)
666 RSSVAL(q, 2*i, data16s[i]);
668 for (i = 0; i < len; i++)
669 SSVAL(q, 2*i, data16s[i]);
673 DEBUG(5,("%s%04x %s: ", tab_depth(depth), ps->data_offset, name));
675 print_asc(5, (unsigned char*)data16s, 2*len);
677 for (i = 0; i < len; i++)
678 DEBUG(5,("%04x ", data16s[i]));
682 ps->data_offset += (len * sizeof(uint16));
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 ********************************************************************/
692 static void dbg_rw_punival(BOOL charmode, char *name, int depth, prs_struct *ps,
693 char *in_buf, char *out_buf, int len)
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));
702 for (i = 0; i < len; i++)
703 SSVAL(out_buf, 2*i, SVAL(in_buf, 2*i));
706 if (ps->bigendian_data) {
707 for (i = 0; i < len; i++)
708 RSSVAL(in_buf, 2*i, SVAL(out_buf,2*i));
710 for (i = 0; i < len; i++)
711 SSVAL(in_buf, 2*i, SVAL(out_buf,2*i));
715 DEBUG(5,("%s%04x %s: ", tab_depth(depth), ps->data_offset, name));
717 print_asc(5, (unsigned char*)out_buf, 2*len);
719 for (i = 0; i < len; i++)
720 DEBUG(5,("%04x ", out_buf[i]));
725 /******************************************************************
726 Stream a unistr. Always little endian.
727 ********************************************************************/
729 BOOL prs_uint16uni(BOOL charmode, char *name, prs_struct *ps, int depth, uint16 *data16s, int len)
731 char *q = prs_mem_get(ps, len * sizeof(uint16));
735 dbg_rw_punival(charmode, name, depth, ps, q, (char *)data16s, len);
736 ps->data_offset += (len * sizeof(uint16));
741 /******************************************************************
742 Stream an array of uint32s. Length is number of uint32s.
743 ********************************************************************/
745 BOOL prs_uint32s(BOOL charmode, char *name, prs_struct *ps, int depth, uint32 *data32s, int len)
748 char *q = prs_mem_get(ps, len * sizeof(uint32));
752 if (UNMARSHALLING(ps)) {
753 if (ps->bigendian_data) {
754 for (i = 0; i < len; i++)
755 data32s[i] = RIVAL(q, 4*i);
757 for (i = 0; i < len; i++)
758 data32s[i] = IVAL(q, 4*i);
761 if (ps->bigendian_data) {
762 for (i = 0; i < len; i++)
763 RSIVAL(q, 4*i, data32s[i]);
765 for (i = 0; i < len; i++)
766 SIVAL(q, 4*i, data32s[i]);
770 DEBUG(5,("%s%04x %s: ", tab_depth(depth), ps->data_offset, name));
772 print_asc(5, (unsigned char*)data32s, 4*len);
774 for (i = 0; i < len; i++)
775 DEBUG(5,("%08x ", data32s[i]));
779 ps->data_offset += (len * sizeof(uint32));
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 ********************************************************************/
789 BOOL prs_buffer5(BOOL charmode, char *name, prs_struct *ps, int depth, BUFFER5 *str)
792 char *q = prs_mem_get(ps, str->buf_len * sizeof(uint16));
796 if (UNMARSHALLING(ps)) {
797 str->buffer = (uint16 *)prs_alloc_mem(ps,str->buf_len * sizeof(uint16));
798 if (str->buffer == NULL)
802 /* If the string is empty, we don't have anything to stream */
806 p = (char *)str->buffer;
808 dbg_rw_punival(charmode, name, depth, ps, q, p, str->buf_len);
810 ps->data_offset += (str->buf_len * sizeof(uint16));
815 /******************************************************************
816 Stream a "not" unicode string, length/buffer specified separately,
817 in byte chars. String is in little-endian format.
818 ********************************************************************/
820 BOOL prs_buffer2(BOOL charmode, char *name, prs_struct *ps, int depth, BUFFER2 *str)
823 char *q = prs_mem_get(ps, str->buf_len);
827 if (UNMARSHALLING(ps)) {
828 str->buffer = (uint16 *)prs_alloc_mem(ps,str->buf_len);
829 if (str->buffer == NULL)
833 p = (char *)str->buffer;
835 dbg_rw_punival(charmode, name, depth, ps, q, p, str->buf_len/2);
836 ps->data_offset += str->buf_len;
841 /******************************************************************
842 Stream a string, length/buffer specified separately,
844 ********************************************************************/
846 BOOL prs_string2(BOOL charmode, char *name, prs_struct *ps, int depth, STRING2 *str)
849 char *q = prs_mem_get(ps, str->str_str_len);
853 if (UNMARSHALLING(ps)) {
854 str->buffer = (unsigned char *)prs_alloc_mem(ps,str->str_str_len);
855 if (str->buffer == NULL)
859 if (UNMARSHALLING(ps)) {
860 for (i = 0; i < str->str_str_len; i++)
861 str->buffer[i] = CVAL(q,i);
863 for (i = 0; i < str->str_str_len; i++)
864 SCVAL(q, i, str->buffer[i]);
867 DEBUG(5,("%s%04x %s: ", tab_depth(depth), ps->data_offset, name));
869 print_asc(5, (unsigned char*)str->buffer, str->str_str_len);
871 for (i = 0; i < str->str_str_len; i++)
872 DEBUG(5,("%02x ", str->buffer[i]));
876 ps->data_offset += str->str_str_len;
881 /******************************************************************
882 Stream a unicode string, length/buffer specified separately,
883 in uint16 chars. The unicode string is already in little-endian format.
884 ********************************************************************/
886 BOOL prs_unistr2(BOOL charmode, char *name, prs_struct *ps, int depth, UNISTR2 *str)
889 char *q = prs_mem_get(ps, str->uni_str_len * sizeof(uint16));
893 /* If the string is empty, we don't have anything to stream */
894 if (str->uni_str_len==0)
897 if (UNMARSHALLING(ps)) {
898 str->buffer = (uint16 *)prs_alloc_mem(ps,str->uni_max_len * sizeof(uint16));
899 if (str->buffer == NULL)
903 p = (char *)str->buffer;
905 dbg_rw_punival(charmode, name, depth, ps, q, p, str->uni_str_len);
907 ps->data_offset += (str->uni_str_len * sizeof(uint16));
912 /******************************************************************
913 Stream a unicode string, length/buffer specified separately,
914 in uint16 chars. The unicode string is already in little-endian format.
915 ********************************************************************/
917 BOOL prs_unistr3(BOOL charmode, char *name, UNISTR3 *str, prs_struct *ps, int depth)
920 char *q = prs_mem_get(ps, str->uni_str_len * sizeof(uint16));
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)
930 p = (char *)str->str.buffer;
932 dbg_rw_punival(charmode, name, depth, ps, q, p, str->uni_str_len);
933 ps->data_offset += (str->uni_str_len * sizeof(uint16));
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 ********************************************************************/
943 BOOL prs_unistr(char *name, prs_struct *ps, int depth, UNISTR *str)
946 unsigned char *p = (unsigned char *)str->buffer;
952 if (MARSHALLING(ps)) {
954 for(len = 0; str->buffer[len] != 0; len++)
957 q = prs_mem_get(ps, (len+1)*2);
963 for(len = 0; str->buffer[len] != 0; len++)
965 if(ps->bigendian_data)
967 /* swap bytes - p is little endian, q is big endian. */
983 * even if the string is 'empty' (only an \0 char)
984 * at this point the leading \0 hasn't been parsed.
994 dump_data(5+depth, (char *)start, len * 2);
996 else { /* unmarshalling */
998 uint32 alloc_len = 0;
999 q = prs_data_p(ps) + prs_offset(ps);
1002 * Work out how much space we need and talloc it.
1004 max_len = (ps->buffer_size - ps->data_offset)/sizeof(uint16);
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++)
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))
1017 p = (unsigned char *)str->buffer;
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))
1025 if(ps->bigendian_data)
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];
1034 p[0] = (unsigned char)q[0];
1035 p[1] = (unsigned char)q[1];
1042 if (len < alloc_len)
1044 /* NULL terminate the UNISTR */
1045 str->buffer[len++] = '\0';
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
1052 ps->data_offset += (len)*2;
1058 /*******************************************************************
1059 Stream a null-terminated string. len is strlen, and therefore does
1060 not include the null-termination character.
1061 ********************************************************************/
1063 BOOL prs_string(char *name, prs_struct *ps, int depth, char *str, int len, int max_buf_size)
1068 len = MIN(len, (max_buf_size-1));
1070 q = prs_mem_get(ps, len+1);
1074 for(i = 0; i < len; i++) {
1075 if (UNMARSHALLING(ps))
1081 /* The terminating null. */
1084 if (MARSHALLING(ps)) {
1088 ps->data_offset += len+1;
1090 dump_data(5+depth, q, len);
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 ********************************************************************/
1100 BOOL prs_uint16_pre(char *name, prs_struct *ps, int depth, uint16 *data16, uint32 *offset)
1102 *offset = ps->data_offset;
1103 if (UNMARSHALLING(ps)) {
1105 return prs_uint16(name, ps, depth, data16);
1107 char *q = prs_mem_get(ps, sizeof(uint16));
1110 ps->data_offset += sizeof(uint16);
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 ********************************************************************/
1120 BOOL prs_uint16_post(char *name, prs_struct *ps, int depth, uint16 *data16,
1121 uint32 ptr_uint16, uint32 start_offset)
1123 if (MARSHALLING(ps)) {
1125 * Writing - temporarily move the offset pointer.
1127 uint16 data_size = ps->data_offset - start_offset;
1128 uint32 old_offset = ps->data_offset;
1130 ps->data_offset = ptr_uint16;
1131 if(!prs_uint16(name, ps, depth, &data_size)) {
1132 ps->data_offset = old_offset;
1135 ps->data_offset = old_offset;
1137 ps->data_offset = start_offset + (uint32)(*data16);
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 ********************************************************************/
1147 BOOL prs_uint32_pre(char *name, prs_struct *ps, int depth, uint32 *data32, uint32 *offset)
1149 *offset = ps->data_offset;
1150 if (UNMARSHALLING(ps) && (data32 != NULL)) {
1152 return prs_uint32(name, ps, depth, data32);
1154 ps->data_offset += sizeof(uint32);
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 ********************************************************************/
1164 BOOL prs_uint32_post(char *name, prs_struct *ps, int depth, uint32 *data32,
1165 uint32 ptr_uint32, uint32 data_size)
1167 if (MARSHALLING(ps)) {
1169 * Writing - temporarily move the offset pointer.
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;
1177 ps->data_offset = old_offset;
1182 /* useful function to store a structure in rpc wire format */
1183 int tdb_prs_store(TDB_CONTEXT *tdb, char *keystr, prs_struct *ps)
1185 TDB_DATA kbuf, dbuf;
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);
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)
1196 TDB_DATA kbuf, dbuf;
1198 kbuf.dsize = strlen(keystr)+1;
1200 dbuf = tdb_fetch(tdb, kbuf);
1201 if (!dbuf.dptr) return -1;
1204 prs_init(ps, 0, mem_ctx, UNMARSHALL);
1205 prs_give_memory(ps, dbuf.dptr, dbuf.dsize, True);
1210 /*******************************************************************
1212 ********************************************************************/
1213 BOOL prs_hash1(prs_struct *ps, uint32 offset, uint8 sess_key[16])
1220 #ifdef DEBUG_PASSWORD
1221 DEBUG(100, ("prs_hash1\n"));
1222 dump_data(100, sess_key, 16);
1223 dump_data(100, q, 68);
1225 SamOEMhash((uchar *) q, sess_key, 68);
1227 #ifdef DEBUG_PASSWORD
1228 dump_data(100, q, 68);