1 /*-
2  * Copyright (c) 2003-2010 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: head/lib/libarchive/archive_write.c 201099 2009-12-28 03:03:00Z kientzle $");
28 
29 /*
30  * This file contains the "essential" portions of the write API, that
31  * is, stuff that will essentially always be used by any client that
32  * actually needs to write an archive.  Optional pieces have been, as
33  * far as possible, separated out into separate files to reduce
34  * needlessly bloating statically-linked clients.
35  */
36 
37 #ifdef HAVE_SYS_WAIT_H
38 #include <sys/wait.h>
39 #endif
40 #ifdef HAVE_ERRNO_H
41 #include <errno.h>
42 #endif
43 #ifdef HAVE_LIMITS_H
44 #include <limits.h>
45 #endif
46 #include <stdio.h>
47 #ifdef HAVE_STDLIB_H
48 #include <stdlib.h>
49 #endif
50 #ifdef HAVE_STRING_H
51 #include <string.h>
52 #endif
53 #include <time.h>
54 #ifdef HAVE_UNISTD_H
55 #include <unistd.h>
56 #endif
57 
58 #include "archive.h"
59 #include "archive_entry.h"
60 #include "archive_private.h"
61 #include "archive_write_private.h"
62 
63 static struct archive_vtable *archive_write_vtable(void);
64 
65 static int	_archive_filter_code(struct archive *, int);
66 static const char *_archive_filter_name(struct archive *, int);
67 static int64_t	_archive_filter_bytes(struct archive *, int);
68 static int  _archive_write_filter_count(struct archive *);
69 static int	_archive_write_close(struct archive *);
70 static int	_archive_write_free(struct archive *);
71 static int	_archive_write_header(struct archive *, struct archive_entry *);
72 static int	_archive_write_finish_entry(struct archive *);
73 static ssize_t	_archive_write_data(struct archive *, const void *, size_t);
74 
75 struct archive_none {
76 	size_t buffer_size;
77 	size_t avail;
78 	char *buffer;
79 	char *next;
80 };
81 
82 static struct archive_vtable *
83 archive_write_vtable(void)
84 {
85 	static struct archive_vtable av;
86 	static int inited = 0;
87 
88 	if (!inited) {
89 		av.archive_close = _archive_write_close;
90 		av.archive_filter_bytes = _archive_filter_bytes;
91 		av.archive_filter_code = _archive_filter_code;
92 		av.archive_filter_name = _archive_filter_name;
93 		av.archive_filter_count = _archive_write_filter_count;
94 		av.archive_free = _archive_write_free;
95 		av.archive_write_header = _archive_write_header;
96 		av.archive_write_finish_entry = _archive_write_finish_entry;
97 		av.archive_write_data = _archive_write_data;
98 		inited = 1;
99 	}
100 	return (&av);
101 }
102 
103 /*
104  * Allocate, initialize and return an archive object.
105  */
106 struct archive *
107 archive_write_new(void)
108 {
109 	struct archive_write *a;
110 	unsigned char *nulls;
111 
112 	a = (struct archive_write *)malloc(sizeof(*a));
113 	if (a == NULL)
114 		return (NULL);
115 	memset(a, 0, sizeof(*a));
116 	a->archive.magic = ARCHIVE_WRITE_MAGIC;
117 	a->archive.state = ARCHIVE_STATE_NEW;
118 	a->archive.vtable = archive_write_vtable();
119 	/*
120 	 * The value 10240 here matches the traditional tar default,
121 	 * but is otherwise arbitrary.
122 	 * TODO: Set the default block size from the format selected.
123 	 */
124 	a->bytes_per_block = 10240;
125 	a->bytes_in_last_block = -1;	/* Default */
126 
127 	/* Initialize a block of nulls for padding purposes. */
128 	a->null_length = 1024;
129 	nulls = (unsigned char *)malloc(a->null_length);
130 	if (nulls == NULL) {
131 		free(a);
132 		return (NULL);
133 	}
134 	memset(nulls, 0, a->null_length);
135 	a->nulls = nulls;
136 	return (&a->archive);
137 }
138 
139 /*
140  * Set the block size.  Returns 0 if successful.
141  */
142 int
143 archive_write_set_bytes_per_block(struct archive *_a, int bytes_per_block)
144 {
145 	struct archive_write *a = (struct archive_write *)_a;
146 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
147 	    ARCHIVE_STATE_NEW, "archive_write_set_bytes_per_block");
148 	a->bytes_per_block = bytes_per_block;
149 	return (ARCHIVE_OK);
150 }
151 
152 /*
153  * Get the current block size.  -1 if it has never been set.
154  */
155 int
156 archive_write_get_bytes_per_block(struct archive *_a)
157 {
158 	struct archive_write *a = (struct archive_write *)_a;
159 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
160 	    ARCHIVE_STATE_ANY, "archive_write_get_bytes_per_block");
161 	return (a->bytes_per_block);
162 }
163 
164 /*
165  * Set the size for the last block.
166  * Returns 0 if successful.
167  */
168 int
169 archive_write_set_bytes_in_last_block(struct archive *_a, int bytes)
170 {
171 	struct archive_write *a = (struct archive_write *)_a;
172 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
173 	    ARCHIVE_STATE_ANY, "archive_write_set_bytes_in_last_block");
174 	a->bytes_in_last_block = bytes;
175 	return (ARCHIVE_OK);
176 }
177 
178 /*
179  * Return the value set above.  -1 indicates it has not been set.
180  */
181 int
182 archive_write_get_bytes_in_last_block(struct archive *_a)
183 {
184 	struct archive_write *a = (struct archive_write *)_a;
185 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
186 	    ARCHIVE_STATE_ANY, "archive_write_get_bytes_in_last_block");
187 	return (a->bytes_in_last_block);
188 }
189 
190 /*
191  * dev/ino of a file to be rejected.  Used to prevent adding
192  * an archive to itself recursively.
193  */
194 int
195 archive_write_set_skip_file(struct archive *_a, int64_t d, int64_t i)
196 {
197 	struct archive_write *a = (struct archive_write *)_a;
198 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
199 	    ARCHIVE_STATE_ANY, "archive_write_set_skip_file");
200 	a->skip_file_set = 1;
201 	a->skip_file_dev = d;
202 	a->skip_file_ino = i;
203 	return (ARCHIVE_OK);
204 }
205 
206 /*
207  * Allocate and return the next filter structure.
208  */
209 struct archive_write_filter *
210 __archive_write_allocate_filter(struct archive *_a)
211 {
212 	struct archive_write *a = (struct archive_write *)_a;
213 	struct archive_write_filter *f;
214 
215 	f = calloc(1, sizeof(*f));
216 	f->archive = _a;
217 	if (a->filter_first == NULL)
218 		a->filter_first = f;
219 	else
220 		a->filter_last->next_filter = f;
221 	a->filter_last = f;
222 	return f;
223 }
224 
225 /*
226  * Write data to a particular filter.
227  */
228 int
229 __archive_write_filter(struct archive_write_filter *f,
230     const void *buff, size_t length)
231 {
232 	int r;
233 	if (length == 0)
234 		return(ARCHIVE_OK);
235 	if (f->write == NULL)
236 		/* If unset, a fatal error has already ocuured, so this filter
237 		 * didn't open. We cannot write anything. */
238 		return(ARCHIVE_FATAL);
239 	r = (f->write)(f, buff, length);
240 	f->bytes_written += length;
241 	return (r);
242 }
243 
244 /*
245  * Open a filter.
246  */
247 int
248 __archive_write_open_filter(struct archive_write_filter *f)
249 {
250 	if (f->open == NULL)
251 		return (ARCHIVE_OK);
252 	return (f->open)(f);
253 }
254 
255 /*
256  * Close a filter.
257  */
258 int
259 __archive_write_close_filter(struct archive_write_filter *f)
260 {
261 	if (f->close != NULL)
262 		return (f->close)(f);
263 	if (f->next_filter != NULL)
264 		return (__archive_write_close_filter(f->next_filter));
265 	return (ARCHIVE_OK);
266 }
267 
268 int
269 __archive_write_output(struct archive_write *a, const void *buff, size_t length)
270 {
271 	return (__archive_write_filter(a->filter_first, buff, length));
272 }
273 
274 int
275 __archive_write_nulls(struct archive_write *a, size_t length)
276 {
277 	if (length == 0)
278 		return (ARCHIVE_OK);
279 
280 	while (length > 0) {
281 		size_t to_write = length < a->null_length ? length : a->null_length;
282 		int r = __archive_write_output(a, a->nulls, to_write);
283 		if (r < ARCHIVE_OK)
284 			return (r);
285 		length -= to_write;
286 	}
287 	return (ARCHIVE_OK);
288 }
289 
290 static int
291 archive_write_client_open(struct archive_write_filter *f)
292 {
293 	struct archive_write *a = (struct archive_write *)f->archive;
294 	struct archive_none *state;
295 	void *buffer;
296 	size_t buffer_size;
297 
298 	f->bytes_per_block = archive_write_get_bytes_per_block(f->archive);
299 	f->bytes_in_last_block =
300 	    archive_write_get_bytes_in_last_block(f->archive);
301 	buffer_size = f->bytes_per_block;
302 
303 	state = (struct archive_none *)calloc(1, sizeof(*state));
304 	buffer = (char *)malloc(buffer_size);
305 	if (state == NULL || buffer == NULL) {
306 		free(state);
307 		free(buffer);
308 		archive_set_error(f->archive, ENOMEM,
309 		    "Can't allocate data for output buffering");
310 		return (ARCHIVE_FATAL);
311 	}
312 
313 	state->buffer_size = buffer_size;
314 	state->buffer = buffer;
315 	state->next = state->buffer;
316 	state->avail = state->buffer_size;
317 	f->data = state;
318 
319 	if (a->client_opener == NULL)
320 		return (ARCHIVE_OK);
321 	return (a->client_opener(f->archive, a->client_data));
322 }
323 
324 static int
325 archive_write_client_write(struct archive_write_filter *f,
326     const void *_buff, size_t length)
327 {
328 	struct archive_write *a = (struct archive_write *)f->archive;
329         struct archive_none *state = (struct archive_none *)f->data;
330 	const char *buff = (const char *)_buff;
331 	ssize_t remaining, to_copy;
332 	ssize_t bytes_written;
333 
334 	remaining = length;
335 
336 	/*
337 	 * If there is no buffer for blocking, just pass the data
338 	 * straight through to the client write callback.  In
339 	 * particular, this supports "no write delay" operation for
340 	 * special applications.  Just set the block size to zero.
341 	 */
342 	if (state->buffer_size == 0) {
343 		while (remaining > 0) {
344 			bytes_written = (a->client_writer)(&a->archive,
345 			    a->client_data, buff, remaining);
346 			if (bytes_written <= 0)
347 				return (ARCHIVE_FATAL);
348 			remaining -= bytes_written;
349 			buff += bytes_written;
350 		}
351 		return (ARCHIVE_OK);
352 	}
353 
354 	/* If the copy buffer isn't empty, try to fill it. */
355 	if (state->avail < state->buffer_size) {
356 		/* If buffer is not empty... */
357 		/* ... copy data into buffer ... */
358 		to_copy = ((size_t)remaining > state->avail) ?
359 			state->avail : (size_t)remaining;
360 		memcpy(state->next, buff, to_copy);
361 		state->next += to_copy;
362 		state->avail -= to_copy;
363 		buff += to_copy;
364 		remaining -= to_copy;
365 		/* ... if it's full, write it out. */
366 		if (state->avail == 0) {
367 			char *p = state->buffer;
368 			size_t to_write = state->buffer_size;
369 			while (to_write > 0) {
370 				bytes_written = (a->client_writer)(&a->archive,
371 				    a->client_data, p, to_write);
372 				if (bytes_written <= 0)
373 					return (ARCHIVE_FATAL);
374 				if ((size_t)bytes_written > to_write) {
375 					archive_set_error(&(a->archive),
376 					    -1, "write overrun");
377 					return (ARCHIVE_FATAL);
378 				}
379 				p += bytes_written;
380 				to_write -= bytes_written;
381 			}
382 			state->next = state->buffer;
383 			state->avail = state->buffer_size;
384 		}
385 	}
386 
387 	while ((size_t)remaining >= state->buffer_size) {
388 		/* Write out full blocks directly to client. */
389 		bytes_written = (a->client_writer)(&a->archive,
390 		    a->client_data, buff, state->buffer_size);
391 		if (bytes_written <= 0)
392 			return (ARCHIVE_FATAL);
393 		buff += bytes_written;
394 		remaining -= bytes_written;
395 	}
396 
397 	if (remaining > 0) {
398 		/* Copy last bit into copy buffer. */
399 		memcpy(state->next, buff, remaining);
400 		state->next += remaining;
401 		state->avail -= remaining;
402 	}
403 	return (ARCHIVE_OK);
404 }
405 
406 static int
407 archive_write_client_close(struct archive_write_filter *f)
408 {
409 	struct archive_write *a = (struct archive_write *)f->archive;
410 	struct archive_none *state = (struct archive_none *)f->data;
411 	ssize_t block_length;
412 	ssize_t target_block_length;
413 	ssize_t bytes_written;
414 	int ret = ARCHIVE_OK;
415 
416 	/* If there's pending data, pad and write the last block */
417 	if (state->next != state->buffer) {
418 		block_length = state->buffer_size - state->avail;
419 
420 		/* Tricky calculation to determine size of last block */
421 		if (a->bytes_in_last_block <= 0)
422 			/* Default or Zero: pad to full block */
423 			target_block_length = a->bytes_per_block;
424 		else
425 			/* Round to next multiple of bytes_in_last_block. */
426 			target_block_length = a->bytes_in_last_block *
427 			    ( (block_length + a->bytes_in_last_block - 1) /
428 			        a->bytes_in_last_block);
429 		if (target_block_length > a->bytes_per_block)
430 			target_block_length = a->bytes_per_block;
431 		if (block_length < target_block_length) {
432 			memset(state->next, 0,
433 			    target_block_length - block_length);
434 			block_length = target_block_length;
435 		}
436 		bytes_written = (a->client_writer)(&a->archive,
437 		    a->client_data, state->buffer, block_length);
438 		ret = bytes_written <= 0 ? ARCHIVE_FATAL : ARCHIVE_OK;
439 	}
440 	if (a->client_closer)
441 		(*a->client_closer)(&a->archive, a->client_data);
442 	free(state->buffer);
443 	free(state);
444 	/* Clear the close handler myself not to be called again. */
445 	f->close = NULL;
446 	a->client_data = NULL;
447 	return (ret);
448 }
449 
450 /*
451  * Open the archive using the current settings.
452  */
453 int
454 archive_write_open(struct archive *_a, void *client_data,
455     archive_open_callback *opener, archive_write_callback *writer,
456     archive_close_callback *closer)
457 {
458 	struct archive_write *a = (struct archive_write *)_a;
459 	struct archive_write_filter *client_filter;
460 	int ret, r1;
461 
462 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
463 	    ARCHIVE_STATE_NEW, "archive_write_open");
464 	archive_clear_error(&a->archive);
465 
466 	a->client_writer = writer;
467 	a->client_opener = opener;
468 	a->client_closer = closer;
469 	a->client_data = client_data;
470 
471 	client_filter = __archive_write_allocate_filter(_a);
472 	client_filter->open = archive_write_client_open;
473 	client_filter->write = archive_write_client_write;
474 	client_filter->close = archive_write_client_close;
475 
476 	ret = __archive_write_open_filter(a->filter_first);
477 	if (ret < ARCHIVE_WARN) {
478 		r1 = __archive_write_close_filter(a->filter_first);
479 		return (r1 < ret ? r1 : ret);
480 	}
481 
482 	a->archive.state = ARCHIVE_STATE_HEADER;
483 	if (a->format_init)
484 		ret = (a->format_init)(a);
485 	return (ret);
486 }
487 
488 /*
489  * Close out the archive.
490  */
491 static int
492 _archive_write_close(struct archive *_a)
493 {
494 	struct archive_write *a = (struct archive_write *)_a;
495 	int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
496 
497 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
498 	    ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL,
499 	    "archive_write_close");
500 	if (a->archive.state == ARCHIVE_STATE_NEW
501 	    || a->archive.state == ARCHIVE_STATE_CLOSED)
502 		return (ARCHIVE_OK); /* Okay to close() when not open. */
503 
504 	archive_clear_error(&a->archive);
505 
506 	/* Finish the last entry. */
507 	if (a->archive.state == ARCHIVE_STATE_DATA)
508 		r = ((a->format_finish_entry)(a));
509 
510 	/* Finish off the archive. */
511 	/* TODO: have format closers invoke compression close. */
512 	if (a->format_close != NULL) {
513 		r1 = (a->format_close)(a);
514 		if (r1 < r)
515 			r = r1;
516 	}
517 
518 	/* Finish the compression and close the stream. */
519 	r1 = __archive_write_close_filter(a->filter_first);
520 	if (r1 < r)
521 		r = r1;
522 
523 	if (a->archive.state != ARCHIVE_STATE_FATAL)
524 		a->archive.state = ARCHIVE_STATE_CLOSED;
525 	return (r);
526 }
527 
528 static int
529 _archive_write_filter_count(struct archive *_a)
530 {
531 	struct archive_write *a = (struct archive_write *)_a;
532 	struct archive_write_filter *p = a->filter_first;
533 	int count = 0;
534 	while(p) {
535 		count++;
536 		p = p->next_filter;
537 	}
538 	return count;
539 }
540 
541 void
542 __archive_write_filters_free(struct archive *_a)
543 {
544 	struct archive_write *a = (struct archive_write *)_a;
545 	int r = ARCHIVE_OK, r1;
546 
547 	while (a->filter_first != NULL) {
548 		struct archive_write_filter *next
549 		    = a->filter_first->next_filter;
550 		if (a->filter_first->free != NULL) {
551 			r1 = (*a->filter_first->free)(a->filter_first);
552 			if (r > r1)
553 				r = r1;
554 		}
555 		free(a->filter_first);
556 		a->filter_first = next;
557 	}
558 	a->filter_last = NULL;
559 }
560 
561 /*
562  * Destroy the archive structure.
563  *
564  * Be careful: user might just call write_new and then write_free.
565  * Don't assume we actually wrote anything or performed any non-trivial
566  * initialization.
567  */
568 static int
569 _archive_write_free(struct archive *_a)
570 {
571 	struct archive_write *a = (struct archive_write *)_a;
572 	int r = ARCHIVE_OK, r1;
573 
574 	if (_a == NULL)
575 		return (ARCHIVE_OK);
576 	/* It is okay to call free() in state FATAL. */
577 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
578 	    ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_write_free");
579 	if (a->archive.state != ARCHIVE_STATE_FATAL)
580 		r = archive_write_close(&a->archive);
581 
582 	/* Release format resources. */
583 	if (a->format_free != NULL) {
584 		r1 = (a->format_free)(a);
585 		if (r1 < r)
586 			r = r1;
587 	}
588 
589 	__archive_write_filters_free(_a);
590 
591 	/* Release various dynamic buffers. */
592 	free((void *)(uintptr_t)(const void *)a->nulls);
593 	archive_string_free(&a->archive.error_string);
594 	a->archive.magic = 0;
595 	__archive_clean(&a->archive);
596 	free(a);
597 	return (r);
598 }
599 
600 /*
601  * Write the appropriate header.
602  */
603 static int
604 _archive_write_header(struct archive *_a, struct archive_entry *entry)
605 {
606 	struct archive_write *a = (struct archive_write *)_a;
607 	int ret, r2;
608 
609 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
610 	    ARCHIVE_STATE_DATA | ARCHIVE_STATE_HEADER, "archive_write_header");
611 	archive_clear_error(&a->archive);
612 
613 	if (a->format_write_header == NULL) {
614 		archive_set_error(&(a->archive), -1,
615 		    "Format must be set before you can write to an archive.");
616 		a->archive.state = ARCHIVE_STATE_FATAL;
617 		return (ARCHIVE_FATAL);
618 	}
619 
620 	/* In particular, "retry" and "fatal" get returned immediately. */
621 	ret = archive_write_finish_entry(&a->archive);
622 	if (ret == ARCHIVE_FATAL) {
623 		a->archive.state = ARCHIVE_STATE_FATAL;
624 		return (ARCHIVE_FATAL);
625 	}
626 	if (ret < ARCHIVE_OK && ret != ARCHIVE_WARN)
627 		return (ret);
628 
629 	if (a->skip_file_set &&
630 	    archive_entry_dev_is_set(entry) &&
631 	    archive_entry_ino_is_set(entry) &&
632 	    archive_entry_dev(entry) == (dev_t)a->skip_file_dev &&
633 	    archive_entry_ino64(entry) == a->skip_file_ino) {
634 		archive_set_error(&a->archive, 0,
635 		    "Can't add archive to itself");
636 		return (ARCHIVE_FAILED);
637 	}
638 
639 	/* Format and write header. */
640 	r2 = ((a->format_write_header)(a, entry));
641 	if (r2 == ARCHIVE_FATAL) {
642 		a->archive.state = ARCHIVE_STATE_FATAL;
643 		return (ARCHIVE_FATAL);
644 	}
645 	if (r2 < ret)
646 		ret = r2;
647 
648 	a->archive.state = ARCHIVE_STATE_DATA;
649 	return (ret);
650 }
651 
652 static int
653 _archive_write_finish_entry(struct archive *_a)
654 {
655 	struct archive_write *a = (struct archive_write *)_a;
656 	int ret = ARCHIVE_OK;
657 
658 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
659 	    ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
660 	    "archive_write_finish_entry");
661 	if (a->archive.state & ARCHIVE_STATE_DATA)
662 		ret = (a->format_finish_entry)(a);
663 	a->archive.state = ARCHIVE_STATE_HEADER;
664 	return (ret);
665 }
666 
667 /*
668  * Note that the compressor is responsible for blocking.
669  */
670 static ssize_t
671 _archive_write_data(struct archive *_a, const void *buff, size_t s)
672 {
673 	struct archive_write *a = (struct archive_write *)_a;
674 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
675 	    ARCHIVE_STATE_DATA, "archive_write_data");
676 	archive_clear_error(&a->archive);
677 	return ((a->format_write_data)(a, buff, s));
678 }
679 
680 static struct archive_write_filter *
681 filter_lookup(struct archive *_a, int n)
682 {
683 	struct archive_write *a = (struct archive_write *)_a;
684 	struct archive_write_filter *f = a->filter_first;
685 	if (n == -1)
686 		return a->filter_last;
687 	if (n < 0)
688 		return NULL;
689 	while (n > 0 && f != NULL) {
690 		f = f->next_filter;
691 		--n;
692 	}
693 	return f;
694 }
695 
696 static int
697 _archive_filter_code(struct archive *_a, int n)
698 {
699 	struct archive_write_filter *f = filter_lookup(_a, n);
700 	return f == NULL ? -1 : f->code;
701 }
702 
703 static const char *
704 _archive_filter_name(struct archive *_a, int n)
705 {
706 	struct archive_write_filter *f = filter_lookup(_a, n);
707 	return f == NULL ? NULL : f->name;
708 }
709 
710 static int64_t
711 _archive_filter_bytes(struct archive *_a, int n)
712 {
713 	struct archive_write_filter *f = filter_lookup(_a, n);
714 	return f == NULL ? -1 : f->bytes_written;
715 }
716