1 /* Hey EMACS -*- linux-c -*- */
2 /* $Id: files9x.c 1343 2005-07-06 15:26:11Z roms $ */
3 
4 /*  libtifiles - file format library, a part of the TiLP project
5  *  Copyright (C) 1999-2005  Romain Lievin
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software Foundation,
19  *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /*
23 	VarEntry structure management routines
24 */
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <glib.h>
30 
31 #include "tifiles.h"
32 #include "logging.h"
33 
34 /**
35  * tifiles_ve_create:
36  *
37  * Allocate a new VarEntry structure.
38  *
39  * Return value: the entry or NULL if error.
40  **/
tifiles_ve_create(void)41 TIEXPORT2 VarEntry* TICALL tifiles_ve_create(void)
42 {
43 	return g_malloc0(sizeof(VarEntry));
44 }
45 
46 /**
47  * tifiles_ve_create_alloc_data:
48  * @size: length of data.
49  *
50  * Allocate a new VarEntry structure and space for data.
51  *
52  * Return value: the entry or NULL if error.
53  **/
tifiles_ve_create_alloc_data(uint32_t size)54 TIEXPORT2 VarEntry* TICALL tifiles_ve_create_alloc_data(uint32_t size)
55 {
56 	VarEntry* ve = tifiles_ve_create();
57 	if (ve != NULL)
58 	{
59 		ve->data = (uint8_t *)g_malloc0(size);
60 	}
61 
62 	return ve;
63 }
64 
65 /**
66  * tifiles_ve_create_with_data:
67  * @size: length of data.
68  *
69  * Deprecated version of tifiles_ve_create_alloc_data().
70  **/
tifiles_ve_create_with_data(uint32_t size)71 TIEXPORT2 VarEntry* TICALL tifiles_ve_create_with_data(uint32_t size)
72 {
73 	return tifiles_ve_create_alloc_data(size);
74 }
75 
76 /**
77  * tifiles_ve_create_with_data2:
78  * @data: data.
79  * @size: length of data.
80  *
81  * Allocate a new VarEntry structure and set size + data. data should have been allocated by tifiles_ve_alloc_data().
82  *
83  * Return value: the entry or NULL if error.
84  **/
tifiles_ve_create_with_data2(uint32_t size,uint8_t * data)85 TIEXPORT2 VarEntry* TICALL tifiles_ve_create_with_data2(uint32_t size, uint8_t * data)
86 {
87 	VarEntry* ve = tifiles_ve_create();
88 	if (ve != NULL)
89 	{
90 		ve->data = data;
91 		ve->size = size;
92 	}
93 
94 	return ve;
95 }
96 
97 /**
98  * tifiles_ve_delete:
99  * @ve: var entry.
100  *
101  * Free data buffer and the structure itself.
102  *
103  * Return value: none.
104  **/
tifiles_ve_delete(VarEntry * ve)105 TIEXPORT2 void TICALL tifiles_ve_delete(VarEntry* ve)
106 {
107 	if (ve != NULL)
108 	{
109 		g_free(ve->data);
110 		g_free(ve);
111 	}
112 	else
113 	{
114 		tifiles_critical("%s(NULL)", __FUNCTION__);
115 	}
116 }
117 
118 /**
119  * tifiles_ve_alloc_data:
120  * @size: length of data.
121  *
122  * Allocate space for data field of VarEntry.
123  *
124  * Return value: allocated space or NULL if error.
125  **/
tifiles_ve_alloc_data(size_t size)126 TIEXPORT2 void * TICALL tifiles_ve_alloc_data(size_t size)
127 {
128 	return g_malloc0((size+1) * sizeof(uint8_t));
129 }
130 
131 /**
132  * tifiles_ve_realloc_data:
133  * @size: length of data.
134  *
135  * Reallocate space for data field of VarEntry.
136  *
137  * Return value: allocated space or NULL if error.
138  **/
tifiles_ve_realloc_data(VarEntry * ve,size_t size)139 TIEXPORT2 VarEntry * TICALL tifiles_ve_realloc_data(VarEntry* ve, size_t size)
140 {
141 	if (ve != NULL)
142 	{
143 		uint8_t * data = g_realloc(ve->data, (size+1) * sizeof(uint8_t));
144 		if (size > ve->size)
145 		{
146 			memset(data + ve->size, 0x00, size - ve->size);
147 		}
148 		ve->data = data;
149 	}
150 	return ve;
151 }
152 
153 /**
154  * tifiles_ve_free_data:
155  * @data: length of data.
156  *
157  * Free space for data field of VarEntry.
158  **/
tifiles_ve_free_data(void * data)159 TIEXPORT2 void TICALL tifiles_ve_free_data(void * data)
160 {
161 	return g_free(data);
162 }
163 
164 /**
165  * tifiles_ve_create_array:
166  * @nelts: size of NULL-terminated array (number of VarEntry structures).
167  *
168  * Allocate a NULL-terminated array of VarEntry structures. You have to allocate
169  * each element of the array by yourself.
170  *
171  * Return value: the array or NULL if error.
172  **/
tifiles_ve_create_array(unsigned int nelts)173 TIEXPORT2 VarEntry** TICALL tifiles_ve_create_array(unsigned int nelts)
174 {
175 	return g_malloc0((nelts + 1) * sizeof(VarEntry *));
176 }
177 
178 /**
179  * tifiles_ve_resize_array:
180  * @array: address of array
181  * @nelts: size of NULL-terminated array (number of VarEntry structures).
182  *
183  * Re-allocate a NULL-terminated array of VarEntry structures. You have to allocate
184  * each element of the array by yourself.
185  *
186  * Return value: the array or NULL if error.
187  **/
tifiles_ve_resize_array(VarEntry ** array,unsigned int nelts)188 TIEXPORT2 VarEntry** TICALL tifiles_ve_resize_array(VarEntry** array, unsigned int nelts)
189 {
190 	VarEntry ** ptr = g_realloc(array, (nelts + 1) * sizeof(VarEntry *));
191 	if (ptr != NULL)
192 	{
193 		ptr[nelts] = NULL;
194 	}
195 	return ptr;
196 }
197 
198 /**
199  * tifiles_ve_delete_array:
200  * @array: an NULL-terminated array of VarEntry structures.
201  *
202  * Free the whole array (data buffer, VarEntry structure and array itself).
203  *
204  * Return value: none.
205  **/
tifiles_ve_delete_array(VarEntry ** array)206 TIEXPORT2 void TICALL tifiles_ve_delete_array(VarEntry** array)
207 {
208 	VarEntry** ptr;
209 
210 	if (array != NULL)
211 	{
212 		for (ptr = array; *ptr; ptr++)
213 		{
214 			tifiles_ve_delete(*ptr);
215 		}
216 		g_free(array);
217 	}
218 	else
219 	{
220 		tifiles_critical("%s(NULL)", __FUNCTION__);
221 	}
222 }
223 
224 /**
225  * tifiles_ve_copy:
226  * @dst: destination entry.
227  * @src: source entry.
228  *
229  * Copy VarEntry and its content from src to dst.
230  * If data is NULL, a new buffer is allocated before copying.
231  *
232  * Return value: the dst pointer or NULL if malloc error.
233  **/
tifiles_ve_copy(VarEntry * dst,VarEntry * src)234 TIEXPORT2 VarEntry* TICALL tifiles_ve_copy(VarEntry* dst, VarEntry* src)
235 {
236 	int alloc;
237 
238 	if (src != NULL && dst != NULL)
239 	{
240 		alloc = (dst->data == NULL);
241 
242 		memcpy(dst, src, sizeof(VarEntry));
243 		if (alloc)
244 		{
245 			dst->data = (uint8_t *)g_malloc0(dst->size);
246 			if (dst->data == NULL)
247 				return NULL;
248 		}
249 		memcpy(dst->data, src->data, src->size);
250 	}
251 	else
252 	{
253 		tifiles_critical("%s(): an argument is NULL", __FUNCTION__);
254 		return NULL;
255 	}
256 
257 	return dst;
258 }
259 
260 /**
261  * tifiles_ve_dup:
262  * @src: source entry.
263  *
264  * Duplicate VarEntry and its content from src to dst (full copy).
265  *
266  * Return value: a newly allocated entry (must be freed with #tifiles_ve_delete when no longer needed).
267  **/
tifiles_ve_dup(VarEntry * src)268 TIEXPORT2 VarEntry* TICALL tifiles_ve_dup(VarEntry* src)
269 {
270 	VarEntry* dst = NULL;
271 
272 	if (src != NULL)
273 	{
274 		dst = g_malloc0(sizeof(VarEntry));
275 		if (dst != NULL)
276 		{
277 			memcpy(dst, src, sizeof(VarEntry));
278 			dst->data = (uint8_t *)g_malloc0(dst->size);
279 
280 			if (src->data != NULL && dst->data != NULL)
281 			{
282 				memcpy(dst->data, src->data, dst->size);
283 			}
284 		}
285 	}
286 	else
287 	{
288 		tifiles_critical("%s(NULL)", __FUNCTION__);
289 	}
290 
291 	return dst;
292 }
293 
294 // ---
295 
296 /**
297  * tifiles_fp_create:
298  *
299  * Allocate a new FlashPage structure.
300  *
301  * Return value: the entry or NULL if error.
302  **/
tifiles_fp_create(void)303 TIEXPORT2 FlashPage* TICALL tifiles_fp_create(void)
304 {
305 	return g_malloc0(sizeof(FlashPage));
306 }
307 
308 /**
309  * tifiles_fp_alloc_data:
310  * @size: length of data.
311  *
312  * Allocate space for data field of FlashPage.
313  *
314  * Return value: allocated space or NULL if error.
315  **/
tifiles_fp_alloc_data(size_t size)316 TIEXPORT2 void * TICALL tifiles_fp_alloc_data(size_t size)
317 {
318 	uint8_t *data;
319 
320 	data = g_malloc0((size+1) * sizeof(uint8_t));
321 	if (data != NULL)
322 	{
323 		memset(data, 0xFF, size);
324 	}
325 
326 	return data;
327 }
328 
329 /**
330  * tifiles_fp_realloc_data:
331  * @size: new length of data.
332  *
333  * Reallocate space for data field of FlashPage.
334  *
335  * Return value: flash page, or NULL if error.
336  **/
tifiles_fp_realloc_data(FlashPage * fp,size_t size)337 TIEXPORT2 FlashPage * TICALL tifiles_fp_realloc_data(FlashPage* fp, size_t size)
338 {
339 	if (fp != NULL)
340 	{
341 		uint8_t * data = g_realloc(fp->data, (size+1) * sizeof(uint8_t));
342 		if (size > fp->size)
343 		{
344 			memset(data + fp->size, 0xFF, size - fp->size);
345 		}
346 		fp->data = data;
347 	}
348 	return fp;
349 }
350 
351 /**
352  * tifiles_fp_free_data:
353  * @data: length of data.
354  *
355  * Free space for data field of FlashPage.
356  **/
tifiles_fp_free_data(void * data)357 TIEXPORT2 void TICALL tifiles_fp_free_data(void * data)
358 {
359 	return g_free(data);
360 }
361 
362 /**
363  * tifiles_fp_create_alloc_data:
364  * @size: length of data.
365  *
366  * Allocate a new FlashPage structure and space for data.
367  *
368  * Return value: the entry or NULL if error.
369  **/
tifiles_fp_create_alloc_data(uint32_t size)370 TIEXPORT2 FlashPage* TICALL tifiles_fp_create_alloc_data(uint32_t size)
371 {
372 	FlashPage* ve = tifiles_fp_create();
373 	if (ve != NULL)
374 	{
375 		ve->data = (uint8_t *)g_malloc0(size);
376 	}
377 
378 	return ve;
379 }
380 
381 /**
382  * tifiles_fp_create_alloc_data:
383  * @size: length of data.
384  *
385  * Deprecated version of tifiles_ve_create_alloc_data().
386  **/
tifiles_fp_create_with_data(uint32_t size)387 TIEXPORT2 FlashPage* TICALL tifiles_fp_create_with_data(uint32_t size)
388 {
389 	return tifiles_fp_create_alloc_data(size);
390 }
391 
392 /**
393  * tifiles_fp_create_with_data2:
394  * @data: data.
395  * @size: length of data.
396  *
397  * Allocate a new FlashPage structure and set size + data. data should have been allocated by tifiles_fp_alloc_data().
398  *
399  * Return value: the entry or NULL if error.
400  **/
tifiles_fp_create_with_data2(uint32_t size,uint8_t * data)401 TIEXPORT2 FlashPage* TICALL tifiles_fp_create_with_data2(uint32_t size, uint8_t * data)
402 {
403 	FlashPage* ve = tifiles_fp_create();
404 	if (ve != NULL)
405 	{
406 		ve->data = data;
407 		ve->size = size;
408 	}
409 
410 	return ve;
411 }
412 
413 /**
414  * tifiles_fp_create_array:
415  * @nelts: size of NULL-terminated array (number of FlashPage structures).
416  *
417  * Allocate a NULL-terminated array of FlashPage structures. You have to allocate
418  * each element of the array by yourself.
419  *
420  * Return value: the array or NULL if error.
421  **/
tifiles_fp_create_array(unsigned int nelts)422 TIEXPORT2 FlashPage** TICALL tifiles_fp_create_array(unsigned int nelts)
423 {
424 	return g_malloc0((nelts + 1) * sizeof(FlashPage*));
425 }
426 
427 /**
428  * tifiles_fp_resize_array:
429  * @array: address of array
430  * @nelts: size of NULL-terminated array (number of FlashPage structures).
431  *
432  * Re-allocate a NULL-terminated array of FlashPage structures. You have to allocate
433  * each element of the array by yourself.
434  *
435  * Return value: the array or NULL if error.
436  **/
tifiles_fp_resize_array(FlashPage ** array,unsigned int nelts)437 TIEXPORT2 FlashPage** TICALL tifiles_fp_resize_array(FlashPage** array, unsigned int nelts)
438 {
439 	FlashPage ** ptr = g_realloc(array, (nelts + 1) * sizeof(FlashPage *));
440 	if (ptr != NULL)
441 	{
442 		ptr[nelts] = NULL;
443 	}
444 	return ptr;
445 }
446 
447 /**
448  * tifiles_fp_delete:
449  * @ve: var entry.
450  *
451  * Free data buffer and the structure itself.
452  *
453  * Return value: none.
454  **/
tifiles_fp_delete(FlashPage * fp)455 TIEXPORT2 void TICALL tifiles_fp_delete(FlashPage* fp)
456 {
457 	if (fp != NULL)
458 	{
459 		g_free(fp->data);
460 		g_free(fp);
461 	}
462 	else
463 	{
464 		tifiles_critical("%s(NULL)", __FUNCTION__);
465 	}
466 }
467 
468 /**
469  * tifiles_fp_delete_array:
470  * @array: an NULL-terminated array of FlashPage structures.
471  *
472  * Free the whole array (data buffer, FlashPage structure and array itself).
473  *
474  * Return value: none.
475  **/
tifiles_fp_delete_array(FlashPage ** array)476 TIEXPORT2 void TICALL tifiles_fp_delete_array(FlashPage** array)
477 {
478 	FlashPage** ptr;
479 
480 	if (array != NULL)
481 	{
482 		for (ptr = array; *ptr; ptr++)
483 		{
484 			tifiles_fp_delete(*ptr);
485 		}
486 		g_free(array);
487 	}
488 	else
489 	{
490 		tifiles_critical("%s(NULL)", __FUNCTION__);
491 	}
492 }
493