archive_read_open_file.c 6.2 KB

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