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
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 extern int DEBUGLEVEL;
28 /*******************************************************************
29 debug output for parsing info.
31 XXXX side-effect of this function is to increase the debug depth XXXX
33 ********************************************************************/
34 void prs_debug(prs_struct *ps, int depth, char *desc, char *fn_name)
36 DEBUG(5+depth, ("%s%06x %s %s\n", tab_depth(depth), ps->offset, fn_name, desc));
39 /*******************************************************************
40 initialise a parse structure
41 ********************************************************************/
42 void prs_init(prs_struct *ps, uint32 size,
43 uint8 align, uint32 margin,
51 mem_buf_init(&(ps->data), margin);
55 mem_alloc_data(ps->data, size);
56 ps->data->offset.start = 0;
57 ps->data->offset.end = 0xffffffff;
61 /*******************************************************************
62 initialise a parse structure
63 ********************************************************************/
64 void prs_mem_free(prs_struct *ps)
66 mem_buf_free(&(ps->data));
69 /*******************************************************************
70 align a pointer to a multiple of align_offset bytes. looks like it
71 will work for offsets of 0, 2 and 4...
72 ********************************************************************/
73 void prs_align(prs_struct *ps)
75 int mod = ps->offset & (ps->align-1);
76 if (ps->align != 0 && mod != 0)
78 ps->offset += ps->align - mod;
82 /*******************************************************************
83 attempt, if appropriate, to grow a data buffer.
85 depends on the data stream mode (io)
86 ********************************************************************/
87 BOOL prs_grow(prs_struct *ps)
89 return mem_grow_data(&(ps->data), ps->io, ps->offset, False);
93 /*******************************************************************
95 ********************************************************************/
96 BOOL prs_uint8(char *name, prs_struct *ps, int depth, uint8 *data8)
98 char *q = mem_data(&(ps->data), ps->offset);
99 if (q == NULL) return False;
101 DBG_RW_CVAL(name, depth, ps->offset, ps->io, q, *data8)
107 /*******************************************************************
109 ********************************************************************/
110 BOOL prs_uint16(char *name, prs_struct *ps, int depth, uint16 *data16)
112 char *q = mem_data(&(ps->data), ps->offset);
113 if (q == NULL) return False;
115 DBG_RW_SVAL(name, depth, ps->offset, ps->io, q, *data16)
121 /*******************************************************************
123 ********************************************************************/
124 BOOL prs_uint32(char *name, prs_struct *ps, int depth, uint32 *data32)
126 char *q = mem_data(&(ps->data), ps->offset);
127 if (q == NULL) return False;
129 DBG_RW_IVAL(name, depth, ps->offset, ps->io, q, *data32)
136 /******************************************************************
137 stream an array of uint8s. length is number of uint8s
138 ********************************************************************/
139 BOOL prs_uint8s(BOOL charmode, char *name, prs_struct *ps, int depth, uint8 *data8s, int len)
141 char *q = mem_data(&(ps->data), ps->offset);
142 if (q == NULL) return False;
144 DBG_RW_PCVAL(charmode, name, depth, ps->offset, ps->io, q, data8s, len)
150 /******************************************************************
151 stream an array of uint16s. length is number of uint16s
152 ********************************************************************/
153 BOOL prs_uint16s(BOOL charmode, char *name, prs_struct *ps, int depth, uint16 *data16s, int len)
155 char *q = mem_data(&(ps->data), ps->offset);
156 if (q == NULL) return False;
158 DBG_RW_PSVAL(charmode, name, depth, ps->offset, ps->io, q, data16s, len)
159 ps->offset += len * sizeof(uint16);
164 /******************************************************************
165 stream an array of uint32s. length is number of uint32s
166 ********************************************************************/
167 BOOL prs_uint32s(BOOL charmode, char *name, prs_struct *ps, int depth, uint32 *data32s, int len)
169 char *q = mem_data(&(ps->data), ps->offset);
170 if (q == NULL) return False;
172 DBG_RW_PIVAL(charmode, name, depth, ps->offset, ps->io, q, data32s, len)
173 ps->offset += len * sizeof(uint32);
178 /******************************************************************
179 stream a "not" unicode string, length/buffer specified separately,
181 ********************************************************************/
182 BOOL prs_uninotstr2(BOOL charmode, char *name, prs_struct *ps, int depth, UNINOTSTR2 *str)
184 char *q = mem_data(&(ps->data), ps->offset);
185 if (q == NULL) return False;
187 DBG_RW_PSVAL(charmode, name, depth, ps->offset, ps->io, q, str->buffer, str->uni_max_len)
188 ps->offset += str->uni_buf_len;
193 /******************************************************************
194 stream a string, length/buffer specified separately,
196 ********************************************************************/
197 BOOL prs_string2(BOOL charmode, char *name, prs_struct *ps, int depth, STRING2 *str)
199 char *q = mem_data(&(ps->data), ps->offset);
200 if (q == NULL) return False;
202 DBG_RW_PCVAL(charmode, name, depth, ps->offset, ps->io, q, str->buffer, str->str_max_len)
203 ps->offset += str->str_str_len * sizeof(uint8);
208 /******************************************************************
209 stream a unicode string, length/buffer specified separately,
211 ********************************************************************/
212 BOOL prs_unistr2(BOOL charmode, char *name, prs_struct *ps, int depth, UNISTR2 *str)
214 char *q = mem_data(&(ps->data), ps->offset);
215 if (q == NULL) return False;
217 DBG_RW_PSVAL(charmode, name, depth, ps->offset, ps->io, q, str->buffer, str->uni_max_len)
218 ps->offset += str->uni_str_len * sizeof(uint16);
223 /*******************************************************************
224 stream a unicode null-terminated string
225 ********************************************************************/
226 BOOL prs_unistr(char *name, prs_struct *ps, int depth, UNISTR *str)
228 char *q = mem_data(&(ps->data), ps->offset);
230 uint8 *start = (uint8*)q;
232 if (q == NULL) return False;
236 RW_SVAL(ps->io, q, str->buffer[i],0);
240 } while ((i < sizeof(str->buffer) / sizeof(str->buffer[0])) &&
241 (str->buffer[i] != 0));
245 dump_data(5+depth, (char *)start, ps->offset);
250 /*******************************************************************
251 stream a null-terminated string. len is strlen, and therefore does
252 not include the null-termination character.
254 len == 0 indicates variable length string
255 (up to max size of pstring - 1024 chars).
257 ********************************************************************/
258 BOOL prs_string(char *name, prs_struct *ps, int depth, char *str, uint16 len)
260 char *q = mem_data(&(ps->data), ps->offset);
261 uint8 *start = (uint8*)q;
262 int i = -1; /* start off at zero after 1st i++ */
264 if (q == NULL) return False;
270 if (i < len || len == 0)
272 RW_CVAL(ps->io, q, str[i],0);
277 RW_CVAL(ps->io, q, dummy,0);
282 } while (i < sizeof(pstring) && (len == 0 ? str[i] != 0 : i < len) );
286 dump_data(5+depth, (char *)start, ps->offset);