47#define DICSIZ (1<<DICBIT)
50#define MAXMATCH (1<<MATCHBIT)
52#define PERCFLAG (0x8000)
54#define NC (UCHARMAX+MAXMATCH+2-THRESHOLD)
65#define MAXHASHVAL (3*DICSIZ+((DICSIZ>>9)+1)*UCHARMAX)
68#define WINDOWSIZE (1<<WINBIT)
71#define BUFSIZE (1<<BUFBIT)
79 word Left[2*
NC-1], Right[2*
NC-1];
117static byte GetC(
ARDATA *ar);
118static void PutC(
ARDATA *ar,
byte c);
119static int BRead(
ARDATA *ar,
byte *p, word n);
120static void BWrite(
ARDATA *ar,
byte *p,
int n);
121static void FillBuf(
ARDATA *ar, word n);
122static word GetBits(
ARDATA *ar,
short n);
123static void PutBits(
ARDATA *ar, word n, word x);
124static void InitGetBits(
ARDATA *ar);
125static void InitPutBits(
ARDATA *ar);
127static void ReadPtLen(
ARDATA *ar,
short nn,
short nbit,
short ispecial);
128static void ReadCLen(
ARDATA *ar);
129static word DecodeC(
ARDATA *ar);
130static word DecodeP(
ARDATA *ar);
131static void InitVars(
ARDATA *ar);
132static void DecodeBuffer(
ARDATA *ar, word count,
ARPByte arbuffer);
133static void Decode(
ARDATA *ar);
134static void CountLen(
ARDATA *ar,
short i, word *lencnt,
short nparm);
135static void MakeLen(
ARDATA *ar,
short root, word *lencnt,
byte *len,
short nparm,
ARPWord sortptr);
136static void DownHeap(word *heap,
short i,
short heapsize,
ARPWord freqparm);
137static void MakeCode(
short n,
ARPByte len,
ARPWord code, word *lencnt);
139static void CountTFreq(
ARDATA *ar);
140static void WritePtLen(
ARDATA *ar,
short n,
short nbit,
short ispecial);
141static void WriteCLen(
ARDATA *ar);
142static void EncodeC(
ARDATA *ar,
short c);
143static void EncodeP(
ARDATA *ar, word p);
144static void SendBlock(
ARDATA *ar);
145static word Output(
ARDATA *ar, word c, word p, word outputpos);
146static void InitSlide(
ARDATA *ar);
147static short Hash(
short p,
byte c);
148static short Child(
ARDATA *ar,
short q,
byte c);
149static void MakeChild(
ARDATA *ar,
short q,
byte c,
short r);
150static void Split(
ARDATA *ar,
short old);
151static void InsertNode(
ARDATA *ar);
152static void DeleteNode(
ARDATA *ar);
153static void GetNextMatch(
ARDATA *ar);
154static int allocate_memory(
ARDATA *ar);
155static void free_memory(
ARDATA *ar);
156static void Encode(
ARDATA *ar);
160static inline byte GetC(
ARDATA *ar)
169static inline void PutC(
ARDATA *ar,
byte c)
182static int BRead(
ARDATA *ar,
byte *p, word n)
191static void BWrite(
ARDATA *ar,
byte *p,
int n)
202static void FillBuf(
ARDATA *ar, word n)
222static word GetBits(
ARDATA *ar,
short n)
229static void PutBits(
ARDATA *ar, word n, word x)
256static void InitGetBits(
ARDATA *ar)
264static void InitPutBits(
ARDATA *ar)
275 word count[17], weight[17];
278 word i, k, len, ch, jutbits, avail;
283 for(i=0; i<nchar; i++)
287 start[i+1] = start[i] + (count[i]<<(16-i));
293 jutbits = 16-tablebits;
294 for(i=1; i<=tablebits; i++)
296 start[i] >>= jutbits;
297 weight[i] = 1<<(tablebits-i);
302 weight[i] = 1<<(16-i);
305 i = start[tablebits+1]>>jutbits;
316 mask = 1<<(15-tablebits);
317 for(ch=0; ch<nchar; ch++)
323 nextcode = k+weight[len];
326 for(i=k; i<nextcode; i++)
331 p = &table[k>>jutbits];
337 ar->
Right[avail] = 0;
351 start[len] = nextcode;
355static void ReadPtLen(
ARDATA *ar,
short nn,
short nbit,
short ispecial)
359 n = GetBits(ar, nbit);
362 c = GetBits(ar, nbit);
409static void ReadCLen(
ARDATA *ar)
413 n = GetBits(ar,
CBIT);
416 c = GetBits(ar,
CBIT);
419 for(i=0; i<4096; i++)
440 FillBuf(ar, ar->
PtLen[c]);
444 c = 2+GetBits(ar, 4);
446 c = 19+GetBits(ar,
CBIT);
469static word DecodeC(
ARDATA *ar)
475 ReadPtLen(ar,
NT,
TBIT, 3);
481 ReadPtLen(ar,
NP,
PBIT, -1);
499 FillBuf(ar, ar->
CLen[j]);
503static word DecodeP(
ARDATA *ar)
519 FillBuf(ar, ar->
PtLen[j]);
523 j = (1<<j)+GetBits(ar,j);
528static void InitVars(
ARDATA *ar)
530 memset(ar, 0,
sizeof(
ARDATA));
533static void DecodeBuffer(
ARDATA *ar, word count,
ARPByte arbuffer)
540 arbuffer[r] = arbuffer[ar->
Decode_I];
543 if(r == count)
return;
554 if(r == count)
return;
563 arbuffer[r] = arbuffer[ar->
Decode_I];
566 if(r == count)
return;
573static void Decode(
ARDATA *ar)
589 DecodeBuffer(ar, a, dp);
600static void CountLen(
ARDATA *ar,
short i, word *lencnt,
short nparm)
603 short buf1[
NC/2+1], buf2[
NC/2+1];
604 short *old = buf1, *
new = buf2;
605 int oldsz, newsz = 1;
613 oldsz = newsz; newsz = 0;
615 for(n = 0; n < oldsz; n++)
627 new[newsz] = ar->
Left[j];
628 new[newsz+1] = ar->
Right[j];
636static void MakeLen(
ARDATA *ar,
short root, word *lencnt,
byte *len,
short nparm,
ARPWord sortptr)
642 CountLen(ar, root, lencnt, nparm);
645 cum += lencnt[i]<<(16-i);
672static void DownHeap(word *heap,
short i,
short heapsize,
ARPWord freqparm)
679 if(j < heapsize && freqparm[heap[j]] > freqparm[heap[j+1]]) j++;
680 if(freqparm[k] <= freqparm[heap[j]])
break;
688static void MakeCode(
short n,
ARPByte len,
ARPWord code, word *lencnt)
694 start[i+1] = (start[i]+lencnt[i]) << 1;
705 short i, j, k, avail;
712 for(i=0; i<nparm; i++)
723 codeparm[heap[1]] = 0;
726 for(i=heapsize/2; i>0; i--) DownHeap(heap, i, heapsize, freqparm);
736 heap[1] = heap[heapsize];
738 DownHeap(heap, 1, heapsize, freqparm);
747 freqparm[k] = freqparm[i]+freqparm[j];
749 DownHeap(heap, 1, heapsize, freqparm);
752 }
while(heapsize > 1);
753 MakeLen(ar, k, lencnt, len, nparm, codeparm);
754 MakeCode(nparm, len, codeparm, lencnt);
758static void CountTFreq(
ARDATA *ar)
760 short i, k, n, count;
761 for(i = 0; i<
NT; i++) ar->
TFreq[i] = 0;
763 while(n>0 && ar->
CLen[n-1] == 0)
775 while(i<n && ar->CLen[i] == 0)
781 ar->
TFreq[0] += count;
797static void WritePtLen(
ARDATA *ar,
short n,
short nbit,
short ispecial)
800 while(n>0 && ar->
PtLen[n-1] == 0) n--;
801 PutBits(ar, nbit, n);
812 PutBits(ar, k, (1<<k)-2);
816 while(i<6 && ar->PtLen[i] == 0) i++;
817 PutBits(ar, 2, (i-3) & 3);
822static void WriteCLen(
ARDATA *ar)
824 short i, k, n, count;
826 while(n>0 && ar->
CLen[n-1] == 0) n--;
827 PutBits(ar,
CBIT, n);
836 while(i<n && ar->CLen[i] == 0)
843 for(k=0; k<count; k++)
849 PutBits(ar, 4, count-3);
860 PutBits(ar,
CBIT, count-20);
868static void EncodeC(
ARDATA *ar,
short c)
873static void EncodeP(
ARDATA *ar, word p)
884 if(c > 1) PutBits(ar, c-1, p & (0xFFFF>>(17-c)));
887static void SendBlock(
ARDATA *ar)
889 word i, k, flags, root, pos, size;
892 size = ar->
CFreq[root];
893 PutBits(ar, 16, size);
899 WritePtLen(ar,
NT,
TBIT, 3);
902 PutBits(ar,
TBIT, 0);
903 PutBits(ar,
TBIT, root);
909 PutBits(ar,
TBIT, 0);
910 PutBits(ar,
TBIT, 0);
911 PutBits(ar,
CBIT, 0);
912 PutBits(ar,
CBIT, root);
916 WritePtLen(ar,
NP,
PBIT, -1);
919 PutBits(ar,
PBIT, 0);
920 PutBits(ar,
PBIT, root);
923 for(i=0; i<size; i++)
927 flags = ar->
Buf[pos];
934 k = ar->
Buf[pos] + (1<<8);
937 k = ar->
Buf[pos] << 8;
950 for(i=0; i<
NC; i++) ar->
CFreq[i] = 0;
951 for(i=0; i<
NP; i++) ar->
PFreq[i] = 0;
954static word Output(
ARDATA *ar, word c, word p, word outputpos)
965 ar->
CPos = outputpos;
969 ar->
Buf[outputpos] = c;
975 ar->
Buf[outputpos] = p>>8;
977 ar->
Buf[outputpos] = p;
992static void InitSlide(
ARDATA *ar)
1010static inline short Hash(
short p,
byte c)
1015static inline short Child(
ARDATA *ar,
short q,
byte c)
1018 r = ar->
Next[Hash(q, c)];
1024static inline void MakeChild(
ARDATA *ar,
short q,
byte c,
short r)
1037static void Split(
ARDATA *ar,
short old)
1056static void InsertNode(
ARDATA *ar)
1096 r = Child(ar, q, c);
1099 MakeChild(ar, q, c, ar->
Pos);
1134 r = Child(ar, q, *t1);
1137 MakeChild(ar, q, *t1, ar->
Pos);
1153static void DeleteNode(
ARDATA *ar)
1194 s = Child(ar, r, ar->
Text[t+ar->
Level[r]]);
1211static void GetNextMatch(
ARDATA *ar)
1227static int allocate_memory(
ARDATA *ar)
1242 return ar->
Buf != NULL;
1245#define FREEMEM(p) if(p) {TAP_MemFree(p); p = NULL;}
1247static void free_memory(
ARDATA *ar)
1260static void Encode(
ARDATA *ar)
1263 if(!allocate_memory(ar))
1269 short lastmatchlen, lastmatchpos;
1289 outputpos = Output(ar, ar->
Text[ar->
Pos-1], 0, outputpos);
1294 while(lastmatchlen > 0)
1313 if(!inputbuffer || inputsize > 0x7ffa)
1332 if(outputbuffer) memcpy(outputbuffer, inputbuffer, inputsize);
1333 compsize = inputsize;
void LogEntryFBLibPrintf(bool Console, char *format,...)
word UncompressBlock(byte *inputbuffer, word inputsize, byte *outputbuffer, word BufferSize)
word CompressBlock(byte *inputbuffer, word inputsize, byte *outputbuffer)