Support SMPTE-240M colourspace in vo_vdpau.
[mplayer/glamo.git] / loader / ext.c
blob23c0292c6c3e73a9c4465df4a0e90422fb23ae6a
1 /********************************************************
4 * Stub functions for Wine module
7 ********************************************************/
9 /*
10 * Modified for use with MPlayer, detailed changelog at
11 * http://svn.mplayerhq.hu/mplayer/trunk/
14 #include "config.h"
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #ifdef HAVE_SYS_MMAN_H
19 #include <sys/mman.h>
20 #else
21 #include "osdep/mmap.h"
22 #endif
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <ctype.h>
29 #include "osdep/mmap_anon.h"
30 #include "wine/windef.h"
31 #include "wine/winbase.h"
32 #include "wine/debugtools.h"
33 #include "wine/heap.h"
34 #include "ext.h"
36 #if 0
37 //REMOVE SIMPLIFY
38 static void* mymalloc(unsigned int size)
40 printf("malloc %d\n", size);
41 return malloc(size);
44 #undef malloc
45 #define malloc mymalloc
46 #endif
48 int dbg_header_err( const char *dbg_channel, const char *func )
50 return 0;
52 int dbg_header_warn( const char *dbg_channel, const char *func )
54 return 0;
56 int dbg_header_fixme( const char *dbg_channel, const char *func )
58 return 0;
60 int dbg_header_trace( const char *dbg_channel, const char *func )
62 return 0;
64 int dbg_vprintf( const char *format, va_list args )
66 return 0;
68 int __vprintf( const char *format, ... )
70 #ifdef DETAILED_OUT
71 va_list va;
72 va_start(va, format);
73 vprintf(format, va);
74 va_end(va);
75 #endif
76 return 0;
79 HANDLE WINAPI GetProcessHeap(void)
81 return 1;
84 LPVOID WINAPI HeapAlloc(HANDLE heap, DWORD flags, DWORD size)
86 //static int i = 5;
87 void* m = (flags & 0x8) ? calloc(size, 1) : malloc(size);
88 //printf("HeapAlloc %p %d (%d)\n", m, size, flags);
89 //if (--i == 0)
90 // abort();
91 return m;
94 WIN_BOOL WINAPI HeapFree(HANDLE heap, DWORD flags, LPVOID mem)
96 if (mem) free(mem);
97 //printf("HeapFree %p\n", mem);
98 //if (!mem)
99 // abort();
100 return 1;
103 static int last_error;
105 DWORD WINAPI GetLastError(void)
107 return last_error;
110 VOID WINAPI SetLastError(DWORD error)
112 last_error=error;
115 WIN_BOOL WINAPI ReadFile(HANDLE handle, LPVOID mem, DWORD size, LPDWORD result, LPOVERLAPPED flags)
117 *result=read(handle, mem, size);
118 return *result;
120 INT WINAPI lstrcmpiA(LPCSTR c1, LPCSTR c2)
122 return strcasecmp(c1,c2);
124 LPSTR WINAPI lstrcpynA(LPSTR dest, LPCSTR src, INT num)
126 return strncpy(dest,src,num);
128 INT WINAPI lstrlenA(LPCSTR s)
130 return strlen(s);
132 INT WINAPI lstrlenW(LPCWSTR s)
134 int l;
135 if(!s)
136 return 0;
137 l=0;
138 while(s[l])
139 l++;
140 return l;
142 LPSTR WINAPI lstrcpynWtoA(LPSTR dest, LPCWSTR src, INT count)
144 LPSTR result = dest;
145 int moved=0;
146 if((dest==0) || (src==0))
147 return 0;
148 while(moved<count)
150 *dest=*src;
151 moved++;
152 if(*src==0)
153 break;
154 src++;
155 dest++;
157 return result;
159 /* i stands here for ignore case! */
160 int wcsnicmp(const unsigned short* s1, const unsigned short* s2, int n)
163 if(s1==0)
164 return;
165 if(s2==0)
166 return;
168 while(n>0)
170 if (((*s1 | *s2) & 0xff00) || toupper((char)*s1) != toupper((char)*s2))
173 if(*s1<*s2)
174 return -1;
175 else
176 if(*s1>*s2)
177 return 1;
178 else
179 if(*s1==0)
180 return 0;
182 s1++;
183 s2++;
184 n--;
186 return 0;
189 WIN_BOOL WINAPI IsBadReadPtr(LPCVOID data, UINT size)
191 if(size==0)
192 return 0;
193 if(data==NULL)
194 return 1;
195 return 0;
197 LPSTR HEAP_strdupA(HANDLE heap, DWORD flags, LPCSTR string)
199 // return strdup(string);
200 char* answ = (char*) malloc(strlen(string) + 1);
201 strcpy(answ, string);
202 return answ;
204 LPWSTR HEAP_strdupAtoW(HANDLE heap, DWORD flags, LPCSTR string)
206 int size, i;
207 WCHAR* answer;
208 if(string==0)
209 return 0;
210 size=strlen(string);
211 answer = (WCHAR*) malloc(sizeof(WCHAR) * (size + 1));
212 for(i=0; i<=size; i++)
213 answer[i]=(short)string[i];
214 return answer;
216 LPSTR HEAP_strdupWtoA(HANDLE heap, DWORD flags, LPCWSTR string)
218 int size, i;
219 char* answer;
220 if(string==0)
221 return 0;
222 size=0;
223 while(string[size])
224 size++;
225 answer = (char*) malloc(size + 2);
226 for(i=0; i<=size; i++)
227 answer[i]=(char)string[i];
228 return answer;
231 /***********************************************************************
232 * FILE_dommap
235 //#define MAP_PRIVATE
236 //#define MAP_SHARED
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 )
242 int fd = -1;
243 int pos;
244 LPVOID ret;
246 if (size_high || offset_high)
247 printf("offsets larger than 4Gb not supported\n");
249 if (unix_handle == -1)
251 ret = mmap_anon( start, size_low, prot, flags, offset_low );
253 else
255 fd = unix_handle;
256 ret = mmap( start, size_low, prot, flags, fd, offset_low );
259 if (ret != (LPVOID)-1)
261 // printf("address %08x\n", *(int*)ret);
262 // printf("%x\n", ret);
263 return ret;
266 // printf("mmap %d\n", errno);
268 /* mmap() failed; if this is because the file offset is not */
269 /* page-aligned (EINVAL), or because the underlying filesystem */
270 /* does not support mmap() (ENOEXEC), we do it by hand. */
272 if (unix_handle == -1) return ret;
273 if ((errno != ENOEXEC) && (errno != EINVAL)) return ret;
274 if (prot & PROT_WRITE)
276 /* We cannot fake shared write mappings */
277 #ifdef MAP_SHARED
278 if (flags & MAP_SHARED) return ret;
279 #endif
280 #ifdef MAP_PRIVATE
281 if (!(flags & MAP_PRIVATE)) return ret;
282 #endif
284 /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/
285 /* Reserve the memory with an anonymous mmap */
286 ret = FILE_dommap( -1, start, size_high, size_low, 0, 0,
287 PROT_READ | PROT_WRITE, flags );
288 if (ret == (LPVOID)-1)
289 // {
290 // perror(
291 return ret;
292 /* Now read in the file */
293 if ((pos = lseek( fd, offset_low, SEEK_SET )) == -1)
295 FILE_munmap( ret, size_high, size_low );
296 // printf("lseek\n");
297 return (LPVOID)-1;
299 read( fd, ret, size_low );
300 lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */
301 mprotect( ret, size_low, prot ); /* Set the right protection */
302 // printf("address %08x\n", *(int*)ret);
303 return ret;
307 /***********************************************************************
308 * FILE_munmap
310 int FILE_munmap( LPVOID start, DWORD size_high, DWORD size_low )
312 if (size_high)
313 printf("offsets larger than 4Gb not supported\n");
314 return munmap( start, size_low );
317 struct file_mapping_s;
318 typedef struct file_mapping_s
320 int mapping_size;
321 char* name;
322 LPVOID handle;
323 struct file_mapping_s* next;
324 struct file_mapping_s* prev;
325 }file_mapping;
326 static file_mapping* fm=0;
330 #define PAGE_NOACCESS 0x01
331 #define PAGE_READONLY 0x02
332 #define PAGE_READWRITE 0x04
333 #define PAGE_WRITECOPY 0x08
334 #define PAGE_EXECUTE 0x10
335 #define PAGE_EXECUTE_READ 0x20
336 #define PAGE_EXECUTE_READWRITE 0x40
337 #define PAGE_EXECUTE_WRITECOPY 0x80
338 #define PAGE_GUARD 0x100
339 #define PAGE_NOCACHE 0x200
341 HANDLE WINAPI CreateFileMappingA(HANDLE handle, LPSECURITY_ATTRIBUTES lpAttr,
342 DWORD flProtect,
343 DWORD dwMaxHigh, DWORD dwMaxLow,
344 LPCSTR name)
346 int hFile = (int)handle;
347 unsigned int len;
348 LPVOID answer;
349 int anon=0;
350 int mmap_access=0;
351 if(hFile<0)
352 anon=1;
354 if(!anon)
356 len=lseek(hFile, 0, SEEK_END);
357 lseek(hFile, 0, SEEK_SET);
359 else len=dwMaxLow;
361 if(flProtect & PAGE_READONLY)
362 mmap_access |=PROT_READ;
363 else
364 mmap_access |=PROT_READ|PROT_WRITE;
366 if(anon)
367 answer=mmap_anon(NULL, len, mmap_access, MAP_PRIVATE, 0);
368 else
369 answer=mmap(NULL, len, mmap_access, MAP_PRIVATE, hFile, 0);
371 if(answer!=(LPVOID)-1)
373 if(fm==0)
375 fm = (file_mapping*) malloc(sizeof(file_mapping));
376 fm->prev=NULL;
378 else
380 fm->next = (file_mapping*) malloc(sizeof(file_mapping));
381 fm->next->prev=fm;
382 fm=fm->next;
384 fm->next=NULL;
385 fm->handle=answer;
386 if(name)
388 fm->name = (char*) malloc(strlen(name)+1);
389 strcpy(fm->name, name);
391 else
392 fm->name=NULL;
393 fm->mapping_size=len;
395 return (HANDLE)answer;
397 return (HANDLE)0;
399 WIN_BOOL WINAPI UnmapViewOfFile(LPVOID handle)
401 file_mapping* p;
402 int result;
403 if(fm==0)
404 return 0;
405 for(p=fm; p; p=p->next)
407 if(p->handle==handle)
409 result=munmap((void*)handle, p->mapping_size);
410 if(p->next)p->next->prev=p->prev;
411 if(p->prev)p->prev->next=p->next;
412 if(p->name)
413 free(p->name);
414 if(p==fm)
415 fm=p->prev;
416 free(p);
417 return result;
420 return 0;
422 //static int va_size=0;
423 struct virt_alloc_s;
424 typedef struct virt_alloc_s
426 int mapping_size;
427 char* address;
428 struct virt_alloc_s* next;
429 struct virt_alloc_s* prev;
430 int state;
431 }virt_alloc;
432 static virt_alloc* vm=0;
433 #define MEM_COMMIT 0x00001000
434 #define MEM_RESERVE 0x00002000
436 LPVOID WINAPI VirtualAlloc(LPVOID address, DWORD size, DWORD type, DWORD protection)
438 void* answer;
439 long pgsz;
441 //printf("VirtualAlloc(0x%08X, %u, 0x%08X, 0x%08X)\n", (unsigned)address, size, type, protection);
443 if ((type&(MEM_RESERVE|MEM_COMMIT)) == 0) return NULL;
445 if (type&MEM_RESERVE && (unsigned)address&0xffff) {
446 size += (unsigned)address&0xffff;
447 address = (unsigned)address&~0xffff;
449 pgsz = sysconf(_SC_PAGESIZE);
450 if (type&MEM_COMMIT && (unsigned)address%pgsz) {
451 size += (unsigned)address%pgsz;
452 address -= (unsigned)address%pgsz;
455 if (type&MEM_RESERVE && size<0x10000) size = 0x10000;
456 if (size%pgsz) size += pgsz - size%pgsz;
458 if(address!=0)
460 //check whether we can allow to allocate this
461 virt_alloc* str=vm;
462 while(str)
464 if((unsigned)address>=(unsigned)str->address+str->mapping_size)
466 str=str->prev;
467 continue;
469 if((unsigned)address+size<=(unsigned)str->address)
471 str=str->prev;
472 continue;
474 if(str->state==0)
476 #warning FIXME
477 if( ((unsigned)address >= (unsigned)str->address)
478 && ((unsigned)address+size<=(unsigned)str->address+str->mapping_size)
479 && (type & MEM_COMMIT))
481 return address; //returning previously reserved memory
483 //printf(" VirtualAlloc(...) does not commit or not entirely within reserved, and\n");
485 /*printf(" VirtualAlloc(...) (0x%08X, %u) overlaps with (0x%08X, %u, state=%d)\n",
486 (unsigned)address, size, (unsigned)str->address, str->mapping_size, str->state);*/
487 return NULL;
491 answer=mmap_anon(address, size, PROT_READ | PROT_WRITE | PROT_EXEC,
492 MAP_PRIVATE, 0);
493 // answer=FILE_dommap(-1, address, 0, size, 0, 0,
494 // PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE);
496 if (answer != (void *)-1 && address && answer != address) {
497 /* It is dangerous to try mmap() with MAP_FIXED since it does not
498 always detect conflicts or non-allocation and chaos ensues after
499 a successful call but an overlapping or non-allocated region. */
500 munmap(answer, size);
501 answer = (void *) -1;
502 errno = EINVAL;
503 //printf(" VirtualAlloc(...) cannot satisfy requested address but address=NULL would work.\n");
505 if(answer==(void*)-1)
507 /*printf(" VirtualAlloc(...) mmap(0x%08X, %u, ...) failed with errno=%d (\"%s\")\n",
508 (unsigned)address, size, errno, strerror(errno));*/
509 return NULL;
511 else
513 virt_alloc *new_vm = (virt_alloc*) malloc(sizeof(virt_alloc));
514 new_vm->mapping_size=size;
515 new_vm->address=(char*)answer;
516 new_vm->prev=vm;
517 if(type == MEM_RESERVE)
518 new_vm->state=0;
519 else
520 new_vm->state=1;
521 if(vm)
522 vm->next=new_vm;
523 vm=new_vm;
524 vm->next=0;
525 //if(va_size!=0)
526 // printf("Multiple VirtualAlloc!\n");
527 //printf(" VirtualAlloc(...) provides (0x%08X, %u)\n", (unsigned)answer, size);
528 return answer;
532 WIN_BOOL WINAPI VirtualFree(LPVOID address, SIZE_T dwSize, DWORD dwFreeType)//not sure
534 virt_alloc* str=vm;
535 int answer;
537 //printf("VirtualFree(0x%08X, %d, 0x%08X)\n", (unsigned)address, dwSize, dwFreeType);
538 while(str)
540 if(address!=str->address)
542 str=str->prev;
543 continue;
545 //printf(" VirtualFree(...) munmap(0x%08X, %d)\n", (unsigned)str->address, str->mapping_size);
546 answer=munmap(str->address, str->mapping_size);
547 if(str->next)str->next->prev=str->prev;
548 if(str->prev)str->prev->next=str->next;
549 if(vm==str)vm=str->prev;
550 free(str);
551 return 0;
553 return -1;
556 INT WINAPI WideCharToMultiByte(UINT codepage, DWORD flags, LPCWSTR src,
557 INT srclen,LPSTR dest, INT destlen, LPCSTR defch, WIN_BOOL* used_defch)
559 int i;
560 if(srclen==-1){srclen=0; while(src[srclen++]);}
561 if(destlen==0)
562 return srclen;
563 if(used_defch)
564 *used_defch=0;
565 for(i=0; i<min(srclen, destlen); i++)
566 *dest++=(char)*src++;
567 return min(srclen, destlen);
569 INT WINAPI MultiByteToWideChar(UINT codepage,DWORD flags, LPCSTR src, INT srclen,
570 LPWSTR dest, INT destlen)
572 int i;
573 if(srclen==-1){srclen=0; while(src[srclen++]);}
574 if(destlen==0)
575 return srclen;
576 for(i=0; i<min(srclen, destlen); i++)
577 *dest++=(WCHAR)*src++;
578 return min(srclen, destlen);
580 HANDLE WINAPI OpenFileMappingA(DWORD access, WIN_BOOL prot, LPCSTR name)
582 file_mapping* p;
583 if(fm==0)
584 return (HANDLE)0;
585 if(name==0)
586 return (HANDLE)0;
587 for(p=fm; p; p=p->prev)
589 if(p->name==0)
590 continue;
591 if(strcmp(p->name, name)==0)
592 return (HANDLE)p->handle;
594 return 0;