home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume31 / unzip50 / part05 < prev    next >
Encoding:
Text File  |  1992-08-22  |  58.7 KB  |  1,558 lines

  1. Newsgroups: comp.sources.misc
  2. From: zip-bugs@cs.ucla.edu (Info-ZIP group)
  3. Subject:  v31i108:  unzip50 - Info-ZIP portable UnZip, version 5.0, Part05/14
  4. Message-ID: <1992Aug24.025340.24367@sparky.imd.sterling.com>
  5. X-Md4-Signature: 98e992236da685418380831738bd265e
  6. Date: Mon, 24 Aug 1992 02:53:40 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: zip-bugs@cs.ucla.edu (Info-ZIP group)
  10. Posting-number: Volume 31, Issue 108
  11. Archive-name: unzip50/part05
  12. Supersedes: unzip: Volume 29, Issue 31-42
  13. Environment: UNIX, VMS, OS/2, MS-DOS, MACINTOSH, WIN-NT, LINUX, MINIX, COHERENT AMIGA?, !ATARI, symlink, SGI, DEC, Cray, Convex, Amdahl, Sun
  14.  
  15. #! /bin/sh
  16. # This is a shell archive.  Remove anything before this line, then feed it
  17. # into a shell via "sh file" or similar.  To overwrite existing files,
  18. # type "sh file -c".
  19. # The tool that generated this appeared in the comp.sources.unix newsgroup;
  20. # send mail to comp-sources-unix@uunet.uu.net if you want that tool.
  21. # Contents:  misc.c unzip.c.B
  22. # Wrapped by kent@sparky on Sun Aug 23 21:09:32 1992
  23. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  24. echo If this archive is complete, you will see the following message:
  25. echo '          "shar: End of archive 5 (of 14)."'
  26. if test -f 'misc.c' -a "${1}" != "-c" ; then 
  27.   echo shar: Will not clobber existing file \"'misc.c'\"
  28. else
  29.   echo shar: Extracting \"'misc.c'\" \(24826 characters\)
  30.   sed "s/^X//" >'misc.c' <<'END_OF_FILE'
  31. X/*---------------------------------------------------------------------------
  32. X
  33. X  misc.c
  34. X
  35. X  This file contains a number of useful but not particularly closely related
  36. X  functions; their main claim to fame is that they don't change much, so this
  37. X  file should rarely need to be recompiled.  The CRC-32 stuff is from crc32.c;
  38. X  do_string() is from nunzip.c; makeword() and makelong() are from unzip.c;
  39. X  memset() and memcpy() are from zmemset.c and zmemcpy.c, respectively; and
  40. X  dos_to_unix_time() is from set_file_time_and_close() in file_io.c.  ebcdic[],
  41. X  check_for_newer(), dateformat(), and return_VMS() are new.  Things lumped
  42. X  together here to cut down on the size of unzip.c and the number of associ-
  43. X  ated files floating around.
  44. X
  45. X  ---------------------------------------------------------------------------
  46. X
  47. X  Copyrights:  see accompanying file "COPYING" in UnZip source distribution.
  48. X
  49. X  ---------------------------------------------------------------------------*/
  50. X
  51. X
  52. X#include "unzip.h"
  53. X#ifdef MSWIN
  54. X#  include "wizunzip.h"
  55. X#endif
  56. X
  57. X
  58. X
  59. X#ifndef ZIPINFO   /* no need to calculate CRCs */
  60. X
  61. X/**************************/
  62. X/*  Function UpdateCRC()  */
  63. X/**************************/
  64. X
  65. X /*--------------------------------------------------------------------
  66. X
  67. X   First, the polynomial itself and its table of feedback terms.  The
  68. X   polynomial is
  69. X   X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0
  70. X
  71. X   Note that we take it "backwards" and put the highest-order term in
  72. X   the lowest-order bit.  The X^32 term is "implied"; the LSB is the
  73. X   X^31 term, etc.  The X^0 term (usually shown as "+1") results in
  74. X   the MSB being 1.
  75. X
  76. X   Note that the usual hardware shift register implementation, which
  77. X   is what we're using (we're merely optimizing it by doing eight-bit
  78. X   chunks at a time) shifts bits into the lowest-order term.  In our
  79. X   implementation, that means shifting towards the right.  Why do we
  80. X   do it this way?  Because the calculated CRC must be transmitted in
  81. X   order from highest-order term to lowest-order term.  UARTs transmit
  82. X   characters in order from LSB to MSB.  By storing the CRC this way,
  83. X   we hand it to the UART in the order low-byte to high-byte; the UART
  84. X   sends each low-bit to hight-bit; and the result is transmission bit
  85. X   by bit from highest- to lowest-order term without requiring any bit
  86. X   shuffling on our part.  Reception works similarly.
  87. X
  88. X   The feedback terms table consists of 256, 32-bit entries.  Notes:
  89. X
  90. X       The table can be generated at runtime if desired; code to do so
  91. X       is shown later.  It might not be obvious, but the feedback
  92. X       terms simply represent the results of eight shift/xor opera-
  93. X       tions for all combinations of data and CRC register values.
  94. X
  95. X       The values must be right-shifted by eight bits by the "updcrc"
  96. X       logic; the shift must be unsigned (bring in zeroes).  On some
  97. X       hardware you could probably optimize the shift in assembler by
  98. X       using byte-swap instructions.
  99. X       polynomial $edb88320
  100. X
  101. X   --------------------------------------------------------------------*/
  102. X
  103. XULONG crc_32_tab[] =
  104. X{
  105. X    0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
  106. X    0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
  107. X    0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
  108. X    0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
  109. X    0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
  110. X    0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
  111. X    0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
  112. X    0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
  113. X    0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
  114. X    0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
  115. X    0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
  116. X    0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
  117. X    0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
  118. X    0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
  119. X    0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
  120. X    0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
  121. X    0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
  122. X    0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
  123. X    0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
  124. X    0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
  125. X    0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
  126. X    0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
  127. X    0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
  128. X    0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
  129. X    0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
  130. X    0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
  131. X    0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
  132. X    0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
  133. X    0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
  134. X    0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
  135. X    0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
  136. X    0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
  137. X    0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
  138. X    0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
  139. X    0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
  140. X    0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
  141. X    0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
  142. X    0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
  143. X    0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
  144. X    0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
  145. X    0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
  146. X    0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
  147. X    0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
  148. X    0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
  149. X    0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
  150. X    0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
  151. X    0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
  152. X    0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
  153. X    0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
  154. X    0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
  155. X    0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
  156. X    0x2d02ef8dL
  157. X}; /* end crc_32_tab[] */
  158. X
  159. X
  160. Xvoid UpdateCRC(s, len)
  161. X    register byte *s;
  162. X    register int len;
  163. X{
  164. X    register ULONG crcval = crc32val;
  165. X
  166. X    /* update running CRC calculation with contents of a buffer */
  167. X    while (len--)
  168. X        crcval = crc_32_tab[((byte) crcval ^ (*s++)) & 0xff] ^ (crcval >> 8);
  169. X    crc32val = crcval;
  170. X}
  171. X
  172. X#endif /* !ZIPINFO */
  173. X
  174. X
  175. X
  176. X
  177. X
  178. X/**************************/
  179. X/*  Function do_string()  */
  180. X/**************************/
  181. X
  182. Xint do_string(len, option)      /* return PK-type error code */
  183. X    unsigned int len;           /* without prototype, UWORD converted to this */
  184. X    int option;
  185. X{
  186. X    int block_length, error = 0;
  187. X    UWORD comment_bytes_left, extra_len;
  188. X
  189. X
  190. X/*---------------------------------------------------------------------------
  191. X    This function processes arbitrary-length (well, usually) strings.  Three
  192. X    options are allowed:  SKIP, wherein the string is skipped pretty logical,
  193. X    eh?); DISPLAY, wherein the string is printed to standard output after un-
  194. X    dergoing any necessary or unnecessary character conversions; and FILENAME,
  195. X    wherein the string is put into the filename[] array after undergoing ap-
  196. X    propriate conversions (including case-conversion, if that is indicated:
  197. X    see the global variable pInfo->lcflag).  The latter option should be OK,
  198. X    since filename is now dimensioned at 1025, but we check anyway.
  199. X
  200. X    The string, by the way, is assumed to start at the current file-pointer
  201. X    position; its length is given by len.  So start off by checking length
  202. X    of string:  if zero, we're already set.
  203. X  ---------------------------------------------------------------------------*/
  204. X
  205. X    if (!len)
  206. X        return (0);             /* 0:  no error */
  207. X
  208. X    switch (option) {
  209. X
  210. X    /*
  211. X     * First case:  print string on standard output.  First set loop vari-
  212. X     * ables, then loop through the comment in chunks of OUTBUFSIZ bytes,
  213. X     * converting formats and printing as we go.  The second half of the
  214. X     * loop conditional was added because the file might be truncated, in
  215. X     * which case comment_bytes_left will remain at some non-zero value for
  216. X     * all time.  outbuf is used as a scratch buffer because it is avail-
  217. X     * able (we should be either before or in between any file processing).
  218. X     * [The typecast in front of the MIN() macro was added because of the
  219. X     * new promotion rules under ANSI C; readbuf() wants an int, but MIN()
  220. X     * returns a signed long, if I understand things correctly.  The proto-
  221. X     * type should handle it, but just in case...]
  222. X     */
  223. X
  224. X    case DISPLAY:
  225. X        comment_bytes_left = len;
  226. X        block_length = OUTBUFSIZ;    /* for the while statement, first time */
  227. X        while (comment_bytes_left > 0 && block_length > 0) {
  228. X            if ((block_length = readbuf((char *)outbuf,
  229. X                   (int) MIN(OUTBUFSIZ, comment_bytes_left))) <= 0)
  230. X                return 51;                      /* 51:  unexpected EOF */
  231. X            comment_bytes_left -= block_length;
  232. X            NUKE_CRs(outbuf, block_length);     /* (modifies block_length) */
  233. X
  234. X            /*  this is why we allocated an extra byte for outbuf: */
  235. X            outbuf[block_length] = '\0';        /* terminate w/zero:  ASCIIZ */
  236. X
  237. X            A_TO_N(outbuf);     /* translate string to native */
  238. X
  239. X            printf("%s", outbuf);
  240. X        }
  241. X#ifdef MSWIN
  242. X        /* ran out of local mem -- had to cheat */
  243. X        WriteStringToMsgWin(outbuf, bRealTimeMsgUpdate);
  244. X#else /* !MSWIN */
  245. X        printf("\n");   /* assume no newline at end */
  246. X#endif /* ?MSWIN */
  247. X        break;
  248. X
  249. X    /*
  250. X     * Second case:  read string into filename[] array.  The filename should
  251. X     * never ever be longer than FILNAMSIZ-1 (1024), but for now we'll check,
  252. X     * just to be sure.
  253. X     */
  254. X
  255. X    case FILENAME:
  256. X        extra_len = 0;
  257. X        if (len >= FILNAMSIZ) {
  258. X            fprintf(stderr, "warning:  filename too long--truncating.\n");
  259. X            error = 1;          /* 1:  warning error */
  260. X            extra_len = len - FILNAMSIZ + 1;
  261. X            len = FILNAMSIZ - 1;
  262. X        }
  263. X        if (readbuf(filename, len) <= 0)
  264. X            return 51;          /* 51:  unexpected EOF */
  265. X        filename[len] = '\0';   /* terminate w/zero:  ASCIIZ */
  266. X
  267. X        A_TO_N(filename);       /* translate string to native */
  268. X
  269. X#ifndef ZIPINFO
  270. X        if (pInfo->lcflag)
  271. X            TOLOWER(filename, filename);  /* replace with lowercase filename */
  272. X#endif
  273. X
  274. X        if (!extra_len)         /* we're done here */
  275. X            break;
  276. X
  277. X        /*
  278. X         * We truncated the filename, so print what's left and then fall
  279. X         * through to the SKIP routine.
  280. X         */
  281. X        fprintf(stderr, "[ %s ]\n", filename);
  282. X        len = extra_len;
  283. X        /*  FALL THROUGH...  */
  284. X
  285. X    /*
  286. X     * Third case:  skip string, adjusting readbuf's internal variables
  287. X     * as necessary (and possibly skipping to and reading a new block of
  288. X     * data).
  289. X     */
  290. X
  291. X    case SKIP:
  292. X        LSEEK(cur_zipfile_bufstart + (inptr-inbuf) + len)
  293. X        break;
  294. X
  295. X    /*
  296. X     * Fourth case:  assume we're at the start of an "extra field"; malloc
  297. X     * storage for it and read data into the allocated space.
  298. X     */
  299. X
  300. X    case EXTRA_FIELD:
  301. X        if (extra_field != (byte *)NULL)
  302. X            free(extra_field);
  303. X        if ((extra_field = (byte *)malloc(len)) == (byte *)NULL) {
  304. X            fprintf(stderr,
  305. X              "warning:  extra field too long (%d).  Ignoring...\n", len);
  306. X            LSEEK(cur_zipfile_bufstart + (inptr-inbuf) + len)
  307. X        } else
  308. X            if (readbuf((char *)extra_field, len) <= 0)
  309. X                return 51;      /* 51:  unexpected EOF */
  310. X        break;
  311. X
  312. X    } /* end switch (option) */
  313. X    return error;
  314. X
  315. X} /* end function do_string() */
  316. X
  317. X
  318. X
  319. X
  320. X
  321. X#ifndef ZIPINFO
  322. X#ifndef VMS
  323. X
  324. X/*********************************/
  325. X/*  Function dos_to_unix_time()  */
  326. X/*********************************/
  327. X
  328. Xtime_t dos_to_unix_time(ddate, dtime)
  329. X    unsigned ddate, dtime;
  330. X{
  331. X    static short yday[]={0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
  332. X    int yr, mo, dy, hh, mm, ss, leap;
  333. X    long m_time, days=0;
  334. X#if (!defined(MACOS) && !defined(MSC) && !defined(__GO32__))
  335. X#if (defined(BSD) || defined(MTS))
  336. X#ifndef __386BSD__
  337. X    static struct timeb tbp;
  338. X#endif /* __386BSD__ */
  339. X#else /* !(BSD || MTS) */
  340. X    extern long timezone;    /* declared in <time.h> for MSC (& Borland?) */
  341. X#endif /* ?(BSD || MTS) */
  342. X#endif /* !MACOS && !MSC && !__GO32__ */
  343. X
  344. X#   define YRBASE  1970
  345. X
  346. X    /* dissect date */
  347. X    yr = ((ddate >> 9) & 0x7f) + (1980 - YRBASE);
  348. X    mo = ((ddate >> 5) & 0x0f) - 1;
  349. X    dy = (ddate & 0x1f) - 1;
  350. X
  351. X    /* dissect time */
  352. X    hh = (dtime >> 11) & 0x1f;
  353. X    mm = (dtime >> 5) & 0x3f;
  354. X    ss = (dtime & 0x1f) * 2;
  355. X
  356. X    /* leap = # of leap years from BASE up to but not including current year */
  357. X    leap = ((yr + YRBASE - 1) / 4);   /* leap year base factor */
  358. X
  359. X    /* How many days from BASE to this year? (& add expired days this year) */
  360. X    days = (yr * 365) + (leap - 492) + yday[mo];
  361. X
  362. X    /* if year is a leap year and month is after February, add another day */
  363. X    if ((mo > 1) && ((yr+YRBASE)%4 == 0) && ((yr+YRBASE) != 2100))
  364. X        ++days;                 /* OK through 2199 */
  365. X
  366. X    /* convert date & time to seconds relative to 00:00:00, 01/01/YRBASE */
  367. X    m_time = ((long)(days + dy) * 86400L) + ((long)hh * 3600) + (mm * 60) + ss;
  368. X      /* - 1;   MS-DOS times always rounded up to nearest even second */
  369. X
  370. X#if (!defined(MACOS) && !defined(__GO32__))
  371. X#if (defined(BSD) || defined(MTS))
  372. X#ifndef __386BSD__
  373. X    ftime(&tbp);
  374. X    m_time += tbp.timezone * 60L;
  375. X#endif
  376. X#else /* !(BSD || MTS) */
  377. X#ifdef WIN32
  378. X    /* later... */
  379. X#else /* !WIN32 */
  380. X    tzset();                    /* set `timezone' */
  381. X#endif /* ?WIN32 */
  382. X    m_time += timezone;         /* account for timezone differences */
  383. X#endif /* ?(BSD || MTS) */
  384. X#endif /* !MACOS && !__GO32__ */
  385. X
  386. X#ifdef __386BSD__
  387. X    m_time += localtime((time_t *) &m_time))->tm_gmtoff;
  388. X#else
  389. X    if (localtime((time_t *)&m_time)->tm_isdst)
  390. X        m_time -= 60L * 60L;    /* adjust for daylight savings time */
  391. X#endif /* __386BSD__ */
  392. X
  393. X    return m_time;
  394. X
  395. X} /* end function dos_to_unix_time() */
  396. X
  397. X#endif /* !VMS */
  398. X
  399. X
  400. X
  401. X
  402. X
  403. X/********************************/
  404. X/*  Function check_for_newer()  */  /* could make this into a macro for Unix */
  405. X/********************************/
  406. X
  407. Xint check_for_newer(filename)   /* return 1 if existing file newer or equal; */
  408. X    char *filename;             /*  0 if older; -1 if doesn't exist yet */
  409. X{
  410. X#ifdef VMS
  411. X    unsigned short timbuf[7];
  412. X    int dy, mo, yr, hh, mm, ss, dy2, mo2, yr2, hh2, mm2, ss2;
  413. X    struct FAB fab;
  414. X    struct XABDAT xdat;
  415. X
  416. X
  417. X    if (stat(filename, &statbuf))
  418. X        return DOES_NOT_EXIST;
  419. X
  420. X    fab  = cc$rms_fab;
  421. X    xdat = cc$rms_xabdat;
  422. X
  423. X    fab.fab$l_xab = &xdat;
  424. X    fab.fab$l_fna = filename;
  425. X    fab.fab$b_fns = strlen(filename);
  426. X    fab.fab$l_fop = FAB$M_GET | FAB$M_UFO;
  427. X
  428. X    if ((sys$open(&fab) & 1) == 0)       /* open failure:  report exists and */
  429. X        return EXISTS_AND_OLDER;         /*  older so new copy will be made  */
  430. X    sys$numtim(&timbuf,&xdat.xab$q_cdt);
  431. X    fab.fab$l_xab = 0L;
  432. X
  433. X    sys$dassgn(fab.fab$l_stv);
  434. X    sys$close(&fab);   /* be sure file is closed and RMS knows about it */
  435. X
  436. X    yr = timbuf[0];
  437. X    yr2 = ((lrec.last_mod_file_date >> 9) & 0x7f) + 1980;
  438. X    if (yr > yr2)
  439. X        return EXISTS_AND_NEWER;
  440. X    else if (yr < yr2)
  441. X        return EXISTS_AND_OLDER;
  442. X
  443. X    mo = timbuf[1];
  444. X    mo2 = ((lrec.last_mod_file_date >> 5) & 0x0f);
  445. X    if (mo > mo2)
  446. X        return EXISTS_AND_NEWER;
  447. X    else if (mo < mo2)
  448. X        return EXISTS_AND_OLDER;
  449. X
  450. X    dy = timbuf[2];
  451. X    dy2 = (lrec.last_mod_file_date & 0x1f);
  452. X    if (dy > dy2)
  453. X        return EXISTS_AND_NEWER;
  454. X    else if (dy < dy2)
  455. X        return EXISTS_AND_OLDER;
  456. X
  457. X    hh = timbuf[3];
  458. X    hh2 = (lrec.last_mod_file_time >> 11) & 0x1f;
  459. X    if (hh > hh2)
  460. X        return EXISTS_AND_NEWER;
  461. X    else if (hh < hh2)
  462. X        return EXISTS_AND_OLDER;
  463. X
  464. X    mm = timbuf[4];
  465. X    mm2 = (lrec.last_mod_file_time >> 5) & 0x3f;
  466. X    if (mm > mm2)
  467. X        return EXISTS_AND_NEWER;
  468. X    else if (mm < mm2)
  469. X        return EXISTS_AND_OLDER;
  470. X
  471. X    /* round to nearest 2 secs--may become 60, but doesn't matter for compare */
  472. X    ss = (int)((float)timbuf[5] + (float)timbuf[6]*.01 + 1.) & -2;
  473. X    ss2 = (lrec.last_mod_file_time & 0x1f) * 2;
  474. X    if (ss >= ss2)
  475. X        return EXISTS_AND_NEWER;
  476. X
  477. X    return EXISTS_AND_OLDER;
  478. X
  479. X#else /* !VMS */
  480. X#ifdef OS2
  481. X    long existing, archive;
  482. X
  483. X    if ((existing = GetFileTime(filename)) == -1)
  484. X        return DOES_NOT_EXIST;
  485. X    archive = ((long) lrec.last_mod_file_date) << 16 | lrec.last_mod_file_time;
  486. X
  487. X    return (existing >= archive);
  488. X#else /* !OS2 */
  489. X    time_t existing, archive;
  490. X
  491. X    if (stat(filename, &statbuf))
  492. X        return DOES_NOT_EXIST;
  493. X
  494. X    /* round up existing filetime to nearest 2 seconds for comparison */
  495. X    existing = (statbuf.st_mtime & 1) ? statbuf.st_mtime+1 : statbuf.st_mtime;
  496. X    archive  = dos_to_unix_time(lrec.last_mod_file_date,
  497. X                                lrec.last_mod_file_time);
  498. X
  499. X    return (existing >= archive);
  500. X#endif /* ?OS2 */
  501. X#endif /* ?VMS */
  502. X
  503. X} /* end function check_for_newer() */
  504. X
  505. X
  506. X
  507. X
  508. X
  509. X/***************************/
  510. X/*  Function dateformat()  */
  511. X/***************************/
  512. X
  513. Xint dateformat()
  514. X{
  515. X
  516. X/*-----------------------------------------------------------------------------
  517. X  For those operating systems which support it, this function returns a value
  518. X  which tells how national convention says that numeric dates are displayed.
  519. X
  520. X  Return values are DF_YMD, DF_DMY and DF_MDY.  The meanings should be fairly
  521. X  obvious.
  522. X -----------------------------------------------------------------------------*/
  523. X
  524. X#ifdef OS2
  525. X    switch (GetCountryInfo()) {
  526. X            case 0 /* DATEFMT_MM_DD_YY */ :
  527. X                return DF_MDY;
  528. X            case 1 /* DATEFMT_DD_MM_YY */ :
  529. X                return DF_DMY;
  530. X            case 2 /* DATEFMT_YY_MM_DD */ :
  531. X                return DF_YMD;
  532. X        }
  533. X#else /* !OS2 */
  534. X#if (defined(MSDOS) && !defined(MSWIN))
  535. X    unsigned short _CountryInfo[18];
  536. X#ifdef __GO32__
  537. X    unsigned short *CountryInfo = _CountryInfo;
  538. X
  539. X    bdos(0x38, (unsigned)CountryInfo, 0);
  540. X#else /* !__GO32__ */
  541. X    unsigned short far *CountryInfo = _CountryInfo;
  542. X    union REGS regs;
  543. X    struct SREGS sregs;
  544. X
  545. X    regs.x.ax = 0x3800;
  546. X    regs.x.dx = FP_OFF(CountryInfo);
  547. X    sregs.ds  = FP_SEG(CountryInfo);
  548. X    int86x(0x21, ®s, ®s, &sregs);
  549. X#endif /* ?__GO32__ */
  550. X
  551. X    switch(CountryInfo[0]) {
  552. X        case 0:
  553. X            return DF_MDY;
  554. X        case 1:
  555. X            return DF_DMY;
  556. X        case 2:
  557. X            return DF_YMD;
  558. X    }
  559. X#endif /* MSDOS && !MSWIN */
  560. X#endif /* ?OS2 */
  561. X
  562. X    return DF_MDY;   /* default for Unix, VMS, etc. */
  563. X
  564. X} /* end function dateformat() */
  565. X
  566. X#endif /* !ZIPINFO */
  567. X
  568. X
  569. X
  570. X
  571. X
  572. X#ifdef EBCDIC
  573. X
  574. X/*
  575. X * This is the MTS ASCII->EBCDIC translation table. It provides a 1-1
  576. X * translation from ISO 8859/1 8-bit ASCII to IBM Code Page 37 EBCDIC.
  577. X */
  578. X
  579. Xunsigned char ebcdic[] =
  580. X{
  581. X    0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f,
  582. X    0x16, 0x05, 0x25, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  583. X    0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26,
  584. X    0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f,
  585. X    0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d,
  586. X    0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61,
  587. X    0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  588. X    0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f,
  589. X    0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  590. X    0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
  591. X    0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
  592. X    0xe7, 0xe8, 0xe9, 0xba, 0xe0, 0xbb, 0xb0, 0x6d,
  593. X    0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  594. X    0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
  595. X    0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6,
  596. X    0xa7, 0xa8, 0xa9, 0xc0, 0x4f, 0xd0, 0xa1, 0x07,
  597. X    0x20, 0x21, 0x22, 0x23, 0x24, 0x15, 0x06, 0x17,
  598. X    0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x1b,
  599. X    0x30, 0x31, 0x1a, 0x33, 0x34, 0x35, 0x36, 0x08,
  600. X    0x38, 0x39, 0x3a, 0x3b, 0x04, 0x14, 0x3e, 0xff,
  601. X    0x41, 0xaa, 0x4a, 0xb1, 0x9f, 0xb2, 0x6a, 0xb5,
  602. X    0xbd, 0xb4, 0x9a, 0x8a, 0x5f, 0xca, 0xaf, 0xbc,
  603. X    0x90, 0x8f, 0xea, 0xfa, 0xbe, 0xa0, 0xb6, 0xb3,
  604. X    0x9d, 0xda, 0x9b, 0x8b, 0xb7, 0xb8, 0xb9, 0xab,
  605. X    0x64, 0x65, 0x62, 0x66, 0x63, 0x67, 0x9e, 0x68,
  606. X    0x74, 0x71, 0x72, 0x73, 0x78, 0x75, 0x76, 0x77,
  607. X    0xac, 0x69, 0xed, 0xee, 0xeb, 0xef, 0xec, 0xbf,
  608. X    0x80, 0xfd, 0xfe, 0xfb, 0xfc, 0xad, 0xae, 0x59,
  609. X    0x44, 0x45, 0x42, 0x46, 0x43, 0x47, 0x9c, 0x48,
  610. X    0x54, 0x51, 0x52, 0x53, 0x58, 0x55, 0x56, 0x57,
  611. X    0x8c, 0x49, 0xcd, 0xce, 0xcb, 0xcf, 0xcc, 0xe1,
  612. X    0x70, 0xdd, 0xde, 0xdb, 0xdc, 0x8d, 0x8e, 0xdf
  613. X}; /* end ebcdic[] */
  614. X
  615. X#endif                          /* EBCDIC */
  616. X
  617. X
  618. X
  619. X
  620. X
  621. X/*************************/
  622. X/*  Function makeword()  */
  623. X/*************************/
  624. X
  625. XUWORD makeword(b)
  626. X    byte *b;
  627. X{
  628. X    /*
  629. X     * Convert Intel style 'short' integer to non-Intel non-16-bit
  630. X     * host format.  This routine also takes care of byte-ordering.
  631. X     */
  632. X    return ((b[1] << 8) | b[0]);
  633. X}
  634. X
  635. X
  636. X
  637. X
  638. X
  639. X/*************************/
  640. X/*  Function makelong()  */
  641. X/*************************/
  642. X
  643. XULONG makelong(sig)
  644. X    byte *sig;
  645. X{
  646. X    /*
  647. X     * Convert intel style 'long' variable to non-Intel non-16-bit
  648. X     * host format.  This routine also takes care of byte-ordering.
  649. X     */
  650. X    return (((ULONG) sig[3]) << 24)
  651. X        + (((ULONG) sig[2]) << 16)
  652. X        + (((ULONG) sig[1]) << 8)
  653. X        + ((ULONG) sig[0]);
  654. X}
  655. X
  656. X
  657. X
  658. X
  659. X
  660. X#ifdef VMS
  661. X
  662. X/***************************/
  663. X/*  Function return_VMS()  */
  664. X/***************************/
  665. X
  666. Xvoid return_VMS(zip_error)
  667. X    int zip_error;
  668. X{
  669. X#ifdef RETURN_CODES
  670. X/*---------------------------------------------------------------------------
  671. X    Do our own, explicit processing of error codes and print message, since
  672. X    VMS misinterprets return codes as rather obnoxious system errors ("access
  673. X    violation," for example).
  674. X  ---------------------------------------------------------------------------*/
  675. X
  676. X    switch (zip_error) {
  677. X
  678. X    case 0:
  679. X        break;   /* life is fine... */
  680. X    case 1:
  681. X        fprintf(stderr, "\n[return-code 1:  warning error \
  682. X(e.g., failed CRC or unknown compression method)]\n");
  683. X        break;
  684. X    case 2:
  685. X    case 3:
  686. X        fprintf(stderr, "\n[return-code %d:  error in zipfile \
  687. X(e.g., can't find local file header sig)]\n",
  688. X                zip_error);
  689. X        break;
  690. X    case 4:
  691. X    case 5:
  692. X    case 6:
  693. X    case 7:
  694. X    case 8:
  695. X        fprintf(stderr, "\n[return-code %d:  insufficient memory]\n",
  696. X          zip_error);
  697. X        break;
  698. X    case 9:
  699. X        fprintf(stderr, "\n[return-code 9:  zipfile not found]\n");
  700. X        break;
  701. X    case 10:   /* this is the one that gives "access violation," I think */
  702. X        fprintf(stderr, "\n[return-code 10:  bad or illegal parameters \
  703. Xspecified on command line]\n");
  704. X        break;
  705. X    case 11:   /* I'm not sure this one is implemented, but maybe soon? */
  706. X        fprintf(stderr,
  707. X          "\n[return-code 11:  no files found to extract/view/etc.]\n");
  708. X        break;
  709. X    case 50:
  710. X        fprintf(stderr,
  711. X  "\n[return-code 50:  disk full (or otherwise unable to open output file)]\n");
  712. X        break;
  713. X    case 51:
  714. X        fprintf(stderr,
  715. X          "\n[return-code 51:  unexpected EOF in zipfile (i.e., truncated)]\n");
  716. X        break;
  717. X    default:
  718. X        fprintf(stderr, "\n[return-code %d:  unknown return-code \
  719. X(who put this one in?  Wasn't me...)]\n",
  720. X                zip_error);
  721. X        break;
  722. X    }
  723. X#endif /* RETURN_CODES */
  724. X
  725. X    exit(0);   /* everything okey-dokey as far as VMS concerned */
  726. X
  727. X} /* end function return_VMS() */
  728. X
  729. X#endif /* VMS */
  730. X
  731. X
  732. X
  733. X
  734. X
  735. X#ifdef ZMEM   /* memset, memcpy for systems without them */
  736. X
  737. X/***********************/
  738. X/*  Function memset()  */
  739. X/***********************/
  740. X
  741. Xchar *memset(buf, init, len)
  742. X    register char *buf, init;   /* buffer loc and initializer */
  743. X    register unsigned int len;  /* length of the buffer */
  744. X{
  745. X    char *start;
  746. X
  747. X    start = buf;
  748. X    while (len--)
  749. X        *(buf++) = init;
  750. X    return (start);
  751. X}
  752. X
  753. X
  754. X
  755. X
  756. X
  757. X/***********************/
  758. X/*  Function memcpy()  */
  759. X/***********************/
  760. X
  761. Xchar *memcpy(dst, src, len)
  762. X    register char *dst, *src;
  763. X    register unsigned int len;
  764. X{
  765. X    char *start;
  766. X
  767. X    start = dst;
  768. X    while (len-- > 0)
  769. X        *dst++ = *src++;
  770. X    return (start);
  771. X}
  772. X
  773. X#endif /* ZMEM */
  774. END_OF_FILE
  775.   if test 24826 -ne `wc -c <'misc.c'`; then
  776.     echo shar: \"'misc.c'\" unpacked with wrong size!
  777.   fi
  778.   # end of 'misc.c'
  779. fi
  780. if test -f 'unzip.c.B' -a "${1}" != "-c" ; then 
  781.   echo shar: Will not clobber existing file \"'unzip.c.B'\"
  782. else
  783.   echo shar: Extracting \"'unzip.c.B'\" \(31190 characters\)
  784.   sed "s/^X//" >'unzip.c.B' <<'END_OF_FILE'
  785. X
  786. X/********************************/
  787. X/*  Function process_zipfile()  */
  788. X/********************************/
  789. X
  790. Xint process_zipfile()    /* return PK-type error code */
  791. X{
  792. X    int error=0, error_in_archive;
  793. X    longint real_ecrec_offset, expect_ecrec_offset;
  794. X
  795. X
  796. X/*---------------------------------------------------------------------------
  797. X    Open the zipfile for reading and in BINARY mode to prevent CR/LF trans-
  798. X    lation, which would corrupt the bitstreams.
  799. X  ---------------------------------------------------------------------------*/
  800. X
  801. X#ifdef VMS
  802. X    if (check_format())         /* check for variable-length format */
  803. X        return 2;               /* 2:  error in zipfile */
  804. X#endif /* VMS */
  805. X
  806. X    if (open_input_file())      /* this should never happen, given the */
  807. X        return 9;               /*   stat() test in main(), but... */
  808. X
  809. X/*---------------------------------------------------------------------------
  810. X    Reconstruct the various PK signature strings, and find and process the
  811. X    end-of-central-directory header.
  812. X  ---------------------------------------------------------------------------*/
  813. X
  814. X    strcat(local_hdr_sig, LOCAL_HDR_SIG);
  815. X    strcat(central_hdr_sig, CENTRAL_HDR_SIG);
  816. X    strcat(end_central_sig, END_CENTRAL_SIG);
  817. X/*  strcat(extd_local_sig, EXTD_LOCAL_SIG);  */
  818. X
  819. X    if (find_end_central_dir()) {   /* not found; nothing to do */
  820. X        close(zipfd);
  821. X        return 2;                   /* 2:  error in zipfile */
  822. X    }
  823. X
  824. X    real_ecrec_offset = cur_zipfile_bufstart+(inptr-inbuf);
  825. X#ifdef TEST
  826. X    printf("\n  found end-of-central-dir signature at offset %ld (%.8lXh)\n",
  827. X      real_ecrec_offset, real_ecrec_offset);
  828. X    printf("    from beginning of file; offset %d (%.4Xh) within block\n",
  829. X      inptr-inbuf, inptr-inbuf);
  830. X#endif
  831. X
  832. X    if ((error_in_archive = process_end_central_dir()) > 1) {
  833. X        close(zipfd);
  834. X        return error_in_archive;
  835. X    }
  836. X
  837. X    if (zflag) {
  838. X        close(zipfd);
  839. X        return 0;
  840. X    }
  841. X
  842. X/*---------------------------------------------------------------------------
  843. X    Test the end-of-central-directory info for incompatibilities and incon-
  844. X    sistencies.
  845. X  ---------------------------------------------------------------------------*/
  846. X
  847. X#ifndef PAKFIX
  848. X    if (ecrec.number_this_disk == 0) {
  849. X#else /* PAKFIX */
  850. X    error = ((ecrec.number_this_disk == 1) &&
  851. X             (ecrec.num_disk_with_start_central_dir == 1));
  852. X    if ((ecrec.number_this_disk == 0) || error) {
  853. X        if (error) {
  854. X            fprintf(stderr,
  855. X     "\n     Warning:  zipfile claims to be disk 2 of a two-part archive;\n\
  856. X     attempting to process anyway.  If no further errors occur, this\n\
  857. X     archive was probably created by PAK v2.51 or earlier.  This bug\n\
  858. X     was reported to NoGate in March 1991 and was supposed to have been\n\
  859. X     fixed by mid-1991; as of mid-1992 it still hadn't been.\n\n");
  860. X            error_in_archive = 1;  /* 1:  warning */
  861. X        }
  862. X#endif /* ?PAKFIX */
  863. X        expect_ecrec_offset = ecrec.offset_start_central_directory +
  864. X                              ecrec.size_central_directory;
  865. X        if ((extra_bytes = real_ecrec_offset - expect_ecrec_offset) < 0) {
  866. X            fprintf(stderr, "\nerror:  missing %ld bytes in zipfile (\
  867. Xattempting to process anyway)\n\n", -extra_bytes);
  868. X            error_in_archive = 2;       /* 2:  (weak) error in zipfile */
  869. X        } else if (extra_bytes > 0) {
  870. X            if ((ecrec.offset_start_central_directory == 0) &&
  871. X                (ecrec.size_central_directory != 0))   /* zip 1.5 -go bug */
  872. X            {
  873. X                fprintf(stderr, "\nerror:  NULL central directory offset (\
  874. Xattempting to process anyway)\n\n");
  875. X                ecrec.offset_start_central_directory = extra_bytes;
  876. X                extra_bytes = 0;
  877. X                error_in_archive = 2;   /* 2:  (weak) error in zipfile */
  878. X            } else {
  879. X                fprintf(stderr, "\nwarning:  extra %ld bytes at beginning or\
  880. X within zipfile\n          (attempting to process anyway)\n\n", extra_bytes);
  881. X                error_in_archive = 1;   /* 1:  warning error */
  882. X            }
  883. X        }
  884. X
  885. X    /*-----------------------------------------------------------------------
  886. X        Check for empty zipfile and exit now if so.
  887. X      -----------------------------------------------------------------------*/
  888. X
  889. X        if (expect_ecrec_offset == 0L  &&  ecrec.size_central_directory == 0) {
  890. X            fprintf(stderr, "warning:  zipfile is empty\n");
  891. X            close(zipfd);
  892. X            return (error_in_archive > 1)? error_in_archive : 1;
  893. X        }
  894. X
  895. X    /*-----------------------------------------------------------------------
  896. X        Compensate for missing or extra bytes, and seek to where the start
  897. X        of central directory should be.  If header not found, uncompensate
  898. X        and try again (necessary for at least some Atari archives created
  899. X        with STZIP, as well as archives created by J.H. Holm's ZIPSPLIT).
  900. X      -----------------------------------------------------------------------*/
  901. X
  902. X        LSEEK( ecrec.offset_start_central_directory )
  903. X        if ((readbuf(sig, 4) <= 0) || strncmp(sig, central_hdr_sig, 4)) {
  904. X            longint tmp = extra_bytes;
  905. X
  906. X            extra_bytes = 0;
  907. X            LSEEK( ecrec.offset_start_central_directory )
  908. X            if ((readbuf(sig, 4) <= 0) || strncmp(sig, central_hdr_sig, 4)) {
  909. X                fprintf(stderr,
  910. X            "error:  start of central directory not found; zipfile corrupt.\n");
  911. X                fprintf(stderr, ReportMsg);
  912. X                close(zipfd);
  913. X                return 3;           /* 3:  severe error in zipfile */
  914. X            }
  915. X            fprintf(stderr, "error:  reported length of central directory is \
  916. X%d bytes too\n        long (Atari STZIP zipfile?  J.H. Holm ZIPSPLIT zipfile?)\
  917. X.\n        Compensating...\n\n", -tmp);
  918. X            error_in_archive = 2;   /* 2:  (weak) error in zipfile */
  919. X        }
  920. X
  921. X    /*-----------------------------------------------------------------------
  922. X        Seek to the start of the central directory one last time, since we
  923. X        have just read the first entry's signature bytes; then list, extract
  924. X        or test member files as instructed, and close the zipfile.
  925. X      -----------------------------------------------------------------------*/
  926. X
  927. X        LSEEK( ecrec.offset_start_central_directory )
  928. X        if (vflag)
  929. X            error = list_files();               /* LIST 'EM */
  930. X        else
  931. X            error = extract_or_test_files();    /* EXTRACT OR TEST 'EM */
  932. X        if (error > error_in_archive)   /* don't overwrite stronger error */
  933. X            error_in_archive = error;   /*  with (for example) a warning */
  934. X    } else {
  935. X        fprintf(stderr, "\nerror:  zipfile is part of multi-disk archive \
  936. X(sorry, not supported).\n");
  937. X    /*  fprintf(stderr, "Please report to zip-bugs@cs.ucla.edu\n");  */
  938. X        error_in_archive = 11;  /* 11:  no files found */
  939. X    }
  940. X
  941. X    close(zipfd);
  942. X    return error_in_archive;
  943. X
  944. X} /* end function process_zipfile() */
  945. X
  946. X
  947. X
  948. X
  949. X
  950. X/************************************/
  951. X/*  Function find_end_central_dir() */
  952. X/************************************/
  953. X
  954. Xint find_end_central_dir()   /* return 0 if found, 1 otherwise */
  955. X{
  956. X    int i, numblks;
  957. X    longint tail_len;
  958. X
  959. X
  960. X
  961. X/*---------------------------------------------------------------------------
  962. X    Treat case of short zipfile separately.
  963. X  ---------------------------------------------------------------------------*/
  964. X
  965. X    if (ziplen <= INBUFSIZ) {
  966. X        lseek(zipfd, 0L, SEEK_SET);
  967. X        if ((incnt = read(zipfd,(char *)inbuf,(unsigned int)ziplen)) ==
  968. X             (int)ziplen)
  969. X
  970. X            /* 'P' must be at least 22 bytes from end of zipfile */
  971. X            for (inptr = inbuf+(int)ziplen-22;  inptr >= inbuf;  --inptr)
  972. X                if ((ascii_to_native(*inptr) == 'P')  &&
  973. X                     !strncmp((char *)inptr, end_central_sig, 4)) {
  974. X                    incnt -= inptr - inbuf;
  975. X                    return 0;   /* found it! */
  976. X                }               /* ...otherwise fall through & fail */
  977. X
  978. X/*---------------------------------------------------------------------------
  979. X    Zipfile is longer than INBUFSIZ:  may need to loop.  Start with short
  980. X    block at end of zipfile (if not TOO short).
  981. X  ---------------------------------------------------------------------------*/
  982. X
  983. X    } else {
  984. X        if ((tail_len = ziplen % INBUFSIZ) > ECREC_SIZE) {
  985. X            cur_zipfile_bufstart = lseek(zipfd, ziplen-tail_len, SEEK_SET);
  986. X            if ((incnt = read(zipfd,(char *)inbuf,(unsigned int)tail_len)) !=
  987. X                 (int)tail_len)
  988. X                goto fail;      /* shut up; it's expedient. */
  989. X
  990. X            /* 'P' must be at least 22 bytes from end of zipfile */
  991. X            for (inptr = inbuf+(int)tail_len-22;  inptr >= inbuf;  --inptr)
  992. X                if ((ascii_to_native(*inptr) == 'P')  &&
  993. X                     !strncmp((char *)inptr, end_central_sig, 4)) {
  994. X                    incnt -= inptr - inbuf;
  995. X                    return 0;   /* found it */
  996. X                }               /* ...otherwise search next block */
  997. X            strncpy((char *)hold, (char *)inbuf, 3);    /* sig may span block
  998. X                                                           boundary */
  999. X        } else {
  1000. X            cur_zipfile_bufstart = ziplen - tail_len;
  1001. X        }
  1002. X
  1003. X    /*-----------------------------------------------------------------------
  1004. X        Loop through blocks of zipfile data, starting at the end and going
  1005. X        toward the beginning.  Need only check last 65557 bytes of zipfile:
  1006. X        comment may be up to 65535 bytes long, end-of-central-directory rec-
  1007. X        ord is 18 bytes (shouldn't hardcode this number, but what the hell:
  1008. X        already did so above (22=18+4)), and sig itself is 4 bytes.
  1009. X      -----------------------------------------------------------------------*/
  1010. X
  1011. X        numblks = (int)
  1012. X                  ((MIN(ziplen,65557L) - tail_len + (INBUFSIZ-1)) / INBUFSIZ);
  1013. X        /*          =amount to search=   ==done==   ==rounding==    =blksiz= */
  1014. X
  1015. X        for (i = 1;  i <= numblks;  ++i) {
  1016. X            cur_zipfile_bufstart -= INBUFSIZ;
  1017. X            lseek(zipfd, cur_zipfile_bufstart, SEEK_SET);
  1018. X            if ((incnt = read(zipfd,(char *)inbuf,INBUFSIZ)) != INBUFSIZ)
  1019. X                break;          /* fall through and fail */
  1020. X
  1021. X            for (inptr = inbuf+INBUFSIZ-1;  inptr >= inbuf;  --inptr)
  1022. X                if ((ascii_to_native(*inptr) == 'P')  &&
  1023. X                     !strncmp((char *)inptr, end_central_sig, 4)) {
  1024. X                    incnt -= inptr - inbuf;
  1025. X                    return 0;   /* found it */
  1026. X                }
  1027. X            strncpy((char *)hold, (char *)inbuf, 3);    /* sig may span block
  1028. X                                                           boundary */
  1029. X        }
  1030. X
  1031. X    } /* end if (ziplen > INBUFSIZ) */
  1032. X
  1033. X/*---------------------------------------------------------------------------
  1034. X    Searched through whole region where signature should be without finding
  1035. X    it.  Print informational message and die a horrible death.
  1036. X  ---------------------------------------------------------------------------*/
  1037. X
  1038. Xfail:
  1039. X#ifdef MSWIN
  1040. X    MessageBeep(1);
  1041. X#endif
  1042. X
  1043. X    fprintf(stderr, "\nFile:  %s\n\n\
  1044. X     End-of-central-directory signature not found.  Either this file is not\n\
  1045. X     a zipfile, or it constitutes one disk of a multi-part archive.  In the\n\
  1046. X     latter case the central directory and zipfile comment will be found on\n\
  1047. X     the last disk(s) of this archive.\n", zipfn);
  1048. X    return 1;
  1049. X
  1050. X} /* end function find_end_central_dir() */
  1051. X
  1052. X
  1053. X
  1054. X
  1055. X
  1056. X/***************************************/
  1057. X/*  Function process_end_central_dir() */
  1058. X/***************************************/
  1059. X
  1060. Xint process_end_central_dir()    /* return PK-type error code */
  1061. X{
  1062. X    ec_byte_rec byterec;
  1063. X    int error=0;
  1064. X
  1065. X
  1066. X/*---------------------------------------------------------------------------
  1067. X    Read the end-of-central-directory record and do any necessary machine-
  1068. X    type conversions (byte ordering, structure padding compensation) by
  1069. X    reading data into character array, then copying to struct.
  1070. X  ---------------------------------------------------------------------------*/
  1071. X
  1072. X    if (readbuf((char *) byterec, ECREC_SIZE+4) <= 0)
  1073. X        return 51;
  1074. X
  1075. X    ecrec.number_this_disk =
  1076. X        makeword(&byterec[NUMBER_THIS_DISK]);
  1077. X    ecrec.num_disk_with_start_central_dir =
  1078. X        makeword(&byterec[NUM_DISK_WITH_START_CENTRAL_DIR]);
  1079. X    ecrec.num_entries_centrl_dir_ths_disk =
  1080. X        makeword(&byterec[NUM_ENTRIES_CENTRL_DIR_THS_DISK]);
  1081. X    ecrec.total_entries_central_dir =
  1082. X        makeword(&byterec[TOTAL_ENTRIES_CENTRAL_DIR]);
  1083. X    ecrec.size_central_directory =
  1084. X        makelong(&byterec[SIZE_CENTRAL_DIRECTORY]);
  1085. X    ecrec.offset_start_central_directory =
  1086. X        makelong(&byterec[OFFSET_START_CENTRAL_DIRECTORY]);
  1087. X    ecrec.zipfile_comment_length =
  1088. X        makeword(&byterec[ZIPFILE_COMMENT_LENGTH]);
  1089. X
  1090. X/*---------------------------------------------------------------------------
  1091. X    Get the zipfile comment, if any, and print it out.  (Comment may be up
  1092. X    to 64KB long.  May the fleas of a thousand camels infest the armpits of
  1093. X    anyone who actually takes advantage of this fact.)  Then position the
  1094. X    file pointer to the beginning of the central directory and fill buffer.
  1095. X  ---------------------------------------------------------------------------*/
  1096. X
  1097. X#ifdef MSWIN
  1098. X    cchComment = ecrec.zipfile_comment_length; /* save for comment button */
  1099. X    if (ecrec.zipfile_comment_length && zflag) {
  1100. X#else /* !MSWIN */
  1101. X    if (ecrec.zipfile_comment_length && !quietflg) {
  1102. X        if (!zflag)
  1103. X          printf("[%s] comment:\n", zipfn);
  1104. X#endif /* ?MSWIN */
  1105. X        if (do_string(ecrec.zipfile_comment_length,DISPLAY)) {
  1106. X            fprintf(stderr, "\ncaution:  zipfile comment truncated\n");
  1107. X            error = 1;          /* 1:  warning error */
  1108. X        }
  1109. X    }
  1110. X
  1111. X    return error;
  1112. X
  1113. X} /* end function process_end_central_dir() */
  1114. X
  1115. X
  1116. X
  1117. X
  1118. X
  1119. X/* also referenced in UpdateListBox() in updatelb.c (Windows version) */
  1120. Xchar *Headers[][2] = {
  1121. X    {" Length    Date    Time    Name",
  1122. X     " ------    ----    ----    ----"},
  1123. X    {" Length  Method   Size  Ratio   Date    Time   CRC-32     Name",
  1124. X     " ------  ------   ----  -----   ----    ----   ------     ----"}
  1125. X};
  1126. X
  1127. X/*************************/
  1128. X/* Function list_files() */
  1129. X/*************************/
  1130. X
  1131. Xint list_files()    /* return PK-type error code */
  1132. X{
  1133. X    char **fnamev;
  1134. X    int do_this_file=FALSE, ratio, error, error_in_archive=0;
  1135. X    int which_hdr=(vflag>1), date_format;
  1136. X    UWORD j, yr, mo, dy, hh, mm, members=0;
  1137. X    ULONG tot_csize=0L, tot_ucsize=0L;
  1138. X#ifdef OS2
  1139. X    ULONG tot_easize=0L, tot_eafiles=0L, ea_size;
  1140. X#endif
  1141. X#ifdef MSWIN
  1142. X    PSTR psLBEntry;  /* list box entry */
  1143. X#endif
  1144. X    min_info info;
  1145. X    static char *method[NUM_METHODS+1] =
  1146. X        {"Stored", "Shrunk", "Reduce1", "Reduce2", "Reduce3", "Reduce4",
  1147. X         "Implode", "Token", "Deflate", unkn};
  1148. X
  1149. X
  1150. X
  1151. X/*---------------------------------------------------------------------------
  1152. X    Unlike extract_or_test_files(), this routine confines itself to the cen-
  1153. X    tral directory.  Thus its structure is somewhat simpler, since we can do
  1154. X    just a single loop through the entire directory, listing files as we go.
  1155. X
  1156. X    So to start off, print the heading line and then begin main loop through
  1157. X    the central directory.  The results will look vaguely like the following:
  1158. X
  1159. X  Length  Method   Size  Ratio   Date    Time   CRC-32     Name ("^" ==> case
  1160. X  ------  ------   ----  -----   ----    ----   ------     ----   conversion)
  1161. X   44004  Implode  13041  71%  11-02-89  19:34  8b4207f7   Makefile.UNIX
  1162. X    3438  Shrunk    2209  36%  09-15-90  14:07  a2394fd8  ^dos-file.ext
  1163. X  ---------------------------------------------------------------------------*/
  1164. X
  1165. X    pInfo = &info;
  1166. X    date_format = dateformat();
  1167. X
  1168. X#ifndef MSWIN
  1169. X    if (quietflg < 2)
  1170. X        if (U_flag)
  1171. X            printf("%s\n%s\n", Headers[which_hdr][0], Headers[which_hdr][1]);
  1172. X        else
  1173. X            printf("%s (\"^\" ==> case\n%s   conversion)\n", 
  1174. X              Headers[which_hdr][0], Headers[which_hdr][1]);
  1175. X#endif /* !MSWIN */
  1176. X
  1177. X    for (j = 0; j < ecrec.total_entries_central_dir; ++j) {
  1178. X
  1179. X        if (readbuf(sig, 4) <= 0)
  1180. X            return 51;          /* 51:  unexpected EOF */
  1181. X        if (strncmp(sig, central_hdr_sig, 4)) {  /* just to make sure */
  1182. X            fprintf(stderr, CentSigMsg, j);  /* sig not found */
  1183. X            fprintf(stderr, ReportMsg);   /* check binary transfers */
  1184. X            return 3;           /* 3:  error in zipfile */
  1185. X        }
  1186. X        if ((error = process_cdir_file_hdr()) != 0)  /* (sets pInfo->lcflag) */
  1187. X            return error;       /* only 51 (EOF) defined */
  1188. X
  1189. X        /*
  1190. X         * We could DISPLAY the filename instead of storing (and possibly trun-
  1191. X         * cating, in the case of a very long name) and printing it, but that
  1192. X         * has the disadvantage of not allowing case conversion--and it's nice
  1193. X         * to be able to see in the listing precisely how you have to type each
  1194. X         * filename in order for unzip to consider it a match.  Speaking of
  1195. X         * which, if member names were specified on the command line, check in
  1196. X         * with match() to see if the current file is one of them, and make a
  1197. X         * note of it if it is.
  1198. X         */
  1199. X
  1200. X        if ((error = do_string(crec.filename_length, FILENAME)) != 0) {
  1201. X            error_in_archive = error;  /*             ^--(uses pInfo->lcflag) */
  1202. X            if (error > 1)      /* fatal:  can't continue */
  1203. X                return error;
  1204. X        }
  1205. X        if (extra_field != (byte *)NULL)
  1206. X            free(extra_field);
  1207. X        extra_field = (byte *)NULL;
  1208. X        if ((error = do_string(crec.extra_field_length, EXTRA_FIELD)) != 0) {
  1209. X            error_in_archive = error;  
  1210. X            if (error > 1)      /* fatal:  can't continue */
  1211. X                return error;
  1212. X        }
  1213. X        if (!process_all_files) {   /* check if specified on command line */
  1214. X            do_this_file = FALSE;
  1215. X            fnamev = fnv;       /* don't destroy permanent filename ptr */
  1216. X            for (--fnamev; *++fnamev;)
  1217. X                if (match(filename, *fnamev)) {
  1218. X                    do_this_file = TRUE;
  1219. X                    break;      /* found match, so stop looping */
  1220. X                }
  1221. X        }
  1222. X        /*
  1223. X         * If current file was specified on command line, or if no names were
  1224. X         * specified, do the listing for this file.  Otherwise, get rid of the
  1225. X         * file comment and go back for the next file.
  1226. X         */
  1227. X
  1228. X        if (process_all_files || do_this_file) {
  1229. X
  1230. X            yr = (((crec.last_mod_file_date >> 9) & 0x7f) + 80) % (unsigned)100;
  1231. X            mo = (crec.last_mod_file_date >> 5) & 0x0f;
  1232. X            dy = crec.last_mod_file_date & 0x1f;
  1233. X
  1234. X            /* twist date so it displays according to national convention */
  1235. X            switch (date_format) {
  1236. X                case DF_YMD:
  1237. X                    hh = mo; mo = yr; yr = dy; dy = hh; break;
  1238. X                case DF_DMY:
  1239. X                    hh = mo; mo = dy; dy = hh;
  1240. X            }
  1241. X            hh = (crec.last_mod_file_time >> 11) & 0x1f;
  1242. X            mm = (crec.last_mod_file_time >> 5) & 0x3f;
  1243. X
  1244. X            csize = (longint) crec.compressed_size;
  1245. X            ucsize = (longint) crec.uncompressed_size;
  1246. X            if (crec.general_purpose_bit_flag & 1)
  1247. X                csize -= 12;    /* if encrypted, don't count encrypt hdr */
  1248. X
  1249. X            ratio = (ucsize == 0) ? 0 :   /* .zip can have 0-length members */
  1250. X                ((ucsize > 2000000L) ?    /* risk signed overflow if mult. */
  1251. X                (int) ((ucsize-csize) / (ucsize/1000L)) + 5 :   /* big */
  1252. X                (int) ((1000L*(ucsize-csize)) / ucsize) + 5);   /* small */
  1253. X
  1254. X#if 0       /* GRR/Euro:  add this?  define p above */
  1255. X#if (defined(DOS_OS2) || (defined(UNIX) && !defined(VMS)))
  1256. X            for (p = filename;  *p;  ++p)
  1257. X                if (!isprint(*p))
  1258. X                    *p = '?';  /* change non-printable chars to '?' */
  1259. X#endif /* DOS_OS2 || UNIX */
  1260. X#endif /* 0 */
  1261. X
  1262. X#ifdef MSWIN
  1263. X#ifdef NEED_EARLY_REDRAW
  1264. X            /* turn on listbox redrawing just before adding last line */
  1265. X            if (j == (ecrec.total_entries_central_dir-1))
  1266. X                (void)SendMessage(hWndList, WM_SETREDRAW, TRUE, 0L);
  1267. X#endif /* NEED_EARLY_REDRAW */
  1268. X            psLBEntry =
  1269. X              (PSTR)LocalAlloc(LMEM_FIXED, FILNAMSIZ+LONG_FORM_FNAME_INX);
  1270. X            switch (which_hdr) {
  1271. X                case 0:   /* short form */
  1272. X                    OemToAnsi(filename, filename);  /* translate to ANSI */
  1273. X                    wsprintf(psLBEntry, "%7ld  %02u-%02u-%02u  %02u:%02u  %c%s",
  1274. X                      (long)ucsize, mo, dy, yr, hh, mm, (pInfo->lcflag?'^':' '),
  1275. X                      (LPSTR)filename);
  1276. X                    SendMessage(hWndList, LB_ADDSTRING, 0,
  1277. X                      (LONG)(LPSTR)psLBEntry);
  1278. X                    break;
  1279. X                case 1:   /* verbose */
  1280. X                    OemToAnsi(filename, filename);  /* translate to ANSI */
  1281. X                    wsprintf(psLBEntry, 
  1282. X                 "%7ld  %-7s%7ld %3d%%  %02u-%02u-%02u  %02u:%02u  %08lx  %c%s",
  1283. X                      (long)ucsize, (LPSTR)method[methnum], (long)csize,
  1284. X                      ratio/10, mo, dy, yr, hh, mm, (unsigned long)crec.crc32,
  1285. X                      (pInfo->lcflag?'^':' '), (LPSTR)filename);
  1286. X                    SendMessage(hWndList, LB_ADDSTRING, 0,
  1287. X                      (LONG)(LPSTR)psLBEntry);
  1288. X            }
  1289. X            LocalFree((HANDLE)psLBEntry);
  1290. X#else /* !MSWIN */
  1291. X            switch (which_hdr) {
  1292. X                case 0:   /* short form */
  1293. X                    printf("%7ld  %02u-%02u-%02u  %02u:%02u  %c%s\n",
  1294. X                      ucsize, mo, dy, yr, hh, mm, (pInfo->lcflag?'^':' '),
  1295. X                      filename);
  1296. X                    break;
  1297. X                case 1:   /* verbose */
  1298. X                    printf(
  1299. X              "%7ld  %-7s%7ld %3d%%  %02u-%02u-%02u  %02u:%02u  %08lx  %c%s\n",
  1300. X                      ucsize, method[methnum], csize, ratio/10, mo, dy, yr,
  1301. X                      hh, mm, crec.crc32, (pInfo->lcflag?'^':' '), filename);
  1302. X            }
  1303. X#endif /* ?MSWIN */
  1304. X
  1305. X            error = do_string(crec.file_comment_length, (QCOND? DISPLAY:SKIP));
  1306. X            if (error) {
  1307. X                error_in_archive = error;  /* might be just warning */
  1308. X                if (error > 1)  /* fatal */
  1309. X                    return error;
  1310. X            }
  1311. X            tot_ucsize += (ULONG) ucsize;
  1312. X            tot_csize += (ULONG) csize;
  1313. X            ++members;
  1314. X#ifdef OS2
  1315. X            if ((ea_size = SizeOfEAs(extra_field)) != 0) {
  1316. X                tot_easize += ea_size;
  1317. X                tot_eafiles++;
  1318. X            }
  1319. X#endif
  1320. X        } else {        /* not listing this file */
  1321. X            SKIP_(crec.file_comment_length)
  1322. X        }
  1323. X    }                   /* end for-loop (j: files in central directory) */
  1324. X
  1325. X/*---------------------------------------------------------------------------
  1326. X    Print footer line and totals (compressed size, uncompressed size, number
  1327. X    of members in zipfile).
  1328. X  ---------------------------------------------------------------------------*/
  1329. X
  1330. X    ratio = (tot_ucsize == 0) ? 
  1331. X        0 : ((tot_ucsize > 4000000L) ?   /* risk unsigned overflow if mult. */
  1332. X        (int) ((tot_ucsize - tot_csize) / (tot_ucsize/1000L)) + 5 :
  1333. X        (int) ((tot_ucsize - tot_csize) * 1000L / tot_ucsize) + 5);
  1334. X
  1335. X    if (quietflg < 2) {
  1336. X#ifdef MSWIN
  1337. X        /* Display just the totals since the dashed lines get displayed
  1338. X         * in UpdateListBox(). Get just enough space to display total.
  1339. X         */
  1340. X        switch (which_hdr) {
  1341. X            case 0:         /* short */
  1342. X                wsprintf(lpumb->szTotalsLine, "%7lu                    %-7u", 
  1343. X                  (ULONG)tot_ucsize, members);
  1344. X                break;
  1345. X            case 1:         /* verbose */
  1346. X                wsprintf(lpumb->szTotalsLine, 
  1347. X                  "%7lu         %7lu %3d%%                              %-7u", 
  1348. X                  (ULONG)tot_ucsize, (ULONG)tot_csize, ratio / 10, members);
  1349. X                break;
  1350. X        }
  1351. X#else /* !MSWIN */
  1352. X        switch (which_hdr) {
  1353. X        case 0:         /* short */
  1354. X            printf("%s\n%7lu                    %-7u\n",
  1355. X                   " ------                    -------",
  1356. X                   tot_ucsize, members);
  1357. X            break;
  1358. X        case 1:         /* verbose */
  1359. X            printf(
  1360. X              "%s\n%7lu         %7lu %3d%%                              %-7u\n",
  1361. X              " ------          ------  ---                              -------",
  1362. X              tot_ucsize, tot_csize, ratio / 10, members);
  1363. X        }
  1364. X#endif /* ?MSWIN */
  1365. X#ifdef OS2
  1366. X        if (tot_eafiles && tot_easize)
  1367. X            printf("\n%ld file%s %ld bytes of EA's attached.\n", tot_eafiles, 
  1368. X              tot_eafiles == 1 ? " has" : "s have a total of", tot_easize);
  1369. X#endif
  1370. X    }
  1371. X/*---------------------------------------------------------------------------
  1372. X    Double check that we're back at the end-of-central-directory record.
  1373. X  ---------------------------------------------------------------------------*/
  1374. X
  1375. X    readbuf(sig, 4);
  1376. X    if (strncmp(sig, end_central_sig, 4)) {     /* just to make sure again */
  1377. X        fprintf(stderr, EndSigMsg);  /* didn't find end-of-central-dir sig */
  1378. X/*      fprintf(stderr, ReportMsg);   */
  1379. X        error_in_archive = 1;        /* 1:  warning error */
  1380. X    }
  1381. X    return error_in_archive;
  1382. X
  1383. X} /* end function list_files() */
  1384. X
  1385. X
  1386. X
  1387. X
  1388. X
  1389. X/**************************************/
  1390. X/*  Function process_cdir_file_hdr()  */
  1391. X/**************************************/
  1392. X
  1393. Xint process_cdir_file_hdr()    /* return PK-type error code */
  1394. X{
  1395. X    cdir_byte_hdr byterec;
  1396. X
  1397. X
  1398. X/*---------------------------------------------------------------------------
  1399. X    Read the next central directory entry and do any necessary machine-type
  1400. X    conversions (byte ordering, structure padding compensation--do so by
  1401. X    copying the data from the array into which it was read (byterec) to the
  1402. X    usable struct (crec)).
  1403. X  ---------------------------------------------------------------------------*/
  1404. X
  1405. X    if (readbuf((char *) byterec, CREC_SIZE) <= 0)
  1406. X        return 51;   /* 51:  unexpected EOF */
  1407. X
  1408. X    crec.version_made_by[0] = byterec[C_VERSION_MADE_BY_0];
  1409. X    crec.version_made_by[1] = byterec[C_VERSION_MADE_BY_1];
  1410. X    crec.version_needed_to_extract[0] = byterec[C_VERSION_NEEDED_TO_EXTRACT_0];
  1411. X    crec.version_needed_to_extract[1] = byterec[C_VERSION_NEEDED_TO_EXTRACT_1];
  1412. X
  1413. X    crec.general_purpose_bit_flag =
  1414. X        makeword(&byterec[C_GENERAL_PURPOSE_BIT_FLAG]);
  1415. X    crec.compression_method =
  1416. X        makeword(&byterec[C_COMPRESSION_METHOD]);
  1417. X    crec.last_mod_file_time =
  1418. X        makeword(&byterec[C_LAST_MOD_FILE_TIME]);
  1419. X    crec.last_mod_file_date =
  1420. X        makeword(&byterec[C_LAST_MOD_FILE_DATE]);
  1421. X    crec.crc32 =
  1422. X        makelong(&byterec[C_CRC32]);
  1423. X    crec.compressed_size =
  1424. X        makelong(&byterec[C_COMPRESSED_SIZE]);
  1425. X    crec.uncompressed_size =
  1426. X        makelong(&byterec[C_UNCOMPRESSED_SIZE]);
  1427. X    crec.filename_length =
  1428. X        makeword(&byterec[C_FILENAME_LENGTH]);
  1429. X    crec.extra_field_length =
  1430. X        makeword(&byterec[C_EXTRA_FIELD_LENGTH]);
  1431. X    crec.file_comment_length =
  1432. X        makeword(&byterec[C_FILE_COMMENT_LENGTH]);
  1433. X    crec.disk_number_start =
  1434. X        makeword(&byterec[C_DISK_NUMBER_START]);
  1435. X    crec.internal_file_attributes =
  1436. X        makeword(&byterec[C_INTERNAL_FILE_ATTRIBUTES]);
  1437. X    crec.external_file_attributes =
  1438. X        makelong(&byterec[C_EXTERNAL_FILE_ATTRIBUTES]);  /* LONG, not word! */
  1439. X    crec.relative_offset_local_header =
  1440. X        makelong(&byterec[C_RELATIVE_OFFSET_LOCAL_HEADER]);
  1441. X
  1442. X    pInfo->hostnum = MIN(crec.version_made_by[1], NUM_HOSTS);
  1443. X/*  extnum = MIN(crec.version_needed_to_extract[1], NUM_HOSTS); */
  1444. X    methnum = MIN(crec.compression_method, NUM_METHODS);
  1445. X    if (methnum == NUM_METHODS)
  1446. X        sprintf(unkn, "Unk:%03d", crec.compression_method);
  1447. X
  1448. X/*---------------------------------------------------------------------------
  1449. X    Set flag for lowercase conversion of filename, depending on which OS the
  1450. X    file is coming from.  This section could be ifdef'd if some people have
  1451. X    come to love DOS uppercase filenames under Unix...but really, guys, get
  1452. X    a life. :)  NOTE THAT ALL SYSTEM NAMES NOW HAVE TRAILING UNDERSCORES!!!
  1453. X    This is to prevent interference with compiler command-line defines such
  1454. X    as -DUNIX, for example, which are then used in "#ifdef UNIX" constructs.
  1455. X  ---------------------------------------------------------------------------*/
  1456. X
  1457. X    pInfo->lcflag = 0;
  1458. X    if (!U_flag)   /* as long as user hasn't specified case-preservation */
  1459. X        switch (pInfo->hostnum) {
  1460. X            case DOS_OS2_FAT_:
  1461. X        /*  case VMS_:              VMS Zip converts filenames to lowercase */
  1462. X            case VM_CMS_:           /* all caps? */
  1463. X            case CPM_:              /* like DOS, right? */
  1464. X        /*  case ATARI_:            ? */
  1465. X        /*  case Z_SYSTEM_:         ? */
  1466. X        /*  case TOPS20_:           (if we had such a thing...) */
  1467. X                pInfo->lcflag = 1;  /* convert filename to lowercase */
  1468. X                break;
  1469. X
  1470. X            default:                /* AMIGA_, UNIX_, (ATARI_), OS2_HPFS_, */
  1471. X                break;              /*   MAC_, (Z_SYSTEM_):  no conversion */
  1472. X        }
  1473. X
  1474. X    return 0;
  1475. X
  1476. X} /* end function process_cdir_file_hdr() */
  1477. X
  1478. X
  1479. X
  1480. X
  1481. X
  1482. X/***************************************/
  1483. X/*  Function process_local_file_hdr()  */
  1484. X/***************************************/
  1485. X
  1486. Xint process_local_file_hdr()    /* return PK-type error code */
  1487. X{
  1488. X    local_byte_hdr byterec;
  1489. X
  1490. X
  1491. X/*---------------------------------------------------------------------------
  1492. X    Read the next local file header and do any necessary machine-type con-
  1493. X    versions (byte ordering, structure padding compensation--do so by copy-
  1494. X    ing the data from the array into which it was read (byterec) to the
  1495. X    usable struct (lrec)).
  1496. X  ---------------------------------------------------------------------------*/
  1497. X
  1498. X    if (readbuf((char *) byterec, LREC_SIZE) <= 0)
  1499. X        return 51;   /* 51:  unexpected EOF */
  1500. X
  1501. X    lrec.version_needed_to_extract[0] = byterec[L_VERSION_NEEDED_TO_EXTRACT_0];
  1502. X    lrec.version_needed_to_extract[1] = byterec[L_VERSION_NEEDED_TO_EXTRACT_1];
  1503. X
  1504. X    lrec.general_purpose_bit_flag = makeword(&byterec[L_GENERAL_PURPOSE_BIT_FLAG]);
  1505. X    lrec.compression_method = makeword(&byterec[L_COMPRESSION_METHOD]);
  1506. X    lrec.last_mod_file_time = makeword(&byterec[L_LAST_MOD_FILE_TIME]);
  1507. X    lrec.last_mod_file_date = makeword(&byterec[L_LAST_MOD_FILE_DATE]);
  1508. X    lrec.crc32 = makelong(&byterec[L_CRC32]);
  1509. X    lrec.compressed_size = makelong(&byterec[L_COMPRESSED_SIZE]);
  1510. X    lrec.uncompressed_size = makelong(&byterec[L_UNCOMPRESSED_SIZE]);
  1511. X    lrec.filename_length = makeword(&byterec[L_FILENAME_LENGTH]);
  1512. X    lrec.extra_field_length = makeword(&byterec[L_EXTRA_FIELD_LENGTH]);
  1513. X
  1514. X    csize = (longint) lrec.compressed_size;
  1515. X    ucsize = (longint) lrec.uncompressed_size;
  1516. X
  1517. X    if ((lrec.general_purpose_bit_flag & 8) != 0) {
  1518. X        /* Can't trust local header, use central directory: */
  1519. X      lrec.crc32 = pInfo->crc;
  1520. X        lrec.compressed_size = pInfo->compr_size;
  1521. X        csize = (longint) lrec.compressed_size;
  1522. X    }
  1523. X
  1524. X    return 0;                 /* 0:  no error */
  1525. X
  1526. X} /* end function process_local_file_hdr() */
  1527. END_OF_FILE
  1528.  if test 31190 -ne `wc -c <'unzip.c.B'`; then
  1529.     echo shar: \"'unzip.c.B'\" unpacked with wrong size!
  1530.  elif test -f 'unzip.c.A'; then
  1531.     echo shar: Combining  \"'unzip.c'\" \(60418 characters\)
  1532.     cat 'unzip.c.A' 'unzip.c.B' > 'unzip.c'
  1533.     if test 60418 -ne `wc -c <'unzip.c'`; then
  1534.       echo shar: \"'unzip.c'\" combined with wrong size!
  1535.     else
  1536.       rm unzip.c.A unzip.c.B
  1537.     fi
  1538.   fi
  1539.   # end of 'unzip.c.B'
  1540. fi
  1541. echo shar: End of archive 5 \(of 14\).
  1542. cp /dev/null ark5isdone
  1543. MISSING=""
  1544. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ; do
  1545.     if test ! -f ark${I}isdone ; then
  1546.     MISSING="${MISSING} ${I}"
  1547.     fi
  1548. done
  1549. if test "${MISSING}" = "" ; then
  1550.     echo You have unpacked all 14 archives.
  1551.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  1552. else
  1553.     echo You still must unpack the following archives:
  1554.     echo "        " ${MISSING}
  1555. fi
  1556. exit 0
  1557. exit 0 # Just in case...
  1558.