zip_source_file_stdio.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. zip_source_file_stdio.c -- read-only stdio file source implementation
  3. Copyright (C) 2020 Dieter Baron and Thomas Klausner
  4. This file is part of libzip, a library to manipulate ZIP archives.
  5. The authors can be contacted at <libzip@nih.at>
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions
  8. are met:
  9. 1. Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. 2. Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in
  13. the documentation and/or other materials provided with the
  14. distribution.
  15. 3. The names of the authors may not be used to endorse or promote
  16. products derived from this software without specific prior
  17. written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
  19. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  22. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  24. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  28. IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "zipint.h"
  31. #include "zip_source_file.h"
  32. #include "zip_source_file_stdio.h"
  33. #include <fcntl.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <sys/stat.h>
  37. #ifdef _WIN32
  38. #ifndef S_IWUSR
  39. #define S_IWUSR _S_IWRITE
  40. #endif
  41. #endif
  42. /* clang-format off */
  43. static zip_source_file_operations_t ops_stdio_read = {
  44. _zip_stdio_op_close,
  45. NULL,
  46. NULL,
  47. NULL,
  48. NULL,
  49. _zip_stdio_op_read,
  50. NULL,
  51. NULL,
  52. _zip_stdio_op_seek,
  53. _zip_stdio_op_stat,
  54. NULL,
  55. _zip_stdio_op_tell,
  56. NULL
  57. };
  58. /* clang-format on */
  59. ZIP_EXTERN zip_source_t *
  60. zip_source_filep(zip_t *za, FILE *file, zip_uint64_t start, zip_int64_t len) {
  61. if (za == NULL) {
  62. return NULL;
  63. }
  64. return zip_source_filep_create(file, start, len, &za->error);
  65. }
  66. ZIP_EXTERN zip_source_t *
  67. zip_source_filep_create(FILE *file, zip_uint64_t start, zip_int64_t length, zip_error_t *error) {
  68. if (file == NULL || length < -1) {
  69. zip_error_set(error, ZIP_ER_INVAL, 0);
  70. return NULL;
  71. }
  72. return zip_source_file_common_new(NULL, file, start, length, NULL, &ops_stdio_read, NULL, error);
  73. }
  74. void
  75. _zip_stdio_op_close(zip_source_file_context_t *ctx) {
  76. fclose((FILE *)ctx->f);
  77. }
  78. zip_int64_t
  79. _zip_stdio_op_read(zip_source_file_context_t *ctx, void *buf, zip_uint64_t len) {
  80. size_t i;
  81. if (len > SIZE_MAX) {
  82. len = SIZE_MAX;
  83. }
  84. if ((i = fread(buf, 1, (size_t)len, ctx->f)) == 0) {
  85. if (ferror((FILE *)ctx->f)) {
  86. zip_error_set(&ctx->error, ZIP_ER_READ, errno);
  87. return -1;
  88. }
  89. }
  90. return (zip_int64_t)i;
  91. }
  92. bool
  93. _zip_stdio_op_seek(zip_source_file_context_t *ctx, void *f, zip_int64_t offset, int whence) {
  94. #if ZIP_FSEEK_MAX > ZIP_INT64_MAX
  95. if (offset > ZIP_FSEEK_MAX || offset < ZIP_FSEEK_MIN) {
  96. zip_error_set(&ctx->error, ZIP_ER_SEEK, EOVERFLOW);
  97. return false;
  98. }
  99. #endif
  100. if (fseeko((FILE *)f, (off_t)offset, whence) < 0) {
  101. zip_error_set(&ctx->error, ZIP_ER_SEEK, errno);
  102. return false;
  103. }
  104. return true;
  105. }
  106. bool
  107. _zip_stdio_op_stat(zip_source_file_context_t *ctx, zip_source_file_stat_t *st) {
  108. struct stat sb;
  109. int ret;
  110. if (ctx->fname) {
  111. ret = stat(ctx->fname, &sb);
  112. }
  113. else {
  114. ret = fstat(fileno((FILE *)ctx->f), &sb);
  115. }
  116. if (ret < 0) {
  117. if (errno == ENOENT) {
  118. st->exists = false;
  119. return true;
  120. }
  121. zip_error_set(&ctx->error, ZIP_ER_READ, errno);
  122. return false;
  123. }
  124. st->size = (zip_uint64_t)sb.st_size;
  125. st->mtime = sb.st_mtime;
  126. st->regular_file = S_ISREG(sb.st_mode);
  127. st->exists = true;
  128. /* We're using UNIX file API, even on Windows; thus, we supply external file attributes with Unix values. */
  129. /* TODO: This could be improved on Windows by providing Windows-specific file attributes */
  130. ctx->attributes.valid = ZIP_FILE_ATTRIBUTES_HOST_SYSTEM | ZIP_FILE_ATTRIBUTES_EXTERNAL_FILE_ATTRIBUTES;
  131. ctx->attributes.host_system = ZIP_OPSYS_UNIX;
  132. ctx->attributes.external_file_attributes = (((zip_uint32_t)sb.st_mode) << 16) | ((sb.st_mode & S_IWUSR) ? 0 : 1);
  133. return true;
  134. }
  135. zip_int64_t
  136. _zip_stdio_op_tell(zip_source_file_context_t *ctx, void *f) {
  137. off_t offset = ftello((FILE *)f);
  138. if (offset < 0) {
  139. zip_error_set(&ctx->error, ZIP_ER_SEEK, errno);
  140. }
  141. return offset;
  142. }
  143. /*
  144. * fopen replacement that sets the close-on-exec flag
  145. * some implementations support an fopen 'e' flag for that,
  146. * but e.g. macOS doesn't.
  147. */
  148. FILE *
  149. _zip_fopen_close_on_exec(const char *name, bool writeable) {
  150. int fd;
  151. int flags;
  152. FILE *fp;
  153. flags = O_CLOEXEC;
  154. if (writeable) {
  155. flags |= O_RDWR;
  156. }
  157. else {
  158. flags |= O_RDONLY;
  159. }
  160. /* mode argument needed on Windows */
  161. if ((fd = open(name, flags, 0666)) < 0) {
  162. return NULL;
  163. }
  164. if ((fp = fdopen(fd, writeable ? "r+b" : "rb")) == NULL) {
  165. return NULL;
  166. }
  167. return fp;
  168. }