"Fossies" - the Fresh Open Source Software Archive

Member "src/Common/Exception.h" (10 Oct 2018, 3908 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Exception.h" see the Fossies "Dox" file reference documentation.

    1 /*
    2  Derived from source code of TrueCrypt 7.1a, which is
    3  Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
    4  by the TrueCrypt License 3.0.
    5 
    6  Modifications and additions to the original source code (contained in this file)
    7  and all other portions of this file are Copyright (c) 2013-2017 IDRIX
    8  and are governed by the Apache License 2.0 the full text of which is
    9  contained in the file License.txt included in VeraCrypt binary and source
   10  code distribution packages.
   11 */
   12 
   13 #ifndef TC_HEADER_Common_Exception
   14 #define TC_HEADER_Common_Exception
   15 
   16 #include "Platform/PlatformBase.h"
   17 #include "Dlgcode.h"
   18 #include "Language.h"
   19 #include <strsafe.h>
   20 
   21 namespace VeraCrypt
   22 {
   23     struct Exception
   24     {
   25         virtual void Show (HWND parent) const = 0;
   26     };
   27 
   28     struct SystemException : public Exception
   29     {
   30         SystemException (const char *srcPos) : ErrorCode (GetLastError()), SrcPos (srcPos) { }
   31 
   32         void Show (HWND parent) const
   33         {
   34             SetLastError (ErrorCode);
   35             handleWin32Error (parent, SrcPos);
   36         }
   37 
   38         DWORD ErrorCode;
   39         const char *SrcPos;
   40     };
   41 
   42     struct ErrorException : public Exception
   43     {
   44         ErrorException (char *langId, const char *srcPos) : SrcPos (srcPos), ErrLangId (langId) { }
   45         ErrorException (const wstring &errMsg, const char *srcPos) : SrcPos (srcPos), ErrLangId(NULL), ErrMsg (errMsg) { }
   46 
   47         void Show (HWND parent) const
   48         {
   49             if (ErrMsg.empty())
   50                 ::ErrorDirect (AppendSrcPos (GetString (ErrLangId? ErrLangId : ""), SrcPos).c_str (), parent);
   51             else
   52                 ::ErrorDirect (AppendSrcPos (ErrMsg.c_str(), SrcPos).c_str (), parent);
   53         }
   54 
   55         const char *SrcPos;
   56         char *ErrLangId;
   57         wstring ErrMsg;
   58     };
   59 
   60     struct ParameterIncorrect : public Exception
   61     {
   62         ParameterIncorrect (const char *srcPos) : SrcPos (srcPos) { }
   63 
   64         void Show (HWND parent) const
   65         {
   66             string msgBody = "Parameter incorrect.\n\n\n(If you report a bug in connection with this, please include the following technical information in the bug report:\n" + string (SrcPos) + ")";
   67             MessageBoxA (parent, msgBody.c_str(), "VeraCrypt", MB_ICONERROR | MB_SETFOREGROUND);
   68         }
   69 
   70         const char *SrcPos;
   71     };
   72 
   73     struct RandInitFailed : public Exception
   74     {
   75         RandInitFailed (const char *srcPos, DWORD dwLastError) : SrcPos (srcPos), LastError (dwLastError) { }
   76 
   77         void Show (HWND parent) const
   78         {
   79             char szErrCode[16];
   80             StringCchPrintfA (szErrCode, ARRAYSIZE(szErrCode), "0x%.8X", LastError);
   81             string msgBody = "The Random Generator initialization failed.\n\n\n(If you report a bug in connection with this, please include the following technical information in the bug report:\n" + string (SrcPos) + "\nLast Error = " + string (szErrCode) + ")";
   82             MessageBoxA (parent, msgBody.c_str(), "VeraCrypt", MB_ICONERROR | MB_SETFOREGROUND);
   83         }
   84 
   85         const char *SrcPos;
   86         DWORD LastError;
   87     };
   88 
   89     struct CryptoApiFailed : public Exception
   90     {
   91         CryptoApiFailed (const char *srcPos, DWORD dwLastError) : SrcPos (srcPos), LastError (dwLastError) { }
   92 
   93         void Show (HWND parent) const
   94         {
   95             char szErrCode[16];
   96             StringCchPrintfA (szErrCode, ARRAYSIZE(szErrCode), "0x%.8X", LastError);
   97             string msgBody = "Windows Crypto API failed.\n\n\n(If you report a bug in connection with this, please include the following technical information in the bug report:\n" + string (SrcPos) + "\nLast Error = " + string (szErrCode) + ")";
   98             MessageBoxA (parent, msgBody.c_str(), "VeraCrypt", MB_ICONERROR | MB_SETFOREGROUND);
   99         }
  100 
  101         const char *SrcPos;
  102         DWORD LastError;
  103     };
  104 
  105     struct TimeOut : public Exception
  106     {
  107         TimeOut (const char *srcPos) : SrcPos (srcPos) { }
  108         void Show (HWND parent) const { ErrorDirect (AppendSrcPos (L"Timeout", SrcPos).c_str (), parent); }
  109 
  110         const char *SrcPos;
  111     };
  112 
  113     struct UserAbort : public Exception
  114     {
  115         UserAbort (const char *srcPos) { }
  116         void Show (HWND parent) const { }
  117     };
  118 }
  119 
  120 #define throw_sys_if(condition) do { if (condition) throw SystemException( SRC_POS ); } while (false)
  121 
  122 
  123 #endif // TC_HEADER_Common_Exception