/*
 * This file was generated automatically by ExtUtils::ParseXS version 3.51 from the
 * contents of Lha.xs. Do not edit this file, edit Lha.xs instead.
 *
 *    ANY CHANGES MADE HERE WILL BE LOST!
 *
 */

#line 1 "lib/Archive/Lha.xs"
/*
  XS for Archive::Lha

  This XS is, though largely modified, based on LHa for UNIX.
  See lib/Archive/Lha.pm for Authors/Copyright/License information.
*/
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
#include "Lha.h"

/*
  these are not from LHa for UNIX
*/

void
destroy_stash(LhaStash * stash)
{
  Safefree(stash->tree->left);
  Safefree(stash->tree->right);
  Safefree(stash->tree);
  Safefree(stash->pt->table);
  Safefree(stash->pt->length);
  Safefree(stash->pt);
  Safefree(stash->c->table);
  Safefree(stash->c->length);
  Safefree(stash->c);
  Safefree(stash->bit);
  Safefree(stash->queue);
  Safefree(stash);
}

void
safe_croak(LhaStash * stash, char * dying_message)
{
  destroy_stash(stash);
  croak("%s", dying_message);
}

void
output(LhaStash * stash, unsigned char * queue, int len)
{
  dSP;
  ENTER;
  SAVETMPS;
  PUSHMARK(SP);
  XPUSHs(sv_2mortal(newSVpv(queue, len)));
  PUTBACK;
  call_sv(stash->write, G_VOID);
  SPAGAIN;
  PUTBACK;
  FREETMPS;
  LEAVE;
}

void
input(LhaStash * stash, int len)
{
  int n;
  dSP;
  ENTER;
  SAVETMPS;
  PUSHMARK(SP);
  XPUSHs(sv_2mortal(newSViv(len)));
  PUTBACK;
  n = call_sv(stash->read, G_SCALAR);
  SPAGAIN;
  if (n != 1)
    safe_croak(stash, "There's something wrong in 'read' callback");
  Copy(POPp, stash->bit->readbuf, len, unsigned char);
  PUTBACK;
  FREETMPS;
  LEAVE;
}

/*
  modified from LHa for UNIX: bitio.c ver 1.14
    original authors:
      Source All chagned       1995.01.14  N.Watazaki
      Separated from crcio.c   2002.10.26  Koji Arai
*/

unsigned short
shiftbits(LhaBitstream * bit, unsigned char n)
{
  return (bit->value << n)
       + (bit->buf >> (char_bit - n));
}

void
fillbuf(LhaStash * stash, unsigned char n)
{
  unsigned short len;
  LhaBitstream * bit = stash->bit;
  while (n > bit->pos) {
    n -= bit->pos;
    bit->value = shiftbits(bit, bit->pos);
    if (stash->encoded_size > 0) {
      if (bit->readpos == 0) {
        if (stash->encoded_size > readbuf_size)
          len = readbuf_size;
        else
          len = stash->encoded_size;
        input(stash, len);
      }
      bit->buf = bit->readbuf[bit->readpos++];
      if (bit->readpos == readbuf_size)
        bit->readpos = 0;
      stash->encoded_size--;
    }
    else
      bit->buf = 0;
    bit->pos = char_bit;
  }
  bit->pos -= n;
  bit->value = shiftbits(bit, n);
  bit->buf <<= n;
}

unsigned short
peekbits(LhaStash * stash, unsigned char n)
{
  return (stash->bit->value >> (ushort_bit - n));
}

unsigned short
getbits(LhaStash * stash, unsigned char n)
{
  unsigned short bits = peekbits(stash, n);
  fillbuf(stash, n);
  return bits;
}

void
init_bitstream(LhaStash * stash)
{
  LhaBitstream  * bitstream;

  Newxz(bitstream, 1, LhaBitstream);
  stash->bit = bitstream;

  stash->bit->blocksize = 0;
  stash->bit->readpos = 0;
  stash->bit->value = 0;
  stash->bit->buf = 0;
  stash->bit->pos = 0;
  fillbuf(stash, ushort_bit);
}

/*
  modified from LHa for UNIX: maketbl.c ver 1.14
    original author(s):
      Source All chagned       1995.01.14  N.Watazaki
*/

void
make_table(LhaStash * stash, LhaTable * table, unsigned short nchar)
{
  unsigned short count[ushort_bit + 1];
  unsigned short weight[ushort_bit + 1];
  unsigned short start[ushort_bit + 1];
  unsigned short total, avail;
  unsigned short from, to;
  unsigned char  bits_to_shift, bit;
  unsigned int   i, j, l;
  char           n;
  unsigned short *p;

  if (table->bit > ushort_bit) {
    safe_croak(stash, "Table is broken: table bit is too large");
  }

  for(i = 1; i <= ushort_bit; i++) {
    count[i]  = 0;
    weight[i] = 1 << (ushort_bit - i);
  }

  for(i = 0; i < nchar; i++)
    if (table->length[i] > ushort_bit) {
      safe_croak(stash, "Table is broken: bit length is too large");
    }
    else
      count[table->length[i]]++;

  total = 0;
  for(i = 1; i <= ushort_bit; i++) {
    start[i] = total;
    total += weight[i] * count[i];
  }
  if (total & ushort_max) {
    safe_croak(stash, "Table is broken: total mismatch");
  }

  bits_to_shift = ushort_bit - table->bit;
  for(i = 1; i <= table->bit; i++) {
    start[i]  >>= bits_to_shift;
    weight[i] >>= bits_to_shift;
  }

  from = start[table->bit + 1] >> bits_to_shift;
  to   = min(1 << table->bit, table->size);
  if (from)
    for(i = from; i < to; i++)
      table->table[i] = 0;

  avail = nchar;
  for(i = 0; i < nchar; i++) {
    bit = table->length[i];
    if (bit == 0)
      continue;
    l = start[bit] + weight[bit];
    if (bit <= table->bit) {
      l = min(l, table->size);
      for(j = start[bit]; j < l; j++)
        table->table[j] = i;
    }
    else {
      j = start[bit];
      if ((j >> bits_to_shift) > table->size) {
        safe_croak(stash, "Table is broken");
      }
      p = &(table->table[j >> bits_to_shift]);
      j <<= table->bit;
      n = bit - table->bit;
      while (--n >= 0) {
        if (*p == 0) {
          stash->tree->right[avail] = stash->tree->left[avail] = 0;
          *p = avail++;
        }
        if (j & ushort_center)
          p = &(stash->tree->right[*p]);
        else
          p = &(stash->tree->left[*p]);
        j <<= 1;
      }
      *p = i;
    }
    start[bit] = l;
  }
}

/*
  modified from LHa for UNIX: huf.c ver 1.14
    original authors:
      Source All chagned       1995.01.14  N.Watazaki
      Support LH7 & Bug Fixed  2000.10. 6  t.okamoto
*/

void
read_pt_len(LhaStash * stash, short nn, unsigned char nbit, short threshold)
{
  int i, c;
  unsigned short n, mask;

  n = getbits(stash, nbit);
  if (n == 0) {
    c = getbits(stash, nbit);
    for(i = 0; i < nn; i++)
      stash->pt->length[i] = 0;
    for(i = 0; i < stash->pt->size; i++)
      stash->pt->table[i] = c;
  }
  else {
    i = 0;
    while (i < min(n, stash->pt->length_size)) {
      c = peekbits(stash, 3);
      if (c != 7)
        fillbuf(stash, 3);
      else {
        mask = create_mask(3);
        while (stash->bit->value & mask) {
          mask >>= 1;
          c++;
        }
        fillbuf(stash, (unsigned char)(c - 3));
      }
      stash->pt->length[i++] = c;
      if (i == threshold) {
        c = getbits(stash, 2);
        while (--c >= 0 && i < stash->pt->length_size)
          stash->pt->length[i++] = 0;
      }
    }
    while (i < nn)
      stash->pt->length[i++] = 0;
    make_table(stash, stash->pt, nn);
  }
}

void
read_c_len(LhaStash * stash)
{
  short i, c, n;
  unsigned short mask;

  n = getbits(stash, stash->CBIT);
  if (n == 0) {
    c = getbits(stash, stash->CBIT);
    for(i = 0; i < stash->c->length_size; i++)
      stash->c->length[i] = 0;
    for(i = 0; i < stash->c->size; i++)
      stash->c->table[i] = c;
  }
  else {
    i = 0;
    while (i < min(n, stash->c->length_size)) {
      c = stash->pt->table[peekbits(stash, stash->pt->bit)];
      if (c >= stash->NT) {
        mask = create_mask(stash->pt->bit);
        do {
          if (stash->bit->value & mask)
            c = stash->tree->right[c];
          else
            c = stash->tree->left[c];
          mask >>= 1;
        } while (c >= stash->NT && (mask || c != stash->tree->left[c]));
      }
      fillbuf(stash, stash->pt->length[c]);
      if (c <= 2) {
        if (c == 0)
          c = 1;
        else if (c == 1)
          c = getbits(stash, 4) + 3;
        else
          c = getbits(stash, stash->CBIT) + 20;
        while (--c >= 0)
          stash->c->length[i++] = 0;
      }
      else
        stash->c->length[i++] = c - 2;
    }
    while (i < stash->c->length_size)
      stash->c->length[i++] = 0;
    make_table(stash, stash->c, stash->c->length_size);
  }
}

unsigned short
decode_c(LhaStash * stash)
{
  unsigned short j, mask;

  if (stash->bit->blocksize == 0) {
    stash->bit->blocksize = getbits(stash, ushort_bit);
    read_pt_len(stash, stash->NT, stash->TBIT, 3);
    read_c_len(stash);
    read_pt_len(stash, stash->NP, stash->PBIT, -1);
  }
  stash->bit->blocksize--;
  j = stash->c->table[peekbits(stash, stash->c->bit)];
  if (j < stash->c->length_size)
    fillbuf(stash, stash->c->length[j]);
  else {
    fillbuf(stash, stash->c->bit);
    mask = create_mask(0);
    do {
      if (stash->bit->value & mask)
        j = stash->tree->right[j];
      else
        j = stash->tree->left[j];
      mask >>= 1;
    } while (j >= stash->c->length_size && (mask || j != stash->tree->left[j]));
    fillbuf(stash, (unsigned char)(stash->c->length[j] - stash->c->bit));
  }
  return j;
}

unsigned short
decode_p(LhaStash * stash)
{
  unsigned short j, mask;

  j = stash->pt->table[peekbits(stash, stash->pt->bit)];
  if (j < stash->NP)
    fillbuf(stash, stash->pt->length[j]);
  else {
    fillbuf(stash, stash->pt->bit);
    mask = create_mask(0);
    do {
      if (stash->bit->value & mask)
        j = stash->tree->right[j];
      else
        j = stash->tree->left[j];
      mask >>= 1;
    } while (j >= stash->NP && (mask || j != stash->tree->left[j]));
    fillbuf(stash, (unsigned char)(stash->pt->length[j] - stash->pt->bit));
  }
  if (j != 0)
    j = (1 << (j - 1)) + getbits(stash, (unsigned char)(j - 1));
  return j;
}

/*
  modified from LHa for UNIX: crcio.c ver 1.14
    original author(s):
      Source All chagned       1995.01.14  N.Watazaki
*/

unsigned short
calc_crc16(unsigned short crc, unsigned char * str, unsigned int len)
{
  while (len-- > 0)
    crc = crctable[(crc ^ *str++) & uchar_max] ^ (crc >> char_bit);
  return crc;
}

/*
  this is not from LHa for UNIX
*/

void
init_tables(HV * self, LhaStash * stash)
{
  LhaTable * pt_table;
  LhaTable * c_table;
  LhaTree  * tree;

  stash->NPT  = self_ushort("NPT");
  stash->NP   = self_ushort("NP");
  stash->NT   = self_ushort("NT");
  stash->NC   = self_ushort("NC");
  stash->PBIT = self_uchar("PBIT");
  stash->TBIT = self_uchar("TBIT");
  stash->CBIT = self_uchar("CBIT");

  Newxz(pt_table, 1, LhaTable);
  Newxz(c_table,  1, LhaTable);
  Newxz(tree,     1, LhaTree);

  pt_table->bit         = self_uchar("PT_TABLE_BIT");
  pt_table->size        = self_ushort("PT_TABLE_SIZE");
  pt_table->length_size = stash->NPT;

  Newxz(pt_table->table,  pt_table->size,        unsigned short);
  Newxz(pt_table->length, pt_table->length_size, unsigned char);

  c_table->bit         = self_uchar("C_TABLE_BIT");
  c_table->size        = self_ushort("C_TABLE_SIZE");
  c_table->length_size = stash->NC;

  Newxz(c_table->table,  c_table->size,        unsigned short);
  Newxz(c_table->length, c_table->length_size, unsigned char);

  Newxz(tree->left,  2 * stash->NC - 1, unsigned short);
  Newxz(tree->right, 2 * stash->NC - 1, unsigned short);

  stash->tree = tree;
  stash->pt   = pt_table;
  stash->c    = c_table;
}

#line 463 "lib/Archive/Lha.c"
#ifndef PERL_UNUSED_VAR
#  define PERL_UNUSED_VAR(var) if (0) var = var
#endif

#ifndef dVAR
#  define dVAR		dNOOP
#endif


/* This stuff is not part of the API! You have been warned. */
#ifndef PERL_VERSION_DECIMAL
#  define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
#endif
#ifndef PERL_DECIMAL_VERSION
#  define PERL_DECIMAL_VERSION \
	  PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
#endif
#ifndef PERL_VERSION_GE
#  define PERL_VERSION_GE(r,v,s) \
	  (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
#endif
#ifndef PERL_VERSION_LE
#  define PERL_VERSION_LE(r,v,s) \
	  (PERL_DECIMAL_VERSION <= PERL_VERSION_DECIMAL(r,v,s))
#endif

/* XS_INTERNAL is the explicit static-linkage variant of the default
 * XS macro.
 *
 * XS_EXTERNAL is the same as XS_INTERNAL except it does not include
 * "STATIC", ie. it exports XSUB symbols. You probably don't want that
 * for anything but the BOOT XSUB.
 *
 * See XSUB.h in core!
 */


/* TODO: This might be compatible further back than 5.10.0. */
#if PERL_VERSION_GE(5, 10, 0) && PERL_VERSION_LE(5, 15, 1)
#  undef XS_EXTERNAL
#  undef XS_INTERNAL
#  if defined(__CYGWIN__) && defined(USE_DYNAMIC_LOADING)
#    define XS_EXTERNAL(name) __declspec(dllexport) XSPROTO(name)
#    define XS_INTERNAL(name) STATIC XSPROTO(name)
#  endif
#  if defined(__SYMBIAN32__)
#    define XS_EXTERNAL(name) EXPORT_C XSPROTO(name)
#    define XS_INTERNAL(name) EXPORT_C STATIC XSPROTO(name)
#  endif
#  ifndef XS_EXTERNAL
#    if defined(HASATTRIBUTE_UNUSED) && !defined(__cplusplus)
#      define XS_EXTERNAL(name) void name(pTHX_ CV* cv __attribute__unused__)
#      define XS_INTERNAL(name) STATIC void name(pTHX_ CV* cv __attribute__unused__)
#    else
#      ifdef __cplusplus
#        define XS_EXTERNAL(name) extern "C" XSPROTO(name)
#        define XS_INTERNAL(name) static XSPROTO(name)
#      else
#        define XS_EXTERNAL(name) XSPROTO(name)
#        define XS_INTERNAL(name) STATIC XSPROTO(name)
#      endif
#    endif
#  endif
#endif

/* perl >= 5.10.0 && perl <= 5.15.1 */


/* The XS_EXTERNAL macro is used for functions that must not be static
 * like the boot XSUB of a module. If perl didn't have an XS_EXTERNAL
 * macro defined, the best we can do is assume XS is the same.
 * Dito for XS_INTERNAL.
 */
#ifndef XS_EXTERNAL
#  define XS_EXTERNAL(name) XS(name)
#endif
#ifndef XS_INTERNAL
#  define XS_INTERNAL(name) XS(name)
#endif

/* Now, finally, after all this mess, we want an ExtUtils::ParseXS
 * internal macro that we're free to redefine for varying linkage due
 * to the EXPORT_XSUB_SYMBOLS XS keyword. This is internal, use
 * XS_EXTERNAL(name) or XS_INTERNAL(name) in your code if you need to!
 */

#undef XS_EUPXS
#if defined(PERL_EUPXS_ALWAYS_EXPORT)
#  define XS_EUPXS(name) XS_EXTERNAL(name)
#else
   /* default to internal */
#  define XS_EUPXS(name) XS_INTERNAL(name)
#endif

#ifndef PERL_ARGS_ASSERT_CROAK_XS_USAGE
#define PERL_ARGS_ASSERT_CROAK_XS_USAGE assert(cv); assert(params)

/* prototype to pass -Wmissing-prototypes */
STATIC void
S_croak_xs_usage(const CV *const cv, const char *const params);

STATIC void
S_croak_xs_usage(const CV *const cv, const char *const params)
{
    const GV *const gv = CvGV(cv);

    PERL_ARGS_ASSERT_CROAK_XS_USAGE;

    if (gv) {
        const char *const gvname = GvNAME(gv);
        const HV *const stash = GvSTASH(gv);
        const char *const hvname = stash ? HvNAME(stash) : NULL;

        if (hvname)
	    Perl_croak_nocontext("Usage: %s::%s(%s)", hvname, gvname, params);
        else
	    Perl_croak_nocontext("Usage: %s(%s)", gvname, params);
    } else {
        /* Pants. I don't think that it should be possible to get here. */
	Perl_croak_nocontext("Usage: CODE(0x%" UVxf ")(%s)", PTR2UV(cv), params);
    }
}
#undef  PERL_ARGS_ASSERT_CROAK_XS_USAGE

#define croak_xs_usage        S_croak_xs_usage

#endif

/* NOTE: the prototype of newXSproto() is different in versions of perls,
 * so we define a portable version of newXSproto()
 */
#ifdef newXS_flags
#define newXSproto_portable(name, c_impl, file, proto) newXS_flags(name, c_impl, file, proto, 0)
#else
#define newXSproto_portable(name, c_impl, file, proto) (PL_Sv=(SV*)newXS(name, c_impl, file), sv_setpv(PL_Sv, proto), (CV*)PL_Sv)
#endif /* !defined(newXS_flags) */

#if PERL_VERSION_LE(5, 21, 5)
#  define newXS_deffile(a,b) Perl_newXS(aTHX_ a,b,file)
#else
#  define newXS_deffile(a,b) Perl_newXS_deffile(aTHX_ a,b)
#endif

#line 607 "lib/Archive/Lha.c"

XS_EUPXS(XS_Archive__Lha__Decode__Base_decode); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Archive__Lha__Decode__Base_decode)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "hashref");
    {
	SV *	hashref = ST(0)
;
	unsigned short	RETVAL;
	dXSTARG;
#line 468 "lib/Archive/Lha.xs"
    HV * self;
    unsigned char * queue;
    LhaStash      * stash;
    unsigned short  crc16;
    int i, c, matchlen, matchoff, matchpos, adjust;
    unsigned int dicsize, dicsize1, total, loc;

    dSP;
    self = (HV *) SvRV(hashref);

    if ( !hash_exists(self, "read") )
      croak("'read' callback is missing");
    if ( !hash_exists(self, "write") )
      croak("'write' callback is missing");

    dicsize  = self_uint("DICSIZE");
    dicsize1 = dicsize - 1;

    Newxz(queue, dicsize, unsigned char);
    Newxz(stash, 1, LhaStash);

    stash->queue = queue;

    stash->read          = self_sv("read");
    stash->write         = self_sv("write");
    stash->original_size = self_uint("original_size");
    stash->encoded_size  = self_uint("encoded_size");

    init_tables(self, stash);
    init_bitstream(stash);

    adjust = (1 << uchar_bit) - self_uchar("THRESHOLD");
    crc16  = 0;
    loc    = 0;
    total  = 0;
    while ( total < stash->original_size ) {
      c = decode_c(stash);
      if (c <= uchar_max) {
        queue[loc++] = c;
        if (loc == dicsize) {
          output(stash, queue, dicsize);
          crc16 = calc_crc16(crc16, queue, dicsize);
          loc = 0;
        }
        total++;
      }
      else {
        matchlen = c - adjust;
        matchoff = decode_p(stash) + 1;
        matchpos = (loc - matchoff) & dicsize1;
        total += matchlen;
        for(i = 0; i < matchlen; i++) {
          queue[loc++] = queue[(matchpos + i) & dicsize1];
          if (loc == dicsize) {
            output(stash, queue, dicsize);
            crc16 = calc_crc16(crc16, queue, dicsize);
            loc = 0;
          }
        }
      }
    }
    if (loc) {
      output(stash, queue, loc);
      crc16 = calc_crc16(crc16, queue, loc);
    }

    destroy_stash(stash);

    RETVAL = crc16;

#line 691 "lib/Archive/Lha.c"
	XSprePUSH;
	PUSHu((UV)RETVAL);
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Archive__Lha__CRC_update); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Archive__Lha__CRC_update)
{
    dVAR; dXSARGS;
    if (items != 3)
       croak_xs_usage(cv,  "crc, str, len");
    {
	unsigned short	RETVAL;
	dXSTARG;
	unsigned short	crc = (unsigned short)SvUV(ST(0))
;
	SV *	str = ST(1)
;
	STRLEN	len = (STRLEN)SvUV(ST(2))
;
#line 550 "lib/Archive/Lha.xs"
    RETVAL = calc_crc16(crc, SvPV(str, len), len);

#line 717 "lib/Archive/Lha.c"
	XSprePUSH;
	PUSHu((UV)RETVAL);
    }
    XSRETURN(1);
}

#ifdef __cplusplus
extern "C" {
#endif
XS_EXTERNAL(boot_Archive__Lha); /* prototype to pass -Wmissing-prototypes */
XS_EXTERNAL(boot_Archive__Lha)
{
#if PERL_VERSION_LE(5, 21, 5)
    dVAR; dXSARGS;
#else
    dVAR; dXSBOOTARGSXSAPIVERCHK;
#endif
#if PERL_VERSION_LE(5, 8, 999) /* PERL_VERSION_LT is 5.33+ */
    char* file = __FILE__;
#else
    const char* file = __FILE__;
#endif

    PERL_UNUSED_VAR(file);

    PERL_UNUSED_VAR(cv); /* -W */
    PERL_UNUSED_VAR(items); /* -W */
#if PERL_VERSION_LE(5, 21, 5)
    XS_VERSION_BOOTCHECK;
#  ifdef XS_APIVERSION_BOOTCHECK
    XS_APIVERSION_BOOTCHECK;
#  endif
#endif

        newXS_deffile("Archive::Lha::Decode::Base::decode", XS_Archive__Lha__Decode__Base_decode);
        newXS_deffile("Archive::Lha::CRC::update", XS_Archive__Lha__CRC_update);
#if PERL_VERSION_LE(5, 21, 5)
#  if PERL_VERSION_GE(5, 9, 0)
    if (PL_unitcheckav)
        call_list(PL_scopestack_ix, PL_unitcheckav);
#  endif
    XSRETURN_YES;
#else
    Perl_xs_boot_epilog(aTHX_ ax);
#endif
}

#ifdef __cplusplus
}
#endif
