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

#ifndef SFXProcsH
#define SFXProcsH
#include <windows.h>
#include <commctrl.h>
#include <dir.h>
#include <stdio.h> // added for spanning
#include "SFXrc.h"
#include "SFXStructs.h"

#define Str_Dlg_FileExists "FILEEXIST"
#define Str_Dlg_Password   "PASSWD"
#define MaxInt (int)(2147483647)
const unsigned CRCPOLY = 0xEDB88320;
const int  UCHARMAX = 256;
const int  CHARBIT = 8;
// 'DZ'#0#1;  // current header signature
const int SFX_HEADER_SIG = 0x01005A44;
  // 'DZ'#10#1; // current end of header signature
const int SFX_HEADER_END_SIG = 0x010A5A44;
  // 'DZ'#20#1; // detached header signature
const int SFX_DETACHED_HEADER_SIG = 0x01145A44;
  // 'DZ'#20#2; // end of detached header signature
const int SFX_DETACHED_HEADER_END_SIG = 0x02145A44;

const int ZipLocalHeaderSig = 0x4034b51;// +1 because ZipDll doesn't like
const int ZipCentralHeaderSig = 0x2014b51;// this sigs in the .bin
const int ZipEndOfCentralSig = 0x6054b51;
const int ZipExtLocalHeaderSig = 0x8074b51;
const int  RAND_HEAD_LEN = 12;
const unsigned CRC_MASK = 0xFFFFFFFF;
// window size--must be a power OF two, and at least 32k
const int WSIZE = 32768;   // = 0x8000
const int BufSize  = 8192;
#define SPAN_TYPE_SPANNED 1
#define SPAN_TYPE_MULTI_VOL 2

extern char* Cap_Err;
extern char* Err_WrongFileSize;
extern char* Err_Directory;
extern char* Err_Run_Run;
extern char* Err_Run_Inst;
extern char* Err_Archive;
extern char* Err_ZipUnknownComp;
extern char* Err_CannotOpenFile;
extern char* Err_CannotCloseFile;
extern char* Err_Abortion;
extern char* Err_WriteFile;
extern char* Err_CRC32;
extern char* Msg_RunCheckBox_Run;
extern char* Msg_RunCheckBox_Inst;
extern char* Msg_InsertDisk;

extern HINSTANCE MInstance;
MSG Msg; // structure for process messages
HANDLE VH_InFile;
HANDLE VH_OutFile;
char PathBuf[MAXPATH]; // buffer for path operations
char TempBuf[MAXPATH];
unsigned VArr_CRC32Table[257];
unsigned VArr_CryptKey[3];
unsigned VDW_OffsetDelta;
int VInt_FileBegin;
int VInt_BytesToGo;
bool VBool_CheckDeleteVolatilePath;
unsigned VDW_CRC32Val;
bool VBool_SpannedArchive;
TSFXFileHeader VRec_SFXHeader;
char* VStr_SFX_Caption;
char* VStr_SFX_StartMsg;
char* VStr_SFX_CmdLine;
char* VStr_SFX_RegFailPath;
char* VStr_SFX_Path;
char* VStr_ExtractPath;
char* VStr_ExeName;
char* VStr_VolatilePath;
char* VStr_VolatilePath_Unexpanded;
char* VStr_CurrentFile;
char* VStr_Password;
TZipCentralHeader VRec_ZipHeader;
// added for span
char* VStr_DetachName; // name of the detached archive the sfx belongs to
char* VStr_DetachExt; // file ext of the detached archive the sfx belongs to
char* VStr_SourceDir;

int VInt_SpanType;
char TempFileBuf[BufSize];
char Tempfile[MAXPATH];
HANDLE VH_TempFile;
char drive[MAXDRIVE + 1]; // drive name
int drt; // drivetype
TZipCentralHeader* CentralHeaders[65535];
int CentralHeadersIndex;

extern bool VBool_Cancelled;
extern bool CALLBACK FileExistsDialogProc(HWND DlgWin, UINT DlgMessage, WPARAM DlgWParam,
                                   LPARAM DlgLParam);
extern bool CALLBACK PasswordQueryDialogProc(HWND DlgWin, UINT DlgMessage, WPARAM DlgWParam,
                                      LPARAM DlgLParam);
extern void Inflate(void);
extern void Release_Fixed_Tables(void);
//---------------------------------------------------------------------------
bool CALLBACK FindChE(HWND wnd, LPARAM lParam);
int MsgBox(const HWND wndPar, const char* sMsg,const char* sTitle,
           const unsigned uType);
void ErrorMsgBox(const HWND wndPar,const char* sMsg);
void ErrorHalt(const char* Msg, const char* Arg);
void Make_CRC32Table(void);
void GetDefParams(void);
unsigned GetExeSize(void);
unsigned FSeek(const UINT Offset,const WORD MoveMethod);
unsigned FRead(void* Buffer,const unsigned cNum);

char* ReadSFXString(const int Len);
char* GetPathFromRegistry(char* Path);
void AppendDirSeparator(char* Dir);
char* RemoveDirSeparator(char* Dir);
char* ExpandEnv(char* Str);
void FormatStr(const char* Format,const char* args, char* Dest);
void FormatStr2Arg(const char* Format, const char* arg1, const char* arg2, char* Dest);
char* GetCurDir(void);
char* GetUniqueVolatilePath(void);
char* ExpandPath(const char* Rel);
void AddFilesListViewCol(const HWND wndDlg, const int Index,const char* Caption,
                         const int Direction, int Width);
void GetRunCheckBoxText(char* Dest);
void ResizeControl(const HWND wnd, const bool Reposition, int yDiff);
void FillListView(HWND wndOwner);
char* StrGetEditText(HWND wndPar);
void SelectAllInFilesListView(const HWND wndDlg);
void EnableChildren(const HWND wnd, const bool Enable);
bool Extract(HWND wndOwner);
unsigned ExecuteCMD(void);
void GetRunErrorMessage(char* Dest);
void GetRunString(const char* Run, const char* Inst, char* Dest);
unsigned FindEOCRecord(void);
void CheckCodePage(char* IN, const TZipCentralHeader recHdr);
void AddFileToList(const HWND wndOwner, char* Name, const TZipCentralHeader* Rec);
void ProcessMessages(void);
void GetFileFromList(HWND wndOwner, const int iiItem, TZipCentralHeader*& Rec, char* Dest);
bool ForceDirectories(char* Dir);
bool ExtractFile(HWND wndOwner,char* Filename, const TZipCentralHeader* rec,bool& PasswordFailed);
void DeSelectInFilesListView(const HWND wndDlg, const int Item);
void ShowFirstSelected(const HWND wndList);
void GetArgument(const int Index, char* Dest);
bool TestForInf(char* sr1);
unsigned ExecInf(char* Path);
bool DirectoryExists(const char* Dir);
void RemoveDirEx(void);
bool decrypt_pw(char* Encrypt_Head, BYTE EncHead_len, WORD BitFlag, long CRC,
                long FileDate, char* Password);
void Unstore(void);
bool IsWinNT(void);
void seedk(char* passwd);
int decrypt_byte(void);
void UpdateKeys(byte c);
void Crc32_Buf(char* str, int len, unsigned& crc);
unsigned UpdC32(unsigned Crc,byte Chr);
bool SfxFileExists(const char* FileName);
bool CompareText(char* s1, char* s2);
void ExtractFileName(char* FileName, char* Dest);
void ExtractFilePath(char* Filename, char* Dest);
void FileSetDate(const HANDLE Handle,const unsigned short Date,const unsigned short Time);
void SfxExit(bool success, bool doexit);
// added for spanned archives
void CheckIfSpanned(void);
void OpenRightArchive(HWND wndOwner, const int DiskNumber);
void GetNewDisk(HWND wndOwner, int DiskSeq);
void RWJoinData(HWND wndOwner, char *Buffer, int ReadLen, int& DiskNbr,bool Copy);
void WriteJoin(void *Buffer, int BufferSize);
bool IsRightDisk(int DiskSeq);
void ExtractToTempFile(const HWND wndOwner, unsigned& LocalOffset, HANDLE& OldHandle);
void RollBackTempFile(const HWND wndOwner, const HANDLE OldHandle);
TZipCentralHeader* GetFromItemData(const int Index);
unsigned AddToItemData(const TZipCentralHeader* rec);
#endif

