Bzip2ArchivePlugin.cxx 4.16 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "Bzip2ArchivePlugin.hxx"
25 26 27
#include "../ArchivePlugin.hxx"
#include "../ArchiveFile.hxx"
#include "../ArchiveVisitor.hxx"
Max Kellermann's avatar
Max Kellermann committed
28
#include "input/InputStream.hxx"
29
#include "input/LocalOpen.hxx"
30
#include "fs/Path.hxx"
Max Kellermann's avatar
Max Kellermann committed
31 32

#include <bzlib.h>
33

34 35
#include <stdexcept>

36
class Bzip2ArchiveFile final : public ArchiveFile {
Max Kellermann's avatar
Max Kellermann committed
37
	std::string name;
38
	std::shared_ptr<InputStream> istream;
39

40
public:
41
	Bzip2ArchiveFile(Path path, InputStreamPtr &&_is)
42
		:name(path.GetBase().c_str()),
43
		 istream(std::move(_is)) {
44
		// remove .bz2 suffix
Max Kellermann's avatar
Max Kellermann committed
45
		const size_t len = name.length();
46
		if (len > 4)
Max Kellermann's avatar
Max Kellermann committed
47
			name.erase(len - 4);
48
	}
49

50
	virtual void Visit(ArchiveVisitor &visitor) override {
Max Kellermann's avatar
Max Kellermann committed
51
		visitor.VisitArchiveEntry(name.c_str());
52 53
	}

54
	InputStreamPtr OpenStream(const char *path,
55
				  Mutex &mutex) override;
56 57
};

58
class Bzip2InputStream final : public InputStream {
59
	std::shared_ptr<InputStream> input;
60

61
	bool eof = false;
62

63
	bz_stream bzstream;
64 65

	char buffer[5000];
66

67
public:
68 69
	Bzip2InputStream(const std::shared_ptr<InputStream> &_input,
			 const char *uri,
70
			 Mutex &mutex);
71 72
	~Bzip2InputStream();

73
	/* virtual methods from InputStream */
74
	bool IsEOF() noexcept override;
75
	size_t Read(void *ptr, size_t size) override;
76 77

private:
78
	void Open();
79
	bool FillBuffer();
80
};
81 82 83

/* single archive handling allocation helpers */

84 85
inline void
Bzip2InputStream::Open()
86
{
87 88 89
	bzstream.bzalloc = nullptr;
	bzstream.bzfree = nullptr;
	bzstream.opaque = nullptr;
90

91 92
	bzstream.next_in = (char *)buffer;
	bzstream.avail_in = 0;
93

94
	int ret = BZ2_bzDecompressInit(&bzstream, 0, 0);
95 96
	if (ret != BZ_OK)
		throw std::runtime_error("BZ2_bzDecompressInit() has failed");
97

98
	SetReady();
99 100 101 102
}

/* archive open && listing routine */

103
static std::unique_ptr<ArchiveFile>
104
bz2_open(Path pathname)
105
{
106
	static Mutex mutex;
107
	auto is = OpenLocalInputStream(pathname, mutex);
108
	return std::make_unique<Bzip2ArchiveFile>(pathname, std::move(is));
109 110 111 112
}

/* single archive handling */

113
Bzip2InputStream::Bzip2InputStream(const std::shared_ptr<InputStream> &_input,
114
				   const char *_uri,
115 116
				   Mutex &_mutex)
	:InputStream(_uri, _mutex),
117
	 input(_input)
118
{
119
	Open();
120 121 122 123
}

Bzip2InputStream::~Bzip2InputStream()
{
124
	BZ2_bzDecompressEnd(&bzstream);
125 126
}

127
InputStreamPtr
128
Bzip2ArchiveFile::OpenStream(const char *path,
129
			     Mutex &mutex)
130
{
131
	return std::make_unique<Bzip2InputStream>(istream, path, mutex);
132 133
}

134
inline bool
135
Bzip2InputStream::FillBuffer()
136
{
137
	if (bzstream.avail_in > 0)
138
		return true;
139

140
	size_t count = input->LockRead(buffer, sizeof(buffer));
141 142
	if (count == 0)
		return false;
143

144 145
	bzstream.next_in = buffer;
	bzstream.avail_in = count;
146
	return true;
147 148
}

149
size_t
150
Bzip2InputStream::Read(void *ptr, size_t length)
151
{
152 153
	const ScopeUnlock unlock(mutex);

154
	int bz_result;
155
	size_t nbytes = 0;
156

157
	if (eof)
158 159
		return 0;

160 161
	bzstream.next_out = (char *)ptr;
	bzstream.avail_out = length;
162

163
	do {
164
		if (!FillBuffer())
165
			return 0;
166

167
		bz_result = BZ2_bzDecompress(&bzstream);
168

169
		if (bz_result == BZ_STREAM_END) {
170
			eof = true;
171 172 173
			break;
		}

174 175
		if (bz_result != BZ_OK)
			throw std::runtime_error("BZ2_bzDecompress() has failed");
176
	} while (bzstream.avail_out == length);
177

178 179
	nbytes = length - bzstream.avail_out;
	offset += nbytes;
180

181
	return nbytes;
182 183
}

184
bool
185
Bzip2InputStream::IsEOF() noexcept
186
{
187
	return eof;
188 189 190 191 192 193
}

/* exported structures */

static const char *const bz2_extensions[] = {
	"bz2",
194
	nullptr
195 196
};

197
const ArchivePlugin bz2_archive_plugin = {
198 199 200 201 202
	"bz2",
	nullptr,
	nullptr,
	bz2_open,
	bz2_extensions,
203
};