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