]> git.llucax.com Git - z.facultad/75.06/jacu.git/blob - src/jacu.c
ca93ae6aca2d98747a698fdba327c2d3e6388c95
[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                 printf("Total Bytes read = %ld\n", total);
173                 /* Limpiando */
174                 fclose(fp);
175                 fclose(fp_out);
176                 bs_destroy(bs);
177
178                 /* Comprimo con huffman */
179                 i = shuff_encode_file("tmp.comp", argv[optind+1], volumesize);
180
181                 /* borro el temporal */
182                 remove("tmp.comp");
183
184                 /* Muestro bpb */
185                 printf("Comprimido a %.04f bpb.\n", get_file_size(argv[optind+1])*8.0/get_file_size(argv[optind]));
186                 return i;
187         }
188         
189         if (dflag == 1) { 
190                 /* Descomprimo */
191                 FILE *fp_out;
192                 FILE *fp_in;
193                 Uint32 block_size, k;
194                 char *block, *mtf, *orig;
195                 char *z;
196                 int z_len;
197
198                 shuff_decode_file(argv[optind], "tmp.comp"); /*argv[optind+1]);*/
199                 fp_in = fopen("tmp.comp", "rb");
200                 fp_out = fopen(argv[optind+1], "wb");
201
202                 while (!feof(fp_in)) {
203                         block_size = 0;
204                         fread(&block_size, sizeof(Uint32), 1, fp_in);
205                         if (block_size > 0) {
206                                 printf("PageSize = %ld\n", block_size);
207                                 fread(&z_len, sizeof(int), 1, fp_in);
208                                 z = malloc(sizeof(char)*z_len);
209                                 fread(z, z_len, sizeof(char), fp_in);
210
211                                 /*printf("MTF Z (len=%d) = [", z_len);
212                                 {
213                                         int ii;
214                                         for(ii=0; ii<z_len; ii++)
215                                                 printf("(%c)", z[ii]);
216                                         printf("]\n");
217         
218                                 }*/
219                                 
220                                 block = malloc(block_size*sizeof(char)+sizeof(Uint32));
221                                 orig = malloc(block_size*sizeof(char));
222                                 fread(block, block_size+sizeof(Uint32), sizeof(char), fp_in);
223
224                                 /*printf("Antes MTF_inv = [");
225                                 {
226                                         int ii;
227                                         for(ii=0; ii<block_size+sizeof(Uint32); ii++)
228                                                 printf("(%c)", block[ii]);
229                                         printf("]\n");
230                                 }*/
231                                 /* Hago el MTF inverso */
232                                 mtf = jacu_mtf_inv(z, block, block_size*sizeof(char)+sizeof(Uint32));
233
234                                 /*printf("Luego de MTF Inv= [");
235                                 {
236                                         int ii;
237                                         for(ii=0; ii<block_size+sizeof(Uint32); ii++)
238                                                 printf("(%c)", mtf[ii]);
239                                         printf("]\n");
240                                 }*/
241
242                                 /* Luego de hacer el MTF inverso ya puedo recuperar el k */
243                                 memcpy(&k, mtf, sizeof(Uint32));
244
245                                 /*printf("Restored : k=%ld\n", k);*/
246                                 bs_restore(orig, mtf+sizeof(Uint32), k, block_size);
247
248                                 fwrite(orig, block_size, sizeof(char), fp_out);
249                                 free(block);
250                                 free(orig);
251                                 free(mtf);
252                         }
253                 }
254                 fclose(fp_in);
255                 fclose(fp_out);
256         }
257
258         return 0;
259 }
260
261 long get_file_size(const char* filename)
262 {
263         FILE* file;
264         long  file_size;
265
266         if (!(file = fopen(filename, "ab"))) return -1;
267         file_size = ftell(file);
268         fclose(file);
269         return file_size;
270 }
271