[INTSAFE.H]

- Rename internal macros and add comments to make it more obvious what the macro does
- Add UShortMult

svn path=/trunk/; revision=60522
This commit is contained in:
Timo Kreuzer 2013-10-04 13:37:25 +00:00
parent 2752d3ffd5
commit 1753f36879

View file

@ -119,6 +119,7 @@ C_ASSERT(sizeof(INT) == 4);
C_ASSERT(sizeof(UINT) == 4); C_ASSERT(sizeof(UINT) == 4);
C_ASSERT(sizeof(LONG) == 4); C_ASSERT(sizeof(LONG) == 4);
C_ASSERT(sizeof(ULONG) == 4); C_ASSERT(sizeof(ULONG) == 4);
C_ASSERT(sizeof(DWORD) == 4);
C_ASSERT(sizeof(UINT_PTR) == sizeof(ULONG_PTR)); C_ASSERT(sizeof(UINT_PTR) == sizeof(ULONG_PTR));
/* Undefine these to avoid conflicts with limits.h */ /* Undefine these to avoid conflicts with limits.h */
@ -270,6 +271,7 @@ C_ASSERT(sizeof(UINT_PTR) == sizeof(ULONG_PTR));
#endif #endif
/* Convert unsigned to signed or unsigned */
#define DEFINE_SAFE_CONVERT_UTOX(_Name, _TypeFrom, _TypeTo) \ #define DEFINE_SAFE_CONVERT_UTOX(_Name, _TypeFrom, _TypeTo) \
_Must_inspect_result_ \ _Must_inspect_result_ \
__forceinline \ __forceinline \
@ -332,7 +334,8 @@ DEFINE_SAFE_CONVERT_UTOX(ULongLongToULong, ULONGLONG, ULONG)
DEFINE_SAFE_CONVERT_UTOX(ULongLongToULongPtr, ULONGLONG, ULONG_PTR) DEFINE_SAFE_CONVERT_UTOX(ULongLongToULongPtr, ULONGLONG, ULONG_PTR)
#define DEFINE_SAFE_CONVERT_ITOU(_Name, _TypeFrom, _TypeTo) \ /* Convert signed to unsigned */
#define DEFINE_SAFE_CONVERT_STOU(_Name, _TypeFrom, _TypeTo) \
_Must_inspect_result_ \ _Must_inspect_result_ \
__forceinline \ __forceinline \
INTSAFE_RESULT \ INTSAFE_RESULT \
@ -352,60 +355,61 @@ INTSAFE_NAME(_Name)( \
} \ } \
} }
DEFINE_SAFE_CONVERT_ITOU(Int8ToUChar, INT8, UCHAR) DEFINE_SAFE_CONVERT_STOU(Int8ToUChar, INT8, UCHAR)
DEFINE_SAFE_CONVERT_ITOU(Int8ToUInt8, INT8, UINT8) DEFINE_SAFE_CONVERT_STOU(Int8ToUInt8, INT8, UINT8)
DEFINE_SAFE_CONVERT_ITOU(Int8ToUShort, INT8, USHORT) DEFINE_SAFE_CONVERT_STOU(Int8ToUShort, INT8, USHORT)
DEFINE_SAFE_CONVERT_ITOU(Int8ToUInt, INT8, UINT) DEFINE_SAFE_CONVERT_STOU(Int8ToUInt, INT8, UINT)
DEFINE_SAFE_CONVERT_ITOU(Int8ToULong, INT8, ULONG) DEFINE_SAFE_CONVERT_STOU(Int8ToULong, INT8, ULONG)
DEFINE_SAFE_CONVERT_ITOU(Int8ToUIntPtr, INT8, UINT_PTR) DEFINE_SAFE_CONVERT_STOU(Int8ToUIntPtr, INT8, UINT_PTR)
DEFINE_SAFE_CONVERT_ITOU(Int8ToULongPtr, INT8, ULONG_PTR) DEFINE_SAFE_CONVERT_STOU(Int8ToULongPtr, INT8, ULONG_PTR)
DEFINE_SAFE_CONVERT_ITOU(Int8ToULongLong, INT8, ULONGLONG) DEFINE_SAFE_CONVERT_STOU(Int8ToULongLong, INT8, ULONGLONG)
DEFINE_SAFE_CONVERT_ITOU(ShortToUChar, SHORT, UCHAR) DEFINE_SAFE_CONVERT_STOU(ShortToUChar, SHORT, UCHAR)
DEFINE_SAFE_CONVERT_ITOU(ShortToUInt8, SHORT, UINT8) DEFINE_SAFE_CONVERT_STOU(ShortToUInt8, SHORT, UINT8)
DEFINE_SAFE_CONVERT_ITOU(ShortToUShort, SHORT, USHORT) DEFINE_SAFE_CONVERT_STOU(ShortToUShort, SHORT, USHORT)
DEFINE_SAFE_CONVERT_ITOU(ShortToUInt, SHORT, UINT) DEFINE_SAFE_CONVERT_STOU(ShortToUInt, SHORT, UINT)
DEFINE_SAFE_CONVERT_ITOU(ShortToULong, SHORT, ULONG) DEFINE_SAFE_CONVERT_STOU(ShortToULong, SHORT, ULONG)
DEFINE_SAFE_CONVERT_ITOU(ShortToUIntPtr, SHORT, UINT_PTR) DEFINE_SAFE_CONVERT_STOU(ShortToUIntPtr, SHORT, UINT_PTR)
DEFINE_SAFE_CONVERT_ITOU(ShortToULongPtr, SHORT, ULONG_PTR) DEFINE_SAFE_CONVERT_STOU(ShortToULongPtr, SHORT, ULONG_PTR)
DEFINE_SAFE_CONVERT_ITOU(ShortToDWordPtr, SHORT, DWORD_PTR) DEFINE_SAFE_CONVERT_STOU(ShortToDWordPtr, SHORT, DWORD_PTR)
DEFINE_SAFE_CONVERT_ITOU(ShortToULongLong, SHORT, ULONGLONG) DEFINE_SAFE_CONVERT_STOU(ShortToULongLong, SHORT, ULONGLONG)
DEFINE_SAFE_CONVERT_ITOU(IntToUChar, INT, UCHAR) DEFINE_SAFE_CONVERT_STOU(IntToUChar, INT, UCHAR)
DEFINE_SAFE_CONVERT_ITOU(IntToUInt8, INT, UINT8) DEFINE_SAFE_CONVERT_STOU(IntToUInt8, INT, UINT8)
DEFINE_SAFE_CONVERT_ITOU(IntToUShort, INT, USHORT) DEFINE_SAFE_CONVERT_STOU(IntToUShort, INT, USHORT)
DEFINE_SAFE_CONVERT_ITOU(IntToUInt, INT, UINT) DEFINE_SAFE_CONVERT_STOU(IntToUInt, INT, UINT)
DEFINE_SAFE_CONVERT_ITOU(IntToULong, INT, ULONG) DEFINE_SAFE_CONVERT_STOU(IntToULong, INT, ULONG)
DEFINE_SAFE_CONVERT_ITOU(IntToULongLong, INT, ULONGLONG) DEFINE_SAFE_CONVERT_STOU(IntToULongLong, INT, ULONGLONG)
DEFINE_SAFE_CONVERT_ITOU(LongToUChar, LONG, UCHAR) DEFINE_SAFE_CONVERT_STOU(LongToUChar, LONG, UCHAR)
DEFINE_SAFE_CONVERT_ITOU(LongToUInt8, LONG, UINT8) DEFINE_SAFE_CONVERT_STOU(LongToUInt8, LONG, UINT8)
DEFINE_SAFE_CONVERT_ITOU(LongToUShort, LONG, USHORT) DEFINE_SAFE_CONVERT_STOU(LongToUShort, LONG, USHORT)
DEFINE_SAFE_CONVERT_ITOU(LongToUInt, LONG, UINT) DEFINE_SAFE_CONVERT_STOU(LongToUInt, LONG, UINT)
DEFINE_SAFE_CONVERT_ITOU(LongToULong, LONG, ULONG) DEFINE_SAFE_CONVERT_STOU(LongToULong, LONG, ULONG)
DEFINE_SAFE_CONVERT_ITOU(LongToUIntPtr, LONG, UINT_PTR) DEFINE_SAFE_CONVERT_STOU(LongToUIntPtr, LONG, UINT_PTR)
DEFINE_SAFE_CONVERT_ITOU(LongToULongPtr, LONG, ULONG_PTR) DEFINE_SAFE_CONVERT_STOU(LongToULongPtr, LONG, ULONG_PTR)
DEFINE_SAFE_CONVERT_ITOU(LongToULongLong, LONG, ULONGLONG) DEFINE_SAFE_CONVERT_STOU(LongToULongLong, LONG, ULONGLONG)
DEFINE_SAFE_CONVERT_ITOU(IntPtrToUChar, INT_PTR, UCHAR) DEFINE_SAFE_CONVERT_STOU(IntPtrToUChar, INT_PTR, UCHAR)
DEFINE_SAFE_CONVERT_ITOU(IntPtrToUInt8, INT_PTR, UINT8) DEFINE_SAFE_CONVERT_STOU(IntPtrToUInt8, INT_PTR, UINT8)
DEFINE_SAFE_CONVERT_ITOU(IntPtrToUShort, INT_PTR, USHORT) DEFINE_SAFE_CONVERT_STOU(IntPtrToUShort, INT_PTR, USHORT)
DEFINE_SAFE_CONVERT_ITOU(IntPtrToUInt, INT_PTR, UINT) DEFINE_SAFE_CONVERT_STOU(IntPtrToUInt, INT_PTR, UINT)
DEFINE_SAFE_CONVERT_ITOU(IntPtrToULong, INT_PTR, ULONG) DEFINE_SAFE_CONVERT_STOU(IntPtrToULong, INT_PTR, ULONG)
DEFINE_SAFE_CONVERT_ITOU(IntPtrToUIntPtr, INT_PTR, UINT_PTR) DEFINE_SAFE_CONVERT_STOU(IntPtrToUIntPtr, INT_PTR, UINT_PTR)
DEFINE_SAFE_CONVERT_ITOU(IntPtrToULongPtr, INT_PTR, ULONG_PTR) DEFINE_SAFE_CONVERT_STOU(IntPtrToULongPtr, INT_PTR, ULONG_PTR)
DEFINE_SAFE_CONVERT_ITOU(IntPtrToULongLong, INT_PTR, ULONGLONG) DEFINE_SAFE_CONVERT_STOU(IntPtrToULongLong, INT_PTR, ULONGLONG)
DEFINE_SAFE_CONVERT_ITOU(LongPtrToUChar, LONG_PTR, UCHAR) DEFINE_SAFE_CONVERT_STOU(LongPtrToUChar, LONG_PTR, UCHAR)
DEFINE_SAFE_CONVERT_ITOU(LongPtrToUInt8, LONG_PTR, UINT8) DEFINE_SAFE_CONVERT_STOU(LongPtrToUInt8, LONG_PTR, UINT8)
DEFINE_SAFE_CONVERT_ITOU(LongPtrToUShort, LONG_PTR, USHORT) DEFINE_SAFE_CONVERT_STOU(LongPtrToUShort, LONG_PTR, USHORT)
DEFINE_SAFE_CONVERT_ITOU(LongPtrToUInt, LONG_PTR, UINT) DEFINE_SAFE_CONVERT_STOU(LongPtrToUInt, LONG_PTR, UINT)
DEFINE_SAFE_CONVERT_ITOU(LongPtrToULong, LONG_PTR, ULONG) DEFINE_SAFE_CONVERT_STOU(LongPtrToULong, LONG_PTR, ULONG)
DEFINE_SAFE_CONVERT_ITOU(LongPtrToUIntPtr, LONG_PTR, UINT_PTR) DEFINE_SAFE_CONVERT_STOU(LongPtrToUIntPtr, LONG_PTR, UINT_PTR)
DEFINE_SAFE_CONVERT_ITOU(LongPtrToULongPtr, LONG_PTR, ULONG_PTR) DEFINE_SAFE_CONVERT_STOU(LongPtrToULongPtr, LONG_PTR, ULONG_PTR)
DEFINE_SAFE_CONVERT_ITOU(LongPtrToULongLong, LONG_PTR, ULONGLONG) DEFINE_SAFE_CONVERT_STOU(LongPtrToULongLong, LONG_PTR, ULONGLONG)
#ifdef _CHAR_UNSIGNED #ifdef _CHAR_UNSIGNED
DEFINE_SAFE_CONVERT_ITOU(ShortToChar, SHORT, UCHAR) DEFINE_SAFE_CONVERT_STOU(ShortToChar, SHORT, UCHAR)
DEFINE_SAFE_CONVERT_ITOU(LongPtrToChar, LONG_PTR, UCHAR) DEFINE_SAFE_CONVERT_STOU(LongPtrToChar, LONG_PTR, UCHAR)
#endif #endif
#define DEFINE_SAFE_CONVERT_ITOI(_Name, _TypeFrom, _TypeTo) \ /* Convert signed to signed */
#define DEFINE_SAFE_CONVERT_STOS(_Name, _TypeFrom, _TypeTo) \
_Must_inspect_result_ \ _Must_inspect_result_ \
__forceinline \ __forceinline \
INTSAFE_RESULT \ INTSAFE_RESULT \
@ -425,28 +429,28 @@ INTSAFE_NAME(_Name)( \
} \ } \
} }
DEFINE_SAFE_CONVERT_ITOI(ShortToInt8, SHORT, INT8) DEFINE_SAFE_CONVERT_STOS(ShortToInt8, SHORT, INT8)
DEFINE_SAFE_CONVERT_ITOI(IntToInt8, INT, INT8) DEFINE_SAFE_CONVERT_STOS(IntToInt8, INT, INT8)
DEFINE_SAFE_CONVERT_ITOI(IntToShort, INT, SHORT) DEFINE_SAFE_CONVERT_STOS(IntToShort, INT, SHORT)
DEFINE_SAFE_CONVERT_ITOI(LongToInt8, LONG, INT8) DEFINE_SAFE_CONVERT_STOS(LongToInt8, LONG, INT8)
DEFINE_SAFE_CONVERT_ITOI(LongToShort, LONG, SHORT) DEFINE_SAFE_CONVERT_STOS(LongToShort, LONG, SHORT)
DEFINE_SAFE_CONVERT_ITOI(LongToInt, LONG, INT) DEFINE_SAFE_CONVERT_STOS(LongToInt, LONG, INT)
DEFINE_SAFE_CONVERT_ITOI(IntPtrToInt8, INT_PTR, INT8) DEFINE_SAFE_CONVERT_STOS(IntPtrToInt8, INT_PTR, INT8)
DEFINE_SAFE_CONVERT_ITOI(IntPtrToShort, INT_PTR, SHORT) DEFINE_SAFE_CONVERT_STOS(IntPtrToShort, INT_PTR, SHORT)
DEFINE_SAFE_CONVERT_ITOI(IntPtrToInt, INT_PTR, INT) DEFINE_SAFE_CONVERT_STOS(IntPtrToInt, INT_PTR, INT)
DEFINE_SAFE_CONVERT_ITOI(IntPtrToLong, INT_PTR, LONG) DEFINE_SAFE_CONVERT_STOS(IntPtrToLong, INT_PTR, LONG)
DEFINE_SAFE_CONVERT_ITOI(IntPtrToLongPtr, INT_PTR, LONG_PTR) DEFINE_SAFE_CONVERT_STOS(IntPtrToLongPtr, INT_PTR, LONG_PTR)
DEFINE_SAFE_CONVERT_ITOI(LongPtrToInt8, LONG_PTR, INT8) DEFINE_SAFE_CONVERT_STOS(LongPtrToInt8, LONG_PTR, INT8)
DEFINE_SAFE_CONVERT_ITOI(LongPtrToShort, LONG_PTR, SHORT) DEFINE_SAFE_CONVERT_STOS(LongPtrToShort, LONG_PTR, SHORT)
DEFINE_SAFE_CONVERT_ITOI(LongPtrToInt, LONG_PTR, INT) DEFINE_SAFE_CONVERT_STOS(LongPtrToInt, LONG_PTR, INT)
DEFINE_SAFE_CONVERT_ITOI(LongPtrToLong, LONG_PTR, LONG) DEFINE_SAFE_CONVERT_STOS(LongPtrToLong, LONG_PTR, LONG)
DEFINE_SAFE_CONVERT_ITOI(LongPtrToIntPtr, LONG_PTR, INT_PTR) DEFINE_SAFE_CONVERT_STOS(LongPtrToIntPtr, LONG_PTR, INT_PTR)
DEFINE_SAFE_CONVERT_ITOI(LongLongToLong, LONGLONG, LONG) DEFINE_SAFE_CONVERT_STOS(LongLongToLong, LONGLONG, LONG)
DEFINE_SAFE_CONVERT_ITOI(LongLongToIntPtr, LONGLONG, INT_PTR) DEFINE_SAFE_CONVERT_STOS(LongLongToIntPtr, LONGLONG, INT_PTR)
DEFINE_SAFE_CONVERT_ITOI(LongLongToLongPtr, LONGLONG, LONG_PTR) DEFINE_SAFE_CONVERT_STOS(LongLongToLongPtr, LONGLONG, LONG_PTR)
#ifndef _CHAR_UNSIGNED #ifndef _CHAR_UNSIGNED
DEFINE_SAFE_CONVERT_ITOI(ShortToChar, SHORT, CHAR) DEFINE_SAFE_CONVERT_STOS(ShortToChar, SHORT, CHAR)
DEFINE_SAFE_CONVERT_ITOI(LongPtrToChar, LONG_PTR, CHAR) DEFINE_SAFE_CONVERT_STOS(LongPtrToChar, LONG_PTR, CHAR)
#endif #endif
@ -466,6 +470,7 @@ DEFINE_SAFE_CONVERT_ITOI(LongPtrToChar, LONG_PTR, CHAR)
#define RtlInt8ToSIZET RtlInt8ToULongPtr #define RtlInt8ToSIZET RtlInt8ToULongPtr
#define RtlIntToSizeT RtlIntToUIntPtr #define RtlIntToSizeT RtlIntToUIntPtr
#define RtlIntToSIZET RtlIntToULongPtr #define RtlIntToSIZET RtlIntToULongPtr
#define RtlULongToSSIZET RtlULongToLongPtr
#define RtlULongToByte RtlULongToUInt8 #define RtlULongToByte RtlULongToUInt8
#define RtlULongLongToInt64 RtlULongLongToLongLong #define RtlULongLongToInt64 RtlULongLongToLongLong
#define RtlULongLongToLong64 RtlULongLongToLongLong #define RtlULongLongToLong64 RtlULongLongToLongLong
@ -502,6 +507,7 @@ DEFINE_SAFE_CONVERT_ITOI(LongPtrToChar, LONG_PTR, CHAR)
#define Int8ToSIZET Int8ToULongPtr #define Int8ToSIZET Int8ToULongPtr
#define IntToSizeT IntToUIntPtr #define IntToSizeT IntToUIntPtr
#define IntToSIZET IntToULongPtr #define IntToSIZET IntToULongPtr
#define ULongToSSIZET ULongToLongPtr
#define ULongToByte ULongToUInt8 #define ULongToByte ULongToUInt8
#define ULongLongToInt64 ULongLongToLongLong #define ULongLongToInt64 ULongLongToLongLong
#define ULongLongToLong64 ULongLongToLongLong #define ULongLongToLong64 ULongLongToLongLong
@ -657,7 +663,7 @@ INTSAFE_NAME(ULongLongMult)(
+ M1Hi * M2Hi * 0x100000000 * 0x100000000 + M1Hi * M2Hi * 0x100000000 * 0x100000000
We get an overflow when We get an overflow when
a) M1Hi * M2Hi != 0, so when M1Hi or M2Hi are not 0 a) M1Hi * M2Hi != 0, so when M1Hi and M2Hi are both not 0
b) The product of the nonzero high part and the other low part b) The product of the nonzero high part and the other low part
is larger than 32 bits. is larger than 32 bits.
c) The addition of the product from b) shifted left by 32 and c) The addition of the product from b) shifted left by 32 and
@ -712,6 +718,21 @@ DEFINE_SAFE_MULT_U32(SizeTMult, size_t, ULongLongToSizeT)
DEFINE_SAFE_MULT_U32(SIZETMult, SIZE_T, ULongLongToSIZET) DEFINE_SAFE_MULT_U32(SIZETMult, SIZE_T, ULongLongToSIZET)
#endif #endif
#define DEFINE_SAFE_MULT_U16(_Name, _Type, _Convert) \
__checkReturn \
__forceinline \
INTSAFE_RESULT \
INTSAFE_NAME(_Name)( \
_In_ _Type Multiplicand, \
_In_ _Type Multiplier, \
_Out_ _Deref_out_range_(==, Multiplicand * Multiplier) _Type* pOutput) \
{ \
ULONG Result = ((ULONG)Multiplicand) * ((ULONG)Multiplier); \
return INTSAFE_NAME(_Convert)(Result, pOutput); \
}
DEFINE_SAFE_MULT_U16(UShortMult, USHORT, ULongToUShort)
#ifdef _NTINTSAFE_H_INCLUDED_ #ifdef _NTINTSAFE_H_INCLUDED_
@ -731,6 +752,8 @@ DEFINE_SAFE_MULT_U32(SIZETMult, SIZE_T, ULongLongToSIZET)
#define RtlULong64Sub RtlULongLongSub #define RtlULong64Sub RtlULongLongSub
#define RtlDWord64Sub RtlULongLongSub #define RtlDWord64Sub RtlULongLongSub
#define RtlUInt64Sub RtlULongLongSub #define RtlUInt64Sub RtlULongLongSub
#define RtlUInt16Mult RtlUShortMult
#define RtlWordMult RtlUShortMult
#ifdef _WIN64 #ifdef _WIN64
#define RtlIntPtrSub RtlLongLongSub #define RtlIntPtrSub RtlLongLongSub
#define RtlLongPtrSub RtlLongLongSub #define RtlLongPtrSub RtlLongLongSub
@ -757,6 +780,8 @@ DEFINE_SAFE_MULT_U32(SIZETMult, SIZE_T, ULongLongToSIZET)
#define ULong64Sub ULongLongSub #define ULong64Sub ULongLongSub
#define DWord64Sub ULongLongSub #define DWord64Sub ULongLongSub
#define UInt64Sub ULongLongSub #define UInt64Sub ULongLongSub
#define UInt16Mult UShortMult
#define WordMult UShortMult
#ifdef _WIN64 #ifdef _WIN64
#define IntPtrSub LongLongSub #define IntPtrSub LongLongSub
#define LongPtrSub LongLongSub #define LongPtrSub LongLongSub