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;
103 unsigned char *salida, *data;
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(unsigned char)*pagesize);
113 salida = malloc(sizeof(unsigned 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 unsigned char *block, *mtf, *orig;
202 int z_len,moredata = 0,decoded = 0;
204 /* Inicializo el descompresor */
205 if ((shuff = shuff_init_decoder(argv[optind],NULL)) == NULL) return 1;
207 /* Abrimos el archivo de salida */
208 fp_out = fopen(argv[optind+1], "wb");
210 /* Descomprimimos de a chunks segun convenga */
213 moredata = shuff_decode_chunk(shuff,(char*)&block_size,sizeof(Uint32),&decoded);
214 if (block_size > 0) {
215 moredata = shuff_decode_chunk(shuff,(char*)&z_len,sizeof(int),&decoded);
216 z = malloc(sizeof(unsigned char)*z_len);
217 moredata = shuff_decode_chunk(shuff,z,z_len,&decoded);
219 /*printf("MTF Z (len=%d) = [", z_len);
222 for(ii=0; ii<z_len; ii++)
223 printf("(%c)", z[ii]);
228 block = malloc(block_size*sizeof(unsigned char)+sizeof(Uint32));
229 orig = malloc(block_size*sizeof(unsigned char));
230 moredata = shuff_decode_chunk(shuff,block,block_size+sizeof(Uint32),&decoded);
232 /*printf("Antes MTF_inv = [");
235 for(ii=0; ii<block_size+sizeof(Uint32); ii++)
236 printf("(%c)", block[ii]);
239 /* Hago el MTF inverso */
240 mtf = jacu_mtf_inv(z, block, block_size*sizeof(unsigned char)+sizeof(Uint32));
242 /*printf("Luego de MTF Inv= [");
245 for(ii=0; ii<block_size+sizeof(Uint32); ii++)
246 printf("(%c)", mtf[ii]);
250 /* Luego de hacer el MTF inverso ya puedo recuperar el k */
251 memcpy(&k, mtf, sizeof(Uint32));
253 /*printf("Restored : k=%ld\n", k);*/
254 bs_restore(orig, mtf+sizeof(Uint32), k, block_size);
256 fwrite(orig, block_size, sizeof(unsigned char), fp_out);
267 /* Shutdown Huffman */
268 shuff_deinit_decoder(shuff);
275 long get_file_size(const char* filename)
280 if (!(file = fopen(filename, "ab"))) return -1;
281 file_size = ftell(file);