strcpy(shuff->targetfile,outputfile);
}
- /* Levanto cuantos bytes decodeo y la freq table */
+ /* 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(unsigned long int),256,shuff->decoderfp);
+ /* Armo el arbol de huffman que uso para decodificar */
shuff->codetree = shuff_buildtree(shuff->freqtable);
return shuff;
strcpy(fshuff->sourcefile,inputfile);
strcpy(fshuff->targetfile,outputfile);
fshuff->volsize = volsize;
+ fshuff->bychunk = 0;
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 *cshuff = (HUFF_STATE*)malloc(sizeof(HUFF_STATE));
int i;
- /* Inicializo la estructura para trabajar con Huff Static by Chunks */
+ /* 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->sourcefile,outputfile);
strcat(cshuff->sourcefile,"~");
cshuff->volsize = volsize;
+ cshuff->bychunk = 1;
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;
+ 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;
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);
+ if (shuff->freqtable) free(shuff->freqtable);
+ if (shuff->coderfp) fclose(shuff->coderfp);
+ if (shuff->bychunk) unlink(shuff->sourcefile);
+ if (shuff->sourcefile) free(shuff->sourcefile);
+ if (shuff->targetfile) free(shuff->targetfile);
/* 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->freqtable) free(shuff->freqtable);
+ if (shuff->sourcefile != NULL) free(shuff->sourcefile);
if (shuff->targetfile != NULL) free(shuff->targetfile);
if (shuff->decoderfp != NULL) vfclose(shuff->decoderfp);
} SHUFFCODE;
typedef struct t_huff {
- FILE *coderfp; /* filepointer usado en el coder de bychunk */
- VFILE *decoderfp; /* filepointer usado en ambos decoders */
+ FILE *coderfp; /* fpointer usado en el coder de bychunk para el temp */
+ VFILE *decoderfp; /* fpointer al archivo a descomrimir */
char *sourcefile; /* Nombre del archivo a comprimir */
char *targetfile; /* Nombre del archivo comprimido */
long volsize; /* Tamanio de volumen para multivol */
- char preloadfreq; /* 1 Freqtable preloaded (bychunk | canonico) - 0 byfile */
+ char bychunk; /* 0 works byfile, 1 works bychunk */
+ char preloadfreq; /* 1 Freqtable has been preloaded (bychunk | canonic) */
t_freq *freqtable; /* Tabla de frecuencias */
t_freq sumfreq; /* Frecuencia total acumulada */
SHUFFNODE *codetree; /* Puntero al arbol de codigos prefijos */