home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / sources / misc / 3845 < prev    next >
Encoding:
Text File  |  1992-08-22  |  60.1 KB  |  1,678 lines

  1. Newsgroups: comp.sources.misc
  2. Path: sparky!kent
  3. From: zip-bugs@cs.ucla.edu (Info-ZIP group)
  4. Subject:  v31i095:  zip19 - Info-ZIP portable Zip, version 1.9, Part03/11
  5. Message-ID: <1992Aug23.064551.29045@sparky.imd.sterling.com>
  6. Followup-To: comp.sources.d
  7. X-Md4-Signature: 5ca732e6f20f0ad06e2b983530c09eca
  8. Sender: kent@sparky.imd.sterling.com (Kent Landfield)
  9. Organization: Sterling Software
  10. References: <csm-v31i093=zip19.014410@sparky.IMD.Sterling.COM>
  11. Date: Sun, 23 Aug 1992 06:45:51 GMT
  12. Approved: kent@sparky.imd.sterling.com
  13. Lines: 1663
  14.  
  15. Submitted-by: zip-bugs@cs.ucla.edu (Info-ZIP group)
  16. Posting-number: Volume 31, Issue 95
  17. Archive-name: zip19/part03
  18. Supersedes: zip: Volume 23, Issue 88-96
  19. Environment: UNIX, VMS, OS/2, MS-DOS, MACINTOSH, WIN-NT, LINUX, MINIX, XOS, !AMIGA, ATARI, symlink, SGI, DEC, Cray, Convex, Amdahl, Sun, PC
  20.  
  21. #! /bin/sh
  22. # This is a shell archive.  Remove anything before this line, then feed it
  23. # into a shell via "sh file" or similar.  To overwrite existing files,
  24. # type "sh file -c".
  25. # The tool that generated this appeared in the comp.sources.unix newsgroup;
  26. # send mail to comp-sources-unix@uunet.uu.net if you want that tool.
  27. # Contents:  trees.c util.c vms/makefile.vms
  28. # Wrapped by kent@sparky on Sun Aug 23 01:00:43 1992
  29. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  30. echo If this archive is complete, you will see the following message:
  31. echo '          "shar: End of archive 3 (of 11)."'
  32. if test -f 'trees.c' -a "${1}" != "-c" ; then 
  33.   echo shar: Will not clobber existing file \"'trees.c'\"
  34. else
  35.   echo shar: Extracting \"'trees.c'\" \(40777 characters\)
  36.   sed "s/^X//" >'trees.c' <<'END_OF_FILE'
  37. X/*
  38. X
  39. X Copyright (C) 1990-1992 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  40. X Kai Uwe Rommel and Igor Mandrichenko.
  41. X Permission is granted to any individual or institution to use, copy, or
  42. X redistribute this software so long as all of the original files are included
  43. X unmodified, that it is not sold for profit, and that this copyright notice
  44. X is retained.
  45. X
  46. X*/
  47. X
  48. X/*
  49. X *  trees.c by Jean-loup Gailly
  50. X *
  51. X *  This is a new version of im_ctree.c originally written by Richard B. Wales
  52. X *  for the defunct implosion method.
  53. X *
  54. X *  PURPOSE
  55. X *
  56. X *      Encode various sets of source values using variable-length
  57. X *      binary code trees.
  58. X *
  59. X *  DISCUSSION
  60. X *
  61. X *      The PKZIP "deflation" process uses several Huffman trees. The more
  62. X *      common source values are represented by shorter bit sequences.
  63. X *
  64. X *      Each code tree is stored in the ZIP file in a compressed form
  65. X *      which is itself a Huffman encoding of the lengths of
  66. X *      all the code strings (in ascending order by source values).
  67. X *      The actual code strings are reconstructed from the lengths in
  68. X *      the UNZIP process, as described in the "application note"
  69. X *      (APPNOTE.TXT) distributed as part of PKWARE's PKZIP program.
  70. X *
  71. X *  REFERENCES
  72. X *
  73. X *      Lynch, Thomas J.
  74. X *          Data Compression:  Techniques and Applications, pp. 53-55.
  75. X *          Lifetime Learning Publications, 1985.  ISBN 0-534-03418-7.
  76. X *
  77. X *      Storer, James A.
  78. X *          Data Compression:  Methods and Theory, pp. 49-50.
  79. X *          Computer Science Press, 1988.  ISBN 0-7167-8156-5.
  80. X *
  81. X *      Sedgewick, R.
  82. X *          Algorithms, p290.
  83. X *          Addison-Wesley, 1983. ISBN 0-201-06672-6.
  84. X *
  85. X *  INTERFACE
  86. X *
  87. X *      void ct_init (ush *attr, int *method)
  88. X *          Allocate the match buffer, initialize the various tables and save
  89. X *          the location of the internal file attribute (ascii/binary) and
  90. X *          method (DEFLATE/STORE)
  91. X *
  92. X *      void ct_tally (int dist, int lc);
  93. X *          Save the match info and tally the frequency counts.
  94. X *
  95. X *      long flush_block (char *buf, ulg stored_len, int eof)
  96. X *          Determine the best encoding for the current block: dynamic trees,
  97. X *          static trees or store, and output the encoded block to the zip
  98. X *          file. Returns the total compressed length for the file so far.
  99. X *
  100. X */
  101. X
  102. X#include <ctype.h>
  103. X#include "zip.h"
  104. X
  105. X/* ===========================================================================
  106. X * Constants
  107. X */
  108. X
  109. X#define MAX_BITS 15
  110. X/* All codes must not exceed MAX_BITS bits */
  111. X
  112. X#define MAX_BL_BITS 7
  113. X/* Bit length codes must not exceed MAX_BL_BITS bits */
  114. X
  115. X#define LENGTH_CODES 29
  116. X/* number of length codes, not counting the special END_BLOCK code */
  117. X
  118. X#define LITERALS  256
  119. X/* number of literal bytes 0..255 */
  120. X
  121. X#define END_BLOCK 256
  122. X/* end of block literal code */
  123. X
  124. X#define L_CODES (LITERALS+1+LENGTH_CODES)
  125. X/* number of Literal or Length codes, including the END_BLOCK code */
  126. X
  127. X#define D_CODES   30
  128. X/* number of distance codes */
  129. X
  130. X#define BL_CODES  19
  131. X/* number of codes used to transfer the bit lengths */
  132. X
  133. X
  134. Xlocal int near extra_lbits[LENGTH_CODES] /* extra bits for each length code */
  135. X   = {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};
  136. X
  137. Xlocal int near extra_dbits[D_CODES] /* extra bits for each distance code */
  138. X   = {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};
  139. X
  140. Xlocal int near extra_blbits[BL_CODES]/* extra bits for each bit length code */
  141. X   = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
  142. X
  143. X#define STORED_BLOCK 0
  144. X#define STATIC_TREES 1
  145. X#define DYN_TREES    2
  146. X/* The three kinds of block type */
  147. X
  148. X#ifndef LIT_BUFSIZE
  149. X#  ifdef SMALL_MEM
  150. X#    define LIT_BUFSIZE  0x2000
  151. X#  else
  152. X#  ifdef MEDIUM_MEM
  153. X#    define LIT_BUFSIZE  0x4000
  154. X#  else
  155. X#    define LIT_BUFSIZE  0x8000
  156. X#  endif
  157. X#  endif
  158. X#endif
  159. X#define DIST_BUFSIZE  LIT_BUFSIZE
  160. X/* Sizes of match buffers for literals/lengths and distances.  There are
  161. X * 4 reasons for limiting LIT_BUFSIZE to 64K:
  162. X *   - frequencies can be kept in 16 bit counters
  163. X *   - if compression is not successful for the first block, all input data is
  164. X *     still in the window so we can still emit a stored block even when input
  165. X *     comes from standard input.  (This can also be done for all blocks if
  166. X *     LIT_BUFSIZE is not greater than 32K.)
  167. X *   - if compression is not successful for a file smaller than 64K, we can
  168. X *     even emit a stored file instead of a stored block (saving 5 bytes).
  169. X *   - creating new Huffman trees less frequently may not provide fast
  170. X *     adaptation to changes in the input data statistics. (Take for
  171. X *     example a binary file with poorly compressible code followed by
  172. X *     a highly compressible string table.) Smaller buffer sizes give
  173. X *     fast adaptation but have of course the overhead of transmitting trees
  174. X *     more frequently.
  175. X *   - I can't count above 4
  176. X * The current code is general and allows DIST_BUFSIZE < LIT_BUFSIZE (to save
  177. X * memory at the expense of compression). Some optimizations would be possible
  178. X * if we rely on DIST_BUFSIZE == LIT_BUFSIZE.
  179. X */
  180. X
  181. X#define REP_3_6      16
  182. X/* repeat previous bit length 3-6 times (2 bits of repeat count) */
  183. X
  184. X#define REPZ_3_10    17
  185. X/* repeat a zero length 3-10 times  (3 bits of repeat count) */
  186. X
  187. X#define REPZ_11_138  18
  188. X/* repeat a zero length 11-138 times  (7 bits of repeat count) */
  189. X
  190. X/* ===========================================================================
  191. X * Local data
  192. X */
  193. X
  194. X/* Data structure describing a single value and its code string. */
  195. Xtypedef struct ct_data {
  196. X    union {
  197. X        ush  freq;       /* frequency count */
  198. X        ush  code;       /* bit string */
  199. X    } fc;
  200. X    union {
  201. X        ush  dad;        /* father node in Huffman tree */
  202. X        ush  len;        /* length of bit string */
  203. X    } dl;
  204. X} ct_data;
  205. X
  206. X#define Freq fc.freq
  207. X#define Code fc.code
  208. X#define Dad  dl.dad
  209. X#define Len  dl.len
  210. X
  211. X#define HEAP_SIZE (2*L_CODES+1)
  212. X/* maximum heap size */
  213. X
  214. Xlocal ct_data near dyn_ltree[HEAP_SIZE];   /* literal and length tree */
  215. Xlocal ct_data near dyn_dtree[2*D_CODES+1]; /* distance tree */
  216. X
  217. Xlocal ct_data near static_ltree[L_CODES+2];
  218. X/* The static literal tree. Since the bit lengths are imposed, there is no
  219. X * need for the L_CODES extra codes used during heap construction. However
  220. X * The codes 286 and 287 are needed to build a canonical tree (see ct_init
  221. X * below).
  222. X */
  223. X
  224. Xlocal ct_data near static_dtree[D_CODES];
  225. X/* The static distance tree. (Actually a trivial tree since all codes use
  226. X * 5 bits.)
  227. X */
  228. X
  229. Xlocal ct_data near bl_tree[2*BL_CODES+1];
  230. X/* Huffman tree for the bit lengths */
  231. X
  232. Xtypedef struct tree_desc {
  233. X    ct_data near *dyn_tree;      /* the dynamic tree */
  234. X    ct_data near *static_tree;   /* corresponding static tree or NULL */
  235. X    int     near *extra_bits;    /* extra bits for each code or NULL */
  236. X    int     extra_base;          /* base index for extra_bits */
  237. X    int     elems;               /* max number of elements in the tree */
  238. X    int     max_length;          /* max bit length for the codes */
  239. X    int     max_code;            /* largest code with non zero frequency */
  240. X} tree_desc;
  241. X
  242. Xlocal tree_desc near l_desc =
  243. X{dyn_ltree, static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS, 0};
  244. X
  245. Xlocal tree_desc near d_desc =
  246. X{dyn_dtree, static_dtree, extra_dbits, 0,          D_CODES, MAX_BITS, 0};
  247. X
  248. Xlocal tree_desc near bl_desc =
  249. X{bl_tree, NULL,       extra_blbits, 0,         BL_CODES, MAX_BL_BITS, 0};
  250. X
  251. X
  252. Xlocal ush near bl_count[MAX_BITS+1];
  253. X/* number of codes at each bit length for an optimal tree */
  254. X
  255. Xlocal uch near bl_order[BL_CODES]
  256. X   = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
  257. X/* The lengths of the bit length codes are sent in order of decreasing
  258. X * probability, to avoid transmitting the lengths for unused bit length codes.
  259. X */
  260. X
  261. Xlocal int near heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
  262. Xlocal int heap_len;               /* number of elements in the heap */
  263. Xlocal int heap_max;               /* element of largest frequency */
  264. X/* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
  265. X * The same heap array is used to build all trees.
  266. X */
  267. X
  268. Xlocal uch near depth[2*L_CODES+1];
  269. X/* Depth of each subtree used as tie breaker for trees of equal frequency */
  270. X
  271. Xlocal uch length_code[MAX_MATCH-MIN_MATCH+1];
  272. X/* length code for each normalized match length (0 == MIN_MATCH) */
  273. X
  274. Xlocal uch dist_code[512];
  275. X/* distance codes. The first 256 values correspond to the distances
  276. X * 3 .. 258, the last 256 values correspond to the top 8 bits of
  277. X * the 15 bit distances.
  278. X */
  279. X
  280. Xlocal int near base_length[LENGTH_CODES];
  281. X/* First normalized length for each code (0 = MIN_MATCH) */
  282. X
  283. Xlocal int near base_dist[D_CODES];
  284. X/* First normalized distance for each code (0 = distance of 1) */
  285. X
  286. X#ifndef DYN_ALLOC
  287. X  local uch far l_buf[LIT_BUFSIZE];  /* buffer for literals/lengths */
  288. X  local ush far d_buf[DIST_BUFSIZE]; /* buffer for distances */
  289. X#else
  290. X  local uch far *l_buf;
  291. X  local ush far *d_buf;
  292. X#endif
  293. X
  294. Xlocal uch near flag_buf[(LIT_BUFSIZE/8)];
  295. X/* flag_buf is a bit array distinguishing literals from lengths in
  296. X * l_buf, and thus indicating the presence or absence of a distance.
  297. X */
  298. X
  299. Xlocal unsigned last_lit;    /* running index in l_buf */
  300. Xlocal unsigned last_dist;   /* running index in d_buf */
  301. Xlocal unsigned last_flags;  /* running index in flag_buf */
  302. Xlocal uch flags;            /* current flags not yet saved in flag_buf */
  303. Xlocal uch flag_bit;         /* current bit used in flags */
  304. X/* bits are filled in flags starting at bit 0 (least significant).
  305. X * Note: these flags are overkill in the current code since we don't
  306. X * take advantage of DIST_BUFSIZE == LIT_BUFSIZE.
  307. X */
  308. X
  309. Xlocal ulg opt_len;        /* bit length of current block with optimal trees */
  310. Xlocal ulg static_len;     /* bit length of current block with static trees */
  311. X
  312. Xlocal ulg compressed_len; /* total bit length of compressed file */
  313. X
  314. Xlocal ulg input_len;      /* total byte length of input file */
  315. X/* input_len is for debugging only since we can get it by other means. */
  316. X
  317. Xush *file_type;        /* pointer to UNKNOWN, BINARY or ASCII */
  318. Xint *file_method;      /* pointer to DEFLATE or STORE */
  319. X
  320. X#ifdef DEBUG
  321. Xextern ulg bits_sent;  /* bit length of the compressed data */
  322. Xextern ulg isize;      /* byte length of input file */
  323. X#endif
  324. X
  325. Xextern long block_start;       /* window offset of current block */
  326. Xextern unsigned near strstart; /* window offset of current string */
  327. X
  328. X/* ===========================================================================
  329. X * Local (static) routines in this file.
  330. X */
  331. X
  332. Xlocal void init_block     OF((void));
  333. Xlocal void pqdownheap     OF((ct_data near *tree, int k));
  334. Xlocal void gen_bitlen     OF((tree_desc near *desc));
  335. Xlocal void gen_codes      OF((ct_data near *tree, int max_code));
  336. Xlocal void build_tree     OF((tree_desc near *desc));
  337. Xlocal void scan_tree      OF((ct_data near *tree, int max_code));
  338. Xlocal void send_tree      OF((ct_data near *tree, int max_code));
  339. Xlocal int  build_bl_tree  OF((void));
  340. Xlocal void send_all_trees OF((int lcodes, int dcodes, int blcodes));
  341. Xlocal void compress_block OF((ct_data near *ltree, ct_data near *dtree));
  342. Xlocal void set_file_type  OF((void));
  343. X
  344. X
  345. X#ifndef DEBUG
  346. X#  define send_code(c, tree) send_bits(tree[c].Code, tree[c].Len)
  347. X   /* Send a code of the given tree. c and tree must not have side effects */
  348. X
  349. X#else /* DEBUG */
  350. X#  define send_code(c, tree) \
  351. X     { if (verbose>1) fprintf(stderr,"\ncd %3d ",(c)); \
  352. X       send_bits(tree[c].Code, tree[c].Len); }
  353. X#endif
  354. X
  355. X#define d_code(dist) \
  356. X   ((dist) < 256 ? dist_code[dist] : dist_code[256+((dist)>>7)])
  357. X/* Mapping from a distance to a distance code. dist is the distance - 1 and
  358. X * must not have side effects. dist_code[256] and dist_code[257] are never
  359. X * used.
  360. X */
  361. X
  362. X#define MAX(a,b) (a >= b ? a : b)
  363. X/* the arguments must not have side effects */
  364. X
  365. X/* ===========================================================================
  366. X * Allocate the match buffer, initialize the various tables and save the
  367. X * location of the internal file attribute (ascii/binary) and method
  368. X * (DEFLATE/STORE).
  369. X */
  370. Xvoid ct_init(attr, method)
  371. X    ush  *attr;   /* pointer to internal file attribute */
  372. X    int  *method; /* pointer to compression method */
  373. X{
  374. X    int n;        /* iterates over tree elements */
  375. X    int bits;     /* bit counter */
  376. X    int length;   /* length value */
  377. X    int code;     /* code value */
  378. X    int dist;     /* distance index */
  379. X
  380. X    file_type = attr;
  381. X    file_method = method;
  382. X    compressed_len = input_len = 0L;
  383. X        
  384. X    if (static_dtree[0].Len != 0) return; /* ct_init already called */
  385. X
  386. X#ifdef DYN_ALLOC
  387. X    d_buf = (ush far*) fcalloc(DIST_BUFSIZE, sizeof(ush));
  388. X    l_buf = (uch far*) fcalloc(LIT_BUFSIZE/2, 2);
  389. X    /* Avoid using the value 64K on 16 bit machines */
  390. X    if (l_buf == NULL || d_buf == NULL) error("ct_init: out of memory");
  391. X#endif
  392. X
  393. X    /* Initialize the mapping length (0..255) -> length code (0..28) */
  394. X    length = 0;
  395. X    for (code = 0; code < LENGTH_CODES-1; code++) {
  396. X        base_length[code] = length;
  397. X        for (n = 0; n < (1<<extra_lbits[code]); n++) {
  398. X            length_code[length++] = (uch)code;
  399. X        }
  400. X    }
  401. X    Assert (length == 256, "ct_init: length != 256");
  402. X    /* Note that the length 255 (match length 258) can be represented
  403. X     * in two different ways: code 284 + 5 bits or code 285, so we
  404. X     * overwrite length_code[255] to use the best encoding:
  405. X     */
  406. X    length_code[length-1] = (uch)code;
  407. X
  408. X    /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
  409. X    dist = 0;
  410. X    for (code = 0 ; code < 16; code++) {
  411. X        base_dist[code] = dist;
  412. X        for (n = 0; n < (1<<extra_dbits[code]); n++) {
  413. X            dist_code[dist++] = (uch)code;
  414. X        }
  415. X    }
  416. X    Assert (dist == 256, "ct_init: dist != 256");
  417. X    dist >>= 7; /* from now on, all distances are divided by 128 */
  418. X    for ( ; code < D_CODES; code++) {
  419. X        base_dist[code] = dist << 7;
  420. X        for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
  421. X            dist_code[256 + dist++] = (uch)code;
  422. X        }
  423. X    }
  424. X    Assert (dist == 256, "ct_init: 256+dist != 512");
  425. X
  426. X    /* Construct the codes of the static literal tree */
  427. X    for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
  428. X    n = 0;
  429. X    while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
  430. X    while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
  431. X    while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
  432. X    while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
  433. X    /* Codes 286 and 287 do not exist, but we must include them in the
  434. X     * tree construction to get a canonical Huffman tree (longest code
  435. X     * all ones)
  436. X     */
  437. X    gen_codes(static_ltree, L_CODES+1);
  438. X
  439. X    /* The static distance tree is trivial: */
  440. X    for (n = 0; n < D_CODES; n++) {
  441. X        static_dtree[n].Len = 5;
  442. X        static_dtree[n].Code = bi_reverse(n, 5);
  443. X    }
  444. X
  445. X    /* Initialize the first block of the first file: */
  446. X    init_block();
  447. X}
  448. X
  449. X/* ===========================================================================
  450. X * Initialize a new block.
  451. X */
  452. Xlocal void init_block()
  453. X{
  454. X    int n; /* iterates over tree elements */
  455. X
  456. X    /* Initialize the trees. */
  457. X    for (n = 0; n < L_CODES;  n++) dyn_ltree[n].Freq = 0;
  458. X    for (n = 0; n < D_CODES;  n++) dyn_dtree[n].Freq = 0;
  459. X    for (n = 0; n < BL_CODES; n++) bl_tree[n].Freq = 0;
  460. X
  461. X    dyn_ltree[END_BLOCK].Freq = 1;
  462. X    opt_len = static_len = 0L;
  463. X    last_lit = last_dist = last_flags = 0;
  464. X    flags = 0; flag_bit = 1;
  465. X}
  466. X
  467. X#define SMALLEST 1
  468. X/* Index within the heap array of least frequent node in the Huffman tree */
  469. X
  470. X
  471. X/* ===========================================================================
  472. X * Remove the smallest element from the heap and recreate the heap with
  473. X * one less element. Updates heap and heap_len.
  474. X */
  475. X#define pqremove(tree, top) \
  476. X{\
  477. X    top = heap[SMALLEST]; \
  478. X    heap[SMALLEST] = heap[heap_len--]; \
  479. X    pqdownheap(tree, SMALLEST); \
  480. X}
  481. X
  482. X/* ===========================================================================
  483. X * Compares to subtrees, using the tree depth as tie breaker when
  484. X * the subtrees have equal frequency. This minimizes the worst case length.
  485. X */
  486. X#define smaller(tree, n, m) \
  487. X   (tree[n].Freq < tree[m].Freq || \
  488. X   (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
  489. X
  490. X/* ===========================================================================
  491. X * Restore the heap property by moving down the tree starting at node k,
  492. X * exchanging a node with the smallest of its two sons if necessary, stopping
  493. X * when the heap property is re-established (each father smaller than its
  494. X * two sons).
  495. X */
  496. Xlocal void pqdownheap(tree, k)
  497. X    ct_data near *tree;  /* the tree to restore */
  498. X    int k;               /* node to move down */
  499. X{
  500. X    int v = heap[k];
  501. X    int j = k << 1;  /* left son of k */
  502. X    while (j <= heap_len) {
  503. X        /* Set j to the smallest of the two sons: */
  504. X        if (j < heap_len && smaller(tree, heap[j+1], heap[j])) j++;
  505. X
  506. X        /* Exit if v is smaller than both sons */
  507. X        if (smaller(tree, v, heap[j])) break;
  508. X
  509. X        /* Exchange v with the smallest son */
  510. X        heap[k] = heap[j],  k = j;
  511. X
  512. X        /* And continue down the tree, setting j to the left son of k */
  513. X        j <<= 1;
  514. X    }
  515. X    heap[k] = v;
  516. X}
  517. X
  518. X/* ===========================================================================
  519. X * Compute the optimal bit lengths for a tree and update the total bit length
  520. X * for the current block.
  521. X * IN assertion: the fields freq and dad are set, heap[heap_max] and
  522. X *    above are the tree nodes sorted by increasing frequency.
  523. X * OUT assertions: the field len is set to the optimal bit length, the
  524. X *     array bl_count contains the frequencies for each bit length.
  525. X *     The length opt_len is updated; static_len is also updated if stree is
  526. X *     not null.
  527. X */
  528. Xlocal void gen_bitlen(desc)
  529. X    tree_desc near *desc; /* the tree descriptor */
  530. X{
  531. X    ct_data near *tree  = desc->dyn_tree;
  532. X    int near *extra     = desc->extra_bits;
  533. X    int base            = desc->extra_base;
  534. X    int max_code        = desc->max_code;
  535. X    int max_length      = desc->max_length;
  536. X    ct_data near *stree = desc->static_tree;
  537. X    int h;              /* heap index */
  538. X    int n, m;           /* iterate over the tree elements */
  539. X    int bits;           /* bit length */
  540. X    int xbits;          /* extra bits */
  541. X    ush f;              /* frequency */
  542. X    int overflow = 0;   /* number of elements with bit length too large */
  543. X
  544. X    for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
  545. X
  546. X    /* In a first pass, compute the optimal bit lengths (which may
  547. X     * overflow in the case of the bit length tree).
  548. X     */
  549. X    tree[heap[heap_max]].Len = 0; /* root of the heap */
  550. X
  551. X    for (h = heap_max+1; h < HEAP_SIZE; h++) {
  552. X        n = heap[h];
  553. X        bits = tree[tree[n].Dad].Len + 1;
  554. X        if (bits > max_length) bits = max_length, overflow++;
  555. X        tree[n].Len = bits;
  556. X        /* We overwrite tree[n].Dad which is no longer needed */
  557. X
  558. X        if (n > max_code) continue; /* not a leaf node */
  559. X
  560. X        bl_count[bits]++;
  561. X        xbits = 0;
  562. X        if (n >= base) xbits = extra[n-base];
  563. X        f = tree[n].Freq;
  564. X        opt_len += (ulg)f * (bits + xbits);
  565. X        if (stree) static_len += (ulg)f * (stree[n].Len + xbits);
  566. X    }
  567. X    if (overflow == 0) return;
  568. X
  569. X    Trace((stderr,"\nbit length overflow\n"));
  570. X    /* This happens for example on obj2 and pic of the Calgary corpus */
  571. X
  572. X    /* Find the first bit length which could increase: */
  573. X    do {
  574. X        bits = max_length-1;
  575. X        while (bl_count[bits] == 0) bits--;
  576. X        bl_count[bits]--;      /* move one leaf down the tree */
  577. X        bl_count[bits+1] += 2; /* move one overflow item as its brother */
  578. X        bl_count[max_length]--;
  579. X        /* The brother of the overflow item also moves one step up,
  580. X         * but this does not affect bl_count[max_length]
  581. X         */
  582. X        overflow -= 2;
  583. X    } while (overflow > 0);
  584. X
  585. X    /* Now recompute all bit lengths, scanning in increasing frequency.
  586. X     * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
  587. X     * lengths instead of fixing only the wrong ones. This idea is taken
  588. X     * from 'ar' written by Haruhiko Okumura.)
  589. X     */
  590. X    for (bits = max_length; bits != 0; bits--) {
  591. X        n = bl_count[bits];
  592. X        while (n != 0) {
  593. X            m = heap[--h];
  594. X            if (m > max_code) continue;
  595. X            if (tree[m].Len != (unsigned) bits) {
  596. X                Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
  597. X                opt_len += ((long)bits-(long)tree[m].Len)*(long)tree[m].Freq;
  598. X                tree[m].Len = bits;
  599. X            }
  600. X            n--;
  601. X        }
  602. X    }
  603. X}
  604. X
  605. X/* ===========================================================================
  606. X * Generate the codes for a given tree and bit counts (which need not be
  607. X * optimal).
  608. X * IN assertion: the array bl_count contains the bit length statistics for
  609. X * the given tree and the field len is set for all tree elements.
  610. X * OUT assertion: the field code is set for all tree elements of non
  611. X *     zero code length.
  612. X */
  613. Xlocal void gen_codes (tree, max_code)
  614. X    ct_data near *tree;        /* the tree to decorate */
  615. X    int max_code;              /* largest code with non zero frequency */
  616. X{
  617. X    ush next_code[MAX_BITS+1]; /* next code value for each bit length */
  618. X    ush code = 0;              /* running code value */
  619. X    int bits;                  /* bit index */
  620. X    int n;                     /* code index */
  621. X
  622. X    /* The distribution counts are first used to generate the code values
  623. X     * without bit reversal.
  624. X     */
  625. X    for (bits = 1; bits <= MAX_BITS; bits++) {
  626. X        next_code[bits] = code = (code + bl_count[bits-1]) << 1;
  627. X    }
  628. X    /* Check that the bit counts in bl_count are consistent. The last code
  629. X     * must be all ones.
  630. X     */
  631. X    Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
  632. X            "inconsistent bit counts");
  633. X    Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
  634. X
  635. X    for (n = 0;  n <= max_code; n++) {
  636. X        int len = tree[n].Len;
  637. X        if (len == 0) continue;
  638. X        /* Now reverse the bits */
  639. X        tree[n].Code = bi_reverse(next_code[len]++, len);
  640. X
  641. X        Tracec(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
  642. X             n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
  643. X    }
  644. X}
  645. X
  646. X/* ===========================================================================
  647. X * Construct one Huffman tree and assigns the code bit strings and lengths.
  648. X * Update the total bit length for the current block.
  649. X * IN assertion: the field freq is set for all tree elements.
  650. X * OUT assertions: the fields len and code are set to the optimal bit length
  651. X *     and corresponding code. The length opt_len is updated; static_len is
  652. X *     also updated if stree is not null. The field max_code is set.
  653. X */
  654. Xlocal void build_tree(desc)
  655. X    tree_desc near *desc; /* the tree descriptor */
  656. X{
  657. X    ct_data near *tree   = desc->dyn_tree;
  658. X    ct_data near *stree  = desc->static_tree;
  659. X    int elems            = desc->elems;
  660. X    int n, m;          /* iterate over heap elements */
  661. X    int max_code = -1; /* largest code with non zero frequency */
  662. X    int node = elems;  /* next internal node of the tree */
  663. X
  664. X    /* Construct the initial heap, with least frequent element in
  665. X     * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
  666. X     * heap[0] is not used.
  667. X     */
  668. X    heap_len = 0, heap_max = HEAP_SIZE;
  669. X
  670. X    for (n = 0; n < elems; n++) {
  671. X        if (tree[n].Freq != 0) {
  672. X            heap[++heap_len] = max_code = n;
  673. X            depth[n] = 0;
  674. X        } else {
  675. X            tree[n].Len = 0;
  676. X        }
  677. X    }
  678. X
  679. X    /* The pkzip format requires that at least one distance code exists,
  680. X     * and that at least one bit should be sent even if there is only one
  681. X     * possible code. So to avoid special checks later on we force at least
  682. X     * two codes of non zero frequency.
  683. X     */
  684. X    while (heap_len < 2) {
  685. X        int new = heap[++heap_len] = (max_code < 2 ? ++max_code : 0);
  686. X        tree[new].Freq = 1;
  687. X        depth[new] = 0;
  688. X        opt_len--; if (stree) static_len -= stree[new].Len;
  689. X        /* new is 0 or 1 so it does not have extra bits */
  690. X    }
  691. X    desc->max_code = max_code;
  692. X
  693. X    /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
  694. X     * establish sub-heaps of increasing lengths:
  695. X     */
  696. X    for (n = heap_len/2; n >= 1; n--) pqdownheap(tree, n);
  697. X
  698. X    /* Construct the Huffman tree by repeatedly combining the least two
  699. X     * frequent nodes.
  700. X     */
  701. X    do {
  702. X        pqremove(tree, n);   /* n = node of least frequency */
  703. X        m = heap[SMALLEST];  /* m = node of next least frequency */
  704. X
  705. X        heap[--heap_max] = n; /* keep the nodes sorted by frequency */
  706. X        heap[--heap_max] = m;
  707. X
  708. X        /* Create a new node father of n and m */
  709. X        tree[node].Freq = tree[n].Freq + tree[m].Freq;
  710. X        depth[node] = (uch) (MAX(depth[n], depth[m]) + 1);
  711. X        tree[n].Dad = tree[m].Dad = node;
  712. X#ifdef DUMP_BL_TREE
  713. X        if (tree == bl_tree) {
  714. X            fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
  715. X                    node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
  716. X        }
  717. X#endif
  718. X        /* and insert the new node in the heap */
  719. X        heap[SMALLEST] = node++;
  720. X        pqdownheap(tree, SMALLEST);
  721. X
  722. X    } while (heap_len >= 2);
  723. X
  724. X    heap[--heap_max] = heap[SMALLEST];
  725. X
  726. X    /* At this point, the fields freq and dad are set. We can now
  727. X     * generate the bit lengths.
  728. X     */
  729. X    gen_bitlen(desc);
  730. X
  731. X    /* The field len is now set, we can generate the bit codes */
  732. X    gen_codes (tree, max_code);
  733. X}
  734. X
  735. X/* ===========================================================================
  736. X * Scan a literal or distance tree to determine the frequencies of the codes
  737. X * in the bit length tree. Updates opt_len to take into account the repeat
  738. X * counts. (The contribution of the bit length codes will be added later
  739. X * during the construction of bl_tree.)
  740. X */
  741. Xlocal void scan_tree (tree, max_code)
  742. X    ct_data near *tree; /* the tree to be scanned */
  743. X    int max_code;       /* and its largest code of non zero frequency */
  744. X{
  745. X    int n;                     /* iterates over all tree elements */
  746. X    int prevlen = -1;          /* last emitted length */
  747. X    int curlen;                /* length of current code */
  748. X    int nextlen = tree[0].Len; /* length of next code */
  749. X    int count = 0;             /* repeat count of the current code */
  750. X    int max_count = 7;         /* max repeat count */
  751. X    int min_count = 4;         /* min repeat count */
  752. X
  753. X    if (nextlen == 0) max_count = 138, min_count = 3;
  754. X    tree[max_code+1].Len = (ush)-1; /* guard */
  755. X
  756. X    for (n = 0; n <= max_code; n++) {
  757. X        curlen = nextlen; nextlen = tree[n+1].Len;
  758. X        if (++count < max_count && curlen == nextlen) {
  759. X            continue;
  760. X        } else if (count < min_count) {
  761. X            bl_tree[curlen].Freq += count;
  762. X        } else if (curlen != 0) {
  763. X            if (curlen != prevlen) bl_tree[curlen].Freq++;
  764. X            bl_tree[REP_3_6].Freq++;
  765. X        } else if (count <= 10) {
  766. X            bl_tree[REPZ_3_10].Freq++;
  767. X        } else {
  768. X            bl_tree[REPZ_11_138].Freq++;
  769. X        }
  770. X        count = 0; prevlen = curlen;
  771. X        if (nextlen == 0) {
  772. X            max_count = 138, min_count = 3;
  773. X        } else if (curlen == nextlen) {
  774. X            max_count = 6, min_count = 3;
  775. X        } else {
  776. X            max_count = 7, min_count = 4;
  777. X        }
  778. X    }
  779. X}
  780. X
  781. X/* ===========================================================================
  782. X * Send a literal or distance tree in compressed form, using the codes in
  783. X * bl_tree.
  784. X */
  785. Xlocal void send_tree (tree, max_code)
  786. X    ct_data near *tree; /* the tree to be scanned */
  787. X    int max_code;       /* and its largest code of non zero frequency */
  788. X{
  789. X    int n;                     /* iterates over all tree elements */
  790. X    int prevlen = -1;          /* last emitted length */
  791. X    int curlen;                /* length of current code */
  792. X    int nextlen = tree[0].Len; /* length of next code */
  793. X    int count = 0;             /* repeat count of the current code */
  794. X    int max_count = 7;         /* max repeat count */
  795. X    int min_count = 4;         /* min repeat count */
  796. X
  797. X    /* tree[max_code+1].Len = -1; */  /* guard already set */
  798. X    if (nextlen == 0) max_count = 138, min_count = 3;
  799. X
  800. X    for (n = 0; n <= max_code; n++) {
  801. X        curlen = nextlen; nextlen = tree[n+1].Len;
  802. X        if (++count < max_count && curlen == nextlen) {
  803. X            continue;
  804. X        } else if (count < min_count) {
  805. X            do { send_code(curlen, bl_tree); } while (--count != 0);
  806. X
  807. X        } else if (curlen != 0) {
  808. X            if (curlen != prevlen) {
  809. X                send_code(curlen, bl_tree); count--;
  810. X            }
  811. X            Assert(count >= 3 && count <= 6, " 3_6?");
  812. X            send_code(REP_3_6, bl_tree); send_bits(count-3, 2);
  813. X
  814. X        } else if (count <= 10) {
  815. X            send_code(REPZ_3_10, bl_tree); send_bits(count-3, 3);
  816. X
  817. X        } else {
  818. X            send_code(REPZ_11_138, bl_tree); send_bits(count-11, 7);
  819. X        }
  820. X        count = 0; prevlen = curlen;
  821. X        if (nextlen == 0) {
  822. X            max_count = 138, min_count = 3;
  823. X        } else if (curlen == nextlen) {
  824. X            max_count = 6, min_count = 3;
  825. X        } else {
  826. X            max_count = 7, min_count = 4;
  827. X        }
  828. X    }
  829. X}
  830. X
  831. X/* ===========================================================================
  832. X * Construct the Huffman tree for the bit lengths and return the index in
  833. X * bl_order of the last bit length code to send.
  834. X */
  835. Xlocal int build_bl_tree()
  836. X{
  837. X    int max_blindex;  /* index of last bit length code of non zero freq */
  838. X
  839. X    /* Determine the bit length frequencies for literal and distance trees */
  840. X    scan_tree(dyn_ltree, l_desc.max_code);
  841. X    scan_tree(dyn_dtree, d_desc.max_code);
  842. X
  843. X    /* Build the bit length tree: */
  844. X    build_tree(&bl_desc);
  845. X    /* opt_len now includes the length of the tree representations, except
  846. X     * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
  847. X     */
  848. X
  849. X    /* Determine the number of bit length codes to send. The pkzip format
  850. X     * requires that at least 4 bit length codes be sent. (appnote.txt says
  851. X     * 3 but the actual value used is 4.)
  852. X     */
  853. X    for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
  854. X        if (bl_tree[bl_order[max_blindex]].Len != 0) break;
  855. X    }
  856. X    /* Update opt_len to include the bit length tree and counts */
  857. X    opt_len += 3*(max_blindex+1) + 5+5+4;
  858. X    Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld", opt_len, static_len));
  859. X
  860. X    return max_blindex;
  861. X}
  862. X
  863. X/* ===========================================================================
  864. X * Send the header for a block using dynamic Huffman trees: the counts, the
  865. X * lengths of the bit length codes, the literal tree and the distance tree.
  866. X * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
  867. X */
  868. Xlocal void send_all_trees(lcodes, dcodes, blcodes)
  869. X    int lcodes, dcodes, blcodes; /* number of codes for each tree */
  870. X{
  871. X    int rank;                    /* index in bl_order */
  872. X
  873. X    Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
  874. X    Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
  875. X            "too many codes");
  876. X    Tracev((stderr, "\nbl counts: "));
  877. X    send_bits(lcodes-257, 5); /* not -255 as stated in appnote.txt */
  878. X    send_bits(dcodes-1,   5);
  879. X    send_bits(blcodes-4,  4); /* not -3 as stated in appnote.txt */
  880. X    for (rank = 0; rank < blcodes; rank++) {
  881. X        Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
  882. X        send_bits(bl_tree[bl_order[rank]].Len, 3);
  883. X    }
  884. X    Tracev((stderr, "\nbl tree: sent %ld", bits_sent));
  885. X
  886. X    send_tree(dyn_ltree, lcodes-1); /* send the literal tree */
  887. X    Tracev((stderr, "\nlit tree: sent %ld", bits_sent));
  888. X
  889. X    send_tree(dyn_dtree, dcodes-1); /* send the distance tree */
  890. X    Tracev((stderr, "\ndist tree: sent %ld", bits_sent));
  891. X}
  892. X
  893. X/* ===========================================================================
  894. X * Determine the best encoding for the current block: dynamic trees, static
  895. X * trees or store, and output the encoded block to the zip file. This function
  896. X * returns the total compressed length for the file so far.
  897. X */
  898. Xulg flush_block(buf, stored_len, eof)
  899. X    char *buf;        /* input block, or NULL if too old */
  900. X    ulg stored_len;   /* length of input block */
  901. X    int eof;          /* true if this is the last block for a file */
  902. X{
  903. X    ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
  904. X    int max_blindex;  /* index of last bit length code of non zero freq */
  905. X
  906. X    flag_buf[last_flags] = flags; /* Save the flags for the last 8 items */
  907. X
  908. X     /* Check if the file is ascii or binary */
  909. X    if (*file_type == (ush)UNKNOWN) set_file_type();
  910. X
  911. X    /* Construct the literal and distance trees */
  912. X    build_tree(&l_desc);
  913. X    Tracev((stderr, "\nlit data: dyn %ld, stat %ld", opt_len, static_len));
  914. X
  915. X    build_tree(&d_desc);
  916. X    Tracev((stderr, "\ndist data: dyn %ld, stat %ld", opt_len, static_len));
  917. X    /* At this point, opt_len and static_len are the total bit lengths of
  918. X     * the compressed block data, excluding the tree representations.
  919. X     */
  920. X
  921. X    /* Build the bit length tree for the above two trees, and get the index
  922. X     * in bl_order of the last bit length code to send.
  923. X     */
  924. X    max_blindex = build_bl_tree();
  925. X
  926. X    /* Determine the best encoding. Compute first the block length in bytes */
  927. X    opt_lenb = (opt_len+3+7)>>3;
  928. X    static_lenb = (static_len+3+7)>>3;
  929. X    input_len += stored_len; /* for debugging only */
  930. X
  931. X    Trace((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u dist %u ",
  932. X            opt_lenb, opt_len, static_lenb, static_len, stored_len,
  933. X            last_lit, last_dist));
  934. X
  935. X    if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
  936. X
  937. X    /* If compression failed and this is the first and last block,
  938. X     * and if the zip file can be seeked (to rewrite the local header),
  939. X     * the whole file is transformed into a stored file:
  940. X     */
  941. X#ifdef FORCE_METHOD
  942. X    if (level == 1 && eof && compressed_len == 0L) { /* force stored file */
  943. X#else
  944. X    if (stored_len <= opt_lenb && eof && compressed_len == 0L && seekable()) {
  945. X#endif
  946. X        /* Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: */
  947. X        if (buf == NULL) error ("block vanished");
  948. X
  949. X        copy_block(buf, (unsigned)stored_len, 0); /* without header */
  950. X        compressed_len = stored_len << 3;
  951. X        *file_method = STORE;
  952. X
  953. X#ifdef FORCE_METHOD
  954. X    } else if (level == 2 && buf != NULL) { /* force stored block */
  955. X#else
  956. X    } else if (stored_len+4 <= opt_lenb && buf != NULL) {
  957. X                       /* 4: two words for the lengths */
  958. X#endif
  959. X        /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
  960. X         * Otherwise we can't have processed more than WSIZE input bytes since
  961. X         * the last block flush, because compression would have been
  962. X         * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
  963. X         * transform a block into a stored block.
  964. X         */
  965. X        send_bits((STORED_BLOCK<<1)+eof, 3);  /* send block type */
  966. X        compressed_len = (compressed_len + 3 + 7) & ~7L;
  967. X        compressed_len += (stored_len + 4) << 3;
  968. X
  969. X        copy_block(buf, (unsigned)stored_len, 1); /* with header */
  970. X
  971. X#ifdef FORCE_METHOD
  972. X    } else if (level == 3) { /* force static trees */
  973. X#else
  974. X    } else if (static_lenb == opt_lenb) {
  975. X#endif
  976. X        send_bits((STATIC_TREES<<1)+eof, 3);
  977. X        compress_block(static_ltree, static_dtree);
  978. X        compressed_len += 3 + static_len;
  979. X    } else {
  980. X        send_bits((DYN_TREES<<1)+eof, 3);
  981. X        send_all_trees(l_desc.max_code+1, d_desc.max_code+1, max_blindex+1);
  982. X        compress_block(dyn_ltree, dyn_dtree);
  983. X        compressed_len += 3 + opt_len;
  984. X    }
  985. X    Assert (compressed_len == bits_sent, "bad compressed size");
  986. X    init_block();
  987. X
  988. X    if (eof) {
  989. X        Assert (input_len == isize, "bad input size");
  990. X        bi_windup();
  991. X        compressed_len += 7;  /* align on byte boundary */
  992. X    }
  993. X    Tracev((stderr,"\ncomprlen %lu(%lu) ", compressed_len>>3,
  994. X           compressed_len-7*eof));
  995. X
  996. X    return compressed_len >> 3;
  997. X}
  998. X
  999. X/* ===========================================================================
  1000. X * Save the match info and tally the frequency counts. Return true if
  1001. X * the current block must be flushed.
  1002. X */
  1003. Xint ct_tally (dist, lc)
  1004. X    int dist;  /* distance of matched string */
  1005. X    int lc;    /* match length-MIN_MATCH or unmatched char (if dist==0) */
  1006. X{
  1007. X    l_buf[last_lit++] = (uch)lc;
  1008. X    if (dist == 0) {
  1009. X        /* lc is the unmatched char */
  1010. X        dyn_ltree[lc].Freq++;
  1011. X    } else {
  1012. X        /* Here, lc is the match length - MIN_MATCH */
  1013. X        dist--;             /* dist = match distance - 1 */
  1014. X        Assert((ush)dist < (ush)MAX_DIST &&
  1015. X               (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
  1016. X               (ush)d_code(dist) < (ush)D_CODES,  "ct_tally: bad match");
  1017. X
  1018. X        dyn_ltree[length_code[lc]+LITERALS+1].Freq++;
  1019. X        dyn_dtree[d_code(dist)].Freq++;
  1020. X
  1021. X        d_buf[last_dist++] = dist;
  1022. X        flags |= flag_bit;
  1023. X    }
  1024. X    flag_bit <<= 1;
  1025. X
  1026. X    /* Output the flags if they fill a byte: */
  1027. X    if ((last_lit & 7) == 0) {
  1028. X        flag_buf[last_flags++] = flags;
  1029. X        flags = 0, flag_bit = 1;
  1030. X    }
  1031. X    /* Try to guess if it is profitable to stop the current block here */
  1032. X    if (level > 2 && (last_lit & 0xfff) == 0) {
  1033. X        /* Compute an upper bound for the compressed length */
  1034. X        ulg out_length = (ulg)last_lit*8L;
  1035. X        ulg in_length = (ulg)strstart-block_start;
  1036. X        int dcode;
  1037. X        for (dcode = 0; dcode < D_CODES; dcode++) {
  1038. X            out_length += (ulg)dyn_dtree[dcode].Freq*(5L+extra_dbits[dcode]);
  1039. X        }
  1040. X        out_length >>= 3;
  1041. X        Trace((stderr,"\nlast_lit %u, last_dist %u, in %ld, out ~%ld(%ld%%) ",
  1042. X               last_lit, last_dist, in_length, out_length,
  1043. X               100L - out_length*100L/in_length));
  1044. X        if (last_dist < last_lit/2 && out_length < in_length/2) return 1;
  1045. X    }
  1046. X    return (last_lit == LIT_BUFSIZE-1 || last_dist == DIST_BUFSIZE);
  1047. X    /* We avoid equality with LIT_BUFSIZE because of wraparound at 64K
  1048. X     * on 16 bit machines and because stored blocks are restricted to
  1049. X     * 64K-1 bytes.
  1050. X     */
  1051. X}
  1052. X
  1053. X/* ===========================================================================
  1054. X * Send the block data compressed using the given Huffman trees
  1055. X */
  1056. Xlocal void compress_block(ltree, dtree)
  1057. X    ct_data near *ltree; /* literal tree */
  1058. X    ct_data near *dtree; /* distance tree */
  1059. X{
  1060. X    unsigned dist;      /* distance of matched string */
  1061. X    int lc;             /* match length or unmatched char (if dist == 0) */
  1062. X    unsigned lx = 0;    /* running index in l_buf */
  1063. X    unsigned dx = 0;    /* running index in d_buf */
  1064. X    unsigned fx = 0;    /* running index in flag_buf */
  1065. X    uch flag = 0;       /* current flags */
  1066. X    unsigned code;      /* the code to send */
  1067. X    int extra;          /* number of extra bits to send */
  1068. X
  1069. X    if (last_lit != 0) do {
  1070. X        if ((lx & 7) == 0) flag = flag_buf[fx++];
  1071. X        lc = l_buf[lx++];
  1072. X        if ((flag & 1) == 0) {
  1073. X            send_code(lc, ltree); /* send a literal byte */
  1074. X            Tracecv(isgraph(lc), (stderr," '%c' ", lc));
  1075. X        } else {
  1076. X            /* Here, lc is the match length - MIN_MATCH */
  1077. X            code = length_code[lc];
  1078. X            send_code(code+LITERALS+1, ltree); /* send the length code */
  1079. X            extra = extra_lbits[code];
  1080. X            if (extra != 0) {
  1081. X                lc -= base_length[code];
  1082. X                send_bits(lc, extra);        /* send the extra length bits */
  1083. X            }
  1084. X            dist = d_buf[dx++];
  1085. X            /* Here, dist is the match distance - 1 */
  1086. X            code = d_code(dist);
  1087. X            Assert (code < D_CODES, "bad d_code");
  1088. X
  1089. X            send_code(code, dtree);       /* send the distance code */
  1090. X            extra = extra_dbits[code];
  1091. X            if (extra != 0) {
  1092. X                dist -= base_dist[code];
  1093. X                send_bits(dist, extra);   /* send the extra distance bits */
  1094. X            }
  1095. X        } /* literal or match pair ? */
  1096. X        flag >>= 1;
  1097. X    } while (lx < last_lit);
  1098. X
  1099. X    send_code(END_BLOCK, ltree);
  1100. X}
  1101. X
  1102. X/* ===========================================================================
  1103. X * Set the file type to ASCII or BINARY, using a crude approximation:
  1104. X * binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise.
  1105. X * IN assertion: the fields freq of dyn_ltree are set and the total of all
  1106. X * frequencies does not exceed 64K (to fit in an int on 16 bit machines).
  1107. X */
  1108. Xlocal void set_file_type()
  1109. X{
  1110. X    int n = 0;
  1111. X    unsigned ascii_freq = 0;
  1112. X    unsigned bin_freq = 0;
  1113. X    while (n < 7)        bin_freq += dyn_ltree[n++].Freq;
  1114. X    while (n < 128)    ascii_freq += dyn_ltree[n++].Freq;
  1115. X    while (n < LITERALS) bin_freq += dyn_ltree[n++].Freq;
  1116. X    *file_type = bin_freq > (ascii_freq >> 2) ? BINARY : ASCII;
  1117. X    if (*file_type == BINARY && translate_eol) {
  1118. X        warn("-l used on binary file", "");
  1119. X    }
  1120. X}
  1121. END_OF_FILE
  1122.   if test 40777 -ne `wc -c <'trees.c'`; then
  1123.     echo shar: \"'trees.c'\" unpacked with wrong size!
  1124.   fi
  1125.   # end of 'trees.c'
  1126. fi
  1127. if test -f 'util.c' -a "${1}" != "-c" ; then 
  1128.   echo shar: Will not clobber existing file \"'util.c'\"
  1129. else
  1130.   echo shar: Extracting \"'util.c'\" \(12887 characters\)
  1131.   sed "s/^X//" >'util.c' <<'END_OF_FILE'
  1132. X/*
  1133. X
  1134. X Copyright (C) 1990-1992 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  1135. X Kai Uwe Rommel and Igor Mandrichenko.
  1136. X Permission is granted to any individual or institution to use, copy, or
  1137. X redistribute this software so long as all of the original files are included
  1138. X unmodified, that it is not sold for profit, and that this copyright notice
  1139. X is retained.
  1140. X
  1141. X*/
  1142. X
  1143. X/*
  1144. X *  util.c by Mark Adler.
  1145. X */
  1146. X
  1147. X#include "zip.h"
  1148. X#include <ctype.h>
  1149. X
  1150. X#if defined(MSDOS) && !defined(OS2) && !defined(__GO32__) && !defined(WIN32)
  1151. X#  include <dos.h>
  1152. X#endif
  1153. X
  1154. Xuch upper[256], lower[256];
  1155. X/* Country-dependent case map table */
  1156. X
  1157. X
  1158. X#ifndef UTIL /* UTIL picks out namecmp code (all utils) and crc32 (zipcloak) */
  1159. X
  1160. X/* Local functions */
  1161. X#ifdef PROTO
  1162. X  local int recmatch(char *, char *);
  1163. X  local unsigned ident(unsigned chr);
  1164. X#endif /* PROTO */
  1165. X
  1166. Xchar *isshexp(p)
  1167. Xchar *p;                /* candidate sh expression */
  1168. X/* If p is a sh expression, a pointer to the first special character is
  1169. X   returned.  Otherwise, NULL is returned. */
  1170. X{
  1171. X  for (; *p; p++)
  1172. X    if (*p == '\\' && *(p+1))
  1173. X      p++;
  1174. X#ifdef VMS
  1175. X    else if (*p == '%' || *p == '*')
  1176. X#else /* !VMS */
  1177. X    else if (*p == '?' || *p == '*' || *p == '[')
  1178. X#endif /* ?VMS */
  1179. X      return p;
  1180. X  return NULL;
  1181. X}
  1182. X
  1183. X
  1184. Xlocal int recmatch(p, s)
  1185. Xchar *p;                /* sh pattern to match */
  1186. Xchar *s;                /* string to match it to */
  1187. X/* Recursively compare the sh pattern p with the string s and return 1 if
  1188. X   they match, and 0 or 2 if they don't or if there is a syntax error in the
  1189. X   pattern.  This routine recurses on itself no deeper than the number of
  1190. X   characters in the pattern. */
  1191. X{
  1192. X  int c;                /* pattern char or start of range in [-] loop */ 
  1193. X
  1194. X  /* Get first character, the pattern for new recmatch calls follows */
  1195. X  c = *p++;
  1196. X
  1197. X  /* If that was the end of the pattern, match if string empty too */
  1198. X  if (c == 0)
  1199. X    return *s == 0;
  1200. X
  1201. X  /* '?' (or '%') matches any character (but not an empty string) */
  1202. X#ifdef VMS
  1203. X  if (c == '%')
  1204. X#else /* !VMS */
  1205. X  if (c == '?')
  1206. X#endif /* ?VMS */
  1207. X    return *s ? recmatch(p, s + 1) : 0;
  1208. X
  1209. X  /* '*' matches any number of characters, including zero */
  1210. X  if (c == '*')
  1211. X  {
  1212. X    if (*p == 0)
  1213. X      return 1;
  1214. X    for (; *s; s++)
  1215. X      if ((c = recmatch(p, s)) != 0)
  1216. X        return c;
  1217. X    return 2;           /* 2 means give up--shmatch will return false */
  1218. X  }
  1219. X
  1220. X#ifndef VMS             /* No bracket matching in VMS */
  1221. X  /* Parse and process the list of characters and ranges in brackets */
  1222. X  if (c == '[')
  1223. X  {
  1224. X    int e;              /* flag true if next char to be taken literally */
  1225. X    char *q;            /* pointer to end of [-] group */
  1226. X    int r;              /* flag true to match anything but the range */
  1227. X
  1228. X    if (*s == 0)                        /* need a character to match */
  1229. X      return 0;
  1230. X    p += (r = *p == '!');               /* see if reverse */
  1231. X    for (q = p, e = 0; *q; q++)         /* find closing bracket */
  1232. X      if (e)
  1233. X        e = 0;
  1234. X      else
  1235. X        if (*q == '\\')
  1236. X          e = 1;
  1237. X        else if (*q == ']')
  1238. X          break;
  1239. X    if (*q != ']')                      /* nothing matches if bad syntax */
  1240. X      return 0;
  1241. X    for (c = 0, e = *p == '-'; p < q; p++)      /* go through the list */
  1242. X    {
  1243. X      if (e == 0 && *p == '\\')         /* set escape flag if \ */
  1244. X        e = 1;
  1245. X      else if (e == 0 && *p == '-')     /* set start of range if - */
  1246. X        c = *(p-1);
  1247. X      else
  1248. X      {
  1249. X        if (*(p+1) != '-')
  1250. X          for (c = c ? c : *p; c <= *p; c++)    /* compare range */
  1251. X            if (case_map(c) == case_map(*s))
  1252. X              return r ? 0 : recmatch(q + 1, s + 1);
  1253. X        c = e = 0;                      /* clear range, escape flags */
  1254. X      }
  1255. X    }
  1256. X    return r ? recmatch(q + 1, s + 1) : 0;      /* bracket match failed */
  1257. X  }
  1258. X#endif /* !VMS */
  1259. X
  1260. X  /* If escape ('\'), just compare next character */
  1261. X  if (c == '\\')
  1262. X    if ((c = *p++) == 0)                /* if \ at end, then syntax error */
  1263. X      return 0;
  1264. X
  1265. X  /* Just a character--compare it */
  1266. X  return case_map(c) == case_map(*s) ? recmatch(p, ++s) : 0;
  1267. X}
  1268. X
  1269. X
  1270. Xint shmatch(p, s)
  1271. Xchar *p;                /* sh pattern to match */
  1272. Xchar *s;                /* string to match it to */
  1273. X/* Compare the sh pattern p with the string s and return true if they match,
  1274. X   false if they don't or if there is a syntax error in the pattern. */
  1275. X{
  1276. X  return recmatch(p, s) == 1;
  1277. X}
  1278. X
  1279. X
  1280. X#ifdef MSDOS
  1281. X
  1282. Xint dosmatch(p, s)
  1283. Xchar *p;                /* dos pattern to match */
  1284. Xchar *s;                /* string to match it to */
  1285. X/* Break the pattern and string into name and extension parts and match
  1286. X   each separately using shmatch(). */
  1287. X{
  1288. X  char *p1, *p2;        /* pattern sections */
  1289. X  char *s1, *s2;        /* string sections */
  1290. X  int r;                /* result */
  1291. X
  1292. X  if ((p1 = malloc(strlen(p) + 1)) == NULL ||
  1293. X      (s1 = malloc(strlen(s) + 1)) == NULL)
  1294. X  {
  1295. X    if (p1 != NULL)
  1296. X      free((voidp *)p1);
  1297. X    return 0;
  1298. X  }
  1299. X  strcpy(p1, p);
  1300. X  strcpy(s1, s);
  1301. X  if ((p2 = strrchr(p1, '.')) != NULL)
  1302. X    *p2++ = 0;
  1303. X  else
  1304. X    p2 = "";
  1305. X  if ((s2 = strrchr(s1, '.')) != NULL)
  1306. X    *s2++ = 0;
  1307. X  else
  1308. X    s2 = "";
  1309. X  r = shmatch(p2, s2) && shmatch(p1, s1);
  1310. X  free((voidp *)p1);
  1311. X  free((voidp *)s1);
  1312. X  return r;
  1313. X}
  1314. X#endif /* MSDOS */
  1315. X
  1316. Xvoidp far **search(b, a, n, cmp)
  1317. Xvoidp *b;               /* pointer to value to search for */
  1318. Xvoidp far **a;          /* table of pointers to values, sorted */
  1319. Xextent n;               /* number of pointers in a[] */
  1320. Xint (*cmp) OF((voidp *, voidp far *));  /* comparison function for search */
  1321. X/* Search for b in the pointer list a[0..n-1] using the compare function
  1322. X   cmp(b, c) where c is an element of a[i] and cmp() returns negative if
  1323. X   *b < *c, zero if *b == *c, or positive if *b > *c.  If *b is found,
  1324. X   search returns a pointer to the entry in a[], else search() returns
  1325. X   NULL.  The nature and size of *b and *c (they can be different) are
  1326. X   left up to the cmp() function.  A binary search is used, and it is
  1327. X   assumed that the list is sorted in ascending order. */
  1328. X{
  1329. X  voidp far **i;        /* pointer to midpoint of current range */
  1330. X  voidp far **l;        /* pointer to lower end of current range */
  1331. X  int r;                /* result of (*cmp)() call */
  1332. X  voidp far **u;        /* pointer to upper end of current range */
  1333. X
  1334. X  l = (voidp far **)a;  u = l + (n-1);
  1335. X  while (u >= l)
  1336. X    if ((r = (*cmp)(b, *(i = l + ((u - l) >> 1)))) < 0)
  1337. X      u = i - 1;
  1338. X    else if (r > 0)
  1339. X      l = i + 1;
  1340. X    else
  1341. X      return (voidp far **)i;
  1342. X  return NULL;          /* If b were in list, it would belong at l */
  1343. X}
  1344. X
  1345. X#endif /* !UTIL */
  1346. X
  1347. X
  1348. X/* Table of CRC-32's of all single byte values (made by makecrc.c) */
  1349. Xlocal ulg crctab[] = {
  1350. X  0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
  1351. X  0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
  1352. X  0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
  1353. X  0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
  1354. X  0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
  1355. X  0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
  1356. X  0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
  1357. X  0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
  1358. X  0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
  1359. X  0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
  1360. X  0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
  1361. X  0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
  1362. X  0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
  1363. X  0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
  1364. X  0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
  1365. X  0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
  1366. X  0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
  1367. X  0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
  1368. X  0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
  1369. X  0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
  1370. X  0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
  1371. X  0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
  1372. X  0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
  1373. X  0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
  1374. X  0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
  1375. X  0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
  1376. X  0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
  1377. X  0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
  1378. X  0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
  1379. X  0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
  1380. X  0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
  1381. X  0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
  1382. X  0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
  1383. X  0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
  1384. X  0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
  1385. X  0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
  1386. X  0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
  1387. X  0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
  1388. X  0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
  1389. X  0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
  1390. X  0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
  1391. X  0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
  1392. X  0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
  1393. X  0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
  1394. X  0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
  1395. X  0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
  1396. X  0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
  1397. X  0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
  1398. X  0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
  1399. X  0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
  1400. X  0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
  1401. X  0x2d02ef8dL
  1402. X};
  1403. X
  1404. X
  1405. Xulg crc32(c, b)
  1406. Xulg c;                  /* current contents of crc shift register */
  1407. Xint b;                  /* byte (eight bits) to run through */
  1408. X/* Return the CRC-32 c updated with the eight bits in b. */
  1409. X{
  1410. X  return crctab[((int)c ^ b) & 0xff] ^ (c >> 8);
  1411. X}
  1412. X
  1413. X
  1414. X#ifndef UTIL /* UTIL picks out namecmp code (all utils) and crc32 (zipcloak) */
  1415. X
  1416. Xulg updcrc(s, n)
  1417. Xchar *s;                /* pointer to bytes to pump through */
  1418. Xextent n;               /* number of bytes in s[] */
  1419. X/* Run a set of bytes through the crc shift register.  If s is a NULL
  1420. X   pointer, then initialize the crc shift register contents instead.
  1421. X   Return the current crc in either case. */
  1422. X{
  1423. X  register ulg c;       /* temporary variable */
  1424. X
  1425. X  static ulg crc = 0xffffffffL; /* shift register contents */
  1426. X
  1427. X  if (s == NULL)
  1428. X    c = 0xffffffffL;
  1429. X  else
  1430. X  {
  1431. X    c = crc;
  1432. X    while (n--)
  1433. X      c = crctab[((int)c ^ (*s++)) & 0xff] ^ (c >> 8);
  1434. X  }
  1435. X  crc = c;
  1436. X  return c ^ 0xffffffffL;       /* (instead of ~c for 64-bit machines) */
  1437. X}
  1438. X
  1439. X#endif /* !UTIL */
  1440. X
  1441. X
  1442. X#if (defined(MSDOS) && !defined(OS2) && !defined(__GO32__) && !defined(WIN32))
  1443. X
  1444. Xlocal unsigned ident(unsigned chr)
  1445. X{
  1446. X   return chr; /* in al */
  1447. X}
  1448. X
  1449. Xvoid init_upper()
  1450. X{
  1451. X  static struct country {
  1452. X    uch ignore[18];
  1453. X    int (far *casemap)(int);
  1454. X    uch filler[16];
  1455. X  } country_info;
  1456. X
  1457. X  struct country far *info = &country_info;
  1458. X  union REGS regs;
  1459. X  struct SREGS sregs;
  1460. X  int c;
  1461. X
  1462. X  regs.x.ax = 0x3800; /* get country info */
  1463. X  regs.x.dx = FP_OFF(info);
  1464. X  sregs.ds  = FP_SEG(info);
  1465. X  intdosx(®s, ®s, &sregs);
  1466. X  for (c = 0; c < 128; c++) {
  1467. X    upper[c] = (uch) toupper(c);
  1468. X    lower[c] = (uch) c;
  1469. X  }
  1470. X  for (; c < sizeof(upper); c++) {
  1471. X    upper[c] = (uch) (*country_info.casemap)(ident(c));
  1472. X    /* ident() required because casemap takes its parameter in al */
  1473. X    lower[c] = (uch) c;
  1474. X  }
  1475. X  for (c = 0; c < sizeof(upper); c++ ) {
  1476. X    int u = upper[c];
  1477. X    if (u != c && lower[u] == (uch) u) {
  1478. X      lower[u] = (uch)c;
  1479. X    }
  1480. X  }
  1481. X  for (c = 'A'; c <= 'Z'; c++) {
  1482. X    lower[c] = (uch) (c - 'A' + 'a');
  1483. X  }
  1484. X}
  1485. X#else
  1486. X#  ifndef OS2
  1487. X
  1488. Xvoid init_upper()
  1489. X{
  1490. X  int c;
  1491. X  for (c = 0; c < sizeof(upper); c++) upper[c] = lower[c] = c;
  1492. X  for (c = 'a'; c <= 'z';        c++) upper[c] = c - 'a' + 'A';
  1493. X  for (c = 'A'; c <= 'Z';        c++) lower[c] = c - 'A' + 'a';
  1494. X}
  1495. X#  endif /* OS2 */
  1496. X
  1497. X#endif /* MSDOS && !__GO32__ && !OS2 */
  1498. X
  1499. Xint namecmp(string1, string2)
  1500. X  char *string1, *string2;
  1501. X/* Compare the two strings ignoring case, and correctly taking into
  1502. X * account national language characters. For operating systems with
  1503. X * case sensitive file names, this function is equivalent to strcmp.
  1504. X */
  1505. X{
  1506. X  int d;
  1507. X
  1508. X  for (;;)
  1509. X  {
  1510. X    d = (int) (unsigned char) case_map(*string1)
  1511. X      - (int) (unsigned char) case_map(*string2);
  1512. X    
  1513. X    if (d || *string1 == 0 || *string2 == 0)
  1514. X      return d;
  1515. X      
  1516. X    string1++;
  1517. X    string2++;
  1518. X  }
  1519. X}
  1520. END_OF_FILE
  1521.   if test 12887 -ne `wc -c <'util.c'`; then
  1522.     echo shar: \"'util.c'\" unpacked with wrong size!
  1523.   fi
  1524.   # end of 'util.c'
  1525. fi
  1526. if test -f 'vms/makefile.vms' -a "${1}" != "-c" ; then 
  1527.   echo shar: Will not clobber existing file \"'vms/makefile.vms'\"
  1528. else
  1529.   echo shar: Extracting \"'vms/makefile.vms'\" \(3279 characters\)
  1530.   sed "s/^X//" >'vms/makefile.vms' <<'END_OF_FILE'
  1531. X#============================================================================
  1532. X# Makefile for VMS Zip, ZipCloak, ZipNote  and ZipSplit          Greg Roelofs
  1533. X# Version:  1.8f   [for use with Todd Aven's MAKE/VMS 3.4]       25 June 1992
  1534. X#============================================================================
  1535. X
  1536. X# Most recent revisions:  25 June 1992
  1537. X
  1538. X
  1539. X#####################
  1540. X# MACRO DEFINITIONS #
  1541. X#####################
  1542. X
  1543. XCRYPTO =
  1544. XCLOAK =
  1545. XCFLAGS =
  1546. XUFLAGS = /def=UTIL
  1547. X# Uncomment next three lines for decryption version:
  1548. X#CRYPTO = crypt.obj,
  1549. X#CLOAK = zipcloak.exe
  1550. X#CFLAGS = /def=CRYPT
  1551. X#UFLAGS = /def=(UTIL,CRYPT)
  1552. X
  1553. XCC = cc
  1554. XLIB =
  1555. X# Uncomment next two lines to use the GNU compiler (also add /undef=__STDC__
  1556. X# to CFLAGS and UFLAGS, possibly split UFLAGS into two /def's, and possibly
  1557. X# replace /obj=$@ [below] with copy/rename/delete setup).  NOT TESTED.
  1558. X#CC = gcc
  1559. X#LIB = gnu_cc:[000000]gcclib.olb/lib,
  1560. X
  1561. XE = .exe
  1562. XO = .obj
  1563. XLD = link
  1564. XLDFLAGS =
  1565. X
  1566. XZIPS = zip$E zipnote$E zipsplit$E $(CLOAK)
  1567. X
  1568. X# object file lists
  1569. XOBJZ = zip$O, zipfile$O, zipup$O, fileio$O, $(CRYPTO) util$O,-
  1570. X   globals$O, VMSmunch$O, vms$O
  1571. XOBJI = deflate$O, trees$O, bits$O
  1572. XOBJN = zipnote$O, zipfile_$O, zipup_$O, fileio_$O, util_$O,-
  1573. X   globals$O, VMSmunch$O
  1574. XOBJS = zipsplit$O, zipfile_$O, zipup_$O, fileio_$O, util_$O,-
  1575. X   globals$O, VMSmunch$O
  1576. XOBJC = zipcloak$O, zipfile_$O, zipup_$O, fileio_$O, util$O,-
  1577. X   globals$O, VMSmunch$O, crypt_$O
  1578. X
  1579. X
  1580. X###############################################
  1581. X# BASIC COMPILE INSTRUCTIONS AND DEPENDENCIES #
  1582. X###############################################
  1583. X
  1584. Xdefault:    $(ZIPS)
  1585. X
  1586. X
  1587. X# suffix rules
  1588. X*.obj:    *.c                # `*.c' necessary?
  1589. X    $(CC) $(CFLAGS) $<
  1590. X
  1591. X*_.obj:    *.c                # `$*' not legal
  1592. X    $(CC) $(UFLAGS) /obj=$@ $<
  1593. X
  1594. X
  1595. X# executables makerules (trailing `$' makes line a data line)
  1596. Xzip$E:        $(OBJZ), $(OBJI)
  1597. X    $(LD) $(LDFLAGS) $(OBJZ), $(OBJI), $(LIB) sys$input/opt
  1598. X    sys$share:vaxcrtl.exe/shareable $
  1599. X
  1600. Xzipnote$E:    $(OBJN)
  1601. X    $(LD) $(LDFLAGS) $(OBJN), $(LIB) sys$input/opt
  1602. X    sys$share:vaxcrtl.exe/shareable $
  1603. X
  1604. Xzipcloak$E:    $(OBJC)
  1605. X    $(LD) $(LDFLAGS) $(OBJC), $(LIB) sys$input/opt
  1606. X    sys$share:vaxcrtl.exe/shareable $
  1607. X
  1608. Xzipsplit$E:    $(OBJS)
  1609. X    $(LD) $(LDFLAGS) $(OBJS), $(LIB) sys$input/opt
  1610. X    sys$share:vaxcrtl.exe/shareable $
  1611. X
  1612. X# dependencies for zip, zipnote, zipcloak, and zipsplit
  1613. X$(OBJZ):    zip.h ziperr.h tailor.h
  1614. X$(OBJI):    zip.h ziperr.h tailor.h
  1615. X$(OBJN):    zip.h ziperr.h tailor.h
  1616. X$(OBJS):    zip.h ziperr.h tailor.h
  1617. X$(OBJC):    zip.h ziperr.h tailor.h
  1618. Xfileio$O:    VMSmunch.h
  1619. Xvms$O:          vms.c zip.h
  1620. XVMSmunch$O:     VMSmunch.c VMSmunch.h
  1621. Xzip$O:        revision.h
  1622. Xzipcloak$O:    revision.h
  1623. Xzipfile$O:    VMSmunch.h
  1624. Xzipnote$O:    revision.h
  1625. Xzipsplit$O:    revision.h
  1626. Xzipup$O:    revision.h
  1627. X
  1628. X
  1629. Xclean:
  1630. X    del *.obj;*
  1631. X    del *.exe;*         # use "purge/log" instead?
  1632. X
  1633. X
  1634. X# the backslash '\' is the continuation character if it occurs as
  1635. X# the last non-white character on the line.
  1636. X# the hyphen '-' is the DCL continuation character, so if it occurs
  1637. X# as the last non-white character on the line, the next line will
  1638. X# not have the dollar sign '$' prepended.
  1639. X
  1640. X
  1641. X################################
  1642. X# INDIVIDUAL MACHINE MAKERULES #
  1643. X################################
  1644. X
  1645. Xgeneric:    default        # first try if unknown
  1646. Xgeneric2:    default        # second try if unknown
  1647. Xvax:        default
  1648. Xvms:        default
  1649. X
  1650. Xall:        $(ZIPS)
  1651. Xzip:        zip$E
  1652. Xzipcloak:    zipcloak$E
  1653. Xzipnote:    zipnote$E
  1654. Xzipsplit:    zipsplit$E
  1655. END_OF_FILE
  1656.   if test 3279 -ne `wc -c <'vms/makefile.vms'`; then
  1657.     echo shar: \"'vms/makefile.vms'\" unpacked with wrong size!
  1658.   fi
  1659.   # end of 'vms/makefile.vms'
  1660. fi
  1661. echo shar: End of archive 3 \(of 11\).
  1662. cp /dev/null ark3isdone
  1663. MISSING=""
  1664. for I in 1 2 3 4 5 6 7 8 9 10 11 ; do
  1665.     if test ! -f ark${I}isdone ; then
  1666.     MISSING="${MISSING} ${I}"
  1667.     fi
  1668. done
  1669. if test "${MISSING}" = "" ; then
  1670.     echo You have unpacked all 11 archives.
  1671.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  1672. else
  1673.     echo You still must unpack the following archives:
  1674.     echo "        " ${MISSING}
  1675. fi
  1676. exit 0
  1677. exit 0 # Just in case...
  1678.