+ /* Proceso el buffer sacando simbolos hasta que se me agote */
+ while ((bitsleft > 0) && (shuff->bytesleft > 0)) {
+ currnode = shuff_decode_symbols(currnode,codebuffer,&bitsleft,&decoded_symbol);
+ /* Si obtuve un symbolo valido lo emito*/
+ if (decoded_symbol != 256) {
+ fputc(decoded_symbol,fpdest);
+ currnode = shuff->codetree;
+ --(shuff->bytesleft);
+ }
+ }
+ }
+
+ /* Close destination */
+ fclose(fpdest);
+
+ return 1;
+}
+
+/** Inicializa un descompresor de huffman */
+HUFF_STATE *shuff_init_decoder(char *inputfile, char *outputfile)
+{
+ /* Locals */
+ HUFF_STATE *shuff = (HUFF_STATE*)malloc(sizeof(HUFF_STATE));
+ shuff->freqtable = (t_freq*)malloc(sizeof(t_freq)*256);
+
+ /* Init fields */
+ shuff->codebuffer = 0;
+ shuff->bitsleft = 0;
+ shuff->coderfp = NULL;
+ shuff->targetfile = NULL;
+ shuff->sourcefile = (char*)malloc(sizeof(char)*(strlen(inputfile)+1));
+ strcpy(shuff->sourcefile,inputfile);
+ if (outputfile != NULL) {
+ shuff->targetfile = (char*)malloc(sizeof(char)*(strlen(outputfile)+1));
+ strcpy(shuff->targetfile,outputfile);
+ }
+
+ /* Levanto cuantos bytes debo decodificar y la freqtable */
+ if ((shuff->decoderfp = vfopen(shuff->sourcefile,"r",0)) == NULL) return NULL;
+ vfread(&(shuff->bytesleft),sizeof(unsigned long int),1,shuff->decoderfp);
+ vfread(shuff->freqtable,sizeof(t_freq),256,shuff->decoderfp);
+ /* Armo el arbol de huffman que uso para decodificar */
+ shuff->codetree = shuff_buildtree(shuff->freqtable);
+
+ return shuff;
+}
+
+/** Inicializa compresor de huffman por archivo */
+HUFF_STATE *shuff_init_encoder_byfile(char *inputfile, char *outputfile, long volsize)
+{
+ /* Locals */
+ HUFF_STATE *fshuff = (HUFF_STATE*)malloc(sizeof(HUFF_STATE));
+ int i;
+
+ /* Inicializo la estructura para trabajar con Huff Static by File */
+ fshuff->coderfp = NULL;
+ fshuff->decoderfp = NULL;
+ fshuff->sourcefile = (char*)malloc(sizeof(char)*(strlen(inputfile)+1));
+ fshuff->targetfile = (char*)malloc(sizeof(char)*(strlen(outputfile)+1));
+ strcpy(fshuff->sourcefile,inputfile);
+ strcpy(fshuff->targetfile,outputfile);
+ fshuff->volsize = volsize;
+ fshuff->bychunk = 0;
+ fshuff->canonic = 0;
+ fshuff->freqtable = (t_freq*)malloc(sizeof(t_freq)*256);
+ for (i = 0; i < 256; ++i) fshuff->freqtable[i] = 0;
+ fshuff->sumfreq = 0;
+ fshuff->codetree = NULL;
+
+ return fshuff;
+}
+
+/** Inicializa compresor de huffman de a chunks */
+HUFF_STATE *shuff_init_encoder_bychunk(char *outputfile, long volsize)
+{
+ /* Locals */
+ HUFF_STATE *cshuff = (HUFF_STATE*)malloc(sizeof(HUFF_STATE));
+ int i;
+
+ /* Inicializo la estructura para trabajar con Huff Static by Chunks */
+ cshuff->decoderfp = NULL;
+ cshuff->sourcefile = (char*)malloc(sizeof(char)*(strlen(outputfile)+2));
+ cshuff->targetfile = (char*)malloc(sizeof(char)*(strlen(outputfile)+1));
+ strcpy(cshuff->targetfile,outputfile);
+ strcpy(cshuff->sourcefile,outputfile);
+ strcat(cshuff->sourcefile,"~");
+ cshuff->volsize = volsize;
+ cshuff->bychunk = 1;
+ cshuff->canonic = 0;
+ cshuff->freqtable = (t_freq*)malloc(sizeof(t_freq)*256);
+ for (i = 0; i < 256; ++i) cshuff->freqtable[i] = 0;
+ cshuff->sumfreq = 0;
+ cshuff->codetree = NULL;
+
+ /* Abrimos un archivo temporal para ir tirando los chunks */
+ if ((cshuff->coderfp = fopen(cshuff->sourcefile,"w")) == NULL) return NULL;
+
+ return cshuff;
+}
+
+/** Carga un modelo estadistico para huffman */
+int shuff_loadmodel(HUFF_STATE *shuff, char *modelfile) {