Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef SANDBOX_SRC_WIN_UTILS_H_ |
michael@0 | 6 | #define SANDBOX_SRC_WIN_UTILS_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include <windows.h> |
michael@0 | 9 | #include <string> |
michael@0 | 10 | #include "base/basictypes.h" |
michael@0 | 11 | |
michael@0 | 12 | namespace sandbox { |
michael@0 | 13 | |
michael@0 | 14 | // Prefix for path used by NT calls. |
michael@0 | 15 | const wchar_t kNTPrefix[] = L"\\??\\"; |
michael@0 | 16 | const size_t kNTPrefixLen = arraysize(kNTPrefix) - 1; |
michael@0 | 17 | |
michael@0 | 18 | const wchar_t kNTObjManPrefix[] = L"\\Device\\"; |
michael@0 | 19 | const size_t kNTObjManPrefixLen = arraysize(kNTObjManPrefix) - 1; |
michael@0 | 20 | |
michael@0 | 21 | // Automatically acquires and releases a lock when the object is |
michael@0 | 22 | // is destroyed. |
michael@0 | 23 | class AutoLock { |
michael@0 | 24 | public: |
michael@0 | 25 | // Acquires the lock. |
michael@0 | 26 | explicit AutoLock(CRITICAL_SECTION *lock) : lock_(lock) { |
michael@0 | 27 | ::EnterCriticalSection(lock); |
michael@0 | 28 | }; |
michael@0 | 29 | |
michael@0 | 30 | // Releases the lock; |
michael@0 | 31 | ~AutoLock() { |
michael@0 | 32 | ::LeaveCriticalSection(lock_); |
michael@0 | 33 | }; |
michael@0 | 34 | |
michael@0 | 35 | private: |
michael@0 | 36 | CRITICAL_SECTION *lock_; |
michael@0 | 37 | DISALLOW_IMPLICIT_CONSTRUCTORS(AutoLock); |
michael@0 | 38 | }; |
michael@0 | 39 | |
michael@0 | 40 | // Basic implementation of a singleton which calls the destructor |
michael@0 | 41 | // when the exe is shutting down or the DLL is being unloaded. |
michael@0 | 42 | template <typename Derived> |
michael@0 | 43 | class SingletonBase { |
michael@0 | 44 | public: |
michael@0 | 45 | static Derived* GetInstance() { |
michael@0 | 46 | static Derived* instance = NULL; |
michael@0 | 47 | if (NULL == instance) { |
michael@0 | 48 | instance = new Derived(); |
michael@0 | 49 | // Microsoft CRT extension. In an exe this this called after |
michael@0 | 50 | // winmain returns, in a dll is called in DLL_PROCESS_DETACH |
michael@0 | 51 | _onexit(OnExit); |
michael@0 | 52 | } |
michael@0 | 53 | return instance; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | private: |
michael@0 | 57 | // this is the function that gets called by the CRT when the |
michael@0 | 58 | // process is shutting down. |
michael@0 | 59 | static int __cdecl OnExit() { |
michael@0 | 60 | delete GetInstance(); |
michael@0 | 61 | return 0; |
michael@0 | 62 | } |
michael@0 | 63 | }; |
michael@0 | 64 | |
michael@0 | 65 | // Convert a short path (C:\path~1 or \\??\\c:\path~1) to the long version of |
michael@0 | 66 | // the path. If the path is not a valid filesystem path, the function returns |
michael@0 | 67 | // false and the output parameter is not modified. |
michael@0 | 68 | bool ConvertToLongPath(const std::wstring& short_path, std::wstring* long_path); |
michael@0 | 69 | |
michael@0 | 70 | // Sets result to true if the path contains a reparse point. The return value |
michael@0 | 71 | // is ERROR_SUCCESS when the function succeeds or the appropriate error code |
michael@0 | 72 | // when the function fails. |
michael@0 | 73 | // This function is not smart. It looks for each element in the path and |
michael@0 | 74 | // returns true if any of them is a reparse point. |
michael@0 | 75 | DWORD IsReparsePoint(const std::wstring& full_path, bool* result); |
michael@0 | 76 | |
michael@0 | 77 | // Returns true if the handle corresponds to the object pointed by this path. |
michael@0 | 78 | bool SameObject(HANDLE handle, const wchar_t* full_path); |
michael@0 | 79 | |
michael@0 | 80 | // Resolves a handle to an nt path. Returns true if the handle can be resolved. |
michael@0 | 81 | bool GetPathFromHandle(HANDLE handle, std::wstring* path); |
michael@0 | 82 | |
michael@0 | 83 | // Resolves a win32 path to an nt path using GetPathFromHandle. The path must |
michael@0 | 84 | // exist. Returs true if the translation was succesful. |
michael@0 | 85 | bool GetNtPathFromWin32Path(const std::wstring& path, std::wstring* nt_path); |
michael@0 | 86 | |
michael@0 | 87 | // Translates a reserved key name to its handle. |
michael@0 | 88 | // For example "HKEY_LOCAL_MACHINE" returns HKEY_LOCAL_MACHINE. |
michael@0 | 89 | // Returns NULL if the name does not represent any reserved key name. |
michael@0 | 90 | HKEY GetReservedKeyFromName(const std::wstring& name); |
michael@0 | 91 | |
michael@0 | 92 | // Resolves a user-readable registry path to a system-readable registry path. |
michael@0 | 93 | // For example, HKEY_LOCAL_MACHINE\\Software\\microsoft is translated to |
michael@0 | 94 | // \\registry\\machine\\software\\microsoft. Returns false if the path |
michael@0 | 95 | // cannot be resolved. |
michael@0 | 96 | bool ResolveRegistryName(std::wstring name, std::wstring* resolved_name); |
michael@0 | 97 | |
michael@0 | 98 | // Writes |length| bytes from the provided |buffer| into the address space of |
michael@0 | 99 | // |child_process|, at the specified |address|, preserving the original write |
michael@0 | 100 | // protection attributes. Returns true on success. |
michael@0 | 101 | bool WriteProtectedChildMemory(HANDLE child_process, void* address, |
michael@0 | 102 | const void* buffer, size_t length); |
michael@0 | 103 | |
michael@0 | 104 | } // namespace sandbox |
michael@0 | 105 | |
michael@0 | 106 | // Resolves a function name in NTDLL to a function pointer. The second parameter |
michael@0 | 107 | // is a pointer to the function pointer. |
michael@0 | 108 | void ResolveNTFunctionPtr(const char* name, void* ptr); |
michael@0 | 109 | |
michael@0 | 110 | #endif // SANDBOX_SRC_WIN_UTILS_H_ |