HTTP_DealWithProxy: Only add http:// to proxy string when needed.
[wine/multimedia.git] / dlls / version / resource.c
bloba29baf4702608f2f2598db023facc96334563cef
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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"
42 #include "wine/winuser16.h"
43 #include "winver.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(ver);
50 /**********************************************************************
51 * find_entry_by_id
53 * Find an entry by id in a resource directory
54 * Copied from loader/pe_resource.c
56 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
57 WORD id, const void *root )
59 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
60 int min, max, pos;
62 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
63 min = dir->NumberOfNamedEntries;
64 max = min + dir->NumberOfIdEntries - 1;
65 while (min <= max)
67 pos = (min + max) / 2;
68 if (entry[pos].u1.s2.Id == id)
69 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
70 if (entry[pos].u1.s2.Id > id) max = pos - 1;
71 else min = pos + 1;
73 return NULL;
77 /**********************************************************************
78 * find_entry_default
80 * Find a default entry in a resource directory
81 * Copied from loader/pe_resource.c
83 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
84 const void *root )
86 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
88 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
89 return (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry->u2.s3.OffsetToDirectory);
93 /**********************************************************************
94 * find_entry_by_name
96 * Find an entry by name in a resource directory
97 * Copied from loader/pe_resource.c
99 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
100 LPCSTR name, const void *root )
102 const IMAGE_RESOURCE_DIRECTORY *ret = NULL;
103 LPWSTR nameW;
104 DWORD namelen;
106 if (!HIWORD(name)) return find_entry_by_id( dir, LOWORD(name), root );
107 if (name[0] == '#')
109 return find_entry_by_id( dir, atoi(name+1), root );
112 namelen = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
113 if ((nameW = HeapAlloc( GetProcessHeap(), 0, namelen * sizeof(WCHAR) )))
115 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
116 const IMAGE_RESOURCE_DIR_STRING_U *str;
117 int min, max, res, pos;
119 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, namelen );
120 namelen--; /* remove terminating null */
121 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
122 min = 0;
123 max = dir->NumberOfNamedEntries - 1;
124 while (min <= max)
126 pos = (min + max) / 2;
127 str = (IMAGE_RESOURCE_DIR_STRING_U *)((char *)root + entry[pos].u1.s1.NameOffset);
128 res = strncmpiW( nameW, str->NameString, str->Length );
129 if (!res && namelen == str->Length)
131 ret = (IMAGE_RESOURCE_DIRECTORY *)((char *)root + entry[pos].u2.s3.OffsetToDirectory);
132 break;
134 if (res < 0) max = pos - 1;
135 else min = pos + 1;
137 HeapFree( GetProcessHeap(), 0, nameW );
139 return ret;
143 /***********************************************************************
144 * read_xx_header [internal]
146 static int read_xx_header( HFILE lzfd )
148 IMAGE_DOS_HEADER mzh;
149 char magic[3];
151 LZSeek( lzfd, 0, SEEK_SET );
152 if ( sizeof(mzh) != LZRead( lzfd, (LPSTR)&mzh, sizeof(mzh) ) )
153 return 0;
154 if ( mzh.e_magic != IMAGE_DOS_SIGNATURE )
155 return 0;
157 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
158 if ( 2 != LZRead( lzfd, magic, 2 ) )
159 return 0;
161 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
163 if ( magic[0] == 'N' && magic[1] == 'E' )
164 return IMAGE_OS2_SIGNATURE;
165 if ( magic[0] == 'P' && magic[1] == 'E' )
166 return IMAGE_NT_SIGNATURE;
168 magic[2] = '\0';
169 WARN("Can't handle %s files.\n", magic );
170 return 0;
173 /***********************************************************************
174 * load_ne_resource [internal]
176 static BOOL find_ne_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
177 DWORD *resLen, DWORD *resOff )
179 IMAGE_OS2_HEADER nehd;
180 NE_TYPEINFO *typeInfo;
181 NE_NAMEINFO *nameInfo;
182 DWORD nehdoffset;
183 LPBYTE resTab;
184 DWORD resTabSize;
185 int count;
187 /* Read in NE header */
188 nehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
189 if ( sizeof(nehd) != LZRead( lzfd, (LPSTR)&nehd, sizeof(nehd) ) ) return 0;
191 resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
192 if ( !resTabSize )
194 TRACE("No resources in NE dll\n" );
195 return FALSE;
198 /* Read in resource table */
199 resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
200 if ( !resTab ) return FALSE;
202 LZSeek( lzfd, nehd.ne_rsrctab + nehdoffset, SEEK_SET );
203 if ( resTabSize != LZRead( lzfd, resTab, resTabSize ) )
205 HeapFree( GetProcessHeap(), 0, resTab );
206 return FALSE;
209 /* Find resource */
210 typeInfo = (NE_TYPEINFO *)(resTab + 2);
212 if (HIWORD(typeid) != 0) /* named type */
214 BYTE len = strlen( typeid );
215 while (typeInfo->type_id)
217 if (!(typeInfo->type_id & 0x8000))
219 BYTE *p = resTab + typeInfo->type_id;
220 if ((*p == len) && !strncasecmp( p+1, typeid, len )) goto found_type;
222 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
223 typeInfo->count * sizeof(NE_NAMEINFO));
226 else /* numeric type id */
228 WORD id = LOWORD(typeid) | 0x8000;
229 while (typeInfo->type_id)
231 if (typeInfo->type_id == id) goto found_type;
232 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
233 typeInfo->count * sizeof(NE_NAMEINFO));
236 TRACE("No typeid entry found for %p\n", typeid );
237 HeapFree( GetProcessHeap(), 0, resTab );
238 return FALSE;
240 found_type:
241 nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
243 if (HIWORD(resid) != 0) /* named resource */
245 BYTE len = strlen( resid );
246 for (count = typeInfo->count; count > 0; count--, nameInfo++)
248 BYTE *p = resTab + nameInfo->id;
249 if (nameInfo->id & 0x8000) continue;
250 if ((*p == len) && !strncasecmp( p+1, resid, len )) goto found_name;
253 else /* numeric resource id */
255 WORD id = LOWORD(resid) | 0x8000;
256 for (count = typeInfo->count; count > 0; count--, nameInfo++)
257 if (nameInfo->id == id) goto found_name;
259 TRACE("No resid entry found for %p\n", typeid );
260 HeapFree( GetProcessHeap(), 0, resTab );
261 return FALSE;
263 found_name:
264 /* Return resource data */
265 if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
266 if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
268 HeapFree( GetProcessHeap(), 0, resTab );
269 return TRUE;
272 /***********************************************************************
273 * load_pe_resource [internal]
275 static BOOL find_pe_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
276 DWORD *resLen, DWORD *resOff )
278 IMAGE_NT_HEADERS pehd;
279 DWORD pehdoffset;
280 PIMAGE_DATA_DIRECTORY resDataDir;
281 PIMAGE_SECTION_HEADER sections;
282 LPBYTE resSection;
283 DWORD resSectionSize;
284 const void *resDir;
285 const IMAGE_RESOURCE_DIRECTORY *resPtr;
286 const IMAGE_RESOURCE_DATA_ENTRY *resData;
287 int i, nSections;
288 BOOL ret = FALSE;
290 /* Read in PE header */
291 pehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
292 if ( sizeof(pehd) != LZRead( lzfd, (LPSTR)&pehd, sizeof(pehd) ) ) return 0;
294 resDataDir = pehd.OptionalHeader.DataDirectory+IMAGE_FILE_RESOURCE_DIRECTORY;
295 if ( !resDataDir->Size )
297 TRACE("No resources in PE dll\n" );
298 return FALSE;
301 /* Read in section table */
302 nSections = pehd.FileHeader.NumberOfSections;
303 sections = HeapAlloc( GetProcessHeap(), 0,
304 nSections * sizeof(IMAGE_SECTION_HEADER) );
305 if ( !sections ) return FALSE;
307 LZSeek( lzfd, pehdoffset +
308 sizeof(DWORD) + /* Signature */
309 sizeof(IMAGE_FILE_HEADER) +
310 pehd.FileHeader.SizeOfOptionalHeader, SEEK_SET );
312 if ( nSections * sizeof(IMAGE_SECTION_HEADER) !=
313 LZRead( lzfd, (LPSTR)sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) )
315 HeapFree( GetProcessHeap(), 0, sections );
316 return FALSE;
319 /* Find resource section */
320 for ( i = 0; i < nSections; i++ )
321 if ( resDataDir->VirtualAddress >= sections[i].VirtualAddress
322 && resDataDir->VirtualAddress < sections[i].VirtualAddress +
323 sections[i].SizeOfRawData )
324 break;
326 if ( i == nSections )
328 HeapFree( GetProcessHeap(), 0, sections );
329 TRACE("Couldn't find resource section\n" );
330 return FALSE;
333 /* Read in resource section */
334 resSectionSize = sections[i].SizeOfRawData;
335 resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
336 if ( !resSection )
338 HeapFree( GetProcessHeap(), 0, sections );
339 return FALSE;
342 LZSeek( lzfd, sections[i].PointerToRawData, SEEK_SET );
343 if ( resSectionSize != LZRead( lzfd, resSection, resSectionSize ) ) goto done;
345 /* Find resource */
346 resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
348 resPtr = (PIMAGE_RESOURCE_DIRECTORY)resDir;
349 resPtr = find_entry_by_name( resPtr, typeid, resDir );
350 if ( !resPtr )
352 TRACE("No typeid entry found for %p\n", typeid );
353 goto done;
355 resPtr = find_entry_by_name( resPtr, resid, resDir );
356 if ( !resPtr )
358 TRACE("No resid entry found for %p\n", resid );
359 goto done;
361 resPtr = find_entry_default( resPtr, resDir );
362 if ( !resPtr )
364 TRACE("No default language entry found for %p\n", resid );
365 goto done;
368 /* Find resource data section */
369 resData = (PIMAGE_RESOURCE_DATA_ENTRY)resPtr;
370 for ( i = 0; i < nSections; i++ )
371 if ( resData->OffsetToData >= sections[i].VirtualAddress
372 && resData->OffsetToData < sections[i].VirtualAddress +
373 sections[i].SizeOfRawData )
374 break;
376 if ( i == nSections )
378 TRACE("Couldn't find resource data section\n" );
379 goto done;
382 /* Return resource data */
383 if ( resLen ) *resLen = resData->Size;
384 if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
385 + sections[i].PointerToRawData;
386 ret = TRUE;
388 done:
389 HeapFree( GetProcessHeap(), 0, resSection );
390 HeapFree( GetProcessHeap(), 0, sections );
391 return ret;
395 /*************************************************************************
396 * GetFileResourceSize [VER.2]
398 DWORD WINAPI GetFileResourceSize16( LPCSTR lpszFileName, LPCSTR lpszResType,
399 LPCSTR lpszResId, LPDWORD lpdwFileOffset )
401 BOOL retv = FALSE;
402 HFILE lzfd;
403 OFSTRUCT ofs;
404 DWORD reslen;
406 TRACE("(%s,type=0x%lx,id=0x%lx,off=%p)\n",
407 debugstr_a(lpszFileName), (LONG)lpszResType, (LONG)lpszResId,
408 lpszResId );
410 lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
411 if ( lzfd < 0 ) return 0;
413 switch ( read_xx_header( lzfd ) )
415 case IMAGE_OS2_SIGNATURE:
416 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
417 &reslen, lpdwFileOffset );
418 break;
420 case IMAGE_NT_SIGNATURE:
421 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
422 &reslen, lpdwFileOffset );
423 break;
426 LZClose( lzfd );
427 return retv? reslen : 0;
431 /*************************************************************************
432 * GetFileResource [VER.3]
434 DWORD WINAPI GetFileResource16( LPCSTR lpszFileName, LPCSTR lpszResType,
435 LPCSTR lpszResId, DWORD dwFileOffset,
436 DWORD dwResLen, LPVOID lpvData )
438 BOOL retv = FALSE;
439 HFILE lzfd;
440 OFSTRUCT ofs;
441 DWORD reslen = dwResLen;
443 TRACE("(%s,type=0x%lx,id=0x%lx,off=%ld,len=%ld,data=%p)\n",
444 debugstr_a(lpszFileName), (LONG)lpszResType, (LONG)lpszResId,
445 dwFileOffset, dwResLen, lpvData );
447 lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
448 if ( lzfd < 0 ) return 0;
450 if ( !dwFileOffset )
452 switch ( read_xx_header( lzfd ) )
454 case IMAGE_OS2_SIGNATURE:
455 retv = find_ne_resource( lzfd, lpszResType, lpszResId,
456 &reslen, &dwFileOffset );
457 break;
459 case IMAGE_NT_SIGNATURE:
460 retv = find_pe_resource( lzfd, lpszResType, lpszResId,
461 &reslen, &dwFileOffset );
462 break;
465 if ( !retv )
467 LZClose( lzfd );
468 return 0;
472 LZSeek( lzfd, dwFileOffset, SEEK_SET );
473 reslen = LZRead( lzfd, lpvData, min( reslen, dwResLen ) );
474 LZClose( lzfd );
476 return reslen;