1/*
2   Unix SMB/Netbios implementation.
3   Version 1.9.
4   Samba memory buffer functions
5   Copyright (C) Andrew Tridgell              1992-1997
6   Copyright (C) Luke Kenneth Casson Leighton 1996-1997
7   Copyright (C) Jeremy Allison 1999.
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software
21   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22*/
23
24extern int DEBUGLEVEL;
25
26#include "includes.h"
27
28
29/*******************************************************************
30 debug output for parsing info.
31
32 XXXX side-effect of this function is to increase the debug depth XXXX
33
34 ********************************************************************/
35void prs_debug(prs_struct *ps, int depth, char *desc, char *fn_name)
36{
37	DEBUG(5+depth, ("%s%06x %s %s\n", tab_depth(depth), ps->data_offset, fn_name, desc));
38}
39
40/*******************************************************************
41 Initialise a parse structure - malloc the data if requested.
42 ********************************************************************/
43
44BOOL prs_init(prs_struct *ps, uint32 size, uint8 align, BOOL io)
45{
46	ZERO_STRUCTP(ps);
47	ps->io = io;
48	ps->bigendian_data = False;
49	ps->align = align;
50	ps->is_dynamic = False;
51	ps->data_offset = 0;
52	ps->buffer_size = 0;
53	ps->data_p = NULL;
54
55	if (size != 0) {
56		ps->buffer_size = size;
57		if((ps->data_p = (char *)malloc((size_t)size)) == NULL) {
58			DEBUG(0,("prs_init: malloc fail for %u bytes.\n", (unsigned int)size));
59			return False;
60		}
61		ps->is_dynamic = True; /* We own this memory. */
62	}
63
64	return True;
65}
66
67/*******************************************************************
68 Delete the memory in a parse structure - if we own it.
69 ********************************************************************/
70
71void prs_mem_free(prs_struct *ps)
72{
73	if(ps->is_dynamic && (ps->data_p != NULL))
74		free(ps->data_p);
75	ps->is_dynamic = False;
76	ps->data_p = NULL;
77	ps->buffer_size = 0;
78	ps->data_offset = 0;
79}
80
81/*******************************************************************
82 Hand some already allocated memory to a prs_struct.
83 ********************************************************************/
84
85void prs_give_memory(prs_struct *ps, char *buf, uint32 size, BOOL is_dynamic)
86{
87	ps->is_dynamic = is_dynamic;
88	ps->data_p = buf;
89	ps->buffer_size = size;
90}
91
92/*******************************************************************
93 Take some memory back from a prs_struct.
94 ********************************************************************/
95
96char *prs_take_memory(prs_struct *ps, uint32 *psize)
97{
98	char *ret = ps->data_p;
99	if(psize)
100		*psize = ps->buffer_size;
101	ps->is_dynamic = False;
102	prs_mem_free(ps);
103	return ret;
104}
105
106/*******************************************************************
107 Attempt, if needed, to grow a data buffer.
108 Also depends on the data stream mode (io).
109 ********************************************************************/
110
111BOOL prs_grow(prs_struct *ps, uint32 extra_space)
112{
113	uint32 new_size;
114	char *new_data;
115
116	if(ps->data_offset + extra_space <= ps->buffer_size)
117		return True;
118
119	/*
120	 * We cannot grow the buffer if we're not reading
121	 * into the prs_struct, or if we don't own the memory.
122	 */
123
124	if(UNMARSHALLING(ps) || !ps->is_dynamic) {
125		DEBUG(0,("prs_grow: Buffer overflow - unable to expand buffer by %u bytes.\n",
126				(unsigned int)extra_space));
127		return False;
128	}
129
130	/*
131	 * Decide how much extra space we really need.
132	 */
133
134	extra_space -= (ps->buffer_size - ps->data_offset);
135
136	if(ps->buffer_size == 0) {
137
138		/*
139		 * Ensure we have at least a PDU's length, or extra_space, whichever
140		 * is greater.
141		 */
142
143		new_size = MAX(MAX_PDU_FRAG_LEN,extra_space);
144
145		if((new_data = malloc(new_size)) == NULL) {
146			DEBUG(0,("prs_grow: Malloc failure for size %u.\n", (unsigned int)new_size));
147			return False;
148		}
149		memset(new_data, '\0', new_size );
150	} else {
151
152		/*
153		 * If the current buffer size is bigger than the space needed, just
154		 * double it, else add extra_space.
155		 */
156
157		new_size = MAX(ps->buffer_size*2, ps->buffer_size + extra_space);
158
159		if((new_data = Realloc(ps->data_p, new_size)) == NULL) {
160			DEBUG(0,("prs_grow: Realloc failure for size %u.\n",
161				(unsigned int)new_size));
162			return False;
163		}
164	}
165
166	ps->buffer_size = new_size;
167	ps->data_p = new_data;
168
169	return True;
170}
171
172/*******************************************************************
173 Attempt to force a data buffer to grow by len bytes.
174 This is only used when appending more data onto a prs_struct
175 when reading an rpc reply, before unmarshalling it.
176 ********************************************************************/
177
178BOOL prs_force_grow(prs_struct *ps, uint32 extra_space)
179{
180	uint32 new_size = ps->buffer_size + extra_space;
181	char *new_data;
182
183	if(!UNMARSHALLING(ps) || !ps->is_dynamic) {
184		DEBUG(0,("prs_force_grow: Buffer overflow - unable to expand buffer by %u bytes.\n",
185				(unsigned int)extra_space));
186		return False;
187	}
188
189	if((new_data = Realloc(ps->data_p, new_size)) == NULL) {
190		DEBUG(0,("prs_force_grow: Realloc failure for size %u.\n",
191			(unsigned int)new_size));
192		return False;
193	}
194
195	ps->buffer_size = new_size;
196	ps->data_p = new_data;
197
198	return True;
199}
200
201/*******************************************************************
202 Get the data pointer (external interface).
203 ********************************************************************/
204
205char *prs_data_p(prs_struct *ps)
206{
207	return ps->data_p;
208}
209
210/*******************************************************************
211 Get the current data size (external interface).
212 ********************************************************************/
213
214uint32 prs_data_size(prs_struct *ps)
215{
216	return ps->buffer_size;
217}
218
219/*******************************************************************
220 Fetch the current offset (external interface).
221 ********************************************************************/
222
223uint32 prs_offset(prs_struct *ps)
224{
225	return ps->data_offset;
226}
227
228/*******************************************************************
229 Set the current offset (external interface).
230 ********************************************************************/
231
232BOOL prs_set_offset(prs_struct *ps, uint32 offset)
233{
234	if(offset <= ps->data_offset) {
235		ps->data_offset = offset;
236		return True;
237	}
238
239	if(!prs_grow(ps, offset - ps->data_offset))
240		return False;
241
242	ps->data_offset = offset;
243	return True;
244}
245
246/*******************************************************************
247 Append the data from one parse_struct into another.
248 ********************************************************************/
249
250BOOL prs_append_prs_data(prs_struct *dst, prs_struct *src)
251{
252	if(!prs_grow(dst, prs_offset(src)))
253		return False;
254
255	memcpy(&dst->data_p[dst->data_offset], prs_data_p(src), (size_t)prs_offset(src));
256	dst->data_offset += prs_offset(src);
257
258	return True;
259}
260
261/*******************************************************************
262 Append the data from a buffer into a parse_struct.
263 ********************************************************************/
264
265BOOL prs_append_data(prs_struct *dst, char *src, uint32 len)
266{
267	if(!prs_grow(dst, len))
268		return False;
269
270	memcpy(&dst->data_p[dst->data_offset], src, (size_t)len);
271	dst->data_offset += len;
272
273	return True;
274}
275
276/*******************************************************************
277 Set the data as big-endian (external interface).
278 ********************************************************************/
279
280void prs_set_bigendian_data(prs_struct *ps)
281{
282	ps->bigendian_data = True;
283}
284
285/*******************************************************************
286 Align a the data_len to a multiple of align bytes - filling with
287 zeros.
288 ********************************************************************/
289
290BOOL prs_align(prs_struct *ps)
291{
292	uint32 mod = ps->data_offset & (ps->align-1);
293
294	if (ps->align != 0 && mod != 0) {
295		uint32 extra_space = (ps->align - mod);
296		if(!prs_grow(ps, extra_space))
297			return False;
298		memset(&ps->data_p[ps->data_offset], '\0', (size_t)extra_space);
299		ps->data_offset += extra_space;
300	}
301
302	return True;
303}
304
305/*******************************************************************
306 Ensure we can read/write to a given offset.
307 ********************************************************************/
308
309char *prs_mem_get(prs_struct *ps, uint32 extra_size)
310{
311	if(UNMARSHALLING(ps)) {
312		/*
313		 * If reading, ensure that we can read the requested size item.
314		 */
315		if (ps->data_offset + extra_size > ps->buffer_size) {
316			DEBUG(0,("prs_mem_get: reading data of size %u would overrun buffer.\n",
317					(unsigned int)extra_size ));
318			return NULL;
319		}
320	} else {
321		/*
322		 * Writing - grow the buffer if needed.
323		 */
324		if(!prs_grow(ps, extra_size))
325			return False;
326	}
327	return &ps->data_p[ps->data_offset];
328}
329
330/*******************************************************************
331 Change the struct type.
332 ********************************************************************/
333
334void prs_switch_type(prs_struct *ps, BOOL io)
335{
336	if ((ps->io ^ io) == True)
337		ps->io=io;
338}
339
340/*******************************************************************
341 Force a prs_struct to be dynamic even when it's size is 0.
342 ********************************************************************/
343
344void prs_force_dynamic(prs_struct *ps)
345{
346	ps->is_dynamic=True;
347}
348
349/*******************************************************************
350 Stream a uint8.
351 ********************************************************************/
352
353BOOL prs_uint8(char *name, prs_struct *ps, int depth, uint8 *data8)
354{
355	char *q = prs_mem_get(ps, sizeof(uint8));
356	if (q == NULL)
357		return False;
358
359	DBG_RW_CVAL(name, depth, ps->data_offset, ps->io, q, *data8)
360	ps->data_offset += sizeof(uint8);
361
362	return True;
363}
364
365/*******************************************************************
366 Stream a uint16.
367 ********************************************************************/
368
369BOOL prs_uint16(char *name, prs_struct *ps, int depth, uint16 *data16)
370{
371	char *q = prs_mem_get(ps, sizeof(uint16));
372	if (q == NULL)
373		return False;
374
375	DBG_RW_SVAL(name, depth, ps->data_offset, ps->io, ps->bigendian_data, q, *data16)
376	ps->data_offset += sizeof(uint16);
377
378	return True;
379}
380
381/*******************************************************************
382 Stream a uint32.
383 ********************************************************************/
384
385BOOL prs_uint32(char *name, prs_struct *ps, int depth, uint32 *data32)
386{
387	char *q = prs_mem_get(ps, sizeof(uint32));
388	if (q == NULL)
389		return False;
390
391	DBG_RW_IVAL(name, depth, ps->data_offset, ps->io, ps->bigendian_data, q, *data32)
392	ps->data_offset += sizeof(uint32);
393
394	return True;
395}
396
397
398/******************************************************************
399 Stream an array of uint8s. Length is number of uint8s.
400 ********************************************************************/
401
402BOOL prs_uint8s(BOOL charmode, char *name, prs_struct *ps, int depth, uint8 *data8s, int len)
403{
404	char *q = prs_mem_get(ps, len * sizeof(uint8));
405	if (q == NULL)
406		return False;
407
408	DBG_RW_PCVAL(charmode, name, depth, ps->data_offset, ps->io, q, data8s, len)
409	ps->data_offset += (len * sizeof(uint8));
410
411	return True;
412}
413
414/******************************************************************
415 Stream an array of uint32s. Length is number of uint32s.
416 ********************************************************************/
417
418BOOL prs_uint32s(BOOL charmode, char *name, prs_struct *ps, int depth, uint32 *data32s, int len)
419{
420	char *q = prs_mem_get(ps, len * sizeof(uint32));
421	if (q == NULL)
422		return False;
423
424	DBG_RW_PIVAL(charmode, name, depth, ps->data_offset, ps->io, ps->bigendian_data, q, data32s, len)
425	ps->data_offset += (len * sizeof(uint32));
426
427	return True;
428}
429
430/******************************************************************
431 Stream a "not" unicode string, length/buffer specified separately,
432 in byte chars. String is in little-endian format.
433 ********************************************************************/
434
435BOOL prs_buffer2(BOOL charmode, char *name, prs_struct *ps, int depth, BUFFER2 *str)
436{
437	char *p = (char *)str->buffer;
438	char *q = prs_mem_get(ps, str->buf_len);
439	if (q == NULL)
440		return False;
441
442	/* If we're using big-endian, reverse to get little-endian. */
443	if(ps->bigendian_data)
444		DBG_RW_PSVAL(charmode, name, depth, ps->data_offset, ps->io, ps->bigendian_data, q, p, str->buf_len/2)
445	else
446		DBG_RW_PCVAL(charmode, name, depth, ps->data_offset, ps->io, q, p, str->buf_len)
447	ps->data_offset += str->buf_len;
448
449	return True;
450}
451
452/******************************************************************
453 Stream a string, length/buffer specified separately,
454 in uint8 chars.
455 ********************************************************************/
456
457BOOL prs_string2(BOOL charmode, char *name, prs_struct *ps, int depth, STRING2 *str)
458{
459	char *q = prs_mem_get(ps, str->str_str_len * sizeof(uint8));
460	if (q == NULL)
461		return False;
462
463	DBG_RW_PCVAL(charmode, name, depth, ps->data_offset, ps->io, q, str->buffer, str->str_max_len)
464	ps->data_offset += (str->str_str_len * sizeof(uint8));
465
466	return True;
467}
468
469/******************************************************************
470 Stream a unicode string, length/buffer specified separately,
471 in uint16 chars. We use DBG_RW_PCVAL, not DBG_RW_PSVAL here
472 as the unicode string is already in little-endian format.
473 ********************************************************************/
474
475BOOL prs_unistr2(BOOL charmode, char *name, prs_struct *ps, int depth, UNISTR2 *str)
476{
477	char *p = (char *)str->buffer;
478	char *q = prs_mem_get(ps, str->uni_str_len * sizeof(uint16));
479	if (q == NULL)
480		return False;
481
482	/* If we're using big-endian, reverse to get little-endian. */
483	if(ps->bigendian_data)
484		DBG_RW_PSVAL(charmode, name, depth, ps->data_offset, ps->io, ps->bigendian_data, q, p, str->uni_str_len)
485	else
486		DBG_RW_PCVAL(charmode, name, depth, ps->data_offset, ps->io, q, p, str->uni_str_len * 2)
487	ps->data_offset += (str->uni_str_len * sizeof(uint16));
488
489	return True;
490}
491
492/******************************************************************
493 Stream a unicode string, length/buffer specified separately,
494 in uint16 chars. We use DBG_RW_PCVAL, not DBG_RW_PSVAL here
495 as the unicode string is already in little-endian format.
496 ********************************************************************/
497
498BOOL prs_unistr3(BOOL charmode, char *name, UNISTR3 *str, prs_struct *ps, int depth)
499{
500	char *p = (char *)str->str.buffer;
501	char *q = prs_mem_get(ps, str->uni_str_len * sizeof(uint16));
502	if (q == NULL)
503		return False;
504
505	/* If we're using big-endian, reverse to get little-endian. */
506	if(ps->bigendian_data)
507		DBG_RW_PSVAL(charmode, name, depth, ps->data_offset, ps->io, ps->bigendian_data, q, p, str->uni_str_len)
508	else
509		DBG_RW_PCVAL(charmode, name, depth, ps->data_offset, ps->io, q, p, str->uni_str_len * 2)
510	ps->data_offset += (str->uni_str_len * sizeof(uint16));
511
512	return True;
513}
514
515/*******************************************************************
516 Stream a unicode  null-terminated string. As the string is already
517 in little-endian format then do it as a stream of bytes.
518 ********************************************************************/
519
520BOOL prs_unistr(char *name, prs_struct *ps, int depth, UNISTR *str)
521{
522	int len = 0;
523	unsigned char *p = (unsigned char *)str->buffer;
524	uint8 *start;
525	char *q;
526
527	for(len = 0; len < (sizeof(str->buffer) / sizeof(str->buffer[0])) &&
528			   str->buffer[len] != 0; len++)
529		;
530
531	q = prs_mem_get(ps, len*2);
532	if (q == NULL)
533		return False;
534
535	start = (uint8*)q;
536
537	len = 0;
538	do
539	{
540		if(ps->bigendian_data) {
541			RW_SVAL(ps->io, ps->bigendian_data, q, *p, 0)
542			p += 2;
543			q += 2;
544		} else {
545			RW_CVAL(ps->io, q, *p, 0);
546			p++;
547			q++;
548			RW_CVAL(ps->io, q, *p, 0);
549			p++;
550			q++;
551		}
552		len++;
553	} while ((len < (sizeof(str->buffer) / sizeof(str->buffer[0]))) &&
554		     (str->buffer[len] != 0));
555
556	ps->data_offset += len*2;
557
558	dump_data(5+depth, (char *)start, len * 2);
559
560	return True;
561}
562
563/*******************************************************************
564 Stream a null-terminated string.  len is strlen, and therefore does
565 not include the null-termination character.
566 ********************************************************************/
567
568BOOL prs_string(char *name, prs_struct *ps, int depth, char *str, int len, int max_buf_size)
569{
570	char *q;
571	uint8 *start;
572	int i;
573
574	len = MIN(len, (max_buf_size-1));
575
576	q = prs_mem_get(ps, len+1);
577	if (q == NULL)
578		return False;
579
580	start = (uint8*)q;
581
582	for(i = 0; i < len; i++) {
583		RW_CVAL(ps->io, q, str[i],0);
584		q++;
585	}
586
587	/* The terminating null. */
588	str[i] = '\0';
589
590	if (MARSHALLING(ps)) {
591		RW_CVAL(ps->io, q, str[i], 0);
592	}
593
594	ps->data_offset += len+1;
595
596	dump_data(5+depth, (char *)start, len);
597
598	return True;
599}
600
601/*******************************************************************
602 prs_uint16 wrapper. Call this and it sets up a pointer to where the
603 uint16 should be stored, or gets the size if reading.
604 ********************************************************************/
605
606BOOL prs_uint16_pre(char *name, prs_struct *ps, int depth, uint16 *data16, uint32 *offset)
607{
608	(*offset) = ps->data_offset;
609	if (UNMARSHALLING(ps)) {
610		/* reading. */
611		return prs_uint16(name, ps, depth, data16);
612	} else {
613		char *q = prs_mem_get(ps, sizeof(uint16));
614		if(q ==NULL)
615			return False;
616		ps->data_offset += sizeof(uint16);
617	}
618	return True;
619}
620
621/*******************************************************************
622 prs_uint16 wrapper.  call this and it retrospectively stores the size.
623 does nothing on reading, as that is already handled by ...._pre()
624 ********************************************************************/
625
626BOOL prs_uint16_post(char *name, prs_struct *ps, int depth, uint16 *data16,
627				uint32 ptr_uint16, uint32 start_offset)
628{
629	if (MARSHALLING(ps)) {
630		/*
631		 * Writing - temporarily move the offset pointer.
632		 */
633		uint16 data_size = ps->data_offset - start_offset;
634		uint32 old_offset = ps->data_offset;
635
636		ps->data_offset = ptr_uint16;
637		if(!prs_uint16(name, ps, depth, &data_size)) {
638			ps->data_offset = old_offset;
639			return False;
640		}
641		ps->data_offset = old_offset;
642	} else {
643		ps->data_offset = start_offset + (uint32)(*data16);
644	}
645	return True;
646}
647
648/*******************************************************************
649 prs_uint32 wrapper. Call this and it sets up a pointer to where the
650 uint32 should be stored, or gets the size if reading.
651 ********************************************************************/
652
653BOOL prs_uint32_pre(char *name, prs_struct *ps, int depth, uint32 *data32, uint32 *offset)
654{
655	(*offset) = ps->data_offset;
656	if (UNMARSHALLING(ps)) {
657		/* reading. */
658		return prs_uint32(name, ps, depth, data32);
659	} else {
660		ps->data_offset += sizeof(uint32);
661	}
662	return True;
663}
664
665/*******************************************************************
666 prs_uint32 wrapper.  call this and it retrospectively stores the size.
667 does nothing on reading, as that is already handled by ...._pre()
668 ********************************************************************/
669
670BOOL prs_uint32_post(char *name, prs_struct *ps, int depth, uint32 *data32,
671				uint32 ptr_uint32, uint32 data_size)
672{
673	if (MARSHALLING(ps)) {
674		/*
675		 * Writing - temporarily move the offset pointer.
676		 */
677		uint32 old_offset = ps->data_offset;
678		ps->data_offset = ptr_uint32;
679		if(!prs_uint32(name, ps, depth, &data_size)) {
680			ps->data_offset = old_offset;
681			return False;
682		}
683		ps->data_offset = old_offset;
684	}
685	return True;
686}
687