Import libarchive and bsdtar version 2.0.25
[dragonfly/port-amd64.git] / contrib / libarchive-2.0 / libarchive / archive_write_set_compression_none.c
blobb7a975554290e52429b2eba47e4b589846c199d0
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_write_set_compression_none.c,v 1.12 2007/03/03 07:37:36 kientzle Exp $");
29 #ifdef HAVE_ERRNO_H
30 #include <errno.h>
31 #endif
32 #ifdef HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
35 #ifdef HAVE_STRING_H
36 #include <string.h>
37 #endif
39 #include "archive.h"
40 #include "archive_private.h"
41 #include "archive_write_private.h"
43 static int archive_compressor_none_finish(struct archive_write *a);
44 static int archive_compressor_none_init(struct archive_write *);
45 static int archive_compressor_none_write(struct archive_write *,
46 const void *, size_t);
48 struct archive_none {
49 char *buffer;
50 ssize_t buffer_size;
51 char *next; /* Current insert location */
52 ssize_t avail; /* Free space left in buffer */
55 int
56 archive_write_set_compression_none(struct archive *_a)
58 struct archive_write *a = (struct archive_write *)_a;
59 __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
60 ARCHIVE_STATE_NEW, "archive_write_set_compression_none");
61 a->compression_init = &archive_compressor_none_init;
62 return (0);
66 * Setup callback.
68 static int
69 archive_compressor_none_init(struct archive_write *a)
71 int ret;
72 struct archive_none *state;
74 a->archive.compression_code = ARCHIVE_COMPRESSION_NONE;
75 a->archive.compression_name = "none";
77 if (a->client_opener != NULL) {
78 ret = (a->client_opener)(&a->archive, a->client_data);
79 if (ret != 0)
80 return (ret);
83 state = (struct archive_none *)malloc(sizeof(*state));
84 if (state == NULL) {
85 archive_set_error(&a->archive, ENOMEM,
86 "Can't allocate data for output buffering");
87 return (ARCHIVE_FATAL);
89 memset(state, 0, sizeof(*state));
91 state->buffer_size = a->bytes_per_block;
92 if (state->buffer_size != 0) {
93 state->buffer = (char *)malloc(state->buffer_size);
94 if (state->buffer == NULL) {
95 archive_set_error(&a->archive, ENOMEM,
96 "Can't allocate output buffer");
97 free(state);
98 return (ARCHIVE_FATAL);
102 state->next = state->buffer;
103 state->avail = state->buffer_size;
105 a->compression_data = state;
106 a->compression_write = archive_compressor_none_write;
107 a->compression_finish = archive_compressor_none_finish;
108 return (ARCHIVE_OK);
112 * Write data to the stream.
114 static int
115 archive_compressor_none_write(struct archive_write *a, const void *vbuff,
116 size_t length)
118 const char *buff;
119 ssize_t remaining, to_copy;
120 ssize_t bytes_written;
121 struct archive_none *state;
123 state = (struct archive_none *)a->compression_data;
124 buff = (const char *)vbuff;
125 if (a->client_writer == NULL) {
126 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
127 "No write callback is registered? "
128 "This is probably an internal programming error.");
129 return (ARCHIVE_FATAL);
132 remaining = length;
135 * If there is no buffer for blocking, just pass the data
136 * straight through to the client write callback. In
137 * particular, this supports "no write delay" operation for
138 * special applications. Just set the block size to zero.
140 if (state->buffer_size == 0) {
141 while (remaining > 0) {
142 bytes_written = (a->client_writer)(&a->archive,
143 a->client_data, buff, remaining);
144 if (bytes_written <= 0)
145 return (ARCHIVE_FATAL);
146 remaining -= bytes_written;
147 buff += bytes_written;
149 a->archive.file_position += length;
150 return (ARCHIVE_OK);
153 while ((remaining > 0) || (state->avail == 0)) {
155 * If we have a full output block, write it and reset the
156 * output buffer.
158 if (state->avail == 0) {
159 bytes_written = (a->client_writer)(&a->archive,
160 a->client_data, state->buffer, state->buffer_size);
161 if (bytes_written <= 0)
162 return (ARCHIVE_FATAL);
163 /* XXX TODO: if bytes_written < state->buffer_size */
164 a->archive.raw_position += bytes_written;
165 state->next = state->buffer;
166 state->avail = state->buffer_size;
169 /* Now we have space in the buffer; copy new data into it. */
170 to_copy = (remaining > state->avail) ?
171 state->avail : remaining;
172 memcpy(state->next, buff, to_copy);
173 state->next += to_copy;
174 state->avail -= to_copy;
175 buff += to_copy;
176 remaining -= to_copy;
178 a->archive.file_position += length;
179 return (ARCHIVE_OK);
184 * Finish the compression.
186 static int
187 archive_compressor_none_finish(struct archive_write *a)
189 ssize_t block_length;
190 ssize_t target_block_length;
191 ssize_t bytes_written;
192 int ret;
193 int ret2;
194 struct archive_none *state;
196 state = (struct archive_none *)a->compression_data;
197 ret = ret2 = ARCHIVE_OK;
198 if (a->client_writer == NULL) {
199 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
200 "No write callback is registered? "
201 "This is probably an internal programming error.");
202 return (ARCHIVE_FATAL);
205 /* If there's pending data, pad and write the last block */
206 if (state->next != state->buffer) {
207 block_length = state->buffer_size - state->avail;
209 /* Tricky calculation to determine size of last block */
210 target_block_length = block_length;
211 if (a->bytes_in_last_block <= 0)
212 /* Default or Zero: pad to full block */
213 target_block_length = a->bytes_per_block;
214 else
215 /* Round to next multiple of bytes_in_last_block. */
216 target_block_length = a->bytes_in_last_block *
217 ( (block_length + a->bytes_in_last_block - 1) /
218 a->bytes_in_last_block);
219 if (target_block_length > a->bytes_per_block)
220 target_block_length = a->bytes_per_block;
221 if (block_length < target_block_length) {
222 memset(state->next, 0,
223 target_block_length - block_length);
224 block_length = target_block_length;
226 bytes_written = (a->client_writer)(&a->archive,
227 a->client_data, state->buffer, block_length);
228 if (bytes_written <= 0)
229 ret = ARCHIVE_FATAL;
230 else {
231 a->archive.raw_position += bytes_written;
232 ret = ARCHIVE_OK;
236 /* Close the output */
237 if (a->client_closer != NULL)
238 ret2 = (a->client_closer)(&a->archive, a->client_data);
240 free(state->buffer);
241 free(state);
242 a->compression_data = NULL;
244 return (ret != ARCHIVE_OK ? ret : ret2);