]> git.llucax.com Git - z.facultad/75.06/jacu.git/blob - src/jacu.c
Limpio de basura todo el codigo
[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                         /* Le aplico el MTF, salteo el tamaƱo del bloque para que no se pierda. */
134                         mtf = jacu_mtf(salida+sizeof(Uint32), i+sizeof(Uint32), &z, &z_len);
135                         
136                         /* Si me lo piden, aplico ZG. */
137                         if (zflag) {
138                                 size_t len;
139                                 char buff[2];
140                                 ZG zg;
141                                 zg_init(&zg);
142                                 /* TODO HACER LO MISMO QUE EN EL ELSE XXX */
143                                 for (j = 0; j < i; ++j)
144                                         if ((len = zg_group(&zg, buff, mtf[j]))) shuff_scanfreq_chunk(shuff,buff,len);
145                         } else {
146                                 /* Guardo el PageSize */
147                                 shuff_scanfreq_chunk(shuff,salida,sizeof(Uint32));
148                                 
149                                 /* Guardo el Z len y el Z */
150                                 shuff_scanfreq_chunk(shuff,(char*)&z_len,sizeof(int));
151                                 shuff_scanfreq_chunk(shuff,z,z_len);                                                            
152
153                                 /* Guardo la salida del MTF */                          
154                                 shuff_scanfreq_chunk(shuff,mtf,i+sizeof(Uint32));                               
155                         }
156                         free(mtf);
157                         free(z);
158                 }
159
160                 /* Limpiando */
161                 fclose(fp);             
162                 bs_destroy(bs);
163
164                 /* Comprimo con Huffman */              
165                 shuff_encode_file(shuff);
166                 if (sflag == 1) shuff_savemodel(shuff);
167                 /* Shutdown Huffman */
168                 shuff_deinit_encoder(shuff);
169                 free(shuff);
170
171                 /* Muestro bpb */
172                 printf("Comprimido a %.04f bpb.\n", get_file_size(argv[optind+1])*8.0/get_file_size(argv[optind]));
173                 return 0;
174         }
175         
176         if (dflag == 1) { 
177                 /* Descomprimo */
178                 FILE *fp_out;
179                 /*FILE *fp_in;*/
180                 Uint32 block_size, k;
181                 unsigned char *block, *mtf, *orig;
182                 unsigned char *z;
183                 int z_len,moredata = 0,decoded = 0;
184
185                 /* Inicializo el descompresor */
186                 if ((shuff = shuff_init_decoder(argv[optind],NULL)) == NULL) return 1;
187                                 
188                 /* Abrimos el archivo de salida */
189                 fp_out = fopen(argv[optind+1], "wb");
190
191                 /* Descomprimimos de a chunks segun convenga */
192                 do {
193                         block_size = 0;                                         
194                         moredata = shuff_decode_chunk(shuff,(char*)&block_size,sizeof(Uint32),&decoded);
195                         if (block_size > 0) {
196                                 moredata = shuff_decode_chunk(shuff,(char*)&z_len,sizeof(int),&decoded);                                
197                                 z = malloc(sizeof(unsigned char)*z_len);
198                                 moredata = shuff_decode_chunk(shuff,z,z_len,&decoded);                          
199
200                                 block = malloc(block_size*sizeof(unsigned char)+sizeof(Uint32));
201                                 orig = malloc(block_size*sizeof(unsigned char));
202                                 moredata = shuff_decode_chunk(shuff,block,block_size+sizeof(Uint32),&decoded);                          
203
204                                 mtf = jacu_mtf_inv(z, block, block_size*sizeof(unsigned char)+sizeof(Uint32));
205
206                                 /* Luego de hacer el MTF inverso ya puedo recuperar el k */
207                                 memcpy(&k, mtf, sizeof(Uint32));
208
209                                 /*printf("Restored : k=%ld\n", k);*/
210                                 bs_restore(orig, mtf+sizeof(Uint32), k, block_size);
211
212                                 fwrite(orig, block_size, sizeof(unsigned char), fp_out);
213                                 free(block);
214                                 free(orig);
215                                 free(mtf);
216                                 free(z);
217                         }
218                 } while (moredata);             
219                 
220                 /* Close up files */
221                 fclose(fp_out);
222                 
223                 /* Shutdown Huffman */
224                 shuff_deinit_decoder(shuff);
225                 free(shuff);
226         }
227
228         return 0;
229 }
230
231 long get_file_size(const char* filename)
232 {
233         FILE* file;
234         long  file_size;
235
236         if (!(file = fopen(filename, "ab"))) return -1;
237         file_size = ftell(file);
238         fclose(file);
239         return file_size;
240 }