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 *----------------------------------------------------------------------------
23 #include "statichuff.h"
27 /** Coloca un bit en un buffer statico */
28 void putbit(char bit, char restart, char flush, VFILE *fp)
30 static unsigned long int bits_buffer = 0;
31 static unsigned char bits_used = 0;
33 /* me obligan a emitir el output */
34 if ((flush == 1) && (bits_used > 0)) {
35 bits_buffer = bits_buffer << ((sizeof(unsigned long int)*8) - bits_used);
36 vfwrite(&bits_buffer,sizeof(unsigned long int),1,fp);
41 /* me indican que comienza un nuevo output */
46 /* inserto el bit en el buffer */
47 bits_buffer = bits_buffer << 1;
51 /* lleno el buffer, escribo */
52 if (bits_used == 32) {
53 vfwrite(&bits_buffer,sizeof(unsigned long int),1,fp);
60 /** Realiza la copia de los datos de un nodo de huffman a otro */
61 void shuff_cpynode(SHUFFNODE *node1, SHUFFNODE *node2)
63 node1->symbol = node2->symbol;
64 node1->freq = node2->freq;
65 node1->lchild = node2->lchild;
66 node1->rchild = node2->rchild;
69 /** Realiza una comparacion de dos nodos de huffman */
70 int shuff_compnode(const void *node1, const void *node2)
72 if (((SHUFFNODE*)node1)->freq < ((SHUFFNODE*)node2)->freq) return 1;
73 if (((SHUFFNODE*)node1)->freq > ((SHUFFNODE*)node2)->freq) return -1;
77 /** Destruye un arbol de huffman recursivamente */
78 void shuff_destroy_tree(SHUFFNODE *node) {
79 /* Si llegue a una hoja, destruyo y vuelvo */
80 if (node->symbol < 256) {
85 /* Desciendo por izq, luego por derecha y luego libero */
86 shuff_destroy_tree(node->lchild);
87 shuff_destroy_tree(node->rchild);
93 /** Reescala las frecuencias de huffman a la mitad */
94 int shuff_rescalefreq(t_freq *freqtable)
99 /* Divido por la mitad las frecuencias, asegurando de no perder */
100 for (i = 0; i < 256; i++) {
101 freqtable[i] = (freqtable[i] >> 2) | 1;
102 totalfreq += freqtable[i];
108 /** Escanea las frecuencias de un chunk de datos */
109 int shuff_scanfreq_chunk(HUFF_STATE *chunkshuff, char* chunk, int chunksize)
113 unsigned char symbol = 0;
115 /* Contamos las frecuencias del chunk a menos que se use un canonico */
116 if (!chunkshuff->canonic) {
117 for (i = 0; i < chunksize; ++i) {
119 chunkshuff->freqtable[symbol] += 1;
120 chunkshuff->sumfreq += 1;
122 /* Si llegue al tope de freq acumulada, halve em */
123 if (chunkshuff->sumfreq == 14930352)
124 chunkshuff->sumfreq = shuff_rescalefreq(chunkshuff->freqtable);
128 /* Dumpeamos el chunk en el temporal homero */
129 fwrite(chunk,chunksize,1,chunkshuff->coderfp);
134 /** Escanea las frecuencias de un archivo y genera el modelo */
135 int shuff_scanfreq(char *inputfile, t_freq *freqtable)
142 /* Inicializamos la tabla de frecuencias */
143 for (i = 0; i < 256; ++i) freqtable[i] = 0;
145 /* Abrimos el file */
146 if ((fp = fopen(inputfile,"r")) == NULL) return 0;
148 /* Contamos las frecuencias */
150 if (symbol == EOF) continue;
155 /* Si llegue al tope de freq acumulada, halve em */
156 if (sumfreq == 14930352)
157 sumfreq = shuff_rescalefreq(freqtable);
164 /** Genera un input list que sera utilizada para generar el arbol */
165 SHUFFNODE *shuff_buildlist(t_freq *freqtable, int *nonzerofreqs)
167 int i,j = 0,nonzero = 0;
168 SHUFFNODE *inputlist;
170 /* Calculo cuantas frequencias > 0 hay y creo la tabla */
171 for (i = 0; i < 256; ++i) if (freqtable[i] > 0) nonzero++;
172 inputlist = (SHUFFNODE*)malloc(sizeof(SHUFFNODE)*nonzero);
174 /* Cargo la inputlist del huffman solo con freqs > 0 */
175 for (i = 0; i < 256; ++i)
176 if (freqtable[i] > 0) {
177 inputlist[j].symbol = i;
178 inputlist[j].freq = freqtable[i];
179 inputlist[j].lchild = NULL;
180 inputlist[j].rchild = NULL;
184 *nonzerofreqs = nonzero;
188 /** Genera el arbol de huffman en base a la tabla de frecuencias */
189 SHUFFNODE *shuff_buildtree(t_freq *ftable)
191 SHUFFNODE *lastsymbol;
192 SHUFFNODE *node1,*node2,*root;
193 SHUFFNODE *inputlist;
196 /* Genero la input list en base a la cual genera el arbol */
197 inputlist = shuff_buildlist(ftable, &freqcount);
198 lastsymbol = inputlist+(freqcount-1);
200 while (lastsymbol > inputlist) {
201 /* Ordeno la lista por frecuencia descendente */
202 qsort(inputlist,freqcount,sizeof(SHUFFNODE),shuff_compnode);
203 /* Tomo los ultimos dos elementos, generando dos nodos del arbol */
204 node1 = (SHUFFNODE*)malloc(sizeof(SHUFFNODE));
205 node2 = (SHUFFNODE*)malloc(sizeof(SHUFFNODE));
206 shuff_cpynode(node1,lastsymbol-1);
207 shuff_cpynode(node2,lastsymbol);
209 /* Nodo ficticio con la suma de las probs y los ptros a childs */
210 lastsymbol->symbol = 256;
211 lastsymbol->freq = node1->freq + node2->freq;
212 lastsymbol->lchild = node1;
213 lastsymbol->rchild = node2;
217 /* Copio la raiz para poder liberar la lista sin perderla */
218 root = (SHUFFNODE*)malloc(sizeof(SHUFFNODE));
219 shuff_cpynode(root,lastsymbol);
224 /* Devuelvo el puntero a la raiz del arbol de huffman */
228 /** Imprime los codigos prefijos generados para los symbolos */
229 void shuff_printcodes(SHUFFCODE *codetable,t_freq *freqtable)
232 unsigned short int auxcode;
235 for (i = 0; i < 256; ++i) {
236 if (codetable[i].codelength > 0) {
237 auxcode = codetable[i].code;
238 printf("Symbol:%i Freq: %lu Code:",i,freqtable[i]);
239 for (j = codetable[i].codelength-1; j >= 0; --j) {
240 auxcode = codetable[i].code;
241 auxcode = auxcode >> j;
245 printf(" Length:%i\n",codetable[i].codelength);
250 /** Inicializa la tabla de codigos prefijos */
251 void shuff_zerocodes(SHUFFCODE *table)
255 /* Inicializo los codigos prefijos */
256 for (i = 0; i < 256; ++i) {
258 table[i].codelength = 0;
262 /** Genera la tabla de codigos prefijos en base al árbol de huffman */
263 void shuff_buildcodes(SHUFFCODE *table, SHUFFNODE *node, int level, int code)
265 if (node->symbol < 256) {
266 /* Guardo el codigo en la tabla */
267 table[node->symbol].code = code;
268 table[node->symbol].codelength = level;
272 shuff_buildcodes(table,node->lchild,level+1,code);
274 shuff_buildcodes(table,node->rchild,level+1,code);
278 /** Realiza la compresion / encoding efectivo de un archivo */
279 int shuff_encode_symbols(HUFF_STATE *shuff, SHUFFCODE *ctable)
284 unsigned long int sourcesize;
286 SHUFFCODE symbolcode;
288 /* Abrimos el source y el destino */
289 if (shuff->coderfp != NULL) {
290 fclose(shuff->coderfp); /* close bychunk temp file */
291 shuff->coderfp = NULL;
293 if ((fpsource = fopen(shuff->sourcefile,"r")) == NULL) return 0;
294 if ((fpdest = vfopen(shuff->targetfile,"w",shuff->volsize)) == NULL) return 0;
296 /* Guardamos el size el archivo original e inputlist como header */
297 fseek(fpsource,0,SEEK_END);
298 sourcesize = ftell(fpsource);
299 vfwrite(&sourcesize,sizeof(unsigned long int),1,fpdest);
300 vfwrite(shuff->freqtable,sizeof(t_freq),256,fpdest);
303 fseek(fpsource,0,SEEK_SET);
304 while (!feof(fpsource)) {
305 /* Levanto un symbolo (byte) */
306 symbol = fgetc(fpsource);
307 if (symbol == EOF) continue;
309 /* Cargamos el codigo y lo emitimos */
310 symbolcode = ctable[symbol];
311 for (i = symbolcode.codelength; i > 0; --i) {
312 bit = (symbolcode.code >> (i-1)) & 1;
313 putbit(bit,0,0,fpdest);
317 /* Hacemos un flush de lo que haya quedado en el buffer de salida */
318 putbit(0,0,1,fpdest);
324 /** Prepara las estructuras de datos necesarias para una compresion */
325 int shuff_encode_file(HUFF_STATE *shuff)
328 SHUFFCODE *codetable = (SHUFFCODE*)malloc(sizeof(SHUFFCODE)*256);
330 /* Veo si debo armar una freqtable o si esta preloaded */
331 if ((!shuff->canonic) && (!shuff->bychunk))
332 if (!shuff_scanfreq(shuff->sourcefile,shuff->freqtable)) return 0;
334 /* Genero el arbol de huffman */
335 shuff->codetree = shuff_buildtree(shuff->freqtable);
337 /* Armo la tabla de codigos prefijos para el encoder */
338 shuff_zerocodes(codetable);
339 shuff_buildcodes(codetable,shuff->codetree,0,0);
340 /*shuff_printcodes(codetable,shuff->freqtable);*/
342 /* Encodeo byte per byte */
343 shuff_encode_symbols(shuff,codetable);
345 /* Free up memory baby yeah */
351 /** Decodifica una serie de bits en un symbolo y lo devuelve */
352 SHUFFNODE *shuff_decode_symbols(SHUFFNODE *entrynode, unsigned long int buffer,
353 int *bitsleft, unsigned short int *symbol)
357 /* Levanto el symbolo y si es uno valido, devuelvo */
358 *symbol = entrynode->symbol;
359 if (*symbol != 256) return entrynode;
360 if (*bitsleft == 0) return entrynode;
362 /* Obtengo otro bit a procesar y me muevo en el arbol */
363 bit = (buffer >> ((*bitsleft)-1)) & 1;
365 if (bit == 0) return shuff_decode_symbols(entrynode->lchild,buffer,bitsleft,symbol);
366 else return shuff_decode_symbols(entrynode->rchild,buffer,bitsleft,symbol);
369 /** Decodifica chunksize symbolos y los devuelve en un chunk de datos */
370 int shuff_decode_chunk(HUFF_STATE *shuff, char *chunk, int chunksize, unsigned long int *decodedbytes)
372 SHUFFNODE *currnode = shuff->codetree;
373 unsigned short int decoded_symbol;
376 while (!vfeof(shuff->decoderfp) && (shuff->bytesleft > 0) && (*decodedbytes < chunksize)) {
378 /* Leo un buffer de 32 bits si es que quedo vacio el anterior */
379 if (shuff->bitsleft == 0) {
380 if (vfread(&(shuff->codebuffer),sizeof(unsigned long int),1,shuff->decoderfp) != 1) continue;
381 shuff->bitsleft = sizeof(unsigned long int) * 8;
384 /* Proceso el buffer sacando simbolos till se me agote el buffer, file o chunk */
385 while ((shuff->bitsleft > 0) && (shuff->bytesleft > 0) && (*decodedbytes < chunksize)) {
386 currnode = shuff_decode_symbols(currnode,shuff->codebuffer,&(shuff->bitsleft),&decoded_symbol);
387 /* Si obtuve un symbolo valido lo emito*/
388 if (decoded_symbol != 256) {
389 chunk[(*decodedbytes)++] = decoded_symbol;
390 currnode = shuff->codetree;
391 --(shuff->bytesleft);
396 if (shuff->bytesleft == 0) return 0;
400 /** Realiza la descompresión de un archivo comprimido */
401 int shuff_decode_file(HUFF_STATE *shuff)
404 unsigned long int codebuffer;
406 unsigned short int decoded_symbol;
409 /* Comienzo a decodificar, pues la tabla ya la levante en el decinit */
410 if ((fpdest = fopen(shuff->targetfile,"w")) == NULL) return 0;
411 currnode = shuff->codetree;
413 while (!vfeof(shuff->decoderfp) && (shuff->bytesleft > 0)) {
415 /* Leo un buffer de 32 bits */
416 if (vfread(&codebuffer,sizeof(unsigned long int),1,shuff->decoderfp) != 1) continue;
417 bitsleft = sizeof(unsigned long int) * 8;
419 /* Proceso el buffer sacando simbolos hasta que se me agote */
420 while ((bitsleft > 0) && (shuff->bytesleft > 0)) {
421 currnode = shuff_decode_symbols(currnode,codebuffer,&bitsleft,&decoded_symbol);
422 /* Si obtuve un symbolo valido lo emito*/
423 if (decoded_symbol != 256) {
424 fputc(decoded_symbol,fpdest);
425 currnode = shuff->codetree;
426 --(shuff->bytesleft);
431 /* Close destination */
437 /** Inicializa un descompresor de huffman */
438 HUFF_STATE *shuff_init_decoder(char *inputfile, char *outputfile)
441 HUFF_STATE *shuff = (HUFF_STATE*)malloc(sizeof(HUFF_STATE));
442 shuff->freqtable = (t_freq*)malloc(sizeof(t_freq)*256);
445 shuff->codebuffer = 0;
447 shuff->coderfp = NULL;
448 shuff->targetfile = NULL;
449 shuff->sourcefile = (char*)malloc(sizeof(char)*(strlen(inputfile)+1));
450 strcpy(shuff->sourcefile,inputfile);
451 if (outputfile != NULL) {
452 shuff->targetfile = (char*)malloc(sizeof(char)*(strlen(outputfile)+1));
453 strcpy(shuff->targetfile,outputfile);
456 /* Levanto cuantos bytes debo decodificar y la freqtable */
457 if ((shuff->decoderfp = vfopen(shuff->sourcefile,"r",0)) == NULL) return NULL;
458 vfread(&(shuff->bytesleft),sizeof(unsigned long int),1,shuff->decoderfp);
459 vfread(shuff->freqtable,sizeof(t_freq),256,shuff->decoderfp);
460 /* Armo el arbol de huffman que uso para decodificar */
461 shuff->codetree = shuff_buildtree(shuff->freqtable);
466 /** Inicializa compresor de huffman por archivo */
467 HUFF_STATE *shuff_init_encoder_byfile(char *inputfile, char *outputfile, long volsize)
470 HUFF_STATE *fshuff = (HUFF_STATE*)malloc(sizeof(HUFF_STATE));
473 /* Inicializo la estructura para trabajar con Huff Static by File */
474 fshuff->coderfp = NULL;
475 fshuff->decoderfp = NULL;
476 fshuff->sourcefile = (char*)malloc(sizeof(char)*(strlen(inputfile)+1));
477 fshuff->targetfile = (char*)malloc(sizeof(char)*(strlen(outputfile)+1));
478 strcpy(fshuff->sourcefile,inputfile);
479 strcpy(fshuff->targetfile,outputfile);
480 fshuff->volsize = volsize;
483 fshuff->freqtable = (t_freq*)malloc(sizeof(t_freq)*256);
484 for (i = 0; i < 256; ++i) fshuff->freqtable[i] = 0;
486 fshuff->codetree = NULL;
491 /** Inicializa compresor de huffman de a chunks */
492 HUFF_STATE *shuff_init_encoder_bychunk(char *outputfile, long volsize)
495 HUFF_STATE *cshuff = (HUFF_STATE*)malloc(sizeof(HUFF_STATE));
498 /* Inicializo la estructura para trabajar con Huff Static by Chunks */
499 cshuff->decoderfp = NULL;
500 cshuff->sourcefile = (char*)malloc(sizeof(char)*(strlen(outputfile)+2));
501 cshuff->targetfile = (char*)malloc(sizeof(char)*(strlen(outputfile)+1));
502 strcpy(cshuff->targetfile,outputfile);
503 strcpy(cshuff->sourcefile,outputfile);
504 strcat(cshuff->sourcefile,"~");
505 cshuff->volsize = volsize;
508 cshuff->freqtable = (t_freq*)malloc(sizeof(t_freq)*256);
509 for (i = 0; i < 256; ++i) cshuff->freqtable[i] = 0;
511 cshuff->codetree = NULL;
513 /* Abrimos un archivo temporal para ir tirando los chunks */
514 if ((cshuff->coderfp = fopen(cshuff->sourcefile,"w")) == NULL) return NULL;
519 /** Carga un modelo estadistico para huffman */
520 int shuff_loadmodel(HUFF_STATE *shuff, char *modelfile) {
524 if ((shuff) && (shuff->freqtable) && (modelfile)) {
525 /* Cargo el modelo de disco */
526 if ((fp = fopen(modelfile,"r")) == NULL) return 0;
527 if (fread(shuff->freqtable,sizeof(t_freq),256,fp) != 256) return 0;
535 /** Graba un modelo estadístico de huffman */
536 int shuff_savemodel(HUFF_STATE *shuff) {
542 if ((shuff) && (shuff->targetfile) && (shuff->freqtable)) {
543 /* Preparo el nombre del archivo con la tabla */
544 auxfilename = (char*)malloc(strlen(shuff->targetfile)+1);
545 stopchar = strrchr(shuff->targetfile,'.');
546 strncpy(auxfilename,shuff->targetfile,stopchar - shuff->targetfile);
547 auxfilename[stopchar - shuff->targetfile] = 0;
548 strcat(auxfilename,".ftb");
550 /* Lo creamos y dumpeamos la tabla de frecuencias (modelo) */
551 if ((fp = fopen(auxfilename,"w")) == NULL) return 0;
552 fwrite(shuff->freqtable,sizeof(t_freq),256,fp);
560 /** Desinicializa un compresor de huffman */
561 void shuff_deinit_encoder(HUFF_STATE *shuff)
563 /* Libero mallocs y cierro archivos */
564 if (shuff->freqtable) free(shuff->freqtable);
565 if (shuff->coderfp) fclose(shuff->coderfp);
566 if (shuff->bychunk) unlink(shuff->sourcefile);
567 if (shuff->sourcefile) free(shuff->sourcefile);
568 if (shuff->targetfile) free(shuff->targetfile);
570 /* Destruyo recursivamente el arbol de codigos */
571 if (shuff->codetree) shuff_destroy_tree(shuff->codetree);
574 /** Desinicializa un descompresor de huffman */
575 void shuff_deinit_decoder(HUFF_STATE *shuff)
577 /* Libero mallocs y cierro archivos */
578 if (shuff->freqtable) free(shuff->freqtable);
579 if (shuff->sourcefile != NULL) free(shuff->sourcefile);
580 if (shuff->targetfile != NULL) free(shuff->targetfile);
581 if (shuff->decoderfp != NULL) vfclose(shuff->decoderfp);
583 /* Destruyo recursivamente el arbol de codigos */
584 if (shuff->codetree) shuff_destroy_tree(shuff->codetree);