3 #include "blocksorting/bs.h"
5 #include "zerogrouping/zerogrouping.h"
6 #include "statichuff/statichuff.h"
7 #include "vfile/vfile.h"
8 #include "vfile/common.h"
13 long fsize(const char* filename);
15 typedef struct _flags_ {
25 int comprimir(char *src, char *dst, Uint32 pagesize, Uint32 volumesize, t_Flags *flags, char *staticmodel);
26 int descomprimir(char *src, char *dst);
28 int main(int argc, char* argv[])
30 long int volumesize = 0;
31 Uint32 pagesize = 32768; /* 32KB */
34 char *staticmodel = NULL;
36 memset(&flags, 0, sizeof(t_Flags));
38 while ((ch = getopt(argc, argv, "scdzm:t:q:")) != -1) {
41 case 'c': flags.cflag = 1;
44 case 'd': flags.dflag = 1;
47 case 'z': flags.zflag = 1;
50 case 'm': flags.mflag = 1;
54 case 's': flags.sflag = 1;
57 case 't': flags.tflag = 1;
58 volumesize = atol(optarg);
61 case 'q': flags.qflag = 1;
64 case 0: pagesize = 1024; /* 1K */
66 case 1: pagesize = 2048; /* 2K */
68 case 2: pagesize = 4096; /* 4K */
70 case 3: pagesize = 8192; /* 8K */
72 case 4: pagesize = 16384; /* 16K */
74 case 5: pagesize = 32768; /* 32K */
76 case 6: pagesize = 65536; /* 64K */
78 case 7: pagesize = 131072; /* 128K */
80 case 8: pagesize = 262144; /* 256K */
82 case 9: pagesize = 524288; /* 512K */
84 default: pagesize = 0; /* error */
88 default: fprintf(stderr, "Usage: %s [-cdzs][-q blksize][-t volsize][-m modeldumpfile] source target\n", argv[0]);
93 if ( (argc == 1) || (flags.cflag & flags.dflag) || !(flags.cflag | flags.dflag) || ((argc - optind) < 2) || (flags.mflag & flags.sflag)) {
94 fprintf(stderr, "Usage: %s [-cdzs][-q compressionquality][-t volsize][-m modeldumpfile] source target\n", argv[0]);
97 if ((flags.tflag) && (volumesize <= 0l)) {
98 fprintf(stderr,"Error: The volume size must be a non-zero value\n");
101 if ((flags.qflag) && (pagesize <= 1u)) {
102 fprintf(stderr,"Error: El nivel de compresiĆ³n debe ser entre 0 (menor) y 9 (mayor).\n");
106 if (flags.cflag == 1) {
107 return comprimir(argv[optind], argv[optind+1], pagesize, volumesize, &flags, staticmodel);
110 if (flags.dflag == 1) {
111 return descomprimir(argv[optind], argv[optind+1]);
117 long fsize(const char* filename)
122 //printf("%s!!!\n", filename);
123 if (!(file = fopen(filename, "ab"))) return -1;
124 //printf("Abrio!\n");
125 file_size = ftell(file);
130 int comprimir(char *src, char *dst, Uint32 pagesize, Uint32 volumesize, t_Flags *flags, char *staticmodel)
136 Uint32 i, j, total, k;
138 unsigned char *salida, *data;
142 /* Preparo el compresor huffman */
143 if ((shuff = shuff_init_encoder_bychunk(dst, volumesize*1024)) == NULL) return 1;
144 if (flags->mflag == 1) shuff_loadmodel(shuff, staticmodel);
146 /* Preparo el BS alocando mem para la Salida: V(vector) + K(colnum) */
147 data = malloc(sizeof(unsigned char)*pagesize);
148 salida = malloc(sizeof(unsigned char)*pagesize+sizeof(Uint32));
149 bs = bs_create(pagesize);
151 /* Abrimos el archivo a comprimir y encodeamos bloques */
152 fp = fopen(src, "rb");
154 /* Guardamos el pagesize como header (huffencoded) */
155 shuff_scanfreq_chunk(shuff,(char*)&pagesize,sizeof(Uint32));
157 /* Guardamos cabecera para indicar si usamos ZG (huffencoded) */
159 shuff_scanfreq_chunk(shuff, "\001", 1);
161 shuff_scanfreq_chunk(shuff, "\000", 1);
166 i = bs_readblock(fp, data, pagesize);
170 /* Aplico BS guardando su resultado + el K en salida */
171 bs_solve(data, salida, bs, &k, i);
173 /* Le aplico el MTF a salida */
174 mtf = jacu_mtf(salida, i+sizeof(Uint32), &z, &z_len);
176 /* Guardo el z_len y el Z */
177 shuff_scanfreq_chunk(shuff,(char*)&z_len,sizeof(int));
178 shuff_scanfreq_chunk(shuff,z,z_len);
180 /* Si me lo piden, aplico ZG. */
183 unsigned char buff[2];
184 Uint32 total_size = i + sizeof(Uint32);
186 /* Guardo la salida del MTF con ceros agrupados (ZG) */
188 for (j = 0; j < total_size; ++j)
189 if ((len = zg_group(&zg, buff, mtf[j])))
190 shuff_scanfreq_chunk(shuff, buff, len);
192 /* Flusheo ultimo zgrouping */
193 if ((len = zg_group_finish(&zg,buff)))
194 shuff_scanfreq_chunk(shuff, buff, len);
196 /* Comprimo la salida del MTF */
197 shuff_scanfreq_chunk(shuff,mtf,i+sizeof(Uint32));
204 if (fclose(fp)) fprintf(stderr, "Error al cerrar archivo de entrada!\n");
207 /* Comprimo con Huffman */
208 shuff_encode_file(shuff);
209 if (flags->sflag == 1) shuff_savemodel(shuff);
210 /* Shutdown Huffman */
211 shuff_deinit_encoder(shuff);
215 //printf("%s: %ld bytes.\n", src, fsize(src));
216 //printf("%s: %ld bytes.\n", dst, vfsize(dst));
217 printf("%s: %.04f bits/byte.\n", dst, 1.0/fsize(src)*vfsize(dst)*8.0f);
221 int descomprimir(char *src, char *dst)
225 Uint32 block_size = 0,zgungrouped = 0, k;
226 unsigned char *block, *mtf, *orig;
227 unsigned char *z, *zgbuffer;
228 int zgmoved = 0,z_len=0,moredata = 0,decoded = 0;
229 unsigned char use_zg = 0,zgbyte = 0,retbytes = 0;
233 /* Inicializo el descompresor */
234 if ((shuff = shuff_init_decoder(src, NULL)) == NULL) return 1;
236 /* Abrimos el archivo de salida */
237 fp_out = fopen(dst, "wb");
239 /* Descomprimo primero que nada el pagesize utilizado para comprimir */
240 if (!(moredata = shuff_decode_chunk(shuff,(char*)&block_size,sizeof(Uint32),&decoded))) return 1;
242 /* Descomprimo byte que indica si se usa ZG */
243 if (!(moredata = shuff_decode_chunk(shuff, &use_zg, 1, &decoded))) return 1;
244 if (use_zg) zg_init(&zg);
247 zgbuffer = malloc(sizeof(unsigned char)*256);
248 block = malloc(block_size*sizeof(unsigned char)+sizeof(Uint32));
249 orig = malloc(block_size*sizeof(unsigned char));
251 /* Descomprimimos de a chunks segun convenga */
253 if (block_size > 0) {
254 /* Descomprimo el Zlen y el Z del MTF*/
255 moredata = shuff_decode_chunk(shuff,(char*)&z_len,sizeof(int),&decoded);
256 z = malloc(sizeof(unsigned char)*z_len);
257 moredata = shuff_decode_chunk(shuff,z,z_len,&decoded);
259 /* Veo si se uso Zero Grouping para comprimir */
261 /* Desagrupo bytes hasta completar la pagina or End of Source File */
264 /* Levanto un byte zerogrouped y lo paso por el zg_ungroup */
266 moredata = shuff_decode_chunk(shuff,&zgbyte,1,&decoded);
267 retbytes = zg_ungroup(&zg,zgbuffer,zgbyte);
268 /* Muevo del zgbuffer a mi bloque lo que corresponda */
269 while ((zgmoved < retbytes) && (zgungrouped < block_size+sizeof(Uint32))) {
270 block[zgungrouped++] = zgbuffer[zgmoved++];
272 } while ((moredata) && (zgungrouped < block_size+sizeof(Uint32)));
274 /* Me fijo si el ultimo byte procesado que me completo la pagina fue un 0 */
276 /* Leo un byte mas (un 0 seguro) y zg_ungroup cambiara su estado */
277 moredata = shuff_decode_chunk(shuff,&zgbyte,1,&decoded);
278 zg_ungroup(&zg,zgbuffer,zgbyte);
281 /* Normalizo variables para continuar en common code */
282 decoded = zgungrouped;
285 /* Levanto una salida de MTF */
286 moredata = shuff_decode_chunk(shuff,block,block_size+sizeof(Uint32),&decoded);
289 /* Le aplico MTF inverso a la salida de MTF levantada previamente */
290 mtf = jacu_mtf_inv(z, block, decoded);
292 /* Ya tengo la salida del BS, tonces levanto su K */
293 memcpy(&k, mtf, sizeof(Uint32));
295 /* Obtengo el chunk original aplicando BS Inverso */
296 bs_restore(orig, mtf+sizeof(Uint32), k, decoded - sizeof(Uint32));
298 fwrite(orig, decoded - sizeof(Uint32), sizeof(unsigned char), fp_out);
305 /* Close up files and free mem */
311 /* Shutdown Huffman */
312 shuff_deinit_decoder(shuff);