Subversion Repositories autosfx

Rev

Blame | Last modification | View Log | RSS feed

  1. //---------------------------------------------------------------------------
  2.  
  3. #ifndef ZipFncH
  4. #define ZipFncH
  5. /*
  6.  
  7.   Copyright (c) 1990-2007 Info-ZIP.  All rights reserved.
  8.  
  9.   See the accompanying file LICENSE, version 2007-Mar-4 or later
  10.   (the contents of which are also included in zip.h) for terms of use.
  11.   If, for some reason, all these files are missing, the Info-ZIP license
  12.   also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html
  13.  
  14.   parts Copyright (C) 1997 Mike White, Eric W. Engler
  15. ************************************************************************
  16.  Copyright (C) 2009, 2010  by Russell J. Peters, Roger Aelbrecht
  17.  
  18.    This file is part of TZipMaster Version 1.9.
  19.  
  20.     TZipMaster is free software: you can redistribute it and/or modify
  21.     it under the terms of the GNU Lesser General Public License as published by
  22.     the Free Software Foundation, either version 3 of the License, or
  23.     (at your option) any later version.
  24.  
  25.     TZipMaster is distributed in the hope that it will be useful,
  26.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  27.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  28.     GNU Lesser General Public License for more details.
  29.  
  30.     You should have received a copy of the GNU Lesser General Public License
  31.     along with TZipMaster.  If not, see <http://www.gnu.org/licenses/>.
  32.  
  33.     contact: problems@delphizip.org (include ZipMaster in the subject).
  34.     updates: http://www.delphizip.org
  35.     DelphiZip maillist subscribe at http://www.freelists.org/list/delphizip
  36. ************************************************************************/
  37.  
  38. #include "common.h"
  39. #include "DZRaw.h"
  40. #include "dzoper.h"
  41. #include "ZipDflt.h"
  42.  
  43. /* Lengths of headers after signatures in bytes */
  44. #define LOCHEAD 26
  45. #define CENHEAD 42
  46. #define ENDHEAD 18
  47.  
  48. #define OS_NTFS 0x0B00
  49.  
  50. #include "Crypt.h"
  51.  
  52. // Local option flags
  53. #define PURGE   0 // RCV Changed: was DELETE. (delete is also a function)
  54. #define ADD     1
  55. #define UPDATE  2
  56. #define FRESHEN 3
  57.  
  58. //#define ZFT_DEVICE (-1)
  59. #define ZFT_LABEL (-2)
  60.  
  61. #pragma pack(push, 4)
  62. union FOpts
  63. {
  64.     struct
  65.     {
  66.         unsigned dosflag: 2;  // nz forces dos
  67.         unsigned level:   4;
  68.         unsigned noext:   1;
  69.         unsigned keepver: 1;    // version was active
  70.         unsigned enc:       3;  // do encoded
  71.         unsigned copyold:   1;  // copy from old zip
  72.                 unsigned needver:   1;  // need unique version name
  73.         unsigned needutf8:  1;  // need utf8 name
  74.         unsigned nameutf8:  1;  // name is utf8
  75.                 unsigned nameextd:  1;  // has extended chars
  76.                 unsigned cmntextd:  1;  // has extended chars
  77.                 unsigned namenew:   1;  // name has been changed
  78.                 unsigned nameuser:  1;  // user changed name
  79.     };
  80.     unsigned long _opts;      // make copying/clearing easier
  81. };// FOpts;
  82.  
  83. // hname = zname = name in header (8 bit char)
  84. // iname = internal name
  85. // xname = xname = external name of found file
  86. // common fields
  87. class XItem //: public ZIName
  88. {
  89. protected:
  90.     char* fhname;        // header name field
  91.     const TCHAR* fxname;
  92.     const TCHAR* finame;
  93.     bool __fastcall GetIsFolder(void) const;
  94.     inline int  __fastcall GetEnc(void) const {return (int)options.enc;}
  95.     void __fastcall SetEnc(int value) {options.enc = (unsigned)value & 7;}
  96.     void __fastcall Setiname(const TCHAR *value);
  97.     void __fastcall Setxname(const TCHAR *value);
  98.     inline DZStrW GetXName(void) const {return DZStrW(fxname);}
  99.     inline void __fastcall SetXName(const DZStrW& value) {Setxname(value.c_str());}
  100.     inline DZStrW GetIName(void) const {return DZStrW(finame);}
  101.     inline void __fastcall SetIName(const DZStrW& value) {Setiname(value.c_str());}
  102.     inline const char* Gethname(void) const {return (const char*)fhname;}  
  103.     void __fastcall Sethname(const char* value);  
  104.     inline DZStrA GetHName(void) const {return DZStrA(fhname);}
  105.     inline void __fastcall SetHName(const DZStrA& value) {Sethname(value.c_str());}
  106. public:
  107.     FOpts       options;
  108.     const TCHAR *Base;
  109.     const char  *Passw;     //* Password if set on a per file base -P switch added R.Aelbrecht */
  110.     ZInt64        len;           // uncompressed Size of the file. RCV Added
  111.     XItem();
  112.     XItem(const XItem& other);
  113.     virtual ~XItem();
  114.     XItem& operator=(const XItem& other);
  115.     DZStrW __fastcall FullPath(void) const;  // full path of name
  116.     __property const TCHAR *xname = {read=fxname, write=Setxname};
  117.     __property DZStrW XName = {read=GetXName, write=SetXName};
  118.     __property const TCHAR *iname = {read=finame, write=Setiname};
  119.     __property DZStrW IName = {read=GetIName, write=SetIName};
  120.     __property const char *hname = {read=Gethname, write=Sethname};
  121.     __property DZStrA HName = {read=GetHName, write=SetHName};
  122.     __property int Enc = {read=GetEnc, write=SetEnc};
  123.     __property bool IsFolder = {read=GetIsFolder};
  124. };
  125.  
  126. // found files
  127. class FndItem : public XItem
  128. {
  129. public:
  130.     FndItem  *nxt;       //* Link to next name
  131.     FndItem();
  132.     FndItem(const XItem& other);
  133.         ~FndItem();
  134. };
  135.  
  136. // as read or for processing
  137. class ZipItem : public XItem
  138. {
  139. private:
  140.         char* fhname;        // header name field
  141.     DZRawData fextra;
  142.     DZRawData fcextra;
  143.     XNTFSData* fntfs;
  144.         wchar_t* fcomment;
  145.     inline WORD GetExt(void) const {return (WORD)extra.Length();}
  146.     inline WORD GetCext(void) const {return (WORD)cextra.Length();}
  147.     inline void SetExt(WORD value) {extra.SetLength(value);}
  148.     inline void SetCext(WORD value) {cextra.SetLength(value);}
  149.     inline DZStrW GetComment(void) const {return DZStrW(fcomment);}
  150.         void __fastcall SetComment(const DZStrW& value);
  151. protected:
  152.         void __fastcall Copy(const ZipItem& other);
  153. public:
  154. #pragma pack(push, 2)
  155.         ush           vem,      // version made
  156.     ver,                    // version required
  157.     flg,                    // general bit flag
  158.     how;                    // method
  159.         ::extent nam,           // length zname
  160. //    ext,                  // length local header extra data
  161. //    cext,                 // length central header extra data
  162.     com;                    // length comment
  163.     ush           dsk,      // disk number of local header
  164.     att,
  165.     lflg;                   // local header flag
  166. #pragma pack(pop)
  167.     ulg           atx;
  168.     ulg           tim,        // file time (dos)
  169.     crc;                    // crc
  170.     ZInt64        siz;        // compressed
  171.     ZInt64        off;        // offset local header
  172.     int           mark;       // Marker for files to operate on
  173.         int           trash;      // Marker for files to delete
  174.     __property DZRawData extra = {read=fextra, write=fextra};
  175.     __property DZRawData cextra = {read=fcextra, write=fcextra};
  176.     __property XNTFSData* ntfs = {read=fntfs, write=fntfs};
  177.     __property DZStrW Comment = {read=GetComment, write=SetComment};
  178.     __property WORD ext = {read =GetExt, write=SetExt};
  179.     __property WORD cext = {read =GetCext, write=SetCext};
  180.     ZipItem  *nxt;       // Pointer to next header in list
  181.     ZipItem();
  182.     ZipItem(const FndItem* f);
  183.     ZipItem(const ZipItem& other);
  184.         ZipItem& operator=(const ZipItem& other);
  185.     ~ZipItem();
  186. };
  187. #pragma pack(pop)
  188.  
  189. // node used for duplicate checks
  190. class HL_node
  191. {
  192. public:
  193.     struct HL_node *nxt;
  194.     ulg hash;
  195.     const XItem *xdata;
  196.     HL_node();//:nxt(NULL),hash(0),xdata(NULL){};
  197. };
  198.  
  199. #define NODE_BLOCK_SIZE 5000
  200. class HL_block
  201. {
  202. private:
  203.     HL_block(void);
  204.     HL_block(const HL_block&);
  205.     HL_block& operator=(const HL_block&);
  206. public:
  207.     HL_node nodes[NODE_BLOCK_SIZE];
  208.     HL_block *prv;   // link for removal
  209.     HL_block(HL_block *prev);
  210.     ~HL_block(void);
  211. };
  212.  
  213.  
  214. class HashList
  215. {
  216. private:
  217.     HashList(void);
  218.     HashList(const HashList&);
  219.     HashList& operator=(const HashList&);
  220. public:
  221.     HashList(int siz);
  222.     virtual ~HashList(void);
  223. protected:
  224.     HL_node **Table;
  225.     HL_block *blocks;
  226.     int nno;
  227.     unsigned Mask;
  228.     unsigned Hash;
  229.     unsigned Index;
  230.     HL_node * NewNode(const XItem *x, ulg hash);
  231.     const XItem *AddANode(const XItem *xname);
  232.     XItem *FindAName(void);
  233.     virtual bool Matches(const HL_node *node) const = 0;
  234.     virtual HL_node* Prepare(void) = 0;
  235. };
  236.  
  237. class HashListExt : public HashList
  238. {
  239. private:
  240.     DZStrW fxname;
  241.     HashListExt(void);
  242.     HashListExt(const HashListExt&);
  243.     HashListExt& operator=(const HashList&);
  244. public:
  245.     HashListExt(int siz): HashList(siz){;}
  246.     ~HashListExt(void);                    
  247.     XItem *FindName(const DZStrW& name);
  248.     const XItem *AddNode(const XItem *xname);
  249. protected:
  250.     bool Matches(const HL_node *node) const;
  251.     HL_node* Prepare(void);
  252. };
  253.  
  254. class HashListInt : public HashList
  255. {
  256. private:
  257.     HashListInt(void);
  258.     HashListInt(const HashList&);
  259.     HashListInt& operator=(const HashList&);
  260. protected:
  261.     DZStrW finame;
  262.     bool Matches(const HL_node *node) const;
  263.     HL_node* Prepare(void);
  264. public:
  265.     HashListInt(int siz): HashList(siz){;}
  266.     ~HashListInt(void);
  267.     XItem *FindName(const DZStrW& name);
  268.     const XItem *AddNode(const XItem *xname);
  269. };
  270.  
  271. /*************/
  272. /*  ZGlobals  */
  273. /*************/
  274.  
  275. class ZipFunc : public ZipDflt
  276. {
  277. private:
  278.     ZipFunc(void);
  279.     ZipFunc(const ZipFunc&);
  280.     ZipFunc& operator=(const ZipFunc&);
  281. protected:
  282.     int fglobal_error_code; // in UnzErr() in dllmain
  283.     int ffiles_acted_on;
  284.     unsigned fEncodeAs;
  285.     char ffnamebuf[MAX_PATH + 1];
  286.     DZStrW fRootDir;
  287.     DZStrA fzcomment;
  288.     DZStrW ftempath;
  289.     DZStrW ftempzip;
  290.     DZStrA fGPassword;
  291.     DZStrW fStartCDir;
  292.     const char* fuser_key; // user entered key or NULL RP 1.73
  293.     int fzcomlen;
  294.     int fResetArchiveBit;
  295.     bool fHowToMove;
  296.     DWORD fStrFileAttr;
  297.     DWORD fStrFileDate;  
  298.     DZStrW fExcludes;
  299.     ZFilter *fSpecials;
  300.     int fpcount;
  301.     FndItem *ffound;
  302.     FndItem **ffnxt;
  303.     ZipItem *fzfiles;
  304.     ZipItem *fzfound;   // list of prepared found
  305.     ::extent fzcount;
  306.     ZipItem* VerFiles;
  307.     int flatest;
  308.     int fNoExtChk;// method;
  309.         int fadjust;
  310.     int faction;
  311.     int fversion;
  312.     int fdirnames;
  313. //    int fpathput;
  314.         int fjunk_sfx;
  315.     int frecurse;
  316.     ulg fbefore;
  317.     int flinkput;
  318.     int ffcount;
  319.     int fNoPrecalc;
  320.     int fGEncrypt;
  321.     ulg VerDate;
  322.  
  323.     DZStrW flabel;
  324.     int fzipstate;
  325.     struct stati64 fzipstatb;
  326.     ulg flabel_time;
  327.     ulg flabel_mode;
  328.     time_t flabel_utim;
  329.  
  330.     ZInt64 fOutPosn; //tempzn;
  331.     ZInt64 fcenbeg;
  332.     ZInt64 fzipbeg;
  333.     int fAllowGrow;                // new 1.73
  334.     int fCallback_Exception;       // new 1.73.2.6
  335.     int fBatchStarted;             // new 1.73.4.2
  336.     DZStrW fFullPath;               // 1,74
  337.     int fdoall;                    // new 178.5.0
  338.     uch *fhwbuf;                  // allocated buffer
  339.         int  fhwsize;                  // size of buffer
  340.     int  fhwcnt;                   // current byte count
  341.     HashListInt *fndlist;           // existing files
  342.         HashListInt *IntList;           // internal names
  343. public:
  344.     int fArchiveFiles;
  345.     ZipFunc(const DllCommands *C);
  346.     ~ZipFunc(void);
  347.     void ZipCleanup(void);        //ZipSel            
  348.     int Init(void); // after construction
  349. protected:
  350.     int     ZipProcess(void);
  351.     DZStrW __fastcall     CleanedPath(const DZStrW &pth);
  352.         ZipItem    *zsearch(const DZStrW &name);
  353.         int     trash(void);
  354.         DZStrW tempname(void);
  355. private:
  356.     int  LocXData;              // offset to local header extra data (for redo)
  357.         void    finish(void);                      //ZipSel
  358.     int     replace(const DZStrW &oldname, const DZStrW &newname);
  359.     int     replaceOrig(const DZStrW &d, const DZStrW &s);
  360.         int     TrashDir(const DZStrW &dir);
  361.     int   check_dupExt(void);
  362.     int zipup(ZipItem *);
  363.     int zipVersion(ZipItem *);
  364.     int __fastcall SetVers(ZipItem* z, int mthd);
  365.  
  366.     int __fastcall  PutLocal(ZipItem *);
  367.     int __fastcall  UpdateLocal(ZipItem *);
  368.     int __fastcall  putextended(ZipItem *);
  369.     int __fastcall  putcentral(ZipItem *);
  370.     int __fastcall  zipcopy(ZipItem *);
  371.     int __fastcall  PutEnd(unsigned CentralCnt, __int64 CentralOffs);
  372.     int     fcopy(ZInt64);
  373.     ulg     zfiletime(const DZStrW &name, ulg *, __int64 *, ztimbuf *);
  374.     int     filecopy(HANDLE f, HANDLE g);
  375.     void    HWriteInit(void);
  376.     int     HWrite(const void *arg, int siz);
  377.         char *  ChangeBWSlash(char *fn);
  378.     int     __fastcall NameVer(ZipItem* z);
  379.     void DupName(bool fatal, const XItem* o, const XItem* n, const DZStrW name);
  380.     int __fastcall PrepareHeaderName(ZipItem* z, bool NoComment);
  381. }; /* end of struct ZGlobals */
  382.  
  383. void  stamp(const DZStrW &name, ulg);
  384. ulg     FileTime2DosTime(_FILETIME ftime);
  385. int     NtfsFileTime2utime(const FILETIME *pft, time_t *ut);
  386. int     percent(__int64 n, __int64 m);
  387. int     setfileattr(const DZStrW&, int);
  388.  
  389. int __fastcall SameNameExt(const DZStrW &fname, const DZStrW &oname);
  390.  
  391. DZStrW last(const DZStrW &s, int);
  392. int __fastcall IsShExp(const DZStrW &p);
  393.  
  394. #endif
  395.  
  396.  
  397.  
  398.