2 #include "statichuff.h"
4 void putbit(char bit, char restart, char flush, FILE *fp)
6 static unsigned long int bits_buffer = 0;
7 static unsigned char bits_used = 0;
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 fwrite(&bits_buffer,sizeof(unsigned long int),1,fp);
18 /* me indican que comienza un nuevo output */
23 /* inserto el bit en el buffer */
24 bits_buffer = bits_buffer << 1;
28 /* lleno el buffer, escribo */
29 if (bits_used == 32) {
30 fwrite(&bits_buffer,sizeof(unsigned long int),1,fp);
37 void shuff_cpynode(SHUFFNODE *node1, SHUFFNODE *node2)
39 node1->symbol = node2->symbol;
40 node1->freq = node2->freq;
41 node1->lchild = node2->lchild;
42 node1->rchild = node2->rchild;
45 int shuff_compnode(SHUFFNODE *node1, SHUFFNODE *node2)
47 if (node1->freq < node2->freq) return 1;
48 if (node1->freq > node2->freq) return -1;
52 int shuff_rescalefreq(t_freq *freqtable)
57 /* Divido por la mitad las frecuencias, asegurando de no perder */
58 /* frequencias en 1, por ello le sumo 1 antes de partir */
59 for (i = 0; i < 256; i++) {
60 freqtable[i] = (freqtable[i] << 2) | 1;
61 totalfreq += freqtable[i];
67 int shuff_scanfreq(char *inputfile, t_freq *freqtable)
74 /* Inicializamos la tabla de frecuencias */
75 for (i = 0; i < 256; ++i) freqtable[i] = 0;
78 if ((fp = fopen(inputfile,"rb")) == NULL) return 0;
80 /* Contamos las frecuencias */
82 if (symbol == EOF) continue;
87 /* Si llegue al tope de freq acumulada, halve em */
88 if (sumfreq == 14930352)
89 sumfreq = shuff_rescalefreq(freqtable);
96 SHUFFNODE *shuff_buildlist(t_freq *freqtable, int *nonzerofreqs)
98 int i,j = 0,nonzero = 0;
101 /* Calculo cuantas frequencias > 0 hay y creo la tabla */
102 for (i = 0; i < 256; ++i) if (freqtable[i] > 0) nonzero++;
103 inputlist = (SHUFFNODE*)malloc(sizeof(SHUFFNODE)*nonzero);
105 /* Cargo la inputlist del huffman solo con freqs > 0 */
106 for (i = 0; i < 256; ++i)
107 if (freqtable[i] > 0) {
108 inputlist[j].symbol = i;
109 inputlist[j].freq = freqtable[i];
110 inputlist[j].lchild = NULL;
111 inputlist[j].rchild = NULL;
115 *nonzerofreqs = nonzero;
119 SHUFFNODE *shuff_buildtree(SHUFFNODE *list, int listcount)
121 SHUFFNODE *lastsymbol = list+(listcount-1);
122 SHUFFNODE *node1,*node2,*fictnode;
124 /* Ordenamos inicialmente la inputlist para tomar las dos freqs min */
125 while (lastsymbol > list) {
126 /* Ordeno la lista por frecuencia descendente */
127 qsort(list,listcount,sizeof(SHUFFNODE),shuff_compnode);
128 /* Tomo los ultimos dos elementos, generando dos nodos del arbol */
129 node1 = (SHUFFNODE*)malloc(sizeof(SHUFFNODE));
130 node2 = (SHUFFNODE*)malloc(sizeof(SHUFFNODE));
131 shuff_cpynode(node1,lastsymbol-1);
132 shuff_cpynode(node2,lastsymbol);
134 /* Nodo ficticio con la suma de las probs y los ptros a childs */
135 lastsymbol->symbol = 256;
136 lastsymbol->freq = node1->freq + node2->freq;
137 lastsymbol->lchild = node1;
138 lastsymbol->rchild = node2;
142 /* Devuelvo el puntero a la raiz del arbol de huffman */
146 void shuff_printcodes(SHUFFCODE *codetable,t_freq *freqtable)
149 unsigned short int auxcode;
152 for (i = 0; i < 256; ++i) {
153 if (codetable[i].codelength > 0) {
154 auxcode = codetable[i].code;
155 printf("Symbol:%i Freq: %li Code:",i,freqtable[i]);
156 for (j = codetable[i].codelength-1; j >= 0; --j) {
157 auxcode = codetable[i].code;
158 auxcode = auxcode >> j;
162 printf(" Length:%i\n",codetable[i].codelength);
167 void shuff_zerocodes(SHUFFCODE *table)
171 /* Inicializo los codigos prefijos */
172 for (i = 0; i < 256; ++i) {
174 table[i].codelength = 0;
178 void shuff_buildcodes(SHUFFCODE *table, SHUFFNODE *node, int level, int code)
180 if (node->symbol < 256) {
181 /* Guardo el codigo en la tabla */
182 table[node->symbol].code = code;
183 table[node->symbol].codelength = level;
187 shuff_buildcodes(table,node->lchild,level+1,code);
189 shuff_buildcodes(table,node->rchild,level+1,code);
195 int shuff_encode_symbols(t_freq *ftable, SHUFFCODE *ctable, char* inputfile, char *outputfile) {
197 FILE *fpsource,*fpdest;
199 unsigned long int sourcesize;
201 SHUFFCODE symbolcode;
203 /* Abrimos el file */
204 if ((fpsource = fopen(inputfile,"rb")) == NULL) return 0;
205 if ((fpdest = fopen(outputfile,"wb")) == NULL) return 0;
207 /* Guardamos el size el archivo original e inputlist como header */
208 fseek(fpsource,0,SEEK_END);
209 sourcesize = ftell(fpsource);
210 fwrite(&sourcesize,sizeof(unsigned long int),1,fpdest);
211 fwrite(ftable,sizeof(t_freq),256,fpdest);
214 fseek(fpsource,0,SEEK_SET);
215 while (!feof(fpsource)) {
216 /* Levanto un symbolo (byte) */
217 symbol = fgetc(fpsource);
218 if (symbol == EOF) continue;
220 /* Cargamos el codigo y lo emitimos */
221 symbolcode = ctable[symbol];
222 for (i = symbolcode.codelength; i > 0; --i) {
223 bit = (symbolcode.code >> (i-1)) & 1;
224 putbit(bit,0,0,fpdest);
228 /* Hacemos un flush de lo que haya quedado en el buffer de salida */
229 putbit(0,0,1,fpdest);
235 int shuff_encode_file(char *inputfile, char *outputfile)
238 t_freq *freqtable = (t_freq*)malloc(sizeof(t_freq)*256);
239 SHUFFNODE *inputlist;
241 SHUFFCODE *codetable = (SHUFFCODE*)malloc(sizeof(SHUFFCODE)*256);
244 /* Armamos la tabla de frecuencias */
245 if (!shuff_scanfreq(inputfile,freqtable)) return -1;
247 /* Armo el input list y genero el arbol de huffman */
248 inputlist = shuff_buildlist(freqtable, &freqcount);
249 codetree = shuff_buildtree(inputlist,freqcount);
251 /* Armo la tabla de codigos prefijos para el encoder */
252 shuff_zerocodes(codetable);
253 shuff_buildcodes(codetable,codetree,0,0);
254 shuff_printcodes(codetable,freqtable);
256 /* Encodeo byte per byte */
257 shuff_encode_symbols(freqtable,codetable,inputfile,outputfile);
260 int shuff_decode(char *inputfile, char *outputfile)
262 SHUFFNODE *inputlist;
264 t_freq *ftable = (t_freq*)malloc(sizeof(t_freq)*256);
265 unsigned long int bytesleft;
270 /* Levanto cuantos bytes decodeo y la freq table */
271 if ((fpsource = fopen(inputfile,"rb")) == NULL) return 0;
272 if ((fpdest = fopen(outputfile,"wb")) == NULL) return 0;
273 fread(&bytesleft,sizeof(unsigned long int),1,fpsource);
274 fread(ftable,sizeof(unsigned long int),256,fpsource);
275 inputlist = shuff_buildlist(ftable, &freqcount);
276 codetree = shuff_buildtree(inputlist,freqcount);
284 int main(int argc, char* argv[])
286 if (argc == 1) return -1;
289 shuff_encode_file(argv[1],"output.shf");
292 shuff_decode("output.shf","decoded.dat");