summaryrefslogtreecommitdiff
path: root/tools/d2c/bits/manager.c
blob: 560bade302a13347989d53ec44ac12ecb1d020c2 (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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430

/* Chrysalide - Outil d'analyse de fichiers binaires
 * manager.c - compréhension et manipulation des champs de bits
 *
 * Copyright (C) 2016-2017 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "manager.h"


#include <assert.h>
#include <inttypes.h>
#include <malloc.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>


#include "../helpers.h"



/* --------------------------- GESTION DES CHAMPS DE BITS --------------------------- */


/* Elément d'un mot décodé */
struct _raw_bitfield
{
    char *name;                             /* Désignation humaine         */
    unsigned int start;                     /* Position de départ          */
    unsigned int length;                    /* Taille du champ             */

    bool used;                              /* Champ défini & utilisé      */

};




/* Représentation de l'ensemble des bits de codage */
struct _coding_bits
{
    raw_bitfield *fields;                   /* Champs de bits détectés     */
    size_t bf_count;                        /* Nombre de ces champs        */
    uint64_t bits;                          /* Bits invariables            */
    uint64_t mask;                          /* Emplacement de ces bits     */
    unsigned int curpos;                    /* Position pendant l'analyse  */

};














/******************************************************************************
*                                                                             *
*  Paramètres  : field = champ de bits à consulter.                           *
*                                                                             *
*  Description : Indique le nombre de bits utilisés par le champ.             *
*                                                                             *
*  Retour      : Nombre de bits considérés.                                   *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

unsigned int get_raw_bitfield_length(const raw_bitfield *field)
{
    return field->length;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : field = champ de bits à traiter.                             *
*                                                                             *
*  Description : Marque un champ de bits comme étant utile.                   *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void mark_raw_bitfield_as_used(raw_bitfield *field)
{
    field->used = true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : field = champ de bits à traiter.                             *
*                fd    = descripteur d'un flux ouvert en écriture.            *
*                                                                             *
*  Description : Imprime la désignation d'un champ de bits dans du code.      *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void write_raw_bitfield(const raw_bitfield *field, int fd)
{
    dprintf(fd, "raw_%s", field->name);

}










/******************************************************************************
*                                                                             *
*  Paramètres  : -                                                            *
*                                                                             *
*  Description : Crée un nouveau gestionnaire des bits d'encodage brut.       *
*                                                                             *
*  Retour      : Nouvelle structure prête à emploi.                           *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

coding_bits *create_coding_bits(void)
{
    coding_bits *result;                    /* Définition vierge à renvoyer*/

    result = (coding_bits *)calloc(1, sizeof(coding_bits));

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bits = gestionnaire d'un ensemble de bits à libérer.         *
*                                                                             *
*  Description : Supprime de la mémoire un gestionnaire de bits d'encodage.   *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void delete_coding_bits(coding_bits *bits)
{
    size_t i;                               /* Boucle de parcours          */

    for (i = 0; i < bits->bf_count; i++)
        free(bits->fields[i].name);

    if (bits->fields != NULL)
        free(bits->fields);

    free(bits);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bits   = gestionnaire de bits d'encodage brut à consulter.   *
*                name   = désignation humaine du champ remarqué.              *
*                length = taille du champ à mémoriser.                        *
*                                                                             *
*  Description : Note la présence d'un champ remarquable dans une définition. *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void register_named_field_in_bits(coding_bits *bits, char *name, unsigned int length)
{
    raw_bitfield *field;                    /* Nouveau champ à constituer  */

    assert((bits->curpos + length) < 64);

    bits->fields = (raw_bitfield *)realloc(bits->fields,
                                              ++bits->bf_count * sizeof(raw_bitfield));

    field = &bits->fields[bits->bf_count - 1];

    field->name = make_string_lower(name);
    field->start = bits->curpos;
    field->length = length;

    bits->curpos += length;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bits = gestionnaire de bits d'encodage brut à consulter.     *
*                val  = valeur du bit à prendre en compte.                    *
*                                                                             *
*  Description : Note la présence d'un bit invariable dans une définition.    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void register_bit_in_bits(coding_bits *bits, int val)
{
    assert(bits->curpos < 64);

    bits->bits |= (val ? 1 : 0) << bits->curpos;
    bits->mask |= 1 << bits->curpos;

    bits->curpos++;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bits = gestionnaire de bits d'encodage brut à consulter.     *
*                                                                             *
*  Description : Indique le nombre de bits traités.                           *
*                                                                             *
*  Retour      : Quantité, positive ou nulle.                                 *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

unsigned int count_coded_bits(const coding_bits *bits)
{
    return bits->curpos;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bits = gestionnaire d'encodage brut à consulter.             *
*                name = désignation humaine du champ à retrouver.             *
*                                                                             *
*  Description : Recherche un champ donné dans un ensemble de champs de bits. *
*                                                                             *
*  Retour      : Structure associée au champ trouvé ou NULL en cas d'échec.   *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

raw_bitfield *find_named_field_in_bits(const coding_bits *bits, const char *name)
{
    raw_bitfield *result;                   /* Champ de bits à retourner   */
    size_t i;                               /* Boucle de parcours          */

    result = NULL;

    for (i = 0; i < bits->bf_count && result == NULL; i++)
        if (strcmp(bits->fields[i].name, name) == 0)
            result = &bits->fields[i];

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : spec = spécification servant de base à l'opération.          *
*                fd   = descripteur d'un flux ouvert en écriture.             *
*                wide = taille des mots manipulés (en bits).                  *
*                                                                             *
*  Description : Déclare les variables C associées aux champs de bits.        *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool declare_used_bits_fields(const coding_bits *bits, int fd)
{
    unsigned int wide;                      /* Taille des mots             */
    size_t i;                               /* Boucle de parcours          */
    off_t start;                            /* Point de départ dans le code*/
    off_t end;                              /* Point d'arrivée dans le code*/

    wide = count_coded_bits(bits);

    for (i = 0; i < bits->bf_count; i++)
        if (bits->fields[i].used)
        {
            start = lseek(fd, 0, SEEK_CUR);

            dprintf(fd, "\tuint%u_t ", wide);
            write_raw_bitfield(&bits->fields[i], fd);
            dprintf(fd, ";");

            end = lseek(fd, 0, SEEK_CUR);

            dprintf(fd, "%*s", 42 - (int)(end - start), "/");
            dprintf(fd, "* Champ brut à décoder        */\n");

        }

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : spec = spécification servant de base à l'opération.          *
*                fd   = descripteur d'un flux ouvert en écriture.             *
*                                                                             *
*  Description : Vérifie que les bits fixes correspondent au masque attendu.  *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool check_bits_correctness(const coding_bits *bits, int fd)
{
    switch (bits->curpos)
    {
        case 8:
            dprintf(fd, "\tif ((raw & 0x%" PRIx8 ") != 0x%" PRIx8 ") return NULL;\n",
                    (uint8_t)bits->mask, (uint8_t)bits->bits);
            break;

        case 16:
            dprintf(fd, "\tif ((raw & 0x%" PRIx16 ") != 0x%" PRIx16 ") return NULL;\n",
                    (uint16_t)bits->mask, (uint16_t)bits->bits);
            break;

        case 32:
            dprintf(fd, "\tif ((raw & 0x%" PRIx32 ") != 0x%" PRIx32 ") return NULL;\n",
                    (uint32_t)bits->mask, (uint32_t)bits->bits);
            break;

        case 64:
            dprintf(fd, "\tif ((raw & 0x%" PRIx64 ") != 0x%" PRIx64 ") return NULL;\n",
                    bits->mask, bits->bits);
            break;

    }

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : spec = spécification servant de base à l'opération.          *
*                fd   = descripteur d'un flux ouvert en écriture.             *
*                                                                             *
*  Description : Définit les variables C associées aux champs de bits.        *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool define_used_bits_fields(const coding_bits *bits, int fd)
{
    bool got_one;                           /* Suit le nombre d'impressions*/
    size_t i;                               /* Boucle de parcours          */
    raw_bitfield *rf;                       /* Accès confortable à un champ*/

    got_one = false;

    for (i = 0; i < bits->bf_count; i++)
    {
        rf = &bits->fields[i];
        if (!rf->used) continue;

        dprintf(fd, "\t");
        write_raw_bitfield(rf, fd);
        dprintf(fd, " = (raw >> %u) & 0x%llx;\n", rf->start, (1ull << rf->length) - 1);

        got_one = true;

    }

    if (got_one)
        dprintf(fd, "\n");

    return true;

}