summaryrefslogtreecommitdiff
path: root/src/common/bits.c
blob: 318b05746792a740a2498806f337b2917e957eb4 (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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731

/* Chrysalide - Outil d'analyse de fichiers binaires
 * bits.c - manipulation d'un champ de bits quelconque
 *
 * Copyright (C) 2015 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 "bits.h"


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



/* ----------------------------- CHAMPS DE BITS SIMPLES ----------------------------- */


/* Champ de bits simple */
struct _bitfield_t
{
    size_t length;                          /* Nombre de bits représentés  */
    size_t requested;                       /* Nombre de mots alloués      */

    void *tail;                             /* Limite du tableau de bits   */

    GMutex mutex;                           /* Garantie d'atomicité        */
    unsigned long bits[0];                  /* Mémoire d'accès associée    */

};


/* Crée un champ de bits initialisé à zéro. */
static bitfield_t *_create_bit_field(size_t, bool, size_t);

/* Crée une copie de champ de bits initialisé à zéro. */
static bitfield_t *_create_bit_field_from(const bitfield_t *, bool, size_t);

/* Crée une copie d'un champ de bits classique. */
static bitfield_t *_dup_bit_field(const bitfield_t *, size_t);



/* ---------------------------------------------------------------------------------- */
/*                               CHAMPS DE BITS SIMPLES                               */
/* ---------------------------------------------------------------------------------- */


/******************************************************************************
*                                                                             *
*  Paramètres  : length = nom de bits du champ à représenter.                 *
*                state  = état initial de chaque des bits.                    *
*                extra  = espace mémoire supplémentaire à ajouter au final.   *
*                                                                             *
*  Description : Crée un champ de bits initialisé à zéro.                     *
*                                                                             *
*  Retour      : Champ de bits mis en place.                                  *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bitfield_t *_create_bit_field(size_t length, bool state, size_t extra)
{
    bitfield_t *result;                     /* Création à retourner        */
    size_t requested;                       /* Nombre de mots à allouer    */
    size_t base;                            /* Allocation de base en octets*/

    requested = length / sizeof(unsigned long);
    if (length % sizeof(unsigned long) != 0) requested++;

    base = sizeof(bitfield_t) + requested * sizeof(unsigned long);

    result = (bitfield_t *)malloc(base + extra);

    result->length = length;
    result->requested = requested;

    result->tail = ((char *)result) + base;

    g_mutex_init(&result->mutex);

    if (state)
        set_all_in_bit_field(result);
    else
        reset_all_in_bit_field(result);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : length = nom de bits du champ à représenter.                 *
*                state  = état initial de chaque des bits.                    *
*                                                                             *
*  Description : Crée un champ de bits initialisé.                            *
*                                                                             *
*  Retour      : Champ de bits mis en place.                                  *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bitfield_t *create_bit_field(size_t length, bool state)
{
    return _create_bit_field(length, state, 0);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : field = champde bits à prendre pour modèle.                  *
*                state = état initial de chaque des bits.                     *
*                extra = espace mémoire supplémentaire à ajouter au final.    *
*                                                                             *
*  Description : Crée une copie de champ de bits initialisé à zéro.           *
*                                                                             *
*  Retour      : Champ de bits mis en place.                                  *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bitfield_t *_create_bit_field_from(const bitfield_t *field, bool state, size_t extra)
{
    return _create_bit_field(field->length, state, extra);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : field = champde bits à prendre pour modèle.                  *
*                state = état initial de chaque des bits.                     *
*                                                                             *
*  Description : Crée une copie de champ de bits initialisé à zéro.           *
*                                                                             *
*  Retour      : Champ de bits mis en place.                                  *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bitfield_t *create_bit_field_from(const bitfield_t *field, bool state)
{
    return _create_bit_field_from(field, state, 0);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : field = champ de bits à effacer.                             *
*                                                                             *
*  Description : Supprime de la mémoire un champ de bits donné.               *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void delete_bit_field(bitfield_t *field)
{
    g_mutex_clear(&field->mutex);

    free(field);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : dest = champ de bits à modifier.                             *
*                src  = champ de bits à utiliser pour l'opération.            *
*                                                                             *
*  Description : Copie un champ de bits dans un autre.                        *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void copy_bit_field(bitfield_t *dest, const bitfield_t *src)
{
    assert(dest->length == src->length);

    memcpy(dest->bits, src->bits, dest->requested * sizeof(unsigned long));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : field = champ de bits à dupliquer.                           *
*                extra = espace mémoire supplémentaire à ajouter au final.    *
*                                                                             *
*  Description : Crée une copie d'un champ de bits classique.                 *
*                                                                             *
*  Retour      : Champ de bits mis en place.                                  *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bitfield_t *_dup_bit_field(const bitfield_t *field, size_t extra)
{
    bitfield_t *result;                     /* Copie à retourner           */
    size_t requested;                       /* Nombre de mots à allouer    */

    result = _create_bit_field(field->length, false, extra);

    requested = field->length / sizeof(unsigned long);
    if (field->length % sizeof(unsigned long) != 0) requested++;

    memcpy(result->bits, field->bits, requested * sizeof(unsigned long));

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : field = champ de bits à dupliquer.                           *
*                                                                             *
*  Description : Crée une copie d'un champ de bits classique.                 *
*                                                                             *
*  Retour      : Champ de bits mis en place.                                  *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bitfield_t *dup_bit_field(const bitfield_t *field)
{
    return _dup_bit_field(field, 0);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : field = champ de bits à modifier.                            *
*                                                                             *
*  Description : Bascule à 0 un champ de bits dans son intégralité.           *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void reset_all_in_bit_field(bitfield_t *field)
{
    memset(field->bits, 0u, field->requested * sizeof(unsigned long));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : field = champ de bits à modifier.                            *
*                                                                             *
*  Description : Bascule à 1 un champ de bits dans son intégralité.           *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void set_all_in_bit_field(bitfield_t *field)
{
    memset(field->bits, ~0u, field->requested * sizeof(unsigned long));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : field = champ de bits à modifier.                            *
*                first = indice du premier bit à traiter.                     *
*                count = nombre de bits à marquer.                            *
*                                                                             *
*  Description : Bascule à 1 une partie d'un champ de bits.                   *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void set_in_bit_field(bitfield_t *field, size_t first, size_t count)
{
    size_t last;                            /* Point d'arrêt de la boucle  */
    size_t i;                               /* Boucle de parcours          */
    size_t index;                           /* Cellule de tableau visée    */
    size_t remaining;                       /* Nombre de bits restants     */

    last = first + count;

    assert(last <= field->length);

    for (i = first; i < last; i++)
    {
        index = i / (sizeof(unsigned long) * 8);
        remaining = i % (sizeof(unsigned long) * 8);

        field->bits[index] |= (1ul << remaining);

    }

}


/******************************************************************************
*                                                                             *
*  Paramètres  : field = champ de bits à modifier.                            *
*                first = indice du premier bit à traiter.                     *
*                count = nombre de bits à marquer.                            *
*                                                                             *
*  Description : Bascule à 1 de façon atomique une partie d'un champ de bits. *
*                                                                             *
*  Retour      : true si la zone traitée était entièrement vierge.            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool set_atomic_in_bit_field(bitfield_t *field, size_t first, size_t count)
{
    bool result;                            /* Bilan à retourner           */
    size_t start;                           /* Mot de départ               */
    size_t end;                             /* Mot d'arrivée               */
    size_t remaining;                       /* Nombre de bits restants     */
    unsigned long mask;                     /* Nouvelle valeur à insérer   */
    unsigned long oldval;                   /* Ancienne valeur présente    */

    start = first / (sizeof(unsigned long) * 8);
    end = (first + count - 1) / (sizeof(unsigned long) * 8);

    if (start == end)
    {
        remaining = first % (sizeof(unsigned long) * 8);

        assert(count > 0 && count <= (sizeof(unsigned long) * 8));

        if (count == 64)
        {
            assert(remaining == 0);
            mask = ~0lu;
        }
        else
        {
            mask = (1lu << count) - 1;
            mask <<= remaining;
        }

        /**
         * Pour une raison inconnue, l'appel suivant est parfois sans effet :
         *
         *    oldval = g_atomic_pointer_or(&field->bits[start], mask);
         *
         * On bascule donc dans l'équivalent plus long à exécuter...
         */

        g_mutex_lock(&field->mutex);

        oldval = field->bits[start];

        field->bits[start] |= mask;

        g_mutex_unlock(&field->mutex);

        result = ((oldval & mask) == 0);

        assert(test_in_bit_field(field, first, count));

    }
    else
    {
        g_mutex_lock(&field->mutex);

        result = !test_in_bit_field(field, first, count);

        if (result)
            set_in_bit_field(field, first, count);

        g_mutex_unlock(&field->mutex);

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : dest = champ de bits à modifier.                             *
*                src  = champ de bits à utiliser pour l'opération.            *
*                                                                             *
*  Description : Réalise une opération ET logique entre deux champs de bits.  *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void and_bit_field(bitfield_t *dest, const bitfield_t *src)
{
    size_t i;                               /* Boucle de parcours          */

    assert(dest->length == src->length);

    for (i = 0; i < dest->requested; i++)
        dest->bits[i] &= src->bits[i];

}


/******************************************************************************
*                                                                             *
*  Paramètres  : dest = champ de bits à modifier.                             *
*                src  = champ de bits à utiliser pour l'opération.            *
*                                                                             *
*  Description : Réalise une opération OU logique entre deux champs de bits.  *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void or_bit_field(bitfield_t *dest, const bitfield_t *src)
{
    size_t i;                               /* Boucle de parcours          */

    assert(dest->length == src->length);

    for (i = 0; i < dest->requested; i++)
        dest->bits[i] |= src->bits[i];

}


/******************************************************************************
*                                                                             *
*  Paramètres  : field = champ de bits à modifier.                            *
*                first = indice du premier bit à traiter.                     *
*                count = nombre de bits à marquer.                            *
*                                                                             *
*  Description : Détermine si un bit est à 1 dans un champ de bits.           *
*                                                                             *
*  Retour      : true si le bit correspondant est à l'état haut.              *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool test_in_bit_field(const bitfield_t *field, size_t first, size_t count)
{
    bool result;                            /* Valeur retrouvée à renvoyer */
    size_t last;                            /* Point d'arrêt de la boucle  */
    size_t i;                               /* Boucle de parcours          */
    size_t index;                           /* Cellule de tableau visée    */
    size_t remaining;                       /* Nombre de bits restants     */

    last = first + count;

    assert(last <= field->length);

    result = true;

    for (i = first; i < last && result; i++)
    {
        index = i / (sizeof(unsigned long) * 8);
        remaining = i % (sizeof(unsigned long) * 8);

        result = field->bits[index] & (1ul << remaining);

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : a = premier champ de bits à comparer.                        *
*                b = second champ de bits à comparer.                         *
*                                                                             *
*  Description : Indique si deux champs de bits sont identiques ou non.       *
*                                                                             *
*  Retour      : true si les champs de bits sont égaux ou false sinon.        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool is_bit_field_equal_to(const bitfield_t *a, const bitfield_t *b)
{
    bool result;                            /* Résultat à retourner        */
    size_t i;                               /* Boucle de parcours          */
    size_t remaining;                       /* Nombre de bits restants     */

    if (a->length != b->length)
        return false;

    result = true;

    for (i = 0; i < (a->requested - 1) && result; i++)
        result = (a->bits[i] == b->bits[i]);

    if (result)
    {
        remaining = a->length % sizeof(unsigned long);

        if (remaining == 0)
            result = (a->bits[i] == b->bits[i]);

        else
        {
            remaining = (1 << (remaining + 1)) - 1;

            result = ((a->bits[i] & remaining) == (b->bits[i] & remaining));

        }

    }

    return result;

}




unsigned long gfw(const bitfield_t *field)
{
    return field->bits[0];

}

#if 0

/* ---------------------------------------------------------------------------------- */
/*                           CHAMPS LIES À UNE ZONE MEMOIRE                           */
/* ---------------------------------------------------------------------------------- */


/******************************************************************************
*                                                                             *
*  Paramètres  : range = espace mémoire à couvrir.                            *
*                state = état initial de chaque des bits.                     *
*                                                                             *
*  Description : Crée un champ de bits couvrant une zone mémoire.             *
*                                                                             *
*  Retour      : Champ de bits mis en place.                                  *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

memfield_t *create_mem_field(const mrange_t *range, bool state)
{
    bitfield_t *result;                     /* Création à retourner        */

    result = _create_bit_field(get_mrange_length(range), state, sizeof(vmpa2t));

    copy_vmpa((vmpa2t *)result->tail, get_mrange_addr(range));

    return (memfield_t *)result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : range = espace mémoire à couvrir.                            *
*                                                                             *
*  Description : Crée une copie de champ de bits couvrant une zone mémoire.   *
*                                                                             *
*  Retour      : Champ de bits mis en place.                                  *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

memfield_t *create_mem_field_from(const memfield_t *field)
{
    bitfield_t *result;                     /* Création à retourner        */

    result = _create_bit_field_from((bitfield_t *)field, false, sizeof(vmpa2t));

    copy_vmpa((vmpa2t *)result->tail, (vmpa2t *)field->tail);

    return (memfield_t *)result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : field = champ de bits à effacer.                             *
*                                                                             *
*  Description : Supprime de la mémoire un champ de bits donné.               *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void delete_mem_field(memfield_t *field)
{
    delete_bit_field((bitfield_t *)field);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : field = champ de bits à dupliquer.                           *
*                                                                             *
*  Description : Crée une copie d'un champ de bits couvrant une zone mémoire. *
*                                                                             *
*  Retour      : Champ de bits mis en place.                                  *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

memfield_t *dup_mem_field(const memfield_t *field)
{
    bitfield_t *result;                     /* Création à retourner        */

    result = _dup_bit_field((bitfield_t *)field, sizeof(vmpa2t));

    copy_vmpa((vmpa2t *)result->tail, (vmpa2t *)field->tail);

    return (memfield_t *)result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : field = champ de bits à modifier.                            *
*                addr  = emplacement en mémoire à traiter.                    *
*                                                                             *
*  Description : Bascule à 1 un bit d'un champ de bits.                       *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void set_in_mem_field(memfield_t *field, const vmpa2t *addr)
{
    vmpa2t *start;                          /* Adresse de début            */
    phys_t offset;                          /* Décallage de départ         */

    start = (vmpa2t *)field->tail;

    assert(cmp_vmpa(start, addr) <= 0);

    offset = compute_vmpa_diff(start, addr);

    set_in_bit_field((bitfield_t *)field, offset, 1);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : field = champ de bits à modifier.                            *
*                addr  = emplacement en mémoire à tester.                     *
*                                                                             *
*  Description : Détermine si un bit est à 1 dans un champ de bits.           *
*                                                                             *
*  Retour      : true si le bit correspondant est à l'état haut.              *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool test_in_mem_field(memfield_t *field, const vmpa2t *addr)
{
    bool result;                            /* Valeur retrouvée à renvoyer */
    vmpa2t *start;                          /* Adresse de début            */
    phys_t offset;                          /* Décallage de départ         */

    start = (vmpa2t *)field->tail;

    assert(cmp_vmpa(start, addr) <= 0);

    offset = compute_vmpa_diff(start, addr);

    result = test_in_bit_field((bitfield_t *)field, offset, 1);

    return result;

}
#endif