Bzip2ArchivePlugin.cxx 4.27 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
	bool eof = false;
63

64
	bz_stream bzstream;
65 66

	char buffer[5000];
67

68
public:
69
	Bzip2InputStream(std::shared_ptr<InputStream> _input,
70
			 const char *uri,
71
			 Mutex &mutex);
72
	~Bzip2InputStream() 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

/* single archive handling allocation helpers */

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

93 94
	bzstream.next_in = (char *)buffer;
	bzstream.avail_in = 0;
95

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

100
	SetReady();
101 102 103 104
}

/* archive open && listing routine */

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

/* single archive handling */

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

Bzip2InputStream::~Bzip2InputStream()
{
126
	BZ2_bzDecompressEnd(&bzstream);
127 128
}

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

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

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

146 147
	bzstream.next_in = buffer;
	bzstream.avail_in = count;
148
	return true;
149 150
}

151
size_t
152
Bzip2InputStream::Read(std::unique_lock<Mutex> &, void *ptr, size_t length)
153
{
154 155
	const ScopeUnlock unlock(mutex);

156
	int bz_result;
157
	size_t nbytes = 0;
158

159
	if (eof)
160 161
		return 0;

162 163
	bzstream.next_out = (char *)ptr;
	bzstream.avail_out = length;
164

165
	do {
166
		if (!FillBuffer())
167
			return 0;
168

169
		bz_result = BZ2_bzDecompress(&bzstream);
170

171
		if (bz_result == BZ_STREAM_END) {
172
			eof = true;
173 174 175
			break;
		}

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

180 181
	nbytes = length - bzstream.avail_out;
	offset += nbytes;
182

183
	return nbytes;
184 185
}

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

/* exported structures */

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

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