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 "input/LocalOpen.hxx"
32
#include "util/RefCount.hxx"
33 34
#include "util/Error.hxx"
#include "util/Domain.hxx"
Max Kellermann's avatar
Max Kellermann committed
35
#include "fs/Traits.hxx"
36
#include "fs/Path.hxx"
Max Kellermann's avatar
Max Kellermann committed
37 38

#include <bzlib.h>
39 40 41 42

#include <stddef.h>

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

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

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

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

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

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

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

		delete this;
	}
78 79 80 81 82 83

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

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

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

92
struct Bzip2InputStream final : public InputStream {
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 107 108 109

	/* virtual methods from InputStream */
	bool IsEOF() override;
	size_t Read(void *ptr, size_t size, Error &error) override;
110
};
111

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

114 115
/* single archive handling allocation helpers */

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

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

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

133
	SetReady();
134 135 136 137 138
	return true;
}

/* archive open && listing routine */

139
static ArchiveFile *
140
bz2_open(Path pathname, Error &error)
141
{
142 143
	static Mutex mutex;
	static Cond cond;
144
	InputStream *is = OpenLocalInputStream(pathname, 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
};