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