Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
| 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
| 6 | * |
| 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 8 | */ |
| 9 | |
| 10 | #include "libbb.h" |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 11 | |
| 12 | #define ZIPPED (ENABLE_FEATURE_SEAMLESS_LZMA \ |
| 13 | || ENABLE_FEATURE_SEAMLESS_BZ2 \ |
| 14 | || ENABLE_FEATURE_SEAMLESS_GZ \ |
| 15 | /* || ENABLE_FEATURE_SEAMLESS_Z */ \ |
| 16 | ) |
| 17 | |
| 18 | #if ZIPPED |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 19 | #include "unarchive.h" |
| 20 | #endif |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 21 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 22 | ssize_t FAST_FUNC safe_read(int fd, void *buf, size_t count) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 23 | { |
| 24 | ssize_t n; |
| 25 | |
| 26 | do { |
| 27 | n = read(fd, buf, count); |
| 28 | } while (n < 0 && errno == EINTR); |
| 29 | |
| 30 | return n; |
| 31 | } |
| 32 | |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 33 | /* Suppose that you are a shell. You start child processes. |
| 34 | * They work and eventually exit. You want to get user input. |
| 35 | * You read stdin. But what happens if last child switched |
| 36 | * its stdin into O_NONBLOCK mode? |
| 37 | * |
| 38 | * *** SURPRISE! It will affect the parent too! *** |
| 39 | * *** BIG SURPRISE! It stays even after child exits! *** |
| 40 | * |
| 41 | * This is a design bug in UNIX API. |
| 42 | * fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) | O_NONBLOCK); |
| 43 | * will set nonblocking mode not only on _your_ stdin, but |
| 44 | * also on stdin of your parent, etc. |
| 45 | * |
| 46 | * In general, |
| 47 | * fd2 = dup(fd1); |
| 48 | * fcntl(fd2, F_SETFL, fcntl(fd2, F_GETFL, 0) | O_NONBLOCK); |
| 49 | * sets both fd1 and fd2 to O_NONBLOCK. This includes cases |
| 50 | * where duping is done implicitly by fork() etc. |
| 51 | * |
| 52 | * We need |
| 53 | * fcntl(fd2, F_SETFD, fcntl(fd2, F_GETFD, 0) | O_NONBLOCK); |
| 54 | * (note SETFD, not SETFL!) but such thing doesn't exist. |
| 55 | * |
| 56 | * Alternatively, we need nonblocking_read(fd, ...) which doesn't |
| 57 | * require O_NONBLOCK dance at all. Actually, it exists: |
| 58 | * n = recv(fd, buf, len, MSG_DONTWAIT); |
| 59 | * "MSG_DONTWAIT: |
| 60 | * Enables non-blocking operation; if the operation |
| 61 | * would block, EAGAIN is returned." |
| 62 | * but recv() works only for sockets! |
| 63 | * |
| 64 | * So far I don't see any good solution, I can only propose |
| 65 | * that affected readers should be careful and use this routine, |
| 66 | * which detects EAGAIN and uses poll() to wait on the fd. |
Denis Vlasenko | 081efd1 | 2008-02-20 22:57:24 +0000 | [diff] [blame] | 67 | * Thankfully, poll() doesn't care about O_NONBLOCK flag. |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 68 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 69 | ssize_t FAST_FUNC nonblock_safe_read(int fd, void *buf, size_t count) |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 70 | { |
| 71 | struct pollfd pfd[1]; |
| 72 | ssize_t n; |
| 73 | |
| 74 | while (1) { |
| 75 | n = safe_read(fd, buf, count); |
| 76 | if (n >= 0 || errno != EAGAIN) |
| 77 | return n; |
| 78 | /* fd is in O_NONBLOCK mode. Wait using poll and repeat */ |
| 79 | pfd[0].fd = fd; |
| 80 | pfd[0].events = POLLIN; |
| 81 | safe_poll(pfd, 1, -1); |
| 82 | } |
| 83 | } |
| 84 | |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 85 | /* |
| 86 | * Read all of the supplied buffer from a file. |
| 87 | * This does multiple reads as necessary. |
| 88 | * Returns the amount read, or -1 on an error. |
| 89 | * A short read is returned on an end of file. |
| 90 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 91 | ssize_t FAST_FUNC full_read(int fd, void *buf, size_t len) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 92 | { |
| 93 | ssize_t cc; |
| 94 | ssize_t total; |
| 95 | |
| 96 | total = 0; |
| 97 | |
| 98 | while (len) { |
| 99 | cc = safe_read(fd, buf, len); |
| 100 | |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 101 | if (cc < 0) { |
| 102 | if (total) { |
| 103 | /* we already have some! */ |
| 104 | /* user can do another read to know the error code */ |
| 105 | return total; |
| 106 | } |
| 107 | return cc; /* read() returns -1 on failure. */ |
| 108 | } |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 109 | if (cc == 0) |
| 110 | break; |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 111 | buf = ((char *)buf) + cc; |
| 112 | total += cc; |
| 113 | len -= cc; |
| 114 | } |
| 115 | |
| 116 | return total; |
| 117 | } |
| 118 | |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 119 | /* Die with an error message if we can't read the entire buffer. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 120 | void FAST_FUNC xread(int fd, void *buf, size_t count) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 121 | { |
| 122 | if (count) { |
| 123 | ssize_t size = full_read(fd, buf, count); |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 124 | if ((size_t)size != count) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 125 | bb_error_msg_and_die("short read"); |
| 126 | } |
| 127 | } |
| 128 | |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 129 | /* Die with an error message if we can't read one character. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 130 | unsigned char FAST_FUNC xread_char(int fd) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 131 | { |
| 132 | char tmp; |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 133 | xread(fd, &tmp, 1); |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 134 | return tmp; |
| 135 | } |
| 136 | |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 137 | // Reads one line a-la fgets (but doesn't save terminating '\n'). |
| 138 | // Reads byte-by-byte. Useful when it is important to not read ahead. |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 139 | // Bytes are appended to pfx (which must be malloced, or NULL). |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 140 | char* FAST_FUNC xmalloc_reads(int fd, char *buf, size_t *maxsz_p) |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 141 | { |
| 142 | char *p; |
Denis Vlasenko | 0b6c6a9 | 2008-03-24 00:04:42 +0000 | [diff] [blame] | 143 | size_t sz = buf ? strlen(buf) : 0; |
Denis Vlasenko | 9f57cf6 | 2009-03-18 17:32:44 +0000 | [diff] [blame] | 144 | size_t maxsz = maxsz_p ? *maxsz_p : (INT_MAX - 4095); |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 145 | |
| 146 | goto jump_in; |
Denis Vlasenko | 0b6c6a9 | 2008-03-24 00:04:42 +0000 | [diff] [blame] | 147 | while (sz < maxsz) { |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 148 | if ((size_t)(p - buf) == sz) { |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 149 | jump_in: |
| 150 | buf = xrealloc(buf, sz + 128); |
| 151 | p = buf + sz; |
| 152 | sz += 128; |
| 153 | } |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 154 | /* nonblock_safe_read() because we are used by e.g. shells */ |
| 155 | if (nonblock_safe_read(fd, p, 1) != 1) { /* EOF/error */ |
Denis Vlasenko | f99afb5 | 2008-02-24 23:32:36 +0000 | [diff] [blame] | 156 | if (p == buf) { /* we read nothing */ |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 157 | free(buf); |
| 158 | return NULL; |
| 159 | } |
| 160 | break; |
| 161 | } |
| 162 | if (*p == '\n') |
| 163 | break; |
| 164 | p++; |
| 165 | } |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 166 | *p = '\0'; |
Denis Vlasenko | 0b6c6a9 | 2008-03-24 00:04:42 +0000 | [diff] [blame] | 167 | if (maxsz_p) |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 168 | *maxsz_p = p - buf; |
| 169 | p++; |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 170 | return xrealloc(buf, p - buf); |
| 171 | } |
| 172 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 173 | ssize_t FAST_FUNC read_close(int fd, void *buf, size_t size) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 174 | { |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 175 | /*int e;*/ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 176 | size = full_read(fd, buf, size); |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 177 | /*e = errno;*/ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 178 | close(fd); |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 179 | /*errno = e;*/ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 180 | return size; |
| 181 | } |
| 182 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 183 | ssize_t FAST_FUNC open_read_close(const char *filename, void *buf, size_t size) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 184 | { |
| 185 | int fd = open(filename, O_RDONLY); |
| 186 | if (fd < 0) |
| 187 | return fd; |
| 188 | return read_close(fd, buf, size); |
| 189 | } |
| 190 | |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 191 | |
Denis Vlasenko | fe733a9 | 2008-06-24 16:08:22 +0000 | [diff] [blame] | 192 | // Read (potentially big) files in one go. File size is estimated |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 193 | // by stat. Extra '\0' byte is appended. |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 194 | void* FAST_FUNC xmalloc_read(int fd, size_t *maxsz_p) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 195 | { |
| 196 | char *buf; |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 197 | size_t size, rd_size, total; |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 198 | size_t to_read; |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 199 | struct stat st; |
| 200 | |
Denis Vlasenko | 9f57cf6 | 2009-03-18 17:32:44 +0000 | [diff] [blame] | 201 | to_read = maxsz_p ? *maxsz_p : (INT_MAX - 4095); /* max to read */ |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 202 | |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 203 | /* Estimate file size */ |
| 204 | st.st_size = 0; /* in case fstat fails, assume 0 */ |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 205 | fstat(fd, &st); |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 206 | /* /proc/N/stat files report st_size 0 */ |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 207 | /* In order to make such files readable, we add small const */ |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 208 | size = (st.st_size | 0x3ff) + 1; |
| 209 | |
| 210 | total = 0; |
| 211 | buf = NULL; |
| 212 | while (1) { |
| 213 | if (to_read < size) |
| 214 | size = to_read; |
| 215 | buf = xrealloc(buf, total + size + 1); |
| 216 | rd_size = full_read(fd, buf + total, size); |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 217 | if ((ssize_t)rd_size == (ssize_t)(-1)) { /* error */ |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 218 | free(buf); |
| 219 | return NULL; |
| 220 | } |
| 221 | total += rd_size; |
| 222 | if (rd_size < size) /* EOF */ |
| 223 | break; |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 224 | if (to_read <= rd_size) |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 225 | break; |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 226 | to_read -= rd_size; |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 227 | /* grow by 1/8, but in [1k..64k] bounds */ |
| 228 | size = ((total / 8) | 0x3ff) + 1; |
| 229 | if (size > 64*1024) |
| 230 | size = 64*1024; |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 231 | } |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 232 | xrealloc(buf, total + 1); |
| 233 | buf[total] = '\0'; |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 234 | |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 235 | if (maxsz_p) |
| 236 | *maxsz_p = total; |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 237 | return buf; |
| 238 | } |
| 239 | |
| 240 | #ifdef USING_LSEEK_TO_GET_SIZE |
| 241 | /* Alternatively, file size can be obtained by lseek to the end. |
| 242 | * The code is slightly bigger. Retained in case fstat approach |
| 243 | * will not work for some weird cases (/proc, block devices, etc). |
| 244 | * (NB: lseek also can fail to work for some weird files) */ |
| 245 | |
| 246 | // Read (potentially big) files in one go. File size is estimated by |
| 247 | // lseek to end. |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 248 | void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p) |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 249 | { |
| 250 | char *buf; |
| 251 | size_t size; |
| 252 | int fd; |
| 253 | off_t len; |
Denis Vlasenko | d67cef2 | 2007-06-13 06:47:47 +0000 | [diff] [blame] | 254 | |
Denis Vlasenko | f3745ea | 2008-04-19 19:32:08 +0000 | [diff] [blame] | 255 | fd = open(filename, O_RDONLY); |
| 256 | if (fd < 0) |
| 257 | return NULL; |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 258 | |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 259 | /* /proc/N/stat files report len 0 here */ |
| 260 | /* In order to make such files readable, we add small const */ |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 261 | size = 0x3ff; /* read only 1k on unseekable files */ |
| 262 | len = lseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */ |
| 263 | if (len != (off_t)-1) { |
| 264 | xlseek(fd, 0, SEEK_SET); |
Denis Vlasenko | 9f57cf6 | 2009-03-18 17:32:44 +0000 | [diff] [blame] | 265 | size = maxsz_p ? *maxsz_p : (INT_MAX - 4095); |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 266 | if (len < size) |
| 267 | size = len; |
| 268 | } |
| 269 | |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 270 | buf = xmalloc(size + 1); |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 271 | size = read_close(fd, buf, size); |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 272 | if ((ssize_t)size < 0) { |
| 273 | free(buf); |
| 274 | return NULL; |
| 275 | } |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 276 | xrealloc(buf, size + 1); |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 277 | buf[size] = '\0'; |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 278 | |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 279 | if (maxsz_p) |
| 280 | *maxsz_p = size; |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 281 | return buf; |
| 282 | } |
Denis Vlasenko | 0a14462 | 2008-04-20 14:45:43 +0000 | [diff] [blame] | 283 | #endif |
Denis Vlasenko | f3745ea | 2008-04-19 19:32:08 +0000 | [diff] [blame] | 284 | |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 285 | // Read (potentially big) files in one go. File size is estimated |
| 286 | // by stat. |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 287 | void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *maxsz_p) |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 288 | { |
| 289 | char *buf; |
| 290 | int fd; |
| 291 | |
| 292 | fd = open(filename, O_RDONLY); |
| 293 | if (fd < 0) |
| 294 | return NULL; |
| 295 | |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 296 | buf = xmalloc_read(fd, maxsz_p); |
Denis Vlasenko | f62ab2d | 2008-07-09 09:50:33 +0000 | [diff] [blame] | 297 | close(fd); |
| 298 | return buf; |
| 299 | } |
| 300 | |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 301 | void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p) |
Denis Vlasenko | f3745ea | 2008-04-19 19:32:08 +0000 | [diff] [blame] | 302 | { |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 303 | void *buf = xmalloc_open_read_close(filename, maxsz_p); |
Denis Vlasenko | f3745ea | 2008-04-19 19:32:08 +0000 | [diff] [blame] | 304 | if (!buf) |
| 305 | bb_perror_msg_and_die("can't read '%s'", filename); |
| 306 | return buf; |
| 307 | } |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 308 | |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 309 | int FAST_FUNC open_zipped(const char *fname) |
| 310 | { |
| 311 | #if !ZIPPED |
| 312 | return open(fname, O_RDONLY); |
| 313 | #else |
| 314 | unsigned char magic[2]; |
| 315 | char *sfx; |
| 316 | int fd; |
| 317 | #if BB_MMU |
| 318 | USE_DESKTOP(long long) int FAST_FUNC (*xformer)(int src_fd, int dst_fd); |
| 319 | enum { xformer_prog = 0 }; |
| 320 | #else |
| 321 | enum { xformer = 0 }; |
| 322 | const char *xformer_prog; |
| 323 | #endif |
| 324 | |
| 325 | fd = open(fname, O_RDONLY); |
| 326 | if (fd < 0) |
| 327 | return fd; |
| 328 | |
| 329 | sfx = strrchr(fname, '.'); |
| 330 | if (sfx) { |
| 331 | if (ENABLE_FEATURE_SEAMLESS_LZMA && strcmp(sfx, ".lzma") == 0) |
| 332 | /* .lzma has no header/signature, just trust it */ |
| 333 | open_transformer(fd, unpack_lzma_stream, "unlzma"); |
| 334 | else |
| 335 | if ((ENABLE_FEATURE_SEAMLESS_GZ && strcmp(sfx, ".gz") == 0) |
| 336 | || (ENABLE_FEATURE_SEAMLESS_BZ2 && strcmp(sfx, ".bz2") == 0) |
| 337 | ) { |
| 338 | /* .gz and .bz2 both have 2-byte signature, and their |
| 339 | * unpack_XXX_stream want this header skipped. */ |
| 340 | xread(fd, &magic, 2); |
Denis Vlasenko | ec73d30 | 2008-08-05 17:43:29 +0000 | [diff] [blame] | 341 | #if ENABLE_FEATURE_SEAMLESS_GZ |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 342 | #if BB_MMU |
| 343 | xformer = unpack_gz_stream; |
| 344 | #else |
| 345 | xformer_prog = "gunzip"; |
| 346 | #endif |
Denis Vlasenko | ec73d30 | 2008-08-05 17:43:29 +0000 | [diff] [blame] | 347 | #endif |
| 348 | if (!ENABLE_FEATURE_SEAMLESS_GZ |
| 349 | || magic[0] != 0x1f || magic[1] != 0x8b |
| 350 | ) { |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 351 | if (!ENABLE_FEATURE_SEAMLESS_BZ2 |
| 352 | || magic[0] != 'B' || magic[1] != 'Z' |
| 353 | ) { |
| 354 | bb_error_msg_and_die("no gzip" |
| 355 | USE_FEATURE_SEAMLESS_BZ2("/bzip2") |
| 356 | " magic"); |
| 357 | } |
| 358 | #if BB_MMU |
| 359 | xformer = unpack_bz2_stream; |
| 360 | #else |
| 361 | xformer_prog = "bunzip2"; |
| 362 | #endif |
| 363 | } else { |
| 364 | #if !BB_MMU |
| 365 | /* NOMMU version of open_transformer execs |
| 366 | * an external unzipper that wants |
| 367 | * file position at the start of the file */ |
| 368 | xlseek(fd, 0, SEEK_SET); |
| 369 | #endif |
| 370 | } |
| 371 | open_transformer(fd, xformer, xformer_prog); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | return fd; |
| 376 | #endif |
| 377 | } |
| 378 | |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 379 | void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) |
| 380 | { |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 381 | int fd; |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 382 | char *image; |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 383 | |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 384 | fd = open_zipped(fname); |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 385 | if (fd < 0) |
| 386 | return NULL; |
| 387 | |
Denis Vlasenko | b605272 | 2008-07-10 17:43:01 +0000 | [diff] [blame] | 388 | image = xmalloc_read(fd, maxsz_p); |
| 389 | if (!image) |
| 390 | bb_perror_msg("read error from '%s'", fname); |
| 391 | close(fd); |
| 392 | |
| 393 | return image; |
| 394 | } |