Import libarchive-2.4.17. See NEWS for details.
[dragonfly.git] / contrib / libarchive-2 / libarchive / archive_write_set_compression_none.c
blobbdecb240d9f46fa367cffcb3a10e94f33a230e66
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.16 2007/12/30 04:58:22 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->compressor.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->compressor.data = state;
106 a->compressor.write = archive_compressor_none_write;
107 a->compressor.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->compressor.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 a->archive.raw_position += bytes_written;
147 remaining -= bytes_written;
148 buff += bytes_written;
150 a->archive.file_position += length;
151 return (ARCHIVE_OK);
154 /* If the copy buffer isn't empty, try to fill it. */
155 if (state->avail < state->buffer_size) {
156 /* If buffer is not empty... */
157 /* ... copy data into buffer ... */
158 to_copy = (remaining > state->avail) ?
159 state->avail : remaining;
160 memcpy(state->next, buff, to_copy);
161 state->next += to_copy;
162 state->avail -= to_copy;
163 buff += to_copy;
164 remaining -= to_copy;
165 /* ... if it's full, write it out. */
166 if (state->avail == 0) {
167 bytes_written = (a->client_writer)(&a->archive,
168 a->client_data, state->buffer, state->buffer_size);
169 if (bytes_written <= 0)
170 return (ARCHIVE_FATAL);
171 /* XXX TODO: if bytes_written < state->buffer_size */
172 a->archive.raw_position += bytes_written;
173 state->next = state->buffer;
174 state->avail = state->buffer_size;
178 while (remaining > state->buffer_size) {
179 /* Write out full blocks directly to client. */
180 bytes_written = (a->client_writer)(&a->archive,
181 a->client_data, buff, state->buffer_size);
182 if (bytes_written <= 0)
183 return (ARCHIVE_FATAL);
184 a->archive.raw_position += bytes_written;
185 buff += bytes_written;
186 remaining -= bytes_written;
189 if (remaining > 0) {
190 /* Copy last bit into copy buffer. */
191 memcpy(state->next, buff, remaining);
192 state->next += remaining;
193 state->avail -= remaining;
196 a->archive.file_position += length;
197 return (ARCHIVE_OK);
202 * Finish the compression.
204 static int
205 archive_compressor_none_finish(struct archive_write *a)
207 ssize_t block_length;
208 ssize_t target_block_length;
209 ssize_t bytes_written;
210 int ret;
211 int ret2;
212 struct archive_none *state;
214 state = (struct archive_none *)a->compressor.data;
215 ret = ret2 = ARCHIVE_OK;
216 if (a->client_writer == NULL) {
217 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
218 "No write callback is registered? "
219 "This is probably an internal programming error.");
220 return (ARCHIVE_FATAL);
223 /* If there's pending data, pad and write the last block */
224 if (state->next != state->buffer) {
225 block_length = state->buffer_size - state->avail;
227 /* Tricky calculation to determine size of last block */
228 target_block_length = block_length;
229 if (a->bytes_in_last_block <= 0)
230 /* Default or Zero: pad to full block */
231 target_block_length = a->bytes_per_block;
232 else
233 /* Round to next multiple of bytes_in_last_block. */
234 target_block_length = a->bytes_in_last_block *
235 ( (block_length + a->bytes_in_last_block - 1) /
236 a->bytes_in_last_block);
237 if (target_block_length > a->bytes_per_block)
238 target_block_length = a->bytes_per_block;
239 if (block_length < target_block_length) {
240 memset(state->next, 0,
241 target_block_length - block_length);
242 block_length = target_block_length;
244 bytes_written = (a->client_writer)(&a->archive,
245 a->client_data, state->buffer, block_length);
246 if (bytes_written <= 0)
247 ret = ARCHIVE_FATAL;
248 else {
249 a->archive.raw_position += bytes_written;
250 ret = ARCHIVE_OK;
253 if (state->buffer)
254 free(state->buffer);
255 free(state);
256 a->compressor.data = NULL;
258 return (ret);