2004-02-09 23:22:54 +00:00
|
|
|
/*-
|
2007-01-09 08:05:56 +00:00
|
|
|
* Copyright (c) 2003-2007 Tim Kientzle
|
2004-02-09 23:22:54 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
2007-01-09 08:05:56 +00:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2004-02-09 23:22:54 +00:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2004-03-09 19:50:41 +00:00
|
|
|
#include "archive_platform.h"
|
2004-02-09 23:22:54 +00:00
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
#ifdef HAVE_ERRNO_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <errno.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STDLIB_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <stdlib.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <string.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
#include "archive.h"
|
|
|
|
#include "archive_private.h"
|
2007-03-03 07:37:37 +00:00
|
|
|
#include "archive_write_private.h"
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
static int archive_compressor_none_finish(struct archive_write *a);
|
|
|
|
static int archive_compressor_none_init(struct archive_write *);
|
|
|
|
static int archive_compressor_none_write(struct archive_write *,
|
|
|
|
const void *, size_t);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
struct archive_none {
|
|
|
|
char *buffer;
|
|
|
|
ssize_t buffer_size;
|
|
|
|
char *next; /* Current insert location */
|
|
|
|
ssize_t avail; /* Free space left in buffer */
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_write_set_compression_none(struct archive *_a)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_write *a = (struct archive_write *)_a;
|
|
|
|
__archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
|
|
|
|
ARCHIVE_STATE_NEW, "archive_write_set_compression_none");
|
2007-05-29 01:00:21 +00:00
|
|
|
a->compressor.init = &archive_compressor_none_init;
|
2004-02-09 23:22:54 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Setup callback.
|
|
|
|
*/
|
|
|
|
static int
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_compressor_none_init(struct archive_write *a)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct archive_none *state;
|
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
a->archive.compression_code = ARCHIVE_COMPRESSION_NONE;
|
|
|
|
a->archive.compression_name = "none";
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
if (a->client_opener != NULL) {
|
2007-03-03 07:37:37 +00:00
|
|
|
ret = (a->client_opener)(&a->archive, a->client_data);
|
2004-02-09 23:22:54 +00:00
|
|
|
if (ret != 0)
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
state = (struct archive_none *)malloc(sizeof(*state));
|
|
|
|
if (state == NULL) {
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_set_error(&a->archive, ENOMEM,
|
2004-02-09 23:22:54 +00:00
|
|
|
"Can't allocate data for output buffering");
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
}
|
|
|
|
memset(state, 0, sizeof(*state));
|
|
|
|
|
|
|
|
state->buffer_size = a->bytes_per_block;
|
2006-11-26 19:00:50 +00:00
|
|
|
if (state->buffer_size != 0) {
|
|
|
|
state->buffer = (char *)malloc(state->buffer_size);
|
|
|
|
if (state->buffer == NULL) {
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_set_error(&a->archive, ENOMEM,
|
2006-11-26 19:00:50 +00:00
|
|
|
"Can't allocate output buffer");
|
|
|
|
free(state);
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
state->next = state->buffer;
|
|
|
|
state->avail = state->buffer_size;
|
|
|
|
|
2007-05-29 01:00:21 +00:00
|
|
|
a->compressor.data = state;
|
|
|
|
a->compressor.write = archive_compressor_none_write;
|
|
|
|
a->compressor.finish = archive_compressor_none_finish;
|
2004-02-09 23:22:54 +00:00
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write data to the stream.
|
|
|
|
*/
|
2004-11-05 05:26:30 +00:00
|
|
|
static int
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_compressor_none_write(struct archive_write *a, const void *vbuff,
|
2004-02-09 23:22:54 +00:00
|
|
|
size_t length)
|
|
|
|
{
|
|
|
|
const char *buff;
|
|
|
|
ssize_t remaining, to_copy;
|
2004-11-05 05:26:30 +00:00
|
|
|
ssize_t bytes_written;
|
2004-02-09 23:22:54 +00:00
|
|
|
struct archive_none *state;
|
|
|
|
|
2007-05-29 01:00:21 +00:00
|
|
|
state = (struct archive_none *)a->compressor.data;
|
2006-11-10 06:39:46 +00:00
|
|
|
buff = (const char *)vbuff;
|
2004-02-09 23:22:54 +00:00
|
|
|
if (a->client_writer == NULL) {
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
|
2004-02-09 23:22:54 +00:00
|
|
|
"No write callback is registered? "
|
|
|
|
"This is probably an internal programming error.");
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
remaining = length;
|
2006-11-26 19:00:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If there is no buffer for blocking, just pass the data
|
|
|
|
* straight through to the client write callback. In
|
|
|
|
* particular, this supports "no write delay" operation for
|
|
|
|
* special applications. Just set the block size to zero.
|
|
|
|
*/
|
|
|
|
if (state->buffer_size == 0) {
|
|
|
|
while (remaining > 0) {
|
2007-03-03 07:37:37 +00:00
|
|
|
bytes_written = (a->client_writer)(&a->archive,
|
|
|
|
a->client_data, buff, remaining);
|
2006-11-26 19:00:50 +00:00
|
|
|
if (bytes_written <= 0)
|
|
|
|
return (ARCHIVE_FATAL);
|
2007-03-17 14:59:04 +00:00
|
|
|
a->archive.raw_position += bytes_written;
|
2006-11-26 19:00:50 +00:00
|
|
|
remaining -= bytes_written;
|
|
|
|
buff += bytes_written;
|
|
|
|
}
|
2007-03-03 07:37:37 +00:00
|
|
|
a->archive.file_position += length;
|
2006-11-26 19:00:50 +00:00
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|
|
|
|
|
2007-12-30 04:58:22 +00:00
|
|
|
/* If the copy buffer isn't empty, try to fill it. */
|
|
|
|
if (state->avail < state->buffer_size) {
|
|
|
|
/* If buffer is not empty... */
|
|
|
|
/* ... copy data into buffer ... */
|
|
|
|
to_copy = (remaining > state->avail) ?
|
|
|
|
state->avail : remaining;
|
|
|
|
memcpy(state->next, buff, to_copy);
|
|
|
|
state->next += to_copy;
|
|
|
|
state->avail -= to_copy;
|
|
|
|
buff += to_copy;
|
|
|
|
remaining -= to_copy;
|
|
|
|
/* ... if it's full, write it out. */
|
2004-02-09 23:22:54 +00:00
|
|
|
if (state->avail == 0) {
|
2007-03-03 07:37:37 +00:00
|
|
|
bytes_written = (a->client_writer)(&a->archive,
|
|
|
|
a->client_data, state->buffer, state->buffer_size);
|
2004-11-05 05:26:30 +00:00
|
|
|
if (bytes_written <= 0)
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
/* XXX TODO: if bytes_written < state->buffer_size */
|
2007-03-03 07:37:37 +00:00
|
|
|
a->archive.raw_position += bytes_written;
|
2004-02-09 23:22:54 +00:00
|
|
|
state->next = state->buffer;
|
|
|
|
state->avail = state->buffer_size;
|
|
|
|
}
|
2007-12-30 04:58:22 +00:00
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2007-12-30 04:58:22 +00:00
|
|
|
while (remaining > state->buffer_size) {
|
|
|
|
/* Write out full blocks directly to client. */
|
|
|
|
bytes_written = (a->client_writer)(&a->archive,
|
|
|
|
a->client_data, buff, state->buffer_size);
|
|
|
|
if (bytes_written <= 0)
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
a->archive.raw_position += bytes_written;
|
|
|
|
buff += bytes_written;
|
|
|
|
remaining -= bytes_written;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remaining > 0) {
|
|
|
|
/* Copy last bit into copy buffer. */
|
|
|
|
memcpy(state->next, buff, remaining);
|
|
|
|
state->next += remaining;
|
|
|
|
state->avail -= remaining;
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
2007-12-30 04:58:22 +00:00
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
a->archive.file_position += length;
|
2004-11-05 05:26:30 +00:00
|
|
|
return (ARCHIVE_OK);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finish the compression.
|
|
|
|
*/
|
|
|
|
static int
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_compressor_none_finish(struct archive_write *a)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
|
|
|
ssize_t block_length;
|
|
|
|
ssize_t target_block_length;
|
2004-11-05 05:26:30 +00:00
|
|
|
ssize_t bytes_written;
|
2004-02-09 23:22:54 +00:00
|
|
|
int ret;
|
|
|
|
int ret2;
|
|
|
|
struct archive_none *state;
|
|
|
|
|
2007-05-29 01:00:21 +00:00
|
|
|
state = (struct archive_none *)a->compressor.data;
|
2004-02-09 23:22:54 +00:00
|
|
|
ret = ret2 = ARCHIVE_OK;
|
|
|
|
if (a->client_writer == NULL) {
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
|
2004-02-09 23:22:54 +00:00
|
|
|
"No write callback is registered? "
|
|
|
|
"This is probably an internal programming error.");
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If there's pending data, pad and write the last block */
|
|
|
|
if (state->next != state->buffer) {
|
|
|
|
block_length = state->buffer_size - state->avail;
|
|
|
|
|
|
|
|
/* Tricky calculation to determine size of last block */
|
|
|
|
target_block_length = block_length;
|
|
|
|
if (a->bytes_in_last_block <= 0)
|
|
|
|
/* Default or Zero: pad to full block */
|
|
|
|
target_block_length = a->bytes_per_block;
|
|
|
|
else
|
|
|
|
/* Round to next multiple of bytes_in_last_block. */
|
|
|
|
target_block_length = a->bytes_in_last_block *
|
|
|
|
( (block_length + a->bytes_in_last_block - 1) /
|
|
|
|
a->bytes_in_last_block);
|
|
|
|
if (target_block_length > a->bytes_per_block)
|
|
|
|
target_block_length = a->bytes_per_block;
|
|
|
|
if (block_length < target_block_length) {
|
|
|
|
memset(state->next, 0,
|
|
|
|
target_block_length - block_length);
|
|
|
|
block_length = target_block_length;
|
|
|
|
}
|
2007-03-03 07:37:37 +00:00
|
|
|
bytes_written = (a->client_writer)(&a->archive,
|
|
|
|
a->client_data, state->buffer, block_length);
|
2004-11-05 05:26:30 +00:00
|
|
|
if (bytes_written <= 0)
|
|
|
|
ret = ARCHIVE_FATAL;
|
|
|
|
else {
|
2007-03-03 07:37:37 +00:00
|
|
|
a->archive.raw_position += bytes_written;
|
2004-11-05 05:26:30 +00:00
|
|
|
ret = ARCHIVE_OK;
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
2007-04-15 04:42:52 +00:00
|
|
|
if (state->buffer)
|
|
|
|
free(state->buffer);
|
2004-02-09 23:22:54 +00:00
|
|
|
free(state);
|
2007-05-29 01:00:21 +00:00
|
|
|
a->compressor.data = NULL;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2007-05-29 01:00:21 +00:00
|
|
|
return (ret);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|