Lots of warning fixed, one missing WINAPI in ddraw.c added.
[wine] / loader / resource.c
1 /*
2  * Resources
3  *
4  * Copyright 1993 Robert J. Amstadt
5  * Copyright 1995 Alexandre Julliard
6  */
7
8 #include <assert.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include "winbase.h"
16 #include "windef.h"
17 #include "winuser.h"
18 #include "wine/winbase16.h"
19 #include "wine/winuser16.h"
20 #include "ldt.h"
21 #include "global.h"
22 #include "heap.h"
23 #include "callback.h"
24 #include "cursoricon.h"
25 #include "neexe.h"
26 #include "task.h"
27 #include "process.h"
28 #include "module.h"
29 #include "file.h"
30 #include "debug.h"
31 #include "libres.h"
32 #include "winerror.h"
33 #include "debugstr.h"
34
35 extern WORD WINE_LanguageId;
36
37 #define HRSRC_MAP_BLOCKSIZE 16
38
39 typedef struct _HRSRC_ELEM
40 {
41     HANDLE hRsrc;
42     WORD     type;
43 } HRSRC_ELEM;
44
45 typedef struct _HRSRC_MAP
46 {
47     int nAlloc;
48     int nUsed;
49     HRSRC_ELEM *elem;
50 } HRSRC_MAP;
51
52 /**********************************************************************
53  *          MapHRsrc32To16
54  */
55 static HRSRC16 MapHRsrc32To16( NE_MODULE *pModule, HANDLE hRsrc32, WORD type )
56 {
57     HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
58     HRSRC_ELEM *newElem;
59     int i;
60
61     /* On first call, initialize HRSRC map */
62     if ( !map )
63     {
64         if ( !(map = (HRSRC_MAP *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 
65                                              sizeof(HRSRC_MAP) ) ) )
66         {
67             ERR( resource, "Cannot allocate HRSRC map\n" );
68             return 0;
69         }
70         pModule->hRsrcMap = (LPVOID)map;
71     }
72
73     /* Check whether HRSRC32 already in map */
74     for ( i = 0; i < map->nUsed; i++ )
75         if ( map->elem[i].hRsrc == hRsrc32 )
76             return (HRSRC16)(i + 1);
77
78     /* If no space left, grow table */
79     if ( map->nUsed == map->nAlloc )
80     {
81         if ( !(newElem = (HRSRC_ELEM *)HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 
82                                                     map->elem, 
83                                                     (map->nAlloc + HRSRC_MAP_BLOCKSIZE) 
84                                                     * sizeof(HRSRC_ELEM) ) ))
85         {
86             ERR( resource, "Cannot grow HRSRC map\n" );
87             return 0;
88         }
89         map->elem = newElem;
90         map->nAlloc += HRSRC_MAP_BLOCKSIZE;
91     }
92
93     /* Add HRSRC32 to table */
94     map->elem[map->nUsed].hRsrc = hRsrc32;
95     map->elem[map->nUsed].type  = type;
96     map->nUsed++;
97
98     return (HRSRC16)map->nUsed;
99 }
100
101 /**********************************************************************
102  *          MapHRsrc16To32
103  */
104 static HANDLE MapHRsrc16To32( NE_MODULE *pModule, HRSRC16 hRsrc16 )
105 {
106     HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
107     if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
108
109     return map->elem[(int)hRsrc16-1].hRsrc;
110 }
111
112 /**********************************************************************
113  *          MapHRsrc16ToType
114  */
115 static WORD MapHRsrc16ToType( NE_MODULE *pModule, HRSRC16 hRsrc16 )
116 {
117     HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
118     if ( !map || !hRsrc16 || (int)hRsrc16 > map->nUsed ) return 0;
119
120     return map->elem[(int)hRsrc16-1].type;
121 }
122
123
124 /**********************************************************************
125  *          RES_FindResource
126  */
127 static HRSRC RES_FindResource( HMODULE hModule, LPCSTR type,
128                                LPCSTR name, WORD lang, 
129                                BOOL bUnicode, BOOL bRet16 )
130 {
131     HRSRC hRsrc = 0;
132
133     HMODULE16 hMod16   = MapHModuleLS( hModule );
134     NE_MODULE *pModule = NE_GetPtr( hMod16 );
135     WINE_MODREF *wm    = pModule && pModule->module32? 
136                          MODULE32_LookupHMODULE( pModule->module32 ) : NULL;
137
138     TRACE( resource, "(%08x %s, %08x%s, %08x%s, %04x, %s, %s)\n",
139            hModule, NE_MODULE_NAME(pModule),
140            (UINT)type, HIWORD(type)? (bUnicode? debugstr_w((LPWSTR)type) : debugstr_a(type)) : "",
141            (UINT)name, HIWORD(name)? (bUnicode? debugstr_w((LPWSTR)name) : debugstr_a(name)) : "",
142            lang,
143            bUnicode? "W"  : "A",
144            bRet16?   "NE" : "PE" );
145
146     if ( !pModule ) return 0;
147
148     if ( wm )
149     {
150         /* 32-bit PE/ELF module */
151         LPWSTR typeStr, nameStr;
152
153         if ( HIWORD( type ) && !bUnicode )
154             typeStr = HEAP_strdupAtoW( GetProcessHeap(), 0, type );
155         else
156             typeStr = (LPWSTR)type;
157         if ( HIWORD( name ) && !bUnicode )
158             nameStr = HEAP_strdupAtoW( GetProcessHeap(), 0, name );
159         else
160             nameStr = (LPWSTR)name;
161
162         switch ( wm->type ) 
163         {
164         case MODULE32_PE:
165             hRsrc = PE_FindResourceExW( wm, nameStr, typeStr, lang );
166             break;
167
168         case MODULE32_ELF:
169             hRsrc = LIBRES_FindResource( hModule, nameStr, typeStr );
170             break;
171         
172         default:
173             ERR( resource, "unknown module type %d\n", wm->type );
174             break;
175         }
176
177         if ( HIWORD( type ) && !bUnicode ) 
178             HeapFree( GetProcessHeap(), 0, typeStr );
179         if ( HIWORD( name ) && !bUnicode ) 
180             HeapFree( GetProcessHeap(), 0, nameStr );
181
182
183         /* If we need to return 16-bit HRSRC, perform conversion */
184         if ( bRet16 )
185             hRsrc = MapHRsrc32To16( pModule, hRsrc, 
186                                     HIWORD( type )? 0 : LOWORD( type ) );
187     }
188     else
189     {
190         /* 16-bit NE module */
191         LPSTR typeStr, nameStr;
192
193         if ( HIWORD( type ) && bUnicode )
194             typeStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)type );
195         else
196             typeStr = (LPSTR)type;
197         if ( HIWORD( name ) && bUnicode )
198             nameStr = HEAP_strdupWtoA( GetProcessHeap(), 0, (LPCWSTR)name );
199         else
200             nameStr = (LPSTR)name;
201
202         hRsrc = NE_FindResource( pModule, nameStr, typeStr );
203
204         if ( HIWORD( type ) && bUnicode ) 
205             HeapFree( GetProcessHeap(), 0, typeStr );
206         if ( HIWORD( name ) && bUnicode ) 
207             HeapFree( GetProcessHeap(), 0, nameStr );
208
209
210         /* If we need to return 32-bit HRSRC, no conversion is necessary,
211            we simply use the 16-bit HRSRC as 32-bit HRSRC */
212     }
213
214     return hRsrc;
215 }
216
217 /**********************************************************************
218  *          RES_SizeofResource
219  */
220 static DWORD RES_SizeofResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
221 {
222     DWORD size = 0;
223
224     HMODULE16 hMod16   = MapHModuleLS( hModule );
225     NE_MODULE *pModule = NE_GetPtr( hMod16 );
226     WINE_MODREF *wm    = pModule && pModule->module32? 
227                          MODULE32_LookupHMODULE( pModule->module32 ) : NULL;
228
229     TRACE( resource, "(%08x %s, %08x, %s)\n",
230            hModule, NE_MODULE_NAME(pModule), hRsrc, bRet16? "NE" : "PE" );
231
232     if ( !pModule || !hRsrc ) return 0;
233
234     if ( wm )
235     {
236         /* 32-bit PE/ELF module */
237
238         /* If we got a 16-bit hRsrc, convert it */
239         HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
240
241         switch ( wm->type ) 
242         {
243         case MODULE32_PE:
244             size = PE_SizeofResource( hModule, hRsrc32 );
245             break;
246
247         case MODULE32_ELF:
248             size = LIBRES_SizeofResource( hModule, hRsrc32 );
249             break;
250
251         default:
252             ERR( resource, "unknown module type %d\n", wm->type );
253             break;
254         }
255     }
256     else
257     {
258         /* 16-bit NE module */
259
260         /* If we got a 32-bit hRsrc, we don't need to convert it */
261
262         size = NE_AccessResource( pModule, hRsrc );
263     }
264
265     return size;
266 }
267
268 /**********************************************************************
269  *          RES_AccessResource
270  */
271 static HFILE RES_AccessResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
272 {
273     HFILE hFile = HFILE_ERROR;
274
275     HMODULE16 hMod16   = MapHModuleLS( hModule );
276     NE_MODULE *pModule = NE_GetPtr( hMod16 );
277     WINE_MODREF *wm    = pModule && pModule->module32? 
278                          MODULE32_LookupHMODULE( pModule->module32 ) : NULL;
279
280     TRACE( resource, "(%08x %s, %08x, %s)\n",
281            hModule, NE_MODULE_NAME(pModule), hRsrc, bRet16? "NE" : "PE" );
282
283     if ( !pModule || !hRsrc ) return HFILE_ERROR;
284
285     if ( wm )
286     {
287         /* 32-bit PE/ELF module */
288 #if 0
289         /* If we got a 16-bit hRsrc, convert it */
290         HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
291 #endif
292
293         FIXME( resource, "32-bit modules not yet supported.\n" );
294         hFile = HFILE_ERROR;
295
296         /* If we need to return a 16-bit file handle, convert it */
297         if ( bRet16 )
298             hFile = FILE_AllocDosHandle( hFile );
299     }
300     else
301     {
302         /* 16-bit NE module */
303
304         /* If we got a 32-bit hRsrc, we don't need to convert it */
305
306         hFile = NE_AccessResource( pModule, hRsrc );
307
308         /* If we are to return a 32-bit file handle, convert it */
309         if ( !bRet16 )
310             hFile = FILE_GetHandle( hFile );
311     }
312
313     return hFile;
314 }
315
316 /**********************************************************************
317  *          RES_LoadResource
318  */
319 static HGLOBAL RES_LoadResource( HMODULE hModule, HRSRC hRsrc, BOOL bRet16 )
320 {
321     HGLOBAL hMem = 0;
322
323     HMODULE16 hMod16   = MapHModuleLS( hModule );
324     NE_MODULE *pModule = NE_GetPtr( hMod16 );
325     WINE_MODREF *wm    = pModule && pModule->module32? 
326                          MODULE32_LookupHMODULE( pModule->module32 ) : NULL;
327
328     TRACE( resource, "(%08x %s, %08x, %s)\n",
329            hModule, NE_MODULE_NAME(pModule), hRsrc, bRet16? "NE" : "PE" );
330
331     if ( !pModule || !hRsrc ) return 0;
332
333     if ( wm )
334     {
335         /* 32-bit PE/ELF module */
336
337         /* If we got a 16-bit hRsrc, convert it */
338         HRSRC hRsrc32 = HIWORD(hRsrc)? hRsrc : MapHRsrc16To32( pModule, hRsrc );
339
340         switch ( wm->type ) 
341         {
342         case MODULE32_PE:
343             hMem = PE_LoadResource( wm, hRsrc32 );
344             break;
345
346         case MODULE32_ELF:
347             hMem = LIBRES_LoadResource( hModule, hRsrc32 );
348             break;
349
350         default:
351             ERR( resource, "unknown module type %d\n", wm->type );
352             break;
353         }
354
355         /* If we need to return a 16-bit resource, convert it */
356         if ( bRet16 )
357         {
358             WORD type   = MapHRsrc16ToType( pModule, hRsrc );
359             DWORD size  = SizeofResource( hModule, hRsrc );
360             LPVOID bits = LockResource( hMem );
361
362             hMem = NE_LoadPEResource( pModule, type, bits, size );
363         }
364     }
365     else
366     {
367         /* 16-bit NE module */
368
369         /* If we got a 32-bit hRsrc, we don't need to convert it */
370
371         hMem = NE_LoadResource( pModule, hRsrc );
372
373         /* If we are to return a 32-bit resource, we should probably
374            convert it but we don't for now.  FIXME !!! */
375     }
376
377     return hMem;
378 }
379
380 /**********************************************************************
381  *          RES_LockResource
382  */
383 static LPVOID RES_LockResource( HGLOBAL handle, BOOL bRet16 )
384 {
385     LPVOID bits = NULL;
386
387     TRACE( resource, "(%08x, %s)\n", handle, bRet16? "NE" : "PE" );
388
389     if ( HIWORD( handle ) )
390     {
391         /* 32-bit memory handle */
392
393         if ( bRet16 )
394             FIXME( resource, "can't return SEGPTR to 32-bit resource %08x.\n", handle );
395         else
396             bits = (LPVOID)handle;
397     }
398     else
399     {
400         /* 16-bit memory handle */
401
402         /* May need to reload the resource if discarded */
403         SEGPTR segPtr = WIN16_GlobalLock16( handle );
404         
405         if ( bRet16 )
406             bits = (LPVOID)segPtr;
407         else
408             bits = PTR_SEG_TO_LIN( segPtr );
409     }
410
411     return bits;
412 }
413
414 /**********************************************************************
415  *          RES_FreeResource
416  */
417 static BOOL RES_FreeResource( HGLOBAL handle )
418 {
419     HGLOBAL retv = handle;
420
421     TRACE( resource, "(%08x)\n", handle );
422
423     if ( HIWORD( handle ) )
424     {
425         /* 32-bit memory handle: nothing to do */
426     }
427     else
428     {
429         /* 16-bit memory handle */
430         NE_MODULE *pModule = NE_GetPtr( FarGetOwner16( handle ) );
431
432         /* Try NE resource first */
433         retv = NE_FreeResource( pModule, handle );
434
435         /* If this failed, call USER.DestroyIcon32; this will check
436            whether it is a shared cursor/icon; if not it will call
437            GlobalFree16() */
438         if ( retv ) {
439             if ( Callout.DestroyIcon32 )
440                 retv = Callout.DestroyIcon32( handle, CID_RESOURCE );
441             else
442                 retv = GlobalFree16( handle );
443         }
444     }
445
446     return (BOOL)retv;
447 }
448
449
450 /**********************************************************************
451  *          FindResource16   (KERNEL.60)
452  */
453 HRSRC16 WINAPI FindResource16( HMODULE16 hModule, SEGPTR name, SEGPTR type )
454 {
455     LPCSTR nameStr = HIWORD(name)? PTR_SEG_TO_LIN(name) : (LPCSTR)name;
456     LPCSTR typeStr = HIWORD(type)? PTR_SEG_TO_LIN(type) : (LPCSTR)type;
457
458     return RES_FindResource( hModule, typeStr, nameStr, 
459                              WINE_LanguageId, FALSE, TRUE );
460 }
461
462 /**********************************************************************
463  *          FindResourceA    (KERNEL32.128)
464  */
465 HANDLE WINAPI FindResourceA( HMODULE hModule, LPCSTR name, LPCSTR type )
466 {
467     return RES_FindResource( hModule, type, name, 
468                              WINE_LanguageId, FALSE, FALSE );
469 }
470
471 /**********************************************************************
472  *          FindResourceExA  (KERNEL32.129)
473  */
474 HANDLE WINAPI FindResourceExA( HMODULE hModule, 
475                                LPCSTR type, LPCSTR name, WORD lang )
476 {
477     return RES_FindResource( hModule, type, name, 
478                              lang, FALSE, FALSE );
479 }
480
481 /**********************************************************************
482  *          FindResourceExW  (KERNEL32.130)
483  */
484 HRSRC WINAPI FindResourceExW( HMODULE hModule, 
485                               LPCWSTR type, LPCWSTR name, WORD lang )
486 {
487     return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name, 
488                              lang, TRUE, FALSE );
489 }
490
491 /**********************************************************************
492  *          FindResourceW    (KERNEL32.131)
493  */
494 HRSRC WINAPI FindResourceW(HINSTANCE hModule, LPCWSTR name, LPCWSTR type)
495 {
496     return RES_FindResource( hModule, (LPCSTR)type, (LPCSTR)name, 
497                              WINE_LanguageId, TRUE, FALSE );
498 }
499
500 /**********************************************************************
501  *          LoadResource16   (KERNEL.61)
502  */
503 HGLOBAL16 WINAPI LoadResource16( HMODULE16 hModule, HRSRC16 hRsrc )
504 {
505     return RES_LoadResource( hModule, hRsrc, TRUE );
506 }
507
508 /**********************************************************************
509  *          LoadResource     (KERNEL32.370)
510  */
511 HGLOBAL WINAPI LoadResource( HINSTANCE hModule, HRSRC hRsrc )
512 {
513     return RES_LoadResource( hModule, hRsrc, FALSE );
514 }
515
516 /**********************************************************************
517  *          LockResource16   (KERNEL.62)
518  */
519 SEGPTR WINAPI WIN16_LockResource16( HGLOBAL16 handle )
520 {
521     return (SEGPTR)RES_LockResource( handle, TRUE );
522 }
523 LPVOID WINAPI LockResource16( HGLOBAL16 handle )
524 {
525     return RES_LockResource( handle, FALSE );
526 }
527
528 /**********************************************************************
529  *          LockResource     (KERNEL32.384)
530  */
531 LPVOID WINAPI LockResource( HGLOBAL handle )
532 {
533     return RES_LockResource( handle, FALSE );
534 }
535
536 /**********************************************************************
537  *          FreeResource16   (KERNEL.63)
538  */
539 BOOL16 WINAPI FreeResource16( HGLOBAL16 handle )
540 {
541     return RES_FreeResource( handle );
542 }
543
544 /**********************************************************************
545  *          FreeResource     (KERNEL32.145)
546  */
547 BOOL WINAPI FreeResource( HGLOBAL handle )
548 {
549     return RES_FreeResource( handle );
550 }
551
552 /**********************************************************************
553  *          AccessResource16 (KERNEL.64)
554  */
555 INT16 WINAPI AccessResource16( HINSTANCE16 hModule, HRSRC16 hRsrc )
556 {
557     return RES_AccessResource( hModule, hRsrc, TRUE );
558 }
559
560 /**********************************************************************
561  *          AccessResource   (KERNEL32.64)
562  */
563 INT WINAPI AccessResource( HMODULE hModule, HRSRC hRsrc )
564 {
565     return RES_AccessResource( hModule, hRsrc, FALSE );
566 }
567
568 /**********************************************************************
569  *          SizeofResource16 (KERNEL.65)
570  */
571 DWORD WINAPI SizeofResource16( HMODULE16 hModule, HRSRC16 hRsrc )
572 {
573     return RES_SizeofResource( hModule, hRsrc, TRUE );
574 }
575
576 /**********************************************************************
577  *          SizeofResource   (KERNEL32.522)
578  */
579 DWORD WINAPI SizeofResource( HINSTANCE hModule, HRSRC hRsrc )
580 {
581     return RES_SizeofResource( hModule, hRsrc, FALSE );
582 }
583
584
585
586 /**********************************************************************
587  *                      LoadAccelerators16      [USER.177]
588  */
589 HACCEL16 WINAPI LoadAccelerators16(HINSTANCE16 instance, SEGPTR lpTableName)
590 {
591     HRSRC16     hRsrc;
592
593     if (HIWORD(lpTableName))
594         TRACE(accel, "%04x '%s'\n",
595                       instance, (char *)PTR_SEG_TO_LIN( lpTableName ) );
596     else
597         TRACE(accel, "%04x %04x\n",
598                        instance, LOWORD(lpTableName) );
599
600     if (!(hRsrc = FindResource16( instance, lpTableName, RT_ACCELERATOR16 ))) {
601       WARN(accel, "couldn't find accelerator table resource\n");
602       return 0;
603     }
604
605     TRACE(accel, "returning HACCEL 0x%x\n", hRsrc);
606     return LoadResource16(instance,hRsrc);
607 }
608
609 /**********************************************************************
610  *                      LoadAccelerators32W     [USER.177]
611  * The image layout seems to look like this (not 100% sure):
612  * 00:  BYTE    type            type of accelerator
613  * 01:  BYTE    pad             (to WORD boundary)
614  * 02:  WORD    event
615  * 04:  WORD    IDval           
616  * 06:  WORD    pad             (to DWORD boundary)
617  */
618 HACCEL WINAPI LoadAcceleratorsW(HINSTANCE instance,LPCWSTR lpTableName)
619 {
620     HRSRC hRsrc;
621     HACCEL hMem,hRetval=0;
622     DWORD size;
623
624     if (HIWORD(lpTableName))
625         TRACE(accel, "%p '%s'\n",
626                       (LPVOID)instance, (char *)( lpTableName ) );
627     else
628         TRACE(accel, "%p 0x%04x\n",
629                        (LPVOID)instance, LOWORD(lpTableName) );
630
631     if (!(hRsrc = FindResourceW( instance, lpTableName, RT_ACCELERATORW )))
632     {
633       WARN(accel, "couldn't find accelerator table resource\n");
634     } else {
635       hMem = LoadResource( instance, hRsrc );
636       size = SizeofResource( instance, hRsrc );
637       if(size>=sizeof(PE_ACCEL))
638       {
639         LPPE_ACCEL accel_table = (LPPE_ACCEL) hMem;
640         LPACCEL16 accel16;
641         int i,nrofaccells = size/sizeof(PE_ACCEL);
642
643         hRetval = GlobalAlloc16(0,sizeof(ACCEL16)*nrofaccells);
644         accel16 = (LPACCEL16)GlobalLock16(hRetval);
645         for (i=0;i<nrofaccells;i++) {
646                 accel16[i].fVirt = accel_table[i].fVirt;
647                 accel16[i].key = accel_table[i].key;
648                 accel16[i].cmd = accel_table[i].cmd;
649         }
650         accel16[i-1].fVirt |= 0x80;
651       }
652     }
653     TRACE(accel, "returning HACCEL 0x%x\n", hRsrc);
654     return hRetval;
655 }
656
657 HACCEL WINAPI LoadAcceleratorsA(HINSTANCE instance,LPCSTR lpTableName)
658 {
659         LPWSTR   uni;
660         HACCEL result;
661         if (HIWORD(lpTableName))
662                 uni = HEAP_strdupAtoW( GetProcessHeap(), 0, lpTableName );
663         else
664                 uni = (LPWSTR)lpTableName;
665         result = LoadAcceleratorsW(instance,uni);
666         if (HIWORD(uni)) HeapFree( GetProcessHeap(), 0, uni);
667         return result;
668 }
669
670 /**********************************************************************
671  *             CopyAcceleratorTable32A   (USER32.58)
672  */
673 INT WINAPI CopyAcceleratorTableA(HACCEL src, LPACCEL dst, INT entries)
674 {
675   return CopyAcceleratorTableW(src, dst, entries);
676 }
677
678 /**********************************************************************
679  *             CopyAcceleratorTable32W   (USER32.59)
680  *
681  * By mortene@pvv.org 980321
682  */
683 INT WINAPI CopyAcceleratorTableW(HACCEL src, LPACCEL dst,
684                                      INT entries)
685 {
686   int i,xsize;
687   LPACCEL16 accel = (LPACCEL16)GlobalLock16(src);
688   BOOL done = FALSE;
689
690   /* Do parameter checking to avoid the explosions and the screaming
691      as far as possible. */
692   if((dst && (entries < 1)) || (src == (HACCEL)NULL) || !accel) {
693     WARN(accel, "Application sent invalid parameters (%p %p %d).\n",
694          (LPVOID)src, (LPVOID)dst, entries);
695     return 0;
696   }
697   xsize = GlobalSize16(src)/sizeof(ACCEL16);
698   if (xsize>entries) entries=xsize;
699
700   i=0;
701   while(!done) {
702     /* Spit out some debugging information. */
703     TRACE(accel, "accel %d: type 0x%02x, event '%c', IDval 0x%04x.\n",
704           i, accel[i].fVirt, accel[i].key, accel[i].cmd);
705
706     /* Copy data to the destination structure array (if dst == NULL,
707        we're just supposed to count the number of entries). */
708     if(dst) {
709       dst[i].fVirt = accel[i].fVirt;
710       dst[i].key = accel[i].key;
711       dst[i].cmd = accel[i].cmd;
712
713       /* Check if we've reached the end of the application supplied
714          accelerator table. */
715       if(i+1 == entries) {
716         /* Turn off the high order bit, just in case. */
717         dst[i].fVirt &= 0x7f;
718         done = TRUE;
719       }
720     }
721
722     /* The highest order bit seems to mark the end of the accelerator
723        resource table, but not always. Use GlobalSize() check too. */
724     if((accel[i].fVirt & 0x80) != 0) done = TRUE;
725
726     i++;
727   }
728
729   return i;
730 }
731
732 /*********************************************************************
733  *                    CreateAcceleratorTable   (USER32.64)
734  *
735  * By mortene@pvv.org 980321
736  */
737 HACCEL WINAPI CreateAcceleratorTableA(LPACCEL lpaccel, INT cEntries)
738 {
739   HACCEL        hAccel;
740   LPACCEL16     accel;
741   int           i;
742
743   /* Do parameter checking just in case someone's trying to be
744      funny. */
745   if(cEntries < 1) {
746     WARN(accel, "Application sent invalid parameters (%p %d).\n",
747          lpaccel, cEntries);
748     SetLastError(ERROR_INVALID_PARAMETER);
749     return (HACCEL)NULL;
750   }
751   FIXME(accel, "should check that the accelerator descriptions are valid,"
752         " return NULL and SetLastError() if not.\n");
753
754
755   /* Allocate memory and copy the table. */
756   hAccel = GlobalAlloc16(0,cEntries*sizeof(ACCEL16));
757
758   TRACE(accel, "handle %x\n", hAccel);
759   if(!hAccel) {
760     ERR(accel, "Out of memory.\n");
761     SetLastError(ERROR_NOT_ENOUGH_MEMORY);
762     return (HACCEL)NULL;
763   }
764   accel = GlobalLock16(hAccel);
765   for (i=0;i<cEntries;i++) {
766         accel[i].fVirt = lpaccel[i].fVirt;
767         accel[i].key = lpaccel[i].key;
768         accel[i].cmd = lpaccel[i].cmd;
769   }
770   /* Set the end-of-table terminator. */
771   accel[cEntries-1].fVirt |= 0x80;
772
773   TRACE(accel, "Allocated accelerator handle %x\n", hAccel);
774   return hAccel;
775 }
776
777
778 /******************************************************************************
779  * DestroyAcceleratorTable [USER32.130]
780  * Destroys an accelerator table
781  *
782  * NOTES
783  *    By mortene@pvv.org 980321
784  *
785  * PARAMS
786  *    handle [I] Handle to accelerator table
787  *
788  * RETURNS STD
789  */
790 BOOL WINAPI DestroyAcceleratorTable( HACCEL handle )
791 {
792     FIXME(accel, "(0x%x): stub\n", handle);
793     /* FIXME: GlobalFree16(handle); */
794     return TRUE;
795 }
796   
797 /**********************************************************************
798  *                                      LoadString16
799  */
800 INT16 WINAPI LoadString16( HINSTANCE16 instance, UINT16 resource_id,
801                            LPSTR buffer, INT16 buflen )
802 {
803     HGLOBAL16 hmem;
804     HRSRC16 hrsrc;
805     unsigned char *p;
806     int string_num;
807     int i;
808
809     TRACE(resource,"inst=%04x id=%04x buff=%08x len=%d\n",
810                      instance, resource_id, (int) buffer, buflen);
811
812     hrsrc = FindResource16( instance, (SEGPTR)((resource_id>>4)+1), RT_STRING16 );
813     if (!hrsrc) return 0;
814     hmem = LoadResource16( instance, hrsrc );
815     if (!hmem) return 0;
816     
817     p = LockResource16(hmem);
818     string_num = resource_id & 0x000f;
819     for (i = 0; i < string_num; i++)
820         p += *p + 1;
821     
822     TRACE(resource, "strlen = %d\n", (int)*p );
823     
824     i = MIN(buflen - 1, *p);
825     if (buffer == NULL)
826         return i;
827     if (i > 0) {
828         memcpy(buffer, p + 1, i);
829         buffer[i] = '\0';
830     } else {
831         if (buflen > 1) {
832             buffer[0] = '\0';
833             return 0;
834         }
835         WARN(resource,"Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
836     }
837     FreeResource16( hmem );
838
839     TRACE(resource,"'%s' loaded !\n", buffer);
840     return i;
841 }
842
843 /**********************************************************************
844  *      LoadString32W           (USER32.376)
845  */
846 INT WINAPI LoadStringW( HINSTANCE instance, UINT resource_id,
847                             LPWSTR buffer, INT buflen )
848 {
849     HGLOBAL hmem;
850     HRSRC hrsrc;
851     WCHAR *p;
852     int string_num;
853     int i;
854
855     if (HIWORD(resource_id)==0xFFFF) /* netscape 3 passes this */
856         resource_id = (UINT)(-((INT)resource_id));
857     TRACE(resource, "instance = %04x, id = %04x, buffer = %08x, "
858            "length = %d\n", instance, (int)resource_id, (int) buffer, buflen);
859
860     /* Use bits 4 - 19 (incremented by 1) as resourceid, mask out 
861      * 20 - 31. */
862     hrsrc = FindResourceW( instance, (LPCWSTR)(((resource_id>>4)&0xffff)+1),
863                              RT_STRINGW );
864     if (!hrsrc) return 0;
865     hmem = LoadResource( instance, hrsrc );
866     if (!hmem) return 0;
867     
868     p = LockResource(hmem);
869     string_num = resource_id & 0x000f;
870     for (i = 0; i < string_num; i++)
871         p += *p + 1;
872     
873     TRACE(resource, "strlen = %d\n", (int)*p );
874     
875     i = MIN(buflen - 1, *p);
876     if (buffer == NULL)
877         return i;
878     if (i > 0) {
879         memcpy(buffer, p + 1, i * sizeof (WCHAR));
880         buffer[i] = (WCHAR) 0;
881     } else {
882         if (buflen > 1) {
883             buffer[0] = (WCHAR) 0;
884             return 0;
885         }
886 #if 0
887         WARN(resource,"Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
888 #endif
889     }
890
891     TRACE(resource,"%s loaded !\n", debugstr_w(buffer));
892     return i;
893 }
894
895 /**********************************************************************
896  *      LoadString32A   (USER32.375)
897  */
898 INT WINAPI LoadStringA( HINSTANCE instance, UINT resource_id,
899                             LPSTR buffer, INT buflen )
900 {
901     INT retval;
902     LPWSTR buffer2 = NULL;
903     if (buffer && buflen)
904         buffer2 = HeapAlloc( GetProcessHeap(), 0, buflen * 2 );
905     retval = LoadStringW(instance,resource_id,buffer2,buflen);
906
907     if (buffer2)
908     {
909         if (retval) {
910             lstrcpynWtoA( buffer, buffer2, buflen );
911             retval = lstrlenA( buffer );
912         }
913         else
914             *buffer = 0;
915         HeapFree( GetProcessHeap(), 0, buffer2 );
916     }
917     return retval;
918 }
919
920 /* Messages...used by FormatMessage32* (KERNEL32.something)
921  * 
922  * They can be specified either directly or using a message ID and
923  * loading them from the resource.
924  * 
925  * The resourcedata has following format:
926  * start:
927  * 0: DWORD nrofentries
928  * nrofentries * subentry:
929  *      0: DWORD firstentry
930  *      4: DWORD lastentry
931  *      8: DWORD offset from start to the stringentries
932  *
933  * (lastentry-firstentry) * stringentry:
934  * 0: WORD len (0 marks end)
935  * 2: WORD flags
936  * 4: CHAR[len-4]
937  *      (stringentry i of a subentry refers to the ID 'firstentry+i')
938  *
939  * Yes, ANSI strings in win32 resources. Go figure.
940  */
941
942 /**********************************************************************
943  *      LoadMessage32A          (internal)
944  */
945 INT WINAPI LoadMessageA( HMODULE instance, UINT id, WORD lang,
946                       LPSTR buffer, INT buflen )
947 {
948     HGLOBAL     hmem;
949     HRSRC       hrsrc;
950     PMESSAGE_RESOURCE_DATA      mrd;
951     PMESSAGE_RESOURCE_BLOCK     mrb;
952     PMESSAGE_RESOURCE_ENTRY     mre;
953     int         i,slen;
954
955     TRACE(resource, "instance = %08lx, id = %08lx, buffer = %p, length = %ld\n", (DWORD)instance, (DWORD)id, buffer, (DWORD)buflen);
956
957     /*FIXME: I am not sure about the '1' ... But I've only seen those entries*/
958     hrsrc = FindResourceExW(instance,RT_MESSAGELISTW,(LPWSTR)1,lang);
959     if (!hrsrc) return 0;
960     hmem = LoadResource( instance, hrsrc );
961     if (!hmem) return 0;
962     
963     mrd = (PMESSAGE_RESOURCE_DATA)LockResource(hmem);
964     mre = NULL;
965     mrb = &(mrd->Blocks[0]);
966     for (i=mrd->NumberOfBlocks;i--;) {
967         if ((id>=mrb->LowId) && (id<=mrb->HighId)) {
968             mre = (PMESSAGE_RESOURCE_ENTRY)(((char*)mrd)+mrb->OffsetToEntries);
969             id  -= mrb->LowId;
970             break;
971         }
972         mrb++;
973     }
974     if (!mre)
975         return 0;
976     for (i=id;i--;) {
977         if (!mre->Length)
978                 return 0;
979         mre = (PMESSAGE_RESOURCE_ENTRY)(((char*)mre)+(mre->Length));
980     }
981     slen=mre->Length;
982     TRACE(resource,"    - strlen=%d\n",slen);
983     i = MIN(buflen - 1, slen);
984     if (buffer == NULL)
985         return slen; /* different to LoadString */
986     if (i>0) {
987         lstrcpynA(buffer,(char*)mre->Text,i);
988         buffer[i]=0;
989     } else {
990         if (buflen>1) {
991             buffer[0]=0;
992             return 0;
993         }
994     }
995     if (buffer)
996             TRACE(resource,"'%s' copied !\n", buffer);
997     return i;
998 }
999
1000 /**********************************************************************
1001  *      LoadMessage32W  (internal)
1002  */
1003 INT WINAPI LoadMessageW( HMODULE instance, UINT id, WORD lang,
1004                       LPWSTR buffer, INT buflen )
1005 {
1006     INT retval;
1007     LPSTR buffer2 = NULL;
1008     if (buffer && buflen)
1009         buffer2 = HeapAlloc( GetProcessHeap(), 0, buflen );
1010     retval = LoadMessageA(instance,id,lang,buffer2,buflen);
1011     if (buffer)
1012     {
1013         if (retval) {
1014             lstrcpynAtoW( buffer, buffer2, buflen );
1015             retval = lstrlenW( buffer );
1016         }
1017         HeapFree( GetProcessHeap(), 0, buffer2 );
1018     }
1019     return retval;
1020 }
1021
1022
1023 /**********************************************************************
1024  *      EnumResourceTypesA      (KERNEL32.90)
1025  */
1026 BOOL WINAPI EnumResourceTypesA( HMODULE hmodule,ENUMRESTYPEPROCA lpfun,
1027                                     LONG lParam)
1028 {
1029         /* FIXME: move WINE_MODREF stuff here */
1030     return PE_EnumResourceTypesA(hmodule,lpfun,lParam);
1031 }
1032
1033 /**********************************************************************
1034  *      EnumResourceTypesW      (KERNEL32.91)
1035  */
1036 BOOL WINAPI EnumResourceTypesW( HMODULE hmodule,ENUMRESTYPEPROCW lpfun,
1037                                     LONG lParam)
1038 {
1039         /* FIXME: move WINE_MODREF stuff here */
1040     return PE_EnumResourceTypesW(hmodule,lpfun,lParam);
1041 }
1042
1043 /**********************************************************************
1044  *      EnumResourceNamesA      (KERNEL32.88)
1045  */
1046 BOOL WINAPI EnumResourceNamesA( HMODULE hmodule, LPCSTR type,
1047                                     ENUMRESNAMEPROCA lpfun, LONG lParam )
1048 {
1049         /* FIXME: move WINE_MODREF stuff here */
1050     return PE_EnumResourceNamesA(hmodule,type,lpfun,lParam);
1051 }
1052 /**********************************************************************
1053  *      EnumResourceNamesW      (KERNEL32.89)
1054  */
1055 BOOL WINAPI EnumResourceNamesW( HMODULE hmodule, LPCWSTR type,
1056                                     ENUMRESNAMEPROCW lpfun, LONG lParam )
1057 {
1058         /* FIXME: move WINE_MODREF stuff here */
1059     return PE_EnumResourceNamesW(hmodule,type,lpfun,lParam);
1060 }
1061
1062 /**********************************************************************
1063  *      EnumResourceLanguagesA  (KERNEL32.86)
1064  */
1065 BOOL WINAPI EnumResourceLanguagesA( HMODULE hmodule, LPCSTR type,
1066                                         LPCSTR name, ENUMRESLANGPROCA lpfun,
1067                                         LONG lParam)
1068 {
1069         /* FIXME: move WINE_MODREF stuff here */
1070     return PE_EnumResourceLanguagesA(hmodule,type,name,lpfun,lParam);
1071 }
1072 /**********************************************************************
1073  *      EnumResourceLanguagesW  (KERNEL32.87)
1074  */
1075 BOOL WINAPI EnumResourceLanguagesW( HMODULE hmodule, LPCWSTR type,
1076                                         LPCWSTR name, ENUMRESLANGPROCW lpfun,
1077                                         LONG lParam)
1078 {
1079         /* FIXME: move WINE_MODREF stuff here */
1080     return PE_EnumResourceLanguagesW(hmodule,type,name,lpfun,lParam);
1081 }