Rizin
unix-like reverse engineering framework and cli tools
w32-sys.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2010 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_types.h>
5 #include <rz_util.h>
6 
7 #if __WINDOWS__
8 #include <rz_windows.h>
9 #include <stdio.h>
10 #include <tchar.h>
11 
12 #define BUFSIZE 1024
13 void rz_sys_perror_str(const char *fun);
14 
15 #define ErrorExit(x) \
16  { \
17  rz_sys_perror(x); \
18  return false; \
19  }
20 char *ReadFromPipe(HANDLE fh, int *outlen);
21 
22 RZ_API char *rz_sys_get_src_dir_w32(void) {
23  TCHAR fullpath[MAX_PATH + 1];
24  TCHAR shortpath[MAX_PATH + 1];
25 
26  if (!GetModuleFileName(NULL, fullpath, MAX_PATH + 1) ||
27  !GetShortPathName(fullpath, shortpath, MAX_PATH + 1)) {
28  return NULL;
29  }
30  char *path = rz_sys_conv_win_to_utf8(shortpath);
31  char *dir = rz_file_dirname(path);
32  if (!rz_sys_getenv_asbool("RZ_ALT_SRC_DIR")) {
33  char *tmp = dir;
34  dir = rz_file_dirname(tmp);
35  free(tmp);
36  }
37  return dir;
38 }
39 
40 RZ_API bool rz_sys_create_child_proc_w32(const char *cmdline, HANDLE in, HANDLE out, HANDLE err) {
41  PROCESS_INFORMATION pi = { 0 };
42  STARTUPINFO si = { 0 };
43  LPTSTR cmdline_;
44  bool ret = false;
45  const size_t max_length = 32768 * sizeof(TCHAR);
46  LPTSTR _cmdline_ = malloc(max_length);
47 
48  if (!_cmdline_) {
49  RZ_LOG_ERROR("Failed to allocate memory\n");
50  return false;
51  }
52 
53  // Set up members of the STARTUPINFO structure.
54  // This structure specifies the STDIN and STDOUT handles for redirection.
55  si.cb = sizeof(STARTUPINFO);
56  si.hStdError = err;
57  si.hStdOutput = out;
58  si.hStdInput = in;
59  si.dwFlags |= STARTF_USESTDHANDLES;
60  cmdline_ = rz_sys_conv_utf8_to_win(cmdline);
61  ExpandEnvironmentStrings(cmdline_, _cmdline_, max_length - 1);
62  if ((ret = CreateProcess(NULL,
63  _cmdline_, // command line
64  NULL, // process security attributes
65  NULL, // primary thread security attributes
66  TRUE, // handles are inherited
67  0, // creation flags
68  NULL, // use parent's environment
69  NULL, // use parent's current directory
70  &si, // STARTUPINFO pointer
71  &pi))) { // receives PROCESS_INFORMATION
72  ret = true;
73  CloseHandle(pi.hProcess);
74  CloseHandle(pi.hThread);
75  } else {
76  rz_sys_perror("CreateProcess");
77  }
78  free(cmdline_);
79  free(_cmdline_);
80  return ret;
81 }
82 
83 char *ReadFromPipe(HANDLE fh, int *outlen) {
84  DWORD dwRead;
85  CHAR chBuf[BUFSIZE];
86  BOOL bSuccess = FALSE;
87  char *str;
88  int strl = 0;
89  int strsz = BUFSIZE + 1;
90 
91  if (outlen) {
92  *outlen = 0;
93  }
94  str = malloc(strsz);
95  if (!str) {
96  return NULL;
97  }
98  while (true) {
99  bSuccess = ReadFile(fh, chBuf, BUFSIZE, &dwRead, NULL);
100  if (!bSuccess || dwRead == 0) {
101  break;
102  }
103  if (strl + dwRead > strsz) {
104  char *str_tmp = str;
105  strsz += 4096;
106  str = realloc(str, strsz);
107  if (!str) {
108  free(str_tmp);
109  return NULL;
110  }
111  }
112  memcpy(str + strl, chBuf, dwRead);
113  strl += dwRead;
114  }
115  str[strl] = 0;
116  if (outlen) {
117  *outlen = strl;
118  }
119  return str;
120 }
121 
122 RZ_API char **rz_sys_utf8_argv_new(int argc, const wchar_t **argv) {
123  char **utf8_argv = calloc(argc + 1, sizeof(wchar_t *));
124  if (!utf8_argv) {
125  return NULL;
126  }
127  int i;
128  for (i = 0; i < argc; i++) {
129  utf8_argv[i] = rz_utf16_to_utf8(argv[i]);
130  }
131  return utf8_argv;
132 }
133 
134 RZ_API void rz_sys_utf8_argv_free(int argc, char **utf8_argv) {
135  int i;
136  for (i = 0; i < argc; i++) {
137  free(utf8_argv[i]);
138  }
139  free(utf8_argv);
140 }
141 #endif
si
lzma_index ** i
Definition: index.h:629
static bool err
Definition: armass.c:435
const lzma_allocator const uint8_t * in
Definition: block.h:527
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
FILE * fh
Definition: cabinfo.c:52
#define RZ_API
#define NULL
Definition: cris-opc.c:27
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
void * malloc(size_t size)
Definition: malloc.c:123
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
#define TRUE
Definition: mybfd.h:103
#define FALSE
Definition: mybfd.h:102
#define BUFSIZE
RZ_API char * rz_file_dirname(const char *path)
Definition: file.c:120
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
RZ_API void rz_sys_perror_str(const char *fun)
Definition: sys.c:737
RZ_API bool rz_sys_getenv_asbool(const char *key)
Return true if the environment variable has the value 1, false otherwise.
Definition: sys.c:511
#define rz_sys_perror(x)
Definition: rz_types.h:336
DWORD * HANDLE
DWORD