]> git.llucax.com Git - z.facultad/75.06/jacu.git/blob - src/jacu.c
Me esta tirando un segmentation fault en el free(salida), si alguien lo puede ver...
[z.facultad/75.06/jacu.git] / src / jacu.c
1
2 #include "blocksorting/bs.h"
3 #include "mtf/mtf.h"
4 #include "zerogrouping/zerogrouping.h"
5 #include "statichuff/statichuff.h"
6 #include "vfile/vfile.h"
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <unistd.h>
10
11 long get_file_size(const char* filename);
12
13 int main(int argc, char* argv[])
14 {       
15         int cflag = 0;
16         int dflag = 0;
17         int zflag = 0;
18         int tflag = 0;
19         int pflag = 0;
20         long int volumesize = 0;
21         size_t pagesize = 32768; /* 32KB */
22         int ch;
23         t_BlockSort *bs;
24                         
25         while ((ch = getopt(argc, argv, "cdzt:p:")) != -1) { 
26                  
27                 switch (ch) { 
28                         case 'c': cflag = 1; 
29                                           break;
30
31                         case 'd': dflag = 1; 
32                                           break; 
33
34                         case 'z': zflag = 1; 
35                                           break; 
36
37                         case 't': tflag = 1; 
38                                           volumesize = atol(optarg);
39                                           break; 
40
41                         case 'p': pflag = 1; 
42                                           pagesize = atoi(optarg);
43                                           break; 
44
45                         default: fprintf(stderr, "Usage: %s [-cdpt] sourcefile targetfile\n", argv[0]); 
46                                          return(2);
47                 }
48         }
49                 
50         if ( (argc == 1) || (cflag & dflag) || !(cflag | dflag) || ((argc - optind) < 2) ) {
51                 fprintf(stderr, "Usage: %s [-cdt] sourcefile targetfile\n", argv[0]); 
52                 return (3);
53         }
54         if ((tflag) && (volumesize <= 0l)) {
55                 fprintf(stderr,"Error: The volume size must be a non-zero value\n");
56                 return (4);
57         }
58         if ((pflag) && (pagesize <= 1u)) {
59                 fprintf(stderr,"Error: El tamaño de página debe ser mayor a 1 byte.\n");
60                 return (5);
61         }
62                 
63         if (cflag == 1) {
64                 /* Comprimo */
65                 /* No me gusta el tmpfile ... es para probar como anda todo junto */
66                 FILE *fp, *fp_out;
67                 unsigned long int i, j, total, k;
68                 char *mtf;
69                 char *salida, *data, c;
70                 data = malloc(sizeof(char)*pagesize);
71                 /* Reservo lugar tambien para guardar el k y el tamaño  */
72                 salida = malloc(sizeof(char)*(pagesize)+sizeof(unsigned long int)*2);
73                 bs = bs_create(pagesize);
74
75                 fp = fopen(argv[optind], "rb");
76                 fp_out = fopen("tmp.comp", "wb");
77
78                 c = fgetc(fp);
79                 total = 0;
80                 while (!feof(fp)) {
81                         i = 0;
82                         while ((!feof(fp)) && (i < pagesize)) {
83                                 data[i++] = c;
84                                 c = fgetc(fp);
85                                 total++;
86                         }
87
88                         /* Hago el BS */
89                         bs_solve(data, salida, bs, &k, i);
90
91                         /* Le aplico el MTF, salteo el tamaño del bloque para que no se pierda. */
92                         mtf = jacu_mtf(salida+sizeof(unsigned long int), i+sizeof(unsigned long int));
93
94                         /* Si me lo piden, aplico ZG. */
95                         if (zflag)
96                         {
97                                 size_t len;
98                                 char buff[2];
99                                 ZG zg;
100                                 zg_init(&zg);
101                                 for (j = 0; j < i; ++j)
102                                         if ((len = zg_group(&zg, buff, mtf[j]))) fwrite(buff, 1, len, fp_out);
103                         }
104                         else
105                         {
106                                 for(j=0; j<i; j++)
107                                         fputc(mtf[j], fp_out);
108                         }
109                         free(mtf);
110                 }
111
112                 /* Limpiando */
113                 fclose(fp);
114                 fclose(fp_out);
115                 bs_destroy(bs);
116
117                 /* Comprimo con huffman */
118                 i = shuff_encode_file("tmp.comp",argv[optind+1], volumesize);
119
120                 /* borro el temporal */
121                 unlink("tmp.comp");
122
123                 /* Muestro bpb */
124                 printf("Comprimido a %.04f bpb.\n", get_file_size(argv[optind+1])*8.0/get_file_size(argv[optind]));
125                 return i;
126         }
127         
128         if (dflag == 1) { 
129                 /* Descomprimo */
130                 FILE *fp_out;
131                 VFILE *fp_in;
132                 unsigned long int block_size, k;
133                 char *block, *mtf, *orig;
134                 int *pos;
135
136                 shuff_decode_file(argv[optind], "tmp.comp"); /*argv[optind+1]);*/
137                 fp_in = vfopen("tmp.comp", "r", 0);
138                 fp_out = fopen(argv[optind+1], "wb");
139
140                 while (!vfeof(fp_in)) {
141                         block_size = 0;
142                         vfread(&block_size, sizeof(unsigned long int), 1, fp_in);
143                         if (block_size > 0) {
144                                 block = malloc((block_size+sizeof(unsigned long int))*sizeof(char));
145                                 orig = malloc(block_size*sizeof(char));
146                                 vfread(block, block_size, sizeof(char), fp_in);
147
148                                 mtf = jacu_mtf_inv(block, /*XXX NO LO TENGO XXX */pos, block_size);
149
150                                 memcpy(&k, block, sizeof(unsigned long int));
151
152                                 bs_restore(orig, block+sizeof(unsigned long int), k, block_size);
153
154                                 fwrite(orig, block_size, sizeof(char), fp_out);
155                                 free(block);
156                                 free(orig);
157                                 free(mtf);
158                         }
159                 }
160                 vfclose(fp_in);
161                 fclose(fp_out);
162         }
163
164         return 0;
165 }
166
167 long get_file_size(const char* filename)
168 {
169         FILE* file;
170         long  file_size;
171
172         if (!(file = fopen(filename, "ab"))) return -1;
173         file_size = ftell(file);
174         fclose(file);
175         return file_size;
176 }
177