Bzip2ArchivePlugin.cxx 4.18 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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
#include <stdexcept>
35
#include <utility>
36

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

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

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

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

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

62
	bz_stream bzstream{};
63

64
	bool eof = false;
65 66

	char buffer[5000];
67

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

74
	/* virtual methods from InputStream */
75
	[[nodiscard]] bool IsEOF() const noexcept override;
76 77
	size_t Read(std::unique_lock<Mutex> &lock,
		    void *ptr, size_t size) override;
78 79

private:
80
	void Open();
81
	bool FillBuffer();
82
};
83 84 85

/* archive open && listing routine */

86
static std::unique_ptr<ArchiveFile>
87
bz2_open(Path pathname)
88
{
89
	static Mutex mutex;
90
	auto is = OpenLocalInputStream(pathname, mutex);
91
	return std::make_unique<Bzip2ArchiveFile>(pathname, std::move(is));
92 93 94 95
}

/* single archive handling */

96
Bzip2InputStream::Bzip2InputStream(std::shared_ptr<InputStream> _input,
97
				   const char *_uri,
98 99
				   Mutex &_mutex)
	:InputStream(_uri, _mutex),
100
	 input(std::move(_input))
101
{
102 103 104 105 106 107 108
	bzstream.next_in = (char *)buffer;

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

	SetReady();
109 110
}

111
Bzip2InputStream::~Bzip2InputStream() noexcept
112
{
113
	BZ2_bzDecompressEnd(&bzstream);
114 115
}

116
InputStreamPtr
117
Bzip2ArchiveFile::OpenStream(const char *path,
118
			     Mutex &mutex)
119
{
120
	return std::make_unique<Bzip2InputStream>(istream, path, mutex);
121 122
}

123
inline bool
124
Bzip2InputStream::FillBuffer()
125
{
126
	if (bzstream.avail_in > 0)
127
		return true;
128

129
	size_t count = input->LockRead(buffer, sizeof(buffer));
130 131
	if (count == 0)
		return false;
132

133 134
	bzstream.next_in = buffer;
	bzstream.avail_in = count;
135
	return true;
136 137
}

138
size_t
139
Bzip2InputStream::Read(std::unique_lock<Mutex> &, void *ptr, size_t length)
140
{
141
	if (eof)
142 143
		return 0;

144 145
	const ScopeUnlock unlock(mutex);

146 147
	bzstream.next_out = (char *)ptr;
	bzstream.avail_out = length;
148

149
	do {
150
		const bool had_input = FillBuffer();
151

152
		const int bz_result = BZ2_bzDecompress(&bzstream);
153

154
		if (bz_result == BZ_STREAM_END) {
155
			eof = true;
156 157 158
			break;
		}

159 160
		if (bz_result != BZ_OK)
			throw std::runtime_error("BZ2_bzDecompress() has failed");
161 162 163

		if (!had_input && bzstream.avail_out == length)
			throw std::runtime_error("Unexpected end of bzip2 file");
164
	} while (bzstream.avail_out == length);
165

166
	const size_t nbytes = length - bzstream.avail_out;
167
	offset += nbytes;
168

169
	return nbytes;
170 171
}

172
bool
173
Bzip2InputStream::IsEOF() const noexcept
174
{
175
	return eof;
176 177 178 179 180 181
}

/* exported structures */

static const char *const bz2_extensions[] = {
	"bz2",
182
	nullptr
183 184
};

185
const ArchivePlugin bz2_archive_plugin = {
186 187 188 189 190
	"bz2",
	nullptr,
	nullptr,
	bz2_open,
	bz2_extensions,
191
};