/[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.1 - (show annotations) (download) (as text)
Tue Jan 8 00:09:09 2002 UTC (18 years, 2 months ago) by stdarg
Branch: MAIN
File MIME type: text/x-chdr
Err, I don't know if I already added these files or not, so I'll add them again.

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[8192];
18
19 zip->instream.next_in = new_data->data;
20 zip->instream.avail_in = new_data->len;
21 zip->instream.next_out = buf;
22 zip->instream.avail_out = sizeof(buf);
23 inflate(&zip->instream, Z_SYNC_FLUSH);
24 if (zip->instream.avail_out != sizeof(buf)) {
25 my_iobuf.data = buf;
26 my_iobuf.len = sizeof(buf) - zip->instream.avail_out;
27 my_iobuf.max = sizeof(buf);
28 sockbuf_filter(idx, event, level, &my_iobuf);
29 }
30 return(0);
31 }
32
33 static int zipmode_eof_and_err(int idx, int event, int level, void *ignore, zipmode_t *zip)
34 {
35 char buf[8192];
36 sockbuf_iobuf_t iobuf;
37
38 /* If there is any buffered data, do one more on->read callback. */
39 zip->instream.next_out = buf;
40 zip->instream.avail_out = sizeof(buf);
41 inflate(&zip->instream, Z_FINISH);
42
43 if (zip->instream.avail_out != sizeof(buf)) {
44 iobuf.data = buf;
45 iobuf.len = sizeof(buf) - zip->instream.avail_out;
46 iobuf.max = sizeof(buf);
47 sockbuf_filter(idx, SOCKBUF_READ, level, &iobuf);
48 }
49 /* And now continue the EOF/ERR event chain. */
50 sockbuf_filter(idx, event, level, ignore);
51 return(0);
52 }
53
54 static int zipmode_write(int idx, int event, int level, sockbuf_iobuf_t *data, zipmode_t *zip)
55 {
56 sockbuf_iobuf_t my_iobuf;
57 char buf[8192];
58 int flags;
59
60 zip->outstream.next_in = data->data;
61 zip->outstream.avail_in = data->len;
62 zip->outstream.next_out = buf;
63 zip->outstream.avail_out = sizeof(buf);
64 zip->counter++;
65 if (zip->counter == 1) {
66 zip->counter = 0;
67 flags = Z_SYNC_FLUSH;
68 }
69 else flags = Z_NO_FLUSH;
70 deflate(&zip->outstream, flags);
71 if (zip->outstream.avail_out != sizeof(buf)) {
72 my_iobuf.data = buf;
73 my_iobuf.len = sizeof(buf) - zip->outstream.avail_out;
74 my_iobuf.max = sizeof(buf);
75 sockbuf_filter(idx, event, level, &my_iobuf);
76 }
77 return(0);
78 }
79
80 static sockbuf_event_t zipmode_filter = {
81 (Function) 5,
82 (Function) "zip-mode",
83 zipmode_read,
84 NULL,
85 zipmode_eof_and_err,
86 zipmode_eof_and_err,
87 zipmode_write
88 };
89
90 int zipmode_on(int idx)
91 {
92 zipmode_t *zip;
93
94 zip = (zipmode_t *)calloc(sizeof(*zip), 1);
95 deflateInit(&zip->outstream, Z_DEFAULT_COMPRESSION);
96 inflateInit(&zip->instream);
97
98 sockbuf_attach_filter(idx, zipmode_filter, zip);
99 return(0);
100 }
101
102 int zipmode_off(int idx)
103 {
104 zipmode_t *zip;
105
106 sockbuf_detach_filter(idx, zipmode_filter, &zip);
107 if (zip) {
108 deflateEnd(&zip->outstream);
109 inflateEnd(&zip->instream);
110 free(zip);
111 }
112 return(0);
113 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23