BVB Source Codes

mars Show unique_path.cpp Source code

Return Download mars: download unique_path.cpp Source code - Download mars Source code - Type:.cpp
  1. //  filesystem unique_path.cpp  --------------------------------------------------------//
  2.  
  3. //  Copyright Beman Dawes 2010
  4.  
  5. //  Distributed under the Boost Software License, Version 1.0.
  6. //  See http://www.boost.org/LICENSE_1_0.txt
  7.  
  8. //  Library home page: http://www.boost.org/libs/filesystem
  9.  
  10. //--------------------------------------------------------------------------------------//
  11.  
  12. // define BOOST_FILESYSTEM_SOURCE so that <boost/filesystem/config.hpp> knows
  13. // the library is being built (possibly exporting rather than importing code)
  14. #define BOOST_FILESYSTEM_SOURCE
  15.  
  16. #ifndef BOOST_SYSTEM_NO_DEPRECATED
  17. # define BOOST_SYSTEM_NO_DEPRECATED
  18. #endif
  19.  
  20. #include <boost/filesystem/operations.hpp>
  21. #include <cassert>
  22.  
  23. # ifdef BOOST_POSIX_API
  24. #   include <fcntl.h>
  25. #   ifdef BOOST_HAS_UNISTD_H
  26. #      include <unistd.h>
  27. #   endif
  28. # else // BOOST_WINDOWS_API
  29. #   include <windows.h>
  30. #   include <wincrypt.h>
  31. #   pragma comment(lib, "Advapi32.lib")
  32. # endif
  33.  
  34. namespace {
  35.  
  36. void fail(int err, mars_boost::system::error_code* ec)
  37. {
  38.   if (ec == 0)
  39.     BOOST_FILESYSTEM_THROW( mars_boost::system::system_error(err,
  40.       mars_boost::system::system_category(),
  41.       "mars_boost::filesystem::unique_path"));
  42.  
  43.   ec->assign(err, mars_boost::system::system_category());
  44.   return;
  45. }
  46.  
  47. #ifdef BOOST_WINDOWS_API
  48.  
  49. int acquire_crypt_handle(HCRYPTPROV& handle)
  50. {
  51.   if (::CryptAcquireContextW(&handle, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
  52.     return 0;
  53.  
  54.   int errval = ::GetLastError();
  55.   if (errval != NTE_BAD_KEYSET)
  56.     return errval;
  57.  
  58.   if (::CryptAcquireContextW(&handle, 0, 0, PROV_RSA_FULL, CRYPT_NEWKEYSET | CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
  59.     return 0;
  60.  
  61.   errval = ::GetLastError();
  62.   // Another thread could have attempted to create the keyset at the same time.
  63.   if (errval != NTE_EXISTS)
  64.     return errval;
  65.  
  66.   if (::CryptAcquireContextW(&handle, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
  67.     return 0;
  68.  
  69.   return ::GetLastError();
  70. }
  71.  
  72. #endif
  73.  
  74. void system_crypt_random(void* buf, std::size_t len, mars_boost::system::error_code* ec)
  75. {
  76. # ifdef BOOST_POSIX_API
  77.  
  78.   int file = open("/dev/urandom", O_RDONLY);
  79.   if (file == -1)
  80.   {
  81.     file = open("/dev/random", O_RDONLY);
  82.     if (file == -1)
  83.     {
  84.       fail(errno, ec);
  85.       return;
  86.     }
  87.   }
  88.  
  89.   size_t bytes_read = 0;
  90.   while (bytes_read < len)
  91.   {
  92.     ssize_t n = read(file, buf, len - bytes_read);
  93.     if (n == -1)
  94.     {
  95.       close(file);
  96.       fail(errno, ec);
  97.       return;
  98.     }
  99.     bytes_read += n;
  100.     buf = static_cast<char*>(buf) + n;
  101.   }
  102.  
  103.   close(file);
  104.  
  105. # else // BOOST_WINDOWS_API
  106.  
  107.   HCRYPTPROV handle;
  108.   int errval = acquire_crypt_handle(handle);
  109.  
  110.   if (!errval)
  111.   {
  112.     BOOL gen_ok = ::CryptGenRandom(handle, len, static_cast<unsigned char*>(buf));
  113.     if (!gen_ok)
  114.       errval = ::GetLastError();
  115.     ::CryptReleaseContext(handle, 0);
  116.   }
  117.  
  118.   if (!errval) return;
  119.  
  120.   fail(errval, ec);
  121. # endif
  122. }
  123.  
  124. }  // unnamed namespace
  125.  
  126. namespace mars_boost {} namespace boost = mars_boost; namespace mars_boost { namespace filesystem { namespace detail {
  127.  
  128. BOOST_FILESYSTEM_DECL
  129. path unique_path(const path& model, system::error_code* ec)
  130. {
  131.   std::wstring s (model.wstring());  // std::string ng for MBCS encoded POSIX
  132.   const wchar_t hex[] = L"0123456789abcdef";
  133.   char ran[] = "123456789abcdef";  // init to avoid clang static analyzer message
  134.                                    // see ticket #8954
  135.   assert(sizeof(ran) == 16);
  136.   const int max_nibbles = 2 * sizeof(ran);   // 4-bits per nibble
  137.  
  138.   int nibbles_used = max_nibbles;
  139.   for(std::wstring::size_type i=0; i < s.size(); ++i)
  140.   {
  141.     if (s[i] == L'%')                        // digit request
  142.     {
  143.       if (nibbles_used == max_nibbles)
  144.       {
  145.         system_crypt_random(ran, sizeof(ran), ec);
  146.         if (ec != 0 && *ec)
  147.           return "";
  148.         nibbles_used = 0;
  149.       }
  150.       int c = ran[nibbles_used/2];
  151.       c >>= 4 * (nibbles_used++ & 1);  // if odd, shift right 1 nibble
  152.       s[i] = hex[c & 0xf];             // convert to hex digit and replace
  153.     }
  154.   }
  155.  
  156.   if (ec != 0) ec->clear();
  157.  
  158.   return s;
  159. }
  160.  
  161. }}}
  162.  
downloadunique_path.cpp Source code - Download mars Source code
Related Source Codes/Software:
Hero - Elegant transition library for iOS & tvOS 2017-06-09
deep-photo-styletransfer - Code and data for paper "Deep Photo Style Transfer... 2017-06-09
mastodon - A GNU Social-compatible microblogging server ... 2017-06-09
plyr - A simple HTML5, YouTube and Vimeo player ... 2017-06-08
prepack - Prepack is a partial evaluator for JavaScript. Pre... 2017-06-08
Public-APIs - 2017-06-09
lottie-ios - An iOS library to natively render After Effects ve... 2017-06-09
Awesome-Hacking - A collection of various awesome lists for hackers,... 2017-06-09
algorithms - Minimal examples of data structures and algorithms... 2017-06-10
lectures - Oxford Deep NLP 2017 course 2017-06-10
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top