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