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 |
#define ZIPMODE_LEVEL SOCKBUF_LEVEL_COMPRESSION |
10 |
|
11 |
typedef struct { |
12 |
z_stream instream, outstream; |
13 |
int counter; |
14 |
} zipmode_t; |
15 |
|
16 |
static int zipmode_read(void *client_data, int idx, char *data, int len) |
17 |
{ |
18 |
zipmode_t *zip = client_data; |
19 |
char buf[4096]; |
20 |
int r, buflen; |
21 |
|
22 |
zip->instream.next_in = data; |
23 |
zip->instream.avail_in = 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 |
buflen = sizeof(buf) - zip->instream.avail_out; |
30 |
sockbuf_on_read(idx, ZIPMODE_LEVEL, buf, buflen); |
31 |
} |
32 |
} while (r == Z_OK && zip->instream.avail_in); |
33 |
return(0); |
34 |
} |
35 |
|
36 |
static int zipmode_eof(void *client_data, int idx, int err, const char *errmsg) |
37 |
{ |
38 |
zipmode_t *zip = client_data; |
39 |
int buflen; |
40 |
char buf[8192]; |
41 |
|
42 |
/* If there is any buffered data, do one more on->read callback. */ |
43 |
zip->instream.next_out = buf; |
44 |
zip->instream.avail_out = sizeof(buf); |
45 |
inflate(&zip->instream, Z_FINISH); |
46 |
|
47 |
if (zip->instream.avail_out != sizeof(buf)) { |
48 |
buflen = sizeof(buf) - zip->instream.avail_out; |
49 |
sockbuf_on_read(idx, ZIPMODE_LEVEL, buf, buflen); |
50 |
} |
51 |
printf("zip stats: input %d compressed / %d uncompressed\n", zip->instream.total_in, zip->instream.total_out); |
52 |
printf("zip stats: output %d compressed / %d uncompressed\n", zip->outstream.total_out, zip->outstream.total_in); |
53 |
/* And now continue the EOF/ERR event chain. */ |
54 |
sockbuf_on_eof(idx, ZIPMODE_LEVEL, err, errmsg); |
55 |
return(0); |
56 |
} |
57 |
|
58 |
static int zipmode_write(void *client_data, int idx, const char *data, int len) |
59 |
{ |
60 |
zipmode_t *zip = client_data; |
61 |
char buf[4096]; |
62 |
int flags, r, buflen; |
63 |
|
64 |
zip->outstream.next_in = (char *)data; |
65 |
zip->outstream.avail_in = len; |
66 |
do { |
67 |
zip->outstream.next_out = buf; |
68 |
zip->outstream.avail_out = sizeof(buf); |
69 |
zip->counter++; |
70 |
if (zip->counter == 1) { |
71 |
zip->counter = 0; |
72 |
flags = Z_SYNC_FLUSH; |
73 |
} |
74 |
else flags = Z_NO_FLUSH; |
75 |
r = deflate(&zip->outstream, flags); |
76 |
if (zip->outstream.avail_out != sizeof(buf)) { |
77 |
buflen = sizeof(buf) - zip->outstream.avail_out; |
78 |
sockbuf_on_write(idx, ZIPMODE_LEVEL, buf, buflen); |
79 |
} |
80 |
} while (r == Z_OK && zip->outstream.avail_in); |
81 |
return(0); |
82 |
} |
83 |
|
84 |
static sockbuf_filter_t zipmode_filter = { |
85 |
"zipmode", |
86 |
ZIPMODE_LEVEL, |
87 |
NULL, zipmode_eof, NULL, |
88 |
zipmode_read, zipmode_write |
89 |
}; |
90 |
|
91 |
int zipmode_on(int idx) |
92 |
{ |
93 |
zipmode_t *zip; |
94 |
|
95 |
zip = (zipmode_t *)calloc(1, sizeof(*zip)); |
96 |
deflateInit(&zip->outstream, Z_DEFAULT_COMPRESSION); |
97 |
inflateInit(&zip->instream); |
98 |
|
99 |
sockbuf_attach_filter(idx, &zipmode_filter, zip); |
100 |
return(0); |
101 |
} |
102 |
|
103 |
int zipmode_off(int idx) |
104 |
{ |
105 |
zipmode_t *zip; |
106 |
|
107 |
sockbuf_detach_filter(idx, &zipmode_filter, &zip); |
108 |
if (zip) { |
109 |
deflateEnd(&zip->outstream); |
110 |
inflateEnd(&zip->instream); |
111 |
free(zip); |
112 |
} |
113 |
return(0); |
114 |
} |