xref: /freebsd/contrib/libarchive/libarchive/archive_read_open_fd.c (revision 401026e4825a05abba6f945cf1b74b3328876fa2)
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 #include "archive_platform_stat.h"
52 
53 struct read_fd_data {
54 	int	 fd;
55 	size_t	 block_size;
56 	int64_t	 size;
57 	char	 use_lseek;
58 	void	*buffer;
59 };
60 
61 static int	file_close(struct archive *, void *);
62 static ssize_t	file_read(struct archive *, void *, const void **buff);
63 static int64_t	file_seek(struct archive *, void *, int64_t request, int);
64 static int64_t	file_skip(struct archive *, void *, int64_t request);
65 
66 int
archive_read_open_fd(struct archive * a,int fd,size_t block_size)67 archive_read_open_fd(struct archive *a, int fd, size_t block_size)
68 {
69 	la_seek_stat_t st;
70 	struct read_fd_data *mine;
71 	void *b;
72 
73 	archive_clear_error(a);
74 	if (la_seek_fstat(fd, &st) != 0) {
75 		archive_set_error(a, errno, "Can't stat fd %d", fd);
76 		return (ARCHIVE_FATAL);
77 	}
78 
79 	mine = calloc(1, sizeof(*mine));
80 	b = malloc(block_size);
81 	if (mine == NULL || b == NULL) {
82 		archive_set_error(a, ENOMEM, "No memory");
83 		free(mine);
84 		free(b);
85 		return (ARCHIVE_FATAL);
86 	}
87 	mine->block_size = block_size;
88 	mine->buffer = b;
89 	mine->fd = fd;
90 	/*
91 	 * Skip support is a performance optimization for anything
92 	 * that supports lseek().  On FreeBSD, only regular files and
93 	 * raw disk devices support lseek() and there's no portable
94 	 * way to determine if a device is a raw disk device, so we
95 	 * only enable this optimization for regular files.
96 	 */
97 	if (S_ISREG(st.st_mode)) {
98 		archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
99 		mine->use_lseek = 1;
100 		mine->size = st.st_size;
101 	}
102 #if defined(__CYGWIN__) || defined(_WIN32)
103 	setmode(mine->fd, O_BINARY);
104 #endif
105 
106 	archive_read_set_read_callback(a, file_read);
107 	archive_read_set_skip_callback(a, file_skip);
108 	archive_read_set_seek_callback(a, file_seek);
109 	archive_read_set_close_callback(a, file_close);
110 	archive_read_set_callback_data(a, mine);
111 	return (archive_read_open1(a));
112 }
113 
114 static ssize_t
file_read(struct archive * a,void * client_data,const void ** buff)115 file_read(struct archive *a, void *client_data, const void **buff)
116 {
117 	struct read_fd_data *mine = (struct read_fd_data *)client_data;
118 	ssize_t bytes_read;
119 
120 	*buff = mine->buffer;
121 	for (;;) {
122 		bytes_read = read(mine->fd, mine->buffer, mine->block_size);
123 		if (bytes_read < 0) {
124 			if (errno == EINTR)
125 				continue;
126 			archive_set_error(a, errno, "Error reading fd %d",
127 			    mine->fd);
128 		}
129 		return (bytes_read);
130 	}
131 }
132 
133 static int64_t
file_skip(struct archive * a,void * client_data,int64_t request)134 file_skip(struct archive *a, void *client_data, int64_t request)
135 {
136 	struct read_fd_data *mine = (struct read_fd_data *)client_data;
137 	la_seek_t skip = (la_seek_t)request;
138 	int64_t old_offset, new_offset;
139 	int skip_bits = sizeof(skip) * 8 - 1;  /* off_t is a signed type. */
140 
141 	if (!mine->use_lseek)
142 		return (0);
143 
144 	/* Reduce a request that would overflow the 'skip' variable. */
145 	if (sizeof(request) > sizeof(skip)) {
146 		const int64_t max_skip =
147 		    (((int64_t)1 << (skip_bits - 1)) - 1) * 2 + 1;
148 		if (request > max_skip)
149 			skip = (off_t)max_skip;
150 	}
151 
152 	/* Reduce 'skip' to the next smallest multiple of block_size */
153 	skip = (la_seek_t)(((int64_t)skip / mine->block_size) * mine->block_size);
154 
155 	if (skip == 0)
156 		return (0);
157 
158 	if ((old_offset = lseek(mine->fd, 0, SEEK_CUR)) >= 0) {
159 		if (old_offset >= mine->size ||
160 		    skip > mine->size - old_offset) {
161 			/* Do not seek past end of file. */
162 			errno = ESPIPE;
163 		} else if ((new_offset = lseek(mine->fd, skip, SEEK_CUR)) >= 0)
164 			return (new_offset - old_offset);
165 	}
166 
167 	/* If seek failed once, it will probably fail again. */
168 	mine->use_lseek = 0;
169 
170 	/* Let libarchive recover with read+discard. */
171 	if (errno == ESPIPE)
172 		return (0);
173 
174 	/*
175 	 * There's been an error other than ESPIPE. This is most
176 	 * likely caused by a programmer error (too large request)
177 	 * or a corrupted archive file.
178 	 */
179 	archive_set_error(a, errno, "Error seeking");
180 	return (-1);
181 }
182 
183 /*
184  * TODO: Store the offset and use it in the read callback.
185  */
186 static int64_t
file_seek(struct archive * a,void * client_data,int64_t request,int whence)187 file_seek(struct archive *a, void *client_data, int64_t request, int whence)
188 {
189 	struct read_fd_data *mine = (struct read_fd_data *)client_data;
190 	la_seek_t seek = (la_seek_t)request;
191 	int64_t r;
192 	int seek_bits = sizeof(seek) * 8 - 1;  /* off_t is a signed type. */
193 
194 	/* We use off_t here because lseek() is declared that way. */
195 
196 	/* Do not perform a seek which cannot be fulfilled. */
197 	if (sizeof(request) > sizeof(seek)) {
198 		const int64_t max_seek =
199 		    (((int64_t)1 << (seek_bits - 1)) - 1) * 2 + 1;
200 		const int64_t min_seek = ~max_seek;
201 		if (request < min_seek || request > max_seek) {
202 			errno = EOVERFLOW;
203 			goto err;
204 		}
205 	}
206 
207 	r = lseek(mine->fd, seek, whence);
208 	if (r >= 0)
209 		return r;
210 
211 err:
212 	if (errno == ESPIPE) {
213 		archive_set_error(a, errno,
214 		    "A file descriptor(%d) is not seekable(PIPE)", mine->fd);
215 		return (ARCHIVE_FAILED);
216 	} else {
217 		/* If the input is corrupted or truncated, fail. */
218 		archive_set_error(a, errno,
219 		    "Error seeking in a file descriptor(%d)", mine->fd);
220 		return (ARCHIVE_FATAL);
221 	}
222 }
223 
224 static int
file_close(struct archive * a,void * client_data)225 file_close(struct archive *a, void *client_data)
226 {
227 	struct read_fd_data *mine = (struct read_fd_data *)client_data;
228 
229 	(void)a; /* UNUSED */
230 	free(mine->buffer);
231 	free(mine);
232 	return (ARCHIVE_OK);
233 }
234