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