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" 27228763Smm__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); 62232153Smmstatic int64_t file_skip(struct archive *, void *, int64_t request); 63228753Smm 64228753Smmint 65228753Smmarchive_read_open_FILE(struct archive *a, FILE *f) 66228753Smm{ 67228753Smm struct stat st; 68228753Smm struct read_FILE_data *mine; 69228753Smm size_t block_size = 128 * 1024; 70228753Smm void *b; 71228753Smm 72228753Smm archive_clear_error(a); 73228753Smm mine = (struct read_FILE_data *)malloc(sizeof(*mine)); 74228753Smm b = malloc(block_size); 75228753Smm if (mine == NULL || b == NULL) { 76228753Smm archive_set_error(a, ENOMEM, "No memory"); 77228753Smm free(mine); 78228753Smm free(b); 79228753Smm return (ARCHIVE_FATAL); 80228753Smm } 81228753Smm mine->block_size = block_size; 82228753Smm mine->buffer = b; 83228753Smm mine->f = f; 84228753Smm /* 85228753Smm * If we can't fstat() the file, it may just be that it's not 86228753Smm * a file. (FILE * objects can wrap many kinds of I/O 87228753Smm * streams, some of which don't support fileno()).) 88228753Smm */ 89228753Smm if (fstat(fileno(mine->f), &st) == 0 && S_ISREG(st.st_mode)) { 90228753Smm archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino); 91228753Smm /* Enable the seek optimization only for regular files. */ 92228753Smm mine->can_skip = 1; 93228753Smm } else 94228753Smm mine->can_skip = 0; 95228753Smm 96228753Smm#if defined(__CYGWIN__) || defined(_WIN32) 97228753Smm setmode(fileno(mine->f), O_BINARY); 98228753Smm#endif 99228753Smm 100232153Smm archive_read_set_read_callback(a, file_read); 101232153Smm archive_read_set_skip_callback(a, file_skip); 102232153Smm archive_read_set_close_callback(a, file_close); 103232153Smm archive_read_set_callback_data(a, mine); 104232153Smm return (archive_read_open1(a)); 105228753Smm} 106228753Smm 107228753Smmstatic ssize_t 108228753Smmfile_read(struct archive *a, void *client_data, const void **buff) 109228753Smm{ 110228753Smm struct read_FILE_data *mine = (struct read_FILE_data *)client_data; 111248616Smm size_t bytes_read; 112228753Smm 113228753Smm *buff = mine->buffer; 114228753Smm bytes_read = fread(mine->buffer, 1, mine->block_size, mine->f); 115248616Smm if (bytes_read < mine->block_size && ferror(mine->f)) { 116228753Smm archive_set_error(a, errno, "Error reading file"); 117228753Smm } 118228753Smm return (bytes_read); 119228753Smm} 120228753Smm 121232153Smmstatic int64_t 122232153Smmfile_skip(struct archive *a, void *client_data, int64_t request) 123232153Smm{ 124232153Smm struct read_FILE_data *mine = (struct read_FILE_data *)client_data; 125232153Smm#if HAVE_FSEEKO 126232153Smm off_t skip = (off_t)request; 127232153Smm#elif HAVE__FSEEKI64 128232153Smm int64_t skip = request; 129228753Smm#else 130232153Smm long skip = (long)request; 131228753Smm#endif 132232153Smm int skip_bits = sizeof(skip) * 8 - 1; 133228753Smm 134228753Smm (void)a; /* UNUSED */ 135228753Smm 136228753Smm /* 137228753Smm * If we can't skip, return 0 as the amount we did step and 138228753Smm * the caller will work around by reading and discarding. 139228753Smm */ 140228753Smm if (!mine->can_skip) 141228753Smm return (0); 142228753Smm if (request == 0) 143228753Smm return (0); 144228753Smm 145232153Smm /* If request is too big for a long or an off_t, reduce it. */ 146232153Smm if (sizeof(request) > sizeof(skip)) { 147232153Smm int64_t max_skip = 148232153Smm (((int64_t)1 << (skip_bits - 1)) - 1) * 2 + 1; 149232153Smm if (request > max_skip) 150232153Smm skip = max_skip; 151232153Smm } 152232153Smm 153228753Smm#if HAVE_FSEEKO 154232153Smm if (fseeko(mine->f, skip, SEEK_CUR) != 0) 155232153Smm#elif HAVE__FSEEKI64 156232153Smm if (_fseeki64(mine->f, skip, SEEK_CUR) != 0) 157228753Smm#else 158232153Smm if (fseek(mine->f, skip, SEEK_CUR) != 0) 159228753Smm#endif 160228753Smm { 161228753Smm mine->can_skip = 0; 162228753Smm return (0); 163228753Smm } 164228753Smm return (request); 165228753Smm} 166228753Smm 167228753Smmstatic int 168228753Smmfile_close(struct archive *a, void *client_data) 169228753Smm{ 170228753Smm struct read_FILE_data *mine = (struct read_FILE_data *)client_data; 171228753Smm 172228753Smm (void)a; /* UNUSED */ 173228753Smm if (mine->buffer != NULL) 174228753Smm free(mine->buffer); 175228753Smm free(mine); 176228753Smm return (ARCHIVE_OK); 177228753Smm} 178