home *** CD-ROM | disk | FTP | other *** search
/ Current Shareware 1994 January / SHAR194.ISO / compress / gnuzip_.zip / ZIP.C < prev    next >
C/C++ Source or Header  |  1993-03-28  |  3KB  |  116 lines

  1. /* zip.c -- compress files to the gzip or pkzip format
  2.  * Copyright (C) 1992-1993 Jean-loup Gailly
  3.  * This is free software; you can redistribute it and/or modify it under the
  4.  * terms of the GNU General Public License, see the file COPYING.
  5.  */
  6.  
  7. #ifndef lint
  8. static char rcsid[] = "$Id: zip.c,v 0.11 1993/02/10 16:07:22 jloup Exp $";
  9. #endif
  10.  
  11. #include "tailor.h"
  12. #include "gzip.h"
  13. #include "crypt.h"
  14.  
  15. #include <ctype.h>
  16. #include <stdio.h>
  17.  
  18. #if HAVE_UNISTD_H
  19. #  include <sys/types.h>
  20. #  include <unistd.h>
  21. #endif
  22.  
  23. local ulg crc;       /* crc on uncompressed file data */
  24. long overhead;       /* number of bytes in gzip header */
  25.  
  26. /* ===========================================================================
  27.  * Deflate in to out.
  28.  * IN assertions: the input and output buffers are cleared.
  29.  */
  30. void zip(in, out)
  31.     int in, out;            /* input and output file descriptors */
  32. {
  33.     uch  flags = 0;         /* general purpose bit flags */
  34.     ush  attr = 0;          /* ascii/binary flag */
  35.     ush  deflate_flags = 0; /* pkzip -es, -en or -ex equivalent */
  36.  
  37.     ifd = in;
  38.     ofd = out;
  39.     outcnt = 0;
  40.  
  41.     /* Write the header to the gzip file. See algorithm.doc for the format */
  42.  
  43.     method = DEFLATED;
  44.     put_byte(GZIP_MAGIC[0]); /* magic header */
  45.     put_byte(GZIP_MAGIC[1]);
  46.     put_byte(DEFLATED);      /* compression method */
  47.  
  48.     if (save_orig_name) {
  49.     flags |= ORIG_NAME;
  50.     }
  51.     /* Should look ahead a little in the input to determine the
  52.      * ascii/binary flag. To be done.
  53.      */
  54.  
  55.     put_byte(flags);         /* general flags */
  56.     put_long(time_stamp);
  57.  
  58.     /* Write deflated file to zip file */
  59.     crc = updcrc(0, 0);
  60.  
  61.     bi_init(out);
  62.     ct_init(&attr, &method);
  63.     lm_init(level, &deflate_flags);
  64.  
  65.     put_byte((uch)deflate_flags); /* extra flags */
  66.     put_byte(OS_CODE);            /* OS identifier */
  67.  
  68.     if (save_orig_name) {
  69.     char *p = basename(ifname); /* Don't save the directory part. */
  70.     do {
  71.         put_byte(casemap(*p));
  72.     } while (*p++);
  73.     }
  74.     overhead = (long)outcnt;
  75.  
  76.     (void)deflate();
  77.  
  78. #if !defined(NO_SIZE_CHECK) && !defined(RECORD_IO)
  79.   /* Check input size (but not in VMS -- variable record lengths mess it up)
  80.    * and not on MSDOS -- diet in TSR mode reports an incorrect file size)
  81.    */
  82.     if (ifile_size != -1L && isize != (ulg)ifile_size) {
  83.     Trace((stderr, " actual=%ld, read=%ld ", ifile_size, isize));
  84.     fprintf(stderr, " file size changed while zipping %s\n", ifname);
  85.     }
  86. #endif
  87.  
  88.     /* Write the crc and uncompressed size */
  89.     put_long(crc);
  90.     put_long(isize);
  91.  
  92.     flush_outbuf();
  93. }
  94.  
  95.  
  96. /* ===========================================================================
  97.  * Read a new buffer from the current input file, perform end-of-line
  98.  * translation, and update the crc and input file size.
  99.  * IN assertion: size >= 2 (for end-of-line translation)
  100.  */
  101. int file_read(buf, size)
  102.     char *buf;
  103.     unsigned size;
  104. {
  105.     unsigned len;
  106.  
  107.     Assert(insize == 0, "inbuf not empty");
  108.  
  109.     len = read(ifd, buf, size);
  110.     if (len == (unsigned)(-1) || len == 0) return (int)len;
  111.  
  112.     crc = updcrc((uch*)buf, len);
  113.     isize += (ulg)len;
  114.     return (int)len;
  115. }
  116.