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