1
|
1 /********************************************************
|
|
2 *
|
|
3 *
|
|
4 * Stub functions for Wine module
|
|
5 *
|
|
6 *
|
|
7 ********************************************************/
|
|
8 #include <config.h>
|
|
9 #ifdef HAVE_MALLOC_H
|
|
10 #include <malloc.h>
|
|
11 #else
|
|
12 #include <stdlib.h>
|
|
13 #endif
|
|
14 #include <stdio.h>
|
|
15 #include <unistd.h>
|
|
16 #include <sys/mman.h>
|
|
17 #include <errno.h>
|
|
18 #include <fcntl.h>
|
|
19 #include <string.h>
|
|
20 #include <stdarg.h>
|
|
21 #include <wine/windef.h>
|
|
22 //#include <wine/winbase.h>
|
|
23 int dbg_header_err( const char *dbg_channel, const char *func )
|
|
24 {
|
|
25 return 0;
|
|
26 }
|
|
27 int dbg_header_warn( const char *dbg_channel, const char *func )
|
|
28 {
|
|
29 return 0;
|
|
30 }
|
|
31 int dbg_header_fixme( const char *dbg_channel, const char *func )
|
|
32 {
|
|
33 return 0;
|
|
34 }
|
|
35 int dbg_header_trace( const char *dbg_channel, const char *func )
|
|
36 {
|
|
37 return 0;
|
|
38 }
|
|
39 int dbg_vprintf( const char *format, ... )
|
|
40 {
|
|
41 return 0;
|
|
42 }
|
|
43 int __vprintf( const char *format, ... )
|
|
44 {
|
|
45 #ifdef DETAILED_OUT
|
|
46 va_list va;
|
|
47 va_start(va, format);
|
|
48 vprintf(format, va);
|
|
49 va_end(va);
|
|
50 fflush(stdout);
|
|
51 #endif
|
|
52 return 0;
|
|
53 }
|
|
54
|
|
55 int GetProcessHeap()
|
|
56 {
|
|
57 return 1;
|
|
58 }
|
|
59
|
|
60 void* HeapAlloc(int heap, int flags, int size)
|
|
61 {
|
|
62 if(flags & 0x8)
|
|
63 return calloc(size, 1);
|
|
64 else
|
|
65 return malloc(size);
|
|
66 }
|
|
67
|
|
68 int HeapFree(int heap, int flags, void* mem)
|
|
69 {
|
|
70 free(mem);
|
|
71 return 1;
|
|
72 }
|
|
73
|
|
74 static int last_error;
|
|
75
|
|
76 int GetLastError()
|
|
77 {
|
|
78 return last_error;
|
|
79 }
|
|
80
|
|
81 int SetLastError(int error)
|
|
82 {
|
|
83 return last_error=error;
|
|
84 }
|
|
85
|
|
86 int ReadFile(int handle, void* mem, unsigned long size, long* result, long flags)
|
|
87 {
|
|
88 *result=read(handle, mem, size);
|
|
89 return *result;
|
|
90 }
|
|
91 int lstrcmpiA(const char* c1, const char* c2)
|
|
92 {
|
|
93 return strcasecmp(c1,c2);
|
|
94 }
|
|
95 int lstrcpynA(char* dest, const char* src, int num)
|
|
96 {
|
|
97 return strncmp(dest,src,num);
|
|
98 }
|
|
99 int lstrlenA(const char* s)
|
|
100 {
|
|
101 return strlen(s);
|
|
102 }
|
|
103 int lstrlenW(const short* s)
|
|
104 {
|
|
105 int l;
|
|
106 if(!s)
|
|
107 return 0;
|
|
108 l=0;
|
|
109 while(s[l])
|
|
110 l++;
|
|
111 return l;
|
|
112 }
|
|
113 int lstrcpynWtoA(char* dest, const char* src, int count)
|
|
114 {
|
|
115 int moved=0;
|
|
116 if((dest==0) || (src==0))
|
|
117 return 0;
|
|
118 while(moved<count)
|
|
119 {
|
|
120 *dest=*src;
|
|
121 moved++;
|
|
122 if(*src==0)
|
|
123 return moved;
|
|
124 src++;
|
|
125 dest++;
|
|
126 }
|
|
127 }
|
|
128 int wcsnicmp(const unsigned short* s1, const unsigned short* s2, int n)
|
|
129 {
|
|
130 if(s1==0)
|
|
131 return;
|
|
132 if(s2==0)
|
|
133 return;
|
|
134 while(n>0)
|
|
135 {
|
|
136 if(*s1<*s2)
|
|
137 return -1;
|
|
138 else
|
|
139 if(*s1>*s2)
|
|
140 return 1;
|
|
141 else
|
|
142 if(*s1==0)
|
|
143 return 0;
|
|
144 s1++;
|
|
145 s2++;
|
|
146 n--;
|
|
147 }
|
|
148 return 0;
|
|
149 }
|
|
150
|
|
151
|
|
152 int IsBadReadPtr(void* data, int size)
|
|
153 {
|
|
154 if(size==0)
|
|
155 return 0;
|
|
156 if(data==NULL)
|
|
157 return 1;
|
|
158 return 0;
|
|
159 }
|
|
160 char* HEAP_strdupA(const char* string)
|
|
161 {
|
|
162 // return strdup(string);
|
|
163 char* answ=malloc(strlen(string)+1);
|
|
164 strcpy(answ, string);
|
|
165 return answ;
|
|
166 }
|
|
167 short* HEAP_strdupAtoW(void* heap, void* hz, const char* string)
|
|
168 {
|
|
169 int size, i;
|
|
170 short* answer;
|
|
171 if(string==0)
|
|
172 return 0;
|
|
173 size=strlen(string);
|
|
174 answer=malloc(size+size+2);
|
|
175 for(i=0; i<=size; i++)
|
|
176 answer[i]=(short)string[i];
|
|
177 return answer;
|
|
178 }
|
|
179 char* HEAP_strdupWtoA(void* heap, void* hz, const short* string)
|
|
180 {
|
|
181 int size, i;
|
|
182 char* answer;
|
|
183 if(string==0)
|
|
184 return 0;
|
|
185 size=0;
|
|
186 while(string[size])
|
|
187 size++;
|
|
188 answer=malloc(size+2);
|
|
189 for(i=0; i<=size; i++)
|
|
190 answer[i]=(char)string[i];
|
|
191 return answer;
|
|
192 }
|
|
193
|
|
194 /***********************************************************************
|
|
195 * FILE_dommap
|
|
196 */
|
|
197
|
|
198 //#define MAP_PRIVATE
|
|
199 //#define MAP_SHARED
|
|
200 #undef MAP_ANON
|
|
201 LPVOID FILE_dommap( int unix_handle, LPVOID start,
|
|
202 DWORD size_high, DWORD size_low,
|
|
203 DWORD offset_high, DWORD offset_low,
|
|
204 int prot, int flags )
|
|
205 {
|
|
206 int fd = -1;
|
|
207 int pos;
|
|
208 LPVOID ret;
|
|
209
|
|
210 if (size_high || offset_high)
|
|
211 printf("offsets larger than 4Gb not supported\n");
|
|
212
|
|
213 if (unix_handle == -1)
|
|
214 {
|
|
215 #ifdef MAP_ANON
|
|
216 // printf("Anonymous\n");
|
|
217 flags |= MAP_ANON;
|
|
218 #else
|
|
219 static int fdzero = -1;
|
|
220
|
|
221 if (fdzero == -1)
|
|
222 {
|
|
223 if ((fdzero = open( "/dev/zero", O_RDONLY )) == -1)
|
|
224 {
|
|
225 perror( "/dev/zero: open" );
|
|
226 exit(1);
|
|
227 }
|
|
228 }
|
|
229 fd = fdzero;
|
|
230 #endif /* MAP_ANON */
|
|
231 /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
|
|
232 #ifdef MAP_SHARED
|
|
233 flags &= ~MAP_SHARED;
|
|
234 #endif
|
|
235 #ifdef MAP_PRIVATE
|
|
236 flags |= MAP_PRIVATE;
|
|
237 #endif
|
|
238 }
|
|
239 else fd = unix_handle;
|
|
240 // printf("fd %x, start %x, size %x, pos %x, prot %x\n",fd,start,size_low, offset_low, prot);
|
|
241 // if ((ret = mmap( start, size_low, prot,
|
|
242 // flags, fd, offset_low )) != (LPVOID)-1)
|
|
243 if ((ret = mmap( start, size_low, prot,
|
|
244 MAP_PRIVATE | MAP_FIXED, fd, offset_low )) != (LPVOID)-1)
|
|
245 {
|
|
246 // printf("address %08x\n", *(int*)ret);
|
|
247 // printf("%x\n", ret);
|
|
248 return ret;
|
|
249 }
|
|
250
|
|
251 // printf("mmap %d\n", errno);
|
|
252
|
|
253 /* mmap() failed; if this is because the file offset is not */
|
|
254 /* page-aligned (EINVAL), or because the underlying filesystem */
|
|
255 /* does not support mmap() (ENOEXEC), we do it by hand. */
|
|
256
|
|
257 if (unix_handle == -1) return ret;
|
|
258 if ((errno != ENOEXEC) && (errno != EINVAL)) return ret;
|
|
259 if (prot & PROT_WRITE)
|
|
260 {
|
|
261 /* We cannot fake shared write mappings */
|
|
262 #ifdef MAP_SHARED
|
|
263 if (flags & MAP_SHARED) return ret;
|
|
264 #endif
|
|
265 #ifdef MAP_PRIVATE
|
|
266 if (!(flags & MAP_PRIVATE)) return ret;
|
|
267 #endif
|
|
268 }
|
|
269 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
|
|
270 /* Reserve the memory with an anonymous mmap */
|
|
271 ret = FILE_dommap( -1, start, size_high, size_low, 0, 0,
|
|
272 PROT_READ | PROT_WRITE, flags );
|
|
273 if (ret == (LPVOID)-1)
|
|
274 // {
|
|
275 // perror(
|
|
276 return ret;
|
|
277 /* Now read in the file */
|
|
278 if ((pos = lseek( fd, offset_low, SEEK_SET )) == -1)
|
|
279 {
|
|
280 FILE_munmap( ret, size_high, size_low );
|
|
281 // printf("lseek\n");
|
|
282 return (LPVOID)-1;
|
|
283 }
|
|
284 read( fd, ret, size_low );
|
|
285 lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */
|
|
286 mprotect( ret, size_low, prot ); /* Set the right protection */
|
|
287 // printf("address %08x\n", *(int*)ret);
|
|
288 return ret;
|
|
289 }
|
|
290
|
|
291
|
|
292 /***********************************************************************
|
|
293 * FILE_munmap
|
|
294 */
|
|
295 int FILE_munmap( LPVOID start, DWORD size_high, DWORD size_low )
|
|
296 {
|
|
297 if (size_high)
|
|
298 printf("offsets larger than 4Gb not supported\n");
|
|
299 return munmap( start, size_low );
|
|
300 }
|
|
301 static int mapping_size=0;
|
|
302
|
|
303 struct file_mapping_s;
|
|
304 typedef struct file_mapping_s
|
|
305 {
|
|
306 int mapping_size;
|
|
307 char* name;
|
|
308 HANDLE handle;
|
|
309 struct file_mapping_s* next;
|
|
310 struct file_mapping_s* prev;
|
|
311 }file_mapping;
|
|
312 static file_mapping* fm=0;
|
|
313
|
|
314
|
|
315
|
|
316 #define PAGE_NOACCESS 0x01
|
|
317 #define PAGE_READONLY 0x02
|
|
318 #define PAGE_READWRITE 0x04
|
|
319 #define PAGE_WRITECOPY 0x08
|
|
320 #define PAGE_EXECUTE 0x10
|
|
321 #define PAGE_EXECUTE_READ 0x20
|
|
322 #define PAGE_EXECUTE_READWRITE 0x40
|
|
323 #define PAGE_EXECUTE_WRITECOPY 0x80
|
|
324 #define PAGE_GUARD 0x100
|
|
325 #define PAGE_NOCACHE 0x200
|
|
326
|
|
327 HANDLE CreateFileMappingA(int hFile, void* lpAttr,
|
|
328 DWORD flProtect, DWORD dwMaxHigh, DWORD dwMaxLow, const char* name)
|
|
329 {
|
|
330 unsigned int len;
|
|
331 HANDLE answer;
|
|
332 int anon=0;
|
|
333 int mmap_access=0;
|
|
334 if(hFile<0)
|
|
335 {
|
|
336 anon=1;
|
|
337 hFile=open("/dev/zero", O_RDWR);
|
|
338 if(hFile<0)
|
|
339 return 0;
|
|
340 }
|
|
341 if(!anon)
|
|
342 {
|
|
343 len=lseek(hFile, 0, SEEK_END);
|
|
344 lseek(hFile, 0, SEEK_SET);
|
|
345 }
|
|
346 else len=dwMaxLow;
|
|
347
|
|
348 if(flProtect & PAGE_READONLY)
|
|
349 mmap_access |=PROT_READ;
|
|
350 else
|
|
351 mmap_access |=PROT_READ|PROT_WRITE;
|
|
352
|
|
353 answer=(HANDLE)mmap(NULL, len, mmap_access, MAP_PRIVATE, hFile, 0);
|
|
354 if(anon)
|
|
355 close(hFile);
|
|
356 if(answer!=(HANDLE)-1)
|
|
357 {
|
|
358 if(fm==0)
|
|
359 {
|
|
360 fm=malloc(sizeof(file_mapping));
|
|
361 fm->prev=NULL;
|
|
362 }
|
|
363 else
|
|
364 {
|
|
365 fm->next=malloc(sizeof(file_mapping));
|
|
366 fm->next->prev=fm;
|
|
367 fm=fm->next;
|
|
368 }
|
|
369 fm->next=NULL;
|
|
370 fm->handle=answer;
|
|
371 if(name)
|
|
372 {
|
|
373 fm->name=malloc(strlen(name)+1);
|
|
374 strcpy(fm->name, name);
|
|
375 }
|
|
376 else
|
|
377 fm->name=NULL;
|
|
378 fm->mapping_size=len;
|
|
379
|
|
380 if(anon)
|
|
381 close(hFile);
|
|
382 return answer;
|
|
383 }
|
|
384 return (HANDLE)0;
|
|
385 }
|
|
386 int UnmapViewOfFile(HANDLE handle)
|
|
387 {
|
|
388 file_mapping* p;
|
|
389 int result;
|
|
390 if(fm==0)
|
|
391 return (HANDLE)0;
|
|
392 for(p=fm; p; p=p->next)
|
|
393 {
|
|
394 if(p->handle==handle)
|
|
395 {
|
|
396 result=munmap((void*)handle, p->mapping_size);
|
|
397 if(p->next)p->next->prev=p->prev;
|
|
398 if(p->prev)p->prev->next=p->next;
|
|
399 if(p->name)
|
|
400 free(p->name);
|
|
401 if(p==fm)
|
|
402 fm=p->prev;
|
|
403 free(p);
|
|
404 return result;
|
|
405 }
|
|
406 }
|
|
407 return 0;
|
|
408 }
|
|
409 //static int va_size=0;
|
|
410 struct virt_alloc_s;
|
|
411 typedef struct virt_alloc_s
|
|
412 {
|
|
413 int mapping_size;
|
|
414 char* address;
|
|
415 struct virt_alloc_s* next;
|
|
416 struct virt_alloc_s* prev;
|
|
417 int state;
|
|
418 }virt_alloc;
|
|
419 static virt_alloc* vm=0;
|
|
420 #define MEM_COMMIT 0x00001000
|
|
421 #define MEM_RESERVE 0x00002000
|
|
422
|
|
423 void* VirtualAlloc(void* address, DWORD size, DWORD type, DWORD protection)
|
|
424 {
|
|
425 void* answer;
|
|
426 int fd=open("/dev/zero", O_RDWR);
|
|
427 size=(size+0xffff)&(~0xffff);
|
|
428 // printf("VirtualAlloc(0x%08X, %d)\n", address
|
|
429 if(address!=0)
|
|
430 {
|
|
431 //check whether we can allow to allocate this
|
|
432 virt_alloc* str=vm;
|
|
433 while(str)
|
|
434 {
|
|
435 if((unsigned)address>=(unsigned)str->address+str->mapping_size)
|
|
436 {
|
|
437 str=str->prev;
|
|
438 continue;
|
|
439 }
|
|
440 if((unsigned)address+size<(unsigned)str->address)
|
|
441 {
|
|
442 str=str->prev;
|
|
443 continue;
|
|
444 }
|
|
445 if(str->state==0)
|
|
446 {
|
|
447 #warning FIXME
|
|
448 if(((unsigned)address+size<(unsigned)str->address+str->mapping_size) && (type & MEM_COMMIT))
|
|
449 {
|
|
450 close(fd);
|
|
451 return address; //returning previously reserved memory
|
|
452 }
|
|
453 return NULL;
|
|
454 }
|
|
455 close(fd);
|
|
456 return NULL;
|
|
457 }
|
|
458 answer=mmap(address, size, PROT_READ | PROT_WRITE | PROT_EXEC,
|
|
459 MAP_FIXED | MAP_PRIVATE, fd, 0);
|
|
460 }
|
|
461 else
|
|
462 answer=mmap(address, size, PROT_READ | PROT_WRITE | PROT_EXEC,
|
|
463 MAP_PRIVATE, fd, 0);
|
|
464 // answer=FILE_dommap(-1, address, 0, size, 0, 0,
|
|
465 // PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE);
|
|
466 close(fd);
|
|
467 if(answer==(void*)-1)
|
|
468 {
|
|
469 printf("Error no %d\n", errno);
|
|
470 printf("VirtualAlloc(0x%08X, %d) failed\n", address, size);
|
|
471 return NULL;
|
|
472 }
|
|
473 else
|
|
474 {
|
|
475 virt_alloc *new_vm=malloc(sizeof(virt_alloc));
|
|
476 new_vm->mapping_size=size;
|
|
477 new_vm->address=answer;
|
|
478 new_vm->prev=vm;
|
|
479 if(type == MEM_RESERVE)
|
|
480 new_vm->state=0;
|
|
481 else
|
|
482 new_vm->state=1;
|
|
483 if(vm)
|
|
484 vm->next=new_vm;
|
|
485 vm=new_vm;
|
|
486 vm->next=0;
|
|
487 // if(va_size!=0)
|
|
488 // printf("Multiple VirtualAlloc!\n");
|
|
489 // printf("answer=0x%08x\n", answer);
|
|
490 return answer;
|
|
491 }
|
|
492 }
|
|
493 int VirtualFree(void* address, int t1, int t2)//not sure
|
|
494 {
|
|
495 virt_alloc* str=vm;
|
|
496 int answer;
|
|
497 while(str)
|
|
498 {
|
|
499 if(address!=str->address)
|
|
500 {
|
|
501 str=str->prev;
|
|
502 continue;
|
|
503 }
|
|
504 answer=munmap(str->address, str->mapping_size);
|
|
505 if(str->next)str->next->prev=str->prev;
|
|
506 if(str->prev)str->prev->next=str->next;
|
|
507 if(vm==str)vm=0;
|
|
508 free(str);
|
|
509 return 0;
|
|
510 }
|
|
511 return -1;
|
|
512 }
|
|
513
|
|
514 int WideCharToMultiByte(unsigned int codepage, long flags, const short* src,
|
|
515 int srclen,char* dest, int destlen, const char* defch, int* used_defch)
|
|
516 {
|
|
517 int i;
|
|
518 if(src==0)
|
|
519 return 0;
|
|
520 for(i=0; i<srclen; i++)
|
|
521 printf("%c", src[i]);
|
|
522 printf("\n");
|
|
523 if(dest==0)
|
|
524 {
|
|
525 for(i=0; i<srclen; i++)
|
|
526 {
|
|
527 src++;
|
|
528 if(*src==0)
|
|
529 return i+1;
|
|
530 }
|
|
531 return srclen+1;
|
|
532 }
|
|
533 if(used_defch)
|
|
534 *used_defch=0;
|
|
535 for(i=0; i<min(srclen, destlen); i++)
|
|
536 {
|
|
537 *dest=(char)*src;
|
|
538 dest++;
|
|
539 src++;
|
|
540 if(*src==0)
|
|
541 return i+1;
|
|
542 }
|
|
543 return min(srclen, destlen);
|
|
544 }
|
|
545 int MultiByteToWideChar(unsigned int codepage,long flags, const char* src, int srclen,
|
|
546 short* dest, int destlen)
|
|
547 {
|
|
548 return 0;
|
|
549 }
|
|
550 HANDLE OpenFileMappingA(long access, long prot, char* name)
|
|
551 {
|
|
552 file_mapping* p;
|
|
553 if(fm==0)
|
|
554 return (HANDLE)0;
|
|
555 if(name==0)
|
|
556 return (HANDLE)0;
|
|
557 for(p=fm; p; p=p->prev)
|
|
558 {
|
|
559 if(p->name==0)
|
|
560 continue;
|
|
561 if(strcmp(p->name, name)==0)
|
|
562 return p->handle;
|
|
563 }
|
|
564 return 0;
|
|
565 }
|