1 /*----------------------------------------------------------------------------
2 * jacu - Just Another Compression Utility
3 *----------------------------------------------------------------------------
4 * This file is part of jacu.
6 * jacu is free software; you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation; either version 2 of the License, or (at your option) any later
11 * jacu is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License along
17 * with jacu; if not, write to the Free Software Foundation, Inc., 59 Temple
18 * Place, Suite 330, Boston, MA 02111-1307 USA
19 *----------------------------------------------------------------------------
22 #include "blocksorting/bs.h"
24 #include "zerogrouping/zerogrouping.h"
25 #include "statichuff/statichuff.h"
26 #include "vfile/vfile.h"
27 #include "vfile/common.h"
32 long fsize(const char* filename);
34 /* Flags del archivo comprimido */
37 #define FLAGS_RESERVED_1 0x4
38 #define FLAGS_RESERVED_2 0x8
39 #define FLAGS_RESERVED_3 0x16
40 #define FLAGS_RESERVED_4 0x64
41 #define FLAGS_RESERVED_5 0x128
42 #define FLAGS_RESERVED_6 0x255
44 typedef struct _flags_ {
52 int rflag; /* Richard Dictionary :-) */
55 int comprimir(char *src, char *dst, Uint32 pagesize, Uint32 volumesize, t_Flags *flags, char *staticmodel);
56 int descomprimir(char *src, char *dst);
58 char is_flags_on(unsigned char flags, unsigned char flag)
60 return (flags & flag);
63 char flag_on(unsigned char flags, unsigned char flag)
65 return (flags | flag);
68 char flag_off(unsigned char flags, unsigned char flag)
70 return (flags & (~flag));
73 int main(int argc, char* argv[])
75 long int volumesize = 0;
76 Uint32 pagesize = 32768; /* 32KB */
79 char *staticmodel = NULL;
81 memset(&flags, 0, sizeof(t_Flags));
83 while ((ch = getopt(argc, argv, "rscdzm:t:q:")) != -1) {
86 case 'c': flags.cflag = 1;
89 case 'd': flags.dflag = 1;
92 case 'z': flags.zflag = 1;
95 case 'm': flags.mflag = 1;
99 case 's': flags.sflag = 1;
102 case 't': flags.tflag = 1;
103 volumesize = atol(optarg);
106 case 'r': flags.rflag = 1;
108 case 'q': flags.qflag = 1;
109 switch (atoi(optarg))
111 case 0: pagesize = 1024; /* 1K */
113 case 1: pagesize = 2048; /* 2K */
115 case 2: pagesize = 4096; /* 4K */
117 case 3: pagesize = 8192; /* 8K */
119 case 4: pagesize = 16384; /* 16K */
121 case 5: pagesize = 32768; /* 32K */
123 case 6: pagesize = 65536; /* 64K */
125 case 7: pagesize = 131072; /* 128K */
127 case 8: pagesize = 262144; /* 256K */
129 case 9: pagesize = 524288; /* 512K */
131 default: pagesize = 0; /* error */
135 default: fprintf(stderr, "Usage: %s [-cdzsr][-q blksize][-t volsize][-m modeldumpfile] source target\n", argv[0]);
140 if ( (argc == 1) || (flags.cflag & flags.dflag) || !(flags.cflag | flags.dflag) || ((argc - optind) < 2) || (flags.mflag & flags.sflag)) {
141 fprintf(stderr, "Usage: %s [-cdzsr][-q compressionquality][-t volsize][-m modeldumpfile] source target\n", argv[0]);
144 if ((flags.tflag) && (volumesize <= 0l)) {
145 fprintf(stderr,"Error: The volume size must be a non-zero value\n");
148 if ((flags.qflag) && (pagesize <= 1u)) {
149 fprintf(stderr,"Error: El nivel de compresiĆ³n debe ser entre 0 (menor) y 9 (mayor).\n");
153 if (flags.cflag == 1) {
154 return comprimir(argv[optind], argv[optind+1], pagesize, volumesize, &flags, staticmodel);
157 if (flags.dflag == 1) {
158 return descomprimir(argv[optind], argv[optind+1]);
164 long fsize(const char* filename)
169 if (!(file = fopen(filename, "ab"))) return -1;
170 file_size = ftell(file);
175 int comprimir(char *src, char *dst, Uint32 pagesize, Uint32 volumesize, t_Flags *flags, char *staticmodel)
181 Uint32 i, j, total, k;
183 unsigned char *salida, *data;
185 unsigned char file_flags = 0;
188 /* Abrimos el archivo a comprimir y encodeamos bloques */
189 if ((fp = fopen(src, "rb")) == NULL) return 1;
191 /* Preparo el compresor huffman */
192 if ((shuff = shuff_init_encoder_bychunk(dst, volumesize*1024)) == NULL) return 1;
193 if (flags->mflag == 1) shuff_loadmodel(shuff, staticmodel);
195 /* Preparo el BS alocando mem para la Salida: V(vector) + K(colnum) */
196 data = malloc(sizeof(unsigned char)*pagesize);
197 salida = malloc(sizeof(unsigned char)*pagesize+sizeof(Uint32));
198 bs = bs_create(pagesize);
200 /* Guardamos el pagesize como header (huffencoded) */
201 shuff_scanfreq_chunk(shuff,(char*)&pagesize,sizeof(Uint32));
203 /* Guardamos cabecera para indicar si usamos ZG (huffencoded) */
205 file_flags = flag_on(file_flags, FLAGS_ZG);
207 file_flags = flag_on(file_flags, FLAGS_WS);
209 shuff_scanfreq_chunk(shuff, &file_flags, 1);
214 i = bs_readblock(fp, data, pagesize, flags->rflag);
218 /* Aplico BS guardando su resultado + el K en salida */
219 bs_solve(data, salida, bs, &k, i);
221 /* Le aplico el MTF a salida */
222 mtf = jacu_mtf(salida, i+sizeof(Uint32), &z, &z_len);
224 /* Guardo el z_len y el Z */
225 shuff_scanfreq_chunk(shuff,(char*)&z_len,sizeof(int));
226 shuff_scanfreq_chunk(shuff,z,z_len);
228 /* Si me lo piden, aplico ZG. */
231 unsigned char buff[2];
232 Uint32 total_size = i + sizeof(Uint32);
234 /* Guardo la salida del MTF con ceros agrupados (ZG) */
236 for (j = 0; j < total_size; ++j)
237 if ((len = zg_group(&zg, buff, mtf[j])))
238 shuff_scanfreq_chunk(shuff, buff, len);
240 /* Flusheo ultimo zgrouping */
241 if ((len = zg_group_finish(&zg,buff)))
242 shuff_scanfreq_chunk(shuff, buff, len);
244 /* Comprimo la salida del MTF */
245 shuff_scanfreq_chunk(shuff,mtf,i+sizeof(Uint32));
252 if (fclose(fp)) fprintf(stderr, "Error al cerrar archivo de entrada!\n");
257 /* Comprimo con Huffman */
258 shuff_encode_file(shuff);
259 if (flags->sflag == 1) shuff_savemodel(shuff);
260 /* Shutdown Huffman */
261 shuff_deinit_encoder(shuff);
265 printf("%s: %.04f bits/byte.\n", dst, vfsize(dst)*8.0f/fsize(src));
269 int descomprimir(char *src, char *dst)
273 Uint32 block_size = 0, k;
274 unsigned char *block, *mtf, *orig;
276 Uint32 z_len=0,moredata = 0,decoded = 0;
277 unsigned char file_flags = 0,retbytes = 0;
280 /* Inicializo el descompresor */
281 if ((shuff = shuff_init_decoder(src, NULL)) == NULL) return 1;
283 /* Abrimos el archivo de salida */
284 fp_out = fopen(dst, "wb");
286 /* Descomprimo primero que nada el pagesize utilizado para comprimir */
287 if (!(moredata = shuff_decode_chunk(shuff,(char*)&block_size,sizeof(Uint32),&decoded))) return 1;
289 /* Descomprimo byte que indica si se usa ZG */
290 if (!(moredata = shuff_decode_chunk(shuff, &file_flags, 1, &decoded))) return 1;
293 block = malloc(block_size*sizeof(unsigned char)+sizeof(Uint32));
294 orig = malloc(block_size*sizeof(unsigned char));
296 /* Descomprimimos de a chunks segun convenga */
298 if (block_size > 0) {
299 /* Descomprimo el Zlen y el Z del MTF*/
300 moredata = shuff_decode_chunk(shuff,(char*)&z_len,sizeof(int),&decoded);
301 z = malloc(sizeof(unsigned char)*z_len);
302 moredata = shuff_decode_chunk(shuff,z,z_len,&decoded);
304 /* Veo si se uso Zero Grouping para comprimir */
305 if (is_flags_on(file_flags, FLAGS_ZG)) {
307 unsigned char zgbuffer[255];
308 unsigned char zgbyte = 0;
310 Uint32 zgungrouped = 0;
311 /* Desagrupo bytes hasta completar la pagina or End of Source File */
314 /* Levanto un byte zerogrouped y lo paso por el zg_ungroup */
316 moredata = shuff_decode_chunk(shuff,&zgbyte,1,&decoded);
317 retbytes = zg_ungroup(&zg,zgbuffer,zgbyte);
318 /* Muevo del zgbuffer a mi bloque lo que corresponda */
319 while ((zgmoved < retbytes) && (zgungrouped < block_size+sizeof(Uint32))) {
320 block[zgungrouped++] = zgbuffer[zgmoved++];
322 } while ((moredata) && (zgungrouped < block_size+sizeof(Uint32)));
324 /* Me fijo si el ultimo byte procesado que me completo la pagina fue un 0 */
326 /* Leo un byte mas (un 0 seguro) y zg_ungroup cambiara su estado */
327 moredata = shuff_decode_chunk(shuff,&zgbyte,1,&decoded);
328 zg_ungroup(&zg,zgbuffer,zgbyte);
331 /* Normalizo variables para continuar en common code */
332 decoded = zgungrouped;
335 /* Levanto una salida de MTF */
336 moredata = shuff_decode_chunk(shuff,block,block_size+sizeof(Uint32),&decoded);
339 /* Le aplico MTF inverso a la salida de MTF levantada previamente */
340 mtf = jacu_mtf_inv(z, block, decoded);
342 /* Ya tengo la salida del BS, tonces levanto su K */
343 memcpy(&k, mtf, sizeof(Uint32));
345 /* Obtengo el chunk original aplicando BS Inverso */
346 bs_restore(orig, mtf+sizeof(Uint32), k, decoded - sizeof(Uint32));
348 decoded -= sizeof(Uint32);
349 if (is_flags_on(file_flags, FLAGS_WS)) {
350 orig = bs_finalblock(orig, decoded, &decoded);
353 fwrite(orig, decoded, sizeof(unsigned char), fp_out);
360 /* Close up files and free mem */
365 /* Shutdown Huffman */
366 shuff_deinit_decoder(shuff);