diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2020-01-14 19:36:53 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2020-01-14 20:07:47 +0300 |
commit | 0bff1d2776e0da589dd98eca69e7f04fb07dfd02 (patch) | |
tree | f7f8b905a34133d80e93d20770bdb092c04e247d /src | |
parent | clang-format: don't BinPackArguments (diff) | |
download | winapi-debug-0bff1d2776e0da589dd98eca69e7f04fb07dfd02.tar.gz winapi-debug-0bff1d2776e0da589dd98eca69e7f04fb07dfd02.zip |
clang-format all the code
Diffstat (limited to 'src')
-rw-r--r-- | src/dbghelp.cpp | 243 | ||||
-rw-r--r-- | src/error.cpp | 75 | ||||
-rw-r--r-- | src/module.cpp | 99 | ||||
-rw-r--r-- | src/repo.cpp | 302 | ||||
-rw-r--r-- | src/utils/file.cpp | 110 |
5 files changed, 363 insertions, 466 deletions
diff --git a/src/dbghelp.cpp b/src/dbghelp.cpp index 38cb6f5..40c2ba5 100644 --- a/src/dbghelp.cpp +++ b/src/dbghelp.cpp @@ -5,180 +5,137 @@ #include "pdb/all.hpp" -#include <safeint.h> - #include <Windows.h> +#include <safeint.h> #pragma warning(push, 0) #include <DbgHelp.h> #pragma warning(pop, 0) #include <cstddef> #include <cstring> - #include <stdexcept> #include <string> -namespace pdb -{ - namespace - { - void set_dbghelp_options() - { - SymSetOptions(SymGetOptions() - | SYMOPT_DEBUG - | SYMOPT_LOAD_LINES - | SYMOPT_UNDNAME); - } - - void initialize(HANDLE id) - { - set_dbghelp_options(); - - if (!SymInitialize(id, NULL, FALSE)) - throw error::windows(GetLastError()); - } - - void clean_up(HANDLE id) - { - if (!SymCleanup(id)) - throw error::windows(GetLastError()); - } - - Address next_offline_base = 0x10000000; - - Address gen_next_offline_base(std::size_t pdb_size) - { - const auto base = next_offline_base; - using msl::utilities::SafeAdd; - if (!SafeAdd(next_offline_base, pdb_size, next_offline_base)) - throw std::runtime_error{"no more PDB files can be added, the internal address space is exhausted"}; - return base; - } - - BOOL CALLBACK enum_symbols_callback( - SYMBOL_INFO *info, - ULONG, - VOID *raw_callback_ptr) - { - const auto callback_ptr = reinterpret_cast<DbgHelp::OnSymbol*>(raw_callback_ptr); - const auto& callback = *callback_ptr; - callback(SymbolInfo{*info}); - return TRUE; - } - } +namespace pdb { +namespace { - DbgHelp::DbgHelp() - { - initialize(id); - } +void set_dbghelp_options() { + SymSetOptions(SymGetOptions() | SYMOPT_DEBUG | SYMOPT_LOAD_LINES | SYMOPT_UNDNAME); +} - DbgHelp::~DbgHelp() - { - try - { - close(); - } - catch (...) - { } - } +void initialize(HANDLE id) { + set_dbghelp_options(); - void DbgHelp::close() - { - if (!closed) - { - clean_up(id); - closed = true; - } - } + if (!SymInitialize(id, NULL, FALSE)) + throw error::windows(GetLastError()); +} - ModuleInfo DbgHelp::load_pdb(const std::string& path) const - { - DWORD size = 0; - if (!msl::utilities::SafeCast(file::get_size(path), size)) - throw std::range_error{"PDB file is too large"}; +void clean_up(HANDLE id) { + if (!SymCleanup(id)) + throw error::windows(GetLastError()); +} - const auto offline_base = SymLoadModule64( - id, - NULL, - path.c_str(), - NULL, - gen_next_offline_base(size), - size); +Address next_offline_base = 0x10000000; - if (!offline_base) - throw error::windows(GetLastError()); +Address gen_next_offline_base(std::size_t pdb_size) { + const auto base = next_offline_base; + using msl::utilities::SafeAdd; + if (!SafeAdd(next_offline_base, pdb_size, next_offline_base)) + throw std::runtime_error{ + "no more PDB files can be added, the internal address space is exhausted"}; + return base; +} - return get_module_info(offline_base); - } +BOOL CALLBACK enum_symbols_callback(SYMBOL_INFO* info, ULONG, VOID* raw_callback_ptr) { + const auto callback_ptr = reinterpret_cast<DbgHelp::OnSymbol*>(raw_callback_ptr); + const auto& callback = *callback_ptr; + callback(SymbolInfo{*info}); + return TRUE; +} - ModuleInfo DbgHelp::get_module_info(Address offline_base) const - { - ModuleInfo info; +} // namespace - if (!SymGetModuleInfo64( - id, - offline_base, - &static_cast<ModuleInfo::Raw&>(info))) - throw error::windows(GetLastError()); +DbgHelp::DbgHelp() { + initialize(id); +} - return info; +DbgHelp::~DbgHelp() { + try { + close(); + } catch (...) { } +} - void DbgHelp::enum_symbols(const ModuleInfo& module, const OnSymbol& callback) const - { - if (!SymEnumSymbols( - id, - module.get_offline_base(), - NULL, - &enum_symbols_callback, - const_cast<OnSymbol*>(&callback))) - throw error::windows(GetLastError()); +void DbgHelp::close() { + if (!closed) { + clean_up(id); + closed = true; } +} - SymbolInfo DbgHelp::resolve_symbol(Address offline) const - { - Address displacement = 0; - SymbolInfo symbol; +ModuleInfo DbgHelp::load_pdb(const std::string& path) const { + DWORD size = 0; + if (!msl::utilities::SafeCast(file::get_size(path), size)) + throw std::range_error{"PDB file is too large"}; - if (!SymFromAddr( - id, - offline, - &displacement, - &static_cast<SYMBOL_INFO&>(symbol))) - throw error::windows(GetLastError()); + const auto offline_base = + SymLoadModule64(id, NULL, path.c_str(), NULL, gen_next_offline_base(size), size); - symbol.set_displacement(displacement); - return symbol; - } + if (!offline_base) + throw error::windows(GetLastError()); - SymbolInfo DbgHelp::resolve_symbol(const std::string& name) const - { - SymbolInfo symbol; + return get_module_info(offline_base); +} - if (!SymFromName( - id, - name.c_str(), - &static_cast<SYMBOL_INFO&>(symbol))) - throw error::windows(GetLastError()); +ModuleInfo DbgHelp::get_module_info(Address offline_base) const { + ModuleInfo info; - return symbol; - } + if (!SymGetModuleInfo64(id, offline_base, &static_cast<ModuleInfo::Raw&>(info))) + throw error::windows(GetLastError()); - LineInfo DbgHelp::resolve_line(Address offline) const - { - IMAGEHLP_LINE64 raw; - std::memset(&raw, 0, sizeof(raw)); - raw.SizeOfStruct = sizeof(raw); + return info; +} - DWORD displacement = 0; +void DbgHelp::enum_symbols(const ModuleInfo& module, const OnSymbol& callback) const { + if (!SymEnumSymbols(id, + module.get_offline_base(), + NULL, + &enum_symbols_callback, + const_cast<OnSymbol*>(&callback))) + throw error::windows(GetLastError()); +} - if (!SymGetLineFromAddr64( - id, - offline, - &displacement, - &raw)) - throw error::windows(GetLastError()); +SymbolInfo DbgHelp::resolve_symbol(Address offline) const { + Address displacement = 0; + SymbolInfo symbol; - return LineInfo{raw}; - } + if (!SymFromAddr(id, offline, &displacement, &static_cast<SYMBOL_INFO&>(symbol))) + throw error::windows(GetLastError()); + + symbol.set_displacement(displacement); + return symbol; } + +SymbolInfo DbgHelp::resolve_symbol(const std::string& name) const { + SymbolInfo symbol; + + if (!SymFromName(id, name.c_str(), &static_cast<SYMBOL_INFO&>(symbol))) + throw error::windows(GetLastError()); + + return symbol; +} + +LineInfo DbgHelp::resolve_line(Address offline) const { + IMAGEHLP_LINE64 raw; + std::memset(&raw, 0, sizeof(raw)); + raw.SizeOfStruct = sizeof(raw); + + DWORD displacement = 0; + + if (!SymGetLineFromAddr64(id, offline, &displacement, &raw)) + throw error::windows(GetLastError()); + + return LineInfo{raw}; +} + +} // namespace pdb diff --git a/src/error.cpp b/src/error.cpp index 10b668b..320c5d0 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -9,45 +9,40 @@ #include <string> -namespace pdb -{ - namespace error - { - namespace - { - std::string trim_trailing_newline(const std::string& s) - { - const auto last_pos = s.find_last_not_of("\r\n"); - if (std::string::npos == last_pos) - return {}; - return s.substr(0, last_pos + 1); - } - } - - std::string CategoryWindows::message(int code) const - { - char* buf; - - const auto nbwritten = FormatMessageA( - FORMAT_MESSAGE_ALLOCATE_BUFFER - | FORMAT_MESSAGE_FROM_SYSTEM - | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - code, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - reinterpret_cast<char*>(&buf), - 0, - NULL); - - if (0 == nbwritten) - { - LocalFree(buf); - return "Couldn't format the error message"; - } - - std::string msg{buf, nbwritten}; - LocalFree(buf); - return trim_trailing_newline(msg); - } +namespace pdb { +namespace error { +namespace { + +std::string trim_trailing_newline(const std::string& s) { + const auto last_pos = s.find_last_not_of("\r\n"); + if (std::string::npos == last_pos) + return {}; + return s.substr(0, last_pos + 1); +} + +} // namespace + +std::string CategoryWindows::message(int code) const { + char* buf; + + const auto nbwritten = FormatMessageA( + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + code, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + reinterpret_cast<char*>(&buf), + 0, + NULL); + + if (0 == nbwritten) { + LocalFree(buf); + return "Couldn't format the error message"; } + + std::string msg{buf, nbwritten}; + LocalFree(buf); + return trim_trailing_newline(msg); } + +} // namespace error +} // namespace pdb diff --git a/src/module.cpp b/src/module.cpp index 2c1d26c..0bf9329 100644 --- a/src/module.cpp +++ b/src/module.cpp @@ -8,69 +8,58 @@ #include <safeint.h> #include <cstring> - #include <sstream> #include <stdexcept> #include <string> -namespace pdb -{ - ModuleInfo::ModuleInfo() - : ModuleInfo{create_raw()} - { } +namespace pdb { + +ModuleInfo::ModuleInfo() : ModuleInfo{create_raw()} {} - ModuleInfo::ModuleInfo(const Raw& raw) - : raw{raw} - { - if (raw.SizeOfStruct != sizeof(raw)) - throw std::runtime_error{"invalid IMAGEHLP_MODULE64.SizeOfStruct"}; - } +ModuleInfo::ModuleInfo(const Raw& raw) : raw{raw} { + if (raw.SizeOfStruct != sizeof(raw)) + throw std::runtime_error{"invalid IMAGEHLP_MODULE64.SizeOfStruct"}; +} - ModuleInfo::Raw ModuleInfo::create_raw() - { - Raw raw; - std::memset(&raw, 0, sizeof(raw)); - raw.SizeOfStruct = sizeof(raw); - return raw; - } +ModuleInfo::Raw ModuleInfo::create_raw() { + Raw raw; + std::memset(&raw, 0, sizeof(raw)); + raw.SizeOfStruct = sizeof(raw); + return raw; +} - Address Module::translate_offline_address(Address offline) const - { - if (offline < get_offline_base()) - throw std::range_error{invalid_offline_address(offline)}; - const auto offset = offline - get_offline_base(); - auto online = offset; - if (!msl::utilities::SafeAdd(online, get_online_base(), online)) - throw std::range_error{invalid_offline_address(offline)}; - return online; - } +Address Module::translate_offline_address(Address offline) const { + if (offline < get_offline_base()) + throw std::range_error{invalid_offline_address(offline)}; + const auto offset = offline - get_offline_base(); + auto online = offset; + if (!msl::utilities::SafeAdd(online, get_online_base(), online)) + throw std::range_error{invalid_offline_address(offline)}; + return online; +} - Address Module::translate_online_address(Address online) const - { - if (online < get_online_base()) - throw std::range_error{invalid_online_address(online)}; - const auto offset = online - get_online_base(); - auto offline = offset; - if (!msl::utilities::SafeAdd(offline, get_offline_base(), offline)) - throw std::range_error{invalid_online_address(offline)}; - return offline; - } +Address Module::translate_online_address(Address online) const { + if (online < get_online_base()) + throw std::range_error{invalid_online_address(online)}; + const auto offset = online - get_online_base(); + auto offline = offset; + if (!msl::utilities::SafeAdd(offline, get_offline_base(), offline)) + throw std::range_error{invalid_online_address(offline)}; + return offline; +} - std::string Module::invalid_offline_address(Address offline) const - { - std::ostringstream oss; - oss << "offline address " << format_address(offline) - << " doesn't belong to module " << get_name() - << " (base offline address " << format_address(get_offline_base()) << ')'; - return oss.str(); - } +std::string Module::invalid_offline_address(Address offline) const { + std::ostringstream oss; + oss << "offline address " << format_address(offline) << " doesn't belong to module " + << get_name() << " (base offline address " << format_address(get_offline_base()) << ')'; + return oss.str(); +} - std::string Module::invalid_online_address(Address online) const - { - std::ostringstream oss; - oss << "online address " << format_address(online) - << " doesn't belong to module " << get_name() - << " (base online address " << format_address(get_online_base()) << ')'; - return oss.str(); - } +std::string Module::invalid_online_address(Address online) const { + std::ostringstream oss; + oss << "online address " << format_address(online) << " doesn't belong to module " << get_name() + << " (base online address " << format_address(get_online_base()) << ')'; + return oss.str(); } + +} // namespace pdb diff --git a/src/repo.cpp b/src/repo.cpp index 7a19c83..d7f2c74 100644 --- a/src/repo.cpp +++ b/src/repo.cpp @@ -11,194 +11,160 @@ #include <string> #include <utility> -namespace pdb -{ - namespace - { - std::string pdb_already_loaded(Address online_base, const std::string& path) - { - std::ostringstream oss; - oss << "module with online base address " << format_address(online_base) - << " has already been loaded: " << path; - return oss.str(); - } - - std::string pdb_already_loaded(const std::string& path) - { - std::ostringstream oss; - oss << "module has already been loaded: " << path; - return oss.str(); - } - - std::string offline_base_already_used(Address base) - { - std::ostringstream oss; - oss << "module with offline base address " << format_address(base) - << " has already been loaded (shouldn't happen)"; - return oss.str(); - } - - std::string module_not_found(Address base) - { - std::ostringstream oss; - oss << "module with base address " << format_address(base) - << " wasn't found"; - return oss.str(); - } - - std::string guess_module_no_modules(Address pivot) - { - std::ostringstream oss; - oss << "couldn't select a module for address " << format_address(pivot) - << ": no modules have been loaded yet"; - return oss.str(); - } - - std::string guess_module_address_too_low(Address pivot) - { - std::ostringstream oss; - oss << "couldn't select a module for address " << format_address(pivot) - << ": it's too low"; - return oss.str(); - } - - template <typename Value> - const Module& lookup_module( - const std::map<Address, Value>& modules, - Address base) - { - const auto it = modules.find(base); - if (it == modules.cend()) - throw std::runtime_error{module_not_found(base)}; - return it->second; - } - - template <typename Value> - const Module& guess_module( - const std::map<Address, Value>& modules, - Address pivot) - { - if (modules.empty()) - throw std::range_error{guess_module_no_modules(pivot)}; - - auto it = modules.lower_bound(pivot); - - if (it == modules.cend()) - { - --it; - return it->second; - } - - if (it->first > pivot) - { - if (it == modules.cbegin()) - throw std::range_error{guess_module_address_too_low(pivot)}; - --it; - return it->second; - } - - return it->second; - } - } +namespace pdb { +namespace { + +std::string pdb_already_loaded(Address online_base, const std::string& path) { + std::ostringstream oss; + oss << "module with online base address " << format_address(online_base) + << " has already been loaded: " << path; + return oss.str(); +} - Address Repo::add_pdb(Address online_base, const std::string& path) - { - if (online_bases.find(online_base) != online_bases.cend()) - throw std::runtime_error{pdb_already_loaded(online_base, path)}; +std::string pdb_already_loaded(const std::string& path) { + std::ostringstream oss; + oss << "module has already been loaded: " << path; + return oss.str(); +} - auto file_id = file::query_id(path); - if (file_ids.find(file_id) != file_ids.cend()) - throw std::runtime_error{pdb_already_loaded(path)}; +std::string offline_base_already_used(Address base) { + std::ostringstream oss; + oss << "module with offline base address " << format_address(base) + << " has already been loaded (shouldn't happen)"; + return oss.str(); +} - Module module{online_base, dbghelp.load_pdb(path)}; - const auto offline_base = module.get_offline_base(); +std::string module_not_found(Address base) { + std::ostringstream oss; + oss << "module with base address " << format_address(base) << " wasn't found"; + return oss.str(); +} - if (offline_bases.find(offline_base) != offline_bases.cend()) - throw std::runtime_error{offline_base_already_used(offline_base)}; +std::string guess_module_no_modules(Address pivot) { + std::ostringstream oss; + oss << "couldn't select a module for address " << format_address(pivot) + << ": no modules have been loaded yet"; + return oss.str(); +} - file_ids.emplace(std::move(file_id)); - const auto it = online_bases.emplace(online_base, std::move(module)); - offline_bases.emplace(offline_base, it.first->second); +std::string guess_module_address_too_low(Address pivot) { + std::ostringstream oss; + oss << "couldn't select a module for address " << format_address(pivot) << ": it's too low"; + return oss.str(); +} - return offline_base; - } +template <typename Value> +const Module& lookup_module(const std::map<Address, Value>& modules, Address base) { + const auto it = modules.find(base); + if (it == modules.cend()) + throw std::runtime_error{module_not_found(base)}; + return it->second; +} - void Repo::enum_symbols(const OnSymbol& callback) const - { - for (const auto& it : offline_bases) - enum_symbols(it.second, callback); - } +template <typename Value> +const Module& guess_module(const std::map<Address, Value>& modules, Address pivot) { + if (modules.empty()) + throw std::range_error{guess_module_no_modules(pivot)}; - void Repo::enum_symbols(Address offline_base, const OnSymbol& callback) const - { - const auto it = offline_bases.find(offline_base); - if (it == offline_bases.cend()) - throw std::runtime_error{"unknown module"}; - enum_symbols(it->second, callback); - } + auto it = modules.lower_bound(pivot); - void Repo::enum_symbols(const Module& module, const OnSymbol& callback) const - { - dbghelp.enum_symbols(module, [&] (const SymbolInfo& raw) - { - callback(symbol_from_buffer(module, raw)); - }); + if (it == modules.cend()) { + --it; + return it->second; } - Symbol Repo::resolve_symbol(Address online) const - { - return symbol_from_buffer(dbghelp.resolve_symbol(address_online_to_offline(online))); + if (it->first > pivot) { + if (it == modules.cbegin()) + throw std::range_error{guess_module_address_too_low(pivot)}; + --it; + return it->second; } - Symbol Repo::resolve_symbol(const std::string& name) const - { - return symbol_from_buffer(dbghelp.resolve_symbol(name)); - } + return it->second; +} - LineInfo Repo::resolve_line(Address online) const - { - return dbghelp.resolve_line(address_online_to_offline(online)); - } +} // namespace - const Module& Repo::module_with_online_base(Address base) const - { - return lookup_module(online_bases, base); - } +Address Repo::add_pdb(Address online_base, const std::string& path) { + if (online_bases.find(online_base) != online_bases.cend()) + throw std::runtime_error{pdb_already_loaded(online_base, path)}; - const Module& Repo::module_with_offline_base(Address base) const - { - return lookup_module(offline_bases, base); - } + auto file_id = file::query_id(path); + if (file_ids.find(file_id) != file_ids.cend()) + throw std::runtime_error{pdb_already_loaded(path)}; - Symbol Repo::symbol_from_buffer(const SymbolInfo& raw) const - { - return symbol_from_buffer(module_with_offline_base(raw.get_offline_base()), raw); - } + Module module{online_base, dbghelp.load_pdb(path)}; + const auto offline_base = module.get_offline_base(); - Symbol Repo::symbol_from_buffer(const Module& module, const SymbolInfo& raw) - { - return {module.translate_offline_address(raw.get_offline_address()), raw}; - } + if (offline_bases.find(offline_base) != offline_bases.cend()) + throw std::runtime_error{offline_base_already_used(offline_base)}; - Address Repo::address_online_to_offline(Address online) const - { - return module_from_online_address(online) - .translate_online_address(online); - } + file_ids.emplace(std::move(file_id)); + const auto it = online_bases.emplace(online_base, std::move(module)); + offline_bases.emplace(offline_base, it.first->second); - Address Repo::address_offline_to_online(Address offline) const - { - return module_from_offline_address(offline) - .translate_offline_address(offline); - } + return offline_base; +} - const Module& Repo::module_from_online_address(Address online) const - { - return guess_module(online_bases, online); - } +void Repo::enum_symbols(const OnSymbol& callback) const { + for (const auto& it : offline_bases) + enum_symbols(it.second, callback); +} - const Module& Repo::module_from_offline_address(Address offline) const - { - return guess_module(offline_bases, offline); - } +void Repo::enum_symbols(Address offline_base, const OnSymbol& callback) const { + const auto it = offline_bases.find(offline_base); + if (it == offline_bases.cend()) + throw std::runtime_error{"unknown module"}; + enum_symbols(it->second, callback); } + +void Repo::enum_symbols(const Module& module, const OnSymbol& callback) const { + dbghelp.enum_symbols(module, + [&](const SymbolInfo& raw) { callback(symbol_from_buffer(module, raw)); }); +} + +Symbol Repo::resolve_symbol(Address online) const { + return symbol_from_buffer(dbghelp.resolve_symbol(address_online_to_offline(online))); +} + +Symbol Repo::resolve_symbol(const std::string& name) const { + return symbol_from_buffer(dbghelp.resolve_symbol(name)); +} + +LineInfo Repo::resolve_line(Address online) const { + return dbghelp.resolve_line(address_online_to_offline(online)); +} + +const Module& Repo::module_with_online_base(Address base) const { + return lookup_module(online_bases, base); +} + +const Module& Repo::module_with_offline_base(Address base) const { + return lookup_module(offline_bases, base); +} + +Symbol Repo::symbol_from_buffer(const SymbolInfo& raw) const { + return symbol_from_buffer(module_with_offline_base(raw.get_offline_base()), raw); +} + +Symbol Repo::symbol_from_buffer(const Module& module, const SymbolInfo& raw) { + return {module.translate_offline_address(raw.get_offline_address()), raw}; +} + +Address Repo::address_online_to_offline(Address online) const { + return module_from_online_address(online).translate_online_address(online); +} + +Address Repo::address_offline_to_online(Address offline) const { + return module_from_offline_address(offline).translate_offline_address(offline); +} + +const Module& Repo::module_from_online_address(Address online) const { + return guess_module(online_bases, online); +} + +const Module& Repo::module_from_offline_address(Address offline) const { + return guess_module(offline_bases, offline); +} + +} // namespace pdb diff --git a/src/utils/file.cpp b/src/utils/file.cpp index c0bb4bd..c25307a 100644 --- a/src/utils/file.cpp +++ b/src/utils/file.cpp @@ -5,70 +5,60 @@ #include "pdb/all.hpp" -#include <safeint.h> - #include <Windows.h> +#include <safeint.h> #include <cstddef> - #include <stdexcept> #include <string> -namespace pdb -{ - namespace file - { - std::size_t get_size(const std::string& path) - { - const Handle handle{CreateFileA( - path.c_str(), - FILE_READ_ATTRIBUTES, - FILE_SHARE_READ, - NULL, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, - NULL)}; - - if (handle.get() == INVALID_HANDLE_VALUE) - throw error::windows(GetLastError()); - - LARGE_INTEGER size; - - if (!GetFileSizeEx(handle.get(), &size)) - throw error::windows(GetLastError()); - - std::size_t result = 0; - - if (!msl::utilities::SafeCast(size.QuadPart, result)) - throw std::runtime_error{"invalid file size"}; - - return result; - } - - ID query_id(const std::string& path) - { - const Handle handle{CreateFileA( - path.c_str(), - FILE_READ_ATTRIBUTES, - FILE_SHARE_READ | FILE_SHARE_WRITE, - NULL, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, - NULL)}; - - if (handle.get() == INVALID_HANDLE_VALUE) - throw error::windows(GetLastError()); - - FILE_ID_INFO id; - - if (!GetFileInformationByHandleEx( - handle.get(), - FileIdInfo, - &id, - sizeof(id))) - throw error::windows(GetLastError()); - - return {id}; - } - } +namespace pdb { +namespace file { + +std::size_t get_size(const std::string& path) { + const Handle handle{CreateFileA(path.c_str(), + FILE_READ_ATTRIBUTES, + FILE_SHARE_READ, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + NULL)}; + + if (handle.get() == INVALID_HANDLE_VALUE) + throw error::windows(GetLastError()); + + LARGE_INTEGER size; + + if (!GetFileSizeEx(handle.get(), &size)) + throw error::windows(GetLastError()); + + std::size_t result = 0; + + if (!msl::utilities::SafeCast(size.QuadPart, result)) + throw std::runtime_error{"invalid file size"}; + + return result; } + +ID query_id(const std::string& path) { + const Handle handle{CreateFileA(path.c_str(), + FILE_READ_ATTRIBUTES, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + NULL)}; + + if (handle.get() == INVALID_HANDLE_VALUE) + throw error::windows(GetLastError()); + + FILE_ID_INFO id; + + if (!GetFileInformationByHandleEx(handle.get(), FileIdInfo, &id, sizeof(id))) + throw error::windows(GetLastError()); + + return {id}; +} + +} // namespace file +} // namespace pdb |