Rizin
unix-like reverse engineering framework and cli tools
zip_source_file_win32.c
Go to the documentation of this file.
1 /*
2  zip_source_file_win32.c -- read-only Windows file source implementation
3  Copyright (C) 1999-2022 Dieter Baron and Thomas Klausner
4 
5  This file is part of libzip, a library to manipulate ZIP archives.
6  The authors can be contacted at <info@libzip.org>
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions
10  are met:
11  1. Redistributions of source code must retain the above copyright
12  notice, this list of conditions and the following disclaimer.
13  2. Redistributions in binary form must reproduce the above copyright
14  notice, this list of conditions and the following disclaimer in
15  the documentation and/or other materials provided with the
16  distribution.
17  3. The names of the authors may not be used to endorse or promote
18  products derived from this software without specific prior
19  written permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
22  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 #include "zip_source_file_win32.h"
35 
37 
39 
40 /* clang-format off */
41 
44  NULL,
45  NULL,
46  NULL,
47  NULL,
49  NULL,
50  NULL,
53  NULL,
55  NULL
56 };
57 
58 /* clang-format on */
59 
62  if (za == NULL) {
63  return NULL;
64  }
65 
67 }
68 
69 
72  if (h == INVALID_HANDLE_VALUE || length < -1) {
74  return NULL;
75  }
76 
78 }
79 
80 
81 void
83  CloseHandle((HANDLE)ctx->f);
84 }
85 
86 
89  DWORD i;
90 
91  /* TODO: cap len to "DWORD_MAX" */
92  if (!ReadFile((HANDLE)ctx->f, buf, (DWORD)len, &i, NULL)) {
94  return -1;
95  }
96 
97  return (zip_int64_t)i;
98 }
99 
100 
101 bool
103  LARGE_INTEGER li;
104  DWORD method;
105 
106  switch (whence) {
107  case SEEK_SET:
108  method = FILE_BEGIN;
109  break;
110  case SEEK_END:
111  method = FILE_END;
112  break;
113  case SEEK_CUR:
114  method = FILE_CURRENT;
115  break;
116  default:
118  return false;
119  }
120 
121  li.QuadPart = (LONGLONG)offset;
122  if (!SetFilePointerEx((HANDLE)f, li, NULL, method)) {
124  return false;
125  }
126 
127  return true;
128 }
129 
130 
131 static bool
133  return _zip_stat_win32(ctx, st, (HANDLE)ctx->f);
134 }
135 
136 
139  LARGE_INTEGER zero;
140  LARGE_INTEGER new_offset;
141 
142  zero.QuadPart = 0;
143  if (!SetFilePointerEx((HANDLE)f, zero, &new_offset, FILE_CURRENT)) {
145  return -1;
146  }
147 
148  return (zip_int64_t)new_offset.QuadPart;
149 }
150 
151 
152 int
154  /* Note: This list isn't exhaustive, but should cover common cases. */
155  switch (win32err) {
156  case ERROR_INVALID_PARAMETER:
157  return EINVAL;
158  case ERROR_FILE_NOT_FOUND:
159  case ERROR_PATH_NOT_FOUND:
160  return ENOENT;
161  case ERROR_INVALID_HANDLE:
162  return EBADF;
163  case ERROR_ACCESS_DENIED:
164  return EACCES;
165  case ERROR_FILE_EXISTS:
166  return EEXIST;
167  case ERROR_TOO_MANY_OPEN_FILES:
168  return EMFILE;
169  case ERROR_DISK_FULL:
170  return ENOSPC;
171  default:
172  return 10000 + win32err;
173  }
174 }
175 
176 
177 static bool
179  FILETIME mtimeft;
180  time_t mtime;
181  LARGE_INTEGER size;
182 
183  if (!GetFileTime(h, NULL, NULL, &mtimeft)) {
185  return false;
186  }
187  if (!_zip_filetime_to_time_t(mtimeft, &mtime)) {
189  return false;
190  }
191 
192  st->exists = true;
193  st->mtime = mtime;
194 
195  if (GetFileType(h) == FILE_TYPE_DISK) {
196  st->regular_file = 1;
197 
198  if (!GetFileSizeEx(h, &size)) {
200  return false;
201  }
202 
203  st->size = (zip_uint64_t)size.QuadPart;
204  }
205 
206  /* TODO: fill in ctx->attributes */
207 
208  return true;
209 }
210 
211 
212 bool
213 _zip_filetime_to_time_t(FILETIME ft, time_t *t) {
214  /*
215  Inspired by http://stackoverflow.com/questions/6161776/convert-windows-filetime-to-second-in-unix-linux
216  */
217  const zip_int64_t WINDOWS_TICK = 10000000LL;
218  const zip_int64_t SEC_TO_UNIX_EPOCH = 11644473600LL;
219  ULARGE_INTEGER li;
220  zip_int64_t secs;
221  time_t temp;
222 
223  li.LowPart = ft.dwLowDateTime;
224  li.HighPart = ft.dwHighDateTime;
225  secs = (li.QuadPart / WINDOWS_TICK - SEC_TO_UNIX_EPOCH);
226 
227  temp = (time_t)secs;
228  if (secs != (zip_int64_t)temp) {
229  return false;
230  }
231 
232  *t = temp;
233  return true;
234 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void start
Definition: sflib.h:133
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
voidpf void uLong size
Definition: ioapi.h:138
voidpf uLong offset
Definition: ioapi.h:144
voidpf void * buf
Definition: ioapi.h:138
#define INVALID_HANDLE_VALUE
Definition: iowin32.c:21
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
#define ZIP_ER_SEEK
Definition: zip.h:109
#define ZIP_EXTERN
Definition: zip.h:54
#define ZIP_ER_INVAL
Definition: zip.h:123
#define ZIP_ER_READ
Definition: zip.h:110
#define ENOENT
Definition: sftypes.h:112
#define ENOSPC
Definition: sftypes.h:138
#define EEXIST
Definition: sftypes.h:127
#define EINVAL
Definition: sftypes.h:132
#define EMFILE
Definition: sftypes.h:134
#define ERANGE
Definition: sftypes.h:144
#define EACCES
Definition: sftypes.h:123
#define EBADF
Definition: sftypes.h:119
int time_t
Definition: sftypes.h:66
#define f(i)
Definition: sha256.c:46
#define h(i)
Definition: sha256.c:48
zip_error_t * error
Definition: zip.h:284
Definition: zipint.h:278
zip_error_t error
Definition: zipint.h:281
void error(const char *msg)
Definition: untgz.c:593
DWORD * HANDLE
DWORD
#define SEEK_SET
Definition: zip.c:88
#define SEEK_CUR
Definition: zip.c:80
#define SEEK_END
Definition: zip.c:84
zip_source_t * zip_source_file_common_new(const char *fname, void *file, zip_uint64_t start, zip_int64_t len, const zip_stat_t *st, zip_source_file_operations_t *ops, void *ops_userdata, zip_error_t *error)
int _zip_win32_error_to_errno(DWORD win32err)
void _zip_win32_op_close(zip_source_file_context_t *ctx)
static zip_source_file_operations_t ops_win32_read
bool _zip_filetime_to_time_t(FILETIME ft, time_t *t)
zip_int64_t _zip_win32_op_tell(zip_source_file_context_t *ctx, void *f)
ZIP_EXTERN zip_source_t * zip_source_win32handle(zip_t *za, HANDLE h, zip_uint64_t start, zip_int64_t len)
static bool _zip_win32_op_stat(zip_source_file_context_t *ctx, zip_source_file_stat_t *st)
zip_int64_t _zip_win32_op_read(zip_source_file_context_t *ctx, void *buf, zip_uint64_t len)
ZIP_EXTERN zip_source_t * zip_source_win32handle_create(HANDLE h, zip_uint64_t start, zip_int64_t length, zip_error_t *error)
static bool _zip_stat_win32(zip_source_file_context_t *ctx, zip_source_file_stat_t *st, HANDLE h)
bool _zip_win32_op_seek(zip_source_file_context_t *ctx, void *f, zip_int64_t offset, int whence)
uint64_t zip_uint64_t
Definition: zipconf.h:39
int64_t zip_int64_t
Definition: zipconf.h:38
zip_t * za
Definition: ziptool.c:79