home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / sources / misc / 3858 < prev    next >
Encoding:
Text File  |  1992-08-23  |  58.9 KB  |  1,564 lines

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