vx32

Local 9vx git repository for patches.
git clone git://r-36.net/vx32
Log | Files | Refs

ezlib.c (1686B)


      1 
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <stdarg.h>
      5 #include <unistd.h>
      6 #include <errno.h>
      7 
      8 #include "zlib.h"
      9 
     10 
     11 #define BUFSIZE (64*1024)
     12 
     13 static z_stream s;
     14 static char inbuf[BUFSIZE];
     15 static char outbuf[BUFSIZE];
     16 
     17 
     18 void fatal(const char *fmt, ...)
     19 {
     20 	va_list ap;
     21 	va_start(ap, fmt);
     22 	vfprintf(stderr, fmt, ap);
     23 	va_end(ap);
     24 	fputc('\n', stderr);
     25 	exit(2);
     26 }
     27 
     28 int main(int argc, char **argv)
     29 {
     30 	int rc = deflateInit2(&s, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
     31 				-15, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
     32 	if (rc != Z_OK)
     33 		fatal("deflateInit: %d", rc);
     34 
     35 	// Compress the input file
     36 	ssize_t inlen;
     37 	while ((inlen = read(STDIN_FILENO, inbuf, BUFSIZE)) > 0) {
     38 
     39 		// Compress this input block
     40 		s.next_in = inbuf;
     41 		s.avail_in = inlen;
     42 		do {
     43 			s.next_out = outbuf;
     44 			s.avail_out = BUFSIZE;
     45 			if ((rc = deflate(&s, 0)) != Z_OK)
     46 				fatal("deflate: %d", rc);
     47 
     48 			// Write any output the decompressor produced
     49 			ssize_t outlen = write(STDOUT_FILENO, outbuf,
     50 						BUFSIZE - s.avail_out);
     51 			if (outlen < 0)
     52 				fatal("write error: %d", errno);
     53 
     54 			// Continue decompressing the input block until done
     55 		} while (s.avail_in > 0);
     56 	}
     57 	if (inlen < 0)
     58 		fatal("read error: %d", errno);
     59 
     60 	// Flush the output
     61 	s.avail_in = 0;
     62 	int done = 0;
     63 	do {
     64 		s.next_out = outbuf;
     65 		s.avail_out = BUFSIZE;
     66 		int rc = deflate(&s, Z_FINISH);
     67 		if (rc == Z_STREAM_END)
     68 			done = 1;
     69 		else if (rc != Z_OK)
     70 			fatal("deflate: %d", rc);
     71 
     72 		// Write compressor output
     73 		ssize_t outlen = write(STDOUT_FILENO, outbuf,
     74 					BUFSIZE - s.avail_out);
     75 		if (outlen < 0)
     76 			fatal("write error: %d", errno);
     77 
     78 	} while (!done);
     79 
     80 	rc = deflateEnd(&s);
     81 	if (rc != Z_OK)
     82 		fatal("deflateEnd: %d", rc);
     83 
     84 	return 0;
     85 }
     86