]> git.llucax.com Git - z.facultad/75.06/jacu.git/blob - src/jacu.c
7e798278b77dac704d4ba73153c2e168428e7738
[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++] = c;
111                                 c = fgetc(fp);
112                                 total++;
113                         }
114
115                         /* Hago el BS */
116                         bs_solve(data, salida, bs, &k, i);
117
118                         printf("BS k=%ld\n", *(Uint32 *)(salida+sizeof(Uint32)));
119                         printf("PageSize = %ld\n", *(Uint32 *)salida);
120
121                         printf("Antes de MTF = %ld [", i);
122                         {
123                                 int ii;
124                                 for(ii=0; ii<(i+sizeof(Uint32)); ii++)
125                                         printf("(%c)", salida[ii+sizeof(Uint32)]);
126                                 printf("]\n");
127                         }
128                         /* Le aplico el MTF, salteo el tamaño del bloque para que no se pierda. */
129                         mtf = jacu_mtf(salida+sizeof(Uint32), i+sizeof(Uint32), &z, &z_len);
130
131                         printf("MTF Z (len=%d) = [", z_len);
132                         {
133                                 int ii;
134                                 for(ii=0; ii<z_len; ii++)
135                                         printf("(%c)", z[ii]);
136                                 printf("]\n");
137
138                         }
139                         /* Si me lo piden, aplico ZG. */
140                         if (zflag) {
141                                 size_t len;
142                                 char buff[2];
143                                 ZG zg;
144                                 zg_init(&zg);
145                                 /* TODO HACER LO MISMO QUE EN EL ELSE XXX */
146                                 for (j = 0; j < i; ++j)
147                                         if ((len = zg_group(&zg, buff, mtf[j]))) fwrite(buff, 1, len, fp_out);
148                         } else {
149                                 /* Guardo el PageSize */
150                                 fwrite(salida, sizeof(Uint32), 1, fp_out);
151
152                                 /* Guardo el Z len y el Z */
153                                 fwrite(&z_len, sizeof(int), 1, fp_out);
154                                 fwrite(z, z_len, sizeof(char), fp_out);
155
156                                 /* Guardo la salida del MTF */
157                                 printf("Despues de MTF : [");
158                                 for(j=0; j<(i+sizeof(Uint32)); j++) {
159                                         fputc(mtf[j+sizeof(Uint32)], fp_out);
160                                         putchar('(');
161                                         fputc(mtf[j+sizeof(Uint32)], stdout);
162                                         putchar(')');
163                                 }
164                                 printf("]\n");
165                         }
166                         free(mtf);
167                         free(z);
168                 }
169
170                 /* Limpiando */
171                 fclose(fp);
172                 fclose(fp_out);
173                 bs_destroy(bs);
174
175                 /* Comprimo con huffman */
176                 i = shuff_encode_file("tmp.comp", argv[optind+1], volumesize);
177
178                 /* borro el temporal */
179                 remove("tmp.comp");
180
181                 /* Muestro bpb */
182                 printf("Comprimido a %.04f bpb.\n", get_file_size(argv[optind+1])*8.0/get_file_size(argv[optind]));
183                 return i;
184         }
185         
186         if (dflag == 1) { 
187                 /* Descomprimo */
188                 FILE *fp_out;
189                 FILE *fp_in;
190                 Uint32 block_size, k;
191                 char *block, *mtf, *orig;
192                 char *z;
193                 int z_len;
194
195                 shuff_decode_file(argv[optind], "tmp.comp"); /*argv[optind+1]);*/
196                 fp_in = fopen("tmp.comp", "rb");
197                 fp_out = fopen(argv[optind+1], "wb");
198
199                 while (!feof(fp_in)) {
200                         block_size = 0;
201                         PERR("Leo bloque");
202                         fread(&block_size, sizeof(Uint32), 1, fp_in);
203                         printf("PageSize = %ld\n", block_size);
204                         fread(&z_len, sizeof(int), 1, fp_in);
205                         z = malloc(sizeof(char)*z_len);
206                         fread(z, z_len, sizeof(char), fp_in);
207
208                         printf("MTF Z (len=%d) = [", z_len);
209                         {
210                                 int ii;
211                                 for(ii=0; ii<z_len; ii++)
212                                         printf("(%c)", z[ii]);
213                                 printf("]\n");
214
215                         }
216                         if (block_size > 0) {
217                                 block = malloc(block_size*sizeof(char)+sizeof(Uint32));
218                                 orig = malloc(block_size*sizeof(char));
219                                 fread(block, block_size+sizeof(Uint32), sizeof(char), fp_in);
220
221                                 printf("Antes MTF_inv = [");
222                                 {
223                                         int ii;
224                                         for(ii=0; ii<block_size+sizeof(Uint32); ii++)
225                                                 printf("(%c)", block[ii]);
226                                         printf("]\n");
227                                 }
228                                 /* Hago el MTF inverso */
229                                 PERR("Haciendo MTF Inv");
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                                 PERR("Recuperando K");
242                                 memcpy(&k, mtf, sizeof(Uint32));
243
244                                 printf("Restored : k=%ld\n", k);
245                                 PERR("BS_Restore");
246                                 bs_restore(orig, block+sizeof(Uint32), k, block_size);
247
248                                 PERR("Saving Data");
249                                 fwrite(orig, block_size, sizeof(char), fp_out);
250                                 free(block);
251                                 free(orig);
252                                 free(mtf);
253                         }
254                 }
255                 fclose(fp_in);
256                 fclose(fp_out);
257         }
258
259         return 0;
260 }
261
262 long get_file_size(const char* filename)
263 {
264         FILE* file;
265         long  file_size;
266
267         if (!(file = fopen(filename, "ab"))) return -1;
268         file_size = ftell(file);
269         fclose(file);
270         return file_size;
271 }
272