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 28 __FBSDID("$FreeBSD$"); 29 30 #ifdef HAVE_ERRNO_H 31 #include <errno.h> 32 #endif 33 #ifdef HAVE_STDLIB_H 34 #include <stdlib.h> 35 #endif 36 37 #include "archive.h" 38 #include "archive_write_private.h" 39 40 struct write_grzip { 41 struct archive_write_program_data *pdata; 42 }; 43 44 static int archive_write_grzip_open(struct archive_write_filter *); 45 static int archive_write_grzip_options(struct archive_write_filter *, 46 const char *, const char *); 47 static int archive_write_grzip_write(struct archive_write_filter *, 48 const void *, size_t); 49 static int archive_write_grzip_close(struct archive_write_filter *); 50 static int archive_write_grzip_free(struct archive_write_filter *); 51 52 int 53 archive_write_add_filter_grzip(struct archive *_a) 54 { 55 struct archive_write_filter *f = __archive_write_allocate_filter(_a); 56 struct write_grzip *data; 57 58 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, 59 ARCHIVE_STATE_NEW, "archive_write_add_filter_grzip"); 60 61 data = calloc(1, sizeof(*data)); 62 if (data == NULL) { 63 archive_set_error(_a, ENOMEM, "Can't allocate memory"); 64 return (ARCHIVE_FATAL); 65 } 66 data->pdata = __archive_write_program_allocate("grzip"); 67 if (data->pdata == NULL) { 68 free(data); 69 archive_set_error(_a, ENOMEM, "Can't allocate memory"); 70 return (ARCHIVE_FATAL); 71 } 72 73 f->name = "grzip"; 74 f->code = ARCHIVE_FILTER_GRZIP; 75 f->data = data; 76 f->open = archive_write_grzip_open; 77 f->options = archive_write_grzip_options; 78 f->write = archive_write_grzip_write; 79 f->close = archive_write_grzip_close; 80 f->free = archive_write_grzip_free; 81 82 /* Note: This filter always uses an external program, so we 83 * return "warn" to inform of the fact. */ 84 archive_set_error(_a, ARCHIVE_ERRNO_MISC, 85 "Using external grzip program for grzip compression"); 86 return (ARCHIVE_WARN); 87 } 88 89 static int 90 archive_write_grzip_options(struct archive_write_filter *f, const char *key, 91 const char *value) 92 { 93 (void)f; /* UNUSED */ 94 (void)key; /* UNUSED */ 95 (void)value; /* UNUSED */ 96 /* Note: The "warn" return is just to inform the options 97 * supervisor that we didn't handle it. It will generate 98 * a suitable error if no one used this option. */ 99 return (ARCHIVE_WARN); 100 } 101 102 static int 103 archive_write_grzip_open(struct archive_write_filter *f) 104 { 105 struct write_grzip *data = (struct write_grzip *)f->data; 106 107 return __archive_write_program_open(f, data->pdata, "grzip"); 108 } 109 110 static int 111 archive_write_grzip_write(struct archive_write_filter *f, 112 const void *buff, size_t length) 113 { 114 struct write_grzip *data = (struct write_grzip *)f->data; 115 116 return __archive_write_program_write(f, data->pdata, buff, length); 117 } 118 119 static int 120 archive_write_grzip_close(struct archive_write_filter *f) 121 { 122 struct write_grzip *data = (struct write_grzip *)f->data; 123 124 return __archive_write_program_close(f, data->pdata); 125 } 126 127 static int 128 archive_write_grzip_free(struct archive_write_filter *f) 129 { 130 struct write_grzip *data = (struct write_grzip *)f->data; 131 132 __archive_write_program_free(data->pdata); 133 free(data); 134 return (ARCHIVE_OK); 135 } 136