]> git.llucax.com Git - z.facultad/75.06/jacu.git/blob - src/statichuff/statichuff.c
Fclose fix
[z.facultad/75.06/jacu.git] / src / statichuff / statichuff.c
1
2 #include "statichuff.h"
3 #include <stdlib.h>
4 #include <string.h>
5
6 void putbit(char bit, char restart, char flush, VFILE *fp)
7 {
8         static unsigned long int bits_buffer = 0;
9         static unsigned char bits_used = 0;     
10
11         /* me obligan a emitir el output */
12         if ((flush == 1) && (bits_used > 0)) {
13                 bits_buffer = bits_buffer << ((sizeof(unsigned long int)*8) - bits_used);
14                 vfwrite(&bits_buffer,sizeof(unsigned long int),1,fp);
15                 bits_buffer = 0;
16                 bits_used = 0;
17                 return;
18         }       
19         /* me indican que comienza un nuevo output */
20         if (restart) {
21                 bits_buffer = 0;
22                 bits_used = 0;
23         }                       
24         /* inserto el bit en el buffer */       
25         bits_buffer = bits_buffer << 1;
26         bits_buffer |= bit;
27         bits_used++;
28         
29         /* lleno el buffer, escribo */
30         if (bits_used == 32) {
31                 vfwrite(&bits_buffer,sizeof(unsigned long int),1,fp);
32                 bits_buffer = 0;
33                 bits_used = 0;
34         }       
35         return;
36 }
37
38 void shuff_cpynode(SHUFFNODE *node1, SHUFFNODE *node2)
39 {
40         node1->symbol = node2->symbol;
41         node1->freq = node2->freq;
42         node1->lchild = node2->lchild;
43         node1->rchild = node2->rchild;  
44 }
45
46 int shuff_compnode(const void *node1, const void *node2)
47 {       
48         if (((SHUFFNODE*)node1)->freq < ((SHUFFNODE*)node2)->freq) return 1;
49         if (((SHUFFNODE*)node1)->freq > ((SHUFFNODE*)node2)->freq) return -1;
50         return 0;
51 }
52
53 int shuff_rescalefreq(t_freq *freqtable)
54
55         int i;
56         t_freq totalfreq = 0;
57         
58         /* Divido por la mitad las frecuencias, asegurando de no perder */
59         for (i = 0; i < 256; i++) {             
60                 freqtable[i] = (freqtable[i] >> 2) | 1;
61                 totalfreq += freqtable[i];
62         }
63         
64         return totalfreq;
65 }
66
67 int shuff_scanfreq_chunk(HUFF_STATE *chunkshuff, char* chunk, int chunksize)
68 {       
69         /* Locals */                    
70         int i = 0;
71         unsigned char symbol = 0;       
72                 
73         /* Contamos las frecuencias del chunk a menos que se use un canonico */                 
74         if (!chunkshuff->canonic) {
75                 for (i = 0; i < chunksize; ++i) {                               
76                         symbol = chunk[i];              
77                         chunkshuff->freqtable[symbol] += 1;
78                         chunkshuff->sumfreq += 1;
79                                 
80                         /* Si llegue al tope de freq acumulada, halve em */
81                         if (chunkshuff->sumfreq == 14930352) 
82                                 chunkshuff->sumfreq = shuff_rescalefreq(chunkshuff->freqtable);
83                 }
84         }
85         
86         /* Dumpeamos el chunk en el temporal homero */
87         fwrite(chunk,chunksize,1,chunkshuff->coderfp);
88                 
89         return 1;
90 }
91
92 int shuff_scanfreq(char *inputfile, t_freq *freqtable)
93 {
94         /* Locals */    
95         FILE *fp;
96         t_freq sumfreq = 0;
97         int i,symbol;
98         
99         /* Inicializamos la tabla de frecuencias */     
100         for (i = 0; i < 256; ++i) freqtable[i] = 0;
101                 
102         /* Abrimos el file */
103         if ((fp = fopen(inputfile,"r")) == NULL) return 0;
104         while (!feof(fp)) {             
105                 /* Contamos las frecuencias */          
106                 symbol = fgetc(fp);
107                 if (symbol == EOF) continue;
108                 
109                 freqtable[symbol]++;            
110                 ++sumfreq;
111                                 
112                 /* Si llegue al tope de freq acumulada, halve em */
113                 if (sumfreq == 14930352) 
114                         sumfreq = shuff_rescalefreq(freqtable);
115         }
116         
117         fclose(fp);
118         return 1;
119 }
120
121 SHUFFNODE *shuff_buildlist(t_freq *freqtable, int *nonzerofreqs)
122 {
123         int i,j = 0,nonzero = 0;        
124         SHUFFNODE *inputlist;
125         
126         /* Calculo cuantas frequencias > 0 hay y creo la tabla */
127         for (i = 0; i < 256; ++i) if (freqtable[i] > 0) nonzero++;
128         inputlist = (SHUFFNODE*)malloc(sizeof(SHUFFNODE)*nonzero);
129                 
130         /* Cargo la inputlist del huffman solo con freqs > 0 */
131         for (i = 0; i < 256; ++i)
132                 if (freqtable[i] > 0) {                 
133                         inputlist[j].symbol = i;
134                         inputlist[j].freq = freqtable[i];
135                         inputlist[j].lchild = NULL;
136                         inputlist[j].rchild = NULL;                     
137                         j++;
138                 }
139                 
140         *nonzerofreqs = nonzero;
141         return inputlist;
142 }
143
144 SHUFFNODE *shuff_buildtree(t_freq *ftable)
145 {
146         SHUFFNODE *lastsymbol;
147         SHUFFNODE *node1,*node2,*root;
148         SHUFFNODE *inputlist;
149         int freqcount = 0;
150
151         /* Genero la input list en base a la cual genera el arbol */
152         inputlist = shuff_buildlist(ftable, &freqcount);        
153         lastsymbol = inputlist+(freqcount-1);
154         
155         while (lastsymbol > inputlist) {                
156                 /* Ordeno la lista por frecuencia descendente */
157                 qsort(inputlist,freqcount,sizeof(SHUFFNODE),shuff_compnode);                            
158                 /* Tomo los ultimos dos elementos, generando dos nodos del arbol */
159                 node1 = (SHUFFNODE*)malloc(sizeof(SHUFFNODE));
160                 node2 = (SHUFFNODE*)malloc(sizeof(SHUFFNODE));
161                 shuff_cpynode(node1,lastsymbol-1);
162                 shuff_cpynode(node2,lastsymbol);                
163                 lastsymbol -= 1;
164                 /* Nodo ficticio con la suma de las probs y los ptros a childs */
165                 lastsymbol->symbol = 256;
166                 lastsymbol->freq = node1->freq + node2->freq;
167                 lastsymbol->lchild = node1;
168                 lastsymbol->rchild = node2;
169                 --freqcount;
170         }
171         
172         /* Copio la raiz para poder liberar la lista sin perderla */
173         root = (SHUFFNODE*)malloc(sizeof(SHUFFNODE));
174         shuff_cpynode(root,lastsymbol);
175         
176         /* Free up mem */
177         free(inputlist);
178         
179         /* Devuelvo el puntero a la raiz del arbol de huffman */
180         return root;
181 }
182
183 void shuff_printcodes(SHUFFCODE *codetable,t_freq *freqtable)
184 {
185         int i,j;
186         unsigned short int auxcode;
187         unsigned char bit;
188         
189         for (i = 0; i < 256; ++i) {
190                 if (codetable[i].codelength > 0) {
191                         auxcode = codetable[i].code;                    
192                         printf("Symbol:%i  Freq: %lu  Code:",i,freqtable[i]);
193                         for (j = codetable[i].codelength-1; j >= 0; --j) {
194                                 auxcode = codetable[i].code;                    
195                                 auxcode = auxcode >> j;
196                                 bit = auxcode & 1;                              
197                                 printf("%i",bit);
198                         }
199                         printf("  Length:%i\n",codetable[i].codelength);
200                 }               
201         }
202 }
203
204 void shuff_zerocodes(SHUFFCODE *table)
205 {
206         int i;
207         
208         /* Inicializo los codigos prefijos */   
209         for (i = 0; i < 256; ++i) {
210                 table[i].code = 0;
211                 table[i].codelength = 0;
212         }
213 }
214
215 void shuff_buildcodes(SHUFFCODE *table, SHUFFNODE *node, int level, int code)
216 {
217         if (node->symbol < 256) {
218                 /* Guardo el codigo en la tabla */
219                 table[node->symbol].code = code;
220                 table[node->symbol].codelength = level;         
221         }
222         else {
223                 code = code << 1;
224                 shuff_buildcodes(table,node->lchild,level+1,code);
225                 code |= 1;
226                 shuff_buildcodes(table,node->rchild,level+1,code);
227         }
228 }
229
230 int shuff_encode_symbols(HUFF_STATE *shuff, SHUFFCODE *ctable)
231 {
232         FILE *fpsource;
233         VFILE *fpdest;
234         int symbol,i;
235         unsigned long int sourcesize;
236         char bit;
237         SHUFFCODE symbolcode;
238                 
239         /* Abrimos el source y el destino */
240         if (shuff->coderfp != NULL) {
241                 fclose(shuff->coderfp); /* close bychunk temp file */
242                 shuff->coderfp = NULL;
243         }
244         if ((fpsource = fopen(shuff->sourcefile,"r")) == NULL) return 0;
245         if ((fpdest = vfopen(shuff->targetfile,"w",shuff->volsize)) == NULL) return 0;
246                 
247         /* Guardamos el size el archivo original e inputlist como header */
248         fseek(fpsource,0,SEEK_END);
249         sourcesize = ftell(fpsource);
250         vfwrite(&sourcesize,sizeof(unsigned long int),1,fpdest);
251         vfwrite(shuff->freqtable,sizeof(t_freq),256,fpdest);
252         
253         /* Encodeo */
254         fseek(fpsource,0,SEEK_SET);
255         while (!feof(fpsource)) {
256                 /* Levanto un symbolo (byte) */         
257                 symbol = fgetc(fpsource);
258                 if (symbol == EOF) continue;
259                 
260                 /* Cargamos el codigo y lo emitimos */
261                 symbolcode = ctable[symbol];
262                 for (i = symbolcode.codelength; i > 0; --i) {
263                         bit = (symbolcode.code >> (i-1)) & 1;
264                         putbit(bit,0,0,fpdest);
265                 }               
266         }
267         
268         /* Hacemos un flush de lo que haya quedado en el buffer de salida */
269         putbit(0,0,1,fpdest);
270         fclose(fpsource);
271         vfclose(fpdest);
272         return 1;       
273 }
274
275 int shuff_encode_file(HUFF_STATE *shuff)
276 {
277         /* Locals */            
278         SHUFFCODE *codetable = (SHUFFCODE*)malloc(sizeof(SHUFFCODE)*256);
279         
280         /* Veo si debo armar una freqtable o si esta preloaded */
281         if ((!shuff->canonic) && (!shuff->bychunk)) 
282                 if (!shuff_scanfreq(shuff->sourcefile,shuff->freqtable)) return 0;
283         
284         /* Genero el arbol de huffman */
285         shuff->codetree = shuff_buildtree(shuff->freqtable);
286
287         /* Armo la tabla de codigos prefijos para el encoder */
288         shuff_zerocodes(codetable);
289         shuff_buildcodes(codetable,shuff->codetree,0,0);
290         /*shuff_printcodes(codetable,shuff->freqtable);*/
291
292         /* Encodeo byte per byte */
293         shuff_encode_symbols(shuff,codetable);
294         
295         /* Free up memory baby yeah */  
296         free(codetable);
297         
298         return 1;
299 }
300
301 SHUFFNODE *shuff_decode_symbols(SHUFFNODE *entrynode, unsigned long int buffer, 
302                                                          int *bitsleft, unsigned short int *symbol)
303 {
304         char bit = 0;
305                 
306         /* Levanto el symbolo y si es uno valido, devuelvo */
307         *symbol = entrynode->symbol;
308         if (*symbol != 256) return entrynode;           
309         if (*bitsleft == 0) return entrynode;
310                 
311         /* Obtengo otro bit a procesar y me muevo en el arbol */
312         bit = (buffer >> ((*bitsleft)-1)) & 1;  
313         --(*bitsleft);
314         if (bit == 0) return shuff_decode_symbols(entrynode->lchild,buffer,bitsleft,symbol);
315         else return shuff_decode_symbols(entrynode->rchild,buffer,bitsleft,symbol);
316 }
317
318 int shuff_decode_chunk(HUFF_STATE *shuff, char *chunk, int chunksize, int *decodedbytes)
319 {
320         SHUFFNODE *currnode = shuff->codetree;  
321         unsigned short int decoded_symbol;      
322         *decodedbytes = 0;
323         
324         while (!vfeof(shuff->decoderfp) && (shuff->bytesleft > 0) && (*decodedbytes < chunksize)) {
325                 
326                 /* Leo un buffer de 32 bits si es que quedo vacio el anterior */
327                 if (shuff->bitsleft == 0) {
328                         if (vfread(&(shuff->codebuffer),sizeof(unsigned long int),1,shuff->decoderfp) != 1) continue;
329                         shuff->bitsleft = sizeof(unsigned long int) * 8;
330                 }
331                 
332                 /* Proceso el buffer sacando simbolos till se me agote el buffer, file o chunk */
333                 while ((shuff->bitsleft > 0) && (shuff->bytesleft > 0) && (*decodedbytes < chunksize)) {        
334                         currnode = shuff_decode_symbols(currnode,shuff->codebuffer,&(shuff->bitsleft),&decoded_symbol);
335                         /* Si obtuve un symbolo valido lo emito*/
336                         if (decoded_symbol != 256) {                            
337                                 chunk[(*decodedbytes)++] = decoded_symbol;
338                                 currnode = shuff->codetree;                             
339                                 --(shuff->bytesleft);
340                         }                                               
341                 }               
342         }
343         
344         if (shuff->bytesleft == 0) return 0;
345         else return 1;
346 }
347
348 int shuff_decode_file(HUFF_STATE *shuff)
349 {       
350         SHUFFNODE *currnode;    
351         unsigned long int codebuffer;   
352         FILE *fpdest;
353         unsigned short int decoded_symbol;
354         int bitsleft;   
355         
356         /* Comienzo a decodificar, pues la tabla ya la levante en el decinit */
357         if ((fpdest = fopen(shuff->targetfile,"w")) == NULL) return 0;  
358         currnode = shuff->codetree;
359         
360         while (!vfeof(shuff->decoderfp) && (shuff->bytesleft > 0)) {
361                 
362                 /* Leo un buffer de 32 bits */
363                 if (vfread(&codebuffer,sizeof(unsigned long int),1,shuff->decoderfp) != 1) continue;
364                 bitsleft = sizeof(unsigned long int) * 8;
365                 
366                 /* Proceso el buffer sacando simbolos hasta que se me agote */
367                 while ((bitsleft > 0) && (shuff->bytesleft > 0)) {      
368                         currnode = shuff_decode_symbols(currnode,codebuffer,&bitsleft,&decoded_symbol);
369                         /* Si obtuve un symbolo valido lo emito*/
370                         if (decoded_symbol != 256) {
371                                 fputc(decoded_symbol,fpdest);
372                                 currnode = shuff->codetree;
373                                 --(shuff->bytesleft);
374                         }                                               
375                 }               
376         }
377                         
378         /* Close destination */
379         fclose(fpdest); 
380         
381         return 1;
382 }
383
384 HUFF_STATE *shuff_init_decoder(char *inputfile, char *outputfile)
385 {
386         /* Locals */
387         HUFF_STATE *shuff = (HUFF_STATE*)malloc(sizeof(HUFF_STATE));                    
388         shuff->freqtable = (t_freq*)malloc(sizeof(t_freq)*256); 
389         
390         /* Init fields */
391         shuff->codebuffer = 0;
392         shuff->bitsleft = 0;
393         shuff->coderfp = NULL;
394         shuff->targetfile = NULL;
395         shuff->sourcefile = (char*)malloc(sizeof(char)*(strlen(inputfile)+1));
396         strcpy(shuff->sourcefile,inputfile);
397         if (outputfile != NULL) {
398                 shuff->targetfile = (char*)malloc(sizeof(char)*(strlen(outputfile)+1));
399                 strcpy(shuff->targetfile,outputfile);
400         }       
401         
402         /* Levanto cuantos bytes debo decodificar y la freqtable */
403         if ((shuff->decoderfp = vfopen(shuff->sourcefile,"r",0)) == NULL) return NULL;  
404         vfread(&(shuff->bytesleft),sizeof(unsigned long int),1,shuff->decoderfp);
405         vfread(shuff->freqtable,sizeof(t_freq),256,shuff->decoderfp);           
406         /* Armo el arbol de huffman que uso para decodificar */
407         shuff->codetree = shuff_buildtree(shuff->freqtable);
408         
409         return shuff;
410 }
411
412 HUFF_STATE *shuff_init_encoder_byfile(char *inputfile, char *outputfile, long volsize)
413 {
414         /* Locals */
415         HUFF_STATE *fshuff = (HUFF_STATE*)malloc(sizeof(HUFF_STATE));                   
416         int i;
417         
418         /* Inicializo la estructura para trabajar con Huff Static by File */
419         fshuff->coderfp = NULL; 
420         fshuff->decoderfp = NULL;       
421         fshuff->sourcefile = (char*)malloc(sizeof(char)*(strlen(inputfile)+1));
422         fshuff->targetfile = (char*)malloc(sizeof(char)*(strlen(outputfile)+1));
423         strcpy(fshuff->sourcefile,inputfile);   
424         strcpy(fshuff->targetfile,outputfile);
425         fshuff->volsize = volsize;
426         fshuff->bychunk = 0;
427         fshuff->canonic = 0;    
428         fshuff->freqtable = (t_freq*)malloc(sizeof(t_freq)*256);
429         for (i = 0; i < 256; ++i) fshuff->freqtable[i] = 0;     
430         fshuff->sumfreq = 0;            
431         fshuff->codetree = NULL;
432         
433         return fshuff;
434 }
435
436 HUFF_STATE *shuff_init_encoder_bychunk(char *outputfile, long volsize)
437 {
438         /* Locals */
439         HUFF_STATE *cshuff = (HUFF_STATE*)malloc(sizeof(HUFF_STATE));                   
440         int i;
441         
442         /* Inicializo la estructura para trabajar con Huff Static by Chunks */          
443         cshuff->decoderfp = NULL;
444         cshuff->sourcefile = (char*)malloc(sizeof(char)*(strlen(outputfile)+2));
445         cshuff->targetfile = (char*)malloc(sizeof(char)*(strlen(outputfile)+1));
446         strcpy(cshuff->targetfile,outputfile);  
447         strcpy(cshuff->sourcefile,outputfile);
448         strcat(cshuff->sourcefile,"~"); 
449         cshuff->volsize = volsize;
450         cshuff->bychunk = 1;
451         cshuff->canonic = 0;
452         cshuff->freqtable = (t_freq*)malloc(sizeof(t_freq)*256);        
453         for (i = 0; i < 256; ++i) cshuff->freqtable[i] = 0;     
454         cshuff->sumfreq = 0;    
455         cshuff->codetree = NULL;        
456         
457         /* Abrimos un archivo temporal para ir tirando los chunks */    
458         if ((cshuff->coderfp = fopen(cshuff->sourcefile,"w")) == NULL) return NULL;     
459
460         return cshuff;
461 }
462
463 int shuff_loadmodel(HUFF_STATE *shuff, char *modelfile) {
464
465         FILE *fp;
466         
467         if ((shuff) && (shuff->freqtable) && (modelfile)) {
468                 /* Cargo el modelo de disco */
469                 if ((fp = fopen(modelfile,"r")) == NULL) return 0;
470                 if (fread(shuff->freqtable,sizeof(t_freq),256,fp) != 256) return 0;
471                 shuff->canonic = 1;
472                 if (fp) fclose(fp);             
473                 return 1;
474         }       
475         return 0;       
476 }
477
478 int shuff_savemodel(HUFF_STATE *shuff) {
479
480         FILE *fp;
481         char *auxfilename;
482         char *stopchar;
483         
484         if ((shuff) && (shuff->targetfile) && (shuff->freqtable)) {
485                 /* Preparo el nombre del archivo con la tabla */
486                 auxfilename = (char*)malloc(strlen(shuff->targetfile)+1);               
487                 stopchar = strrchr(shuff->targetfile,'.');              
488                 strncpy(auxfilename,shuff->targetfile,stopchar - shuff->targetfile);
489                 auxfilename[stopchar - shuff->targetfile] = 0;
490                 strcat(auxfilename,".ftb");
491                 
492                 /* Lo creamos y dumpeamos la tabla de frecuencias (modelo) */
493                 if ((fp = fopen(auxfilename,"w")) == NULL) return 0;
494                 fwrite(shuff->freqtable,sizeof(t_freq),256,fp);
495                 if (fp) fclose(fp);
496                                 
497                 return 1;
498         }       
499         return 0;
500 }
501
502 void shuff_deinit_encoder(HUFF_STATE *shuff)
503 {
504         /* Libero mallocs y cierro archivos */
505         if (shuff->freqtable) free(shuff->freqtable);
506         if (shuff->coderfp) fclose(shuff->coderfp);
507         if (shuff->bychunk) unlink(shuff->sourcefile);
508         if (shuff->sourcefile) free(shuff->sourcefile); 
509         if (shuff->targetfile) free(shuff->targetfile);                         
510         
511         /* Destruyo recursivamente el arbol de codigos */
512 }
513
514 void shuff_deinit_decoder(HUFF_STATE *shuff)
515 {
516         /* Libero mallocs y cierro archivos */  
517         if (shuff->freqtable) free(shuff->freqtable);
518         if (shuff->sourcefile != NULL) free(shuff->sourcefile);
519         if (shuff->targetfile != NULL) free(shuff->targetfile);
520         if (shuff->decoderfp != NULL) vfclose(shuff->decoderfp);
521                 
522         /* Destruyo recursivamente el arbol de codigos */
523 }