📄 misc.hpp
字号:
/* misc.hpp
*
* Copyright (C) 2003 Sawtooth Consulting Ltd.
*
* This file is part of yaSSL.
*
* yaSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* yaSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
/* based on Wei Dai's misc.h from CryptoPP */
#ifndef TAO_CRYPT_MISC_HPP
#define TAO_CRYPT_MISC_HPP
#if !defined(DO_TAOCRYPT_KERNEL_MODE)
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#else
#include "kernelc.hpp"
#endif
#include "types.hpp"
#include "type_traits.hpp"
namespace TaoCrypt {
#ifdef YASSL_PURE_C
// library allocation
struct new_t {}; // TaoCrypt New type
extern new_t tc; // pass in parameter
} // namespace TaoCrypt
void* operator new (size_t, TaoCrypt::new_t);
void* operator new[](size_t, TaoCrypt::new_t);
void operator delete (void*, TaoCrypt::new_t);
void operator delete[](void*, TaoCrypt::new_t);
namespace TaoCrypt {
template<typename T>
void tcDelete(T* ptr)
{
if (ptr) ptr->~T();
::operator delete(ptr, TaoCrypt::tc);
}
template<typename T>
void tcArrayDelete(T* ptr)
{
// can't do array placement destruction since not tracking size in
// allocation, only allow builtins to use array placement since they
// don't need destructors called
typedef char builtin[IsFundamentalType<T>::Yes ? 1 : -1];
(void)sizeof(builtin);
::operator delete[](ptr, TaoCrypt::tc);
}
#define NEW_TC new (tc)
// to resolve compiler generated operator delete on base classes with
// virtual destructors (when on stack), make sure doesn't get called
class virtual_base {
public:
static void operator delete(void*) { assert(0); }
};
#else // YASSL_PURE_C
template<typename T>
void tcDelete(T* ptr)
{
delete ptr;
}
template<typename T>
void tcArrayDelete(T* ptr)
{
delete[] ptr;
}
#define NEW_TC new
class virtual_base {};
#endif // YASSL_PURE_C
#if defined(_MSC_VER) || defined(__BCPLUSPLUS__)
#define INTEL_INTRINSICS
#define FAST_ROTATE
#elif defined(__MWERKS__) && TARGET_CPU_PPC
#define PPC_INTRINSICS
#define FAST_ROTATE
#elif defined(__GNUC__) && defined(__i386__)
// GCC does peephole optimizations which should result in using rotate
// instructions
#define FAST_ROTATE
#endif
// no gas on these systems ?, disable for now
#if defined(__sun__) || defined (__QNX__)
#define TAOCRYPT_DISABLE_X86ASM
#endif
// Turn on ia32 ASM for Big Integer
// CodeWarrior defines _MSC_VER
#if !defined(TAOCRYPT_DISABLE_X86ASM) && ((defined(_MSC_VER) && \
!defined(__MWERKS__) && defined(_M_IX86)) || \
(defined(__GNUC__) && defined(__i386__)))
#define TAOCRYPT_X86ASM_AVAILABLE
#endif
// Turn on ia32 ASM for Ciphers and Message Digests
// Seperate define since these are more complex, use member offsets
// and user may want to turn off while leaving Big Integer optos on
#if defined(TAOCRYPT_X86ASM_AVAILABLE) && !defined(DISABLE_TAO_ASM)
#define TAO_ASM
#endif
// Extra word in older vtable implementations, for ASM member offset
#if defined(__GNUC__) && __GNUC__ < 3
#define OLD_GCC_OFFSET
#endif
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
# define TAOCRYPT_MALLOC_ALIGNMENT_IS_16
#endif
#if defined(__linux__) || defined(__sun__) || defined(__CYGWIN__)
# define TAOCRYPT_MEMALIGN_AVAILABLE
#endif
#if defined(_WIN32) || defined(__CYGWIN__)
#define TAOCRYPT_WIN32_AVAILABLE
#endif
#if defined(__unix__) || defined(__MACH__)
#define TAOCRYPT_UNIX_AVAILABLE
#endif
// VC60 workaround: it doesn't allow typename in some places
#if defined(_MSC_VER) && (_MSC_VER < 1300)
#define CPP_TYPENAME
#else
#define CPP_TYPENAME typename
#endif
#ifdef _MSC_VER
#define TAOCRYPT_NO_VTABLE __declspec(novtable)
#else
#define TAOCRYPT_NO_VTABLE
#endif
// ***************** DLL related ********************
#ifdef TAOCRYPT_WIN32_AVAILABLE
#ifdef TAOCRYPT_EXPORTS
#define TAOCRYPT_IS_DLL
#define TAOCRYPT_DLL __declspec(dllexport)
#elif defined(TAOCRYPT_IMPORTS)
#define TAOCRYPT_IS_DLL
#define TAOCRYPT_DLL __declspec(dllimport)
#else
#define TAOCRYPT_DLL
#endif // EXPORTS
#define TAOCRYPT_API __stdcall
#define TAOCRYPT_CDECL __cdecl
#else // TAOCRYPT_WIN32_AVAILABLE
#define TAOCRYPT_DLL
#define TAOCRYPT_API
#define TAOCRYPT_CDECL
#endif // TAOCRYPT_WIN32_AVAILABLE
// ****************** tempalte stuff *******************
#if defined(TAOCRYPT_MANUALLY_INSTANTIATE_TEMPLATES) && \
!defined(TAOCRYPT_IMPORTS)
#define TAOCRYPT_DLL_TEMPLATE_CLASS template class TAOCRYPT_DLL
#elif defined(__MWERKS__)
#define TAOCRYPT_DLL_TEMPLATE_CLASS extern class TAOCRYPT_DLL
#else
#define TAOCRYPT_DLL_TEMPLATE_CLASS extern template class TAOCRYPT_DLL
#endif
#if defined(TAOCRYPT_MANUALLY_INSTANTIATE_TEMPLATES) && \
!defined(TAOCRYPT_EXPORTS)
#define TAOCRYPT_STATIC_TEMPLATE_CLASS template class
#elif defined(__MWERKS__)
#define TAOCRYPT_STATIC_TEMPLATE_CLASS extern class
#else
#define TAOCRYPT_STATIC_TEMPLATE_CLASS extern template class
#endif
// ************** compile-time assertion ***************
template <bool b>
struct CompileAssert
{
static char dummy[2*b-1];
};
#define TAOCRYPT_COMPILE_ASSERT(assertion) \
TAOCRYPT_COMPILE_ASSERT_INSTANCE(assertion, __LINE__)
#if defined(TAOCRYPT_EXPORTS) || defined(TAOCRYPT_IMPORTS)
#define TAOCRYPT_COMPILE_ASSERT_INSTANCE(assertion, instance)
#else
#define TAOCRYPT_COMPILE_ASSERT_INSTANCE(assertion, instance) \
(void)sizeof(CompileAssert<(assertion)>)
#endif
#define TAOCRYPT_ASSERT_JOIN(X, Y) TAOCRYPT_DO_ASSERT_JOIN(X, Y)
#define TAOCRYPT_DO_ASSERT_JOIN(X, Y) X##Y
/*************** helpers *****************************/
inline unsigned int BitsToBytes(unsigned int bitCount)
{
return ((bitCount+7)/(8));
}
inline unsigned int BytesToWords(unsigned int byteCount)
{
return ((byteCount+WORD_SIZE-1)/WORD_SIZE);
}
inline unsigned int BitsToWords(unsigned int bitCount)
{
return ((bitCount+WORD_BITS-1)/(WORD_BITS));
}
inline void CopyWords(word* r, const word* a, word32 n)
{
for (word32 i = 0; i < n; i++)
r[i] = a[i];
}
inline unsigned int CountWords(const word* X, unsigned int N)
{
while (N && X[N-1]==0)
N--;
return N;
}
inline void SetWords(word* r, word a, unsigned int n)
{
for (unsigned int i=0; i<n; i++)
r[i] = a;
}
enum ByteOrder { LittleEndianOrder = 0, BigEndianOrder = 1 };
enum CipherDir {ENCRYPTION, DECRYPTION};
inline CipherDir ReverseDir(CipherDir dir)
{
return (dir == ENCRYPTION) ? DECRYPTION : ENCRYPTION;
}
template <typename ENUM_TYPE, int VALUE>
struct EnumToType
{
static ENUM_TYPE ToEnum() { return (ENUM_TYPE)VALUE; }
};
typedef EnumToType<ByteOrder, LittleEndianOrder> LittleEndian;
typedef EnumToType<ByteOrder, BigEndianOrder> BigEndian;
#ifndef BIG_ENDIAN_ORDER
typedef LittleEndian HostByteOrder;
#else
typedef BigEndian HostByteOrder;
#endif
inline ByteOrder GetHostByteOrder()
{
return HostByteOrder::ToEnum();
}
inline bool HostByteOrderIs(ByteOrder order)
{
return order == GetHostByteOrder();
}
void xorbuf(byte*, const byte*, unsigned int);
template <class T>
inline bool IsPowerOf2(T n)
{
return n > 0 && (n & (n-1)) == 0;
}
template <class T1, class T2>
inline T2 ModPowerOf2(T1 a, T2 b)
{
assert(IsPowerOf2(b));
return T2(a) & (b-1);
}
template <class T>
inline T RoundDownToMultipleOf(T n, T m)
{
return n - (IsPowerOf2(m) ? ModPowerOf2(n, m) : (n%m));
}
template <class T>
inline T RoundUpToMultipleOf(T n, T m)
{
return RoundDownToMultipleOf(n+m-1, m);
}
template <class T>
inline unsigned int GetAlignment(T* dummy = 0) // VC60 workaround
{
#if (_MSC_VER >= 1300)
return __alignof(T);
#elif defined(__GNUC__)
return __alignof__(T);
#else
return sizeof(T);
#endif
}
inline bool IsAlignedOn(const void* p, unsigned int alignment)
{
return IsPowerOf2(alignment) ? ModPowerOf2((size_t)p, alignment) == 0
: (size_t)p % alignment == 0;
}
template <class T>
inline bool IsAligned(const void* p, T* dummy = 0) // VC60 workaround
{
return IsAlignedOn(p, GetAlignment<T>());
}
template <class T> inline T rotlFixed(T x, unsigned int y)
{
assert(y < sizeof(T)*8);
return (x<<y) | (x>>(sizeof(T)*8-y));
}
template <class T> inline T rotrFixed(T x, unsigned int y)
{
assert(y < sizeof(T)*8);
return (x>>y) | (x<<(sizeof(T)*8-y));
}
#ifdef INTEL_INTRINSICS
#pragma intrinsic(_lrotl, _lrotr)
template<> inline word32 rotlFixed(word32 x, word32 y)
{
assert(y < 32);
return y ? _lrotl(x, y) : x;
}
template<> inline word32 rotrFixed(word32 x, word32 y)
{
assert(y < 32);
return y ? _lrotr(x, y) : x;
}
#endif // INTEL_INTRINSICS
#ifdef min
#undef min
#endif
inline word32 min(word32 a, word32 b)
{
return a < b ? a : b;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -