?? misc.h
字號:
#ifndef CRYPTOPP_MISC_H
#define CRYPTOPP_MISC_H
#include "cryptlib.h"
#include "smartptr.h"
#include <string.h> // for memcpy and memmove
#ifdef _MSC_VER
#include <stdlib.h>
#if _MSC_VER >= 1400
// VC2005 workaround: disable declarations that conflict with winnt.h
#define _interlockedbittestandset CRYPTOPP_DISABLED_INTRINSIC_1
#define _interlockedbittestandreset CRYPTOPP_DISABLED_INTRINSIC_2
#include <intrin.h>
#undef _interlockedbittestandset
#undef _interlockedbittestandreset
#define CRYPTOPP_FAST_ROTATE(x) 1
#elif _MSC_VER >= 1300
#define CRYPTOPP_FAST_ROTATE(x) ((x) == 32 | (x) == 64)
#else
#define CRYPTOPP_FAST_ROTATE(x) ((x) == 32)
#endif
#elif (defined(__MWERKS__) && TARGET_CPU_PPC) || \
(defined(__GNUC__) && (defined(_ARCH_PWR2) || defined(_ARCH_PWR) || defined(_ARCH_PPC) || defined(_ARCH_PPC64) || defined(_ARCH_COM)))
#define CRYPTOPP_FAST_ROTATE(x) ((x) == 32)
#elif defined(__GNUC__) && (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86) // depend on GCC's peephole optimization to generate rotate instructions
#define CRYPTOPP_FAST_ROTATE(x) 1
#else
#define CRYPTOPP_FAST_ROTATE(x) 0
#endif
#ifdef __BORLANDC__
#include <mem.h>
#endif
#if defined(__GNUC__) && defined(__linux__)
#define CRYPTOPP_BYTESWAP_AVAILABLE
#include <byteswap.h>
#endif
NAMESPACE_BEGIN(CryptoPP)
// ************** compile-time assertion ***************
template <bool b>
struct CompileAssert
{
static char dummy[2*b-1];
};
#define CRYPTOPP_COMPILE_ASSERT(assertion) CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, __LINE__)
#if defined(CRYPTOPP_EXPORTS) || defined(CRYPTOPP_IMPORTS)
#define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance)
#else
#define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance) static CompileAssert<(assertion)> CRYPTOPP_ASSERT_JOIN(cryptopp_assert_, instance)
#endif
#define CRYPTOPP_ASSERT_JOIN(X, Y) CRYPTOPP_DO_ASSERT_JOIN(X, Y)
#define CRYPTOPP_DO_ASSERT_JOIN(X, Y) X##Y
// ************** misc classes ***************
class CRYPTOPP_DLL Empty
{
};
//! _
template <class BASE1, class BASE2>
class CRYPTOPP_NO_VTABLE TwoBases : public BASE1, public BASE2
{
};
//! _
template <class BASE1, class BASE2, class BASE3>
class CRYPTOPP_NO_VTABLE ThreeBases : public BASE1, public BASE2, public BASE3
{
};
template <class T>
class ObjectHolder
{
protected:
T m_object;
};
class NotCopyable
{
public:
NotCopyable() {}
private:
NotCopyable(const NotCopyable &);
void operator=(const NotCopyable &);
};
template <class T>
struct NewObject
{
T* operator()() const {return new T;}
};
/*! This function safely initializes a static object in a multithreaded environment without using locks.
It may leak memory when two threads try to initialize the static object at the same time
but this should be acceptable since each static object is only initialized once per session.
*/
template <class T, class F = NewObject<T>, int instance=0>
class Singleton
{
public:
Singleton(F objectFactory = F()) : m_objectFactory(objectFactory) {}
// prevent this function from being inlined
CRYPTOPP_NOINLINE const T & Ref(CRYPTOPP_NOINLINE_DOTDOTDOT) const;
private:
F m_objectFactory;
};
template <class T, class F, int instance>
const T & Singleton<T, F, instance>::Ref(CRYPTOPP_NOINLINE_DOTDOTDOT) const
{
static simple_ptr<T> s_pObject;
static char s_objectState = 0;
retry:
switch (s_objectState)
{
case 0:
s_objectState = 1;
try
{
s_pObject.m_p = m_objectFactory();
}
catch(...)
{
s_objectState = 0;
throw;
}
s_objectState = 2;
break;
case 1:
goto retry;
default:
break;
}
return *s_pObject.m_p;
}
// ************** misc functions ***************
#if (!__STDC_WANT_SECURE_LIB__)
inline void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
{
if (count > sizeInBytes)
throw InvalidArgument("memcpy_s: buffer overflow");
memcpy(dest, src, count);
}
inline void memmove_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
{
if (count > sizeInBytes)
throw InvalidArgument("memmove_s: buffer overflow");
memmove(dest, src, count);
}
#endif
// can't use std::min or std::max in MSVC60 or Cygwin 1.1.0
template <class T> inline const T& STDMIN(const T& a, const T& b)
{
return b < a ? b : a;
}
template <class T1, class T2> inline const T1 UnsignedMin(const T1& a, const T2& b)
{
CRYPTOPP_COMPILE_ASSERT((sizeof(T1)<=sizeof(T2) && T2(-1)>0) || (sizeof(T1)>sizeof(T2) && T1(-1)>0));
assert(a==0 || a>0); // GCC workaround: get rid of the warning "comparison is always true due to limited range of data type"
assert(b>=0);
if (sizeof(T1)<=sizeof(T2))
return b < (T2)a ? (T1)b : a;
else
return (T1)b < a ? (T1)b : a;
}
template <class T> inline const T& STDMAX(const T& a, const T& b)
{
return a < b ? b : a;
}
#define RETURN_IF_NONZERO(x) size_t returnedValue = x; if (returnedValue) return returnedValue
// this version of the macro is fastest on Pentium 3 and Pentium 4 with MSVC 6 SP5 w/ Processor Pack
#define GETBYTE(x, y) (unsigned int)byte((x)>>(8*(y)))
// these may be faster on other CPUs/compilers
// #define GETBYTE(x, y) (unsigned int)(((x)>>(8*(y)))&255)
// #define GETBYTE(x, y) (((byte *)&(x))[y])
#define CRYPTOPP_GET_BYTE_AS_BYTE(x, y) byte((x)>>(8*(y)))
template <class T>
unsigned int Parity(T value)
{
for (unsigned int i=8*sizeof(value)/2; i>0; i/=2)
value ^= value >> i;
return (unsigned int)value&1;
}
template <class T>
unsigned int BytePrecision(const T &value)
{
if (!value)
return 0;
unsigned int l=0, h=8*sizeof(value);
while (h-l > 8)
{
unsigned int t = (l+h)/2;
if (value >> t)
l = t;
else
h = t;
}
return h/8;
}
template <class T>
unsigned int BitPrecision(const T &value)
{
if (!value)
return 0;
unsigned int l=0, h=8*sizeof(value);
while (h-l > 1)
{
unsigned int t = (l+h)/2;
if (value >> t)
l = t;
else
h = t;
}
return h;
}
template <class T>
inline T Crop(T value, size_t size)
{
if (size < 8*sizeof(value))
return T(value & ((T(1) << size) - 1));
else
return value;
}
template <class T1, class T2>
inline bool SafeConvert(T1 from, T2 &to)
{
to = (T2)from;
if (from != to || (from > 0) != (to > 0))
return false;
return true;
}
inline size_t BitsToBytes(size_t bitCount)
{
return ((bitCount+7)/(8));
}
inline size_t BytesToWords(size_t byteCount)
{
return ((byteCount+WORD_SIZE-1)/WORD_SIZE);
}
inline size_t BitsToWords(size_t bitCount)
{
return ((bitCount+WORD_BITS-1)/(WORD_BITS));
}
inline size_t BitsToDwords(size_t bitCount)
{
return ((bitCount+2*WORD_BITS-1)/(2*WORD_BITS));
}
CRYPTOPP_DLL void CRYPTOPP_API xorbuf(byte *buf, const byte *mask, size_t count);
CRYPTOPP_DLL void CRYPTOPP_API xorbuf(byte *output, const byte *input, const byte *mask, size_t count);
template <class T>
inline bool IsPowerOf2(const T &n)
{
return n > 0 && (n & (n-1)) == 0;
}
template <class T1, class T2>
inline T2 ModPowerOf2(const T1 &a, const T2 &b)
{
assert(IsPowerOf2(b));
return T2(a) & (b-1);
}
template <class T1, class T2>
inline T1 RoundDownToMultipleOf(const T1 &n, const T2 &m)
{
if (IsPowerOf2(m))
return n - ModPowerOf2(n, m);
else
return n - n%m;
}
template <class T1, class T2>
inline T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
{
if (n+m-1 < n)
throw InvalidArgument("RoundUpToMultipleOf: integer overflow");
return RoundDownToMultipleOf(n+m-1, m);
}
template <class T>
inline unsigned int GetAlignmentOf(T *dummy=NULL) // VC60 workaround
{
#if CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X86
if (sizeof(T) < 16)
return 1; // alignment not needed on x86 and x64
#endif
#if (_MSC_VER >= 1300)
return __alignof(T);
#elif defined(__GNUC__)
return __alignof__(T);
#elif defined(CRYPTOPP_SLOW_WORD64)
return UnsignedMin(4U, sizeof(T));
#else
return sizeof(T);
#endif
}
inline bool IsAlignedOn(const void *p, unsigned int alignment)
{
return alignment==1 || (IsPowerOf2(alignment) ? ModPowerOf2((size_t)p, alignment) == 0 : (size_t)p % alignment == 0);
}
template <class T>
inline bool IsAligned(const void *p, T *dummy=NULL) // VC60 workaround
{
return IsAlignedOn(p, GetAlignmentOf<T>());
}
#ifdef IS_LITTLE_ENDIAN
typedef LittleEndian NativeByteOrder;
#else
typedef BigEndian NativeByteOrder;
#endif
inline ByteOrder GetNativeByteOrder()
{
return NativeByteOrder::ToEnum();
}
inline bool NativeByteOrderIs(ByteOrder order)
{
return order == GetNativeByteOrder();
}
template <class T>
std::string IntToString(T a, unsigned int base = 10)
{
if (a == 0)
return "0";
bool negate = false;
if (a < 0)
{
negate = true;
a = 0-a; // VC .NET does not like -a
}
std::string result;
while (a > 0)
{
T digit = a % base;
result = char((digit < 10 ? '0' : ('a' - 10)) + digit) + result;
a /= base;
}
if (negate)
result = "-" + result;
return result;
}
template <class T1, class T2>
inline T1 SaturatingSubtract(const T1 &a, const T2 &b)
{
return T1((a > b) ? (a - b) : 0);
}
template <class T>
inline CipherDir GetCipherDir(const T &obj)
{
return obj.IsForwardTransformation() ? ENCRYPTION : DECRYPTION;
}
CRYPTOPP_DLL void CRYPTOPP_API CallNewHandler();
inline void IncrementCounterByOne(byte *inout, unsigned int s)
{
for (int i=s-1, carry=1; i>=0 && carry; i--)
carry = !++inout[i];
}
inline void IncrementCounterByOne(byte *output, const byte *input, unsigned int s)
{
int i, carry;
for (i=s-1, carry=1; i>=0 && carry; i--)
carry = ((output[i] = input[i]+1) == 0);
memcpy_s(output, s, input, i+1);
}
// ************** rotate functions ***************
template <class T> inline T rotlFixed(T x, unsigned int y)
{
assert(y < sizeof(T)*8);
return T((x<<y) | (x>>(sizeof(T)*8-y)));
}
template <class T> inline T rotrFixed(T x, unsigned int y)
{
assert(y < sizeof(T)*8);
return T((x>>y) | (x<<(sizeof(T)*8-y)));
}
template <class T> inline T rotlVariable(T x, unsigned int y)
{
assert(y < sizeof(T)*8);
return T((x<<y) | (x>>(sizeof(T)*8-y)));
}
template <class T> inline T rotrVariable(T x, unsigned int y)
{
assert(y < sizeof(T)*8);
return T((x>>y) | (x<<(sizeof(T)*8-y)));
}
template <class T> inline T rotlMod(T x, unsigned int y)
{
y %= sizeof(T)*8;
return T((x<<y) | (x>>(sizeof(T)*8-y)));
}
template <class T> inline T rotrMod(T x, unsigned int y)
{
y %= sizeof(T)*8;
return T((x>>y) | (x<<(sizeof(T)*8-y)));
}
#ifdef _MSC_VER
template<> inline word32 rotlFixed<word32>(word32 x, unsigned int y)
{
assert(y < 8*sizeof(x));
return y ? _lrotl(x, y) : x;
}
template<> inline word32 rotrFixed<word32>(word32 x, unsigned int y)
{
assert(y < 8*sizeof(x));
return y ? _lrotr(x, y) : x;
}
template<> inline word32 rotlVariable<word32>(word32 x, unsigned int y)
{
assert(y < 8*sizeof(x));
return _lrotl(x, y);
}
template<> inline word32 rotrVariable<word32>(word32 x, unsigned int y)
{
assert(y < 8*sizeof(x));
return _lrotr(x, y);
}
template<> inline word32 rotlMod<word32>(word32 x, unsigned int y)
{
return _lrotl(x, y);
}
template<> inline word32 rotrMod<word32>(word32 x, unsigned int y)
{
return _lrotr(x, y);
}
#endif // #ifdef _MSC_VER
#if _MSC_VER >= 1300 && !defined(__INTEL_COMPILER)
// Intel C++ Compiler 10.0 calls a function instead of using the rotate instruction when using these instructions
template<> inline word64 rotlFixed<word64>(word64 x, unsigned int y)
{
assert(y < 8*sizeof(x));
return y ? _rotl64(x, y) : x;
}
template<> inline word64 rotrFixed<word64>(word64 x, unsigned int y)
{
assert(y < 8*sizeof(x));
return y ? _rotr64(x, y) : x;
}
template<> inline word64 rotlVariable<word64>(word64 x, unsigned int y)
{
assert(y < 8*sizeof(x));
return _rotl64(x, y);
}
template<> inline word64 rotrVariable<word64>(word64 x, unsigned int y)
{
assert(y < 8*sizeof(x));
return _rotr64(x, y);
}
template<> inline word64 rotlMod<word64>(word64 x, unsigned int y)
{
return _rotl64(x, y);
}
template<> inline word64 rotrMod<word64>(word64 x, unsigned int y)
{
return _rotr64(x, y);
}
#endif // #if _MSC_VER >= 1310
#if _MSC_VER >= 1400 && !defined(__INTEL_COMPILER)
// Intel C++ Compiler 10.0 gives undefined externals with these
template<> inline word16 rotlFixed<word16>(word16 x, unsigned int y)
{
assert(y < 8*sizeof(x));
return y ? _rotl16(x, y) : x;
}
template<> inline word16 rotrFixed<word16>(word16 x, unsigned int y)
{
assert(y < 8*sizeof(x));
return y ? _rotr16(x, y) : x;
}
template<> inline word16 rotlVariable<word16>(word16 x, unsigned int y)
{
assert(y < 8*sizeof(x));
return _rotl16(x, y);
}
template<> inline word16 rotrVariable<word16>(word16 x, unsigned int y)
{
assert(y < 8*sizeof(x));
return _rotr16(x, y);
}
template<> inline word16 rotlMod<word16>(word16 x, unsigned int y)
{
return _rotl16(x, y);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -