xref: /freebsd/contrib/libarchive/libarchive/archive_read_open_file.c (revision 2e113ef82465598b8c26e0ca415fbe90677fbd47)
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 #ifdef HAVE_SYS_STAT_H
29 #include <sys/stat.h>
30 #endif
31 #ifdef HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
34 #ifdef HAVE_FCNTL_H
35 #include <fcntl.h>
36 #endif
37 #ifdef HAVE_IO_H
38 #include <io.h>
39 #endif
40 #ifdef HAVE_STDLIB_H
41 #include <stdlib.h>
42 #endif
43 #ifdef HAVE_STRING_H
44 #include <string.h>
45 #endif
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
49 
50 #include "archive.h"
51 
52 struct read_FILE_data {
53 	FILE    *f;
54 	size_t	 block_size;
55 	int64_t	 size;
56 	void	*buffer;
57 	char	 can_skip;
58 };
59 
60 static int	FILE_close(struct archive *, void *);
61 static ssize_t	FILE_read(struct archive *, void *, const void **buff);
62 static int64_t	FILE_seek(struct archive *, void *, int64_t, int);
63 static int64_t	FILE_skip(struct archive *, void *, int64_t);
64 
65 int
archive_read_open_FILE(struct archive * a,FILE * f)66 archive_read_open_FILE(struct archive *a, FILE *f)
67 {
68 	struct stat st;
69 	struct read_FILE_data *mine;
70 	size_t block_size = 128 * 1024;
71 	void *b;
72 
73 	archive_clear_error(a);
74 	mine = calloc(1, sizeof(*mine));
75 	b = malloc(block_size);
76 	if (mine == NULL || b == NULL) {
77 		archive_set_error(a, ENOMEM, "No memory");
78 		free(mine);
79 		free(b);
80 		return (ARCHIVE_FATAL);
81 	}
82 	mine->block_size = block_size;
83 	mine->buffer = b;
84 	mine->f = f;
85 	/*
86 	 * If we can't fstat() the file, it may just be that it's not
87 	 * a file.  (On some platforms, FILE * objects can wrap I/O
88 	 * streams that don't support fileno()).  As a result, fileno()
89 	 * should be used cautiously.)
90 	 */
91 	if (fstat(fileno(mine->f), &st) == 0 && S_ISREG(st.st_mode)) {
92 		archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
93 		/* Enable the seek optimization only for regular files. */
94 		mine->can_skip = 1;
95 		mine->size = st.st_size;
96 	}
97 
98 #if defined(__CYGWIN__) || defined(_WIN32)
99 	setmode(fileno(mine->f), O_BINARY);
100 #endif
101 
102 	archive_read_set_read_callback(a, FILE_read);
103 	archive_read_set_skip_callback(a, FILE_skip);
104 	archive_read_set_seek_callback(a, FILE_seek);
105 	archive_read_set_close_callback(a, FILE_close);
106 	archive_read_set_callback_data(a, mine);
107 	return (archive_read_open1(a));
108 }
109 
110 static ssize_t
FILE_read(struct archive * a,void * client_data,const void ** buff)111 FILE_read(struct archive *a, void *client_data, const void **buff)
112 {
113 	struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
114 	size_t bytes_read;
115 
116 	*buff = mine->buffer;
117 	bytes_read = fread(mine->buffer, 1, mine->block_size, mine->f);
118 	if (bytes_read < mine->block_size && ferror(mine->f)) {
119 		archive_set_error(a, errno, "Error reading file");
120 	}
121 	return (bytes_read);
122 }
123 
124 static int64_t
FILE_skip(struct archive * a,void * client_data,int64_t request)125 FILE_skip(struct archive *a, void *client_data, int64_t request)
126 {
127 	struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
128 #if HAVE__FSEEKI64
129 	int64_t skip = request;
130 #elif HAVE_FSEEKO
131 	off_t skip = (off_t)request;
132 #else
133 	long skip = (long)request;
134 #endif
135 	int64_t old_offset, new_offset = -1;
136 	int skip_bits = sizeof(skip) * 8 - 1;
137 
138 	(void)a; /* UNUSED */
139 
140 	/*
141 	 * If we can't skip, return 0 as the amount we did step and
142 	 * the caller will work around by reading and discarding.
143 	 */
144 	if (!mine->can_skip)
145 		return (0);
146 	if (request == 0)
147 		return (0);
148 
149 	/* If request is too big for a long or an off_t, reduce it. */
150 	if (sizeof(request) > sizeof(skip)) {
151 		const int64_t max_skip =
152 		    (((int64_t)1 << (skip_bits - 1)) - 1) * 2 + 1;
153 		if (request > max_skip)
154 			skip = max_skip;
155 	}
156 
157 #ifdef __ANDROID__
158         /* fileno() isn't safe on all platforms ... see above. */
159 	old_offset = lseek(fileno(mine->f), 0, SEEK_CUR);
160 #elif HAVE__FSEEKI64
161 	old_offset = _ftelli64(mine->f);
162 #elif HAVE_FSEEKO
163 	old_offset = ftello(mine->f);
164 #else
165 	old_offset = ftell(mine->f);
166 #endif
167 	if (old_offset >= 0) {
168 		if (old_offset < mine->size &&
169 		    skip <= mine->size - old_offset) {
170 #ifdef __ANDROID__
171 			new_offset = lseek(fileno(mine->f), skip, SEEK_CUR);
172 #elif HAVE__FSEEKI64
173 			if (_fseeki64(mine->f, skip, SEEK_CUR) == 0)
174 				new_offset = _ftelli64(mine->f);
175 #elif HAVE_FSEEKO
176 			if (fseeko(mine->f, skip, SEEK_CUR) == 0)
177 				new_offset = ftello(mine->f);
178 #else
179 			if (fseek(mine->f, skip, SEEK_CUR) == 0)
180 				new_offset = ftell(mine->f);
181 #endif
182 			if (new_offset >= 0)
183 				return (new_offset - old_offset);
184 		}
185 	}
186 
187 	mine->can_skip = 0;
188 	return (0);
189 }
190 
191 /*
192  * TODO: Store the offset and use it in the read callback.
193  */
194 static int64_t
FILE_seek(struct archive * a,void * client_data,int64_t request,int whence)195 FILE_seek(struct archive *a, void *client_data, int64_t request, int whence)
196 {
197 	struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
198 #if HAVE__FSEEKI64
199 	int64_t seek = request;
200 #elif HAVE_FSEEKO
201 	off_t seek = (off_t)request;
202 #else
203 	long seek = (long)request;
204 #endif
205 	int seek_bits = sizeof(seek) * 8 - 1;
206 	(void)a; /* UNUSED */
207 
208 	/* Reduce a request that would overflow the 'seek' variable. */
209 	if (sizeof(request) > sizeof(seek)) {
210 		const int64_t max_seek =
211 		    (((int64_t)1 << (seek_bits - 1)) - 1) * 2 + 1;
212 		const int64_t min_seek = ~max_seek;
213 		if (request > max_seek)
214 			seek = max_seek;
215 		else if (request < min_seek)
216 			seek = min_seek;
217 	}
218 
219 #ifdef __ANDROID__
220 	/* Newer Android versions have fseeko...to meditate. */
221 	int64_t ret = lseek(fileno(mine->f), seek, whence);
222 	if (ret >= 0) {
223 		return ret;
224 	}
225 #elif HAVE__FSEEKI64
226 	if (_fseeki64(mine->f, seek, whence) == 0) {
227 		return _ftelli64(mine->f);
228 	}
229 #elif HAVE_FSEEKO
230 	if (fseeko(mine->f, seek, whence) == 0) {
231 		return ftello(mine->f);
232 	}
233 #else
234 	if (fseek(mine->f, seek, whence) == 0) {
235 		return ftell(mine->f);
236 	}
237 #endif
238 	/* If we arrive here, the input is corrupted or truncated so fail. */
239 	archive_set_error(a, errno, "Error seeking in FILE* pointer");
240 	return (ARCHIVE_FATAL);
241 }
242 
243 static int
FILE_close(struct archive * a,void * client_data)244 FILE_close(struct archive *a, void *client_data)
245 {
246 	struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
247 
248 	(void)a; /* UNUSED */
249 	free(mine->buffer);
250 	free(mine);
251 	return (ARCHIVE_OK);
252 }
253