nsiproxy.sys: Implement change notifications for NSI_IP_UNICAST_TABLE.
[wine.git] / dlls / krnl386.exe16 / resource.c
blobec6440d6a66524974cf36f77d4f3df271591c59e
1 /*
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wownt32.h"
33 #include "wine/winbase16.h"
34 #include "wine/winuser16.h"
35 #include "kernel16_private.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(resource);
40 /* handle conversions */
41 #define HRSRC_32(h16) ((HRSRC)(ULONG_PTR)(h16))
42 #define HGLOBAL_32(h16) ((HGLOBAL)(ULONG_PTR)(h16))
44 static inline NE_MODULE *get_module( HMODULE16 mod )
46 if (!mod) mod = TASK_GetCurrent()->hModule;
47 return NE_GetPtr( mod );
50 #define HRSRC_MAP_BLOCKSIZE 16
52 typedef struct _HRSRC_ELEM
54 HRSRC hRsrc;
55 WORD type;
56 } HRSRC_ELEM;
58 typedef struct _HRSRC_MAP
60 int nAlloc;
61 int nUsed;
62 HRSRC_ELEM *elem;
63 } HRSRC_MAP;
66 /**********************************************************************
67 * MapHRsrc32To16
69 static HRSRC16 MapHRsrc32To16( NE_MODULE *pModule, HRSRC hRsrc32, WORD type )
71 HRSRC_MAP *map = pModule->rsrc32_map;
72 HRSRC_ELEM *newElem;
73 int i;
75 /* On first call, initialize HRSRC map */
76 if ( !map )
78 if ( !(map = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HRSRC_MAP) ) ) )
80 ERR("Cannot allocate HRSRC map\n" );
81 return 0;
83 pModule->rsrc32_map = map;
86 /* Check whether HRSRC32 already in map */
87 for ( i = 0; i < map->nUsed; i++ )
88 if ( map->elem[i].hRsrc == hRsrc32 )
89 return (HRSRC16)(i + 1);
91 /* If no space left, grow table */
92 if ( map->nUsed == map->nAlloc )
95 if (map->elem)
96 newElem = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
97 map->elem, (map->nAlloc + HRSRC_MAP_BLOCKSIZE) * sizeof(HRSRC_ELEM) );
98 else
99 newElem = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
100 (map->nAlloc + HRSRC_MAP_BLOCKSIZE) * sizeof(HRSRC_ELEM) );
102 if ( !newElem )
104 ERR("Cannot grow HRSRC map\n" );
105 return 0;
107 map->elem = newElem;
108 map->nAlloc += HRSRC_MAP_BLOCKSIZE;
111 /* Add HRSRC32 to table */
112 map->elem[map->nUsed].hRsrc = hRsrc32;
113 map->elem[map->nUsed].type = type;
114 map->nUsed++;
116 return (HRSRC16)map->nUsed;
119 /**********************************************************************
120 * MapHRsrc16To32
122 static HRSRC MapHRsrc16To32( NE_MODULE *pModule, HRSRC16 hRsrc16 )
124 HRSRC_MAP *map = pModule->rsrc32_map;
125 if ( !map || !hRsrc16 || hRsrc16 > map->nUsed ) return 0;
127 return map->elem[hRsrc16-1].hRsrc;
130 /**********************************************************************
131 * MapHRsrc16ToType
133 static WORD MapHRsrc16ToType( NE_MODULE *pModule, HRSRC16 hRsrc16 )
135 HRSRC_MAP *map = pModule->rsrc32_map;
136 if ( !map || !hRsrc16 || hRsrc16 > map->nUsed ) return 0;
138 return map->elem[hRsrc16-1].type;
142 /**********************************************************************
143 * get_res_name
145 * Convert a resource name from '#xxx' form to numerical id.
147 static inline LPCSTR get_res_name( LPCSTR name )
149 if (HIWORD(name) && name[0] == '#') name = MAKEINTRESOURCEA( atoi( name + 1 ) );
150 return name;
154 /**********************************************************************
155 * next_typeinfo
157 static inline NE_TYPEINFO *next_typeinfo( NE_TYPEINFO *info )
159 return (NE_TYPEINFO *)((char*)(info + 1) + info->count * sizeof(NE_NAMEINFO));
163 /**********************************************************************
164 * get_default_res_handler
166 static inline FARPROC16 get_default_res_handler(void)
168 static FARPROC16 handler;
170 if (!handler) handler = GetProcAddress16( GetModuleHandle16("KERNEL"), "DefResourceHandler" );
171 return handler;
175 /***********************************************************************
176 * NE_FindNameTableId
178 * Find the type and resource id from their names.
179 * Return value is MAKELONG( typeId, resId ), or 0 if not found.
181 static DWORD NE_FindNameTableId( NE_MODULE *pModule, LPCSTR typeId, LPCSTR resId )
183 NE_TYPEINFO *pTypeInfo = (NE_TYPEINFO *)((char *)pModule + pModule->ne_rsrctab + 2);
184 NE_NAMEINFO *pNameInfo;
185 HGLOBAL16 handle;
186 WORD *p;
187 DWORD ret = 0;
188 int count;
190 for (; pTypeInfo->type_id != 0;
191 pTypeInfo = (NE_TYPEINFO *)((char*)(pTypeInfo+1) +
192 pTypeInfo->count * sizeof(NE_NAMEINFO)))
194 if (pTypeInfo->type_id != 0x800f) continue;
195 pNameInfo = (NE_NAMEINFO *)(pTypeInfo + 1);
196 for (count = pTypeInfo->count; count > 0; count--, pNameInfo++)
198 TRACE("NameTable entry: type=%04x id=%04x\n",
199 pTypeInfo->type_id, pNameInfo->id );
200 handle = LoadResource16( pModule->self,
201 (HRSRC16)((char *)pNameInfo - (char *)pModule) );
202 for(p = LockResource16(handle); p && *p; p = (WORD *)((char*)p+*p))
204 TRACE(" type=%04x '%s' id=%04x '%s'\n",
205 p[1], (char *)(p+3), p[2],
206 (char *)(p+3)+strlen((char *)(p+3))+1 );
207 /* Check for correct type */
209 if (p[1] & 0x8000)
211 if (!HIWORD(typeId)) continue;
212 if (stricmp( typeId, (char *)(p + 3) )) continue;
214 else if (HIWORD(typeId) || (((DWORD)typeId & ~0x8000)!= p[1]))
215 continue;
217 /* Now check for the id */
219 if (p[2] & 0x8000)
221 if (!HIWORD(resId)) continue;
222 if (stricmp( resId, (char*)(p+3)+strlen((char*)(p+3))+1 )) continue;
225 else if (HIWORD(resId) || ((LOWORD(resId) & ~0x8000) != p[2]))
226 continue;
228 /* If we get here, we've found the entry */
230 TRACE(" Found!\n" );
231 ret = MAKELONG( p[1], p[2] );
232 break;
234 FreeResource16( handle );
235 if (ret) return ret;
238 return 0;
242 /***********************************************************************
243 * NE_FindTypeSection
245 * Find header struct for a particular resource type.
247 static NE_TYPEINFO *NE_FindTypeSection( LPBYTE pResTab, NE_TYPEINFO *pTypeInfo, LPCSTR typeId )
249 /* start from pTypeInfo */
251 if (HIWORD(typeId) != 0) /* Named type */
253 LPCSTR str = typeId;
254 BYTE len = strlen( str );
255 while (pTypeInfo->type_id)
257 if (!(pTypeInfo->type_id & 0x8000))
259 BYTE *p = pResTab + pTypeInfo->type_id;
260 if ((*p == len) && !_strnicmp( (char*)p+1, str, len ))
262 TRACE(" Found type '%s'\n", str );
263 return pTypeInfo;
266 TRACE(" Skipping type %04x\n", pTypeInfo->type_id );
267 pTypeInfo = next_typeinfo(pTypeInfo);
270 else /* Numeric type id */
272 WORD id = LOWORD(typeId) | 0x8000;
273 while (pTypeInfo->type_id)
275 if (pTypeInfo->type_id == id)
277 TRACE(" Found type %04x\n", id );
278 return pTypeInfo;
280 TRACE(" Skipping type %04x\n", pTypeInfo->type_id );
281 pTypeInfo = next_typeinfo(pTypeInfo);
284 return NULL;
288 /***********************************************************************
289 * NE_FindResourceFromType
291 * Find a resource once the type info structure has been found.
293 static NE_NAMEINFO *NE_FindResourceFromType( LPBYTE pResTab, NE_TYPEINFO *pTypeInfo, LPCSTR resId )
295 BYTE *p;
296 int count;
297 NE_NAMEINFO *pNameInfo = (NE_NAMEINFO *)(pTypeInfo + 1);
299 if (HIWORD(resId) != 0) /* Named resource */
301 LPCSTR str = resId;
302 BYTE len = strlen( str );
303 for (count = pTypeInfo->count; count > 0; count--, pNameInfo++)
305 if (pNameInfo->id & 0x8000) continue;
306 p = pResTab + pNameInfo->id;
307 if ((*p == len) && !_strnicmp( (char*)p+1, str, len ))
308 return pNameInfo;
311 else /* Numeric resource id */
313 WORD id = LOWORD(resId) | 0x8000;
314 for (count = pTypeInfo->count; count > 0; count--, pNameInfo++)
315 if (pNameInfo->id == id) return pNameInfo;
317 return NULL;
321 /***********************************************************************
322 * DefResourceHandler (KERNEL.456)
324 * This is the default LoadProc() function.
326 HGLOBAL16 WINAPI NE_DefResourceHandler( HGLOBAL16 hMemObj, HMODULE16 hModule,
327 HRSRC16 hRsrc )
329 HGLOBAL16 handle;
330 WORD sizeShift;
331 NE_NAMEINFO* pNameInfo;
332 NE_MODULE* pModule = NE_GetPtr( hModule );
334 if (!pModule) return 0;
336 sizeShift = *(WORD *)((char *)pModule + pModule->ne_rsrctab);
337 pNameInfo = (NE_NAMEINFO *)((char *)pModule + hRsrc);
339 if ( hMemObj )
340 handle = GlobalReAlloc16( hMemObj, pNameInfo->length << sizeShift, 0 );
341 else
342 handle = AllocResource16( hModule, hRsrc, 0 );
344 if (handle)
346 if (!NE_READ_DATA( pModule, GlobalLock16( handle ),
347 (int)pNameInfo->offset << sizeShift,
348 (int)pNameInfo->length << sizeShift ))
350 GlobalFree16( handle );
351 handle = 0;
354 return handle;
358 /**********************************************************************
359 * SetResourceHandler (KERNEL.67)
361 FARPROC16 WINAPI SetResourceHandler16( HMODULE16 hModule, LPCSTR typeId, FARPROC16 resourceHandler )
363 LPBYTE pResTab;
364 NE_TYPEINFO *pTypeInfo;
365 FARPROC16 prevHandler = NULL;
366 NE_MODULE *pModule = NE_GetPtr( hModule );
368 if (!pModule || !pModule->ne_rsrctab) return NULL;
370 pResTab = (LPBYTE)pModule + pModule->ne_rsrctab;
371 pTypeInfo = (NE_TYPEINFO *)(pResTab + 2);
373 TRACE("module=%04x type=%s\n", hModule, debugstr_a(typeId) );
375 for (;;)
377 if (!(pTypeInfo = NE_FindTypeSection( pResTab, pTypeInfo, typeId )))
378 break;
379 prevHandler = pTypeInfo->resloader;
380 pTypeInfo->resloader = resourceHandler;
381 pTypeInfo = next_typeinfo(pTypeInfo);
383 if (!prevHandler) prevHandler = get_default_res_handler();
384 return prevHandler;
387 static inline DWORD get_dword(LPCVOID *p)
389 DWORD ret = *(const DWORD *)*p;
390 *p = (const DWORD *)*p + 1;
391 return ret;
394 static inline void put_dword(LPVOID *p, DWORD d)
396 *(DWORD*)*p = d;
397 *p = (DWORD *)*p + 1;
400 static inline WORD get_word(LPCVOID *p)
402 WORD ret = *(const WORD *)*p;
403 *p = (const WORD *)*p + 1;
404 return ret;
407 static inline void put_word(LPVOID *p, WORD w)
409 *(WORD*)*p = w;
410 *p = (WORD *)*p + 1;
413 static inline BYTE get_byte(LPCVOID *p)
415 BYTE ret = *(const BYTE *)*p;
416 *p = (const BYTE *)*p + 1;
417 return ret;
420 static inline void put_byte(LPVOID *p, BYTE b)
422 *(BYTE *)*p = b;
423 *p = (BYTE *)*p + 1;
426 /* convert a resource name */
427 static void convert_name( LPVOID *dst, LPCVOID *src )
429 int len;
430 switch (*(const WORD *)*src)
432 case 0x0000:
433 get_word( src );
434 put_byte( dst, 0 );
435 break;
436 case 0xffff:
437 get_word( src );
438 put_byte( dst, 0xff );
439 put_word( dst, get_word(src) );
440 break;
441 default:
442 len = WideCharToMultiByte( CP_ACP, 0, *src, -1, *dst, 0x7fffffff, NULL,NULL );
443 *dst = (char *)*dst + len;
444 *src = (LPCWSTR)*src + lstrlenW( *src ) + 1;
445 break;
449 /**********************************************************************
450 * ConvertDialog32To16 (KERNEL.615)
452 VOID WINAPI ConvertDialog32To16( LPCVOID dialog32, DWORD size, LPVOID dialog16 )
454 WORD nbItems, data, dialogEx;
455 DWORD style;
457 style = get_dword( &dialog32 );
458 put_dword( &dialog16, style );
459 dialogEx = (style == 0xffff0001); /* DIALOGEX resource */
460 if (dialogEx)
462 put_dword( &dialog16, get_dword( &dialog32 ) ); /* helpID */
463 put_dword( &dialog16, get_dword( &dialog32 ) ); /* exStyle */
464 style = get_dword( &dialog32 );
465 put_dword( &dialog16, style ); /* style */
467 else
468 dialog32 = (const DWORD *)dialog32 + 1; /* exStyle ignored in 16-bit standard dialog */
470 nbItems = get_word( &dialog32 );
471 put_byte( &dialog16, nbItems );
472 put_word( &dialog16, get_word( &dialog32 ) ); /* x */
473 put_word( &dialog16, get_word( &dialog32 ) ); /* y */
474 put_word( &dialog16, get_word( &dialog32 ) ); /* cx */
475 put_word( &dialog16, get_word( &dialog32 ) ); /* cy */
477 /* Transfer menu name */
478 convert_name( &dialog16, &dialog32 );
480 /* Transfer class name */
481 convert_name( &dialog16, &dialog32 );
483 /* Transfer window caption */
484 WideCharToMultiByte( CP_ACP, 0, dialog32, -1, dialog16, 0x7fffffff, NULL, NULL );
485 dialog16 = (LPSTR)dialog16 + strlen( dialog16 ) + 1;
486 dialog32 = (LPCWSTR)dialog32 + lstrlenW( dialog32 ) + 1;
488 /* Transfer font info */
489 if (style & DS_SETFONT)
491 put_word( &dialog16, get_word( &dialog32 ) ); /* pointSize */
492 if (dialogEx)
494 put_word( &dialog16, get_word( &dialog32 ) ); /* weight */
495 put_word( &dialog16, get_word( &dialog32 ) ); /* italic */
497 WideCharToMultiByte( CP_ACP, 0, dialog32, -1, dialog16, 0x7fffffff, NULL, NULL ); /* faceName */
498 dialog16 = (LPSTR)dialog16 + strlen( dialog16 ) + 1;
499 dialog32 = (LPCWSTR)dialog32 + lstrlenW( dialog32 ) + 1;
502 /* Transfer dialog items */
503 while (nbItems)
505 /* align on DWORD boundary (32-bit only) */
506 dialog32 = (LPCVOID)(((UINT_PTR)dialog32 + 3) & ~3);
508 if (dialogEx)
510 put_dword( &dialog16, get_dword( &dialog32 ) ); /* helpID */
511 put_dword( &dialog16, get_dword( &dialog32 ) ); /* exStyle */
512 put_dword( &dialog16, get_dword( &dialog32 ) ); /* style */
514 else
516 style = get_dword( &dialog32 ); /* save style */
517 dialog32 = (const DWORD *)dialog32 + 1; /* ignore exStyle */
520 put_word( &dialog16, get_word( &dialog32 ) ); /* x */
521 put_word( &dialog16, get_word( &dialog32 ) ); /* y */
522 put_word( &dialog16, get_word( &dialog32 ) ); /* cx */
523 put_word( &dialog16, get_word( &dialog32 ) ); /* cy */
525 if (dialogEx)
526 put_dword( &dialog16, get_dword( &dialog32 ) ); /* ID */
527 else
529 put_word( &dialog16, get_word( &dialog32 ) ); /* ID */
530 put_dword( &dialog16, style ); /* style from above */
533 /* Transfer class name */
534 switch (*(const WORD *)dialog32)
536 case 0x0000:
537 get_word( &dialog32 );
538 put_byte( &dialog16, 0 );
539 break;
540 case 0xffff:
541 get_word( &dialog32 );
542 put_byte( &dialog16, get_word( &dialog32 ) );
543 break;
544 default:
545 WideCharToMultiByte( CP_ACP, 0, dialog32, -1, dialog16, 0x7fffffff, NULL, NULL );
546 dialog16 = (LPSTR)dialog16 + strlen( dialog16 ) + 1;
547 dialog32 = (LPCWSTR)dialog32 + lstrlenW( dialog32 ) + 1;
548 break;
551 /* Transfer window name */
552 convert_name( &dialog16, &dialog32 );
554 /* Transfer data */
555 data = get_word( &dialog32 );
556 if (dialogEx)
557 put_word(&dialog16, data);
558 else
559 put_byte(&dialog16,(BYTE)data);
561 if (data)
563 memcpy( dialog16, dialog32, data );
564 dialog16 = (BYTE *)dialog16 + data;
565 dialog32 = (const BYTE *)dialog32 + data;
568 /* Next item */
569 nbItems--;
574 /**********************************************************************
575 * GetDialog32Size (KERNEL.618)
577 WORD WINAPI GetDialog32Size16( LPCVOID dialog32 )
579 LPCVOID p = dialog32;
580 WORD nbItems, data, dialogEx;
581 DWORD style;
583 style = get_dword(&p);
584 dialogEx = (style == 0xffff0001); /* DIALOGEX resource */
585 if (dialogEx)
587 p = (const DWORD *)p + 1; /* helpID */
588 p = (const DWORD *)p + 1; /* exStyle */
589 style = get_dword(&p); /* style */
591 else
592 p = (const DWORD *)p + 1; /* exStyle */
594 nbItems = get_word(&p);
595 p = (const WORD *)p + 1; /* x */
596 p = (const WORD *)p + 1; /* y */
597 p = (const WORD *)p + 1; /* cx */
598 p = (const WORD *)p + 1; /* cy */
600 /* Skip menu name */
601 switch (*(const WORD *)p)
603 case 0x0000: p = (const WORD *)p + 1; break;
604 case 0xffff: p = (const WORD *)p + 2; break;
605 default: p = (LPCWSTR)p + lstrlenW( p ) + 1; break;
608 /* Skip class name */
609 switch (*(const WORD *)p)
611 case 0x0000: p = (const WORD *)p + 1; break;
612 case 0xffff: p = (const WORD *)p + 2; break;
613 default: p = (LPCWSTR)p + lstrlenW( p ) + 1; break;
616 /* Skip window caption */
617 p = (LPCWSTR)p + lstrlenW( p ) + 1;
619 /* Skip font info */
620 if (style & DS_SETFONT)
622 p = (const WORD *)p + 1; /* pointSize */
623 if (dialogEx)
625 p = (const WORD *)p + 1; /* weight */
626 p = (const WORD *)p + 1; /* italic */
628 p = (LPCWSTR)p + lstrlenW( p ) + 1; /* faceName */
631 /* Skip dialog items */
632 while (nbItems)
634 /* align on DWORD boundary */
635 p = (LPCVOID)(((UINT_PTR)p + 3) & ~3);
637 if (dialogEx)
639 p = (const DWORD *)p + 1; /* helpID */
640 p = (const DWORD *)p + 1; /* exStyle */
641 p = (const DWORD *)p + 1; /* style */
643 else
645 p = (const DWORD *)p + 1; /* style */
646 p = (const DWORD *)p + 1; /* exStyle */
649 p = (const WORD *)p + 1; /* x */
650 p = (const WORD *)p + 1; /* y */
651 p = (const WORD *)p + 1; /* cx */
652 p = (const WORD *)p + 1; /* cy */
654 if (dialogEx)
655 p = (const DWORD *)p + 1; /* ID */
656 else
657 p = (const WORD *)p + 1; /* ID */
659 /* Skip class name */
660 switch (*(const WORD *)p)
662 case 0x0000: p = (const WORD *)p + 1; break;
663 case 0xffff: p = (const WORD *)p + 2; break;
664 default: p = (LPCWSTR)p + lstrlenW( p ) + 1; break;
667 /* Skip window name */
668 switch (*(const WORD *)p)
670 case 0x0000: p = (const WORD *)p + 1; break;
671 case 0xffff: p = (const WORD *)p + 2; break;
672 default: p = (LPCWSTR)p + lstrlenW( p ) + 1; break;
675 /* Skip data */
676 data = get_word(&p);
677 p = (const BYTE *)p + data;
679 /* Next item */
680 nbItems--;
683 return (WORD)((LPCSTR)p - (LPCSTR)dialog32);
687 /**********************************************************************
688 * ConvertMenu32To16 (KERNEL.616)
690 VOID WINAPI ConvertMenu32To16( LPCVOID menu32, DWORD size, LPVOID menu16 )
692 WORD version, headersize, flags, level = 1;
694 version = get_word( &menu32 );
695 headersize = get_word( &menu32 );
696 put_word( &menu16, version );
697 put_word( &menu16, headersize );
698 if ( headersize )
700 memcpy( menu16, menu32, headersize );
701 menu16 = (BYTE *)menu16 + headersize;
702 menu32 = (const BYTE *)menu32 + headersize;
705 while ( level )
706 if ( version == 0 ) /* standard */
708 flags = get_word( &menu32 );
709 put_word( &menu16, flags );
710 if ( !(flags & MF_POPUP) )
711 put_word( &menu16, get_word( &menu32 ) ); /* ID */
712 else
713 level++;
715 WideCharToMultiByte( CP_ACP, 0, menu32, -1, menu16, 0x7fffffff, NULL, NULL );
716 menu16 = (LPSTR)menu16 + strlen( menu16 ) + 1;
717 menu32 = (LPCWSTR)menu32 + lstrlenW( menu32 ) + 1;
719 if ( flags & MF_END )
720 level--;
722 else /* extended */
724 put_dword( &menu16, get_dword( &menu32 ) ); /* fType */
725 put_dword( &menu16, get_dword( &menu32 ) ); /* fState */
726 put_word( &menu16, get_dword( &menu32 ) ); /* ID */
727 flags = get_word( &menu32 );
728 put_byte(&menu16,flags);
730 WideCharToMultiByte( CP_ACP, 0, menu32, -1, menu16, 0x7fffffff, NULL, NULL );
731 menu16 = (LPSTR)menu16 + strlen( menu16 ) + 1;
732 menu32 = (LPCWSTR)menu32 + lstrlenW( menu32 ) + 1;
734 /* align on DWORD boundary (32-bit only) */
735 menu32 = (LPCVOID)(((UINT_PTR)menu32 + 3) & ~3);
737 /* If popup, transfer helpid */
738 if ( flags & 1)
740 put_dword( &menu16, get_dword( &menu32 ) );
741 level++;
744 if ( flags & MF_END )
745 level--;
750 /**********************************************************************
751 * GetMenu32Size (KERNEL.617)
753 WORD WINAPI GetMenu32Size16( LPCVOID menu32 )
755 LPCVOID p = menu32;
756 WORD version, headersize, flags, level = 1;
758 version = get_word(&p);
759 headersize = get_word(&p);
760 p = (const BYTE *)p + headersize;
762 while ( level )
763 if ( version == 0 ) /* standard */
765 flags = get_word(&p);
766 if ( !(flags & MF_POPUP) )
767 p = (const WORD *)p + 1; /* ID */
768 else
769 level++;
771 p = (LPCWSTR)p + lstrlenW( p ) + 1;
773 if ( flags & MF_END )
774 level--;
776 else /* extended */
778 p = (const DWORD *)p + 1; /* fType */
779 p = (const DWORD *)p + 1; /* fState */
780 p = (const DWORD *)p + 1; /* ID */
781 flags = get_word(&p);
783 p = (LPCWSTR)p + lstrlenW( p ) + 1;
785 /* align on DWORD boundary (32-bit only) */
786 p = (LPCVOID)(((UINT_PTR)p + 3) & ~3);
788 /* If popup, skip helpid */
789 if ( flags & 1)
791 p = (const DWORD *)p + 1;
792 level++;
795 if ( flags & MF_END )
796 level--;
799 return (WORD)((LPCSTR)p - (LPCSTR)menu32);
803 /**********************************************************************
804 * ConvertAccelerator32To16
806 static void ConvertAccelerator32To16( LPCVOID acc32, DWORD size, LPVOID acc16 )
808 BYTE type;
812 /* Copy type */
813 type = get_byte(&acc32);
814 put_byte(&acc16, type);
815 /* Skip padding */
816 get_byte(&acc32);
817 /* Copy event and IDval */
818 put_word(&acc16, get_word(&acc32));
819 put_word(&acc16, get_word(&acc32));
820 /* Skip padding */
821 get_word(&acc32);
823 } while ( !( type & 0x80 ) );
827 /**********************************************************************
828 * NE_LoadPEResource
830 static HGLOBAL16 NE_LoadPEResource( NE_MODULE *pModule, WORD type, LPCVOID bits, DWORD size )
832 HGLOBAL16 handle;
834 TRACE("module=%04x type=%04x\n", pModule->self, type );
836 handle = GlobalAlloc16( 0, size );
838 switch (type)
840 case RT_MENU:
841 ConvertMenu32To16( bits, size, GlobalLock16( handle ) );
842 break;
843 case RT_DIALOG:
844 ConvertDialog32To16( bits, size, GlobalLock16( handle ) );
845 break;
846 case RT_ACCELERATOR:
847 ConvertAccelerator32To16( bits, size, GlobalLock16( handle ) );
848 break;
849 case RT_STRING:
850 FIXME("not yet implemented!\n" );
851 /* fall through */
852 default:
853 memcpy( GlobalLock16( handle ), bits, size );
854 break;
856 return handle;
860 /**********************************************************************
861 * AllocResource (KERNEL.66)
863 HGLOBAL16 WINAPI AllocResource16( HMODULE16 hModule, HRSRC16 hRsrc, DWORD size)
865 NE_NAMEINFO *pNameInfo=NULL;
866 WORD sizeShift;
867 HGLOBAL16 ret;
869 NE_MODULE *pModule = NE_GetPtr( hModule );
870 if (!pModule || !pModule->ne_rsrctab || !hRsrc) return 0;
872 TRACE("module=%04x res=%04x size=%ld\n", hModule, hRsrc, size );
874 sizeShift = *(WORD *)((char *)pModule + pModule->ne_rsrctab);
875 pNameInfo = (NE_NAMEINFO*)((char*)pModule + hRsrc);
876 if (size < (DWORD)pNameInfo->length << sizeShift)
877 size = (DWORD)pNameInfo->length << sizeShift;
878 ret = GlobalAlloc16( GMEM_FIXED, size );
879 if (ret) FarSetOwner16( ret, hModule );
880 return ret;
884 /**********************************************************************
885 * DirectResAlloc (KERNEL.168)
887 * Check Schulman, p. 232 for details
889 HGLOBAL16 WINAPI DirectResAlloc16( HINSTANCE16 hInstance, WORD wType,
890 UINT16 wSize )
892 HGLOBAL16 ret;
893 TRACE("(%04x,%04x,%04x)\n", hInstance, wType, wSize );
894 if (!(hInstance = GetExePtr( hInstance ))) return 0;
895 if(wType != 0x10) /* 0x10 is the only observed value, passed from
896 CreateCursorIndirect. */
897 TRACE("(wType=%x)\n", wType);
898 ret = GlobalAlloc16( GMEM_MOVEABLE, wSize );
899 if (ret) FarSetOwner16( ret, hInstance );
900 return ret;
904 /**********************************************************************
905 * AccessResource (KERNEL.64)
907 INT16 WINAPI AccessResource16( HINSTANCE16 hModule, HRSRC16 hRsrc )
909 HFILE16 fd;
910 NE_MODULE *pModule = NE_GetPtr( hModule );
912 if (!pModule || !pModule->ne_rsrctab || !hRsrc) return -1;
914 TRACE("module=%04x res=%04x\n", pModule->self, hRsrc );
916 if ((fd = _lopen16( NE_MODULE_NAME(pModule), OF_READ )) != HFILE_ERROR16)
918 WORD sizeShift = *(WORD *)((char *)pModule + pModule->ne_rsrctab);
919 NE_NAMEINFO *pNameInfo = (NE_NAMEINFO*)((char*)pModule + hRsrc);
920 _llseek16( fd, (int)pNameInfo->offset << sizeShift, SEEK_SET );
922 return fd;
926 /**********************************************************************
927 * FindResource (KERNEL.60)
929 HRSRC16 WINAPI FindResource16( HMODULE16 hModule, LPCSTR name, LPCSTR type )
931 NE_TYPEINFO *pTypeInfo;
932 NE_NAMEINFO *pNameInfo;
933 LPBYTE pResTab;
934 NE_MODULE *pModule = get_module( hModule );
936 if (!pModule) return 0;
938 if (pModule->module32)
940 /* 32-bit PE module */
941 HRSRC hRsrc32 = FindResourceA( pModule->module32, name, type );
942 return MapHRsrc32To16( pModule, hRsrc32, HIWORD(type) ? 0 : LOWORD(type) );
945 TRACE("module=%04x name=%s type=%s\n", pModule->self, debugstr_a(name), debugstr_a(type) );
947 if (!pModule->ne_rsrctab) return 0;
949 type = get_res_name( type );
950 name = get_res_name( name );
952 if (HIWORD(type) || HIWORD(name))
954 DWORD id = NE_FindNameTableId( pModule, type, name );
955 if (id) /* found */
957 type = (LPCSTR)(ULONG_PTR)LOWORD(id);
958 name = (LPCSTR)(ULONG_PTR)HIWORD(id);
961 pResTab = (LPBYTE)pModule + pModule->ne_rsrctab;
962 pTypeInfo = (NE_TYPEINFO *)( pResTab + 2 );
964 for (;;)
966 if (!(pTypeInfo = NE_FindTypeSection( pResTab, pTypeInfo, type ))) break;
967 if ((pNameInfo = NE_FindResourceFromType( pResTab, pTypeInfo, name )))
969 TRACE(" Found id %p\n", name );
970 return (HRSRC16)( (char *)pNameInfo - (char *)pModule );
972 pTypeInfo = next_typeinfo(pTypeInfo);
974 return 0;
978 /**********************************************************************
979 * LoadResource (KERNEL.61)
981 HGLOBAL16 WINAPI LoadResource16( HMODULE16 hModule, HRSRC16 hRsrc )
983 NE_TYPEINFO *pTypeInfo;
984 NE_NAMEINFO *pNameInfo = NULL;
985 NE_MODULE *pModule = get_module( hModule );
986 int d;
988 if (!hRsrc || !pModule) return 0;
990 if (pModule->module32)
992 /* load 32-bit resource and convert it */
993 HRSRC hRsrc32 = MapHRsrc16To32( pModule, hRsrc );
994 WORD type = MapHRsrc16ToType( pModule, hRsrc );
995 HGLOBAL hMem = LoadResource( pModule->module32, hRsrc32 );
996 DWORD size = SizeofResource( pModule->module32, hRsrc32 );
997 if (!hMem) return 0;
998 return NE_LoadPEResource( pModule, type, LockResource( hMem ), size );
1001 /* first, verify hRsrc (just an offset from pModule to the needed pNameInfo) */
1003 d = pModule->ne_rsrctab + 2;
1004 pTypeInfo = (NE_TYPEINFO *)((char *)pModule + d);
1005 while( hRsrc > d )
1007 if (pTypeInfo->type_id == 0) break; /* terminal entry */
1008 d += sizeof(NE_TYPEINFO) + pTypeInfo->count * sizeof(NE_NAMEINFO);
1009 if (hRsrc < d)
1011 if( ((d - hRsrc)%sizeof(NE_NAMEINFO)) == 0 )
1013 pNameInfo = (NE_NAMEINFO *)((char *)pModule + hRsrc);
1014 break;
1016 else break; /* NE_NAMEINFO boundary mismatch */
1018 pTypeInfo = (NE_TYPEINFO *)((char *)pModule + d);
1021 if (pNameInfo)
1023 if (pNameInfo->handle && !(GlobalFlags16(pNameInfo->handle) & GMEM_DISCARDED))
1025 pNameInfo->usage++;
1026 TRACE(" Already loaded, new count=%d\n", pNameInfo->usage );
1028 else
1030 FARPROC16 resloader = pTypeInfo->resloader;
1031 if (resloader && resloader != get_default_res_handler())
1033 WORD args[3];
1034 DWORD ret;
1036 args[2] = pNameInfo->handle;
1037 args[1] = pModule->self;
1038 args[0] = hRsrc;
1039 WOWCallback16Ex( (DWORD)resloader, WCB16_PASCAL, sizeof(args), args, &ret );
1040 pNameInfo->handle = LOWORD(ret);
1042 else
1043 pNameInfo->handle = NE_DefResourceHandler( pNameInfo->handle, pModule->self, hRsrc );
1045 if (pNameInfo->handle)
1047 pNameInfo->usage++;
1048 pNameInfo->flags |= NE_SEGFLAGS_LOADED;
1051 return pNameInfo->handle;
1053 return 0;
1057 /**********************************************************************
1058 * LockResource (KERNEL.62)
1060 SEGPTR WINAPI WIN16_LockResource16( HGLOBAL16 handle )
1062 TRACE("(%04x)\n", handle );
1063 /* May need to reload the resource if discarded */
1064 return WOWGlobalLock16( handle );
1068 /**********************************************************************
1069 * LockResource16 (KERNEL32.@)
1071 LPVOID WINAPI LockResource16( HGLOBAL16 handle )
1073 return MapSL( WIN16_LockResource16(handle) );
1077 /**********************************************************************
1078 * SizeofResource (KERNEL.65)
1080 DWORD WINAPI SizeofResource16( HMODULE16 hModule, HRSRC16 hRsrc )
1082 NE_MODULE *pModule;
1084 TRACE("(%x, %x)\n", hModule, hRsrc );
1086 if (!hRsrc) return 0;
1087 if (!(pModule = get_module( hModule ))) return 0;
1088 if (pModule->ne_rsrctab)
1090 WORD sizeShift = *(WORD *)((char *)pModule + pModule->ne_rsrctab);
1091 NE_NAMEINFO *pNameInfo = (NE_NAMEINFO*)((char*)pModule + hRsrc);
1092 return (DWORD)pNameInfo->length << sizeShift;
1094 if (pModule->module32)
1096 /* 32-bit PE module */
1097 return SizeofResource( pModule->module32, MapHRsrc16To32( pModule, hRsrc ) );
1099 return 0;
1103 /**********************************************************************
1104 * FreeResource (KERNEL.63)
1106 BOOL16 WINAPI FreeResource16( HGLOBAL16 handle )
1108 FARPROC16 proc;
1109 HMODULE16 user;
1110 NE_MODULE *pModule = NE_GetPtr( FarGetOwner16( handle ) );
1112 TRACE("(%04x)\n", handle );
1114 /* Try NE resource first */
1116 if (pModule && pModule->ne_rsrctab)
1118 NE_TYPEINFO *pTypeInfo = (NE_TYPEINFO *)((char *)pModule + pModule->ne_rsrctab + 2);
1119 while (pTypeInfo->type_id)
1121 WORD count;
1122 NE_NAMEINFO *pNameInfo = (NE_NAMEINFO *)(pTypeInfo + 1);
1123 for (count = pTypeInfo->count; count > 0; count--)
1125 if (pNameInfo->handle == handle)
1127 if (pNameInfo->usage > 0) pNameInfo->usage--;
1128 if (pNameInfo->usage == 0)
1130 GlobalFree16( pNameInfo->handle );
1131 pNameInfo->handle = 0;
1132 pNameInfo->flags &= ~NE_SEGFLAGS_LOADED;
1134 return FALSE;
1136 pNameInfo++;
1138 pTypeInfo = (NE_TYPEINFO *)pNameInfo;
1142 /* If this failed, call USER.DestroyIcon32; this will check
1143 whether it is a shared cursor/icon; if not it will call
1144 GlobalFree16() */
1145 user = GetModuleHandle16( "user" );
1146 if (user && (proc = GetProcAddress16( user, "DestroyIcon32" )))
1148 WORD args[2];
1149 DWORD result;
1151 args[1] = handle;
1152 args[0] = 1; /* CID_RESOURCE */
1153 WOWCallback16Ex( (SEGPTR)proc, WCB16_PASCAL, sizeof(args), args, &result );
1154 return LOWORD(result);
1156 else
1157 return GlobalFree16( handle );