Bonjour,

Voila j'ai fait ce code sur la compression/décompression de Huffman il marche parfaitement seulement voila quand je lui passe un petit coup de Valgrind il me met ce message:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
==4429== Invalid free() / delete / delete[]
==4429==    at 0x4025DFA: free (vg_replace_malloc.c:323)
==4429==    by 0x80488D6: main (in /home/scary/Documents/huffman)
==4429==  Address 0xbe8c29f8 is on thread 1's stack
==4429==
==4429== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 17 from 1)
==4429== malloc/free: in use at exit: 0 bytes in 0 blocks.
==4429== malloc/free: 256 allocs, 257 frees, 7,776 bytes allocated.
==4429== For counts of detected errors, rerun with: -v
==4429== All heap blocks were freed -- no leaks are possible.
Apparemment je libère plus que je n'alloue (ce qui est un soucis) mais malheureusement je n'arrive absolument pas à voir où est le soucis dans le code. Voici le code:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
typedef unsigned char uchar;
typedef unsigned int uint;
typedef struct _arbre *ptr_arbre;
typedef struct _list *ptr_list;
 
typedef struct _arbre
{
    /* donnees */
    uchar c;
    uint freq;
    uint code_huff;
    uchar nbr_bits;
    /* les fils */
    ptr_arbre fils_d;
    ptr_arbre fils_g;
}h_arbre;
 
typedef struct _list
{
    ptr_arbre noeud;
    ptr_list suiv;
}list;
 
typedef struct _ch_context
{
    FILE *fin;
    FILE *fout;
    long fsize;
    uint nbr_elem;
    ptr_arbre r_arbre;
    ptr_arbre car_table;
}ch_context;
 
typedef struct _dh_context
{
    FILE *fin;
    FILE *fout;
    long fsize;
    uint nbr_elem;
    ptr_arbre r_arbre;
}dh_context;
 
long get_file_size (const char *file);
void ch_free_arbre (ptr_arbre arbre);
void ch_free_context (ch_context *ct);
int ch_inserer (ptr_list *lst, ptr_arbre arbre);
int ch_calc_freq (ch_context *ct);
void ch_set_bits (ptr_arbre arbre, uint code_huff, uint nbr_bits);
int ch_creation_arbre (ch_context *ct);
int ch_ecriture_table (ch_context *ct);
ptr_arbre ch_get_arbre (ch_context *ct, uchar c);
int ch_compression (ch_context *ct);
int comp_huffman (const char *fsrc, const char *fdest);
void dh_free_arbre (ptr_arbre arbre);
void dh_free_context (dh_context *ct);
int dh_creation_arbre (dh_context *ct);
int dh_decompression (dh_context *ct);
int decomp_huffman (const char *fsrc, const char *fdest);
 
/*******************************************************
**************************main**************************
*******************************************************/
 
int main (int argc, char **argv)
{
    if ((argc != 4) || (argv[1][0] != '-') || ((argv[1][1] != 'c') && (argv[1][1] != 'd')))
    {
        printf ("Utilisation:\n");
        printf (" Huffman {-c, -d} src dest\n");
        printf (" -c : pour la compression\n");
        printf (" -d : pour la decompression\n");
        printf (" src : fichier source\n");
        printf (" dest : fichier destination\n");
    }
    else
    {
        char *fsrc = NULL, *fdest = NULL;
        char comp = argv[1][1];
 
        /* les deux fichiers */
        fsrc = argv[2];
        fdest = argv[3];
        if (comp == 'c')
        {
            /* compression */
            if (comp_huffman (fsrc, fdest))
            {
                long insize = get_file_size (fsrc);
                long outsize = get_file_size (fdest);
 
                if (insize < 0)
                {
                    printf ("Echec lors du calcul de la taille original du fichier");
 
                    return 0;
                }
                if (outsize < 0)
                {
                    printf ("Echec lors du calcul de la taille compresse du fichier");
 
                    return 0;
                }
                printf ("Compression reussie\n");
                printf ("Taile du fichier non compressee : %ld (octets)\n", insize);
                printf ("Taile du fichier compressee : %ld (octets)\n", outsize);
                printf ("Taux de compression : %ld%%\n",((outsize - insize)*100)/outsize);
            }
            else
            {
                printf ("Erreur de compression\n");
            }
            free (fsrc);
            fsrc = NULL;
            fdest = NULL;
        }
        else
        {
            /* decompression */
            if (decomp_huffman (fsrc, fdest))
            {
                printf ("Decompression OK\n");
            }
            else
            {
                printf ("Erreur de decompression\n");
            }
            free (fsrc);
            fsrc = NULL;
            fdest = NULL;
        }
    }
 
    return 0;
}
 
long get_file_size (const char *file)
{
    long size;
    FILE *fp = fopen (file, "rb");
 
    if (!fp)
    {
        fprintf (stderr, "get_file_size: erreur lors de l'ouverture de file");
 
        return -1;
    }
    fseek (fp, 0, 2);
    size = ftell (fp);
    fclose (fp);
 
    return size;
}
 
void ch_free_arbre (ptr_arbre arbre)
{
    /* on libere tous les noeud sauf les feuille */
    if (arbre)
    {
        if (arbre->fils_g && arbre->fils_d)
        {
            ch_free_arbre (arbre->fils_g);
            ch_free_arbre (arbre->fils_d);
            free (arbre);
        }
    }
 
    return ;
}
 
void ch_free_context (ch_context *ct)
{
    ch_free_arbre (ct->r_arbre);
    if (ct->car_table)
    {
        free (ct->car_table);
    }
    if (ct->fin)
    {
        fclose (ct->fin);
    }
    if (ct->fout)
    {
        fclose (ct->fout);
    }
 
    return ;
}
 
int ch_inserer (ptr_list *lst, ptr_arbre arbre)
{
    if (*lst == 0)
    {
        (*lst) = malloc (sizeof (list));
 
        if (*lst == NULL)
        {
            free (*lst);
            fprintf (stderr, "ch_inserer: Allocation impossible pour *lst");
 
            return 0;
        }
        (*lst)->noeud = arbre;
        (*lst)->suiv = 0;
    }
    else
    {
        ptr_list p = (*lst);
 
        if (p->noeud->freq > arbre->freq)
        {
            ptr_list l = malloc (sizeof (list));
 
            if (l == NULL)
            {
                free (l);
                fprintf (stderr, "ch_inserer: Allocation impossible du if pour l");
 
                return 0;
            }
            l->noeud = arbre;
            l->suiv = (*lst);
            (*lst) = l;
        }
        else
        {
            ptr_list q = NULL, l = NULL;
 
            while (p)
            {
                if (p->noeud->freq > arbre->freq)
                {
                    break;
                }
                q = p;
                p = p->suiv;
            }
            l = malloc (sizeof (list));
            if (l == NULL)
            {
                free (l);
                l = NULL;
                fprintf (stderr, "ch_inserer: Allocation impossible du else pour l");
 
                return 0;
            }
            l->noeud = arbre;
            l->suiv = p;
            q->suiv = l;
        }
    }
 
    return 1;
}
 
int ch_calc_freq (ch_context *ct)
{
    uint *car_freq = NULL;
    int i, j;
 
    car_freq = malloc (256 * sizeof (uint));
 
    if (car_freq == NULL)
    {
        free (car_freq);
        car_freq = NULL;
        fprintf (stderr, "ch_calc_freq: Allocation impossible pour car_freq");
 
        return 0;
    }
    memset (car_freq, 0, 256 * sizeof (uint));
    rewind (ct->fin);
    /* etude statistique */
    while (!feof (ct->fin))
    {
        uchar c;
 
        fread (&c, 1, 1, ct->fin);
        if (car_freq[c] == 0)
        {
            (ct->nbr_elem)++;
        }
        car_freq[c] = car_freq[c] + 1;
    }
    /* creation de la table des caracteres du fichier */
    ct->car_table = (ptr_arbre)malloc ((ct->nbr_elem) * sizeof (h_arbre));
    if (ct->car_table == NULL)
    {
        free (ct->car_table);
        ct->car_table = NULL;
        fprintf (stderr, "ch_calc_freq: Allocation impossible pour ct->car_table");
 
        return 0;
    }
    for (i=0,j=0;i<256;i++)
    {
        if (car_freq[i] != 0)
        {
            ct->car_table[j].c = (uchar)i;
            ct->car_table[j].freq = car_freq[i];
            ct->car_table[j].code_huff = 0;
            ct->car_table[j].nbr_bits = 0;
            ct->car_table[j].fils_d = 0;
            ct->car_table[j].fils_g = 0;
            j++;
        }
    }
    free (car_freq);
 
    return 1;
}
 
void ch_set_bits (ptr_arbre arbre, uint code_huff, uint nbr_bits)
{
    if (arbre)
    {
        if (arbre->fils_d && arbre->fils_g)
        {
            ch_set_bits (arbre->fils_d, code_huff, nbr_bits + 1);
            ch_set_bits (arbre->fils_g, code_huff | (0x1 << nbr_bits), nbr_bits + 1);
        }
        else
        {
            arbre->code_huff = code_huff;
            arbre->nbr_bits = nbr_bits;
        }
    }
 
    return ;
}
 
int ch_creation_arbre (ch_context *ct)
{
    ptr_list lst = 0;
    uint i, nbr_elem = ct->nbr_elem;
 
    for (i=0;i<nbr_elem;i++)
    {
        if (!ch_inserer (&lst, &(ct->car_table[i])))
        {
            fprintf (stderr, "ch_creation_arbre: echec de ch_inserer");
 
            return 0;
        }
    }
    /* les elements sont classes par ordre croissant */
    /* maintenant on va grouper les deux premier */
    while (nbr_elem > 1)
    {
        ptr_arbre a, a1, a2;
        ptr_list p;
 
        p = lst;
        a1 = p->noeud;
        a2 = p->suiv->noeud;
        lst = p->suiv->suiv;
        free (p->suiv);
        free (p);
        a = malloc (sizeof (h_arbre));
 
        if (a == NULL)
        {
            free(a);
            a = NULL;
            fprintf (stderr, "ch_creation_arbre: Allocation impossible pour a");
 
            return 0;
        }
        a->c = -1;
        a->freq = a1->freq + a2->freq;
        a->code_huff = 0;
        a->fils_d = a1;
        a->fils_g = a2;
        if (!ch_inserer (&lst, a))
        {
            fprintf (stderr, "ch_creation_arbre: erreur lors de l'insertion");
 
            return 0;
        }
        nbr_elem--;
    }
    ct->r_arbre = lst->noeud;
    free (lst);
    /* calcul du codage pour chaque caractere */
    ch_set_bits (ct->r_arbre, 0, 0);
 
    return 1;
}
 
int ch_ecriture_table (ch_context *ct)
{
    uint i, nbr_elem = ct->nbr_elem;
 
    /* nombre de caracteres */
    if (!fwrite (&nbr_elem, sizeof (uint), 1, ct->fout))
    {
        fprintf (stderr, "ch_ecriture_table: Erreur ecriture nbr_elem");
 
        return 0;
    }
    /* la taille du fichier */
    if (!fwrite (&(ct->fsize), sizeof (uint), 1, ct->fout))
    {
        fprintf (stderr, "ch_ecriture_table: Erreur ecriture ct->fsize");
 
        return 0;
    }
    /* ecriture de la table */
    for (i= 0;i<nbr_elem;i++)
    {
        uchar c, nbr_bits;
        uint code_huff;
 
        /* le caractere */
        c = ct->car_table[i].c;
        if (!fwrite (&c, 1, 1, ct->fout))
        {
            fprintf (stderr, "ch_ecriture_table: Erreur ecriture c");
 
            return 0;
        }
        /* le nombre de bit necessaire */
        nbr_bits = ct->car_table[i].nbr_bits;
        if (!fwrite (&nbr_bits, 1, 1, ct->fout))
        {
            fprintf (stderr, "ch_ecriture_table: Erreur ecriture nbr_bits");
 
            return 0;
        }
        /* le code huffman */
        code_huff = ct->car_table[i].code_huff;
        if (nbr_bits <= 8)
        {
            if (!fwrite (&code_huff, 1, 1, ct->fout))
            {
                fprintf (stderr, "ch_ecriture_table: Erreur ecriture code_huff premier test");
 
                return 0;
            }
        }
        else if (nbr_bits <= 16)
        {
            if (!fwrite (&code_huff, 2, 1, ct->fout))
            {
                fprintf (stderr, "ch_ecriture_table: Erreur ecriture code_huff second test");
 
                return 0;
            }
        }
        else
        {
            if (!fwrite (&code_huff, 4, 1, ct->fout))
            {
                fprintf (stderr, "ch_ecriture_table: Erreur ecriture code_huff troisieme test");
 
                return 0;
            }
        }
    }
 
    return 1;
}
 
ptr_arbre ch_get_arbre (ch_context *ct, uchar c)
{
    ptr_arbre a;
    uint i;
 
    a = ct->car_table;
    i = ct->nbr_elem;
 
    while (i--)
    {
        if (a->c == c)
        {
            return a;
        }
        a++;
    }
 
    return NULL;
}
 
int ch_compression (ch_context *ct)
{
    ptr_arbre a;
    uchar ucar = 0, c, nbr_bits;
    uint code_huff, pc = 0, i;
 
    rewind (ct->fin);
    while (!feof (ct->fin))
    {
        fread (&c, 1, 1, ct->fin);
        a = ch_get_arbre (ct, c);
        code_huff = a->code_huff;
        nbr_bits = a->nbr_bits;
        for (i=0;i<nbr_bits;i++)
        {
            ucar = ucar << 1;
            if (code_huff & 0x1)
            {
                ucar = ucar | 0x1;
            }
            code_huff = code_huff >> 1;
            pc++;
            if (pc == 8)
            {
                if (!fwrite (&ucar, 1, 1, ct->fout))
                {
                    fprintf (stderr, "ch_compression: erreur ecriture ucar");
 
                    return 0;
                }
                pc = 0;
                ucar = 0;
            }
        }
    }
    if (pc != 0)
    {
        /* ecriture du dernier caractere */
        while (pc != 8)
        {
            ucar = ucar << 1;
            pc++;
        }
        if (!fwrite (&ucar, 1, 1, ct->fout))
        {
            fprintf (stderr, "ch_compression: erreur ecriture ucar");
 
            return 0;
        }
    }
 
    return 1;
}
 
int comp_huffman (const char *fsrc, const char *fdest)
{
    ch_context ct;
 
    memset (&ct, 0, sizeof (ch_context));
    ct.fin = fopen (fsrc, "rb");
    if (!(ct.fin))
    {
        return 0;
    }
    fseek (ct.fin, 0, 2);
    ct.fsize = ftell (ct.fin);
    /* calcule de la frequence */
    if (!ch_calc_freq (&ct))
    {
        ch_free_context (&ct);
        fprintf (stderr, "comp_huffman: erreur lors du calcul de la frequence");
 
        return 0;
    }
    /* creation de l'arbre de huffman */
    if (!ch_creation_arbre (&ct))
    {
        ch_free_context (&ct);
        fprintf (stderr, "comp_huffman: erreur lors de la creation de l'arbre");
 
        return 0;
    }
    /* pres pour la compression */
    ct.fout = fopen (fdest, "wb");
    if (!(ct.fout))
    {
        ch_free_context (&ct);
        fprintf (stderr, "comp_huffman: erreur lors de l'ouverture de fdest");
 
        return 0;
    }
    /* ecriture de la table */
    if (!ch_ecriture_table (&ct))
    {
        ch_free_context (&ct);
        fprintf (stderr, "comp_huffman: erreur lors de l'ecriture de la table");
 
        return 0;
    }
    /* compression des donnees */
    if (!ch_compression (&ct))
    {
        ch_free_context (&ct);
        fprintf (stderr, "comp_huffman: erreur lors de la compression des donnees");
 
        return 0;
    }
    ch_free_context (&ct);
 
    return 1;
}
 
void dh_free_arbre (ptr_arbre arbre)
{
    /* on libere tous les noeuds sauf les feuilles */
    if (arbre)
    {
        dh_free_arbre (arbre->fils_g);
        dh_free_arbre (arbre->fils_d);
        free (arbre);
    }
 
    return ;
}
 
void dh_free_context (dh_context *ct)
{
    dh_free_arbre (ct->r_arbre);
    if (ct->fin)
    {
        fclose (ct->fin);
    }
    if (ct->fout)
    {
        fclose (ct->fout);
    }
 
    return ;
}
 
int dh_creation_arbre (dh_context *ct)
{
    uint i, nbr_elem;
 
    /* nombre d'element */
    if (!fread (&nbr_elem, sizeof (uint), 1, ct->fin))
    {
        fprintf (stderr, "dh_creation_arbre: erreur de lecture nbr_elem");
 
        return 0;
    }
    /* la taille du fichier */
    if (!fread (&(ct->fsize), sizeof (uint), 1, ct->fin))
    {
        fprintf (stderr, "dh_creation_arbre: erreur de lecture ct->fsize");
 
        return 0;
    }
    /* allocation de la memoire */
    ct->nbr_elem = nbr_elem;
    ct->r_arbre = (ptr_arbre)malloc (sizeof (h_arbre));
    if (ct->r_arbre == NULL)
    {
        free (ct->r_arbre);
        ct->r_arbre = NULL;
        fprintf (stderr, "dh_creation_arbre: Allocation impossible pour ct->r_arbre");
 
        return 0;
    }
    ct->r_arbre->c = 0xFF;
    ct->r_arbre->fils_d = 0;
    ct->r_arbre->fils_g = 0;
    for (i=0;i<nbr_elem;i++)
    {
        uchar c, nbr_bits, j;
        uint code_huff = 0;
        ptr_arbre a;
 
        /* le caractere */
        if (!fread (&c, 1, 1, ct->fin))
        {
            fprintf (stderr, "dh_creation_arbre: erreur de lecture c");
 
            return 0;
        }
        /* le nombre de bit necessaire */
        if (!fread (&nbr_bits, 1, 1, ct->fin))
        {
            fprintf (stderr, "dh_creation_arbre: erreur de lecture nbr_bits");
 
            return 0;
        }
        /* le code huffman */
        if (nbr_bits <= 8)
        {
            if (!fread (&code_huff, 1, 1, ct->fin))
            {
                fprintf (stderr, "dh_creation_arbre: erreur de lecture code_huff pour le premier test");
 
                return 0;
            }
        }
        else if (nbr_bits <= 16)
        {
            if (!fread (&code_huff, 2, 1, ct->fin))
            {
                fprintf (stderr, "dh_creation_arbre: erreur de lecture code_huff pour le second test");
 
                return 0;
            }
        }
        else
        {
            if (!fread (&code_huff, 4, 1, ct->fin))
            {
                fprintf (stderr, "dh_creation_arbre: erreur de lecture code_huff pour le troisieme test");
 
                return 0;
            }
        }
        /* ajout a l'arbre de huffman */
        a = ct->r_arbre;
        for (j=0;j<nbr_bits;j++,code_huff>>=1)
        {
            if (code_huff & 0x1)
            {
                if (a->fils_g == 0)
                {
                    a->fils_g = malloc (sizeof (h_arbre));
                    if (a->fils_g == NULL)
                    {
                        free (a->fils_g);
                        a->fils_g = NULL;
                        fprintf (stderr, "dh_creation_arbre: Allocation impossible pour a->fils_g");
 
                        return 0;
                    }
                    a = a->fils_g;
                    a->c = -1;
                    a->fils_g = 0;
                    a->fils_d = 0;
                }
                else
                {
                    a = a->fils_g;
                }
            }
            else
            {
                if (a->fils_d == 0)
                {
                    a->fils_d = malloc (sizeof (h_arbre));
                    if (a->fils_d == NULL)
                    {
                        free (a->fils_d);
                        a->fils_d = NULL;
                        fprintf (stderr, "dh_creation_arbre: Allocation impossible pour a->fils_d");
 
                        return 0;
                    }
                    a = a->fils_d;
                    a->c = 0xFF;
                    a->fils_g = 0;
                    a->fils_d = 0;
                }
                else
                {
                    a = a->fils_d;
                }
            }
        }
        a->c = c;
    }
 
    return 1;
}
 
/* Fonction de decompression */
int dh_decompression (dh_context* ct)
{
    ptr_arbre a = ct->r_arbre;
    uint j, i = 0, fsize = ct->fsize;
    uchar c;
 
    while (!feof (ct->fin))
    {
        fread (&c, 1, 1, ct->fin);
        for (j=0;j<8;j++)
        {
            if ((c & 0x80) == 0x80) /* 0x80 = 10000000b */
            {
                a = a->fils_g;
            }
            else
            {
                a = a->fils_d;
            }
            c = c << 1;
            if ((a->fils_d == 0) && (a->fils_g == 0))
            {
                /* une feuille */
                if (!fwrite (&(a->c), 1, 1, ct->fout))
                {
                    fprintf (stderr, "dh_creation_arbre: erreur de ecriture a->c");
 
                    return 0;
                }
                if (++i == fsize)
                {
                    return 1;
                }
                a = ct->r_arbre;
            }
        }
    }
 
    return 0;
}
 
int decomp_huffman (const char *fsrc, const char *fdest)
{
    dh_context ct;
 
    memset (&ct, 0, sizeof (dh_context));
    ct.fin = fopen (fsrc, "rb");
    if (!(ct.fin))
    {
        fprintf (stderr, "decomp_huffman: erreur ouverture ct.fin");
 
        return 0;
    }
    /* recreation de l'arbre */
    if (!dh_creation_arbre (&ct))
    {
        dh_free_context (&ct);
        fprintf (stderr, "decomp_huffman: erreur lors de la recreation de l'arbre");
 
        return 0;
    }
    /* pres pour la decompression */
    ct.fout = fopen (fdest, "wb");
    if (!(ct.fout))
    {
        dh_free_context (&ct);
        fprintf (stderr, "decomp_huffman: erreur ouverture ct.fout");
 
        return 0;
    }
    /* decompression des donnees */
    if (!dh_decompression (&ct))
    {
        dh_free_context (&ct);
        fprintf (stderr, "decomp_huffman: erreur lors de la decompression des donnees");
 
        return 0;
    }
    dh_free_context (&ct);
 
    return 1;
}
D'ailleurs si vous avez des conseils d'amélioration surtout n'hésitez pas Merci d'avance.