michael@0: // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef SANDBOX_SRC_WIN_UTILS_H_ michael@0: #define SANDBOX_SRC_WIN_UTILS_H_ michael@0: michael@0: #include michael@0: #include michael@0: #include "base/basictypes.h" michael@0: michael@0: namespace sandbox { michael@0: michael@0: // Prefix for path used by NT calls. michael@0: const wchar_t kNTPrefix[] = L"\\??\\"; michael@0: const size_t kNTPrefixLen = arraysize(kNTPrefix) - 1; michael@0: michael@0: const wchar_t kNTObjManPrefix[] = L"\\Device\\"; michael@0: const size_t kNTObjManPrefixLen = arraysize(kNTObjManPrefix) - 1; michael@0: michael@0: // Automatically acquires and releases a lock when the object is michael@0: // is destroyed. michael@0: class AutoLock { michael@0: public: michael@0: // Acquires the lock. michael@0: explicit AutoLock(CRITICAL_SECTION *lock) : lock_(lock) { michael@0: ::EnterCriticalSection(lock); michael@0: }; michael@0: michael@0: // Releases the lock; michael@0: ~AutoLock() { michael@0: ::LeaveCriticalSection(lock_); michael@0: }; michael@0: michael@0: private: michael@0: CRITICAL_SECTION *lock_; michael@0: DISALLOW_IMPLICIT_CONSTRUCTORS(AutoLock); michael@0: }; michael@0: michael@0: // Basic implementation of a singleton which calls the destructor michael@0: // when the exe is shutting down or the DLL is being unloaded. michael@0: template michael@0: class SingletonBase { michael@0: public: michael@0: static Derived* GetInstance() { michael@0: static Derived* instance = NULL; michael@0: if (NULL == instance) { michael@0: instance = new Derived(); michael@0: // Microsoft CRT extension. In an exe this this called after michael@0: // winmain returns, in a dll is called in DLL_PROCESS_DETACH michael@0: _onexit(OnExit); michael@0: } michael@0: return instance; michael@0: } michael@0: michael@0: private: michael@0: // this is the function that gets called by the CRT when the michael@0: // process is shutting down. michael@0: static int __cdecl OnExit() { michael@0: delete GetInstance(); michael@0: return 0; michael@0: } michael@0: }; michael@0: michael@0: // Convert a short path (C:\path~1 or \\??\\c:\path~1) to the long version of michael@0: // the path. If the path is not a valid filesystem path, the function returns michael@0: // false and the output parameter is not modified. michael@0: bool ConvertToLongPath(const std::wstring& short_path, std::wstring* long_path); michael@0: michael@0: // Sets result to true if the path contains a reparse point. The return value michael@0: // is ERROR_SUCCESS when the function succeeds or the appropriate error code michael@0: // when the function fails. michael@0: // This function is not smart. It looks for each element in the path and michael@0: // returns true if any of them is a reparse point. michael@0: DWORD IsReparsePoint(const std::wstring& full_path, bool* result); michael@0: michael@0: // Returns true if the handle corresponds to the object pointed by this path. michael@0: bool SameObject(HANDLE handle, const wchar_t* full_path); michael@0: michael@0: // Resolves a handle to an nt path. Returns true if the handle can be resolved. michael@0: bool GetPathFromHandle(HANDLE handle, std::wstring* path); michael@0: michael@0: // Resolves a win32 path to an nt path using GetPathFromHandle. The path must michael@0: // exist. Returs true if the translation was succesful. michael@0: bool GetNtPathFromWin32Path(const std::wstring& path, std::wstring* nt_path); michael@0: michael@0: // Translates a reserved key name to its handle. michael@0: // For example "HKEY_LOCAL_MACHINE" returns HKEY_LOCAL_MACHINE. michael@0: // Returns NULL if the name does not represent any reserved key name. michael@0: HKEY GetReservedKeyFromName(const std::wstring& name); michael@0: michael@0: // Resolves a user-readable registry path to a system-readable registry path. michael@0: // For example, HKEY_LOCAL_MACHINE\\Software\\microsoft is translated to michael@0: // \\registry\\machine\\software\\microsoft. Returns false if the path michael@0: // cannot be resolved. michael@0: bool ResolveRegistryName(std::wstring name, std::wstring* resolved_name); michael@0: michael@0: // Writes |length| bytes from the provided |buffer| into the address space of michael@0: // |child_process|, at the specified |address|, preserving the original write michael@0: // protection attributes. Returns true on success. michael@0: bool WriteProtectedChildMemory(HANDLE child_process, void* address, michael@0: const void* buffer, size_t length); michael@0: michael@0: } // namespace sandbox michael@0: michael@0: // Resolves a function name in NTDLL to a function pointer. The second parameter michael@0: // is a pointer to the function pointer. michael@0: void ResolveNTFunctionPtr(const char* name, void* ptr); michael@0: michael@0: #endif // SANDBOX_SRC_WIN_UTILS_H_