wineandroid: Implement wglCreateContextAttribsARB.
[wine.git] / dlls / krnl386.exe16 / resource.c
blobda54f6dd5406a6bb14ce540a02b2b4c6f48de70f
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 "config.h"
25 #include "wine/port.h"
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/types.h>
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wownt32.h"
36 #include "wine/winbase16.h"
37 #include "wine/winuser16.h"
38 #include "wine/unicode.h"
39 #include "kernel16_private.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
58 HRSRC hRsrc;
59 WORD type;
60 } HRSRC_ELEM;
62 typedef struct _HRSRC_MAP
64 int nAlloc;
65 int nUsed;
66 HRSRC_ELEM *elem;
67 } HRSRC_MAP;
70 /**********************************************************************
71 * MapHRsrc32To16
73 static HRSRC16 MapHRsrc32To16( NE_MODULE *pModule, HRSRC hRsrc32, WORD type )
75 HRSRC_MAP *map = pModule->rsrc32_map;
76 HRSRC_ELEM *newElem;
77 int i;
79 /* On first call, initialize HRSRC map */
80 if ( !map )
82 if ( !(map = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HRSRC_MAP) ) ) )
84 ERR("Cannot allocate HRSRC map\n" );
85 return 0;
87 pModule->rsrc32_map = 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 )
99 if (map->elem)
100 newElem = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
101 map->elem, (map->nAlloc + HRSRC_MAP_BLOCKSIZE) * sizeof(HRSRC_ELEM) );
102 else
103 newElem = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
104 (map->nAlloc + HRSRC_MAP_BLOCKSIZE) * sizeof(HRSRC_ELEM) );
106 if ( !newElem )
108 ERR("Cannot grow HRSRC map\n" );
109 return 0;
111 map->elem = newElem;
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;
118 map->nUsed++;
120 return (HRSRC16)map->nUsed;
123 /**********************************************************************
124 * MapHRsrc16To32
126 static HRSRC MapHRsrc16To32( NE_MODULE *pModule, HRSRC16 hRsrc16 )
128 HRSRC_MAP *map = pModule->rsrc32_map;
129 if ( !map || !hRsrc16 || hRsrc16 > map->nUsed ) return 0;
131 return map->elem[hRsrc16-1].hRsrc;
134 /**********************************************************************
135 * MapHRsrc16ToType
137 static WORD MapHRsrc16ToType( NE_MODULE *pModule, HRSRC16 hRsrc16 )
139 HRSRC_MAP *map = pModule->rsrc32_map;
140 if ( !map || !hRsrc16 || hRsrc16 > map->nUsed ) return 0;
142 return map->elem[hRsrc16-1].type;
146 /**********************************************************************
147 * get_res_name
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 = MAKEINTRESOURCEA( atoi( name + 1 ) );
154 return name;
158 /**********************************************************************
159 * next_typeinfo
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" );
175 return handler;
179 /***********************************************************************
180 * NE_FindNameTableId
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->ne_rsrctab + 2);
188 NE_NAMEINFO *pNameInfo;
189 HGLOBAL16 handle;
190 WORD *p;
191 DWORD ret = 0;
192 int count;
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)((char *)pNameInfo - (char *)pModule) );
206 for(p = 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 */
213 if (p[1] & 0x8000)
215 if (!HIWORD(typeId)) continue;
216 if (strcasecmp( typeId, (char *)(p + 3) )) continue;
218 else if (HIWORD(typeId) || (((DWORD)typeId & ~0x8000)!= p[1]))
219 continue;
221 /* Now check for the id */
223 if (p[2] & 0x8000)
225 if (!HIWORD(resId)) continue;
226 if (strcasecmp( resId, (char*)(p+3)+strlen((char*)(p+3))+1 )) continue;
229 else if (HIWORD(resId) || ((LOWORD(resId) & ~0x8000) != p[2]))
230 continue;
232 /* If we get here, we've found the entry */
234 TRACE(" Found!\n" );
235 ret = MAKELONG( p[1], p[2] );
236 break;
238 FreeResource16( handle );
239 if (ret) return ret;
242 return 0;
246 /***********************************************************************
247 * NE_FindTypeSection
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 */
257 LPCSTR str = typeId;
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( (char*)p+1, str, len ))
266 TRACE(" Found type '%s'\n", str );
267 return pTypeInfo;
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 );
282 return pTypeInfo;
284 TRACE(" Skipping type %04x\n", pTypeInfo->type_id );
285 pTypeInfo = next_typeinfo(pTypeInfo);
288 return NULL;
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 )
299 BYTE *p;
300 int count;
301 NE_NAMEINFO *pNameInfo = (NE_NAMEINFO *)(pTypeInfo + 1);
303 if (HIWORD(resId) != 0) /* Named resource */
305 LPCSTR str = resId;
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( (char*)p+1, str, len ))
312 return pNameInfo;
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;
321 return NULL;
325 /***********************************************************************
326 * DefResourceHandler (KERNEL.456)
328 * This is the default LoadProc() function.
330 HGLOBAL16 WINAPI NE_DefResourceHandler( HGLOBAL16 hMemObj, HMODULE16 hModule,
331 HRSRC16 hRsrc )
333 HGLOBAL16 handle;
334 WORD sizeShift;
335 NE_NAMEINFO* pNameInfo;
336 NE_MODULE* pModule = NE_GetPtr( hModule );
338 if (!pModule) return 0;
340 sizeShift = *(WORD *)((char *)pModule + pModule->ne_rsrctab);
341 pNameInfo = (NE_NAMEINFO *)((char *)pModule + hRsrc);
343 if ( hMemObj )
344 handle = GlobalReAlloc16( hMemObj, pNameInfo->length << sizeShift, 0 );
345 else
346 handle = AllocResource16( hModule, hRsrc, 0 );
348 if (handle)
350 if (!NE_READ_DATA( pModule, GlobalLock16( handle ),
351 (int)pNameInfo->offset << sizeShift,
352 (int)pNameInfo->length << sizeShift ))
354 GlobalFree16( handle );
355 handle = 0;
358 return handle;
362 /**********************************************************************
363 * SetResourceHandler (KERNEL.67)
365 FARPROC16 WINAPI SetResourceHandler16( HMODULE16 hModule, LPCSTR typeId, FARPROC16 resourceHandler )
367 LPBYTE pResTab;
368 NE_TYPEINFO *pTypeInfo;
369 FARPROC16 prevHandler = NULL;
370 NE_MODULE *pModule = NE_GetPtr( hModule );
372 if (!pModule || !pModule->ne_rsrctab) return NULL;
374 pResTab = (LPBYTE)pModule + pModule->ne_rsrctab;
375 pTypeInfo = (NE_TYPEINFO *)(pResTab + 2);
377 TRACE("module=%04x type=%s\n", hModule, debugstr_a(typeId) );
379 for (;;)
381 if (!(pTypeInfo = NE_FindTypeSection( pResTab, pTypeInfo, typeId )))
382 break;
383 memcpy_unaligned( &prevHandler, &pTypeInfo->resloader, sizeof(FARPROC16) );
384 memcpy_unaligned( &pTypeInfo->resloader, &resourceHandler, sizeof(FARPROC16) );
385 pTypeInfo = next_typeinfo(pTypeInfo);
387 if (!prevHandler) prevHandler = get_default_res_handler();
388 return prevHandler;
391 static inline DWORD get_dword(LPCVOID *p)
393 DWORD ret = *(const DWORD *)*p;
394 *p = (const DWORD *)*p + 1;
395 return ret;
398 static inline void put_dword(LPVOID *p, DWORD d)
400 *(DWORD*)*p = d;
401 *p = (DWORD *)*p + 1;
404 static inline WORD get_word(LPCVOID *p)
406 WORD ret = *(const WORD *)*p;
407 *p = (const WORD *)*p + 1;
408 return ret;
411 static inline void put_word(LPVOID *p, WORD w)
413 *(WORD*)*p = w;
414 *p = (WORD *)*p + 1;
417 static inline BYTE get_byte(LPCVOID *p)
419 BYTE ret = *(const BYTE *)*p;
420 *p = (const BYTE *)*p + 1;
421 return ret;
424 static inline void put_byte(LPVOID *p, BYTE b)
426 *(BYTE *)*p = b;
427 *p = (BYTE *)*p + 1;
430 /* convert a resource name */
431 static void convert_name( LPVOID *dst, LPCVOID *src )
433 int len;
434 switch (*(const WORD *)*src)
436 case 0x0000:
437 get_word( src );
438 put_byte( dst, 0 );
439 break;
440 case 0xffff:
441 get_word( src );
442 put_byte( dst, 0xff );
443 put_word( dst, get_word(src) );
444 break;
445 default:
446 len = WideCharToMultiByte( CP_ACP, 0, *src, -1, *dst, 0x7fffffff, NULL,NULL );
447 *dst = (char *)*dst + len;
448 *src = (LPCWSTR)*src + strlenW( *src ) + 1;
449 break;
453 /**********************************************************************
454 * ConvertDialog32To16 (KERNEL.615)
456 VOID WINAPI ConvertDialog32To16( LPCVOID dialog32, DWORD size, LPVOID dialog16 )
458 WORD nbItems, data, dialogEx;
459 DWORD style;
461 style = get_dword( &dialog32 );
462 put_dword( &dialog16, style );
463 dialogEx = (style == 0xffff0001); /* DIALOGEX resource */
464 if (dialogEx)
466 put_dword( &dialog16, get_dword( &dialog32 ) ); /* helpID */
467 put_dword( &dialog16, get_dword( &dialog32 ) ); /* exStyle */
468 style = get_dword( &dialog32 );
469 put_dword( &dialog16, style ); /* style */
471 else
472 dialog32 = (const DWORD *)dialog32 + 1; /* exStyle ignored in 16-bit standard dialog */
474 nbItems = get_word( &dialog32 );
475 put_byte( &dialog16, nbItems );
476 put_word( &dialog16, get_word( &dialog32 ) ); /* x */
477 put_word( &dialog16, get_word( &dialog32 ) ); /* y */
478 put_word( &dialog16, get_word( &dialog32 ) ); /* cx */
479 put_word( &dialog16, get_word( &dialog32 ) ); /* cy */
481 /* Transfer menu name */
482 convert_name( &dialog16, &dialog32 );
484 /* Transfer class name */
485 convert_name( &dialog16, &dialog32 );
487 /* Transfer window caption */
488 WideCharToMultiByte( CP_ACP, 0, dialog32, -1, dialog16, 0x7fffffff, NULL, NULL );
489 dialog16 = (LPSTR)dialog16 + strlen( dialog16 ) + 1;
490 dialog32 = (LPCWSTR)dialog32 + strlenW( dialog32 ) + 1;
492 /* Transfer font info */
493 if (style & DS_SETFONT)
495 put_word( &dialog16, get_word( &dialog32 ) ); /* pointSize */
496 if (dialogEx)
498 put_word( &dialog16, get_word( &dialog32 ) ); /* weight */
499 put_word( &dialog16, get_word( &dialog32 ) ); /* italic */
501 WideCharToMultiByte( CP_ACP, 0, dialog32, -1, dialog16, 0x7fffffff, NULL, NULL ); /* faceName */
502 dialog16 = (LPSTR)dialog16 + strlen( dialog16 ) + 1;
503 dialog32 = (LPCWSTR)dialog32 + strlenW( dialog32 ) + 1;
506 /* Transfer dialog items */
507 while (nbItems)
509 /* align on DWORD boundary (32-bit only) */
510 dialog32 = (LPCVOID)(((UINT_PTR)dialog32 + 3) & ~3);
512 if (dialogEx)
514 put_dword( &dialog16, get_dword( &dialog32 ) ); /* helpID */
515 put_dword( &dialog16, get_dword( &dialog32 ) ); /* exStyle */
516 put_dword( &dialog16, get_dword( &dialog32 ) ); /* style */
518 else
520 style = get_dword( &dialog32 ); /* save style */
521 dialog32 = (const DWORD *)dialog32 + 1; /* ignore exStyle */
524 put_word( &dialog16, get_word( &dialog32 ) ); /* x */
525 put_word( &dialog16, get_word( &dialog32 ) ); /* y */
526 put_word( &dialog16, get_word( &dialog32 ) ); /* cx */
527 put_word( &dialog16, get_word( &dialog32 ) ); /* cy */
529 if (dialogEx)
530 put_dword( &dialog16, get_dword( &dialog32 ) ); /* ID */
531 else
533 put_word( &dialog16, get_word( &dialog32 ) ); /* ID */
534 put_dword( &dialog16, style ); /* style from above */
537 /* Transfer class name */
538 switch (*(const WORD *)dialog32)
540 case 0x0000:
541 get_word( &dialog32 );
542 put_byte( &dialog16, 0 );
543 break;
544 case 0xffff:
545 get_word( &dialog32 );
546 put_byte( &dialog16, get_word( &dialog32 ) );
547 break;
548 default:
549 WideCharToMultiByte( CP_ACP, 0, dialog32, -1, dialog16, 0x7fffffff, NULL, NULL );
550 dialog16 = (LPSTR)dialog16 + strlen( dialog16 ) + 1;
551 dialog32 = (LPCWSTR)dialog32 + strlenW( dialog32 ) + 1;
552 break;
555 /* Transfer window name */
556 convert_name( &dialog16, &dialog32 );
558 /* Transfer data */
559 data = get_word( &dialog32 );
560 if (dialogEx)
561 put_word(&dialog16, data);
562 else
563 put_byte(&dialog16,(BYTE)data);
565 if (data)
567 memcpy( dialog16, dialog32, data );
568 dialog16 = (BYTE *)dialog16 + data;
569 dialog32 = (const BYTE *)dialog32 + data;
572 /* Next item */
573 nbItems--;
578 /**********************************************************************
579 * GetDialog32Size (KERNEL.618)
581 WORD WINAPI GetDialog32Size16( LPCVOID dialog32 )
583 LPCVOID p = dialog32;
584 WORD nbItems, data, dialogEx;
585 DWORD style;
587 style = get_dword(&p);
588 dialogEx = (style == 0xffff0001); /* DIALOGEX resource */
589 if (dialogEx)
591 p = (const DWORD *)p + 1; /* helpID */
592 p = (const DWORD *)p + 1; /* exStyle */
593 style = get_dword(&p); /* style */
595 else
596 p = (const DWORD *)p + 1; /* exStyle */
598 nbItems = get_word(&p);
599 p = (const WORD *)p + 1; /* x */
600 p = (const WORD *)p + 1; /* y */
601 p = (const WORD *)p + 1; /* cx */
602 p = (const WORD *)p + 1; /* cy */
604 /* Skip menu name */
605 switch (*(const WORD *)p)
607 case 0x0000: p = (const WORD *)p + 1; break;
608 case 0xffff: p = (const WORD *)p + 2; break;
609 default: p = (LPCWSTR)p + strlenW( p ) + 1; break;
612 /* Skip class name */
613 switch (*(const WORD *)p)
615 case 0x0000: p = (const WORD *)p + 1; break;
616 case 0xffff: p = (const WORD *)p + 2; break;
617 default: p = (LPCWSTR)p + strlenW( p ) + 1; break;
620 /* Skip window caption */
621 p = (LPCWSTR)p + strlenW( p ) + 1;
623 /* Skip font info */
624 if (style & DS_SETFONT)
626 p = (const WORD *)p + 1; /* pointSize */
627 if (dialogEx)
629 p = (const WORD *)p + 1; /* weight */
630 p = (const WORD *)p + 1; /* italic */
632 p = (LPCWSTR)p + strlenW( p ) + 1; /* faceName */
635 /* Skip dialog items */
636 while (nbItems)
638 /* align on DWORD boundary */
639 p = (LPCVOID)(((UINT_PTR)p + 3) & ~3);
641 if (dialogEx)
643 p = (const DWORD *)p + 1; /* helpID */
644 p = (const DWORD *)p + 1; /* exStyle */
645 p = (const DWORD *)p + 1; /* style */
647 else
649 p = (const DWORD *)p + 1; /* style */
650 p = (const DWORD *)p + 1; /* exStyle */
653 p = (const WORD *)p + 1; /* x */
654 p = (const WORD *)p + 1; /* y */
655 p = (const WORD *)p + 1; /* cx */
656 p = (const WORD *)p + 1; /* cy */
658 if (dialogEx)
659 p = (const DWORD *)p + 1; /* ID */
660 else
661 p = (const WORD *)p + 1; /* ID */
663 /* Skip class name */
664 switch (*(const WORD *)p)
666 case 0x0000: p = (const WORD *)p + 1; break;
667 case 0xffff: p = (const WORD *)p + 2; break;
668 default: p = (LPCWSTR)p + strlenW( p ) + 1; break;
671 /* Skip window name */
672 switch (*(const WORD *)p)
674 case 0x0000: p = (const WORD *)p + 1; break;
675 case 0xffff: p = (const WORD *)p + 2; break;
676 default: p = (LPCWSTR)p + strlenW( p ) + 1; break;
679 /* Skip data */
680 data = get_word(&p);
681 p = (const BYTE *)p + data;
683 /* Next item */
684 nbItems--;
687 return (WORD)((LPCSTR)p - (LPCSTR)dialog32);
691 /**********************************************************************
692 * ConvertMenu32To16 (KERNEL.616)
694 VOID WINAPI ConvertMenu32To16( LPCVOID menu32, DWORD size, LPVOID menu16 )
696 WORD version, headersize, flags, level = 1;
698 version = get_word( &menu32 );
699 headersize = get_word( &menu32 );
700 put_word( &menu16, version );
701 put_word( &menu16, headersize );
702 if ( headersize )
704 memcpy( menu16, menu32, headersize );
705 menu16 = (BYTE *)menu16 + headersize;
706 menu32 = (const BYTE *)menu32 + headersize;
709 while ( level )
710 if ( version == 0 ) /* standard */
712 flags = get_word( &menu32 );
713 put_word( &menu16, flags );
714 if ( !(flags & MF_POPUP) )
715 put_word( &menu16, get_word( &menu32 ) ); /* ID */
716 else
717 level++;
719 WideCharToMultiByte( CP_ACP, 0, menu32, -1, menu16, 0x7fffffff, NULL, NULL );
720 menu16 = (LPSTR)menu16 + strlen( menu16 ) + 1;
721 menu32 = (LPCWSTR)menu32 + strlenW( menu32 ) + 1;
723 if ( flags & MF_END )
724 level--;
726 else /* extended */
728 put_dword( &menu16, get_dword( &menu32 ) ); /* fType */
729 put_dword( &menu16, get_dword( &menu32 ) ); /* fState */
730 put_word( &menu16, get_dword( &menu32 ) ); /* ID */
731 flags = get_word( &menu32 );
732 put_byte(&menu16,flags);
734 WideCharToMultiByte( CP_ACP, 0, menu32, -1, menu16, 0x7fffffff, NULL, NULL );
735 menu16 = (LPSTR)menu16 + strlen( menu16 ) + 1;
736 menu32 = (LPCWSTR)menu32 + strlenW( menu32 ) + 1;
738 /* align on DWORD boundary (32-bit only) */
739 menu32 = (LPCVOID)(((UINT_PTR)menu32 + 3) & ~3);
741 /* If popup, transfer helpid */
742 if ( flags & 1)
744 put_dword( &menu16, get_dword( &menu32 ) );
745 level++;
748 if ( flags & MF_END )
749 level--;
754 /**********************************************************************
755 * GetMenu32Size (KERNEL.617)
757 WORD WINAPI GetMenu32Size16( LPCVOID menu32 )
759 LPCVOID p = menu32;
760 WORD version, headersize, flags, level = 1;
762 version = get_word(&p);
763 headersize = get_word(&p);
764 p = (const BYTE *)p + headersize;
766 while ( level )
767 if ( version == 0 ) /* standard */
769 flags = get_word(&p);
770 if ( !(flags & MF_POPUP) )
771 p = (const WORD *)p + 1; /* ID */
772 else
773 level++;
775 p = (LPCWSTR)p + strlenW( p ) + 1;
777 if ( flags & MF_END )
778 level--;
780 else /* extended */
782 p = (const DWORD *)p + 1; /* fType */
783 p = (const DWORD *)p + 1; /* fState */
784 p = (const DWORD *)p + 1; /* ID */
785 flags = get_word(&p);
787 p = (LPCWSTR)p + strlenW( p ) + 1;
789 /* align on DWORD boundary (32-bit only) */
790 p = (LPCVOID)(((UINT_PTR)p + 3) & ~3);
792 /* If popup, skip helpid */
793 if ( flags & 1)
795 p = (const DWORD *)p + 1;
796 level++;
799 if ( flags & MF_END )
800 level--;
803 return (WORD)((LPCSTR)p - (LPCSTR)menu32);
807 /**********************************************************************
808 * ConvertAccelerator32To16
810 static void ConvertAccelerator32To16( LPCVOID acc32, DWORD size, LPVOID acc16 )
812 BYTE type;
816 /* Copy type */
817 type = get_byte(&acc32);
818 put_byte(&acc16, type);
819 /* Skip padding */
820 get_byte(&acc32);
821 /* Copy event and IDval */
822 put_word(&acc16, get_word(&acc32));
823 put_word(&acc16, get_word(&acc32));
824 /* Skip padding */
825 get_word(&acc32);
827 } while ( !( type & 0x80 ) );
831 /**********************************************************************
832 * NE_LoadPEResource
834 static HGLOBAL16 NE_LoadPEResource( NE_MODULE *pModule, WORD type, LPCVOID bits, DWORD size )
836 HGLOBAL16 handle;
838 TRACE("module=%04x type=%04x\n", pModule->self, type );
840 handle = GlobalAlloc16( 0, size );
842 switch (type)
844 case RT_MENU:
845 ConvertMenu32To16( bits, size, GlobalLock16( handle ) );
846 break;
847 case RT_DIALOG:
848 ConvertDialog32To16( bits, size, GlobalLock16( handle ) );
849 break;
850 case RT_ACCELERATOR:
851 ConvertAccelerator32To16( bits, size, GlobalLock16( handle ) );
852 break;
853 case RT_STRING:
854 FIXME("not yet implemented!\n" );
855 /* fall through */
856 default:
857 memcpy( GlobalLock16( handle ), bits, size );
858 break;
860 return handle;
864 /**********************************************************************
865 * AllocResource (KERNEL.66)
867 HGLOBAL16 WINAPI AllocResource16( HMODULE16 hModule, HRSRC16 hRsrc, DWORD size)
869 NE_NAMEINFO *pNameInfo=NULL;
870 WORD sizeShift;
871 HGLOBAL16 ret;
873 NE_MODULE *pModule = NE_GetPtr( hModule );
874 if (!pModule || !pModule->ne_rsrctab || !hRsrc) return 0;
876 TRACE("module=%04x res=%04x size=%d\n", hModule, hRsrc, size );
878 sizeShift = *(WORD *)((char *)pModule + pModule->ne_rsrctab);
879 pNameInfo = (NE_NAMEINFO*)((char*)pModule + hRsrc);
880 if (size < (DWORD)pNameInfo->length << sizeShift)
881 size = (DWORD)pNameInfo->length << sizeShift;
882 ret = GlobalAlloc16( GMEM_FIXED, size );
883 if (ret) FarSetOwner16( ret, hModule );
884 return ret;
888 /**********************************************************************
889 * DirectResAlloc (KERNEL.168)
891 * Check Schulman, p. 232 for details
893 HGLOBAL16 WINAPI DirectResAlloc16( HINSTANCE16 hInstance, WORD wType,
894 UINT16 wSize )
896 HGLOBAL16 ret;
897 TRACE("(%04x,%04x,%04x)\n", hInstance, wType, wSize );
898 if (!(hInstance = GetExePtr( hInstance ))) return 0;
899 if(wType != 0x10) /* 0x10 is the only observed value, passed from
900 CreateCursorIndirect. */
901 TRACE("(wType=%x)\n", wType);
902 ret = GlobalAlloc16( GMEM_MOVEABLE, wSize );
903 if (ret) FarSetOwner16( ret, hInstance );
904 return ret;
908 /**********************************************************************
909 * AccessResource (KERNEL.64)
911 INT16 WINAPI AccessResource16( HINSTANCE16 hModule, HRSRC16 hRsrc )
913 HFILE16 fd;
914 NE_MODULE *pModule = NE_GetPtr( hModule );
916 if (!pModule || !pModule->ne_rsrctab || !hRsrc) return -1;
918 TRACE("module=%04x res=%04x\n", pModule->self, hRsrc );
920 if ((fd = _lopen16( NE_MODULE_NAME(pModule), OF_READ )) != HFILE_ERROR16)
922 WORD sizeShift = *(WORD *)((char *)pModule + pModule->ne_rsrctab);
923 NE_NAMEINFO *pNameInfo = (NE_NAMEINFO*)((char*)pModule + hRsrc);
924 _llseek16( fd, (int)pNameInfo->offset << sizeShift, SEEK_SET );
926 return fd;
930 /**********************************************************************
931 * FindResource (KERNEL.60)
933 HRSRC16 WINAPI FindResource16( HMODULE16 hModule, LPCSTR name, LPCSTR type )
935 NE_TYPEINFO *pTypeInfo;
936 NE_NAMEINFO *pNameInfo;
937 LPBYTE pResTab;
938 NE_MODULE *pModule = get_module( hModule );
940 if (!pModule) return 0;
942 if (pModule->module32)
944 /* 32-bit PE module */
945 HRSRC hRsrc32 = FindResourceA( pModule->module32, name, type );
946 return MapHRsrc32To16( pModule, hRsrc32, HIWORD(type) ? 0 : LOWORD(type) );
949 TRACE("module=%04x name=%s type=%s\n", pModule->self, debugstr_a(name), debugstr_a(type) );
951 if (!pModule->ne_rsrctab) return 0;
953 type = get_res_name( type );
954 name = get_res_name( name );
956 if (HIWORD(type) || HIWORD(name))
958 DWORD id = NE_FindNameTableId( pModule, type, name );
959 if (id) /* found */
961 type = (LPCSTR)(ULONG_PTR)LOWORD(id);
962 name = (LPCSTR)(ULONG_PTR)HIWORD(id);
965 pResTab = (LPBYTE)pModule + pModule->ne_rsrctab;
966 pTypeInfo = (NE_TYPEINFO *)( pResTab + 2 );
968 for (;;)
970 if (!(pTypeInfo = NE_FindTypeSection( pResTab, pTypeInfo, type ))) break;
971 if ((pNameInfo = NE_FindResourceFromType( pResTab, pTypeInfo, name )))
973 TRACE(" Found id %p\n", name );
974 return (HRSRC16)( (char *)pNameInfo - (char *)pModule );
976 pTypeInfo = next_typeinfo(pTypeInfo);
978 return 0;
982 /**********************************************************************
983 * LoadResource (KERNEL.61)
985 HGLOBAL16 WINAPI LoadResource16( HMODULE16 hModule, HRSRC16 hRsrc )
987 NE_TYPEINFO *pTypeInfo;
988 NE_NAMEINFO *pNameInfo = NULL;
989 NE_MODULE *pModule = get_module( hModule );
990 int d;
992 if (!hRsrc || !pModule) return 0;
994 if (pModule->module32)
996 /* load 32-bit resource and convert it */
997 HRSRC hRsrc32 = MapHRsrc16To32( pModule, hRsrc );
998 WORD type = MapHRsrc16ToType( pModule, hRsrc );
999 HGLOBAL hMem = LoadResource( pModule->module32, hRsrc32 );
1000 DWORD size = SizeofResource( pModule->module32, hRsrc32 );
1001 if (!hMem) return 0;
1002 return NE_LoadPEResource( pModule, type, LockResource( hMem ), size );
1005 /* first, verify hRsrc (just an offset from pModule to the needed pNameInfo) */
1007 d = pModule->ne_rsrctab + 2;
1008 pTypeInfo = (NE_TYPEINFO *)((char *)pModule + d);
1009 while( hRsrc > d )
1011 if (pTypeInfo->type_id == 0) break; /* terminal entry */
1012 d += sizeof(NE_TYPEINFO) + pTypeInfo->count * sizeof(NE_NAMEINFO);
1013 if (hRsrc < d)
1015 if( ((d - hRsrc)%sizeof(NE_NAMEINFO)) == 0 )
1017 pNameInfo = (NE_NAMEINFO *)((char *)pModule + hRsrc);
1018 break;
1020 else break; /* NE_NAMEINFO boundary mismatch */
1022 pTypeInfo = (NE_TYPEINFO *)((char *)pModule + d);
1025 if (pNameInfo)
1027 if (pNameInfo->handle && !(GlobalFlags16(pNameInfo->handle) & GMEM_DISCARDED))
1029 pNameInfo->usage++;
1030 TRACE(" Already loaded, new count=%d\n", pNameInfo->usage );
1032 else
1034 FARPROC16 resloader;
1035 memcpy_unaligned( &resloader, &pTypeInfo->resloader, sizeof(FARPROC16) );
1036 if (resloader && resloader != get_default_res_handler())
1038 WORD args[3];
1039 DWORD ret;
1041 args[2] = pNameInfo->handle;
1042 args[1] = pModule->self;
1043 args[0] = hRsrc;
1044 WOWCallback16Ex( (DWORD)resloader, WCB16_PASCAL, sizeof(args), args, &ret );
1045 pNameInfo->handle = LOWORD(ret);
1047 else
1048 pNameInfo->handle = NE_DefResourceHandler( pNameInfo->handle, pModule->self, hRsrc );
1050 if (pNameInfo->handle)
1052 pNameInfo->usage++;
1053 pNameInfo->flags |= NE_SEGFLAGS_LOADED;
1056 return pNameInfo->handle;
1058 return 0;
1062 /**********************************************************************
1063 * LockResource (KERNEL.62)
1065 SEGPTR WINAPI WIN16_LockResource16( HGLOBAL16 handle )
1067 TRACE("(%04x)\n", handle );
1068 /* May need to reload the resource if discarded */
1069 return WOWGlobalLock16( handle );
1073 /**********************************************************************
1074 * LockResource16 (KERNEL32.@)
1076 LPVOID WINAPI LockResource16( HGLOBAL16 handle )
1078 return MapSL( WIN16_LockResource16(handle) );
1082 /**********************************************************************
1083 * SizeofResource (KERNEL.65)
1085 DWORD WINAPI SizeofResource16( HMODULE16 hModule, HRSRC16 hRsrc )
1087 NE_MODULE *pModule;
1089 TRACE("(%x, %x)\n", hModule, hRsrc );
1091 if (!hRsrc) return 0;
1092 if (!(pModule = get_module( hModule ))) return 0;
1093 if (pModule->ne_rsrctab)
1095 WORD sizeShift = *(WORD *)((char *)pModule + pModule->ne_rsrctab);
1096 NE_NAMEINFO *pNameInfo = (NE_NAMEINFO*)((char*)pModule + hRsrc);
1097 return (DWORD)pNameInfo->length << sizeShift;
1099 if (pModule->module32)
1101 /* 32-bit PE module */
1102 return SizeofResource( pModule->module32, MapHRsrc16To32( pModule, hRsrc ) );
1104 return 0;
1108 /**********************************************************************
1109 * FreeResource (KERNEL.63)
1111 BOOL16 WINAPI FreeResource16( HGLOBAL16 handle )
1113 FARPROC16 proc;
1114 HMODULE16 user;
1115 NE_MODULE *pModule = NE_GetPtr( FarGetOwner16( handle ) );
1117 TRACE("(%04x)\n", handle );
1119 /* Try NE resource first */
1121 if (pModule && pModule->ne_rsrctab)
1123 NE_TYPEINFO *pTypeInfo = (NE_TYPEINFO *)((char *)pModule + pModule->ne_rsrctab + 2);
1124 while (pTypeInfo->type_id)
1126 WORD count;
1127 NE_NAMEINFO *pNameInfo = (NE_NAMEINFO *)(pTypeInfo + 1);
1128 for (count = pTypeInfo->count; count > 0; count--)
1130 if (pNameInfo->handle == handle)
1132 if (pNameInfo->usage > 0) pNameInfo->usage--;
1133 if (pNameInfo->usage == 0)
1135 GlobalFree16( pNameInfo->handle );
1136 pNameInfo->handle = 0;
1137 pNameInfo->flags &= ~NE_SEGFLAGS_LOADED;
1139 return FALSE;
1141 pNameInfo++;
1143 pTypeInfo = (NE_TYPEINFO *)pNameInfo;
1147 /* If this failed, call USER.DestroyIcon32; this will check
1148 whether it is a shared cursor/icon; if not it will call
1149 GlobalFree16() */
1150 user = GetModuleHandle16( "user" );
1151 if (user && (proc = GetProcAddress16( user, "DestroyIcon32" )))
1153 WORD args[2];
1154 DWORD result;
1156 args[1] = handle;
1157 args[0] = 1; /* CID_RESOURCE */
1158 WOWCallback16Ex( (SEGPTR)proc, WCB16_PASCAL, sizeof(args), args, &result );
1159 return LOWORD(result);
1161 else
1162 return GlobalFree16( handle );