Bzip2ArchivePlugin.cxx 5.54 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14 15 16 17
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 19 20 21 22 23
 */

/**
  * single bz2 archive handling (requires libbz2)
  */

24
#include "config.h"
25
#include "Bzip2ArchivePlugin.hxx"
26 27 28
#include "../ArchivePlugin.hxx"
#include "../ArchiveFile.hxx"
#include "../ArchiveVisitor.hxx"
Max Kellermann's avatar
Max Kellermann committed
29 30
#include "input/InputStream.hxx"
#include "input/InputPlugin.hxx"
31
#include "util/RefCount.hxx"
32 33
#include "util/Error.hxx"
#include "util/Domain.hxx"
Max Kellermann's avatar
Max Kellermann committed
34 35 36
#include "fs/Traits.hxx"

#include <bzlib.h>
37 38 39 40

#include <stddef.h>

#ifdef HAVE_OLDER_BZIP2
41 42
#define BZ2_bzDecompressInit bzDecompressInit
#define BZ2_bzDecompress bzDecompress
43 44
#endif

45
class Bzip2ArchiveFile final : public ArchiveFile {
46
public:
47
	RefCount ref;
48

Max Kellermann's avatar
Max Kellermann committed
49
	std::string name;
50
	InputStream *const istream;
51

52
	Bzip2ArchiveFile(const char *path, InputStream *_is)
53
		:ArchiveFile(bz2_archive_plugin),
54
		 name(PathTraitsUTF8::GetBase(path)),
55 56
		 istream(_is) {
		// remove .bz2 suffix
Max Kellermann's avatar
Max Kellermann committed
57
		const size_t len = name.length();
58
		if (len > 4)
Max Kellermann's avatar
Max Kellermann committed
59
			name.erase(len - 4);
60
	}
61

62
	~Bzip2ArchiveFile() {
63
		istream->Close();
64
	}
65

66 67 68 69
	void Ref() {
		ref.Increment();
	}

70
	void Unref() {
71
		if (!ref.Decrement())
72 73 74 75
			return;

		delete this;
	}
76 77 78 79 80 81

	virtual void Close() override {
		Unref();
	}

	virtual void Visit(ArchiveVisitor &visitor) override {
Max Kellermann's avatar
Max Kellermann committed
82
		visitor.VisitArchiveEntry(name.c_str());
83 84
	}

85 86 87
	virtual InputStream *OpenStream(const char *path,
					Mutex &mutex, Cond &cond,
					Error &error) override;
88 89
};

90
struct Bzip2InputStream {
91
	InputStream base;
92

93
	Bzip2ArchiveFile *archive;
94 95 96

	bool eof;

97
	bz_stream bzstream;
98 99

	char buffer[5000];
100 101 102 103 104

	Bzip2InputStream(Bzip2ArchiveFile &context, const char *uri,
			 Mutex &mutex, Cond &cond);
	~Bzip2InputStream();

105
	bool Open(Error &error);
106
	void Close();
107
};
108

109
extern const InputPlugin bz2_inputplugin;
110

111 112
static constexpr Domain bz2_domain("bz2");

113 114
/* single archive handling allocation helpers */

115
inline bool
116
Bzip2InputStream::Open(Error &error)
117
{
118 119 120
	bzstream.bzalloc = nullptr;
	bzstream.bzfree = nullptr;
	bzstream.opaque = nullptr;
121

122 123
	bzstream.next_in = (char *)buffer;
	bzstream.avail_in = 0;
124

125
	int ret = BZ2_bzDecompressInit(&bzstream, 0, 0);
126
	if (ret != BZ_OK) {
127 128
		error.Set(bz2_domain, ret,
			  "BZ2_bzDecompressInit() has failed");
129 130 131
		return false;
	}

132
	base.ready = true;
133 134 135
	return true;
}

136 137
inline void
Bzip2InputStream::Close()
138
{
139
	BZ2_bzDecompressEnd(&bzstream);
140 141 142 143
}

/* archive open && listing routine */

144
static ArchiveFile *
145
bz2_open(const char *pathname, Error &error)
146
{
147 148
	static Mutex mutex;
	static Cond cond;
149
	InputStream *is = InputStream::OpenReady(pathname, mutex, cond, error);
150 151
	if (is == nullptr)
		return nullptr;
152

153
	return new Bzip2ArchiveFile(pathname, is);
154 155 156 157
}

/* single archive handling */

158 159
Bzip2InputStream::Bzip2InputStream(Bzip2ArchiveFile &_context, const char *uri,
				   Mutex &mutex, Cond &cond)
160 161
	:base(bz2_inputplugin, uri, mutex, cond),
	 archive(&_context), eof(false)
162
{
163
	archive->Ref();
164 165 166 167
}

Bzip2InputStream::~Bzip2InputStream()
{
168
	archive->Unref();
169 170
}

171
InputStream *
172 173
Bzip2ArchiveFile::OpenStream(const char *path,
			     Mutex &mutex, Cond &cond,
174
			     Error &error)
175
{
176
	Bzip2InputStream *bis = new Bzip2InputStream(*this, path, mutex, cond);
177
	if (!bis->Open(error)) {
178
		delete bis;
179
		return nullptr;
180
	}
181

182
	return &bis->base;
183 184 185
}

static void
186
bz2_is_close(InputStream *is)
187
{
188
	Bzip2InputStream *bis = (Bzip2InputStream *)is;
189

190 191
	bis->Close();
	delete bis;
192 193
}

194
static bool
195
bz2_fillbuffer(Bzip2InputStream *bis, Error &error)
196 197 198 199
{
	size_t count;
	bz_stream *bzstream;

200
	bzstream = &bis->bzstream;
201 202

	if (bzstream->avail_in > 0)
203
		return true;
204

205 206
	count = bis->archive->istream->Read(bis->buffer, sizeof(bis->buffer),
					    error);
207 208
	if (count == 0)
		return false;
209

210
	bzstream->next_in = bis->buffer;
211
	bzstream->avail_in = count;
212
	return true;
213 214 215
}

static size_t
216
bz2_is_read(InputStream *is, void *ptr, size_t length,
217
	    Error &error)
218
{
219
	Bzip2InputStream *bis = (Bzip2InputStream *)is;
220 221
	bz_stream *bzstream;
	int bz_result;
222
	size_t nbytes = 0;
223

224
	if (bis->eof)
225 226
		return 0;

227
	bzstream = &bis->bzstream;
228
	bzstream->next_out = (char *)ptr;
229
	bzstream->avail_out = length;
230

231
	do {
232
		if (!bz2_fillbuffer(bis, error))
233
			return 0;
234 235 236

		bz_result = BZ2_bzDecompress(bzstream);

237
		if (bz_result == BZ_STREAM_END) {
238
			bis->eof = true;
239 240 241
			break;
		}

242
		if (bz_result != BZ_OK) {
243 244
			error.Set(bz2_domain, bz_result,
				  "BZ2_bzDecompress() has failed");
245
			return 0;
246
		}
247
	} while (bzstream->avail_out == length);
248

249 250
	nbytes = length - bzstream->avail_out;
	is->offset += nbytes;
251

252
	return nbytes;
253 254 255
}

static bool
256
bz2_is_eof(InputStream *is)
257
{
258
	Bzip2InputStream *bis = (Bzip2InputStream *)is;
259

260
	return bis->eof;
261 262 263 264 265 266
}

/* exported structures */

static const char *const bz2_extensions[] = {
	"bz2",
267
	nullptr
268 269
};

270
const InputPlugin bz2_inputplugin = {
271 272 273 274 275 276 277 278 279 280 281 282
	nullptr,
	nullptr,
	nullptr,
	nullptr,
	bz2_is_close,
	nullptr,
	nullptr,
	nullptr,
	nullptr,
	bz2_is_read,
	bz2_is_eof,
	nullptr,
283 284
};

285
const struct archive_plugin bz2_archive_plugin = {
286 287 288 289 290
	"bz2",
	nullptr,
	nullptr,
	bz2_open,
	bz2_extensions,
291 292
};