zip_source_win32handle.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*
  2. zip_source_win32file.c -- create data source from HANDLE (Win32)
  3. Copyright (c) 1999-2017 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 <wchar.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "zipint.h"
  34. #include "zipwin32.h"
  35. static zip_int64_t _win32_read_file(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd);
  36. static int _win32_create_temp_file(_zip_source_win32_read_file_t *ctx);
  37. static int _zip_filetime_to_time_t(FILETIME ft, time_t *t);
  38. static int _zip_seek_win32_u(void *h, zip_uint64_t offset, int whence, zip_error_t *error);
  39. static int _zip_seek_win32(void *h, zip_int64_t offset, int whence, zip_error_t *error);
  40. static int _zip_win32_error_to_errno(unsigned long win32err);
  41. static int _zip_stat_win32(void *h, zip_stat_t *st, _zip_source_win32_read_file_t *ctx);
  42. ZIP_EXTERN zip_source_t *
  43. zip_source_win32handle(zip_t *za, HANDLE h, zip_uint64_t start, zip_int64_t len)
  44. {
  45. if (za == NULL)
  46. return NULL;
  47. return zip_source_win32handle_create(h, start, len, &za->error);
  48. }
  49. ZIP_EXTERN zip_source_t *
  50. zip_source_win32handle_create(HANDLE h, zip_uint64_t start, zip_int64_t length, zip_error_t *error)
  51. {
  52. if (h == INVALID_HANDLE_VALUE || length < -1) {
  53. zip_error_set(error, ZIP_ER_INVAL, 0);
  54. return NULL;
  55. }
  56. return _zip_source_win32_handle_or_name(NULL, h, start, length, 1, NULL, NULL, error);
  57. }
  58. zip_source_t *
  59. _zip_source_win32_handle_or_name(const void *fname, HANDLE h, zip_uint64_t start, zip_int64_t len, int closep, const zip_stat_t *st, _zip_source_win32_file_ops_t *ops, zip_error_t *error)
  60. {
  61. _zip_source_win32_read_file_t *ctx;
  62. zip_source_t *zs;
  63. if (h == INVALID_HANDLE_VALUE && fname == NULL) {
  64. zip_error_set(error, ZIP_ER_INVAL, 0);
  65. return NULL;
  66. }
  67. if ((ctx = (_zip_source_win32_read_file_t *)malloc(sizeof(_zip_source_win32_read_file_t))) == NULL) {
  68. zip_error_set(error, ZIP_ER_MEMORY, 0);
  69. return NULL;
  70. }
  71. ctx->fname = NULL;
  72. if (fname) {
  73. if ((ctx->fname = ops->op_strdup(fname)) == NULL) {
  74. zip_error_set(error, ZIP_ER_MEMORY, 0);
  75. free(ctx);
  76. return NULL;
  77. }
  78. }
  79. ctx->ops = ops;
  80. ctx->h = h;
  81. ctx->start = start;
  82. ctx->end = (len < 0 ? 0 : start + (zip_uint64_t)len);
  83. ctx->closep = ctx->fname ? 1 : closep;
  84. if (st) {
  85. memcpy(&ctx->st, st, sizeof(ctx->st));
  86. ctx->st.name = NULL;
  87. ctx->st.valid &= ~ZIP_STAT_NAME;
  88. }
  89. else {
  90. zip_stat_init(&ctx->st);
  91. }
  92. ctx->tmpname = NULL;
  93. ctx->hout = INVALID_HANDLE_VALUE;
  94. zip_error_init(&ctx->error);
  95. ctx->supports = ZIP_SOURCE_SUPPORTS_READABLE | zip_source_make_command_bitmap(ZIP_SOURCE_SUPPORTS, ZIP_SOURCE_TELL, -1);
  96. if (ctx->fname) {
  97. HANDLE th;
  98. th = ops->op_open(ctx);
  99. if (th == INVALID_HANDLE_VALUE || GetFileType(th) == FILE_TYPE_DISK) {
  100. ctx->supports = ZIP_SOURCE_SUPPORTS_WRITABLE;
  101. }
  102. if (th != INVALID_HANDLE_VALUE) {
  103. CloseHandle(th);
  104. }
  105. }
  106. else if (GetFileType(ctx->h) == FILE_TYPE_DISK) {
  107. ctx->supports = ZIP_SOURCE_SUPPORTS_SEEKABLE;
  108. }
  109. if ((zs = zip_source_function_create(_win32_read_file, ctx, error)) == NULL) {
  110. free(ctx->fname);
  111. free(ctx);
  112. return NULL;
  113. }
  114. return zs;
  115. }
  116. static zip_int64_t
  117. _win32_read_file(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd)
  118. {
  119. _zip_source_win32_read_file_t *ctx;
  120. char *buf;
  121. zip_uint64_t n;
  122. DWORD i;
  123. ctx = (_zip_source_win32_read_file_t *)state;
  124. buf = (char *)data;
  125. switch (cmd) {
  126. case ZIP_SOURCE_BEGIN_WRITE:
  127. if (ctx->fname == NULL) {
  128. zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0);
  129. return -1;
  130. }
  131. return _win32_create_temp_file(ctx);
  132. case ZIP_SOURCE_COMMIT_WRITE: {
  133. if (!CloseHandle(ctx->hout)) {
  134. ctx->hout = INVALID_HANDLE_VALUE;
  135. zip_error_set(&ctx->error, ZIP_ER_WRITE, _zip_win32_error_to_errno(GetLastError()));
  136. }
  137. ctx->hout = INVALID_HANDLE_VALUE;
  138. if (ctx->ops->op_rename_temp(ctx) < 0) {
  139. zip_error_set(&ctx->error, ZIP_ER_RENAME, _zip_win32_error_to_errno(GetLastError()));
  140. return -1;
  141. }
  142. free(ctx->tmpname);
  143. ctx->tmpname = NULL;
  144. return 0;
  145. }
  146. case ZIP_SOURCE_CLOSE:
  147. if (ctx->fname) {
  148. CloseHandle(ctx->h);
  149. ctx->h = INVALID_HANDLE_VALUE;
  150. }
  151. return 0;
  152. case ZIP_SOURCE_ERROR:
  153. return zip_error_to_data(&ctx->error, data, len);
  154. case ZIP_SOURCE_FREE:
  155. free(ctx->fname);
  156. free(ctx->tmpname);
  157. if (ctx->closep && ctx->h != INVALID_HANDLE_VALUE)
  158. CloseHandle(ctx->h);
  159. free(ctx);
  160. return 0;
  161. case ZIP_SOURCE_OPEN:
  162. if (ctx->fname) {
  163. if ((ctx->h = ctx->ops->op_open(ctx)) == INVALID_HANDLE_VALUE) {
  164. zip_error_set(&ctx->error, ZIP_ER_OPEN, _zip_win32_error_to_errno(GetLastError()));
  165. return -1;
  166. }
  167. }
  168. if (ctx->closep && ctx->start > 0) {
  169. if (_zip_seek_win32_u(ctx->h, ctx->start, SEEK_SET, &ctx->error) < 0) {
  170. return -1;
  171. }
  172. }
  173. ctx->current = ctx->start;
  174. return 0;
  175. case ZIP_SOURCE_READ:
  176. if (ctx->end > 0) {
  177. n = ctx->end - ctx->current;
  178. if (n > len) {
  179. n = len;
  180. }
  181. }
  182. else {
  183. n = len;
  184. }
  185. if (n > SIZE_MAX)
  186. n = SIZE_MAX;
  187. if (!ctx->closep) {
  188. if (_zip_seek_win32_u(ctx->h, ctx->current, SEEK_SET, &ctx->error) < 0) {
  189. return -1;
  190. }
  191. }
  192. if (!ReadFile(ctx->h, buf, (DWORD)n, &i, NULL)) {
  193. zip_error_set(&ctx->error, ZIP_ER_READ, _zip_win32_error_to_errno(GetLastError()));
  194. return -1;
  195. }
  196. ctx->current += i;
  197. return (zip_int64_t)i;
  198. case ZIP_SOURCE_REMOVE:
  199. if (ctx->ops->op_remove(ctx->fname) < 0) {
  200. zip_error_set(&ctx->error, ZIP_ER_REMOVE, _zip_win32_error_to_errno(GetLastError()));
  201. return -1;
  202. }
  203. return 0;
  204. case ZIP_SOURCE_ROLLBACK_WRITE:
  205. if (ctx->hout) {
  206. CloseHandle(ctx->hout);
  207. ctx->hout = INVALID_HANDLE_VALUE;
  208. }
  209. ctx->ops->op_remove(ctx->tmpname);
  210. free(ctx->tmpname);
  211. ctx->tmpname = NULL;
  212. return 0;
  213. case ZIP_SOURCE_SEEK: {
  214. zip_int64_t new_current;
  215. int need_seek;
  216. zip_source_args_seek_t *args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error);
  217. if (args == NULL)
  218. return -1;
  219. need_seek = ctx->closep;
  220. switch (args->whence) {
  221. case SEEK_SET:
  222. new_current = args->offset;
  223. break;
  224. case SEEK_END:
  225. if (ctx->end == 0) {
  226. LARGE_INTEGER zero;
  227. LARGE_INTEGER new_offset;
  228. if (_zip_seek_win32(ctx->h, args->offset, SEEK_END, &ctx->error) < 0) {
  229. return -1;
  230. }
  231. zero.QuadPart = 0;
  232. if (!SetFilePointerEx(ctx->h, zero, &new_offset, FILE_CURRENT)) {
  233. zip_error_set(&ctx->error, ZIP_ER_SEEK, _zip_win32_error_to_errno(GetLastError()));
  234. return -1;
  235. }
  236. new_current = new_offset.QuadPart;
  237. need_seek = 0;
  238. }
  239. else {
  240. new_current = (zip_int64_t)ctx->end + args->offset;
  241. }
  242. break;
  243. case SEEK_CUR:
  244. new_current = (zip_int64_t)ctx->current + args->offset;
  245. break;
  246. default:
  247. zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
  248. return -1;
  249. }
  250. if (new_current < 0 || (zip_uint64_t)new_current < ctx->start || (ctx->end != 0 && (zip_uint64_t)new_current > ctx->end)) {
  251. zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);
  252. return -1;
  253. }
  254. ctx->current = (zip_uint64_t)new_current;
  255. if (need_seek) {
  256. if (_zip_seek_win32_u(ctx->h, ctx->current, SEEK_SET, &ctx->error) < 0) {
  257. return -1;
  258. }
  259. }
  260. return 0;
  261. }
  262. case ZIP_SOURCE_SEEK_WRITE: {
  263. zip_source_args_seek_t *args;
  264. args = ZIP_SOURCE_GET_ARGS(zip_source_args_seek_t, data, len, &ctx->error);
  265. if (args == NULL) {
  266. return -1;
  267. }
  268. if (_zip_seek_win32(ctx->hout, args->offset, args->whence, &ctx->error) < 0) {
  269. return -1;
  270. }
  271. return 0;
  272. }
  273. case ZIP_SOURCE_STAT: {
  274. if (len < sizeof(ctx->st))
  275. return -1;
  276. if (ctx->st.valid != 0)
  277. memcpy(data, &ctx->st, sizeof(ctx->st));
  278. else {
  279. DWORD win32err;
  280. zip_stat_t *st;
  281. HANDLE h;
  282. int success;
  283. st = (zip_stat_t *)data;
  284. if (ctx->h != INVALID_HANDLE_VALUE) {
  285. h = ctx->h;
  286. }
  287. else {
  288. h = ctx->ops->op_open(ctx);
  289. if (h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND) {
  290. zip_error_set(&ctx->error, ZIP_ER_READ, ENOENT);
  291. return -1;
  292. }
  293. }
  294. success = _zip_stat_win32(h, st, ctx);
  295. win32err = GetLastError();
  296. /* We're done with the handle, so close it if we just opened it. */
  297. if (h != ctx->h) {
  298. CloseHandle(h);
  299. }
  300. if (success < 0) {
  301. /* TODO: Is this the correct error to return in all cases? */
  302. zip_error_set(&ctx->error, ZIP_ER_READ, _zip_win32_error_to_errno(win32err));
  303. return -1;
  304. }
  305. }
  306. return sizeof(ctx->st);
  307. }
  308. case ZIP_SOURCE_SUPPORTS:
  309. return ctx->supports;
  310. case ZIP_SOURCE_TELL:
  311. return (zip_int64_t)ctx->current;
  312. case ZIP_SOURCE_TELL_WRITE:
  313. {
  314. LARGE_INTEGER zero;
  315. LARGE_INTEGER offset;
  316. zero.QuadPart = 0;
  317. if (!SetFilePointerEx(ctx->hout, zero, &offset, FILE_CURRENT)) {
  318. zip_error_set(&ctx->error, ZIP_ER_TELL, _zip_win32_error_to_errno(GetLastError()));
  319. return -1;
  320. }
  321. return offset.QuadPart;
  322. }
  323. case ZIP_SOURCE_WRITE:
  324. {
  325. DWORD ret;
  326. if (!WriteFile(ctx->hout, data, (DWORD)len, &ret, NULL) || ret != len) {
  327. zip_error_set(&ctx->error, ZIP_ER_WRITE, _zip_win32_error_to_errno(GetLastError()));
  328. return -1;
  329. }
  330. return (zip_int64_t)ret;
  331. }
  332. default:
  333. zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0);
  334. return -1;
  335. }
  336. }
  337. static int
  338. _win32_create_temp_file(_zip_source_win32_read_file_t *ctx)
  339. {
  340. zip_uint32_t value;
  341. /*
  342. Windows has GetTempFileName(), but it closes the file after
  343. creation, leaving it open to a horrible race condition. So
  344. we reinvent the wheel.
  345. */
  346. int i;
  347. HANDLE th = INVALID_HANDLE_VALUE;
  348. void *temp = NULL;
  349. SECURITY_INFORMATION si;
  350. SECURITY_ATTRIBUTES sa;
  351. PSECURITY_DESCRIPTOR psd = NULL;
  352. PSECURITY_ATTRIBUTES psa = NULL;
  353. DWORD len;
  354. BOOL success;
  355. /*
  356. Read the DACL from the original file, so we can copy it to the temp file.
  357. If there is no original file, or if we can't read the DACL, we'll use the
  358. default security descriptor.
  359. */
  360. if (ctx->h != INVALID_HANDLE_VALUE && GetFileType(ctx->h) == FILE_TYPE_DISK) {
  361. si = DACL_SECURITY_INFORMATION | UNPROTECTED_DACL_SECURITY_INFORMATION;
  362. len = 0;
  363. success = GetUserObjectSecurity(ctx->h, &si, NULL, len, &len);
  364. if (!success && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
  365. if ((psd = (PSECURITY_DESCRIPTOR)malloc(len)) == NULL) {
  366. zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0);
  367. return -1;
  368. }
  369. success = GetUserObjectSecurity(ctx->h, &si, psd, len, &len);
  370. }
  371. if (success) {
  372. sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  373. sa.bInheritHandle = FALSE;
  374. sa.lpSecurityDescriptor = psd;
  375. psa = &sa;
  376. }
  377. }
  378. value = GetTickCount();
  379. for (i = 0; i < 1024 && th == INVALID_HANDLE_VALUE; i++) {
  380. th = ctx->ops->op_create_temp(ctx, &temp, value + i, psa);
  381. if (th == INVALID_HANDLE_VALUE && GetLastError() != ERROR_FILE_EXISTS)
  382. break;
  383. }
  384. if (th == INVALID_HANDLE_VALUE) {
  385. free(temp);
  386. free(psd);
  387. zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, _zip_win32_error_to_errno(GetLastError()));
  388. return -1;
  389. }
  390. free(psd);
  391. ctx->hout = th;
  392. ctx->tmpname = temp;
  393. return 0;
  394. }
  395. static int
  396. _zip_seek_win32_u(HANDLE h, zip_uint64_t offset, int whence, zip_error_t *error)
  397. {
  398. if (offset > ZIP_INT64_MAX) {
  399. zip_error_set(error, ZIP_ER_SEEK, EOVERFLOW);
  400. return -1;
  401. }
  402. return _zip_seek_win32(h, (zip_int64_t)offset, whence, error);
  403. }
  404. static int
  405. _zip_seek_win32(HANDLE h, zip_int64_t offset, int whence, zip_error_t *error)
  406. {
  407. LARGE_INTEGER li;
  408. DWORD method;
  409. switch (whence) {
  410. case SEEK_SET:
  411. method = FILE_BEGIN;
  412. break;
  413. case SEEK_END:
  414. method = FILE_END;
  415. break;
  416. case SEEK_CUR:
  417. method = FILE_CURRENT;
  418. break;
  419. default:
  420. zip_error_set(error, ZIP_ER_SEEK, EINVAL);
  421. return -1;
  422. }
  423. li.QuadPart = (LONGLONG)offset;
  424. if (!SetFilePointerEx(h, li, NULL, method)) {
  425. zip_error_set(error, ZIP_ER_SEEK, _zip_win32_error_to_errno(GetLastError()));
  426. return -1;
  427. }
  428. return 0;
  429. }
  430. static int
  431. _zip_win32_error_to_errno(DWORD win32err)
  432. {
  433. /*
  434. Note: This list isn't exhaustive, but should cover common cases.
  435. */
  436. switch (win32err) {
  437. case ERROR_INVALID_PARAMETER:
  438. return EINVAL;
  439. case ERROR_FILE_NOT_FOUND:
  440. return ENOENT;
  441. case ERROR_INVALID_HANDLE:
  442. return EBADF;
  443. case ERROR_ACCESS_DENIED:
  444. return EACCES;
  445. case ERROR_FILE_EXISTS:
  446. return EEXIST;
  447. case ERROR_TOO_MANY_OPEN_FILES:
  448. return EMFILE;
  449. case ERROR_DISK_FULL:
  450. return ENOSPC;
  451. default:
  452. return 0;
  453. }
  454. }
  455. static int
  456. _zip_stat_win32(HANDLE h, zip_stat_t *st, _zip_source_win32_read_file_t *ctx)
  457. {
  458. FILETIME mtimeft;
  459. time_t mtime;
  460. LARGE_INTEGER size;
  461. int regularp;
  462. if (!GetFileTime(h, NULL, NULL, &mtimeft)) {
  463. zip_error_set(&ctx->error, ZIP_ER_READ, _zip_win32_error_to_errno(GetLastError()));
  464. return -1;
  465. }
  466. if (_zip_filetime_to_time_t(mtimeft, &mtime) < 0) {
  467. zip_error_set(&ctx->error, ZIP_ER_READ, ERANGE);
  468. return -1;
  469. }
  470. regularp = 0;
  471. if (GetFileType(h) == FILE_TYPE_DISK) {
  472. regularp = 1;
  473. }
  474. if (!GetFileSizeEx(h, &size)) {
  475. zip_error_set(&ctx->error, ZIP_ER_READ, _zip_win32_error_to_errno(GetLastError()));
  476. return -1;
  477. }
  478. zip_stat_init(st);
  479. st->mtime = mtime;
  480. st->valid |= ZIP_STAT_MTIME;
  481. if (ctx->end != 0) {
  482. st->size = ctx->end - ctx->start;
  483. st->valid |= ZIP_STAT_SIZE;
  484. }
  485. else if (regularp) {
  486. st->size = (zip_uint64_t)size.QuadPart;
  487. st->valid |= ZIP_STAT_SIZE;
  488. }
  489. return 0;
  490. }
  491. static int
  492. _zip_filetime_to_time_t(FILETIME ft, time_t *t)
  493. {
  494. /*
  495. Inspired by http://stackoverflow.com/questions/6161776/convert-windows-filetime-to-second-in-unix-linux
  496. */
  497. const zip_int64_t WINDOWS_TICK = 10000000LL;
  498. const zip_int64_t SEC_TO_UNIX_EPOCH = 11644473600LL;
  499. ULARGE_INTEGER li;
  500. zip_int64_t secs;
  501. time_t temp;
  502. li.LowPart = ft.dwLowDateTime;
  503. li.HighPart = ft.dwHighDateTime;
  504. secs = (li.QuadPart / WINDOWS_TICK - SEC_TO_UNIX_EPOCH);
  505. temp = (time_t)secs;
  506. if (secs != (zip_int64_t)temp)
  507. return -1;
  508. *t = temp;
  509. return 0;
  510. }