1 /********************************************************
4 * Stub functions for Wine module
7 ********************************************************/
10 * Modified for use with MPlayer, detailed CVS changelog at
11 * http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/
29 #include "wine/windef.h"
30 #include "wine/winbase.h"
31 #include "wine/debugtools.h"
32 #include "wine/heap.h"
37 static void* mymalloc(unsigned int size)
39 printf("malloc %d\n", size);
44 #define malloc mymalloc
47 int dbg_header_err( const char *dbg_channel, const char *func )
51 int dbg_header_warn( const char *dbg_channel, const char *func )
55 int dbg_header_fixme( const char *dbg_channel, const char *func )
59 int dbg_header_trace( const char *dbg_channel, const char *func )
63 int dbg_vprintf( const char *format, va_list args )
67 int __vprintf( const char *format, ... )
78 HANDLE WINAPI GetProcessHeap(void)
83 LPVOID WINAPI HeapAlloc(HANDLE heap, DWORD flags, DWORD size)
86 void* m = (flags & 0x8) ? calloc(size, 1) : malloc(size);
87 //printf("HeapAlloc %p %d (%d)\n", m, size, flags);
93 WIN_BOOL WINAPI HeapFree(HANDLE heap, DWORD flags, LPVOID mem)
96 //printf("HeapFree %p\n", mem);
102 static int last_error;
104 DWORD WINAPI GetLastError(void)
109 VOID WINAPI SetLastError(DWORD error)
114 WIN_BOOL WINAPI ReadFile(HANDLE handle, LPVOID mem, DWORD size, LPDWORD result, LPOVERLAPPED flags)
116 *result=read(handle, mem, size);
119 INT WINAPI lstrcmpiA(LPCSTR c1, LPCSTR c2)
121 return strcasecmp(c1,c2);
123 LPSTR WINAPI lstrcpynA(LPSTR dest, LPCSTR src, INT num)
125 return strncpy(dest,src,num);
127 INT WINAPI lstrlenA(LPCSTR s)
131 INT WINAPI lstrlenW(LPCWSTR s)
141 LPSTR WINAPI lstrcpynWtoA(LPSTR dest, LPCWSTR src, INT count)
145 if((dest==0) || (src==0))
158 /* i stands here for ignore case! */
159 int wcsnicmp(const unsigned short* s1, const unsigned short* s2, int n)
169 if (((*s1 | *s2) & 0xff00) || toupper((char)*s1) != toupper((char)*s2))
188 WIN_BOOL WINAPI IsBadReadPtr(LPCVOID data, UINT size)
196 LPSTR HEAP_strdupA(HANDLE heap, DWORD flags, LPCSTR string)
198 // return strdup(string);
199 char* answ = (char*) malloc(strlen(string) + 1);
200 strcpy(answ, string);
203 LPWSTR HEAP_strdupAtoW(HANDLE heap, DWORD flags, LPCSTR string)
210 answer = (WCHAR*) malloc(sizeof(WCHAR) * (size + 1));
211 for(i=0; i<=size; i++)
212 answer[i]=(short)string[i];
215 LPSTR HEAP_strdupWtoA(HANDLE heap, DWORD flags, LPCWSTR string)
224 answer = (char*) malloc(size + 2);
225 for(i=0; i<=size; i++)
226 answer[i]=(char)string[i];
230 /***********************************************************************
234 //#define MAP_PRIVATE
237 LPVOID FILE_dommap( int unix_handle, LPVOID start,
238 DWORD size_high, DWORD size_low,
239 DWORD offset_high, DWORD offset_low,
240 int prot, int flags )
246 if (size_high || offset_high)
247 printf("offsets larger than 4Gb not supported\n");
249 if (unix_handle == -1)
252 // printf("Anonymous\n");
255 static int fdzero = -1;
259 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
261 perror( "Cannot open /dev/zero for READ. Check permissions! error: " );
266 #endif /* MAP_ANON */
267 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
269 flags &= ~MAP_SHARED;
272 flags |= MAP_PRIVATE;
275 else fd = unix_handle;
276 // printf("fd %x, start %x, size %x, pos %x, prot %x\n",fd,start,size_low, offset_low, prot);
277 // if ((ret = mmap( start, size_low, prot,
278 // flags, fd, offset_low )) != (LPVOID)-1)
279 if ((ret = mmap( start, size_low, prot,
280 MAP_PRIVATE | MAP_FIXED, fd, offset_low )) != (LPVOID)-1)
282 // printf("address %08x\n", *(int*)ret);
283 // printf("%x\n", ret);
287 // printf("mmap %d\n", errno);
289 /* mmap() failed; if this is because the file offset is not */
290 /* page-aligned (EINVAL), or because the underlying filesystem */
291 /* does not support mmap() (ENOEXEC), we do it by hand. */
293 if (unix_handle == -1) return ret;
294 if ((errno != ENOEXEC) && (errno != EINVAL)) return ret;
295 if (prot & PROT_WRITE)
297 /* We cannot fake shared write mappings */
299 if (flags & MAP_SHARED) return ret;
302 if (!(flags & MAP_PRIVATE)) return ret;
305 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
306 /* Reserve the memory with an anonymous mmap */
307 ret = FILE_dommap( -1, start, size_high, size_low, 0, 0,
308 PROT_READ | PROT_WRITE, flags );
309 if (ret == (LPVOID)-1)
313 /* Now read in the file */
314 if ((pos = lseek( fd, offset_low, SEEK_SET )) == -1)
316 FILE_munmap( ret, size_high, size_low );
317 // printf("lseek\n");
320 read( fd, ret, size_low );
321 lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */
322 mprotect( ret, size_low, prot ); /* Set the right protection */
323 // printf("address %08x\n", *(int*)ret);
328 /***********************************************************************
331 int FILE_munmap( LPVOID start, DWORD size_high, DWORD size_low )
334 printf("offsets larger than 4Gb not supported\n");
335 return munmap( start, size_low );
337 static int mapping_size=0;
339 struct file_mapping_s;
340 typedef struct file_mapping_s
345 struct file_mapping_s* next;
346 struct file_mapping_s* prev;
348 static file_mapping* fm=0;
352 #define PAGE_NOACCESS 0x01
353 #define PAGE_READONLY 0x02
354 #define PAGE_READWRITE 0x04
355 #define PAGE_WRITECOPY 0x08
356 #define PAGE_EXECUTE 0x10
357 #define PAGE_EXECUTE_READ 0x20
358 #define PAGE_EXECUTE_READWRITE 0x40
359 #define PAGE_EXECUTE_WRITECOPY 0x80
360 #define PAGE_GUARD 0x100
361 #define PAGE_NOCACHE 0x200
363 HANDLE WINAPI CreateFileMappingA(HANDLE handle, LPSECURITY_ATTRIBUTES lpAttr,
365 DWORD dwMaxHigh, DWORD dwMaxLow,
368 int hFile = (int)handle;
376 hFile=open("/dev/zero", O_RDWR);
378 perror( "Cannot open /dev/zero for READ+WRITE. Check permissions! error: " );
384 len=lseek(hFile, 0, SEEK_END);
385 lseek(hFile, 0, SEEK_SET);
389 if(flProtect & PAGE_READONLY)
390 mmap_access |=PROT_READ;
392 mmap_access |=PROT_READ|PROT_WRITE;
394 answer=mmap(NULL, len, mmap_access, MAP_PRIVATE, hFile, 0);
397 if(answer!=(LPVOID)-1)
401 fm = (file_mapping*) malloc(sizeof(file_mapping));
406 fm->next = (file_mapping*) malloc(sizeof(file_mapping));
414 fm->name = (char*) malloc(strlen(name)+1);
415 strcpy(fm->name, name);
419 fm->mapping_size=len;
423 return (HANDLE)answer;
427 WIN_BOOL WINAPI UnmapViewOfFile(LPVOID handle)
433 for(p=fm; p; p=p->next)
435 if(p->handle==handle)
437 result=munmap((void*)handle, p->mapping_size);
438 if(p->next)p->next->prev=p->prev;
439 if(p->prev)p->prev->next=p->next;
450 //static int va_size=0;
452 typedef struct virt_alloc_s
456 struct virt_alloc_s* next;
457 struct virt_alloc_s* prev;
460 static virt_alloc* vm=0;
461 #define MEM_COMMIT 0x00001000
462 #define MEM_RESERVE 0x00002000
464 LPVOID WINAPI VirtualAlloc(LPVOID address, DWORD size, DWORD type, DWORD protection)
470 //printf("VirtualAlloc(0x%08X, %u, 0x%08X, 0x%08X)\n", (unsigned)address, size, type, protection);
472 if ((type&(MEM_RESERVE|MEM_COMMIT)) == 0) return NULL;
474 fd=open("/dev/zero", O_RDWR);
476 perror( "Cannot open /dev/zero for READ+WRITE. Check permissions! error: " );
480 if (type&MEM_RESERVE && (unsigned)address&0xffff) {
481 size += (unsigned)address&0xffff;
482 address = (unsigned)address&~0xffff;
484 pgsz = sysconf(_SC_PAGESIZE);
485 if (type&MEM_COMMIT && (unsigned)address%pgsz) {
486 size += (unsigned)address%pgsz;
487 address -= (unsigned)address%pgsz;
490 if (type&MEM_RESERVE && size<0x10000) size = 0x10000;
491 if (size%pgsz) size += pgsz - size%pgsz;
495 //check whether we can allow to allocate this
499 if((unsigned)address>=(unsigned)str->address+str->mapping_size)
504 if((unsigned)address+size<=(unsigned)str->address)
512 if( ((unsigned)address >= (unsigned)str->address)
513 && ((unsigned)address+size<=(unsigned)str->address+str->mapping_size)
514 && (type & MEM_COMMIT))
517 return address; //returning previously reserved memory
519 //printf(" VirtualAlloc(...) does not commit or not entirely within reserved, and\n");
521 /*printf(" VirtualAlloc(...) (0x%08X, %u) overlaps with (0x%08X, %u, state=%d)\n",
522 (unsigned)address, size, (unsigned)str->address, str->mapping_size, str->state);*/
528 answer=mmap(address, size, PROT_READ | PROT_WRITE | PROT_EXEC,
530 // answer=FILE_dommap(-1, address, 0, size, 0, 0,
531 // PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE);
533 if (answer != (void *)-1 && address && answer != address) {
534 /* It is dangerous to try mmap() with MAP_FIXED since it does not
535 always detect conflicts or non-allocation and chaos ensues after
536 a successful call but an overlapping or non-allocated region. */
537 munmap(answer, size);
538 answer = (void *) -1;
540 //printf(" VirtualAlloc(...) cannot satisfy requested address but address=NULL would work.\n");
542 if(answer==(void*)-1)
544 /*printf(" VirtualAlloc(...) mmap(0x%08X, %u, ...) failed with errno=%d (\"%s\")\n",
545 (unsigned)address, size, errno, strerror(errno));*/
550 virt_alloc *new_vm = (virt_alloc*) malloc(sizeof(virt_alloc));
551 new_vm->mapping_size=size;
552 new_vm->address=(char*)answer;
554 if(type == MEM_RESERVE)
563 // printf("Multiple VirtualAlloc!\n");
564 //printf(" VirtualAlloc(...) provides (0x%08X, %u)\n", (unsigned)answer, size);
569 WIN_BOOL WINAPI VirtualFree(LPVOID address, SIZE_T dwSize, DWORD dwFreeType)//not sure
574 //printf("VirtualFree(0x%08X, %d, 0x%08X)\n", (unsigned)address, dwSize, dwFreeType);
577 if(address!=str->address)
582 //printf(" VirtualFree(...) munmap(0x%08X, %d)\n", (unsigned)str->address, str->mapping_size);
583 answer=munmap(str->address, str->mapping_size);
584 if(str->next)str->next->prev=str->prev;
585 if(str->prev)str->prev->next=str->next;
586 if(vm==str)vm=str->prev;
593 INT WINAPI WideCharToMultiByte(UINT codepage, DWORD flags, LPCWSTR src,
594 INT srclen,LPSTR dest, INT destlen, LPCSTR defch, WIN_BOOL* used_defch)
599 if ((srclen==-1)&&(dest==0)) return 0;
600 if(srclen==-1){srclen=0; while(src[srclen++]);}
601 // for(i=0; i<srclen; i++)
602 // printf("%c", src[i]);
606 for(i=0; i<srclen; i++)
616 for(i=0; i<min(srclen, destlen); i++)
624 return min(srclen, destlen);
626 INT WINAPI MultiByteToWideChar(UINT codepage,DWORD flags, LPCSTR src, INT srclen,
627 LPWSTR dest, INT destlen)
631 HANDLE WINAPI OpenFileMappingA(DWORD access, WIN_BOOL prot, LPCSTR name)
638 for(p=fm; p; p=p->prev)
642 if(strcmp(p->name, name)==0)
643 return (HANDLE)p->handle;