Author: tkreuzer
Date: Fri Oct 4 13:37:25 2013
New Revision: 60522
URL:
http://svn.reactos.org/svn/reactos?rev=60522&view=rev
Log:
[INTSAFE.H]
- Rename internal macros and add comments to make it more obvious what the macro does
- Add UShortMult
Modified:
trunk/reactos/include/psdk/intsafe.h
Modified: trunk/reactos/include/psdk/intsafe.h
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/include/psdk/intsafe.h?rev…
==============================================================================
--- trunk/reactos/include/psdk/intsafe.h [iso-8859-1] (original)
+++ trunk/reactos/include/psdk/intsafe.h [iso-8859-1] Fri Oct 4 13:37:25 2013
@@ -119,6 +119,7 @@
C_ASSERT(sizeof(UINT) == 4);
C_ASSERT(sizeof(LONG) == 4);
C_ASSERT(sizeof(ULONG) == 4);
+C_ASSERT(sizeof(DWORD) == 4);
C_ASSERT(sizeof(UINT_PTR) == sizeof(ULONG_PTR));
/* Undefine these to avoid conflicts with limits.h */
@@ -270,6 +271,7 @@
#endif
+/* Convert unsigned to signed or unsigned */
#define DEFINE_SAFE_CONVERT_UTOX(_Name, _TypeFrom, _TypeTo) \
_Must_inspect_result_ \
__forceinline \
@@ -332,7 +334,8 @@
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_ \
__forceinline \
INTSAFE_RESULT \
@@ -352,60 +355,61 @@
} \
}
-DEFINE_SAFE_CONVERT_ITOU(Int8ToUChar, INT8, UCHAR)
-DEFINE_SAFE_CONVERT_ITOU(Int8ToUInt8, INT8, UINT8)
-DEFINE_SAFE_CONVERT_ITOU(Int8ToUShort, INT8, USHORT)
-DEFINE_SAFE_CONVERT_ITOU(Int8ToUInt, INT8, UINT)
-DEFINE_SAFE_CONVERT_ITOU(Int8ToULong, INT8, ULONG)
-DEFINE_SAFE_CONVERT_ITOU(Int8ToUIntPtr, INT8, UINT_PTR)
-DEFINE_SAFE_CONVERT_ITOU(Int8ToULongPtr, INT8, ULONG_PTR)
-DEFINE_SAFE_CONVERT_ITOU(Int8ToULongLong, INT8, ULONGLONG)
-DEFINE_SAFE_CONVERT_ITOU(ShortToUChar, SHORT, UCHAR)
-DEFINE_SAFE_CONVERT_ITOU(ShortToUInt8, SHORT, UINT8)
-DEFINE_SAFE_CONVERT_ITOU(ShortToUShort, SHORT, USHORT)
-DEFINE_SAFE_CONVERT_ITOU(ShortToUInt, SHORT, UINT)
-DEFINE_SAFE_CONVERT_ITOU(ShortToULong, SHORT, ULONG)
-DEFINE_SAFE_CONVERT_ITOU(ShortToUIntPtr, SHORT, UINT_PTR)
-DEFINE_SAFE_CONVERT_ITOU(ShortToULongPtr, SHORT, ULONG_PTR)
-DEFINE_SAFE_CONVERT_ITOU(ShortToDWordPtr, SHORT, DWORD_PTR)
-DEFINE_SAFE_CONVERT_ITOU(ShortToULongLong, SHORT, ULONGLONG)
-DEFINE_SAFE_CONVERT_ITOU(IntToUChar, INT, UCHAR)
-DEFINE_SAFE_CONVERT_ITOU(IntToUInt8, INT, UINT8)
-DEFINE_SAFE_CONVERT_ITOU(IntToUShort, INT, USHORT)
-DEFINE_SAFE_CONVERT_ITOU(IntToUInt, INT, UINT)
-DEFINE_SAFE_CONVERT_ITOU(IntToULong, INT, ULONG)
-DEFINE_SAFE_CONVERT_ITOU(IntToULongLong, INT, ULONGLONG)
-DEFINE_SAFE_CONVERT_ITOU(LongToUChar, LONG, UCHAR)
-DEFINE_SAFE_CONVERT_ITOU(LongToUInt8, LONG, UINT8)
-DEFINE_SAFE_CONVERT_ITOU(LongToUShort, LONG, USHORT)
-DEFINE_SAFE_CONVERT_ITOU(LongToUInt, LONG, UINT)
-DEFINE_SAFE_CONVERT_ITOU(LongToULong, LONG, ULONG)
-DEFINE_SAFE_CONVERT_ITOU(LongToUIntPtr, LONG, UINT_PTR)
-DEFINE_SAFE_CONVERT_ITOU(LongToULongPtr, LONG, ULONG_PTR)
-DEFINE_SAFE_CONVERT_ITOU(LongToULongLong, LONG, ULONGLONG)
-DEFINE_SAFE_CONVERT_ITOU(IntPtrToUChar, INT_PTR, UCHAR)
-DEFINE_SAFE_CONVERT_ITOU(IntPtrToUInt8, INT_PTR, UINT8)
-DEFINE_SAFE_CONVERT_ITOU(IntPtrToUShort, INT_PTR, USHORT)
-DEFINE_SAFE_CONVERT_ITOU(IntPtrToUInt, INT_PTR, UINT)
-DEFINE_SAFE_CONVERT_ITOU(IntPtrToULong, INT_PTR, ULONG)
-DEFINE_SAFE_CONVERT_ITOU(IntPtrToUIntPtr, INT_PTR, UINT_PTR)
-DEFINE_SAFE_CONVERT_ITOU(IntPtrToULongPtr, INT_PTR, ULONG_PTR)
-DEFINE_SAFE_CONVERT_ITOU(IntPtrToULongLong, INT_PTR, ULONGLONG)
-DEFINE_SAFE_CONVERT_ITOU(LongPtrToUChar, LONG_PTR, UCHAR)
-DEFINE_SAFE_CONVERT_ITOU(LongPtrToUInt8, LONG_PTR, UINT8)
-DEFINE_SAFE_CONVERT_ITOU(LongPtrToUShort, LONG_PTR, USHORT)
-DEFINE_SAFE_CONVERT_ITOU(LongPtrToUInt, LONG_PTR, UINT)
-DEFINE_SAFE_CONVERT_ITOU(LongPtrToULong, LONG_PTR, ULONG)
-DEFINE_SAFE_CONVERT_ITOU(LongPtrToUIntPtr, LONG_PTR, UINT_PTR)
-DEFINE_SAFE_CONVERT_ITOU(LongPtrToULongPtr, LONG_PTR, ULONG_PTR)
-DEFINE_SAFE_CONVERT_ITOU(LongPtrToULongLong, LONG_PTR, ULONGLONG)
+DEFINE_SAFE_CONVERT_STOU(Int8ToUChar, INT8, UCHAR)
+DEFINE_SAFE_CONVERT_STOU(Int8ToUInt8, INT8, UINT8)
+DEFINE_SAFE_CONVERT_STOU(Int8ToUShort, INT8, USHORT)
+DEFINE_SAFE_CONVERT_STOU(Int8ToUInt, INT8, UINT)
+DEFINE_SAFE_CONVERT_STOU(Int8ToULong, INT8, ULONG)
+DEFINE_SAFE_CONVERT_STOU(Int8ToUIntPtr, INT8, UINT_PTR)
+DEFINE_SAFE_CONVERT_STOU(Int8ToULongPtr, INT8, ULONG_PTR)
+DEFINE_SAFE_CONVERT_STOU(Int8ToULongLong, INT8, ULONGLONG)
+DEFINE_SAFE_CONVERT_STOU(ShortToUChar, SHORT, UCHAR)
+DEFINE_SAFE_CONVERT_STOU(ShortToUInt8, SHORT, UINT8)
+DEFINE_SAFE_CONVERT_STOU(ShortToUShort, SHORT, USHORT)
+DEFINE_SAFE_CONVERT_STOU(ShortToUInt, SHORT, UINT)
+DEFINE_SAFE_CONVERT_STOU(ShortToULong, SHORT, ULONG)
+DEFINE_SAFE_CONVERT_STOU(ShortToUIntPtr, SHORT, UINT_PTR)
+DEFINE_SAFE_CONVERT_STOU(ShortToULongPtr, SHORT, ULONG_PTR)
+DEFINE_SAFE_CONVERT_STOU(ShortToDWordPtr, SHORT, DWORD_PTR)
+DEFINE_SAFE_CONVERT_STOU(ShortToULongLong, SHORT, ULONGLONG)
+DEFINE_SAFE_CONVERT_STOU(IntToUChar, INT, UCHAR)
+DEFINE_SAFE_CONVERT_STOU(IntToUInt8, INT, UINT8)
+DEFINE_SAFE_CONVERT_STOU(IntToUShort, INT, USHORT)
+DEFINE_SAFE_CONVERT_STOU(IntToUInt, INT, UINT)
+DEFINE_SAFE_CONVERT_STOU(IntToULong, INT, ULONG)
+DEFINE_SAFE_CONVERT_STOU(IntToULongLong, INT, ULONGLONG)
+DEFINE_SAFE_CONVERT_STOU(LongToUChar, LONG, UCHAR)
+DEFINE_SAFE_CONVERT_STOU(LongToUInt8, LONG, UINT8)
+DEFINE_SAFE_CONVERT_STOU(LongToUShort, LONG, USHORT)
+DEFINE_SAFE_CONVERT_STOU(LongToUInt, LONG, UINT)
+DEFINE_SAFE_CONVERT_STOU(LongToULong, LONG, ULONG)
+DEFINE_SAFE_CONVERT_STOU(LongToUIntPtr, LONG, UINT_PTR)
+DEFINE_SAFE_CONVERT_STOU(LongToULongPtr, LONG, ULONG_PTR)
+DEFINE_SAFE_CONVERT_STOU(LongToULongLong, LONG, ULONGLONG)
+DEFINE_SAFE_CONVERT_STOU(IntPtrToUChar, INT_PTR, UCHAR)
+DEFINE_SAFE_CONVERT_STOU(IntPtrToUInt8, INT_PTR, UINT8)
+DEFINE_SAFE_CONVERT_STOU(IntPtrToUShort, INT_PTR, USHORT)
+DEFINE_SAFE_CONVERT_STOU(IntPtrToUInt, INT_PTR, UINT)
+DEFINE_SAFE_CONVERT_STOU(IntPtrToULong, INT_PTR, ULONG)
+DEFINE_SAFE_CONVERT_STOU(IntPtrToUIntPtr, INT_PTR, UINT_PTR)
+DEFINE_SAFE_CONVERT_STOU(IntPtrToULongPtr, INT_PTR, ULONG_PTR)
+DEFINE_SAFE_CONVERT_STOU(IntPtrToULongLong, INT_PTR, ULONGLONG)
+DEFINE_SAFE_CONVERT_STOU(LongPtrToUChar, LONG_PTR, UCHAR)
+DEFINE_SAFE_CONVERT_STOU(LongPtrToUInt8, LONG_PTR, UINT8)
+DEFINE_SAFE_CONVERT_STOU(LongPtrToUShort, LONG_PTR, USHORT)
+DEFINE_SAFE_CONVERT_STOU(LongPtrToUInt, LONG_PTR, UINT)
+DEFINE_SAFE_CONVERT_STOU(LongPtrToULong, LONG_PTR, ULONG)
+DEFINE_SAFE_CONVERT_STOU(LongPtrToUIntPtr, LONG_PTR, UINT_PTR)
+DEFINE_SAFE_CONVERT_STOU(LongPtrToULongPtr, LONG_PTR, ULONG_PTR)
+DEFINE_SAFE_CONVERT_STOU(LongPtrToULongLong, LONG_PTR, ULONGLONG)
#ifdef _CHAR_UNSIGNED
-DEFINE_SAFE_CONVERT_ITOU(ShortToChar, SHORT, UCHAR)
-DEFINE_SAFE_CONVERT_ITOU(LongPtrToChar, LONG_PTR, UCHAR)
-#endif
-
-
-#define DEFINE_SAFE_CONVERT_ITOI(_Name, _TypeFrom, _TypeTo) \
+DEFINE_SAFE_CONVERT_STOU(ShortToChar, SHORT, UCHAR)
+DEFINE_SAFE_CONVERT_STOU(LongPtrToChar, LONG_PTR, UCHAR)
+#endif
+
+
+/* Convert signed to signed */
+#define DEFINE_SAFE_CONVERT_STOS(_Name, _TypeFrom, _TypeTo) \
_Must_inspect_result_ \
__forceinline \
INTSAFE_RESULT \
@@ -425,28 +429,28 @@
} \
}
-DEFINE_SAFE_CONVERT_ITOI(ShortToInt8, SHORT, INT8)
-DEFINE_SAFE_CONVERT_ITOI(IntToInt8, INT, INT8)
-DEFINE_SAFE_CONVERT_ITOI(IntToShort, INT, SHORT)
-DEFINE_SAFE_CONVERT_ITOI(LongToInt8, LONG, INT8)
-DEFINE_SAFE_CONVERT_ITOI(LongToShort, LONG, SHORT)
-DEFINE_SAFE_CONVERT_ITOI(LongToInt, LONG, INT)
-DEFINE_SAFE_CONVERT_ITOI(IntPtrToInt8, INT_PTR, INT8)
-DEFINE_SAFE_CONVERT_ITOI(IntPtrToShort, INT_PTR, SHORT)
-DEFINE_SAFE_CONVERT_ITOI(IntPtrToInt, INT_PTR, INT)
-DEFINE_SAFE_CONVERT_ITOI(IntPtrToLong, INT_PTR, LONG)
-DEFINE_SAFE_CONVERT_ITOI(IntPtrToLongPtr, INT_PTR, LONG_PTR)
-DEFINE_SAFE_CONVERT_ITOI(LongPtrToInt8, LONG_PTR, INT8)
-DEFINE_SAFE_CONVERT_ITOI(LongPtrToShort, LONG_PTR, SHORT)
-DEFINE_SAFE_CONVERT_ITOI(LongPtrToInt, LONG_PTR, INT)
-DEFINE_SAFE_CONVERT_ITOI(LongPtrToLong, LONG_PTR, LONG)
-DEFINE_SAFE_CONVERT_ITOI(LongPtrToIntPtr, LONG_PTR, INT_PTR)
-DEFINE_SAFE_CONVERT_ITOI(LongLongToLong, LONGLONG, LONG)
-DEFINE_SAFE_CONVERT_ITOI(LongLongToIntPtr, LONGLONG, INT_PTR)
-DEFINE_SAFE_CONVERT_ITOI(LongLongToLongPtr, LONGLONG, LONG_PTR)
+DEFINE_SAFE_CONVERT_STOS(ShortToInt8, SHORT, INT8)
+DEFINE_SAFE_CONVERT_STOS(IntToInt8, INT, INT8)
+DEFINE_SAFE_CONVERT_STOS(IntToShort, INT, SHORT)
+DEFINE_SAFE_CONVERT_STOS(LongToInt8, LONG, INT8)
+DEFINE_SAFE_CONVERT_STOS(LongToShort, LONG, SHORT)
+DEFINE_SAFE_CONVERT_STOS(LongToInt, LONG, INT)
+DEFINE_SAFE_CONVERT_STOS(IntPtrToInt8, INT_PTR, INT8)
+DEFINE_SAFE_CONVERT_STOS(IntPtrToShort, INT_PTR, SHORT)
+DEFINE_SAFE_CONVERT_STOS(IntPtrToInt, INT_PTR, INT)
+DEFINE_SAFE_CONVERT_STOS(IntPtrToLong, INT_PTR, LONG)
+DEFINE_SAFE_CONVERT_STOS(IntPtrToLongPtr, INT_PTR, LONG_PTR)
+DEFINE_SAFE_CONVERT_STOS(LongPtrToInt8, LONG_PTR, INT8)
+DEFINE_SAFE_CONVERT_STOS(LongPtrToShort, LONG_PTR, SHORT)
+DEFINE_SAFE_CONVERT_STOS(LongPtrToInt, LONG_PTR, INT)
+DEFINE_SAFE_CONVERT_STOS(LongPtrToLong, LONG_PTR, LONG)
+DEFINE_SAFE_CONVERT_STOS(LongPtrToIntPtr, LONG_PTR, INT_PTR)
+DEFINE_SAFE_CONVERT_STOS(LongLongToLong, LONGLONG, LONG)
+DEFINE_SAFE_CONVERT_STOS(LongLongToIntPtr, LONGLONG, INT_PTR)
+DEFINE_SAFE_CONVERT_STOS(LongLongToLongPtr, LONGLONG, LONG_PTR)
#ifndef _CHAR_UNSIGNED
-DEFINE_SAFE_CONVERT_ITOI(ShortToChar, SHORT, CHAR)
-DEFINE_SAFE_CONVERT_ITOI(LongPtrToChar, LONG_PTR, CHAR)
+DEFINE_SAFE_CONVERT_STOS(ShortToChar, SHORT, CHAR)
+DEFINE_SAFE_CONVERT_STOS(LongPtrToChar, LONG_PTR, CHAR)
#endif
@@ -466,6 +470,7 @@
#define RtlInt8ToSIZET RtlInt8ToULongPtr
#define RtlIntToSizeT RtlIntToUIntPtr
#define RtlIntToSIZET RtlIntToULongPtr
+#define RtlULongToSSIZET RtlULongToLongPtr
#define RtlULongToByte RtlULongToUInt8
#define RtlULongLongToInt64 RtlULongLongToLongLong
#define RtlULongLongToLong64 RtlULongLongToLongLong
@@ -502,6 +507,7 @@
#define Int8ToSIZET Int8ToULongPtr
#define IntToSizeT IntToUIntPtr
#define IntToSIZET IntToULongPtr
+#define ULongToSSIZET ULongToLongPtr
#define ULongToByte ULongToUInt8
#define ULongLongToInt64 ULongLongToLongLong
#define ULongLongToLong64 ULongLongToLongLong
@@ -657,7 +663,7 @@
+ M1Hi * M2Hi * 0x100000000 * 0x100000000
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
is larger than 32 bits.
c) The addition of the product from b) shifted left by 32 and
@@ -711,6 +717,21 @@
DEFINE_SAFE_MULT_U32(SizeTMult, size_t, ULongLongToSizeT)
DEFINE_SAFE_MULT_U32(SIZETMult, SIZE_T, ULongLongToSIZET)
#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_
@@ -731,6 +752,8 @@
#define RtlULong64Sub RtlULongLongSub
#define RtlDWord64Sub RtlULongLongSub
#define RtlUInt64Sub RtlULongLongSub
+#define RtlUInt16Mult RtlUShortMult
+#define RtlWordMult RtlUShortMult
#ifdef _WIN64
#define RtlIntPtrSub RtlLongLongSub
#define RtlLongPtrSub RtlLongLongSub
@@ -757,6 +780,8 @@
#define ULong64Sub ULongLongSub
#define DWord64Sub ULongLongSub
#define UInt64Sub ULongLongSub
+#define UInt16Mult UShortMult
+#define WordMult UShortMult
#ifdef _WIN64
#define IntPtrSub LongLongSub
#define LongPtrSub LongLongSub