]> git.llucax.com Git - z.facultad/75.06/jacu.git/blob - src/jacu.c
9971abf8e61ea979bcf3a5d033b067297fac8384
[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 "vfile/common.h"
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <unistd.h>
11
12 long get_file_size(const char* filename);
13
14 int main(int argc, char* argv[])
15 {       
16         int cflag = 0;
17         int dflag = 0;
18         int zflag = 0;
19         int tflag = 0;
20         int qflag = 0;
21         long int volumesize = 0;
22         size_t pagesize = 32768; /* 32KB */
23         int ch;
24         t_BlockSort *bs;
25                         
26         while ((ch = getopt(argc, argv, "cdzt:q:")) != -1) { 
27                  
28                 switch (ch) { 
29                         case 'c': cflag = 1; 
30                                           break;
31
32                         case 'd': dflag = 1; 
33                                           break; 
34
35                         case 'z': zflag = 1; 
36                                           break; 
37
38                         case 't': tflag = 1; 
39                                 volumesize = atol(optarg);
40                                 break; 
41
42                         case 'q': qflag = 1; 
43                                 switch (atoi(optarg))
44                                 {
45                                         case 0: pagesize = 1024; /* 1K */
46                                                 break;
47                                         case 1: pagesize = 2048; /* 2K */
48                                                 break;
49                                         case 2: pagesize = 4096; /* 4K */
50                                                 break;
51                                         case 3: pagesize = 8192; /* 8K */
52                                                 break;
53                                         case 4: pagesize = 16384; /* 16K */
54                                                 break;
55                                         case 5: pagesize = 32768; /* 32K */
56                                                 break;
57                                         case 6: pagesize = 65536; /* 64K */
58                                                 break;
59                                         case 7: pagesize = 131072; /* 128K */
60                                                 break;
61                                         case 8: pagesize = 262144; /* 256K */
62                                                 break;
63                                         case 9: pagesize = 524288; /* 512K */
64                                                 break;
65                                         default: pagesize = 0; /* error */
66                                 }
67                                 break; 
68
69                         default: fprintf(stderr, "Usage: %s [-cdpt] sourcefile targetfile\n", argv[0]); 
70                                          return(2);
71                 }
72         }
73                 
74         if ( (argc == 1) || (cflag & dflag) || !(cflag | dflag) || ((argc - optind) < 2) ) {
75                 fprintf(stderr, "Usage: %s [-cdt] sourcefile targetfile\n", argv[0]); 
76                 return (3);
77         }
78         if ((tflag) && (volumesize <= 0l)) {
79                 fprintf(stderr,"Error: The volume size must be a non-zero value\n");
80                 return (4);
81         }
82         if ((qflag) && (pagesize <= 1u)) {
83                 fprintf(stderr,"Error: El nivel de compresión debe ser entre 0 (menor) y 9 (mayor).\n");
84                 return (5);
85         }
86                 
87         if (cflag == 1) {
88                 /* Comprimo */
89                 /* No me gusta el tmpfile ... es para probar como anda todo junto */
90                 FILE *fp, *fp_out;
91                 Uint32 i, j, total, k;
92                 char *mtf;
93                 char *salida, *data, c;
94                 char *z;
95                 int z_len;
96
97                 data = malloc(sizeof(char)*pagesize);
98                 /* Reservo lugar tambien para guardar el k y el tamaño  */
99                 salida = malloc(sizeof(char)*pagesize+sizeof(Uint32)*2);
100                 bs = bs_create(pagesize);
101
102                 fp = fopen(argv[optind], "rb");
103                 fp_out = fopen("tmp.comp", "wb");
104
105                 /*c = fgetc(fp);*/
106                 total = 0;
107                 while (!feof(fp)) {
108                         i = 0;
109                         while ((!feof(fp)) && (i < pagesize)) {
110                                 data[i++] = fgetc(fp);
111                                 total++;
112                         }
113
114                         /* Saco un EOF que lee de mas */
115                         if (i<pagesize) i--;
116
117                         /* Hago el BS */
118                         bs_solve(data, salida, bs, &k, i);
119
120                         /*printf("BS k=%ld\n", *(Uint32 *)(salida+sizeof(Uint32)));
121                         printf("PageSize = %ld\n", *(Uint32 *)salida);
122
123                         printf("Antes de MTF = %ld [", i);
124                         {
125                                 int ii;
126                                 for(ii=0; ii<(i+sizeof(Uint32)); ii++)
127                                         printf("(%c)", salida[ii+sizeof(Uint32)]);
128                                 printf("]\n");
129                         }*/
130                         /* Le aplico el MTF, salteo el tamaño del bloque para que no se pierda. */
131                         mtf = jacu_mtf(salida+sizeof(Uint32), i+sizeof(Uint32), &z, &z_len);
132 /*
133                         printf("MTF Z (len=%d) = [", z_len);
134                         {
135                                 int ii;
136                                 for(ii=0; ii<z_len; ii++)
137                                         printf("(%c)", z[ii]);
138                                 printf("]\n");
139
140                         }*/
141                         /* Si me lo piden, aplico ZG. */
142                         if (zflag) {
143                                 size_t len;
144                                 char buff[2];
145                                 ZG zg;
146                                 zg_init(&zg);
147                                 /* TODO HACER LO MISMO QUE EN EL ELSE XXX */
148                                 for (j = 0; j < i; ++j)
149                                         if ((len = zg_group(&zg, buff, mtf[j]))) fwrite(buff, 1, len, fp_out);
150                         } else {
151                                 /* Guardo el PageSize */
152                                 fwrite(salida, sizeof(Uint32), 1, fp_out);
153
154                                 /* Guardo el Z len y el Z */
155                                 fwrite(&z_len, sizeof(int), 1, fp_out);
156                                 fwrite(z, z_len, sizeof(char), fp_out);
157
158                                 /* Guardo la salida del MTF */
159                                 /*printf("Despues de MTF : [");*/
160                                 for(j=0; j<(i+sizeof(Uint32)); j++) {
161                                         fputc(mtf[j], fp_out);
162                         /*              putchar('(');
163                                         fputc(mtf[j], stdout);
164                                         putchar(')');*/
165                                 }
166                                 /*printf("]\n");*/
167                         }
168                         free(mtf);
169                         free(z);
170                 }
171
172                 /* Limpiando */
173                 fclose(fp);
174                 fclose(fp_out);
175                 bs_destroy(bs);
176
177                 /* Comprimo con huffman */
178                 i = shuff_encode_file("tmp.comp", argv[optind+1], volumesize);
179
180                 /* borro el temporal */
181                 remove("tmp.comp");
182
183                 /* Muestro bpb */
184                 printf("Comprimido a %.04f bpb.\n", get_file_size(argv[optind+1])*8.0/get_file_size(argv[optind]));
185                 return i;
186         }
187         
188         if (dflag == 1) { 
189                 /* Descomprimo */
190                 FILE *fp_out;
191                 FILE *fp_in;
192                 Uint32 block_size, k;
193                 char *block, *mtf, *orig;
194                 char *z;
195                 int z_len;
196
197                 shuff_decode_file(argv[optind], "tmp.comp"); /*argv[optind+1]);*/
198                 fp_in = fopen("tmp.comp", "rb");
199                 fp_out = fopen(argv[optind+1], "wb");
200
201                 while (!feof(fp_in)) {
202                         block_size = 0;
203                         fread(&block_size, sizeof(Uint32), 1, fp_in);
204                         if (block_size > 0) {
205                                 fread(&z_len, sizeof(int), 1, fp_in);
206                                 z = malloc(sizeof(char)*z_len);
207                                 fread(z, z_len, sizeof(char), fp_in);
208
209                                 /*printf("MTF Z (len=%d) = [", z_len);
210                                 {
211                                         int ii;
212                                         for(ii=0; ii<z_len; ii++)
213                                                 printf("(%c)", z[ii]);
214                                         printf("]\n");
215         
216                                 }*/
217                                 
218                                 block = malloc(block_size*sizeof(char)+sizeof(Uint32));
219                                 orig = malloc(block_size*sizeof(char));
220                                 fread(block, block_size+sizeof(Uint32), sizeof(char), fp_in);
221
222                                 /*printf("Antes MTF_inv = [");
223                                 {
224                                         int ii;
225                                         for(ii=0; ii<block_size+sizeof(Uint32); ii++)
226                                                 printf("(%c)", block[ii]);
227                                         printf("]\n");
228                                 }*/
229                                 /* Hago el MTF inverso */
230                                 mtf = jacu_mtf_inv(z, block, block_size*sizeof(char)+sizeof(Uint32));
231
232                                 /*printf("Luego de MTF Inv= [");
233                                 {
234                                         int ii;
235                                         for(ii=0; ii<block_size+sizeof(Uint32); ii++)
236                                                 printf("(%c)", mtf[ii]);
237                                         printf("]\n");
238                                 }*/
239
240                                 /* Luego de hacer el MTF inverso ya puedo recuperar el k */
241                                 memcpy(&k, mtf, sizeof(Uint32));
242
243                                 /*printf("Restored : k=%ld\n", k);*/
244                                 bs_restore(orig, mtf+sizeof(Uint32), k, block_size);
245
246                                 fwrite(orig, block_size, sizeof(char), fp_out);
247                                 free(block);
248                                 free(orig);
249                                 free(mtf);
250                                 free(z);
251                         }
252                 }
253                 fclose(fp_in);
254                 fclose(fp_out);
255         }
256
257         return 0;
258 }
259
260 long get_file_size(const char* filename)
261 {
262         FILE* file;
263         long  file_size;
264
265         if (!(file = fopen(filename, "ab"))) return -1;
266         file_size = ftell(file);
267         fclose(file);
268         return file_size;
269 }
270