2009-03-13 18:43:16 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2009 The Music Player Daemon Project
|
|
|
|
* 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"
|
2008-12-16 21:45:59 +01:00
|
|
|
#include "archive_api.h"
|
2009-03-02 20:13:08 +01:00
|
|
|
#include "input_plugin.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
|
|
|
|
|
|
|
|
#define BZ_BUFSIZE 5000
|
|
|
|
|
|
|
|
typedef struct {
|
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-15 21:09:13 +01:00
|
|
|
|
|
|
|
bool eof;
|
|
|
|
|
2009-12-15 08:55:46 +01:00
|
|
|
bz_stream bzstream;
|
|
|
|
char *buffer;
|
2008-12-16 21:45:59 +01:00
|
|
|
} bz2_context;
|
|
|
|
|
|
|
|
static const struct input_plugin bz2_inputplugin;
|
|
|
|
|
|
|
|
/* single archive handling allocation helpers */
|
|
|
|
|
|
|
|
static bool
|
|
|
|
bz2_alloc(bz2_context *data)
|
|
|
|
{
|
|
|
|
data->bzstream.bzalloc = NULL;
|
|
|
|
data->bzstream.bzfree = NULL;
|
|
|
|
data->bzstream.opaque = NULL;
|
|
|
|
|
|
|
|
data->buffer = g_malloc(BZ_BUFSIZE);
|
|
|
|
data->bzstream.next_in = (void *) data->buffer;
|
|
|
|
data->bzstream.avail_in = 0;
|
|
|
|
|
|
|
|
if (BZ2_bzDecompressInit(&data->bzstream, 0, 0) != BZ_OK) {
|
|
|
|
g_free(data->buffer);
|
|
|
|
g_free(data);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bz2_destroy(bz2_context *data)
|
|
|
|
{
|
|
|
|
BZ2_bzDecompressEnd(&data->bzstream);
|
|
|
|
g_free(data->buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* archive open && listing routine */
|
|
|
|
|
|
|
|
static struct archive_file *
|
2009-12-15 08:55:46 +01:00
|
|
|
bz2_open(char *pathname)
|
2008-12-16 21:45:59 +01:00
|
|
|
{
|
|
|
|
bz2_context *context;
|
|
|
|
char *name;
|
|
|
|
int len;
|
|
|
|
|
2009-12-15 09:00:18 +01:00
|
|
|
context = g_malloc(sizeof(*context));
|
|
|
|
|
2008-12-16 21:45:59 +01:00
|
|
|
//open archive
|
|
|
|
if (!input_stream_open(&context->istream, pathname)) {
|
|
|
|
g_warning("failed to open an bzip2 archive %s\n",pathname);
|
|
|
|
g_free(context);
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-12-15 08:55:46 +01:00
|
|
|
|
2008-12-16 21:45:59 +01:00
|
|
|
//capture filename
|
|
|
|
name = strrchr(pathname, '/');
|
|
|
|
if (name == NULL) {
|
|
|
|
g_warning("failed to get bzip2 name from %s\n",pathname);
|
|
|
|
g_free(context);
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-12-15 08:55:46 +01:00
|
|
|
|
|
|
|
context->name = g_strdup(name + 1);
|
|
|
|
|
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
|
|
|
|
2008-12-16 21:45:59 +01:00
|
|
|
return (struct archive_file *) context;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bz2_scan_reset(struct archive_file *file)
|
|
|
|
{
|
|
|
|
bz2_context *context = (bz2_context *) file;
|
|
|
|
context->reset = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
bz2_scan_next(struct archive_file *file)
|
|
|
|
{
|
|
|
|
bz2_context *context = (bz2_context *) file;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
bz2_context *context = (bz2_context *) file;
|
2009-12-15 08:55:46 +01:00
|
|
|
|
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,
|
|
|
|
G_GNUC_UNUSED const char *path)
|
2008-12-16 21:45:59 +01:00
|
|
|
{
|
|
|
|
bz2_context *context = (bz2_context *) file;
|
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-01-30 00:53:32 +01:00
|
|
|
is->data = context;
|
2008-12-16 21:45:59 +01:00
|
|
|
is->seekable = false;
|
|
|
|
|
|
|
|
if (!bz2_alloc(context)) {
|
|
|
|
g_warning("alloc bz2 failed\n");
|
|
|
|
return false;
|
|
|
|
}
|
2009-12-15 08:55:46 +01:00
|
|
|
|
2009-12-15 21:09:13 +01:00
|
|
|
context->eof = false;
|
|
|
|
|
2008-12-16 21:45:59 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bz2_is_close(struct input_stream *is)
|
|
|
|
{
|
2009-01-30 00:53:32 +01:00
|
|
|
bz2_context *context = (bz2_context *) is->data;
|
2008-12-16 21:45:59 +01:00
|
|
|
bz2_destroy(context);
|
|
|
|
is->data = NULL;
|
2009-12-15 19:45:50 +01:00
|
|
|
|
|
|
|
bz2_close((struct archive_file *)context);
|
2008-12-16 21:45:59 +01:00
|
|
|
}
|
|
|
|
|
2009-12-15 09:08:30 +01:00
|
|
|
static bool
|
2009-12-15 21:09:13 +01:00
|
|
|
bz2_fillbuffer(bz2_context *context)
|
2008-12-16 21:45:59 +01:00
|
|
|
{
|
|
|
|
size_t count;
|
|
|
|
bz_stream *bzstream;
|
|
|
|
|
|
|
|
bzstream = &context->bzstream;
|
|
|
|
|
|
|
|
if (bzstream->avail_in > 0)
|
2009-12-15 09:08:30 +01:00
|
|
|
return true;
|
2008-12-16 21:45:59 +01:00
|
|
|
|
|
|
|
count = input_stream_read(&context->istream,
|
2009-12-15 08:55:46 +01:00
|
|
|
context->buffer, BZ_BUFSIZE);
|
2009-12-15 21:09:13 +01:00
|
|
|
if (count == 0)
|
|
|
|
return false;
|
2008-12-16 21:45:59 +01:00
|
|
|
|
2009-12-15 21:09:13 +01:00
|
|
|
bzstream->next_in = context->buffer;
|
|
|
|
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-12-15 15:22:45 +01:00
|
|
|
bz2_is_read(struct input_stream *is, void *ptr, size_t length)
|
2008-12-16 21:45:59 +01:00
|
|
|
{
|
2009-01-30 00:53:32 +01:00
|
|
|
bz2_context *context = (bz2_context *) 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-15 21:09:13 +01:00
|
|
|
if (context->eof)
|
2008-12-16 21:45:59 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
bzstream = &context->bzstream;
|
|
|
|
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 {
|
|
|
|
if (!bz2_fillbuffer(context)) {
|
|
|
|
is->error = -1;
|
|
|
|
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) {
|
|
|
|
context->eof = true;
|
2008-12-16 21:45:59 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-12-15 21:09:13 +01:00
|
|
|
if (bz_result != BZ_OK) {
|
|
|
|
is->error = bz_result;
|
|
|
|
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-01-30 00:53:32 +01:00
|
|
|
bz2_context *context = (bz2_context *) is->data;
|
2008-12-16 21:45:59 +01:00
|
|
|
|
2009-12-15 21:09:13 +01:00
|
|
|
return context->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,
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct archive_plugin bz2_plugin = {
|
|
|
|
.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
|
|
|
|
};
|
|
|
|
|