1、哈夫曼算法#include #include #include #include #include #include #include #include #include #define EXIT_OK 0#define EXIT_FAILED -1/FILE*infile, *outfile;unsigned long inttextsize = 0, codesize = 0, printcount = 0;void Error(char *message)printf(n%sn, message);exit(-1);/* LZSS Parameters */#define N 4096/
2、* Size of string buffer */#define F 60/* Size of look-ahead buffer 60#define THRESHOLD 2#define NIL N /* End of trees node*/unsigned chartext_bufN + F -1;/-1int match_position, match_length,lsonN + 1, rsonN + 257, dadN + 1;void InitTree(void)/* Initializing tree */inti;for (i = N + 1; i = N + 256; i
3、+)rsoni = NIL;/* root */for (i = 0; i = 0) if (rsonp != NIL)p = rsonp;else rsonp = r;dadr = p;return; else if (lsonp != NIL)p = lsonp;else lsonp = r;dadr = p;return;for (i = 1; i THRESHOLD) if (i match_length) match_position = (r - p) & (N - 1) - 1;if (match_length = i) = F)break;if (i = match_lengt
4、h) if (c = (r - p) & (N - 1) - 1) match_positon) match_position = c;dadr = dadp;lsonr = lsonp;rsonr = rsonp;dadlsonp = r;dadrsonp = r;if (rsondadp = p)rsondadp = r;elselsondadp = r;dadp = NIL;/* remove p */void DeleteNode(int p)/* Deleting node from the tree */intq;if (dadp = NIL)return; /* unregist
5、ered */if (rsonp = NIL)q = lsonp;elseif (lsonp = NIL)q = rsonp;else q = lsonp;if (rsonq != NIL) do q = rsonq; while (rsonq != NIL);rsondadq = lsonq;dadlsonq = dadq;lsonq = lsonp;dadlsonp = q;rsonq = rsonp;dadrsonp = q;dadq = dadp;if (rsondadp = p)rsondadp = q;elselsondadp = q;dadp = NIL;/* Huffman c
6、oding parameters */#define N_CHAR(256 - THRESHOLD + F)/* character code (= 0.N_CHAR-1) */#define T (N_CHAR * 2 - 1)/* Size of table */#define R (T - 1) /* root position */#define MAX_FREQ0x8000/* update when cumulative frequency */* reaches to this value */typedef unsigned char uchar;/* Tables for e
7、ncoding/decoding upper 6 bits of* sliding dictionary pointer*/* encoder table */uchar p_len64 = 0x03, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05,0x05, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06, 0x06,0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,0x07, 0x07, 0x07
8、, 0x07, 0x07, 0x07, 0x07, 0x07,0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08;uchar p_code64 = 0x00, 0x20, 0x30, 0x40, 0x50, 0x58, 0x60, 0x68,0x70, 0x78, 0x80, 0x88, 0x90, 0x94, 0x98, 0x9C, 0xA0, 0xA4, 0xA8
9、, 0xAC, 0xB0, 0xB4, 0xB8, 0xBC,0xC0, 0xC2, 0xC4, 0xC6, 0xC8, 0xCA, 0xCC, 0xCE,0xD0, 0xD2, 0xD4, 0xD6, 0xD8, 0xDA, 0xDC, 0xDE,0xE0, 0xE2, 0xE4, 0xE6, 0xE8, 0xEA, 0xEC, 0xEE,0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF;/* decoder table */uchar d_code256
10、 = 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,0x02, 0x02, 0x0
11、2, 0x02, 0x02, 0x02, 0x02, 0x02,0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,0x06, 0x06, 0x06, 0x06, 0x06, 0x0
12、6, 0x06, 0x06,0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A,0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B,0x0C, 0x0C, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D,0x0E
13、, 0x0E, 0x0E, 0x0E, 0x0F, 0x0F, 0x0F, 0x0F,0x10, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11,0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13,0x14, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15,0x16, 0x16, 0x16, 0x16, 0x17, 0x17, 0x17, 0x17,0x18, 0x18, 0x19, 0x19, 0x1A, 0x1A, 0x1B, 0x1B,0x1C, 0x1C, 0x1D, 0x1D
14、, 0x1E, 0x1E, 0x1F, 0x1F,0x20, 0x20, 0x21, 0x21, 0x22, 0x22, 0x23, 0x23,0x24, 0x24, 0x25, 0x25, 0x26, 0x26, 0x27, 0x27,0x28, 0x28, 0x29, 0x29, 0x2A, 0x2A, 0x2B, 0x2B,0x2C, 0x2C, 0x2D, 0x2D, 0x2E, 0x2E, 0x2F, 0x2F,0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E
15、, 0x3F,;uchar d_len256 = 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04
16、, 0x04,0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,0x05, 0x05,
17、 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,0x06, 0x06, 0x06, 0x06, 0x06,
18、 0x06, 0x06, 0x06,0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07
19、,0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,0x08, 0x08, 0x08,
20、 0x08, 0x08, 0x08, 0x08, 0x08,;unsigned freqT + 1; /* cumulative freq table */* pointing parent nodes.* area T.(T + N_CHAR - 1) are pointers for leaves*/int prntT + N_CHAR;/* pointing children nodes (son, son + 1)*/int sonT;unsigned getbuf = 0;uchar getlen = 0;int GetBit(void)/* get one bit */int i;while (getlen = 8) if (i = getc(infile) 0) i = 0;getbuf |= i (8 - getlen);getlen += 8;i = getbuf;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1