]>
Commit | Line | Data |
---|---|---|
b24c88b3 RC |
1 | /* |
2 | * Copyright (c) 2001, Robert Collins. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * A copy of the GNU General Public License can be found at | |
10 | * http://www.gnu.org/ | |
11 | * | |
12 | * Written by Robert Collins <rbtcollins@hotmail.com> | |
13 | * | |
14 | */ | |
15 | ||
16 | /* Archive IO operations for bz2 files. | |
17 | derived from the fd convenience functions in the libbz2 package. | |
18 | */ | |
19 | ||
20 | #if 0 | |
21 | static const char *cvsid = | |
22 | "\n%%% $Id$\n"; | |
23 | #endif | |
24 | ||
25 | #include "win32.h" | |
26 | #include <stdio.h> | |
27 | #include <errno.h> | |
28 | #include "log.h" | |
29 | #include "port.h" | |
30 | ||
31 | #include "io_stream.h" | |
32 | #include "compress.h" | |
33 | #include "compress_bz.h" | |
34 | #undef _WIN32 | |
35 | #include "bzlib.h" | |
36 | ||
37 | compress_bz::compress_bz (io_stream * parent) : peeklen (0) | |
38 | { | |
39 | /* read only via this constructor */ | |
40 | original = 0; | |
41 | lasterr = 0; | |
42 | if (!parent || parent->error ()) | |
43 | { | |
44 | lasterr = EBADF; | |
45 | return; | |
46 | } | |
47 | original = parent; | |
48 | ||
49 | initialisedOk = 0; | |
50 | bufN = 0; | |
51 | writing = 0; | |
52 | strm.bzalloc = 0; | |
53 | strm.bzfree = 0; | |
54 | strm.opaque = 0; | |
55 | int ret = BZ2_bzDecompressInit (&(strm), 0, 0); | |
56 | if (ret) | |
57 | { | |
58 | lasterr = ret; | |
59 | return; | |
60 | } | |
61 | strm.avail_in = 0; | |
62 | strm.next_in = 0; | |
63 | initialisedOk = 1; | |
64 | } | |
65 | ||
66 | ssize_t compress_bz::read (void *buffer, size_t len) | |
67 | { | |
68 | if (!initialisedOk || writing) | |
69 | return EBADF; | |
70 | if (len == 0) | |
71 | return 0; | |
72 | ||
73 | if (peeklen) | |
74 | { | |
75 | ssize_t tmplen = min (peeklen, len); | |
76 | peeklen -= tmplen; | |
77 | memcpy (buffer, peekbuf, tmplen); | |
78 | memmove (peekbuf, peekbuf + tmplen, tmplen); | |
79 | ssize_t tmpread = read (&((char *) buffer)[tmplen], len - tmplen); | |
80 | if (tmpread >= 0) | |
81 | return tmpread + tmplen; | |
82 | else | |
83 | return tmpread; | |
84 | } | |
85 | ||
86 | strm.avail_out = len; | |
87 | strm.next_out = (char *) buffer; | |
88 | int | |
89 | rlen = 1; | |
90 | while (1) | |
91 | { | |
92 | if (original->error ()) | |
93 | { | |
94 | lasterr = original->error (); | |
95 | return -1; | |
96 | } | |
97 | if (strm.avail_in == 0 && rlen > 0) | |
98 | { | |
99 | rlen = original->read (buf, 4096); | |
100 | if (rlen < 0) | |
101 | { | |
102 | lasterr = rlen; | |
103 | return -1; | |
104 | } | |
105 | bufN = rlen; | |
106 | strm.avail_in = rlen; | |
107 | strm.next_in = buf; | |
108 | } | |
109 | int | |
110 | ret = BZ2_bzDecompress (&strm); | |
111 | if (ret != BZ_OK && ret != BZ_STREAM_END) | |
112 | { | |
113 | lasterr = ret; | |
114 | return -1; | |
115 | } | |
116 | if (ret == BZ_OK && rlen == 0 && strm.avail_out) | |
117 | { | |
118 | /* unexpected end of file */ | |
119 | lasterr = EIO; | |
120 | return -1; | |
121 | } | |
122 | if (ret == BZ_STREAM_END) | |
123 | return len - strm.avail_out; | |
124 | if (strm.avail_out == 0) | |
125 | return len; | |
126 | } | |
127 | ||
128 | /* not reached */ | |
129 | return 0; | |
130 | } | |
131 | ||
ca9506cc | 132 | ssize_t compress_bz::write (const void *buffer, size_t len) |
b24c88b3 RC |
133 | { |
134 | log (LOG_TIMESTAMP, "compress_bz::write called"); | |
135 | return 0; | |
136 | } | |
137 | ||
138 | ssize_t compress_bz::peek (void *buffer, size_t len) | |
139 | { | |
b24c88b3 RC |
140 | if (writing) |
141 | { | |
142 | lasterr = EBADF; | |
143 | return -1; | |
144 | } | |
145 | /* can only peek 512 bytes */ | |
146 | if (len > 512) | |
147 | return ENOMEM; | |
148 | ||
149 | if (len > peeklen) | |
150 | { | |
151 | size_t want = len - peeklen; | |
152 | ssize_t got = read (&peekbuf[peeklen], want); | |
153 | if (got >= 0) | |
154 | peeklen += got; | |
155 | else | |
156 | /* error */ | |
157 | return got; | |
158 | /* we may have read less than requested. */ | |
159 | memcpy (buffer, peekbuf, peeklen); | |
160 | return peeklen; | |
161 | } | |
162 | else | |
163 | { | |
164 | memcpy (buffer, peekbuf, len); | |
165 | return len; | |
166 | } | |
167 | return 0; | |
168 | } | |
169 | ||
170 | long | |
171 | compress_bz::tell () | |
172 | { | |
173 | log (LOG_TIMESTAMP, "compress_bz::tell called"); | |
174 | return 0; | |
175 | } | |
176 | ||
ca9506cc RC |
177 | int |
178 | compress_bz::seek (long where, io_stream_seek_t whence) | |
179 | { | |
180 | log (LOG_TIMESTAMP, "compress_bz::seek called"); | |
1ac649ed | 181 | return -1; |
ca9506cc RC |
182 | } |
183 | ||
b24c88b3 RC |
184 | int |
185 | compress_bz::error () | |
186 | { | |
187 | log (LOG_TIMESTAMP, "compress_bz::error called"); | |
188 | return 0; | |
189 | } | |
190 | ||
191 | int | |
192 | compress_bz::set_mtime (int time) | |
193 | { | |
194 | if (original) | |
195 | return original->set_mtime (time); | |
196 | return 1; | |
197 | } | |
198 | ||
199 | int | |
200 | compress_bz::get_mtime () | |
201 | { | |
202 | if (original) | |
203 | return original->get_mtime (); | |
204 | return 0; | |
205 | } | |
206 | ||
207 | compress_bz::~compress_bz () | |
208 | { | |
b24c88b3 RC |
209 | if (initialisedOk) |
210 | BZ2_bzDecompressEnd (&strm); | |
e9440f0f | 211 | destroyed = 1; |
b24c88b3 RC |
212 | return; |
213 | } |