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