2 * versioninfo.c: Version information support
5 * Dick Porter (dick@ximian.com)
7 * (C) 2007 Novell, Inc.
14 #include <sys/types.h>
20 #include <mono/io-layer/wapi.h>
21 #include <mono/io-layer/wapi-private.h>
22 #include <mono/io-layer/versioninfo.h>
23 #include <mono/io-layer/io-portability.h>
24 #include <mono/io-layer/error.h>
25 #include <mono/utils/strenc.h>
26 #include <mono/utils/mono-mmap.h>
29 // #define DEBUG(...) g_message(__VA_ARGS__)
34 #define ALIGN32(ptr) ptr = (gpointer)((char *)ptr + 3); ptr = (gpointer)((char *)ptr - ((gsize)ptr & 3));
36 static WapiImageSectionHeader
*
37 get_enclosing_section_header (guint32 rva
, WapiImageNTHeaders32
*nt_headers
)
39 WapiImageSectionHeader
*section
= _WAPI_IMAGE_FIRST_SECTION32 (nt_headers
);
42 for (i
= 0; i
< GUINT16_FROM_LE (nt_headers
->FileHeader
.NumberOfSections
); i
++, section
++) {
43 guint32 size
= GUINT32_FROM_LE (section
->Misc
.VirtualSize
);
45 size
= GUINT32_FROM_LE (section
->SizeOfRawData
);
48 if ((rva
>= GUINT32_FROM_LE (section
->VirtualAddress
)) &&
49 (rva
< (GUINT32_FROM_LE (section
->VirtualAddress
) + size
))) {
57 /* This works for both 32bit and 64bit files, as the differences are
58 * all after the section header block
61 get_ptr_from_rva (guint32 rva
, WapiImageNTHeaders32
*ntheaders
, gpointer file_map
)
63 WapiImageSectionHeader
*section_header
;
66 section_header
= get_enclosing_section_header (rva
, ntheaders
);
67 if (section_header
== NULL
) {
71 delta
= (guint32
)(GUINT32_FROM_LE (section_header
->VirtualAddress
) -
72 GUINT32_FROM_LE (section_header
->PointerToRawData
));
74 return((guint8
*)file_map
+ rva
- delta
);
78 scan_resource_dir (WapiImageResourceDirectory
*root
,
79 WapiImageNTHeaders32
*nt_headers
,
81 WapiImageResourceDirectoryEntry
*entry
,
82 int level
, guint32 res_id
, guint32 lang_id
,
85 WapiImageResourceDirectoryEntry swapped_entry
;
86 gboolean is_string
, is_dir
;
87 guint32 name_offset
, dir_offset
, data_offset
;
89 swapped_entry
.Name
= GUINT32_FROM_LE (entry
->Name
);
90 swapped_entry
.OffsetToData
= GUINT32_FROM_LE (entry
->OffsetToData
);
92 is_string
= swapped_entry
.NameIsString
;
93 is_dir
= swapped_entry
.DataIsDirectory
;
94 name_offset
= swapped_entry
.NameOffset
;
95 dir_offset
= swapped_entry
.OffsetToDirectory
;
96 data_offset
= swapped_entry
.OffsetToData
;
99 /* Normally holds a directory entry for each type of
102 if ((is_string
== FALSE
&&
103 name_offset
!= res_id
) ||
104 (is_string
== TRUE
)) {
107 } else if (level
== 1) {
108 /* Normally holds a directory entry for each resource
111 } else if (level
== 2) {
112 /* Normally holds a directory entry for each language
114 if ((is_string
== FALSE
&&
115 name_offset
!= lang_id
&&
117 (is_string
== TRUE
)) {
121 g_assert_not_reached ();
124 if (is_dir
== TRUE
) {
125 WapiImageResourceDirectory
*res_dir
= (WapiImageResourceDirectory
*)((guint8
*)root
+ dir_offset
);
126 WapiImageResourceDirectoryEntry
*sub_entries
= (WapiImageResourceDirectoryEntry
*)(res_dir
+ 1);
129 entries
= GUINT16_FROM_LE (res_dir
->NumberOfNamedEntries
) + GUINT16_FROM_LE (res_dir
->NumberOfIdEntries
);
131 for (i
= 0; i
< entries
; i
++) {
132 WapiImageResourceDirectoryEntry
*sub_entry
= &sub_entries
[i
];
135 ret
= scan_resource_dir (root
, nt_headers
, file_map
,
136 sub_entry
, level
+ 1, res_id
,
145 WapiImageResourceDataEntry
*data_entry
= (WapiImageResourceDataEntry
*)((guint8
*)root
+ data_offset
);
146 *size
= GUINT32_FROM_LE (data_entry
->Size
);
148 return(get_ptr_from_rva (GUINT32_FROM_LE (data_entry
->OffsetToData
), nt_headers
, file_map
));
153 find_pe_file_resources32 (gpointer file_map
, guint32 map_size
,
154 guint32 res_id
, guint32 lang_id
,
157 WapiImageDosHeader
*dos_header
;
158 WapiImageNTHeaders32
*nt_headers
;
159 WapiImageResourceDirectory
*resource_dir
;
160 WapiImageResourceDirectoryEntry
*resource_dir_entry
;
161 guint32 resource_rva
, entries
, i
;
164 dos_header
= (WapiImageDosHeader
*)file_map
;
165 if (dos_header
->e_magic
!= IMAGE_DOS_SIGNATURE
) {
166 DEBUG ("%s: Bad dos signature 0x%x", __func__
, dos_header
->e_magic
);
168 SetLastError (ERROR_INVALID_DATA
);
172 if (map_size
< sizeof(WapiImageNTHeaders32
) + GUINT32_FROM_LE (dos_header
->e_lfanew
)) {
173 DEBUG ("%s: File is too small: %d", __func__
, map_size
);
175 SetLastError (ERROR_BAD_LENGTH
);
179 nt_headers
= (WapiImageNTHeaders32
*)((guint8
*)file_map
+ GUINT32_FROM_LE (dos_header
->e_lfanew
));
180 if (nt_headers
->Signature
!= IMAGE_NT_SIGNATURE
) {
181 DEBUG ("%s: Bad NT signature 0x%x", __func__
, nt_headers
->Signature
);
183 SetLastError (ERROR_INVALID_DATA
);
187 if (nt_headers
->OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
188 /* Do 64-bit stuff */
189 resource_rva
= GUINT32_FROM_LE (((WapiImageNTHeaders64
*)nt_headers
)->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
);
191 resource_rva
= GUINT32_FROM_LE (nt_headers
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
);
194 if (resource_rva
== 0) {
195 DEBUG ("%s: No resources in file!", __func__
);
197 SetLastError (ERROR_INVALID_DATA
);
201 resource_dir
= (WapiImageResourceDirectory
*)get_ptr_from_rva (resource_rva
, (WapiImageNTHeaders32
*)nt_headers
, file_map
);
202 if (resource_dir
== NULL
) {
203 DEBUG ("%s: Can't find resource directory", __func__
);
205 SetLastError (ERROR_INVALID_DATA
);
209 entries
= GUINT16_FROM_LE (resource_dir
->NumberOfNamedEntries
) + GUINT16_FROM_LE (resource_dir
->NumberOfIdEntries
);
210 resource_dir_entry
= (WapiImageResourceDirectoryEntry
*)(resource_dir
+ 1);
212 for (i
= 0; i
< entries
; i
++) {
213 WapiImageResourceDirectoryEntry
*direntry
= &resource_dir_entry
[i
];
214 ret
= scan_resource_dir (resource_dir
,
215 (WapiImageNTHeaders32
*)nt_headers
,
216 file_map
, direntry
, 0, res_id
,
227 find_pe_file_resources64 (gpointer file_map
, guint32 map_size
,
228 guint32 res_id
, guint32 lang_id
,
231 WapiImageDosHeader
*dos_header
;
232 WapiImageNTHeaders64
*nt_headers
;
233 WapiImageResourceDirectory
*resource_dir
;
234 WapiImageResourceDirectoryEntry
*resource_dir_entry
;
235 guint32 resource_rva
, entries
, i
;
238 dos_header
= (WapiImageDosHeader
*)file_map
;
239 if (dos_header
->e_magic
!= IMAGE_DOS_SIGNATURE
) {
240 DEBUG ("%s: Bad dos signature 0x%x", __func__
, dos_header
->e_magic
);
242 SetLastError (ERROR_INVALID_DATA
);
246 if (map_size
< sizeof(WapiImageNTHeaders64
) + GUINT32_FROM_LE (dos_header
->e_lfanew
)) {
247 DEBUG ("%s: File is too small: %d", __func__
, map_size
);
249 SetLastError (ERROR_BAD_LENGTH
);
253 nt_headers
= (WapiImageNTHeaders64
*)((guint8
*)file_map
+ GUINT32_FROM_LE (dos_header
->e_lfanew
));
254 if (nt_headers
->Signature
!= IMAGE_NT_SIGNATURE
) {
255 DEBUG ("%s: Bad NT signature 0x%x", __func__
,
256 nt_headers
->Signature
);
258 SetLastError (ERROR_INVALID_DATA
);
262 if (nt_headers
->OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
263 /* Do 64-bit stuff */
264 resource_rva
= GUINT32_FROM_LE (((WapiImageNTHeaders64
*)nt_headers
)->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
);
266 resource_rva
= GUINT32_FROM_LE (nt_headers
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
);
269 if (resource_rva
== 0) {
270 DEBUG ("%s: No resources in file!", __func__
);
272 SetLastError (ERROR_INVALID_DATA
);
276 resource_dir
= (WapiImageResourceDirectory
*)get_ptr_from_rva (resource_rva
, (WapiImageNTHeaders32
*)nt_headers
, file_map
);
277 if (resource_dir
== NULL
) {
278 DEBUG ("%s: Can't find resource directory", __func__
);
280 SetLastError (ERROR_INVALID_DATA
);
284 entries
= GUINT16_FROM_LE (resource_dir
->NumberOfNamedEntries
) + GUINT16_FROM_LE (resource_dir
->NumberOfIdEntries
);
285 resource_dir_entry
= (WapiImageResourceDirectoryEntry
*)(resource_dir
+ 1);
287 for (i
= 0; i
< entries
; i
++) {
288 WapiImageResourceDirectoryEntry
*direntry
= &resource_dir_entry
[i
];
289 ret
= scan_resource_dir (resource_dir
,
290 (WapiImageNTHeaders32
*)nt_headers
,
291 file_map
, direntry
, 0, res_id
,
302 find_pe_file_resources (gpointer file_map
, guint32 map_size
,
303 guint32 res_id
, guint32 lang_id
,
306 /* Figure this out when we support 64bit PE files */
308 return find_pe_file_resources32 (file_map
, map_size
, res_id
,
311 return find_pe_file_resources64 (file_map
, map_size
, res_id
,
317 map_pe_file (gunichar2
*filename
, gint32
*map_size
, void **handle
)
324 /* According to the MSDN docs, a search path is applied to
325 * filename. FIXME: implement this, for now just pass it
329 filename_ext
= mono_unicode_to_external (filename
);
330 if (filename_ext
== NULL
) {
331 DEBUG ("%s: unicode conversion returned NULL", __func__
);
333 SetLastError (ERROR_INVALID_NAME
);
337 fd
= _wapi_open (filename_ext
, O_RDONLY
, 0);
339 DEBUG ("%s: Error opening file %s: %s", __func__
, filename_ext
, strerror (errno
));
341 SetLastError (_wapi_get_win32_file_error (errno
));
342 g_free (filename_ext
);
347 if (fstat (fd
, &statbuf
) == -1) {
348 DEBUG ("%s: Error stat()ing file %s: %s", __func__
, filename_ext
, strerror (errno
));
350 SetLastError (_wapi_get_win32_file_error (errno
));
351 g_free (filename_ext
);
355 *map_size
= statbuf
.st_size
;
357 /* Check basic file size */
358 if (statbuf
.st_size
< sizeof(WapiImageDosHeader
)) {
359 DEBUG ("%s: File %s is too small: %lld", __func__
, filename_ext
, statbuf
.st_size
);
361 SetLastError (ERROR_BAD_LENGTH
);
362 g_free (filename_ext
);
367 file_map
= mono_file_map (statbuf
.st_size
, MONO_MMAP_READ
| MONO_MMAP_PRIVATE
, fd
, 0, handle
);
368 if (file_map
== NULL
) {
369 DEBUG ("%s: Error mmap()int file %s: %s", __func__
, filename_ext
, strerror (errno
));
371 SetLastError (_wapi_get_win32_file_error (errno
));
372 g_free (filename_ext
);
377 /* Don't need the fd any more */
379 g_free (filename_ext
);
385 unmap_pe_file (gpointer file_map
, void *handle
)
387 mono_file_unmap (file_map
, handle
);
391 unicode_chars (const gunichar2
*str
)
396 if (str
[len
] == '\0') {
404 unicode_compare (const gunichar2
*str1
, const gunichar2
*str2
)
406 while (*str1
&& *str2
) {
407 if (*str1
!= *str2
) {
414 return(*str1
== *str2
);
417 /* compare a little-endian null-terminated utf16 string and a normal string.
418 * Can be used only for ascii or latin1 chars.
421 unicode_string_equals (const gunichar2
*str1
, const gchar
*str2
)
423 while (*str1
&& *str2
) {
424 if (GUINT16_TO_LE (*str1
) != *str2
) {
431 return(*str1
== *str2
);
442 /* Returns a pointer to the value data, because there's no way to know
443 * how big that data is (value_len is set to zero for most blocks :-( )
446 get_versioninfo_block (gconstpointer data
, version_data
*block
)
448 block
->data_len
= GUINT16_FROM_LE (*((guint16
*)data
));
449 data
= (char *)data
+ sizeof(guint16
);
450 block
->value_len
= GUINT16_FROM_LE (*((guint16
*)data
));
451 data
= (char *)data
+ sizeof(guint16
);
453 /* No idea what the type is supposed to indicate */
454 block
->type
= GUINT16_FROM_LE (*((guint16
*)data
));
455 data
= (char *)data
+ sizeof(guint16
);
456 block
->key
= ((gunichar2
*)data
);
458 /* Skip over the key (including the terminator) */
459 data
= ((gunichar2
*)data
) + (unicode_chars (block
->key
) + 1);
461 /* align on a 32-bit boundary */
468 get_fixedfileinfo_block (gconstpointer data
, version_data
*block
)
470 gconstpointer data_ptr
;
471 gint32 data_len
; /* signed to guard against underflow */
472 WapiFixedFileInfo
*ffi
;
474 data_ptr
= get_versioninfo_block (data
, block
);
475 data_len
= block
->data_len
;
477 if (block
->value_len
!= sizeof(WapiFixedFileInfo
)) {
478 DEBUG ("%s: FIXEDFILEINFO size mismatch", __func__
);
482 if (!unicode_string_equals (block
->key
, "VS_VERSION_INFO")) {
483 DEBUG ("%s: VS_VERSION_INFO mismatch", __func__
);
488 ffi
= ((WapiFixedFileInfo
*)data_ptr
);
489 if ((ffi
->dwSignature
!= VS_FFI_SIGNATURE
) ||
490 (ffi
->dwStrucVersion
!= VS_FFI_STRUCVERSION
)) {
491 DEBUG ("%s: FIXEDFILEINFO bad signature", __func__
);
500 get_varfileinfo_block (gconstpointer data_ptr
, version_data
*block
)
502 /* data is pointing at a Var block
504 data_ptr
= get_versioninfo_block (data_ptr
, block
);
510 get_string_block (gconstpointer data_ptr
,
511 const gunichar2
*string_key
,
512 gpointer
*string_value
,
513 guint32
*string_value_len
,
516 guint16 data_len
= block
->data_len
;
517 guint16 string_len
= 28; /* Length of the StringTable block */
518 char *orig_data_ptr
= (char *)data_ptr
- 28;
520 /* data_ptr is pointing at an array of one or more String blocks
521 * with total length (not including alignment padding) of
524 while (((char *)data_ptr
- (char *)orig_data_ptr
) < data_len
) {
525 /* align on a 32-bit boundary */
528 data_ptr
= get_versioninfo_block (data_ptr
, block
);
529 if (block
->data_len
== 0) {
530 /* We must have hit padding, so give up
533 DEBUG ("%s: Hit 0-length block, giving up", __func__
);
538 string_len
= string_len
+ block
->data_len
;
540 if (string_key
!= NULL
&&
541 string_value
!= NULL
&&
542 string_value_len
!= NULL
&&
543 unicode_compare (string_key
, block
->key
) == TRUE
) {
544 *string_value
= (gpointer
)data_ptr
;
545 *string_value_len
= block
->value_len
;
548 /* Skip over the value */
549 data_ptr
= ((gunichar2
*)data_ptr
) + block
->value_len
;
555 /* Returns a pointer to the byte following the Stringtable block, or
556 * NULL if the data read hits padding. We can't recover from this
557 * because the data length does not include padding bytes, so it's not
558 * possible to just return the start position + length
560 * If lang == NULL it means we're just stepping through this block
563 get_stringtable_block (gconstpointer data_ptr
,
565 const gunichar2
*string_key
,
566 gpointer
*string_value
,
567 guint32
*string_value_len
,
570 guint16 data_len
= block
->data_len
;
571 guint16 string_len
= 36; /* length of the StringFileInfo block */
573 gchar
*lowercase_lang
;
575 /* data_ptr is pointing at an array of StringTable blocks,
576 * with total length (not including alignment padding) of
580 while(string_len
< data_len
) {
581 /* align on a 32-bit boundary */
584 data_ptr
= get_versioninfo_block (data_ptr
, block
);
585 if (block
->data_len
== 0) {
586 /* We must have hit padding, so give up
589 DEBUG ("%s: Hit 0-length block, giving up", __func__
);
593 string_len
= string_len
+ block
->data_len
;
595 found_lang
= g_utf16_to_utf8 (block
->key
, 8, NULL
, NULL
, NULL
);
596 if (found_lang
== NULL
) {
597 DEBUG ("%s: Didn't find a valid language key, giving up", __func__
);
601 lowercase_lang
= g_utf8_strdown (found_lang
, -1);
603 found_lang
= lowercase_lang
;
604 lowercase_lang
= NULL
;
606 if (lang
!= NULL
&& !strcmp (found_lang
, lang
)) {
607 /* Got the one we're interested in */
608 data_ptr
= get_string_block (data_ptr
, string_key
,
610 string_value_len
, block
);
612 data_ptr
= get_string_block (data_ptr
, NULL
, NULL
,
618 if (data_ptr
== NULL
) {
619 /* Child block hit padding */
620 DEBUG ("%s: Child block hit 0-length block, giving up", __func__
);
628 #if G_BYTE_ORDER == G_BIG_ENDIAN
630 big_up_string_block (gconstpointer data_ptr
, version_data
*block
)
632 guint16 data_len
= block
->data_len
;
633 guint16 string_len
= 28; /* Length of the StringTable block */
635 char *orig_data_ptr
= (char *)data_ptr
- 28;
637 /* data_ptr is pointing at an array of one or more String
638 * blocks with total length (not including alignment padding)
641 while (((char *)data_ptr
- (char *)orig_data_ptr
) < data_len
) {
642 /* align on a 32-bit boundary */
645 data_ptr
= get_versioninfo_block (data_ptr
, block
);
646 if (block
->data_len
== 0) {
647 /* We must have hit padding, so give up
650 DEBUG ("%s: Hit 0-length block, giving up", __func__
);
654 string_len
= string_len
+ block
->data_len
;
656 big_value
= g_convert ((gchar
*)block
->key
,
657 unicode_chars (block
->key
) * 2,
658 "UTF-16BE", "UTF-16LE", NULL
, NULL
,
660 if (big_value
== NULL
) {
661 DEBUG ("%s: Didn't find a valid string, giving up", __func__
);
665 /* The swapped string should be exactly the same
666 * length as the original little-endian one, but only
667 * copy the number of original chars just to be on the
670 memcpy (block
->key
, big_value
, unicode_chars (block
->key
) * 2);
673 big_value
= g_convert ((gchar
*)data_ptr
,
674 unicode_chars (data_ptr
) * 2,
675 "UTF-16BE", "UTF-16LE", NULL
, NULL
,
677 if (big_value
== NULL
) {
678 DEBUG ("%s: Didn't find a valid data string, giving up", __func__
);
681 memcpy ((gpointer
)data_ptr
, big_value
,
682 unicode_chars (data_ptr
) * 2);
685 data_ptr
= ((gunichar2
*)data_ptr
) + block
->value_len
;
691 /* Returns a pointer to the byte following the Stringtable block, or
692 * NULL if the data read hits padding. We can't recover from this
693 * because the data length does not include padding bytes, so it's not
694 * possible to just return the start position + length
697 big_up_stringtable_block (gconstpointer data_ptr
, version_data
*block
)
699 guint16 data_len
= block
->data_len
;
700 guint16 string_len
= 36; /* length of the StringFileInfo block */
703 /* data_ptr is pointing at an array of StringTable blocks,
704 * with total length (not including alignment padding) of
708 while(string_len
< data_len
) {
709 /* align on a 32-bit boundary */
712 data_ptr
= get_versioninfo_block (data_ptr
, block
);
713 if (block
->data_len
== 0) {
714 /* We must have hit padding, so give up
717 DEBUG ("%s: Hit 0-length block, giving up", __func__
);
721 string_len
= string_len
+ block
->data_len
;
723 big_value
= g_convert ((gchar
*)block
->key
, 16, "UTF-16BE",
724 "UTF-16LE", NULL
, NULL
, NULL
);
725 if (big_value
== NULL
) {
726 DEBUG ("%s: Didn't find a valid string, giving up", __func__
);
730 memcpy (block
->key
, big_value
, 16);
733 data_ptr
= big_up_string_block (data_ptr
, block
);
735 if (data_ptr
== NULL
) {
736 /* Child block hit padding */
737 DEBUG ("%s: Child block hit 0-length block, giving up", __func__
);
745 /* Follows the data structures and turns all UTF-16 strings from the
746 * LE found in the resource section into UTF-16BE
749 big_up (gconstpointer datablock
, guint32 size
)
751 gconstpointer data_ptr
;
752 gint32 data_len
; /* signed to guard against underflow */
755 data_ptr
= get_fixedfileinfo_block (datablock
, &block
);
756 if (data_ptr
!= NULL
) {
757 WapiFixedFileInfo
*ffi
= (WapiFixedFileInfo
*)data_ptr
;
759 /* Byteswap all the fields */
760 ffi
->dwFileVersionMS
= GUINT32_SWAP_LE_BE (ffi
->dwFileVersionMS
);
761 ffi
->dwFileVersionLS
= GUINT32_SWAP_LE_BE (ffi
->dwFileVersionLS
);
762 ffi
->dwProductVersionMS
= GUINT32_SWAP_LE_BE (ffi
->dwProductVersionMS
);
763 ffi
->dwProductVersionLS
= GUINT32_SWAP_LE_BE (ffi
->dwProductVersionLS
);
764 ffi
->dwFileFlagsMask
= GUINT32_SWAP_LE_BE (ffi
->dwFileFlagsMask
);
765 ffi
->dwFileFlags
= GUINT32_SWAP_LE_BE (ffi
->dwFileFlags
);
766 ffi
->dwFileOS
= GUINT32_SWAP_LE_BE (ffi
->dwFileOS
);
767 ffi
->dwFileType
= GUINT32_SWAP_LE_BE (ffi
->dwFileType
);
768 ffi
->dwFileSubtype
= GUINT32_SWAP_LE_BE (ffi
->dwFileSubtype
);
769 ffi
->dwFileDateMS
= GUINT32_SWAP_LE_BE (ffi
->dwFileDateMS
);
770 ffi
->dwFileDateLS
= GUINT32_SWAP_LE_BE (ffi
->dwFileDateLS
);
772 /* The FFI and header occupies the first 92 bytes
774 data_ptr
= (char *)data_ptr
+ sizeof(WapiFixedFileInfo
);
775 data_len
= block
.data_len
- 92;
777 /* There now follow zero or one StringFileInfo blocks
778 * and zero or one VarFileInfo blocks
780 while (data_len
> 0) {
781 /* align on a 32-bit boundary */
784 data_ptr
= get_versioninfo_block (data_ptr
, &block
);
785 if (block
.data_len
== 0) {
786 /* We must have hit padding, so give
789 DEBUG ("%s: Hit 0-length block, giving up", __func__
);
793 data_len
= data_len
- block
.data_len
;
795 if (unicode_string_equals (block
.key
, "VarFileInfo")) {
796 data_ptr
= get_varfileinfo_block (data_ptr
,
798 data_ptr
= ((guchar
*)data_ptr
) + block
.value_len
;
799 } else if (unicode_string_equals (block
.key
,
801 data_ptr
= big_up_stringtable_block (data_ptr
,
805 DEBUG ("%s: Not a valid VERSIONINFO child block", __func__
);
809 if (data_ptr
== NULL
) {
810 /* Child block hit padding */
811 DEBUG ("%s: Child block hit 0-length block, giving up", __func__
);
820 VerQueryValue (gconstpointer datablock
, const gunichar2
*subblock
, gpointer
*buffer
, guint32
*len
)
822 gchar
*subblock_utf8
, *lang_utf8
= NULL
;
823 gboolean ret
= FALSE
;
825 gconstpointer data_ptr
;
826 gint32 data_len
; /* signed to guard against underflow */
827 gboolean want_var
= FALSE
;
828 gboolean want_string
= FALSE
;
830 const gunichar2
*string_key
= NULL
;
831 gpointer string_value
= NULL
;
832 guint32 string_value_len
= 0;
833 gchar
*lowercase_lang
;
835 subblock_utf8
= g_utf16_to_utf8 (subblock
, -1, NULL
, NULL
, NULL
);
836 if (subblock_utf8
== NULL
) {
840 if (!strcmp (subblock_utf8
, "\\VarFileInfo\\Translation")) {
842 } else if (!strncmp (subblock_utf8
, "\\StringFileInfo\\", 16)) {
844 memcpy (lang
, subblock
+ 16, 8 * sizeof(gunichar2
));
845 lang_utf8
= g_utf16_to_utf8 (lang
, 8, NULL
, NULL
, NULL
);
846 lowercase_lang
= g_utf8_strdown (lang_utf8
, -1);
848 lang_utf8
= lowercase_lang
;
849 lowercase_lang
= NULL
;
850 string_key
= subblock
+ 25;
853 if (!strcmp (subblock_utf8
, "\\")) {
854 data_ptr
= get_fixedfileinfo_block (datablock
, &block
);
855 if (data_ptr
!= NULL
) {
856 *buffer
= (gpointer
)data_ptr
;
857 *len
= block
.value_len
;
861 } else if (want_var
|| want_string
) {
862 data_ptr
= get_fixedfileinfo_block (datablock
, &block
);
863 if (data_ptr
!= NULL
) {
864 /* The FFI and header occupies the first 92
867 data_ptr
= (char *)data_ptr
+ sizeof(WapiFixedFileInfo
);
868 data_len
= block
.data_len
- 92;
870 /* There now follow zero or one StringFileInfo
871 * blocks and zero or one VarFileInfo blocks
873 while (data_len
> 0) {
874 /* align on a 32-bit boundary */
877 data_ptr
= get_versioninfo_block (data_ptr
,
879 if (block
.data_len
== 0) {
880 /* We must have hit padding,
881 * so give up processing now
883 DEBUG ("%s: Hit 0-length block, giving up", __func__
);
887 data_len
= data_len
- block
.data_len
;
889 if (unicode_string_equals (block
.key
, "VarFileInfo")) {
890 data_ptr
= get_varfileinfo_block (data_ptr
, &block
);
892 *buffer
= (gpointer
)data_ptr
;
893 *len
= block
.value_len
;
897 /* Skip over the Var block */
898 data_ptr
= ((guchar
*)data_ptr
) + block
.value_len
;
900 } else if (unicode_string_equals (block
.key
, "StringFileInfo")) {
901 data_ptr
= get_stringtable_block (data_ptr
, lang_utf8
, string_key
, &string_value
, &string_value_len
, &block
);
903 string_value
!= NULL
&&
904 string_value_len
!= 0) {
905 *buffer
= string_value
;
906 *len
= unicode_chars (string_value
) + 1; /* Include trailing null */
912 DEBUG ("%s: Not a valid VERSIONINFO child block", __func__
);
916 if (data_ptr
== NULL
) {
917 /* Child block hit padding */
918 DEBUG ("%s: Child block hit 0-length block, giving up", __func__
);
930 g_free (subblock_utf8
);
935 GetFileVersionInfoSize (gunichar2
*filename
, guint32
*handle
)
938 gpointer versioninfo
;
943 /* This value is unused, but set to zero */
946 file_map
= map_pe_file (filename
, &map_size
, &map_handle
);
947 if (file_map
== NULL
) {
951 versioninfo
= find_pe_file_resources (file_map
, map_size
, RT_VERSION
, 0, &size
);
952 if (versioninfo
== NULL
) {
953 /* Didn't find the resource, so set the return value
959 unmap_pe_file (file_map
, map_handle
);
965 GetFileVersionInfo (gunichar2
*filename
, guint32 handle G_GNUC_UNUSED
, guint32 len
, gpointer data
)
968 gpointer versioninfo
;
972 gboolean ret
= FALSE
;
974 file_map
= map_pe_file (filename
, &map_size
, &map_handle
);
975 if (file_map
== NULL
) {
979 versioninfo
= find_pe_file_resources (file_map
, map_size
, RT_VERSION
,
981 if (versioninfo
!= NULL
) {
982 /* This could probably process the data so that
983 * VerQueryValue() doesn't have to follow the data
984 * blocks every time. But hey, these functions aren't
985 * likely to appear in many profiles.
987 memcpy (data
, versioninfo
, len
< size
?len
:size
);
990 #if G_BYTE_ORDER == G_BIG_ENDIAN
995 unmap_pe_file (file_map
, map_handle
);
1001 copy_lang (gunichar2
*lang_out
, guint32 lang_len
, const gchar
*text
)
1004 int chars
= strlen (text
);
1007 unitext
= g_utf8_to_utf16 (text
, -1, NULL
, NULL
, NULL
);
1008 g_assert (unitext
!= NULL
);
1010 if (chars
< (lang_len
- 1)) {
1011 memcpy (lang_out
, (gpointer
)unitext
, chars
* 2);
1012 lang_out
[chars
] = '\0';
1015 memcpy (lang_out
, (gpointer
)unitext
, (lang_len
- 1) * 2);
1016 lang_out
[lang_len
] = '\0';
1026 VerLanguageName (guint32 lang
, gunichar2
*lang_out
, guint32 lang_len
)
1028 int primary
, secondary
;
1029 const char *name
= NULL
;
1031 primary
= lang
& 0x3FF;
1032 secondary
= (lang
>> 10) & 0x3F;
1038 name
= "Process Default Language";
1046 name
= "Arabic (Saudi Arabia)";
1049 name
= "Arabic (Iraq)";
1052 name
= "Arabic (Egypt)";
1055 name
= "Arabic (Libya)";
1058 name
= "Arabic (Algeria)";
1061 name
= "Arabic (Morocco)";
1064 name
= "Arabic (Tunisia)";
1067 name
= "Arabic (Oman)";
1070 name
= "Arabic (Yemen)";
1073 name
= "Arabic (Syria)";
1076 name
= "Arabic (Jordan)";
1079 name
= "Arabic (Lebanon)";
1082 name
= "Arabic (Kuwait)";
1085 name
= "Arabic (U.A.E.)";
1088 name
= "Arabic (Bahrain)";
1091 name
= "Arabic (Qatar)";
1098 name
= "Bulgarian (Bulgaria)";
1108 name
= "Catalan (Spain)";
1119 name
= "Chinese (Taiwan)";
1122 name
= "Chinese (PRC)";
1125 name
= "Chinese (Hong Kong S.A.R.)";
1128 name
= "Chinese (Singapore)";
1131 name
= "Chinese (Macau S.A.R.)";
1138 name
= "Czech (Czech Republic)";
1148 name
= "Danish (Denmark)";
1159 name
= "German (Germany)";
1162 name
= "German (Switzerland)";
1165 name
= "German (Austria)";
1168 name
= "German (Luxembourg)";
1171 name
= "German (Liechtenstein)";
1178 name
= "Greek (Greece)";
1189 name
= "English (United States)";
1192 name
= "English (United Kingdom)";
1195 name
= "English (Australia)";
1198 name
= "English (Canada)";
1201 name
= "English (New Zealand)";
1204 name
= "English (Ireland)";
1207 name
= "English (South Africa)";
1210 name
= "English (Jamaica)";
1213 name
= "English (Caribbean)";
1216 name
= "English (Belize)";
1219 name
= "English (Trinidad and Tobago)";
1222 name
= "English (Zimbabwe)";
1225 name
= "English (Philippines)";
1228 name
= "English (India)";
1231 name
= "English (Malaysia)";
1234 name
= "English (Singapore)";
1241 name
= "Spanish (Spain)";
1244 name
= "Spanish (Traditional Sort)";
1247 name
= "Spanish (Mexico)";
1250 name
= "Spanish (International Sort)";
1253 name
= "Spanish (Guatemala)";
1256 name
= "Spanish (Costa Rica)";
1259 name
= "Spanish (Panama)";
1262 name
= "Spanish (Dominican Republic)";
1265 name
= "Spanish (Venezuela)";
1268 name
= "Spanish (Colombia)";
1271 name
= "Spanish (Peru)";
1274 name
= "Spanish (Argentina)";
1277 name
= "Spanish (Ecuador)";
1280 name
= "Spanish (Chile)";
1283 name
= "Spanish (Uruguay)";
1286 name
= "Spanish (Paraguay)";
1289 name
= "Spanish (Bolivia)";
1292 name
= "Spanish (El Salvador)";
1295 name
= "Spanish (Honduras)";
1298 name
= "Spanish (Nicaragua)";
1301 name
= "Spanish (Puerto Rico)";
1304 name
= "Spanish (United States)";
1311 name
= "Finnish (Finland)";
1322 name
= "French (France)";
1325 name
= "French (Belgium)";
1328 name
= "French (Canada)";
1331 name
= "French (Switzerland)";
1334 name
= "French (Luxembourg)";
1337 name
= "French (Monaco)";
1344 name
= "Hebrew (Israel)";
1354 name
= "Hungarian (Hungary)";
1364 name
= "Icelandic (Iceland)";
1375 name
= "Italian (Italy)";
1378 name
= "Italian (Switzerland)";
1385 name
= "Japanese (Japan)";
1395 name
= "Korean (Korea)";
1406 name
= "Dutch (Netherlands)";
1409 name
= "Dutch (Belgium)";
1417 name
= "Norwegian (Bokmal)";
1420 name
= "Norwegian (Nynorsk)";
1427 name
= "Polish (Poland)";
1438 name
= "Portuguese (Brazil)";
1441 name
= "Portuguese (Portugal)";
1448 name
= "Romansh (Switzerland)";
1455 name
= "Romanian (Romania)";
1465 name
= "Russian (Russia)";
1475 name
= "Croatian (Croatia)";
1481 name
= "Serbian (Latin)";
1484 name
= "Serbian (Cyrillic)";
1487 name
= "Croatian (Bosnia and Herzegovina)";
1490 name
= "Bosnian (Latin, Bosnia and Herzegovina)";
1493 name
= "Serbian (Latin, Bosnia and Herzegovina)";
1496 name
= "Serbian (Cyrillic, Bosnia and Herzegovina)";
1499 name
= "Bosnian (Cyrillic, Bosnia and Herzegovina)";
1506 name
= "Slovak (Slovakia)";
1516 name
= "Albanian (Albania)";
1526 name
= "Swedish (Sweden)";
1532 name
= "Swedish (Finland)";
1539 name
= "Thai (Thailand)";
1549 name
= "Turkish (Turkey)";
1559 name
= "Urdu (Islamic Republic of Pakistan)";
1569 name
= "Indonesian (Indonesia)";
1572 name
= "Indonesian";
1579 name
= "Ukrainian (Ukraine)";
1589 name
= "Belarusian (Belarus)";
1592 name
= "Belarusian";
1599 name
= "Slovenian (Slovenia)";
1609 name
= "Estonian (Estonia)";
1619 name
= "Latvian (Latvia)";
1629 name
= "Lithuanian (Lithuania)";
1632 name
= "Lithuanian";
1639 name
= "Tajik (Tajikistan)";
1646 name
= "Farsi (Iran)";
1656 name
= "Vietnamese (Viet Nam)";
1659 name
= "Vietnamese";
1666 name
= "Armenian (Armenia)";
1676 name
= "Azeri (Latin) (Azerbaijan)";
1679 name
= "Azeri (Latin)";
1682 name
= "Azeri (Cyrillic)";
1689 name
= "Basque (Spain)";
1699 name
= "Upper Sorbian (Germany)";
1702 name
= "Lower Sorbian (Germany)";
1709 name
= "FYRO Macedonian (Former Yugoslav Republic of Macedonia)";
1712 name
= "FYRO Macedonian";
1719 name
= "Tswana (South Africa)";
1729 name
= "Xhosa (South Africa)";
1739 name
= "Zulu (South Africa)";
1749 name
= "Afrikaans (South Africa)";
1759 name
= "Georgian (Georgia)";
1769 name
= "Faroese (Faroe Islands)";
1779 name
= "Hindi (India)";
1789 name
= "Maltese (Malta)";
1799 name
= "Sami (Northern) (Norway)";
1802 name
= "Sami, Northern (Norway)";
1805 name
= "Sami, Northern (Sweden)";
1808 name
= "Sami, Northern (Finland)";
1811 name
= "Sami, Lule (Norway)";
1814 name
= "Sami, Lule (Sweden)";
1817 name
= "Sami, Southern (Norway)";
1820 name
= "Sami, Southern (Sweden)";
1823 name
= "Sami, Skolt (Finland)";
1826 name
= "Sami, Inari (Finland)";
1833 name
= "Irish (Ireland)";
1841 name
= "Malay (Malaysia)";
1844 name
= "Malay (Brunei Darussalam)";
1851 name
= "Kazakh (Kazakhstan)";
1861 name
= "Kyrgyz (Kyrgyzstan)";
1864 name
= "Kyrgyz (Cyrillic)";
1871 name
= "Swahili (Kenya)";
1881 name
= "Turkmen (Turkmenistan)";
1888 name
= "Uzbek (Latin) (Uzbekistan)";
1891 name
= "Uzbek (Latin)";
1894 name
= "Uzbek (Cyrillic)";
1901 name
= "Tatar (Russia)";
1912 name
= "Bengali (India)";
1919 name
= "Punjabi (India)";
1929 name
= "Gujarati (India)";
1939 name
= "Tamil (India)";
1949 name
= "Telugu (India)";
1959 name
= "Kannada (India)";
1970 name
= "Malayalam (India)";
1977 name
= "Assamese (India)";
1984 name
= "Marathi (India)";
1994 name
= "Sanskrit (India)";
2004 name
= "Mongolian (Mongolia)";
2007 name
= "Mongolian (Cyrillic)";
2010 name
= "Mongolian (PRC)";
2017 name
= "Tibetan (PRC)";
2020 name
= "Tibetan (Bhutan)";
2027 name
= "Welsh (United Kingdom)";
2037 name
= "Khmer (Cambodia)";
2044 name
= "Lao (Lao PDR)";
2051 name
= "Galician (Spain)";
2061 name
= "Konkani (India)";
2071 name
= "Syriac (Syria)";
2081 name
= "Sinhala (Sri Lanka)";
2088 name
= "Inuktitut (Syllabics, Canada)";
2091 name
= "Inuktitut (Latin, Canada)";
2098 name
= "Amharic (Ethiopia)";
2105 name
= "Tamazight (Algeria, Latin)";
2112 name
= "Nepali (Nepal)";
2119 name
= "Frisian (Netherlands)";
2126 name
= "Pashto (Afghanistan)";
2133 name
= "Filipino (Philippines)";
2140 name
= "Divehi (Maldives)";
2150 name
= "Hausa (Nigeria, Latin)";
2157 name
= "Yoruba (Nigeria)";
2165 name
= "Quechua (Bolivia)";
2168 name
= "Quechua (Ecuador)";
2171 name
= "Quechua (Peru)";
2178 name
= "Northern Sotho (South Africa)";
2181 name
= "Northern Sotho";
2188 name
= "Bashkir (Russia)";
2195 name
= "Luxembourgish (Luxembourg)";
2202 name
= "Greenlandic (Greenland)";
2216 name
= "Mapudungun (Chile)";
2223 name
= "Mohawk (Mohawk)";
2230 name
= "Breton (France)";
2237 name
= "Invariant Language (Invariant Country)";
2244 name
= "Uighur (PRC)";
2251 name
= "Maori (New Zealand)";
2261 name
= "Corsican (France)";
2268 name
= "Alsatian (France)";
2275 name
= "Yakut (Russia)";
2282 name
= "K'iche (Guatemala)";
2289 name
= "Kinyarwanda (Rwanda)";
2296 name
= "Wolof (Senegal)";
2303 name
= "Dari (Afghanistan)";
2309 name
= "Language Neutral";
2314 name
= "Language Neutral";
2316 return copy_lang (lang_out
, lang_len
, name
);