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 /* 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);
136 /* Si me lo piden, aplico 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);
146 /* Guardo el PageSize */
147 shuff_scanfreq_chunk(shuff,salida,sizeof(Uint32));
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);
153 /* Guardo la salida del MTF */
154 shuff_scanfreq_chunk(shuff,mtf,i+sizeof(Uint32));
164 /* Comprimo con Huffman */
165 shuff_encode_file(shuff);
166 if (sflag == 1) shuff_savemodel(shuff);
167 /* Shutdown Huffman */
168 shuff_deinit_encoder(shuff);
172 printf("Comprimido a %.04f bpb.\n", get_file_size(argv[optind+1])*8.0/get_file_size(argv[optind]));
180 Uint32 block_size, k;
181 unsigned char *block, *mtf, *orig;
183 int z_len,moredata = 0,decoded = 0;
185 /* Inicializo el descompresor */
186 if ((shuff = shuff_init_decoder(argv[optind],NULL)) == NULL) return 1;
188 /* Abrimos el archivo de salida */
189 fp_out = fopen(argv[optind+1], "wb");
191 /* Descomprimimos de a chunks segun convenga */
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);
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);
204 mtf = jacu_mtf_inv(z, block, block_size*sizeof(unsigned char)+sizeof(Uint32));
206 /* Luego de hacer el MTF inverso ya puedo recuperar el k */
207 memcpy(&k, mtf, sizeof(Uint32));
209 /*printf("Restored : k=%ld\n", k);*/
210 bs_restore(orig, mtf+sizeof(Uint32), k, block_size);
212 fwrite(orig, block_size, sizeof(unsigned char), fp_out);
223 /* Shutdown Huffman */
224 shuff_deinit_decoder(shuff);
231 long get_file_size(const char* filename)
236 if (!(file = fopen(filename, "ab"))) return -1;
237 file_size = ftell(file);