32static unsigned int matchlen(
unsigned char *s,
unsigned char *d,
unsigned int maxmatch)
36 for (current=0; current<maxmatch && *s++==*d++; ++current)
42#define HASH(cptr) (int)((((unsigned int)(unsigned char)cptr[0]<<8) | ((unsigned int)(unsigned char)cptr[2])) ^ ((unsigned int)(unsigned char)cptr[1]<<4))
44static int refcompress(
unsigned char *from,
int len,
unsigned char *dest,
int maxback,
int quick)
74 if ((
unsigned int)maxback > (
unsigned int)131071)
77 hashtbl = (
int *)
galloc(65536L*
sizeof(
int));
80 link = (
int *)
galloc(131072L*
sizeof(
int));
84 memset(hashtbl,-1,65536L*
sizeof(
int));
93 mlen =
qmin(len,1028);
96 hoffset = hashtbl[hash];
97 minhoffset =
qmax(cptr-from-131071,0);
100 if (hoffset>=minhoffset)
105 if (cptr[blen]==tptr[blen])
107 tlen = matchlen(cptr,tptr,mlen);
110 toffset = (cptr-1)-tptr;
111 if (toffset<1024 && tlen<=10)
113 else if (toffset<16384 && tlen<=67)
118 if (tlen-tcost+4 > blen-bcost+4)
123 if (blen>=1028)
break;
127 }
while ((hoffset = link[hoffset&131071]) >= minhoffset);
135 if (bcost>=blen || len<4)
137 hoffset = (cptr-from);
138 link[hoffset&131071] = hashtbl[hash];
139 hashtbl[hash] = hoffset;
149 tlen =
qmin(112,run&~3);
151 *to++ = (
unsigned char) (0xe0+(tlen>>2)-1);
152 memcpy(to,rptr,tlen);
159 *to++ = (
unsigned char) (((boffset>>8)<<5) + ((blen-3)<<2) + run);
160 *to++ = (
unsigned char) boffset;
165 *to++ = (
unsigned char) (0x80 + (blen-4));
166 *to++ = (
unsigned char) ((run<<6) + (boffset>>8));
167 *to++ = (
unsigned char) boffset;
172 *to++ = (
unsigned char) (0xc0 + ((boffset>>16)<<4) + (((blen-5)>>8)<<2) + run);
173 *to++ = (
unsigned char) (boffset>>8);
174 *to++ = (
unsigned char) (boffset);
175 *to++ = (
unsigned char) (blen-5);
180 memcpy(to, rptr, run);
187 hoffset = (cptr-from);
188 link[hoffset&131071] = hashtbl[hash];
189 hashtbl[hash] = hoffset;
194 for (i=0; i < (int)blen; ++i)
197 hoffset = (cptr-from);
198 link[hoffset&131071] = hashtbl[hash];
199 hashtbl[hash] = hoffset;
212 tlen =
qmin(112,run&~3);
214 *to++ = (
unsigned char) (0xe0+(tlen>>2)-1);
215 memcpy(to,rptr,tlen);
220 *to++ = (
unsigned char) (0xfc+run);
237int GCALL REF_encode(
void *compresseddata,
const void *source,
int sourcesize,
int *opts)
247 if (sourcesize>0xffffff)
249 gputm(compresseddata, (
unsigned int) 0x90fb, 2);
250 gputm((
char *)compresseddata+2, (
unsigned int) sourcesize, 4);
255 gputm(compresseddata, (
unsigned int) 0x10fb, 2);
256 gputm((
char *)compresseddata+2, (
unsigned int) sourcesize, 3);
259 plen = hlen+refcompress((
unsigned char *)source, sourcesize, (
unsigned char *)compresseddata+hlen, maxback, quick);
int GCALL REF_encode(void *compresseddata, const void *source, int sourcesize, int *opts)