]> git.llucax.com Git - z.facultad/75.06/jacu.git/blobdiff - src/statichuff/statichuff.c
Modifico el main del Jacu para que compile con la nueva API de Huffman por lo que...
[z.facultad/75.06/jacu.git] / src / statichuff / statichuff.c
index 28b932d35edba9adcd7ec2ff47dedf25cece68bb..55346bd4bb04dcb32eeec11478f284630f5d8a1b 100644 (file)
@@ -1,6 +1,7 @@
 
 #include "statichuff.h"
 #include <stdlib.h>
+#include <string.h>
 
 void putbit(char bit, char restart, char flush, VFILE *fp)
 {
@@ -78,7 +79,10 @@ int shuff_scanfreq_chunk(HUFF_STATE *chunkshuff, char* chunk, int chunksize)
                /* 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;
 }
@@ -230,7 +234,8 @@ int shuff_encode_symbols(HUFF_STATE *shuff, SHUFFCODE *ctable)
        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;
                
@@ -304,6 +309,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;    
@@ -312,7 +347,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;
        
@@ -334,6 +369,7 @@ int shuff_decode_file(HUFF_STATE *shuff)
                }               
        }
                        
+       /* Close destination */
        fclose(fpdest); 
        
        return 1;
@@ -346,6 +382,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));
@@ -356,7 +394,7 @@ HUFF_STATE *shuff_init_decoder(char *inputfile, char *outputfile)
        }       
        
        /* 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);
@@ -418,11 +456,11 @@ HUFF_STATE *shuff_init_encoder_bychunk(char *outputfile, long volsize)
 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 */
 }