summaryrefslogtreecommitdiff
path: root/src/analysis/types/basic.c
blob: dcd863465a6c7e1150e48aa63ee3d5ea17eef6c1 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345

/* Chrysalide - Outil d'analyse de fichiers binaires
 * basic.c - manipulation des types de données de base
 *
 * Copyright (C) 2012 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  OpenIDA 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.
 *
 *  OpenIDA 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "basic.h"


#include <malloc.h>
#include <string.h>


#include "../type-int.h"



/* Description de type basique (instance) */
struct _GBasicType
{
    GDataType parent;                       /* A laisser en premier        */

    BaseType type;                          /* Type représenté si connu    */

};

/* Description de type basique (classe) */
struct _GBasicTypeClass
{
    GDataTypeClass parent;                  /* A laisser en premier        */

};


/* Initialise la classe des types basiques. */
static void g_basic_type_class_init(GBasicTypeClass *);

/* Initialise l'instance d'un type basique. */
static void g_basic_type_init(GBasicType *);

/* Crée un copie d'un type existant. */
static GDataType *g_basic_type_dup(const GBasicType *);

/* Décrit le type fourni sous forme de caractères. */
static char *g_basic_type_to_string(const GBasicType *);

/* Procède à l'impression de la description d'un type. */
static void g_basic_type_output(const GBasicType *, GLangOutput *, GBufferLine *, bool, bool);



/* Indique le type défini pour un type basique. */
G_DEFINE_TYPE(GBasicType, g_basic_type, G_TYPE_DATA_TYPE);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des types basiques.                     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_basic_type_class_init(GBasicTypeClass *klass)
{

}


/******************************************************************************
*                                                                             *
*  Paramètres  : type = instance à initialiser.                               *
*                                                                             *
*  Description : Initialise l'instance d'un type basique.                     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_basic_type_init(GBasicType *type)
{
    GDataType *data_type;                   /* Version basique             */

    data_type = G_DATA_TYPE(type);

    data_type->dup = (type_dup_fc)g_basic_type_dup;
    data_type->to_string = (type_to_string_fc)g_basic_type_to_string;
    data_type->output = (output_type_fc)g_basic_type_output;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : type = type simple à représenter.                            *
*                                                                             *
*  Description : Crée une représentation de type basique.                     *
*                                                                             *
*  Retour      : Adresse de la structure mise en place.                       *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GDataType *g_basic_type_new(BaseType type)
{
    GBasicType *result;                 /* Structure à retourner       */

    result = g_object_new(G_TYPE_BASIC_TYPE, NULL);

    result->type = type;

    return G_DATA_TYPE(result);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : type = type à dupliquer.                                     *
*                                                                             *
*  Description : Crée un copie d'un type existant.                            *
*                                                                             *
*  Retour      : Nouvelle instance de type identique à celle fournie.         *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static GDataType *g_basic_type_dup(const GBasicType *type)
{
    return g_basic_type_new(type->type);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : type = type à convertir.                                     *
*                                                                             *
*  Description : Décrit le type fourni sous forme de caractères.              *
*                                                                             *
*  Retour      : Chaîne à libérer de la mémoire après usage.                  *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static char *g_basic_type_to_string(const GBasicType *type)
{
    const char *desc;                       /* Représentation à copier     */

    switch (type->type)
    {
        case BTP_VOID:
            desc = "void";
            break;

        case BTP_WCHAR_T:
            desc = "wchar_t";
            break;

        case BTP_BOOL:
            desc = "bool";
            break;

        case BTP_CHAR:
            desc = "char";
            break;

        case BTP_SCHAR:
            desc = "signed char";
            break;

        case BTP_UCHAR:
            desc = "unsigned char";
            break;

        case BTP_SHORT:
            desc = "short";
            break;

        case BTP_USHORT:
            desc = "unsigned short";
            break;

        case BTP_INT:
            desc = "int";
            break;

        case BTP_UINT:
            desc = "unsigned int";
            break;

        case BTP_LONG:
            desc = "long";
            break;

        case BTP_ULONG:
            desc = "unsigned long";
            break;

        case BTP_LONG_LONG:
            desc = "long long";
            break;

        case BTP_ULONG_LONG:
            desc = "unsigned long long";
            break;

        case BTP_INT128:
            desc = "__int128";
            break;

        case BTP_UINT128:
            desc = "unsigned __int128";
            break;

        case BTP_FLOAT:
            desc = "float";
            break;

        case BTP_DOUBLE:
            desc = "double";
            break;

        case BTP_LONG_DOUBLE:
            desc = "long double";
            break;

        case BTP_FLOAT128:
            desc = "__float128";
            break;

        case BTP_ELLIPSIS:
            desc = "...";
            break;

        case BTP_754R_64:
            desc = "__float754r_64";
            break;

        case BTP_754R_128:
            desc = "__float754r_128";
            break;

        case BTP_754R_32:
            desc = "__float754r_32";
            break;

        case BTP_754R_16:
            desc = "__float754r_16";
            break;

        case BTP_CHAR32_T:
            desc = "char32_t";
            break;

        case BTP_CHAR16_T:
            desc = "char16_t";
            break;

        default:
        case BTP_OTHER:
            desc = "user";
            break;

    }

    return strdup(desc);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : type   = type à afficher.                                    *
*                lang   = langage à utiliser pour la sortie humaine.          *
*                buffer = tampon mis à disposition pour la sortie.            *
*                info   = nature du cadre de destination.                     *
*                full   = besoin de descriptions étendues ?                   *
*                                                                             *
*  Description : Procède à l'impression de la description d'un type.          *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_basic_type_output(const GBasicType *type, GLangOutput *lang, GBufferLine *line, bool info, bool full)
{
    char *text;                             /* Version humaine à imprimer  */
    size_t len;                             /* Taille de cette version     */

    text = g_basic_type_to_string(type);
    len = strlen(text);

    g_buffer_line_append_text(line, BLC_LAST_USED, text, len, info ? RTT_COMMENT : RTT_RAW, NULL);

    free(text);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : type = type à consulter.                                     *
*                                                                             *
*  Description : Fournit le type de base géré par le type.                    *
*                                                                             *
*  Retour      : Type basique.                                                *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

BaseType g_basic_type_get_btype(const GBasicType *type)
{
    return type->type;

}