Import libarchive-2.5.5.
[dragonfly.git] / contrib / libarchive-2.0 / libarchive / archive_write_set_compression_none.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
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.
13  *
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.
24  */
25
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_write_set_compression_none.c,v 1.13 2007/03/17 14:59:04 cperciva Exp $");
28
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
38
39 #include "archive.h"
40 #include "archive_private.h"
41 #include "archive_write_private.h"
42
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);
47
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 */
53 };
54
55 int
56 archive_write_set_compression_none(struct archive *_a)
57 {
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);
63 }
64
65 /*
66  * Setup callback.
67  */
68 static int
69 archive_compressor_none_init(struct archive_write *a)
70 {
71         int ret;
72         struct archive_none *state;
73
74         a->archive.compression_code = ARCHIVE_COMPRESSION_NONE;
75         a->archive.compression_name = "none";
76
77         if (a->client_opener != NULL) {
78                 ret = (a->client_opener)(&a->archive, a->client_data);
79                 if (ret != 0)
80                         return (ret);
81         }
82
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);
88         }
89         memset(state, 0, sizeof(*state));
90
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);
99                 }
100         }
101
102         state->next = state->buffer;
103         state->avail = state->buffer_size;
104
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);
109 }
110
111 /*
112  * Write data to the stream.
113  */
114 static int
115 archive_compressor_none_write(struct archive_write *a, const void *vbuff,
116     size_t length)
117 {
118         const char *buff;
119         ssize_t remaining, to_copy;
120         ssize_t bytes_written;
121         struct archive_none *state;
122
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);
130         }
131
132         remaining = length;
133
134         /*
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.
139          */
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;
149                 }
150                 a->archive.file_position += length;
151                 return (ARCHIVE_OK);
152         }
153
154         while ((remaining > 0) || (state->avail == 0)) {
155                 /*
156                  * If we have a full output block, write it and reset the
157                  * output buffer.
158                  */
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;
168                 }
169
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;
176                 buff += to_copy;
177                 remaining -= to_copy;
178         }
179         a->archive.file_position += length;
180         return (ARCHIVE_OK);
181 }
182
183
184 /*
185  * Finish the compression.
186  */
187 static int
188 archive_compressor_none_finish(struct archive_write *a)
189 {
190         ssize_t block_length;
191         ssize_t target_block_length;
192         ssize_t bytes_written;
193         int ret;
194         int ret2;
195         struct archive_none *state;
196
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);
204         }
205
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;
209
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;
215                 else
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;
226                 }
227                 bytes_written = (a->client_writer)(&a->archive,
228                     a->client_data, state->buffer, block_length);
229                 if (bytes_written <= 0)
230                         ret = ARCHIVE_FATAL;
231                 else {
232                         a->archive.raw_position += bytes_written;
233                         ret = ARCHIVE_OK;
234                 }
235         }
236
237         /* Close the output */
238         if (a->client_closer != NULL)
239                 ret2 = (a->client_closer)(&a->archive, a->client_data);
240
241         free(state->buffer);
242         free(state);
243         a->compression_data = NULL;
244
245         return (ret != ARCHIVE_OK ? ret : ret2);
246 }