

/**-----------------------------------------------------------------------
  *   Bloque de includes, por fin me ocupan menos de una pagina de
  * impresisn, lo que hay que hacer para tener todos los prototipos..
  *
  **/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>

#define NO_SUB_PRAGMAS
#include <libraries/xpksub.h>

#include "ilzr.h"

/**-----------------------------------------------------------------------
  *   Prototipos para todas estas paridas necesarias para compilar.
  *
  **/

long __saveds __asm XpksUnpackChunk( REG __a0 XPARAMS* xpar )
{
    /*  variables para input - output   */
  
register  CHARS   *inpb;
register  CHARS   *outb;
          CHARS   *bumpcode;
          CHARS   *outbstart;
    signed long    outbsize;
          ULONG    temp;
    signed char    shift;
          UBYTE    bitcount;
          ULONG    matchpos;
          ULONG    matchlen;      
          

  inpb     = xpar->InBuf;
  outb     = xpar->OutBuf;

  shift    = 0;
  
  outbstart= outb;
  outbsize = *((unsigned short *)inpb);
  inpb    += 2;     /* short leido */

  bitcount = INIT_BIT_BUMP;
  bumpcode = &outb[ 1<<INIT_BIT_BUMP ]; 
  
  if( outbsize > xpar->OutBufLen )
    return( XPKERR_SMALLBUF );
  
  xpar->OutBufLen = outbsize; 
    
  while( outbsize > 0 )
    {
    if( *inpb & (0x80>>shift) )
      {
      shift++;
      if( shift > 7 )
        {
        shift=0;
        inpb++;
        }
          
        temp = ( ((ULONG *)inpb)[0]);
        temp = ( temp>>(24-shift) );
        *outb= (CHARS)( temp & 0xFF);
        outb++;
        inpb++;
        outbsize--;
        }
      else
        {
        shift++;
        if( shift > 7 )
          {
          shift=0;
        inpb++;
        }
      
      if( bumpcode <  outb )
        {
        bitcount++;
        bumpcode = &outbstart[ 1<<bitcount ];
        }

      temp     = ( ((ULONG *)inpb)[0]);
      temp     = temp >> ( 32 - bitcount - shift );
      matchpos = temp & ((1<<bitcount)-1);
      
      shift   += bitcount;    /* ALWAYS bitccount >= 8 */
      shift   -=8;
      inpb++;
      if( shift > 7 )
        {
        shift-=8;
        inpb++;
        }
      
      temp     = ( ((ULONG *)inpb)[0]);
      temp     = temp >> ( 32 - BITS_LOOKAHEAD - shift );
      matchlen = temp & ((1<<BITS_LOOKAHEAD)-1);
      
      shift   += BITS_LOOKAHEAD;
      if( shift > 7 )
        {
        shift-=8;
        inpb++;
        }
        
      matchlen += MIN_MATCH;
      matchpos--;
      for( temp = 0 ; temp < matchlen ; temp++ )
        {
        *outb=outbstart[ matchpos + temp ];
        outb++;
        }
      outbsize -= matchlen;
      }
    }
  return( 0 );
}

/************************** End of ILZR.C *************************/
