| Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors |
| license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
| [email protected] | bcff05a | 2010-04-14 01:46:43 | [diff] [blame] | 5 | #include "base/file_version_info_win.h" |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 6 | |
| [email protected] | f539333 | 2009-06-03 15:01:29 | [diff] [blame] | 7 | #include <windows.h> |
| Bruce Dawson | a1e1cfcb | 2022-11-22 20:04:35 | [diff] [blame] | 8 | |
| avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 9 | #include <stddef.h> |
| [email protected] | f539333 | 2009-06-03 15:01:29 | [diff] [blame] | 10 | |
| Lei Zhang | 4bb23de | 2019-10-04 16:17:08 | [diff] [blame] | 11 | #include <utility> |
| 12 | |
| Hans Wennborg | c3cffa6 | 2020-04-27 10:09:12 | [diff] [blame] | 13 | #include "base/check.h" |
| [email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 14 | #include "base/files/file_path.h" |
| David Benjamin | 04cc2b4 | 2019-01-29 05:30:33 | [diff] [blame] | 15 | #include "base/memory/ptr_util.h" |
| jdoerrie | 5c4dc4e | 2019-02-01 18:02:33 | [diff] [blame] | 16 | #include "base/strings/string_util.h" |
| Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 17 | #include "base/threading/scoped_blocking_call.h" |
| fdoray | 5b7de9e9 | 2016-06-29 23:13:11 | [diff] [blame] | 18 | #include "base/win/resource_util.h" |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 19 | |
| fdoray | 5b7de9e9 | 2016-06-29 23:13:11 | [diff] [blame] | 20 | namespace { |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 21 | |
| fdoray | 5b7de9e9 | 2016-06-29 23:13:11 | [diff] [blame] | 22 | struct LanguageAndCodePage { |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 23 | WORD language; |
| 24 | WORD code_page; |
| fdoray | 5b7de9e9 | 2016-06-29 23:13:11 | [diff] [blame] | 25 | }; |
| 26 | |
| Lei Zhang | 4bb23de | 2019-10-04 16:17:08 | [diff] [blame] | 27 | // Returns the \VarFileInfo\Translation value extracted from the |
| fdoray | 5b7de9e9 | 2016-06-29 23:13:11 | [diff] [blame] | 28 | // VS_VERSION_INFO resource in |data|. |
| 29 | LanguageAndCodePage* GetTranslate(const void* data) { |
| Lei Zhang | 4bb23de | 2019-10-04 16:17:08 | [diff] [blame] | 30 | static constexpr wchar_t kTranslation[] = L"\\VarFileInfo\\Translation"; |
| 31 | LPVOID translate = nullptr; |
| 32 | UINT dummy_size; |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 33 | if (::VerQueryValue(data, kTranslation, &translate, &dummy_size)) { |
| Lei Zhang | 4bb23de | 2019-10-04 16:17:08 | [diff] [blame] | 34 | return static_cast<LanguageAndCodePage*>(translate); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 35 | } |
| fdoray | 5b7de9e9 | 2016-06-29 23:13:11 | [diff] [blame] | 36 | return nullptr; |
| 37 | } |
| 38 | |
| Lei Zhang | 4bb23de | 2019-10-04 16:17:08 | [diff] [blame] | 39 | const VS_FIXEDFILEINFO& GetVsFixedFileInfo(const void* data) { |
| 40 | static constexpr wchar_t kRoot[] = L"\\"; |
| 41 | LPVOID fixed_file_info = nullptr; |
| 42 | UINT dummy_size; |
| 43 | CHECK(::VerQueryValue(data, kRoot, &fixed_file_info, &dummy_size)); |
| 44 | return *static_cast<VS_FIXEDFILEINFO*>(fixed_file_info); |
| fdoray | 5b7de9e9 | 2016-06-29 23:13:11 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | } // namespace |
| 48 | |
| 49 | FileVersionInfoWin::~FileVersionInfoWin() = default; |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 50 | |
| 51 | // static |
| David Benjamin | 04cc2b4 | 2019-01-29 05:30:33 | [diff] [blame] | 52 | std::unique_ptr<FileVersionInfo> |
| 53 | FileVersionInfo::CreateFileVersionInfoForModule(HMODULE module) { |
| fdoray | 5b7de9e9 | 2016-06-29 23:13:11 | [diff] [blame] | 54 | void* data; |
| 55 | size_t version_info_length; |
| 56 | const bool has_version_resource = base::win::GetResourceFromModule( |
| 57 | module, VS_VERSION_INFO, RT_VERSION, &data, &version_info_length); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 58 | if (!has_version_resource) { |
| fdoray | 5b7de9e9 | 2016-06-29 23:13:11 | [diff] [blame] | 59 | return nullptr; |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 60 | } |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 61 | |
| fdoray | 5b7de9e9 | 2016-06-29 23:13:11 | [diff] [blame] | 62 | const LanguageAndCodePage* translate = GetTranslate(data); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 63 | if (!translate) { |
| fdoray | 5b7de9e9 | 2016-06-29 23:13:11 | [diff] [blame] | 64 | return nullptr; |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 65 | } |
| fdoray | 5b7de9e9 | 2016-06-29 23:13:11 | [diff] [blame] | 66 | |
| David Benjamin | 04cc2b4 | 2019-01-29 05:30:33 | [diff] [blame] | 67 | return base::WrapUnique( |
| 68 | new FileVersionInfoWin(data, translate->language, translate->code_page)); |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // static |
| David Benjamin | 04cc2b4 | 2019-01-29 05:30:33 | [diff] [blame] | 72 | std::unique_ptr<FileVersionInfo> FileVersionInfo::CreateFileVersionInfo( |
| Lei Zhang | 4bb23de | 2019-10-04 16:17:08 | [diff] [blame] | 73 | const base::FilePath& file_path) { |
| David Benjamin | 04cc2b4 | 2019-01-29 05:30:33 | [diff] [blame] | 74 | return FileVersionInfoWin::CreateFileVersionInfoWin(file_path); |
| 75 | } |
| 76 | |
| 77 | // static |
| 78 | std::unique_ptr<FileVersionInfoWin> |
| Lei Zhang | 4bb23de | 2019-10-04 16:17:08 | [diff] [blame] | 79 | FileVersionInfoWin::CreateFileVersionInfoWin(const base::FilePath& file_path) { |
| Etienne Bergeron | 436d4221 | 2019-02-26 17:15:12 | [diff] [blame] | 80 | base::ScopedBlockingCall scoped_blocking_call(FROM_HERE, |
| 81 | base::BlockingType::MAY_BLOCK); |
| [email protected] | 45446a5 | 2010-11-04 17:41:00 | [diff] [blame] | 82 | |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 83 | DWORD dummy; |
| Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 84 | const wchar_t* path = file_path.value().c_str(); |
| fdoray | 5b7de9e9 | 2016-06-29 23:13:11 | [diff] [blame] | 85 | const DWORD length = ::GetFileVersionInfoSize(path, &dummy); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 86 | if (length == 0) { |
| fdoray | 5b7de9e9 | 2016-06-29 23:13:11 | [diff] [blame] | 87 | return nullptr; |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 88 | } |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 89 | |
| fdoray | 5b7de9e9 | 2016-06-29 23:13:11 | [diff] [blame] | 90 | std::vector<uint8_t> data(length, 0); |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 91 | |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 92 | if (!::GetFileVersionInfo(path, dummy, length, data.data())) { |
| fdoray | 5b7de9e9 | 2016-06-29 23:13:11 | [diff] [blame] | 93 | return nullptr; |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 94 | } |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 95 | |
| fdoray | 5b7de9e9 | 2016-06-29 23:13:11 | [diff] [blame] | 96 | const LanguageAndCodePage* translate = GetTranslate(data.data()); |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 97 | if (!translate) { |
| fdoray | 5b7de9e9 | 2016-06-29 23:13:11 | [diff] [blame] | 98 | return nullptr; |
| Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 99 | } |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 100 | |
| David Benjamin | 04cc2b4 | 2019-01-29 05:30:33 | [diff] [blame] | 101 | return base::WrapUnique(new FileVersionInfoWin( |
| 102 | std::move(data), translate->language, translate->code_page)); |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 103 | } |
| 104 | |
| Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame] | 105 | std::u16string FileVersionInfoWin::company_name() { |
| Jan Wilken Dörrie | 82577690 | 2021-03-04 20:28:35 | [diff] [blame] | 106 | return GetStringValue(u"CompanyName"); |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 107 | } |
| 108 | |
| Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame] | 109 | std::u16string FileVersionInfoWin::company_short_name() { |
| Jan Wilken Dörrie | 82577690 | 2021-03-04 20:28:35 | [diff] [blame] | 110 | return GetStringValue(u"CompanyShortName"); |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 111 | } |
| 112 | |
| Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame] | 113 | std::u16string FileVersionInfoWin::internal_name() { |
| Jan Wilken Dörrie | 82577690 | 2021-03-04 20:28:35 | [diff] [blame] | 114 | return GetStringValue(u"InternalName"); |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 115 | } |
| 116 | |
| Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame] | 117 | std::u16string FileVersionInfoWin::product_name() { |
| Jan Wilken Dörrie | 82577690 | 2021-03-04 20:28:35 | [diff] [blame] | 118 | return GetStringValue(u"ProductName"); |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 119 | } |
| 120 | |
| Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame] | 121 | std::u16string FileVersionInfoWin::product_short_name() { |
| Jan Wilken Dörrie | 82577690 | 2021-03-04 20:28:35 | [diff] [blame] | 122 | return GetStringValue(u"ProductShortName"); |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 123 | } |
| 124 | |
| Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame] | 125 | std::u16string FileVersionInfoWin::product_version() { |
| Jan Wilken Dörrie | 82577690 | 2021-03-04 20:28:35 | [diff] [blame] | 126 | return GetStringValue(u"ProductVersion"); |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 127 | } |
| 128 | |
| Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame] | 129 | std::u16string FileVersionInfoWin::file_description() { |
| Jan Wilken Dörrie | 82577690 | 2021-03-04 20:28:35 | [diff] [blame] | 130 | return GetStringValue(u"FileDescription"); |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 131 | } |
| 132 | |
| Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame] | 133 | std::u16string FileVersionInfoWin::file_version() { |
| Jan Wilken Dörrie | 82577690 | 2021-03-04 20:28:35 | [diff] [blame] | 134 | return GetStringValue(u"FileVersion"); |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 135 | } |
| 136 | |
| Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame] | 137 | std::u16string FileVersionInfoWin::original_filename() { |
| Jan Wilken Dörrie | 82577690 | 2021-03-04 20:28:35 | [diff] [blame] | 138 | return GetStringValue(u"OriginalFilename"); |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 139 | } |
| 140 | |
| Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame] | 141 | std::u16string FileVersionInfoWin::special_build() { |
| Jan Wilken Dörrie | 82577690 | 2021-03-04 20:28:35 | [diff] [blame] | 142 | return GetStringValue(u"SpecialBuild"); |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 143 | } |
| 144 | |
| Jan Wilken Dörrie | 677e0c87 | 2021-03-10 10:04:38 | [diff] [blame] | 145 | bool FileVersionInfoWin::GetValue(const char16_t* name, |
| Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame] | 146 | std::u16string* value) const { |
| Lei Zhang | 4bb23de | 2019-10-04 16:17:08 | [diff] [blame] | 147 | const struct LanguageAndCodePage lang_codepages[] = { |
| 148 | // Use the language and codepage from the DLL. |
| 149 | {language_, code_page_}, |
| 150 | // Use the default language and codepage from the DLL. |
| 151 | {::GetUserDefaultLangID(), code_page_}, |
| 152 | // Use the language from the DLL and Latin codepage (most common). |
| 153 | {language_, 1252}, |
| 154 | // Use the default language and Latin codepage (most common). |
| 155 | {::GetUserDefaultLangID(), 1252}, |
| 156 | }; |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 157 | |
| Lei Zhang | 4bb23de | 2019-10-04 16:17:08 | [diff] [blame] | 158 | for (const auto& lang_codepage : lang_codepages) { |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 159 | wchar_t sub_block[MAX_PATH]; |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 160 | _snwprintf_s(sub_block, MAX_PATH, MAX_PATH, |
| Lei Zhang | 4bb23de | 2019-10-04 16:17:08 | [diff] [blame] | 161 | L"\\StringFileInfo\\%04x%04x\\%ls", lang_codepage.language, |
| 162 | lang_codepage.code_page, base::as_wcstr(name)); |
| 163 | LPVOID value_ptr = nullptr; |
| avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 164 | uint32_t size; |
| Lei Zhang | 4bb23de | 2019-10-04 16:17:08 | [diff] [blame] | 165 | BOOL r = ::VerQueryValue(data_, sub_block, &value_ptr, &size); |
| 166 | if (r && value_ptr && size) { |
| Jan Wilken Dörrie | 677e0c87 | 2021-03-10 10:04:38 | [diff] [blame] | 167 | value->assign(static_cast<char16_t*>(value_ptr), size - 1); |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 168 | return true; |
| 169 | } |
| 170 | } |
| 171 | return false; |
| 172 | } |
| 173 | |
| Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame] | 174 | std::u16string FileVersionInfoWin::GetStringValue(const char16_t* name) const { |
| 175 | std::u16string str; |
| Lei Zhang | 4bb23de | 2019-10-04 16:17:08 | [diff] [blame] | 176 | GetValue(name, &str); |
| 177 | return str; |
| initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 178 | } |
| fdoray | 5b7de9e9 | 2016-06-29 23:13:11 | [diff] [blame] | 179 | |
| Alan Screen | e93de3a | 2019-10-02 13:56:09 | [diff] [blame] | 180 | base::Version FileVersionInfoWin::GetFileVersion() const { |
| Ali Hijazi | a887789 | 2022-11-10 20:51:03 | [diff] [blame] | 181 | return base::Version({HIWORD(fixed_file_info_->dwFileVersionMS), |
| 182 | LOWORD(fixed_file_info_->dwFileVersionMS), |
| 183 | HIWORD(fixed_file_info_->dwFileVersionLS), |
| 184 | LOWORD(fixed_file_info_->dwFileVersionLS)}); |
| Alan Screen | e93de3a | 2019-10-02 13:56:09 | [diff] [blame] | 185 | } |
| 186 | |
| fdoray | 5b7de9e9 | 2016-06-29 23:13:11 | [diff] [blame] | 187 | FileVersionInfoWin::FileVersionInfoWin(std::vector<uint8_t>&& data, |
| 188 | WORD language, |
| 189 | WORD code_page) |
| 190 | : owned_data_(std::move(data)), |
| 191 | data_(owned_data_.data()), |
| 192 | language_(language), |
| 193 | code_page_(code_page), |
| 194 | fixed_file_info_(GetVsFixedFileInfo(data_)) { |
| 195 | DCHECK(!owned_data_.empty()); |
| 196 | } |
| 197 | |
| 198 | FileVersionInfoWin::FileVersionInfoWin(void* data, |
| 199 | WORD language, |
| 200 | WORD code_page) |
| 201 | : data_(data), |
| 202 | language_(language), |
| 203 | code_page_(code_page), |
| 204 | fixed_file_info_(GetVsFixedFileInfo(data)) { |
| 205 | DCHECK(data_); |
| 206 | } |