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 void *buffer;
56 char can_skip;
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_skip(struct archive *, void *, int64_t request);
62
63 int
archive_read_open_FILE(struct archive * a,FILE * f)64 archive_read_open_FILE(struct archive *a, FILE *f)
65 {
66 struct stat st;
67 struct read_FILE_data *mine;
68 size_t block_size = 128 * 1024;
69 void *b;
70
71 archive_clear_error(a);
72 mine = malloc(sizeof(*mine));
73 b = malloc(block_size);
74 if (mine == NULL || b == NULL) {
75 archive_set_error(a, ENOMEM, "No memory");
76 free(mine);
77 free(b);
78 return (ARCHIVE_FATAL);
79 }
80 mine->block_size = block_size;
81 mine->buffer = b;
82 mine->f = f;
83 /*
84 * If we can't fstat() the file, it may just be that it's not
85 * a file. (On some platforms, FILE * objects can wrap I/O
86 * streams that don't support fileno()). As a result, fileno()
87 * should be used cautiously.)
88 */
89 if (fstat(fileno(mine->f), &st) == 0 && S_ISREG(st.st_mode)) {
90 archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
91 /* Enable the seek optimization only for regular files. */
92 mine->can_skip = 1;
93 } else
94 mine->can_skip = 0;
95
96 #if defined(__CYGWIN__) || defined(_WIN32)
97 setmode(fileno(mine->f), O_BINARY);
98 #endif
99
100 archive_read_set_read_callback(a, file_read);
101 archive_read_set_skip_callback(a, file_skip);
102 archive_read_set_close_callback(a, file_close);
103 archive_read_set_callback_data(a, mine);
104 return (archive_read_open1(a));
105 }
106
107 static ssize_t
file_read(struct archive * a,void * client_data,const void ** buff)108 file_read(struct archive *a, void *client_data, const void **buff)
109 {
110 struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
111 size_t bytes_read;
112
113 *buff = mine->buffer;
114 bytes_read = fread(mine->buffer, 1, mine->block_size, mine->f);
115 if (bytes_read < mine->block_size && ferror(mine->f)) {
116 archive_set_error(a, errno, "Error reading file");
117 }
118 return (bytes_read);
119 }
120
121 static int64_t
file_skip(struct archive * a,void * client_data,int64_t request)122 file_skip(struct archive *a, void *client_data, int64_t request)
123 {
124 struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
125 #if HAVE_FSEEKO
126 off_t skip = (off_t)request;
127 #elif HAVE__FSEEKI64
128 int64_t skip = request;
129 #else
130 long skip = (long)request;
131 #endif
132 int skip_bits = sizeof(skip) * 8 - 1;
133
134 (void)a; /* UNUSED */
135
136 /*
137 * If we can't skip, return 0 as the amount we did step and
138 * the caller will work around by reading and discarding.
139 */
140 if (!mine->can_skip)
141 return (0);
142 if (request == 0)
143 return (0);
144
145 /* If request is too big for a long or an off_t, reduce it. */
146 if (sizeof(request) > sizeof(skip)) {
147 int64_t max_skip =
148 (((int64_t)1 << (skip_bits - 1)) - 1) * 2 + 1;
149 if (request > max_skip)
150 skip = max_skip;
151 }
152
153 #ifdef __ANDROID__
154 /* fileno() isn't safe on all platforms ... see above. */
155 if (lseek(fileno(mine->f), skip, SEEK_CUR) < 0)
156 #elif HAVE__FSEEKI64
157 if (_fseeki64(mine->f, skip, SEEK_CUR) != 0)
158 #elif HAVE_FSEEKO
159 if (fseeko(mine->f, skip, SEEK_CUR) != 0)
160 #else
161 if (fseek(mine->f, skip, SEEK_CUR) != 0)
162 #endif
163 {
164 mine->can_skip = 0;
165 return (0);
166 }
167 return (request);
168 }
169
170 static int
file_close(struct archive * a,void * client_data)171 file_close(struct archive *a, void *client_data)
172 {
173 struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
174
175 (void)a; /* UNUSED */
176 free(mine->buffer);
177 free(mine);
178 return (ARCHIVE_OK);
179 }
180