From fda048987789cf1bcf067115a593042af5ade502 Mon Sep 17 00:00:00 2001 From: Alan Kennedy Date: Wed, 23 Jun 2004 08:28:26 +0000 Subject: [PATCH] Listo en teoria Huffman Compresor/Descompresor ByFile y Bychunk. Maniana detallo un poco el API y si tengo pilas ahora lo acoplo en JACU, sino tomorrow porque no doy mas. Vamo que llegamos al viernes.. --- src/statichuff/main_bychunk.c | 21 +++++++++++++++---- src/statichuff/statichuff.c | 39 ++++++++++++++++++++++++++++++++--- src/statichuff/statichuff.h | 4 +++- 3 files changed, 56 insertions(+), 8 deletions(-) diff --git a/src/statichuff/main_bychunk.c b/src/statichuff/main_bychunk.c index eafe694..43426e8 100644 --- a/src/statichuff/main_bychunk.c +++ b/src/statichuff/main_bychunk.c @@ -11,7 +11,7 @@ int main(int argc, char* argv[]) int dflag = 0; int tflag = 0; long int volumesize = 0; - int lastchunk,i,j,ch; + int lastchunk,i,j,ch,decoded = 0; while ((ch = getopt(argc, argv, "cdt:")) != -1) { @@ -58,7 +58,7 @@ int main(int argc, char* argv[]) /* Le indico al huffman que efectivamente comprima los chunks */ shuff_encode_file(shuff); - /* De init shuffman by chunks */ + /* De init encoder */ shuff_deinit_encoder(shuff); /* Free mem allocated by main */ @@ -70,8 +70,21 @@ int main(int argc, char* argv[]) } if (dflag == 1) { - /* Descomprimo */ - /*return shuff_decode_file(argv[optind],argv[optind+1]);*/ + /* Init decoder */ + shuff = shuff_init_decoder(argv[optind],NULL); + fp = fopen("chunkdump.txt","w"); + + /* Gimme chunks till last one */ + while (shuff_decode_chunk(shuff,chunk,4,&decoded)) + fwrite(chunk,decoded,1,fp); + + /* Last chunk written alone */ + fwrite(chunk,decoded,1,fp); + fclose(fp); + + /* Deinit decoder */ + shuff_deinit_decoder(shuff); + free(shuff); } return 0; diff --git a/src/statichuff/statichuff.c b/src/statichuff/statichuff.c index a2f6265..908729b 100644 --- a/src/statichuff/statichuff.c +++ b/src/statichuff/statichuff.c @@ -308,6 +308,36 @@ 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) +{ + SHUFFNODE *currnode = shuff->codetree; + unsigned short int decoded_symbol; + *decodedbytes = 0; + + while (!vfeof(shuff->decoderfp) && (shuff->bytesleft > 0) && (*decodedbytes < chunksize)) { + + /* Leo un buffer de 32 bits si es que quedo vacio el anterior */ + if (shuff->bitsleft == 0) { + if (vfread(&(shuff->codebuffer),sizeof(unsigned long int),1,shuff->decoderfp) != 1) continue; + shuff->bitsleft = sizeof(unsigned long int) * 8; + } + + /* Proceso el buffer sacando simbolos till se me agote el buffer, file o chunk */ + while ((shuff->bitsleft > 0) && (shuff->bytesleft > 0) && (*decodedbytes < chunksize)) { + currnode = shuff_decode_symbols(currnode,shuff->codebuffer,&(shuff->bitsleft),&decoded_symbol); + /* Si obtuve un symbolo valido lo emito*/ + if (decoded_symbol != 256) { + chunk[(*decodedbytes)++] = decoded_symbol; + currnode = shuff->codetree; + --(shuff->bytesleft); + } + } + } + + if (shuff->bytesleft == 0) return 0; + else return 1; +} + int shuff_decode_file(HUFF_STATE *shuff) { SHUFFNODE *currnode; @@ -316,7 +346,7 @@ int shuff_decode_file(HUFF_STATE *shuff) unsigned short int decoded_symbol; int bitsleft; - /* Levanto cuantos bytes decodeo y la freq table */ + /* Comienzo a decodificar, pues la tabla ya la levante en el decinit */ if ((fpdest = fopen(shuff->targetfile,"w")) == NULL) return 0; currnode = shuff->codetree; @@ -338,6 +368,7 @@ int shuff_decode_file(HUFF_STATE *shuff) } } + /* Close destination */ fclose(fpdest); return 1; @@ -350,6 +381,8 @@ HUFF_STATE *shuff_init_decoder(char *inputfile, char *outputfile) 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)); @@ -425,8 +458,8 @@ void shuff_deinit_encoder(HUFF_STATE *shuff) free(shuff->freqtable); if (shuff->coderfp != NULL) fclose(shuff->coderfp); if (shuff->preloadfreq == 1) unlink(shuff->sourcefile); - free(shuff->sourcefile); - free(shuff->targetfile); + if (shuff->targetfile != NULL) free(shuff->targetfile); + free(shuff->sourcefile); /* Destruyo recursivamente el arbol de codigos */ } diff --git a/src/statichuff/statichuff.h b/src/statichuff/statichuff.h index d892aea..35a550c 100644 --- a/src/statichuff/statichuff.h +++ b/src/statichuff/statichuff.h @@ -30,7 +30,9 @@ typedef struct t_huff { t_freq *freqtable; /* Tabla de frecuencias */ t_freq sumfreq; /* Frecuencia total acumulada */ SHUFFNODE *codetree; /* Puntero al arbol de codigos prefijos */ - unsigned long int bytesleft; /* cuanto falta descomprimir en un bychunk */ + unsigned long int bytesleft; /* Cuanto falta descomprimir en un bychunk */ + unsigned long int codebuffer; /* Buffer de descompresion para bychunk */ + int bitsleft; /* Posicion en el buffer de descompresion para bychunk */ } HUFF_STATE; -- 2.43.0