Rizin
unix-like reverse engineering framework and cli tools
io_w32.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2008-2011 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include "rz_io.h"
5 #include "rz_lib.h"
6 
7 #if __WINDOWS__
8 #include <rz_windows.h>
9 #include <sys/types.h>
10 
11 typedef struct {
12  HANDLE hnd;
13  ut64 winbase;
14 } RzIOW32;
15 #define RzIOW32_HANDLE(x) (((RzIOW32 *)x)->hnd)
16 
17 static int w32__write(RzIO *io, RzIODesc *fd, const ut8 *buf, int count) {
18  if (!fd || !fd->data)
19  return -1;
20  return WriteFile(RzIOW32_HANDLE(fd), buf, count, NULL, NULL);
21 }
22 
23 static int w32__read(RzIO *io, RzIODesc *fd, ut8 *buf, int count) {
24  DWORD ret;
25  return ReadFile(RzIOW32_HANDLE(fd), buf, count, &ret, NULL) ? ret : -1;
26 }
27 
28 static int w32__close(RzIODesc *fd) {
29  if (fd->data) {
30  // TODO: handle return value
31  CloseHandle(RzIOW32_HANDLE(fd));
32  RZ_FREE(fd->data);
33  return 0;
34  }
35  return -1;
36 }
37 
38 // TODO: handle filesize and so on
39 static ut64 w32__lseek(RzIO *io, RzIODesc *fd, ut64 offset, int whence) {
40  SetFilePointer(RzIOW32_HANDLE(fd), offset, 0, !whence ? FILE_BEGIN : whence == 1 ? FILE_CURRENT
41  : FILE_END);
42  return (!whence) ? offset : whence == 1 ? io->off + offset
43  : ST64_MAX;
44 }
45 
46 static bool w32__plugin_open(RzIO *io, const char *pathname, bool many) {
47  return (!strncmp(pathname, "w32://", 6));
48 }
49 
50 static RzIODesc *w32__open(RzIO *io, const char *pathname, int rw, int mode) {
51  if (!strncmp(pathname, "w32://", 6)) {
52  RzIOW32 *w32 = RZ_NEW0(RzIOW32);
53  if (!w32) {
54  return NULL;
55  }
56  const char *filename = pathname + 6;
57  LPTSTR filename_ = rz_sys_conv_utf8_to_win(filename);
58  w32->hnd = CreateFile(filename_,
59  GENERIC_READ | (rw ? GENERIC_WRITE : 0),
60  FILE_SHARE_READ | FILE_SHARE_WRITE,
61  NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
62  free(filename_);
63  if (w32->hnd != INVALID_HANDLE_VALUE)
64  return rz_io_desc_new(io, &rz_io_plugin_w32,
65  pathname, rw, mode, w32);
66  free(w32);
67  }
68  return NULL;
69 }
70 
71 static char *w32__system(RzIO *io, RzIODesc *fd, const char *cmd) {
72  if (io && fd && fd->data && cmd && !strcmp(cmd, "winbase")) {
73  RzIOW32 *w32 = (RzIOW32 *)fd->data;
74  io->cb_printf("%" PFMT64u, w32->winbase);
75  }
76  return NULL;
77 }
78 
80  .name = "w32",
81  .desc = "w32 API io",
82  .license = "LGPL3",
83  .uris = "w32://",
84  .open = w32__open,
85  .close = w32__close,
86  .read = w32__read,
87  .check = w32__plugin_open,
88  .lseek = w32__lseek,
89  .system = w32__system,
90  .write = w32__write,
91 };
92 
93 #ifndef RZ_PLUGIN_INCORE
96  .data = &rz_io_plugin_w32,
98 };
99 #endif
100 
101 #else
103  .name = (void *)0
104 };
105 
106 #endif
RZ_API RzLibStruct rizin_plugin
#define RZ_API
#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 count
Definition: sflib.h:98
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 cmd
Definition: sflib.h:79
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
struct rz_io_plugin_t rz_io_plugin_w32
Definition: io_w32.c:102
const char * filename
Definition: ioapi.h:137
voidpf uLong offset
Definition: ioapi.h:144
const char int mode
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
#define INVALID_HANDLE_VALUE
Definition: iowin32.c:21
uint8_t ut8
Definition: lh5801.h:11
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char pathname
Definition: sflib.h:66
RZ_API RzIODesc * rz_io_desc_new(RzIO *io, RzIOPlugin *plugin, const char *uri, int flags, int mode, void *data)
Definition: io_desc.c:11
@ RZ_LIB_TYPE_IO
Definition: rz_lib.h:69
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define PFMT64u
Definition: rz_types.h:395
#define RZ_FREE(x)
Definition: rz_types.h:369
#define ST64_MAX
Definition: rz_types_base.h:84
#define RZ_VERSION
Definition: rz_version.h:8
HANDLE hnd
const char * name
Definition: rz_io.h:115
const char * version
Definition: rz_io.h:117
Definition: rz_io.h:59
ut64 off
Definition: rz_io.h:61
PrintfCallback cb_printf
Definition: rz_io.h:91
DWORD * HANDLE
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
DWORD
static const z80_opcode fd[]
Definition: z80_tab.h:997