Subversion Repositories autosfx

Rev

Blame | Last modification | View Log | RSS feed

  1. //---------------------------------------------------------------------------
  2.  
  3. #ifndef ZipDfltH
  4. #define ZipDfltH
  5. /*
  6.   Copyright (c) 1990-2007 Info-ZIP.  All rights reserved.
  7.  
  8.   See the accompanying file LICENSE, version 2007-Mar-4 or later
  9.   (the contents of which are also included in zip.h) for terms of use.
  10.   If, for some reason, all these files are missing, the Info-ZIP license
  11.   also may be found at:  ftp://ftp.info-zip.org/pub/infozip/license.html
  12.  
  13.   parts Copyright (C) 1997 Mike White, Eric W. Engler
  14. ************************************************************************
  15.  Copyright (C) 2009, 2010  by Russell J. Peters, Roger Aelbrecht
  16.  
  17.    This file is part of TZipMaster Version 1.9.
  18.  
  19.     TZipMaster is free software: you can redistribute it and/or modify
  20.     it under the terms of the GNU Lesser General Public License as published by
  21.     the Free Software Foundation, either version 3 of the License, or
  22.     (at your option) any later version.
  23.  
  24.     TZipMaster is distributed in the hope that it will be useful,
  25.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  26.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  27.     GNU Lesser General Public License for more details.
  28.  
  29.     You should have received a copy of the GNU Lesser General Public License
  30.     along with TZipMaster.  If not, see <http://www.gnu.org/licenses/>.
  31.  
  32.     contact: problems@delphizip.org (include ZipMaster in the subject).
  33.     updates: http://www.delphizip.org
  34.     DelphiZip maillist subscribe at http://www.freelists.org/list/delphizip
  35. ************************************************************************/
  36.  
  37. #include "dzoper.h"
  38. #include "zipasrt.h"
  39. #include "helpers.h"
  40.  
  41. /* Set up portability
  42.  * Anything defined in tailor.h will cause the default in this
  43.  * header file to be ignored.
  44.  */
  45. #include <stdio.h>
  46.  
  47. /* MSDOS file attribute for directories */
  48. #define MSDOS_DIR_ATTR  0x10
  49.  
  50.  
  51. /* System independent replacement for "struct utimbuf",
  52.  * which is missing in many older OS environments.
  53.  */
  54.  
  55. typedef struct  ztimbuf
  56. {
  57.     time_t  actime;     /* new access time */
  58.     time_t  modtime;    /* new modification time */
  59. }ztimbuf;
  60.  
  61. #define MIN_MATCH 3
  62. #define MAX_MATCH 258
  63.  
  64. /* The minimum and maximum match lengths */
  65. // #ifndef WSIZE
  66. #define ZWSIZE (0x8000)
  67. // #endif
  68.  
  69. /* Maximum window size = 32K. If you are really short of memory, compile
  70.  * with a smaller WSIZE but this reduces the compression ratio for files
  71.  * of size > ZWSIZE. ZWSIZE must be a power of two in the current implementation.
  72.  */
  73. #define MIN_LOOKAHEAD (MAX_MATCH + MIN_MATCH + 1)
  74.  
  75. /* Minimum amount of lookahead, except at the end of the input file.
  76.  * See deflate.c for comments about the MIN_MATCH + 1.
  77.  */
  78. #define MAX_DIST  (ZWSIZE - MIN_LOOKAHEAD)
  79.  
  80. /* In order to simplify the code, particularly on 16 bit machines, match
  81.  * distances are limited to MAX_DIST instead of ZWSIZE.
  82.  */
  83. /* Forget FILENAME_MAX (incorrectly = 14 on some System V) */
  84. #define FNMAX 256
  85.  
  86. /* Lengths of headers after signatures in bytes */
  87. #define LOCHEAD 26
  88. #define CENHEAD 42
  89. #define ENDHEAD 18
  90.  
  91. /* internal file attribute */
  92. #define UNKNOWN   (-1)
  93. #define BINARY    0
  94. #define ASCII     1
  95. #define __EBCDIC  2
  96.  
  97.  
  98. //#define BEST    - 1         // Use best method (deflation or store)
  99. #define STORE   0           // Store method
  100. #define DEFLATE 8           // Deflation method
  101.  
  102. #define HASH_BITS 15    // Number of bits used to hash strings
  103. #define HASH_SIZE (unsigned)(1 << HASH_BITS)
  104. #define HASH_MASK (HASH_SIZE - 1)
  105. #define WMASK     (ZWSIZE - 1)
  106.  
  107. // HASH_SIZE and ZWSIZE must be powers of two
  108. #define NIL 0
  109.  
  110. // Tail of hash chains
  111. typedef ush Pos;
  112.  
  113. #pragma pack(push, 4)
  114. struct ct_data{
  115.     union
  116.     {
  117.         ush freq; // frequency count
  118.         ush code; // bit string
  119.     } fc;
  120.     union
  121.     {
  122.         ush dad; // father node in Huffman tree
  123.         ush len; // length of bit string
  124.     } dl;
  125. };
  126. #pragma pack(pop)
  127.  
  128. #pragma pack(push, 4)
  129. struct tree_desc
  130. {
  131.         ct_data *dyn_tree;    // the dynamic tree
  132.     ct_data *static_tree; // corresponding static tree or NULL
  133.         int *extra_bits;      // extra bits for each code or NULL
  134.         int extra_base;       // base index for extra_bits
  135.         int elems;            // max number of elements in the tree
  136.         int max_length;       // max bit length for the codes
  137.         int max_code;         // largest code with non zero frequency
  138. };
  139. #pragma pack(pop)
  140.  
  141. #define Freq          fc.freq
  142. #define Code          fc.code
  143. #define Dad           dl.dad
  144. #define Len           dl.len
  145.  
  146. #define ADD           1
  147. #define MAX_BITS      15  // All codes must not exceed MAX_BITS bits
  148. #define MAX_BL_BITS   7   // Bit length codes must not exceed MAX_BL_BITS bits
  149. #define LENGTH_CODES  29
  150. // number of length codes, not counting the special END_BLOCK code
  151. #define LITERALS      256 // number of literal bytes 0..255
  152. #define END_BLOCK     256 // end of block literal code
  153. #define L_CODES       (LITERALS + 1 + LENGTH_CODES)
  154. // number of Literal or Length codes, including the END_BLOCK code
  155. #define D_CODES       30                  // number of distance codes
  156. #define BL_CODES      19
  157. // number of codes used to transfer the bit lengths
  158. #define HEAP_SIZE     (2 * L_CODES + 1) // maximum heap size
  159. #define Max(a, b)   (a >= b ? a : b)
  160. #define REP_3_6       16
  161. #define REPZ_3_10     17
  162. #define REPZ_11_138   18
  163.  
  164. extern int      extra_lbits[LENGTH_CODES];  // extra bits for each length code
  165. extern int      extra_dbits[D_CODES];       // extra bits for each distance code
  166. extern int      extra_blbits[BL_CODES];     // extra bits for each bit length code
  167.  
  168. #ifndef LIT_BUFSIZE
  169. #define LIT_BUFSIZE 0x8000
  170. #endif
  171. #define DIST_BUFSIZE  LIT_BUFSIZE
  172.  
  173. /*************/
  174. /*  ZGlobals  */
  175. /*************/
  176.  
  177. class ZipDflt : public DZOp
  178. {
  179.  
  180.     private:
  181.         ZipDflt(void);
  182.         ZipDflt(const ZipDflt&);
  183.         ZipDflt& operator=(const ZipDflt&);
  184.  
  185.         protected:
  186.                 unsigned fcalls;        // crypt static: ensure diff. random header each time
  187.                 ulg fkeys[3];           // crypt static: keys defining pseudo-random sequence
  188.                 const char* fkey;       // crypt static: decryption password or NULL
  189. #ifdef DEBUG
  190.         unsigned fbits_sent;
  191. #endif
  192.         unsigned short fbi_buf;
  193.                 int fbi_valid;
  194.         uch f_outbuf[4096];
  195.                 uch *fin_buf;
  196.                 unsigned fin_size;
  197.         unsigned fin_offset;
  198.         unsigned fout_offset;
  199.         ulg fwindow_size;
  200.         int flevel;
  201.         int fsliding;
  202.         Pos fhead[HASH_SIZE];
  203.         Pos fprev[ZWSIZE];
  204.         int fnice_match;
  205.         unsigned fgood_match;
  206.         unsigned fstrstart;
  207.         unsigned flookahead;
  208.         unsigned fins_h;
  209.         unsigned fmatch_start;
  210.         unsigned int fmax_chain_length;
  211.         unsigned int fmax_lazy_match;
  212.         unsigned int fprev_length;
  213.         long fblock_start;
  214.                 int feofile;
  215.         DZStrW fzipfile;
  216.         int fdispose;
  217.                 HANDLE fhInz;     // handle to 'orig' zip file
  218.         ZStreamIO *fZipInfile;
  219.         ZStreamIO *fZipOutfile;
  220.  
  221.         tree_desc fl_desc;
  222.         tree_desc fd_desc;
  223.         tree_desc fbl_desc;
  224.         ct_data fdyn_ltree[HEAP_SIZE]; // literal and length tree
  225.         ct_data fdyn_dtree[2 *D_CODES + 1]; // distance tree
  226.         ct_data fstatic_ltree[L_CODES + 2];
  227.         ct_data fstatic_dtree[D_CODES];
  228.         int *fextra_bits;
  229.         int fextra_base;
  230.         ct_data fbl_tree[2 *BL_CODES + 1];
  231.         ush *ffile_type;
  232.         int *ffile_method;
  233.         int fbase_length[LENGTH_CODES];
  234.         uch flength_code[MAX_MATCH - MIN_MATCH + 1];
  235.         int fbase_dist[D_CODES];
  236.         uch fdist_code[512];
  237.         ush fbl_count[MAX_BITS + 1];
  238.         ulg fopt_len;
  239.         ulg fstatic_len;               // bit length of current block with static trees
  240.         unsigned flast_lit;            // running index in l_buf
  241.         unsigned flast_dist;           // running index in d_buf
  242.         unsigned flast_flags;          // running index in flag_buf
  243.         uch fflags;                    // current flags not yet saved in flag_buf
  244.         uch fflag_bit;                 // current bit used in flags
  245.         int fheap[2 *L_CODES + 1];     // heap used to build the Huffman trees
  246.         int fheap_len;                 // number of elements in the heap
  247.         int fheap_max;                 // element of largest frequency
  248.         uch fdepth[2 *L_CODES + 1];
  249.                 uch fflag_buf[(LIT_BUFSIZE / 8)];
  250.         uch fl_buf[LIT_BUFSIZE];       // buffer for literals/lengths
  251.                 ush fd_buf[DIST_BUFSIZE];      // buffer for distances
  252.         int fFileError;                // 1.78.6.0
  253.                 ulg fcrc;
  254. #ifdef DEBUG
  255.         ZInt64 finput_len;
  256. #endif
  257.         ZInt64 fisize;
  258.         ZInt64 fcompressed_len;
  259.                 ZInt64 fimax;
  260. #ifdef DEBUG
  261.         unsigned __int64 fToTest, fTested;
  262. #endif
  263.  
  264.     public:
  265.         uch fwindow[2L *ZWSIZE];
  266.         ZipDflt(const DllCommands *C);
  267.         ~ZipDflt(void);
  268.                 void    lm_init(int pack_level, ush *flags);
  269.         ZInt64  deflate(void);
  270.                 void    crypthead(const char *, ulg);
  271.  
  272.     protected:
  273.         unsigned __fastcall zfwrite(const uch *buf, ::extent);
  274.         int read_buf(uch *buf, unsigned size);
  275.                 int file_read(uch *buf, unsigned size);
  276.         /* in Trees.c */
  277.         void              ct_init(ush *attr, int *method);
  278.         void              bi_init(void);
  279.                 int __fastcall    ct_tally(int dist, int lc);
  280.         ZInt64 __fastcall  FlushBlock(int eof);
  281.  
  282.         /* in Bits.c */
  283.                 void __fastcall     send_bits(int value, int length);
  284.         void __fastcall     bi_windup(void);
  285.                 void __fastcall     copy_block(const uch *block, unsigned len, int header);
  286.         int __fastcall      longest_match(unsigned cur_match);
  287.  
  288.     private:
  289.         void __fastcall  flush_outbuf(unsigned w, unsigned bytes);
  290.         void init_block(void);
  291.         void __fastcall  pqdownheap(ct_data *tree, int k);
  292.         void __fastcall  gen_bitlen(tree_desc *desc);
  293.         void __fastcall  gen_codes(ct_data *tree, int max_code);
  294.         void __fastcall  build_tree(tree_desc *desc);
  295.         void __fastcall  scan_tree(ct_data *tree, int max_code);
  296.         void __fastcall  send_tree(ct_data *tree, int max_code);
  297.         int __fastcall   build_bl_tree(void);
  298.         void             send_all_trees(int lcodes, int dcodes, int blcodes);
  299.         void             compress_block(ct_data *ltree, ct_data *dtree);
  300.         void             set_file_type(void);
  301.         ZInt64          deflate_fast(void);
  302.         void fill_window(void);
  303. #ifdef _DEBUG
  304.         void check_match(unsigned start, unsigned match, int length);
  305. #endif
  306. };
  307.  
  308. unsigned __fastcall bi_reverse(unsigned value, int length);
  309.  
  310. #endif
  311.  
  312.  
  313.  
  314.  
  315.