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