2 #include "blocksorting/bs.h"
4 #include "zerogrouping/zerogrouping.h"
5 #include "statichuff/statichuff.h"
6 #include "vfile/vfile.h"
7 #include "vfile/common.h"
12 long get_file_size(const char* filename);
14 int main(int argc, char* argv[])
21 long int volumesize = 0;
22 size_t pagesize = 32768; /* 32KB */
26 while ((ch = getopt(argc, argv, "cdzt:q:")) != -1) {
39 volumesize = atol(optarg);
45 case 0: pagesize = 1024; /* 1K */
47 case 1: pagesize = 2048; /* 2K */
49 case 2: pagesize = 4096; /* 4K */
51 case 3: pagesize = 8192; /* 8K */
53 case 4: pagesize = 16384; /* 16K */
55 case 5: pagesize = 32768; /* 32K */
57 case 6: pagesize = 65536; /* 64K */
59 case 7: pagesize = 131072; /* 128K */
61 case 8: pagesize = 262144; /* 256K */
63 case 9: pagesize = 524288; /* 512K */
65 default: pagesize = 0; /* error */
69 default: fprintf(stderr, "Usage: %s [-cdpt] sourcefile targetfile\n", argv[0]);
74 if ( (argc == 1) || (cflag & dflag) || !(cflag | dflag) || ((argc - optind) < 2) ) {
75 fprintf(stderr, "Usage: %s [-cdt] sourcefile targetfile\n", argv[0]);
78 if ((tflag) && (volumesize <= 0l)) {
79 fprintf(stderr,"Error: The volume size must be a non-zero value\n");
82 if ((qflag) && (pagesize <= 1u)) {
83 fprintf(stderr,"Error: El nivel de compresión debe ser entre 0 (menor) y 9 (mayor).\n");
89 /* No me gusta el tmpfile ... es para probar como anda todo junto */
91 Uint32 i, j, total, k;
93 char *salida, *data, c;
97 data = malloc(sizeof(char)*pagesize);
98 /* Reservo lugar tambien para guardar el k y el tamaño */
99 salida = malloc(sizeof(char)*pagesize+sizeof(Uint32)*2);
100 bs = bs_create(pagesize);
102 fp = fopen(argv[optind], "rb");
103 fp_out = fopen("tmp.comp", "wb");
109 while ((!feof(fp)) && (i < pagesize)) {
110 data[i++] = fgetc(fp);
114 /* Saco un EOF que lee de mas */
118 bs_solve(data, salida, bs, &k, i);
120 /*printf("BS k=%ld\n", *(Uint32 *)(salida+sizeof(Uint32)));
121 printf("PageSize = %ld\n", *(Uint32 *)salida);
123 printf("Antes de MTF = %ld [", i);
126 for(ii=0; ii<(i+sizeof(Uint32)); ii++)
127 printf("(%c)", salida[ii+sizeof(Uint32)]);
130 /* Le aplico el MTF, salteo el tamaño del bloque para que no se pierda. */
131 mtf = jacu_mtf(salida+sizeof(Uint32), i+sizeof(Uint32), &z, &z_len);
133 printf("MTF Z (len=%d) = [", z_len);
136 for(ii=0; ii<z_len; ii++)
137 printf("(%c)", z[ii]);
141 /* Si me lo piden, aplico ZG. */
147 /* TODO HACER LO MISMO QUE EN EL ELSE XXX */
148 for (j = 0; j < i; ++j)
149 if ((len = zg_group(&zg, buff, mtf[j]))) fwrite(buff, 1, len, fp_out);
151 /* Guardo el PageSize */
152 fwrite(salida, sizeof(Uint32), 1, fp_out);
154 /* Guardo el Z len y el Z */
155 fwrite(&z_len, sizeof(int), 1, fp_out);
156 fwrite(z, z_len, sizeof(char), fp_out);
158 /* Guardo la salida del MTF */
159 /*printf("Despues de MTF : [");*/
160 for(j=0; j<(i+sizeof(Uint32)); j++) {
161 fputc(mtf[j], fp_out);
163 fputc(mtf[j], stdout);
172 printf("Total Bytes read = %ld\n", total);
178 /* Comprimo con huffman */
179 i = shuff_encode_file("tmp.comp", argv[optind+1], volumesize);
181 /* borro el temporal */
185 printf("Comprimido a %.04f bpb.\n", get_file_size(argv[optind+1])*8.0/get_file_size(argv[optind]));
193 Uint32 block_size, k;
194 char *block, *mtf, *orig;
198 shuff_decode_file(argv[optind], "tmp.comp"); /*argv[optind+1]);*/
199 fp_in = fopen("tmp.comp", "rb");
200 fp_out = fopen(argv[optind+1], "wb");
202 while (!feof(fp_in)) {
204 fread(&block_size, sizeof(Uint32), 1, fp_in);
205 if (block_size > 0) {
206 printf("PageSize = %ld\n", block_size);
207 fread(&z_len, sizeof(int), 1, fp_in);
208 z = malloc(sizeof(char)*z_len);
209 fread(z, z_len, sizeof(char), fp_in);
211 /*printf("MTF Z (len=%d) = [", z_len);
214 for(ii=0; ii<z_len; ii++)
215 printf("(%c)", z[ii]);
220 block = malloc(block_size*sizeof(char)+sizeof(Uint32));
221 orig = malloc(block_size*sizeof(char));
222 fread(block, block_size+sizeof(Uint32), sizeof(char), fp_in);
224 /*printf("Antes MTF_inv = [");
227 for(ii=0; ii<block_size+sizeof(Uint32); ii++)
228 printf("(%c)", block[ii]);
231 /* Hago el MTF inverso */
232 mtf = jacu_mtf_inv(z, block, block_size*sizeof(char)+sizeof(Uint32));
234 /*printf("Luego de MTF Inv= [");
237 for(ii=0; ii<block_size+sizeof(Uint32); ii++)
238 printf("(%c)", mtf[ii]);
242 /* Luego de hacer el MTF inverso ya puedo recuperar el k */
243 memcpy(&k, mtf, sizeof(Uint32));
245 /*printf("Restored : k=%ld\n", k);*/
246 bs_restore(orig, mtf+sizeof(Uint32), k, block_size);
248 fwrite(orig, block_size, sizeof(char), fp_out);
261 long get_file_size(const char* filename)
266 if (!(file = fopen(filename, "ab"))) return -1;
267 file_size = ftell(file);