2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2010-01-01 05:55:13 +01:00
|
|
|
* Copyright (C) 2003-2010 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"
|
2009-12-16 16:11:45 +01:00
|
|
|
#include "archive/bz2_archive_plugin.h"
|
2008-12-16 21:45:59 +01:00
|
|
|
#include "archive_api.h"
|
2009-03-02 20:13:08 +01:00
|
|
|
#include "input_plugin.h"
|
2009-12-31 10:02:55 +01:00
|
|
|
#include "refcount.h"
|
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
|
|
|
|
|
2009-12-16 16:01:39 +01:00
|
|
|
struct bz2_archive_file {
|
2009-12-16 16:28:26 +01:00
|
|
|
struct archive_file base;
|
|
|
|
|
2009-12-31 10:02:55 +01:00
|
|
|
struct refcount ref;
|
|
|
|
|
2009-12-15 08:55:46 +01:00
|
|
|
char *name;
|
|
|
|
bool reset;
|
2008-12-16 21:45:59 +01:00
|
|
|
struct input_stream istream;
|
2009-12-31 10:43:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct bz2_input_stream {
|
|
|
|
struct bz2_archive_file *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];
|
2009-12-16 16:01:39 +01:00
|
|
|
};
|
2008-12-16 21:45:59 +01:00
|
|
|
|
|
|
|
static const struct input_plugin bz2_inputplugin;
|
|
|
|
|
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 */
|
|
|
|
|
|
|
|
static bool
|
2009-12-31 10:43:38 +01:00
|
|
|
bz2_alloc(struct bz2_input_stream *data, GError **error_r)
|
2008-12-16 21:45:59 +01:00
|
|
|
{
|
2009-11-14 23:53:04 +01:00
|
|
|
int ret;
|
|
|
|
|
2008-12-16 21:45:59 +01:00
|
|
|
data->bzstream.bzalloc = NULL;
|
|
|
|
data->bzstream.bzfree = NULL;
|
|
|
|
data->bzstream.opaque = NULL;
|
|
|
|
|
|
|
|
data->bzstream.next_in = (void *) data->buffer;
|
|
|
|
data->bzstream.avail_in = 0;
|
|
|
|
|
2009-11-14 23:53:04 +01:00
|
|
|
ret = BZ2_bzDecompressInit(&data->bzstream, 0, 0);
|
|
|
|
if (ret != BZ_OK) {
|
2008-12-16 21:45:59 +01:00
|
|
|
g_free(data);
|
2009-11-14 23:53:04 +01:00
|
|
|
|
|
|
|
g_set_error(error_r, bz2_quark(), ret,
|
|
|
|
"BZ2_bzDecompressInit() has failed");
|
2008-12-16 21:45:59 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-12-31 10:43:38 +01:00
|
|
|
bz2_destroy(struct bz2_input_stream *data)
|
2008-12-16 21:45:59 +01:00
|
|
|
{
|
|
|
|
BZ2_bzDecompressEnd(&data->bzstream);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
{
|
2009-12-16 16:01:39 +01:00
|
|
|
struct bz2_archive_file *context;
|
2008-12-16 21:45:59 +01:00
|
|
|
int len;
|
|
|
|
|
2009-12-15 09:00:18 +01:00
|
|
|
context = g_malloc(sizeof(*context));
|
2009-12-16 16:28:26 +01:00
|
|
|
archive_file_init(&context->base, &bz2_archive_plugin);
|
2009-12-31 10:02:55 +01:00
|
|
|
refcount_init(&context->ref);
|
2009-12-15 09:00:18 +01:00
|
|
|
|
2008-12-16 21:45:59 +01:00
|
|
|
//open archive
|
2009-12-16 16:40:22 +01:00
|
|
|
if (!input_stream_open(&context->istream, pathname, error_r)) {
|
2008-12-16 21:45:59 +01:00
|
|
|
g_free(context);
|
|
|
|
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
|
|
|
|
bz2_scan_reset(struct archive_file *file)
|
|
|
|
{
|
2009-12-16 16:01:39 +01:00
|
|
|
struct bz2_archive_file *context = (struct bz2_archive_file *) file;
|
2008-12-16 21:45:59 +01:00
|
|
|
context->reset = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
bz2_scan_next(struct archive_file *file)
|
|
|
|
{
|
2009-12-16 16:01:39 +01:00
|
|
|
struct bz2_archive_file *context = (struct bz2_archive_file *) file;
|
2008-12-16 21:45:59 +01:00
|
|
|
char *name = NULL;
|
2009-12-15 08:55:46 +01:00
|
|
|
|
2008-12-16 21:45:59 +01:00
|
|
|
if (context->reset) {
|
|
|
|
name = context->name;
|
|
|
|
context->reset = false;
|
|
|
|
}
|
2009-12-15 08:55:46 +01:00
|
|
|
|
2008-12-16 21:45:59 +01:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bz2_close(struct archive_file *file)
|
|
|
|
{
|
2009-12-16 16:01:39 +01:00
|
|
|
struct bz2_archive_file *context = (struct bz2_archive_file *) file;
|
2009-12-15 08:55:46 +01:00
|
|
|
|
2009-12-31 10:02:55 +01:00
|
|
|
if (!refcount_dec(&context->ref))
|
|
|
|
return;
|
|
|
|
|
2009-12-15 19:42:54 +01:00
|
|
|
g_free(context->name);
|
2008-12-16 21:45:59 +01:00
|
|
|
|
|
|
|
input_stream_close(&context->istream);
|
|
|
|
g_free(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* single archive handling */
|
|
|
|
|
2009-01-30 00:53:32 +01:00
|
|
|
static bool
|
|
|
|
bz2_open_stream(struct archive_file *file, struct input_stream *is,
|
2009-11-14 23:53:04 +01:00
|
|
|
G_GNUC_UNUSED const char *path, GError **error_r)
|
2008-12-16 21:45:59 +01:00
|
|
|
{
|
2009-12-16 16:01:39 +01:00
|
|
|
struct bz2_archive_file *context = (struct bz2_archive_file *) file;
|
2009-12-31 10:43:38 +01:00
|
|
|
struct bz2_input_stream *bis = g_new(struct bz2_input_stream, 1);
|
|
|
|
|
|
|
|
bis->archive = context;
|
2009-12-15 08:55:46 +01:00
|
|
|
|
2008-12-16 21:45:59 +01:00
|
|
|
//setup file ops
|
|
|
|
is->plugin = &bz2_inputplugin;
|
|
|
|
//insert back reference
|
2009-12-31 10:43:38 +01:00
|
|
|
is->data = bis;
|
2009-12-31 16:01:10 +01:00
|
|
|
is->ready = true;
|
2008-12-16 21:45:59 +01:00
|
|
|
is->seekable = false;
|
|
|
|
|
2009-12-31 10:43:38 +01:00
|
|
|
if (!bz2_alloc(bis, error_r)) {
|
|
|
|
g_free(bis);
|
2008-12-16 21:45:59 +01:00
|
|
|
return false;
|
2009-12-31 10:43:38 +01:00
|
|
|
}
|
2009-12-15 08:55:46 +01:00
|
|
|
|
2009-12-31 10:43:38 +01:00
|
|
|
bis->eof = false;
|
2009-12-15 21:09:13 +01:00
|
|
|
|
2009-12-31 10:02:55 +01:00
|
|
|
refcount_inc(&context->ref);
|
|
|
|
|
2008-12-16 21:45:59 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bz2_is_close(struct input_stream *is)
|
|
|
|
{
|
2009-12-31 10:43:38 +01:00
|
|
|
struct bz2_input_stream *bis = (struct bz2_input_stream *)is->data;
|
|
|
|
|
|
|
|
bz2_destroy(bis);
|
|
|
|
|
|
|
|
bz2_close(&bis->archive->base);
|
2009-12-15 19:45:50 +01:00
|
|
|
|
2009-12-31 10:43:38 +01:00
|
|
|
g_free(bis);
|
2008-12-16 21:45:59 +01:00
|
|
|
}
|
|
|
|
|
2009-12-15 09:08:30 +01:00
|
|
|
static bool
|
2009-12-31 10:43:38 +01:00
|
|
|
bz2_fillbuffer(struct bz2_input_stream *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-31 10:43:38 +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
|
|
|
{
|
2009-12-31 10:43:38 +01:00
|
|
|
struct bz2_input_stream *bis = (struct bz2_input_stream *)is->data;
|
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;
|
2008-12-16 21:45:59 +01:00
|
|
|
bzstream->next_out = 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)
|
|
|
|
{
|
2009-12-31 10:43:38 +01:00
|
|
|
struct bz2_input_stream *bis = (struct bz2_input_stream *)is->data;
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct input_plugin bz2_inputplugin = {
|
|
|
|
.close = bz2_is_close,
|
|
|
|
.read = bz2_is_read,
|
|
|
|
.eof = bz2_is_eof,
|
|
|
|
};
|
|
|
|
|
2009-12-16 16:04:16 +01:00
|
|
|
const struct archive_plugin bz2_archive_plugin = {
|
2008-12-16 21:45:59 +01:00
|
|
|
.name = "bz2",
|
|
|
|
.open = bz2_open,
|
|
|
|
.scan_reset = bz2_scan_reset,
|
|
|
|
.scan_next = bz2_scan_next,
|
2009-01-30 00:53:32 +01:00
|
|
|
.open_stream = bz2_open_stream,
|
2008-12-16 21:45:59 +01:00
|
|
|
.close = bz2_close,
|
|
|
|
.suffixes = bz2_extensions
|
|
|
|
};
|
|
|
|
|