Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: 8a2b942557e13c5b5ae4edeaf715ba0eb4f9a450 [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;
Peter Kasting134ef9af2024-12-28 02:30:0933 if (::VerQueryValue(data, kTranslation, &translate, &dummy_size)) {
Lei Zhang4bb23de2019-10-04 16:17:0834 return static_cast<LanguageAndCodePage*>(translate);
Peter Kasting134ef9af2024-12-28 02:30:0935 }
fdoray5b7de9e92016-06-29 23:13:1136 return nullptr;
37}
38
Lei Zhang4bb23de2019-10-04 16:17:0839const 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);
fdoray5b7de9e92016-06-29 23:13:1145}
46
47} // namespace
48
49FileVersionInfoWin::~FileVersionInfoWin() = default;
initial.commitd7cae122008-07-26 21:49:3850
51// static
David Benjamin04cc2b42019-01-29 05:30:3352std::unique_ptr<FileVersionInfo>
53FileVersionInfo::CreateFileVersionInfoForModule(HMODULE module) {
fdoray5b7de9e92016-06-29 23:13:1154 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 Kasting134ef9af2024-12-28 02:30:0958 if (!has_version_resource) {
fdoray5b7de9e92016-06-29 23:13:1159 return nullptr;
Peter Kasting134ef9af2024-12-28 02:30:0960 }
initial.commitd7cae122008-07-26 21:49:3861
fdoray5b7de9e92016-06-29 23:13:1162 const LanguageAndCodePage* translate = GetTranslate(data);
Peter Kasting134ef9af2024-12-28 02:30:0963 if (!translate) {
fdoray5b7de9e92016-06-29 23:13:1164 return nullptr;
Peter Kasting134ef9af2024-12-28 02:30:0965 }
fdoray5b7de9e92016-06-29 23:13:1166
David Benjamin04cc2b42019-01-29 05:30:3367 return base::WrapUnique(
68 new FileVersionInfoWin(data, translate->language, translate->code_page));
initial.commitd7cae122008-07-26 21:49:3869}
70
71// static
David Benjamin04cc2b42019-01-29 05:30:3372std::unique_ptr<FileVersionInfo> FileVersionInfo::CreateFileVersionInfo(
Lei Zhang4bb23de2019-10-04 16:17:0873 const base::FilePath& file_path) {
David Benjamin04cc2b42019-01-29 05:30:3374 return FileVersionInfoWin::CreateFileVersionInfoWin(file_path);
75}
76
77// static
78std::unique_ptr<FileVersionInfoWin>
Lei Zhang4bb23de2019-10-04 16:17:0879FileVersionInfoWin::CreateFileVersionInfoWin(const base::FilePath& file_path) {
Etienne Bergeron436d42212019-02-26 17:15:1280 base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
81 base::BlockingType::MAY_BLOCK);
[email protected]45446a52010-11-04 17:41:0082
initial.commitd7cae122008-07-26 21:49:3883 DWORD dummy;
Jan Wilken Dörrieb630aca2019-12-04 10:59:1184 const wchar_t* path = file_path.value().c_str();
fdoray5b7de9e92016-06-29 23:13:1185 const DWORD length = ::GetFileVersionInfoSize(path, &dummy);
Peter Kasting134ef9af2024-12-28 02:30:0986 if (length == 0) {
fdoray5b7de9e92016-06-29 23:13:1187 return nullptr;
Peter Kasting134ef9af2024-12-28 02:30:0988 }
initial.commitd7cae122008-07-26 21:49:3889
fdoray5b7de9e92016-06-29 23:13:1190 std::vector<uint8_t> data(length, 0);
initial.commitd7cae122008-07-26 21:49:3891
Peter Kasting134ef9af2024-12-28 02:30:0992 if (!::GetFileVersionInfo(path, dummy, length, data.data())) {
fdoray5b7de9e92016-06-29 23:13:1193 return nullptr;
Peter Kasting134ef9af2024-12-28 02:30:0994 }
initial.commitd7cae122008-07-26 21:49:3895
fdoray5b7de9e92016-06-29 23:13:1196 const LanguageAndCodePage* translate = GetTranslate(data.data());
Peter Kasting134ef9af2024-12-28 02:30:0997 if (!translate) {
fdoray5b7de9e92016-06-29 23:13:1198 return nullptr;
Peter Kasting134ef9af2024-12-28 02:30:0999 }
initial.commitd7cae122008-07-26 21:49:38100
David Benjamin04cc2b42019-01-29 05:30:33101 return base::WrapUnique(new FileVersionInfoWin(
102 std::move(data), translate->language, translate->code_page));
initial.commitd7cae122008-07-26 21:49:38103}
104
Jan Wilken Dörrie85285b02021-03-11 23:38:47105std::u16string FileVersionInfoWin::company_name() {
Jan Wilken Dörrie825776902021-03-04 20:28:35106 return GetStringValue(u"CompanyName");
initial.commitd7cae122008-07-26 21:49:38107}
108
Jan Wilken Dörrie85285b02021-03-11 23:38:47109std::u16string FileVersionInfoWin::company_short_name() {
Jan Wilken Dörrie825776902021-03-04 20:28:35110 return GetStringValue(u"CompanyShortName");
initial.commitd7cae122008-07-26 21:49:38111}
112
Jan Wilken Dörrie85285b02021-03-11 23:38:47113std::u16string FileVersionInfoWin::internal_name() {
Jan Wilken Dörrie825776902021-03-04 20:28:35114 return GetStringValue(u"InternalName");
initial.commitd7cae122008-07-26 21:49:38115}
116
Jan Wilken Dörrie85285b02021-03-11 23:38:47117std::u16string FileVersionInfoWin::product_name() {
Jan Wilken Dörrie825776902021-03-04 20:28:35118 return GetStringValue(u"ProductName");
initial.commitd7cae122008-07-26 21:49:38119}
120
Jan Wilken Dörrie85285b02021-03-11 23:38:47121std::u16string FileVersionInfoWin::product_short_name() {
Jan Wilken Dörrie825776902021-03-04 20:28:35122 return GetStringValue(u"ProductShortName");
initial.commitd7cae122008-07-26 21:49:38123}
124
Jan Wilken Dörrie85285b02021-03-11 23:38:47125std::u16string FileVersionInfoWin::product_version() {
Jan Wilken Dörrie825776902021-03-04 20:28:35126 return GetStringValue(u"ProductVersion");
initial.commitd7cae122008-07-26 21:49:38127}
128
Jan Wilken Dörrie85285b02021-03-11 23:38:47129std::u16string FileVersionInfoWin::file_description() {
Jan Wilken Dörrie825776902021-03-04 20:28:35130 return GetStringValue(u"FileDescription");
initial.commitd7cae122008-07-26 21:49:38131}
132
Jan Wilken Dörrie85285b02021-03-11 23:38:47133std::u16string FileVersionInfoWin::file_version() {
Jan Wilken Dörrie825776902021-03-04 20:28:35134 return GetStringValue(u"FileVersion");
initial.commitd7cae122008-07-26 21:49:38135}
136
Jan Wilken Dörrie85285b02021-03-11 23:38:47137std::u16string FileVersionInfoWin::original_filename() {
Jan Wilken Dörrie825776902021-03-04 20:28:35138 return GetStringValue(u"OriginalFilename");
initial.commitd7cae122008-07-26 21:49:38139}
140
Jan Wilken Dörrie85285b02021-03-11 23:38:47141std::u16string FileVersionInfoWin::special_build() {
Jan Wilken Dörrie825776902021-03-04 20:28:35142 return GetStringValue(u"SpecialBuild");
initial.commitd7cae122008-07-26 21:49:38143}
144
Jan Wilken Dörrie677e0c872021-03-10 10:04:38145bool FileVersionInfoWin::GetValue(const char16_t* name,
Jan Wilken Dörrie85285b02021-03-11 23:38:47146 std::u16string* value) const {
Lei Zhang4bb23de2019-10-04 16:17:08147 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.commitd7cae122008-07-26 21:49:38157
Lei Zhang4bb23de2019-10-04 16:17:08158 for (const auto& lang_codepage : lang_codepages) {
initial.commitd7cae122008-07-26 21:49:38159 wchar_t sub_block[MAX_PATH];
initial.commitd7cae122008-07-26 21:49:38160 _snwprintf_s(sub_block, MAX_PATH, MAX_PATH,
Lei Zhang4bb23de2019-10-04 16:17:08161 L"\\StringFileInfo\\%04x%04x\\%ls", lang_codepage.language,
162 lang_codepage.code_page, base::as_wcstr(name));
163 LPVOID value_ptr = nullptr;
avi9b6f42932015-12-26 22:15:14164 uint32_t size;
Lei Zhang4bb23de2019-10-04 16:17:08165 BOOL r = ::VerQueryValue(data_, sub_block, &value_ptr, &size);
166 if (r && value_ptr && size) {
Jan Wilken Dörrie677e0c872021-03-10 10:04:38167 value->assign(static_cast<char16_t*>(value_ptr), size - 1);
initial.commitd7cae122008-07-26 21:49:38168 return true;
169 }
170 }
171 return false;
172}
173
Jan Wilken Dörrie85285b02021-03-11 23:38:47174std::u16string FileVersionInfoWin::GetStringValue(const char16_t* name) const {
175 std::u16string str;
Lei Zhang4bb23de2019-10-04 16:17:08176 GetValue(name, &str);
177 return str;
initial.commitd7cae122008-07-26 21:49:38178}
fdoray5b7de9e92016-06-29 23:13:11179
Alan Screene93de3a2019-10-02 13:56:09180base::Version FileVersionInfoWin::GetFileVersion() const {
Ali Hijazia8877892022-11-10 20:51:03181 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 Screene93de3a2019-10-02 13:56:09185}
186
fdoray5b7de9e92016-06-29 23:13:11187FileVersionInfoWin::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
198FileVersionInfoWin::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}