blob: 6813ec4f97a94565aa747d40536ea8f96faf2551 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2011 The Chromium Authors
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]bcff05a2010-04-14 01:46:435#include "base/file_version_info_win.h"
initial.commitd7cae122008-07-26 21:49:386
[email protected]f5393332009-06-03 15:01:297#include <windows.h>
Bruce Dawsona1e1cfcb2022-11-22 20:04:358
avi9b6f42932015-12-26 22:15:149#include <stddef.h>
[email protected]f5393332009-06-03 15:01:2910
Lei Zhang4bb23de2019-10-04 16:17:0811#include <utility>
12
Hans Wennborgc3cffa62020-04-27 10:09:1213#include "base/check.h"
[email protected]57999812013-02-24 05:40:5214#include "base/files/file_path.h"
David Benjamin04cc2b42019-01-29 05:30:3315#include "base/memory/ptr_util.h"
jdoerrie5c4dc4e2019-02-01 18:02:3316#include "base/strings/string_util.h"
Etienne Pierre-Doray3879b052018-09-17 14:17:2217#include "base/threading/scoped_blocking_call.h"
fdoray5b7de9e92016-06-29 23:13:1118#include "base/win/resource_util.h"
initial.commitd7cae122008-07-26 21:49:3819
fdoray5b7de9e92016-06-29 23:13:1120namespace {
initial.commitd7cae122008-07-26 21:49:3821
fdoray5b7de9e92016-06-29 23:13:1122struct LanguageAndCodePage {
initial.commitd7cae122008-07-26 21:49:3823 WORD language;
24 WORD code_page;
fdoray5b7de9e92016-06-29 23:13:1125};
26
Lei Zhang4bb23de2019-10-04 16:17:0827// Returns the \VarFileInfo\Translation value extracted from the
fdoray5b7de9e92016-06-29 23:13:1128// VS_VERSION_INFO resource in |data|.
29LanguageAndCodePage* GetTranslate(const void* data) {
Lei Zhang4bb23de2019-10-04 16:17:0830 static constexpr wchar_t kTranslation[] = L"\\VarFileInfo\\Translation";
31 LPVOID translate = nullptr;
32 UINT dummy_size;
33 if (::VerQueryValue(data, kTranslation, &translate, &dummy_size))
34 return static_cast<LanguageAndCodePage*>(translate);
fdoray5b7de9e92016-06-29 23:13:1135 return nullptr;
36}
37
Lei Zhang4bb23de2019-10-04 16:17:0838const VS_FIXEDFILEINFO& GetVsFixedFileInfo(const void* data) {
39 static constexpr wchar_t kRoot[] = L"\\";
40 LPVOID fixed_file_info = nullptr;
41 UINT dummy_size;
42 CHECK(::VerQueryValue(data, kRoot, &fixed_file_info, &dummy_size));
43 return *static_cast<VS_FIXEDFILEINFO*>(fixed_file_info);
fdoray5b7de9e92016-06-29 23:13:1144}
45
46} // namespace
47
48FileVersionInfoWin::~FileVersionInfoWin() = default;
initial.commitd7cae122008-07-26 21:49:3849
50// static
David Benjamin04cc2b42019-01-29 05:30:3351std::unique_ptr<FileVersionInfo>
52FileVersionInfo::CreateFileVersionInfoForModule(HMODULE module) {
fdoray5b7de9e92016-06-29 23:13:1153 void* data;
54 size_t version_info_length;
55 const bool has_version_resource = base::win::GetResourceFromModule(
56 module, VS_VERSION_INFO, RT_VERSION, &data, &version_info_length);
57 if (!has_version_resource)
58 return nullptr;
initial.commitd7cae122008-07-26 21:49:3859
fdoray5b7de9e92016-06-29 23:13:1160 const LanguageAndCodePage* translate = GetTranslate(data);
61 if (!translate)
62 return nullptr;
63
David Benjamin04cc2b42019-01-29 05:30:3364 return base::WrapUnique(
65 new FileVersionInfoWin(data, translate->language, translate->code_page));
initial.commitd7cae122008-07-26 21:49:3866}
67
68// static
David Benjamin04cc2b42019-01-29 05:30:3369std::unique_ptr<FileVersionInfo> FileVersionInfo::CreateFileVersionInfo(
Lei Zhang4bb23de2019-10-04 16:17:0870 const base::FilePath& file_path) {
David Benjamin04cc2b42019-01-29 05:30:3371 return FileVersionInfoWin::CreateFileVersionInfoWin(file_path);
72}
73
74// static
75std::unique_ptr<FileVersionInfoWin>
Lei Zhang4bb23de2019-10-04 16:17:0876FileVersionInfoWin::CreateFileVersionInfoWin(const base::FilePath& file_path) {
Etienne Bergeron436d42212019-02-26 17:15:1277 base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
78 base::BlockingType::MAY_BLOCK);
[email protected]45446a52010-11-04 17:41:0079
initial.commitd7cae122008-07-26 21:49:3880 DWORD dummy;
Jan Wilken Dörrieb630aca2019-12-04 10:59:1181 const wchar_t* path = file_path.value().c_str();
fdoray5b7de9e92016-06-29 23:13:1182 const DWORD length = ::GetFileVersionInfoSize(path, &dummy);
initial.commitd7cae122008-07-26 21:49:3883 if (length == 0)
fdoray5b7de9e92016-06-29 23:13:1184 return nullptr;
initial.commitd7cae122008-07-26 21:49:3885
fdoray5b7de9e92016-06-29 23:13:1186 std::vector<uint8_t> data(length, 0);
initial.commitd7cae122008-07-26 21:49:3887
fdoray5b7de9e92016-06-29 23:13:1188 if (!::GetFileVersionInfo(path, dummy, length, data.data()))
89 return nullptr;
initial.commitd7cae122008-07-26 21:49:3890
fdoray5b7de9e92016-06-29 23:13:1191 const LanguageAndCodePage* translate = GetTranslate(data.data());
92 if (!translate)
93 return nullptr;
initial.commitd7cae122008-07-26 21:49:3894
David Benjamin04cc2b42019-01-29 05:30:3395 return base::WrapUnique(new FileVersionInfoWin(
96 std::move(data), translate->language, translate->code_page));
initial.commitd7cae122008-07-26 21:49:3897}
98
Jan Wilken Dörrie85285b02021-03-11 23:38:4799std::u16string FileVersionInfoWin::company_name() {
Jan Wilken Dörrie825776902021-03-04 20:28:35100 return GetStringValue(u"CompanyName");
initial.commitd7cae122008-07-26 21:49:38101}
102
Jan Wilken Dörrie85285b02021-03-11 23:38:47103std::u16string FileVersionInfoWin::company_short_name() {
Jan Wilken Dörrie825776902021-03-04 20:28:35104 return GetStringValue(u"CompanyShortName");
initial.commitd7cae122008-07-26 21:49:38105}
106
Jan Wilken Dörrie85285b02021-03-11 23:38:47107std::u16string FileVersionInfoWin::internal_name() {
Jan Wilken Dörrie825776902021-03-04 20:28:35108 return GetStringValue(u"InternalName");
initial.commitd7cae122008-07-26 21:49:38109}
110
Jan Wilken Dörrie85285b02021-03-11 23:38:47111std::u16string FileVersionInfoWin::product_name() {
Jan Wilken Dörrie825776902021-03-04 20:28:35112 return GetStringValue(u"ProductName");
initial.commitd7cae122008-07-26 21:49:38113}
114
Jan Wilken Dörrie85285b02021-03-11 23:38:47115std::u16string FileVersionInfoWin::product_short_name() {
Jan Wilken Dörrie825776902021-03-04 20:28:35116 return GetStringValue(u"ProductShortName");
initial.commitd7cae122008-07-26 21:49:38117}
118
Jan Wilken Dörrie85285b02021-03-11 23:38:47119std::u16string FileVersionInfoWin::product_version() {
Jan Wilken Dörrie825776902021-03-04 20:28:35120 return GetStringValue(u"ProductVersion");
initial.commitd7cae122008-07-26 21:49:38121}
122
Jan Wilken Dörrie85285b02021-03-11 23:38:47123std::u16string FileVersionInfoWin::file_description() {
Jan Wilken Dörrie825776902021-03-04 20:28:35124 return GetStringValue(u"FileDescription");
initial.commitd7cae122008-07-26 21:49:38125}
126
Jan Wilken Dörrie85285b02021-03-11 23:38:47127std::u16string FileVersionInfoWin::file_version() {
Jan Wilken Dörrie825776902021-03-04 20:28:35128 return GetStringValue(u"FileVersion");
initial.commitd7cae122008-07-26 21:49:38129}
130
Jan Wilken Dörrie85285b02021-03-11 23:38:47131std::u16string FileVersionInfoWin::original_filename() {
Jan Wilken Dörrie825776902021-03-04 20:28:35132 return GetStringValue(u"OriginalFilename");
initial.commitd7cae122008-07-26 21:49:38133}
134
Jan Wilken Dörrie85285b02021-03-11 23:38:47135std::u16string FileVersionInfoWin::special_build() {
Jan Wilken Dörrie825776902021-03-04 20:28:35136 return GetStringValue(u"SpecialBuild");
initial.commitd7cae122008-07-26 21:49:38137}
138
Jan Wilken Dörrie677e0c872021-03-10 10:04:38139bool FileVersionInfoWin::GetValue(const char16_t* name,
Jan Wilken Dörrie85285b02021-03-11 23:38:47140 std::u16string* value) const {
Lei Zhang4bb23de2019-10-04 16:17:08141 const struct LanguageAndCodePage lang_codepages[] = {
142 // Use the language and codepage from the DLL.
143 {language_, code_page_},
144 // Use the default language and codepage from the DLL.
145 {::GetUserDefaultLangID(), code_page_},
146 // Use the language from the DLL and Latin codepage (most common).
147 {language_, 1252},
148 // Use the default language and Latin codepage (most common).
149 {::GetUserDefaultLangID(), 1252},
150 };
initial.commitd7cae122008-07-26 21:49:38151
Lei Zhang4bb23de2019-10-04 16:17:08152 for (const auto& lang_codepage : lang_codepages) {
initial.commitd7cae122008-07-26 21:49:38153 wchar_t sub_block[MAX_PATH];
initial.commitd7cae122008-07-26 21:49:38154 _snwprintf_s(sub_block, MAX_PATH, MAX_PATH,
Lei Zhang4bb23de2019-10-04 16:17:08155 L"\\StringFileInfo\\%04x%04x\\%ls", lang_codepage.language,
156 lang_codepage.code_page, base::as_wcstr(name));
157 LPVOID value_ptr = nullptr;
avi9b6f42932015-12-26 22:15:14158 uint32_t size;
Lei Zhang4bb23de2019-10-04 16:17:08159 BOOL r = ::VerQueryValue(data_, sub_block, &value_ptr, &size);
160 if (r && value_ptr && size) {
Jan Wilken Dörrie677e0c872021-03-10 10:04:38161 value->assign(static_cast<char16_t*>(value_ptr), size - 1);
initial.commitd7cae122008-07-26 21:49:38162 return true;
163 }
164 }
165 return false;
166}
167
Jan Wilken Dörrie85285b02021-03-11 23:38:47168std::u16string FileVersionInfoWin::GetStringValue(const char16_t* name) const {
169 std::u16string str;
Lei Zhang4bb23de2019-10-04 16:17:08170 GetValue(name, &str);
171 return str;
initial.commitd7cae122008-07-26 21:49:38172}
fdoray5b7de9e92016-06-29 23:13:11173
Alan Screene93de3a2019-10-02 13:56:09174base::Version FileVersionInfoWin::GetFileVersion() const {
Ali Hijazia8877892022-11-10 20:51:03175 return base::Version({HIWORD(fixed_file_info_->dwFileVersionMS),
176 LOWORD(fixed_file_info_->dwFileVersionMS),
177 HIWORD(fixed_file_info_->dwFileVersionLS),
178 LOWORD(fixed_file_info_->dwFileVersionLS)});
Alan Screene93de3a2019-10-02 13:56:09179}
180
fdoray5b7de9e92016-06-29 23:13:11181FileVersionInfoWin::FileVersionInfoWin(std::vector<uint8_t>&& data,
182 WORD language,
183 WORD code_page)
184 : owned_data_(std::move(data)),
185 data_(owned_data_.data()),
186 language_(language),
187 code_page_(code_page),
188 fixed_file_info_(GetVsFixedFileInfo(data_)) {
189 DCHECK(!owned_data_.empty());
190}
191
192FileVersionInfoWin::FileVersionInfoWin(void* data,
193 WORD language,
194 WORD code_page)
195 : data_(data),
196 language_(language),
197 code_page_(code_page),
198 fixed_file_info_(GetVsFixedFileInfo(data)) {
199 DCHECK(data_);
200}