//---------------------------------------------------------------------------

#ifndef SFXInflateH
#define SFXInflateH
#include <windows.h>
#include "SFXStructs.h"
//---------------------------------------------------------------------------
/* inflate.h for UnZip -- put in the public domain by Mark Adler
   version c14f, 23 November 1995
 * This version modified by Roger Aelbrecht
 * for BCB/TZipBuilderSFX, Jan 16, 2003.
 */

/* You can do whatever you like with this source file, though I would
 * prefer that if you modify it and redistribute it that you include
 * comments to that effect with your name and the date.  Thank you. */
// Macro's NEEDBITS, DUMPBITS replaced by functions

extern HANDLE VH_OutFile;
extern int VInt_BytesToGo;
extern unsigned VDW_CRC32Val;
extern TZipCentralHeader VRec_ZipHeader;

extern void Crc32_Buf(char* str, int len, unsigned& crc);
extern unsigned FRead(void* Buffer,const unsigned cNum);
extern int decrypt_byte(void);
extern void UpdateKeys(byte c);

// window size--must be a power OF two, and at least 32k
const int WSIZE = 32768;   // = 0x8000
const int BMAX = 16;    // maximum bit length of any code (16 for explode)
const int N_MAX = 288;  // maximum number of codes in any set
static const int lbits = 9;     // bits in base literal/length lookup table
static const int dbits = 6;     // bits in base distance lookup table
/* Tables for deflate from PKZIP's appnote.txt. */
const unsigned border[] =
{      /* Order of the bit length code lengths */
  16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
};
const unsigned short cplens[] =
{   /* Copy lengths for literal codes 257..285 */
  3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
};
        /* note: see note #13 above about the 258 in this list. */
const unsigned short cplext[] =
{   /* Extra bits for literal codes 257..285 */
  0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99
};                              /* 99==invalid */
const unsigned short cpdist[] =
{   /* Copy offsets for distance codes 0..29 */
  1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  8193, 12289, 16385, 24577
};
const unsigned short cpdext[] =
{   /* Extra bits for distance codes */
  0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  12, 12, 13, 13
};
/* And'ing with mask_bits[n] masks the lower n bits */
const unsigned short mask_bits[] =
{
  0x0000,
  0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
  0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
};

char BufType[WSIZE];

unsigned hufts;
long  bb; // bit buffer (Static)
unsigned  bk; // bits in bit buffer (Static)
unsigned  wp; // Static Global
char*  InBuf;
char*  Slide;
unsigned  InPTR; // Index FOR ZipFile input buffer
unsigned  ZipCount; // Count OF bytes in ZipFile input buffer
Thuft* fixed_tl;
Thuft* fixed_td;
bool Fixed_inited = false;// fixed tables are initialized
int huft_build(unsigned *b, unsigned n, unsigned s, const unsigned short * d,
               const unsigned short * e, struct Thuft **t, int *m);
int huft_free(Thuft* t);
int inflate_codes(Thuft *tl, Thuft *td, int bl, int bd);
int GetNextByte(void);
int Get_Compressed(void);
void NEEDBITS(unsigned n, unsigned long& b, unsigned& k);
void DUMPBITS(unsigned n, unsigned long& b, unsigned& k);
void inflate_stored(void);
void inflate_fixed(void);
void inflate_dynamic(void);
void inflate_block(int* e);
void Inflate(void);
void Init_Fixed_Tables(void);
void Release_Fixed_Tables(void);

#endif
