file_version_info_win.cpp (2963B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 // This is a partial implementation of Chromium's source file 8 // base/file_version_info_win.cc 9 10 #include "base/file_version_info_win.h" 11 12 #include <windows.h> 13 14 #include "base/files/file_path.h" 15 #include "base/memory/ptr_util.h" 16 #include "base/threading/scoped_blocking_call.h" 17 18 19 namespace { 20 21 struct LanguageAndCodePage { 22 WORD language; 23 WORD code_page; 24 }; 25 26 // Returns the \VarFileInfo\Translation value extracted from the 27 // VS_VERSION_INFO resource in |data|. 28 LanguageAndCodePage* GetTranslate(const void* data) { 29 static constexpr wchar_t kTranslation[] = L"\\VarFileInfo\\Translation"; 30 LPVOID translate = nullptr; 31 UINT dummy_size; 32 if (::VerQueryValue(data, kTranslation, &translate, &dummy_size)) 33 return static_cast<LanguageAndCodePage*>(translate); 34 return nullptr; 35 } 36 37 const VS_FIXEDFILEINFO& GetVsFixedFileInfo(const void* data) { 38 static constexpr wchar_t kRoot[] = L"\\"; 39 LPVOID fixed_file_info = nullptr; 40 UINT dummy_size; 41 CHECK(::VerQueryValue(data, kRoot, &fixed_file_info, &dummy_size)); 42 return *static_cast<VS_FIXEDFILEINFO*>(fixed_file_info); 43 } 44 45 } // namespace 46 47 // static 48 std::unique_ptr<FileVersionInfoWin> 49 FileVersionInfoWin::CreateFileVersionInfoWin(const base::FilePath& file_path) { 50 base::ScopedBlockingCall scoped_blocking_call(FROM_HERE, 51 base::BlockingType::MAY_BLOCK); 52 53 DWORD dummy; 54 const wchar_t* path = file_path.value().c_str(); 55 const DWORD length = ::GetFileVersionInfoSize(path, &dummy); 56 if (length == 0) 57 return nullptr; 58 59 std::vector<uint8_t> data(length, 0); 60 61 if (!::GetFileVersionInfo(path, dummy, length, data.data())) 62 return nullptr; 63 64 const LanguageAndCodePage* translate = GetTranslate(data.data()); 65 if (!translate) 66 return nullptr; 67 68 return base::WrapUnique(new FileVersionInfoWin( 69 std::move(data), translate->language, translate->code_page)); 70 } 71 72 base::Version FileVersionInfoWin::GetFileVersion() const { 73 return base::Version({HIWORD(fixed_file_info_.dwFileVersionMS), 74 LOWORD(fixed_file_info_.dwFileVersionMS), 75 HIWORD(fixed_file_info_.dwFileVersionLS), 76 LOWORD(fixed_file_info_.dwFileVersionLS)}); 77 } 78 79 FileVersionInfoWin::FileVersionInfoWin(std::vector<uint8_t>&& data, 80 WORD language, 81 WORD code_page) 82 : owned_data_(std::move(data)), 83 data_(owned_data_.data()), 84 language_(language), 85 code_page_(code_page), 86 fixed_file_info_(GetVsFixedFileInfo(data_)) { 87 DCHECK(!owned_data_.empty()); 88 89 (void)language_; 90 (void)code_page_; 91 }