]> git.llucax.com Git - z.facultad/75.06/jacu.git/blob - src/jacu.c
Se agrega el huffman dinamico de inet probado para comparar con el nuestro.
[z.facultad/75.06/jacu.git] / src / jacu.c
1
2 /* Jacu Team - GPL */
3 #include "blocksorting/bs.h"
4 #include "mtf/mtf.h"
5 #include "zerogrouping/zerogrouping.h"
6 #include "statichuff/statichuff.h"
7 #include "vfile/vfile.h"
8 #include "vfile/common.h"
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <unistd.h>
12
13 long fsize(const char* filename);
14
15 typedef struct _flags_ {
16         int cflag;
17         int dflag;
18         int zflag;
19         int tflag;
20         int qflag;
21         int sflag;
22         int mflag;
23         int rflag; /* Richard Dictionary :-) */
24 } t_Flags;
25
26 int comprimir(char *src, char *dst, Uint32 pagesize, Uint32 volumesize, t_Flags *flags, char *staticmodel);
27 int descomprimir(char *src, char *dst);
28
29 int main(int argc, char* argv[])
30 {       
31         long int volumesize = 0;
32         Uint32 pagesize = 32768; /* 32KB */
33         int ch;
34         t_Flags flags;
35         char *staticmodel = NULL;
36                         
37         memset(&flags, 0, sizeof(t_Flags));
38
39         while ((ch = getopt(argc, argv, "rscdzm:t:q:")) != -1) { 
40                  
41                 switch (ch) { 
42                         case 'c': flags.cflag = 1; 
43                                           break;
44
45                         case 'd': flags.dflag = 1; 
46                                           break; 
47
48                         case 'z': flags.zflag = 1; 
49                                           break; 
50                         
51                         case 'm': flags.mflag = 1;
52                                           staticmodel = optarg;
53                                           break; 
54                         
55                         case 's': flags.sflag = 1;                                        
56                                           break;
57
58                         case 't': flags.tflag = 1; 
59                                 volumesize = atol(optarg);
60                                 break; 
61
62                         case 'r': flags.rflag = 1;
63                                 break;
64                         case 'q': flags.qflag = 1; 
65                                 switch (atoi(optarg))
66                                 {
67                                         case 0: pagesize = 1024; /* 1K */
68                                                 break;
69                                         case 1: pagesize = 2048; /* 2K */
70                                                 break;
71                                         case 2: pagesize = 4096; /* 4K */
72                                                 break;
73                                         case 3: pagesize = 8192; /* 8K */
74                                                 break;
75                                         case 4: pagesize = 16384; /* 16K */
76                                                 break;
77                                         case 5: pagesize = 32768; /* 32K */
78                                                 break;
79                                         case 6: pagesize = 65536; /* 64K */
80                                                 break;
81                                         case 7: pagesize = 131072; /* 128K */
82                                                 break;
83                                         case 8: pagesize = 262144; /* 256K */
84                                                 break;
85                                         case 9: pagesize = 524288; /* 512K */
86                                                 break;
87                                         default: pagesize = 0; /* error */
88                                 }
89                                 break; 
90
91                         default: fprintf(stderr, "Usage: %s [-cdzsr][-q blksize][-t volsize][-m modeldumpfile] source target\n", argv[0]); 
92                                          return(2);
93                 }
94         }
95                 
96         if ( (argc == 1) || (flags.cflag & flags.dflag) || !(flags.cflag | flags.dflag) || ((argc - optind) < 2) || (flags.mflag & flags.sflag)) {
97                 fprintf(stderr, "Usage: %s [-cdzsr][-q compressionquality][-t volsize][-m modeldumpfile] source target\n", argv[0]); 
98                 return (3);
99         }
100         if ((flags.tflag) && (volumesize <= 0l)) {
101                 fprintf(stderr,"Error: The volume size must be a non-zero value\n");
102                 return (4);
103         }
104         if ((flags.qflag) && (pagesize <= 1u)) {
105                 fprintf(stderr,"Error: El nivel de compresiĆ³n debe ser entre 0 (menor) y 9 (mayor).\n");
106                 return (5);
107         }
108                 
109         if (flags.cflag == 1) {
110                 return comprimir(argv[optind], argv[optind+1], pagesize, volumesize, &flags, staticmodel);
111         }
112         
113         if (flags.dflag == 1) { 
114                 return descomprimir(argv[optind], argv[optind+1]);
115         }
116
117         return 0;
118 }
119
120 long fsize(const char* filename)
121 {
122         FILE* file;
123         long  file_size;
124
125         if (!(file = fopen(filename, "ab"))) return -1;
126         file_size = ftell(file);
127         fclose(file);
128         return file_size;
129 }
130
131 int comprimir(char *src, char *dst, Uint32 pagesize, Uint32 volumesize, t_Flags *flags, char *staticmodel)
132 {
133         /* Comprimo */          
134         t_BlockSort *bs;
135         HUFF_STATE *shuff;
136         FILE *fp;
137         Uint32 i, j, total, k;
138         unsigned char *mtf;
139         unsigned char *salida, *data;
140         unsigned char *z;
141         int z_len;
142         
143         /* Abrimos el archivo a comprimir y encodeamos bloques */
144         if ((fp = fopen(src, "rb")) == NULL) return 1;
145         
146         /* Preparo el compresor huffman */
147         if ((shuff = shuff_init_encoder_bychunk(dst, volumesize*1024)) == NULL) return 1;
148         if (flags->mflag == 1) shuff_loadmodel(shuff, staticmodel);
149         
150         /* Preparo el BS alocando mem para la Salida: V(vector) + K(colnum) */
151         data = malloc(sizeof(unsigned char)*pagesize);
152         salida = malloc(sizeof(unsigned char)*pagesize+sizeof(Uint32));
153         bs = bs_create(pagesize);
154
155         /* Guardamos el pagesize como header (huffencoded) */
156         shuff_scanfreq_chunk(shuff,(char*)&pagesize,sizeof(Uint32));
157
158         /* Guardamos cabecera para indicar si usamos ZG (huffencoded) */
159         if (flags->zflag)
160                 shuff_scanfreq_chunk(shuff, "\001", 1);
161         else
162                 shuff_scanfreq_chunk(shuff, "\000", 1);
163
164         total = 0;
165         while (!feof(fp)) {
166                 i = 0;
167                 i = bs_readblock(fp, data, pagesize, flags->rflag);
168                 total += i;
169
170
171                 /* Aplico BS guardando su resultado + el K en salida */
172                 bs_solve(data, salida, bs, &k, i);
173
174                 /* Le aplico el MTF a salida */
175                 mtf = jacu_mtf(salida, i+sizeof(Uint32), &z, &z_len);
176                                 
177                 /* Guardo el z_len y el Z */
178                 shuff_scanfreq_chunk(shuff,(char*)&z_len,sizeof(int));
179                 shuff_scanfreq_chunk(shuff,z,z_len);                    
180                 
181                 /* Si me lo piden, aplico ZG. */
182                 if (flags->zflag) {
183                         Uint32 len;
184                         unsigned char buff[2];
185                         Uint32 total_size = i + sizeof(Uint32);
186                         ZG zg;
187                         /* Guardo la salida del MTF con ceros agrupados (ZG) */
188                         zg_init(&zg);
189                         for (j = 0; j < total_size; ++j)
190                                 if ((len = zg_group(&zg, buff, mtf[j])))
191                                         shuff_scanfreq_chunk(shuff, buff, len);
192
193                                 /* Flusheo ultimo zgrouping */
194                                 if ((len = zg_group_finish(&zg,buff)))
195                                         shuff_scanfreq_chunk(shuff, buff, len);
196                 } else {
197                         /* Comprimo la salida del MTF */
198                         shuff_scanfreq_chunk(shuff,mtf,i+sizeof(Uint32));
199                 }
200                 free(mtf);
201                 free(z);
202         }
203
204         /* Limpiando */
205         if (fclose(fp)) fprintf(stderr, "Error al cerrar archivo de entrada!\n");
206         bs_destroy(bs);
207         free(data);
208         free(salida);
209
210         /* Comprimo con Huffman */              
211         shuff_encode_file(shuff);
212         if (flags->sflag == 1) shuff_savemodel(shuff);
213         /* Shutdown Huffman */
214         shuff_deinit_encoder(shuff);
215         free(shuff);
216
217         /* Muestro bpb */
218         printf("%s: %.04f bits/byte.\n", dst, vfsize(dst)*8.0f/fsize(src));
219         return 0;
220 }
221
222 int descomprimir(char *src, char *dst)
223 {
224         /* Descomprimo */
225         FILE *fp_out;
226         Uint32 block_size = 0, k;
227         unsigned char *block, *mtf, *orig;
228         unsigned char *z;
229         int z_len=0,moredata = 0,decoded = 0;
230         unsigned char use_zg = 0,retbytes = 0;
231         HUFF_STATE *shuff;
232
233         /* Inicializo el descompresor */
234         if ((shuff = shuff_init_decoder(src, NULL)) == NULL) return 1;
235                         
236         /* Abrimos el archivo de salida */
237         fp_out = fopen(dst, "wb");
238         
239         /* Descomprimo primero que nada el pagesize utilizado para comprimir */
240         if (!(moredata = shuff_decode_chunk(shuff,(char*)&block_size,sizeof(Uint32),&decoded))) return 1;
241
242         /* Descomprimo byte que indica si se usa ZG */
243         if (!(moredata = shuff_decode_chunk(shuff, &use_zg, 1, &decoded))) return 1;
244
245         /* Creo buffers */
246         block = malloc(block_size*sizeof(unsigned char)+sizeof(Uint32));
247         orig = malloc(block_size*sizeof(unsigned char));
248
249         /* Descomprimimos de a chunks segun convenga */
250         do {                    
251                 if (block_size > 0) {
252                         /* Descomprimo el Zlen y el Z del MTF*/
253                         moredata = shuff_decode_chunk(shuff,(char*)&z_len,sizeof(int),&decoded);                                        
254                         z = malloc(sizeof(unsigned char)*z_len);
255                         moredata = shuff_decode_chunk(shuff,z,z_len,&decoded);                          
256                         
257                         /* Veo si se uso Zero Grouping para comprimir */
258                         if (use_zg) {
259                                 ZG zg;
260                                 unsigned char zgbuffer[255];
261                                 unsigned char zgbyte = 0;
262                                 int zgmoved = 0;
263                                 Uint32 zgungrouped = 0;
264                                 /* Desagrupo bytes hasta completar la pagina or End of Source File */
265                                 zg_init(&zg);
266                                 do {
267                                         /* Levanto un byte zerogrouped y lo paso por el zg_ungroup */
268                                         zgmoved = 0;
269                                         moredata = shuff_decode_chunk(shuff,&zgbyte,1,&decoded);
270                                         retbytes = zg_ungroup(&zg,zgbuffer,zgbyte);
271                                         /* Muevo del zgbuffer a mi bloque lo que corresponda */
272                                         while ((zgmoved < retbytes) && (zgungrouped < block_size+sizeof(Uint32))) {
273                                                 block[zgungrouped++] = zgbuffer[zgmoved++];
274                                         }
275                                 } while ((moredata) && (zgungrouped < block_size+sizeof(Uint32)));
276
277                                 /* Me fijo si el ultimo byte procesado que me completo la pagina fue un 0 */
278                                 if (zgbyte == 0) {
279                                         /* Leo un byte mas (un 0 seguro) y zg_ungroup cambiara su estado */
280                                         moredata = shuff_decode_chunk(shuff,&zgbyte,1,&decoded);
281                                         zg_ungroup(&zg,zgbuffer,zgbyte);
282                                 }
283
284                                 /* Normalizo variables para continuar en common code */
285                                 decoded = zgungrouped;
286                         }
287                         else {
288                                 /* Levanto una salida de MTF */
289                                 moredata = shuff_decode_chunk(shuff,block,block_size+sizeof(Uint32),&decoded);
290                         }
291                         
292                         /* Le aplico MTF inverso a la salida de MTF levantada previamente */
293                         mtf = jacu_mtf_inv(z, block, decoded);
294
295                         /* Ya tengo la salida del BS, tonces levanto su K */
296                         memcpy(&k, mtf, sizeof(Uint32));
297
298                         /* Obtengo el chunk original aplicando BS Inverso */
299                         bs_restore(orig, mtf+sizeof(Uint32), k, decoded - sizeof(Uint32));
300
301                         fwrite(orig, decoded - sizeof(Uint32), sizeof(unsigned char), fp_out);
302                         free(mtf);
303                         free(z);
304                 }
305                 else return 1;
306         } while (moredata);
307         
308         /* Close up files and free mem */
309         fclose(fp_out);
310         free(block);
311         free(orig);
312
313         /* Shutdown Huffman */
314         shuff_deinit_decoder(shuff);
315         free(shuff);
316         return 0;
317 }