+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 decodeo y la freq table */
+ if ((shuff->decoderfp = vfopen(shuff->sourcefile,"r",0)) == NULL) return 0;
+ vfread(&(shuff->bytesleft),sizeof(unsigned long int),1,shuff->decoderfp);
+ vfread(shuff->freqtable,sizeof(unsigned long int),256,shuff->decoderfp);
+ shuff->codetree = shuff_buildtree(shuff->freqtable);
+
+ return shuff;
+}
+
+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->preloadfreq = 0;
+ fshuff->freqtable = (t_freq*)malloc(sizeof(t_freq)*256);
+ for (i = 0; i < 256; ++i) fshuff->freqtable[i] = 0;
+ fshuff->sumfreq = 0;
+ fshuff->bytesleft = 0;
+ fshuff->codetree = NULL;
+
+ return fshuff;
+}
+
+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->preloadfreq = 1;
+ cshuff->freqtable = (t_freq*)malloc(sizeof(t_freq)*256);
+ for (i = 0; i < 256; ++i) cshuff->freqtable[i] = 0;
+ cshuff->sumfreq = 0;
+ cshuff->bytesleft = 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;
+}
+
+void shuff_deinit_encoder(HUFF_STATE *shuff)
+{
+ /* Libero mallocs y cierro archivos */
+ free(shuff->freqtable);
+ if (shuff->coderfp != NULL) fclose(shuff->coderfp);
+ if (shuff->preloadfreq == 1) unlink(shuff->sourcefile);
+ if (shuff->targetfile != NULL) free(shuff->targetfile);
+ free(shuff->sourcefile);
+
+ /* Destruyo recursivamente el arbol de codigos */
+}
+
+void shuff_deinit_decoder(HUFF_STATE *shuff)
+{
+ /* Libero mallocs y cierro archivos */
+ free(shuff->freqtable);
+ free(shuff->sourcefile);
+ if (shuff->targetfile != NULL) free(shuff->targetfile);
+ if (shuff->decoderfp != NULL) vfclose(shuff->decoderfp);
+
+ /* Destruyo recursivamente el arbol de codigos */
+}