#include "statichuff.h"
#include <stdlib.h>
+#include <string.h>
void putbit(char bit, char restart, char flush, VFILE *fp)
{
/* Si llegue al tope de freq acumulada, halve em */
if (chunkshuff->sumfreq == 14930352)
chunkshuff->sumfreq = shuff_rescalefreq(chunkshuff->freqtable);
- }
+ }
+
+ /* Dumpeamos el chunk en el temporal homero */
+ fwrite(chunk,chunksize,1,chunkshuff->coderfp);
return 1;
}
char bit;
SHUFFCODE symbolcode;
- /* Abrimos el file */
+ /* Abrimos el source y el destino */
+ if (shuff->coderfp != NULL) fclose(shuff->coderfp); /* close bychunk temp file */
if ((fpsource = fopen(shuff->sourcefile,"r")) == NULL) return 0;
if ((fpdest = vfopen(shuff->targetfile,"w",shuff->volsize)) == NULL) 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));
}
/* Levanto cuantos bytes decodeo y la freq table */
- if ((shuff->decoderfp = vfopen(shuff->sourcefile,"r",0)) == NULL) return 0;
+ 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);
shuff->codetree = shuff_buildtree(shuff->freqtable);
void shuff_deinit_encoder(HUFF_STATE *shuff)
{
/* Libero mallocs y cierro archivos */
- free(shuff->freqtable);
- free(shuff->sourcefile);
- free(shuff->targetfile);
+ free(shuff->freqtable);
if (shuff->coderfp != NULL) fclose(shuff->coderfp);
- if (shuff->decoderfp != NULL) vfclose(shuff->decoderfp);
+ if (shuff->preloadfreq == 1) unlink(shuff->sourcefile);
+ if (shuff->targetfile != NULL) free(shuff->targetfile);
+ free(shuff->sourcefile);
/* Destruyo recursivamente el arbol de codigos */
}