2006-04-13 20:47:06 +00:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
#if defined(__POWERPC__)
|
|
|
|
static unsigned long LSWAPIB (unsigned long a) { return(((a>>8)&0xff00)+((a&0xff00)<<8)+(a<<24)+(a>>24)); }
|
|
|
|
static unsigned short SSWAPIB (unsigned short a) { return((a>>8)+(a<<8)); }
|
|
|
|
#else
|
|
|
|
#define LSWAPIB(a) (a)
|
|
|
|
#define SSWAPIB(a) (a)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define USENEW 1
|
|
|
|
long lzwcompress (unsigned char *ucompbuf, long ucompleng, unsigned char *compbuf)
|
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
long i, j, numnodes, *lptr, bitcnt, nbits, oneupnbits, hmask, *child;
|
|
|
|
long *sibly;
|
2006-04-13 20:47:06 +00:00
|
|
|
#if USENEW
|
2006-04-24 19:04:22 +00:00
|
|
|
long *sibry;
|
2006-04-13 20:47:06 +00:00
|
|
|
#endif
|
2006-04-24 19:04:22 +00:00
|
|
|
unsigned char *nodev, *cptr, *eptr;
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
nodev = (unsigned char *)malloc((ucompleng+256)*sizeof(char)); if (!nodev) return(0);
|
|
|
|
child = (long *)malloc((ucompleng+256)*sizeof(long)); if (!child) { free(nodev); return(0); }
|
|
|
|
sibly = (long *)malloc((ucompleng+256)*sizeof(long)); if (!sibly) { free(child); free(nodev); return(0); }
|
2006-04-13 20:47:06 +00:00
|
|
|
#if USENEW
|
2006-04-24 19:04:22 +00:00
|
|
|
sibry = (long *)malloc((ucompleng+256)*sizeof(long)); if (!sibry) { free(sibly); free(child); free(nodev); return(0); }
|
2006-04-13 20:47:06 +00:00
|
|
|
#endif
|
|
|
|
|
2006-11-13 23:12:47 +00:00
|
|
|
for (i=255;i>=0;i--) { nodev[i] = i; child[i] = -1; }
|
2006-04-24 19:04:22 +00:00
|
|
|
memset(compbuf,0,ucompleng+15);
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
cptr = ucompbuf; eptr = &ucompbuf[ucompleng];
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
numnodes = 256; bitcnt = (4<<3); nbits = 8; oneupnbits = (1<<8); hmask = ((oneupnbits>>1)-1);
|
|
|
|
do
|
|
|
|
{
|
2006-11-13 23:12:47 +00:00
|
|
|
for (i=cptr[0];i>=0;i=j)
|
2006-04-24 19:04:22 +00:00
|
|
|
{
|
|
|
|
cptr++; if (cptr >= eptr) goto lzwcompbreak2b;
|
|
|
|
j = child[i]; if (j < 0) { child[i] = numnodes; break; }
|
2006-04-13 20:47:06 +00:00
|
|
|
#if USENEW
|
2006-04-24 19:04:22 +00:00
|
|
|
//This is about 2x faster when ucompbuf is more random, 5% slower when very compressible
|
|
|
|
while (cptr[0] != nodev[j])
|
|
|
|
{
|
|
|
|
if (cptr[0] < nodev[j])
|
|
|
|
{ if (sibly[j] < 0) { sibly[j] = numnodes; goto lzwcompbreak2a; } j = sibly[j]; }
|
|
|
|
else { if (sibry[j] < 0) { sibry[j] = numnodes; goto lzwcompbreak2a; } j = sibry[j]; }
|
|
|
|
}
|
2006-04-13 20:47:06 +00:00
|
|
|
#else
|
2006-11-13 23:12:47 +00:00
|
|
|
for (;nodev[j]!=cptr[0];j=sibly[j])
|
2006-04-24 19:04:22 +00:00
|
|
|
if (sibly[j] < 0) { sibly[j] = numnodes; goto lzwcompbreak2a; }
|
2006-04-13 20:47:06 +00:00
|
|
|
#endif
|
2006-04-24 19:04:22 +00:00
|
|
|
}
|
2006-11-13 23:12:47 +00:00
|
|
|
lzwcompbreak2a:
|
|
|
|
nodev[numnodes] = cptr[0];
|
|
|
|
lzwcompbreak2b:
|
|
|
|
child[numnodes] = sibly[numnodes] = -1;
|
2006-04-13 20:47:06 +00:00
|
|
|
#if USENEW
|
2006-04-24 19:04:22 +00:00
|
|
|
sibry[numnodes] = -1;
|
2006-04-13 20:47:06 +00:00
|
|
|
#endif
|
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
lptr = (long *)&compbuf[bitcnt>>3]; lptr[0] |= LSWAPIB(i<<(bitcnt&7));
|
|
|
|
bitcnt += nbits; if ((i&hmask) > ((numnodes-1)&hmask)) bitcnt--;
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
numnodes++; if (numnodes > oneupnbits) { nbits++; oneupnbits <<= 1; hmask = ((oneupnbits>>1)-1); }
|
|
|
|
} while ((cptr < eptr) && (bitcnt < (ucompleng<<3)));
|
2006-04-13 20:47:06 +00:00
|
|
|
|
|
|
|
#if USENEW
|
2006-04-24 19:04:22 +00:00
|
|
|
free(sibry);
|
2006-04-13 20:47:06 +00:00
|
|
|
#endif
|
2006-04-24 19:04:22 +00:00
|
|
|
free(sibly);
|
|
|
|
free(child); free(nodev);
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
lptr = (long *)compbuf;
|
|
|
|
if (((bitcnt+7)>>3) < ucompleng) { lptr[0] = LSWAPIB(numnodes); return((bitcnt+7)>>3); }
|
|
|
|
memcpy(compbuf,ucompbuf,ucompleng); return(ucompleng);
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
long lzwuncompress (unsigned char *compbuf, long compleng, unsigned char *ucompbuf, long ucompleng)
|
|
|
|
{
|
2006-04-24 19:04:22 +00:00
|
|
|
long i, dat, leng, bitcnt, *lptr, numnodes, totnodes, nbits, oneupnbits, hmask, *prefix;
|
|
|
|
unsigned char ch, *ucptr, *suffix;
|
|
|
|
long ucomp = (long)ucompbuf;
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
if (compleng >= ucompleng) { memcpy(ucompbuf,compbuf,ucompleng); return ucompleng; }
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
totnodes = LSWAPIB(((long *)compbuf)[0]); if (totnodes <= 0 || totnodes >= ucompleng+256) return 0;
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
prefix = (long *)malloc(totnodes*sizeof(long)); if (!prefix) return 0;
|
|
|
|
suffix = (unsigned char *)malloc(totnodes*sizeof(char)); if (!suffix) { free(prefix); return 0; }
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
numnodes = 256; bitcnt = (4<<3); nbits = 8; oneupnbits = (1<<8); hmask = ((oneupnbits>>1)-1);
|
|
|
|
do
|
|
|
|
{
|
|
|
|
lptr = (long *)&compbuf[bitcnt>>3]; dat = ((LSWAPIB(lptr[0])>>(bitcnt&7))&(oneupnbits-1));
|
|
|
|
bitcnt += nbits; if ((dat&hmask) > ((numnodes-1)&hmask)) { dat &= hmask; bitcnt--; }
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
prefix[numnodes] = dat;
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
ucompbuf++;
|
2006-11-13 23:12:47 +00:00
|
|
|
for (leng=0;dat>=256;dat=prefix[dat]) {
|
2006-04-24 19:04:22 +00:00
|
|
|
if ((long)ucompbuf+leng-ucomp > ucompleng) goto bail;
|
|
|
|
ucompbuf[leng++] = suffix[dat];
|
|
|
|
}
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
ucptr = &ucompbuf[leng-1];
|
2006-11-13 23:12:47 +00:00
|
|
|
for (i=(leng>>1)-1;i>=0;i--) { ch = ucompbuf[i]; ucompbuf[i] = ucptr[-i]; ucptr[-i] = ch; }
|
2006-04-24 19:04:22 +00:00
|
|
|
ucompbuf[-1] = dat; ucompbuf += leng;
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2006-04-24 19:04:22 +00:00
|
|
|
suffix[numnodes-1] = suffix[numnodes] = dat;
|
|
|
|
|
|
|
|
numnodes++; if (numnodes > oneupnbits) { nbits++; oneupnbits <<= 1; hmask = ((oneupnbits>>1)-1); }
|
|
|
|
} while (numnodes < totnodes);
|
2006-04-13 20:47:06 +00:00
|
|
|
|
|
|
|
bail:
|
2006-04-24 19:04:22 +00:00
|
|
|
free(suffix); free(prefix);
|
|
|
|
|
|
|
|
return (long)ucompbuf-ucomp;
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|