/[cvs]/eggdrop1.9/testcode/zipmode.c
ViewVC logotype

Contents of /eggdrop1.9/testcode/zipmode.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Thu Jan 10 17:20:55 2002 UTC (18 years, 3 months ago) by stdarg
Branch: MAIN
Changes since 1.2: +32 -27 lines
File MIME type: text/x-chdr
Found the probem with truncated output, I think.

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <zlib.h>
6
7 #include "sockbuf.h"
8
9 typedef struct {
10 z_stream instream, outstream;
11 int counter;
12 } zipmode_t;
13
14 static int zipmode_read(int idx, int event, int level, sockbuf_iobuf_t *new_data, zipmode_t *zip)
15 {
16 sockbuf_iobuf_t my_iobuf;
17 char buf[4096];
18 int r;
19
20 my_iobuf.data = buf;
21 my_iobuf.max = sizeof(buf);
22 zip->instream.next_in = new_data->data;
23 zip->instream.avail_in = new_data->len;
24 do {
25 zip->instream.next_out = buf;
26 zip->instream.avail_out = sizeof(buf);
27 r = inflate(&zip->instream, Z_SYNC_FLUSH);
28 if (zip->instream.avail_out != sizeof(buf)) {
29 my_iobuf.len = sizeof(buf) - zip->instream.avail_out;
30 sockbuf_filter(idx, event, level, &my_iobuf);
31 }
32 } while (r == Z_OK && zip->instream.avail_in);
33 return(0);
34 }
35
36 static int zipmode_eof_and_err(int idx, int event, int level, void *ignore, zipmode_t *zip)
37 {
38 char buf[8192];
39 sockbuf_iobuf_t iobuf;
40
41 /* If there is any buffered data, do one more on->read callback. */
42 zip->instream.next_out = buf;
43 zip->instream.avail_out = sizeof(buf);
44 inflate(&zip->instream, Z_FINISH);
45
46 if (zip->instream.avail_out != sizeof(buf)) {
47 iobuf.data = buf;
48 iobuf.len = sizeof(buf) - zip->instream.avail_out;
49 iobuf.max = sizeof(buf);
50 sockbuf_filter(idx, SOCKBUF_READ, level, &iobuf);
51 }
52 printf("zip stats: input %d compressed / %d uncompressed\n", zip->instream.total_in, zip->instream.total_out);
53 printf("zip stats: output %d compressed / %d uncompressed\n", zip->outstream.total_out, zip->outstream.total_in);
54 /* And now continue the EOF/ERR event chain. */
55 sockbuf_filter(idx, event, level, ignore);
56 return(0);
57 }
58
59 static int zipmode_write(int idx, int event, int level, sockbuf_iobuf_t *data, zipmode_t *zip)
60 {
61 sockbuf_iobuf_t my_iobuf;
62 char buf[4096];
63 int flags, r;
64
65 zip->outstream.next_in = data->data;
66 zip->outstream.avail_in = data->len;
67 do {
68 zip->outstream.next_out = buf;
69 zip->outstream.avail_out = sizeof(buf);
70 zip->counter++;
71 if (zip->counter == 1) {
72 zip->counter = 0;
73 flags = Z_SYNC_FLUSH;
74 }
75 else flags = Z_NO_FLUSH;
76 r = deflate(&zip->outstream, flags);
77 if (zip->outstream.avail_out != sizeof(buf)) {
78 my_iobuf.data = buf;
79 my_iobuf.len = sizeof(buf) - zip->outstream.avail_out;
80 my_iobuf.max = sizeof(buf);
81 sockbuf_filter(idx, event, level, &my_iobuf);
82 }
83 } while (r == Z_OK && zip->outstream.avail_in);
84 return(0);
85 }
86
87 static sockbuf_event_t zipmode_filter = {
88 (Function) 5,
89 (Function) "zip-mode",
90 zipmode_read,
91 NULL,
92 zipmode_eof_and_err,
93 zipmode_eof_and_err,
94 zipmode_write
95 };
96
97 int zipmode_on(int idx)
98 {
99 zipmode_t *zip;
100
101 zip = (zipmode_t *)calloc(sizeof(*zip), 1);
102 deflateInit(&zip->outstream, Z_DEFAULT_COMPRESSION);
103 inflateInit(&zip->instream);
104
105 sockbuf_attach_filter(idx, zipmode_filter, zip);
106 return(0);
107 }
108
109 int zipmode_off(int idx)
110 {
111 zipmode_t *zip;
112
113 sockbuf_detach_filter(idx, zipmode_filter, &zip);
114 if (zip) {
115 deflateEnd(&zip->outstream);
116 inflateEnd(&zip->instream);
117 free(zip);
118 }
119 return(0);
120 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23