38#define HUFFBIGNUM 32000
39#define HUFFTREESIZE 520
76static void HUFF_deltabytes(
const void *source,
void *dest,
int len)
78 const unsigned char *s = (
const unsigned char *) source;
79 unsigned char *d = (
unsigned char *) dest;
82 const unsigned char *send;
89 *d++ = (
unsigned char)(c1-c);
97 unsigned int bitpattern,
102 HUFF_writebits(EC,dest,(
unsigned int) (bitpattern>>16), len-16);
103 HUFF_writebits(EC,dest,(
unsigned int) bitpattern, 16);
128 { HUFF_treechase(EC,EC->
tree_left[node], bits+1);
129 HUFF_treechase(EC,EC->
tree_right[node], bits+1);
137 unsigned int val1, val2;
139 unsigned int ptr2, nodes;
155 list_count[i1++] = 0L;
159 { list_count[i1] = (
unsigned int) EC->
count[i];
181 val1 = list_count[--i];
183 val2 = list_count[--i];
188 val1 = list_count[i];
194 while (list_count[i] > val1)
198 { val1 = list_count[i];
203 val2 = list_count[i];
211 list_count[ptr1] = val1 + val2;
212 list_ptr[ptr1] = nodes;
213 list_count[ptr2] = list_count[--i1];
214 list_ptr[ptr2] = list_ptr[i1];
220 HUFF_treechase(EC,nodes-1, 0);
226 HUFF_treechase(EC,list_ptr[EC->
codes], 1);
231 unsigned int remaining,
234 int min, min1, use, newremaining;
237 {
min = HUFF_minrep(EC,remaining, r-1);
240 newremaining = remaining-(use*r);
241 min1 = HUFF_minrep(EC,newremaining, r-1)+EC->
bitsarray[EC->
clue+r]*use;
265 { dphuf = EC->
repbits[(
unsigned int) num];
266 dbase = (
unsigned int) EC->
repbase[(
unsigned int) num];
301 else if (num<131068L)
305 else if (num<262140L)
309 else if (num<524288L)
313 else if (num<1048576L)
322 HUFF_writebits(EC,dest,(
unsigned int) 0x00000001, dphuf+1);
323 HUFF_writebits(EC,dest,(
unsigned int) (num - dbase), dphuf+2);
333 HUFF_writenum(EC,dest,0L);
334 HUFF_writebits(EC,dest,(
unsigned int) code, 9);
342 HUFF_writeexp(EC,dest,code);
390 unsigned int chainsaw)
392 unsigned char *bptr1;
393 unsigned char *bptr2;
399 unsigned int thres=0;
401 unsigned int pattern;
406 unsigned int remaining;
420 for (i=0; i<768; ++i)
426 while (bptr1<EC->bufptr)
427 { i = (
unsigned int) *bptr1++;
436 while ((i == i1) && (bptr1 < bptr2))
438 i = (
unsigned int) *bptr1++;
448 ++EC->
count[((i+256-i1)&255)+256];
472 while (!EC->
count[i] && (i<256))
531 thres = (int) (EC->
ulen/25);
533 for (i=1;i<=(
unsigned int)EC->
maxdelta;++i)
534 if (EC->
count[256+i] > thres)
536 for (i=1;i<=(
unsigned int)(-EC->
mindelta);++i)
537 if (EC->
count[512-i] > thres)
542 for (i=0, i2=0;i<EC->
dclues;++i)
559 for (i=0;i<EC->
clues;++i)
580 for (i=1; i<EC->
clues; ++i)
585 { i1 = HUFF_minrep(EC,i,i1);
586 if ((i1 <= EC->bitsarray[EC->
clue+i])
604 { count2[i] = EC->
count[i];
607 EC->
count[256+i] = 0;
608 EC->
count[512+i] = 0;
614 while (bptr1<EC->bufptr)
615 { i = (
unsigned int) *bptr1++;
623 while ((i == i1) && (bptr1 < bptr2))
624 { i = (
unsigned int) *bptr1++;
633 if (count2[EC->
clue])
644 {
if (count2[EC->
clue+i3])
645 { rep1 = remaining/i3;
647 remaining = remaining-rep1*i3;
656 if ((ncode<=repn) && (ncode<=irep))
667 {
if (count2[EC->
clue+i3])
668 { rep1 = remaining/i3;
670 remaining = remaining-rep1*i3;
681 if (di <= EC->maxdelta)
683 { di = (i-i1-1)*2+EC->
dclue;
685 di = (i1-i-1)*2+EC->
dclue+1;
686 if (count2[di]>thres)
840 for (i=0; i<EC->
codes; ++i)
842 while (i2 < EC->bitsarray[i1])
844 pattern = pattern << 1;
856 unsigned char *bptr1;
857 unsigned char *bptr2;
863 unsigned int hlen, ibits, rladjust;
864 int di, firstcode, firstbits;
865 unsigned int rep1, repn, ncode, irep, remaining,curpc;
877 HUFF_writenum(EC,dest,(
unsigned int) EC->
ulen);
890 HUFF_writenum(EC,dest,(
unsigned int) i);
893 { HUFF_writenum(EC,dest,(
unsigned int) EC->
clue);
894 HUFF_writenum(EC,dest,(
unsigned int) EC->
clues);
897 { HUFF_writenum(EC,dest,(
unsigned int) EC->
dclue);
898 HUFF_writenum(EC,dest,(
unsigned int) EC->
dclues);
902 HUFF_writenum(EC,dest,(
unsigned int) EC->
mostbits);
906 HUFF_writebits(EC,dest,(
unsigned int) EC->
clue, 8);
911 HUFF_writenum(EC,dest,(
unsigned int) EC->
bitnum[i]);
942 HUFF_writenum(EC,dest,(
unsigned int) di);
963 while (bptr1<EC->bufptr)
964 { i = (
unsigned int) *bptr1++;
973 while ((i == i1) && (bptr1 < bptr2))
974 { i = (
unsigned int) *bptr1++;
995 { rep1 = remaining/i3;
997 remaining = remaining-rep1*i3;
1006 if ((ncode<=repn) && (ncode<=irep))
1009 HUFF_writecode(EC,dest,i1);
1015 HUFF_writenum(EC,dest,(
unsigned int) (i2-rladjust));
1024 { rep1 = remaining/i3;
1026 remaining = remaining-rep1*i3;
1028 HUFF_writecode(EC,dest,EC->
clue+i3);
1039 if ((di <= EC->maxdelta) && (di >= EC->
mindelta))
1040 { di = (i-i1-1)*2+EC->
dclue;
1042 di = (i1-i-1)*2+EC->
dclue+1;
1051 HUFF_writecode(EC,dest,i);
1053 if (((
int) bptr1- (
int) EC->
buffer) >= (
int)(EC->
plen+curpc))
1054 curpc = (int) bptr1 - (
int) EC->
buffer - EC->
plen;
1060 HUFF_writenum(EC,dest,0L);
1061 HUFF_writebits(EC,dest,(
unsigned int) 2, 2);
1065 HUFF_writebits(EC,dest,(
unsigned int) 0,7);
1077 unsigned int uptype=0;
1078 unsigned int chainsaw;
1096 EC->
buffer = (
unsigned char *) (infile->
ptr);
1104 outfile->
ptr = outfile->
ptr;
1113 HUFF_analysis(EC,opt, chainsaw);
1121 if (ulen==infile->
len)
1123 if (deltaed==0) uptype = 0xb0fb;
1124 else if (deltaed==1) uptype = 0xb2fb;
1125 else if (deltaed==2) uptype = 0xb4fb;
1126 HUFF_writebits(EC,outfile,(
unsigned int) uptype, 16);
1127 HUFF_writebits(EC,outfile,(
unsigned int) infile->
len, 32);
1134 if (deltaed==0) uptype = 0xb1fb;
1135 else if (deltaed==1) uptype = 0xb3fb;
1136 else if (deltaed==2) uptype = 0xb5fb;
1137 HUFF_writebits(EC,outfile,(
unsigned int) uptype, 16);
1138 HUFF_writebits(EC,outfile,(
unsigned int) ulen, 32);
1139 HUFF_writebits(EC,outfile,(
unsigned int) infile->
len, 32);
1147 if (ulen==infile->
len)
1149 if (deltaed==0) uptype = 0x30fb;
1150 else if (deltaed==1) uptype = 0x32fb;
1151 else if (deltaed==2) uptype = 0x34fb;
1152 HUFF_writebits(EC,outfile,(
unsigned int) uptype, 16);
1153 HUFF_writebits(EC,outfile,(
unsigned int) infile->
len, 24);
1160 if (deltaed==0) uptype = 0x31fb;
1161 else if (deltaed==1) uptype = 0x33fb;
1162 else if (deltaed==2) uptype = 0x35fb;
1163 HUFF_writebits(EC,outfile,(
unsigned int) uptype, 16);
1164 HUFF_writebits(EC,outfile,(
unsigned int) ulen, 24);
1165 HUFF_writebits(EC,outfile,(
unsigned int) infile->
len, 24);
1169 HUFF_pack(EC,outfile, opt);
1171 return(outfile->
len);
1197 infile.
ptr = (
char *)source;
1201 deltabuf =
galloc(sourcesize);
1202 HUFF_deltabytes(source,deltabuf,sourcesize);
1203 infile.
ptr = (
char *) deltabuf;
1207 deltabuf =
galloc(sourcesize);
1208 HUFF_deltabytes(source,deltabuf,sourcesize);
1209 HUFF_deltabytes(deltabuf,deltabuf,sourcesize);
1210 infile.
ptr = (
char *) deltabuf;
1214 infile.
len = sourcesize;
1215 outfile.
ptr = (
char *)compresseddata;
1216 outfile.
len = sourcesize;
1218 plen = HUFF_packfile(EC,&infile, &outfile, sourcesize, opt);
1220 if (deltabuf)
gfree(deltabuf);
int GCALL HUFF_encode(void *compresseddata, const void *source, int sourcesize, int *opts)
unsigned int tree_left[HUFFTREESIZE]
unsigned int bitsarray[HUFFCODES]
unsigned int patternarray[HUFFCODES]
unsigned int repbase[HUFFREPTBL]
unsigned int bitnum[HUFFMAXBITS+1]
unsigned int repbits[HUFFREPTBL]
unsigned int tree_right[HUFFTREESIZE]
char qleapcode[HUFFCODES]
unsigned int sortptr[HUFFCODES]