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: stable/10/contrib/libarchive/libarchive/archive_read_open_file.c 344674 2019-02-28 22:57:09Z mm $");
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
86302001Smm	 * a file.  (On some platforms, FILE * objects can wrap I/O
87302001Smm	 * streams that don't support fileno()).  As a result, fileno()
88302001Smm	 * should be used cautiously.)
89228753Smm	 */
90228753Smm	if (fstat(fileno(mine->f), &st) == 0 && S_ISREG(st.st_mode)) {
91228753Smm		archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
92228753Smm		/* Enable the seek optimization only for regular files. */
93228753Smm		mine->can_skip = 1;
94228753Smm	} else
95228753Smm		mine->can_skip = 0;
96228753Smm
97228753Smm#if defined(__CYGWIN__) || defined(_WIN32)
98228753Smm	setmode(fileno(mine->f), O_BINARY);
99228753Smm#endif
100228753Smm
101232153Smm	archive_read_set_read_callback(a, file_read);
102232153Smm	archive_read_set_skip_callback(a, file_skip);
103232153Smm	archive_read_set_close_callback(a, file_close);
104232153Smm	archive_read_set_callback_data(a, mine);
105232153Smm	return (archive_read_open1(a));
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;
112248616Smm	size_t bytes_read;
113228753Smm
114228753Smm	*buff = mine->buffer;
115228753Smm	bytes_read = fread(mine->buffer, 1, mine->block_size, mine->f);
116248616Smm	if (bytes_read < mine->block_size && ferror(mine->f)) {
117228753Smm		archive_set_error(a, errno, "Error reading file");
118228753Smm	}
119228753Smm	return (bytes_read);
120228753Smm}
121228753Smm
122232153Smmstatic int64_t
123232153Smmfile_skip(struct archive *a, void *client_data, int64_t request)
124232153Smm{
125232153Smm	struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
126232153Smm#if HAVE_FSEEKO
127232153Smm	off_t skip = (off_t)request;
128232153Smm#elif HAVE__FSEEKI64
129232153Smm	int64_t skip = request;
130228753Smm#else
131232153Smm	long skip = (long)request;
132228753Smm#endif
133232153Smm	int skip_bits = sizeof(skip) * 8 - 1;
134228753Smm
135228753Smm	(void)a; /* UNUSED */
136228753Smm
137228753Smm	/*
138228753Smm	 * If we can't skip, return 0 as the amount we did step and
139228753Smm	 * the caller will work around by reading and discarding.
140228753Smm	 */
141228753Smm	if (!mine->can_skip)
142228753Smm		return (0);
143228753Smm	if (request == 0)
144228753Smm		return (0);
145228753Smm
146232153Smm	/* If request is too big for a long or an off_t, reduce it. */
147232153Smm	if (sizeof(request) > sizeof(skip)) {
148232153Smm		int64_t max_skip =
149232153Smm		    (((int64_t)1 << (skip_bits - 1)) - 1) * 2 + 1;
150232153Smm		if (request > max_skip)
151232153Smm			skip = max_skip;
152232153Smm	}
153232153Smm
154302001Smm#ifdef __ANDROID__
155302001Smm        /* fileno() isn't safe on all platforms ... see above. */
156302001Smm	if (lseek(fileno(mine->f), skip, SEEK_CUR) < 0)
157302001Smm#elif HAVE_FSEEKO
158232153Smm	if (fseeko(mine->f, skip, SEEK_CUR) != 0)
159232153Smm#elif HAVE__FSEEKI64
160232153Smm	if (_fseeki64(mine->f, skip, SEEK_CUR) != 0)
161228753Smm#else
162232153Smm	if (fseek(mine->f, skip, SEEK_CUR) != 0)
163228753Smm#endif
164228753Smm	{
165228753Smm		mine->can_skip = 0;
166228753Smm		return (0);
167228753Smm	}
168228753Smm	return (request);
169228753Smm}
170228753Smm
171228753Smmstatic int
172228753Smmfile_close(struct archive *a, void *client_data)
173228753Smm{
174228753Smm	struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
175228753Smm
176228753Smm	(void)a; /* UNUSED */
177344674Smm	free(mine->buffer);
178228753Smm	free(mine);
179228753Smm	return (ARCHIVE_OK);
180228753Smm}
181