inetmib1: Support the MIB2 UDP table.
[wine] / dlls / kernel32 / resource16.c
CommitLineData
424202bd
AJ
1/*
2 * 16-bit resource functions
3 *
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1997 Alex Korobka
6 * Copyright 1998 Ulrich Weigand
7 * Copyright 1995, 2003 Alexandre Julliard
8 *
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.
13 *
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.
18 *
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
360a3f91 21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
424202bd
AJ
22 */
23
24#include "config.h"
25#include "wine/port.h"
26
e37c6e18 27#include <stdarg.h>
402b79a1 28#include <stdio.h>
424202bd
AJ
29#include <stdlib.h>
30#include <string.h>
31#include <sys/types.h>
32
33#include "windef.h"
e37c6e18 34#include "winbase.h"
424202bd
AJ
35#include "wownt32.h"
36#include "wine/winbase16.h"
37#include "wine/winuser16.h"
38#include "wine/unicode.h"
4776085c 39#include "kernel16_private.h"
424202bd
AJ
40#include "wine/debug.h"
41
42WINE_DEFAULT_DEBUG_CHANNEL(resource);
43
44/* handle conversions */
45#define HRSRC_32(h16) ((HRSRC)(ULONG_PTR)(h16))
46#define HGLOBAL_32(h16) ((HGLOBAL)(ULONG_PTR)(h16))
47
48static inline NE_MODULE *get_module( HMODULE16 mod )
49{
50 if (!mod) mod = TASK_GetCurrent()->hModule;
51 return NE_GetPtr( mod );
52}
53
54#define HRSRC_MAP_BLOCKSIZE 16
55
56typedef struct _HRSRC_ELEM
57{
58 HRSRC hRsrc;
59 WORD type;
60} HRSRC_ELEM;
61
62typedef struct _HRSRC_MAP
63{
64 int nAlloc;
65 int nUsed;
66 HRSRC_ELEM *elem;
67} HRSRC_MAP;
68
69
70/**********************************************************************
71 * MapHRsrc32To16
72 */
73static HRSRC16 MapHRsrc32To16( NE_MODULE *pModule, HRSRC hRsrc32, WORD type )
74{
4417574d 75 HRSRC_MAP *map = pModule->rsrc32_map;
424202bd
AJ
76 HRSRC_ELEM *newElem;
77 int i;
78
79 /* On first call, initialize HRSRC map */
80 if ( !map )
81 {
82 if ( !(map = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HRSRC_MAP) ) ) )
83 {
84 ERR("Cannot allocate HRSRC map\n" );
85 return 0;
86 }
4417574d 87 pModule->rsrc32_map = map;
424202bd
AJ
88 }
89
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);
94
95 /* If no space left, grow table */
96 if ( map->nUsed == map->nAlloc )
97 {
de12a970
OP
98
99 if (map->elem)
9ed61de9 100 newElem = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
de12a970
OP
101 map->elem, (map->nAlloc + HRSRC_MAP_BLOCKSIZE) * sizeof(HRSRC_ELEM) );
102 else
9ed61de9 103 newElem = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
de12a970
OP
104 (map->nAlloc + HRSRC_MAP_BLOCKSIZE) * sizeof(HRSRC_ELEM) );
105
106 if ( !newElem )
424202bd
AJ
107 {
108 ERR("Cannot grow HRSRC map\n" );
109 return 0;
110 }
111 map->elem = newElem;
112 map->nAlloc += HRSRC_MAP_BLOCKSIZE;
113 }
114
115 /* Add HRSRC32 to table */
116 map->elem[map->nUsed].hRsrc = hRsrc32;
117 map->elem[map->nUsed].type = type;
118 map->nUsed++;
119
120 return (HRSRC16)map->nUsed;
121}
122
123/**********************************************************************
124 * MapHRsrc16To32
125 */
126static HRSRC MapHRsrc16To32( NE_MODULE *pModule, HRSRC16 hRsrc16 )
127{
4417574d 128 HRSRC_MAP *map = pModule->rsrc32_map;
424202bd
AJ
129 if ( !map || !hRsrc16 || hRsrc16 > map->nUsed ) return 0;
130
131 return map->elem[hRsrc16-1].hRsrc;
132}
133
134/**********************************************************************
135 * MapHRsrc16ToType
136 */
137static WORD MapHRsrc16ToType( NE_MODULE *pModule, HRSRC16 hRsrc16 )
138{
4417574d 139 HRSRC_MAP *map = pModule->rsrc32_map;
424202bd
AJ
140 if ( !map || !hRsrc16 || hRsrc16 > map->nUsed ) return 0;
141
142 return map->elem[hRsrc16-1].type;
143}
144
145
146/**********************************************************************
147 * get_res_name
148 *
149 * Convert a resource name from '#xxx' form to numerical id.
150 */
151static inline LPCSTR get_res_name( LPCSTR name )
152{
e1646dd2 153 if (HIWORD(name) && name[0] == '#') name = MAKEINTRESOURCEA( atoi( name + 1 ) );
424202bd
AJ
154 return name;
155}
156
157
158/**********************************************************************
159 * next_typeinfo
160 */
161static inline NE_TYPEINFO *next_typeinfo( NE_TYPEINFO *info )
162{
163 return (NE_TYPEINFO *)((char*)(info + 1) + info->count * sizeof(NE_NAMEINFO));
164}
165
166
167/**********************************************************************
168 * get_default_res_handler
169 */
170static inline FARPROC16 get_default_res_handler(void)
171{
172 static FARPROC16 handler;
173
174 if (!handler) handler = GetProcAddress16( GetModuleHandle16("KERNEL"), "DefResourceHandler" );
175 return handler;
176}
177
178
179/***********************************************************************
180 * NE_FindNameTableId
181 *
182 * Find the type and resource id from their names.
183 * Return value is MAKELONG( typeId, resId ), or 0 if not found.
184 */
185static DWORD NE_FindNameTableId( NE_MODULE *pModule, LPCSTR typeId, LPCSTR resId )
186{
4417574d 187 NE_TYPEINFO *pTypeInfo = (NE_TYPEINFO *)((char *)pModule + pModule->ne_rsrctab + 2);
424202bd
AJ
188 NE_NAMEINFO *pNameInfo;
189 HGLOBAL16 handle;
190 WORD *p;
191 DWORD ret = 0;
192 int count;
193
194 for (; pTypeInfo->type_id != 0;
195 pTypeInfo = (NE_TYPEINFO *)((char*)(pTypeInfo+1) +
196 pTypeInfo->count * sizeof(NE_NAMEINFO)))
197 {
198 if (pTypeInfo->type_id != 0x800f) continue;
199 pNameInfo = (NE_NAMEINFO *)(pTypeInfo + 1);
200 for (count = pTypeInfo->count; count > 0; count--, pNameInfo++)
201 {
202 TRACE("NameTable entry: type=%04x id=%04x\n",
203 pTypeInfo->type_id, pNameInfo->id );
204 handle = LoadResource16( pModule->self,
ba41fe20 205 (HRSRC16)((char *)pNameInfo - (char *)pModule) );
424202bd
AJ
206 for(p = (WORD*)LockResource16(handle); p && *p; p = (WORD *)((char*)p+*p))
207 {
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 */
212
213 if (p[1] & 0x8000)
214 {
215 if (!HIWORD(typeId)) continue;
216 if (strcasecmp( typeId, (char *)(p + 3) )) continue;
217 }
218 else if (HIWORD(typeId) || (((DWORD)typeId & ~0x8000)!= p[1]))
219 continue;
220
221 /* Now check for the id */
222
223 if (p[2] & 0x8000)
224 {
225 if (!HIWORD(resId)) continue;
226 if (strcasecmp( resId, (char*)(p+3)+strlen((char*)(p+3))+1 )) continue;
227
228 }
ba41fe20 229 else if (HIWORD(resId) || ((LOWORD(resId) & ~0x8000) != p[2]))
424202bd
AJ
230 continue;
231
232 /* If we get here, we've found the entry */
233
234 TRACE(" Found!\n" );
235 ret = MAKELONG( p[1], p[2] );
236 break;
237 }
238 FreeResource16( handle );
239 if (ret) return ret;
240 }
241 }
242 return 0;
243}
244
245
246/***********************************************************************
247 * NE_FindTypeSection
248 *
249 * Find header struct for a particular resource type.
250 */
251static NE_TYPEINFO *NE_FindTypeSection( LPBYTE pResTab, NE_TYPEINFO *pTypeInfo, LPCSTR typeId )
252{
253 /* start from pTypeInfo */
254
255 if (HIWORD(typeId) != 0) /* Named type */
256 {
257 LPCSTR str = typeId;
258 BYTE len = strlen( str );
259 while (pTypeInfo->type_id)
260 {
261 if (!(pTypeInfo->type_id & 0x8000))
262 {
263 BYTE *p = pResTab + pTypeInfo->type_id;
f58e9f9e 264 if ((*p == len) && !strncasecmp( (char*)p+1, str, len ))
424202bd
AJ
265 {
266 TRACE(" Found type '%s'\n", str );
267 return pTypeInfo;
268 }
269 }
270 TRACE(" Skipping type %04x\n", pTypeInfo->type_id );
271 pTypeInfo = next_typeinfo(pTypeInfo);
272 }
273 }
274 else /* Numeric type id */
275 {
276 WORD id = LOWORD(typeId) | 0x8000;
277 while (pTypeInfo->type_id)
278 {
279 if (pTypeInfo->type_id == id)
280 {
281 TRACE(" Found type %04x\n", id );
282 return pTypeInfo;
283 }
284 TRACE(" Skipping type %04x\n", pTypeInfo->type_id );
285 pTypeInfo = next_typeinfo(pTypeInfo);
286 }
287 }
288 return NULL;
289}
290
291
292/***********************************************************************
293 * NE_FindResourceFromType
294 *
295 * Find a resource once the type info structure has been found.
296 */
297static NE_NAMEINFO *NE_FindResourceFromType( LPBYTE pResTab, NE_TYPEINFO *pTypeInfo, LPCSTR resId )
298{
299 BYTE *p;
300 int count;
301 NE_NAMEINFO *pNameInfo = (NE_NAMEINFO *)(pTypeInfo + 1);
302
303 if (HIWORD(resId) != 0) /* Named resource */
304 {
305 LPCSTR str = resId;
306 BYTE len = strlen( str );
307 for (count = pTypeInfo->count; count > 0; count--, pNameInfo++)
308 {
309 if (pNameInfo->id & 0x8000) continue;
310 p = pResTab + pNameInfo->id;
f58e9f9e 311 if ((*p == len) && !strncasecmp( (char*)p+1, str, len ))
424202bd
AJ
312 return pNameInfo;
313 }
314 }
315 else /* Numeric resource id */
316 {
317 WORD id = LOWORD(resId) | 0x8000;
318 for (count = pTypeInfo->count; count > 0; count--, pNameInfo++)
319 if (pNameInfo->id == id) return pNameInfo;
320 }
321 return NULL;
322}
323
324
325/***********************************************************************
326 * DefResourceHandler (KERNEL.456)
327 *
328 * This is the default LoadProc() function.
329 */
330HGLOBAL16 WINAPI NE_DefResourceHandler( HGLOBAL16 hMemObj, HMODULE16 hModule,
331 HRSRC16 hRsrc )
332{
8eb1630c
AJ
333 HGLOBAL16 handle;
334 WORD sizeShift;
335 NE_NAMEINFO* pNameInfo;
424202bd 336 NE_MODULE* pModule = NE_GetPtr( hModule );
424202bd 337
8eb1630c 338 if (!pModule) return 0;
424202bd 339
8eb1630c
AJ
340 sizeShift = *(WORD *)((char *)pModule + pModule->ne_rsrctab);
341 pNameInfo = (NE_NAMEINFO *)((char *)pModule + hRsrc);
342
343 if ( hMemObj )
344 handle = GlobalReAlloc16( hMemObj, pNameInfo->length << sizeShift, 0 );
345 else
346 handle = AllocResource16( hModule, hRsrc, 0 );
347
348 if (handle)
349 {
1970e467
AJ
350 if (!NE_READ_DATA( pModule, GlobalLock16( handle ),
351 (int)pNameInfo->offset << sizeShift,
352 (int)pNameInfo->length << sizeShift ))
424202bd 353 {
1970e467
AJ
354 GlobalFree16( handle );
355 handle = 0;
424202bd 356 }
424202bd 357 }
8eb1630c 358 return handle;
424202bd
AJ
359}
360
361
362/**********************************************************************
363 * SetResourceHandler (KERNEL.67)
364 */
365FARPROC16 WINAPI SetResourceHandler16( HMODULE16 hModule, LPCSTR typeId, FARPROC16 resourceHandler )
366{
367 LPBYTE pResTab;
368 NE_TYPEINFO *pTypeInfo;
369 FARPROC16 prevHandler = NULL;
370 NE_MODULE *pModule = NE_GetPtr( hModule );
371
4417574d 372 if (!pModule || !pModule->ne_rsrctab) return NULL;
424202bd 373
4417574d 374 pResTab = (LPBYTE)pModule + pModule->ne_rsrctab;
424202bd
AJ
375 pTypeInfo = (NE_TYPEINFO *)(pResTab + 2);
376
377 TRACE("module=%04x type=%s\n", hModule, debugstr_a(typeId) );
378
379 for (;;)
380 {
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);
386 }
387 if (!prevHandler) prevHandler = get_default_res_handler();
388 return prevHandler;
389}
390
a5bf67ef 391static inline DWORD get_dword(LPCVOID *p)
c95b9848 392{
a5bf67ef
AT
393 DWORD ret = *(const DWORD *)*p;
394 *p = (const DWORD *)*p + 1;
c95b9848
AJ
395 return ret;
396}
397
398static inline void put_dword(LPVOID *p, DWORD d)
399{
400 *(DWORD*)*p = d;
401 *p = (DWORD *)*p + 1;
402}
403
a5bf67ef 404static inline WORD get_word(LPCVOID *p)
c95b9848 405{
a5bf67ef
AT
406 WORD ret = *(const WORD *)*p;
407 *p = (const WORD *)*p + 1;
c95b9848
AJ
408 return ret;
409}
410
411static inline void put_word(LPVOID *p, WORD w)
412{
413 *(WORD*)*p = w;
414 *p = (WORD *)*p + 1;
415}
416
a5bf67ef 417static inline BYTE get_byte(LPCVOID *p)
c95b9848 418{
a5bf67ef
AT
419 BYTE ret = *(const BYTE *)*p;
420 *p = (const BYTE *)*p + 1;
c95b9848
AJ
421 return ret;
422}
423
424static inline void put_byte(LPVOID *p, BYTE b)
425{
426 *(BYTE *)*p = b;
427 *p = (BYTE *)*p + 1;
428}
429
430/* convert a resource name */
a5bf67ef 431static void convert_name( LPVOID *dst, LPCVOID *src )
c95b9848
AJ
432{
433 int len;
a5bf67ef 434 switch (*(const WORD *)*src)
c95b9848
AJ
435 {
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;
a5bf67ef 448 *src = (LPCWSTR)*src + strlenW( *src ) + 1;
c95b9848
AJ
449 break;
450 }
451}
424202bd
AJ
452
453/**********************************************************************
454 * ConvertDialog32To16 (KERNEL.615)
455 */
a5bf67ef 456VOID WINAPI ConvertDialog32To16( LPCVOID dialog32, DWORD size, LPVOID dialog16 )
424202bd 457{
424202bd
AJ
458 WORD nbItems, data, dialogEx;
459 DWORD style;
460
c95b9848
AJ
461 style = get_dword( &dialog32 );
462 put_dword( &dialog16, style );
424202bd
AJ
463 dialogEx = (style == 0xffff0001); /* DIALOGEX resource */
464 if (dialogEx)
465 {
c95b9848
AJ
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 */
424202bd
AJ
470 }
471 else
a5bf67ef 472 dialog32 = (const DWORD *)dialog32 + 1; /* exStyle ignored in 16-bit standard dialog */
424202bd 473
c95b9848
AJ
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 */
424202bd
AJ
480
481 /* Transfer menu name */
c95b9848 482 convert_name( &dialog16, &dialog32 );
424202bd
AJ
483
484 /* Transfer class name */
c95b9848 485 convert_name( &dialog16, &dialog32 );
424202bd
AJ
486
487 /* Transfer window caption */
c95b9848 488 WideCharToMultiByte( CP_ACP, 0, dialog32, -1, dialog16, 0x7fffffff, NULL, NULL );
a5bf67ef
AT
489 dialog16 = (LPSTR)dialog16 + strlen( dialog16 ) + 1;
490 dialog32 = (LPCWSTR)dialog32 + strlenW( dialog32 ) + 1;
424202bd
AJ
491
492 /* Transfer font info */
493 if (style & DS_SETFONT)
494 {
c95b9848 495 put_word( &dialog16, get_word( &dialog32 ) ); /* pointSize */
424202bd
AJ
496 if (dialogEx)
497 {
c95b9848
AJ
498 put_word( &dialog16, get_word( &dialog32 ) ); /* weight */
499 put_word( &dialog16, get_word( &dialog32 ) ); /* italic */
424202bd 500 }
a5bf67ef
AT
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;
424202bd
AJ
504 }
505
506 /* Transfer dialog items */
507 while (nbItems)
508 {
509 /* align on DWORD boundary (32-bit only) */
a5bf67ef 510 dialog32 = (LPCVOID)(((UINT_PTR)dialog32 + 3) & ~3);
424202bd
AJ
511
512 if (dialogEx)
513 {
c95b9848
AJ
514 put_dword( &dialog16, get_dword( &dialog32 ) ); /* helpID */
515 put_dword( &dialog16, get_dword( &dialog32 ) ); /* exStyle */
516 put_dword( &dialog16, get_dword( &dialog32 ) ); /* style */
424202bd
AJ
517 }
518 else
519 {
a5bf67ef
AT
520 style = get_dword( &dialog32 ); /* save style */
521 dialog32 = (const DWORD *)dialog32 + 1; /* ignore exStyle */
424202bd
AJ
522 }
523
c95b9848
AJ
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 */
424202bd
AJ
528
529 if (dialogEx)
c95b9848 530 put_dword( &dialog16, get_dword( &dialog32 ) ); /* ID */
424202bd
AJ
531 else
532 {
c95b9848
AJ
533 put_word( &dialog16, get_word( &dialog32 ) ); /* ID */
534 put_dword( &dialog16, style ); /* style from above */
424202bd
AJ
535 }
536
537 /* Transfer class name */
a5bf67ef 538 switch (*(const WORD *)dialog32)
424202bd 539 {
c95b9848
AJ
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:
a5bf67ef
AT
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;
c95b9848 552 break;
424202bd
AJ
553 }
554
555 /* Transfer window name */
c95b9848 556 convert_name( &dialog16, &dialog32 );
424202bd
AJ
557
558 /* Transfer data */
c95b9848 559 data = get_word( &dialog32 );
424202bd 560 if (dialogEx)
c95b9848 561 put_word(&dialog16, data);
424202bd 562 else
c95b9848 563 put_byte(&dialog16,(BYTE)data);
424202bd
AJ
564
565 if (data)
566 {
c95b9848
AJ
567 memcpy( dialog16, dialog32, data );
568 dialog16 = (BYTE *)dialog16 + data;
a5bf67ef 569 dialog32 = (const BYTE *)dialog32 + data;
424202bd
AJ
570 }
571
572 /* Next item */
573 nbItems--;
574 }
575}
576
577
578/**********************************************************************
579 * GetDialog32Size (KERNEL.618)
580 */
a5bf67ef 581WORD WINAPI GetDialog32Size16( LPCVOID dialog32 )
424202bd 582{
a5bf67ef 583 LPCVOID p = dialog32;
424202bd
AJ
584 WORD nbItems, data, dialogEx;
585 DWORD style;
586
c95b9848 587 style = get_dword(&p);
424202bd
AJ
588 dialogEx = (style == 0xffff0001); /* DIALOGEX resource */
589 if (dialogEx)
590 {
a5bf67ef
AT
591 p = (const DWORD *)p + 1; /* helpID */
592 p = (const DWORD *)p + 1; /* exStyle */
c95b9848 593 style = get_dword(&p); /* style */
424202bd
AJ
594 }
595 else
a5bf67ef 596 p = (const DWORD *)p + 1; /* exStyle */
424202bd 597
c95b9848 598 nbItems = get_word(&p);
a5bf67ef
AT
599 p = (const DWORD *)p + 1; /* x */
600 p = (const DWORD *)p + 1; /* y */
601 p = (const DWORD *)p + 1; /* cx */
602 p = (const DWORD *)p + 1; /* cy */
424202bd
AJ
603
604 /* Skip menu name */
a5bf67ef 605 switch (*(const DWORD *)p)
424202bd 606 {
a5bf67ef
AT
607 case 0x0000: p = (const DWORD *)p + 1; break;
608 case 0xffff: p = (const DWORD *)p + 2; break;
609 default: p = (LPCWSTR)p + strlenW( p ) + 1; break;
424202bd
AJ
610 }
611
612 /* Skip class name */
a5bf67ef 613 switch (*(const DWORD *)p)
424202bd 614 {
a5bf67ef
AT
615 case 0x0000: p = (const DWORD *)p + 1; break;
616 case 0xffff: p = (const DWORD *)p + 2; break;
617 default: p = (LPCWSTR)p + strlenW( p ) + 1; break;
424202bd
AJ
618 }
619
620 /* Skip window caption */
a5bf67ef 621 p = (LPCWSTR)p + strlenW( p ) + 1;
424202bd
AJ
622
623 /* Skip font info */
624 if (style & DS_SETFONT)
625 {
a5bf67ef 626 p = (const DWORD *)p + 1; /* pointSize */
424202bd
AJ
627 if (dialogEx)
628 {
a5bf67ef
AT
629 p = (const DWORD *)p + 1; /* weight */
630 p = (const DWORD *)p + 1; /* italic */
424202bd 631 }
a5bf67ef 632 p = (LPCWSTR)p + strlenW( p ) + 1; /* faceName */
424202bd
AJ
633 }
634
635 /* Skip dialog items */
636 while (nbItems)
637 {
638 /* align on DWORD boundary */
a5bf67ef 639 p = (LPCVOID)(((UINT_PTR)p + 3) & ~3);
424202bd
AJ
640
641 if (dialogEx)
642 {
a5bf67ef
AT
643 p = (const DWORD *)p + 1; /* helpID */
644 p = (const DWORD *)p + 1; /* exStyle */
645 p = (const DWORD *)p + 1; /* style */
424202bd
AJ
646 }
647 else
648 {
a5bf67ef
AT
649 p = (const DWORD *)p + 1; /* style */
650 p = (const DWORD *)p + 1; /* exStyle */
424202bd
AJ
651 }
652
a5bf67ef
AT
653 p = (const DWORD *)p + 1; /* x */
654 p = (const DWORD *)p + 1; /* y */
655 p = (const DWORD *)p + 1; /* cx */
656 p = (const DWORD *)p + 1; /* cy */
424202bd
AJ
657
658 if (dialogEx)
a5bf67ef 659 p = (const DWORD *)p + 1; /* ID */
424202bd 660 else
a5bf67ef 661 p = (const DWORD *)p + 1; /* ID */
424202bd
AJ
662
663 /* Skip class name */
a5bf67ef 664 switch (*(const DWORD *)p)
424202bd 665 {
a5bf67ef
AT
666 case 0x0000: p = (const DWORD *)p + 1; break;
667 case 0xffff: p = (const DWORD *)p + 2; break;
668 default: p = (LPCWSTR)p + strlenW( p ) + 1; break;
424202bd
AJ
669 }
670
671 /* Skip window name */
a5bf67ef 672 switch (*(const DWORD *)p)
424202bd 673 {
a5bf67ef
AT
674 case 0x0000: p = (const DWORD *)p + 1; break;
675 case 0xffff: p = (const DWORD *)p + 2; break;
676 default: p = (LPCWSTR)p + strlenW( p ) + 1; break;
424202bd
AJ
677 }
678
679 /* Skip data */
c95b9848 680 data = get_word(&p);
a5bf67ef 681 p = (const BYTE *)p + data;
424202bd
AJ
682
683 /* Next item */
684 nbItems--;
685 }
686
a5bf67ef 687 return (WORD)((LPCSTR)p - (LPCSTR)dialog32);
424202bd
AJ
688}
689
690
691/**********************************************************************
692 * ConvertMenu32To16 (KERNEL.616)
693 */
a5bf67ef 694VOID WINAPI ConvertMenu32To16( LPCVOID menu32, DWORD size, LPVOID menu16 )
424202bd 695{
424202bd
AJ
696 WORD version, headersize, flags, level = 1;
697
c95b9848
AJ
698 version = get_word( &menu32 );
699 headersize = get_word( &menu32 );
700 put_word( &menu16, version );
701 put_word( &menu16, headersize );
424202bd
AJ
702 if ( headersize )
703 {
c95b9848
AJ
704 memcpy( menu16, menu32, headersize );
705 menu16 = (BYTE *)menu16 + headersize;
a5bf67ef 706 menu32 = (const BYTE *)menu32 + headersize;
424202bd
AJ
707 }
708
709 while ( level )
710 if ( version == 0 ) /* standard */
711 {
c95b9848
AJ
712 flags = get_word( &menu32 );
713 put_word( &menu16, flags );
424202bd 714 if ( !(flags & MF_POPUP) )
c95b9848 715 put_word( &menu16, get_word( &menu32 ) ); /* ID */
424202bd
AJ
716 else
717 level++;
718
a5bf67ef
AT
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;
424202bd
AJ
722
723 if ( flags & MF_END )
724 level--;
725 }
726 else /* extended */
727 {
c95b9848
AJ
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);
424202bd 733
a5bf67ef
AT
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;
424202bd
AJ
737
738 /* align on DWORD boundary (32-bit only) */
a5bf67ef 739 menu32 = (LPCVOID)(((UINT_PTR)menu32 + 3) & ~3);
424202bd
AJ
740
741 /* If popup, transfer helpid */
742 if ( flags & 1)
743 {
c95b9848 744 put_dword( &menu16, get_dword( &menu32 ) );
424202bd
AJ
745 level++;
746 }
747
748 if ( flags & MF_END )
749 level--;
750 }
751}
752
753
754/**********************************************************************
755 * GetMenu32Size (KERNEL.617)
756 */
a5bf67ef 757WORD WINAPI GetMenu32Size16( LPCVOID menu32 )
424202bd 758{
a5bf67ef 759 LPCVOID p = menu32;
424202bd
AJ
760 WORD version, headersize, flags, level = 1;
761
c95b9848
AJ
762 version = get_word(&p);
763 headersize = get_word(&p);
a5bf67ef 764 p = (const BYTE *)p + headersize;
424202bd
AJ
765
766 while ( level )
767 if ( version == 0 ) /* standard */
768 {
c95b9848 769 flags = get_word(&p);
424202bd 770 if ( !(flags & MF_POPUP) )
a5bf67ef 771 p = (const WORD *)p + 1; /* ID */
424202bd
AJ
772 else
773 level++;
774
a5bf67ef 775 p = (LPCWSTR)p + strlenW( p ) + 1;
424202bd
AJ
776
777 if ( flags & MF_END )
778 level--;
779 }
780 else /* extended */
781 {
a5bf67ef
AT
782 p = (const DWORD *)p + 1; /* fType */
783 p = (const DWORD *)p + 1; /* fState */
784 p = (const DWORD *)p + 1; /* ID */
c95b9848 785 flags = get_word(&p);
424202bd 786
a5bf67ef 787 p = (LPCWSTR)p + strlenW( p ) + 1;
424202bd
AJ
788
789 /* align on DWORD boundary (32-bit only) */
a5bf67ef 790 p = (LPCVOID)(((UINT_PTR)p + 3) & ~3);
424202bd
AJ
791
792 /* If popup, skip helpid */
793 if ( flags & 1)
794 {
a5bf67ef 795 p = (const DWORD *)p + 1;
424202bd
AJ
796 level++;
797 }
798
799 if ( flags & MF_END )
800 level--;
801 }
802
a5bf67ef 803 return (WORD)((LPCSTR)p - (LPCSTR)menu32);
424202bd
AJ
804}
805
806
807/**********************************************************************
808 * ConvertAccelerator32To16
809 */
a5bf67ef 810static void ConvertAccelerator32To16( LPCVOID acc32, DWORD size, LPVOID acc16 )
424202bd 811{
c95b9848 812 BYTE type;
424202bd
AJ
813
814 do
815 {
816 /* Copy type */
c95b9848
AJ
817 type = get_byte(&acc32);
818 put_byte(&acc16, type);
424202bd 819 /* Skip padding */
c95b9848 820 get_byte(&acc32);
424202bd 821 /* Copy event and IDval */
c95b9848
AJ
822 put_word(&acc16, get_word(&acc32));
823 put_word(&acc16, get_word(&acc32));
424202bd 824 /* Skip padding */
c95b9848 825 get_word(&acc32);
424202bd
AJ
826
827 } while ( !( type & 0x80 ) );
828}
829
830
831/**********************************************************************
832 * NE_LoadPEResource
833 */
a5bf67ef 834static HGLOBAL16 NE_LoadPEResource( NE_MODULE *pModule, WORD type, LPCVOID bits, DWORD size )
424202bd
AJ
835{
836 HGLOBAL16 handle;
837
838 TRACE("module=%04x type=%04x\n", pModule->self, type );
839
840 handle = GlobalAlloc16( 0, size );
841
842 switch (type)
843 {
cf52644a 844 case RT_MENU:
424202bd
AJ
845 ConvertMenu32To16( bits, size, GlobalLock16( handle ) );
846 break;
cf52644a 847 case RT_DIALOG:
424202bd
AJ
848 ConvertDialog32To16( bits, size, GlobalLock16( handle ) );
849 break;
cf52644a 850 case RT_ACCELERATOR:
424202bd
AJ
851 ConvertAccelerator32To16( bits, size, GlobalLock16( handle ) );
852 break;
cf52644a 853 case RT_STRING:
424202bd
AJ
854 FIXME("not yet implemented!\n" );
855 /* fall through */
856 default:
857 memcpy( GlobalLock16( handle ), bits, size );
858 break;
859 }
860 return handle;
861}
862
863
864/**********************************************************************
865 * AllocResource (KERNEL.66)
866 */
867HGLOBAL16 WINAPI AllocResource16( HMODULE16 hModule, HRSRC16 hRsrc, DWORD size)
868{
869 NE_NAMEINFO *pNameInfo=NULL;
870 WORD sizeShift;
871 HGLOBAL16 ret;
872
873 NE_MODULE *pModule = NE_GetPtr( hModule );
4417574d 874 if (!pModule || !pModule->ne_rsrctab || !hRsrc) return 0;
424202bd 875
8c38b880 876 TRACE("module=%04x res=%04x size=%d\n", hModule, hRsrc, size );
424202bd 877
4417574d 878 sizeShift = *(WORD *)((char *)pModule + pModule->ne_rsrctab);
424202bd
AJ
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;
885}
886
887
888/**********************************************************************
889 * DirectResAlloc (KERNEL.168)
890 *
891 * Check Schulman, p. 232 for details
892 */
893HGLOBAL16 WINAPI DirectResAlloc16( HINSTANCE16 hInstance, WORD wType,
894 UINT16 wSize )
895{
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;
905}
906
907
908/**********************************************************************
909 * AccessResource (KERNEL.64)
910 */
911INT16 WINAPI AccessResource16( HINSTANCE16 hModule, HRSRC16 hRsrc )
912{
913 HFILE16 fd;
914 NE_MODULE *pModule = NE_GetPtr( hModule );
915
4417574d 916 if (!pModule || !pModule->ne_rsrctab || !hRsrc) return -1;
424202bd
AJ
917
918 TRACE("module=%04x res=%04x\n", pModule->self, hRsrc );
919
920 if ((fd = _lopen16( NE_MODULE_NAME(pModule), OF_READ )) != HFILE_ERROR16)
921 {
4417574d 922 WORD sizeShift = *(WORD *)((char *)pModule + pModule->ne_rsrctab);
424202bd
AJ
923 NE_NAMEINFO *pNameInfo = (NE_NAMEINFO*)((char*)pModule + hRsrc);
924 _llseek16( fd, (int)pNameInfo->offset << sizeShift, SEEK_SET );
925 }
926 return fd;
927}
928
929
930/**********************************************************************
931 * FindResource (KERNEL.60)
932 */
933HRSRC16 WINAPI FindResource16( HMODULE16 hModule, LPCSTR name, LPCSTR type )
934{
935 NE_TYPEINFO *pTypeInfo;
936 NE_NAMEINFO *pNameInfo;
937 LPBYTE pResTab;
938 NE_MODULE *pModule = get_module( hModule );
939
940 if (!pModule) return 0;
941
942 if (pModule->module32)
943 {
944 /* 32-bit PE module */
945 HRSRC hRsrc32 = FindResourceA( pModule->module32, name, type );
946 return MapHRsrc32To16( pModule, hRsrc32, HIWORD(type) ? 0 : LOWORD(type) );
947 }
948
949 TRACE("module=%04x name=%s type=%s\n", pModule->self, debugstr_a(name), debugstr_a(type) );
950
4417574d 951 if (!pModule->ne_rsrctab) return 0;
424202bd
AJ
952
953 type = get_res_name( type );
954 name = get_res_name( name );
955
956 if (HIWORD(type) || HIWORD(name))
957 {
958 DWORD id = NE_FindNameTableId( pModule, type, name );
959 if (id) /* found */
960 {
961 type = (LPCSTR)(ULONG_PTR)LOWORD(id);
962 name = (LPCSTR)(ULONG_PTR)HIWORD(id);
963 }
964 }
4417574d 965 pResTab = (LPBYTE)pModule + pModule->ne_rsrctab;
424202bd
AJ
966 pTypeInfo = (NE_TYPEINFO *)( pResTab + 2 );
967
968 for (;;)
969 {
970 if (!(pTypeInfo = NE_FindTypeSection( pResTab, pTypeInfo, type ))) break;
971 if ((pNameInfo = NE_FindResourceFromType( pResTab, pTypeInfo, name )))
972 {
c6b005ac 973 TRACE(" Found id %p\n", name );
424202bd
AJ
974 return (HRSRC16)( (char *)pNameInfo - (char *)pModule );
975 }
976 pTypeInfo = next_typeinfo(pTypeInfo);
977 }
978 return 0;
979}
980
981
982/**********************************************************************
983 * LoadResource (KERNEL.61)
984 */
985HGLOBAL16 WINAPI LoadResource16( HMODULE16 hModule, HRSRC16 hRsrc )
986{
987 NE_TYPEINFO *pTypeInfo;
988 NE_NAMEINFO *pNameInfo = NULL;
989 NE_MODULE *pModule = get_module( hModule );
990 int d;
991
992 if (!hRsrc || !pModule) return 0;
993
994 if (pModule->module32)
995 {
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 );
1003 }
1004
1005 /* first, verify hRsrc (just an offset from pModule to the needed pNameInfo) */
1006
4417574d 1007 d = pModule->ne_rsrctab + 2;
424202bd
AJ
1008 pTypeInfo = (NE_TYPEINFO *)((char *)pModule + d);
1009 while( hRsrc > d )
1010 {
1011 if (pTypeInfo->type_id == 0) break; /* terminal entry */
1012 d += sizeof(NE_TYPEINFO) + pTypeInfo->count * sizeof(NE_NAMEINFO);
1013 if (hRsrc < d)
1014 {
1015 if( ((d - hRsrc)%sizeof(NE_NAMEINFO)) == 0 )
1016 {
1017 pNameInfo = (NE_NAMEINFO *)((char *)pModule + hRsrc);
1018 break;
1019 }
1020 else break; /* NE_NAMEINFO boundary mismatch */
1021 }
1022 pTypeInfo = (NE_TYPEINFO *)((char *)pModule + d);
1023 }
1024
1025 if (pNameInfo)
1026 {
1027 if (pNameInfo->handle && !(GlobalFlags16(pNameInfo->handle) & GMEM_DISCARDED))
1028 {
1029 pNameInfo->usage++;
1030 TRACE(" Already loaded, new count=%d\n", pNameInfo->usage );
1031 }
1032 else
1033 {
1034 FARPROC16 resloader;
1035 memcpy_unaligned( &resloader, &pTypeInfo->resloader, sizeof(FARPROC16) );
1036 if (resloader && resloader != get_default_res_handler())
1037 {
1038 WORD args[3];
1039 DWORD ret;
1040
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);
1046 }
1047 else
1048 pNameInfo->handle = NE_DefResourceHandler( pNameInfo->handle, pModule->self, hRsrc );
1049
1050 if (pNameInfo->handle)
1051 {
1052 pNameInfo->usage++;
1053 pNameInfo->flags |= NE_SEGFLAGS_LOADED;
1054 }
1055 }
1056 return pNameInfo->handle;
1057 }
1058 return 0;
1059}
1060
1061
1062/**********************************************************************
1063 * LockResource (KERNEL.62)
1064 */
1065SEGPTR WINAPI WIN16_LockResource16( HGLOBAL16 handle )
1066{
1067 TRACE("(%04x)\n", handle );
1068 /* May need to reload the resource if discarded */
628939d5 1069 return WOWGlobalLock16( handle );
424202bd
AJ
1070}
1071
1072
1073/**********************************************************************
1074 * LockResource16 (KERNEL32.@)
1075 */
1076LPVOID WINAPI LockResource16( HGLOBAL16 handle )
1077{
1078 return MapSL( WIN16_LockResource16(handle) );
1079}
1080
1081
1082/**********************************************************************
1083 * SizeofResource (KERNEL.65)
1084 */
1085DWORD WINAPI SizeofResource16( HMODULE16 hModule, HRSRC16 hRsrc )
1086{
1087 NE_MODULE *pModule = NE_GetPtr( hModule );
1088
1089 TRACE("(%x, %x)\n", hModule, hRsrc );
1090
1091 if (!hRsrc) return 0;
1092 if (!(pModule = get_module( hModule ))) return 0;
4417574d 1093 if (pModule->ne_rsrctab)
424202bd 1094 {
4417574d 1095 WORD sizeShift = *(WORD *)((char *)pModule + pModule->ne_rsrctab);
424202bd
AJ
1096 NE_NAMEINFO *pNameInfo = (NE_NAMEINFO*)((char*)pModule + hRsrc);
1097 return (DWORD)pNameInfo->length << sizeShift;
1098 }
1099 if (pModule->module32)
1100 {
1101 /* 32-bit PE module */
1102 return SizeofResource( pModule->module32, MapHRsrc16To32( pModule, hRsrc ) );
1103 }
1104 return 0;
1105}
1106
1107
1108typedef WORD (WINAPI *pDestroyIcon32Proc)( HGLOBAL16 handle, UINT16 flags );
1109
1110/**********************************************************************
1111 * FreeResource (KERNEL.63)
1112 */
1113BOOL16 WINAPI FreeResource16( HGLOBAL16 handle )
1114{
1115 pDestroyIcon32Proc proc;
1116 HMODULE user;
1117 NE_MODULE *pModule = NE_GetPtr( FarGetOwner16( handle ) );
1118
1119 TRACE("(%04x)\n", handle );
1120
1121 /* Try NE resource first */
1122
4417574d 1123 if (pModule && pModule->ne_rsrctab)
424202bd 1124 {
4417574d 1125 NE_TYPEINFO *pTypeInfo = (NE_TYPEINFO *)((char *)pModule + pModule->ne_rsrctab + 2);
424202bd
AJ
1126 while (pTypeInfo->type_id)
1127 {
1128 WORD count;
1129 NE_NAMEINFO *pNameInfo = (NE_NAMEINFO *)(pTypeInfo + 1);
1130 for (count = pTypeInfo->count; count > 0; count--)
1131 {
1132 if (pNameInfo->handle == handle)
1133 {
1134 if (pNameInfo->usage > 0) pNameInfo->usage--;
1135 if (pNameInfo->usage == 0)
1136 {
1137 GlobalFree16( pNameInfo->handle );
1138 pNameInfo->handle = 0;
1139 pNameInfo->flags &= ~NE_SEGFLAGS_LOADED;
1140 }
1141 return 0;
1142 }
1143 pNameInfo++;
1144 }
1145 pTypeInfo = (NE_TYPEINFO *)pNameInfo;
1146 }
1147 }
1148
1149 /* If this failed, call USER.DestroyIcon32; this will check
1150 whether it is a shared cursor/icon; if not it will call
1151 GlobalFree16() */
1152 user = GetModuleHandleA( "user32.dll" );
1153 if (user && (proc = (pDestroyIcon32Proc)GetProcAddress( user, "DestroyIcon32" )))
1154 return proc( handle, 1 /*CID_RESOURCE*/ );
1155 else
1156 return GlobalFree16( handle );
1157}