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 "wandio.h" |
---|
36 | #include <bzlib.h> |
---|
37 | #include <sys/types.h> |
---|
38 | #include <sys/stat.h> |
---|
39 | #include <fcntl.h> |
---|
40 | #include <stdlib.h> |
---|
41 | #include <string.h> |
---|
42 | #include <errno.h> |
---|
43 | |
---|
44 | /* Libtrace IO module implementing a bzip reader */ |
---|
45 | |
---|
46 | enum err_t { |
---|
47 | ERR_OK = 1, |
---|
48 | ERR_EOF = 0, |
---|
49 | ERR_ERROR = -1 |
---|
50 | }; |
---|
51 | |
---|
52 | struct bz_t { |
---|
53 | bz_stream strm; |
---|
54 | char inbuff[1024*1024]; |
---|
55 | int outoffset; |
---|
56 | io_t *parent; |
---|
57 | enum err_t err; |
---|
58 | }; |
---|
59 | |
---|
60 | |
---|
61 | extern io_source_t bz_source; |
---|
62 | |
---|
63 | #define DATA(io) ((struct bz_t *)((io)->data)) |
---|
64 | #define min(a,b) ((a)<(b) ? (a) : (b)) |
---|
65 | |
---|
66 | io_t *bz_open(io_t *parent) |
---|
67 | { |
---|
68 | io_t *io; |
---|
69 | if (!parent) |
---|
70 | return NULL; |
---|
71 | io = malloc(sizeof(io_t)); |
---|
72 | io->source = &bz_source; |
---|
73 | io->data = malloc(sizeof(struct bz_t)); |
---|
74 | |
---|
75 | DATA(io)->parent = parent; |
---|
76 | |
---|
77 | DATA(io)->strm.next_in = NULL; |
---|
78 | DATA(io)->strm.avail_in = 0; |
---|
79 | DATA(io)->strm.next_out = NULL; |
---|
80 | DATA(io)->strm.avail_out = 0; |
---|
81 | DATA(io)->strm.bzalloc = NULL; |
---|
82 | DATA(io)->strm.bzfree = NULL; |
---|
83 | DATA(io)->strm.opaque = NULL; |
---|
84 | DATA(io)->err = ERR_OK; |
---|
85 | |
---|
86 | BZ2_bzDecompressInit(&DATA(io)->strm, |
---|
87 | 0, /* Verbosity */ |
---|
88 | 0); /* small */ |
---|
89 | |
---|
90 | return io; |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | static off_t bz_read(io_t *io, void *buffer, off_t len) |
---|
95 | { |
---|
96 | if (DATA(io)->err == ERR_EOF) |
---|
97 | return 0; /* EOF */ |
---|
98 | if (DATA(io)->err == ERR_ERROR) { |
---|
99 | errno=EIO; |
---|
100 | return -1; /* ERROR! */ |
---|
101 | } |
---|
102 | |
---|
103 | DATA(io)->strm.avail_out = len; |
---|
104 | DATA(io)->strm.next_out = buffer; |
---|
105 | |
---|
106 | while (DATA(io)->err == ERR_OK && DATA(io)->strm.avail_out > 0) { |
---|
107 | while (DATA(io)->strm.avail_in <= 0) { |
---|
108 | int bytes_read = wandio_read(DATA(io)->parent, |
---|
109 | DATA(io)->inbuff, |
---|
110 | sizeof(DATA(io)->inbuff)); |
---|
111 | if (bytes_read == 0) /* EOF */ |
---|
112 | return len-DATA(io)->strm.avail_out; |
---|
113 | if (bytes_read < 0) { /* Error */ |
---|
114 | /* Errno should already be set */ |
---|
115 | DATA(io)->err = ERR_ERROR; |
---|
116 | /* Return how much data we managed to read ok */ |
---|
117 | if (DATA(io)->strm.avail_out != (uint32_t)len) { |
---|
118 | return len-DATA(io)->strm.avail_out; |
---|
119 | } |
---|
120 | /* Now return error */ |
---|
121 | return -1; |
---|
122 | } |
---|
123 | DATA(io)->strm.next_in = DATA(io)->inbuff; |
---|
124 | DATA(io)->strm.avail_in = bytes_read; |
---|
125 | } |
---|
126 | /* Decompress some data into the output buffer */ |
---|
127 | int err=BZ2_bzDecompress(&DATA(io)->strm); |
---|
128 | switch(err) { |
---|
129 | case BZ_OK: |
---|
130 | DATA(io)->err = ERR_OK; |
---|
131 | break; |
---|
132 | case BZ_STREAM_END: |
---|
133 | DATA(io)->err = ERR_EOF; |
---|
134 | break; |
---|
135 | default: |
---|
136 | errno=EIO; |
---|
137 | DATA(io)->err = ERR_ERROR; |
---|
138 | } |
---|
139 | } |
---|
140 | /* Return the number of bytes decompressed */ |
---|
141 | return len-DATA(io)->strm.avail_out; |
---|
142 | } |
---|
143 | |
---|
144 | static void bz_close(io_t *io) |
---|
145 | { |
---|
146 | BZ2_bzDecompressEnd(&DATA(io)->strm); |
---|
147 | wandio_destroy(DATA(io)->parent); |
---|
148 | free(io->data); |
---|
149 | free(io); |
---|
150 | } |
---|
151 | |
---|
152 | io_source_t bz_source = { |
---|
153 | "bzip", |
---|
154 | bz_read, |
---|
155 | NULL, /* peek */ |
---|
156 | NULL, /* tell */ |
---|
157 | NULL, /* seek */ |
---|
158 | bz_close |
---|
159 | }; |
---|
160 | |
---|