Remove dirent.h dependency for Windows and add utf16<->utf8 conversion function

This commit is contained in:
Marcel 2017-04-15 13:07:33 +02:00 committed by radare
parent 80453e0792
commit a4fc91f29b
10 changed files with 166 additions and 43 deletions

View File

@ -1,7 +1,6 @@
/* radare - LGPL - Copyright 2011-2012 - pancake */
#include <r_fs.h>
#include <dirent.h>
#include <sys/stat.h>
static RFSFile* fs_posix_open(RFSRoot *root, const char *path) {
@ -33,17 +32,60 @@ static void fs_posix_close(RFSFile *file) {
}
static RList *fs_posix_dir(RFSRoot *root, const char *path, int view /*ignored*/) {
char fullpath[4096];
RList *list;
char fullpath[4096];
struct stat st;
#if __WINDOWS__ && !defined(__CYGWIN__)
WIN32_FIND_DATAW entry;
HANDLE fh;
wchar_t *wcpath;
char *wctocbuff;
wchar_t directory[MAX_PATH];
#else
struct dirent *de;
DIR *dir = opendir (path);
if (!dir) {
return NULL;
}
DIR *dir;
#endif
list = r_list_new ();
if (!list) {
closedir (dir);
return NULL;
}
#if __WINDOWS__ && !defined(__CYGWIN__)
wcpath = r_utf8_to_utf16 (path);
if (!wcpath) {
return NULL;
}
swprintf (directory, sizeof (directory), L"%ls\\*.*", wcpath);
fh = FindFirstFileW (directory, &entry);
if (fh == INVALID_HANDLE_VALUE) {
free (wcpath);
return NULL;
}
do {
if ((wctocbuff = r_utf16_to_utf8 (entry.cFileName))) {
RFSFile *fsf = r_fs_file_new (NULL, wctocbuff);
if (!fsf) {
r_list_free (list);
FindClose (fh);
return NULL;
}
fsf->type = 'f';
snprintf (fullpath, sizeof (fullpath)-1, "%s/%s", path, wctocbuff);
if (!stat (fullpath, &st)) {
fsf->type = S_ISDIR (st.st_mode)?'d':'f';
fsf->time = st.st_atime;
} else {
fsf->type = 'f';
fsf->time = 0;
}
r_list_append (list, fsf);
free (wctocbuff);
}
} while (FindNextFileW (fh, &entry));
FindClose (fh);
#else
dir = opendir (path);
if (!dir) {
return NULL;
}
while ((de = readdir (dir))) {
@ -65,6 +107,7 @@ static RList *fs_posix_dir(RFSRoot *root, const char *path, int view /*ignored*/
r_list_append (list, fsf);
}
closedir (dir);
#endif
return list;
}

View File

@ -1,7 +1,6 @@
/* radare - LGPL - Copyright 2016 - pancake */
#include <r_fs.h>
#include <dirent.h>
#include <sys/stat.h>
static RFSFile* fs_squash_open(RFSRoot *root, const char *path) {

View File

@ -10,7 +10,9 @@
#include <r_list.h> // radare linked list
#include <r_flist.h> // radare fixed pointer array iterators
#include <r_th.h>
#if __UNIX__
#include <dirent.h>
#endif
#include <sys/time.h>
#include "r_util/r_base64.h"
#include "r_util/r_base91.h"

View File

@ -7,7 +7,11 @@
* Paths pointing into the webroot are an exception: For reaching the webroot, .. and absolute
* path are ok.
*/
#if __WINDOWS__ && !defined(__CYGWIN__)
R_API HANDLE r_sandbox_opendir(const char *path, WIN32_FIND_DATAW *entry, wchar_t *dir, wchar_t *wcpath);
#else
R_API DIR* r_sandbox_opendir(const char *path);
#endif
R_API int r_sandbox_lseek(int fd, ut64 addr, int mode);
R_API int r_sandbox_close(int fd);
R_API int r_sandbox_read(int fd, ut8 *buf, int len);

View File

@ -9,4 +9,6 @@ R_API int r_utf8_encode_str(const RRune *str, ut8 *dst, const int dst_length);
R_API int r_utf8_size(const ut8 *ptr);
R_API int r_utf8_strlen(const ut8 *str);
R_API int r_isprint(const RRune c);
R_API char *r_utf16_to_utf8(const wchar_t *wc);
R_API wchar_t *r_utf8_to_utf16(const char *cstring);
#endif // R_UTF8_H

View File

@ -36,7 +36,6 @@
#include <r_util.h>
#include <sys/param.h>
#include <ctype.h>
#include <dirent.h>
#if __UNIX__
#define QUICK 1
#include <sys/mman.h>
@ -504,12 +503,20 @@ static void load_1(RMagic *ms, int action, const char *file, int *errs, struct r
static int apprentice_load(RMagic *ms, struct r_magic **magicp, ut32 *nmagicp, const char *fn, int action) {
ut32 marraycount, i, mentrycount = 0, starttest;
struct r_magic_entry *marray;
char subfn[MAXPATHLEN];
struct dirent *d;
struct stat st;
int errs = 0;
#if __UNIX__
DIR *dir;
struct dirent *d;
char subfn[MAXPATHLEN];
#else
HANDLE hdir;
WIN32_FIND_DATAW entry;
wchar_t dir[MAX_PATH];
wchar_t *wcpath;
char *cfname;
char subfn[1024];
#endif
ms->flags |= R_MAGIC_CHECK; /* Enable checks for parsed files */
maxmagic = MAXMAGIS;
@ -529,6 +536,28 @@ static int apprentice_load(RMagic *ms, struct r_magic **magicp, ut32 *nmagicp, c
free (marray);
return -1;
}
#if __WINDOWS__ && !defined(__CYGWIN__)
if ((wcpath = r_utf8_to_utf16 (fn))) {
swprintf (dir, sizeof (dir), L"%ls\\*.*", wcpath);
hdir = FindFirstFileW (dir, &entry);
if (!(hdir == INVALID_HANDLE_VALUE)) {
do {
if (wcsncmp (entry.cFileName, L".", 1) == 0) continue;
if ((cfname = r_utf16_to_utf8 (entry.cFileName))) {
snprintf (subfn, sizeof (subfn), "%s/%s", fn, cfname);
if (stat (subfn, &st) == 0 && S_ISREG (st.st_mode)) {
load_1 (ms, action, subfn, &errs, &marray, &marraycount);
}
free (cfname);
}
} while (FindNextFileW (hdir, &entry));
FindClose (hdir);
}
free (wcpath);
} else {
errs++;
}
#else
dir = opendir (fn);
if (dir) {
while ((d = readdir (dir))) {
@ -539,7 +568,10 @@ static int apprentice_load(RMagic *ms, struct r_magic **magicp, ut32 *nmagicp, c
//else perror (subfn);
}
closedir (dir);
} else errs++;
} else {
errs++;
}
#endif
} else load_1 (ms, action, fn, &errs, &marray, &marraycount);
if (errs)
goto out;

View File

@ -4,7 +4,6 @@
#include "r_util.h"
#include "r_lib.h"
#include <stdio.h>
#include <dirent.h>
R_LIB_VERSION(r_lib);
@ -303,31 +302,6 @@ R_API int r_lib_open_ptr (RLib *lib, const char *file, void *handler, RLibStruct
return ret;
}
#if __WINDOWS__ && !defined(__CYGWIN__)
static char *utf16_to_utf8 (const wchar_t *wc) {
char *rutf8;
int csize;
if ((csize = WideCharToMultiByte (CP_UTF8, 0, wc, -1, NULL, 0, NULL, NULL))) {
if ((rutf8 = malloc (csize))) {
WideCharToMultiByte (CP_UTF8, 0, wc, -1, rutf8, csize, NULL, NULL);
}
}
return rutf8;
}
static wchar_t *utf8_to_utf16 (const char *cstring) {
wchar_t *rutf16;
int wcsize;
if ((wcsize = MultiByteToWideChar (CP_UTF8, 0, cstring, -1, NULL, 0))) {
if ((rutf16 = (wchar_t *) calloc (wcsize, sizeof (wchar_t)))) {
MultiByteToWideChar (CP_UTF8, 0, cstring, -1, rutf16, wcsize);
}
}
return rutf16;
}
#endif
R_API int r_lib_opendir(RLib *lib, const char *path) {
#if __WINDOWS__ && !defined(__CYGWIN__)
@ -351,7 +325,7 @@ R_API int r_lib_opendir(RLib *lib, const char *path) {
return false;
}
#if __WINDOWS__ && !defined(__CYGWIN__)
wcpath = utf8_to_utf16 (path);
wcpath = r_utf8_to_utf16 (path);
if (!wcpath) {
return false;
}
@ -364,7 +338,7 @@ R_API int r_lib_opendir(RLib *lib, const char *path) {
}
do {
swprintf (file, sizeof (file), L"%ls/%ls", wcpath, dir.cFileName);
wctocbuff = utf16_to_utf8 (file);
wctocbuff = r_utf16_to_utf8 (file);
if (wctocbuff) {
if (r_lib_dl_check_filename (wctocbuff)) {
r_lib_open (lib, wctocbuff);

View File

@ -277,7 +277,24 @@ R_API int r_sandbox_kill(int pid, int sig) {
#endif
return -1;
}
#if __WINDOWS__ && !defined(__CYGWIN__)
R_API HANDLE r_sandbox_opendir (const char *path, WIN32_FIND_DATAW *entry, wchar_t *dir, wchar_t *wcpath) {
if (!path) {
return NULL;
}
if (r_sandbox_enable (0)) {
if (path && !r_sandbox_check_path (path)) {
return NULL;
}
}
if (!(wcpath = r_utf8_to_utf16 (path))) {
return NULL;
}
swprintf (dir, MAX_PATH, L"%ls\\*.*", wcpath);
return FindFirstFileW (dir, entry);
}
#else
R_API DIR* r_sandbox_opendir (const char *path) {
if (!path)
return NULL;
@ -286,9 +303,10 @@ R_API DIR* r_sandbox_opendir (const char *path) {
return NULL;
}
}
return opendir (path);
}
#endif
R_API int r_sys_stop () {
int pid;
if (enabled) {

View File

@ -10,7 +10,6 @@
# endif
#endif
#include <sys/types.h>
#include <dirent.h>
#include <r_types.h>
#include <r_util.h>
#include <r_lib.h>
@ -122,6 +121,31 @@ R_API int r_sys_truncate(const char *file, int sz) {
R_API RList *r_sys_dir(const char *path) {
RList *list = NULL;
#if __WINDOWS__ && !defined(__CYGWIN__)
HANDLE fh;
WIN32_FIND_DATAW entry;
wchar_t dir[MAX_PATH];
wchar_t *wcpath;
char *cfname;
fh = r_sandbox_opendir (path, &entry, dir, wcpath);
if (fh == INVALID_HANDLE_VALUE) {
//IFDGB eprintf ("Cannot open directory %ls\n", wcpath);
free (wcpath);
return list;
}
list = r_list_newf (free);
if (list) {
do {
if ((cfname = r_utf16_to_utf8 (entry.cFileName))) {
r_list_append (list, strdup (cfname));
free (cfname);
}
} while (FindNextFileW (fh, &entry));
}
free (wcpath);
FindClose (fh);
#else
struct dirent *entry;
DIR *dir = r_sandbox_opendir (path);
if (dir) {
@ -134,6 +158,7 @@ R_API RList *r_sys_dir(const char *path) {
}
closedir (dir);
}
#endif
return list;
}

View File

@ -299,3 +299,27 @@ R_API int r_isprint (const RRune c) {
return true;
}
R_API char *r_utf16_to_utf8 (const wchar_t *wc) {
char *rutf8;
int csize;
if ((csize = WideCharToMultiByte (CP_UTF8, 0, wc, -1, NULL, 0, NULL, NULL))) {
if ((rutf8 = malloc (csize))) {
WideCharToMultiByte (CP_UTF8, 0, wc, -1, rutf8, csize, NULL, NULL);
}
}
return rutf8;
}
R_API wchar_t *r_utf8_to_utf16 (const char *cstring) {
wchar_t *rutf16;
int wcsize;
if ((wcsize = MultiByteToWideChar (CP_UTF8, 0, cstring, -1, NULL, 0))) {
if ((rutf16 = (wchar_t *) calloc (wcsize, sizeof (wchar_t)))) {
MultiByteToWideChar (CP_UTF8, 0, cstring, -1, rutf16, wcsize);
}
}
return rutf16;
}