Optimized {,p}MoveTo{,Ex}{,16}.
[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/winestring.h"
15 #include "wine/winuser16.h"
16 #include "wine/unicode.h"
17 #include "winerror.h"
18 #include "heap.h"
19 #include "debugtools.h"
20
21 DEFAULT_DEBUG_CHANNEL(ver);
22
23
24 /******************************************************************************
25  *
26  *   This function will print via dprintf[_]ver to stddeb debug info regarding
27  *   the file info structure vffi.
28  *      15-Feb-1998 Dimitrie Paun (dimi@cs.toronto.edu)
29  *      Added this function to clean up the code.
30  *
31  *****************************************************************************/
32 static void print_vffi_debug(VS_FIXEDFILEINFO *vffi)
33 {
34         TRACE(" structversion=%u.%u, fileversion=%u.%u.%u.%u, productversion=%u.%u.%u.%u, flagmask=0x%lx, flags=%s%s%s%s%s%s\n",
35                     HIWORD(vffi->dwStrucVersion),LOWORD(vffi->dwStrucVersion),
36                     HIWORD(vffi->dwFileVersionMS),LOWORD(vffi->dwFileVersionMS),
37                     HIWORD(vffi->dwFileVersionLS),LOWORD(vffi->dwFileVersionLS),
38                     HIWORD(vffi->dwProductVersionMS),LOWORD(vffi->dwProductVersionMS),
39                     HIWORD(vffi->dwProductVersionLS),LOWORD(vffi->dwProductVersionLS),
40                     vffi->dwFileFlagsMask,
41                     (vffi->dwFileFlags & VS_FF_DEBUG) ? "DEBUG," : "",
42                     (vffi->dwFileFlags & VS_FF_PRERELEASE) ? "PRERELEASE," : "",
43                     (vffi->dwFileFlags & VS_FF_PATCHED) ? "PATCHED," : "",
44                     (vffi->dwFileFlags & VS_FF_PRIVATEBUILD) ? "PRIVATEBUILD," : "",
45                     (vffi->dwFileFlags & VS_FF_INFOINFERRED) ? "INFOINFERRED," : "",
46                     (vffi->dwFileFlags & VS_FF_SPECIALBUILD) ? "SPECIALBUILD," : ""
47                     );
48
49         TRACE("(");
50         DPRINTF(" OS=0x%x.0x%x ",
51                 HIWORD(vffi->dwFileOS),
52                 LOWORD(vffi->dwFileOS)
53         );
54         switch (vffi->dwFileOS&0xFFFF0000) {
55         case VOS_DOS:DPRINTF("DOS,");break;
56         case VOS_OS216:DPRINTF("OS/2-16,");break;
57         case VOS_OS232:DPRINTF("OS/2-32,");break;
58         case VOS_NT:DPRINTF("NT,");break;
59         case VOS_UNKNOWN:
60         default:
61                 DPRINTF("UNKNOWN(0x%lx),",vffi->dwFileOS&0xFFFF0000);break;
62         }
63         switch (LOWORD(vffi->dwFileOS)) {
64         case VOS__BASE:DPRINTF("BASE");break;
65         case VOS__WINDOWS16:DPRINTF("WIN16");break;
66         case VOS__WINDOWS32:DPRINTF("WIN32");break;
67         case VOS__PM16:DPRINTF("PM16");break;
68         case VOS__PM32:DPRINTF("PM32");break;
69         default:DPRINTF("UNKNOWN(0x%x)",LOWORD(vffi->dwFileOS));break;
70         }
71         DPRINTF(")\n");
72
73         switch (vffi->dwFileType) {
74         default:
75         case VFT_UNKNOWN:
76                 TRACE("filetype=Unknown(0x%lx)",vffi->dwFileType);
77                 break;
78         case VFT_APP:TRACE("filetype=APP,");break;
79         case VFT_DLL:TRACE("filetype=DLL,");break;
80         case VFT_DRV:
81                 TRACE("filetype=DRV,");
82                 switch(vffi->dwFileSubtype) {
83                 default:
84                 case VFT2_UNKNOWN:
85                         DPRINTF("UNKNOWN(0x%lx)",vffi->dwFileSubtype);
86                         break;
87                 case VFT2_DRV_PRINTER:
88                         DPRINTF("PRINTER");
89                         break;
90                 case VFT2_DRV_KEYBOARD:
91                         DPRINTF("KEYBOARD");
92                         break;
93                 case VFT2_DRV_LANGUAGE:
94                         DPRINTF("LANGUAGE");
95                         break;
96                 case VFT2_DRV_DISPLAY:
97                         DPRINTF("DISPLAY");
98                         break;
99                 case VFT2_DRV_MOUSE:
100                         DPRINTF("MOUSE");
101                         break;
102                 case VFT2_DRV_NETWORK:
103                         DPRINTF("NETWORK");
104                         break;
105                 case VFT2_DRV_SYSTEM:
106                         DPRINTF("SYSTEM");
107                         break;
108                 case VFT2_DRV_INSTALLABLE:
109                         DPRINTF("INSTALLABLE");
110                         break;
111                 case VFT2_DRV_SOUND:
112                         DPRINTF("SOUND");
113                         break;
114                 case VFT2_DRV_COMM:
115                         DPRINTF("COMM");
116                         break;
117                 case VFT2_DRV_INPUTMETHOD:
118                         DPRINTF("INPUTMETHOD");
119                         break;
120                 }
121                 break;
122         case VFT_FONT:
123                 TRACE("filetype=FONT.");
124                 switch (vffi->dwFileSubtype) {
125                 default:
126                         DPRINTF("UNKNOWN(0x%lx)",vffi->dwFileSubtype);
127                         break;
128                 case VFT2_FONT_RASTER:DPRINTF("RASTER");break;
129                 case VFT2_FONT_VECTOR:DPRINTF("VECTOR");break;
130                 case VFT2_FONT_TRUETYPE:DPRINTF("TRUETYPE");break;
131                 }
132                 break;
133         case VFT_VXD:TRACE("filetype=VXD");break;
134         case VFT_STATIC_LIB:TRACE("filetype=STATIC_LIB");break;
135         }
136         DPRINTF("\n");
137         TRACE("  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 DWORD_ALIGN( base, ptr ) \
177     ( (LPBYTE)(base) + ((((LPBYTE)(ptr) - (LPBYTE)(base)) + 3) & ~3) )
178
179 #define VersionInfo16_Value( ver )  \
180     DWORD_ALIGN( (ver), (ver)->szKey + strlen((ver)->szKey) + 1 )
181 #define VersionInfo32_Value( ver )  \
182     DWORD_ALIGN( (ver), (ver)->szKey + strlenW((ver)->szKey) + 1 )
183
184 #define VersionInfo16_Children( ver )  \
185     (VS_VERSION_INFO_STRUCT16 *)( VersionInfo16_Value( ver ) + \
186                            ( ( (ver)->wValueLength + 3 ) & ~3 ) )
187 #define VersionInfo32_Children( ver )  \
188     (VS_VERSION_INFO_STRUCT32 *)( VersionInfo32_Value( ver ) + \
189                            ( ( (ver)->wValueLength * \
190                                ((ver)->bText? 2 : 1) + 3 ) & ~3 ) )
191
192 #define VersionInfo16_Next( ver ) \
193     (VS_VERSION_INFO_STRUCT16 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
194 #define VersionInfo32_Next( ver ) \
195     (VS_VERSION_INFO_STRUCT32 *)( (LPBYTE)ver + (((ver)->wLength + 3) & ~3) )
196
197 /***********************************************************************
198  *           ConvertVersionInfo32To16        [internal]
199  */
200 void ConvertVersionInfo32To16( VS_VERSION_INFO_STRUCT32 *info32, 
201                                VS_VERSION_INFO_STRUCT16 *info16 )
202 {
203     /* Copy data onto local stack to prevent overwrites */
204     WORD wLength = info32->wLength;
205     WORD wValueLength = info32->wValueLength;
206     WORD bText = info32->bText;
207     LPBYTE lpValue = VersionInfo32_Value( info32 );
208     VS_VERSION_INFO_STRUCT32 *child32 = VersionInfo32_Children( info32 );
209     VS_VERSION_INFO_STRUCT16 *child16;
210
211     TRACE("Converting %p to %p\n", info32, info16 );
212     TRACE("wLength %d, wValueLength %d, bText %d, value %p, child %p\n",
213                 wLength, wValueLength, bText, lpValue, child32 );
214
215     /* Convert key */
216     lstrcpyWtoA( info16->szKey, info32->szKey );
217
218     TRACE("Copied key from %p to %p: %s\n", info32->szKey, info16->szKey,
219                 debugstr_a(info16->szKey) );
220
221     /* Convert value */
222     if ( wValueLength == 0 )
223     {
224         info16->wValueLength = 0;
225         TRACE("No value present\n" );
226     }
227     else if ( bText )
228     {
229         info16->wValueLength = lstrlenW( (LPCWSTR)lpValue ) + 1;
230         lstrcpyWtoA( VersionInfo16_Value( info16 ), (LPCWSTR)lpValue );
231
232         TRACE("Copied value from %p to %p: %s\n", lpValue, 
233                     VersionInfo16_Value( info16 ), 
234                     debugstr_a(VersionInfo16_Value( info16 )) );
235     }
236     else
237     {
238         info16->wValueLength = wValueLength;
239         memmove( VersionInfo16_Value( info16 ), lpValue, wValueLength );
240
241         TRACE("Copied value from %p to %p: %d bytes\n", lpValue, 
242                      VersionInfo16_Value( info16 ), wValueLength );
243     }
244
245     /* Convert children */
246     child16 = VersionInfo16_Children( info16 );
247     while ( (DWORD)child32 < (DWORD)info32 + wLength )
248     {
249         VS_VERSION_INFO_STRUCT32 *nextChild = VersionInfo32_Next( child32 );
250
251         ConvertVersionInfo32To16( child32, child16 );
252
253         child16 = VersionInfo16_Next( child16 );
254         child32 = nextChild;
255     }
256
257     /* Fixup length */
258     info16->wLength = (DWORD)child16 - (DWORD)info16;
259
260     TRACE("Finished, length is %d (%p - %p)\n", 
261                 info16->wLength, info16, child16 );
262 }
263
264
265 /***********************************************************************
266  *           GetFileVersionInfoSizeA         [VERSION.2]
267  */
268 DWORD WINAPI GetFileVersionInfoSizeA( LPCSTR filename, LPDWORD handle )
269 {
270     VS_FIXEDFILEINFO *vffi;
271     DWORD len, ret, offset;
272     BYTE buf[144];
273
274     TRACE("(%s,%p)\n", debugstr_a(filename), handle );
275
276     len = GetFileResourceSize16( filename,
277                                  MAKEINTRESOURCEA(VS_FILE_INFO),
278                                  MAKEINTRESOURCEA(VS_VERSION_INFO),
279                                  &offset );
280     if (!len) return 0;
281
282     ret = GetFileResource16( filename,
283                              MAKEINTRESOURCEA(VS_FILE_INFO),
284                              MAKEINTRESOURCEA(VS_VERSION_INFO),
285                              offset, sizeof( buf ), buf );
286     if (!ret) return 0;
287
288     if ( handle ) *handle = offset;
289     
290     if ( VersionInfoIs16( buf ) )
291         vffi = (VS_FIXEDFILEINFO *)VersionInfo16_Value( (VS_VERSION_INFO_STRUCT16 *)buf );
292     else
293         vffi = (VS_FIXEDFILEINFO *)VersionInfo32_Value( (VS_VERSION_INFO_STRUCT32 *)buf );
294
295     if ( vffi->dwSignature != VS_FFI_SIGNATURE )
296     {
297         WARN("vffi->dwSignature is 0x%08lx, but not 0x%08lx!\n",
298                    vffi->dwSignature, VS_FFI_SIGNATURE );
299         return 0;
300     }
301
302     if ( ((VS_VERSION_INFO_STRUCT16 *)buf)->wLength < len )
303         len = ((VS_VERSION_INFO_STRUCT16 *)buf)->wLength;
304
305     if ( TRACE_ON(ver) )
306         print_vffi_debug( vffi );
307
308     return len;
309 }
310
311 /***********************************************************************
312  *           GetFileVersionInfoSizeW         [VERSION.3]
313  */
314 DWORD WINAPI GetFileVersionInfoSizeW( LPCWSTR filename, LPDWORD handle )
315 {
316     LPSTR fn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
317     DWORD ret = GetFileVersionInfoSizeA( fn, handle );
318     HeapFree( GetProcessHeap(), 0, fn );
319     return ret;
320 }
321
322 /***********************************************************************
323  *           GetFileVersionInfoA             [VERSION.1]
324  */
325 DWORD WINAPI GetFileVersionInfoA( LPCSTR filename, DWORD handle,
326                                     DWORD datasize, LPVOID data )
327 {
328     TRACE("(%s,%ld,size=%ld,data=%p)\n",
329                 debugstr_a(filename), handle, datasize, data );
330
331     if ( !GetFileResource16( filename, MAKEINTRESOURCEA(VS_FILE_INFO),
332                                        MAKEINTRESOURCEA(VS_VERSION_INFO),
333                                        handle, datasize, data ) )
334         return FALSE;
335
336     if (    datasize >= sizeof(VS_VERSION_INFO_STRUCT16)
337          && datasize >= ((VS_VERSION_INFO_STRUCT16 *)data)->wLength  
338          && !VersionInfoIs16( data ) )
339     {
340         /* convert resource from PE format to NE format */
341         ConvertVersionInfo32To16( (VS_VERSION_INFO_STRUCT32 *)data, 
342                                   (VS_VERSION_INFO_STRUCT16 *)data );
343     }
344
345     return TRUE;
346 }
347
348 /***********************************************************************
349  *           GetFileVersionInfoW             [VERSION.4]
350  */
351 DWORD WINAPI GetFileVersionInfoW( LPCWSTR filename, DWORD handle,
352                                     DWORD datasize, LPVOID data )
353 {
354     LPSTR fn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
355     DWORD retv = TRUE;
356
357     TRACE("(%s,%ld,size=%ld,data=%p)\n",
358                 debugstr_a(fn), handle, datasize, data );
359
360     if ( !GetFileResource16( fn, MAKEINTRESOURCEA(VS_FILE_INFO),
361                                  MAKEINTRESOURCEA(VS_VERSION_INFO),
362                                  handle, datasize, data ) )
363         retv = FALSE;
364
365     else if (    datasize >= sizeof(VS_VERSION_INFO_STRUCT16)
366               && datasize >= ((VS_VERSION_INFO_STRUCT16 *)data)->wLength 
367               && VersionInfoIs16( data ) )
368     {
369         ERR("Cannot access NE resource in %s\n", debugstr_a(fn) );
370         retv =  FALSE;
371     }
372
373     HeapFree( GetProcessHeap(), 0, fn );
374     return retv;
375 }
376
377
378 /***********************************************************************
379  *           VersionInfo16_FindChild             [internal]
380  */
381 static VS_VERSION_INFO_STRUCT16 *VersionInfo16_FindChild( VS_VERSION_INFO_STRUCT16 *info, 
382                                             LPCSTR szKey, UINT cbKey )
383 {
384     VS_VERSION_INFO_STRUCT16 *child = VersionInfo16_Children( info );
385
386     while ( (DWORD)child < (DWORD)info + info->wLength )
387     {
388         if ( !strncasecmp( child->szKey, szKey, cbKey ) )
389             return child;
390
391         if (!(child->wLength)) return NULL;
392         child = VersionInfo16_Next( child );
393     }
394
395     return NULL;
396 }
397
398 /***********************************************************************
399  *           VersionInfo32_FindChild             [internal]
400  */
401 static VS_VERSION_INFO_STRUCT32 *VersionInfo32_FindChild( VS_VERSION_INFO_STRUCT32 *info, 
402                                             LPCWSTR szKey, UINT cbKey )
403 {
404     VS_VERSION_INFO_STRUCT32 *child = VersionInfo32_Children( info );
405
406     while ( (DWORD)child < (DWORD)info + info->wLength )
407     {
408         if ( !strncmpiW( child->szKey, szKey, cbKey ) )
409             return child;
410
411         child = VersionInfo32_Next( child );
412     }
413
414     return NULL;
415 }
416
417 /***********************************************************************
418  *           VerQueryValueA              [VERSION.12]
419  */
420 DWORD WINAPI VerQueryValueA( LPVOID pBlock, LPCSTR lpSubBlock,
421                                LPVOID *lplpBuffer, UINT *puLen )
422 {
423     VS_VERSION_INFO_STRUCT16 *info = (VS_VERSION_INFO_STRUCT16 *)pBlock;
424     if ( !VersionInfoIs16( info ) )
425     {
426         ERR("called on PE resource!\n" );
427         return FALSE;
428     }
429
430     TRACE("(%p,%s,%p,%p)\n",
431                 pBlock, debugstr_a(lpSubBlock), lplpBuffer, puLen );
432
433     while ( *lpSubBlock )
434     {
435         /* Find next path component */
436         LPCSTR lpNextSlash;
437         for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
438             if ( *lpNextSlash == '\\' )
439                 break;
440
441         /* Skip empty components */
442         if ( lpNextSlash == lpSubBlock )
443         {
444             lpSubBlock++;
445             continue;
446         }
447
448         /* We have a non-empty component: search info for key */
449         info = VersionInfo16_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
450         if ( !info ) return FALSE;
451
452         /* Skip path component */
453         lpSubBlock = lpNextSlash;
454     }
455
456     /* Return value */
457     *lplpBuffer = VersionInfo16_Value( info );
458     *puLen = info->wValueLength;
459
460     return TRUE;
461 }
462
463 /***********************************************************************
464  *           VerQueryValueW              [VERSION.13]
465  */
466 DWORD WINAPI VerQueryValueW( LPVOID pBlock, LPCWSTR lpSubBlock,
467                                LPVOID *lplpBuffer, UINT *puLen )
468 {
469     VS_VERSION_INFO_STRUCT32 *info = (VS_VERSION_INFO_STRUCT32 *)pBlock;
470     if ( VersionInfoIs16( info ) )
471     {
472         ERR("called on NE resource!\n" );
473         return FALSE;
474     }
475
476     TRACE("(%p,%s,%p,%p)\n",
477                 pBlock, debugstr_w(lpSubBlock), lplpBuffer, puLen );
478
479     while ( *lpSubBlock )
480     {
481         /* Find next path component */
482         LPCWSTR lpNextSlash;
483         for ( lpNextSlash = lpSubBlock; *lpNextSlash; lpNextSlash++ )
484             if ( *lpNextSlash == '\\' )
485                 break;
486
487         /* Skip empty components */
488         if ( lpNextSlash == lpSubBlock )
489         {
490             lpSubBlock++;
491             continue;
492         }
493
494         /* We have a non-empty component: search info for key */
495         info = VersionInfo32_FindChild( info, lpSubBlock, lpNextSlash-lpSubBlock );
496         if ( !info ) return FALSE;
497
498         /* Skip path component */
499         lpSubBlock = lpNextSlash;
500     }
501
502     /* Return value */
503     *lplpBuffer = VersionInfo32_Value( info );
504     *puLen = info->wValueLength;
505
506     return TRUE;
507 }
508