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