archive_read_open_file.c 6.8 KB

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