Several useful functions in Chrome sandbox

Source: Internet
Author: User
// Determine whether the handle and path are the same // we get a | full_path | of the form /?? /C:/Some/Foo/bar, and the name that/we'll get from | handle | will be/device/harddiskvolume1/Some/Foo/bar. bool sameobject (handle, const wchar_t * full_path) {STD: wstring path (full_path); dcheck (! Path. Empty (); // check if it's a pipe. If (ispipe (PATH) return true; STD: wstring actual_path; If (! Getpathfromhandle (handle, & actual_path) return false; // This may end with a backslash. const wchar_t kbackslash = '//'; If (path [path. length ()-1] = kbackslash) Path = path. substr (0, path. length ()-1); // perfect match (case-insesitive check ). if (0 = _ wcsicmp (actual_path.c_str (), path. c_str () return true; // look for the drive letter. size_t colon_pos = path. find (L': '); If (colon_pos = 0 | Colon_pos = STD: wstring: NPOs) return false; // only one character for the drive. If (colon_pos> 1 & path [colon_pos-2]! = Kbackslash) return false; // we only need 3 chars, but let's alloc a buffer for four. wchar_t drive [4] = {0}; wchar_t vol_name [max_path]; memcpy (drive, & path [colon_pos-1], 2 * sizeof (* drive )); // We'll get a double NULL terminated string. DWORD vol_length =: querydosdevicew (drive, vol_name, max_path); If (vol_length <2 | vol_length = max_path) return false; // ignore the nulls at the end. V Ol_length = static_cast (wcslen (vol_name); // The two paths shocould be the same length. If (vol_length + path. Size ()-(colon_pos + 1 )! = Actual_path.size () return false; // check up to the drive letter. If (0! = _ Wcsnicmp (actual_path.c_str (), vol_name, vol_length) return false; // check the path after the drive letter. If (0! = _ Wcsicmp (& actual_path [vol_length], & path [colon_pos + 1]) return false; return true ;} // convert a short path to a long path // convert a short path (C:/path ~ 1 Or //?? // C:/path ~ 1) to the long version of // the path. if the path is not a valid filesystem path, the function returns // false and the output parameter is not modified. bool converttolongpath (const STD: wstring & short_path, STD: wstring * long_path) {// check if the path is a NT path. bool is_nt_path = false; STD: wstring Path = short_path; If (0 = path. compare (0, kntprefixlen, kntprefix) {Path = path. substr (Knt Prefixlen); is_nt_path = true;} DWORD size = max_path; scoped_array long_path_buf (New wchar_t [size]); DWORD return_value =: getlongpathname (path. c_str (), long_path_buf.get (), size); While (return_value> = size) {size * = 2; long_path_buf.reset (New wchar_t [size]); return_value = :: getlongpathname (path. c_str (), long_path_buf.get (), size);} DWORD last_error =: getlasterror (); If (0 = return_valu E & (error_file_not_found = last_error | error_path_not_found = last_error | error_invalid_name = last_error) {// the file does not exist, but maybe a sub path needs to be expanded. STD: wstring: size_type last_slash = path. rfind (L' // '); If (STD: wstring: NPOs = last_slash) return false; STD: wstring begin = path. substr (0, last_slash); STD: wstring end = path. substr (last_slash); If (! Converttolongpath (begin, & begin) return false; // OK, it worked. let's reset the return value. path = begin + end; return_value = 1;} else if (0! = Return_value) {Path = long_path_buf.get ();} If (return_value! = 0) {If (is_nt_path) {* long_path = kntprefix; * long_path + = path;} else {* long_path = path;} return true;} return false ;} // obtain the path bool getpathfromhandle (handle, STD: wstring * path) {ntqueryobjectfunction ntqueryobject = NULL; callback ("ntqueryobject", & ntqueryobject); object_name_information initial_buffer; object_name_information * name = & initial_buffer; ulong size = Si Zeof (initial_buffer); // query the name information a first time to get the size of the name. ntstatus status = ntqueryobject (handle, objectnameinformation, name, size, & size); scoped_ptr name_ptr; If (size) {name = reinterpret_cast (New byte [size]); name_ptr.reset (name); // query the name information a second time to get the name of the // object referenced by the handle. status = ntqueryobject (H Andle, objectnameinformation, name, size, & size) ;}if (STATUS_SUCCESS! = Status) return false; Path-> assign (name-> objectname. buffer, name-> objectname. length/sizeof (name-> objectname. buffer [0]); Return true;} // converts a Win32 path to native (NT) type of path // http://stackoverflow.com/questions/4445108/how-can-i-convert-a-native-nt-pathname-into-a-win32-path-name//http://pdh11.blogspot.com/2009/05/pathcanonicalize-versus-what-it-says-on.html// resolves a Win32 path to an NT path using getpathfromhandle. the path must // exist. returs true if the translation was succesful. bool getntpathfromwin32path (const STD: wstring & Path, STD: wstring * nt_path) {handle file =: createfilew (path. c_str (), 0, file_cmd_read | file_cmd_write | file_cmd_delete, null, open_existing, empty, null); If (file = invalid_handle_value) return false; bool Rv = getpathfromhandle (file, nt_path);: closehandle (File); Return RV ;}
 
File Path: D:/project/Chrome/src/sandbox/src/win_utils.cc

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.