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