2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-12-16 21:45:59 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2008-12-16 21:45:59 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* single bz2 archive handling (requires libbz2)
|
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-24 19:18:58 +01:00
|
|
|
#include "Bzip2ArchivePlugin.hxx"
|
2014-01-24 00:09:37 +01:00
|
|
|
#include "../ArchivePlugin.hxx"
|
|
|
|
#include "../ArchiveFile.hxx"
|
|
|
|
#include "../ArchiveVisitor.hxx"
|
2014-01-24 16:18:21 +01:00
|
|
|
#include "input/InputStream.hxx"
|
|
|
|
#include "input/InputPlugin.hxx"
|
2013-01-29 23:20:19 +01:00
|
|
|
#include "util/RefCount.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#include "util/Domain.hxx"
|
2013-10-21 10:26:53 +02:00
|
|
|
#include "fs/Traits.hxx"
|
|
|
|
|
|
|
|
#include <bzlib.h>
|
2008-12-16 21:45:59 +01:00
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_OLDER_BZIP2
|
2009-12-15 08:55:46 +01:00
|
|
|
#define BZ2_bzDecompressInit bzDecompressInit
|
|
|
|
#define BZ2_bzDecompress bzDecompress
|
2008-12-16 21:45:59 +01:00
|
|
|
#endif
|
|
|
|
|
2013-01-29 23:36:58 +01:00
|
|
|
class Bzip2ArchiveFile final : public ArchiveFile {
|
2013-01-29 23:26:51 +01:00
|
|
|
public:
|
2013-01-29 23:20:19 +01:00
|
|
|
RefCount ref;
|
2009-12-31 10:02:55 +01:00
|
|
|
|
2013-10-21 10:26:53 +02:00
|
|
|
std::string name;
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream *const istream;
|
2013-01-30 15:27:23 +01:00
|
|
|
|
2013-10-23 22:08:59 +02:00
|
|
|
Bzip2ArchiveFile(const char *path, InputStream *_is)
|
2013-01-30 15:27:23 +01:00
|
|
|
:ArchiveFile(bz2_archive_plugin),
|
2013-12-04 22:53:43 +01:00
|
|
|
name(PathTraitsUTF8::GetBase(path)),
|
2013-01-30 15:27:23 +01:00
|
|
|
istream(_is) {
|
|
|
|
// remove .bz2 suffix
|
2013-10-21 10:26:53 +02:00
|
|
|
const size_t len = name.length();
|
2013-01-30 15:27:23 +01:00
|
|
|
if (len > 4)
|
2013-10-21 10:26:53 +02:00
|
|
|
name.erase(len - 4);
|
2013-01-30 15:27:23 +01:00
|
|
|
}
|
2013-01-24 19:18:58 +01:00
|
|
|
|
2013-01-30 15:27:23 +01:00
|
|
|
~Bzip2ArchiveFile() {
|
2013-09-05 00:06:31 +02:00
|
|
|
istream->Close();
|
2013-01-30 15:27:23 +01:00
|
|
|
}
|
2013-01-24 19:18:58 +01:00
|
|
|
|
2013-01-29 23:36:58 +01:00
|
|
|
void Ref() {
|
|
|
|
ref.Increment();
|
|
|
|
}
|
|
|
|
|
2013-01-24 19:18:58 +01:00
|
|
|
void Unref() {
|
2013-01-29 23:20:19 +01:00
|
|
|
if (!ref.Decrement())
|
2013-01-24 19:18:58 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
delete this;
|
|
|
|
}
|
2013-01-29 23:36:58 +01:00
|
|
|
|
|
|
|
virtual void Close() override {
|
|
|
|
Unref();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Visit(ArchiveVisitor &visitor) override {
|
2013-10-21 10:26:53 +02:00
|
|
|
visitor.VisitArchiveEntry(name.c_str());
|
2013-01-29 23:36:58 +01:00
|
|
|
}
|
|
|
|
|
2013-10-23 22:08:59 +02:00
|
|
|
virtual InputStream *OpenStream(const char *path,
|
|
|
|
Mutex &mutex, Cond &cond,
|
|
|
|
Error &error) override;
|
2009-12-31 10:43:38 +01:00
|
|
|
};
|
|
|
|
|
2013-01-28 22:15:14 +01:00
|
|
|
struct Bzip2InputStream {
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream base;
|
2009-12-30 23:27:37 +01:00
|
|
|
|
2013-01-28 22:15:14 +01:00
|
|
|
Bzip2ArchiveFile *archive;
|
2009-12-15 21:09:13 +01:00
|
|
|
|
|
|
|
bool eof;
|
|
|
|
|
2009-12-15 08:55:46 +01:00
|
|
|
bz_stream bzstream;
|
2009-12-31 11:01:58 +01:00
|
|
|
|
|
|
|
char buffer[5000];
|
2013-01-28 22:19:01 +01:00
|
|
|
|
|
|
|
Bzip2InputStream(Bzip2ArchiveFile &context, const char *uri,
|
|
|
|
Mutex &mutex, Cond &cond);
|
|
|
|
~Bzip2InputStream();
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
bool Open(Error &error);
|
2013-01-28 22:19:01 +01:00
|
|
|
void Close();
|
2009-12-16 16:01:39 +01:00
|
|
|
};
|
2008-12-16 21:45:59 +01:00
|
|
|
|
2013-10-17 10:20:57 +02:00
|
|
|
extern const InputPlugin bz2_inputplugin;
|
2008-12-16 21:45:59 +01:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain bz2_domain("bz2");
|
|
|
|
|
2008-12-16 21:45:59 +01:00
|
|
|
/* single archive handling allocation helpers */
|
|
|
|
|
2013-01-28 22:19:01 +01:00
|
|
|
inline bool
|
2013-08-10 18:02:44 +02:00
|
|
|
Bzip2InputStream::Open(Error &error)
|
2008-12-16 21:45:59 +01:00
|
|
|
{
|
2013-01-28 22:19:01 +01:00
|
|
|
bzstream.bzalloc = nullptr;
|
|
|
|
bzstream.bzfree = nullptr;
|
|
|
|
bzstream.opaque = nullptr;
|
2009-11-14 23:53:04 +01:00
|
|
|
|
2013-01-28 22:19:01 +01:00
|
|
|
bzstream.next_in = (char *)buffer;
|
|
|
|
bzstream.avail_in = 0;
|
2008-12-16 21:45:59 +01:00
|
|
|
|
2013-01-28 22:19:01 +01:00
|
|
|
int ret = BZ2_bzDecompressInit(&bzstream, 0, 0);
|
2009-11-14 23:53:04 +01:00
|
|
|
if (ret != BZ_OK) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(bz2_domain, ret,
|
|
|
|
"BZ2_bzDecompressInit() has failed");
|
2008-12-16 21:45:59 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-28 22:19:01 +01:00
|
|
|
base.ready = true;
|
2008-12-16 21:45:59 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-28 22:19:01 +01:00
|
|
|
inline void
|
|
|
|
Bzip2InputStream::Close()
|
2008-12-16 21:45:59 +01:00
|
|
|
{
|
2013-01-28 22:19:01 +01:00
|
|
|
BZ2_bzDecompressEnd(&bzstream);
|
2008-12-16 21:45:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* archive open && listing routine */
|
|
|
|
|
2013-01-29 23:26:51 +01:00
|
|
|
static ArchiveFile *
|
2013-08-10 18:02:44 +02:00
|
|
|
bz2_open(const char *pathname, Error &error)
|
2008-12-16 21:45:59 +01:00
|
|
|
{
|
2013-01-27 17:20:50 +01:00
|
|
|
static Mutex mutex;
|
|
|
|
static Cond cond;
|
2013-12-29 18:08:49 +01:00
|
|
|
InputStream *is = InputStream::OpenReady(pathname, mutex, cond, error);
|
2013-01-30 15:27:23 +01:00
|
|
|
if (is == nullptr)
|
|
|
|
return nullptr;
|
2009-12-15 08:55:46 +01:00
|
|
|
|
2013-01-30 15:27:23 +01:00
|
|
|
return new Bzip2ArchiveFile(pathname, is);
|
2008-12-16 21:45:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* single archive handling */
|
|
|
|
|
2013-01-28 22:19:01 +01:00
|
|
|
Bzip2InputStream::Bzip2InputStream(Bzip2ArchiveFile &_context, const char *uri,
|
|
|
|
Mutex &mutex, Cond &cond)
|
2013-01-28 20:32:23 +01:00
|
|
|
:base(bz2_inputplugin, uri, mutex, cond),
|
|
|
|
archive(&_context), eof(false)
|
2013-01-28 22:19:01 +01:00
|
|
|
{
|
2013-01-29 23:36:58 +01:00
|
|
|
archive->Ref();
|
2013-01-28 22:19:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Bzip2InputStream::~Bzip2InputStream()
|
|
|
|
{
|
2013-01-29 23:26:51 +01:00
|
|
|
archive->Unref();
|
2013-01-28 22:19:01 +01:00
|
|
|
}
|
|
|
|
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream *
|
2013-01-29 23:36:58 +01:00
|
|
|
Bzip2ArchiveFile::OpenStream(const char *path,
|
|
|
|
Mutex &mutex, Cond &cond,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2008-12-16 21:45:59 +01:00
|
|
|
{
|
2013-01-29 23:36:58 +01:00
|
|
|
Bzip2InputStream *bis = new Bzip2InputStream(*this, path, mutex, cond);
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!bis->Open(error)) {
|
2013-01-28 22:19:01 +01:00
|
|
|
delete bis;
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2009-12-31 10:43:38 +01:00
|
|
|
}
|
2009-12-15 08:55:46 +01:00
|
|
|
|
2009-12-30 23:27:37 +01:00
|
|
|
return &bis->base;
|
2008-12-16 21:45:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-10-23 22:08:59 +02:00
|
|
|
bz2_is_close(InputStream *is)
|
2008-12-16 21:45:59 +01:00
|
|
|
{
|
2013-01-28 22:15:14 +01:00
|
|
|
Bzip2InputStream *bis = (Bzip2InputStream *)is;
|
2009-12-31 10:43:38 +01:00
|
|
|
|
2013-01-28 22:19:01 +01:00
|
|
|
bis->Close();
|
|
|
|
delete bis;
|
2008-12-16 21:45:59 +01:00
|
|
|
}
|
|
|
|
|
2009-12-15 09:08:30 +01:00
|
|
|
static bool
|
2013-08-10 18:02:44 +02:00
|
|
|
bz2_fillbuffer(Bzip2InputStream *bis, Error &error)
|
2008-12-16 21:45:59 +01:00
|
|
|
{
|
|
|
|
size_t count;
|
|
|
|
bz_stream *bzstream;
|
|
|
|
|
2009-12-31 10:43:38 +01:00
|
|
|
bzstream = &bis->bzstream;
|
2008-12-16 21:45:59 +01:00
|
|
|
|
|
|
|
if (bzstream->avail_in > 0)
|
2009-12-15 09:08:30 +01:00
|
|
|
return true;
|
2008-12-16 21:45:59 +01:00
|
|
|
|
2013-09-05 00:06:31 +02:00
|
|
|
count = bis->archive->istream->Read(bis->buffer, sizeof(bis->buffer),
|
|
|
|
error);
|
2009-12-15 21:09:13 +01:00
|
|
|
if (count == 0)
|
|
|
|
return false;
|
2008-12-16 21:45:59 +01:00
|
|
|
|
2009-12-31 10:43:38 +01:00
|
|
|
bzstream->next_in = bis->buffer;
|
2009-12-15 21:09:13 +01:00
|
|
|
bzstream->avail_in = count;
|
2009-12-15 09:08:30 +01:00
|
|
|
return true;
|
2008-12-16 21:45:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2013-10-23 22:08:59 +02:00
|
|
|
bz2_is_read(InputStream *is, void *ptr, size_t length,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2008-12-16 21:45:59 +01:00
|
|
|
{
|
2013-01-28 22:15:14 +01:00
|
|
|
Bzip2InputStream *bis = (Bzip2InputStream *)is;
|
2008-12-16 21:45:59 +01:00
|
|
|
bz_stream *bzstream;
|
|
|
|
int bz_result;
|
2009-12-15 15:22:45 +01:00
|
|
|
size_t nbytes = 0;
|
2008-12-16 21:45:59 +01:00
|
|
|
|
2009-12-31 10:43:38 +01:00
|
|
|
if (bis->eof)
|
2008-12-16 21:45:59 +01:00
|
|
|
return 0;
|
|
|
|
|
2009-12-31 10:43:38 +01:00
|
|
|
bzstream = &bis->bzstream;
|
2013-01-24 19:18:58 +01:00
|
|
|
bzstream->next_out = (char *)ptr;
|
2009-12-15 15:22:45 +01:00
|
|
|
bzstream->avail_out = length;
|
2008-12-16 21:45:59 +01:00
|
|
|
|
2009-12-15 21:09:13 +01:00
|
|
|
do {
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!bz2_fillbuffer(bis, error))
|
2009-12-15 21:09:13 +01:00
|
|
|
return 0;
|
2008-12-16 21:45:59 +01:00
|
|
|
|
|
|
|
bz_result = BZ2_bzDecompress(bzstream);
|
|
|
|
|
2009-12-15 21:09:13 +01:00
|
|
|
if (bz_result == BZ_STREAM_END) {
|
2009-12-31 10:43:38 +01:00
|
|
|
bis->eof = true;
|
2008-12-16 21:45:59 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-12-15 21:09:13 +01:00
|
|
|
if (bz_result != BZ_OK) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(bz2_domain, bz_result,
|
|
|
|
"BZ2_bzDecompress() has failed");
|
2009-12-15 21:09:13 +01:00
|
|
|
return 0;
|
2008-12-16 21:45:59 +01:00
|
|
|
}
|
2009-12-15 21:09:13 +01:00
|
|
|
} while (bzstream->avail_out == length);
|
2008-12-16 21:45:59 +01:00
|
|
|
|
2009-12-15 15:22:45 +01:00
|
|
|
nbytes = length - bzstream->avail_out;
|
|
|
|
is->offset += nbytes;
|
2008-12-16 21:45:59 +01:00
|
|
|
|
2009-12-15 15:22:45 +01:00
|
|
|
return nbytes;
|
2008-12-16 21:45:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-10-23 22:08:59 +02:00
|
|
|
bz2_is_eof(InputStream *is)
|
2008-12-16 21:45:59 +01:00
|
|
|
{
|
2013-01-28 22:15:14 +01:00
|
|
|
Bzip2InputStream *bis = (Bzip2InputStream *)is;
|
2008-12-16 21:45:59 +01:00
|
|
|
|
2009-12-31 10:43:38 +01:00
|
|
|
return bis->eof;
|
2008-12-16 21:45:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* exported structures */
|
|
|
|
|
|
|
|
static const char *const bz2_extensions[] = {
|
|
|
|
"bz2",
|
2013-10-19 18:19:03 +02:00
|
|
|
nullptr
|
2008-12-16 21:45:59 +01:00
|
|
|
};
|
|
|
|
|
2013-10-17 10:20:57 +02:00
|
|
|
const InputPlugin bz2_inputplugin = {
|
2013-01-24 19:18:58 +01:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
bz2_is_close,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
bz2_is_read,
|
|
|
|
bz2_is_eof,
|
|
|
|
nullptr,
|
2008-12-16 21:45:59 +01:00
|
|
|
};
|
|
|
|
|
2009-12-16 16:04:16 +01:00
|
|
|
const struct archive_plugin bz2_archive_plugin = {
|
2013-01-24 19:18:58 +01:00
|
|
|
"bz2",
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
bz2_open,
|
|
|
|
bz2_extensions,
|
2008-12-16 21:45:59 +01:00
|
|
|
};
|
|
|
|
|