X-Git-Url: https://git.llucax.com/z.facultad/75.06/jacu.git/blobdiff_plain/4c56e227e8a6bf6acb612b95a66f39b2abb7faca..c64d97e6f5f058648074b74ef4d8c3142826777f:/src/statichuff/statichuff.c diff --git a/src/statichuff/statichuff.c b/src/statichuff/statichuff.c index 797ca17..9934509 100644 --- a/src/statichuff/statichuff.c +++ b/src/statichuff/statichuff.c @@ -1,8 +1,30 @@ +/*---------------------------------------------------------------------------- + * jacu - Just Another Compression Utility + *---------------------------------------------------------------------------- + * This file is part of jacu. + * + * jacu is free software; you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 2 of the License, or (at your option) any later + * version. + * + * jacu is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along + * with jacu; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + *---------------------------------------------------------------------------- + */ + #include "statichuff.h" #include #include +/** Coloca un bit en un buffer statico */ void putbit(char bit, char restart, char flush, VFILE *fp) { static unsigned long int bits_buffer = 0; @@ -35,6 +57,7 @@ void putbit(char bit, char restart, char flush, VFILE *fp) return; } +/** Realiza la copia de los datos de un nodo de huffman a otro */ void shuff_cpynode(SHUFFNODE *node1, SHUFFNODE *node2) { node1->symbol = node2->symbol; @@ -43,6 +66,7 @@ void shuff_cpynode(SHUFFNODE *node1, SHUFFNODE *node2) node1->rchild = node2->rchild; } +/** Realiza una comparacion de dos nodos de huffman */ int shuff_compnode(const void *node1, const void *node2) { if (((SHUFFNODE*)node1)->freq < ((SHUFFNODE*)node2)->freq) return 1; @@ -50,6 +74,23 @@ int shuff_compnode(const void *node1, const void *node2) return 0; } +/** Destruye un arbol de huffman recursivamente */ +void shuff_destroy_tree(SHUFFNODE *node) { + /* Si llegue a una hoja, destruyo y vuelvo */ + if (node->symbol < 256) { + free(node); + return; + } + else { + /* Desciendo por izq, luego por derecha y luego libero */ + shuff_destroy_tree(node->lchild); + shuff_destroy_tree(node->rchild); + free(node); + return; + } +} + +/** Reescala las frecuencias de huffman a la mitad */ int shuff_rescalefreq(t_freq *freqtable) { int i; @@ -64,6 +105,7 @@ int shuff_rescalefreq(t_freq *freqtable) return totalfreq; } +/** Escanea las frecuencias de un chunk de datos */ int shuff_scanfreq_chunk(HUFF_STATE *chunkshuff, char* chunk, int chunksize) { /* Locals */ @@ -89,6 +131,7 @@ int shuff_scanfreq_chunk(HUFF_STATE *chunkshuff, char* chunk, int chunksize) return 1; } +/** Escanea las frecuencias de un archivo y genera el modelo */ int shuff_scanfreq(char *inputfile, t_freq *freqtable) { /* Locals */ @@ -118,6 +161,7 @@ int shuff_scanfreq(char *inputfile, t_freq *freqtable) return 1; } +/** Genera un input list que sera utilizada para generar el arbol */ SHUFFNODE *shuff_buildlist(t_freq *freqtable, int *nonzerofreqs) { int i,j = 0,nonzero = 0; @@ -141,6 +185,7 @@ SHUFFNODE *shuff_buildlist(t_freq *freqtable, int *nonzerofreqs) return inputlist; } +/** Genera el arbol de huffman en base a la tabla de frecuencias */ SHUFFNODE *shuff_buildtree(t_freq *ftable) { SHUFFNODE *lastsymbol; @@ -180,6 +225,7 @@ SHUFFNODE *shuff_buildtree(t_freq *ftable) return root; } +/** Imprime los codigos prefijos generados para los symbolos */ void shuff_printcodes(SHUFFCODE *codetable,t_freq *freqtable) { int i,j; @@ -201,6 +247,7 @@ void shuff_printcodes(SHUFFCODE *codetable,t_freq *freqtable) } } +/** Inicializa la tabla de codigos prefijos */ void shuff_zerocodes(SHUFFCODE *table) { int i; @@ -212,6 +259,7 @@ void shuff_zerocodes(SHUFFCODE *table) } } +/** Genera la tabla de codigos prefijos en base al árbol de huffman */ void shuff_buildcodes(SHUFFCODE *table, SHUFFNODE *node, int level, int code) { if (node->symbol < 256) { @@ -227,6 +275,7 @@ void shuff_buildcodes(SHUFFCODE *table, SHUFFNODE *node, int level, int code) } } +/** Realiza la compresion / encoding efectivo de un archivo */ int shuff_encode_symbols(HUFF_STATE *shuff, SHUFFCODE *ctable) { FILE *fpsource; @@ -272,6 +321,7 @@ int shuff_encode_symbols(HUFF_STATE *shuff, SHUFFCODE *ctable) return 1; } +/** Prepara las estructuras de datos necesarias para una compresion */ int shuff_encode_file(HUFF_STATE *shuff) { /* Locals */ @@ -298,6 +348,7 @@ int shuff_encode_file(HUFF_STATE *shuff) return 1; } +/** Decodifica una serie de bits en un symbolo y lo devuelve */ SHUFFNODE *shuff_decode_symbols(SHUFFNODE *entrynode, unsigned long int buffer, int *bitsleft, unsigned short int *symbol) { @@ -315,7 +366,8 @@ SHUFFNODE *shuff_decode_symbols(SHUFFNODE *entrynode, unsigned long int buffer, else return shuff_decode_symbols(entrynode->rchild,buffer,bitsleft,symbol); } -int shuff_decode_chunk(HUFF_STATE *shuff, char *chunk, int chunksize, int *decodedbytes) +/** Decodifica chunksize symbolos y los devuelve en un chunk de datos */ +int shuff_decode_chunk(HUFF_STATE *shuff, char *chunk, int chunksize, unsigned long int *decodedbytes) { SHUFFNODE *currnode = shuff->codetree; unsigned short int decoded_symbol; @@ -345,6 +397,7 @@ int shuff_decode_chunk(HUFF_STATE *shuff, char *chunk, int chunksize, int *decod else return 1; } +/** Realiza la descompresión de un archivo comprimido */ int shuff_decode_file(HUFF_STATE *shuff) { SHUFFNODE *currnode; @@ -381,6 +434,7 @@ int shuff_decode_file(HUFF_STATE *shuff) return 1; } +/** Inicializa un descompresor de huffman */ HUFF_STATE *shuff_init_decoder(char *inputfile, char *outputfile) { /* Locals */ @@ -409,6 +463,7 @@ HUFF_STATE *shuff_init_decoder(char *inputfile, char *outputfile) return shuff; } +/** Inicializa compresor de huffman por archivo */ HUFF_STATE *shuff_init_encoder_byfile(char *inputfile, char *outputfile, long volsize) { /* Locals */ @@ -433,6 +488,7 @@ HUFF_STATE *shuff_init_encoder_byfile(char *inputfile, char *outputfile, long vo return fshuff; } +/** Inicializa compresor de huffman de a chunks */ HUFF_STATE *shuff_init_encoder_bychunk(char *outputfile, long volsize) { /* Locals */ @@ -460,6 +516,7 @@ HUFF_STATE *shuff_init_encoder_bychunk(char *outputfile, long volsize) return cshuff; } +/** Carga un modelo estadistico para huffman */ int shuff_loadmodel(HUFF_STATE *shuff, char *modelfile) { FILE *fp; @@ -475,6 +532,7 @@ int shuff_loadmodel(HUFF_STATE *shuff, char *modelfile) { return 0; } +/** Graba un modelo estadístico de huffman */ int shuff_savemodel(HUFF_STATE *shuff) { FILE *fp; @@ -499,6 +557,7 @@ int shuff_savemodel(HUFF_STATE *shuff) { return 0; } +/** Desinicializa un compresor de huffman */ void shuff_deinit_encoder(HUFF_STATE *shuff) { /* Libero mallocs y cierro archivos */ @@ -509,8 +568,10 @@ void shuff_deinit_encoder(HUFF_STATE *shuff) if (shuff->targetfile) free(shuff->targetfile); /* Destruyo recursivamente el arbol de codigos */ + if (shuff->codetree) shuff_destroy_tree(shuff->codetree); } +/** Desinicializa un descompresor de huffman */ void shuff_deinit_decoder(HUFF_STATE *shuff) { /* Libero mallocs y cierro archivos */ @@ -520,4 +581,5 @@ void shuff_deinit_decoder(HUFF_STATE *shuff) if (shuff->decoderfp != NULL) vfclose(shuff->decoderfp); /* Destruyo recursivamente el arbol de codigos */ + if (shuff->codetree) shuff_destroy_tree(shuff->codetree); }