zip_source_win32w.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. zip_source_win32w.c -- create data source from Windows file (UTF-16)
  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 <stdio.h>
  31. #include "zipint.h"
  32. #include "zipwin32.h"
  33. static void * _win32_strdup_w(const void *str);
  34. static HANDLE _win32_open_w(_zip_source_win32_read_file_t *ctx);
  35. static HANDLE _win32_create_temp_w(_zip_source_win32_read_file_t *ctx, void **temp, zip_uint32_t value, PSECURITY_ATTRIBUTES sa);
  36. static int _win32_rename_temp_w(_zip_source_win32_read_file_t *ctx);
  37. static int _win32_remove_w(const void *fname);
  38. static _zip_source_win32_file_ops_t win32_ops_w = {
  39. _win32_strdup_w,
  40. _win32_open_w,
  41. _win32_create_temp_w,
  42. _win32_rename_temp_w,
  43. _win32_remove_w
  44. };
  45. ZIP_EXTERN zip_source_t *
  46. zip_source_win32w(zip_t *za, const wchar_t *fname, zip_uint64_t start, zip_int64_t len)
  47. {
  48. if (za == NULL)
  49. return NULL;
  50. return zip_source_win32w_create(fname, start, len, &za->error);
  51. }
  52. ZIP_EXTERN zip_source_t *
  53. zip_source_win32w_create(const wchar_t *fname, zip_uint64_t start, zip_int64_t length, zip_error_t *error)
  54. {
  55. if (fname == NULL || length < -1) {
  56. zip_error_set(error, ZIP_ER_INVAL, 0);
  57. return NULL;
  58. }
  59. return _zip_source_win32_handle_or_name(fname, INVALID_HANDLE_VALUE, start, length, 1, NULL, &win32_ops_w, error);
  60. }
  61. static void *
  62. _win32_strdup_w(const void *str)
  63. {
  64. return _wcsdup((const wchar_t *)str);
  65. }
  66. static HANDLE
  67. _win32_open_w(_zip_source_win32_read_file_t *ctx)
  68. {
  69. return CreateFileW(ctx->fname, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  70. }
  71. static HANDLE
  72. _win32_create_temp_w(_zip_source_win32_read_file_t *ctx, void **temp, zip_uint32_t value, PSECURITY_ATTRIBUTES sa)
  73. {
  74. int len;
  75. len = wcslen((const wchar_t *)ctx->fname) + 10;
  76. if (*temp == NULL) {
  77. if ((*temp = malloc(sizeof(wchar_t) * len)) == NULL) {
  78. zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0);
  79. return INVALID_HANDLE_VALUE;
  80. }
  81. }
  82. if (_snwprintf((wchar_t *)*temp, len, L"%s.%08x", (const wchar_t *)ctx->fname, value) != len - 1) {
  83. return INVALID_HANDLE_VALUE;
  84. }
  85. return CreateFileW((const wchar_t *)*temp, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, sa, CREATE_NEW, FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_TEMPORARY, NULL);
  86. }
  87. static int
  88. _win32_rename_temp_w(_zip_source_win32_read_file_t *ctx)
  89. {
  90. if (!MoveFileExW(ctx->tmpname, ctx->fname, MOVEFILE_REPLACE_EXISTING))
  91. return -1;
  92. return 0;
  93. }
  94. static int
  95. _win32_remove_w(const void *fname)
  96. {
  97. DeleteFileW((const wchar_t *)fname);
  98. return 0;
  99. }