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_ {
23 int rflag; /* Richard Dictionary :-) */
26 int comprimir(char *src, char *dst, Uint32 pagesize, Uint32 volumesize, t_Flags *flags, char *staticmodel);
27 int descomprimir(char *src, char *dst);
29 int main(int argc, char* argv[])
31 long int volumesize = 0;
32 Uint32 pagesize = 32768; /* 32KB */
35 char *staticmodel = NULL;
37 memset(&flags, 0, sizeof(t_Flags));
39 while ((ch = getopt(argc, argv, "rscdzm:t:q:")) != -1) {
42 case 'c': flags.cflag = 1;
45 case 'd': flags.dflag = 1;
48 case 'z': flags.zflag = 1;
51 case 'm': flags.mflag = 1;
55 case 's': flags.sflag = 1;
58 case 't': flags.tflag = 1;
59 volumesize = atol(optarg);
62 case 'r': flags.rflag = 1;
64 case 'q': flags.qflag = 1;
67 case 0: pagesize = 1024; /* 1K */
69 case 1: pagesize = 2048; /* 2K */
71 case 2: pagesize = 4096; /* 4K */
73 case 3: pagesize = 8192; /* 8K */
75 case 4: pagesize = 16384; /* 16K */
77 case 5: pagesize = 32768; /* 32K */
79 case 6: pagesize = 65536; /* 64K */
81 case 7: pagesize = 131072; /* 128K */
83 case 8: pagesize = 262144; /* 256K */
85 case 9: pagesize = 524288; /* 512K */
87 default: pagesize = 0; /* error */
91 default: fprintf(stderr, "Usage: %s [-cdzsr][-q blksize][-t volsize][-m modeldumpfile] source target\n", argv[0]);
96 if ( (argc == 1) || (flags.cflag & flags.dflag) || !(flags.cflag | flags.dflag) || ((argc - optind) < 2) || (flags.mflag & flags.sflag)) {
97 fprintf(stderr, "Usage: %s [-cdzsr][-q compressionquality][-t volsize][-m modeldumpfile] source target\n", argv[0]);
100 if ((flags.tflag) && (volumesize <= 0l)) {
101 fprintf(stderr,"Error: The volume size must be a non-zero value\n");
104 if ((flags.qflag) && (pagesize <= 1u)) {
105 fprintf(stderr,"Error: El nivel de compresiĆ³n debe ser entre 0 (menor) y 9 (mayor).\n");
109 if (flags.cflag == 1) {
110 return comprimir(argv[optind], argv[optind+1], pagesize, volumesize, &flags, staticmodel);
113 if (flags.dflag == 1) {
114 return descomprimir(argv[optind], argv[optind+1]);
120 long fsize(const char* filename)
125 if (!(file = fopen(filename, "ab"))) return -1;
126 file_size = ftell(file);
131 int comprimir(char *src, char *dst, Uint32 pagesize, Uint32 volumesize, t_Flags *flags, char *staticmodel)
137 Uint32 i, j, total, k;
139 unsigned char *salida, *data;
143 /* Abrimos el archivo a comprimir y encodeamos bloques */
144 if ((fp = fopen(src, "rb")) == NULL) return 1;
146 /* Preparo el compresor huffman */
147 if ((shuff = shuff_init_encoder_bychunk(dst, volumesize*1024)) == NULL) return 1;
148 if (flags->mflag == 1) shuff_loadmodel(shuff, staticmodel);
150 /* Preparo el BS alocando mem para la Salida: V(vector) + K(colnum) */
151 data = malloc(sizeof(unsigned char)*pagesize);
152 salida = malloc(sizeof(unsigned char)*pagesize+sizeof(Uint32));
153 bs = bs_create(pagesize);
155 /* Guardamos el pagesize como header (huffencoded) */
156 shuff_scanfreq_chunk(shuff,(char*)&pagesize,sizeof(Uint32));
158 /* Guardamos cabecera para indicar si usamos ZG (huffencoded) */
160 shuff_scanfreq_chunk(shuff, "\001", 1);
162 shuff_scanfreq_chunk(shuff, "\000", 1);
167 i = bs_readblock(fp, data, pagesize, flags->rflag);
171 /* Aplico BS guardando su resultado + el K en salida */
172 bs_solve(data, salida, bs, &k, i);
174 /* Le aplico el MTF a salida */
175 mtf = jacu_mtf(salida, i+sizeof(Uint32), &z, &z_len);
177 /* Guardo el z_len y el Z */
178 shuff_scanfreq_chunk(shuff,(char*)&z_len,sizeof(int));
179 shuff_scanfreq_chunk(shuff,z,z_len);
181 /* Si me lo piden, aplico ZG. */
184 unsigned char buff[2];
185 Uint32 total_size = i + sizeof(Uint32);
187 /* Guardo la salida del MTF con ceros agrupados (ZG) */
189 for (j = 0; j < total_size; ++j)
190 if ((len = zg_group(&zg, buff, mtf[j])))
191 shuff_scanfreq_chunk(shuff, buff, len);
193 /* Flusheo ultimo zgrouping */
194 if ((len = zg_group_finish(&zg,buff)))
195 shuff_scanfreq_chunk(shuff, buff, len);
197 /* Comprimo la salida del MTF */
198 shuff_scanfreq_chunk(shuff,mtf,i+sizeof(Uint32));
205 if (fclose(fp)) fprintf(stderr, "Error al cerrar archivo de entrada!\n");
210 /* Comprimo con Huffman */
211 shuff_encode_file(shuff);
212 if (flags->sflag == 1) shuff_savemodel(shuff);
213 /* Shutdown Huffman */
214 shuff_deinit_encoder(shuff);
218 printf("%s: %.04f bits/byte.\n", dst, vfsize(dst)*8.0f/fsize(src));
222 int descomprimir(char *src, char *dst)
226 Uint32 block_size = 0, k;
227 unsigned char *block, *mtf, *orig;
229 int z_len=0,moredata = 0,decoded = 0;
230 unsigned char use_zg = 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;
246 block = malloc(block_size*sizeof(unsigned char)+sizeof(Uint32));
247 orig = malloc(block_size*sizeof(unsigned char));
249 /* Descomprimimos de a chunks segun convenga */
251 if (block_size > 0) {
252 /* Descomprimo el Zlen y el Z del MTF*/
253 moredata = shuff_decode_chunk(shuff,(char*)&z_len,sizeof(int),&decoded);
254 z = malloc(sizeof(unsigned char)*z_len);
255 moredata = shuff_decode_chunk(shuff,z,z_len,&decoded);
257 /* Veo si se uso Zero Grouping para comprimir */
260 unsigned char zgbuffer[255];
261 unsigned char zgbyte = 0;
263 Uint32 zgungrouped = 0;
264 /* Desagrupo bytes hasta completar la pagina or End of Source File */
267 /* Levanto un byte zerogrouped y lo paso por el zg_ungroup */
269 moredata = shuff_decode_chunk(shuff,&zgbyte,1,&decoded);
270 retbytes = zg_ungroup(&zg,zgbuffer,zgbyte);
271 /* Muevo del zgbuffer a mi bloque lo que corresponda */
272 while ((zgmoved < retbytes) && (zgungrouped < block_size+sizeof(Uint32))) {
273 block[zgungrouped++] = zgbuffer[zgmoved++];
275 } while ((moredata) && (zgungrouped < block_size+sizeof(Uint32)));
277 /* Me fijo si el ultimo byte procesado que me completo la pagina fue un 0 */
279 /* Leo un byte mas (un 0 seguro) y zg_ungroup cambiara su estado */
280 moredata = shuff_decode_chunk(shuff,&zgbyte,1,&decoded);
281 zg_ungroup(&zg,zgbuffer,zgbyte);
284 /* Normalizo variables para continuar en common code */
285 decoded = zgungrouped;
288 /* Levanto una salida de MTF */
289 moredata = shuff_decode_chunk(shuff,block,block_size+sizeof(Uint32),&decoded);
292 /* Le aplico MTF inverso a la salida de MTF levantada previamente */
293 mtf = jacu_mtf_inv(z, block, decoded);
295 /* Ya tengo la salida del BS, tonces levanto su K */
296 memcpy(&k, mtf, sizeof(Uint32));
298 /* Obtengo el chunk original aplicando BS Inverso */
299 bs_restore(orig, mtf+sizeof(Uint32), k, decoded - sizeof(Uint32));
301 fwrite(orig, decoded - sizeof(Uint32), sizeof(unsigned char), fp_out);
308 /* Close up files and free mem */
313 /* Shutdown Huffman */
314 shuff_deinit_decoder(shuff);