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