vx32

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

dbz2.c (1673B)


      1 
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <stdarg.h>
      5 #include <unistd.h>
      6 #include <errno.h>
      7 
      8 #include "bzlib.h"
      9 
     10 
     11 #define BUFSIZE (64*1024)
     12 
     13 static bz_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 void bz_internal_error (int errcode)
     29 {
     30 	fatal("Internal bzip2 error: %d", errcode);
     31 }
     32 
     33 int main(int argc, char **argv)
     34 {
     35 	while (1) {
     36 		int rc;
     37 		if ((rc = BZ2_bzDecompressInit(&s, 0, 0)) != BZ_OK)
     38 			fatal("BZ2_bzDecompressInit: %d", rc);
     39 
     40 		// Decompress the input file
     41 		int done = 0;
     42 		do {
     43 			// Read a block of input data
     44 			ssize_t inlen = read(STDIN_FILENO, inbuf, BUFSIZE);
     45 			if (inlen < 0)
     46 				fatal("read error: %d", errno);
     47 			if (inlen == 0)
     48 				fatal("compressed data appears to be truncated");
     49 
     50 			// Decompress this input block
     51 			s.next_in = inbuf;
     52 			s.avail_in = inlen;
     53 			do {
     54 				s.next_out = outbuf;
     55 				s.avail_out = BUFSIZE;
     56 				rc = BZ2_bzDecompress(&s);
     57 				if (rc == BZ_STREAM_END)
     58 					done = 1;
     59 				else if (rc != BZ_OK)
     60 					fatal("BZ2_bzDecompress: %d", rc);
     61 
     62 				// Write any output the decompressor produced
     63 				ssize_t outlen = write(STDOUT_FILENO, outbuf,
     64 							BUFSIZE - s.avail_out);
     65 				if (outlen < 0)
     66 					fatal("write error: %d", errno);
     67 
     68 				// Continue decompressing the input block until done
     69 			} while (s.avail_in > 0 && !done);
     70 		} while (!done);
     71 
     72 		if ((rc = BZ2_bzDecompressEnd(&s)) != BZ_OK)
     73 			fatal("BZ2_bzDecompressEnd: %d", rc);
     74 
     75 		// Indicate to parent that we're done
     76 		//asm volatile("syscall" : : "a" (VXAPC_DONE) : "ecx");
     77 		return 0;
     78 	}
     79 }
     80