]> git.llucax.com Git - z.facultad/75.06/jacu.git/blob - src/jacu.c
089bc7930fcd5aec94425a45d257448af371c607
[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                 unsigned char *mtf;
103                 unsigned char *salida, *data;
104                 unsigned 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(unsigned char)*pagesize);
113                 salida = malloc(sizeof(unsigned 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                 unsigned char *block, *mtf, *orig;
201                 unsigned char *z;
202                 int z_len,moredata = 0,decoded = 0;
203
204                 /* Inicializo el descompresor */
205                 if ((shuff = shuff_init_decoder(argv[optind],NULL)) == NULL) return 1;
206                                 
207                 /* Abrimos el archivo de salida */
208                 fp_out = fopen(argv[optind+1], "wb");
209
210                 /* Descomprimimos de a chunks segun convenga */
211                 do {
212                         block_size = 0;                                         
213                         moredata = shuff_decode_chunk(shuff,(char*)&block_size,sizeof(Uint32),&decoded);
214                         if (block_size > 0) {
215                                 moredata = shuff_decode_chunk(shuff,(char*)&z_len,sizeof(int),&decoded);                                
216                                 z = malloc(sizeof(unsigned char)*z_len);
217                                 moredata = shuff_decode_chunk(shuff,z,z_len,&decoded);                          
218
219                                 /*printf("MTF Z (len=%d) = [", z_len);
220                                 {
221                                         int ii;
222                                         for(ii=0; ii<z_len; ii++)
223                                                 printf("(%c)", z[ii]);
224                                         printf("]\n");
225         
226                                 }*/
227                                 
228                                 block = malloc(block_size*sizeof(unsigned char)+sizeof(Uint32));
229                                 orig = malloc(block_size*sizeof(unsigned char));
230                                 moredata = shuff_decode_chunk(shuff,block,block_size+sizeof(Uint32),&decoded);                          
231
232                                 /*printf("Antes MTF_inv = [");
233                                 {
234                                         int ii;
235                                         for(ii=0; ii<block_size+sizeof(Uint32); ii++)
236                                                 printf("(%c)", block[ii]);
237                                         printf("]\n");
238                                 }*/
239                                 /* Hago el MTF inverso */
240                                 mtf = jacu_mtf_inv(z, block, block_size*sizeof(unsigned char)+sizeof(Uint32));
241
242                                 /*printf("Luego de MTF Inv= [");
243                                 {
244                                         int ii;
245                                         for(ii=0; ii<block_size+sizeof(Uint32); ii++)
246                                                 printf("(%c)", mtf[ii]);
247                                         printf("]\n");
248                                 }*/
249
250                                 /* Luego de hacer el MTF inverso ya puedo recuperar el k */
251                                 memcpy(&k, mtf, sizeof(Uint32));
252
253                                 /*printf("Restored : k=%ld\n", k);*/
254                                 bs_restore(orig, mtf+sizeof(Uint32), k, block_size);
255
256                                 fwrite(orig, block_size, sizeof(unsigned char), fp_out);
257                                 free(block);
258                                 free(orig);
259                                 free(mtf);
260                                 free(z);
261                         }
262                 } while (moredata);             
263                 
264                 /* Close up files */
265                 fclose(fp_out);
266                 
267                 /* Shutdown Huffman */
268                 shuff_deinit_decoder(shuff);
269                 free(shuff);
270         }
271
272         return 0;
273 }
274
275 long get_file_size(const char* filename)
276 {
277         FILE* file;
278         long  file_size;
279
280         if (!(file = fopen(filename, "ab"))) return -1;
281         file_size = ftell(file);
282         fclose(file);
283         return file_size;
284 }