1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef SANDBOX_SRC_WIN_UTILS_H_
6 #define SANDBOX_SRC_WIN_UTILS_H_
10 #include "base/basictypes.h"
14 // Prefix for path used by NT calls.
15 const wchar_t kNTPrefix
[] = L
"\\??\\";
16 const size_t kNTPrefixLen
= arraysize(kNTPrefix
) - 1;
18 const wchar_t kNTObjManPrefix
[] = L
"\\Device\\";
19 const size_t kNTObjManPrefixLen
= arraysize(kNTObjManPrefix
) - 1;
21 // Automatically acquires and releases a lock when the object is
26 explicit AutoLock(CRITICAL_SECTION
*lock
) : lock_(lock
) {
27 ::EnterCriticalSection(lock
);
32 ::LeaveCriticalSection(lock_
);
36 CRITICAL_SECTION
*lock_
;
37 DISALLOW_IMPLICIT_CONSTRUCTORS(AutoLock
);
40 // Basic implementation of a singleton which calls the destructor
41 // when the exe is shutting down or the DLL is being unloaded.
42 template <typename Derived
>
45 static Derived
* GetInstance() {
46 static Derived
* instance
= NULL
;
47 if (NULL
== instance
) {
48 instance
= new Derived();
49 // Microsoft CRT extension. In an exe this this called after
50 // winmain returns, in a dll is called in DLL_PROCESS_DETACH
57 // this is the function that gets called by the CRT when the
58 // process is shutting down.
59 static int __cdecl
OnExit() {
65 // Convert a short path (C:\path~1 or \\??\\c:\path~1) to the long version of
66 // the path. If the path is not a valid filesystem path, the function returns
67 // false and the output parameter is not modified.
68 bool ConvertToLongPath(const std::wstring
& short_path
, std::wstring
* long_path
);
70 // Sets result to true if the path contains a reparse point. The return value
71 // is ERROR_SUCCESS when the function succeeds or the appropriate error code
72 // when the function fails.
73 // This function is not smart. It looks for each element in the path and
74 // returns true if any of them is a reparse point.
75 DWORD
IsReparsePoint(const std::wstring
& full_path
, bool* result
);
77 // Returns true if the handle corresponds to the object pointed by this path.
78 bool SameObject(HANDLE handle
, const wchar_t* full_path
);
80 // Resolves a handle to an nt path. Returns true if the handle can be resolved.
81 bool GetPathFromHandle(HANDLE handle
, std::wstring
* path
);
83 // Resolves a win32 path to an nt path using GetPathFromHandle. The path must
84 // exist. Returs true if the translation was succesful.
85 bool GetNtPathFromWin32Path(const std::wstring
& path
, std::wstring
* nt_path
);
87 // Translates a reserved key name to its handle.
88 // For example "HKEY_LOCAL_MACHINE" returns HKEY_LOCAL_MACHINE.
89 // Returns NULL if the name does not represent any reserved key name.
90 HKEY
GetReservedKeyFromName(const std::wstring
& name
);
92 // Resolves a user-readable registry path to a system-readable registry path.
93 // For example, HKEY_LOCAL_MACHINE\\Software\\microsoft is translated to
94 // \\registry\\machine\\software\\microsoft. Returns false if the path
95 // cannot be resolved.
96 bool ResolveRegistryName(std::wstring name
, std::wstring
* resolved_name
);
98 // Writes |length| bytes from the provided |buffer| into the address space of
99 // |child_process|, at the specified |address|, preserving the original write
100 // protection attributes. Returns true on success.
101 bool WriteProtectedChildMemory(HANDLE child_process
, void* address
,
102 const void* buffer
, size_t length
);
104 } // namespace sandbox
106 // Resolves a function name in NTDLL to a function pointer. The second parameter
107 // is a pointer to the function pointer.
108 void ResolveNTFunctionPtr(const char* name
, void* ptr
);
110 #endif // SANDBOX_SRC_WIN_UTILS_H_