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 /* Flags del archivo comprimido */
18 #define FLAGS_RESERVED_1 0x4
19 #define FLAGS_RESERVED_2 0x8
20 #define FLAGS_RESERVED_3 0x16
21 #define FLAGS_RESERVED_4 0x64
22 #define FLAGS_RESERVED_5 0x128
23 #define FLAGS_RESERVED_6 0x255
25 typedef struct _flags_ {
33 int rflag; /* Richard Dictionary :-) */
36 int comprimir(char *src, char *dst, Uint32 pagesize, Uint32 volumesize, t_Flags *flags, char *staticmodel);
37 int descomprimir(char *src, char *dst);
39 char is_flags_on(unsigned char flags, unsigned char flag)
41 return (flags & flag);
44 char flag_on(unsigned char flags, unsigned char flag)
46 return (flags | flag);
49 char flag_off(unsigned char flags, unsigned char flag)
51 return (flags & (~flag));
54 int main(int argc, char* argv[])
56 long int volumesize = 0;
57 Uint32 pagesize = 32768; /* 32KB */
60 char *staticmodel = NULL;
62 memset(&flags, 0, sizeof(t_Flags));
64 while ((ch = getopt(argc, argv, "rscdzm:t:q:")) != -1) {
67 case 'c': flags.cflag = 1;
70 case 'd': flags.dflag = 1;
73 case 'z': flags.zflag = 1;
76 case 'm': flags.mflag = 1;
80 case 's': flags.sflag = 1;
83 case 't': flags.tflag = 1;
84 volumesize = atol(optarg);
87 case 'r': flags.rflag = 1;
89 case 'q': flags.qflag = 1;
92 case 0: pagesize = 1024; /* 1K */
94 case 1: pagesize = 2048; /* 2K */
96 case 2: pagesize = 4096; /* 4K */
98 case 3: pagesize = 8192; /* 8K */
100 case 4: pagesize = 16384; /* 16K */
102 case 5: pagesize = 32768; /* 32K */
104 case 6: pagesize = 65536; /* 64K */
106 case 7: pagesize = 131072; /* 128K */
108 case 8: pagesize = 262144; /* 256K */
110 case 9: pagesize = 524288; /* 512K */
112 default: pagesize = 0; /* error */
116 default: fprintf(stderr, "Usage: %s [-cdzsr][-q blksize][-t volsize][-m modeldumpfile] source target\n", argv[0]);
121 if ( (argc == 1) || (flags.cflag & flags.dflag) || !(flags.cflag | flags.dflag) || ((argc - optind) < 2) || (flags.mflag & flags.sflag)) {
122 fprintf(stderr, "Usage: %s [-cdzsr][-q compressionquality][-t volsize][-m modeldumpfile] source target\n", argv[0]);
125 if ((flags.tflag) && (volumesize <= 0l)) {
126 fprintf(stderr,"Error: The volume size must be a non-zero value\n");
129 if ((flags.qflag) && (pagesize <= 1u)) {
130 fprintf(stderr,"Error: El nivel de compresiĆ³n debe ser entre 0 (menor) y 9 (mayor).\n");
134 if (flags.cflag == 1) {
135 return comprimir(argv[optind], argv[optind+1], pagesize, volumesize, &flags, staticmodel);
138 if (flags.dflag == 1) {
139 return descomprimir(argv[optind], argv[optind+1]);
145 long fsize(const char* filename)
150 if (!(file = fopen(filename, "ab"))) return -1;
151 file_size = ftell(file);
156 int comprimir(char *src, char *dst, Uint32 pagesize, Uint32 volumesize, t_Flags *flags, char *staticmodel)
162 Uint32 i, j, total, k;
164 unsigned char *salida, *data;
166 unsigned char file_flags = 0;
169 /* Abrimos el archivo a comprimir y encodeamos bloques */
170 if ((fp = fopen(src, "rb")) == NULL) return 1;
172 /* Preparo el compresor huffman */
173 if ((shuff = shuff_init_encoder_bychunk(dst, volumesize*1024)) == NULL) return 1;
174 if (flags->mflag == 1) shuff_loadmodel(shuff, staticmodel);
176 /* Preparo el BS alocando mem para la Salida: V(vector) + K(colnum) */
177 data = malloc(sizeof(unsigned char)*pagesize);
178 salida = malloc(sizeof(unsigned char)*pagesize+sizeof(Uint32));
179 bs = bs_create(pagesize);
181 /* Guardamos el pagesize como header (huffencoded) */
182 shuff_scanfreq_chunk(shuff,(char*)&pagesize,sizeof(Uint32));
184 /* Guardamos cabecera para indicar si usamos ZG (huffencoded) */
186 file_flags = flag_on(file_flags, FLAGS_ZG);
188 file_flags = flag_on(file_flags, FLAGS_WS);
190 shuff_scanfreq_chunk(shuff, &file_flags, 1);
195 i = bs_readblock(fp, data, pagesize, flags->rflag);
199 /* Aplico BS guardando su resultado + el K en salida */
200 bs_solve(data, salida, bs, &k, i);
202 /* Le aplico el MTF a salida */
203 mtf = jacu_mtf(salida, i+sizeof(Uint32), &z, &z_len);
205 /* Guardo el z_len y el Z */
206 shuff_scanfreq_chunk(shuff,(char*)&z_len,sizeof(int));
207 shuff_scanfreq_chunk(shuff,z,z_len);
209 /* Si me lo piden, aplico ZG. */
212 unsigned char buff[2];
213 Uint32 total_size = i + sizeof(Uint32);
215 /* Guardo la salida del MTF con ceros agrupados (ZG) */
217 for (j = 0; j < total_size; ++j)
218 if ((len = zg_group(&zg, buff, mtf[j])))
219 shuff_scanfreq_chunk(shuff, buff, len);
221 /* Flusheo ultimo zgrouping */
222 if ((len = zg_group_finish(&zg,buff)))
223 shuff_scanfreq_chunk(shuff, buff, len);
225 /* Comprimo la salida del MTF */
226 shuff_scanfreq_chunk(shuff,mtf,i+sizeof(Uint32));
233 if (fclose(fp)) fprintf(stderr, "Error al cerrar archivo de entrada!\n");
238 /* Comprimo con Huffman */
239 shuff_encode_file(shuff);
240 if (flags->sflag == 1) shuff_savemodel(shuff);
241 /* Shutdown Huffman */
242 shuff_deinit_encoder(shuff);
246 printf("%s: %.04f bits/byte.\n", dst, vfsize(dst)*8.0f/fsize(src));
250 int descomprimir(char *src, char *dst)
254 Uint32 block_size = 0, k;
255 unsigned char *block, *mtf, *orig;
257 Uint32 z_len=0,moredata = 0,decoded = 0;
258 unsigned char file_flags = 0,retbytes = 0;
261 /* Inicializo el descompresor */
262 if ((shuff = shuff_init_decoder(src, NULL)) == NULL) return 1;
264 /* Abrimos el archivo de salida */
265 fp_out = fopen(dst, "wb");
267 /* Descomprimo primero que nada el pagesize utilizado para comprimir */
268 if (!(moredata = shuff_decode_chunk(shuff,(char*)&block_size,sizeof(Uint32),&decoded))) return 1;
270 /* Descomprimo byte que indica si se usa ZG */
271 if (!(moredata = shuff_decode_chunk(shuff, &file_flags, 1, &decoded))) return 1;
274 block = malloc(block_size*sizeof(unsigned char)+sizeof(Uint32));
275 orig = malloc(block_size*sizeof(unsigned char));
277 /* Descomprimimos de a chunks segun convenga */
279 if (block_size > 0) {
280 /* Descomprimo el Zlen y el Z del MTF*/
281 moredata = shuff_decode_chunk(shuff,(char*)&z_len,sizeof(int),&decoded);
282 z = malloc(sizeof(unsigned char)*z_len);
283 moredata = shuff_decode_chunk(shuff,z,z_len,&decoded);
285 /* Veo si se uso Zero Grouping para comprimir */
286 if (is_flags_on(file_flags, FLAGS_ZG)) {
288 unsigned char zgbuffer[255];
289 unsigned char zgbyte = 0;
291 Uint32 zgungrouped = 0;
292 /* Desagrupo bytes hasta completar la pagina or End of Source File */
295 /* Levanto un byte zerogrouped y lo paso por el zg_ungroup */
297 moredata = shuff_decode_chunk(shuff,&zgbyte,1,&decoded);
298 retbytes = zg_ungroup(&zg,zgbuffer,zgbyte);
299 /* Muevo del zgbuffer a mi bloque lo que corresponda */
300 while ((zgmoved < retbytes) && (zgungrouped < block_size+sizeof(Uint32))) {
301 block[zgungrouped++] = zgbuffer[zgmoved++];
303 } while ((moredata) && (zgungrouped < block_size+sizeof(Uint32)));
305 /* Me fijo si el ultimo byte procesado que me completo la pagina fue un 0 */
307 /* Leo un byte mas (un 0 seguro) y zg_ungroup cambiara su estado */
308 moredata = shuff_decode_chunk(shuff,&zgbyte,1,&decoded);
309 zg_ungroup(&zg,zgbuffer,zgbyte);
312 /* Normalizo variables para continuar en common code */
313 decoded = zgungrouped;
316 /* Levanto una salida de MTF */
317 moredata = shuff_decode_chunk(shuff,block,block_size+sizeof(Uint32),&decoded);
320 /* Le aplico MTF inverso a la salida de MTF levantada previamente */
321 mtf = jacu_mtf_inv(z, block, decoded);
323 /* Ya tengo la salida del BS, tonces levanto su K */
324 memcpy(&k, mtf, sizeof(Uint32));
326 /* Obtengo el chunk original aplicando BS Inverso */
327 bs_restore(orig, mtf+sizeof(Uint32), k, decoded - sizeof(Uint32));
329 decoded -= sizeof(Uint32);
330 if (is_flags_on(file_flags, FLAGS_WS)) {
331 orig = bs_finalblock(orig, decoded, &decoded);
334 fwrite(orig, decoded, sizeof(unsigned char), fp_out);
341 /* Close up files and free mem */
346 /* Shutdown Huffman */
347 shuff_deinit_decoder(shuff);