| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/file_version_info_win.h" | 5 #include "base/file_version_info_win.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> |
| 9 | 10 |
| 11 #include "base/file_version_info.h" |
| 10 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 11 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/macros.h" |
| 12 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
| 13 #include "base/win/resource_util.h" | |
| 14 | 16 |
| 15 using base::FilePath; | 17 using base::FilePath; |
| 16 | 18 |
| 17 namespace { | 19 FileVersionInfoWin::FileVersionInfoWin(void* data, |
| 20 WORD language, |
| 21 WORD code_page) |
| 22 : language_(language), code_page_(code_page) { |
| 23 base::ThreadRestrictions::AssertIOAllowed(); |
| 24 data_.reset((char*) data); |
| 25 fixed_file_info_ = NULL; |
| 26 UINT size; |
| 27 ::VerQueryValue(data_.get(), L"\\", (LPVOID*)&fixed_file_info_, &size); |
| 28 } |
| 18 | 29 |
| 19 struct LanguageAndCodePage { | 30 FileVersionInfoWin::~FileVersionInfoWin() { |
| 31 DCHECK(data_.get()); |
| 32 } |
| 33 |
| 34 typedef struct { |
| 20 WORD language; | 35 WORD language; |
| 21 WORD code_page; | 36 WORD code_page; |
| 22 }; | 37 } LanguageAndCodePage; |
| 23 | |
| 24 // Returns the \\VarFileInfo\\Translation value extracted from the | |
| 25 // VS_VERSION_INFO resource in |data|. | |
| 26 LanguageAndCodePage* GetTranslate(const void* data) { | |
| 27 LanguageAndCodePage* translate = nullptr; | |
| 28 UINT length; | |
| 29 if (::VerQueryValue(data, L"\\VarFileInfo\\Translation", | |
| 30 reinterpret_cast<void**>(&translate), &length)) { | |
| 31 return translate; | |
| 32 } | |
| 33 return nullptr; | |
| 34 } | |
| 35 | |
| 36 VS_FIXEDFILEINFO* GetVsFixedFileInfo(const void* data) { | |
| 37 VS_FIXEDFILEINFO* fixed_file_info = nullptr; | |
| 38 UINT length; | |
| 39 if (::VerQueryValue(data, L"\\", reinterpret_cast<void**>(&fixed_file_info), | |
| 40 &length)) { | |
| 41 return fixed_file_info; | |
| 42 } | |
| 43 return nullptr; | |
| 44 } | |
| 45 | |
| 46 } // namespace | |
| 47 | |
| 48 FileVersionInfoWin::~FileVersionInfoWin() = default; | |
| 49 | 38 |
| 50 // static | 39 // static |
| 51 FileVersionInfo* FileVersionInfo::CreateFileVersionInfoForModule( | 40 FileVersionInfo* FileVersionInfo::CreateFileVersionInfoForModule( |
| 52 HMODULE module) { | 41 HMODULE module) { |
| 53 void* data; | 42 // Note that the use of MAX_PATH is basically in line with what we do for |
| 54 size_t version_info_length; | 43 // all registered paths (PathProviderWin). |
| 55 const bool has_version_resource = base::win::GetResourceFromModule( | 44 wchar_t system_buffer[MAX_PATH]; |
| 56 module, VS_VERSION_INFO, RT_VERSION, &data, &version_info_length); | 45 system_buffer[0] = 0; |
| 57 if (!has_version_resource) | 46 if (!GetModuleFileName(module, system_buffer, MAX_PATH)) |
| 58 return nullptr; | 47 return NULL; |
| 59 | 48 |
| 60 const LanguageAndCodePage* translate = GetTranslate(data); | 49 FilePath app_path(system_buffer); |
| 61 if (!translate) | 50 return CreateFileVersionInfo(app_path); |
| 62 return nullptr; | |
| 63 | |
| 64 return new FileVersionInfoWin(data, translate->language, | |
| 65 translate->code_page); | |
| 66 } | 51 } |
| 67 | 52 |
| 68 // static | 53 // static |
| 69 FileVersionInfo* FileVersionInfo::CreateFileVersionInfo( | 54 FileVersionInfo* FileVersionInfo::CreateFileVersionInfo( |
| 70 const FilePath& file_path) { | 55 const FilePath& file_path) { |
| 71 base::ThreadRestrictions::AssertIOAllowed(); | 56 base::ThreadRestrictions::AssertIOAllowed(); |
| 72 | 57 |
| 73 DWORD dummy; | 58 DWORD dummy; |
| 74 const wchar_t* path = file_path.value().c_str(); | 59 const wchar_t* path = file_path.value().c_str(); |
| 75 DWORD length = ::GetFileVersionInfoSize(path, &dummy); | 60 DWORD length = ::GetFileVersionInfoSize(path, &dummy); |
| 76 if (length == 0) | 61 if (length == 0) |
| 77 return nullptr; | 62 return NULL; |
| 78 | 63 |
| 79 std::vector<uint8_t> data(length, 0); | 64 void* data = calloc(length, 1); |
| 65 if (!data) |
| 66 return NULL; |
| 80 | 67 |
| 81 if (!::GetFileVersionInfo(path, dummy, data.size(), data.data())) | 68 if (!::GetFileVersionInfo(path, dummy, length, data)) { |
| 82 return nullptr; | 69 free(data); |
| 70 return NULL; |
| 71 } |
| 83 | 72 |
| 84 const LanguageAndCodePage* translate = GetTranslate(data.data()); | 73 LanguageAndCodePage* translate = NULL; |
| 85 if (!translate) | 74 uint32_t page_count; |
| 86 return nullptr; | 75 BOOL query_result = VerQueryValue(data, L"\\VarFileInfo\\Translation", |
| 76 (void**) &translate, &page_count); |
| 87 | 77 |
| 88 return new FileVersionInfoWin(std::move(data), translate->language, | 78 if (query_result && translate) { |
| 89 translate->code_page); | 79 return new FileVersionInfoWin(data, translate->language, |
| 80 translate->code_page); |
| 81 |
| 82 } else { |
| 83 free(data); |
| 84 return NULL; |
| 85 } |
| 90 } | 86 } |
| 91 | 87 |
| 92 base::string16 FileVersionInfoWin::company_name() { | 88 base::string16 FileVersionInfoWin::company_name() { |
| 93 return GetStringValue(L"CompanyName"); | 89 return GetStringValue(L"CompanyName"); |
| 94 } | 90 } |
| 95 | 91 |
| 96 base::string16 FileVersionInfoWin::company_short_name() { | 92 base::string16 FileVersionInfoWin::company_short_name() { |
| 97 return GetStringValue(L"CompanyShortName"); | 93 return GetStringValue(L"CompanyShortName"); |
| 98 } | 94 } |
| 99 | 95 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 | 168 |
| 173 i = 0; | 169 i = 0; |
| 174 while (i < arraysize(lang_codepage)) { | 170 while (i < arraysize(lang_codepage)) { |
| 175 wchar_t sub_block[MAX_PATH]; | 171 wchar_t sub_block[MAX_PATH]; |
| 176 WORD language = lang_codepage[i++]; | 172 WORD language = lang_codepage[i++]; |
| 177 WORD code_page = lang_codepage[i++]; | 173 WORD code_page = lang_codepage[i++]; |
| 178 _snwprintf_s(sub_block, MAX_PATH, MAX_PATH, | 174 _snwprintf_s(sub_block, MAX_PATH, MAX_PATH, |
| 179 L"\\StringFileInfo\\%04x%04x\\%ls", language, code_page, name); | 175 L"\\StringFileInfo\\%04x%04x\\%ls", language, code_page, name); |
| 180 LPVOID value = NULL; | 176 LPVOID value = NULL; |
| 181 uint32_t size; | 177 uint32_t size; |
| 182 BOOL r = ::VerQueryValue(data_, sub_block, &value, &size); | 178 BOOL r = ::VerQueryValue(data_.get(), sub_block, &value, &size); |
| 183 if (r && value) { | 179 if (r && value) { |
| 184 value_str->assign(static_cast<wchar_t*>(value)); | 180 value_str->assign(static_cast<wchar_t*>(value)); |
| 185 return true; | 181 return true; |
| 186 } | 182 } |
| 187 } | 183 } |
| 188 return false; | 184 return false; |
| 189 } | 185 } |
| 190 | 186 |
| 191 std::wstring FileVersionInfoWin::GetStringValue(const wchar_t* name) { | 187 std::wstring FileVersionInfoWin::GetStringValue(const wchar_t* name) { |
| 192 std::wstring str; | 188 std::wstring str; |
| 193 if (GetValue(name, &str)) | 189 if (GetValue(name, &str)) |
| 194 return str; | 190 return str; |
| 195 else | 191 else |
| 196 return L""; | 192 return L""; |
| 197 } | 193 } |
| 198 | |
| 199 FileVersionInfoWin::FileVersionInfoWin(std::vector<uint8_t>&& data, | |
| 200 WORD language, | |
| 201 WORD code_page) | |
| 202 : owned_data_(std::move(data)), | |
| 203 data_(owned_data_.data()), | |
| 204 language_(language), | |
| 205 code_page_(code_page), | |
| 206 fixed_file_info_(GetVsFixedFileInfo(data_)) { | |
| 207 DCHECK(!owned_data_.empty()); | |
| 208 } | |
| 209 | |
| 210 FileVersionInfoWin::FileVersionInfoWin(void* data, | |
| 211 WORD language, | |
| 212 WORD code_page) | |
| 213 : data_(data), | |
| 214 language_(language), | |
| 215 code_page_(code_page), | |
| 216 fixed_file_info_(GetVsFixedFileInfo(data)) { | |
| 217 DCHECK(data_); | |
| 218 } | |
| OLD | NEW |