crypt32: Store CERT_KEY_CONTEXT in a platform independent way.
[wine.git] / dlls / ver.dll16 / version.c
blobdac9ff28ddfc3631d02005c205b06e4476558159
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 "winternl.h"
33 #include "winuser.h"
34 #include "winver.h"
35 #include "lzexpand.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ver);
40 /**********************************************************************
41 * find_entry_by_id
43 * Find an entry by id in a resource directory
44 * Copied from loader/pe_resource.c
46 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_id( const IMAGE_RESOURCE_DIRECTORY *dir,
47 WORD id, const void *root )
49 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
50 int min, max, pos;
52 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
53 min = dir->NumberOfNamedEntries;
54 max = min + dir->NumberOfIdEntries - 1;
55 while (min <= max)
57 pos = (min + max) / 2;
58 if (entry[pos].u.Id == id)
59 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s2.OffsetToDirectory);
60 if (entry[pos].u.Id > id) max = pos - 1;
61 else min = pos + 1;
63 return NULL;
67 /**********************************************************************
68 * find_entry_default
70 * Find a default entry in a resource directory
71 * Copied from loader/pe_resource.c
73 static const IMAGE_RESOURCE_DIRECTORY *find_entry_default( const IMAGE_RESOURCE_DIRECTORY *dir,
74 const void *root )
76 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
78 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
79 return (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry->u2.s2.OffsetToDirectory);
83 /**********************************************************************
84 * find_entry_by_name
86 * Find an entry by name in a resource directory
87 * Copied from loader/pe_resource.c
89 static const IMAGE_RESOURCE_DIRECTORY *find_entry_by_name( const IMAGE_RESOURCE_DIRECTORY *dir,
90 LPCSTR name, const void *root )
92 const IMAGE_RESOURCE_DIRECTORY *ret = NULL;
93 LPWSTR nameW;
94 DWORD namelen;
96 if (!HIWORD(name)) return find_entry_by_id( dir, LOWORD(name), root );
97 if (name[0] == '#')
99 return find_entry_by_id( dir, atoi(name+1), root );
102 namelen = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
103 if ((nameW = HeapAlloc( GetProcessHeap(), 0, namelen * sizeof(WCHAR) )))
105 const IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
106 const IMAGE_RESOURCE_DIR_STRING_U *str;
107 int min, max, res, pos;
109 MultiByteToWideChar( CP_ACP, 0, name, -1, nameW, namelen );
110 namelen--; /* remove terminating null */
111 entry = (const IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
112 min = 0;
113 max = dir->NumberOfNamedEntries - 1;
114 while (min <= max)
116 pos = (min + max) / 2;
117 str = (const IMAGE_RESOURCE_DIR_STRING_U *)((const char *)root + entry[pos].u.s.NameOffset);
118 res = wcsnicmp( nameW, str->NameString, str->Length );
119 if (!res && namelen == str->Length)
121 ret = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + entry[pos].u2.s2.OffsetToDirectory);
122 break;
124 if (res < 0) max = pos - 1;
125 else min = pos + 1;
127 HeapFree( GetProcessHeap(), 0, nameW );
129 return ret;
133 /***********************************************************************
134 * read_xx_header [internal]
136 static int read_xx_header( HFILE lzfd )
138 IMAGE_DOS_HEADER mzh;
139 char magic[3];
141 LZSeek( lzfd, 0, FILE_BEGIN );
142 if ( sizeof(mzh) != LZRead( lzfd, (LPSTR)&mzh, sizeof(mzh) ) )
143 return 0;
144 if ( mzh.e_magic != IMAGE_DOS_SIGNATURE )
145 return 0;
147 LZSeek( lzfd, mzh.e_lfanew, FILE_BEGIN );
148 if ( 2 != LZRead( lzfd, magic, 2 ) )
149 return 0;
151 LZSeek( lzfd, mzh.e_lfanew, FILE_BEGIN );
153 if ( magic[0] == 'N' && magic[1] == 'E' )
154 return IMAGE_OS2_SIGNATURE;
155 if ( magic[0] == 'P' && magic[1] == 'E' )
156 return IMAGE_NT_SIGNATURE;
158 magic[2] = '\0';
159 WARN("Can't handle %s files.\n", magic );
160 return 0;
163 /***********************************************************************
164 * find_ne_resource [internal]
166 static BOOL find_ne_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
167 DWORD *resLen, DWORD *resOff )
169 IMAGE_OS2_HEADER nehd;
170 NE_TYPEINFO *typeInfo;
171 NE_NAMEINFO *nameInfo;
172 DWORD nehdoffset;
173 LPBYTE resTab;
174 DWORD resTabSize;
175 int count;
177 /* Read in NE header */
178 nehdoffset = LZSeek( lzfd, 0, FILE_CURRENT );
179 if ( sizeof(nehd) != LZRead( lzfd, (LPSTR)&nehd, sizeof(nehd) ) ) return FALSE;
181 resTabSize = nehd.ne_restab - nehd.ne_rsrctab;
182 if ( !resTabSize )
184 TRACE("No resources in NE dll\n" );
185 return FALSE;
188 /* Read in resource table */
189 resTab = HeapAlloc( GetProcessHeap(), 0, resTabSize );
190 if ( !resTab ) return FALSE;
192 LZSeek( lzfd, nehd.ne_rsrctab + nehdoffset, FILE_BEGIN );
193 if ( resTabSize != LZRead( lzfd, (char*)resTab, resTabSize ) )
195 HeapFree( GetProcessHeap(), 0, resTab );
196 return FALSE;
199 /* Find resource */
200 typeInfo = (NE_TYPEINFO *)(resTab + 2);
202 if (HIWORD(typeid) != 0) /* named type */
204 BYTE len = strlen( typeid );
205 while (typeInfo->type_id)
207 if (!(typeInfo->type_id & 0x8000))
209 BYTE *p = resTab + typeInfo->type_id;
210 if ((*p == len) && !_strnicmp( (char*)p+1, typeid, len )) goto found_type;
212 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
213 typeInfo->count * sizeof(NE_NAMEINFO));
216 else /* numeric type id */
218 WORD id = LOWORD(typeid) | 0x8000;
219 while (typeInfo->type_id)
221 if (typeInfo->type_id == id) goto found_type;
222 typeInfo = (NE_TYPEINFO *)((char *)(typeInfo + 1) +
223 typeInfo->count * sizeof(NE_NAMEINFO));
226 TRACE("No typeid entry found for %p\n", typeid );
227 HeapFree( GetProcessHeap(), 0, resTab );
228 return FALSE;
230 found_type:
231 nameInfo = (NE_NAMEINFO *)(typeInfo + 1);
233 if (HIWORD(resid) != 0) /* named resource */
235 BYTE len = strlen( resid );
236 for (count = typeInfo->count; count > 0; count--, nameInfo++)
238 BYTE *p = resTab + nameInfo->id;
239 if (nameInfo->id & 0x8000) continue;
240 if ((*p == len) && !_strnicmp( (char*)p+1, resid, len )) goto found_name;
243 else /* numeric resource id */
245 WORD id = LOWORD(resid) | 0x8000;
246 for (count = typeInfo->count; count > 0; count--, nameInfo++)
247 if (nameInfo->id == id) goto found_name;
249 TRACE("No resid entry found for %p\n", typeid );
250 HeapFree( GetProcessHeap(), 0, resTab );
251 return FALSE;
253 found_name:
254 /* Return resource data */
255 if ( resLen ) *resLen = nameInfo->length << *(WORD *)resTab;
256 if ( resOff ) *resOff = nameInfo->offset << *(WORD *)resTab;
258 HeapFree( GetProcessHeap(), 0, resTab );
259 return TRUE;
262 /***********************************************************************
263 * find_pe_resource [internal]
265 static BOOL find_pe_resource( HFILE lzfd, LPCSTR typeid, LPCSTR resid,
266 DWORD *resLen, DWORD *resOff )
268 IMAGE_NT_HEADERS pehd;
269 DWORD pehdoffset;
270 PIMAGE_DATA_DIRECTORY resDataDir;
271 PIMAGE_SECTION_HEADER sections;
272 LPBYTE resSection;
273 DWORD resSectionSize;
274 const void *resDir;
275 const IMAGE_RESOURCE_DIRECTORY *resPtr;
276 const IMAGE_RESOURCE_DATA_ENTRY *resData;
277 int i, nSections;
278 BOOL ret = FALSE;
280 /* Read in PE header */
281 pehdoffset = LZSeek( lzfd, 0, FILE_CURRENT );
282 if ( sizeof(pehd) != LZRead( lzfd, (LPSTR)&pehd, sizeof(pehd) ) ) return FALSE;
284 resDataDir = pehd.OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
285 if ( !resDataDir->Size )
287 TRACE("No resources in PE dll\n" );
288 return FALSE;
291 /* Read in section table */
292 nSections = pehd.FileHeader.NumberOfSections;
293 sections = HeapAlloc( GetProcessHeap(), 0,
294 nSections * sizeof(IMAGE_SECTION_HEADER) );
295 if ( !sections ) return FALSE;
297 LZSeek( lzfd, pehdoffset +
298 sizeof(DWORD) + /* Signature */
299 sizeof(IMAGE_FILE_HEADER) +
300 pehd.FileHeader.SizeOfOptionalHeader, FILE_BEGIN );
302 if ( nSections * sizeof(IMAGE_SECTION_HEADER) !=
303 LZRead( lzfd, (LPSTR)sections, nSections * sizeof(IMAGE_SECTION_HEADER) ) )
305 HeapFree( GetProcessHeap(), 0, sections );
306 return FALSE;
309 /* Find resource section */
310 for ( i = 0; i < nSections; i++ )
311 if ( resDataDir->VirtualAddress >= sections[i].VirtualAddress
312 && resDataDir->VirtualAddress < sections[i].VirtualAddress +
313 sections[i].SizeOfRawData )
314 break;
316 if ( i == nSections )
318 HeapFree( GetProcessHeap(), 0, sections );
319 TRACE("Couldn't find resource section\n" );
320 return FALSE;
323 /* Read in resource section */
324 resSectionSize = sections[i].SizeOfRawData;
325 resSection = HeapAlloc( GetProcessHeap(), 0, resSectionSize );
326 if ( !resSection )
328 HeapFree( GetProcessHeap(), 0, sections );
329 return FALSE;
332 LZSeek( lzfd, sections[i].PointerToRawData, FILE_BEGIN );
333 if ( resSectionSize != LZRead( lzfd, (char*)resSection, resSectionSize ) ) goto done;
335 /* Find resource */
336 resDir = resSection + (resDataDir->VirtualAddress - sections[i].VirtualAddress);
338 resPtr = resDir;
339 resPtr = find_entry_by_name( resPtr, typeid, resDir );
340 if ( !resPtr )
342 TRACE("No typeid entry found for %p\n", typeid );
343 goto done;
345 resPtr = find_entry_by_name( resPtr, resid, resDir );
346 if ( !resPtr )
348 TRACE("No resid entry found for %p\n", resid );
349 goto done;
351 resPtr = find_entry_default( resPtr, resDir );
352 if ( !resPtr )
354 TRACE("No default language entry found for %p\n", resid );
355 goto done;
358 /* Find resource data section */
359 resData = (const IMAGE_RESOURCE_DATA_ENTRY*)resPtr;
360 for ( i = 0; i < nSections; i++ )
361 if ( resData->OffsetToData >= sections[i].VirtualAddress
362 && resData->OffsetToData < sections[i].VirtualAddress +
363 sections[i].SizeOfRawData )
364 break;
366 if ( i == nSections )
368 TRACE("Couldn't find resource data section\n" );
369 goto done;
372 /* Return resource data */
373 if ( resLen ) *resLen = resData->Size;
374 if ( resOff ) *resOff = resData->OffsetToData - sections[i].VirtualAddress
375 + sections[i].PointerToRawData;
376 ret = TRUE;
378 done:
379 HeapFree( GetProcessHeap(), 0, resSection );
380 HeapFree( GetProcessHeap(), 0, sections );
381 return ret;
385 /***********************************************************************
386 * find_resource [internal]
388 static DWORD find_resource( HFILE lzfd, LPCSTR type, LPCSTR id, DWORD *reslen, DWORD *offset )
390 DWORD magic = read_xx_header( lzfd );
392 switch (magic)
394 case IMAGE_OS2_SIGNATURE:
395 if (!find_ne_resource( lzfd, type, id, reslen, offset )) magic = 0;
396 break;
397 case IMAGE_NT_SIGNATURE:
398 if (!find_pe_resource( lzfd, type, id, reslen, offset )) magic = 0;
399 break;
401 return magic;
405 /*************************************************************************
406 * GetFileResourceSize [VER.2]
408 DWORD WINAPI GetFileResourceSize16( LPCSTR lpszFileName, LPCSTR lpszResType,
409 LPCSTR lpszResId, LPDWORD lpdwFileOffset )
411 HFILE lzfd;
412 OFSTRUCT ofs;
413 DWORD reslen = 0;
415 TRACE("(%s,type=%p,id=%p,off=%p)\n",
416 debugstr_a(lpszFileName), lpszResType, lpszResId, lpszResId );
418 lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
419 if (lzfd >= 0)
421 if (!find_resource( lzfd, lpszResType, lpszResId, &reslen, lpdwFileOffset )) reslen = 0;
422 LZClose( lzfd );
424 return reslen;
428 /*************************************************************************
429 * GetFileResource [VER.3]
431 DWORD WINAPI GetFileResource16( LPCSTR lpszFileName, LPCSTR lpszResType,
432 LPCSTR lpszResId, DWORD dwFileOffset,
433 DWORD dwResLen, LPVOID lpvData )
435 HFILE lzfd;
436 OFSTRUCT ofs;
437 DWORD reslen = dwResLen;
439 TRACE("(%s,type=%p,id=%p,off=%d,len=%d,data=%p)\n",
440 debugstr_a(lpszFileName), lpszResType, lpszResId,
441 dwFileOffset, dwResLen, lpvData );
443 lzfd = LZOpenFileA( (LPSTR)lpszFileName, &ofs, OF_READ );
444 if ( lzfd < 0 ) return 0;
446 if ( !dwFileOffset )
448 if (!find_resource( lzfd, lpszResType, lpszResId, &reslen, &dwFileOffset ))
450 LZClose( lzfd );
451 return 0;
455 LZSeek( lzfd, dwFileOffset, FILE_BEGIN );
456 reslen = LZRead( lzfd, lpvData, min( reslen, dwResLen ) );
457 LZClose( lzfd );
459 return reslen;
462 /*************************************************************************
463 * GetFileVersionInfoSize [VER.6]
465 DWORD WINAPI GetFileVersionInfoSize16( LPCSTR filename, LPDWORD handle )
467 DWORD offset;
469 TRACE("(%s, %p)\n", debugstr_a(filename), handle );
471 return GetFileResourceSize16( filename, (LPCSTR)RT_VERSION, (LPCSTR)VS_VERSION_INFO, &offset );
474 /*************************************************************************
475 * GetFileVersionInfo [VER.7]
477 DWORD WINAPI GetFileVersionInfo16( LPCSTR filename, DWORD handle, DWORD datasize, LPVOID data )
479 TRACE("(%s, %08x, %d, %p)\n", debugstr_a(filename), handle, datasize, data );
481 return GetFileResource16( filename, (LPCSTR)RT_VERSION, (LPCSTR)VS_VERSION_INFO, 0, datasize, data );
484 /*************************************************************************
485 * VerFindFile [VER.8]
487 DWORD WINAPI VerFindFile16( UINT16 flags, LPSTR lpszFilename,
488 LPSTR lpszWinDir, LPSTR lpszAppDir,
489 LPSTR lpszCurDir, UINT16 *lpuCurDirLen,
490 LPSTR lpszDestDir, UINT16 *lpuDestDirLen )
492 UINT curDirLen, destDirLen;
493 UINT *pcurDirLen = NULL, *pdestDirLen = NULL;
494 DWORD retv;
496 if (lpuCurDirLen) {
497 curDirLen = *lpuCurDirLen;
498 pcurDirLen = &curDirLen;
500 if (lpuDestDirLen) {
501 destDirLen = *lpuDestDirLen;
502 pdestDirLen = &destDirLen;
504 retv = VerFindFileA( flags, lpszFilename, lpszWinDir, lpszAppDir,
505 lpszCurDir, pcurDirLen, lpszDestDir, pdestDirLen );
506 if (lpuCurDirLen)
507 *lpuCurDirLen = (UINT16)curDirLen;
508 if (lpuDestDirLen)
509 *lpuDestDirLen = (UINT16)destDirLen;
510 return retv;
513 /*************************************************************************
514 * VerInstallFile [VER.9]
516 DWORD WINAPI VerInstallFile16( UINT16 flags,
517 LPSTR lpszSrcFilename, LPSTR lpszDestFilename,
518 LPSTR lpszSrcDir, LPSTR lpszDestDir, LPSTR lpszCurDir,
519 LPSTR lpszTmpFile, UINT16 *lpwTmpFileLen )
521 UINT filelen = *lpwTmpFileLen;
523 DWORD retv = VerInstallFileA( flags, lpszSrcFilename, lpszDestFilename,
524 lpszSrcDir, lpszDestDir, lpszCurDir,
525 lpszTmpFile, &filelen);
527 *lpwTmpFileLen = (UINT16)filelen;
528 return retv;
531 /*************************************************************************
532 * VerLanguageName [VER.10]
534 DWORD WINAPI VerLanguageName16( UINT16 uLang, LPSTR lpszLang, UINT16 cbLang )
536 return VerLanguageNameA( uLang, lpszLang, cbLang );
539 /*************************************************************************
540 * VerQueryValue [VER.11]
542 DWORD WINAPI VerQueryValue16( SEGPTR spvBlock, LPSTR lpszSubBlock,
543 SEGPTR *lpspBuffer, UINT16 *lpcb )
545 LPVOID lpvBlock = MapSL( spvBlock );
546 LPVOID buffer = lpvBlock;
547 UINT buflen;
548 DWORD retv;
550 TRACE("(%p, %s, %p, %p)\n",
551 lpvBlock, debugstr_a(lpszSubBlock), lpspBuffer, lpcb );
553 retv = VerQueryValueA( lpvBlock, lpszSubBlock, &buffer, &buflen );
554 if ( !retv ) return FALSE;
556 if ( OFFSETOF( spvBlock ) + ((char *) buffer - (char *) lpvBlock) >= 0x10000 )
558 FIXME("offset %08X too large relative to %04X:%04X\n",
559 (char *) buffer - (char *) lpvBlock, SELECTOROF( spvBlock ), OFFSETOF( spvBlock ) );
560 return FALSE;
563 if (lpcb) *lpcb = buflen;
564 *lpspBuffer = (SEGPTR) ((char *) spvBlock + ((char *) buffer - (char *) lpvBlock));
566 return retv;