ws2_32: Allow arbitrary-sized iovecs again in WSASendTo and WSARecvFrom.
[wine/multimedia.git] / dlls / version / resource.c
blob03076d87af9d30b5610ca9d0eb28aaf440ffeef9
1 /*
2 * Implementation of VERSION.DLL - Resource Access routines
4 * Copyright 1996,1997 Marcus Meissner
5 * Copyright 1997 David Cuthbert
6 * Copyright 1999 Ulrich Weigand
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
36 #include "windef.h"
37 #include "winbase.h"
38 #include "lzexpand.h"
40 #include "wine/unicode.h"
41 #include "wine/winbase16.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(ver);
48 /**********************************************************************
49 * find_entry_by_id
51 * Find an entry by id in a resource directory
52 * Copied from loader/pe_resource.c
54 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
55 WORD id, const void *root )
57 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
58 int min, max, pos;
60 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
61 min = dir->NumberOfNamedEntries;
62 max = min + dir->NumberOfIdEntries - 1;
63 while (min <= max)
65 pos = (min + max) / 2;
66 if (entry[pos].u1.s2.Id == id)
67 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
68 if (entry[pos].u1.s2.Id > id) max = pos - 1;
69 else min = pos + 1;
71 return NULL;
75 /**********************************************************************
76 * find_entry_default
78 * Find a default entry in a resource directory
79 * Copied from loader/pe_resource.c
81 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
82 const void *root )
84 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
86 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
87 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry->u2.s3.OffsetToDirectory);
91 /**********************************************************************
92 * find_entry_by_name
94 * Find an entry by name in a resource directory
95 * Copied from loader/pe_resource.c
97 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
98 LPCSTR name, const void *root )
100 const IMAGE_RESOURCE_DIRECTORY *ret = NULL;
101 LPWSTR nameW;
102 DWORD namelen;
104 if (!HIWORD(name)) return find_entry_by_id( dir, LOWORD(name), root );
105 if (name[0] == '#')
107 return find_entry_by_id( dir, atoi(name+1), root );
110 namelen = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
111 if ((nameW = HeapAlloc( GetProcessHeap(), 0, namelen * sizeof(WCHAR) )))
113 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
114 const IMAGE_RESOURCE_DIR_STRING_U *str;
115 int min, max, res, pos;
117 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, namelen );
118 namelen--; /* remove terminating null */
119 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
120 min = 0;
121 max = dir->NumberOfNamedEntries - 1;
122 while (min <= max)
124 pos = (min + max) / 2;
125 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const char *)root + entry[pos].u1.s1.NameOffset);
126 res = strncmpiW( nameW, str->NameString, str->Length );
127 if (!res && namelen == str->Length)
129 ret = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
130 break;
132 if (res < 0) max = pos - 1;
133 else min = pos + 1;
135 HeapFree( GetProcessHeap(), 0, nameW );
137 return ret;
141 /***********************************************************************
142 * read_xx_header [internal]
144 static int read_xx_header( HFILE lzfd )
146 IMAGE_DOS_HEADER mzh;
147 char magic[3];
149 LZSeek( lzfd, 0, SEEK_SET );
150 if ( sizeof(mzh) != LZRead( lzfd, (LPSTR)&mzh, sizeof(mzh) ) )
151 return 0;
152 if ( mzh.e_magic != IMAGE_DOS_SIGNATURE )
153 return 0;
155 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
156 if ( 2 != LZRead( lzfd, magic, 2 ) )
157 return 0;
159 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
161 if ( magic[0] == 'N' && magic[1] == 'E' )
162 return IMAGE_OS2_SIGNATURE;
163 if ( magic[0] == 'P' && magic[1] == 'E' )
164 return IMAGE_NT_SIGNATURE;
166 magic[2] = '\0';
167 WARN("Can't handle %s files.\n", magic );
168 return 0;
171 /***********************************************************************
172 * load_ne_resource [internal]
174 static BOOL find_ne_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
175 DWORD *resLen, DWORD *resOff )
177 IMAGE_OS2_HEADER nehd;
178 NE_TYPEINFO *typeInfo;
179 NE_NAMEINFO *nameInfo;
180 DWORD nehdoffset;
181 LPBYTE resTab;
182 DWORD resTabSize;
183 int count;
185 /* Read in NE header */
186 nehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
187 if ( sizeof(nehd) != LZRead( lzfd, (LPSTR)&nehd, sizeof(nehd) ) ) return 0;
189 resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
190 if ( !resTabSize )
192 TRACE("No resources in NE dll\n" );
193 return FALSE;
196 /* Read in resource table */
197 resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
198 if ( !resTab ) return FALSE;
200 LZSeek( lzfd, nehd.ne_rsrctab + nehdoffset, SEEK_SET );
201 if ( resTabSize != LZRead( lzfd, (char*)resTab, resTabSize ) )
203 HeapFree( GetProcessHeap(), 0, resTab );
204 return FALSE;
207 /* Find resource */
208 typeInfo = (NE_TYPEINFO *)(resTab + 2);
210 if (HIWORD(typeid) != 0) /* named type */
212 BYTE len = strlen( typeid );
213 while (typeInfo->type_id)
215 if (!(typeInfo->type_id & 0x8000))
217 BYTE *p = resTab + typeInfo->type_id;
218 if ((*p == len) && !strncasecmp( (char*)p+1, typeid, len )) goto found_type;
220 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
221 typeInfo->count * sizeof(NE_NAMEINFO));
224 else /* numeric type id */
226 WORD id = LOWORD(typeid) | 0x8000;
227 while (typeInfo->type_id)
229 if (typeInfo->type_id == id) goto found_type;
230 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
231 typeInfo->count * sizeof(NE_NAMEINFO));
234 TRACE("No typeid entry found for %p\n", typeid );
235 HeapFree( GetProcessHeap(), 0, resTab );
236 return FALSE;
238 found_type:
239 nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
241 if (HIWORD(resid) != 0) /* named resource */
243 BYTE len = strlen( resid );
244 for (count = typeInfo->count; count > 0; count--, nameInfo++)
246 BYTE *p = resTab + nameInfo->id;
247 if (nameInfo->id & 0x8000) continue;
248 if ((*p == len) && !strncasecmp( (char*)p+1, resid, len )) goto found_name;
251 else /* numeric resource id */
253 WORD id = LOWORD(resid) | 0x8000;
254 for (count = typeInfo->count; count > 0; count--, nameInfo++)
255 if (nameInfo->id == id) goto found_name;
257 TRACE("No resid entry found for %p\n", typeid );
258 HeapFree( GetProcessHeap(), 0, resTab );
259 return FALSE;
261 found_name:
262 /* Return resource data */
263 if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
264 if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
266 HeapFree( GetProcessHeap(), 0, resTab );
267 return TRUE;
270 /***********************************************************************
271 * load_pe_resource [internal]
273 static BOOL find_pe_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
274 DWORD *resLen, DWORD *resOff )
276 IMAGE_NT_HEADERS pehd;
277 DWORD pehdoffset;
278 PIMAGE_DATA_DIRECTORY resDataDir;
279 PIMAGE_SECTION_HEADER sections;
280 LPBYTE resSection;
281 DWORD resSectionSize;
282 const void *resDir;
283 const IMAGE_RESOURCE_DIRECTORY *resPtr;
284 const IMAGE_RESOURCE_DATA_ENTRY *resData;
285 int i, nSections;
286 BOOL ret = FALSE;
288 /* Read in PE header */
289 pehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
290 if ( sizeof(pehd) != LZRead( lzfd, (LPSTR)&pehd, sizeof(pehd) ) ) return 0;
292 resDataDir = pehd.OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
293 if ( !resDataDir->Size )
295 TRACE("No resources in PE dll\n" );
296 return FALSE;
299 /* Read in section table */
300 nSections = pehd.FileHeader.NumberOfSections;
301 sections = HeapAlloc( GetProcessHeap(), 0,
302 nSections * sizeof(IMAGE_SECTION_HEADER) );
303 if ( !sections ) return FALSE;
305 LZSeek( lzfd, pehdoffset +
306 sizeof(DWORD) + /* Signature */
307 sizeof(IMAGE_FILE_HEADER) +
308 pehd.FileHeader.SizeOfOptionalHeader, SEEK_SET );
310 if ( nSections * sizeof(IMAGE_SECTION_HEADER) !=
311 LZRead( lzfd, (LPSTR)sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) )
313 HeapFree( GetProcessHeap(), 0, sections );
314 return FALSE;
317 /* Find resource section */
318 for ( i = 0; i < nSections; i++ )
319 if ( resDataDir->VirtualAddress >= sections[i].VirtualAddress
320 && resDataDir->VirtualAddress < sections[i].VirtualAddress +
321 sections[i].SizeOfRawData )
322 break;
324 if ( i == nSections )
326 HeapFree( GetProcessHeap(), 0, sections );
327 TRACE("Couldn't find resource section\n" );
328 return FALSE;
331 /* Read in resource section */
332 resSectionSize = sections[i].SizeOfRawData;
333 resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
334 if ( !resSection )
336 HeapFree( GetProcessHeap(), 0, sections );
337 return FALSE;
340 LZSeek( lzfd, sections[i].PointerToRawData, SEEK_SET );
341 if ( resSectionSize != LZRead( lzfd, (char*)resSection, resSectionSize ) ) goto done;
343 /* Find resource */
344 resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
346 resPtr = (const IMAGE_RESOURCE_DIRECTORY*)resDir;
347 resPtr = find_entry_by_name( resPtr, typeid, resDir );
348 if ( !resPtr )
350 TRACE("No typeid entry found for %p\n", typeid );
351 goto done;
353 resPtr = find_entry_by_name( resPtr, resid, resDir );
354 if ( !resPtr )
356 TRACE("No resid entry found for %p\n", resid );
357 goto done;
359 resPtr = find_entry_default( resPtr, resDir );
360 if ( !resPtr )
362 TRACE("No default language entry found for %p\n", resid );
363 goto done;
366 /* Find resource data section */
367 resData = (const IMAGE_RESOURCE_DATA_ENTRY*)resPtr;
368 for ( i = 0; i < nSections; i++ )
369 if ( resData->OffsetToData >= sections[i].VirtualAddress
370 && resData->OffsetToData < sections[i].VirtualAddress +
371 sections[i].SizeOfRawData )
372 break;
374 if ( i == nSections )
376 TRACE("Couldn't find resource data section\n" );
377 goto done;
380 /* Return resource data */
381 if ( resLen ) *resLen = resData->Size;
382 if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
383 + sections[i].PointerToRawData;
384 ret = TRUE;
386 done:
387 HeapFree( GetProcessHeap(), 0, resSection );
388 HeapFree( GetProcessHeap(), 0, sections );
389 return ret;
393 /*************************************************************************
394 * GetFileResourceSize [VER.2]
396 DWORD WINAPI GetFileResourceSize16( LPCSTR lpszFileName, LPCSTR lpszResType,
397 LPCSTR lpszResId, LPDWORD lpdwFileOffset )
399 BOOL retv = FALSE;
400 HFILE lzfd;
401 OFSTRUCT ofs;
402 DWORD reslen;
404 TRACE("(%s,type=0x%x,id=0x%x,off=%p)\n",
405 debugstr_a(lpszFileName), (LONG)lpszResType, (LONG)lpszResId,
406 lpszResId );
408 lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
409 if ( lzfd < 0 ) return 0;
411 switch ( read_xx_header( lzfd ) )
413 case IMAGE_OS2_SIGNATURE:
414 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
415 &reslen, lpdwFileOffset );
416 break;
418 case IMAGE_NT_SIGNATURE:
419 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
420 &reslen, lpdwFileOffset );
421 break;
424 LZClose( lzfd );
425 return retv? reslen : 0;
429 /*************************************************************************
430 * GetFileResource [VER.3]
432 DWORD WINAPI GetFileResource16( LPCSTR lpszFileName, LPCSTR lpszResType,
433 LPCSTR lpszResId, DWORD dwFileOffset,
434 DWORD dwResLen, LPVOID lpvData )
436 BOOL retv = FALSE;
437 HFILE lzfd;
438 OFSTRUCT ofs;
439 DWORD reslen = dwResLen;
441 TRACE("(%s,type=%p,id=%p,off=%d,len=%d,data=%p)\n",
442 debugstr_a(lpszFileName), lpszResType, lpszResId,
443 dwFileOffset, dwResLen, lpvData );
445 lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
446 if ( lzfd < 0 ) return 0;
448 if ( !dwFileOffset )
450 switch ( read_xx_header( lzfd ) )
452 case IMAGE_OS2_SIGNATURE:
453 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
454 &reslen, &dwFileOffset );
455 break;
457 case IMAGE_NT_SIGNATURE:
458 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
459 &reslen, &dwFileOffset );
460 break;
463 if ( !retv )
465 LZClose( lzfd );
466 return 0;
470 LZSeek( lzfd, dwFileOffset, SEEK_SET );
471 reslen = LZRead( lzfd, lpvData, min( reslen, dwResLen ) );
472 LZClose( lzfd );
474 return reslen;