Bzip2ArchivePlugin.cxx 5.1 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
#include "fs/Traits.hxx"
35
#include "fs/Path.hxx"
Max Kellermann's avatar
Max Kellermann committed
36 37

#include <bzlib.h>
38 39 40 41

#include <stddef.h>

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

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

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

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

63
	~Bzip2ArchiveFile() {
64
		delete istream;
65
	}
66

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

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

		delete this;
	}
77 78 79 80 81 82

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

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

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

91
struct Bzip2InputStream final : public InputStream {
92
	Bzip2ArchiveFile *archive;
93 94 95

	bool eof;

96
	bz_stream bzstream;
97 98

	char buffer[5000];
99 100 101 102 103

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

104
	bool Open(Error &error);
105 106 107 108

	/* virtual methods from InputStream */
	bool IsEOF() override;
	size_t Read(void *ptr, size_t size, Error &error) override;
109
};
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
	SetReady();
133 134 135 136 137
	return true;
}

/* archive open && listing routine */

138
static ArchiveFile *
139
bz2_open(Path pathname, Error &error)
140
{
141 142
	static Mutex mutex;
	static Cond cond;
143 144
	InputStream *is = InputStream::OpenReady(pathname.c_str(), mutex, cond,
						 error);
145 146
	if (is == nullptr)
		return nullptr;
147

148
	return new Bzip2ArchiveFile(pathname, is);
149 150 151 152
}

/* single archive handling */

153 154 155
Bzip2InputStream::Bzip2InputStream(Bzip2ArchiveFile &_context,
				   const char *_uri,
				   Mutex &_mutex, Cond &_cond)
156
	:InputStream(_uri, _mutex, _cond),
157
	 archive(&_context), eof(false)
158
{
159
	archive->Ref();
160 161 162 163
}

Bzip2InputStream::~Bzip2InputStream()
{
164
	BZ2_bzDecompressEnd(&bzstream);
165
	archive->Unref();
166 167
}

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

179
	return bis;
180 181
}

182
static bool
183
bz2_fillbuffer(Bzip2InputStream *bis, Error &error)
184 185 186 187
{
	size_t count;
	bz_stream *bzstream;

188
	bzstream = &bis->bzstream;
189 190

	if (bzstream->avail_in > 0)
191
		return true;
192

193 194
	count = bis->archive->istream->Read(bis->buffer, sizeof(bis->buffer),
					    error);
195 196
	if (count == 0)
		return false;
197

198
	bzstream->next_in = bis->buffer;
199
	bzstream->avail_in = count;
200
	return true;
201 202
}

203 204
size_t
Bzip2InputStream::Read(void *ptr, size_t length, Error &error)
205 206
{
	int bz_result;
207
	size_t nbytes = 0;
208

209
	if (eof)
210 211
		return 0;

212 213
	bzstream.next_out = (char *)ptr;
	bzstream.avail_out = length;
214

215
	do {
216
		if (!bz2_fillbuffer(this, error))
217
			return 0;
218

219
		bz_result = BZ2_bzDecompress(&bzstream);
220

221
		if (bz_result == BZ_STREAM_END) {
222
			eof = true;
223 224 225
			break;
		}

226
		if (bz_result != BZ_OK) {
227 228
			error.Set(bz2_domain, bz_result,
				  "BZ2_bzDecompress() has failed");
229
			return 0;
230
		}
231
	} while (bzstream.avail_out == length);
232

233 234
	nbytes = length - bzstream.avail_out;
	offset += nbytes;
235

236
	return nbytes;
237 238
}

239 240
bool
Bzip2InputStream::IsEOF()
241
{
242
	return eof;
243 244 245 246 247 248
}

/* exported structures */

static const char *const bz2_extensions[] = {
	"bz2",
249
	nullptr
250 251
};

252
const ArchivePlugin bz2_archive_plugin = {
253 254 255 256 257
	"bz2",
	nullptr,
	nullptr,
	bz2_open,
	bz2_extensions,
258 259
};