summaryrefslogtreecommitdiff
path: root/src/analysis/db/misc/rlestr.h
blob: aa2aa7364b1af0215641c4519a6cecbb8321743e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

/* Chrysalide - Outil d'analyse de fichiers binaires
 * rlestr.h - prototypes pour l'encodage par plage unique d'une chaîne de caractères
 *
 * Copyright (C) 2014-2019 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 <http://www.gnu.org/licenses/>.
 */


#ifndef _ANALYSIS_DB_MISC_RLESTR_H
#define _ANALYSIS_DB_MISC_RLESTR_H


#include <stdbool.h>
#include <stdint.h>
#include <sys/types.h>


#include "../../../common/packed.h"
#include "../../../common/sqlite.h"



/* Informations de base pour tout élément ajouté */
typedef struct _rle_string
{
    union
    {
        char *data;                         /* Chaîne de caractères        */
        const char *cst_data;               /* Autre version de chaîne     */
    };

    uint16_t length;                        /* Taille de la chaîne         */
    bool dynamic;                           /* Type d'allocation utilisée  */

} rle_string;


#define setup_empty_rle_string(s) \
    init_static_rle_string(s, NULL);

/* Définit une représentation de chaîne de caractères. */
void init_dynamic_rle_string(rle_string *, char *);

/* Définit une représentation de chaîne de caractères constante. */
void init_static_rle_string(rle_string *, const char *);

/* Copie une chaîne de caractères existante. */
void dup_into_rle_string(rle_string *, const char *);

#define exit_rle_string(rle) unset_rle_string(rle)

#define get_rle_string(rle) (rle)->data

#define get_rle_length(rle) (rle)->length

#define is_rle_string_empty(rle) ((rle)->data == NULL)

/* Constitue une représentation de chaîne de caractères. */
void set_dynamic_rle_string(rle_string *, char *);

/* Constitue une représentation de chaîne de caractères stable. */
void set_static_rle_string(rle_string *, const char *);

/* Libère la mémoire associée à la représentation. */
void unset_rle_string(rle_string *);

/* Effectue la comparaison entre deux chaînes de caractères. */
int cmp_rle_string(const rle_string *, const rle_string *);

/* Importe la définition d'une chaîne de caractères. */
bool unpack_rle_string(rle_string *, packed_buffer_t *);

/* Exporte la définition d'une chaîne de caractères. */
bool pack_rle_string(const rle_string *, packed_buffer_t *);



/* --------------------- MANIPULATIONS AVEC UNE BASE DE DONNEES --------------------- */


/* Définition du tronc commun pour les créations SQLite */
#define SQLITE_RLESTR_CREATE(n)     \
    n " TEXT"

/* Charge les valeurs utiles pour une chaîne de caractères. */
bool load_rle_string(rle_string *, const char *, const bound_value *, size_t);

/* Constitue les champs destinés à une insertion / modification. */
bool store_rle_string(const rle_string *, const char *, bound_value **, size_t *);



#endif  /* _ANALYSIS_DB_MISC_RLESTR_H */