Article 3836 of alt.comp.compression:
The attached file implements LZW compression, is by Ross Williams and 
obtained from ftp.funet.fi in compression as part of the LDSxx.ZIP package.
If I only have an input buffer of upto 4K, what part of the code do I 
need to modify to optimise this code for running on an 8 Bit target 
processor? 

Thanks in Advance, Vin

-------

#include <stdio.h>
#include <stdlib.h>


#define TRUE 1
/* Body of inner unrolled matching loop.         */

#define PS *p++!=*s++

#define ITEMMAX 16     /* Maximum number of bytes in an expanded item.  */

#define UBYTE unsigned char /* Unsigned     byte (1 byte )        */
#define UWORD unsigned int  /* Unsigned     word (2 bytes)        */
#define ULONG unsigned long /* Unsigned longword (4 bytes)        */

#define FLAG_BYTES    4     /* Number of bytes used by copy flag. */
#define FLAG_COMPRESS 0     /* Signals that compression occurred. */
#define FLAG_COPY     1     /* Signals that a copyover occurred.  */

/* Fast copy routine.   */
void fast_copy( register UBYTE *p_src, register UBYTE *p_dst,
                                register ULONG len){

  while(len--) *p_dst++=*p_src++;
}


void lzrw1_compress(p_src_first,src_len,p_dst_first,p_dst_len)
/* Input  : Specify input block using p_src_first and src_len.          */
/* Input  : Point p_dst_first to the start of the output zone (OZ).     */
/* Input  : Point p_dst_len to a ULONG to receive the output length.    */
/* Input  : Input block and output zone must not overlap.               */
/* Output : Length of output block written to *p_dst_len.               */
/* Output : Output block in Mem[p_dst_first..p_dst_first+*p_dst_len-1]. */
/* Output : May write in OZ=Mem[p_dst_first..p_dst_first+src_len+256-1].*/
/* Output : Upon completion guaranteed *p_dst_len<=src_len+FLAG_BYTES.  */
UBYTE *p_src_first,*p_dst_first; ULONG src_len,*p_dst_len;
{
 UBYTE *p_src=p_src_first,*p_dst=p_dst_first;
 UBYTE *p_src_post=p_src_first+src_len,*p_dst_post=p_dst_first+src_len;
 UBYTE *p_src_max1=p_src_post-ITEMMAX,*p_src_max16=p_src_post-16*ITEMMAX;
 UBYTE *hash[4096],*p_control; UWORD control=0,control_bits=0;
 *p_dst=FLAG_COMPRESS; p_dst+=FLAG_BYTES; p_control=p_dst; p_dst+=2;
 while (TRUE)
   {UBYTE *p,*s; UWORD unroll=16,len,index; ULONG offset;
    if (p_dst>p_dst_post) goto overrun;
    if (p_src>p_src_max16)
      {unroll=1;
       if (p_src>p_src_max1)
         {if (p_src==p_src_post) break; goto literal;}}
    begin_unrolled_loop:
       index=((40543*((((p_src[0]<<4)^p_src[1])<<4)^p_src[2]))>>4) & 0xFFF;
       p=hash[index]; hash[index]=s=p_src; offset=s-p;
       if (offset>4095 || p<p_src_first || offset==0 || PS || PS || PS)
         {literal: *p_dst++=*p_src++; control>>=1; control_bits++;}
       else
         {PS || PS || PS || PS || PS || PS || PS ||
          PS || PS || PS || PS || PS || PS || s++; len=s-p_src-1;
          *p_dst++=((offset&0xF00)>>4)+(len-1); *p_dst++=offset&0xFF;
          p_src+=len; control=(control>>1)|0x8000; control_bits++;}
    end_unrolled_loop: if (--unroll) goto begin_unrolled_loop;
    if (control_bits==16)
      {*p_control=control&0xFF; *(p_control+1)=control>>8;
       p_control=p_dst; p_dst+=2; control=control_bits=0;}
   }
 control>>=16-control_bits;
 *p_control++=control&0xFF; *p_control++=control>>8;
 if (p_control==p_dst) p_dst-=2;
 *p_dst_len=p_dst-p_dst_first;
 return;
 overrun: fast_copy(p_src_first,p_dst_first+FLAG_BYTES,src_len);
          *p_dst_first=FLAG_COPY; *p_dst_len=src_len+FLAG_BYTES;
}





void main(int  argc,char *argv[])
{
   ULONG in_file_size,out_file_size;
   unsigned char *inbuff;    // Massive input buffer!!!!

   unsigned char *outbuff;   // Massive input buffer!!!!

   FILE *infile,*outfile;

   printf ("\nLZRW1:  Lempel Ziff Compression Program.");

   if (argc == 3){

      if ((infile = fopen (argv[1], "rb")) != NULL){

         fseek (infile, 0L, 2);
         in_file_size = (ULONG) ftell (infile);
         rewind( infile );

         out_file_size = in_file_size;

         inbuff = (UBYTE *) calloc( (ULONG) in_file_size,sizeof(UBYTE) );

         if( inbuff == NULL ){
            printf("\nError, cant allocate %ld %s bytes\n",
                                 in_file_size,"output");
            exit(1);
         }

         outbuff = (UBYTE *) calloc( out_file_size,sizeof(UBYTE));
         if( outbuff==NULL ){
            printf("\nError, cant allocate %ld %s bytes\n",
                                 out_file_size,"output");
            exit(1);
         }


         // Copy file to buffer
         if( fread( inbuff, sizeof( UBYTE ), (ULONG)in_file_size,infile ) <
                                 in_file_size){
             printf("\nProblem with %s file\n","input");
             free(inbuff);
             fclose(infile);
             exit(1);
         }

         fclose( infile);


         // Compress array
         lzrw1_compress( inbuff, in_file_size,outbuff, &out_file_size );

         free(inbuff);


         if ((outfile = fopen (argv[2], "wb")) != NULL){

            if( fwrite(outbuff,sizeof(UBYTE),
                         out_file_size, outfile)<out_file_size){

               printf("\nProblem with %s file\n","output");

            }
            free(outbuff);
            fclose (outfile);


         }
         else
            printf("\nERROR: Couldn't create output file %s\n", argv[2]);


      }
      else
         printf ("\nERROR:  %s -- File not found!\n", argv[1]);

   }
   else
      printf ("Usage:  LZCOMP <input filename> <output filename>\n\n");



}




