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

Go to the source code of this file.

Functions

static zip_int64_t _zip_win32_named_op_commit_write (zip_source_file_context_t *ctx)
 
static zip_int64_t _zip_win32_named_op_create_temp_output (zip_source_file_context_t *ctx)
 
static bool _zip_win32_named_op_open (zip_source_file_context_t *ctx)
 
static zip_int64_t _zip_win32_named_op_remove (zip_source_file_context_t *ctx)
 
static void _zip_win32_named_op_rollback_write (zip_source_file_context_t *ctx)
 
static bool _zip_win32_named_op_stat (zip_source_file_context_t *ctx, zip_source_file_stat_t *st)
 
static char * _zip_win32_named_op_string_duplicate (zip_source_file_context_t *ctx, const char *string)
 
static zip_int64_t _zip_win32_named_op_write (zip_source_file_context_t *ctx, const void *data, zip_uint64_t len)
 
static HANDLE win32_named_open (zip_source_file_context_t *ctx, const char *name, bool temporary, PSECURITY_ATTRIBUTES security_attributes)
 

Variables

zip_source_file_operations_t _zip_source_file_win32_named_ops
 

Function Documentation

◆ _zip_win32_named_op_commit_write()

static zip_int64_t _zip_win32_named_op_commit_write ( zip_source_file_context_t ctx)
static

Definition at line 66 of file zip_source_file_win32_named.c.

66  {
67  zip_win32_file_operations_t *file_ops = (zip_win32_file_operations_t *)ctx->ops_userdata;
68  DWORD attributes;
69 
70  if (!CloseHandle((HANDLE)ctx->fout)) {
72  return -1;
73  }
74 
75  attributes = file_ops->get_file_attributes(ctx->tmpname);
76  if (attributes == INVALID_FILE_ATTRIBUTES) {
78  return -1;
79  }
80 
81  if (attributes & FILE_ATTRIBUTE_TEMPORARY) {
82  if (!file_ops->set_file_attributes(ctx->tmpname, attributes & ~FILE_ATTRIBUTE_TEMPORARY)) {
84  return -1;
85  }
86  }
87 
88  if (!file_ops->move_file(ctx->tmpname, ctx->fname, MOVEFILE_REPLACE_EXISTING)) {
90  return -1;
91  }
92 
93  return 0;
94 }
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
#define ZIP_ER_WRITE
Definition: zip.h:111
#define ZIP_ER_RENAME
Definition: zip.h:107
zip_error_t * error
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4
DWORD * HANDLE
DWORD
int _zip_win32_error_to_errno(DWORD win32err)

References _zip_win32_error_to_errno(), DWORD, ctx::error, HANDLE, if(), ZIP_ER_RENAME, ZIP_ER_WRITE, and zip_error_set().

◆ _zip_win32_named_op_create_temp_output()

static zip_int64_t _zip_win32_named_op_create_temp_output ( zip_source_file_context_t ctx)
static

Definition at line 97 of file zip_source_file_win32_named.c.

97  {
98  zip_win32_file_operations_t *file_ops = (zip_win32_file_operations_t *)ctx->ops_userdata;
99 
102  void *temp = NULL;
103  PSECURITY_DESCRIPTOR psd = NULL;
104  PSECURITY_ATTRIBUTES psa = NULL;
105  SECURITY_ATTRIBUTES sa;
106  SECURITY_INFORMATION si;
107  DWORD success;
108  PACL dacl = NULL;
109  char *tempname = NULL;
110  size_t tempname_size = 0;
111 
112  if ((HANDLE)ctx->f != INVALID_HANDLE_VALUE && GetFileType((HANDLE)ctx->f) == FILE_TYPE_DISK) {
113  si = DACL_SECURITY_INFORMATION | UNPROTECTED_DACL_SECURITY_INFORMATION;
114  success = GetSecurityInfo((HANDLE)ctx->f, SE_FILE_OBJECT, si, NULL, NULL, &dacl, NULL, &psd);
115  if (success == ERROR_SUCCESS) {
116  sa.nLength = sizeof(SECURITY_ATTRIBUTES);
117  sa.bInheritHandle = FALSE;
118  sa.lpSecurityDescriptor = psd;
119  psa = &sa;
120  }
121  }
122 
123 #ifndef MS_UWP
124  value = GetTickCount();
125 #else
126  value = (zip_uint32_t)(GetTickCount64() & 0xffffffff);
127 #endif
128 
129  if ((tempname = file_ops->allocate_tempname(ctx->fname, 10, &tempname_size)) == NULL) {
131  return -1;
132  }
133 
134  for (i = 0; i < 1024 && th == INVALID_HANDLE_VALUE; i++) {
135  file_ops->make_tempname(tempname, tempname_size, ctx->fname, value + i);
136 
137  th = win32_named_open(ctx, tempname, true, psa);
138  if (th == INVALID_HANDLE_VALUE && GetLastError() != ERROR_FILE_EXISTS)
139  break;
140  }
141 
142  if (th == INVALID_HANDLE_VALUE) {
143  free(tempname);
144  LocalFree(psd);
146  return -1;
147  }
148 
149  LocalFree(psd);
150  ctx->fout = th;
151  ctx->tmpname = tempname;
152 
153  return 0;
154 }
si
lzma_index ** i
Definition: index.h:629
static int value
Definition: cmd_api.c:93
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
#define INVALID_HANDLE_VALUE
Definition: iowin32.c:21
#define ZIP_ER_MEMORY
Definition: zip.h:119
#define ZIP_ER_TMPOPEN
Definition: zip.h:117
#define FALSE
Definition: mybfd.h:102
void(* make_tempname)(char *buf, size_t len, const char *name, zip_uint32_t i)
char *(* allocate_tempname)(const char *name, size_t extra_chars, size_t *lengthp)
static HANDLE win32_named_open(zip_source_file_context_t *ctx, const char *name, bool temporary, PSECURITY_ATTRIBUTES security_attributes)
uint32_t zip_uint32_t
Definition: zipconf.h:37

References _zip_win32_error_to_errno(), zip_win32_file_operations::allocate_tempname, DWORD, ctx::error, FALSE, free(), HANDLE, i, if(), INVALID_HANDLE_VALUE, zip_win32_file_operations::make_tempname, NULL, si, value, win32_named_open(), ZIP_ER_MEMORY, ZIP_ER_TMPOPEN, and zip_error_set().

◆ _zip_win32_named_op_open()

static bool _zip_win32_named_op_open ( zip_source_file_context_t ctx)
static

Definition at line 158 of file zip_source_file_win32_named.c.

158  {
159  HANDLE h = win32_named_open(ctx, ctx->fname, false, NULL);
160 
161  if (h == INVALID_HANDLE_VALUE) {
162  return false;
163  }
164 
165  ctx->f = h;
166  return true;
167 }
#define h(i)
Definition: sha256.c:48

References h, HANDLE, INVALID_HANDLE_VALUE, NULL, and win32_named_open().

◆ _zip_win32_named_op_remove()

static zip_int64_t _zip_win32_named_op_remove ( zip_source_file_context_t ctx)
static

Definition at line 171 of file zip_source_file_win32_named.c.

171  {
172  zip_win32_file_operations_t *file_ops = (zip_win32_file_operations_t *)ctx->ops_userdata;
173 
174  if (!file_ops->delete_file(ctx->fname)) {
176  return -1;
177  }
178 
179  return 0;
180 }
#define ZIP_ER_REMOVE
Definition: zip.h:127

References _zip_win32_error_to_errno(), ctx::error, if(), ZIP_ER_REMOVE, and zip_error_set().

◆ _zip_win32_named_op_rollback_write()

static void _zip_win32_named_op_rollback_write ( zip_source_file_context_t ctx)
static

Definition at line 184 of file zip_source_file_win32_named.c.

184  {
185  zip_win32_file_operations_t *file_ops = (zip_win32_file_operations_t *)ctx->ops_userdata;
186 
187  if (ctx->fout) {
188  CloseHandle((HANDLE)ctx->fout);
189  }
190  file_ops->delete_file(ctx->tmpname);
191 }

References HANDLE, and if().

◆ _zip_win32_named_op_stat()

static bool _zip_win32_named_op_stat ( zip_source_file_context_t ctx,
zip_source_file_stat_t st 
)
static

Definition at line 195 of file zip_source_file_win32_named.c.

195  {
196  zip_win32_file_operations_t *file_ops = (zip_win32_file_operations_t *)ctx->ops_userdata;
197 
198  WIN32_FILE_ATTRIBUTE_DATA file_attributes;
199 
200  if (!file_ops->get_file_attributes_ex(ctx->fname, GetFileExInfoStandard, &file_attributes)) {
201  DWORD error = GetLastError();
202  if (error == ERROR_FILE_NOT_FOUND) {
203  st->exists = false;
204  return true;
205  }
207  return false;
208  }
209 
210  st->exists = true;
211  st->regular_file = false;
212 
213  if (file_attributes.dwFileAttributes != INVALID_FILE_ATTRIBUTES) {
214  if ((file_attributes.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE | FILE_ATTRIBUTE_REPARSE_POINT)) == 0) {
215  st->regular_file = true;
216  }
217  }
218 
219  if (!_zip_filetime_to_time_t(file_attributes.ftLastWriteTime, &st->mtime)) {
221  return false;
222  }
223  st->size = ((zip_uint64_t)file_attributes.nFileSizeHigh << 32) | file_attributes.nFileSizeLow;
224 
225  /* TODO: fill in ctx->attributes */
226 
227  return true;
228 }
#define ZIP_ER_READ
Definition: zip.h:110
#define ERANGE
Definition: sftypes.h:144
void error(const char *msg)
Definition: untgz.c:593
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(), DWORD, ERANGE, ctx::error, error(), zip_source_file_stat::exists, if(), zip_source_file_stat::mtime, zip_source_file_stat::regular_file, zip_source_file_stat::size, ZIP_ER_READ, and zip_error_set().

◆ _zip_win32_named_op_string_duplicate()

static char * _zip_win32_named_op_string_duplicate ( zip_source_file_context_t ctx,
const char *  string 
)
static

Definition at line 232 of file zip_source_file_win32_named.c.

232  {
233  zip_win32_file_operations_t *file_ops = (zip_win32_file_operations_t *)ctx->ops_userdata;
234 
235  return file_ops->string_duplicate(string);
236 }
char *(* string_duplicate)(const char *string)

References zip_win32_file_operations::string_duplicate.

◆ _zip_win32_named_op_write()

static zip_int64_t _zip_win32_named_op_write ( zip_source_file_context_t ctx,
const void *  data,
zip_uint64_t  len 
)
static

Definition at line 240 of file zip_source_file_win32_named.c.

240  {
241  DWORD ret;
242  if (!WriteFile((HANDLE)ctx->fout, data, (DWORD)len, &ret, NULL) || ret != len) {
244  return -1;
245  }
246 
247  return (zip_int64_t)ret;
248 }
size_t len
Definition: 6502dis.c:15
int64_t zip_int64_t
Definition: zipconf.h:38

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

◆ win32_named_open()

static HANDLE win32_named_open ( zip_source_file_context_t ctx,
const char *  name,
bool  temporary,
PSECURITY_ATTRIBUTES  security_attributes 
)
static

Definition at line 252 of file zip_source_file_win32_named.c.

252  {
253  zip_win32_file_operations_t *file_ops = (zip_win32_file_operations_t *)ctx->ops_userdata;
254 
255  DWORD access = GENERIC_READ;
256  DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
257  DWORD creation_disposition = OPEN_EXISTING;
258  DWORD file_attributes = FILE_ATTRIBUTE_NORMAL;
259  HANDLE h;
260 
261  if (temporary) {
262  access = GENERIC_READ | GENERIC_WRITE;
263  share_mode = FILE_SHARE_READ;
264  creation_disposition = CREATE_NEW;
265  file_attributes = FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_TEMPORARY;
266  }
267 
268  h = file_ops->create_file(name, access, share_mode, security_attributes, creation_disposition, file_attributes, NULL);
269 
270  if (h == INVALID_HANDLE_VALUE) {
272  }
273 
274  return h;
275 }
#define ZIP_ER_OPEN
Definition: zip.h:116
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid void void static data static pause access
Definition: sflib.h:64
Definition: z80asm.h:102

References _zip_win32_error_to_errno(), access, DWORD, ctx::error, h, HANDLE, if(), INVALID_HANDLE_VALUE, NULL, ZIP_ER_OPEN, and zip_error_set().

Referenced by _zip_win32_named_op_create_temp_output(), and _zip_win32_named_op_open().

Variable Documentation

◆ _zip_source_file_win32_named_ops

zip_source_file_operations_t _zip_source_file_win32_named_ops
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)
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)
static zip_int64_t _zip_win32_named_op_commit_write(zip_source_file_context_t *ctx)
static bool _zip_win32_named_op_open(zip_source_file_context_t *ctx)
static bool _zip_win32_named_op_stat(zip_source_file_context_t *ctx, zip_source_file_stat_t *st)
static zip_int64_t _zip_win32_named_op_create_temp_output(zip_source_file_context_t *ctx)
static zip_int64_t _zip_win32_named_op_write(zip_source_file_context_t *ctx, const void *data, zip_uint64_t len)
static zip_int64_t _zip_win32_named_op_remove(zip_source_file_context_t *ctx)
static void _zip_win32_named_op_rollback_write(zip_source_file_context_t *ctx)
static char * _zip_win32_named_op_string_duplicate(zip_source_file_context_t *ctx, const char *string)

Definition at line 48 of file zip_source_file_win32_named.c.

Referenced by zip_source_win32a_create(), and zip_source_win32w_create().