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 get_file_size(const char* filename);
15 int main(int argc, char* argv[])
24 long int volumesize = 0;
25 Uint32 pagesize = 32768; /* 32KB */
29 char *staticmodel = NULL;
31 while ((ch = getopt(argc, argv, "scdzm:t:q:")) != -1) {
51 volumesize = atol(optarg);
57 case 0: pagesize = 1024; /* 1K */
59 case 1: pagesize = 2048; /* 2K */
61 case 2: pagesize = 4096; /* 4K */
63 case 3: pagesize = 8192; /* 8K */
65 case 4: pagesize = 16384; /* 16K */
67 case 5: pagesize = 32768; /* 32K */
69 case 6: pagesize = 65536; /* 64K */
71 case 7: pagesize = 131072; /* 128K */
73 case 8: pagesize = 262144; /* 256K */
75 case 9: pagesize = 524288; /* 512K */
77 default: pagesize = 0; /* error */
81 default: fprintf(stderr, "Usage: %s [-cdzs][-q blksize][-t volsize][-m modeldumpfile] source target\n", argv[0]);
86 if ( (argc == 1) || (cflag & dflag) || !(cflag | dflag) || ((argc - optind) < 2) || (mflag & sflag)) {
87 fprintf(stderr, "Usage: %s [-cdzs][-q compressionquality][-t volsize][-m modeldumpfile] source target\n", argv[0]);
90 if ((tflag) && (volumesize <= 0l)) {
91 fprintf(stderr,"Error: The volume size must be a non-zero value\n");
94 if ((qflag) && (pagesize <= 1u)) {
95 fprintf(stderr,"Error: El nivel de compresiĆ³n debe ser entre 0 (menor) y 9 (mayor).\n");
102 Uint32 i, j, total, k;
104 unsigned char *salida, *data;
108 /* Preparo el compresor huffman */
109 if ((shuff = shuff_init_encoder_bychunk(argv[optind+1],volumesize*1024)) == NULL) return 1;
110 if (mflag == 1) shuff_loadmodel(shuff,staticmodel);
112 /* Preparo el BS alocando mem para la Salida: V(vector) + K(colnum) */
113 data = malloc(sizeof(unsigned char)*pagesize);
114 salida = malloc(sizeof(unsigned char)*pagesize+sizeof(Uint32));
115 bs = bs_create(pagesize);
117 /* Abrimos el archivo a comprimir y encodeamos bloques */
118 fp = fopen(argv[optind], "rb");
120 /* Guardamos el pagesize como header (huffencoded) */
121 shuff_scanfreq_chunk(shuff,(char*)&pagesize,sizeof(Uint32));
123 /* Guardamos cabecera para indicar si usamos ZG (huffencoded) */
124 if (zflag) shuff_scanfreq_chunk(shuff, "\001", 1);
125 else shuff_scanfreq_chunk(shuff, "\000", 1);
130 while ((!feof(fp)) && (i < pagesize)) {
131 data[i++] = fgetc(fp);
135 /* Saco un EOF que lee de mas */
138 /* Aplico BS guardando su resultado + el K en salida */
139 bs_solve(data, salida, bs, &k, i);
141 /* Le aplico el MTF a salida */
142 mtf = jacu_mtf(salida, i+sizeof(Uint32), &z, &z_len);
144 /* Guardo el z_len y el Z */
145 shuff_scanfreq_chunk(shuff,(char*)&z_len,sizeof(int));
146 shuff_scanfreq_chunk(shuff,z,z_len);
148 /* Si me lo piden, aplico ZG. */
151 unsigned char buff[2];
152 Uint32 total_size = i + sizeof(Uint32);
154 /* Guardo la salida del MTF con ceros agrupados (ZG) */
156 for (j = 0; j < total_size; ++j)
157 if ((len = zg_group(&zg, buff, mtf[j])))
158 shuff_scanfreq_chunk(shuff, buff, len);
160 /* Flusheo ultimo zgrouping */
161 if ((len = zg_group_finish(&zg,buff)))
162 shuff_scanfreq_chunk(shuff, buff, len);
164 /* Comprimo la salida del MTF */
165 shuff_scanfreq_chunk(shuff,mtf,i+sizeof(Uint32));
175 /* Comprimo con Huffman */
176 shuff_encode_file(shuff);
177 if (sflag == 1) shuff_savemodel(shuff);
178 /* Shutdown Huffman */
179 shuff_deinit_encoder(shuff);
183 printf("Comprimido a %.04f bpb.\n", get_file_size(argv[optind+1])*8.0/get_file_size(argv[optind]));
190 Uint32 block_size = 0,zgungrouped = 0, k;
191 unsigned char *block, *mtf, *orig;
192 unsigned char *z, *zgbuffer;
193 int zgmoved = 0,z_len=0,moredata = 0,decoded = 0;
194 unsigned char use_zg = 0,zgbyte = 0,retbytes = 0;
197 /* Inicializo el descompresor */
198 if ((shuff = shuff_init_decoder(argv[optind],NULL)) == NULL) return 1;
200 /* Abrimos el archivo de salida */
201 fp_out = fopen(argv[optind+1], "wb");
203 /* Descomprimo primero que nada el pagesize utilizado para comprimir */
204 if (!(moredata = shuff_decode_chunk(shuff,(char*)&block_size,sizeof(Uint32),&decoded))) return 1;
206 /* Descomprimo byte que indica si se usa ZG */
207 if (!(moredata = shuff_decode_chunk(shuff, &use_zg, 1, &decoded))) return 1;
208 if (use_zg) zg_init(&zg);
211 zgbuffer = malloc(sizeof(unsigned char)*256);
212 block = malloc(block_size*sizeof(unsigned char)+sizeof(Uint32));
213 orig = malloc(block_size*sizeof(unsigned char));
215 /* Descomprimimos de a chunks segun convenga */
217 if (block_size > 0) {
218 /* Descomprimo el Zlen y el Z del MTF*/
219 moredata = shuff_decode_chunk(shuff,(char*)&z_len,sizeof(int),&decoded);
220 z = malloc(sizeof(unsigned char)*z_len);
221 moredata = shuff_decode_chunk(shuff,z,z_len,&decoded);
223 /* Veo si se uso Zero Grouping para comprimir */
225 /* Desagrupo bytes hasta completar la pagina or End of Source File */
228 /* Levanto un byte zerogrouped y lo paso por el zg_ungroup */
230 moredata = shuff_decode_chunk(shuff,&zgbyte,1,&decoded);
231 retbytes = zg_ungroup(&zg,zgbuffer,zgbyte);
232 /* Muevo del zgbuffer a mi bloque lo que corresponda */
233 while ((zgmoved < retbytes) && (zgungrouped < block_size+sizeof(Uint32))) {
234 block[zgungrouped++] = zgbuffer[zgmoved++];
236 } while ((moredata) && (zgungrouped < block_size+sizeof(Uint32)));
238 /* Me fijo si el ultimo byte procesado que me completo la pagina fue un 0 */
240 /* Leo un byte mas (un 0 seguro) y zg_ungroup cambiara su estado */
241 moredata = shuff_decode_chunk(shuff,&zgbyte,1,&decoded);
242 zg_ungroup(&zg,zgbuffer,zgbyte);
245 /* Normalizo variables para continuar en common code */
246 decoded = zgungrouped;
249 /* Levanto una salida de MTF */
250 moredata = shuff_decode_chunk(shuff,block,block_size+sizeof(Uint32),&decoded);
253 /* Le aplico MTF inverso a la salida de MTF levantada previamente */
254 mtf = jacu_mtf_inv(z, block, decoded);
256 /* Ya tengo la salida del BS, tonces levanto su K */
257 memcpy(&k, mtf, sizeof(Uint32));
259 /* Obtengo el chunk original aplicando BS Inverso */
260 bs_restore(orig, mtf+sizeof(Uint32), k, decoded - sizeof(Uint32));
262 fwrite(orig, decoded - sizeof(Uint32), sizeof(unsigned char), fp_out);
269 /* Close up files and free mem */
275 /* Shutdown Huffman */
276 shuff_deinit_decoder(shuff);
283 long get_file_size(const char* filename)
288 if (!(file = fopen(filename, "ab"))) return -1;
289 file_size = ftell(file);