2015-07-22 06:11:52 +00:00
|
|
|
// trap.h - written and placed in public domain by Jeffrey Walton.
|
|
|
|
// Copyright assigned to Crypto++ project
|
|
|
|
|
|
|
|
#ifndef CRYPTOPP_TRAP_H
|
|
|
|
#define CRYPTOPP_TRAP_H
|
|
|
|
|
2015-08-03 23:17:42 +00:00
|
|
|
#include "config.h"
|
2015-07-22 06:11:52 +00:00
|
|
|
|
2015-08-03 23:17:42 +00:00
|
|
|
// CRYPTOPP_POSIX_ASSERT unconditionally disables the library assert and yields
|
|
|
|
// to Posix assert. CRYPTOPP_POSIX_ASSERT can be set in config.h. if you want
|
|
|
|
// to disable asserts, then define NDEBUG or _NDEBUG when building the library.
|
|
|
|
|
|
|
|
// Needed for NDEBUG and CRYPTOPP_POSIX_ASSERT
|
2015-07-22 06:11:52 +00:00
|
|
|
#include <cassert>
|
|
|
|
|
2015-08-03 23:17:42 +00:00
|
|
|
#if defined(CRYPTOPP_DEBUG)
|
|
|
|
# include <iostream>
|
|
|
|
# include <sstream>
|
|
|
|
# if defined(CRYPTOPP_WIN32_AVAILABLE)
|
|
|
|
# pragma push_macro("WIN32_LEAN_AND_MEAN")
|
2016-05-03 04:23:05 +00:00
|
|
|
# pragma push_macro("_WIN32_WINNT")
|
|
|
|
# pragma push_macro("NOMINMAX")
|
2015-08-03 23:17:42 +00:00
|
|
|
# define WIN32_LEAN_AND_MEAN
|
2016-05-03 04:23:05 +00:00
|
|
|
# define _WIN32_WINNT 0x0400
|
|
|
|
# define NOMINMAX
|
2015-08-03 23:17:42 +00:00
|
|
|
# include <Windows.h>
|
|
|
|
# elif defined(CRYPTOPP_BSD_AVAILABLE) || defined(CRYPTOPP_UNIX_AVAILABLE)
|
|
|
|
# include <signal.h>
|
|
|
|
# endif
|
|
|
|
#endif // CRYPTOPP_DEBUG
|
|
|
|
|
2015-07-22 06:11:52 +00:00
|
|
|
// ************** run-time assertion ***************
|
|
|
|
|
2015-08-03 23:17:42 +00:00
|
|
|
// See test.cpp and DebugTrapHandler for code to install a null signal handler
|
|
|
|
// for SIGTRAP on BSD, Linux and Unix. The handler installs itself during
|
2015-07-26 19:26:47 +00:00
|
|
|
// initialization of the test program.
|
|
|
|
|
2015-08-03 23:17:42 +00:00
|
|
|
#if defined(CRYPTOPP_DEBUG) && (defined(CRYPTOPP_BSD_AVAILABLE) || defined(CRYPTOPP_UNIX_AVAILABLE))
|
2015-07-22 06:11:52 +00:00
|
|
|
# define CRYPTOPP_ASSERT(exp) { \
|
2015-08-03 23:17:42 +00:00
|
|
|
if (!(exp)) { \
|
2015-07-22 06:11:52 +00:00
|
|
|
std::ostringstream oss; \
|
|
|
|
oss << "Assertion failed: " << (char*)(__FILE__) << "(" \
|
|
|
|
<< (int)(__LINE__) << "): " << (char*)(__func__) \
|
|
|
|
<< std::endl; \
|
|
|
|
std::cerr << oss.str(); \
|
|
|
|
raise(SIGTRAP); \
|
|
|
|
} \
|
|
|
|
}
|
2015-08-03 23:17:42 +00:00
|
|
|
#elif defined(CRYPTOPP_DEBUG) && defined(CRYPTOPP_WIN32_AVAILABLE)
|
|
|
|
# define CRYPTOPP_ASSERT(exp) { \
|
|
|
|
if (!(exp)) { \
|
|
|
|
std::ostringstream oss; \
|
|
|
|
oss << "Assertion failed: " << (char*)(__FILE__) << "(" \
|
|
|
|
<< (int)(__LINE__) << "): " << (char*)(__FUNCTION__) \
|
|
|
|
<< std::endl; \
|
|
|
|
DebugBreak(); \
|
|
|
|
std::cerr << oss.str(); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
// Fallback to original behavior for NDEBUG (and non-Windows/non-Unix builds)
|
2015-07-22 06:11:52 +00:00
|
|
|
#else
|
|
|
|
# define CRYPTOPP_ASSERT(exp) assert(exp)
|
2015-08-03 23:17:42 +00:00
|
|
|
#endif // DEBUG and Unix or Windows
|
2015-07-22 06:11:52 +00:00
|
|
|
|
2015-08-03 23:17:42 +00:00
|
|
|
#if defined(CRYPTOPP_DEBUG) && defined(CRYPTOPP_WIN32_AVAILABLE)
|
|
|
|
# pragma pop_macro("WIN32_LEAN_AND_MEAN")
|
2016-05-03 04:23:05 +00:00
|
|
|
# pragma pop_macro("_WIN32_WINNT")
|
|
|
|
# pragma pop_macro("NOMINMAX")
|
2015-08-03 23:17:42 +00:00
|
|
|
#endif
|
2015-07-22 06:11:52 +00:00
|
|
|
|
2015-08-03 23:17:42 +00:00
|
|
|
#endif // CRYPTOPP_TRAP_H
|