2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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$");
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);
51 char *next; /* Current insert location */
52 ssize_t avail; /* Free space left in buffer */
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;
69 archive_compressor_none_init(struct archive_write *a)
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);
83 state = (struct archive_none *)malloc(sizeof(*state));
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");
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;
112 * Write data to the stream.
115 archive_compressor_none_write(struct archive_write *a, const void *vbuff,
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);
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 a->archive.raw_position += bytes_written;
147 remaining -= bytes_written;
148 buff += bytes_written;
150 a->archive.file_position += length;
154 while ((remaining > 0) || (state->avail == 0)) {
156 * If we have a full output block, write it and reset the
159 if (state->avail == 0) {
160 bytes_written = (a->client_writer)(&a->archive,
161 a->client_data, state->buffer, state->buffer_size);
162 if (bytes_written <= 0)
163 return (ARCHIVE_FATAL);
164 /* XXX TODO: if bytes_written < state->buffer_size */
165 a->archive.raw_position += bytes_written;
166 state->next = state->buffer;
167 state->avail = state->buffer_size;
170 /* Now we have space in the buffer; copy new data into it. */
171 to_copy = (remaining > state->avail) ?
172 state->avail : remaining;
173 memcpy(state->next, buff, to_copy);
174 state->next += to_copy;
175 state->avail -= to_copy;
177 remaining -= to_copy;
179 a->archive.file_position += length;
185 * Finish the compression.
188 archive_compressor_none_finish(struct archive_write *a)
190 ssize_t block_length;
191 ssize_t target_block_length;
192 ssize_t bytes_written;
195 struct archive_none *state;
197 state = (struct archive_none *)a->compression_data;
198 ret = ret2 = ARCHIVE_OK;
199 if (a->client_writer == NULL) {
200 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
201 "No write callback is registered? "
202 "This is probably an internal programming error.");
203 return (ARCHIVE_FATAL);
206 /* If there's pending data, pad and write the last block */
207 if (state->next != state->buffer) {
208 block_length = state->buffer_size - state->avail;
210 /* Tricky calculation to determine size of last block */
211 target_block_length = block_length;
212 if (a->bytes_in_last_block <= 0)
213 /* Default or Zero: pad to full block */
214 target_block_length = a->bytes_per_block;
216 /* Round to next multiple of bytes_in_last_block. */
217 target_block_length = a->bytes_in_last_block *
218 ( (block_length + a->bytes_in_last_block - 1) /
219 a->bytes_in_last_block);
220 if (target_block_length > a->bytes_per_block)
221 target_block_length = a->bytes_per_block;
222 if (block_length < target_block_length) {
223 memset(state->next, 0,
224 target_block_length - block_length);
225 block_length = target_block_length;
227 bytes_written = (a->client_writer)(&a->archive,
228 a->client_data, state->buffer, block_length);
229 if (bytes_written <= 0)
232 a->archive.raw_position += bytes_written;
237 /* Close the output */
238 if (a->client_closer != NULL)
239 ret2 = (a->client_closer)(&a->archive, a->client_data);
244 a->compression_data = NULL;
246 return (ret != ARCHIVE_OK ? ret : ret2);