1 /************************************************************************/
2 /*									*/
3 /*  Manage an array of data bytes and its size.				*/
4 /*									*/
5 /************************************************************************/
6 
7 #   include	"appUtilConfig.h"
8 
9 #   include	<stdlib.h>
10 #   include	<string.h>
11 
12 #   include	"utilMemoryBuffer.h"
13 #   include	<appDebugon.h>
14 
utilInitMemoryBuffer(MemoryBuffer * mb)15 void utilInitMemoryBuffer(	MemoryBuffer *	mb )
16     {
17     mb->mbSize= 0;
18     mb->mbBytes= (unsigned char *)0;
19     return;
20     }
21 
utilCleanMemoryBuffer(MemoryBuffer * mb)22 void utilCleanMemoryBuffer(	MemoryBuffer *	mb )
23     {
24     if  ( mb->mbBytes )
25 	{ free( mb->mbBytes );	}
26 
27     mb->mbSize= 0;
28     mb->mbBytes= (unsigned char *)0;
29 
30     return;
31     }
32 
utilEmptyMemoryBuffer(MemoryBuffer * mb)33 void utilEmptyMemoryBuffer(	MemoryBuffer *	mb )
34     {
35     mb->mbSize= 0;
36     if  ( mb->mbBytes )
37 	{ mb->mbBytes[0]= '\0';	}
38 
39     return;
40     }
41 
42 /************************************************************************/
43 /*									*/
44 /*  Resize a memory buffer. Guarantee that there is a terminated buffer	*/
45 /*									*/
46 /************************************************************************/
47 
utilMemoryBufferSetSize(MemoryBuffer * mb,int size)48 int utilMemoryBufferSetSize(	MemoryBuffer *	mb,
49 				int		size )
50     {
51     if  ( utilMemoryBufferIsEmpty( mb ) || size > mb->mbSize )
52 	{
53 	unsigned char *	fresh;
54 
55 	fresh= (unsigned char *)realloc( mb->mbBytes, size+ 1 );
56 	if  ( ! fresh )
57 	    { LXDEB(size,fresh); return -1;	}
58 
59 	if  ( ! mb->mbBytes )
60 	    { fresh[0]= '\0';	}
61 
62 	mb->mbBytes= fresh;
63 	mb->mbSize= size;
64 	fresh[mb->mbSize]= '\0';
65 	}
66 
67     if  ( size < mb->mbSize )
68 	{
69 	mb->mbSize= size;
70 	mb->mbBytes[mb->mbSize]= '\0';
71 	}
72 
73     return 0;
74     }
75 
utilMemoryBufferSetBytes(MemoryBuffer * mb,const unsigned char * bytes,int size)76 int utilMemoryBufferSetBytes(	MemoryBuffer *		mb,
77 				const unsigned char *	bytes,
78 				int			size )
79     {
80     unsigned char *	fresh;
81 
82     fresh= (unsigned char *)realloc( mb->mbBytes, size+ 1 );
83     if  ( ! fresh )
84 	{ LXDEB(size,fresh); return -1;	}
85 
86     memcpy( fresh, bytes, size );
87 
88     mb->mbBytes= fresh;
89     mb->mbSize= size;
90     fresh[mb->mbSize]= '\0';
91 
92     return 0;
93     }
94 
utilMemoryBufferSetString(MemoryBuffer * mb,const char * s)95 int utilMemoryBufferSetString( MemoryBuffer *			mb,
96 				const char *			s )
97     {
98     if  ( ! s )
99 	{ XDEB(s); return -1;	}
100 
101     return utilMemoryBufferSetBytes( mb, (const unsigned char *)s, strlen( s ) );
102     }
103 
utilMemoryBufferAppendBytes(MemoryBuffer * mb,const unsigned char * bytes,int size)104 int utilMemoryBufferAppendBytes(	MemoryBuffer *		mb,
105 					const unsigned char *	bytes,
106 					int			size )
107     {
108     unsigned char *	fresh;
109 
110     fresh= (unsigned char *)realloc( mb->mbBytes, mb->mbSize+ size+ 1 );
111     if  ( ! fresh )
112 	{ LXDEB(size,fresh); return -1;	}
113 
114     memcpy( fresh+ mb->mbSize, bytes, size );
115 
116     mb->mbBytes= fresh;
117     mb->mbSize= mb->mbSize+ size;
118     fresh[mb->mbSize]= '\0';
119 
120     return 0;
121     }
122 
utilMemoryBufferAppendString(MemoryBuffer * mb,const char * s)123 int utilMemoryBufferAppendString(	MemoryBuffer *		mb,
124 					const char *		s )
125     {
126     if  ( ! s )
127 	{ XDEB(s); return -1;	}
128 
129     return utilMemoryBufferAppendBytes( mb,
130 				    (const unsigned char *)s, strlen( s ) );
131     }
132 
utilMemoryAppendBuffer(MemoryBuffer * to,const MemoryBuffer * from)133 int utilMemoryAppendBuffer(		MemoryBuffer *		to,
134 					const MemoryBuffer *	from )
135     {
136     return utilMemoryBufferAppendBytes( to, from->mbBytes, from->mbSize );
137     }
138 
utilCopyMemoryBuffer(MemoryBuffer * mbTo,const MemoryBuffer * mbFrom)139 int utilCopyMemoryBuffer(	MemoryBuffer *		mbTo,
140 				const MemoryBuffer *	mbFrom )
141     {
142     if  ( utilMemoryBufferSetBytes( mbTo, mbFrom->mbBytes, mbFrom->mbSize ) )
143 	{ LDEB(mbFrom->mbSize); return -1;	}
144 
145     return 0;
146     }
147 
utilMemoryBufferGetBytes(int * pSize,const MemoryBuffer * from)148 const unsigned char * utilMemoryBufferGetBytes(	int *			pSize,
149 						const MemoryBuffer *	from )
150     {
151     *pSize= from->mbSize;
152     return from->mbBytes?from->mbBytes:(const unsigned char *)"";
153     }
154 
utilMemoryBufferGetString(const MemoryBuffer * from)155 const char * utilMemoryBufferGetString(	const MemoryBuffer *	from )
156     {
157     return from->mbBytes?(const char *)from->mbBytes:"";
158     }
159 
utilMemoryCompareBuffers(const MemoryBuffer * mb1,const MemoryBuffer * mb2)160 int utilMemoryCompareBuffers(	const MemoryBuffer *	mb1,
161 				const MemoryBuffer *	mb2 )
162     {
163     int		len;
164     int		cmp;
165 
166     len= mb1->mbSize;
167     if  ( len > mb2->mbSize )
168 	{ len=  mb2->mbSize;	}
169 
170     cmp= memcmp( mb1->mbBytes, mb2->mbBytes, len );
171     if  ( cmp > 0 )
172 	{ cmp=  1;	}
173     if  ( cmp < 0 )
174 	{ cmp= -1;	}
175 
176     if  ( cmp == 0 )
177 	{
178 	if  ( mb2->mbSize > mb1->mbSize )
179 	    { cmp=  1;	}
180 	if  ( mb2->mbSize < mb1->mbSize )
181 	    { cmp= -1;	}
182 	}
183 
184     return cmp;
185     }
186 
187 /************************************************************************/
188 /*									*/
189 /*  Replace bytes in a memory buffer.					*/
190 /*									*/
191 /*  1)	Refuse silly parameters. We do not want to create holes in the	*/
192 /*	buffer.								*/
193 /*									*/
194 /************************************************************************/
195 
utilMemoryBufferReplaceBytes(MemoryBuffer * mb,int offHead,int offPast,const unsigned char * addedBytes,int addedCount)196 int utilMemoryBufferReplaceBytes(	MemoryBuffer *		mb,
197 					int			offHead,
198 					int			offPast,
199 					const unsigned char *	addedBytes,
200 					int			addedCount )
201     {
202     int		sizeShift;
203     int		oldSize= mb->mbSize;
204 
205     sizeShift= addedCount- offPast+ offHead;
206 
207     /*  1  */
208     if  ( offHead < 0 || offHead > mb->mbSize )
209 	{ LLDEB(offHead,mb->mbSize); return -1;	}
210 
211     if  ( sizeShift > 0						&&
212 	  utilMemoryBufferSetSize( mb, mb->mbSize+ sizeShift )	)
213 	{ LLDEB(mb->mbSize,sizeShift); return -1;	}
214 
215     if  ( oldSize > 0 )
216 	{
217 	int		n= oldSize- offPast+ 1;
218 
219 	if  ( n < 0 )
220 	    { LDEB(n); return -1;	}
221 	else{
222 	    memmove( mb->mbBytes+ offPast+ sizeShift, mb->mbBytes+ offPast, n );
223 	    }
224 	}
225 
226     memcpy( mb->mbBytes+ offHead, addedBytes, addedCount );
227 
228     if  ( sizeShift < 0 )
229 	{
230 	mb->mbSize += sizeShift;
231 	mb->mbBytes[mb->mbSize]= '\0';
232 	}
233 
234     return 0;
235     }
236 
237 
utilMemoryBufferGetRange(MemoryBuffer * mbTo,const MemoryBuffer * mbFrom,int offFrom,int lenFrom)238 int utilMemoryBufferGetRange(	MemoryBuffer *		mbTo,
239 				const MemoryBuffer *	mbFrom,
240 				int			offFrom,
241 				int			lenFrom )
242     {
243     return utilMemoryBufferReplaceBytes( mbTo, 0, mbTo->mbSize,
244 					    mbFrom->mbBytes+ offFrom, lenFrom );
245     }
246 
247 /************************************************************************/
248 /*									*/
249 /*  Save the contents of a memory buffer to an allocated string.	*/
250 /*									*/
251 /************************************************************************/
252 
utilMemoryStrdup(const MemoryBuffer * mbFrom)253 char * utilMemoryStrdup(	const MemoryBuffer *	mbFrom )
254     {
255     if  ( utilMemoryBufferIsEmpty( mbFrom ) )
256 	{
257 	char * empty= malloc(1);
258 	if  ( empty )
259 	    { *empty= '\0';	}
260 
261 	return empty;
262 	}
263 
264     return strdup( (const char *)mbFrom->mbBytes );
265     }
266 
267 /************************************************************************/
268 /*									*/
269 /*  Compare the contents to a string.					*/
270 /*									*/
271 /************************************************************************/
272 
utilMemoryBufferEqualsString(const MemoryBuffer * mb,const char * s)273 int utilMemoryBufferEqualsString(		const MemoryBuffer *	mb,
274 						const char *		s )
275     {
276     MemoryBuffer	mbLoc;
277 
278     mbLoc.mbBytes= (unsigned char *)s;
279     mbLoc.mbSize= strlen( s );
280 
281     return utilMemoryCompareBuffers( &mbLoc, mb ) == 0;
282     }
283 
284 /************************************************************************/
285 /*									*/
286 /*  Find bytes in a memory buffer.					*/
287 /*									*/
288 /************************************************************************/
289 
utilMemoryBufferLastIndexOf(const MemoryBuffer * mb,int chr)290 int utilMemoryBufferLastIndexOf(	const MemoryBuffer *	mb,
291 					int			chr )
292     {
293     const char *	p;
294 
295     if  ( utilMemoryBufferIsEmpty( mb ) )
296 	{ return -1;	}
297 
298     p= strrchr( (const char *)mb->mbBytes, chr );
299     if  ( p )
300 	{ return p- (const char *)mb->mbBytes;	}
301     else{ return -1;				}
302     }
303 
utilMemoryBufferIndexOf(const MemoryBuffer * mb,int chr)304 int utilMemoryBufferIndexOf(		const MemoryBuffer *	mb,
305 					int			chr )
306     {
307     const char *	p;
308 
309     if  ( utilMemoryBufferIsEmpty( mb ) )
310 	{ return -1;	}
311 
312     p= strchr( (const char *)mb->mbBytes, chr );
313     if  ( p )
314 	{ return p- (const char *)mb->mbBytes;	}
315     else{ return -1;				}
316     }
317