Subversion Repositories autosfx

Rev

Blame | Last modification | View Log | RSS feed

  1. //---------------------------------------------------------------------------
  2.  
  3. #ifndef UnzOpH
  4. #define UnzOpH
  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 "unzerr.h"
  39. #include "dzraw.h"
  40. #include "dzoper.h"
  41. #include "unzsup.h"
  42. #include "unzinf.h"
  43.  
  44. #define SKIP              0     /* choice of activities for do_string() */
  45. #define DISPLAY           1
  46. #define DISPL_8           5     /* Display file comment (ext. ASCII)    */
  47. #define DS_FN             2
  48. #define EXTRA_FIELD       3
  49. #define DS_EF             3
  50.  
  51. #define DOES_NOT_EXIST    -1    /* return values for check_for_newer() */
  52. #define EXISTS_AND_OLDER  0
  53. #define EXISTS_AND_NEWER  1
  54.  
  55. #define ROOT              0     /* checkdir() extract-to path:  called once */
  56. #define INIT              1     /* allocate buildpath:  called once per member */
  57. #define APPEND_DIR        2     /* append a dir comp.:  many times per member */
  58. #define APPEND_NAME       3     /* append actual filename:  once per member */
  59. #define GETPATH           4     /* retrieve the complete path and free it */
  60. #define END               5     /* free root path prior to exiting program */
  61.  
  62. //----------------------------------------------------------
  63.  
  64. /* version_made_by codes (central dir):  make sure these */
  65.  
  66. /*  are not defined on their respective systems!! */
  67. //----------------------------------------------------------
  68. #define FS_FAT_           0     /* filesystem used by MS-DOS, OS/2, Win32 */
  69. #define AMIGA_            1
  70. #define VMS_              2
  71. #define UNIX_             3
  72. #define VM_CMS_           4
  73. #define ATARI_            5     /* what if it's a minix filesystem? [cjh]     */
  74. #define FS_HPFS_          6     /* filesystem used by OS/2, NT                */
  75. #define MAC_              7
  76. #define Z_SYSTEM_         8
  77. #define CPM_              9
  78. #define TOPS20_           10
  79. #define FS_NTFS_          11    /* filesystem used by Windows NT              */
  80. #define QDOS_MAYBE_       12    /* a bit premature, but somebody once started */
  81. #define ACORN_            13    /* Archimedes Acorn RISC OS                   */
  82. #define FS_VFAT_          14    /* filesystem used by Windows 95, NT          */
  83. #define MVS_              15
  84. #define BEBOX_            16
  85. #define TANDEM_           17    /* Tandem/NSK                                 */
  86. #define NUM_HOSTS         18    /* index of last system + 1                   */
  87.  
  88. #define STORED            0     /* compression methods                        */
  89. #define SHRUNK            1
  90. #define REDUCED1          2
  91. #define REDUCED2          3
  92. #define REDUCED3          4
  93. #define REDUCED4          5
  94. #define IMPLODED          6
  95. #define TOKENIZED         7
  96. #define DEFLATED          8
  97. #define ENHDEFLATED       9
  98. #define DCLIMPLODED      10
  99. //#define NUM_METHODS       9     /* index of last method + 1 */
  100. //#define PKRESMOD11       11
  101. //#define BZIPPED          12
  102. //#define NUM_METHODS      13    /* index of last method + 1 */
  103.  
  104. /* don't forget to update //list_files() appropriately if NUM_METHODS changes */
  105.  
  106. /* 9 and 10 not yet implemented */
  107.  
  108. /* extra-field ID values, little-endian: */
  109. #define EF_AV        0x0007     /* PKWARE's authenticity verification */
  110. #define EF_OS2       0x0009     /* OS/2 extended attributes */
  111. #define EF_PKVMS     0x000c     /* PKWARE's VMS */
  112. #define EF_PKUNIX    0x000d     /* PKWARE's Unix */
  113. #define EF_IZVMS     0x4d49     /* Info-ZIP's VMS ("IM") */
  114. #define EF_IZUNIX    0x5855     /* Info-ZIP's old Unix[1] ("UX") */
  115. #define EF_IZUNIX2   0x7855     /* Info-ZIP's new Unix[2] ("Ux") */
  116. #define EF_TIME      0x5455     /* universal timestamp ("UT") */
  117. #define EF_JLMAC     0x07c8     /* Johnny Lee's old Macintosh (= 1992) */
  118. #define EF_ZIPIT     0x2605     /* Thomas Brown's Macintosh (ZipIt) */
  119. #define EF_VMCMS     0x4704     /* Info-ZIP's VM/CMS ("\004G") */
  120. #define EF_MVS       0x470f     /* Info-ZIP's MVS ("\017G") */
  121. #define EF_ACL       0x4c41     /* (OS/2) access control list ("AL") */
  122. #define EF_NTSD      0x4453     /* NT security descriptor ("SD") */
  123. #define EF_BEOS      0x6542     /* BeOS ("Be") */
  124. #define EF_QDOS      0xfb4a     /* SMS/QDOS ("J\373") */
  125. #define EF_AOSVS     0x5356     /* AOS/VS ("VS") */
  126. #define EF_SPARK     0x4341     /* David Pilling's Acorn/SparkFS ("AC") */
  127. #define EF_MD5       0x4b46     /* Fred Kantor's MD5 ("FK") */
  128. #define EF_ASIUNIX   0x756e     /* ASi's U */
  129.  
  130. #define EB_HEADSIZE       4     /* length of extra field block header */
  131. #define EB_ID             0     /* offset of block ID in header */
  132. #define EB_LEN            2     /* offset of data length field in header */
  133. #define EB_UCSIZE_P       0     /* offset of ucsize field in compr. data */
  134. #define EB_CMPRHEADLEN    6     /* lenght of compression header */
  135.  
  136. #define EB_UX_MINLEN      8     /* minimal "UX" field contains atime, mtime */
  137. #define EB_UX_FULLSIZE    12    /* full "UX" field (atime, mtime, uid, gid) */
  138. #define EB_UX_ATIME       0     /* offset of atime in "UX" extra field data */
  139. #define EB_UX_MTIME       4     /* offset of mtime in "UX" extra field data */
  140. #define EB_UX_UID         8     /* byte offset of UID in "UX" field data */
  141. #define EB_UX_GID         10    /* byte offset of GID in "UX" field data */
  142.                                                
  143. /*---------------------------------------------------------------------------
  144.     True sizes of the various headers, as defined by PKWARE--so it is not
  145.     likely that these will ever change.  But if they do, make sure both these
  146.     defines AND the typedefs below get updated accordingly.
  147.   ---------------------------------------------------------------------------*/
  148. #define LREC_SIZE   26          /* lengths of local file headers, central */
  149. #define CREC_SIZE   42          /*  directory headers, and the end-of-    */
  150. #define ECREC_SIZE  18          /*  central-dir record, respectively      */
  151. #define ECREC64_SIZE 56
  152.  
  153. // #define MAX_BITS    13          /* used in unshrink() */
  154. // #define HSIZE       (1 << MAX_BITS)     /* size of global work area */
  155.  
  156. #define LF     10               /* '\n' on ASCII machines; must be 10 due to EBCDIC */
  157. #define CR     13               /* '\r' on ASCII machines; must be 13 due to EBCDIC */
  158. #define CTRLZ  26               /* DOS & OS/2 EOF marker (used in fileio.c, vms.c) */
  159.  
  160. #define ENV_UNZIP     "UNZIP"   /* the standard names */
  161. #define ENV_ZIPINFO   "ZIPINFO"
  162.  
  163. #ifndef S_ISDIR
  164. #ifdef CMS_MVS
  165. #    define S_ISDIR(m)  (false)
  166. #else
  167. #    define S_ISDIR(m)  (((m) & S_IFMT) == S_IFDIR)
  168. #endif
  169. #endif
  170.  
  171. #ifndef IS_VOLID
  172. #  define IS_VOLID(m)  ((m) & 0x08)
  173. #endif
  174.  
  175. #define makeword(p) ((ush)*(ush *)(p))
  176. #define makelong(p) ((ulg)*(ulg*)(p))
  177. #define makeint64(p) ((ZInt64)*(ZInt64*)(p))
  178. /**************/
  179.  
  180. /*  Typedefs  */
  181.  
  182. /**************/
  183.  
  184. typedef uch ec_byte_rec[ECREC_SIZE + 4];
  185.  
  186. /*     define SIGNATURE                         0   space-holder only */
  187. #      define NUMBER_THIS_DISK                  4
  188. #      define NUM_DISK_WITH_START_CENTRAL_DIR   6
  189. #      define NUM_ENTRIES_CENTRL_DIR_THS_DISK   8
  190. #      define TOTAL_ENTRIES_CENTRAL_DIR         10
  191. #      define SIZE_CENTRAL_DIRECTORY            12
  192. #      define OFFSET_START_CENTRAL_DIRECTORY    16
  193. #      define ZIPFILE_COMMENT_LENGTH            20
  194.  
  195. #pragma pack(push, 4)
  196. typedef struct end_central_dir_record
  197. {
  198.     /* END CENTRAL */
  199.         ulg number_this_disk;
  200.         ulg num_disk_start_cdir;
  201.         ZInt64 num_entries_centrl_dir_ths_disk;
  202.         ZInt64 total_entries_central_dir;
  203.         ZInt64 size_central_directory;
  204.     ZInt64 offset_start_central_directory;
  205.     int is_zip64_archive;
  206.     ush zipfile_comment_length;
  207. }ecdir_rec;
  208. #pragma pack(pop)
  209.  
  210. #pragma pack(push, 1)
  211. typedef struct central_directory_file_header
  212. {
  213.         ush made_by;
  214.     uch version_needed_to_extract[2];
  215.     ush general_purpose_bit_flag;
  216.     ush compression_method;
  217.     ush last_mod_file_time;
  218.     ush last_mod_file_date;
  219.         ulg crc32;
  220.     ush filename_length;
  221.     ush extra_field_length;
  222.     ush file_comment_length;
  223.     ulg disk_number_start;
  224.     ush internal_file_attributes;
  225.         ulg external_file_attributes;
  226.     ZInt64 csize;
  227.     ZInt64 ucsize;
  228.     ZInt64 relative_offset_local_header;
  229. }
  230. cdir_file_hdr;
  231. #pragma pack(pop)
  232.  
  233. /*************/
  234.  
  235. class UnzOpr : public UnzInf
  236. {
  237.     protected:
  238.         int fdflag;                    /* -d: all args are files/dirs to be extracted */
  239.         int ffflag;                    /* -f: "freshen" (extract only newer files) */
  240.         int fjflag;                    /* -j: junk pathnames (unzip) */
  241.         int flflag;                    /* -12slmv: listing format (zipinfo)*/
  242.         int foverwrite_all;            /* -o combined with -n: Ok to overwrite used as default */
  243.         int fvolflag;                  /* -$: extract volume labels */
  244.         int fT_flag;                   /* -T: timestamps (unzip) or dec. time fmt (zipinfo) */
  245.         int fuflag;                    /* -u: "update" (extract only newer/brand-new files) */
  246.         int fzflag;                    /* -z: display the zipfile comment (only, for unzip) */
  247.  
  248.         int ffilespecs;                /* number of real file specifications to be matched */
  249.         UnzFileSpec *SrchSpecs;        // files to extract
  250.         int fprocess_all_files;
  251.         int fcreate_dirs;              /* used by main(), mapname(), checkdir() */
  252.         int fextract_flag;
  253.         ZInt64 freal_ecrec_offset;
  254.         ZInt64 fexpect_ecrec_offset;
  255.                 ZInt64 fziplen;
  256.         DZRawData extra_field;
  257.         DZStrA file_name;
  258.         uch *fhold;
  259.         cdir_file_hdr fcrec;           /* used in unzip.c, extract.c, misc.c */
  260.         ecdir_rec fecrec;              /* used in unzip.c, extract.c */
  261.  
  262.         struct stati64 fstatbuf;          /* used by main, mapname, check_for_newer */
  263.         int fno_ecrec;                 /* process static */
  264. #ifdef NOVELL_BUG_FAILSAFE
  265.         int fdne;                      /* true if stat() says file doesn't exist */
  266. #endif
  267.         int fcreated_dir;
  268.         int frenamed_fullpath;
  269.         int ffnlen;
  270.         unsigned fnLabelDrive;    
  271.         DZStrW fendHPFS;
  272.         DZStrW fendFAT;
  273.         DZStrW fdirname;
  274.         DZStrW fwildname;
  275.         DZStrW fmatchname;
  276.         int frootlen;
  277.         int fhave_dirname;
  278.         int fdirnamelen;
  279.         int fnotfirstcall;
  280.         void *fwild_dir;
  281.  
  282.         int ffiles_acted_on;
  283.         DZStrW frootpath;
  284.         DZStrW fTempPath;
  285. //#ifdef CRYPT                    /* If no crypt don't use */
  286.                 char flpszPassword[PWLEN + 1]; /* Used in PassMsg.c                                                                                             */
  287.                 WORD fcchPassword;
  288.                 bool fpwork;
  289.                 int frcode;
  290. //#endif
  291.  
  292.     public:
  293.         UnzOpr(const DllCommands *C);
  294.         ~UnzOpr(void);
  295.         long Exec(const DllCommands *C);
  296.         int FatalError(int c);
  297.         int Init(void);
  298.  
  299.     protected:
  300.         DZStrW UnzMsg;
  301.         DZStrW PrevPath;      // avoid checking path again
  302.         int mapattr(void);
  303.         void close_outfile(void);
  304.         DZStrW GetFullPath(const DZStrW& p);
  305.         int mapname(int renamed);
  306.         int open_input_file(void);
  307.         int open_outfile(void);
  308.         int check_for_newer(const DZStrW& filename);
  309.         int AskOverwrite(const DZStrW& filename, int cond); // true = overwrite
  310.         int do_string(unsigned int len, int option);
  311.                 void UnzErr(int c); // probably FatalError
  312.                 int __fastcall getZip64Data(const DZRawData& extra);//const uch *ef_buf, unsigned ef_len);
  313.         void process_zipfiles(void);
  314.         int extract_or_test_files(void);
  315.         int process_cdir_file_hdr(void);
  316.         int get_cdir_ent(void);
  317.         int process_local_file_hdr(void);
  318.         int uz_end_central(void);
  319.                 int SSExtract(void);
  320.         bool FSetUpToProcessZipFile(const DllCommands *C);
  321.         void TakeDownFromProcessZipFile(void);
  322.         int UnzStreamStream(void);
  323.  
  324.     private:
  325.         int getNTfiletime(FILETIME * ft);
  326.         int do_seekable(int lastchance);
  327.         int find_ecrec(ZInt64 searchlen);
  328.         int rec_find(ZInt64, ulg, int);
  329.         int check_ecrec_zip64(void);
  330.         int find_ecrec64(ZInt64 searchlen);
  331.         int StoreInfo(void);
  332.         int extract_or_test_member(void);
  333.         int SelectMembers(void);
  334.                 void CloseOut(void);
  335.         int CD_Init(const DZStrW& pathcomp, bool renamed_fullpath, DZStrW build);
  336.         int CD_AppendDir(const DZStrW& comp, DZStrW build);
  337.         int CD_AppendName(const DZStrW& comp, DZStrW build);
  338.  
  339.         void GiveGlobals(void);
  340. };
  341.  
  342. #endif
  343.  
  344.  
  345.