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 | #include "wandio.h" |
---|
35 | #include <bzlib.h> |
---|
36 | #include <sys/types.h> |
---|
37 | #include <sys/stat.h> |
---|
38 | #include <fcntl.h> |
---|
39 | #include <stdlib.h> |
---|
40 | #include <string.h> |
---|
41 | |
---|
42 | /* Libtrace IO module implement a bzip writer */ |
---|
43 | |
---|
44 | enum err_t { |
---|
45 | ERR_OK = 1, |
---|
46 | ERR_EOF = 0, |
---|
47 | ERR_ERROR = -1 |
---|
48 | }; |
---|
49 | |
---|
50 | struct bzw_t { |
---|
51 | bz_stream strm; |
---|
52 | char outbuff[1024*1024]; |
---|
53 | int inoffset; |
---|
54 | iow_t *child; |
---|
55 | enum err_t err; |
---|
56 | }; |
---|
57 | |
---|
58 | |
---|
59 | extern iow_source_t bz_wsource; |
---|
60 | |
---|
61 | #define DATA(iow) ((struct bzw_t *)((iow)->data)) |
---|
62 | #define min(a,b) ((a)<(b) ? (a) : (b)) |
---|
63 | |
---|
64 | iow_t *bz_wopen(iow_t *child, int compress_level) |
---|
65 | { |
---|
66 | iow_t *iow; |
---|
67 | if (!child) |
---|
68 | return NULL; |
---|
69 | iow = malloc(sizeof(iow_t)); |
---|
70 | iow->source = &bz_wsource; |
---|
71 | iow->data = malloc(sizeof(struct bzw_t)); |
---|
72 | |
---|
73 | DATA(iow)->child = child; |
---|
74 | |
---|
75 | DATA(iow)->strm.next_in = NULL; |
---|
76 | DATA(iow)->strm.avail_in = 0; |
---|
77 | DATA(iow)->strm.next_out = DATA(iow)->outbuff; |
---|
78 | DATA(iow)->strm.avail_out = sizeof(DATA(iow)->outbuff); |
---|
79 | DATA(iow)->strm.bzalloc = NULL; |
---|
80 | DATA(iow)->strm.bzfree = NULL; |
---|
81 | DATA(iow)->strm.opaque = NULL; |
---|
82 | DATA(iow)->err = ERR_OK; |
---|
83 | |
---|
84 | BZ2_bzCompressInit(&DATA(iow)->strm, |
---|
85 | compress_level, /* Block size */ |
---|
86 | 0, /* Verbosity */ |
---|
87 | 30); /* Work factor */ |
---|
88 | |
---|
89 | return iow; |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | static off_t bz_wwrite(iow_t *iow, const char *buffer, off_t len) |
---|
94 | { |
---|
95 | if (DATA(iow)->err == ERR_EOF) { |
---|
96 | return 0; /* EOF */ |
---|
97 | } |
---|
98 | if (DATA(iow)->err == ERR_ERROR) { |
---|
99 | return -1; /* ERROR! */ |
---|
100 | } |
---|
101 | |
---|
102 | DATA(iow)->strm.next_in = (char*)buffer; |
---|
103 | DATA(iow)->strm.avail_in = len; |
---|
104 | |
---|
105 | while (DATA(iow)->err == ERR_OK && DATA(iow)->strm.avail_in > 0) { |
---|
106 | while (DATA(iow)->strm.avail_out <= 0) { |
---|
107 | int bytes_written = wandio_wwrite(DATA(iow)->child, |
---|
108 | DATA(iow)->outbuff, |
---|
109 | sizeof(DATA(iow)->outbuff)); |
---|
110 | if (bytes_written <= 0) { /* Error */ |
---|
111 | DATA(iow)->err = ERR_ERROR; |
---|
112 | /* Return how much data we managed to write ok */ |
---|
113 | if (DATA(iow)->strm.avail_in != (uint32_t)len) { |
---|
114 | return len-DATA(iow)->strm.avail_in; |
---|
115 | } |
---|
116 | /* Now return error */ |
---|
117 | return -1; |
---|
118 | } |
---|
119 | DATA(iow)->strm.next_out = DATA(iow)->outbuff; |
---|
120 | DATA(iow)->strm.avail_out = sizeof(DATA(iow)->outbuff); |
---|
121 | } |
---|
122 | /* Decompress some data into the output buffer */ |
---|
123 | int err=BZ2_bzCompress(&DATA(iow)->strm, 0); |
---|
124 | switch(err) { |
---|
125 | case BZ_RUN_OK: |
---|
126 | case BZ_OK: |
---|
127 | DATA(iow)->err = ERR_OK; |
---|
128 | break; |
---|
129 | default: |
---|
130 | DATA(iow)->err = ERR_ERROR; |
---|
131 | break; |
---|
132 | } |
---|
133 | } |
---|
134 | /* Return the number of bytes compressed */ |
---|
135 | return len-DATA(iow)->strm.avail_in; |
---|
136 | } |
---|
137 | |
---|
138 | static void bz_wclose(iow_t *iow) |
---|
139 | { |
---|
140 | while (BZ2_bzCompress(&DATA(iow)->strm, BZ_FINISH) == BZ_OK) { |
---|
141 | /* Need to flush the output buffer */ |
---|
142 | wandio_wwrite(DATA(iow)->child, |
---|
143 | DATA(iow)->outbuff, |
---|
144 | sizeof(DATA(iow)->outbuff)-DATA(iow)->strm.avail_out); |
---|
145 | DATA(iow)->strm.next_out = DATA(iow)->outbuff; |
---|
146 | DATA(iow)->strm.avail_out = sizeof(DATA(iow)->outbuff); |
---|
147 | } |
---|
148 | BZ2_bzCompressEnd(&DATA(iow)->strm); |
---|
149 | wandio_wwrite(DATA(iow)->child, |
---|
150 | DATA(iow)->outbuff, |
---|
151 | sizeof(DATA(iow)->outbuff)-DATA(iow)->strm.avail_out); |
---|
152 | wandio_wdestroy(DATA(iow)->child); |
---|
153 | free(iow->data); |
---|
154 | free(iow); |
---|
155 | } |
---|
156 | |
---|
157 | iow_source_t bz_wsource = { |
---|
158 | "bzw", |
---|
159 | bz_wwrite, |
---|
160 | bz_wclose |
---|
161 | }; |
---|
162 | |
---|