2 * 16-bit resource functions
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1997 Alex Korobka
6 * Copyright 1998 Ulrich Weigand
7 * Copyright 1995, 2003 Alexandre Julliard
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/port.h"
30 #include <sys/types.h>
35 #include "wine/winbase16.h"
36 #include "wine/winuser16.h"
37 #include "wine/unicode.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(resource);
44 /* handle conversions */
45 #define HRSRC_32(h16) ((HRSRC)(ULONG_PTR)(h16))
46 #define HGLOBAL_32(h16) ((HGLOBAL)(ULONG_PTR)(h16))
48 static inline NE_MODULE *get_module( HMODULE16 mod )
50 if (!mod) mod = TASK_GetCurrent()->hModule;
51 return NE_GetPtr( mod );
54 #define HRSRC_MAP_BLOCKSIZE 16
56 typedef struct _HRSRC_ELEM
62 typedef struct _HRSRC_MAP
70 /**********************************************************************
73 static HRSRC16 MapHRsrc32To16( NE_MODULE *pModule, HRSRC hRsrc32, WORD type )
75 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
79 /* On first call, initialize HRSRC map */
82 if ( !(map = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HRSRC_MAP) ) ) )
84 ERR("Cannot allocate HRSRC map\n" );
87 pModule->hRsrcMap = map;
90 /* Check whether HRSRC32 already in map */
91 for ( i = 0; i < map->nUsed; i++ )
92 if ( map->elem[i].hRsrc == hRsrc32 )
93 return (HRSRC16)(i + 1);
95 /* If no space left, grow table */
96 if ( map->nUsed == map->nAlloc )
100 newElem = (HRSRC_ELEM *)HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
101 map->elem, (map->nAlloc + HRSRC_MAP_BLOCKSIZE) * sizeof(HRSRC_ELEM) );
103 newElem = (HRSRC_ELEM *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
104 (map->nAlloc + HRSRC_MAP_BLOCKSIZE) * sizeof(HRSRC_ELEM) );
108 ERR("Cannot grow HRSRC map\n" );
112 map->nAlloc += HRSRC_MAP_BLOCKSIZE;
115 /* Add HRSRC32 to table */
116 map->elem[map->nUsed].hRsrc = hRsrc32;
117 map->elem[map->nUsed].type = type;
120 return (HRSRC16)map->nUsed;
123 /**********************************************************************
126 static HRSRC MapHRsrc16To32( NE_MODULE *pModule, HRSRC16 hRsrc16 )
128 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
129 if ( !map || !hRsrc16 || hRsrc16 > map->nUsed ) return 0;
131 return map->elem[hRsrc16-1].hRsrc;
134 /**********************************************************************
137 static WORD MapHRsrc16ToType( NE_MODULE *pModule, HRSRC16 hRsrc16 )
139 HRSRC_MAP *map = (HRSRC_MAP *)pModule->hRsrcMap;
140 if ( !map || !hRsrc16 || hRsrc16 > map->nUsed ) return 0;
142 return map->elem[hRsrc16-1].type;
146 /**********************************************************************
149 * Convert a resource name from '#xxx' form to numerical id.
151 static inline LPCSTR get_res_name( LPCSTR name )
153 if (HIWORD(name) && name[0] == '#') name = (LPCSTR)atoi( name + 1 );
158 /**********************************************************************
161 static inline NE_TYPEINFO *next_typeinfo( NE_TYPEINFO *info )
163 return (NE_TYPEINFO *)((char*)(info + 1) + info->count * sizeof(NE_NAMEINFO));
167 /**********************************************************************
168 * get_default_res_handler
170 static inline FARPROC16 get_default_res_handler(void)
172 static FARPROC16 handler;
174 if (!handler) handler = GetProcAddress16( GetModuleHandle16("KERNEL"), "DefResourceHandler" );
179 /***********************************************************************
182 * Find the type and resource id from their names.
183 * Return value is MAKELONG( typeId, resId ), or 0 if not found.
185 static DWORD NE_FindNameTableId( NE_MODULE *pModule, LPCSTR typeId, LPCSTR resId )
187 NE_TYPEINFO *pTypeInfo = (NE_TYPEINFO *)((char *)pModule + pModule->res_table + 2);
188 NE_NAMEINFO *pNameInfo;
194 for (; pTypeInfo->type_id != 0;
195 pTypeInfo = (NE_TYPEINFO *)((char*)(pTypeInfo+1) +
196 pTypeInfo->count * sizeof(NE_NAMEINFO)))
198 if (pTypeInfo->type_id != 0x800f) continue;
199 pNameInfo = (NE_NAMEINFO *)(pTypeInfo + 1);
200 for (count = pTypeInfo->count; count > 0; count--, pNameInfo++)
202 TRACE("NameTable entry: type=%04x id=%04x\n",
203 pTypeInfo->type_id, pNameInfo->id );
204 handle = LoadResource16( pModule->self,
205 (HRSRC16)((int)pNameInfo - (int)pModule) );
206 for(p = (WORD*)LockResource16(handle); p && *p; p = (WORD *)((char*)p+*p))
208 TRACE(" type=%04x '%s' id=%04x '%s'\n",
209 p[1], (char *)(p+3), p[2],
210 (char *)(p+3)+strlen((char *)(p+3))+1 );
211 /* Check for correct type */
215 if (!HIWORD(typeId)) continue;
216 if (strcasecmp( typeId, (char *)(p + 3) )) continue;
218 else if (HIWORD(typeId) || (((DWORD)typeId & ~0x8000)!= p[1]))
221 /* Now check for the id */
225 if (!HIWORD(resId)) continue;
226 if (strcasecmp( resId, (char*)(p+3)+strlen((char*)(p+3))+1 )) continue;
229 else if (HIWORD(resId) || (((DWORD)resId & ~0x8000) != p[2]))
232 /* If we get here, we've found the entry */
235 ret = MAKELONG( p[1], p[2] );
238 FreeResource16( handle );
246 /***********************************************************************
249 * Find header struct for a particular resource type.
251 static NE_TYPEINFO *NE_FindTypeSection( LPBYTE pResTab, NE_TYPEINFO *pTypeInfo, LPCSTR typeId )
253 /* start from pTypeInfo */
255 if (HIWORD(typeId) != 0) /* Named type */
258 BYTE len = strlen( str );
259 while (pTypeInfo->type_id)
261 if (!(pTypeInfo->type_id & 0x8000))
263 BYTE *p = pResTab + pTypeInfo->type_id;
264 if ((*p == len) && !strncasecmp( p+1, str, len ))
266 TRACE(" Found type '%s'\n", str );
270 TRACE(" Skipping type %04x\n", pTypeInfo->type_id );
271 pTypeInfo = next_typeinfo(pTypeInfo);
274 else /* Numeric type id */
276 WORD id = LOWORD(typeId) | 0x8000;
277 while (pTypeInfo->type_id)
279 if (pTypeInfo->type_id == id)
281 TRACE(" Found type %04x\n", id );
284 TRACE(" Skipping type %04x\n", pTypeInfo->type_id );
285 pTypeInfo = next_typeinfo(pTypeInfo);
292 /***********************************************************************
293 * NE_FindResourceFromType
295 * Find a resource once the type info structure has been found.
297 static NE_NAMEINFO *NE_FindResourceFromType( LPBYTE pResTab, NE_TYPEINFO *pTypeInfo, LPCSTR resId )
301 NE_NAMEINFO *pNameInfo = (NE_NAMEINFO *)(pTypeInfo + 1);
303 if (HIWORD(resId) != 0) /* Named resource */
306 BYTE len = strlen( str );
307 for (count = pTypeInfo->count; count > 0; count--, pNameInfo++)
309 if (pNameInfo->id & 0x8000) continue;
310 p = pResTab + pNameInfo->id;
311 if ((*p == len) && !strncasecmp( p+1, str, len ))
315 else /* Numeric resource id */
317 WORD id = LOWORD(resId) | 0x8000;
318 for (count = pTypeInfo->count; count > 0; count--, pNameInfo++)
319 if (pNameInfo->id == id) return pNameInfo;
325 /***********************************************************************
326 * DefResourceHandler (KERNEL.456)
328 * This is the default LoadProc() function.
330 HGLOBAL16 WINAPI NE_DefResourceHandler( HGLOBAL16 hMemObj, HMODULE16 hModule,
334 NE_MODULE* pModule = NE_GetPtr( hModule );
335 if (pModule && (pModule->flags & NE_FFLAGS_BUILTIN))
338 WORD sizeShift = *(WORD *)((char *)pModule + pModule->res_table);
339 NE_NAMEINFO* pNameInfo = (NE_NAMEINFO*)((char*)pModule + hRsrc);
342 handle = GlobalReAlloc16( hMemObj, pNameInfo->length << sizeShift, 0 );
344 handle = AllocResource16( hModule, hRsrc, 0 );
348 /* NOTE: hRsrcMap points to start of built-in resource data */
349 memcpy( GlobalLock16( handle ),
350 (char *)pModule->hRsrcMap + (pNameInfo->offset << sizeShift),
351 pNameInfo->length << sizeShift );
355 if (pModule && (fd = NE_OpenFile( pModule )) != INVALID_HANDLE_VALUE)
358 WORD sizeShift = *(WORD *)((char *)pModule + pModule->res_table);
359 NE_NAMEINFO* pNameInfo = (NE_NAMEINFO*)((char*)pModule + hRsrc);
361 TRACE("loading, pos=%d, len=%d\n",
362 (int)pNameInfo->offset << sizeShift,
363 (int)pNameInfo->length << sizeShift );
365 handle = GlobalReAlloc16( hMemObj, pNameInfo->length << sizeShift, 0 );
367 handle = AllocResource16( hModule, hRsrc, 0 );
372 SetFilePointer( fd, (int)pNameInfo->offset << sizeShift, NULL, SEEK_SET );
373 ReadFile( fd, GlobalLock16( handle ), (int)pNameInfo->length << sizeShift,
383 /**********************************************************************
384 * SetResourceHandler (KERNEL.67)
386 FARPROC16 WINAPI SetResourceHandler16( HMODULE16 hModule, LPCSTR typeId, FARPROC16 resourceHandler )
389 NE_TYPEINFO *pTypeInfo;
390 FARPROC16 prevHandler = NULL;
391 NE_MODULE *pModule = NE_GetPtr( hModule );
393 if (!pModule || !pModule->res_table) return NULL;
395 pResTab = (LPBYTE)pModule + pModule->res_table;
396 pTypeInfo = (NE_TYPEINFO *)(pResTab + 2);
398 TRACE("module=%04x type=%s\n", hModule, debugstr_a(typeId) );
402 if (!(pTypeInfo = NE_FindTypeSection( pResTab, pTypeInfo, typeId )))
404 memcpy_unaligned( &prevHandler, &pTypeInfo->resloader, sizeof(FARPROC16) );
405 memcpy_unaligned( &pTypeInfo->resloader, &resourceHandler, sizeof(FARPROC16) );
406 pTypeInfo = next_typeinfo(pTypeInfo);
408 if (!prevHandler) prevHandler = get_default_res_handler();
413 /**********************************************************************
414 * ConvertDialog32To16 (KERNEL.615)
416 VOID WINAPI ConvertDialog32To16( LPVOID dialog32, DWORD size, LPVOID dialog16 )
419 WORD nbItems, data, dialogEx;
422 style = *((DWORD *)dialog16)++ = *((DWORD *)p)++;
423 dialogEx = (style == 0xffff0001); /* DIALOGEX resource */
426 *((DWORD *)dialog16)++ = *((DWORD *)p)++; /* helpID */
427 *((DWORD *)dialog16)++ = *((DWORD *)p)++; /* exStyle */
428 style = *((DWORD *)dialog16)++ = *((DWORD *)p)++; /* style */
431 ((DWORD *)p)++; /* exStyle ignored in 16-bit standard dialog */
433 nbItems = *((BYTE *)dialog16)++ = (BYTE)*((WORD *)p)++;
434 *((WORD *)dialog16)++ = *((WORD *)p)++; /* x */
435 *((WORD *)dialog16)++ = *((WORD *)p)++; /* y */
436 *((WORD *)dialog16)++ = *((WORD *)p)++; /* cx */
437 *((WORD *)dialog16)++ = *((WORD *)p)++; /* cy */
439 /* Transfer menu name */
440 switch (*((WORD *)p))
442 case 0x0000: ((WORD *)p)++; *((BYTE *)dialog16)++ = 0; break;
443 case 0xffff: ((WORD *)p)++; *((BYTE *)dialog16)++ = 0xff;
444 *((WORD *)dialog16)++ = *((WORD *)p)++; break;
445 default: WideCharToMultiByte( CP_ACP, 0, (LPWSTR)p, -1, (LPSTR)dialog16, 0x7fffffff, NULL,NULL );
446 ((LPSTR)dialog16) += strlen( (LPSTR)dialog16 ) + 1;
447 ((LPWSTR)p) += strlenW( (LPWSTR)p ) + 1;
451 /* Transfer class name */
452 switch (*((WORD *)p))
454 case 0x0000: ((WORD *)p)++; *((BYTE *)dialog16)++ = 0; break;
455 case 0xffff: ((WORD *)p)++; *((BYTE *)dialog16)++ = 0xff;
456 *((WORD *)dialog16)++ = *((WORD *)p)++; break;
457 default: WideCharToMultiByte( CP_ACP, 0, (LPWSTR)p, -1, (LPSTR)dialog16, 0x7fffffff, NULL,NULL );
458 ((LPSTR)dialog16) += strlen( (LPSTR)dialog16 ) + 1;
459 ((LPWSTR)p) += strlenW( (LPWSTR)p ) + 1;
463 /* Transfer window caption */
464 WideCharToMultiByte( CP_ACP, 0, (LPWSTR)p, -1, (LPSTR)dialog16, 0x7fffffff, NULL,NULL );
465 ((LPSTR)dialog16) += strlen( (LPSTR)dialog16 ) + 1;
466 ((LPWSTR)p) += strlenW( (LPWSTR)p ) + 1;
468 /* Transfer font info */
469 if (style & DS_SETFONT)
471 *((WORD *)dialog16)++ = *((WORD *)p)++; /* pointSize */
474 *((WORD *)dialog16)++ = *((WORD *)p)++; /* weight */
475 *((WORD *)dialog16)++ = *((WORD *)p)++; /* italic */
477 WideCharToMultiByte( CP_ACP, 0, (LPWSTR)p, -1, (LPSTR)dialog16, 0x7fffffff, NULL,NULL ); /* faceName */
478 ((LPSTR)dialog16) += strlen( (LPSTR)dialog16 ) + 1;
479 ((LPWSTR)p) += strlenW( (LPWSTR)p ) + 1;
482 /* Transfer dialog items */
485 /* align on DWORD boundary (32-bit only) */
486 p = (LPVOID)((((int)p) + 3) & ~3);
490 *((DWORD *)dialog16)++ = *((DWORD *)p)++; /* helpID */
491 *((DWORD *)dialog16)++ = *((DWORD *)p)++; /* exStyle */
492 *((DWORD *)dialog16)++ = *((DWORD *)p)++; /* style */
496 style = *((DWORD *)p)++; /* save style */
497 ((DWORD *)p)++; /* ignore exStyle */
500 *((WORD *)dialog16)++ = *((WORD *)p)++; /* x */
501 *((WORD *)dialog16)++ = *((WORD *)p)++; /* y */
502 *((WORD *)dialog16)++ = *((WORD *)p)++; /* cx */
503 *((WORD *)dialog16)++ = *((WORD *)p)++; /* cy */
506 *((DWORD *)dialog16)++ = *((DWORD *)p)++; /* ID */
509 *((WORD *)dialog16)++ = *((WORD *)p)++; /* ID */
510 *((DWORD *)dialog16)++ = style; /* style from above */
513 /* Transfer class name */
514 switch (*((WORD *)p))
516 case 0x0000: ((WORD *)p)++; *((BYTE *)dialog16)++ = 0; break;
517 case 0xffff: ((WORD *)p)++;
518 *((BYTE *)dialog16)++ = (BYTE)*((WORD *)p)++; break;
519 default: WideCharToMultiByte( CP_ACP, 0, (LPWSTR)p, -1, (LPSTR)dialog16, 0x7fffffff, NULL,NULL );
520 ((LPSTR)dialog16) += strlen( (LPSTR)dialog16 ) + 1;
521 ((LPWSTR)p) += strlenW( (LPWSTR)p ) + 1;
525 /* Transfer window name */
526 switch (*((WORD *)p))
528 case 0x0000: ((WORD *)p)++; *((BYTE *)dialog16)++ = 0; break;
529 case 0xffff: ((WORD *)p)++; *((BYTE *)dialog16)++ = 0xff;
530 *((WORD *)dialog16)++ = *((WORD *)p)++; break;
531 default: WideCharToMultiByte( CP_ACP, 0, (LPWSTR)p, -1, (LPSTR)dialog16, 0x7fffffff, NULL,NULL );
532 ((LPSTR)dialog16) += strlen( (LPSTR)dialog16 ) + 1;
533 ((LPWSTR)p) += strlenW( (LPWSTR)p ) + 1;
538 data = *((WORD *)p)++;
540 *((WORD *)dialog16)++ = data;
542 *((BYTE *)dialog16)++ = (BYTE)data;
546 memcpy( dialog16, p, data );
547 (LPSTR)dialog16 += data;
557 /**********************************************************************
558 * GetDialog32Size (KERNEL.618)
560 WORD WINAPI GetDialog32Size16( LPVOID dialog32 )
563 WORD nbItems, data, dialogEx;
566 style = *((DWORD *)p)++;
567 dialogEx = (style == 0xffff0001); /* DIALOGEX resource */
570 ((DWORD *)p)++; /* helpID */
571 ((DWORD *)p)++; /* exStyle */
572 style = *((DWORD *)p)++; /* style */
575 ((DWORD *)p)++; /* exStyle */
577 nbItems = *((WORD *)p)++;
578 ((WORD *)p)++; /* x */
579 ((WORD *)p)++; /* y */
580 ((WORD *)p)++; /* cx */
581 ((WORD *)p)++; /* cy */
584 switch (*((WORD *)p))
586 case 0x0000: ((WORD *)p)++; break;
587 case 0xffff: ((WORD *)p) += 2; break;
588 default: ((LPWSTR)p) += strlenW( (LPWSTR)p ) + 1; break;
591 /* Skip class name */
592 switch (*((WORD *)p))
594 case 0x0000: ((WORD *)p)++; break;
595 case 0xffff: ((WORD *)p) += 2; break;
596 default: ((LPWSTR)p) += strlenW( (LPWSTR)p ) + 1; break;
599 /* Skip window caption */
600 ((LPWSTR)p) += strlenW( (LPWSTR)p ) + 1;
603 if (style & DS_SETFONT)
605 ((WORD *)p)++; /* pointSize */
608 ((WORD *)p)++; /* weight */
609 ((WORD *)p)++; /* italic */
611 ((LPWSTR)p) += strlenW( (LPWSTR)p ) + 1; /* faceName */
614 /* Skip dialog items */
617 /* align on DWORD boundary */
618 p = (LPVOID)((((int)p) + 3) & ~3);
622 ((DWORD *)p)++; /* helpID */
623 ((DWORD *)p)++; /* exStyle */
624 ((DWORD *)p)++; /* style */
628 ((DWORD *)p)++; /* style */
629 ((DWORD *)p)++; /* exStyle */
632 ((WORD *)p)++; /* x */
633 ((WORD *)p)++; /* y */
634 ((WORD *)p)++; /* cx */
635 ((WORD *)p)++; /* cy */
638 ((DWORD *)p)++; /* ID */
640 ((WORD *)p)++; /* ID */
642 /* Skip class name */
643 switch (*((WORD *)p))
645 case 0x0000: ((WORD *)p)++; break;
646 case 0xffff: ((WORD *)p) += 2; break;
647 default: ((LPWSTR)p) += strlenW( (LPWSTR)p ) + 1; break;
650 /* Skip window name */
651 switch (*((WORD *)p))
653 case 0x0000: ((WORD *)p)++; break;
654 case 0xffff: ((WORD *)p) += 2; break;
655 default: ((LPWSTR)p) += strlenW( (LPWSTR)p ) + 1; break;
659 data = *((WORD *)p)++;
666 return (WORD)((LPSTR)p - (LPSTR)dialog32);
670 /**********************************************************************
671 * ConvertMenu32To16 (KERNEL.616)
673 VOID WINAPI ConvertMenu32To16( LPVOID menu32, DWORD size, LPVOID menu16 )
676 WORD version, headersize, flags, level = 1;
678 version = *((WORD *)menu16)++ = *((WORD *)p)++;
679 headersize = *((WORD *)menu16)++ = *((WORD *)p)++;
682 memcpy( menu16, p, headersize );
683 ((LPSTR)menu16) += headersize;
684 ((LPSTR)p) += headersize;
688 if ( version == 0 ) /* standard */
690 flags = *((WORD *)menu16)++ = *((WORD *)p)++;
691 if ( !(flags & MF_POPUP) )
692 *((WORD *)menu16)++ = *((WORD *)p)++; /* ID */
696 WideCharToMultiByte( CP_ACP, 0, (LPWSTR)p, -1, (LPSTR)menu16, 0x7fffffff, NULL,NULL );
697 ((LPSTR)menu16) += strlen( (LPSTR)menu16 ) + 1;
698 ((LPWSTR)p) += strlenW( (LPWSTR)p ) + 1;
700 if ( flags & MF_END )
705 *((DWORD *)menu16)++ = *((DWORD *)p)++; /* fType */
706 *((DWORD *)menu16)++ = *((DWORD *)p)++; /* fState */
707 *((WORD *)menu16)++ = (WORD)*((DWORD *)p)++; /* ID */
708 flags = *((BYTE *)menu16)++ = (BYTE)*((WORD *)p)++;
710 WideCharToMultiByte( CP_ACP, 0, (LPWSTR)p, -1, (LPSTR)menu16, 0x7fffffff, NULL,NULL );
711 ((LPSTR)menu16) += strlen( (LPSTR)menu16 ) + 1;
712 ((LPWSTR)p) += strlenW( (LPWSTR)p ) + 1;
714 /* align on DWORD boundary (32-bit only) */
715 p = (LPVOID)((((int)p) + 3) & ~3);
717 /* If popup, transfer helpid */
720 *((DWORD *)menu16)++ = *((DWORD *)p)++;
724 if ( flags & MF_END )
730 /**********************************************************************
731 * GetMenu32Size (KERNEL.617)
733 WORD WINAPI GetMenu32Size16( LPVOID menu32 )
736 WORD version, headersize, flags, level = 1;
738 version = *((WORD *)p)++;
739 headersize = *((WORD *)p)++;
740 ((LPSTR)p) += headersize;
743 if ( version == 0 ) /* standard */
745 flags = *((WORD *)p)++;
746 if ( !(flags & MF_POPUP) )
747 ((WORD *)p)++; /* ID */
751 ((LPWSTR)p) += strlenW( (LPWSTR)p ) + 1;
753 if ( flags & MF_END )
758 ((DWORD *)p)++; /* fType */
759 ((DWORD *)p)++; /* fState */
760 ((DWORD *)p)++; /* ID */
761 flags = *((WORD *)p)++;
763 ((LPWSTR)p) += strlenW( (LPWSTR)p ) + 1;
765 /* align on DWORD boundary (32-bit only) */
766 p = (LPVOID)((((int)p) + 3) & ~3);
768 /* If popup, skip helpid */
775 if ( flags & MF_END )
779 return (WORD)((LPSTR)p - (LPSTR)menu32);
783 /**********************************************************************
784 * ConvertAccelerator32To16
786 static void ConvertAccelerator32To16( LPVOID acc32, DWORD size, LPVOID acc16 )
793 type = *((BYTE *)acc16)++ = *((BYTE *)acc32)++;
796 /* Copy event and IDval */
797 *((WORD *)acc16)++ = *((WORD *)acc32)++;
798 *((WORD *)acc16)++ = *((WORD *)acc32)++;
802 } while ( !( type & 0x80 ) );
806 /**********************************************************************
809 static HGLOBAL16 NE_LoadPEResource( NE_MODULE *pModule, WORD type, LPVOID bits, DWORD size )
813 TRACE("module=%04x type=%04x\n", pModule->self, type );
815 handle = GlobalAlloc16( 0, size );
820 ConvertMenu32To16( bits, size, GlobalLock16( handle ) );
823 ConvertDialog32To16( bits, size, GlobalLock16( handle ) );
826 ConvertAccelerator32To16( bits, size, GlobalLock16( handle ) );
829 FIXME("not yet implemented!\n" );
832 memcpy( GlobalLock16( handle ), bits, size );
839 /**********************************************************************
840 * AllocResource (KERNEL.66)
842 HGLOBAL16 WINAPI AllocResource16( HMODULE16 hModule, HRSRC16 hRsrc, DWORD size)
844 NE_NAMEINFO *pNameInfo=NULL;
848 NE_MODULE *pModule = NE_GetPtr( hModule );
849 if (!pModule || !pModule->res_table || !hRsrc) return 0;
851 TRACE("module=%04x res=%04x size=%ld\n", hModule, hRsrc, size );
853 sizeShift = *(WORD *)((char *)pModule + pModule->res_table);
854 pNameInfo = (NE_NAMEINFO*)((char*)pModule + hRsrc);
855 if (size < (DWORD)pNameInfo->length << sizeShift)
856 size = (DWORD)pNameInfo->length << sizeShift;
857 ret = GlobalAlloc16( GMEM_FIXED, size );
858 if (ret) FarSetOwner16( ret, hModule );
863 /**********************************************************************
864 * DirectResAlloc (KERNEL.168)
866 * Check Schulman, p. 232 for details
868 HGLOBAL16 WINAPI DirectResAlloc16( HINSTANCE16 hInstance, WORD wType,
872 TRACE("(%04x,%04x,%04x)\n", hInstance, wType, wSize );
873 if (!(hInstance = GetExePtr( hInstance ))) return 0;
874 if(wType != 0x10) /* 0x10 is the only observed value, passed from
875 CreateCursorIndirect. */
876 TRACE("(wType=%x)\n", wType);
877 ret = GlobalAlloc16( GMEM_MOVEABLE, wSize );
878 if (ret) FarSetOwner16( ret, hInstance );
883 /**********************************************************************
884 * AccessResource (KERNEL.64)
886 INT16 WINAPI AccessResource16( HINSTANCE16 hModule, HRSRC16 hRsrc )
889 NE_MODULE *pModule = NE_GetPtr( hModule );
891 if (!pModule || !pModule->res_table || !hRsrc) return -1;
893 TRACE("module=%04x res=%04x\n", pModule->self, hRsrc );
895 if ((fd = _lopen16( NE_MODULE_NAME(pModule), OF_READ )) != HFILE_ERROR16)
897 WORD sizeShift = *(WORD *)((char *)pModule + pModule->res_table);
898 NE_NAMEINFO *pNameInfo = (NE_NAMEINFO*)((char*)pModule + hRsrc);
899 _llseek16( fd, (int)pNameInfo->offset << sizeShift, SEEK_SET );
905 /**********************************************************************
906 * FindResource (KERNEL.60)
908 HRSRC16 WINAPI FindResource16( HMODULE16 hModule, LPCSTR name, LPCSTR type )
910 NE_TYPEINFO *pTypeInfo;
911 NE_NAMEINFO *pNameInfo;
913 NE_MODULE *pModule = get_module( hModule );
915 if (!pModule) return 0;
917 if (pModule->module32)
919 /* 32-bit PE module */
920 HRSRC hRsrc32 = FindResourceA( pModule->module32, name, type );
921 return MapHRsrc32To16( pModule, hRsrc32, HIWORD(type) ? 0 : LOWORD(type) );
924 TRACE("module=%04x name=%s type=%s\n", pModule->self, debugstr_a(name), debugstr_a(type) );
926 if (!pModule->res_table) return 0;
928 type = get_res_name( type );
929 name = get_res_name( name );
931 if (HIWORD(type) || HIWORD(name))
933 DWORD id = NE_FindNameTableId( pModule, type, name );
936 type = (LPCSTR)(ULONG_PTR)LOWORD(id);
937 name = (LPCSTR)(ULONG_PTR)HIWORD(id);
940 pResTab = (LPBYTE)pModule + pModule->res_table;
941 pTypeInfo = (NE_TYPEINFO *)( pResTab + 2 );
945 if (!(pTypeInfo = NE_FindTypeSection( pResTab, pTypeInfo, type ))) break;
946 if ((pNameInfo = NE_FindResourceFromType( pResTab, pTypeInfo, name )))
948 TRACE(" Found id %08lx\n", (DWORD)name );
949 return (HRSRC16)( (char *)pNameInfo - (char *)pModule );
951 pTypeInfo = next_typeinfo(pTypeInfo);
957 /**********************************************************************
958 * LoadResource (KERNEL.61)
960 HGLOBAL16 WINAPI LoadResource16( HMODULE16 hModule, HRSRC16 hRsrc )
962 NE_TYPEINFO *pTypeInfo;
963 NE_NAMEINFO *pNameInfo = NULL;
964 NE_MODULE *pModule = get_module( hModule );
967 if (!hRsrc || !pModule) return 0;
969 if (pModule->module32)
971 /* load 32-bit resource and convert it */
972 HRSRC hRsrc32 = MapHRsrc16To32( pModule, hRsrc );
973 WORD type = MapHRsrc16ToType( pModule, hRsrc );
974 HGLOBAL hMem = LoadResource( pModule->module32, hRsrc32 );
975 DWORD size = SizeofResource( pModule->module32, hRsrc32 );
977 return NE_LoadPEResource( pModule, type, LockResource( hMem ), size );
980 /* first, verify hRsrc (just an offset from pModule to the needed pNameInfo) */
982 d = pModule->res_table + 2;
983 pTypeInfo = (NE_TYPEINFO *)((char *)pModule + d);
986 if (pTypeInfo->type_id == 0) break; /* terminal entry */
987 d += sizeof(NE_TYPEINFO) + pTypeInfo->count * sizeof(NE_NAMEINFO);
990 if( ((d - hRsrc)%sizeof(NE_NAMEINFO)) == 0 )
992 pNameInfo = (NE_NAMEINFO *)((char *)pModule + hRsrc);
995 else break; /* NE_NAMEINFO boundary mismatch */
997 pTypeInfo = (NE_TYPEINFO *)((char *)pModule + d);
1002 if (pNameInfo->handle && !(GlobalFlags16(pNameInfo->handle) & GMEM_DISCARDED))
1005 TRACE(" Already loaded, new count=%d\n", pNameInfo->usage );
1009 FARPROC16 resloader;
1010 memcpy_unaligned( &resloader, &pTypeInfo->resloader, sizeof(FARPROC16) );
1011 if (resloader && resloader != get_default_res_handler())
1016 args[2] = pNameInfo->handle;
1017 args[1] = pModule->self;
1019 WOWCallback16Ex( (DWORD)resloader, WCB16_PASCAL, sizeof(args), args, &ret );
1020 pNameInfo->handle = LOWORD(ret);
1023 pNameInfo->handle = NE_DefResourceHandler( pNameInfo->handle, pModule->self, hRsrc );
1025 if (pNameInfo->handle)
1028 pNameInfo->flags |= NE_SEGFLAGS_LOADED;
1031 return pNameInfo->handle;
1037 /**********************************************************************
1038 * LockResource (KERNEL.62)
1040 SEGPTR WINAPI WIN16_LockResource16( HGLOBAL16 handle )
1042 TRACE("(%04x)\n", handle );
1043 /* May need to reload the resource if discarded */
1044 return K32WOWGlobalLock16( handle );
1048 /**********************************************************************
1049 * LockResource16 (KERNEL32.@)
1051 LPVOID WINAPI LockResource16( HGLOBAL16 handle )
1053 return MapSL( WIN16_LockResource16(handle) );
1057 /**********************************************************************
1058 * SizeofResource (KERNEL.65)
1060 DWORD WINAPI SizeofResource16( HMODULE16 hModule, HRSRC16 hRsrc )
1062 NE_MODULE *pModule = NE_GetPtr( hModule );
1064 TRACE("(%x, %x)\n", hModule, hRsrc );
1066 if (!hRsrc) return 0;
1067 if (!(pModule = get_module( hModule ))) return 0;
1068 if (pModule->res_table)
1070 WORD sizeShift = *(WORD *)((char *)pModule + pModule->res_table);
1071 NE_NAMEINFO *pNameInfo = (NE_NAMEINFO*)((char*)pModule + hRsrc);
1072 return (DWORD)pNameInfo->length << sizeShift;
1074 if (pModule->module32)
1076 /* 32-bit PE module */
1077 return SizeofResource( pModule->module32, MapHRsrc16To32( pModule, hRsrc ) );
1083 typedef WORD (WINAPI *pDestroyIcon32Proc)( HGLOBAL16 handle, UINT16 flags );
1085 /**********************************************************************
1086 * FreeResource (KERNEL.63)
1088 BOOL16 WINAPI FreeResource16( HGLOBAL16 handle )
1090 pDestroyIcon32Proc proc;
1092 NE_MODULE *pModule = NE_GetPtr( FarGetOwner16( handle ) );
1094 TRACE("(%04x)\n", handle );
1096 /* Try NE resource first */
1098 if (pModule && pModule->res_table)
1100 NE_TYPEINFO *pTypeInfo = (NE_TYPEINFO *)((char *)pModule + pModule->res_table + 2);
1101 while (pTypeInfo->type_id)
1104 NE_NAMEINFO *pNameInfo = (NE_NAMEINFO *)(pTypeInfo + 1);
1105 for (count = pTypeInfo->count; count > 0; count--)
1107 if (pNameInfo->handle == handle)
1109 if (pNameInfo->usage > 0) pNameInfo->usage--;
1110 if (pNameInfo->usage == 0)
1112 GlobalFree16( pNameInfo->handle );
1113 pNameInfo->handle = 0;
1114 pNameInfo->flags &= ~NE_SEGFLAGS_LOADED;
1120 pTypeInfo = (NE_TYPEINFO *)pNameInfo;
1124 /* If this failed, call USER.DestroyIcon32; this will check
1125 whether it is a shared cursor/icon; if not it will call
1127 user = GetModuleHandleA( "user32.dll" );
1128 if (user && (proc = (pDestroyIcon32Proc)GetProcAddress( user, "DestroyIcon32" )))
1129 return proc( handle, 1 /*CID_RESOURCE*/ );
1131 return GlobalFree16( handle );