Unicode fixes for Windows (#1703)

* Remove unnecessary calls to `rz_acp_to_utf8()` from function on Windows

This is unnecessary since d7e7bae79e

* Make `rz_file` APIs use `W` variant of Windows APIs

* Rename variables from `var_` -> `wvar`
This commit is contained in:
GustavoLCR 2021-09-17 04:05:00 -03:00 committed by GitHub
parent 34ecf287ed
commit a7602055e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 62 deletions

View file

@ -745,9 +745,6 @@ RZ_API int rz_main_rizin(int argc, const char **argv) {
RZ_FREE(debugbackend);
return main_help(help > 1 ? 2 : 0);
}
#if __WINDOWS__
pfile = rz_acp_to_utf8(pfile);
#endif // __WINDOWS__
if (customRarunProfile) {
char *tfn = rz_file_temp(".rz-run");
if (!rz_file_dump(tfn, (const ut8 *)customRarunProfile, strlen(customRarunProfile), 0)) {
@ -943,9 +940,6 @@ RZ_API int rz_main_rizin(int argc, const char **argv) {
if (!strstr(pfile, "://")) {
opt.ind--; // take filename
}
#if __WINDOWS__
pfile = rz_acp_to_utf8(pfile);
#endif // __WINDOWS__
fh = rz_core_file_open(r, pfile, perms, mapaddr);
iod = (r->io && fh) ? rz_io_desc_get(r->io, fh->fd) : NULL;
if (!strcmp(debugbackend, "gdb")) {
@ -1015,9 +1009,6 @@ RZ_API int rz_main_rizin(int argc, const char **argv) {
RZ_FREE(path);
}
#else
#if __WINDOWS__
f = rz_acp_to_utf8(f);
#endif // __WINDOWS__
if (f) {
char *escaped_path = rz_str_arg_escape(f);
pfile = rz_str_append(pfile, escaped_path);
@ -1064,9 +1055,6 @@ RZ_API int rz_main_rizin(int argc, const char **argv) {
RZ_FREE(pfile);
while (opt.ind < argc) {
pfile = strdup(argv[opt.ind++]);
#if __WINDOWS__
pfile = rz_acp_to_utf8(pfile);
#endif
fh = rz_core_file_open(r, pfile, perms, mapaddr);
if (!fh && perms & RZ_PERM_W) {
perms |= RZ_PERM_CREAT;

View file

@ -547,14 +547,7 @@ static RzCoreFile *rz_diff_load_file_with_core(const char *filename, const char
rz_config_set_b(core->config, "scr.interactive", false);
rz_config_set_b(core->config, "cfg.debug", false);
core->print->scr_prompt = false;
#if __WINDOWS__
char *winpath = rz_acp_to_utf8(filename);
cfile = rz_core_file_open(core, winpath, 0, 0);
free(winpath);
#else
cfile = rz_core_file_open(core, filename, 0, 0);
#endif
if (!cfile) {
rz_diff_error("cannot open file '%s'\n", filename);
goto rz_diff_load_file_with_core_fail;

View file

@ -28,14 +28,7 @@ static RzCore *opencore(const char *fname) {
rz_core_loadlibs(c, RZ_CORE_LOADLIBS_ALL, NULL);
rz_config_set_i(c->config, "scr.interactive", false);
if (fname) {
#if __WINDOWS__
char *winf = rz_acp_to_utf8(fname);
rfile = rz_core_file_open(c, winf, 0, 0);
free(winf);
#else
rfile = rz_core_file_open(c, fname, 0, 0);
#endif
if (!rfile) {
eprintf("Could not open file %s\n", fname);
rz_core_free(c);

View file

@ -836,20 +836,20 @@ RZ_API bool rz_file_rm(const char *file) {
}
if (rz_file_is_directory(file)) {
#if __WINDOWS__
LPTSTR file_ = rz_sys_conv_utf8_to_win(file);
bool ret = RemoveDirectory(file_);
LPWSTR wfile = rz_utf8_to_utf16(file);
bool ret = RemoveDirectoryW(wfile);
free(file_);
free(wfile);
return !ret;
#else
return !rmdir(file);
#endif
} else {
#if __WINDOWS__
LPTSTR file_ = rz_sys_conv_utf8_to_win(file);
bool ret = DeleteFile(file_);
LPWSTR wfile = rz_utf8_to_utf16(file);
bool ret = DeleteFileW(wfile);
free(file_);
free(wfile);
return !ret;
#else
return !unlink(file);
@ -877,7 +877,6 @@ repeat:
#if __WINDOWS__
static RzMmap *file_mmap(RzMmap *m) {
LPTSTR file_ = rz_sys_conv_utf8_to_win(m->filename);
bool is_write = (m->perm & O_WRONLY) || (m->perm & O_RDWR);
HANDLE fh = (HANDLE)_get_osfhandle(m->fd);
m->len = (DWORD)GetFileSize(fh, (LPDWORD)((char *)&m->len + sizeof(DWORD)));
@ -886,7 +885,7 @@ static RzMmap *file_mmap(RzMmap *m) {
goto err;
}
if (m->len != 0) {
m->fm = CreateFileMapping(fh,
m->fm = CreateFileMappingW(fh,
NULL,
is_write ? PAGE_READWRITE : PAGE_READONLY,
0, 0, NULL);
@ -905,7 +904,6 @@ static RzMmap *file_mmap(RzMmap *m) {
return m;
err:
rz_file_mmap_free(m);
free(file_);
return NULL;
}
#elif __UNIX__
@ -1032,32 +1030,32 @@ RZ_API int rz_file_mkstemp(RZ_NULLABLE const char *prefix, char **oname) {
prefix = "rz";
}
#if __WINDOWS__
LPTSTR name = NULL;
LPTSTR path_ = rz_sys_conv_utf8_to_win(path);
LPTSTR prefix_ = rz_sys_conv_utf8_to_win(prefix);
LPWSTR wname = malloc(sizeof(WCHAR) * (MAX_PATH + 1));
LPWSTR wpath = rz_utf8_to_utf16(path);
LPWSTR wprefix = prefix ? rz_utf8_to_utf16(prefix) : _wcsdup(L"");
name = (LPTSTR)malloc(sizeof(TCHAR) * (MAX_PATH + 1));
if (!name) {
if (!(wname && wpath && wprefix)) {
goto err_r_file_mkstemp;
}
if (GetTempFileName(path_, prefix_, 0, name)) {
char *name_ = rz_sys_conv_win_to_utf8(name);
h = rz_sys_open(name_, O_RDWR | O_EXCL | O_BINARY, 0644);
if (GetTempFileNameW(wpath, wprefix, 0, wname)) {
char *name = rz_utf16_to_utf8(wname);
h = rz_sys_open(name, O_RDWR | O_EXCL | O_BINARY, 0644);
if (oname) {
if (h != -1) {
*oname = name_;
*oname = name;
} else {
*oname = NULL;
free(name_);
free(name);
}
} else {
free(name_);
free(name);
}
}
err_r_file_mkstemp:
free(name);
free(path_);
free(prefix_);
free(wname);
free(wpath);
free(wprefix);
#else
char pfxx[1024];
const char *suffix = strchr(prefix, '*');
@ -1101,28 +1099,23 @@ err_r_file_mkstemp:
RZ_API char *rz_file_tmpdir(void) {
#if __WINDOWS__
LPTSTR tmpdir;
char *path = NULL;
DWORD len = 0;
tmpdir = (LPTSTR)calloc(1, sizeof(TCHAR) * (MAX_PATH + 1));
LPWSTR tmpdir = calloc(1, sizeof(WCHAR) * (MAX_PATH + 1));
if (!tmpdir) {
return NULL;
}
if ((len = GetTempPath(MAX_PATH + 1, tmpdir)) == 0) {
if ((len = GetTempPathW(MAX_PATH + 1, tmpdir)) == 0) {
path = rz_sys_getenv("TEMP");
if (!path) {
path = strdup("C:\\WINDOWS\\Temp\\");
}
} else {
tmpdir[len] = 0;
DWORD(WINAPI * glpn)
(LPCTSTR, LPCTSTR, DWORD) = rz_lib_dl_sym(GetModuleHandle(TEXT("kernel32.dll")), W32_TCALL("GetLongPathName"));
if (glpn) {
// Windows XP sometimes returns short path name
glpn(tmpdir, tmpdir, MAX_PATH + 1);
}
path = rz_sys_conv_win_to_utf8(tmpdir);
// Windows XP sometimes returns short path name
GetLongPathNameW(tmpdir, tmpdir, MAX_PATH + 1);
path = rz_utf16_to_utf8(tmpdir);
}
free(tmpdir);
// Windows 7, stat() function fail if tmpdir ends with '\\'
@ -1157,15 +1150,15 @@ RZ_API bool rz_file_copy(const char *src, const char *dst) {
#if HAVE_COPYFILE
return copyfile(src, dst, 0, COPYFILE_DATA | COPYFILE_XATTR) != -1;
#elif __WINDOWS__
PTCHAR s = rz_sys_conv_utf8_to_win(src);
PTCHAR d = rz_sys_conv_utf8_to_win(dst);
PWCHAR s = rz_utf8_to_utf16(src);
PWCHAR d = rz_utf8_to_utf16(dst);
if (!s || !d) {
RZ_LOG_ERROR("rz_file_copy: Failed to allocate memory\n");
free(s);
free(d);
return false;
}
bool ret = CopyFile(s, d, 0);
bool ret = CopyFileW(s, d, 0);
if (!ret) {
rz_sys_perror("rz_file_copy");
}
@ -1194,6 +1187,9 @@ static void recursive_search_glob(const char *path, const char *glob, RzList *li
continue;
}
char *filename = malloc(strlen(path) + strlen(file) + 2);
if (!filename) {
return;
}
strcpy(filename, path);
strcat(filename, file);
if (rz_file_is_directory(filename)) {