home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip21.zip / zipfile.c < prev    next >
C/C++ Source or Header  |  1996-04-01  |  35KB  |  1,086 lines

  1. /*
  2.  
  3.  Copyright (C) 1990-1996 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  4.  Kai Uwe Rommel, Onno van der Linden and Igor Mandrichenko.
  5.  Permission is granted to any individual or institution to use, copy, or
  6.  redistribute this software so long as all of the original files are included,
  7.  that it is not sold for profit, and that this copyright notice is retained.
  8.  
  9. */
  10.  
  11. /*
  12.  *  zipfile.c by Mark Adler.
  13.  */
  14.  
  15. #define NOCPYRT         /* this is not a main module */
  16. #include "zip.h"
  17. #include "revision.h"
  18.  
  19. #ifdef VMS
  20. #  include <rms.h>
  21. #  include <starlet.h>
  22. #  include "vms/vmsmunch.h"
  23. #  include "vms/vmsdefs.h"
  24. #endif
  25.  
  26.  
  27. /* Macros for converting integers in little-endian to machine format */
  28. #define SH(a) (((ush)(uch)(a)[0]) | (((ush)(uch)(a)[1]) << 8))
  29. #define LG(a) ((ulg)SH(a) | ((ulg)SH((a)+2) << 16))
  30.  
  31. /* Macros for writing machine integers to little-endian format */
  32. #define PUTSH(a,f) {putc((char)((a) & 0xff),(f)); putc((char)((a) >> 8),(f));}
  33. #define PUTLG(a,f) {PUTSH((a) & 0xffff,(f)) PUTSH((a) >> 16,(f))}
  34.  
  35.  
  36. /* -- Structure of a ZIP file -- */
  37.  
  38. /* Signatures for zip file information headers */
  39. #define LOCSIG     0x04034b50L
  40. #define CENSIG     0x02014b50L
  41. #define ENDSIG     0x06054b50L
  42. #define EXTLOCSIG  0x08074b50L
  43.  
  44. /* Offsets of values in headers */
  45. #define LOCVER  0               /* version needed to extract */
  46. #define LOCFLG  2               /* encrypt, deflate flags */
  47. #define LOCHOW  4               /* compression method */
  48. #define LOCTIM  6               /* last modified file time, DOS format */
  49. #define LOCDAT  8               /* last modified file date, DOS format */
  50. #define LOCCRC  10              /* uncompressed crc-32 for file */
  51. #define LOCSIZ  14              /* compressed size in zip file */
  52. #define LOCLEN  18              /* uncompressed size */
  53. #define LOCNAM  22              /* length of filename */
  54. #define LOCEXT  24              /* length of extra field */
  55.  
  56. #define EXTCRC  0               /* uncompressed crc-32 for file */
  57. #define EXTSIZ  4               /* compressed size in zip file */
  58. #define EXTLEN  8               /* uncompressed size */
  59.  
  60. #define CENVEM  0               /* version made by */
  61. #define CENVER  2               /* version needed to extract */
  62. #define CENFLG  4               /* encrypt, deflate flags */
  63. #define CENHOW  6               /* compression method */
  64. #define CENTIM  8               /* last modified file time, DOS format */
  65. #define CENDAT  10              /* last modified file date, DOS format */
  66. #define CENCRC  12              /* uncompressed crc-32 for file */
  67. #define CENSIZ  16              /* compressed size in zip file */
  68. #define CENLEN  20              /* uncompressed size */
  69. #define CENNAM  24              /* length of filename */
  70. #define CENEXT  26              /* length of extra field */
  71. #define CENCOM  28              /* file comment length */
  72. #define CENDSK  30              /* disk number start */
  73. #define CENATT  32              /* internal file attributes */
  74. #define CENATX  34              /* external file attributes */
  75. #define CENOFF  38              /* relative offset of local header */
  76.  
  77. #define ENDDSK  0               /* number of this disk */
  78. #define ENDBEG  2               /* number of the starting disk */
  79. #define ENDSUB  4               /* entries on this disk */
  80. #define ENDTOT  6               /* total number of entries */
  81. #define ENDSIZ  8               /* size of entire central directory */
  82. #define ENDOFF  12              /* offset of central on starting disk */
  83. #define ENDCOM  16              /* length of zip file comment */
  84.  
  85.  
  86. /* Local functions */
  87.  
  88. local int zqcmp OF((const zvoid *, const zvoid *));
  89. #ifndef UTIL
  90.    local int rqcmp OF((const zvoid *, const zvoid *));
  91.    local int zbcmp OF((const zvoid *, const zvoid far *));
  92. #  ifdef USE_EF_UX_TIME
  93.      local int ef_scan_ux_time OF((char *ef_buf, extent ef_len,
  94.                                    ztimbuf *z_utim));
  95. #  endif /* USE_EF_UX_TIME */
  96.    local void cutpath OF((char *p));
  97. #endif /* !UTIL */
  98.  
  99.  
  100. local int zqcmp(a, b)
  101. const zvoid *a, *b;           /* pointers to pointers to zip entries */
  102. /* Used by qsort() to compare entries in the zfile list.
  103.  * Compares the internal names z->zname */
  104. {
  105.   return namecmp((*(struct zlist far **)a)->zname,
  106.                  (*(struct zlist far **)b)->zname);
  107. }
  108.  
  109. #ifndef UTIL
  110.  
  111. local int rqcmp(a, b)
  112. const zvoid *a, *b;           /* pointers to pointers to zip entries */
  113. /* Used by qsort() to compare entries in the zfile list.
  114.  * Compare the internal names z->zname, but in reverse order. */
  115. {
  116.   return namecmp((*(struct zlist far **)b)->zname,
  117.                  (*(struct zlist far **)a)->zname);
  118. }
  119.  
  120.  
  121. local int zbcmp(n, z)
  122. const zvoid *n;         /* string to search for */
  123. const zvoid far *z;     /* pointer to a pointer to a zip entry */
  124. /* Used by search() to compare a target to an entry in the zfile list. */
  125. {
  126.   return namecmp((char *)n, ((struct zlist far *)z)->zname);
  127. }
  128.  
  129.  
  130. struct zlist far *zsearch(n)
  131. char *n;                /* name to find */
  132. /* Return a pointer to the entry in zfile with the name n, or NULL if
  133.    not found. */
  134. {
  135.   zvoid far **p;        /* result of search() */
  136.  
  137.   if (zcount && (p = search(n, (zvoid far **)zsort, zcount, zbcmp)) != NULL)
  138.     return *(struct zlist far **)p;
  139.   else
  140.     return NULL;
  141. }
  142.  
  143. #endif /* !UTIL */
  144.  
  145. #ifndef VMS
  146. #  define PATHCUT '/'
  147.  
  148. char *ziptyp(s)
  149. char *s;                /* file name to force to zip */
  150. /* If the file name *s has a dot (other than the first char), or if
  151.    the -A option is used (adjust self-extracting file) then return
  152.    the name, otherwise append .zip to the name.  Allocate the space for
  153.    the name in either case.  Return a pointer to the new name, or NULL
  154.    if malloc() fails. */
  155. {
  156.   char *q;              /* temporary pointer */
  157.   char *t;              /* pointer to malloc'ed string */
  158.  
  159.   if ((t = malloc(strlen(s) + 5)) == NULL)
  160.     return NULL;
  161.   strcpy(t, s);
  162. #ifdef __human68k__
  163.   _toslash(t);
  164. #endif
  165. #ifdef MSDOS
  166.   for (q = t; *q; q++)
  167.     if (*q == '\\')
  168.       *q = '/';
  169. #endif /* MSDOS */
  170.   if (adjust) return t;
  171. #ifndef RISCOS
  172. #  ifdef AMIGA
  173.   if ((q = strrchr(t, '/')) == NULL)
  174.     q = strrchr(t, ':');
  175.   if (strrchr((q ? q + 1 : t), '.') == NULL)
  176. #  else /* !AMIGA */
  177.   if (strrchr((q = strrchr(t, PATHCUT)) == NULL ? t : q + 1, '.') == NULL)
  178. #  endif /* ?AMIGA */
  179. #  ifdef CMS_MVS
  180.      if (strncmp(t,"dd:",3) != 0 && strncmp(t,"DD:",3) != 0)
  181. #  endif /* CMS_MVS */
  182.     strcat(t, ".zip");
  183. #endif /* !RISCOS */
  184.   return t;
  185. }
  186.  
  187. #else /* VMS */
  188.  
  189. # define PATHCUT ']'
  190.  
  191. char *ziptyp(s)
  192. char *s;
  193. {   int status;
  194.     struct FAB fab;
  195.     struct NAM nam;
  196.     static char zero=0;
  197.     char result[NAM$C_MAXRSS+1],exp[NAM$C_MAXRSS+1];
  198.     char *p;
  199.  
  200.     fab = cc$rms_fab;
  201.     nam = cc$rms_nam;
  202.  
  203.     fab.fab$l_fna = s;
  204.     fab.fab$b_fns = strlen(fab.fab$l_fna);
  205.  
  206.     fab.fab$l_dna = "sys$disk:[].zip";          /* Default fspec */
  207.     fab.fab$b_dns = strlen(fab.fab$l_dna);
  208.  
  209.     fab.fab$l_nam = &nam;
  210.  
  211.     nam.nam$l_rsa = result;                     /* Put resultant name of */
  212.     nam.nam$b_rss = sizeof(result)-1;           /* existing zipfile here */
  213.  
  214.     nam.nam$l_esa = exp;                        /* For full spec of */
  215.     nam.nam$b_ess = sizeof(exp)-1;              /* file to create */
  216.  
  217.     status = sys$parse(&fab);
  218.     if( (status & 1) == 0 )
  219.         return &zero;
  220.  
  221.     status = sys$search(&fab);
  222.     if( status & 1 )
  223.     {               /* Existing ZIP file */
  224.         int l;
  225.         if( (p=malloc( (l=nam.nam$b_rsl) + 1 )) != NULL )
  226.         {       result[l] = 0;
  227.                 strcpy(p,result);
  228.         }
  229.     }
  230.     else
  231.     {               /* New ZIP file */
  232.         int l;
  233.         if( (p=malloc( (l=nam.nam$b_esl) + 1 )) != NULL )
  234.         {       exp[l] = 0;
  235.                 strcpy(p,exp);
  236.         }
  237.     }
  238.     return p;
  239. }
  240.  
  241. #endif  /* VMS */
  242.  
  243.  
  244. int readzipfile()
  245. /*
  246.    Make first pass through zip file, reading information from local file
  247.    headers and then verifying that information with the central file
  248.    headers.  Any deviation from the expected zip file format returns an
  249.    error.  At the end, a sorted list of file names in the zip file is made
  250.    to facilitate searching by name.
  251.  
  252.    The name of the zip file is pointed to by the global "zipfile".  The
  253.    globals zfiles, zcount, zcomlen, zcomment, and zsort are filled in.
  254.    Return an error code in the ZE_ class.
  255. */
  256. {
  257.   ulg a = 0L;           /* attributes returned by filetime() */
  258.   char b[CENHEAD];      /* buffer for central headers */
  259.   FILE *f;              /* zip file */
  260.   ush flg;              /* general purpose bit flag */
  261.   int m;                /* mismatch flag */
  262.   extent n;             /* length of name */
  263.   ulg p;                /* current file offset */
  264.   char r;               /* holds reserved bits during memcmp() */
  265.   ulg s;                /* size of data, start of central */
  266.   char *t;              /* temporary variable */
  267.   char far *u;          /* temporary variable */
  268.   struct zlist far * far *x;    /* pointer last entry's link */
  269.   struct zlist far *z;  /* current zip entry structure */
  270.   ulg temp;             /* required to avoid Coherent compiler bug */
  271.  
  272.   /* Initialize zip file info */
  273.   zipbeg = 0;
  274.   zfiles = NULL;                        /* Points to first header */
  275.   zcomlen = 0;                          /* zip file comment length */
  276.  
  277.   /* If zip file exists, read headers and check structure */
  278. #ifdef VMS
  279.   if (zipfile == NULL || !(*zipfile) || !strcmp(zipfile, "-"))
  280.     return ZE_OK;
  281.   {
  282.     int rtype;
  283.  
  284.     if ((VMSmunch(zipfile, GET_RTYPE, (char *)&rtype) == RMS$_NORMAL) &&
  285.         (rtype == FAT$C_VARIABLE)) {
  286.       fprintf(stderr,
  287.      "\n     Error:  zipfile is in variable-length record format.  Please\n\
  288.      run \"bilf b %s\" to convert the zipfile to fixed-length\n\
  289.      record format.\n\n", zipfile);
  290.       return ZE_FORM;
  291.     }
  292.   }
  293.   if ((f = fopen(zipfile, FOPR)) != NULL)
  294. #else /* !VMS */
  295.   if (zipfile != NULL && *zipfile && strcmp(zipfile, "-") &&
  296.       (f = fopen(zipfile, FOPR)) != NULL)
  297. #endif /* ?VMS */
  298.   {
  299.     /* Get any file attribute valid for this OS, to set in the central
  300.      * directory when fixing the archive:
  301.      */
  302. #ifndef UTIL
  303.     if (fix)
  304.       filetime(zipfile, &a, (long*)&s, NULL);
  305. #endif
  306.     x = &zfiles;                        /* first link */
  307.     p = 0;                              /* starting file offset */
  308.     zcount = 0;                         /* number of files */
  309.  
  310.     /* Find start of zip structures */
  311.     for (;;) {
  312.       while ((m = getc(f)) != EOF && m != 0x50) p++;  /* 0x50 == 'P' */
  313.       b[0] = (char) m;
  314.       if (fread(b+1, 3, 1, f) != 1 || (s = LG(b)) == LOCSIG || s == ENDSIG)
  315.         break;
  316.       if (fseek(f, -3L, SEEK_CUR))
  317.         return ferror(f) ? ZE_READ : ZE_EOF;
  318.       p++;
  319.     }
  320.     zipbeg = p;
  321.  
  322.     /* Read local headers */
  323.     while (LG(b) == LOCSIG)
  324.     {
  325.       /* Read local header raw to compare later with central header
  326.          (this requires that the offest of ext in the zlist structure
  327.          be greater than or equal to LOCHEAD) */
  328.       if ((z = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL)
  329.         return ZE_MEM;
  330.       if (fread(b, LOCHEAD, 1, f) != 1) {
  331.         if (fix)
  332.           break;
  333.         else
  334.           return ferror(f) ? ZE_READ : ZE_EOF;
  335.       }
  336.       if (fix) {
  337.         z->ver = SH(LOCVER + b);
  338.         z->vem = dosify ? 20 : OS_CODE + REVISION;
  339.         z->dosflag = dosify;
  340.         flg = z->flg = z->lflg = SH(LOCFLG + b);
  341.         z->how = SH(LOCHOW + b);
  342.         z->tim = LG(LOCTIM + b);          /* time and date into one long */
  343.         z->crc = LG(LOCCRC + b);
  344.         z->siz = LG(LOCSIZ + b);
  345.         z->len = LG(LOCLEN + b);
  346.         n = z->nam = SH(LOCNAM + b);
  347.         z->cext = z->ext = SH(LOCEXT + b);
  348.         z->com = 0;
  349.         z->dsk = 0;
  350.         z->att = 0;
  351.         z->atx = dosify ? a & 0xff : a;     /* Attributes from filetime() */
  352.         z->mark = 0;
  353.         z->trash = 0;
  354.         s = fix > 1 ? 0L : z->siz; /* discard compressed size with -FF */
  355.       } else {
  356.         t = b;  u = (char far *)z;  n = LOCHEAD;
  357.         do {
  358.           *u++ = *t++;
  359.         } while (--n);
  360.         z->ext = SH(LOCEXT + (uch far *)z);
  361.         n = SH(LOCNAM + (uch far *)z);
  362.         flg = SH(b+LOCFLG);
  363.         s = LG(LOCSIZ + (uch far *)z);
  364.       }
  365.       /* Link into list */
  366.       *x = z;
  367.       z->nxt = NULL;
  368.       x = &z->nxt;
  369.  
  370.       /* Read file name and extra field and skip data */
  371.       if (n == 0)
  372.       {
  373.         sprintf(errbuf, "%ld", (ulg)zcount + 1);
  374.         zipwarn("zero-length name for entry #", errbuf);
  375. #ifndef DEBUG
  376.         return ZE_FORM;
  377. #endif
  378.       }
  379.       if ((z->zname = malloc(n+1)) ==  NULL ||
  380.           (z->ext && (z->extra = malloc(z->ext)) == NULL))
  381.         return ZE_MEM;
  382.       if (fread(z->zname, n, 1, f) != 1 ||
  383.           (z->ext && fread(z->extra, z->ext, 1, f) != 1) ||
  384.           (s && fseek(f, (long)s, SEEK_CUR)))
  385.         return ferror(f) ? ZE_READ : ZE_EOF;
  386.       /* If there is an extended local header, s is either 0 or
  387.        * the correct compressed size.
  388.        */
  389.       z->zname[n] = 0;                  /* terminate name */
  390.       if (fix) {
  391.         z->cextra = z->extra;
  392.         if (noisy) fprintf(mesg, "zip: reading %s\n", z->zname);
  393.       }
  394. #ifdef UTIL
  395.       z->name = z->zname;               /* !!! to be checked */
  396. #else /* !UTIL */
  397.       z->name = in2ex(z->zname);        /* convert to external name */
  398.       if (z->name == NULL)
  399.         return ZE_MEM;
  400. #endif /* ?UTIL */
  401.  
  402.       /* Save offset, update for next header */
  403.       z->off = p;
  404.       p += 4 + LOCHEAD + n + z->ext + s;
  405.       zcount++;
  406.  
  407.       /* Skip extended local header if there is one */
  408.       if ((flg & 8) != 0) {
  409.         /* Skip the compressed data if compressed size is unknown.
  410.          * For safety, we should use the central directory.
  411.          */
  412.         if (s == 0) {
  413.           for (;;) {
  414.             while ((m = getc(f)) != EOF && m != 0x50) ;  /* 0x50 == 'P' */
  415.             b[0] = (char) m;
  416.             if (fread(b+1, 15, 1, f) != 1 || LG(b) == EXTLOCSIG)
  417.               break;
  418.             if (fseek(f, -15L, SEEK_CUR))
  419.               return ferror(f) ? ZE_READ : ZE_EOF;
  420.           }
  421.           s = LG(4 + EXTSIZ + b);
  422.           p += s;
  423.           if ((ulg) ftell(f) != p+16L) {
  424.             zipwarn("bad extended local header for ", z->zname);
  425.             return ZE_FORM;
  426.           }
  427.         } else {
  428.           /* compressed size non zero, assume that it is valid: */
  429.           Assert(p == ftell(f), "bad compressed size with extended header");
  430.  
  431.           if (fseek(f, p, SEEK_SET) || fread(b, 16, 1, f) != 1)
  432.             return ferror(f) ? ZE_READ : ZE_EOF;
  433.           if (LG(b) != EXTLOCSIG) {
  434.             zipwarn("extended local header not found for ", z->zname);
  435.             return ZE_FORM;
  436.           }
  437.         }
  438.         /* overwrite the unknown values of the local header: */
  439.         if (fix) {
  440.             /* already in host format */
  441.             z->crc = LG(4 + EXTCRC + b);
  442.             z->siz = s;
  443.             z->len = LG(4 + EXTLEN + b);
  444.         } else {
  445.             /* Keep in Intel format for comparison with central header */
  446.             t = b+4;  u = (char far *)z+LOCCRC;  n = 12;
  447.             do {
  448.               *u++ = *t++;
  449.             } while (--n);
  450.         }
  451.         p += 16L;
  452.       }
  453.       else if (fix > 1) {
  454.         /* Don't trust the compressed size */
  455.         for (;;) {
  456.           while ((m = getc(f)) != EOF && m != 0x50) p++; /* 0x50 == 'P' */
  457.           b[0] = (char) m;
  458.           if (fread(b+1, 3, 1, f) != 1 || (s = LG(b)) == LOCSIG || s == CENSIG)
  459.             break;
  460.           if (fseek(f, -3L, SEEK_CUR))
  461.             return ferror(f) ? ZE_READ : ZE_EOF;
  462.           p++;
  463.         }
  464.         s = p - (z->off + 4 + LOCHEAD + n + z->ext);
  465.         if (s != z->siz) {
  466.           fprintf(stderr, " compressed size %ld, actual size %ld for %s\n",
  467.                   z->siz, s, z->zname);
  468.           z->siz = s;
  469.         }
  470.         /* next LOCSIG already read at this point, don't read it again: */
  471.         continue;
  472.       }
  473.  
  474.       /* Read next signature */
  475.       if (fread(b, 4, 1, f) != 1) {
  476.         if (fix)
  477.           break;
  478.         else
  479.           return ferror(f) ? ZE_READ : ZE_EOF;
  480.       }
  481.     }
  482.  
  483.     /* Point to start of header list and read central headers */
  484.     z = zfiles;
  485.     s = p;                              /* save start of central */
  486.     if (fix) {
  487.       if (LG(b) != CENSIG && noisy) {
  488.         fprintf(mesg, "zip warning: %s %s truncated.\n", zipfile,
  489.                 fix > 1 ? "has been" : "would be");
  490.  
  491.         if (fix == 1) {
  492.           fprintf(mesg,
  493.    "Retry with option -qF to truncate, with -FF to attempt full recovery\n");
  494.           ziperr(ZE_FORM, NULL);
  495.         }
  496.       }
  497.     } else while (LG(b) == CENSIG)
  498.     {
  499.       if (z == NULL)
  500.       {
  501.         zipwarn("extraneous central header signature", "");
  502.         return ZE_FORM;
  503.       }
  504.  
  505.       /* Read central header */
  506.       if (fread(b, CENHEAD, 1, f) != 1)
  507.         return ferror(f) ? ZE_READ : ZE_EOF;
  508.  
  509.       /* Compare local header with that part of central header (except
  510.          for the reserved bits in the general purpose flags and except
  511.          for length of extra fields--authentication can make these
  512.          different in central and local headers) */
  513.       z->lflg = SH(LOCFLG + (uch far *)z);      /* Save reserved bits */
  514.       r = b[CENFLG+1];
  515.       ((uch far *)z)[LOCFLG+1] &= 0x1f; /* Zero out reserved bits */
  516.       b[CENFLG+1] &= 0x1f;
  517.       for (m = 0, u = (char far *)z, n = 0; n < LOCHEAD - 2; n++)
  518.         if (u[n] != b[n+2])
  519.         {
  520.           if (!m && noisy)
  521.             zipwarn("local and central headers differ for ", z->zname);
  522.           m = 1;
  523.           sprintf(errbuf, " offset %d--local = %02x, central = %02x",
  524.                   (int)n, (uch)u[n], (uch)b[n+2]);
  525.           if (noisy) zipwarn(errbuf, "");
  526.           b[n+2] = u[n]; /* fix the zipfile */
  527.         }
  528.       if (m)
  529.         return ZE_FORM;
  530.       b[CENFLG+1] = r;                  /* Restore reserved bits */
  531.  
  532.       /* Overwrite local header with translated central header */
  533.       z->vem = SH(CENVEM + b);
  534.       z->ver = SH(CENVER + b);
  535.       z->flg = SH(CENFLG + b);          /* may be different from z->lflg */
  536.       z->how = SH(CENHOW + b);
  537.       z->tim = LG(CENTIM + b);          /* time and date into one long */
  538.       z->crc = LG(CENCRC + b);
  539.       z->siz = LG(CENSIZ + b);
  540.       z->len = LG(CENLEN + b);
  541.       z->nam = SH(CENNAM + b);
  542.       z->cext = SH(CENEXT + b);         /* may be different from z->ext */
  543.       z->com = SH(CENCOM + b);
  544.       z->dsk = SH(CENDSK + b);
  545.       z->att = SH(CENATT + b);
  546.       z->atx = LG(CENATX + b);
  547.       z->dosflag = (z->vem & 0xff00) == 0;
  548.       temp = LG(CENOFF + b);            /* to avoid Coherent compiler bug */
  549.       if (z->off != temp && !adjust) {
  550.         zipwarn("local offset in central header incorrect for ", z->zname);
  551.         return ZE_FORM;
  552.       }
  553.  
  554.       /* Compare name and extra fields and read comment field */
  555.       if ((t = malloc(z->nam)) == NULL)
  556.         return ZE_MEM;
  557.       if (fread(t, z->nam, 1, f) != 1)
  558.       {
  559.         free((zvoid *)t);
  560.         return ferror(f) ? ZE_READ : ZE_EOF;
  561.       }
  562.       if (memcmp(t, z->zname, z->nam))
  563.       {
  564.         free((zvoid *)t);
  565.         zipwarn("names in local and central differ for ", z->zname);
  566.         return ZE_FORM;
  567.       }
  568.       free((zvoid *)t);
  569.       if (z->cext)
  570.       {
  571.         if ((z->cextra = malloc(z->cext)) == NULL)
  572.           return ZE_MEM;
  573.         if (fread(z->cextra, z->cext, 1, f) != 1)
  574.         {
  575.           free((zvoid *)(z->cextra));
  576.           return ferror(f) ? ZE_READ : ZE_EOF;
  577.         }
  578.         if (z->ext == z->cext && memcmp(z->extra, z->cextra, z->ext) == 0)
  579.         {
  580.           free((zvoid *)(z->cextra));
  581.           z->cextra = z->extra;
  582.         }
  583.       }
  584.       if (z->com)
  585.       {
  586.         if ((z->comment = malloc(z->com)) == NULL)
  587.           return ZE_MEM;
  588.         if (fread(z->comment, z->com, 1, f) != 1)
  589.         {
  590.           free((zvoid *)(z->comment));
  591.           return ferror(f) ? ZE_READ : ZE_EOF;
  592.         }
  593.       }
  594.  
  595.       /* Note oddities */
  596.       if (verbose)
  597.       {
  598.         if ((n = z->vem >> 8) >= NUM_HOSTS)
  599.         {
  600.           sprintf(errbuf, "made by version %d.%d on system type %d: ",
  601.             (ush)(z->vem & 0xff) / (ush)10,
  602.             (ush)(z->vem & 0xff) % (ush)10, z->vem >> 8);
  603.           zipwarn(errbuf, z->zname);
  604.         }
  605.         if (z->ver != 10 && z->ver != 11 && z->ver != 20)
  606.         {
  607.           sprintf(errbuf, "needs unzip %d.%d on system type %d: ",
  608.             (ush)(z->ver & 0xff) / (ush)10,
  609.             (ush)(z->ver & 0xff) % (ush)10, z->ver >> 8);
  610.           zipwarn(errbuf, z->zname);
  611.         }
  612.         if (z->flg != z->lflg)
  613.         {
  614.           sprintf(errbuf, "local flags = 0x%04x, central = 0x%04x: ",
  615.                   z->lflg, z->flg);
  616.           zipwarn(errbuf, z->zname);
  617.         }
  618.         else if (z->flg & ~0xf)
  619.         {
  620.           sprintf(errbuf, "undefined bits used in flags = 0x%04x: ", z->flg);
  621.           zipwarn(errbuf, z->zname);
  622.         }
  623.         if (z->how > DEFLATE)
  624.         {
  625.           sprintf(errbuf, "unknown compression method %u: ", z->how);
  626.           zipwarn(errbuf, z->zname);
  627.         }
  628.         if (z->dsk)
  629.         {
  630.           sprintf(errbuf, "starts on disk %u: ", z->dsk);
  631.           zipwarn(errbuf, z->zname);
  632.         }
  633.         if (z->att & ~1)
  634.         {
  635.           sprintf(errbuf, "unknown internal attributes = 0x%04x: ", z->att);
  636.           zipwarn(errbuf, z->zname);
  637.         }
  638. #if 0
  639. /* This test is ridiculous, it produces an error message for almost every */
  640. /* platform of origin other than MS-DOS, Unix, VMS, and Acorn!  Perhaps   */
  641. /* we could test "if (z->dosflag && z->atx & ~0xffL)", but what for?      */
  642.         if (((n = z->vem >> 8) != 3) && n != 2 && n != 13 && z->atx & ~0xffL)
  643.         {
  644.           sprintf(errbuf, "unknown external attributes = 0x%08lx: ", z->atx);
  645.           zipwarn(errbuf, z->zname);
  646.         }
  647. #endif
  648.         if (z->ext || z->cext)
  649.           if (z->ext && z->cext && z->extra != z->cextra)
  650.           {
  651.             sprintf(errbuf,
  652.                     "local extra (%ld bytes) != central extra (%ld bytes): ",
  653.                     (ulg)z->ext, (ulg)z->cext);
  654.             zipwarn(errbuf, z->zname);
  655.           }
  656. #ifndef RISCOS
  657.           else if (noisy)
  658. #else /* RISCOS */
  659. /* avoid warnings on zipfiles created on RISCOS itself! */
  660. /* probably this can be made more generic with z->vem != OS_CODE */
  661. /* or, was this warning really intended (eg. OS/2)? */
  662.           else if (noisy && ((n = z->vem >> 8) != 13))
  663. #endif
  664.           {
  665.             fprintf(stderr, "zip info: %s has %ld bytes of %sextra data\n",
  666.                     z->zname, z->ext ? (ulg)z->ext : (ulg)z->cext,
  667.                     z->ext ? (z->cext ? "" : "local ") : "central ");
  668.           }
  669.       }
  670.  
  671.       /* Clear actions */
  672.       z->mark = 0;
  673.       z->trash = 0;
  674.  
  675.       /* Update file offset */
  676.       p += 4 + CENHEAD + z->nam + z->cext + z->com;
  677.  
  678.       /* Advance to next header structure */
  679.       z = z->nxt;
  680.  
  681.       /* Read next signature */
  682.       if (fread(b, 4, 1, f) != 1)
  683.         return ferror(f) ? ZE_READ : ZE_EOF;
  684.     }
  685.  
  686.     /* Read end header */
  687.     if (!fix) {
  688.       if (z != NULL || LG(b) != ENDSIG)
  689.       {
  690.         zipwarn("missing end signature--probably not a zip file (did you", "");
  691.         zipwarn("remember to use binary mode when you transferred it?)", "");
  692.         return ZE_FORM;
  693.       }
  694.       if (fread(b, ENDHEAD, 1, f) != 1)
  695.         return ferror(f) ? ZE_READ : ZE_EOF;
  696.       if (SH(ENDDSK + b) || SH(ENDBEG + b) ||
  697.           SH(ENDSUB + b) != SH(ENDTOT + b))
  698.         zipwarn("multiple disk information ignored", "");
  699.       if (zcount != (extent)(SH(ENDSUB + b)))
  700.       {
  701.         zipwarn("count in end of central directory incorrect", "");
  702.         return ZE_FORM;
  703.       }
  704.       temp = LG(ENDSIZ + b);
  705.       if (temp != p - s)
  706.       {
  707.         zipwarn("central directory size is incorrect (made by stzip?)", "");
  708.         /* stzip 0.9 gets this wrong, so be tolerant */
  709.         /* return ZE_FORM; */
  710.       }
  711.       temp = LG(ENDOFF + b);
  712.       if (temp != s && !adjust)
  713.       {
  714.         zipwarn("central directory start is incorrect", "");
  715.         return ZE_FORM;
  716.       }
  717.     }
  718.     cenbeg = s;
  719.     zcomlen = fix ? 0 : SH(ENDCOM + b);
  720.     if (zcomlen)
  721.     {
  722.       if ((zcomment = malloc(zcomlen)) == NULL)
  723.         return ZE_MEM;
  724.       if (fread(zcomment, zcomlen, 1, f) != 1)
  725.       {
  726.         free((zvoid *)zcomment);
  727.         return ferror(f) ? ZE_READ : ZE_EOF;
  728.       }
  729.     }
  730.     if (zipbeg)   /* this is useful info even for -A */
  731.     {
  732.       sprintf(errbuf, " has a preamble of %lu bytes", zipbeg);
  733.       zipwarn(zipfile, errbuf);
  734. #ifdef GREG /* -A option could be useful on files other than unzipsfx */
  735.       if (adjust)
  736.         zipwarn("", "(this should match the size of the prepended unzipsfx stub)");
  737. #endif
  738.     }
  739. #ifndef CMS_MVS
  740.     if (!fix && getc(f) != EOF)
  741.       zipwarn("garbage at end of zip file ignored", "");
  742. #endif /* !CMS_MVS */
  743.  
  744.     /* Done with zip file for now */
  745.     fclose(f);
  746.  
  747.     /* If one or more files, sort by name */
  748.     if (zcount)
  749.     {
  750.       if ((x = zsort =
  751.           (struct zlist far **)malloc(zcount * sizeof(struct zlist far *))) ==
  752.           NULL)
  753.         return ZE_MEM;
  754.       for (z = zfiles; z != NULL; z = z->nxt)
  755.         *x++ = z;
  756.       qsort((char *)zsort, zcount, sizeof(struct zlist far *), zqcmp);
  757.     }
  758.   }
  759.   return ZE_OK;
  760. }
  761.  
  762.  
  763. int putlocal(z, f)
  764. struct zlist far *z;    /* zip entry to write local header for */
  765. FILE *f;                /* file to write to */
  766. /* Write a local header described by *z to file *f.  Return an error code
  767.    in the ZE_ class. */
  768. {
  769.   PUTLG(LOCSIG, f);
  770.   PUTSH(z->ver, f);
  771.   PUTSH(z->lflg, f);
  772.   PUTSH(z->how, f);
  773.   PUTLG(z->tim, f);
  774.   PUTLG(z->crc, f);
  775.   PUTLG(z->siz, f);
  776.   PUTLG(z->len, f);
  777.   PUTSH(z->nam, f);
  778.   PUTSH(z->ext, f);
  779.   if (fwrite(z->zname, 1, z->nam, f) != z->nam ||
  780.       (z->ext && fwrite(z->extra, 1, z->ext, f) != z->ext))
  781.     return ZE_TEMP;
  782.   return ZE_OK;
  783. }
  784.  
  785. int putextended(z, f)
  786. struct zlist far *z;    /* zip entry to write local header for */
  787. FILE *f;                /* file to write to */
  788. /* Write an extended local header described by *z to file *f.
  789.  * Return an error code in the ZE_ class. */
  790. {
  791.   PUTLG(EXTLOCSIG, f);
  792.   PUTLG(z->crc, f);
  793.   PUTLG(z->siz, f);
  794.   PUTLG(z->len, f);
  795.   return ZE_OK;
  796. }
  797.  
  798. int putcentral(z, f)
  799. struct zlist far *z;    /* zip entry to write central header for */
  800. FILE *f;                /* file to write to */
  801. /* Write a central header described by *z to file *f.  Return an error code
  802.    in the ZE_ class. */
  803. {
  804.   PUTLG(CENSIG, f);
  805.   PUTSH(z->vem, f);
  806.   PUTSH(z->ver, f);
  807.   PUTSH(z->flg, f);
  808.   PUTSH(z->how, f);
  809.   PUTLG(z->tim, f);
  810.   PUTLG(z->crc, f);
  811.   PUTLG(z->siz, f);
  812.   PUTLG(z->len, f);
  813.   PUTSH(z->nam, f);
  814.   PUTSH(z->cext, f);
  815.   PUTSH(z->com, f);
  816.   PUTSH(z->dsk, f);
  817.   PUTSH(z->att, f);
  818.   PUTLG(z->atx, f);
  819.   PUTLG(z->off, f);
  820.   if (fwrite(z->zname, 1, z->nam, f) != z->nam ||
  821.       (z->cext && fwrite(z->cextra, 1, z->cext, f) != z->cext) ||
  822.       (z->com && fwrite(z->comment, 1, z->com, f) != z->com))
  823.     return ZE_TEMP;
  824.   return ZE_OK;
  825. }
  826.  
  827.  
  828. int putend(n, s, c, m, z, f)
  829. int n;                  /* number of entries in central directory */
  830. ulg s;                  /* size of central directory */
  831. ulg c;                  /* offset of central directory */
  832. extent m;               /* length of zip file comment (0 if none) */
  833. char *z;                /* zip file comment if m != 0 */
  834. FILE *f;                /* file to write to */
  835. /* Write the end of central directory data to file *f.  Return an error code
  836.    in the ZE_ class. */
  837. {
  838.   PUTLG(ENDSIG, f);
  839.   PUTSH(0, f);
  840.   PUTSH(0, f);
  841.   PUTSH(n, f);
  842.   PUTSH(n, f);
  843.   PUTLG(s, f);
  844.   PUTLG(c, f);
  845.   PUTSH(m, f);
  846.   if (m && fwrite(z, 1, m, f) != m)
  847.     return ZE_TEMP;
  848.   return ZE_OK;
  849. }
  850.  
  851.  
  852. /* Note: a zip "entry" includes a local header (which includes the file
  853.    name), an encryption header if encrypting, the compressed data
  854.    and possibly an extended local header. */
  855.  
  856. int zipcopy(z, x, y)
  857. struct zlist far *z;    /* zip entry to copy */
  858. FILE *x, *y;            /* source and destination files */
  859. /* Copy the zip entry described by *z from file *x to file *y.  Return an
  860.    error code in the ZE_ class.  Also update tempzn by the number of bytes
  861.    copied. */
  862. {
  863.   ulg n;                /* holds local header offset */
  864.  
  865.   Trace((stderr, "zipcopy %s\n", z->zname));
  866.   n = (ulg)(4 + LOCHEAD) + (ulg)z->nam + (ulg)z->ext;
  867.  
  868.   if (fix > 1) {
  869.     if (fseek(x, z->off + n, SEEK_SET)) /* seek to compressed data */
  870.       return ferror(x) ? ZE_READ : ZE_EOF;
  871.  
  872.     if (fix > 2) {
  873.       /* Update length of entry's name, it may have been changed.  This is
  874.          needed to support the ZipNote ability to rename archive entries. */
  875.       z->nam = strlen(z->zname);
  876.       n = (ulg)(4 + LOCHEAD) + (ulg)z->nam + (ulg)z->ext;
  877.     }
  878.  
  879.     /* do not trust the old compressed size */
  880.     if (putlocal(z, y) != ZE_OK)
  881.       return ZE_TEMP;
  882.  
  883.     z->off = tempzn;
  884.     tempzn += n;
  885.     n = z->siz;
  886.   } else {
  887.     if (fseek(x, z->off, SEEK_SET))     /* seek to local header */
  888.       return ferror(x) ? ZE_READ : ZE_EOF;
  889.  
  890.     z->off = tempzn;
  891.     n += z->siz;
  892.   }
  893.   /* copy the compressed data and the extended local header if there is one */
  894.   if (z->lflg & 8) n += 16;
  895.   tempzn += n;
  896.   return fcopy(x, y, n);
  897. }
  898.  
  899.  
  900. #ifndef UTIL
  901.  
  902. #ifdef USE_EF_UX_TIME
  903.  
  904. local int ef_scan_ux_time(ef_buf, ef_len, z_utim)
  905. char *ef_buf;                   /* buffer containing extra field */
  906. extent ef_len;                  /* total length of extra field */
  907. ztimbuf *z_utim;                /* return storage: atime and mtime */
  908. /* This function scans the extra field for a IZUNIX block containing
  909.  * Unix style time_t (GMT) values for the entry's access and modification
  910.  * time.  If a valid block is found, both time stamps are copied to the
  911.  * ztimebuf structure.
  912.  * The return value is 1 (TRUE) in case of success, 0 otherwise.
  913.  */
  914. {
  915.   int r = 0;
  916.   unsigned eb_id;
  917.   extent eb_len;
  918.  
  919.   if (ef_len == 0 || ef_buf == NULL)
  920.     return 0;
  921.  
  922.   Trace((stderr,"\nef_scan_ux_time: scanning extra field of length %u\n",
  923.          ef_len));
  924.   while (ef_len >= EB_HEADSIZE) {
  925.     eb_id = SH(EB_ID + ef_buf);
  926.     eb_len = SH(EB_LEN + ef_buf);
  927.  
  928.     if (eb_len > (ef_len - EB_HEADSIZE)) {
  929.       /* Discovered some extra field inconsistency! */
  930.       Trace((stderr,"ef_scan_ux_time: block length %u > rest ef_size %u\n",
  931.              eb_len, ef_len - EB_HEADSIZE));
  932.       break;
  933.     }
  934.  
  935.     if (eb_id == EF_IZUNIX && eb_len >= EB_UX_MINLEN) {
  936.        Trace((stderr,"ef_scan_ux_time: Found IZUNIX extra field\n"));
  937.        z_utim->actime  = LG((EB_HEADSIZE+EB_UX_ATIME) + ef_buf);
  938.        z_utim->modtime = LG((EB_HEADSIZE+EB_UX_MTIME) + ef_buf);
  939.        Trace((stderr,"  Unix EF access time = %ld\n",z_utim->actime));
  940.        Trace((stderr,"  Unix EF modif. time = %ld\n",z_utim->modtime));
  941.        r = 1;                   /* signal success */
  942.        break;
  943.     }
  944.     /* Skip this extra field block */
  945.     ef_buf += (eb_len + EB_HEADSIZE);
  946.     ef_len -= (eb_len + EB_HEADSIZE);
  947.   }
  948.  
  949.   return r;
  950. }
  951.  
  952. int get_ef_ux_ztime(z, z_utim)
  953. struct zlist far *z;
  954. ztimbuf *z_utim;
  955. {
  956.   int r;
  957.  
  958.   /* First, scan local extra field. */
  959.   r = ef_scan_ux_time(z->extra, z->ext, z_utim);
  960.  
  961.   /* If this was not successful, try central extra field, but only if
  962.      is really different. */
  963.   if (!r && z->cext > 0 && z->cextra != z->extra)
  964.     r = ef_scan_ux_time(z->cextra, z->cext, z_utim);
  965.  
  966.   return r;
  967. }
  968.  
  969. #endif /* USE_EF_UX_TIME */
  970.  
  971.  
  972. local void cutpath(p)
  973. char *p;                /* path string */
  974. /* Cut the last path component off the name *p in place.
  975.  * This should work on both internal and external names.
  976.  */
  977. {
  978.   char *r;              /* pointer to last path delimiter */
  979.  
  980. #ifdef VMS                      /* change [w.x.y]z to [w.x]y.DIR */
  981.   if ((r = strrchr(p, ']')) != NULL)
  982.   {
  983.     *r = 0;
  984.     if ((r = strrchr(p, '.')) != NULL)
  985.     {
  986.       *r = ']';
  987.       strcat(r, ".DIR;1");     /* this assumes a little padding--see PAD */
  988.     } else {
  989.       *p = 0;
  990.     }
  991.   } else {
  992.     if ((r = strrchr(p, '/')) != NULL)
  993.       *r = 0;
  994.     else
  995.       *p = 0;
  996.   }
  997. #else /* !VMS */
  998.   if ((r = strrchr(p, '/')) != NULL)
  999.     *r = 0;
  1000.   else
  1001.     *p = 0;
  1002. #endif /* ?VMS */
  1003. }
  1004.  
  1005. int trash()
  1006. /* Delete the compressed files and the directories that contained the deleted
  1007.    files, if empty.  Return an error code in the ZE_ class.  Failure of
  1008.    destroy() or deletedir() is ignored. */
  1009. {
  1010.   extent i;             /* counter on deleted names */
  1011.   extent n;             /* number of directories to delete */
  1012.   struct zlist far **s; /* table of zip entries to handle, sorted */
  1013.   struct zlist far *z;  /* current zip entry */
  1014.  
  1015.   /* Delete marked names and count directories */
  1016.   n = 0;
  1017.   for (z = zfiles; z != NULL; z = z->nxt)
  1018.     if (z->mark == 1 || z->trash)
  1019.     {
  1020.       z->mark = 1;
  1021.       if (z->zname[z->nam - 1] != '/') { /* don't unlink directory */
  1022.         if (verbose)
  1023.           fprintf(mesg, "zip diagnostic: deleting file %s\n", z->name);
  1024.         if (destroy(z->name)) {
  1025.           zipwarn("error deleting", z->name);
  1026.         }
  1027.         /* Try to delete all paths that lead up to marked names. This is
  1028.          * necessary only with the -D option.
  1029.          */
  1030.         if (!dirnames) {
  1031.           cutpath(z->name);
  1032.           cutpath(z->zname);
  1033.           if (z->zname[0] != '\0') {
  1034.             strcat(z->zname, "/");
  1035.           }
  1036.           z->nam = strlen(z->zname);
  1037.           if (z->nam > 0) n++;
  1038.         }
  1039.       } else {
  1040.         n++;
  1041.       }
  1042.     }
  1043.  
  1044.   /* Construct the list of all marked directories. Some may be duplicated
  1045.    * if -D was used.
  1046.    */
  1047.   if (n)
  1048.   {
  1049.     if ((s = (struct zlist far **)malloc(n*sizeof(struct zlist far *))) ==
  1050.         NULL)
  1051.       return ZE_MEM;
  1052.     n = 0;
  1053.     for (z = zfiles; z != NULL; z = z->nxt) {
  1054.       if (z->mark && z->nam > 0 && z->zname[z->nam - 1] == '/'
  1055.           && (n == 0 || strcmp(z->zname, s[n-1]->zname) != 0)) {
  1056.         s[n++] = z;
  1057.       }
  1058.     }
  1059.     /* Sort the files in reverse order to get subdirectories first.
  1060.      * To avoid problems with strange naming conventions as in VMS,
  1061.      * we sort on the internal names, so x/y/z will always be removed
  1062.      * before x/y. On VMS, x/y/z > x/y but [x.y.z] < [x.y]
  1063.      */
  1064.     qsort((char *)s, n, sizeof(struct zlist far *), rqcmp);
  1065.  
  1066.     for (i = 0; i < n; i++) {
  1067.       char *p = s[i]->name;
  1068.       if (*p == '\0') continue;
  1069.       if (p[strlen(p) - 1] == '/') { /* keep VMS [x.y]z.dir;1 intact */
  1070.         p[strlen(p) - 1] = '\0';
  1071.       }
  1072.       if (i == 0 || strcmp(s[i]->zname, s[i-1]->zname) != 0) {
  1073.         if (verbose) {
  1074.           fprintf(mesg, "zip diagnostic: trashing directory %s (if empty)\n",
  1075.                   s[i]->name);
  1076.         }
  1077.         deletedir(s[i]->name);
  1078.       }
  1079.     }
  1080.     free((zvoid *)s);
  1081.   }
  1082.   return ZE_OK;
  1083. }
  1084.  
  1085. #endif /* !UTIL */
  1086.