Bzip2ArchivePlugin.cxx 4.87 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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
#include "input/InputStream.hxx"
30
#include "input/LocalOpen.hxx"
31
#include "thread/Cond.hxx"
32
#include "util/RefCount.hxx"
33 34
#include "util/Error.hxx"
#include "util/Domain.hxx"
35
#include "fs/Path.hxx"
Max Kellermann's avatar
Max Kellermann committed
36 37

#include <bzlib.h>
38 39 40

#include <stddef.h>

41
class Bzip2ArchiveFile final : public ArchiveFile {
42
public:
43
	RefCount ref;
44

Max Kellermann's avatar
Max Kellermann committed
45
	std::string name;
46
	const InputStreamPtr istream;
47

48
	Bzip2ArchiveFile(Path path, InputStreamPtr &&_is)
49
		:ArchiveFile(bz2_archive_plugin),
50
		 name(path.GetBase().c_str()),
51
		 istream(std::move(_is)) {
52
		// remove .bz2 suffix
Max Kellermann's avatar
Max Kellermann committed
53
		const size_t len = name.length();
54
		if (len > 4)
Max Kellermann's avatar
Max Kellermann committed
55
			name.erase(len - 4);
56
	}
57

58 59 60 61
	void Ref() {
		ref.Increment();
	}

62
	void Unref() {
63
		if (!ref.Decrement())
64 65 66 67
			return;

		delete this;
	}
68 69 70 71 72 73

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

	virtual void Visit(ArchiveVisitor &visitor) override {
Max Kellermann's avatar
Max Kellermann committed
74
		visitor.VisitArchiveEntry(name.c_str());
75 76
	}

77 78 79
	virtual InputStream *OpenStream(const char *path,
					Mutex &mutex, Cond &cond,
					Error &error) override;
80 81
};

82
class Bzip2InputStream final : public InputStream {
83
	Bzip2ArchiveFile *archive;
84

85
	bool eof = false;
86

87
	bz_stream bzstream;
88 89

	char buffer[5000];
90

91
public:
92 93 94 95
	Bzip2InputStream(Bzip2ArchiveFile &context, const char *uri,
			 Mutex &mutex, Cond &cond);
	~Bzip2InputStream();

96
	bool Open(Error &error);
97 98 99 100

	/* virtual methods from InputStream */
	bool IsEOF() override;
	size_t Read(void *ptr, size_t size, Error &error) override;
101 102 103

private:
	bool FillBuffer(Error &error);
104
};
105

106 107
static constexpr Domain bz2_domain("bz2");

108 109
/* single archive handling allocation helpers */

110
inline bool
111
Bzip2InputStream::Open(Error &error)
112
{
113 114 115
	bzstream.bzalloc = nullptr;
	bzstream.bzfree = nullptr;
	bzstream.opaque = nullptr;
116

117 118
	bzstream.next_in = (char *)buffer;
	bzstream.avail_in = 0;
119

120
	int ret = BZ2_bzDecompressInit(&bzstream, 0, 0);
121
	if (ret != BZ_OK) {
122 123
		error.Set(bz2_domain, ret,
			  "BZ2_bzDecompressInit() has failed");
124 125 126
		return false;
	}

127
	SetReady();
128 129 130 131 132
	return true;
}

/* archive open && listing routine */

133
static ArchiveFile *
134
bz2_open(Path pathname, Error &error)
135
{
136 137
	static Mutex mutex;
	static Cond cond;
138
	auto is = OpenLocalInputStream(pathname, mutex, cond, error);
139 140
	if (is == nullptr)
		return nullptr;
141

142
	return new Bzip2ArchiveFile(pathname, std::move(is));
143 144 145 146
}

/* single archive handling */

147 148 149
Bzip2InputStream::Bzip2InputStream(Bzip2ArchiveFile &_context,
				   const char *_uri,
				   Mutex &_mutex, Cond &_cond)
150
	:InputStream(_uri, _mutex, _cond),
151
	 archive(&_context)
152
{
153
	archive->Ref();
154 155 156 157
}

Bzip2InputStream::~Bzip2InputStream()
{
158
	BZ2_bzDecompressEnd(&bzstream);
159
	archive->Unref();
160 161
}

162
InputStream *
163 164
Bzip2ArchiveFile::OpenStream(const char *path,
			     Mutex &mutex, Cond &cond,
165
			     Error &error)
166
{
167
	Bzip2InputStream *bis = new Bzip2InputStream(*this, path, mutex, cond);
168
	if (!bis->Open(error)) {
169
		delete bis;
170
		return nullptr;
171
	}
172

173
	return bis;
174 175
}

176 177
inline bool
Bzip2InputStream::FillBuffer(Error &error)
178
{
179
	if (bzstream.avail_in > 0)
180
		return true;
181

182 183
	size_t count = archive->istream->Read(buffer, sizeof(buffer),
					      error);
184 185
	if (count == 0)
		return false;
186

187 188
	bzstream.next_in = buffer;
	bzstream.avail_in = count;
189
	return true;
190 191
}

192 193
size_t
Bzip2InputStream::Read(void *ptr, size_t length, Error &error)
194 195
{
	int bz_result;
196
	size_t nbytes = 0;
197

198
	if (eof)
199 200
		return 0;

201 202
	bzstream.next_out = (char *)ptr;
	bzstream.avail_out = length;
203

204
	do {
205
		if (!FillBuffer(error))
206
			return 0;
207

208
		bz_result = BZ2_bzDecompress(&bzstream);
209

210
		if (bz_result == BZ_STREAM_END) {
211
			eof = true;
212 213 214
			break;
		}

215
		if (bz_result != BZ_OK) {
216 217
			error.Set(bz2_domain, bz_result,
				  "BZ2_bzDecompress() has failed");
218
			return 0;
219
		}
220
	} while (bzstream.avail_out == length);
221

222 223
	nbytes = length - bzstream.avail_out;
	offset += nbytes;
224

225
	return nbytes;
226 227
}

228 229
bool
Bzip2InputStream::IsEOF()
230
{
231
	return eof;
232 233 234 235 236 237
}

/* exported structures */

static const char *const bz2_extensions[] = {
	"bz2",
238
	nullptr
239 240
};

241
const ArchivePlugin bz2_archive_plugin = {
242 243 244 245 246
	"bz2",
	nullptr,
	nullptr,
	bz2_open,
	bz2_extensions,
247 248
};