home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / ZIP19P1.ZIP / zipup.c < prev   
C/C++ Source or Header  |  1993-01-23  |  12KB  |  427 lines

  1. /*
  2.  
  3.  Copyright (C) 1990-1992 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  4.  Kai Uwe Rommel 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.  unmodified, that it is not sold for profit, and that this copyright notice
  8.  is retained.
  9.  
  10. */
  11.  
  12. /*
  13.  *  zipup.c by Mark Adler. Includes modifications by Jean-loup Gailly.
  14.  */
  15.  
  16. #define NOCPYRT         /* this is not a main module */
  17. #include <ctype.h>
  18. #include "zip.h"
  19. #include "revision.h"
  20. #ifdef OS2
  21. #  include "os2zip.h"
  22. #endif
  23.  
  24. /* Use the raw functions for MSDOS and Unix to save on buffer space.
  25.    They're not used for VMS since it doesn't work (raw is weird on VMS).
  26.    (This sort of stuff belongs in fileio.c, but oh well.) */
  27. #ifdef VMS
  28. #  define fhow "r"
  29. #  define fbad NULL
  30.    typedef void *ftype;
  31. #  define zopen(n,p)   (vms_native?vms_open(n)    :(ftype)fopen((n),(p)))
  32. #  define zread(f,b,n) (vms_native?vms_read(f,b,n):fread((b),1,(n),(FILE*)(f)))
  33. #  define zclose(f)    (vms_native?vms_close(f)   :fclose((FILE*)(f)))
  34. #  define zerr(f)      (vms_native?vms_error(f)   :ferror((FILE*)(f)))
  35. #  define zstdin stdin
  36.    ftype vms_open OF((char *));
  37.    int vms_read OF((ftype, char *, int));
  38.    int vms_close OF((ftype));
  39.    int vms_error OF((ftype));
  40. #else /* !VMS */
  41. #  if defined(MSDOS) && !defined(ATARI_ST)
  42. #    include <io.h>
  43. #    include <fcntl.h>
  44. #    define fhow (O_RDONLY|O_BINARY)
  45. #  else /* !MSDOS */
  46.      int open OF((char *, int));
  47.      int read OF((int, char *, int));
  48.      int close OF((int));
  49.      int lseek OF((int, long, int));
  50. #    define fhow 0
  51. #  endif /* ?MSDOS */
  52.    typedef int ftype;
  53. #  define fbad (-1)
  54. #  define zopen(n,p) open(n,p)
  55. #  define zread(f,b,n) read(f,b,n)
  56. #  define zclose(f) close(f)
  57. #  define zerr(f) (k==(extent)(-1L))
  58. #  define zstdin 0
  59. #endif /* ?VMS */
  60.  
  61.  
  62. /* Local data */
  63.  
  64. #ifndef UTIL
  65. local ulg crc;       /* crc on uncompressed file data */
  66. local ftype ifile;   /* file to compress */
  67. #endif
  68. ulg isize;           /* input file size. global only for debugging */    
  69.  
  70. /* Local functions */
  71. #if defined(PROTO) && !defined(UTIL)
  72.    local int suffixes(char *, char *);
  73. #endif
  74.  
  75.  
  76. /* Note: a zip "entry" includes a local header (which includes the file
  77.    name), an encryption header if encrypting, the compressed data
  78.    and possibly an extended local header. */
  79.  
  80. int zipcopy(z, x, y)
  81. struct zlist far *z;    /* zip entry to copy */
  82. FILE *x, *y;            /* source and destination files */
  83. /* Copy the zip entry described by *z from file *x to file *y.  Return an
  84.    error code in the ZE_ class.  Also update tempzn by the number of bytes
  85.    copied. */
  86. {
  87.   ulg n;                /* holds local header offset */
  88.  
  89.   if (fseek(x, z->off, SEEK_SET))
  90.     return ferror(x) ? ZE_READ : ZE_EOF;
  91.   z->off = tempzn;
  92.   n = 4 + LOCHEAD + (long)z->nam + (long)z->ext + z->siz;
  93.   /* copy the extended local header if there is one */
  94.   if (z->lflg & 8) n += 16;
  95.   tempzn += n;
  96.   return fcopy(x, y, n);
  97. }
  98.  
  99.  
  100. #ifndef UTIL
  101.  
  102. int percent(n, m)
  103. long n, m;               /* n is the original size, m is the new size */
  104. /* Return the percentage compression from n to m using only integer
  105.    operations */
  106. {
  107.   if (n > 0xffffffL)            /* If n >= 16M */
  108.   {                             /*  then divide n and m by 256 */
  109.     n += 0x80;  n >>= 8;
  110.     m += 0x80;  m >>= 8;
  111.   }
  112.   return n ? (int)(1 + (200 * (n - m)/n)) / 2 : 0;
  113. }
  114.  
  115. local int suffixes(a, s)
  116. char *a;                /* name to check suffix of */
  117. char *s;                /* list of suffixes separated by : or ; */
  118. /* Return true if a ends in any of the suffixes in the list s. */
  119. {
  120.   int m;                /* true if suffix matches so far */
  121.   char *p;              /* pointer into special */
  122.   char *q;              /* pointer into name a */
  123.  
  124.   m = 1;
  125. #ifdef VMS
  126.   if( (q = strrchr(a,';')) != NULL )    /* Cut out VMS file version */
  127.     --q;
  128.   else
  129.     q = a + strlen(a) - 1;
  130. #else
  131.   q = a + strlen(a) - 1;
  132. #endif
  133.   for (p = s + strlen(s) - 1; p >= s; p--)
  134.     if (*p == ':' || *p == ';')
  135.       if (m)
  136.         return 1;
  137.       else
  138.       {
  139.         m = 1;
  140. #ifdef VMS
  141.         if( (q = strrchr(a,';')) != NULL )      /* Cut out VMS file version */
  142.           --q;
  143.         else
  144.           q = a + strlen(a) - 1;
  145. #else
  146.         q = a + strlen(a) - 1;
  147. #endif
  148.       }
  149.     else
  150.     {
  151.       m = m && q >= a && case_map(*p) == case_map(*q);
  152.       q--;
  153.     }
  154.   return m;
  155. }
  156.  
  157. int zipup(z, y)
  158. struct zlist far *z;    /* zip entry to compress */
  159. FILE *y;                /* output file */
  160. /* Compress the file z->name into the zip entry described by *z and write
  161.    it to the file *y. Encrypt if requested.  Return an error code in the
  162.    ZE_ class.  Also, update tempzn by the number of bytes written. */
  163. {
  164.   ulg a = 0L;           /* attributes returned by filetime() */
  165.   char *b;              /* malloc'ed file buffer */
  166.   extent k = 0;         /* result of zread */
  167.   int l = 0;            /* true if this file is a symbolic link */
  168.   int m;                /* method for this entry */
  169.   ulg o, p;             /* offsets in zip file */
  170.   long q = -2L;         /* size returned by filetime */
  171.   int r;                /* temporary variable */
  172.   ulg s = 0L;           /* size of compressed data */
  173.  
  174.   if ((z->tim = filetime(z->name, &a, &q)) == 0 || q < -1L)
  175.     return ZE_OPEN;
  176.   /* q is set to -1 if the input file is a device */
  177.  
  178.   z->nam = strlen(z->zname);
  179.  
  180.   /* Select method based on the suffix and the global method */
  181.   m = special != NULL && suffixes(z->name, special) ? STORE : method;
  182.  
  183.   /* Open file to zip up unless it is stdin */
  184.   if (strcmp(z->name, "-") == 0)
  185.   {
  186.     ifile = (ftype)zstdin;
  187. #ifdef MSDOS
  188.     setmode(zstdin, O_BINARY);
  189. #endif
  190.   }
  191.   else
  192.   {
  193. #ifdef VMS
  194.    if (vms_native)
  195.      get_vms_attributes(z);
  196. #endif
  197. #ifdef OS2
  198.     GetEAs(z->name, &z->extra, &z->ext, &z->cextra, &z->cext);
  199.     /* store data in local header, and size only in central headers */
  200. #endif
  201.     l = issymlnk(a);
  202.     if (l)
  203.       ifile = fbad;
  204.     else if (z->name[z->nam - 1] == '/') { /* directory */
  205.       ifile = fbad;
  206.       m = STORE;
  207.     }
  208.     else if ((ifile = zopen(z->name, fhow)) == fbad)
  209.       return ZE_OPEN;
  210.   }
  211.  
  212.   if (l || q == 0)
  213.     m = STORE;
  214.   if (m == BEST)
  215.     m = DEFLATE;
  216.  
  217.   /* Do not create STORED files with extended local headers if the
  218.    * input size is not known, because such files could not be extracted.
  219.    * So if the zip file is not seekable and the input file is not
  220.    * on disk, obey the -0 option by forcing deflation with stored block.
  221.    * Note however that using "zip -0" as filter is not very useful...
  222.    * ??? to be done.
  223.    */
  224.  
  225.   /* Fill in header information and write local header to zip file.
  226.    * This header will later be re-written since compressed length and
  227.    * crc are not yet known.
  228.    */
  229.  
  230.   /* (Assume ext, cext, com, and zname already filled in.) */
  231. #ifdef OS2
  232.   z->vem = z->dosflag ? 20 :            /* Made under MSDOS by PKZIP 2.0 */
  233.   /* We for a FAT file system, we must cheat and pretend that the
  234.    * file was not made on OS2 but under DOS. unzip is confused otherwise.
  235.    */
  236. #else
  237.   z->vem = dosify ? 20 :                /* Made under MSDOS by PKZIP 2.0 */
  238. #endif
  239. #ifdef VMS
  240.                     0x200 + REVISION;   /* Made under VMS by this Zip */
  241. #else /* !VMS */
  242. # ifdef OS2
  243.                     0x600 + REVISION;   /* Made under OS/2 by this Zip */
  244. # else /* !OS2 */
  245. #  ifdef MSDOS
  246.                     0     + REVISION;   /* Made under MSDOS by this Zip */
  247. #  else
  248.                     0x300 + REVISION;   /* Made under Unix by this Zip */
  249. #  endif /* MSDOS */
  250. # endif /* ?OS2 */
  251. #endif /* ?VMS */
  252.  
  253.   z->ver = 20;                          /* Need PKUNZIP 2.0 */
  254.   z->crc = 0;  /* to be updated later */
  255.   /* Assume first that we will need an extended local header: */
  256.   z->flg = 8;  /* to be updated later */
  257. #ifdef CRYPT
  258.   if (key != NULL) {
  259.     z->flg |= 1;
  260.     /* Since we do not yet know the crc here, we pretend that the crc
  261.      * is the modification time:
  262.      */
  263.     z->crc = z->tim << 16;
  264.   }
  265. #endif
  266.   z->lflg = z->flg;
  267.   z->how = m;                             /* may be changed later  */
  268.   z->siz = m == STORE && q >= 0 ? q : 0;  /* will be changed later  */
  269.   z->len = q >= 0 ? q : 0;                /* may be changed later  */
  270.   z->dsk = 0;
  271.   z->att = BINARY;                        /* may be changed later */
  272.   z->atx = z->dosflag ? a & 0xff : a;      /* Attributes from filetime() */
  273.   z->off = tempzn;
  274.   if ((r = putlocal(z, y)) != ZE_OK)
  275.     return r;
  276.   tempzn += 4 + LOCHEAD + z->nam + z->ext;
  277.  
  278. #ifdef CRYPT
  279.   if (key != NULL) {
  280.     crypthead(key, z->crc, y);
  281.     z->siz += 12;  /* to be updated later */
  282.     tempzn += 12;
  283.   }
  284. #endif
  285.   o = ftell(y); /* for debugging only */
  286.  
  287.   /* Write stored or deflated file to zip file */
  288.   isize = 0L;
  289.   crc = updcrc((char *)NULL, 0);
  290.  
  291.   if (m == DEFLATE) {
  292.      bi_init(y);
  293.      z->att = (ush)UNKNOWN;
  294.      ct_init(&z->att, &m);
  295.      lm_init(level, &z->flg);
  296.      s = deflate();
  297.   }
  298.   else
  299.   {
  300.     if ((b = malloc(CBSZ)) == NULL)
  301.        return ZE_MEM;
  302.  
  303.     if (z->name[z->nam - 1] != '/') /* no read for directories */
  304.     while ((k = l ? rdsymlnk(z->name, b, CBSZ) : zread(ifile, b, CBSZ)) > 0)
  305.     {
  306.       isize += k;
  307.       crc = updcrc(b, k);
  308.       if (zfwrite(b, 1, k, y) != k)
  309.       {
  310.         free((voidp *)b);
  311.         return ZE_TEMP;
  312.       }
  313. #ifdef MINIX
  314.       if (l)
  315.         q = k;
  316. #endif /* MINIX */
  317.       if (l)
  318.         break;
  319.     }
  320.     free((voidp *)b);
  321.     s = isize;
  322.   }
  323.   if (ifile != fbad && zerr(ifile))
  324.     return ZE_READ;
  325.   if (ifile != fbad)
  326.     zclose(ifile);
  327.  
  328.   tempzn += s;
  329.   p = tempzn; /* save for future fseek() */
  330.  
  331. #ifndef VMS
  332.   /* Check input size (but not in VMS--variable record lengths mess it up) */
  333.   if (q >= 0 && isize != (ulg)q && !translate_eol)
  334.   {
  335.     fprintf(mesg, " i=%ld, q=%ld ", isize, q);
  336.     error("incorrect input size");
  337.   }
  338. #endif /* !VMS */
  339.  
  340.   /* Try to rewrite the local header with correct information */
  341.   z->crc = crc;
  342.   z->siz = s;
  343. #ifdef CRYPT
  344.   if (key != NULL)
  345.     z->siz += 12;
  346. #endif
  347.   z->len = isize;
  348.   if (fseek(y, z->off, SEEK_SET)) {
  349.     if (z->how != (ush) m)
  350.        error("can't rewrite method");
  351.     if (m == STORE && q < 0)
  352.        error("zip -0 not allowed for input/output from/to pipe or device");
  353.     if ((r = putextended(z, y)) != ZE_OK)
  354.       return r;
  355.     tempzn += 16L;
  356.     z->flg = z->lflg; /* if flg modified by inflate */
  357.   } else {
  358.      /* seek ok, ftell() should work, check compressed size */
  359. #ifndef VMS
  360.     if (p - o != s) {
  361.       fprintf(mesg, " s=%ld, actual=%ld ", s, p-o);
  362.       error("incorrect compressed size");
  363.     }
  364. #endif
  365.     z->how = m;
  366.     if ((z->flg & 1) == 0)
  367.       z->flg &= ~8; /* clear the extended local header flag */
  368.     z->lflg = z->flg;
  369.     /* rewrite the local header: */
  370.     if ((r = putlocal(z, y)) != ZE_OK)
  371.       return r;
  372.     if (fseek(y, p, SEEK_SET))
  373.       return ZE_READ;
  374.     if ((z->flg & 1) != 0) {
  375.       /* encrypted file, extended header still required */
  376.       if ((r = putextended(z, y)) != ZE_OK)
  377.         return r;
  378.       tempzn += 16L;
  379.     }
  380.   }
  381.  
  382.   /* Display statistics */
  383.   if (noisy)
  384.   {
  385.     if (verbose)
  386.       fprintf(mesg, " (in=%lu) (out=%lu)", isize, s);
  387.     if (m == DEFLATE)
  388.       fprintf(mesg, " (deflated %d%%)\n", percent(isize, s));
  389.     else
  390.       fprintf(mesg, " (stored 0%%)\n");
  391.     fflush(mesg);
  392.   }
  393.   return ZE_OK;
  394. }
  395.  
  396.  
  397. int file_read(buf, size)
  398.   char *buf;
  399.   unsigned size;
  400. /* Read a new buffer from the current input file, and update the crc and
  401.  * input file size.
  402.  * IN assertion: size >= 2 (for end-of-line translation)
  403.  */
  404. {
  405.   unsigned len;
  406.   char far *b;
  407.   if (translate_eol) {
  408.     /* static char last_byte = '\0'; */
  409.     size >>= 1;
  410.     b = buf+size;
  411.     size = len = zread(ifile, b, size);
  412.     if (len == (unsigned)EOF || len == 0) return len;
  413.     do {
  414.        /* ??? keep cr lf intact */
  415.        if ((*buf++ = *b++) == '\n') *(buf-1) = '\r', *buf++ = '\n', len++;
  416.     } while (--size != 0);
  417.     buf -= len;
  418.   } else {
  419.     len = zread(ifile, buf, size);
  420.     if (len == (unsigned)EOF || len == 0) return len;
  421.   }
  422.   crc = updcrc(buf, len);
  423.   isize += (ulg)len;
  424.   return len;
  425. }
  426. #endif /* !UTIL */
  427.