Rizin
unix-like reverse engineering framework and cli tools
zip_source_file_win32.c File Reference

Go to the source code of this file.

Functions

static bool _zip_win32_op_stat (zip_source_file_context_t *ctx, zip_source_file_stat_t *st)
 
static bool _zip_stat_win32 (zip_source_file_context_t *ctx, zip_source_file_stat_t *st, HANDLE h)
 
ZIP_EXTERN zip_source_tzip_source_win32handle (zip_t *za, HANDLE h, zip_uint64_t start, zip_int64_t len)
 
ZIP_EXTERN zip_source_tzip_source_win32handle_create (HANDLE h, zip_uint64_t start, zip_int64_t length, zip_error_t *error)
 
void _zip_win32_op_close (zip_source_file_context_t *ctx)
 
zip_int64_t _zip_win32_op_read (zip_source_file_context_t *ctx, void *buf, zip_uint64_t len)
 
bool _zip_win32_op_seek (zip_source_file_context_t *ctx, void *f, zip_int64_t offset, int whence)
 
zip_int64_t _zip_win32_op_tell (zip_source_file_context_t *ctx, void *f)
 
int _zip_win32_error_to_errno (DWORD win32err)
 
bool _zip_filetime_to_time_t (FILETIME ft, time_t *t)
 

Variables

static zip_source_file_operations_t ops_win32_read
 

Function Documentation

◆ _zip_filetime_to_time_t()

bool _zip_filetime_to_time_t ( FILETIME  ft,
time_t t 
)

Definition at line 213 of file zip_source_file_win32.c.

213  {
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 }
int time_t
Definition: sftypes.h:66
int64_t zip_int64_t
Definition: zipconf.h:38

Referenced by _zip_stat_win32(), and _zip_win32_named_op_stat().

◆ _zip_stat_win32()

static bool _zip_stat_win32 ( zip_source_file_context_t ctx,
zip_source_file_stat_t st,
HANDLE  h 
)
static

Definition at line 178 of file zip_source_file_win32.c.

178  {
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 }
#define NULL
Definition: cris-opc.c:27
voidpf void uLong size
Definition: ioapi.h:138
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
#define ZIP_ER_READ
Definition: zip.h:110
#define ERANGE
Definition: sftypes.h:144
#define h(i)
Definition: sha256.c:48
zip_error_t * error
int _zip_win32_error_to_errno(DWORD win32err)
bool _zip_filetime_to_time_t(FILETIME ft, time_t *t)
uint64_t zip_uint64_t
Definition: zipconf.h:39

References _zip_filetime_to_time_t(), _zip_win32_error_to_errno(), ERANGE, ctx::error, zip_source_file_stat::exists, h, zip_source_file_stat::mtime, NULL, zip_source_file_stat::regular_file, zip_source_file_stat::size, ZIP_ER_READ, and zip_error_set().

Referenced by _zip_win32_op_stat().

◆ _zip_win32_error_to_errno()

int _zip_win32_error_to_errno ( DWORD  win32err)

Definition at line 153 of file zip_source_file_win32.c.

153  {
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 }
#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 EACCES
Definition: sftypes.h:123
#define EBADF
Definition: sftypes.h:119

References EACCES, EBADF, EEXIST, EINVAL, EMFILE, ENOENT, and ENOSPC.

Referenced by _zip_stat_win32(), _zip_win32_named_op_commit_write(), _zip_win32_named_op_create_temp_output(), _zip_win32_named_op_remove(), _zip_win32_named_op_stat(), _zip_win32_named_op_write(), _zip_win32_op_read(), _zip_win32_op_seek(), _zip_win32_op_tell(), and win32_named_open().

◆ _zip_win32_op_close()

void _zip_win32_op_close ( zip_source_file_context_t ctx)

Definition at line 82 of file zip_source_file_win32.c.

82  {
83  CloseHandle((HANDLE)ctx->f);
84 }
DWORD * HANDLE

References HANDLE.

◆ _zip_win32_op_read()

zip_int64_t _zip_win32_op_read ( zip_source_file_context_t ctx,
void *  buf,
zip_uint64_t  len 
)

Definition at line 88 of file zip_source_file_win32.c.

88  {
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 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
voidpf void * buf
Definition: ioapi.h:138
DWORD

References _zip_win32_error_to_errno(), DWORD, ctx::error, HANDLE, i, len, NULL, ZIP_ER_READ, and zip_error_set().

◆ _zip_win32_op_seek()

bool _zip_win32_op_seek ( zip_source_file_context_t ctx,
void *  f,
zip_int64_t  offset,
int  whence 
)

Definition at line 102 of file zip_source_file_win32.c.

102  {
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 }
voidpf uLong offset
Definition: ioapi.h:144
#define ZIP_ER_SEEK
Definition: zip.h:109
#define f(i)
Definition: sha256.c:46
#define SEEK_SET
Definition: zip.c:88
#define SEEK_CUR
Definition: zip.c:80
#define SEEK_END
Definition: zip.c:84

References _zip_win32_error_to_errno(), DWORD, EINVAL, ctx::error, f, HANDLE, NULL, SEEK_CUR, SEEK_END, SEEK_SET, ZIP_ER_SEEK, and zip_error_set().

◆ _zip_win32_op_stat()

static bool _zip_win32_op_stat ( zip_source_file_context_t ctx,
zip_source_file_stat_t st 
)
static

Definition at line 132 of file zip_source_file_win32.c.

132  {
133  return _zip_stat_win32(ctx, st, (HANDLE)ctx->f);
134 }
static bool _zip_stat_win32(zip_source_file_context_t *ctx, zip_source_file_stat_t *st, HANDLE h)

References _zip_stat_win32(), and HANDLE.

◆ _zip_win32_op_tell()

zip_int64_t _zip_win32_op_tell ( zip_source_file_context_t ctx,
void *  f 
)

Definition at line 138 of file zip_source_file_win32.c.

138  {
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 }

References _zip_win32_error_to_errno(), ctx::error, f, HANDLE, ZIP_ER_SEEK, and zip_error_set().

◆ zip_source_win32handle()

ZIP_EXTERN zip_source_t* zip_source_win32handle ( zip_t za,
HANDLE  h,
zip_uint64_t  start,
zip_int64_t  len 
)

Definition at line 61 of file zip_source_file_win32.c.

61  {
62  if (za == NULL) {
63  return NULL;
64  }
65 
67 }
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
zip_error_t error
Definition: zipint.h:281
ZIP_EXTERN zip_source_t * zip_source_win32handle_create(HANDLE h, zip_uint64_t start, zip_int64_t length, zip_error_t *error)
zip_t * za
Definition: ziptool.c:79

References zip::error, h, len, NULL, start, za, and zip_source_win32handle_create().

◆ zip_source_win32handle_create()

ZIP_EXTERN zip_source_t* zip_source_win32handle_create ( HANDLE  h,
zip_uint64_t  start,
zip_int64_t  length,
zip_error_t error 
)

Definition at line 71 of file zip_source_file_win32.c.

71  {
72  if (h == INVALID_HANDLE_VALUE || length < -1) {
74  return NULL;
75  }
76 
78 }
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
#define INVALID_HANDLE_VALUE
Definition: iowin32.c:21
#define ZIP_ER_INVAL
Definition: zip.h:123
void error(const char *msg)
Definition: untgz.c:593
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)
static zip_source_file_operations_t ops_win32_read

References error(), h, INVALID_HANDLE_VALUE, length, NULL, ops_win32_read, start, ZIP_ER_INVAL, zip_error_set(), and zip_source_file_common_new().

Referenced by zip_source_win32handle().

Variable Documentation

◆ ops_win32_read

zip_source_file_operations_t ops_win32_read
static
Initial value:
= {
}
void _zip_win32_op_close(zip_source_file_context_t *ctx)
zip_int64_t _zip_win32_op_tell(zip_source_file_context_t *ctx, void *f)
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)
bool _zip_win32_op_seek(zip_source_file_context_t *ctx, void *f, zip_int64_t offset, int whence)

Definition at line 42 of file zip_source_file_win32.c.

Referenced by zip_source_win32handle_create().