/* Chrysalide - Outil d'analyse de fichiers binaires
* szbin.h - prototypes pour une manipulation de données accompagnées d'une taille
*
* Copyright (C) 2024 Cyrille Bagard
*
* This file is part of Chrysalide.
*
* Chrysalide is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Chrysalide is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chrysalide. If not, see .
*/
#ifndef _COMMON_SZBIN_H
#define _COMMON_SZBIN_H
#include
#include
/* Structure associant données et taille */
typedef struct _sized_binary_t
{
union {
const char *static_data; /* Données non modifiées */
char *data; /* Chaîne de caractères */
};
size_t size; /* Taille correspondante */
} sized_binary_t;
#define init_sized_binary(sb) \
do \
{ \
(sb)->data = NULL; \
(sb)->size = 0; \
} \
while (0)
#define setup_sized_binary(sb, s) \
do \
{ \
(sb)->data = malloc(s); \
(sb)->size = s; \
} \
while (0)
#define dup_into_sized_binary(sb, d, s) \
do \
{ \
setup_sized_binary(sb, s); \
memcpy((sb)->data, d, s); \
} \
while (0)
#define exit_sized_binary(sb) \
do \
{ \
if ((sb)->data != NULL) \
{ \
free((sb)->data); \
init_sized_binary(sb); \
} \
} \
while (0)
#endif /* _COMMON_SZBIN_H */