1228753Smm/*- 2228753Smm * Copyright (c) 2003-2007 Tim Kientzle 3228753Smm * All rights reserved. 4228753Smm * 5228753Smm * Redistribution and use in source and binary forms, with or without 6228753Smm * modification, are permitted provided that the following conditions 7228753Smm * are met: 8228753Smm * 1. Redistributions of source code must retain the above copyright 9228753Smm * notice, this list of conditions and the following disclaimer. 10228753Smm * 2. Redistributions in binary form must reproduce the above copyright 11228753Smm * notice, this list of conditions and the following disclaimer in the 12228753Smm * documentation and/or other materials provided with the distribution. 13228753Smm * 14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24228753Smm */ 25228753Smm 26228753Smm#include "archive_platform.h" 27229592Smm__FBSDID("$FreeBSD$"); 28228753Smm 29228753Smm#ifdef HAVE_SYS_STAT_H 30228753Smm#include <sys/stat.h> 31228753Smm#endif 32228753Smm#ifdef HAVE_ERRNO_H 33228753Smm#include <errno.h> 34228753Smm#endif 35228753Smm#ifdef HAVE_FCNTL_H 36228753Smm#include <fcntl.h> 37228753Smm#endif 38228753Smm#ifdef HAVE_IO_H 39228753Smm#include <io.h> 40228753Smm#endif 41228753Smm#ifdef HAVE_STDLIB_H 42228753Smm#include <stdlib.h> 43228753Smm#endif 44228753Smm#ifdef HAVE_STRING_H 45228753Smm#include <string.h> 46228753Smm#endif 47228753Smm#ifdef HAVE_UNISTD_H 48228753Smm#include <unistd.h> 49228753Smm#endif 50228753Smm 51228753Smm#include "archive.h" 52228753Smm 53228753Smmstruct read_FILE_data { 54228753Smm FILE *f; 55228753Smm size_t block_size; 56228753Smm void *buffer; 57228753Smm char can_skip; 58228753Smm}; 59228753Smm 60228753Smmstatic int file_close(struct archive *, void *); 61228753Smmstatic ssize_t file_read(struct archive *, void *, const void **buff); 62228753Smm#if ARCHIVE_API_VERSION < 2 63228753Smmstatic ssize_t file_skip(struct archive *, void *, size_t request); 64228753Smm#else 65228753Smmstatic off_t file_skip(struct archive *, void *, off_t request); 66228753Smm#endif 67228753Smm 68228753Smmint 69228753Smmarchive_read_open_FILE(struct archive *a, FILE *f) 70228753Smm{ 71228753Smm struct stat st; 72228753Smm struct read_FILE_data *mine; 73228753Smm size_t block_size = 128 * 1024; 74228753Smm void *b; 75228753Smm 76228753Smm archive_clear_error(a); 77228753Smm mine = (struct read_FILE_data *)malloc(sizeof(*mine)); 78228753Smm b = malloc(block_size); 79228753Smm if (mine == NULL || b == NULL) { 80228753Smm archive_set_error(a, ENOMEM, "No memory"); 81228753Smm free(mine); 82228753Smm free(b); 83228753Smm return (ARCHIVE_FATAL); 84228753Smm } 85228753Smm mine->block_size = block_size; 86228753Smm mine->buffer = b; 87228753Smm mine->f = f; 88228753Smm /* 89228753Smm * If we can't fstat() the file, it may just be that it's not 90228753Smm * a file. (FILE * objects can wrap many kinds of I/O 91228753Smm * streams, some of which don't support fileno()).) 92228753Smm */ 93228753Smm if (fstat(fileno(mine->f), &st) == 0 && S_ISREG(st.st_mode)) { 94228753Smm archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino); 95228753Smm /* Enable the seek optimization only for regular files. */ 96228753Smm mine->can_skip = 1; 97228753Smm } else 98228753Smm mine->can_skip = 0; 99228753Smm 100228753Smm#if defined(__CYGWIN__) || defined(_WIN32) 101228753Smm setmode(fileno(mine->f), O_BINARY); 102228753Smm#endif 103228753Smm 104228753Smm return (archive_read_open2(a, mine, NULL, file_read, 105228753Smm file_skip, file_close)); 106228753Smm} 107228753Smm 108228753Smmstatic ssize_t 109228753Smmfile_read(struct archive *a, void *client_data, const void **buff) 110228753Smm{ 111228753Smm struct read_FILE_data *mine = (struct read_FILE_data *)client_data; 112228753Smm ssize_t bytes_read; 113228753Smm 114228753Smm *buff = mine->buffer; 115228753Smm bytes_read = fread(mine->buffer, 1, mine->block_size, mine->f); 116228753Smm if (bytes_read < 0) { 117228753Smm archive_set_error(a, errno, "Error reading file"); 118228753Smm } 119228753Smm return (bytes_read); 120228753Smm} 121228753Smm 122228753Smm#if ARCHIVE_API_VERSION < 2 123228753Smmstatic ssize_t 124228753Smmfile_skip(struct archive *a, void *client_data, size_t request) 125228753Smm#else 126228753Smmstatic off_t 127228753Smmfile_skip(struct archive *a, void *client_data, off_t request) 128228753Smm#endif 129228753Smm{ 130228753Smm struct read_FILE_data *mine = (struct read_FILE_data *)client_data; 131228753Smm 132228753Smm (void)a; /* UNUSED */ 133228753Smm 134228753Smm /* 135228753Smm * If we can't skip, return 0 as the amount we did step and 136228753Smm * the caller will work around by reading and discarding. 137228753Smm */ 138228753Smm if (!mine->can_skip) 139228753Smm return (0); 140228753Smm if (request == 0) 141228753Smm return (0); 142228753Smm 143228753Smm#if HAVE_FSEEKO 144228753Smm if (fseeko(mine->f, request, SEEK_CUR) != 0) 145228753Smm#else 146228753Smm if (fseek(mine->f, request, SEEK_CUR) != 0) 147228753Smm#endif 148228753Smm { 149228753Smm mine->can_skip = 0; 150228753Smm return (0); 151228753Smm } 152228753Smm return (request); 153228753Smm} 154228753Smm 155228753Smmstatic int 156228753Smmfile_close(struct archive *a, void *client_data) 157228753Smm{ 158228753Smm struct read_FILE_data *mine = (struct read_FILE_data *)client_data; 159228753Smm 160228753Smm (void)a; /* UNUSED */ 161228753Smm if (mine->buffer != NULL) 162228753Smm free(mine->buffer); 163228753Smm free(mine); 164228753Smm return (ARCHIVE_OK); 165228753Smm} 166