1 /*
2 
3  Copyright (C) 1990,1991 Mark Adler, Richard B. Wales, and Jean-loup Gailly.
4  Permission is granted to any individual or institution to use, copy, or
5  redistribute this software so long as all of the original files are included
6  unmodified, that it is not sold for profit, and that this copyright notice
7  is retained.
8 
9 */
10 
11 /*
12  *  zipup.c by Mark Adler. Includes modifications by Jean-loup Gailly.
13  */
14 
15 #define NOCPYRT         /* this is not a main module */
16 #include <ctype.h>
17 #include "zip.h"
18 #include "zrevisio.h"
19 #include "system.h"
20 
21 /* Use the raw functions for MSDOS and Unix to save on buffer space.
22    They're not used for VMS since it doesn't work (raw is weird on VMS).
23    (This sort of stuff belongs in fileio.c, but oh well.) */
24 #if defined(VMS) || defined(C370)
25    typedef FILE *ftype;
26 #  define fhow FOPR
27 #  define fbad NULL
28 #  define zopen(n,p) fopen(n,p)
29 #  define zread(f,b,n) fread(b,1,n,f)
30 #  define zclose(f) fclose(f)
31 #  define zerr(f) ferror(f)
32 #  define zrew(f) rewind(f)
33 #  define zstdin stdin
34 #else /* !VMS */
35 #  if defined(MSDOS) || defined(WIN32)
36 #    include <io.h>
37 #    include <fcntl.h>
38 #    define fhow (O_RDONLY|O_BINARY)
39 #  elif defined(MACTC5) /* Macintosh */
40 #    define fhow 0
41 #  else /* !MSDOS && !Macintosh */
42 #    ifndef HAVE_UNISTD_H
43        size_t lseek(int handle, size_t offset, int whence);
44 #    endif /* !HAVE_UNISTD_H */
45 #    define fhow 0
46 #  endif /* ?MSDOS */
47    typedef int ftype;
48 #  define fbad (-1)
49 #  define zopen(n,p) open(n,p)
50 #  define zread(f,b,n) read(f,b,n)
51 #  define zclose(f) close(f)
52 #  define zerr(f) (k==(extent)(-1L))
53 #  define zrew(f) lseek(f,0L,0)
54 #  define zstdin 0
55 #endif /* ?VMS */
56 
57 /* Local data */
58 
59 local ftype ifile;		/* file to compress */
60 
61 void lm_free();
62 void ct_free();
63 
64 /* Compress the file fileName and write it to the file *y. Return an error
65    code in the ZE_ class.  Also, update tempzn by the number of bytes written.
66 */
zipup(FILE * inFile,FILE * y)67 int zipup(FILE *inFile, FILE *y)
68 /* ??? Does not yet handle non-seekable y */
69 {
70   int m;				/* method for this entry */
71   long q = -1L;			/* size returned by filetime */
72   ush att;			/* internal file attributes (dummy only) */
73   ush flg;				/* gp compresion flags (dummy only) */
74 
75 	/* Set input file and find its size */
76 #if defined(VMS) || defined(C370)
77 	ifile = inFile;
78 	fseek(ifile, 0L, SEEK_END);
79 	q = ftell(ifile);
80 	fseek(ifile, 0L, SEEK_SET);
81 #else
82 	ifile = fileno( inFile );
83 	q = lseek(ifile, 0L, SEEK_END);
84 	lseek(ifile, 0L, SEEK_SET);
85 #endif /* VMS */
86 
87 	m = (q == 0) ? STORE : DEFLATE;
88 
89   if (m == DEFLATE) {
90 	 bi_init(y);
91 	 att = UNKNOWN;
92 	 ct_init(&att, &m);
93 	 lm_init(level, &flg);
94 	 /* s = */ deflate();
95   }
96   lm_free();
97   ct_free();
98 
99   return(0);
100 }
101 
read_buf(buf,size)102 int read_buf(buf, size)
103   char far *buf;
104   unsigned size;
105 /* Read a new buffer from the current input file, and update the crc and
106  * input file size.
107  * IN assertion: size >= 2 (for end-of-line translation) */
108 {
109   unsigned len;
110 
111   len = zread(ifile, buf, size);
112   if (len == (unsigned)EOF || len == 0) return len;
113   return len;
114 }
115