2 #include "blocksorting/bs.h"
4 #include "zerogrouping/zerogrouping.h"
5 #include "statichuff/statichuff.h"
6 #include "vfile/vfile.h"
11 long get_file_size(const char* filename);
13 int main(int argc, char* argv[])
20 long int volumesize = 0;
21 size_t pagesize = 32768; /* 32KB */
25 while ((ch = getopt(argc, argv, "cdzt:p:")) != -1) {
38 volumesize = atol(optarg);
42 pagesize = atoi(optarg);
45 default: fprintf(stderr, "Usage: %s [-cdpt] sourcefile targetfile\n", argv[0]);
50 if ( (argc == 1) || (cflag & dflag) || !(cflag | dflag) || ((argc - optind) < 2) ) {
51 fprintf(stderr, "Usage: %s [-cdt] sourcefile targetfile\n", argv[0]);
54 if ((tflag) && (volumesize <= 0l)) {
55 fprintf(stderr,"Error: The volume size must be a non-zero value\n");
58 if ((pflag) && (pagesize <= 1u)) {
59 fprintf(stderr,"Error: El tamaño de página debe ser mayor a 1 byte.\n");
65 /* No me gusta el tmpfile ... es para probar como anda todo junto */
67 unsigned long int i, j, total, k;
69 char *salida, *data, c;
70 data = malloc(sizeof(char)*pagesize);
71 /* Reservo lugar tambien para guardar el k y el tamaño */
72 salida = malloc(sizeof(char)*(pagesize)+sizeof(unsigned long int)*2);
73 bs = bs_create(pagesize);
75 fp = fopen(argv[optind], "rb");
76 fp_out = fopen("tmp.comp", "wb");
82 while ((!feof(fp)) && (i < pagesize)) {
89 bs_solve(data, salida, bs, &k, i);
91 /* Le aplico el MTF, salteo el tamaño del bloque para que no se pierda. */
92 mtf = jacu_mtf(salida+sizeof(unsigned long int), i+sizeof(unsigned long int));
94 /* Si me lo piden, aplico ZG. */
101 for (j = 0; j < i; ++j)
102 if ((len = zg_group(&zg, buff, mtf[j]))) fwrite(buff, 1, len, fp_out);
107 fputc(mtf[j], fp_out);
117 /* Comprimo con huffman */
118 i = shuff_encode_file("tmp.comp",argv[optind+1], volumesize);
120 /* borro el temporal */
124 printf("Comprimido a %.04f bpb.\n", get_file_size(argv[optind+1])*8.0/get_file_size(argv[optind]));
132 unsigned long int block_size, k;
133 char *block, *mtf, *orig;
136 shuff_decode_file(argv[optind], "tmp.comp"); /*argv[optind+1]);*/
137 fp_in = vfopen("tmp.comp", "r", 0);
138 fp_out = fopen(argv[optind+1], "wb");
140 while (!vfeof(fp_in)) {
142 vfread(&block_size, sizeof(unsigned long int), 1, fp_in);
143 if (block_size > 0) {
144 block = malloc((block_size+sizeof(unsigned long int))*sizeof(char));
145 orig = malloc(block_size*sizeof(char));
146 vfread(block, block_size, sizeof(char), fp_in);
148 mtf = jacu_mtf_inv(block, /*XXX NO LO TENGO XXX */pos, block_size);
150 memcpy(&k, block, sizeof(unsigned long int));
152 bs_restore(orig, block+sizeof(unsigned long int), k, block_size);
154 fwrite(orig, block_size, sizeof(char), fp_out);
167 long get_file_size(const char* filename)
172 if (!(file = fopen(filename, "ab"))) return -1;
173 file_size = ftell(file);