* src/lib/Evil.h:

* src/lib/evil.c: (evil_last_error_get):
add evil_last_error_get() function to get
useful error string creation.
* src/lib/dlfcn/dlfcn.c: (get_last_error), (dlopen), (dlsym),
(dladdr):
use UNICODE check instead of compiler checks when needed.
use evil_last_error_get()
* src/lib/mman/mman.c: (mmap), (munmap):
file mapping does not work on Windows CE < 5.0.
if it is the case, just read the file and return
the data.
use evil_last_error_get()
* src/lib/mman/Makefile.am:
* src/lib/Makefile.am:
add libevil dependancy


SVN revision: 34775
This commit is contained in:
doursse 2008-06-08 21:39:49 +00:00 committed by doursse
parent c51a00a7d3
commit 0fbdc23c40
7 changed files with 241 additions and 93 deletions

View File

@ -1,3 +1,25 @@
2008-06-08 Vincent Torri <doursse at users dot sf dot net>
* src/lib/Evil.h:
* src/lib/evil.c: (evil_last_error_get):
add evil_last_error_get() function to get
useful error string creation.
* src/lib/dlfcn/dlfcn.c: (get_last_error), (dlopen), (dlsym),
(dladdr):
use UNICODE check instead of compiler checks when needed.
use evil_last_error_get()
* src/lib/mman/mman.c: (mmap), (munmap):
file mapping does not work on Windows CE < 5.0.
if it is the case, just read the file and return
the data.
use evil_last_error_get()
* src/lib/mman/Makefile.am:
* src/lib/Makefile.am:
add libevil dependancy
2008-06-07 Vincent Torri <doursse at users dot sf dot net> 2008-06-07 Vincent Torri <doursse at users dot sf dot net>
* src/lib/Evil.h: * src/lib/Evil.h:

View File

@ -517,7 +517,7 @@ EAPI const char *evil_tmpdir_get(void);
*/ */
EAPI char *evil_getcwd(char *buffer, size_t size); EAPI char *evil_getcwd(char *buffer, size_t size);
#if defined(__CEGCC__) || defined(__MINGW32CE__) #ifdef UNICODE
/** /**
* @brief Convert a string from char * to wchar_t *. * @brief Convert a string from char * to wchar_t *.
@ -557,7 +557,9 @@ EAPI wchar_t *evil_char_to_wchar(const char *text);
*/ */
EAPI char *evil_wchar_to_char(const wchar_t *text); EAPI char *evil_wchar_to_char(const wchar_t *text);
#endif /* __CEGCC__ || __MINGW32CE__ */ #endif /* UNICODE */
EAPI char *evil_last_error_get(void);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -12,4 +12,3 @@ libevil_la_CFLAGS = @win32_cflags@
libevil_la_LIBADD = @win32_libs@ libevil_la_LIBADD = @win32_libs@
libevil_la_DEPENDENCIES = $(top_builddir)/config.h libevil_la_DEPENDENCIES = $(top_builddir)/config.h
libevil_la_LDFLAGS = -no-undefined @lt_enable_auto_import@ -version-info @version_info@ libevil_la_LDFLAGS = -no-undefined @lt_enable_auto_import@ -version-info @version_info@

View File

@ -25,27 +25,14 @@ static int dl_err_viewed = 0;
static void static void
get_last_error(char *desc) get_last_error(char *desc)
{ {
TCHAR *str; char *str;
char *str2;
int l1; int l1;
int l2; int l2;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | str = evil_last_error_get();
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&str, 0, NULL);
#if defined(__CEGCC__) || defined(__MINGW32CE__)
str2 = evil_wchar_to_char(str);
LocalFree(str);
#else
str2 = str;
#endif /* ! __CEGCC__ && ! __MINGW32CE__ */
l1 = strlen(desc); l1 = strlen(desc);
l2 = strlen(str2); l2 = strlen(str);
if (dl_err) if (dl_err)
free(dl_err); free(dl_err);
@ -56,10 +43,10 @@ get_last_error(char *desc)
else else
{ {
memcpy(dl_err, desc, l1); memcpy(dl_err, desc, l1);
memcpy(dl_err + l1, str2, l2); memcpy(dl_err + l1, str, l2);
dl_err[l1 + l2] = '\0'; dl_err[l1 + l2] = '\0';
} }
LocalFree(str2); free(str);
dl_err_viewed = 0; dl_err_viewed = 0;
} }
@ -98,7 +85,7 @@ dlopen(const char* path, int mode __UNUSED__)
else else
new_path[i] = path[i]; new_path[i] = path[i];
} }
#if defined(__CEGCC__) || defined(__MINGW32CE__) #ifdef UNICODE
{ {
wchar_t *wpath; wchar_t *wpath;
@ -109,7 +96,7 @@ dlopen(const char* path, int mode __UNUSED__)
#else #else
module = LoadLibraryEx(new_path, NULL, module = LoadLibraryEx(new_path, NULL,
LOAD_WITH_ALTERED_SEARCH_PATH); LOAD_WITH_ALTERED_SEARCH_PATH);
#endif /* ! __CEGCC__ && ! __MINGW32CE__ */ #endif /* ! UNICODE */
if (!module) if (!module)
get_last_error("LoadLibraryEx returned: "); get_last_error("LoadLibraryEx returned: ");
@ -136,7 +123,7 @@ dlsym(void *handle, const char *symbol)
{ {
FARPROC fp; FARPROC fp;
#if defined(__CEGCC__) || defined(__MINGW32CE__) #ifdef UNICODE
{ {
wchar_t *wsymbol; wchar_t *wsymbol;
@ -146,7 +133,7 @@ dlsym(void *handle, const char *symbol)
} }
#else #else
fp = GetProcAddress(handle, symbol); fp = GetProcAddress(handle, symbol);
#endif /* ! __CEGCC__ && ! __MINGW32CE__ */ #endif /* ! UNICODE */
if (!fp) if (!fp)
get_last_error("GetProcAddress returned: "); get_last_error("GetProcAddress returned: ");
@ -169,11 +156,11 @@ dladdr (void *addr __UNUSED__, Dl_info *info)
if (!ret) if (!ret)
return 0; return 0;
#if defined(__CEGCC__) || defined(__MINGW32CE__) #ifdef UNICODE
path = evil_wchar_to_char(tpath); path = evil_wchar_to_char(tpath);
#else #else
path = tpath; path = tpath;
#endif /* ! __CEGCC__ && ! __MINGW32CE__ */ #endif /* ! UNICODE */
length = strlen (path); length = strlen (path);
if (length >= PATH_MAX) if (length >= PATH_MAX)
@ -195,9 +182,9 @@ dladdr (void *addr __UNUSED__, Dl_info *info)
info->dli_sname = NULL; info->dli_sname = NULL;
info->dli_saddr = NULL; info->dli_saddr = NULL;
#if defined(__CEGCC__) || defined(__MINGW32CE__) #ifdef UNICODE
free (path); free (path);
#endif /* __CEGCC__ || __MINGW32CE__ */ #endif /* ! UNICODE */
return 1; return 1;
} }

View File

@ -15,9 +15,9 @@
# include <sys/syslimits.h> # include <sys/syslimits.h>
#endif /* __CEGCC__ */ #endif /* __CEGCC__ */
#if defined(__CEGCC__) || defined(__MINGW32CE__) #ifdef UNICODE
# include <wchar.h> # include <wchar.h>
#endif /* __CEGCC__ || __MINGW32CE__ */ #endif /* UNICODE */
#include <sys/types.h> #include <sys/types.h>
#include <sys/timeb.h> #include <sys/timeb.h>
@ -497,7 +497,7 @@ evil_getcwd(char *buffer, size_t size)
#endif /* ! __CEGCC__ && ! __MINGW32CE__ */ #endif /* ! __CEGCC__ && ! __MINGW32CE__ */
} }
#if defined(__CEGCC__) || defined(__MINGW32CE__) #ifdef UNICODE
wchar_t * wchar_t *
evil_char_to_wchar(const char *text) evil_char_to_wchar(const char *text)
@ -541,4 +541,34 @@ evil_wchar_to_char(const wchar_t *text)
return atext; return atext;
} }
#endif /* __CEGCC__ || __MINGW32CE__ */ #endif /* UNICODE */
char *
evil_last_error_get(void)
{
char str[PATH_MAX];
LPTSTR str1;
char *str2;
DWORD err;
err = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err,
0, // Default language
(LPTSTR)&str1,
0,
NULL);
#ifdef UNICODE
str2 = evil_wchar_to_char(str1);
LocalFree(str1);
#else
str2 = str1;
#endif /* ! UNICODE */
snprintf(str, PATH_MAX, "(%ld) %s", err, str2);
LocalFree(str2);
return strdup(str);
}

View File

@ -7,5 +7,6 @@ nobase_include_HEADERS = sys/mman.h
libmman_la_SOURCES = mman.c libmman_la_SOURCES = mman.c
libmman_la_CFLAGS = @win32_cflags@ libmman_la_CFLAGS = @win32_cflags@
libmman_la_DEPENDENCIES = $(top_builddir)/config.h libmman_la_LIBADD = $(top_builddir)/src/lib/libevil.la
libmman_la_DEPENDENCIES = $(top_builddir)/config.h $(top_builddir)/src/lib/libevil.la
libmman_la_LDFLAGS = -no-undefined @lt_enable_auto_import@ -version-info @version_info@ libmman_la_LDFLAGS = -no-undefined @lt_enable_auto_import@ -version-info @version_info@

View File

@ -1,4 +1,6 @@
#include <stdio.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h>
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <windows.h> #include <windows.h>
@ -8,6 +10,8 @@
# include <io.h> # include <io.h>
#endif /* ! __CEGCC__ */ #endif /* ! __CEGCC__ */
#include "../Evil.h"
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
# include "config.h" # include "config.h"
#endif /* HAVE_CONFIG_H */ #endif /* HAVE_CONFIG_H */
@ -21,13 +25,15 @@
#endif /* HAVE___ATTRIBUTE__ */ #endif /* HAVE___ATTRIBUTE__ */
#if defined(__CEGCC__) #if defined(__CEGCC__)
# define CreateFileMapping CreateFileMappingW
# define _get_osfhandle get_osfhandle # define _get_osfhandle get_osfhandle
# elif defined (__MINGW32CE__) # elif defined (__MINGW32CE__)
# define _get_osfhandle(FILEDES) ((long)FILEDES) # define _get_osfhandle(FILEDES) ((long)FILEDES)
#endif /* ! __CEGCC__ && ! __MINGW32CE__ */ #endif /* ! __CEGCC__ && ! __MINGW32CE__ */
/***** API *****/
void * void *
mmap(void *addr __UNUSED__, mmap(void *addr __UNUSED__,
size_t len, size_t len,
@ -36,86 +42,187 @@ mmap(void *addr __UNUSED__,
int fd, int fd,
off_t offset) off_t offset)
{ {
OSVERSIONINFO os_version;
void *data; void *data;
HANDLE fm;
DWORD protect = PAGE_NOACCESS;
DWORD access = 0;
/* support only MAP_SHARED */ os_version.dwOSVersionInfoSize = sizeof(os_version);
if (!(flags & MAP_SHARED)) return (void *)~0; if (!GetVersionEx(&os_version))
if (prot & PROT_EXEC)
{ {
if (prot & PROT_READ) char *str;
str = evil_last_error_get();
fprintf(stderr, "[Evil] [mmap] GetVersionEx failed: %s\n", str);
free(str);
return MAP_FAILED;
}
if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_CE) &&
(os_version.dwMajorVersion < 5))
{
size_t size;
data = malloc(len);
if (!data)
{ {
if (prot & PROT_WRITE) fprintf (stderr, "[Evil] [mmap] malloc failed\n");
protect = PAGE_EXECUTE_READWRITE; return MAP_FAILED;
}
size = read(fd, data, len);
if (size != len)
{
fprintf (stderr, "[Evil] [mmap] read failed\n");
free(data);
return MAP_FAILED;
}
if (lseek(fd, 0, SEEK_SET) == -1)
{
fprintf (stderr, "[Evil] [mmap] lseek failed\n");
free(data);
return MAP_FAILED;
}
}
else
{
HANDLE fm;
DWORD protect = PAGE_NOACCESS;
DWORD access = 0;
HANDLE handle;
/* support only MAP_SHARED */
if (!(flags & MAP_SHARED))
return MAP_FAILED;
if (prot & PROT_EXEC)
{
if (prot & PROT_READ)
{
if (prot & PROT_WRITE)
protect = PAGE_EXECUTE_READWRITE;
else
protect = PAGE_EXECUTE_READ;
}
else else
protect = PAGE_EXECUTE_READ; {
if (prot & PROT_WRITE)
protect = PAGE_EXECUTE_WRITECOPY;
else
protect = PAGE_EXECUTE;
}
} }
else else
{ {
if (prot & PROT_WRITE) if (prot & PROT_READ)
protect = PAGE_EXECUTE_WRITECOPY; {
else if (prot & PROT_WRITE)
protect = PAGE_EXECUTE; protect = PAGE_READWRITE;
else
protect = PAGE_READONLY;
}
else if (prot & PROT_WRITE)
protect = PAGE_WRITECOPY;
} }
}
else handle = (HANDLE)_get_osfhandle(fd);
{ if (handle == INVALID_HANDLE_VALUE)
if (prot & PROT_READ)
{ {
if (prot & PROT_WRITE) fprintf(stderr, "[Evil] [mmap] _get_osfhandle failed\n");
protect = PAGE_READWRITE;
else return MAP_FAILED;
protect = PAGE_READONLY;
} }
else if (prot & PROT_WRITE)
protect = PAGE_WRITECOPY;
}
fm = CreateFileMapping((HANDLE)_get_osfhandle (fd), fm = CreateFileMapping(handle, NULL, protect, 0, 0, NULL);
NULL, if (!fm)
protect, {
0, char *str;
0,
NULL); str = evil_last_error_get();
if (!fm) return (void *)~0; fprintf(stderr, "[Evil] [mmap] CreateFileMapping failed: %s\n", str);
free(str);
return MAP_FAILED;
}
if (protect & PAGE_READWRITE)
access = FILE_MAP_ALL_ACCESS;
if (protect & PAGE_WRITECOPY)
access = FILE_MAP_COPY;
#if 0
if (protect & (PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ))
access = FILE_MAP_EXECUTE;
#endif
if (protect & (PAGE_READWRITE | PAGE_READONLY))
access = FILE_MAP_READ;
else
{
if (protect & PAGE_READWRITE) if (protect & PAGE_READWRITE)
access = FILE_MAP_WRITE; access = FILE_MAP_ALL_ACCESS;
if (protect & PAGE_WRITECOPY)
access = FILE_MAP_COPY;
#if 0
if (protect & (PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_READ))
access = FILE_MAP_EXECUTE;
#endif
if (protect & (PAGE_READWRITE | PAGE_READONLY))
access = FILE_MAP_READ;
else
{
if (protect & PAGE_READWRITE)
access = FILE_MAP_WRITE;
}
data = MapViewOfFile(fm,
access,
offset & 0xffff0000,
offset & 0x0000ffff,
len);
CloseHandle(fm);
if (!data)
{
char *str;
str = evil_last_error_get();
fprintf(stderr, "[Evil] [mmap] MapViewOfFile failed: %s\n", str);
free(str);
return MAP_FAILED;
}
} }
data = MapViewOfFile(fm, return data;
access,
offset & 0xffff0000,
offset & 0x0000ffff,
len);
CloseHandle(fm);
if (!data) return (void *)~0;
else return data;
} }
int int
munmap(void *addr, munmap(void *addr,
size_t len __UNUSED__) size_t len __UNUSED__)
{ {
BOOL res; OSVERSIONINFO os_version;
res = UnmapViewOfFile(addr); os_version.dwOSVersionInfoSize = sizeof(os_version);
if (!GetVersionEx(&os_version))
{
char *str;
return (res == 0) ? -1 : 0; str = evil_last_error_get();
fprintf(stderr, "[Evil] [munmap] GetVersionEx failed: %s\n", str);
free(str);
return -1;
}
if ((os_version.dwPlatformId == VER_PLATFORM_WIN32_CE) &&
(os_version.dwMajorVersion < 5))
{
if (addr && (addr != MAP_FAILED))
free(addr);
return 0;
}
else
{
BOOL res;
res = UnmapViewOfFile(addr);
if (!res)
{
char *str;
str = evil_last_error_get();
fprintf(stderr, "[Evil] [munmap] UnmapViewOfFile failed: %s\n", str);
free(str);
}
return (res == 0) ? -1 : 0;
}
} }