1/*- 2 * Copyright (c) 2012 Michihiro NAKAJIMA 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 31#ifdef HAVE_ERRNO_H 32#include <errno.h> 33#endif 34#ifdef HAVE_STDLIB_H 35#include <stdlib.h> 36#endif 37#ifdef HAVE_STRING_H 38#include <string.h> 39#endif 40#ifdef HAVE_UNISTD_H 41#include <unistd.h> 42#endif 43 44#include "archive.h" 45#include "archive_private.h" 46#include "archive_read_private.h" 47 48static const unsigned char grzip_magic[] = { 49 0x47, 0x52, 0x5a, 0x69, 0x70, 0x49, 0x49, 0x00, 50 0x02, 0x04, 0x3a, 0x29 }; 51 52static int grzip_bidder_bid(struct archive_read_filter_bidder *, 53 struct archive_read_filter *); 54static int grzip_bidder_init(struct archive_read_filter *); 55 56 57static int 58grzip_reader_free(struct archive_read_filter_bidder *self) 59{ 60 (void)self; /* UNUSED */ 61 return (ARCHIVE_OK); 62} 63 64int 65archive_read_support_filter_grzip(struct archive *_a) 66{ 67 struct archive_read *a = (struct archive_read *)_a; 68 struct archive_read_filter_bidder *reader; 69 70 archive_check_magic(_a, ARCHIVE_READ_MAGIC, 71 ARCHIVE_STATE_NEW, "archive_read_support_filter_grzip"); 72 73 if (__archive_read_get_bidder(a, &reader) != ARCHIVE_OK) 74 return (ARCHIVE_FATAL); 75 76 reader->data = NULL; 77 reader->bid = grzip_bidder_bid; 78 reader->init = grzip_bidder_init; 79 reader->options = NULL; 80 reader->free = grzip_reader_free; 81 /* This filter always uses an external program. */ 82 archive_set_error(_a, ARCHIVE_ERRNO_MISC, 83 "Using external grzip program for grzip decompression"); 84 return (ARCHIVE_WARN); 85} 86 87/* 88 * Bidder just verifies the header and returns the number of verified bits. 89 */ 90static int 91grzip_bidder_bid(struct archive_read_filter_bidder *self, 92 struct archive_read_filter *filter) 93{ 94 const unsigned char *p; 95 ssize_t avail; 96 97 (void)self; /* UNUSED */ 98 99 p = __archive_read_filter_ahead(filter, sizeof(grzip_magic), &avail); 100 if (p == NULL || avail == 0) 101 return (0); 102 103 if (memcmp(p, grzip_magic, sizeof(grzip_magic))) 104 return (0); 105 106 return (sizeof(grzip_magic) * 8); 107} 108 109static int 110grzip_bidder_init(struct archive_read_filter *self) 111{ 112 int r; 113 114 r = __archive_read_program(self, "grzip -d"); 115 /* Note: We set the format here even if __archive_read_program() 116 * above fails. We do, after all, know what the format is 117 * even if we weren't able to read it. */ 118 self->code = ARCHIVE_FILTER_GRZIP; 119 self->name = "grzip"; 120 return (r); 121} 122