zip_source_file_win32.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. zip_source_file_win32.c -- read-only Windows file source implementation
  3. Copyright (C) 1999-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 "zip_source_file_win32.h"
  31. static bool _zip_win32_op_stat(zip_source_file_context_t *ctx, zip_source_file_stat_t *st);
  32. static bool _zip_stat_win32(zip_source_file_context_t *ctx, zip_source_file_stat_t *st, HANDLE h);
  33. /* clang-format off */
  34. static zip_source_file_operations_t ops_win32_read = {
  35. _zip_win32_op_close,
  36. NULL,
  37. NULL,
  38. NULL,
  39. NULL,
  40. _zip_win32_op_read,
  41. NULL,
  42. NULL,
  43. _zip_win32_op_seek,
  44. _zip_win32_op_stat,
  45. NULL,
  46. _zip_win32_op_tell,
  47. NULL
  48. };
  49. /* clang-format on */
  50. ZIP_EXTERN zip_source_t *
  51. zip_source_win32handle(zip_t *za, HANDLE h, zip_uint64_t start, zip_int64_t len) {
  52. if (za == NULL) {
  53. return NULL;
  54. }
  55. return zip_source_win32handle_create(h, start, len, &za->error);
  56. }
  57. ZIP_EXTERN zip_source_t *
  58. zip_source_win32handle_create(HANDLE h, zip_uint64_t start, zip_int64_t length, zip_error_t *error) {
  59. if (h == INVALID_HANDLE_VALUE || length < -1) {
  60. zip_error_set(error, ZIP_ER_INVAL, 0);
  61. return NULL;
  62. }
  63. return zip_source_file_common_new(NULL, h, start, length, NULL, &ops_win32_read, NULL, error);
  64. }
  65. void
  66. _zip_win32_op_close(zip_source_file_context_t *ctx) {
  67. CloseHandle((HANDLE)ctx->f);
  68. }
  69. zip_int64_t
  70. _zip_win32_op_read(zip_source_file_context_t *ctx, void *buf, zip_uint64_t len) {
  71. DWORD i;
  72. /* TODO: cap len to "DWORD_MAX" */
  73. if (!ReadFile((HANDLE)ctx->f, buf, (DWORD)len, &i, NULL)) {
  74. zip_error_set(&ctx->error, ZIP_ER_READ, _zip_win32_error_to_errno(GetLastError()));
  75. return -1;
  76. }
  77. return (zip_int64_t)i;
  78. }
  79. bool
  80. _zip_win32_op_seek(zip_source_file_context_t *ctx, void *f, zip_int64_t offset, int whence) {
  81. LARGE_INTEGER li;
  82. DWORD method;
  83. switch (whence) {
  84. case SEEK_SET:
  85. method = FILE_BEGIN;
  86. break;
  87. case SEEK_END:
  88. method = FILE_END;
  89. break;
  90. case SEEK_CUR:
  91. method = FILE_CURRENT;
  92. break;
  93. default:
  94. zip_error_set(&ctx->error, ZIP_ER_SEEK, EINVAL);
  95. return -1;
  96. }
  97. li.QuadPart = (LONGLONG)offset;
  98. if (!SetFilePointerEx((HANDLE)f, li, NULL, method)) {
  99. zip_error_set(&ctx->error, ZIP_ER_SEEK, _zip_win32_error_to_errno(GetLastError()));
  100. return false;
  101. }
  102. return true;
  103. }
  104. static bool
  105. _zip_win32_op_stat(zip_source_file_context_t *ctx, zip_source_file_stat_t *st) {
  106. return _zip_stat_win32(ctx, st, (HANDLE)ctx->f);
  107. }
  108. zip_int64_t
  109. _zip_win32_op_tell(zip_source_file_context_t *ctx, void *f) {
  110. LARGE_INTEGER zero;
  111. LARGE_INTEGER new_offset;
  112. zero.QuadPart = 0;
  113. if (!SetFilePointerEx((HANDLE)f, zero, &new_offset, FILE_CURRENT)) {
  114. zip_error_set(&ctx->error, ZIP_ER_SEEK, _zip_win32_error_to_errno(GetLastError()));
  115. return -1;
  116. }
  117. return (zip_int64_t)new_offset.QuadPart;
  118. }
  119. int
  120. _zip_win32_error_to_errno(DWORD win32err) {
  121. /* Note: This list isn't exhaustive, but should cover common cases. */
  122. switch (win32err) {
  123. case ERROR_INVALID_PARAMETER:
  124. return EINVAL;
  125. case ERROR_FILE_NOT_FOUND:
  126. case ERROR_PATH_NOT_FOUND:
  127. return ENOENT;
  128. case ERROR_INVALID_HANDLE:
  129. return EBADF;
  130. case ERROR_ACCESS_DENIED:
  131. return EACCES;
  132. case ERROR_FILE_EXISTS:
  133. return EEXIST;
  134. case ERROR_TOO_MANY_OPEN_FILES:
  135. return EMFILE;
  136. case ERROR_DISK_FULL:
  137. return ENOSPC;
  138. default:
  139. return 10000 + win32err;
  140. }
  141. }
  142. static bool
  143. _zip_stat_win32(zip_source_file_context_t *ctx, zip_source_file_stat_t *st, HANDLE h) {
  144. FILETIME mtimeft;
  145. time_t mtime;
  146. LARGE_INTEGER size;
  147. if (!GetFileTime(h, NULL, NULL, &mtimeft)) {
  148. zip_error_set(&ctx->error, ZIP_ER_READ, _zip_win32_error_to_errno(GetLastError()));
  149. return false;
  150. }
  151. if (_zip_filetime_to_time_t(mtimeft, &mtime) < 0) {
  152. zip_error_set(&ctx->error, ZIP_ER_READ, ERANGE);
  153. return false;
  154. }
  155. st->exists = true;
  156. st->mtime = mtime;
  157. if (GetFileType(h) == FILE_TYPE_DISK) {
  158. st->regular_file = 1;
  159. if (!GetFileSizeEx(h, &size)) {
  160. zip_error_set(&ctx->error, ZIP_ER_READ, _zip_win32_error_to_errno(GetLastError()));
  161. return false;
  162. }
  163. st->size = (zip_uint64_t)size.QuadPart;
  164. }
  165. /* TODO: fill in ctx->attributes */
  166. return true;
  167. }
  168. bool
  169. _zip_filetime_to_time_t(FILETIME ft, time_t *t) {
  170. /*
  171. Inspired by http://stackoverflow.com/questions/6161776/convert-windows-filetime-to-second-in-unix-linux
  172. */
  173. const zip_int64_t WINDOWS_TICK = 10000000LL;
  174. const zip_int64_t SEC_TO_UNIX_EPOCH = 11644473600LL;
  175. ULARGE_INTEGER li;
  176. zip_int64_t secs;
  177. time_t temp;
  178. li.LowPart = ft.dwLowDateTime;
  179. li.HighPart = ft.dwHighDateTime;
  180. secs = (li.QuadPart / WINDOWS_TICK - SEC_TO_UNIX_EPOCH);
  181. temp = (time_t)secs;
  182. if (secs != (zip_int64_t)temp) {
  183. return false;
  184. }
  185. *t = temp;
  186. return true;
  187. }