/*
- Unix SMB/Netbios implementation.
- Version 1.9.
+ Unix SMB/CIFS implementation.
Samba memory buffer functions
Copyright (C) Andrew Tridgell 1992-1997
Copyright (C) Luke Kenneth Casson Leighton 1996-1997
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
-extern int DEBUGLEVEL;
-
#include "includes.h"
-
-/*******************************************************************
-dump a prs to a file
- ********************************************************************/
+/**
+ * Dump a prs to a file: from the current location through to the end.
+ **/
void prs_dump(char *name, int v, prs_struct *ps)
+{
+ prs_dump_region(name, v, ps, ps->data_offset, ps->buffer_size);
+}
+
+
+/**
+ * Dump from the start of the prs to the current location.
+ **/
+void prs_dump_before(char *name, int v, prs_struct *ps)
+{
+ prs_dump_region(name, v, ps, 0, ps->data_offset);
+}
+
+
+/**
+ * Dump everything from the start of the prs up to the current location.
+ **/
+void prs_dump_region(char *name, int v, prs_struct *ps,
+ int from_off, int to_off)
{
int fd, i;
pstring fname;
if (fd != -1 || errno != EEXIST) break;
}
if (fd != -1) {
- write(fd, ps->data_p + ps->data_offset, ps->buffer_size - ps->data_offset);
+ write(fd, ps->data_p + from_off, to_off - from_off);
close(fd);
DEBUG(0,("created %s\n", fname));
}
}
-/*******************************************************************
- Initialise a parse structure - malloc the data if requested.
- ********************************************************************/
+/**
+ * Initialise an expandable parse structure.
+ *
+ * @param size Initial buffer size. If >0, a new buffer will be
+ * created with malloc().
+ *
+ * @return False if allocation fails, otherwise True.
+ **/
BOOL prs_init(prs_struct *ps, uint32 size, TALLOC_CTX *ctx, BOOL io)
{
ZERO_STRUCTP(ps);
DEBUG(0,("prs_init: malloc fail for %u bytes.\n", (unsigned int)size));
return False;
}
+ memset(ps->data_p, '\0', (size_t)size);
ps->is_dynamic = True; /* We own this memory. */
}
void prs_mem_free(prs_struct *ps)
{
- if(ps->is_dynamic && (ps->data_p != NULL))
- free(ps->data_p);
+ if(ps->is_dynamic)
+ SAFE_FREE(ps->data_p);
ps->is_dynamic = False;
- ps->data_p = NULL;
ps->buffer_size = 0;
ps->data_offset = 0;
}
+/*******************************************************************
+ Clear the memory in a parse structure.
+ ********************************************************************/
+
+void prs_mem_clear(prs_struct *ps)
+{
+ memset(ps->data_p, '\0', (size_t)ps->buffer_size);
+}
+
/*******************************************************************
Allocate memory when unmarshalling... Always zero clears.
********************************************************************/
DEBUG(0,("prs_grow: Malloc failure for size %u.\n", (unsigned int)new_size));
return False;
}
- memset(new_data, '\0', new_size );
+ memset(new_data, '\0', (size_t)new_size );
} else {
/*
* If the current buffer size is bigger than the space needed, just
return False;
}
- memset(&new_data[ps->buffer_size], '\0', new_size - ps->buffer_size);
+ memset(&new_data[ps->buffer_size], '\0', (size_t)(new_size - ps->buffer_size));
}
ps->buffer_size = new_size;
ps->data_p = new_data;
return False;
}
- memset(&new_data[ps->buffer_size], '\0', new_size - ps->buffer_size);
+ memset(&new_data[ps->buffer_size], '\0', (size_t)(new_size - ps->buffer_size));
ps->buffer_size = new_size;
ps->data_p = new_data;
BOOL prs_string2(BOOL charmode, char *name, prs_struct *ps, int depth, STRING2 *str)
{
int i;
- char *q = prs_mem_get(ps, str->str_str_len);
+ char *q = prs_mem_get(ps, str->str_max_len);
if (q == NULL)
return False;
if (UNMARSHALLING(ps)) {
- str->buffer = (unsigned char *)prs_alloc_mem(ps,str->str_str_len);
+ str->buffer = (unsigned char *)prs_alloc_mem(ps,str->str_max_len);
if (str->buffer == NULL)
return False;
}