Bzip2ArchivePlugin.cxx 4.3 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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 75 76
	Bzip2InputStream(const Bzip2InputStream &) = delete;
	Bzip2InputStream &operator=(const Bzip2InputStream &) = delete;

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

private:
83
	void Open();
84
	bool FillBuffer();
85
};
86 87 88

/* archive open && listing routine */

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

/* single archive handling */

99
Bzip2InputStream::Bzip2InputStream(std::shared_ptr<InputStream> _input,
100
				   const char *_uri,
101 102
				   Mutex &_mutex)
	:InputStream(_uri, _mutex),
103
	 input(std::move(_input))
104
{
105 106 107 108 109 110 111
	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();
112 113
}

114
Bzip2InputStream::~Bzip2InputStream() noexcept
115
{
116
	BZ2_bzDecompressEnd(&bzstream);
117 118
}

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

126
inline bool
127
Bzip2InputStream::FillBuffer()
128
{
129
	if (bzstream.avail_in > 0)
130
		return true;
131

132
	size_t count = input->LockRead(buffer, sizeof(buffer));
133 134
	if (count == 0)
		return false;
135

136 137
	bzstream.next_in = buffer;
	bzstream.avail_in = count;
138
	return true;
139 140
}

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

147 148
	const ScopeUnlock unlock(mutex);

149 150
	bzstream.next_out = (char *)ptr;
	bzstream.avail_out = length;
151

152
	do {
153
		const bool had_input = FillBuffer();
154

155
		const int bz_result = BZ2_bzDecompress(&bzstream);
156

157
		if (bz_result == BZ_STREAM_END) {
158
			eof = true;
159 160 161
			break;
		}

162 163
		if (bz_result != BZ_OK)
			throw std::runtime_error("BZ2_bzDecompress() has failed");
164 165 166

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

169
	const size_t nbytes = length - bzstream.avail_out;
170
	offset += nbytes;
171

172
	return nbytes;
173 174
}

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

/* exported structures */

183
static constexpr const char *bz2_extensions[] = {
184
	"bz2",
185
	nullptr
186 187
};

188
const ArchivePlugin bz2_archive_plugin = {
189 190 191 192 193
	"bz2",
	nullptr,
	nullptr,
	bz2_open,
	bz2_extensions,
194
};