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) {
/* 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 */
}
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;
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;
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;
}
}
+ /* Close destination */
fclose(fpdest);
return 1;
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));
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 */
}
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;