Bzip2ArchivePlugin.cxx 4.44 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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
#include "fs/Path.hxx"
Max Kellermann's avatar
Max Kellermann committed
34 35

#include <bzlib.h>
36

37 38
#include <stdexcept>

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
	InputStream *OpenStream(const char *path,
				Mutex &mutex, Cond &cond) override;
79 80
};

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

84
	bool eof = false;
85

86
	bz_stream bzstream;
87 88

	char buffer[5000];
89

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

95 96
	/* virtual methods from InputStream */
	bool IsEOF() override;
97
	size_t Read(void *ptr, size_t size) override;
98 99

private:
100
	void Open();
101
	bool FillBuffer();
102
};
103 104 105

/* single archive handling allocation helpers */

106 107
inline void
Bzip2InputStream::Open()
108
{
109 110 111
	bzstream.bzalloc = nullptr;
	bzstream.bzfree = nullptr;
	bzstream.opaque = nullptr;
112

113 114
	bzstream.next_in = (char *)buffer;
	bzstream.avail_in = 0;
115

116
	int ret = BZ2_bzDecompressInit(&bzstream, 0, 0);
117 118
	if (ret != BZ_OK)
		throw std::runtime_error("BZ2_bzDecompressInit() has failed");
119

120
	SetReady();
121 122 123 124
}

/* archive open && listing routine */

125
static ArchiveFile *
126
bz2_open(Path pathname)
127
{
128 129
	static Mutex mutex;
	static Cond cond;
130
	auto is = OpenLocalInputStream(pathname, mutex, cond);
131
	return new Bzip2ArchiveFile(pathname, std::move(is));
132 133 134 135
}

/* single archive handling */

136 137 138
Bzip2InputStream::Bzip2InputStream(Bzip2ArchiveFile &_context,
				   const char *_uri,
				   Mutex &_mutex, Cond &_cond)
139
	:InputStream(_uri, _mutex, _cond),
140
	 archive(&_context)
141
{
142
	Open();
143
	archive->Ref();
144 145 146 147
}

Bzip2InputStream::~Bzip2InputStream()
{
148
	BZ2_bzDecompressEnd(&bzstream);
149
	archive->Unref();
150 151
}

152
InputStream *
153
Bzip2ArchiveFile::OpenStream(const char *path,
154
			     Mutex &mutex, Cond &cond)
155
{
156
	return new Bzip2InputStream(*this, path, mutex, cond);
157 158
}

159
inline bool
160
Bzip2InputStream::FillBuffer()
161
{
162
	if (bzstream.avail_in > 0)
163
		return true;
164

165
	size_t count = archive->istream->Read(buffer, sizeof(buffer));
166 167
	if (count == 0)
		return false;
168

169 170
	bzstream.next_in = buffer;
	bzstream.avail_in = count;
171
	return true;
172 173
}

174
size_t
175
Bzip2InputStream::Read(void *ptr, size_t length)
176 177
{
	int bz_result;
178
	size_t nbytes = 0;
179

180
	if (eof)
181 182
		return 0;

183 184
	bzstream.next_out = (char *)ptr;
	bzstream.avail_out = length;
185

186
	do {
187
		if (!FillBuffer())
188
			return 0;
189

190
		bz_result = BZ2_bzDecompress(&bzstream);
191

192
		if (bz_result == BZ_STREAM_END) {
193
			eof = true;
194 195 196
			break;
		}

197 198
		if (bz_result != BZ_OK)
			throw std::runtime_error("BZ2_bzDecompress() has failed");
199
	} while (bzstream.avail_out == length);
200

201 202
	nbytes = length - bzstream.avail_out;
	offset += nbytes;
203

204
	return nbytes;
205 206
}

207 208
bool
Bzip2InputStream::IsEOF()
209
{
210
	return eof;
211 212 213 214 215 216
}

/* exported structures */

static const char *const bz2_extensions[] = {
	"bz2",
217
	nullptr
218 219
};

220
const ArchivePlugin bz2_archive_plugin = {
221 222 223 224 225
	"bz2",
	nullptr,
	nullptr,
	bz2_open,
	bz2_extensions,
226 227
};