2 #include "blocksorting/bs.h"
4 #include "zerogrouping/zerogrouping.h"
5 #include "statichuff/statichuff.h"
6 #include "vfile/vfile.h"
7 #include "vfile/common.h"
12 long get_file_size(const char* filename);
14 int main(int argc, char* argv[])
23 long int volumesize = 0;
24 size_t pagesize = 32768; /* 32KB */
28 char *staticmodel = NULL;
30 while ((ch = getopt(argc, argv, "scdzm:t:q:")) != -1) {
50 volumesize = atol(optarg);
56 case 0: pagesize = 1024; /* 1K */
58 case 1: pagesize = 2048; /* 2K */
60 case 2: pagesize = 4096; /* 4K */
62 case 3: pagesize = 8192; /* 8K */
64 case 4: pagesize = 16384; /* 16K */
66 case 5: pagesize = 32768; /* 32K */
68 case 6: pagesize = 65536; /* 64K */
70 case 7: pagesize = 131072; /* 128K */
72 case 8: pagesize = 262144; /* 256K */
74 case 9: pagesize = 524288; /* 512K */
76 default: pagesize = 0; /* error */
80 default: fprintf(stderr, "Usage: %s [-cdzs][-q blksize][-t volsize][-m modeldumpfile] source target\n", argv[0]);
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]);
89 if ((tflag) && (volumesize <= 0l)) {
90 fprintf(stderr,"Error: The volume size must be a non-zero value\n");
93 if ((qflag) && (pagesize <= 1u)) {
94 fprintf(stderr,"Error: El nivel de compresiĆ³n debe ser entre 0 (menor) y 9 (mayor).\n");
101 Uint32 i, j, total, k;
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);
111 /* Preparo el BS alocando mem para el K, el Block y su Size */
112 data = malloc(sizeof(char)*pagesize);
113 salida = malloc(sizeof(char)*pagesize+sizeof(Uint32)*2);
114 bs = bs_create(pagesize);
116 /* Abrimos el archivo a comprimir y encodeamos bloques */
117 fp = fopen(argv[optind], "rb");
122 while ((!feof(fp)) && (i < pagesize)) {
123 data[i++] = fgetc(fp);
127 /* Saco un EOF que lee de mas */
130 /* Aplico el BlockSorting */
131 bs_solve(data, salida, bs, &k, i);
133 /*printf("BS k=%ld\n", *(Uint32 *)(salida+sizeof(Uint32)));
134 printf("PageSize = %ld\n", *(Uint32 *)salida);
136 printf("Antes de MTF = %ld [", i);
139 for(ii=0; ii<(i+sizeof(Uint32)); ii++)
140 printf("(%c)", salida[ii+sizeof(Uint32)]);
144 /* Le aplico el MTF, salteo el tamaƱo del bloque para que no se pierda. */
145 mtf = jacu_mtf(salida+sizeof(Uint32), i+sizeof(Uint32), &z, &z_len);
147 printf("MTF Z (len=%d) = [", z_len);
150 for(ii=0; ii<z_len; ii++)
151 printf("(%c)", z[ii]);
155 /* Si me lo piden, aplico ZG. */
161 /* TODO HACER LO MISMO QUE EN EL ELSE XXX */
162 for (j = 0; j < i; ++j)
163 if ((len = zg_group(&zg, buff, mtf[j]))) shuff_scanfreq_chunk(shuff,buff,len);
165 /* Guardo el PageSize */
166 shuff_scanfreq_chunk(shuff,salida,sizeof(Uint32));
168 /* Guardo el Z len y el Z */
169 shuff_scanfreq_chunk(shuff,(char*)&z_len,sizeof(int));
170 shuff_scanfreq_chunk(shuff,z,z_len);
172 /* Guardo la salida del MTF */
173 shuff_scanfreq_chunk(shuff,mtf,i+sizeof(Uint32));
183 /* Comprimo con Huffman */
184 shuff_encode_file(shuff);
185 if (sflag == 1) shuff_savemodel(shuff);
186 /* Shutdown Huffman */
187 shuff_deinit_encoder(shuff);
191 printf("Comprimido a %.04f bpb.\n", get_file_size(argv[optind+1])*8.0/get_file_size(argv[optind]));
199 Uint32 block_size, k;
200 char *block, *mtf, *orig;
204 if ((shuff = shuff_init_decoder(argv[optind],"tmp.comp")) == NULL) return 1;
205 shuff_decode_file(shuff);
206 shuff_deinit_decoder(shuff);
209 fp_in = fopen("tmp.comp", "rb");
210 fp_out = fopen(argv[optind+1], "wb");
212 while (!feof(fp_in)) {
214 fread(&block_size, sizeof(Uint32), 1, fp_in);
215 if (block_size > 0) {
216 fread(&z_len, sizeof(int), 1, fp_in);
217 z = malloc(sizeof(char)*z_len);
218 fread(z, z_len, sizeof(char), fp_in);
220 /*printf("MTF Z (len=%d) = [", z_len);
223 for(ii=0; ii<z_len; ii++)
224 printf("(%c)", z[ii]);
229 block = malloc(block_size*sizeof(char)+sizeof(Uint32));
230 orig = malloc(block_size*sizeof(char));
231 fread(block, block_size+sizeof(Uint32), sizeof(char), fp_in);
233 /*printf("Antes MTF_inv = [");
236 for(ii=0; ii<block_size+sizeof(Uint32); ii++)
237 printf("(%c)", block[ii]);
240 /* Hago el MTF inverso */
241 mtf = jacu_mtf_inv(z, block, block_size*sizeof(char)+sizeof(Uint32));
243 /*printf("Luego de MTF Inv= [");
246 for(ii=0; ii<block_size+sizeof(Uint32); ii++)
247 printf("(%c)", mtf[ii]);
251 /* Luego de hacer el MTF inverso ya puedo recuperar el k */
252 memcpy(&k, mtf, sizeof(Uint32));
254 /*printf("Restored : k=%ld\n", k);*/
255 bs_restore(orig, mtf+sizeof(Uint32), k, block_size);
257 fwrite(orig, block_size, sizeof(char), fp_out);
271 long get_file_size(const char* filename)
276 if (!(file = fopen(filename, "ab"))) return -1;
277 file_size = ftell(file);