ebz2.c - vx32 - Local 9vx git repository for patches.
(HTM) git clone git://r-36.net/vx32
(DIR) Log
(DIR) Files
(DIR) Refs
---
ebz2.c (2621B)
---
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 void fatal(const char *fmt, ...)
18 {
19 va_list ap;
20 va_start(ap, fmt);
21 vfprintf(stderr, fmt, ap);
22 va_end(ap);
23 fputc('\n', stderr);
24 exit(2);
25 }
26
27 void bz_internal_error (int errcode)
28 {
29 fatal("Internal bzip2 error: %d", errcode);
30 }
31
32 int main(int argc, char **argv)
33 {
34 printf("malloc3: %08x\n", malloc(123));
35 printf("malloc3: %08x\n", malloc(123));
36 printf("malloc3: %08x\n", malloc(123));
37 printf("malloc3: %08x\n", malloc(123));
38 printf("malloc4: %08x\n", malloc(1234));
39 printf("malloc4: %08x\n", malloc(1234));
40 printf("malloc4: %08x\n", malloc(1234));
41 printf("malloc4: %08x\n", malloc(1234));
42 printf("malloc5: %08x\n", malloc(12345));
43 printf("malloc5: %08x\n", malloc(12345));
44 printf("malloc5: %08x\n", malloc(12345));
45 printf("malloc5: %08x\n", malloc(12345));
46 printf("malloc6: %08x\n", malloc(123456));
47 printf("malloc6: %08x\n", malloc(123456));
48 printf("malloc6: %08x\n", malloc(123456));
49 printf("malloc6: %08x\n", malloc(123456));
50 printf("malloc7: %08x\n", malloc(1234567));
51 printf("malloc7: %08x\n", malloc(1234567));
52 printf("malloc7: %08x\n", malloc(1234567));
53 printf("malloc7: %08x\n", malloc(1234567));
54 int rc = BZ2_bzCompressInit(&s, 9, 0, 0);
55 if (rc != BZ_OK)
56 fatal("BZ2_bzCompressInit: %d", rc);
57
58 // Compress the input file
59 ssize_t inlen;
60 while ((inlen = read(STDIN_FILENO, inbuf, BUFSIZE)) > 0) {
61
62 // Compress this input block
63 s.next_in = inbuf;
64 s.avail_in = inlen;
65 do {
66 s.next_out = outbuf;
67 s.avail_out = BUFSIZE;
68 if ((rc = BZ2_bzCompress(&s, BZ_RUN)) != BZ_RUN_OK)
69 fatal("BZ2_bzCompress: %d", rc);
70
71 // Write any output the decompressor produced
72 ssize_t outlen = write(STDOUT_FILENO, outbuf,
73 BUFSIZE - s.avail_out);
74 if (outlen < 0)
75 fatal("write error: %d", errno);
76
77 // Continue decompressing the input block until done
78 } while (s.avail_in > 0);
79 }
80 if (inlen < 0)
81 fatal("read error: %d", errno);
82
83 // Flush the output
84 s.avail_in = 0;
85 int done = 0;
86 do {
87 s.next_out = outbuf;
88 s.avail_out = BUFSIZE;
89 int rc = BZ2_bzCompress(&s, BZ_FINISH);
90 if (rc == BZ_STREAM_END)
91 done = 1;
92 else if (rc != BZ_FINISH_OK)
93 fatal("BZ2_bzCompress: %d", rc);
94
95 // Write compressor output
96 ssize_t outlen = write(STDOUT_FILENO, outbuf,
97 BUFSIZE - s.avail_out);
98 if (outlen < 0)
99 fatal("write error: %d", errno);
100
101 } while (!done);
102
103 rc = BZ2_bzCompressEnd(&s);
104 if (rc != BZ_OK)
105 fatal("BZ2_bzCompressEnd: %d", rc);
106
107 return 0;
108 }
109