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;
449 //static int va_size=0;
451 typedef struct virt_alloc_s
455 struct virt_alloc_s* next;
456 struct virt_alloc_s* prev;
459 static virt_alloc* vm=0;
460 #define MEM_COMMIT 0x00001000
461 #define MEM_RESERVE 0x00002000
463 LPVOID WINAPI VirtualAlloc(LPVOID address, DWORD size, DWORD type, DWORD protection)
469 //printf("VirtualAlloc(0x%08X, %u, 0x%08X, 0x%08X)\n", (unsigned)address, size, type, protection);
471 if ((type&(MEM_RESERVE|MEM_COMMIT)) == 0) return NULL;
473 fd=open("/dev/zero", O_RDWR);
475 perror( "Cannot open /dev/zero for READ+WRITE. Check permissions! error: " );
479 if (type&MEM_RESERVE && (unsigned)address&0xffff) {
480 size += (unsigned)address&0xffff;
481 address = (unsigned)address&~0xffff;
483 pgsz = sysconf(_SC_PAGESIZE);
484 if (type&MEM_COMMIT && (unsigned)address%pgsz) {
485 size += (unsigned)address%pgsz;
486 address -= (unsigned)address%pgsz;
489 if (type&MEM_RESERVE && size<0x10000) size = 0x10000;
490 if (size%pgsz) size += pgsz - size%pgsz;
494 //check whether we can allow to allocate this
498 if((unsigned)address>=(unsigned)str->address+str->mapping_size)
503 if((unsigned)address+size<=(unsigned)str->address)
511 if( ((unsigned)address >= (unsigned)str->address)
512 && ((unsigned)address+size<=(unsigned)str->address+str->mapping_size)
513 && (type & MEM_COMMIT))
516 return address; //returning previously reserved memory
518 //printf(" VirtualAlloc(...) does not commit or not entirely within reserved, and\n");
520 /*printf(" VirtualAlloc(...) (0x%08X, %u) overlaps with (0x%08X, %u, state=%d)\n",
521 (unsigned)address, size, (unsigned)str->address, str->mapping_size, str->state);*/
527 answer=mmap(address, size, PROT_READ | PROT_WRITE | PROT_EXEC,
529 // answer=FILE_dommap(-1, address, 0, size, 0, 0,
530 // PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE);
532 if (answer != (void *)-1 && address && answer != address) {
533 /* It is dangerous to try mmap() with MAP_FIXED since it does not
534 always detect conflicts or non-allocation and chaos ensues after
535 a successful call but an overlapping or non-allocated region. */
536 munmap(answer, size);
537 answer = (void *) -1;
539 //printf(" VirtualAlloc(...) cannot satisfy requested address but address=NULL would work.\n");
541 if(answer==(void*)-1)
543 /*printf(" VirtualAlloc(...) mmap(0x%08X, %u, ...) failed with errno=%d (\"%s\")\n",
544 (unsigned)address, size, errno, strerror(errno));*/
549 virt_alloc *new_vm = (virt_alloc*) malloc(sizeof(virt_alloc));
550 new_vm->mapping_size=size;
551 new_vm->address=(char*)answer;
553 if(type == MEM_RESERVE)
562 // printf("Multiple VirtualAlloc!\n");
563 //printf(" VirtualAlloc(...) provides (0x%08X, %u)\n", (unsigned)answer, size);
568 WIN_BOOL WINAPI VirtualFree(LPVOID address, SIZE_T dwSize, DWORD dwFreeType)//not sure
573 //printf("VirtualFree(0x%08X, %d, 0x%08X)\n", (unsigned)address, dwSize, dwFreeType);
576 if(address!=str->address)
581 //printf(" VirtualFree(...) munmap(0x%08X, %d)\n", (unsigned)str->address, str->mapping_size);
582 answer=munmap(str->address, str->mapping_size);
583 if(str->next)str->next->prev=str->prev;
584 if(str->prev)str->prev->next=str->next;
585 if(vm==str)vm=str->prev;
592 INT WINAPI WideCharToMultiByte(UINT codepage, DWORD flags, LPCWSTR src,
593 INT srclen,LPSTR dest, INT destlen, LPCSTR defch, WIN_BOOL* used_defch)
598 if ((srclen==-1)&&(dest==0)) return 0;
599 if(srclen==-1){srclen=0; while(src[srclen++]);}
600 // for(i=0; i<srclen; i++)
601 // printf("%c", src[i]);
605 for(i=0; i<srclen; i++)
615 for(i=0; i<min(srclen, destlen); i++)
623 return min(srclen, destlen);
625 INT WINAPI MultiByteToWideChar(UINT codepage,DWORD flags, LPCSTR src, INT srclen,
626 LPWSTR dest, INT destlen)
630 HANDLE WINAPI OpenFileMappingA(DWORD access, WIN_BOOL prot, LPCSTR name)
637 for(p=fm; p; p=p->prev)
641 if(strcmp(p->name, name)==0)
642 return (HANDLE)p->handle;