advapi32: Make rpcrt4 a delayed import to work around circular dependencies with...
[wine/testsucceed.git] / dlls / ver.dll16 / version.c
blob950b650ca3f66be2379ff1d8257a06e0314bc0b2
1 /*
2 * Implementation of VER.DLL
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 <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
30 #include "windef.h"
31 #include "wine/winbase16.h"
32 #include "winver.h"
33 #include "lzexpand.h"
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(ver);
39 #ifndef SEEK_SET
40 #define SEEK_SET 0
41 #define SEEK_CUR 1
42 #define SEEK_END 2
43 #endif
45 /**********************************************************************
46 * find_entry_by_id
48 * Find an entry by id in a resource directory
49 * Copied from loader/pe_resource.c
51 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
52 WORD id, const void *root )
54 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
55 int min, max, pos;
57 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
58 min = dir->NumberOfNamedEntries;
59 max = min + dir->NumberOfIdEntries - 1;
60 while (min <= max)
62 pos = (min + max) / 2;
63 if (entry[pos].u1.s2.Id == id)
64 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
65 if (entry[pos].u1.s2.Id > id) max = pos - 1;
66 else min = pos + 1;
68 return NULL;
72 /**********************************************************************
73 * find_entry_default
75 * Find a default entry in a resource directory
76 * Copied from loader/pe_resource.c
78 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
79 const void *root )
81 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
83 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
84 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry->u2.s3.OffsetToDirectory);
88 /**********************************************************************
89 * find_entry_by_name
91 * Find an entry by name in a resource directory
92 * Copied from loader/pe_resource.c
94 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
95 LPCSTR name, const void *root )
97 const IMAGE_RESOURCE_DIRECTORY *ret = NULL;
98 LPWSTR nameW;
99 DWORD namelen;
101 if (!HIWORD(name)) return find_entry_by_id( dir, LOWORD(name), root );
102 if (name[0] == '#')
104 return find_entry_by_id( dir, atoi(name+1), root );
107 namelen = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
108 if ((nameW = HeapAlloc( GetProcessHeap(), 0, namelen * sizeof(WCHAR) )))
110 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
111 const IMAGE_RESOURCE_DIR_STRING_U *str;
112 int min, max, res, pos;
114 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, namelen );
115 namelen--; /* remove terminating null */
116 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
117 min = 0;
118 max = dir->NumberOfNamedEntries - 1;
119 while (min <= max)
121 pos = (min + max) / 2;
122 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const char *)root + entry[pos].u1.s1.NameOffset);
123 res = strncmpiW( nameW, str->NameString, str->Length );
124 if (!res && namelen == str->Length)
126 ret = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s3.OffsetToDirectory);
127 break;
129 if (res < 0) max = pos - 1;
130 else min = pos + 1;
132 HeapFree( GetProcessHeap(), 0, nameW );
134 return ret;
138 /***********************************************************************
139 * read_xx_header [internal]
141 static int read_xx_header( HFILE lzfd )
143 IMAGE_DOS_HEADER mzh;
144 char magic[3];
146 LZSeek( lzfd, 0, SEEK_SET );
147 if ( sizeof(mzh) != LZRead( lzfd, (LPSTR)&mzh, sizeof(mzh) ) )
148 return 0;
149 if ( mzh.e_magic != IMAGE_DOS_SIGNATURE )
150 return 0;
152 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
153 if ( 2 != LZRead( lzfd, magic, 2 ) )
154 return 0;
156 LZSeek( lzfd, mzh.e_lfanew, SEEK_SET );
158 if ( magic[0] == 'N' && magic[1] == 'E' )
159 return IMAGE_OS2_SIGNATURE;
160 if ( magic[0] == 'P' && magic[1] == 'E' )
161 return IMAGE_NT_SIGNATURE;
163 magic[2] = '\0';
164 WARN("Can't handle %s files.\n", magic );
165 return 0;
168 /***********************************************************************
169 * find_ne_resource [internal]
171 static BOOL find_ne_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
172 DWORD *resLen, DWORD *resOff )
174 IMAGE_OS2_HEADER nehd;
175 NE_TYPEINFO *typeInfo;
176 NE_NAMEINFO *nameInfo;
177 DWORD nehdoffset;
178 LPBYTE resTab;
179 DWORD resTabSize;
180 int count;
182 /* Read in NE header */
183 nehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
184 if ( sizeof(nehd) != LZRead( lzfd, (LPSTR)&nehd, sizeof(nehd) ) ) return 0;
186 resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
187 if ( !resTabSize )
189 TRACE("No resources in NE dll\n" );
190 return FALSE;
193 /* Read in resource table */
194 resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
195 if ( !resTab ) return FALSE;
197 LZSeek( lzfd, nehd.ne_rsrctab + nehdoffset, SEEK_SET );
198 if ( resTabSize != LZRead( lzfd, (char*)resTab, resTabSize ) )
200 HeapFree( GetProcessHeap(), 0, resTab );
201 return FALSE;
204 /* Find resource */
205 typeInfo = (NE_TYPEINFO *)(resTab + 2);
207 if (HIWORD(typeid) != 0) /* named type */
209 BYTE len = strlen( typeid );
210 while (typeInfo->type_id)
212 if (!(typeInfo->type_id & 0x8000))
214 BYTE *p = resTab + typeInfo->type_id;
215 if ((*p == len) && !strncasecmp( (char*)p+1, typeid, len )) goto found_type;
217 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
218 typeInfo->count * sizeof(NE_NAMEINFO));
221 else /* numeric type id */
223 WORD id = LOWORD(typeid) | 0x8000;
224 while (typeInfo->type_id)
226 if (typeInfo->type_id == id) goto found_type;
227 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
228 typeInfo->count * sizeof(NE_NAMEINFO));
231 TRACE("No typeid entry found for %p\n", typeid );
232 HeapFree( GetProcessHeap(), 0, resTab );
233 return FALSE;
235 found_type:
236 nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
238 if (HIWORD(resid) != 0) /* named resource */
240 BYTE len = strlen( resid );
241 for (count = typeInfo->count; count > 0; count--, nameInfo++)
243 BYTE *p = resTab + nameInfo->id;
244 if (nameInfo->id & 0x8000) continue;
245 if ((*p == len) && !strncasecmp( (char*)p+1, resid, len )) goto found_name;
248 else /* numeric resource id */
250 WORD id = LOWORD(resid) | 0x8000;
251 for (count = typeInfo->count; count > 0; count--, nameInfo++)
252 if (nameInfo->id == id) goto found_name;
254 TRACE("No resid entry found for %p\n", typeid );
255 HeapFree( GetProcessHeap(), 0, resTab );
256 return FALSE;
258 found_name:
259 /* Return resource data */
260 if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
261 if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
263 HeapFree( GetProcessHeap(), 0, resTab );
264 return TRUE;
267 /***********************************************************************
268 * find_pe_resource [internal]
270 static BOOL find_pe_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
271 DWORD *resLen, DWORD *resOff )
273 IMAGE_NT_HEADERS pehd;
274 DWORD pehdoffset;
275 PIMAGE_DATA_DIRECTORY resDataDir;
276 PIMAGE_SECTION_HEADER sections;
277 LPBYTE resSection;
278 DWORD resSectionSize;
279 const void *resDir;
280 const IMAGE_RESOURCE_DIRECTORY *resPtr;
281 const IMAGE_RESOURCE_DATA_ENTRY *resData;
282 int i, nSections;
283 BOOL ret = FALSE;
285 /* Read in PE header */
286 pehdoffset = LZSeek( lzfd, 0, SEEK_CUR );
287 if ( sizeof(pehd) != LZRead( lzfd, (LPSTR)&pehd, sizeof(pehd) ) ) return 0;
289 resDataDir = pehd.OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
290 if ( !resDataDir->Size )
292 TRACE("No resources in PE dll\n" );
293 return FALSE;
296 /* Read in section table */
297 nSections = pehd.FileHeader.NumberOfSections;
298 sections = HeapAlloc( GetProcessHeap(), 0,
299 nSections * sizeof(IMAGE_SECTION_HEADER) );
300 if ( !sections ) return FALSE;
302 LZSeek( lzfd, pehdoffset +
303 sizeof(DWORD) + /* Signature */
304 sizeof(IMAGE_FILE_HEADER) +
305 pehd.FileHeader.SizeOfOptionalHeader, SEEK_SET );
307 if ( nSections * sizeof(IMAGE_SECTION_HEADER) !=
308 LZRead( lzfd, (LPSTR)sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) )
310 HeapFree( GetProcessHeap(), 0, sections );
311 return FALSE;
314 /* Find resource section */
315 for ( i = 0; i < nSections; i++ )
316 if ( resDataDir->VirtualAddress >= sections[i].VirtualAddress
317 && resDataDir->VirtualAddress < sections[i].VirtualAddress +
318 sections[i].SizeOfRawData )
319 break;
321 if ( i == nSections )
323 HeapFree( GetProcessHeap(), 0, sections );
324 TRACE("Couldn't find resource section\n" );
325 return FALSE;
328 /* Read in resource section */
329 resSectionSize = sections[i].SizeOfRawData;
330 resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
331 if ( !resSection )
333 HeapFree( GetProcessHeap(), 0, sections );
334 return FALSE;
337 LZSeek( lzfd, sections[i].PointerToRawData, SEEK_SET );
338 if ( resSectionSize != LZRead( lzfd, (char*)resSection, resSectionSize ) ) goto done;
340 /* Find resource */
341 resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
343 resPtr = resDir;
344 resPtr = find_entry_by_name( resPtr, typeid, resDir );
345 if ( !resPtr )
347 TRACE("No typeid entry found for %p\n", typeid );
348 goto done;
350 resPtr = find_entry_by_name( resPtr, resid, resDir );
351 if ( !resPtr )
353 TRACE("No resid entry found for %p\n", resid );
354 goto done;
356 resPtr = find_entry_default( resPtr, resDir );
357 if ( !resPtr )
359 TRACE("No default language entry found for %p\n", resid );
360 goto done;
363 /* Find resource data section */
364 resData = (const IMAGE_RESOURCE_DATA_ENTRY*)resPtr;
365 for ( i = 0; i < nSections; i++ )
366 if ( resData->OffsetToData >= sections[i].VirtualAddress
367 && resData->OffsetToData < sections[i].VirtualAddress +
368 sections[i].SizeOfRawData )
369 break;
371 if ( i == nSections )
373 TRACE("Couldn't find resource data section\n" );
374 goto done;
377 /* Return resource data */
378 if ( resLen ) *resLen = resData->Size;
379 if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
380 + sections[i].PointerToRawData;
381 ret = TRUE;
383 done:
384 HeapFree( GetProcessHeap(), 0, resSection );
385 HeapFree( GetProcessHeap(), 0, sections );
386 return ret;
390 /***********************************************************************
391 * find_resource [internal]
393 static DWORD find_resource( HFILE lzfd, LPCSTR type, LPCSTR id, DWORD *reslen, DWORD *offset )
395 DWORD magic = read_xx_header( lzfd );
397 switch (magic)
399 case IMAGE_OS2_SIGNATURE:
400 if (!find_ne_resource( lzfd, type, id, reslen, offset )) magic = 0;
401 break;
402 case IMAGE_NT_SIGNATURE:
403 if (!find_pe_resource( lzfd, type, id, reslen, offset )) magic = 0;
404 break;
406 return magic;
410 /*************************************************************************
411 * GetFileResourceSize [VER.2]
413 DWORD WINAPI GetFileResourceSize16( LPCSTR lpszFileName, LPCSTR lpszResType,
414 LPCSTR lpszResId, LPDWORD lpdwFileOffset )
416 HFILE lzfd;
417 OFSTRUCT ofs;
418 DWORD reslen = 0;
420 TRACE("(%s,type=%p,id=%p,off=%p)\n",
421 debugstr_a(lpszFileName), lpszResType, lpszResId, lpszResId );
423 lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
424 if (lzfd >= 0)
426 if (!find_resource( lzfd, lpszResType, lpszResId, &reslen, lpdwFileOffset )) reslen = 0;
427 LZClose( lzfd );
429 return reslen;
433 /*************************************************************************
434 * GetFileResource [VER.3]
436 DWORD WINAPI GetFileResource16( LPCSTR lpszFileName, LPCSTR lpszResType,
437 LPCSTR lpszResId, DWORD dwFileOffset,
438 DWORD dwResLen, LPVOID lpvData )
440 HFILE lzfd;
441 OFSTRUCT ofs;
442 DWORD reslen = dwResLen;
444 TRACE("(%s,type=%p,id=%p,off=%d,len=%d,data=%p)\n",
445 debugstr_a(lpszFileName), lpszResType, lpszResId,
446 dwFileOffset, dwResLen, lpvData );
448 lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
449 if ( lzfd < 0 ) return 0;
451 if ( !dwFileOffset )
453 if (!find_resource( lzfd, lpszResType, lpszResId, &reslen, &dwFileOffset ))
455 LZClose( lzfd );
456 return 0;
460 LZSeek( lzfd, dwFileOffset, SEEK_SET );
461 reslen = LZRead( lzfd, lpvData, min( reslen, dwResLen ) );
462 LZClose( lzfd );
464 return reslen;
467 /*************************************************************************
468 * GetFileVersionInfoSize [VER.6]
470 DWORD WINAPI GetFileVersionInfoSize16( LPCSTR lpszFileName, LPDWORD lpdwHandle )
472 TRACE("(%s, %p)\n", debugstr_a(lpszFileName), lpdwHandle );
473 return GetFileVersionInfoSizeA( lpszFileName, lpdwHandle );
476 /*************************************************************************
477 * GetFileVersionInfo [VER.7]
479 DWORD WINAPI GetFileVersionInfo16( LPCSTR lpszFileName, DWORD handle,
480 DWORD cbBuf, LPVOID lpvData )
482 TRACE("(%s, %08x, %d, %p)\n",
483 debugstr_a(lpszFileName), handle, cbBuf, lpvData );
485 return GetFileVersionInfoA( lpszFileName, handle, cbBuf, lpvData );
488 /*************************************************************************
489 * VerFindFile [VER.8]
491 DWORD WINAPI VerFindFile16( UINT16 flags, LPSTR lpszFilename,
492 LPSTR lpszWinDir, LPSTR lpszAppDir,
493 LPSTR lpszCurDir, UINT16 *lpuCurDirLen,
494 LPSTR lpszDestDir, UINT16 *lpuDestDirLen )
496 UINT curDirLen, destDirLen;
497 DWORD retv = VerFindFileA( flags, lpszFilename, lpszWinDir, lpszAppDir,
498 lpszCurDir, &curDirLen, lpszDestDir, &destDirLen );
500 *lpuCurDirLen = (UINT16)curDirLen;
501 *lpuDestDirLen = (UINT16)destDirLen;
502 return retv;
505 /*************************************************************************
506 * VerInstallFile [VER.9]
508 DWORD WINAPI VerInstallFile16( UINT16 flags,
509 LPSTR lpszSrcFilename, LPSTR lpszDestFilename,
510 LPSTR lpszSrcDir, LPSTR lpszDestDir, LPSTR lpszCurDir,
511 LPSTR lpszTmpFile, UINT16 *lpwTmpFileLen )
513 UINT filelen;
514 DWORD retv = VerInstallFileA( flags, lpszSrcFilename, lpszDestFilename,
515 lpszSrcDir, lpszDestDir, lpszCurDir,
516 lpszTmpFile, &filelen);
518 *lpwTmpFileLen = (UINT16)filelen;
519 return retv;
522 /*************************************************************************
523 * VerLanguageName [VER.10]
525 DWORD WINAPI VerLanguageName16( UINT16 uLang, LPSTR lpszLang, UINT16 cbLang )
527 return VerLanguageNameA( uLang, lpszLang, cbLang );
530 /*************************************************************************
531 * VerQueryValue [VER.11]
533 DWORD WINAPI VerQueryValue16( SEGPTR spvBlock, LPSTR lpszSubBlock,
534 SEGPTR *lpspBuffer, UINT16 *lpcb )
536 LPVOID lpvBlock = MapSL( spvBlock );
537 LPVOID buffer = lpvBlock;
538 UINT buflen;
539 DWORD retv;
541 TRACE("(%p, %s, %p, %p)\n",
542 lpvBlock, debugstr_a(lpszSubBlock), lpspBuffer, lpcb );
544 retv = VerQueryValueA( lpvBlock, lpszSubBlock, &buffer, &buflen );
545 if ( !retv ) return FALSE;
547 if ( OFFSETOF( spvBlock ) + ((char *) buffer - (char *) lpvBlock) >= 0x10000 )
549 FIXME("offset %08X too large relative to %04X:%04X\n",
550 (char *) buffer - (char *) lpvBlock, SELECTOROF( spvBlock ), OFFSETOF( spvBlock ) );
551 return FALSE;
554 if (lpcb) *lpcb = buflen;
555 *lpspBuffer = (SEGPTR) ((char *) spvBlock + ((char *) buffer - (char *) lpvBlock));
557 return retv;