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

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

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


Revision 1.4 - (hide annotations) (download) (as text)
Sun Mar 3 18:14:14 2002 UTC (18 years, 1 month ago) by stdarg
Branch: MAIN
Changes since 1.3: +2 -1 lines
File MIME type: text/x-chdr
*** empty log message ***

1 stdarg 1.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 stdarg 1.3 char buf[4096];
18     int r;
19 stdarg 1.1
20 stdarg 1.3 my_iobuf.data = buf;
21     my_iobuf.max = sizeof(buf);
22 stdarg 1.1 zip->instream.next_in = new_data->data;
23     zip->instream.avail_in = new_data->len;
24 stdarg 1.3 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 stdarg 1.1 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 stdarg 1.2 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 stdarg 1.1 /* 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 stdarg 1.3 char buf[4096];
63     int flags, r;
64 stdarg 1.1
65     zip->outstream.next_in = data->data;
66     zip->outstream.avail_in = data->len;
67 stdarg 1.3 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 stdarg 1.1 return(0);
85     }
86    
87     static sockbuf_event_t zipmode_filter = {
88 stdarg 1.4 (Function) 6,
89 stdarg 1.1 (Function) "zip-mode",
90     zipmode_read,
91     NULL,
92     zipmode_eof_and_err,
93     zipmode_eof_and_err,
94 stdarg 1.4 NULL,
95 stdarg 1.1 zipmode_write
96     };
97    
98     int zipmode_on(int idx)
99     {
100     zipmode_t *zip;
101    
102     zip = (zipmode_t *)calloc(sizeof(*zip), 1);
103     deflateInit(&zip->outstream, Z_DEFAULT_COMPRESSION);
104     inflateInit(&zip->instream);
105    
106     sockbuf_attach_filter(idx, zipmode_filter, zip);
107     return(0);
108     }
109    
110     int zipmode_off(int idx)
111     {
112     zipmode_t *zip;
113    
114     sockbuf_detach_filter(idx, zipmode_filter, &zip);
115     if (zip) {
116     deflateEnd(&zip->outstream);
117     inflateEnd(&zip->instream);
118     free(zip);
119     }
120     return(0);
121     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23