- New implementation of SendMessage, ReceiveMessage, ReplyMessage functions
[wine] / dlls / version / info.c
1 /* 
2  * Implementation of VERSION.DLL - Version Info access
3  * 
4  * Copyright 1996,1997 Marcus Meissner
5  * Copyright 1997 David Cuthbert
6  * Copyright 1999 Ulrich Weigand
7  */
8
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "winreg.h"
13 #include "winver.h"
14 #include "wine/winuser16.h"
15 #include "winerror.h"
16 #include "heap.h"
17 #include "ver.h"
18 #include "debug.h"
19
20
21 /******************************************************************************
22  *
23  *   This function will print via dprintf[_]ver to stddeb debug info regarding
24  *   the file info structure vffi.
25  *      15-Feb-1998 Dimitrie Paun (dimi@cs.toronto.edu)
26  *      Added this function to clean up the code.
27  *
28  *****************************************************************************/
29 static void print_vffi_debug(VS_FIXEDFILEINFO *vffi)
30 {
31         dbg_decl_str(ver, 1024);
32
33         TRACE(ver," structversion=%u.%u, fileversion=%u.%u.%u.%u, productversion=%u.%u.%u.%u, flagmask=0x%lx, flags=%s%s%s%s%s%s\n",
34                     HIWORD(vffi->dwStrucVersion),LOWORD(vffi->dwStrucVersion),
35                     HIWORD(vffi->dwFileVersionMS),LOWORD(vffi->dwFileVersionMS),
36                     HIWORD(vffi->dwFileVersionLS),LOWORD(vffi->dwFileVersionLS),
37                     HIWORD(vffi->dwProductVersionMS),LOWORD(vffi->dwProductVersionMS),
38                     HIWORD(vffi->dwProductVersionLS),LOWORD(vffi->dwProductVersionLS),
39                     vffi->dwFileFlagsMask,
40                     (vffi->dwFileFlags & VS_FF_DEBUG) ? "DEBUG," : "",
41                     (vffi->dwFileFlags & VS_FF_PRERELEASE) ? "PRERELEASE," : "",
42                     (vffi->dwFileFlags & VS_FF_PATCHED) ? "PATCHED," : "",
43                     (vffi->dwFileFlags & VS_FF_PRIVATEBUILD) ? "PRIVATEBUILD," : "",
44                     (vffi->dwFileFlags & VS_FF_INFOINFERRED) ? "INFOINFERRED," : "",
45                     (vffi->dwFileFlags & VS_FF_SPECIALBUILD) ? "SPECIALBUILD," : ""
46                     );
47
48         dsprintf(ver," OS=0x%x.0x%x ",
49                 HIWORD(vffi->dwFileOS),
50                 LOWORD(vffi->dwFileOS)
51         );
52         switch (vffi->dwFileOS&0xFFFF0000) {
53         case VOS_DOS:dsprintf(ver,"DOS,");break;
54         case VOS_OS216:dsprintf(ver,"OS/2-16,");break;
55         case VOS_OS232:dsprintf(ver,"OS/2-32,");break;
56         case VOS_NT:dsprintf(ver,"NT,");break;
57         case VOS_UNKNOWN:
58         default:
59                 dsprintf(ver,"UNKNOWN(0x%lx),",vffi->dwFileOS&0xFFFF0000);break;
60         }
61         switch (LOWORD(vffi->dwFileOS)) {
62         case VOS__BASE:dsprintf(ver,"BASE");break;
63         case VOS__WINDOWS16:dsprintf(ver,"WIN16");break;
64         case VOS__WINDOWS32:dsprintf(ver,"WIN32");break;
65         case VOS__PM16:dsprintf(ver,"PM16");break;
66         case VOS__PM32:dsprintf(ver,"PM32");break;
67         default:dsprintf(ver,"UNKNOWN(0x%x)",LOWORD(vffi->dwFileOS));break;
68         }
69         TRACE(ver, "(%s)\n", dbg_str(ver));
70
71         dbg_reset_str(ver);
72         switch (vffi->dwFileType) {
73         default:
74         case VFT_UNKNOWN:
75                 dsprintf(ver,"filetype=Unknown(0x%lx)",vffi->dwFileType);
76                 break;
77         case VFT_APP:dsprintf(ver,"filetype=APP,");break;
78         case VFT_DLL:dsprintf(ver,"filetype=DLL,");break;
79         case VFT_DRV:
80                 dsprintf(ver,"filetype=DRV,");
81                 switch(vffi->dwFileSubtype) {
82                 default:
83                 case VFT2_UNKNOWN:
84                         dsprintf(ver,"UNKNOWN(0x%lx)",vffi->dwFileSubtype);
85                         break;
86                 case VFT2_DRV_PRINTER:
87                         dsprintf(ver,"PRINTER");
88                         break;
89                 case VFT2_DRV_KEYBOARD:
90                         dsprintf(ver,"KEYBOARD");
91                         break;
92                 case VFT2_DRV_LANGUAGE:
93                         dsprintf(ver,"LANGUAGE");
94                         break;
95                 case VFT2_DRV_DISPLAY:
96                         dsprintf(ver,"DISPLAY");
97                         break;
98                 case VFT2_DRV_MOUSE:
99                         dsprintf(ver,"MOUSE");
100                         break;
101                 case VFT2_DRV_NETWORK:
102                         dsprintf(ver,"NETWORK");
103                         break;
104                 case VFT2_DRV_SYSTEM:
105                         dsprintf(ver,"SYSTEM");
106                         break;
107                 case VFT2_DRV_INSTALLABLE:
108                         dsprintf(ver,"INSTALLABLE");
109                         break;
110                 case VFT2_DRV_SOUND:
111                         dsprintf(ver,"SOUND");
112                         break;
113                 case VFT2_DRV_COMM:
114                         dsprintf(ver,"COMM");
115                         break;
116                 case VFT2_DRV_INPUTMETHOD:
117                         dsprintf(ver,"INPUTMETHOD");
118                         break;
119                 }
120                 break;
121         case VFT_FONT:
122                 dsprintf(ver,"filetype=FONT.");
123                 switch (vffi->dwFileSubtype) {
124                 default:
125                         dsprintf(ver,"UNKNOWN(0x%lx)",vffi->dwFileSubtype);
126                         break;
127                 case VFT2_FONT_RASTER:dsprintf(ver,"RASTER");break;
128                 case VFT2_FONT_VECTOR:dsprintf(ver,"VECTOR");break;
129                 case VFT2_FONT_TRUETYPE:dsprintf(ver,"TRUETYPE");break;
130                 }
131                 break;
132         case VFT_VXD:dsprintf(ver,"filetype=VXD");break;
133         case VFT_STATIC_LIB:dsprintf(ver,"filetype=STATIC_LIB");break;
134         }
135         TRACE(ver, "%s\n", dbg_str(ver));
136
137         TRACE(ver, "  filedata=0x%lx.0x%lx\n",
138                     vffi->dwFileDateMS,vffi->dwFileDateLS);
139 }
140
141
142 /***********************************************************************
143  * Version Info Structure
144  */
145
146 typedef struct
147 {
148     WORD  wLength;
149     WORD  wValueLength;
150     CHAR  szKey[1];
151 #if 0   /* variable length structure */
152     /* DWORD aligned */
153     BYTE  Value[];
154     /* DWORD aligned */
155     VS_VERSION_INFO_STRUCT16 Children[];
156 #endif
157 } VS_VERSION_INFO_STRUCT16;
158
159 typedef struct
160 {
161     WORD  wLength;
162     WORD  wValueLength;
163     WORD  bText;
164     WCHAR szKey[1];
165 #if 0   /* variable length structure */
166     /* DWORD aligned */
167     BYTE  Value[];
168     /* DWORD aligned */
169     VS_VERSION_INFO_STRUCT32 Children[];
170 #endif
171 } VS_VERSION_INFO_STRUCT32;
172
173 #define VersionInfoIs16( ver ) \
174     ( ((VS_VERSION_INFO_STRUCT16 *)ver)->szKey[0] >= ' ' )
175
176 #define VersionInfo16_Value( ver )  \
177     (LPBYTE)( ((DWORD)((ver)->szKey) + (lstrlen32A((ver)->szKey)+1) + 3) & ~3 )
178 #define VersionInfo32_Value( ver )  \
179     (LPBYTE)( ((DWORD)((ver)->szKey) + 2*(lstrlen32W((ver)->szKey)+1) + 3) & ~3 )
180
181 #define VersionInfo16_Children( ver )  \
182     (VS_VERSION_INFO_STRUCT16 *)( VersionInfo16_Value( ver ) + \
183                            ( ( (ver)->wValueLength + 3 ) & ~3 ) )
184 #define VersionInfo32_Children( ver )  \
185     (VS_VERSION_INFO_STRUCT32 *)( VersionInfo32_Value( ver ) + \
186                            ( ( (ver)->wValueLength * \
187                                ((ver)->bText? 2 : 1) + 3 ) & ~3 ) )
188
189 #define VersionInfo16_Next( ver ) \
190     (VS_VERSION_INFO_STRUCT16 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
191 #define VersionInfo32_Next( ver ) \
192     (VS_VERSION_INFO_STRUCT32 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
193
194 /***********************************************************************
195  *           ConvertVersionInfo32To16        [internal]
196  */
197 void ConvertVersionInfo32To16( VS_VERSION_INFO_STRUCT32 *info32, 
198                                VS_VERSION_INFO_STRUCT16 *info16 )
199 {
200     /* Copy data onto local stack to prevent overwrites */
201     WORD wLength = info32->wLength;
202     WORD wValueLength = info32->wValueLength;
203     WORD bText = info32->bText;
204     LPBYTE lpValue = VersionInfo32_Value( info32 );
205     VS_VERSION_INFO_STRUCT32 *child32 = VersionInfo32_Children( info32 );
206     VS_VERSION_INFO_STRUCT16 *child16;
207
208     TRACE( ver, "Converting %p to %p\n", info32, info16 );
209     TRACE( ver, "wLength %d, wValueLength %d, bText %d, value %p, child %p\n",
210                 wLength, wValueLength, bText, lpValue, child32 );
211
212     /* Convert key */
213     lstrcpyWtoA( info16->szKey, info32->szKey );
214
215     TRACE( ver, "Copied key from %p to %p: %s\n", info32->szKey, info16->szKey,
216                 debugstr_a(info16->szKey) );
217
218     /* Convert value */
219     if ( wValueLength == 0 )
220     {
221         info16->wValueLength = 0;
222         TRACE( ver, "No value present\n" );
223     }
224     else if ( bText )
225     {
226         info16->wValueLength = lstrlen32W( (LPCWSTR)lpValue ) + 1;
227         lstrcpyWtoA( VersionInfo16_Value( info16 ), (LPCWSTR)lpValue );
228
229         TRACE( ver, "Copied value from %p to %p: %s\n", lpValue, 
230                     VersionInfo16_Value( info16 ), 
231                     debugstr_a(VersionInfo16_Value( info16 )) );
232     }
233     else
234     {
235         info16->wValueLength = wValueLength;
236         memmove( VersionInfo16_Value( info16 ), lpValue, wValueLength );
237
238         TRACE( ver, "Copied value from %p to %p: %d bytes\n", lpValue, 
239                      VersionInfo16_Value( info16 ), wValueLength );
240     }
241
242     /* Convert children */
243     child16 = VersionInfo16_Children( info16 );
244     while ( (DWORD)child32 < (DWORD)info32 + wLength )
245     {
246         VS_VERSION_INFO_STRUCT32 *nextChild = VersionInfo32_Next( child32 );
247
248         ConvertVersionInfo32To16( child32, child16 );
249
250         child16 = VersionInfo16_Next( child16 );
251         child32 = nextChild;
252     }
253
254     /* Fixup length */
255     info16->wLength = (DWORD)child16 - (DWORD)info16;
256
257     TRACE( ver, "Finished, length is %d (%p - %p)\n", 
258                 info16->wLength, info16, child16 );
259 }
260
261
262 /***********************************************************************
263  *           GetFileVersionInfoSize32A         [VERSION.2]
264  */
265 DWORD WINAPI GetFileVersionInfoSize32A( LPCSTR filename, LPDWORD handle )
266 {
267     VS_FIXEDFILEINFO *vffi;
268     DWORD len, ret, offset;
269     BYTE buf[144];
270
271     TRACE( ver, "(%s,%p)\n", debugstr_a(filename), handle );
272
273     len = GetFileResourceSize32( filename,
274                                  MAKEINTRESOURCE32A(VS_FILE_INFO),
275                                  MAKEINTRESOURCE32A(VS_VERSION_INFO),
276                                  &offset );
277     if (!len) return 0;
278
279     ret = GetFileResource32( filename,
280                              MAKEINTRESOURCE32A(VS_FILE_INFO),
281                              MAKEINTRESOURCE32A(VS_VERSION_INFO),
282                              offset, sizeof( buf ), buf );
283     if (!ret) return 0;
284
285     if ( handle ) *handle = offset;
286     
287     if ( VersionInfoIs16( buf ) )
288         vffi = (VS_FIXEDFILEINFO *)VersionInfo16_Value( (VS_VERSION_INFO_STRUCT16 *)buf );
289     else
290         vffi = (VS_FIXEDFILEINFO *)VersionInfo32_Value( (VS_VERSION_INFO_STRUCT32 *)buf );
291
292     if ( vffi->dwSignature != VS_FFI_SIGNATURE )
293     {
294         WARN( ver, "vffi->dwSignature is 0x%08lx, but not 0x%08lx!\n",
295                    vffi->dwSignature, VS_FFI_SIGNATURE );
296         return 0;
297     }
298
299     if ( ((VS_VERSION_INFO_STRUCT16 *)buf)->wLength < len )
300         len = ((VS_VERSION_INFO_STRUCT16 *)buf)->wLength;
301
302     if ( TRACE_ON( ver ) )
303         print_vffi_debug( vffi );
304
305     return len;
306 }
307
308 /***********************************************************************
309  *           GetFileVersionInfoSize32W         [VERSION.3]
310  */
311 DWORD WINAPI GetFileVersionInfoSize32W( LPCWSTR filename, LPDWORD handle )
312 {
313     LPSTR fn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
314     DWORD ret = GetFileVersionInfoSize32A( fn, handle );
315     HeapFree( GetProcessHeap(), 0, fn );
316     return ret;
317 }
318
319 /***********************************************************************
320  *           GetFileVersionInfo32A             [VERSION.1]
321  */
322 DWORD WINAPI GetFileVersionInfo32A( LPCSTR filename, DWORD handle,
323                                     DWORD datasize, LPVOID data )
324 {
325     TRACE( ver, "(%s,%ld,size=%ld,data=%p)\n",
326                 debugstr_a(filename), handle, datasize, data );
327
328     if ( !GetFileResource32( filename, MAKEINTRESOURCE32A(VS_FILE_INFO),
329                                        MAKEINTRESOURCE32A(VS_VERSION_INFO),
330                                        handle, datasize, data ) )
331         return FALSE;
332
333     if (    datasize >= sizeof(VS_VERSION_INFO_STRUCT16)
334          && datasize >= ((VS_VERSION_INFO_STRUCT16 *)data)->wLength  
335          && !VersionInfoIs16( data ) )
336     {
337         /* convert resource from PE format to NE format */
338         ConvertVersionInfo32To16( (VS_VERSION_INFO_STRUCT32 *)data, 
339                                   (VS_VERSION_INFO_STRUCT16 *)data );
340     }
341
342     return TRUE;
343 }
344
345 /***********************************************************************
346  *           GetFileVersionInfo32W             [VERSION.4]
347  */
348 DWORD WINAPI GetFileVersionInfo32W( LPCWSTR filename, DWORD handle,
349                                     DWORD datasize, LPVOID data )
350 {
351     LPSTR fn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
352     DWORD retv = TRUE;
353
354     TRACE( ver, "(%s,%ld,size=%ld,data=%p)\n",
355                 debugstr_a(fn), handle, datasize, data );
356
357     if ( !GetFileResource32( fn, MAKEINTRESOURCE32A(VS_FILE_INFO),
358                                  MAKEINTRESOURCE32A(VS_VERSION_INFO),
359                                  handle, datasize, data ) )
360         retv = FALSE;
361
362     else if (    datasize >= sizeof(VS_VERSION_INFO_STRUCT16)
363               && datasize >= ((VS_VERSION_INFO_STRUCT16 *)data)->wLength 
364               && VersionInfoIs16( data ) )
365     {
366         ERR( ver, "Cannot access NE resource in %s\n", debugstr_a(fn) );
367         retv =  FALSE;
368     }
369
370     HeapFree( GetProcessHeap(), 0, fn );
371     return retv;
372 }
373
374
375 /***********************************************************************
376  *           VersionInfo16_FindChild             [internal]
377  */
378 VS_VERSION_INFO_STRUCT16 *VersionInfo16_FindChild( VS_VERSION_INFO_STRUCT16 *info, 
379                                             LPCSTR szKey, UINT32 cbKey )
380 {
381     VS_VERSION_INFO_STRUCT16 *child = VersionInfo16_Children( info );
382
383     while ( (DWORD)child < (DWORD)info + info->wLength )
384     {
385         if ( !lstrncmpi32A( child->szKey, szKey, cbKey ) )
386             return child;
387
388         child = VersionInfo16_Next( child );
389     }
390
391     return NULL;
392 }
393
394 /***********************************************************************
395  *           VersionInfo32_FindChild             [internal]
396  */
397 VS_VERSION_INFO_STRUCT32 *VersionInfo32_FindChild( VS_VERSION_INFO_STRUCT32 *info, 
398                                             LPCWSTR szKey, UINT32 cbKey )
399 {
400     VS_VERSION_INFO_STRUCT32 *child = VersionInfo32_Children( info );
401
402     while ( (DWORD)child < (DWORD)info + info->wLength )
403     {
404         if ( !lstrncmpi32W( child->szKey, szKey, cbKey ) )
405             return child;
406
407         child = VersionInfo32_Next( child );
408     }
409
410     return NULL;
411 }
412
413 /***********************************************************************
414  *           VerQueryValue32A              [VERSION.12]
415  */
416 DWORD WINAPI VerQueryValue32A( LPVOID pBlock, LPCSTR lpSubBlock,
417                                LPVOID *lplpBuffer, UINT32 *puLen )
418 {
419     VS_VERSION_INFO_STRUCT16 *info = (VS_VERSION_INFO_STRUCT16 *)pBlock;
420     if ( !VersionInfoIs16( info ) )
421     {
422         ERR( ver, "called on PE resource!\n" );
423         return FALSE;
424     }
425
426     TRACE( ver, "(%p,%s,%p,%p)\n",
427                 pBlock, debugstr_a(lpSubBlock), lplpBuffer, puLen );
428
429     while ( *lpSubBlock )
430     {
431         /* Find next path component */
432         LPCSTR lpNextSlash;
433         for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
434             if ( *lpNextSlash == '\\' )
435                 break;
436
437         /* Skip empty components */
438         if ( lpNextSlash == lpSubBlock )
439         {
440             lpSubBlock++;
441             continue;
442         }
443
444         /* We have a non-empty component: search info for key */
445         info = VersionInfo16_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
446         if ( !info ) return FALSE;
447
448         /* Skip path component */
449         lpSubBlock = lpNextSlash;
450     }
451
452     /* Return value */
453     *lplpBuffer = VersionInfo16_Value( info );
454     *puLen = info->wValueLength;
455
456     return TRUE;
457 }
458
459 /***********************************************************************
460  *           VerQueryValue32W              [VERSION.13]
461  */
462 DWORD WINAPI VerQueryValue32W( LPVOID pBlock, LPCWSTR lpSubBlock,
463                                LPVOID *lplpBuffer, UINT32 *puLen )
464 {
465     VS_VERSION_INFO_STRUCT32 *info = (VS_VERSION_INFO_STRUCT32 *)pBlock;
466     if ( VersionInfoIs16( info ) )
467     {
468         ERR( ver, "called on NE resource!\n" );
469         return FALSE;
470     }
471
472     TRACE( ver, "(%p,%s,%p,%p)\n",
473                 pBlock, debugstr_w(lpSubBlock), lplpBuffer, puLen );
474
475     while ( *lpSubBlock )
476     {
477         /* Find next path component */
478         LPCWSTR lpNextSlash;
479         for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
480             if ( *lpNextSlash == '\\' )
481                 break;
482
483         /* Skip empty components */
484         if ( lpNextSlash == lpSubBlock )
485         {
486             lpSubBlock++;
487             continue;
488         }
489
490         /* We have a non-empty component: search info for key */
491         info = VersionInfo32_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
492         if ( !info ) return FALSE;
493
494         /* Skip path component */
495         lpSubBlock = lpNextSlash;
496     }
497
498     /* Return value */
499     *lplpBuffer = VersionInfo32_Value( info );
500     *puLen = info->wValueLength;
501
502     return TRUE;
503 }
504
505 extern LPCSTR WINE_GetLanguageName( UINT32 langid );
506
507 /***********************************************************************
508  *           VerLanguageName32A              [VERSION.9]
509  */
510 DWORD WINAPI VerLanguageName32A( UINT32 wLang, LPSTR szLang, UINT32 nSize )
511 {
512     char    buffer[80];
513     LPCSTR  name;
514     DWORD   result;
515
516     TRACE( ver, "(%d,%p,%d)\n", wLang, szLang, nSize );
517
518     /* 
519      * First, check \System\CurrentControlSet\control\Nls\Locale\<langid>
520      * from the registry.
521      */
522
523     sprintf( buffer,
524              "\\System\\CurrentControlSet\\control\\Nls\\Locale\\%08x",
525              wLang );
526
527     result = RegQueryValue32A( HKEY_LOCAL_MACHINE, buffer, szLang, (LPDWORD)&nSize );
528     if ( result == ERROR_SUCCESS || result == ERROR_MORE_DATA ) 
529         return nSize;
530
531     /* 
532      * If that fails, use the internal table 
533      * (actually, Windows stores the names in a string table resource ...)
534      */
535
536     name = WINE_GetLanguageName( wLang );
537     lstrcpyn32A( szLang, name, nSize );
538     return lstrlen32A( name );
539 }
540
541 /***********************************************************************
542  *           VerLanguageName32W              [VERSION.10]
543  */
544 DWORD WINAPI VerLanguageName32W( UINT32 wLang, LPWSTR szLang, UINT32 nSize )
545 {
546     char    buffer[80];
547     LPWSTR  keyname;
548     LPCSTR  name;
549     DWORD   result;
550
551     TRACE( ver, "(%d,%p,%d)\n", wLang, szLang, nSize );
552
553     /* 
554      * First, check \System\CurrentControlSet\control\Nls\Locale\<langid>
555      * from the registry.
556      */
557
558     sprintf( buffer,
559              "\\System\\CurrentControlSet\\control\\Nls\\Locale\\%08x",
560              wLang );
561
562     keyname = HEAP_strdupAtoW( GetProcessHeap(), 0, buffer );
563     result = RegQueryValue32W( HKEY_LOCAL_MACHINE, keyname, szLang, (LPDWORD)&nSize );
564     HeapFree( GetProcessHeap(), 0, keyname );
565
566     if ( result == ERROR_SUCCESS || result == ERROR_MORE_DATA ) 
567         return nSize;
568
569     /* 
570      * If that fails, use the internal table 
571      * (actually, Windows stores the names in a string table resource ...)
572      */
573
574     name = WINE_GetLanguageName( wLang );
575     lstrcpynAtoW( szLang, name, nSize );
576     return lstrlen32A( name );
577 }
578
579