32#define BTREEWORD short
34#define BTREEBIGNUM 32000
35#define BTREESLOPAGE 16384
63 unsigned int bitpattern,
68 BTREE_writebits(EC,dest,(
unsigned int) (bitpattern>>16), len-16);
69 BTREE_writebits(EC,dest,(
unsigned int) bitpattern, 16);
88static void BTREE_adjcount(
unsigned char *s,
unsigned char *bend,
BTREEWORD *count)
99 #define COUNTADJ(j) i.b[1] = i.b[0]; i.b[0] = *(s+j); ++count[i.w];
108 #define COUNTADJ(j) i = (BTREEWORD)(((i<<8) | *(s+j))); ++*(count+(int)i);
159static void BTREE_clearcount(
unsigned char *tryq,
BTREEWORD *countbuf)
166 intptr = (
int *) countbuf;
209 unsigned char *cluep,
210 unsigned char *rightp,
211 unsigned char *joinp,
233 *bend = (
unsigned char) clue;
236 while (!cluep[(
unsigned int) (*d++ = *s++)])
239 i = (
unsigned int) *(s-1);
249 { *(d-1) = (
unsigned char) clue;
262static unsigned int BTREE_findbest(
BTREEWORD *countptr,
265 unsigned int *bestval,
271 unsigned int bestsize;
279 for (i2=0;i2<256;++i2)
282 {
for (i1=0;i1<256;++i1)
289 while (bestval[i3-1]<i)
291 bestn[i3] = bestn[i3-1];
292 bestval[i3] = bestval[i3-1];
299 while (bestval[bestsize-1]<(bestval[1]/ratio))
302 i = bestval[1]/ratio;
304 i = bestval[bestsize-1];
319 unsigned int multimax,
326 unsigned char *treebuf;
331 int joinnode, leftnode, rightnode;
335 unsigned int cost, save;
336 unsigned int tcost, tsave;
339 unsigned int freeptr;
344 unsigned int bestsize;
349 unsigned int bt_size;
364 treebuf = (
unsigned char *)
galloc(treebufsize);
368 EC->
buf1 = (
unsigned char *)
galloc(buf1size);
372 EC->
buf2 = (
unsigned char *)
galloc(buf2size);
386 if (quick) ratio = quick;
398 { i = (
unsigned int) *ptr1++;
452 if (count2[sortptr[i]]<count2[sortptr[i-1]] || ((count2[sortptr[i]]==count2[sortptr[i-1]]) && (sortptr[i]>sortptr[i-1])))
455 sortptr[i] = sortptr[i-1];
463 i = sortptr[freeptr++];
464 clue = (
unsigned char) i;
476 bestval[0] = (
unsigned int) -1;
488 BTREE_clearcount(tryq,count);
495 BTREE_adjcount(ptr1, bend, count);
499 bestsize = BTREE_findbest(count,tryq,bestn,bestval,ratio);
510 leftnode = (bestn[i]>>8)&255;
511 rightnode = bestn[i]&255;
513 if ((tryq[leftnode]==1) && (tryq[rightnode]==1))
516 while ((freeptr<
BTREECODES) && (!freeq[sortptr[freeptr]]))
521 joinnode = sortptr[freeptr];
523 cost = 3+count2[joinnode];
531 bestjoin[i1] = (
unsigned char) joinnode;
532 bestn[i1] = bestn[i];
537 EC->
clueq[joinnode] = 3;
541 EC->
clueq[leftnode] = 1;
542 EC->
right[leftnode] = (
unsigned char) rightnode;
543 EC->
join[leftnode] = (
unsigned char) joinnode;
545 freeq[rightnode] = 0;
548 bt_node[bt_size] = joinnode;
549 bt_left[bt_size] = leftnode;
550 bt_right[bt_size] = rightnode;
574 for(i=1;i<bestsize;++i)
575 { leftnode = (bestn[i]>>8)&255;
576 joinnode = bestjoin[i];
577 EC->
clueq[leftnode] = EC->
clueq[joinnode] = 0;
590 BTREE_writebits(EC,dest,(
unsigned int) clue, 8);
591 BTREE_writebits(EC,dest,(
unsigned int) bt_size, 8);
593 for (i=0; i<bt_size;++i)
594 { BTREE_writebits(EC,dest,(
unsigned int) bt_node[i], 8);
595 BTREE_writebits(EC,dest,(
unsigned int) bt_left[i], 8);
596 BTREE_writebits(EC,dest,(
unsigned int) bt_right[i], 8);
607 BTREE_writebits(EC,dest,(
unsigned int) *ptr1++, 8);
609 BTREE_writebits(EC,dest,(
unsigned int) clue, 8);
610 BTREE_writebits(EC,dest,(
unsigned int) 0, 8);
612 BTREE_writebits(EC,dest,0L,7);
628 unsigned int multimax;
644 EC->
buffer = (
unsigned char *) (infile->
ptr);
652 outfile->
ptr = outfile->
ptr;
663 if (ulen==infile->
len)
665 BTREE_writebits(EC,outfile,(
unsigned int) 0x46fb, 16);
666 BTREE_writebits(EC,outfile,(
unsigned int) infile->
len, 24);
673 BTREE_writebits(EC,outfile,(
unsigned int) 0x47fb, 16);
674 BTREE_writebits(EC,outfile,(
unsigned int) ulen, 24);
675 BTREE_writebits(EC,outfile,(
unsigned int) infile->
len, 24);
678 BTREE_treepack(EC,outfile,passes, multimax, 0, zerosuppress);
680 return(outfile->
len);
699 infile.
ptr = (
char *)source;
700 infile.
len = sourcesize;
701 outfile.
ptr = (
char *)compresseddata;
702 outfile.
len = sourcesize;
704 plen = BTREE_compressfile(&EC,&infile, &outfile, sourcesize, opt);
int GCALL BTREE_encode(void *compresseddata, const void *source, int sourcesize, int *opts)
unsigned char join[BTREECODES]
unsigned char right[BTREECODES]
unsigned char clueq[BTREECODES]