1 | #include <zlib.h> |
---|
2 | #include "wandio.h" |
---|
3 | #include <sys/types.h> |
---|
4 | #include <sys/stat.h> |
---|
5 | #include <fcntl.h> |
---|
6 | #include <stdlib.h> |
---|
7 | #include <string.h> |
---|
8 | |
---|
9 | enum err_t { |
---|
10 | ERR_OK = 1, |
---|
11 | ERR_EOF = 0, |
---|
12 | ERR_ERROR = -1 |
---|
13 | }; |
---|
14 | |
---|
15 | struct zlibw_t { |
---|
16 | z_stream strm; |
---|
17 | Bytef outbuff[1024*1024]; |
---|
18 | iow_t *child; |
---|
19 | enum err_t err; |
---|
20 | int inoffset; |
---|
21 | }; |
---|
22 | |
---|
23 | |
---|
24 | extern iow_source_t zlib_wsource; |
---|
25 | |
---|
26 | #define DATA(iow) ((struct zlibw_t *)((iow)->data)) |
---|
27 | #define min(a,b) ((a)<(b) ? (a) : (b)) |
---|
28 | |
---|
29 | iow_t *zlib_wopen(iow_t *child, int compress_level) |
---|
30 | { |
---|
31 | iow_t *iow; |
---|
32 | if (!child) |
---|
33 | return NULL; |
---|
34 | iow = malloc(sizeof(iow_t)); |
---|
35 | iow->source = &zlib_wsource; |
---|
36 | iow->data = malloc(sizeof(struct zlibw_t)); |
---|
37 | |
---|
38 | DATA(iow)->child = child; |
---|
39 | |
---|
40 | DATA(iow)->strm.next_in = NULL; |
---|
41 | DATA(iow)->strm.avail_in = 0; |
---|
42 | DATA(iow)->strm.next_out = DATA(iow)->outbuff; |
---|
43 | DATA(iow)->strm.avail_out = sizeof(DATA(iow)->outbuff); |
---|
44 | DATA(iow)->strm.zalloc = Z_NULL; |
---|
45 | DATA(iow)->strm.zfree = Z_NULL; |
---|
46 | DATA(iow)->strm.opaque = NULL; |
---|
47 | DATA(iow)->err = ERR_OK; |
---|
48 | |
---|
49 | deflateInit2(&DATA(iow)->strm, |
---|
50 | compress_level, /* Level */ |
---|
51 | Z_DEFLATED, /* Method */ |
---|
52 | 15 | 16, /* 15 bits of windowsize, 16 == use gzip header */ |
---|
53 | 9, /* Use maximum (fastest) amount of memory usage */ |
---|
54 | Z_DEFAULT_STRATEGY |
---|
55 | ); |
---|
56 | |
---|
57 | return iow; |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | static off_t zlib_wwrite(iow_t *iow, const char *buffer, off_t len) |
---|
62 | { |
---|
63 | if (DATA(iow)->err == ERR_EOF) { |
---|
64 | return 0; /* EOF */ |
---|
65 | } |
---|
66 | if (DATA(iow)->err == ERR_ERROR) { |
---|
67 | return -1; /* ERROR! */ |
---|
68 | } |
---|
69 | |
---|
70 | DATA(iow)->strm.next_in = (Bytef*)buffer; /* This casts away const, but it's really const |
---|
71 | * anyway |
---|
72 | */ |
---|
73 | DATA(iow)->strm.avail_in = len; |
---|
74 | |
---|
75 | while (DATA(iow)->err == ERR_OK && DATA(iow)->strm.avail_in > 0) { |
---|
76 | while (DATA(iow)->strm.avail_out <= 0) { |
---|
77 | int bytes_written = wandio_wwrite(DATA(iow)->child, |
---|
78 | (char *)DATA(iow)->outbuff, |
---|
79 | sizeof(DATA(iow)->outbuff)); |
---|
80 | if (bytes_written <= 0) { /* Error */ |
---|
81 | DATA(iow)->err = ERR_ERROR; |
---|
82 | /* Return how much data we managed to write ok */ |
---|
83 | if (DATA(iow)->strm.avail_in != len) { |
---|
84 | return len-DATA(iow)->strm.avail_in; |
---|
85 | } |
---|
86 | /* Now return error */ |
---|
87 | return -1; |
---|
88 | } |
---|
89 | DATA(iow)->strm.next_out = DATA(iow)->outbuff; |
---|
90 | DATA(iow)->strm.avail_out = sizeof(DATA(iow)->outbuff); |
---|
91 | } |
---|
92 | /* Decompress some data into the output buffer */ |
---|
93 | int err=deflate(&DATA(iow)->strm, 0); |
---|
94 | switch(err) { |
---|
95 | case Z_OK: |
---|
96 | DATA(iow)->err = ERR_OK; |
---|
97 | break; |
---|
98 | default: |
---|
99 | DATA(iow)->err = ERR_ERROR; |
---|
100 | } |
---|
101 | } |
---|
102 | /* Return the number of bytes decompressed */ |
---|
103 | return len-DATA(iow)->strm.avail_in; |
---|
104 | } |
---|
105 | |
---|
106 | static void zlib_wclose(iow_t *iow) |
---|
107 | { |
---|
108 | while (deflate(&DATA(iow)->strm, Z_FINISH) == Z_OK) { |
---|
109 | /* Need to flush the output buffer */ |
---|
110 | wandio_wwrite(DATA(iow)->child, |
---|
111 | (char*)DATA(iow)->outbuff, |
---|
112 | sizeof(DATA(iow)->outbuff)-DATA(iow)->strm.avail_out); |
---|
113 | DATA(iow)->strm.next_out = DATA(iow)->outbuff; |
---|
114 | DATA(iow)->strm.avail_out = sizeof(DATA(iow)->outbuff); |
---|
115 | } |
---|
116 | deflateEnd(&DATA(iow)->strm); |
---|
117 | wandio_wwrite(DATA(iow)->child, |
---|
118 | (char *)DATA(iow)->outbuff, |
---|
119 | sizeof(DATA(iow)->outbuff)-DATA(iow)->strm.avail_out); |
---|
120 | wandio_wdestroy(DATA(iow)->child); |
---|
121 | free(iow->data); |
---|
122 | free(iow); |
---|
123 | } |
---|
124 | |
---|
125 | iow_source_t zlib_wsource = { |
---|
126 | "zlibw", |
---|
127 | zlib_wwrite, |
---|
128 | zlib_wclose |
---|
129 | }; |
---|
130 | |
---|