2 #include "statichuff.h"
6 void putbit(char bit, char restart, char flush, VFILE *fp)
8 static unsigned long int bits_buffer = 0;
9 static unsigned char bits_used = 0;
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);
19 /* me indican que comienza un nuevo output */
24 /* inserto el bit en el buffer */
25 bits_buffer = bits_buffer << 1;
29 /* lleno el buffer, escribo */
30 if (bits_used == 32) {
31 vfwrite(&bits_buffer,sizeof(unsigned long int),1,fp);
38 void shuff_cpynode(SHUFFNODE *node1, SHUFFNODE *node2)
40 node1->symbol = node2->symbol;
41 node1->freq = node2->freq;
42 node1->lchild = node2->lchild;
43 node1->rchild = node2->rchild;
46 int shuff_compnode(const void *node1, const void *node2)
48 if (((SHUFFNODE*)node1)->freq < ((SHUFFNODE*)node2)->freq) return 1;
49 if (((SHUFFNODE*)node1)->freq > ((SHUFFNODE*)node2)->freq) return -1;
53 void shuff_destroy_tree(SHUFFNODE *node) {
54 /* Si llegue a una hoja, destruyo y vuelvo */
55 if (node->symbol < 256) {
60 /* Desciendo por izq, luego por derecha y luego libero */
61 shuff_destroy_tree(node->lchild);
62 shuff_destroy_tree(node->rchild);
68 int shuff_rescalefreq(t_freq *freqtable)
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];
82 int shuff_scanfreq_chunk(HUFF_STATE *chunkshuff, char* chunk, int chunksize)
86 unsigned char symbol = 0;
88 /* Contamos las frecuencias del chunk a menos que se use un canonico */
89 if (!chunkshuff->canonic) {
90 for (i = 0; i < chunksize; ++i) {
92 chunkshuff->freqtable[symbol] += 1;
93 chunkshuff->sumfreq += 1;
95 /* Si llegue al tope de freq acumulada, halve em */
96 if (chunkshuff->sumfreq == 14930352)
97 chunkshuff->sumfreq = shuff_rescalefreq(chunkshuff->freqtable);
101 /* Dumpeamos el chunk en el temporal homero */
102 fwrite(chunk,chunksize,1,chunkshuff->coderfp);
107 int shuff_scanfreq(char *inputfile, t_freq *freqtable)
114 /* Inicializamos la tabla de frecuencias */
115 for (i = 0; i < 256; ++i) freqtable[i] = 0;
117 /* Abrimos el file */
118 if ((fp = fopen(inputfile,"r")) == NULL) return 0;
120 /* Contamos las frecuencias */
122 if (symbol == EOF) continue;
127 /* Si llegue al tope de freq acumulada, halve em */
128 if (sumfreq == 14930352)
129 sumfreq = shuff_rescalefreq(freqtable);
136 SHUFFNODE *shuff_buildlist(t_freq *freqtable, int *nonzerofreqs)
138 int i,j = 0,nonzero = 0;
139 SHUFFNODE *inputlist;
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);
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;
155 *nonzerofreqs = nonzero;
159 SHUFFNODE *shuff_buildtree(t_freq *ftable)
161 SHUFFNODE *lastsymbol;
162 SHUFFNODE *node1,*node2,*root;
163 SHUFFNODE *inputlist;
166 /* Genero la input list en base a la cual genera el arbol */
167 inputlist = shuff_buildlist(ftable, &freqcount);
168 lastsymbol = inputlist+(freqcount-1);
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);
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;
187 /* Copio la raiz para poder liberar la lista sin perderla */
188 root = (SHUFFNODE*)malloc(sizeof(SHUFFNODE));
189 shuff_cpynode(root,lastsymbol);
194 /* Devuelvo el puntero a la raiz del arbol de huffman */
198 void shuff_printcodes(SHUFFCODE *codetable,t_freq *freqtable)
201 unsigned short int auxcode;
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;
214 printf(" Length:%i\n",codetable[i].codelength);
219 void shuff_zerocodes(SHUFFCODE *table)
223 /* Inicializo los codigos prefijos */
224 for (i = 0; i < 256; ++i) {
226 table[i].codelength = 0;
230 void shuff_buildcodes(SHUFFCODE *table, SHUFFNODE *node, int level, int code)
232 if (node->symbol < 256) {
233 /* Guardo el codigo en la tabla */
234 table[node->symbol].code = code;
235 table[node->symbol].codelength = level;
239 shuff_buildcodes(table,node->lchild,level+1,code);
241 shuff_buildcodes(table,node->rchild,level+1,code);
245 int shuff_encode_symbols(HUFF_STATE *shuff, SHUFFCODE *ctable)
250 unsigned long int sourcesize;
252 SHUFFCODE symbolcode;
254 /* Abrimos el source y el destino */
255 if (shuff->coderfp != NULL) {
256 fclose(shuff->coderfp); /* close bychunk temp file */
257 shuff->coderfp = NULL;
259 if ((fpsource = fopen(shuff->sourcefile,"r")) == NULL) return 0;
260 if ((fpdest = vfopen(shuff->targetfile,"w",shuff->volsize)) == NULL) return 0;
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);
269 fseek(fpsource,0,SEEK_SET);
270 while (!feof(fpsource)) {
271 /* Levanto un symbolo (byte) */
272 symbol = fgetc(fpsource);
273 if (symbol == EOF) continue;
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);
283 /* Hacemos un flush de lo que haya quedado en el buffer de salida */
284 putbit(0,0,1,fpdest);
290 int shuff_encode_file(HUFF_STATE *shuff)
293 SHUFFCODE *codetable = (SHUFFCODE*)malloc(sizeof(SHUFFCODE)*256);
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;
299 /* Genero el arbol de huffman */
300 shuff->codetree = shuff_buildtree(shuff->freqtable);
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);*/
307 /* Encodeo byte per byte */
308 shuff_encode_symbols(shuff,codetable);
310 /* Free up memory baby yeah */
316 SHUFFNODE *shuff_decode_symbols(SHUFFNODE *entrynode, unsigned long int buffer,
317 int *bitsleft, unsigned short int *symbol)
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;
326 /* Obtengo otro bit a procesar y me muevo en el arbol */
327 bit = (buffer >> ((*bitsleft)-1)) & 1;
329 if (bit == 0) return shuff_decode_symbols(entrynode->lchild,buffer,bitsleft,symbol);
330 else return shuff_decode_symbols(entrynode->rchild,buffer,bitsleft,symbol);
333 int shuff_decode_chunk(HUFF_STATE *shuff, char *chunk, int chunksize, int *decodedbytes)
335 SHUFFNODE *currnode = shuff->codetree;
336 unsigned short int decoded_symbol;
339 while (!vfeof(shuff->decoderfp) && (shuff->bytesleft > 0) && (*decodedbytes < chunksize)) {
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;
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);
359 if (shuff->bytesleft == 0) return 0;
363 int shuff_decode_file(HUFF_STATE *shuff)
366 unsigned long int codebuffer;
368 unsigned short int decoded_symbol;
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;
375 while (!vfeof(shuff->decoderfp) && (shuff->bytesleft > 0)) {
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;
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);
393 /* Close destination */
399 HUFF_STATE *shuff_init_decoder(char *inputfile, char *outputfile)
402 HUFF_STATE *shuff = (HUFF_STATE*)malloc(sizeof(HUFF_STATE));
403 shuff->freqtable = (t_freq*)malloc(sizeof(t_freq)*256);
406 shuff->codebuffer = 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);
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);
427 HUFF_STATE *shuff_init_encoder_byfile(char *inputfile, char *outputfile, long volsize)
430 HUFF_STATE *fshuff = (HUFF_STATE*)malloc(sizeof(HUFF_STATE));
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;
443 fshuff->freqtable = (t_freq*)malloc(sizeof(t_freq)*256);
444 for (i = 0; i < 256; ++i) fshuff->freqtable[i] = 0;
446 fshuff->codetree = NULL;
451 HUFF_STATE *shuff_init_encoder_bychunk(char *outputfile, long volsize)
454 HUFF_STATE *cshuff = (HUFF_STATE*)malloc(sizeof(HUFF_STATE));
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;
467 cshuff->freqtable = (t_freq*)malloc(sizeof(t_freq)*256);
468 for (i = 0; i < 256; ++i) cshuff->freqtable[i] = 0;
470 cshuff->codetree = NULL;
472 /* Abrimos un archivo temporal para ir tirando los chunks */
473 if ((cshuff->coderfp = fopen(cshuff->sourcefile,"w")) == NULL) return NULL;
478 int shuff_loadmodel(HUFF_STATE *shuff, char *modelfile) {
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;
493 int shuff_savemodel(HUFF_STATE *shuff) {
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");
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);
517 void shuff_deinit_encoder(HUFF_STATE *shuff)
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);
526 /* Destruyo recursivamente el arbol de codigos */
527 if (shuff->codetree) shuff_destroy_tree(shuff->codetree);
530 void shuff_deinit_decoder(HUFF_STATE *shuff)
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);
538 /* Destruyo recursivamente el arbol de codigos */
539 if (shuff->codetree) shuff_destroy_tree(shuff->codetree);