1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton, |
---|
5 | * New Zealand. |
---|
6 | * |
---|
7 | * Authors: Daniel Lawson |
---|
8 | * Perry Lorier |
---|
9 | * Shane Alcock |
---|
10 | * |
---|
11 | * All rights reserved. |
---|
12 | * |
---|
13 | * This code has been developed by the University of Waikato WAND |
---|
14 | * research group. For further information please see http://www.wand.net.nz/ |
---|
15 | * |
---|
16 | * libtrace is free software; you can redistribute it and/or modify |
---|
17 | * it under the terms of the GNU General Public License as published by |
---|
18 | * the Free Software Foundation; either version 2 of the License, or |
---|
19 | * (at your option) any later version. |
---|
20 | * |
---|
21 | * libtrace is distributed in the hope that it will be useful, |
---|
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
24 | * GNU General Public License for more details. |
---|
25 | * |
---|
26 | * You should have received a copy of the GNU General Public License |
---|
27 | * along with libtrace; if not, write to the Free Software |
---|
28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
29 | * |
---|
30 | * $Id$ |
---|
31 | * |
---|
32 | */ |
---|
33 | |
---|
34 | |
---|
35 | #include <zlib.h> |
---|
36 | #include "wandio.h" |
---|
37 | #include <sys/types.h> |
---|
38 | #include <sys/stat.h> |
---|
39 | #include <fcntl.h> |
---|
40 | #include <stdlib.h> |
---|
41 | #include <string.h> |
---|
42 | |
---|
43 | /* Libtrace IO module implementing a zlib writer */ |
---|
44 | |
---|
45 | enum err_t { |
---|
46 | ERR_OK = 1, |
---|
47 | ERR_EOF = 0, |
---|
48 | ERR_ERROR = -1 |
---|
49 | }; |
---|
50 | |
---|
51 | struct zlibw_t { |
---|
52 | z_stream strm; |
---|
53 | Bytef outbuff[1024*1024]; |
---|
54 | iow_t *child; |
---|
55 | enum err_t err; |
---|
56 | int inoffset; |
---|
57 | }; |
---|
58 | |
---|
59 | |
---|
60 | extern iow_source_t zlib_wsource; |
---|
61 | |
---|
62 | #define DATA(iow) ((struct zlibw_t *)((iow)->data)) |
---|
63 | #define min(a,b) ((a)<(b) ? (a) : (b)) |
---|
64 | |
---|
65 | iow_t *zlib_wopen(iow_t *child, int compress_level) |
---|
66 | { |
---|
67 | iow_t *iow; |
---|
68 | if (!child) |
---|
69 | return NULL; |
---|
70 | iow = malloc(sizeof(iow_t)); |
---|
71 | iow->source = &zlib_wsource; |
---|
72 | iow->data = malloc(sizeof(struct zlibw_t)); |
---|
73 | |
---|
74 | DATA(iow)->child = child; |
---|
75 | |
---|
76 | DATA(iow)->strm.next_in = NULL; |
---|
77 | DATA(iow)->strm.avail_in = 0; |
---|
78 | DATA(iow)->strm.next_out = DATA(iow)->outbuff; |
---|
79 | DATA(iow)->strm.avail_out = sizeof(DATA(iow)->outbuff); |
---|
80 | DATA(iow)->strm.zalloc = Z_NULL; |
---|
81 | DATA(iow)->strm.zfree = Z_NULL; |
---|
82 | DATA(iow)->strm.opaque = NULL; |
---|
83 | DATA(iow)->err = ERR_OK; |
---|
84 | |
---|
85 | deflateInit2(&DATA(iow)->strm, |
---|
86 | compress_level, /* Level */ |
---|
87 | Z_DEFLATED, /* Method */ |
---|
88 | 15 | 16, /* 15 bits of windowsize, 16 == use gzip header */ |
---|
89 | 9, /* Use maximum (fastest) amount of memory usage */ |
---|
90 | Z_DEFAULT_STRATEGY |
---|
91 | ); |
---|
92 | |
---|
93 | return iow; |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | static off_t zlib_wwrite(iow_t *iow, const char *buffer, off_t len) |
---|
98 | { |
---|
99 | if (DATA(iow)->err == ERR_EOF) { |
---|
100 | return 0; /* EOF */ |
---|
101 | } |
---|
102 | if (DATA(iow)->err == ERR_ERROR) { |
---|
103 | return -1; /* ERROR! */ |
---|
104 | } |
---|
105 | |
---|
106 | DATA(iow)->strm.next_in = (Bytef*)buffer; /* This casts away const, but it's really const |
---|
107 | * anyway |
---|
108 | */ |
---|
109 | DATA(iow)->strm.avail_in = len; |
---|
110 | |
---|
111 | while (DATA(iow)->err == ERR_OK && DATA(iow)->strm.avail_in > 0) { |
---|
112 | while (DATA(iow)->strm.avail_out <= 0) { |
---|
113 | int bytes_written = wandio_wwrite(DATA(iow)->child, |
---|
114 | (char *)DATA(iow)->outbuff, |
---|
115 | sizeof(DATA(iow)->outbuff)); |
---|
116 | if (bytes_written <= 0) { /* Error */ |
---|
117 | DATA(iow)->err = ERR_ERROR; |
---|
118 | /* Return how much data we managed to write ok */ |
---|
119 | if (DATA(iow)->strm.avail_in != (uint32_t)len) { |
---|
120 | return len-DATA(iow)->strm.avail_in; |
---|
121 | } |
---|
122 | /* Now return error */ |
---|
123 | return -1; |
---|
124 | } |
---|
125 | DATA(iow)->strm.next_out = DATA(iow)->outbuff; |
---|
126 | DATA(iow)->strm.avail_out = sizeof(DATA(iow)->outbuff); |
---|
127 | } |
---|
128 | /* Decompress some data into the output buffer */ |
---|
129 | int err=deflate(&DATA(iow)->strm, 0); |
---|
130 | switch(err) { |
---|
131 | case Z_OK: |
---|
132 | DATA(iow)->err = ERR_OK; |
---|
133 | break; |
---|
134 | default: |
---|
135 | DATA(iow)->err = ERR_ERROR; |
---|
136 | } |
---|
137 | } |
---|
138 | /* Return the number of bytes decompressed */ |
---|
139 | return len-DATA(iow)->strm.avail_in; |
---|
140 | } |
---|
141 | |
---|
142 | static void zlib_wclose(iow_t *iow) |
---|
143 | { |
---|
144 | while (deflate(&DATA(iow)->strm, Z_FINISH) == Z_OK) { |
---|
145 | /* Need to flush the output buffer */ |
---|
146 | wandio_wwrite(DATA(iow)->child, |
---|
147 | (char*)DATA(iow)->outbuff, |
---|
148 | sizeof(DATA(iow)->outbuff)-DATA(iow)->strm.avail_out); |
---|
149 | DATA(iow)->strm.next_out = DATA(iow)->outbuff; |
---|
150 | DATA(iow)->strm.avail_out = sizeof(DATA(iow)->outbuff); |
---|
151 | } |
---|
152 | deflateEnd(&DATA(iow)->strm); |
---|
153 | wandio_wwrite(DATA(iow)->child, |
---|
154 | (char *)DATA(iow)->outbuff, |
---|
155 | sizeof(DATA(iow)->outbuff)-DATA(iow)->strm.avail_out); |
---|
156 | wandio_wdestroy(DATA(iow)->child); |
---|
157 | free(iow->data); |
---|
158 | free(iow); |
---|
159 | } |
---|
160 | |
---|
161 | iow_source_t zlib_wsource = { |
---|
162 | "zlibw", |
---|
163 | zlib_wwrite, |
---|
164 | zlib_wclose |
---|
165 | }; |
---|
166 | |
---|