home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip22.zip / zipfile.c < prev    next >
C/C++ Source or Header  |  1997-10-26  |  46KB  |  1,465 lines

  1. /*
  2.  
  3.  Copyright (C) 1990-1997 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.  * XXX start of zipfile.h
  28.  */
  29. /* Macros for converting integers in little-endian to machine format */
  30. #define SH(a) (((ush)(uch)(a)[0]) | (((ush)(uch)(a)[1]) << 8))
  31. #define LG(a) ((ulg)SH(a) | ((ulg)SH((a)+2) << 16))
  32.  
  33. /* Macros for writing machine integers to little-endian format */
  34. #define PUTSH(a,f) {putc((char)((a) & 0xff),(f)); putc((char)((a) >> 8),(f));}
  35. #define PUTLG(a,f) {PUTSH((a) & 0xffff,(f)) PUTSH((a) >> 16,(f))}
  36.  
  37.  
  38. /* -- Structure of a ZIP file -- */
  39.  
  40. /* Signatures for zip file information headers */
  41. #define LOCSIG     0x04034b50L
  42. #define CENSIG     0x02014b50L
  43. #define ENDSIG     0x06054b50L
  44. #define EXTLOCSIG  0x08074b50L
  45.  
  46. /* Offsets of values in headers */
  47. #define LOCVER  0               /* version needed to extract */
  48. #define LOCFLG  2               /* encrypt, deflate flags */
  49. #define LOCHOW  4               /* compression method */
  50. #define LOCTIM  6               /* last modified file time, DOS format */
  51. #define LOCDAT  8               /* last modified file date, DOS format */
  52. #define LOCCRC  10              /* uncompressed crc-32 for file */
  53. #define LOCSIZ  14              /* compressed size in zip file */
  54. #define LOCLEN  18              /* uncompressed size */
  55. #define LOCNAM  22              /* length of filename */
  56. #define LOCEXT  24              /* length of extra field */
  57.  
  58. #define EXTCRC  0               /* uncompressed crc-32 for file */
  59. #define EXTSIZ  4               /* compressed size in zip file */
  60. #define EXTLEN  8               /* uncompressed size */
  61.  
  62. #define CENVEM  0               /* version made by */
  63. #define CENVER  2               /* version needed to extract */
  64. #define CENFLG  4               /* encrypt, deflate flags */
  65. #define CENHOW  6               /* compression method */
  66. #define CENTIM  8               /* last modified file time, DOS format */
  67. #define CENDAT  10              /* last modified file date, DOS format */
  68. #define CENCRC  12              /* uncompressed crc-32 for file */
  69. #define CENSIZ  16              /* compressed size in zip file */
  70. #define CENLEN  20              /* uncompressed size */
  71. #define CENNAM  24              /* length of filename */
  72. #define CENEXT  26              /* length of extra field */
  73. #define CENCOM  28              /* file comment length */
  74. #define CENDSK  30              /* disk number start */
  75. #define CENATT  32              /* internal file attributes */
  76. #define CENATX  34              /* external file attributes */
  77. #define CENOFF  38              /* relative offset of local header */
  78.  
  79. #define ENDDSK  0               /* number of this disk */
  80. #define ENDBEG  2               /* number of the starting disk */
  81. #define ENDSUB  4               /* entries on this disk */
  82. #define ENDTOT  6               /* total number of entries */
  83. #define ENDSIZ  8               /* size of entire central directory */
  84. #define ENDOFF  12              /* offset of central on starting disk */
  85. #define ENDCOM  16              /* length of zip file comment */
  86.  
  87.  
  88.  
  89. /* Local functions */
  90.  
  91. local int zqcmp OF((ZCONST zvoid *, ZCONST zvoid *));
  92. #ifndef UTIL
  93.    local void zipoddities OF((struct zlist far *));
  94.    local int readzipfile2 OF((void));
  95.    local int rqcmp OF((ZCONST zvoid *, ZCONST zvoid *));
  96.    local int zbcmp OF((ZCONST zvoid *, ZCONST zvoid far *));
  97. #  ifdef USE_EF_UT_TIME
  98.      local int ef_scan_ut_time OF((char *ef_buf, extent ef_len, int ef_is_cent,
  99.                                    iztimes *z_utim));
  100. #  endif /* USE_EF_UT_TIME */
  101.    local void cutpath OF((char *p));
  102. #endif /* !UTIL */
  103.  
  104. /*
  105.  * XXX end of zipfile.h
  106.  */
  107.  
  108. /* Local data */
  109.  
  110. #ifdef HANDLE_AMIGA_SFX
  111.    ulg amiga_sfx_offset;        /* place where size field needs updating */
  112. #endif
  113.  
  114.  
  115. local int zqcmp(a, b)
  116. ZCONST zvoid *a, *b;          /* pointers to pointers to zip entries */
  117. /* Used by qsort() to compare entries in the zfile list.
  118.  * Compares the internal names z->iname */
  119. {
  120.   return namecmp((*(struct zlist far **)a)->iname,
  121.                  (*(struct zlist far **)b)->iname);
  122. }
  123.  
  124. #ifndef UTIL
  125.  
  126. local int rqcmp(a, b)
  127. ZCONST zvoid *a, *b;          /* pointers to pointers to zip entries */
  128. /* Used by qsort() to compare entries in the zfile list.
  129.  * Compare the internal names z->iname, but in reverse order. */
  130. {
  131.   return namecmp((*(struct zlist far **)b)->iname,
  132.                  (*(struct zlist far **)a)->iname);
  133. }
  134.  
  135.  
  136. local int zbcmp(n, z)
  137. ZCONST zvoid *n;        /* string to search for */
  138. ZCONST zvoid far *z;    /* pointer to a pointer to a zip entry */
  139. /* Used by search() to compare a target to an entry in the zfile list. */
  140. {
  141.   return namecmp((char *)n, ((struct zlist far *)z)->zname);
  142. }
  143.  
  144.  
  145. struct zlist far *zsearch(n)
  146. char *n;                /* name to find */
  147. /* Return a pointer to the entry in zfile with the name n, or NULL if
  148.    not found. */
  149. {
  150.   zvoid far **p;        /* result of search() */
  151.  
  152.   if (zcount && (p = search(n, (zvoid far **)zsort, zcount, zbcmp)) != NULL)
  153.     return *(struct zlist far **)p;
  154.   else
  155.     return NULL;
  156. }
  157.  
  158. #endif /* !UTIL */
  159.  
  160. #ifndef VMS
  161. #  define PATHCUT '/'
  162.  
  163. char *ziptyp(s)
  164. char *s;                /* file name to force to zip */
  165. /* If the file name *s has a dot (other than the first char), or if
  166.    the -A option is used (adjust self-extracting file) then return
  167.    the name, otherwise append .zip to the name.  Allocate the space for
  168.    the name in either case.  Return a pointer to the new name, or NULL
  169.    if malloc() fails. */
  170. {
  171.   char *q;              /* temporary pointer */
  172.   char *t;              /* pointer to malloc'ed string */
  173.  
  174.   if ((t = malloc(strlen(s) + 5)) == NULL)
  175.     return NULL;
  176.   strcpy(t, s);
  177. #ifdef __human68k__
  178.   _toslash(t);
  179. #endif
  180. #ifdef MSDOS
  181.   for (q = t; *q; q++)
  182.     if (*q == '\\')
  183.       *q = '/';
  184. #endif /* MSDOS */
  185.   if (adjust) return t;
  186. #ifndef RISCOS
  187. # ifndef QDOS
  188. #  ifdef AMIGA
  189.   if ((q = strrchr(t, '/')) == NULL)
  190.     q = strrchr(t, ':');
  191.   if (strrchr((q ? q + 1 : t), '.') == NULL)
  192. #  else /* !AMIGA */
  193. #    ifdef TANDEM
  194.   if (strrchr((q = strrchr(t, '.')) == NULL ? t : q + 1, ' ') == NULL)
  195. #    else /* !TANDEM */
  196.   if (strrchr((q = strrchr(t, PATHCUT)) == NULL ? t : q + 1, '.') == NULL)
  197. #    endif /* TANDEM */
  198. #  endif /* ?AMIGA */
  199. #  ifdef CMS_MVS
  200.      if (strncmp(t,"dd:",3) != 0 && strncmp(t,"DD:",3) != 0)
  201. #  endif /* CMS_MVS */
  202. #  ifdef TANDEM       /*  Tandem can't cope with extensions */
  203.     strcat(t, " ZIP");
  204. #  else /* !TANDEM */
  205.     strcat(t, ".zip");
  206. #  endif /* !TANDEM */
  207. # else
  208.   q = LastDir(t);
  209.   if(strrchr(q, '_') == NULL && strrchr(q, '.') == NULL)
  210.   {
  211.       strcat(t, "_zip");
  212.   }
  213. # endif /* QDOS */
  214. #endif /* !RISCOS */
  215.   return t;
  216. }
  217.  
  218. #else /* VMS */
  219.  
  220. # define PATHCUT ']'
  221.  
  222. char *ziptyp(s)
  223. char *s;
  224. {   int status;
  225.     struct FAB fab;
  226.     struct NAM nam;
  227.     static char zero=0;
  228.     char result[NAM$C_MAXRSS+1],exp[NAM$C_MAXRSS+1];
  229.     char *p;
  230.  
  231.     fab = cc$rms_fab;
  232.     nam = cc$rms_nam;
  233.  
  234.     fab.fab$l_fna = s;
  235.     fab.fab$b_fns = strlen(fab.fab$l_fna);
  236.  
  237.     fab.fab$l_dna = "sys$disk:[].zip";          /* Default fspec */
  238.     fab.fab$b_dns = strlen(fab.fab$l_dna);
  239.  
  240.     fab.fab$l_nam = &nam;
  241.  
  242.     nam.nam$l_rsa = result;                     /* Put resultant name of */
  243.     nam.nam$b_rss = sizeof(result)-1;           /* existing zipfile here */
  244.  
  245.     nam.nam$l_esa = exp;                        /* For full spec of */
  246.     nam.nam$b_ess = sizeof(exp)-1;              /* file to create */
  247.  
  248.     status = sys$parse(&fab);
  249.     if( (status & 1) == 0 )
  250.         return &zero;
  251.  
  252.     status = sys$search(&fab);
  253.     if( status & 1 )
  254.     {               /* Existing ZIP file */
  255.         int l;
  256.         if( (p=malloc( (l=nam.nam$b_rsl) + 1 )) != NULL )
  257.         {       result[l] = 0;
  258.                 strcpy(p,result);
  259.         }
  260.     }
  261.     else
  262.     {               /* New ZIP file */
  263.         int l;
  264.         if( (p=malloc( (l=nam.nam$b_esl) + 1 )) != NULL )
  265.         {       exp[l] = 0;
  266.                 strcpy(p,exp);
  267.         }
  268.     }
  269.     return p;
  270. }
  271.  
  272. #endif  /* VMS */
  273.  
  274. #ifndef UTIL
  275.  
  276. local void zipoddities(z)
  277. struct zlist far *z;
  278. {
  279.    if ((z->vem >> 8) >= NUM_HOSTS)
  280.    {
  281.      sprintf(errbuf, "made by version %d.%d on system type %d: ",
  282.              (ush)(z->vem & 0xff) / (ush)10, (ush)(z->vem & 0xff) % (ush)10,
  283.              z->vem >> 8);
  284.      zipwarn(errbuf, z->zname);
  285.    }
  286.    if (z->ver != 10 && z->ver != 11 && z->ver != 20)
  287.    {
  288.        sprintf(errbuf, "needs unzip %d.%d on system type %d: ",
  289.                (ush)(z->ver & 0xff) / (ush)10,
  290.                (ush)(z->ver & 0xff) % (ush)10, z->ver >> 8);
  291.        zipwarn(errbuf, z->zname);
  292.     }
  293.     if (z->flg != z->lflg)
  294.     {
  295.        sprintf(errbuf, "local flags = 0x%04x, central = 0x%04x: ",
  296.                z->lflg, z->flg);
  297.        zipwarn(errbuf, z->zname);
  298.      }
  299.      else if (z->flg & ~0xf)
  300.      {
  301.        sprintf(errbuf, "undefined bits used in flags = 0x%04x: ", z->flg);
  302.        zipwarn(errbuf, z->zname);
  303.      }
  304.      if (z->how > DEFLATE)
  305.      {
  306.        sprintf(errbuf, "unknown compression method %u: ", z->how);
  307.        zipwarn(errbuf, z->zname);
  308.      }
  309.      if (z->dsk)
  310.      {
  311.        sprintf(errbuf, "starts on disk %u: ", z->dsk);
  312.        zipwarn(errbuf, z->zname);
  313.       }
  314.       if (z->att!=ASCII && z->att!=BINARY && z->att!=__EBCDIC)
  315.       {
  316.         sprintf(errbuf, "unknown internal attributes = 0x%04x: ", z->att);
  317.         zipwarn(errbuf, z->zname);
  318.       }
  319. #if 0
  320. /* This test is ridiculous, it produces an error message for almost every */
  321. /* platform of origin other than MS-DOS, Unix, VMS, and Acorn!  Perhaps   */
  322. /* we could test "if (z->dosflag && z->atx & ~0xffL)", but what for?      */
  323.      if (((n = z->vem >> 8) != 3) && n != 2 && n != 13 && z->atx & ~0xffL)
  324.      {
  325.        sprintf(errbuf, "unknown external attributes = 0x%08lx: ", z->atx);
  326.        zipwarn(errbuf, z->zname);
  327.      }
  328. #endif
  329.      if (z->ext || z->cext)
  330.        if (z->ext && z->cext && z->extra != z->cextra)
  331.        {
  332.          sprintf(errbuf,
  333.                  "local extra (%ld bytes) != central extra (%ld bytes): ",
  334.                  (ulg)z->ext, (ulg)z->cext);
  335.           zipwarn(errbuf, z->zname);
  336.        }
  337. #ifndef RISCOS
  338.        else if (noisy)
  339. #else /* RISCOS */
  340. /* avoid warnings on zipfiles created on RISCOS itself! */
  341. /* probably this can be made more generic with z->vem != OS_CODE */
  342. /* or, was this warning really intended (eg. OS/2)? */
  343.        else if (noisy && ((z->vem >> 8) != 13))
  344. #endif
  345.         {
  346.         fprintf(stderr, "zip info: %s has %ld bytes of %sextra data\n",
  347.                 z->zname, z->ext ? (ulg)z->ext : (ulg)z->cext,
  348.                 z->ext ? (z->cext ? "" : "local ") : "central ");
  349.         }
  350. }
  351.  
  352. /*
  353.  * readzipfile2 is called with zip -F or zip -FF
  354.  * read the file from front to back and pick up the pieces
  355.  * NOTE: there are still checks missing to see if the header
  356.  *       that was found is *VALID*
  357.  */
  358. local int readzipfile2()
  359. /*
  360.    The name of the zip file is pointed to by the global "zipfile".  The
  361.    globals zfiles, zcount, zcomlen, zcomment, and zsort are filled in.
  362.    Return an error code in the ZE_ class.
  363. */
  364. {
  365.   ulg a = 0L;           /* attributes returned by filetime() */
  366.   char b[CENHEAD];      /* buffer for central headers */
  367.   FILE *f;              /* zip file */
  368.   ush flg;              /* general purpose bit flag */
  369.   int m;                /* mismatch flag */
  370.   extent n;             /* length of name */
  371.   ulg p;                /* current file offset */
  372.   ulg s;                /* size of data, start of central */
  373.   struct zlist far * far *x;    /* pointer last entry's link */
  374.   struct zlist far *z;  /* current zip entry structure */
  375.  
  376.   /* Initialize zip file info */
  377.   zipbeg = 0;
  378.   zfiles = NULL;                        /* Points to first header */
  379.   zcomlen = 0;                          /* zip file comment length */
  380.  
  381.   /* If zip file exists, read headers and check structure */
  382. #ifdef VMS
  383.   if (zipfile == NULL || !(*zipfile) || !strcmp(zipfile, "-"))
  384.     return ZE_OK;
  385.   {
  386.     int rtype;
  387.  
  388.     if ((VMSmunch(zipfile, GET_RTYPE, (char *)&rtype) == RMS$_NORMAL) &&
  389.         (rtype == FAT$C_VARIABLE)) {
  390.       fprintf(stderr,
  391.      "\n     Error:  zipfile is in variable-length record format.  Please\n\
  392.      run \"bilf b %s\" to convert the zipfile to fixed-length\n\
  393.      record format.\n\n", zipfile);
  394.       return ZE_FORM;
  395.     }
  396.   }
  397.   if ((f = fopen(zipfile, FOPR)) != NULL)
  398. #else /* !VMS */
  399.   if (zipfile != NULL && *zipfile && strcmp(zipfile, "-") &&
  400.       (f = fopen(zipfile, FOPR)) != NULL)
  401. #endif /* ?VMS */
  402.   {
  403.     /* Get any file attribute valid for this OS, to set in the central
  404.      * directory when fixing the archive:
  405.      */
  406. #ifndef UTIL
  407.     filetime(zipfile, &a, (long*)&s, NULL);
  408. #endif
  409.     x = &zfiles;                        /* first link */
  410.     p = 0;                              /* starting file offset */
  411.     zcount = 0;                         /* number of files */
  412. #ifdef HANDLE_AMIGA_SFX
  413.     amiga_sfx_offset = 0L;
  414. #endif
  415.  
  416.     /* Find start of zip structures */
  417.     for (;;) {
  418.       while ((m = getc(f)) != EOF && m != 0x50)    /* 0x50 == 'P' */
  419.       {
  420. #ifdef HANDLE_AMIGA_SFX
  421.         if (p == 0 && m == 0)
  422.           amiga_sfx_offset = 1L;
  423.         else if (amiga_sfx_offset) {
  424.           if ((p == 1 && m != 0) || (p == 2 && m != 3)
  425.                                  || (p == 3 && (uch) m != 0xF3))
  426.             amiga_sfx_offset = 0L;
  427.         }
  428. #endif /* HANDLE_AMIGA_SFX */
  429.         p++;
  430.       }
  431.       b[0] = (char) m;
  432.       if (fread(b+1, 3, 1, f) != 1 || (s = LG(b)) == LOCSIG || s == ENDSIG)
  433.         break;
  434.       if (fseek(f, -3L, SEEK_CUR))
  435.         return ferror(f) ? ZE_READ : ZE_EOF;
  436.       p++;
  437.     }
  438.     zipbeg = p;
  439. #ifdef HANDLE_AMIGA_SFX
  440.     if (amiga_sfx_offset && zipbeg >= 12 && (zipbeg & 3) == 0
  441.         && fseek(f, -12L, SEEK_CUR) == 0 && fread(b, 12, 1, f) == 1
  442.         && LG(b + 4) == 0xF1030000 /* 1009 in Motorola byte order */)
  443.       amiga_sfx_offset = zipbeg - 4;
  444.     else
  445.       amiga_sfx_offset = 0L;
  446. #endif /* HANDLE_AMIGA_SFX */
  447.  
  448.     /* Read local headers */
  449.     while (LG(b) == LOCSIG)
  450.     {
  451.       if ((z = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL)
  452.         return ZE_MEM;
  453.       if (fread(b, LOCHEAD, 1, f) != 1) {
  454.           farfree((zvoid far *)z);
  455.           break;
  456.       }
  457.  
  458.       z->ver = SH(LOCVER + b);
  459.       z->vem = dosify ? 20 : OS_CODE + Z_MAJORVER * 10 + Z_MINORVER;
  460.       z->dosflag = dosify;
  461.       flg = z->flg = z->lflg = SH(LOCFLG + b);
  462.       z->how = SH(LOCHOW + b);
  463.       z->tim = LG(LOCTIM + b);          /* time and date into one long */
  464.       z->crc = LG(LOCCRC + b);
  465.       z->siz = LG(LOCSIZ + b);
  466.       z->len = LG(LOCLEN + b);
  467.       n = z->nam = SH(LOCNAM + b);
  468.       z->cext = z->ext = SH(LOCEXT + b);
  469.  
  470.       z->com = 0;
  471.       z->dsk = 0;
  472.       z->att = 0;
  473.       z->atx = dosify ? a & 0xff : a;     /* Attributes from filetime() */
  474.       z->mark = 0;
  475.       z->trash = 0;
  476.  
  477.       s = fix > 1 ? 0L : z->siz; /* discard compressed size with -FF */
  478.  
  479.       /* Initialize all fields pointing to malloced data to NULL */
  480.       z->zname = z->name = z->iname = z->extra = z->cextra = z->comment = NULL;
  481.  
  482.       /* Link into list */
  483.       *x = z;
  484.       z->nxt = NULL;
  485.       x = &z->nxt;
  486.  
  487.       /* Read file name and extra field and skip data */
  488.       if (n == 0)
  489.       {
  490.         sprintf(errbuf, "%ld", (ulg)zcount + 1);
  491.         zipwarn("zero-length name for entry #", errbuf);
  492. #ifndef DEBUG
  493.         return ZE_FORM;
  494. #endif
  495.       }
  496.       if ((z->iname = malloc(n+1)) ==  NULL ||
  497.           (z->ext && (z->extra = malloc(z->ext)) == NULL))
  498.         return ZE_MEM;
  499.       if (fread(z->iname, n, 1, f) != 1 ||
  500.           (z->ext && fread(z->extra, z->ext, 1, f) != 1) ||
  501.           (s && fseek(f, (long)s, SEEK_CUR)))
  502.         return ferror(f) ? ZE_READ : ZE_EOF;
  503.       /* If there is an extended local header, s is either 0 or
  504.        * the correct compressed size.
  505.        */
  506.       z->iname[n] = 0;                  /* terminate name */
  507.       z->zname = in2ex(z->iname);       /* convert to external name */
  508.       if (z->zname == NULL)
  509.         return ZE_MEM;
  510.       z->name = z->zname;
  511.       z->cextra = z->extra;
  512.       if (noisy) fprintf(mesg, "zip: reading %s\n", z->zname);
  513.  
  514.       /* Save offset, update for next header */
  515.       z->off = p;
  516.       p += 4 + LOCHEAD + n + z->ext + s;
  517.       zcount++;
  518.  
  519.       /* Skip extended local header if there is one */
  520.       if ((flg & 8) != 0) {
  521.         /* Skip the compressed data if compressed size is unknown.
  522.          * For safety, we should use the central directory.
  523.          */
  524.         if (s == 0) {
  525.           for (;;) {
  526.             while ((m = getc(f)) != EOF && m != 0x50) ;  /* 0x50 == 'P' */
  527.             b[0] = (char) m;
  528.             if (fread(b+1, 15, 1, f) != 1 || LG(b) == EXTLOCSIG)
  529.               break;
  530.             if (fseek(f, -15L, SEEK_CUR))
  531.               return ferror(f) ? ZE_READ : ZE_EOF;
  532.           }
  533.           s = LG(4 + EXTSIZ + b);
  534.           p += s;
  535.           if ((ulg) ftell(f) != p+16L) {
  536.             zipwarn("bad extended local header for ", z->zname);
  537.             return ZE_FORM;
  538.           }
  539.         } else {
  540.           /* compressed size non-zero, assume that it is valid: */
  541.           Assert(p == ftell(f), "bad compressed size with extended header");
  542.  
  543.           if (fseek(f, p, SEEK_SET) || fread(b, 16, 1, f) != 1)
  544.             return ferror(f) ? ZE_READ : ZE_EOF;
  545.           if (LG(b) != EXTLOCSIG) {
  546.             zipwarn("extended local header not found for ", z->zname);
  547.             return ZE_FORM;
  548.           }
  549.         }
  550.         /* overwrite the unknown values of the local header: */
  551.  
  552.         /* already in host format */
  553.         z->crc = LG(4 + EXTCRC + b);
  554.         z->siz = s;
  555.         z->len = LG(4 + EXTLEN + b);
  556.  
  557.         p += 16L;
  558.       }
  559.       else if (fix > 1) {
  560.         /* Don't trust the compressed size */
  561.         for (;;) {
  562.           while ((m = getc(f)) != EOF && m != 0x50) p++; /* 0x50 == 'P' */
  563.           b[0] = (char) m;
  564.           if (fread(b+1, 3, 1, f) != 1 || (s = LG(b)) == LOCSIG || s == CENSIG)
  565.             break;
  566.           if (fseek(f, -3L, SEEK_CUR))
  567.             return ferror(f) ? ZE_READ : ZE_EOF;
  568.           p++;
  569.         }
  570.         s = p - (z->off + 4 + LOCHEAD + n + z->ext);
  571.         if (s != z->siz) {
  572.           fprintf(mesg, " compressed size %ld, actual size %ld for %s\n",
  573.                   z->siz, s, z->zname);
  574.           z->siz = s;
  575.         }
  576.         /* next LOCSIG already read at this point, don't read it again: */
  577.         continue;
  578.       }
  579.  
  580.       /* Read next signature */
  581.       if (fread(b, 4, 1, f) != 1)
  582.           break;
  583.     }
  584.  
  585.     s = p;                              /* save start of central */
  586.  
  587.     if (LG(b) != CENSIG && noisy) {
  588.       fprintf(mesg, "zip warning: %s %s truncated.\n", zipfile,
  589.               fix > 1 ? "has been" : "would be");
  590.  
  591.       if (fix == 1) {
  592.         fprintf(mesg,
  593.    "Retry with option -qF to truncate, with -FF to attempt full recovery\n");
  594.         ZIPERR(ZE_FORM, NULL);
  595.       }
  596.     }
  597.  
  598.     cenbeg = s;
  599.     zcomlen = 0;
  600.  
  601.     if (zipbeg && noisy)
  602.       fprintf(mesg, "%s: adjusting offsets for a preamble of %lu bytes\n",
  603.               zipfile, zipbeg);
  604.  
  605.     /* Done with zip file for now */
  606.     fclose(f);
  607.  
  608.     /* If one or more files, sort by name */
  609.     if (zcount)
  610.     {
  611.       if ((x = zsort =
  612.           (struct zlist far **)malloc(zcount * sizeof(struct zlist far *))) ==
  613.           NULL)
  614.         return ZE_MEM;
  615.       for (z = zfiles; z != NULL; z = z->nxt)
  616.         *x++ = z;
  617.       qsort((char *)zsort, zcount, sizeof(struct zlist far *), zqcmp);
  618.     }
  619.   }
  620.   return ZE_OK;
  621. }
  622.  
  623. #endif /* !UTIL */
  624.  
  625. /*
  626.  * readzipfile starts searching for the End Signature at the end of the file
  627.  * The End Signature points to the Central Directory Signature which points
  628.  * to the Local Directory Signature
  629.  * XXX probably some more consistency checks are needed
  630.  */
  631. int readzipfile()
  632. /*
  633.    The name of the zip file is pointed to by the global "zipfile".  The
  634.    globals zfiles, zcount, zcomlen, zcomment, and zsort are filled in.
  635.    Return an error code in the ZE_ class.
  636. */
  637. {
  638.   char b[CENHEAD];      /* buffer for central headers */
  639.   FILE *f;              /* zip file */
  640.   ush flg;              /* general purpose bit flag */
  641.   int m;                /* mismatch flag */
  642.   extent n;             /* length of name */
  643.   struct zlist far * far *x;    /* pointer last entry's link */
  644.   struct zlist far *z;  /* current zip entry structure */
  645.   char *t;              /* temporary pointer */
  646.   char far *u;          /* temporary variable */
  647.   int found;
  648.   char *buf;            /* temp buffer for reading zipfile */
  649.   long deltaoff;
  650.  
  651. #ifndef UTIL
  652.   if (fix && !adjust)
  653.      return readzipfile2();
  654. #endif
  655.  
  656.   /* Initialize zip file info */
  657.   zipbeg = 0;
  658.   zfiles = NULL;                        /* Points to first header */
  659.   zcomlen = 0;                          /* zip file comment length */
  660.  
  661.   /* If zip file exists, read headers and check structure */
  662. #ifdef VMS
  663.   if (zipfile == NULL || !(*zipfile) || !strcmp(zipfile, "-"))
  664.     return ZE_OK;
  665.   {
  666.     int rtype;
  667.  
  668.     if ((VMSmunch(zipfile, GET_RTYPE, (char *)&rtype) == RMS$_NORMAL) &&
  669.         (rtype == FAT$C_VARIABLE)) {
  670.       fprintf(stderr,
  671.      "\n     Error:  zipfile is in variable-length record format.  Please\n\
  672.      run \"bilf b %s\" to convert the zipfile to fixed-length\n\
  673.      record format.\n\n", zipfile);
  674.       return ZE_FORM;
  675.     }
  676.   }
  677.   if ((f = fopen(zipfile, FOPR)) != NULL)
  678. #else /* !VMS */
  679.   if (zipfile != NULL && *zipfile && strcmp(zipfile, "-") &&
  680.       (f = fopen(zipfile, FOPR)) != NULL)
  681. #endif /* ?VMS */
  682.   {
  683.     buf = malloc(4096 + 4);
  684.     if (buf == NULL)
  685.       return ZE_MEM;
  686.  
  687. #ifdef HANDLE_AMIGA_SFX
  688.     amiga_sfx_offset = (fread(buf, 1, 4, f) == 4 && LG(buf) == 0xF3030000);
  689.     /* == 1 if this file is an Amiga executable (presumably UnZipSFX) */
  690. #endif
  691.     found = 0;
  692.     t = &buf[4096];
  693.     t[1] = '\0';
  694.     t[2] = '\0';
  695.     t[3] = '\0';
  696.     if (fseek(f, -4096L, SEEK_END) == 0) {
  697.       zipbeg = (ulg) (ftell(f) + 4096L);
  698.       while (!found && zipbeg >= 4096) {
  699.         zipbeg -= 4096L;
  700.         buf[4096] = t[1];
  701.         buf[4097] = t[2];
  702.         buf[4098] = t[3];
  703. /*
  704.  * XXX error check ??
  705.  */
  706.         fread(buf, 1, 4096, f);
  707.         fseek(f, -8192L, SEEK_CUR);
  708.         t = &buf[4095];
  709. /*
  710.  * XXX far pointer arithmetic in DOS
  711.  */
  712.         while (t >= buf) {
  713.           /* Check for ENDSIG ("PK\5\6" in ASCII) */
  714.           if (LG(t) == ENDSIG) {
  715.             found = 1;
  716. /*
  717.  * XXX error check ??
  718.  * XXX far pointer arithmetic in DOS
  719.  */
  720.             zipbeg += (ulg) (t - buf);
  721.             fseek(f, (long) zipbeg + 4L, SEEK_SET);
  722.             break;
  723.           }
  724.           --t;
  725.         }
  726.       }
  727.     }
  728.     else
  729.        zipbeg = 4096L;
  730. /*
  731.  * XXX warn: garbage at the end of the file ignored
  732.  */
  733.     if (!found && zipbeg > 0) {
  734.       size_t s;
  735.  
  736.       rewind(f);
  737.       s = fread(buf, 1, (size_t) zipbeg, f);
  738.       buf[s] = t[1];
  739.       buf[s + 1] = t[2];
  740.       buf[s + 2] = t[3];
  741.       t = &buf[s - 1];
  742. /*
  743.  * XXX far pointer comparison in DOS
  744.  */
  745.       while (t >= buf) {
  746.         /* Check for ENDSIG ("PK\5\6" in ASCII) */
  747.         if (LG(t) == ENDSIG) {
  748.           found = 1;
  749. /*
  750.  * XXX far pointer arithmetic in DOS
  751.  */
  752.           zipbeg = (ulg) (t - buf);
  753.           fseek(f, (long) zipbeg + 4L, SEEK_SET);
  754.           break;
  755.         }
  756.         --t;
  757.       }
  758.     }
  759.     free(buf);
  760.     if (!found) {
  761.       zipwarn("missing end signature--probably not a zip file (did you", "");
  762.       zipwarn("remember to use binary mode when you transferred it?)", "");
  763.       return ZE_FORM;
  764.     }
  765.     /* Read end header */
  766.     if (fread(b, ENDHEAD, 1, f) != 1)
  767.       return ferror(f) ? ZE_READ : ZE_EOF;
  768.     if (SH(ENDDSK + b) || SH(ENDBEG + b) ||
  769.         SH(ENDSUB + b) != SH(ENDTOT + b))
  770.       zipwarn("multiple disk information ignored", "");
  771.     zcomlen = SH(ENDCOM + b);
  772.     if (zcomlen)
  773.     {
  774.       if ((zcomment = malloc(zcomlen)) == NULL)
  775.         return ZE_MEM;
  776.       if (fread(zcomment, zcomlen, 1, f) != 1)
  777.       {
  778.         free((zvoid *)zcomment);
  779.         zcomment = NULL;
  780.         return ferror(f) ? ZE_READ : ZE_EOF;
  781.       }
  782. #ifdef EBCDIC
  783.       if (zcomment)
  784.          memtoebc(zcomment, zcomment, zcomlen);
  785. #endif /* EBCDIC */
  786.     }
  787. /*
  788.  * XXX assumes central header immediately precedes end header
  789.  */
  790.     cenbeg = zipbeg - LG(ENDSIZ + b);
  791.     deltaoff = adjust ? cenbeg - LG(b + ENDOFF) : 0L;
  792.     if (fseek(f, cenbeg, SEEK_SET) != 0) {
  793.         perror("fseek");
  794.         return ZE_FORM; /* XXX */
  795.     }
  796.  
  797.     x = &zfiles;                        /* first link */
  798.     zcount = 0;                         /* number of files */
  799.  
  800.     if (fread(b, 4, 1, f) != 1)
  801.       return ferror(f) ? ZE_READ : ZE_EOF;
  802.  
  803.     while (LG(b) == CENSIG) {
  804.       /* Read central header. The portion of the central header that should
  805.          be in common with local header is read raw, for later comparison.
  806.          (this requires that the offset of ext in the zlist structure
  807.          be greater than or equal to LOCHEAD) */
  808.       if (fread(b, CENHEAD, 1, f) != 1)
  809.         return ferror(f) ? ZE_READ : ZE_EOF;
  810.       if ((z = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL)
  811.         return ZE_MEM;
  812.       z->vem = SH(CENVEM + b);
  813.       for (u = (char far *)(&(z->ver)), n = 0; n < (CENNAM-CENVER); n++)
  814.         u[n] = b[CENVER + n];
  815.       z->nam = SH(CENNAM + b);          /* used before comparing cen vs. loc */
  816.       z->cext = SH(CENEXT + b);         /* may be different from z->ext */
  817.       z->com = SH(CENCOM + b);
  818.       z->dsk = SH(CENDSK + b);
  819.       z->att = SH(CENATT + b);
  820.       z->atx = LG(CENATX + b);
  821.       z->off = LG(CENOFF + b) + deltaoff;
  822.       z->dosflag = (z->vem & 0xff00) == 0;
  823.  
  824.       /* Initialize all fields pointing to malloced data to NULL */
  825.       z->zname = z->name = z->iname = z->extra = z->cextra = z->comment = NULL;
  826.  
  827.       /* Link into list */
  828.       *x = z;
  829.       z->nxt = NULL;
  830.       x = &z->nxt;
  831.  
  832.       /* Read file name, extra field and comment field */
  833.       if (z->nam == 0)
  834.       {
  835.         sprintf(errbuf, "%lu", (ulg)zcount + 1);
  836.         zipwarn("zero-length name for entry #", errbuf);
  837. #ifndef DEBUG
  838.         farfree((zvoid far *)z);
  839.         return ZE_FORM;
  840. #endif
  841.       }
  842.       if ((z->iname = malloc(z->nam+1)) ==  NULL ||
  843.           (z->cext && (z->cextra = malloc(z->cext)) == NULL) ||
  844.           (z->com && (z->comment = malloc(z->com)) == NULL))
  845.         return ZE_MEM;
  846.       if (fread(z->iname, z->nam, 1, f) != 1 ||
  847.           (z->cext && fread(z->cextra, z->cext, 1, f) != 1) ||
  848.           (z->com && fread(z->comment, z->com, 1, f) != 1))
  849.         return ferror(f) ? ZE_READ : ZE_EOF;
  850.       z->iname[z->nam] = 0;                  /* terminate name */
  851. #ifdef EBCDIC
  852.       if (z->com)
  853.          memtoebc(z->comment, z->comment, z->com);
  854. #endif /* EBCDIC */
  855.       /* Update zipbeg offset, prepare for next header */
  856.       if (z->off < zipbeg)
  857.          zipbeg = z->off;
  858.       zcount++;
  859.       /* Read next signature */
  860.       if (fread(b, 4, 1, f) != 1)
  861.           return ferror(f) ? ZE_READ : ZE_EOF;
  862.     }
  863.  
  864.     /* Point to start of header list and read local headers */
  865.     z = zfiles;
  866.     while (z != NULL) {
  867.       /* Read next signature */
  868.       if (fseek(f, z->off, SEEK_SET) != 0 || fread(b, 4, 1, f) != 1)
  869.         return ferror(f) ? ZE_READ : ZE_EOF;
  870.       if (LG(b) == LOCSIG) {
  871.         if (fread(b, LOCHEAD, 1, f) != 1)
  872.             return ferror(f) ? ZE_READ : ZE_EOF;
  873.         z->lflg = SH(LOCFLG + b);
  874.         n = SH(LOCNAM + b);
  875.         z->ext = SH(LOCEXT + b);
  876.  
  877.         /* Compare name and extra fields */
  878.         if (n != z->nam)
  879.         {
  880. #ifdef EBCDIC
  881.           strtoebc(z->iname, z->iname);
  882. #endif
  883.           zipwarn("name lengths in local and central differ for ", z->iname);
  884.           return ZE_FORM;
  885.         }
  886.         if ((t = malloc(z->nam)) == NULL)
  887.           return ZE_MEM;
  888.         if (fread(t, z->nam, 1, f) != 1)
  889.         {
  890.           free((zvoid *)t);
  891.           return ferror(f) ? ZE_READ : ZE_EOF;
  892.         }
  893.         if (memcmp(t, z->iname, z->nam))
  894.         {
  895.           free((zvoid *)t);
  896. #ifdef EBCDIC
  897.           strtoebc(z->iname, z->iname);
  898. #endif
  899.           zipwarn("names in local and central differ for ", z->iname);
  900.           return ZE_FORM;
  901.         }
  902.         free((zvoid *)t);
  903.         if (z->ext)
  904.         {
  905.           if ((z->extra = malloc(z->ext)) == NULL)
  906.             return ZE_MEM;
  907.           if (fread(z->extra, z->ext, 1, f) != 1)
  908.           {
  909.             free((zvoid *)(z->extra));
  910.             return ferror(f) ? ZE_READ : ZE_EOF;
  911.           }
  912.           if (z->ext == z->cext && memcmp(z->extra, z->cextra, z->ext) == 0)
  913.           {
  914.             free((zvoid *)(z->extra));
  915.             z->extra = z->cextra;
  916.           }
  917.         }
  918.  
  919.         /* Check extended local header if there is one */
  920.         if ((z->lflg & 8) != 0)
  921.         {
  922.           char buf2[16];
  923.           ulg s;                        /* size of compressed data */
  924.  
  925.           s = LG(LOCSIZ + b);
  926.           if (s == 0)
  927.             s = LG((CENSIZ-CENVER) + (char far *)(&(z->ver)));
  928.           if (fseek(f, (z->off + (4+LOCHEAD) + z->nam + z->ext + s), SEEK_SET)
  929.               || (fread(buf2, 16, 1, f) != 1))
  930.             return ferror(f) ? ZE_READ : ZE_EOF;
  931.           if (LG(buf2) != EXTLOCSIG)
  932.           {
  933. #ifdef EBCDIC
  934.             strtoebc(z->iname, z->iname);
  935. #endif
  936.             zipwarn("extended local header not found for ", z->iname);
  937.             return ZE_FORM;
  938.           }
  939.           /* overwrite the unknown values of the local header: */
  940.           for (n = 0; n < 12; n++)
  941.             b[LOCCRC+n] = buf2[4+n];
  942.         }
  943.  
  944.         /* Compare local header with that part of central header (except
  945.            for the reserved bits in the general purpose flags and except
  946.            for the already checked entry name length */
  947.         u = (char far *)(&(z->ver));
  948.         flg = SH((CENFLG-CENVER) + u);          /* Save central flags word */
  949.         u[CENFLG-CENVER+1] &= 0x1f;             /* Mask reserved flag bits */
  950.         b[LOCFLG+1] &= 0x1f;
  951.         for (m = 0, n = 0; n < LOCNAM; n++)
  952.           if (b[n] != u[n])
  953.           {
  954.             if (!m)
  955.             {
  956.               zipwarn("local and central headers differ for ", z->zname);
  957.               m = 1;
  958.             }
  959.             if (noisy)
  960.             {
  961.               sprintf(errbuf, " offset %u--local = %02x, central = %02x",
  962.                       (unsigned)n, (uch)b[n], (uch)u[n]);
  963.               zipwarn(errbuf, "");
  964.             }
  965.           }
  966.         if (m && !adjust)
  967.           return ZE_FORM;
  968.  
  969.         /* Complete the setup of the zlist entry by translating the remaining
  970.          * central header fields in memory, starting with the fields with
  971.          * highest offset. This order of the conversion commands takes into
  972.          * account potential buffer overlaps caused by structure padding.
  973.          */
  974.         z->len = LG((CENLEN-CENVER) + u);
  975.         z->siz = LG((CENSIZ-CENVER) + u);
  976.         z->crc = LG((CENCRC-CENVER) + u);
  977.         z->tim = LG((CENTIM-CENVER) + u);   /* time and date into one long */
  978.         z->how = SH((CENHOW-CENVER) + u);
  979.         z->flg = flg;                       /* may be different from z->lflg */
  980.         z->ver = SH((CENVER-CENVER) + u);
  981.  
  982.         /* Clear actions */
  983.         z->mark = 0;
  984.         z->trash = 0;
  985. #ifdef UTIL
  986. /* We only need z->iname in the utils */
  987.         z->name = z->iname;
  988. #ifdef EBCDIC
  989. /* z->zname is used for printing and must be coded in native charset */
  990.         if ((z->zname = malloc(z->nam+1)) ==  NULL)
  991.           return ZE_MEM;
  992.         strtoebc(z->zname, z->iname);
  993. #else
  994.         z->zname = z->iname;
  995. #endif
  996. #else /* !UTIL */
  997.         z->zname = in2ex(z->iname);       /* convert to external name */
  998.         if (z->zname == NULL)
  999.           return ZE_MEM;
  1000.         z->name = z->zname;
  1001. #endif /* ?UTIL */
  1002.       }
  1003.       else {
  1004. #ifdef EBCDIC
  1005.         strtoebc(z->iname, z->iname);
  1006. #endif
  1007.         zipwarn("local header not found for ", z->iname);
  1008.         return ZE_FORM;
  1009.       }
  1010. #ifndef UTIL
  1011.       if (verbose)
  1012.         zipoddities(z);
  1013. #endif
  1014.       z = z->nxt;
  1015.     }
  1016.  
  1017.     if (zipbeg && noisy)
  1018.       fprintf(mesg, "%s: %s a preamble of %lu bytes\n",
  1019.               zipfile, adjust ? "adjusting offsets for" : "found", zipbeg);
  1020.  
  1021. #ifdef HANDLE_AMIGA_SFX
  1022.     if (zipbeg < 12 || (zipbeg & 3) != 0 /* must be longword aligned */)
  1023.       amiga_sfx_offset = 0;
  1024.     else if (amiga_sfx_offset) {
  1025.       char buf2[16];
  1026.       if (!fseek(f, zipbeg - 12, SEEK_SET) && fread(buf2, 12, 1, f) == 1) {
  1027.         if (LG(buf2 + 4) == 0xF1030000 /* 1009 in Motorola byte order */)
  1028.           /* could also check if LG(buf2) == 0xF2030000... no for now */
  1029.           amiga_sfx_offset = zipbeg - 4;
  1030.         else
  1031.           amiga_sfx_offset = 0L;
  1032.       }
  1033.     }
  1034. #endif /* HANDLE_AMIGA_SFX */
  1035.     /* Done with zip file for now */
  1036.     fclose(f);
  1037.  
  1038.     /* If one or more files, sort by name */
  1039.     if (zcount)
  1040.     {
  1041.       if ((x = zsort =
  1042.           (struct zlist far **)malloc(zcount * sizeof(struct zlist far *))) ==
  1043.           NULL)
  1044.         return ZE_MEM;
  1045.       for (z = zfiles; z != NULL; z = z->nxt)
  1046.         *x++ = z;
  1047.       qsort((char *)zsort, zcount, sizeof(struct zlist far *), zqcmp);
  1048.     }
  1049.   }
  1050.   return ZE_OK;
  1051. }
  1052.  
  1053.  
  1054. int putlocal(z, f)
  1055. struct zlist far *z;    /* zip entry to write local header for */
  1056. FILE *f;                /* file to write to */
  1057. /* Write a local header described by *z to file *f.  Return an error code
  1058.    in the ZE_ class. */
  1059. {
  1060.   PUTLG(LOCSIG, f);
  1061.   PUTSH(z->ver, f);
  1062.   PUTSH(z->lflg, f);
  1063.   PUTSH(z->how, f);
  1064.   PUTLG(z->tim, f);
  1065.   PUTLG(z->crc, f);
  1066.   PUTLG(z->siz, f);
  1067.   PUTLG(z->len, f);
  1068.   PUTSH(z->nam, f);
  1069.   PUTSH(z->ext, f);
  1070.   if (fwrite(z->iname, 1, z->nam, f) != z->nam ||
  1071.       (z->ext && fwrite(z->extra, 1, z->ext, f) != z->ext))
  1072.     return ZE_TEMP;
  1073.   return ZE_OK;
  1074. }
  1075.  
  1076. int putextended(z, f)
  1077. struct zlist far *z;    /* zip entry to write local header for */
  1078. FILE *f;                /* file to write to */
  1079. /* Write an extended local header described by *z to file *f.
  1080.  * Return an error code in the ZE_ class. */
  1081. {
  1082.   PUTLG(EXTLOCSIG, f);
  1083.   PUTLG(z->crc, f);
  1084.   PUTLG(z->siz, f);
  1085.   PUTLG(z->len, f);
  1086.   return ZE_OK;
  1087. }
  1088.  
  1089. int putcentral(z, f)
  1090. struct zlist far *z;    /* zip entry to write central header for */
  1091. FILE *f;                /* file to write to */
  1092. /* Write a central header described by *z to file *f.  Return an error code
  1093.    in the ZE_ class. */
  1094. {
  1095.   PUTLG(CENSIG, f);
  1096.   PUTSH(z->vem, f);
  1097.   PUTSH(z->ver, f);
  1098.   PUTSH(z->flg, f);
  1099.   PUTSH(z->how, f);
  1100.   PUTLG(z->tim, f);
  1101.   PUTLG(z->crc, f);
  1102.   PUTLG(z->siz, f);
  1103.   PUTLG(z->len, f);
  1104.   PUTSH(z->nam, f);
  1105.   PUTSH(z->cext, f);
  1106.   PUTSH(z->com, f);
  1107.   PUTSH(z->dsk, f);
  1108.   PUTSH(z->att, f);
  1109.   PUTLG(z->atx, f);
  1110.   PUTLG(z->off, f);
  1111. #ifdef EBCDIC
  1112.   if (z->com)
  1113.     memtoasc(z->comment, z->comment, z->com);
  1114. #endif
  1115.   if (fwrite(z->iname, 1, z->nam, f) != z->nam ||
  1116.       (z->cext && fwrite(z->cextra, 1, z->cext, f) != z->cext) ||
  1117.       (z->com && fwrite(z->comment, 1, z->com, f) != z->com))
  1118.     return ZE_TEMP;
  1119.   return ZE_OK;
  1120. }
  1121.  
  1122.  
  1123. int putend(n, s, c, m, z, f)
  1124. int n;                  /* number of entries in central directory */
  1125. ulg s;                  /* size of central directory */
  1126. ulg c;                  /* offset of central directory */
  1127. extent m;               /* length of zip file comment (0 if none) */
  1128. char *z;                /* zip file comment if m != 0 */
  1129. FILE *f;                /* file to write to */
  1130. /* Write the end of central directory data to file *f.  Return an error code
  1131.    in the ZE_ class. */
  1132. {
  1133.   PUTLG(ENDSIG, f);
  1134.   PUTSH(0, f);
  1135.   PUTSH(0, f);
  1136.   PUTSH(n, f);
  1137.   PUTSH(n, f);
  1138.   PUTLG(s, f);
  1139.   PUTLG(c, f);
  1140.   PUTSH(m, f);
  1141. /* Write the comment, if any */
  1142. #ifdef EBCDIC
  1143.   memtoasc(z, z, m);
  1144. #endif
  1145.   if (m && fwrite(z, 1, m, f) != m)
  1146.     return ZE_TEMP;
  1147.  
  1148. #ifdef HANDLE_AMIGA_SFX
  1149.   if (amiga_sfx_offset && zipbeg /* -J zeroes this */) {
  1150.     s = ftell(f);
  1151.     while (s & 3) s++, putc(0, f);   /* final marker must be longword aligned */
  1152.     PUTLG(0xF2030000 /* 1010 in Motorola byte order */, f);
  1153.     c = (s - amiga_sfx_offset - 4) / 4;  /* size of archive part in longwords */
  1154.     if (fseek(f, amiga_sfx_offset, SEEK_SET) != 0)
  1155.       return ZE_TEMP;
  1156.     c = ((c >> 24) & 0xFF) | ((c >> 8) & 0xFF00)
  1157.          | ((c & 0xFF00) << 8) | ((c & 0xFF) << 24);     /* invert byte order */
  1158.     PUTLG(c, f);
  1159.     fseek(f, 0, SEEK_END);                                    /* just in case */
  1160.   }
  1161. #endif
  1162.   return ZE_OK;
  1163. }
  1164.  
  1165.  
  1166. /* Note: a zip "entry" includes a local header (which includes the file
  1167.    name), an encryption header if encrypting, the compressed data
  1168.    and possibly an extended local header. */
  1169.  
  1170. int zipcopy(z, x, y)
  1171. struct zlist far *z;    /* zip entry to copy */
  1172. FILE *x, *y;            /* source and destination files */
  1173. /* Copy the zip entry described by *z from file *x to file *y.  Return an
  1174.    error code in the ZE_ class.  Also update tempzn by the number of bytes
  1175.    copied. */
  1176. {
  1177.   ulg n;                /* holds local header offset */
  1178.  
  1179.   Trace((stderr, "zipcopy %s\n", z->zname));
  1180.   n = (ulg)(4 + LOCHEAD) + (ulg)z->nam + (ulg)z->ext;
  1181.  
  1182.   if (fix > 1) {
  1183.     if (fseek(x, z->off + n, SEEK_SET)) /* seek to compressed data */
  1184.       return ferror(x) ? ZE_READ : ZE_EOF;
  1185.  
  1186.     if (fix > 2) {
  1187.       /* Update length of entry's name, it may have been changed.  This is
  1188.          needed to support the ZipNote ability to rename archive entries. */
  1189.       z->nam = strlen(z->iname);
  1190.       n = (ulg)(4 + LOCHEAD) + (ulg)z->nam + (ulg)z->ext;
  1191.     }
  1192.  
  1193.     /* do not trust the old compressed size */
  1194.     if (putlocal(z, y) != ZE_OK)
  1195.       return ZE_TEMP;
  1196.  
  1197.     z->off = tempzn;
  1198.     tempzn += n;
  1199.     n = z->siz;
  1200.   } else {
  1201.     if (fseek(x, z->off, SEEK_SET))     /* seek to local header */
  1202.       return ferror(x) ? ZE_READ : ZE_EOF;
  1203.  
  1204.     z->off = tempzn;
  1205.     n += z->siz;
  1206.   }
  1207.   /* copy the compressed data and the extended local header if there is one */
  1208.   if (z->lflg & 8) n += 16;
  1209.   tempzn += n;
  1210.   return fcopy(x, y, n);
  1211. }
  1212.  
  1213.  
  1214. #ifndef UTIL
  1215.  
  1216. #ifdef USE_EF_UT_TIME
  1217.  
  1218. local int ef_scan_ut_time(ef_buf, ef_len, ef_is_cent, z_utim)
  1219. char *ef_buf;                   /* buffer containing extra field */
  1220. extent ef_len;                  /* total length of extra field */
  1221. int ef_is_cent;                 /* flag indicating "is central extra field" */
  1222. iztimes *z_utim;                /* return storage: atime, mtime, ctime */
  1223. /* This function scans the extra field for EF_TIME or EF_IZUNIX blocks
  1224.  * containing Unix style time_t (GMT) values for the entry's access, creation
  1225.  * and modification time.
  1226.  * If a valid block is found, all time stamps are copied to the iztimes
  1227.  * structure.
  1228.  * The presence of an EF_TIME or EF_IZUNIX2 block results in ignoring
  1229.  * all data from probably present obsolete EF_IZUNIX blocks.
  1230.  * If multiple blocks of the same type are found, only the information from
  1231.  * the last block is used.
  1232.  * The return value is the EF_TIME Flags field (simulated in case of an
  1233.  * EF_IZUNIX block) or 0 in case of failure.
  1234.  */
  1235. {
  1236.   int flags = 0;
  1237.   unsigned eb_id;
  1238.   extent eb_len;
  1239.   int have_new_type_eb = FALSE;
  1240.  
  1241.   if (ef_len == 0 || ef_buf == NULL)
  1242.     return 0;
  1243.  
  1244.   Trace((stderr,"\nef_scan_ut_time: scanning extra field of length %u\n",
  1245.          ef_len));
  1246.   while (ef_len >= EB_HEADSIZE) {
  1247.     eb_id = SH(EB_ID + ef_buf);
  1248.     eb_len = SH(EB_LEN + ef_buf);
  1249.  
  1250.     if (eb_len > (ef_len - EB_HEADSIZE)) {
  1251.       /* Discovered some extra field inconsistency! */
  1252.       Trace((stderr,"ef_scan_ut_time: block length %u > rest ef_size %u\n",
  1253.              eb_len, ef_len - EB_HEADSIZE));
  1254.       break;
  1255.     }
  1256.  
  1257.     switch (eb_id) {
  1258.       case EF_TIME:
  1259.         flags &= ~0x00ff;       /* ignore previous IZUNIX or EF_TIME fields */
  1260.         have_new_type_eb = TRUE;
  1261.         if ( eb_len >= EB_UT_MINLEN && z_utim != NULL) {
  1262.            unsigned eb_idx = EB_UT_TIME1;
  1263.            Trace((stderr,"ef_scan_ut_time: Found TIME extra field\n"));
  1264.            flags |= (ef_buf[EB_HEADSIZE+EB_UT_FLAGS] & 0x00ff);
  1265.            if ((flags & EB_UT_FL_MTIME)) {
  1266.               if ((eb_idx+4) <= eb_len) {
  1267.                  z_utim->mtime = LG((EB_HEADSIZE+eb_idx) + ef_buf);
  1268.                  eb_idx += 4;
  1269.                  Trace((stderr,"  Unix EF modtime = %ld\n", z_utim->mtime));
  1270.               } else {
  1271.                  flags &= ~EB_UT_FL_MTIME;
  1272.                  Trace((stderr,"  Unix EF truncated, no modtime\n"));
  1273.               }
  1274.            }
  1275.            if (ef_is_cent) {
  1276.               break;            /* central version of TIME field ends here */
  1277.            }
  1278.            if (flags & EB_UT_FL_ATIME) {
  1279.               if ((eb_idx+4) <= eb_len) {
  1280.                  z_utim->atime = LG((EB_HEADSIZE+eb_idx) + ef_buf);
  1281.                  eb_idx += 4;
  1282.                  Trace((stderr,"  Unix EF acctime = %ld\n", z_utim->atime));
  1283.               } else {
  1284.                  flags &= ~EB_UT_FL_ATIME;
  1285.               }
  1286.            }
  1287.            if (flags & EB_UT_FL_CTIME) {
  1288.               if ((eb_idx+4) <= eb_len) {
  1289.                  z_utim->ctime = LG((EB_HEADSIZE+eb_idx) + ef_buf);
  1290.                  eb_idx += 4;
  1291.                  Trace((stderr,"  Unix EF cretime = %ld\n", z_utim->ctime));
  1292.               } else {
  1293.                  flags &= ~EB_UT_FL_CTIME;
  1294.               }
  1295.            }
  1296.         }
  1297.         break;
  1298.  
  1299.       case EF_IZUNIX2:
  1300.         if (!have_new_type_eb) {
  1301.            flags &= ~0x00ff;    /* ignore any previous IZUNIX field */
  1302.            have_new_type_eb = TRUE;
  1303.         }
  1304.         break;
  1305.  
  1306.       case EF_IZUNIX:
  1307.         if (eb_len >= EB_UX_MINLEN) {
  1308.            Trace((stderr,"ef_scan_ut_time: Found IZUNIX extra field\n"));
  1309.            if (have_new_type_eb) {
  1310.               break;            /* Ignore IZUNIX extra field block ! */
  1311.            }
  1312.            z_utim->atime = LG((EB_HEADSIZE+EB_UX_ATIME) + ef_buf);
  1313.            z_utim->mtime = LG((EB_HEADSIZE+EB_UX_MTIME) + ef_buf);
  1314.            Trace((stderr,"  Unix EF access time = %ld\n",z_utim->atime));
  1315.            Trace((stderr,"  Unix EF modif. time = %ld\n",z_utim->mtime));
  1316.            flags |= (EB_UT_FL_MTIME | EB_UT_FL_ATIME);  /* signal success */
  1317.         }
  1318.         break;
  1319.  
  1320.       default:
  1321.         break;
  1322.     }
  1323.     /* Skip this extra field block */
  1324.     ef_buf += (eb_len + EB_HEADSIZE);
  1325.     ef_len -= (eb_len + EB_HEADSIZE);
  1326.   }
  1327.  
  1328.   return flags;
  1329. }
  1330.  
  1331. int get_ef_ut_ztime(z, z_utim)
  1332. struct zlist far *z;
  1333. iztimes *z_utim;
  1334. {
  1335.   int r;
  1336.  
  1337.   /* First, scan local extra field. */
  1338.   r = ef_scan_ut_time(z->extra, z->ext, FALSE, z_utim);
  1339.  
  1340.   /* If this was not successful, try central extra field, but only if
  1341.      it is really different. */
  1342.   if (!r && z->cext > 0 && z->cextra != z->extra)
  1343.     r = ef_scan_ut_time(z->cextra, z->cext, TRUE, z_utim);
  1344.  
  1345.   return r;
  1346. }
  1347.  
  1348. #endif /* USE_EF_UT_TIME */
  1349.  
  1350.  
  1351. local void cutpath(p)
  1352. char *p;                /* path string */
  1353. /* Cut the last path component off the name *p in place.
  1354.  * This should work on both internal and external names.
  1355.  */
  1356. {
  1357.   char *r;              /* pointer to last path delimiter */
  1358.  
  1359. #ifdef VMS                      /* change [w.x.y]z to [w.x]y.DIR */
  1360.   if ((r = strrchr(p, ']')) != NULL)
  1361.   {
  1362.     *r = 0;
  1363.     if ((r = strrchr(p, '.')) != NULL)
  1364.     {
  1365.       *r = ']';
  1366.       strcat(r, ".DIR;1");     /* this assumes a little padding--see PAD */
  1367.     } else {
  1368.       *p = 0;
  1369.     }
  1370.   } else {
  1371.     if ((r = strrchr(p, '/')) != NULL)
  1372.       *r = 0;
  1373.     else
  1374.       *p = 0;
  1375.   }
  1376. #else /* !VMS */
  1377.   if ((r = strrchr(p, '/')) != NULL)
  1378.     *r = 0;
  1379.   else
  1380.     *p = 0;
  1381. #endif /* ?VMS */
  1382. }
  1383.  
  1384. int trash()
  1385. /* Delete the compressed files and the directories that contained the deleted
  1386.    files, if empty.  Return an error code in the ZE_ class.  Failure of
  1387.    destroy() or deletedir() is ignored. */
  1388. {
  1389.   extent i;             /* counter on deleted names */
  1390.   extent n;             /* number of directories to delete */
  1391.   struct zlist far **s; /* table of zip entries to handle, sorted */
  1392.   struct zlist far *z;  /* current zip entry */
  1393.  
  1394.   /* Delete marked names and count directories */
  1395.   n = 0;
  1396.   for (z = zfiles; z != NULL; z = z->nxt)
  1397.     if (z->mark == 1 || z->trash)
  1398.     {
  1399.       z->mark = 1;
  1400.       if (z->iname[z->nam - 1] != '/') { /* don't unlink directory */
  1401.         if (verbose)
  1402.           fprintf(mesg, "zip diagnostic: deleting file %s\n", z->name);
  1403.         if (destroy(z->name)) {
  1404.           zipwarn("error deleting ", z->name);
  1405.         }
  1406.         /* Try to delete all paths that lead up to marked names. This is
  1407.          * necessary only with the -D option.
  1408.          */
  1409.         if (!dirnames) {
  1410.           cutpath(z->name);  /* XXX wrong ??? */
  1411.           cutpath(z->iname);
  1412.           if (z->iname[0] != '\0') {
  1413.             strcat(z->iname, "/");
  1414.           }
  1415.           z->nam = strlen(z->iname);
  1416.           if (z->nam > 0) n++;
  1417.         }
  1418.       } else {
  1419.         n++;
  1420.       }
  1421.     }
  1422.  
  1423.   /* Construct the list of all marked directories. Some may be duplicated
  1424.    * if -D was used.
  1425.    */
  1426.   if (n)
  1427.   {
  1428.     if ((s = (struct zlist far **)malloc(n*sizeof(struct zlist far *))) ==
  1429.         NULL)
  1430.       return ZE_MEM;
  1431.     n = 0;
  1432.     for (z = zfiles; z != NULL; z = z->nxt) {
  1433.       if (z->mark && z->nam > 0 && z->iname[z->nam - 1] == '/'
  1434.           && (n == 0 || strcmp(z->name, s[n-1]->name) != 0)) {
  1435.         s[n++] = z;
  1436.       }
  1437.     }
  1438.     /* Sort the files in reverse order to get subdirectories first.
  1439.      * To avoid problems with strange naming conventions as in VMS,
  1440.      * we sort on the internal names, so x/y/z will always be removed
  1441.      * before x/y. On VMS, x/y/z > x/y but [x.y.z] < [x.y]
  1442.      */
  1443.     qsort((char *)s, n, sizeof(struct zlist far *), rqcmp);
  1444.  
  1445.     for (i = 0; i < n; i++) {
  1446.       char *p = s[i]->name;
  1447.       if (*p == '\0') continue;
  1448.       if (p[strlen(p) - 1] == '/') { /* keep VMS [x.y]z.dir;1 intact */
  1449.         p[strlen(p) - 1] = '\0';
  1450.       }
  1451.       if (i == 0 || strcmp(s[i]->name, s[i-1]->name) != 0) {
  1452.         if (verbose) {
  1453.           fprintf(mesg, "deleting directory %s (if empty)                \n",
  1454.                   s[i]->name);
  1455.         }
  1456.         deletedir(s[i]->name);
  1457.       }
  1458.     }
  1459.     free((zvoid *)s);
  1460.   }
  1461.   return ZE_OK;
  1462. }
  1463.  
  1464. #endif /* !UTIL */
  1465.