2001-02-24 20:28:24 +00:00
|
|
|
/********************************************************
|
2001-11-03 19:40:38 +00:00
|
|
|
*
|
2001-02-24 20:28:24 +00:00
|
|
|
*
|
|
|
|
* Stub functions for Wine module
|
|
|
|
*
|
|
|
|
*
|
|
|
|
********************************************************/
|
2005-04-15 20:17:14 +00:00
|
|
|
|
|
|
|
/*
|
2006-06-22 13:34:00 +00:00
|
|
|
* Modified for use with MPlayer, detailed changelog at
|
|
|
|
* http://svn.mplayerhq.hu/mplayer/trunk/
|
2005-04-15 20:17:14 +00:00
|
|
|
*/
|
|
|
|
|
2001-12-11 22:58:14 +00:00
|
|
|
#include "config.h"
|
2001-07-12 15:27:48 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2001-02-24 20:28:24 +00:00
|
|
|
#include <unistd.h>
|
2008-03-01 10:31:51 +00:00
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
2001-02-24 20:28:24 +00:00
|
|
|
#include <sys/mman.h>
|
2008-03-03 09:47:01 +00:00
|
|
|
#else
|
|
|
|
#include "osdep/mmap.h"
|
2008-03-01 10:31:51 +00:00
|
|
|
#endif
|
2001-02-24 20:28:24 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
2001-12-11 22:58:14 +00:00
|
|
|
#include <ctype.h>
|
2002-09-13 19:43:17 +00:00
|
|
|
|
2006-11-25 01:22:20 +00:00
|
|
|
#include "osdep/mmap_anon.h"
|
2002-09-13 19:43:17 +00:00
|
|
|
#include "wine/windef.h"
|
|
|
|
#include "wine/winbase.h"
|
|
|
|
#include "wine/debugtools.h"
|
|
|
|
#include "wine/heap.h"
|
2001-07-12 15:27:48 +00:00
|
|
|
#include "ext.h"
|
|
|
|
|
2001-11-03 19:40:38 +00:00
|
|
|
#if 0
|
|
|
|
//REMOVE SIMPLIFY
|
|
|
|
static void* mymalloc(unsigned int size)
|
|
|
|
{
|
|
|
|
printf("malloc %d\n", size);
|
|
|
|
return malloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef malloc
|
|
|
|
#define malloc mymalloc
|
|
|
|
#endif
|
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
int dbg_header_err( const char *dbg_channel, const char *func )
|
|
|
|
{
|
2001-11-03 19:40:38 +00:00
|
|
|
return 0;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
int dbg_header_warn( const char *dbg_channel, const char *func )
|
|
|
|
{
|
2001-11-03 19:40:38 +00:00
|
|
|
return 0;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
int dbg_header_fixme( const char *dbg_channel, const char *func )
|
|
|
|
{
|
2001-11-03 19:40:38 +00:00
|
|
|
return 0;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
int dbg_header_trace( const char *dbg_channel, const char *func )
|
|
|
|
{
|
2001-11-03 19:40:38 +00:00
|
|
|
return 0;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2001-07-12 15:27:48 +00:00
|
|
|
int dbg_vprintf( const char *format, va_list args )
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2001-11-03 19:40:38 +00:00
|
|
|
return 0;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
int __vprintf( const char *format, ... )
|
|
|
|
{
|
|
|
|
#ifdef DETAILED_OUT
|
|
|
|
va_list va;
|
|
|
|
va_start(va, format);
|
|
|
|
vprintf(format, va);
|
|
|
|
va_end(va);
|
|
|
|
#endif
|
2001-11-03 19:40:38 +00:00
|
|
|
return 0;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2001-11-03 19:40:38 +00:00
|
|
|
|
2001-07-12 15:27:48 +00:00
|
|
|
HANDLE WINAPI GetProcessHeap(void)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-07-12 15:27:48 +00:00
|
|
|
LPVOID WINAPI HeapAlloc(HANDLE heap, DWORD flags, DWORD size)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2007-09-10 12:51:26 +00:00
|
|
|
//static int i = 5;
|
2001-11-03 19:40:38 +00:00
|
|
|
void* m = (flags & 0x8) ? calloc(size, 1) : malloc(size);
|
|
|
|
//printf("HeapAlloc %p %d (%d)\n", m, size, flags);
|
|
|
|
//if (--i == 0)
|
|
|
|
// abort();
|
|
|
|
return m;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2001-10-04 02:21:47 +00:00
|
|
|
WIN_BOOL WINAPI HeapFree(HANDLE heap, DWORD flags, LPVOID mem)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2001-03-18 01:01:03 +00:00
|
|
|
if (mem) free(mem);
|
2001-11-03 19:40:38 +00:00
|
|
|
//printf("HeapFree %p\n", mem);
|
|
|
|
//if (!mem)
|
|
|
|
// abort();
|
2001-02-24 20:28:24 +00:00
|
|
|
return 1;
|
2001-11-03 19:40:38 +00:00
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
static int last_error;
|
|
|
|
|
2001-07-12 15:27:48 +00:00
|
|
|
DWORD WINAPI GetLastError(void)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
return last_error;
|
|
|
|
}
|
|
|
|
|
2001-07-12 15:27:48 +00:00
|
|
|
VOID WINAPI SetLastError(DWORD error)
|
2001-11-03 19:40:38 +00:00
|
|
|
{
|
2001-10-04 02:21:47 +00:00
|
|
|
last_error=error;
|
2001-11-03 19:40:38 +00:00
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-10-04 02:21:47 +00:00
|
|
|
WIN_BOOL WINAPI ReadFile(HANDLE handle, LPVOID mem, DWORD size, LPDWORD result, LPOVERLAPPED flags)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
*result=read(handle, mem, size);
|
|
|
|
return *result;
|
2001-11-03 19:40:38 +00:00
|
|
|
}
|
2001-10-04 02:21:47 +00:00
|
|
|
INT WINAPI lstrcmpiA(LPCSTR c1, LPCSTR c2)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
return strcasecmp(c1,c2);
|
|
|
|
}
|
2001-07-12 15:27:48 +00:00
|
|
|
LPSTR WINAPI lstrcpynA(LPSTR dest, LPCSTR src, INT num)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2001-07-12 15:27:48 +00:00
|
|
|
return strncpy(dest,src,num);
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2001-10-04 02:21:47 +00:00
|
|
|
INT WINAPI lstrlenA(LPCSTR s)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
return strlen(s);
|
2001-11-03 19:40:38 +00:00
|
|
|
}
|
2001-07-12 15:27:48 +00:00
|
|
|
INT WINAPI lstrlenW(LPCWSTR s)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
int l;
|
|
|
|
if(!s)
|
|
|
|
return 0;
|
|
|
|
l=0;
|
|
|
|
while(s[l])
|
|
|
|
l++;
|
|
|
|
return l;
|
|
|
|
}
|
2001-07-12 15:27:48 +00:00
|
|
|
LPSTR WINAPI lstrcpynWtoA(LPSTR dest, LPCWSTR src, INT count)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2001-10-04 02:21:47 +00:00
|
|
|
LPSTR result = dest;
|
2001-02-24 20:28:24 +00:00
|
|
|
int moved=0;
|
|
|
|
if((dest==0) || (src==0))
|
|
|
|
return 0;
|
|
|
|
while(moved<count)
|
|
|
|
{
|
|
|
|
*dest=*src;
|
|
|
|
moved++;
|
|
|
|
if(*src==0)
|
2001-07-12 15:27:48 +00:00
|
|
|
break;
|
2001-02-24 20:28:24 +00:00
|
|
|
src++;
|
|
|
|
dest++;
|
|
|
|
}
|
2001-10-04 02:21:47 +00:00
|
|
|
return result;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2001-12-11 22:58:14 +00:00
|
|
|
/* i stands here for ignore case! */
|
2001-02-24 20:28:24 +00:00
|
|
|
int wcsnicmp(const unsigned short* s1, const unsigned short* s2, int n)
|
|
|
|
{
|
2001-10-04 02:21:47 +00:00
|
|
|
/*
|
2001-02-24 20:28:24 +00:00
|
|
|
if(s1==0)
|
|
|
|
return;
|
|
|
|
if(s2==0)
|
|
|
|
return;
|
2001-10-04 02:21:47 +00:00
|
|
|
*/
|
2001-02-24 20:28:24 +00:00
|
|
|
while(n>0)
|
|
|
|
{
|
2001-12-11 22:58:14 +00:00
|
|
|
if (((*s1 | *s2) & 0xff00) || toupper((char)*s1) != toupper((char)*s2))
|
|
|
|
{
|
|
|
|
|
|
|
|
if(*s1<*s2)
|
|
|
|
return -1;
|
2001-02-24 20:28:24 +00:00
|
|
|
else
|
2001-12-11 22:58:14 +00:00
|
|
|
if(*s1>*s2)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
if(*s1==0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
s1++;
|
|
|
|
s2++;
|
|
|
|
n--;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2001-11-03 19:40:38 +00:00
|
|
|
}
|
|
|
|
|
2001-07-12 15:27:48 +00:00
|
|
|
WIN_BOOL WINAPI IsBadReadPtr(LPCVOID data, UINT size)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
if(size==0)
|
|
|
|
return 0;
|
|
|
|
if(data==NULL)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
2001-11-03 19:40:38 +00:00
|
|
|
}
|
2001-07-12 15:27:48 +00:00
|
|
|
LPSTR HEAP_strdupA(HANDLE heap, DWORD flags, LPCSTR string)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
// return strdup(string);
|
2002-09-13 19:43:17 +00:00
|
|
|
char* answ = (char*) malloc(strlen(string) + 1);
|
2001-02-24 20:28:24 +00:00
|
|
|
strcpy(answ, string);
|
|
|
|
return answ;
|
|
|
|
}
|
2001-07-12 15:27:48 +00:00
|
|
|
LPWSTR HEAP_strdupAtoW(HANDLE heap, DWORD flags, LPCSTR string)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
int size, i;
|
2002-09-13 19:43:17 +00:00
|
|
|
WCHAR* answer;
|
2001-02-24 20:28:24 +00:00
|
|
|
if(string==0)
|
|
|
|
return 0;
|
|
|
|
size=strlen(string);
|
2002-09-13 19:43:17 +00:00
|
|
|
answer = (WCHAR*) malloc(sizeof(WCHAR) * (size + 1));
|
2001-02-24 20:28:24 +00:00
|
|
|
for(i=0; i<=size; i++)
|
|
|
|
answer[i]=(short)string[i];
|
2001-11-03 19:40:38 +00:00
|
|
|
return answer;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2001-07-12 15:27:48 +00:00
|
|
|
LPSTR HEAP_strdupWtoA(HANDLE heap, DWORD flags, LPCWSTR string)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
int size, i;
|
|
|
|
char* answer;
|
|
|
|
if(string==0)
|
|
|
|
return 0;
|
|
|
|
size=0;
|
|
|
|
while(string[size])
|
|
|
|
size++;
|
2002-09-13 19:43:17 +00:00
|
|
|
answer = (char*) malloc(size + 2);
|
2001-02-24 20:28:24 +00:00
|
|
|
for(i=0; i<=size; i++)
|
|
|
|
answer[i]=(char)string[i];
|
2001-11-03 19:40:38 +00:00
|
|
|
return answer;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* FILE_dommap
|
|
|
|
*/
|
|
|
|
|
|
|
|
//#define MAP_PRIVATE
|
|
|
|
//#define MAP_SHARED
|
|
|
|
LPVOID FILE_dommap( int unix_handle, LPVOID start,
|
|
|
|
DWORD size_high, DWORD size_low,
|
|
|
|
DWORD offset_high, DWORD offset_low,
|
|
|
|
int prot, int flags )
|
|
|
|
{
|
|
|
|
int fd = -1;
|
|
|
|
int pos;
|
|
|
|
LPVOID ret;
|
|
|
|
|
|
|
|
if (size_high || offset_high)
|
|
|
|
printf("offsets larger than 4Gb not supported\n");
|
|
|
|
|
|
|
|
if (unix_handle == -1)
|
|
|
|
{
|
2006-11-26 13:09:46 +00:00
|
|
|
ret = mmap_anon( start, size_low, prot, flags, offset_low );
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
else
|
2006-11-25 01:22:20 +00:00
|
|
|
{
|
|
|
|
fd = unix_handle;
|
|
|
|
ret = mmap( start, size_low, prot, flags, fd, offset_low );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != (LPVOID)-1)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
// printf("address %08x\n", *(int*)ret);
|
2001-11-03 19:40:38 +00:00
|
|
|
// printf("%x\n", ret);
|
2001-02-24 20:28:24 +00:00
|
|
|
return ret;
|
2001-11-03 19:40:38 +00:00
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
// printf("mmap %d\n", errno);
|
|
|
|
|
|
|
|
/* mmap() failed; if this is because the file offset is not */
|
|
|
|
/* page-aligned (EINVAL), or because the underlying filesystem */
|
|
|
|
/* does not support mmap() (ENOEXEC), we do it by hand. */
|
|
|
|
|
|
|
|
if (unix_handle == -1) return ret;
|
|
|
|
if ((errno != ENOEXEC) && (errno != EINVAL)) return ret;
|
|
|
|
if (prot & PROT_WRITE)
|
|
|
|
{
|
|
|
|
/* We cannot fake shared write mappings */
|
|
|
|
#ifdef MAP_SHARED
|
|
|
|
if (flags & MAP_SHARED) return ret;
|
|
|
|
#endif
|
|
|
|
#ifdef MAP_PRIVATE
|
|
|
|
if (!(flags & MAP_PRIVATE)) return ret;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
|
|
|
|
/* Reserve the memory with an anonymous mmap */
|
|
|
|
ret = FILE_dommap( -1, start, size_high, size_low, 0, 0,
|
|
|
|
PROT_READ | PROT_WRITE, flags );
|
|
|
|
if (ret == (LPVOID)-1)
|
|
|
|
// {
|
|
|
|
// perror(
|
|
|
|
return ret;
|
|
|
|
/* Now read in the file */
|
|
|
|
if ((pos = lseek( fd, offset_low, SEEK_SET )) == -1)
|
|
|
|
{
|
|
|
|
FILE_munmap( ret, size_high, size_low );
|
|
|
|
// printf("lseek\n");
|
|
|
|
return (LPVOID)-1;
|
|
|
|
}
|
|
|
|
read( fd, ret, size_low );
|
|
|
|
lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */
|
|
|
|
mprotect( ret, size_low, prot ); /* Set the right protection */
|
|
|
|
// printf("address %08x\n", *(int*)ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* FILE_munmap
|
|
|
|
*/
|
|
|
|
int FILE_munmap( LPVOID start, DWORD size_high, DWORD size_low )
|
|
|
|
{
|
|
|
|
if (size_high)
|
|
|
|
printf("offsets larger than 4Gb not supported\n");
|
|
|
|
return munmap( start, size_low );
|
|
|
|
}
|
|
|
|
|
|
|
|
struct file_mapping_s;
|
|
|
|
typedef struct file_mapping_s
|
|
|
|
{
|
|
|
|
int mapping_size;
|
|
|
|
char* name;
|
2001-10-04 02:21:47 +00:00
|
|
|
LPVOID handle;
|
2001-02-24 20:28:24 +00:00
|
|
|
struct file_mapping_s* next;
|
|
|
|
struct file_mapping_s* prev;
|
|
|
|
}file_mapping;
|
|
|
|
static file_mapping* fm=0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define PAGE_NOACCESS 0x01
|
|
|
|
#define PAGE_READONLY 0x02
|
|
|
|
#define PAGE_READWRITE 0x04
|
|
|
|
#define PAGE_WRITECOPY 0x08
|
|
|
|
#define PAGE_EXECUTE 0x10
|
|
|
|
#define PAGE_EXECUTE_READ 0x20
|
|
|
|
#define PAGE_EXECUTE_READWRITE 0x40
|
|
|
|
#define PAGE_EXECUTE_WRITECOPY 0x80
|
|
|
|
#define PAGE_GUARD 0x100
|
|
|
|
#define PAGE_NOCACHE 0x200
|
|
|
|
|
2001-07-12 15:27:48 +00:00
|
|
|
HANDLE WINAPI CreateFileMappingA(HANDLE handle, LPSECURITY_ATTRIBUTES lpAttr,
|
|
|
|
DWORD flProtect,
|
|
|
|
DWORD dwMaxHigh, DWORD dwMaxLow,
|
|
|
|
LPCSTR name)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2001-07-12 15:27:48 +00:00
|
|
|
int hFile = (int)handle;
|
2001-02-24 20:28:24 +00:00
|
|
|
unsigned int len;
|
2001-10-04 02:21:47 +00:00
|
|
|
LPVOID answer;
|
2001-02-24 20:28:24 +00:00
|
|
|
int anon=0;
|
|
|
|
int mmap_access=0;
|
|
|
|
if(hFile<0)
|
2006-11-25 01:22:20 +00:00
|
|
|
anon=1;
|
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
if(!anon)
|
2001-11-03 19:40:38 +00:00
|
|
|
{
|
2001-02-24 20:28:24 +00:00
|
|
|
len=lseek(hFile, 0, SEEK_END);
|
|
|
|
lseek(hFile, 0, SEEK_SET);
|
|
|
|
}
|
|
|
|
else len=dwMaxLow;
|
|
|
|
|
|
|
|
if(flProtect & PAGE_READONLY)
|
|
|
|
mmap_access |=PROT_READ;
|
|
|
|
else
|
|
|
|
mmap_access |=PROT_READ|PROT_WRITE;
|
2001-11-03 19:40:38 +00:00
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
if(anon)
|
2006-11-26 13:09:46 +00:00
|
|
|
answer=mmap_anon(NULL, len, mmap_access, MAP_PRIVATE, 0);
|
2006-11-25 01:22:20 +00:00
|
|
|
else
|
|
|
|
answer=mmap(NULL, len, mmap_access, MAP_PRIVATE, hFile, 0);
|
|
|
|
|
2001-10-04 02:21:47 +00:00
|
|
|
if(answer!=(LPVOID)-1)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
if(fm==0)
|
|
|
|
{
|
2002-09-13 19:43:17 +00:00
|
|
|
fm = (file_mapping*) malloc(sizeof(file_mapping));
|
2001-02-24 20:28:24 +00:00
|
|
|
fm->prev=NULL;
|
2001-11-03 19:40:38 +00:00
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
else
|
|
|
|
{
|
2002-09-13 19:43:17 +00:00
|
|
|
fm->next = (file_mapping*) malloc(sizeof(file_mapping));
|
2001-02-24 20:28:24 +00:00
|
|
|
fm->next->prev=fm;
|
|
|
|
fm=fm->next;
|
|
|
|
}
|
2001-11-03 19:40:38 +00:00
|
|
|
fm->next=NULL;
|
2001-02-24 20:28:24 +00:00
|
|
|
fm->handle=answer;
|
|
|
|
if(name)
|
|
|
|
{
|
2002-09-13 19:43:17 +00:00
|
|
|
fm->name = (char*) malloc(strlen(name)+1);
|
2001-02-24 20:28:24 +00:00
|
|
|
strcpy(fm->name, name);
|
2001-11-03 19:40:38 +00:00
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
else
|
|
|
|
fm->name=NULL;
|
|
|
|
fm->mapping_size=len;
|
2001-11-03 19:40:38 +00:00
|
|
|
|
2001-10-04 02:21:47 +00:00
|
|
|
return (HANDLE)answer;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
return (HANDLE)0;
|
2001-11-03 19:40:38 +00:00
|
|
|
}
|
2001-07-12 15:27:48 +00:00
|
|
|
WIN_BOOL WINAPI UnmapViewOfFile(LPVOID handle)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
file_mapping* p;
|
|
|
|
int result;
|
|
|
|
if(fm==0)
|
2001-10-04 02:21:47 +00:00
|
|
|
return 0;
|
2001-02-24 20:28:24 +00:00
|
|
|
for(p=fm; p; p=p->next)
|
|
|
|
{
|
2001-10-04 02:21:47 +00:00
|
|
|
if(p->handle==handle)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
result=munmap((void*)handle, p->mapping_size);
|
|
|
|
if(p->next)p->next->prev=p->prev;
|
|
|
|
if(p->prev)p->prev->next=p->next;
|
|
|
|
if(p->name)
|
|
|
|
free(p->name);
|
|
|
|
if(p==fm)
|
|
|
|
fm=p->prev;
|
|
|
|
free(p);
|
|
|
|
return result;
|
2001-11-03 19:40:38 +00:00
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2001-11-03 19:40:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
//static int va_size=0;
|
|
|
|
struct virt_alloc_s;
|
|
|
|
typedef struct virt_alloc_s
|
|
|
|
{
|
|
|
|
int mapping_size;
|
|
|
|
char* address;
|
|
|
|
struct virt_alloc_s* next;
|
|
|
|
struct virt_alloc_s* prev;
|
|
|
|
int state;
|
|
|
|
}virt_alloc;
|
|
|
|
static virt_alloc* vm=0;
|
|
|
|
#define MEM_COMMIT 0x00001000
|
|
|
|
#define MEM_RESERVE 0x00002000
|
|
|
|
|
2001-10-04 02:21:47 +00:00
|
|
|
LPVOID WINAPI VirtualAlloc(LPVOID address, DWORD size, DWORD type, DWORD protection)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
void* answer;
|
2004-09-10 16:39:31 +00:00
|
|
|
long pgsz;
|
|
|
|
|
|
|
|
//printf("VirtualAlloc(0x%08X, %u, 0x%08X, 0x%08X)\n", (unsigned)address, size, type, protection);
|
|
|
|
|
|
|
|
if ((type&(MEM_RESERVE|MEM_COMMIT)) == 0) return NULL;
|
|
|
|
|
|
|
|
if (type&MEM_RESERVE && (unsigned)address&0xffff) {
|
|
|
|
size += (unsigned)address&0xffff;
|
2004-10-31 15:40:47 +00:00
|
|
|
address = (unsigned)address&~0xffff;
|
2004-09-10 16:39:31 +00:00
|
|
|
}
|
|
|
|
pgsz = sysconf(_SC_PAGESIZE);
|
|
|
|
if (type&MEM_COMMIT && (unsigned)address%pgsz) {
|
|
|
|
size += (unsigned)address%pgsz;
|
|
|
|
address -= (unsigned)address%pgsz;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type&MEM_RESERVE && size<0x10000) size = 0x10000;
|
|
|
|
if (size%pgsz) size += pgsz - size%pgsz;
|
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
if(address!=0)
|
|
|
|
{
|
2001-11-03 19:40:38 +00:00
|
|
|
//check whether we can allow to allocate this
|
2001-02-24 20:28:24 +00:00
|
|
|
virt_alloc* str=vm;
|
|
|
|
while(str)
|
|
|
|
{
|
|
|
|
if((unsigned)address>=(unsigned)str->address+str->mapping_size)
|
|
|
|
{
|
|
|
|
str=str->prev;
|
|
|
|
continue;
|
2001-11-03 19:40:38 +00:00
|
|
|
}
|
2004-09-10 16:39:31 +00:00
|
|
|
if((unsigned)address+size<=(unsigned)str->address)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
str=str->prev;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(str->state==0)
|
|
|
|
{
|
|
|
|
#warning FIXME
|
2004-09-10 16:39:31 +00:00
|
|
|
if( ((unsigned)address >= (unsigned)str->address)
|
|
|
|
&& ((unsigned)address+size<=(unsigned)str->address+str->mapping_size)
|
|
|
|
&& (type & MEM_COMMIT))
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
return address; //returning previously reserved memory
|
|
|
|
}
|
2004-09-10 16:39:31 +00:00
|
|
|
//printf(" VirtualAlloc(...) does not commit or not entirely within reserved, and\n");
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2004-09-10 16:39:31 +00:00
|
|
|
/*printf(" VirtualAlloc(...) (0x%08X, %u) overlaps with (0x%08X, %u, state=%d)\n",
|
|
|
|
(unsigned)address, size, (unsigned)str->address, str->mapping_size, str->state);*/
|
2001-02-24 20:28:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-11-03 19:40:38 +00:00
|
|
|
}
|
2004-09-10 16:39:31 +00:00
|
|
|
|
2006-11-25 01:22:20 +00:00
|
|
|
answer=mmap_anon(address, size, PROT_READ | PROT_WRITE | PROT_EXEC,
|
2006-11-26 13:09:46 +00:00
|
|
|
MAP_PRIVATE, 0);
|
2001-02-24 20:28:24 +00:00
|
|
|
// answer=FILE_dommap(-1, address, 0, size, 0, 0,
|
|
|
|
// PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE);
|
2006-11-25 01:22:20 +00:00
|
|
|
|
2004-09-10 16:39:31 +00:00
|
|
|
if (answer != (void *)-1 && address && answer != address) {
|
|
|
|
/* It is dangerous to try mmap() with MAP_FIXED since it does not
|
|
|
|
always detect conflicts or non-allocation and chaos ensues after
|
|
|
|
a successful call but an overlapping or non-allocated region. */
|
|
|
|
munmap(answer, size);
|
|
|
|
answer = (void *) -1;
|
|
|
|
errno = EINVAL;
|
|
|
|
//printf(" VirtualAlloc(...) cannot satisfy requested address but address=NULL would work.\n");
|
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
if(answer==(void*)-1)
|
|
|
|
{
|
2004-09-10 22:22:02 +00:00
|
|
|
/*printf(" VirtualAlloc(...) mmap(0x%08X, %u, ...) failed with errno=%d (\"%s\")\n",
|
|
|
|
(unsigned)address, size, errno, strerror(errno));*/
|
2001-02-24 20:28:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-09-13 19:43:17 +00:00
|
|
|
virt_alloc *new_vm = (virt_alloc*) malloc(sizeof(virt_alloc));
|
2001-02-24 20:28:24 +00:00
|
|
|
new_vm->mapping_size=size;
|
2002-09-13 19:43:17 +00:00
|
|
|
new_vm->address=(char*)answer;
|
2001-02-24 20:28:24 +00:00
|
|
|
new_vm->prev=vm;
|
|
|
|
if(type == MEM_RESERVE)
|
|
|
|
new_vm->state=0;
|
|
|
|
else
|
|
|
|
new_vm->state=1;
|
|
|
|
if(vm)
|
|
|
|
vm->next=new_vm;
|
|
|
|
vm=new_vm;
|
|
|
|
vm->next=0;
|
2001-11-26 01:17:24 +00:00
|
|
|
//if(va_size!=0)
|
|
|
|
// printf("Multiple VirtualAlloc!\n");
|
2004-09-10 16:39:31 +00:00
|
|
|
//printf(" VirtualAlloc(...) provides (0x%08X, %u)\n", (unsigned)answer, size);
|
2001-02-24 20:28:24 +00:00
|
|
|
return answer;
|
2001-11-03 19:40:38 +00:00
|
|
|
}
|
|
|
|
}
|
2004-09-10 16:39:31 +00:00
|
|
|
|
2001-11-26 01:17:24 +00:00
|
|
|
WIN_BOOL WINAPI VirtualFree(LPVOID address, SIZE_T dwSize, DWORD dwFreeType)//not sure
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
virt_alloc* str=vm;
|
|
|
|
int answer;
|
2004-09-10 16:39:31 +00:00
|
|
|
|
|
|
|
//printf("VirtualFree(0x%08X, %d, 0x%08X)\n", (unsigned)address, dwSize, dwFreeType);
|
2001-02-24 20:28:24 +00:00
|
|
|
while(str)
|
|
|
|
{
|
2001-11-03 19:40:38 +00:00
|
|
|
if(address!=str->address)
|
|
|
|
{
|
|
|
|
str=str->prev;
|
|
|
|
continue;
|
|
|
|
}
|
2004-09-10 16:39:31 +00:00
|
|
|
//printf(" VirtualFree(...) munmap(0x%08X, %d)\n", (unsigned)str->address, str->mapping_size);
|
2001-11-03 19:40:38 +00:00
|
|
|
answer=munmap(str->address, str->mapping_size);
|
|
|
|
if(str->next)str->next->prev=str->prev;
|
|
|
|
if(str->prev)str->prev->next=str->next;
|
2001-11-26 01:17:24 +00:00
|
|
|
if(vm==str)vm=str->prev;
|
2001-11-03 19:40:38 +00:00
|
|
|
free(str);
|
|
|
|
return 0;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-10-04 02:21:47 +00:00
|
|
|
INT WINAPI WideCharToMultiByte(UINT codepage, DWORD flags, LPCWSTR src,
|
|
|
|
INT srclen,LPSTR dest, INT destlen, LPCSTR defch, WIN_BOOL* used_defch)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
int i;
|
2001-03-18 01:01:03 +00:00
|
|
|
if(srclen==-1){srclen=0; while(src[srclen++]);}
|
2007-03-04 09:04:45 +00:00
|
|
|
if(destlen==0)
|
|
|
|
return srclen;
|
2001-02-24 20:28:24 +00:00
|
|
|
if(used_defch)
|
2001-11-03 19:40:38 +00:00
|
|
|
*used_defch=0;
|
2001-02-24 20:28:24 +00:00
|
|
|
for(i=0; i<min(srclen, destlen); i++)
|
2007-03-04 09:04:45 +00:00
|
|
|
*dest++=(char)*src++;
|
2001-02-24 20:28:24 +00:00
|
|
|
return min(srclen, destlen);
|
|
|
|
}
|
2001-10-04 02:21:47 +00:00
|
|
|
INT WINAPI MultiByteToWideChar(UINT codepage,DWORD flags, LPCSTR src, INT srclen,
|
|
|
|
LPWSTR dest, INT destlen)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2007-03-04 09:04:45 +00:00
|
|
|
int i;
|
|
|
|
if(srclen==-1){srclen=0; while(src[srclen++]);}
|
|
|
|
if(destlen==0)
|
|
|
|
return srclen;
|
|
|
|
for(i=0; i<min(srclen, destlen); i++)
|
|
|
|
*dest++=(WCHAR)*src++;
|
|
|
|
return min(srclen, destlen);
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2001-07-12 15:27:48 +00:00
|
|
|
HANDLE WINAPI OpenFileMappingA(DWORD access, WIN_BOOL prot, LPCSTR name)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
file_mapping* p;
|
|
|
|
if(fm==0)
|
|
|
|
return (HANDLE)0;
|
|
|
|
if(name==0)
|
|
|
|
return (HANDLE)0;
|
|
|
|
for(p=fm; p; p=p->prev)
|
|
|
|
{
|
|
|
|
if(p->name==0)
|
|
|
|
continue;
|
|
|
|
if(strcmp(p->name, name)==0)
|
2001-10-04 02:21:47 +00:00
|
|
|
return (HANDLE)p->handle;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2001-11-03 19:40:38 +00:00
|
|
|
return 0;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|