vx32

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

dzlib.c (1591B)


      1 
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <stdint.h>
      5 #include <stdarg.h>
      6 #include <unistd.h>
      7 #include <errno.h>
      8 
      9 //#include <vxa/vxa.h>
     10 
     11 #include "zlib.h"
     12 
     13 
     14 #define BUFSIZE (64*1024)
     15 
     16 static z_stream s;
     17 static uint8_t inbuf[BUFSIZE];
     18 static uint8_t outbuf[BUFSIZE];
     19 
     20 
     21 void fatal(const char *fmt, ...)
     22 {
     23 	va_list ap;
     24 	va_start(ap, fmt);
     25 	vfprintf(stderr, fmt, ap);
     26 	va_end(ap);
     27 	fputc('\n', stderr);
     28 	exit(2);
     29 }
     30 
     31 int main(int argc, char **argv)
     32 {
     33 	int rc;
     34 	if ((rc = inflateInit2(&s, -15)) != Z_OK)
     35 		fatal("inflateInit: %d", rc);
     36 
     37 	while (1) {
     38 		// Decompress the input file
     39 		int done = 0;
     40 		do {
     41 			// Read a block of input data
     42 			ssize_t inlen = read(STDIN_FILENO, inbuf, BUFSIZE);
     43 			if (inlen < 0)
     44 				fatal("read error: %d", errno);
     45 			if (inlen == 0)
     46 				fatal("compressed data truncated");
     47 
     48 			// Decompress this input block
     49 			s.next_in = inbuf;
     50 			s.avail_in = inlen;
     51 			do {
     52 				s.next_out = outbuf;
     53 				s.avail_out = BUFSIZE;
     54 				rc = inflate(&s, 0);
     55 				if (rc == Z_STREAM_END)
     56 					done = 1;
     57 				else if (rc != Z_OK)
     58 					fatal("inflate: %d", rc);
     59 
     60 				// Write any output the decompressor produced
     61 				ssize_t outlen = write(STDOUT_FILENO, outbuf,
     62 							BUFSIZE - s.avail_out);
     63 				if (outlen < 0)
     64 					fatal("write error: %d", errno);
     65 
     66 				// Continue decompressing until done
     67 			} while (s.avail_in > 0 && !done);
     68 		} while (!done);
     69 
     70 		// Indicate to parent that we're done
     71 		//asm volatile("syscall" : : "a" (VXAPC_DONE) : "ecx");
     72 		return(0);
     73 
     74 		// Get zlib ready for the next stream
     75 		if ((rc = inflateReset(&s)) != Z_OK)
     76 			fatal("inflateReset: %d", rc);
     77 	}
     78 }
     79