2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2013-01-24 19:18:58 +01:00
|
|
|
* Copyright (C) 2003-2013 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"
|
|
|
|
#include "ArchiveInternal.hxx"
|
|
|
|
#include "ArchivePlugin.hxx"
|
2013-01-29 21:21:07 +01:00
|
|
|
#include "ArchiveVisitor.hxx"
|
2013-01-25 22:43:01 +01:00
|
|
|
#include "InputInternal.hxx"
|
2013-01-24 19:14:40 +01:00
|
|
|
#include "InputStream.hxx"
|
2013-01-25 22:43:01 +01:00
|
|
|
#include "InputPlugin.hxx"
|
2013-01-29 23:20:19 +01:00
|
|
|
#include "util/RefCount.hxx"
|
2008-12-16 21:45:59 +01:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <glib.h>
|
|
|
|
#include <bzlib.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-28 22:15:14 +01:00
|
|
|
struct Bzip2ArchiveFile {
|
2009-12-16 16:28:26 +01:00
|
|
|
struct archive_file base;
|
|
|
|
|
2013-01-29 23:20:19 +01:00
|
|
|
RefCount ref;
|
2009-12-31 10:02:55 +01:00
|
|
|
|
2009-12-15 08:55:46 +01:00
|
|
|
char *name;
|
2009-12-30 23:27:37 +01:00
|
|
|
struct input_stream *istream;
|
2013-01-24 19:18:58 +01:00
|
|
|
|
2013-01-28 22:15:14 +01:00
|
|
|
Bzip2ArchiveFile() {
|
2013-01-24 19:18:58 +01:00
|
|
|
archive_file_init(&base, &bz2_archive_plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unref() {
|
2013-01-29 23:20:19 +01:00
|
|
|
if (!ref.Decrement())
|
2013-01-24 19:18:58 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
g_free(name);
|
|
|
|
|
|
|
|
input_stream_close(istream);
|
|
|
|
delete this;
|
|
|
|
}
|
2009-12-31 10:43:38 +01:00
|
|
|
};
|
|
|
|
|
2013-01-28 22:15:14 +01:00
|
|
|
struct Bzip2InputStream {
|
2009-12-30 23:27:37 +01:00
|
|
|
struct input_stream base;
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
bool Open(GError **error_r);
|
|
|
|
void Close();
|
2009-12-16 16:01:39 +01:00
|
|
|
};
|
2008-12-16 21:45:59 +01:00
|
|
|
|
2013-01-24 19:18:58 +01:00
|
|
|
extern const struct input_plugin bz2_inputplugin;
|
2008-12-16 21:45:59 +01:00
|
|
|
|
2009-11-14 23:53:04 +01:00
|
|
|
static inline GQuark
|
|
|
|
bz2_quark(void)
|
|
|
|
{
|
|
|
|
return g_quark_from_static_string("bz2");
|
|
|
|
}
|
|
|
|
|
2008-12-16 21:45:59 +01:00
|
|
|
/* single archive handling allocation helpers */
|
|
|
|
|
2013-01-28 22:19:01 +01:00
|
|
|
inline bool
|
|
|
|
Bzip2InputStream::Open(GError **error_r)
|
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) {
|
|
|
|
g_set_error(error_r, bz2_quark(), 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 */
|
|
|
|
|
|
|
|
static struct archive_file *
|
2009-12-16 16:40:22 +01:00
|
|
|
bz2_open(const char *pathname, GError **error_r)
|
2008-12-16 21:45:59 +01:00
|
|
|
{
|
2013-01-28 22:15:14 +01:00
|
|
|
Bzip2ArchiveFile *context = new Bzip2ArchiveFile();
|
2008-12-16 21:45:59 +01:00
|
|
|
int len;
|
|
|
|
|
|
|
|
//open archive
|
2013-01-27 17:20:50 +01:00
|
|
|
static Mutex mutex;
|
|
|
|
static Cond cond;
|
|
|
|
context->istream = input_stream_open(pathname, mutex, cond,
|
2011-09-14 21:46:41 +02:00
|
|
|
error_r);
|
2009-12-30 23:27:37 +01:00
|
|
|
if (context->istream == NULL) {
|
2013-01-24 19:18:58 +01:00
|
|
|
delete context;
|
2008-12-16 21:45:59 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-12-15 08:55:46 +01:00
|
|
|
|
2009-12-16 16:09:10 +01:00
|
|
|
context->name = g_path_get_basename(pathname);
|
2009-12-15 08:55:46 +01:00
|
|
|
|
2008-12-16 21:45:59 +01:00
|
|
|
//remove suffix
|
|
|
|
len = strlen(context->name);
|
|
|
|
if (len > 4) {
|
2009-12-15 08:55:46 +01:00
|
|
|
context->name[len - 4] = 0; //remove .bz2 suffix
|
2008-12-16 21:45:59 +01:00
|
|
|
}
|
2009-12-15 08:55:46 +01:00
|
|
|
|
2009-12-16 16:28:26 +01:00
|
|
|
return &context->base;
|
2008-12-16 21:45:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-01-29 21:21:07 +01:00
|
|
|
bz2_visit(archive_file *file, ArchiveVisitor &visitor)
|
2008-12-16 21:45:59 +01:00
|
|
|
{
|
2013-01-28 22:15:14 +01:00
|
|
|
Bzip2ArchiveFile *context = (Bzip2ArchiveFile *) file;
|
2009-12-15 08:55:46 +01:00
|
|
|
|
2013-01-29 21:21:07 +01:00
|
|
|
visitor.VisitArchiveEntry(context->name);
|
2008-12-16 21:45:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bz2_close(struct archive_file *file)
|
|
|
|
{
|
2013-01-28 22:15:14 +01:00
|
|
|
Bzip2ArchiveFile *context = (Bzip2ArchiveFile *) file;
|
2009-12-15 08:55:46 +01:00
|
|
|
|
2013-01-24 19:18:58 +01:00
|
|
|
context->Unref();
|
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:20:19 +01:00
|
|
|
archive->ref.Increment();
|
2013-01-28 22:19:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Bzip2InputStream::~Bzip2InputStream()
|
|
|
|
{
|
|
|
|
bz2_close(&archive->base);
|
|
|
|
}
|
|
|
|
|
2009-12-30 23:27:37 +01:00
|
|
|
static struct input_stream *
|
2011-09-14 21:46:41 +02:00
|
|
|
bz2_open_stream(struct archive_file *file, const char *path,
|
2013-01-27 17:20:50 +01:00
|
|
|
Mutex &mutex, Cond &cond,
|
2011-09-14 21:46:41 +02:00
|
|
|
GError **error_r)
|
2008-12-16 21:45:59 +01:00
|
|
|
{
|
2013-01-28 22:15:14 +01:00
|
|
|
Bzip2ArchiveFile *context = (Bzip2ArchiveFile *) file;
|
2013-01-28 22:19:01 +01:00
|
|
|
Bzip2InputStream *bis =
|
|
|
|
new Bzip2InputStream(*context, path, mutex, cond);
|
2008-12-16 21:45:59 +01:00
|
|
|
|
2013-01-28 22:19:01 +01:00
|
|
|
if (!bis->Open(error_r)) {
|
|
|
|
delete bis;
|
2009-12-30 23:27:37 +01:00
|
|
|
return NULL;
|
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
|
|
|
|
bz2_is_close(struct input_stream *is)
|
|
|
|
{
|
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-01-28 22:15:14 +01:00
|
|
|
bz2_fillbuffer(Bzip2InputStream *bis, GError **error_r)
|
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
|
|
|
|
2009-12-30 23:27:37 +01:00
|
|
|
count = input_stream_read(bis->archive->istream,
|
2009-12-31 11:01:58 +01:00
|
|
|
bis->buffer, sizeof(bis->buffer),
|
2009-11-14 23:53:04 +01:00
|
|
|
error_r);
|
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
|
2009-11-14 23:53:04 +01:00
|
|
|
bz2_is_read(struct input_stream *is, void *ptr, size_t length,
|
|
|
|
GError **error_r)
|
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 {
|
2009-12-31 10:43:38 +01:00
|
|
|
if (!bz2_fillbuffer(bis, error_r))
|
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) {
|
2009-11-14 23:53:04 +01:00
|
|
|
g_set_error(error_r, bz2_quark(), 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
|
|
|
|
bz2_is_eof(struct input_stream *is)
|
|
|
|
{
|
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",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2013-01-24 19:18:58 +01:00
|
|
|
const struct input_plugin bz2_inputplugin = {
|
|
|
|
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,
|
2013-01-29 21:21:07 +01:00
|
|
|
bz2_visit,
|
2013-01-24 19:18:58 +01:00
|
|
|
bz2_open_stream,
|
|
|
|
bz2_close,
|
|
|
|
bz2_extensions,
|
2008-12-16 21:45:59 +01:00
|
|
|
};
|
|
|
|
|