[INTSAFE.H]
[reactos.git] / reactos / include / psdk / intsafe.h
1 /*!
2 * \file intsafe.h
3 *
4 * \brief Windows helper functions for integer overflow prevention
5 *
6 * \package This file is part of the ReactOS PSDK package.
7 *
8 * \author
9 * Timo Kreuzer (timo.kreuzer@reactos.org)
10 *
11 * \copyright THIS SOFTWARE IS NOT COPYRIGHTED
12 *
13 * This source code is offered for use in the public domain. You may
14 * use, modify or distribute it freely.
15 *
16 * This code is distributed in the hope that it will be useful but
17 * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
18 * DISCLAIMED. This includes but is not limited to warranties of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * \todo
22 * - missing conversion functions
23 * - multiplication functions
24 * - signed add, sub and multiply functions
25 */
26 #pragma once
27
28 #ifndef _INTSAFE_H_INCLUDED_
29 #define _INTSAFE_H_INCLUDED_
30
31 #include <specstrings.h>
32
33 #if defined(__GNUC__) && !defined(__forceinline)
34 # if ( __MINGW_GNUC_PREREQ(4, 3) && __STDC_VERSION__ >= 199901L)
35 # define __forceinline extern inline __attribute__((__always_inline__,__gnu_inline__))
36 # else
37 # define __forceinline extern __inline__ __attribute__((__always_inline__))
38 # endif
39 #endif
40
41 /* Handle ntintsafe here too */
42 #ifdef _NTINTSAFE_H_INCLUDED_
43 #ifndef _NTDEF_ /* Guard agains redefinition from ntstatus.h */
44 typedef _Return_type_success_(return >= 0) long NTSTATUS;
45 #endif
46 #define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
47 #define STATUS_SUCCESS ((NTSTATUS)0x00000000)
48 #define STATUS_INTEGER_OVERFLOW ((NTSTATUS)0xC0000095)
49 #define INTSAFE_RESULT NTSTATUS
50 #define INTSAFE_SUCCESS STATUS_SUCCESS
51 #define INTSAFE_E_ARITHMETIC_OVERFLOW STATUS_INTEGER_OVERFLOW
52 #define INTSAFE_NAME(name) Rtl##name
53 #else // _NTINTSAFE_H_INCLUDED_
54 #ifndef _HRESULT_DEFINED
55 typedef _Return_type_success_(return >= 0) long HRESULT;
56 #endif
57 #define SUCCEEDED(hr) (((HRESULT)(hr)) >= 0)
58 #define FAILED(hr) (((HRESULT)(hr)) < 0)
59 #define S_OK ((HRESULT)0L)
60 #define INTSAFE_RESULT HRESULT
61 #define INTSAFE_SUCCESS S_OK
62 #define INTSAFE_E_ARITHMETIC_OVERFLOW ((HRESULT)0x80070216L)
63 #define INTSAFE_NAME(name) name
64 #endif // _NTINTSAFE_H_INCLUDED_
65
66 #if !defined(_W64)
67 #if defined(_MSC_VER) && !defined(__midl) && (defined(_M_IX86) || defined(_M_ARM))
68 #define _W64 __w64
69 #else
70 #define _W64
71 #endif
72 #endif
73
74 /* Static assert */
75 #ifndef C_ASSERT
76 #ifdef _MSC_VER
77 # define C_ASSERT(e) typedef char __C_ASSERT__[(e)?1:-1]
78 #else
79 # define C_ASSERT(e) extern void __C_ASSERT__(int [(e)?1:-1])
80 #endif
81 #endif /* C_ASSERT */
82
83 /* Typedefs */
84 #ifndef _WINNT_
85 #ifndef _NTDEF_
86 typedef char CHAR;
87 typedef unsigned char UCHAR, UINT8;
88 typedef signed char INT8;
89 typedef short SHORT;
90 typedef signed short INT16;
91 typedef unsigned short USHORT, UINT16;
92 typedef int INT;
93 typedef unsigned int UINT32;
94 typedef signed int INT32;
95 typedef long LONG;
96 typedef unsigned long ULONG;
97 typedef long long LONGLONG, LONG64;
98 typedef signed long long INT64;
99 typedef unsigned long long ULONGLONG, DWORDLONG, ULONG64, DWORD64, UINT64;
100 #ifdef _WIN64
101 typedef long long INT_PTR, LONG_PTR, SSIZE_T, ptrdiff_t;
102 typedef unsigned long long UINT_PTR, ULONG_PTR, DWORD_PTR, SIZE_T, size_t;
103 #else // _WIN64
104 typedef _W64 int INT_PTR, ptrdiff_t;
105 typedef _W64 unsigned int UINT_PTR, size_t;
106 typedef _W64 long LONG_PTR, SSIZE_T;
107 typedef _W64 unsigned long ULONG_PTR, DWORD_PTR, SIZE_T;
108 #endif // _WIN64
109 #endif
110 typedef unsigned char BYTE;
111 typedef unsigned short WORD;
112 typedef unsigned int UINT;
113 typedef unsigned long DWORD;
114 #endif // _WINNT_
115
116 /* Just to be sure! */
117 C_ASSERT(sizeof(USHORT) == 2);
118 C_ASSERT(sizeof(INT) == 4);
119 C_ASSERT(sizeof(UINT) == 4);
120 C_ASSERT(sizeof(LONG) == 4);
121 C_ASSERT(sizeof(ULONG) == 4);
122 C_ASSERT(sizeof(DWORD) == 4);
123 C_ASSERT(sizeof(UINT_PTR) == sizeof(ULONG_PTR));
124
125 /* Undefine these to avoid conflicts with limits.h */
126 #undef CHAR_MIN
127 #undef CHAR_MAX
128 #undef INT_MIN
129 #undef INT_MAX
130 #undef LONG_MIN
131 #undef LONG_MAX
132 #undef UCHAR_MAX
133 #undef UINT_MAX
134 #undef ULONG_MAX
135
136 /* Integer range margins (use (x-1) to prevent warnings) */
137 #define INT8_MIN (-127 - 1)
138 #define SHORT_MIN (-32767 - 1)
139 #define INT16_MIN (-32767 - 1)
140 #define INT_MIN (-2147483647 - 1)
141 #define INT32_MIN (-2147483647 - 1)
142 #define LONG_MIN (-2147483647L - 1)
143 #define LONGLONG_MIN (-9223372036854775807LL - 1)
144 #define LONG64_MIN (-9223372036854775807LL - 1)
145 #define INT64_MIN (-9223372036854775807LL - 1)
146 //#define INT128_MIN (-170141183460469231731687303715884105728)
147 #ifdef _WIN64
148 #define INT_PTR_MIN INT64_MIN
149 #define LONG_PTR_MIN LONG64_MIN
150 #define PTRDIFF_T_MIN INT64_MIN
151 #define SSIZE_T_MIN INT64_MIN
152 #else // _WIN64
153 #define INT_PTR_MIN INT_MIN
154 #define LONG_PTR_MIN LONG_MIN
155 #define PTRDIFF_T_MIN INT_MIN
156 #define SSIZE_T_MIN INT_MIN
157 #endif // _WIN64
158
159 #define INT8_MAX 127
160 #define UINT8_MAX 0xff
161 #define UCHAR_MAX 0xff
162 #define BYTE_MAX 0xff
163 #define SHORT_MAX 32767
164 #define INT16_MAX 32767
165 #define USHORT_MAX 0xffff
166 #define UINT16_MAX 0xffff
167 #define WORD_MAX 0xffff
168 #define INT_MAX 2147483647
169 #define INT32_MAX 2147483647
170 #define UINT_MAX 0xffffffff
171 #define UINT32_MAX 0xffffffff
172 #define LONG_MAX 2147483647L
173 #define ULONG_MAX 0xffffffffUL
174 #define DWORD_MAX 0xffffffffUL
175 #define LONGLONG_MAX 9223372036854775807LL
176 #define LONG64_MAX 9223372036854775807LL
177 #define INT64_MAX 9223372036854775807LL
178 #define ULONGLONG_MAX 0xffffffffffffffffULL
179 #define DWORDLONG_MAX 0xffffffffffffffffULL
180 #define ULONG64_MAX 0xffffffffffffffffULL
181 #define DWORD64_MAX 0xffffffffffffffffULL
182 #define UINT64_MAX 0xffffffffffffffffULL
183 #define INT128_MAX 170141183460469231731687303715884105727
184 #define UINT128_MAX 0xffffffffffffffffffffffffffffffff
185 #undef SIZE_T_MAX
186 #ifdef _WIN64
187 #define INT_PTR_MAX INT64_MAX
188 #define UINT_PTR_MAX UINT64_MAX
189 #define LONG_PTR_MAX LONG64_MAX
190 #define ULONG_PTR_MAX ULONG64_MAX
191 #define DWORD_PTR_MAX DWORD64_MAX
192 #define PTRDIFF_T_MAX INT64_MAX
193 #define SIZE_T_MAX UINT64_MAX
194 #define SSIZE_T_MAX INT64_MAX
195 #define _SIZE_T_MAX UINT64_MAX
196 #else // _WIN64
197 #define INT_PTR_MAX INT_MAX
198 #define UINT_PTR_MAX UINT_MAX
199 #define LONG_PTR_MAX LONG_MAX
200 #define ULONG_PTR_MAX ULONG_MAX
201 #define DWORD_PTR_MAX DWORD_MAX
202 #define PTRDIFF_T_MAX INT_MAX
203 #define SIZE_T_MAX UINT_MAX
204 #define SSIZE_T_MAX INT_MAX
205 #define _SIZE_T_MAX UINT_MAX
206 #endif // _WIN64
207
208 #ifndef CHAR_MIN
209 #ifdef _CHAR_UNSIGNED
210 #define CHAR_MIN 0
211 #define CHAR_MAX 0xff
212 #else
213 #define CHAR_MIN (-128)
214 #define CHAR_MAX 127
215 #endif
216 #endif
217
218 /* Error values */
219 #define INT8_ERROR (-1)
220 #define UINT8_ERROR 0xff
221 #define BYTE_ERROR 0xff
222 #define SHORT_ERROR (-1)
223 #define INT16_ERROR (-1)
224 #define USHORT_ERROR 0xffff
225 #define UINT16_ERROR 0xffff
226 #define WORD_ERROR 0xffff
227 #define INT_ERROR (-1)
228 #define INT32_ERROR (-1)
229 #define UINT_ERROR 0xffffffff
230 #define UINT32_ERROR 0xffffffff
231 #define LONG_ERROR (-1L)
232 #define ULONG_ERROR 0xffffffffUL
233 #define DWORD_ERROR 0xffffffffUL
234 #define LONGLONG_ERROR (-1LL)
235 #define LONG64_ERROR (-1LL)
236 #define INT64_ERROR (-1LL)
237 #define ULONGLONG_ERROR 0xffffffffffffffffULL
238 #define DWORDLONG_ERROR 0xffffffffffffffffULL
239 #define ULONG64_ERROR 0xffffffffffffffffULL
240 #define UINT64_ERROR 0xffffffffffffffffULL
241 #ifdef _WIN64
242 #define INT_PTR_ERROR (-1LL)
243 #define UINT_PTR_ERROR 0xffffffffffffffffULL
244 #define LONG_PTR_ERROR (-1LL)
245 #define ULONG_PTR_ERROR 0xffffffffffffffffULL
246 #define DWORD_PTR_ERROR 0xffffffffffffffffULL
247 #define PTRDIFF_T_ERROR (-1LL)
248 #define SIZE_T_ERROR 0xffffffffffffffffULL
249 #define SSIZE_T_ERROR (-1LL)
250 #define _SIZE_T_ERROR 0xffffffffffffffffULL
251 #else // _WIN64
252 #define INT_PTR_ERROR (-1)
253 #define UINT_PTR_ERROR 0xffffffff
254 #define LONG_PTR_ERROR (-1L)
255 #define ULONG_PTR_ERROR 0xffffffffUL
256 #define DWORD_PTR_ERROR 0xffffffffUL
257 #define PTRDIFF_T_ERROR (-1)
258 #define SIZE_T_ERROR 0xffffffff
259 #define SSIZE_T_ERROR (-1L)
260 #define _SIZE_T_ERROR 0xffffffffUL
261 #endif // _WIN64
262
263 #define size_t_ERROR SIZE_T_ERROR
264 #define UCHAR_ERROR '\0'
265 #define CHAR_ERROR '\0'
266
267
268 /* 32 bit x 32 bit to 64 bit unsigned multiplication */
269 #ifndef UInt32x32To64
270 #define UInt32x32To64(a,b) ((DWORDLONG)(a)*(DWORDLONG)(b))
271 #endif
272
273
274 /* Convert unsigned to signed or unsigned */
275 #define DEFINE_SAFE_CONVERT_UTOX(_Name, _TypeFrom, _TypeTo) \
276 _Must_inspect_result_ \
277 __forceinline \
278 INTSAFE_RESULT \
279 INTSAFE_NAME(_Name)( \
280 _In_ _TypeFrom Input, \
281 _Out_ _Deref_out_range_(==, Input) _TypeTo *pOutput) \
282 { \
283 if (Input <= _TypeTo ## _MAX) \
284 { \
285 *pOutput = (_TypeTo)Input; \
286 return INTSAFE_SUCCESS; \
287 } \
288 else \
289 { \
290 *pOutput = _TypeTo ## _ERROR; \
291 return INTSAFE_E_ARITHMETIC_OVERFLOW; \
292 } \
293 }
294
295 DEFINE_SAFE_CONVERT_UTOX(ByteToChar, BYTE, CHAR)
296 DEFINE_SAFE_CONVERT_UTOX(ByteToInt8, BYTE, INT8)
297 DEFINE_SAFE_CONVERT_UTOX(UInt8ToChar, UINT8, CHAR)
298 DEFINE_SAFE_CONVERT_UTOX(UInt8ToInt8, UINT8, INT8)
299 DEFINE_SAFE_CONVERT_UTOX(UShortToChar, USHORT, CHAR)
300 DEFINE_SAFE_CONVERT_UTOX(UShortToUChar, USHORT, UCHAR)
301 DEFINE_SAFE_CONVERT_UTOX(UShortToInt8, USHORT, INT8)
302 DEFINE_SAFE_CONVERT_UTOX(UShortToUInt8, USHORT, UINT8)
303 DEFINE_SAFE_CONVERT_UTOX(UShortToShort, USHORT, SHORT)
304 DEFINE_SAFE_CONVERT_UTOX(UIntToUChar, UINT, UCHAR)
305 DEFINE_SAFE_CONVERT_UTOX(UIntToInt8, UINT, INT8)
306 DEFINE_SAFE_CONVERT_UTOX(UIntToUInt8, UINT, UINT8)
307 DEFINE_SAFE_CONVERT_UTOX(UIntToShort, UINT, SHORT)
308 DEFINE_SAFE_CONVERT_UTOX(UIntToUShort, UINT, USHORT)
309 DEFINE_SAFE_CONVERT_UTOX(UIntToInt, UINT, INT)
310 DEFINE_SAFE_CONVERT_UTOX(UIntToLong, UINT, LONG)
311 DEFINE_SAFE_CONVERT_UTOX(UIntPtrToUChar, UINT_PTR, UCHAR)
312 DEFINE_SAFE_CONVERT_UTOX(UIntPtrToInt8, UINT_PTR, INT8)
313 DEFINE_SAFE_CONVERT_UTOX(UIntPtrToUInt8, UINT_PTR, UINT8)
314 DEFINE_SAFE_CONVERT_UTOX(UIntPtrToShort, UINT_PTR, SHORT)
315 DEFINE_SAFE_CONVERT_UTOX(UIntPtrToUShort, UINT_PTR, USHORT)
316 DEFINE_SAFE_CONVERT_UTOX(UIntPtrToInt16, UINT_PTR, INT16)
317 DEFINE_SAFE_CONVERT_UTOX(UIntPtrToUInt16, UINT_PTR, UINT16)
318 DEFINE_SAFE_CONVERT_UTOX(UIntPtrToInt, UINT_PTR, INT)
319 DEFINE_SAFE_CONVERT_UTOX(UIntPtrToLong, UINT_PTR, LONG)
320 DEFINE_SAFE_CONVERT_UTOX(UIntPtrToIntPtr, UINT_PTR, INT_PTR)
321 DEFINE_SAFE_CONVERT_UTOX(UIntPtrToLongPtr, UINT_PTR, LONG_PTR)
322 DEFINE_SAFE_CONVERT_UTOX(ULongToUChar, ULONG, UCHAR)
323 DEFINE_SAFE_CONVERT_UTOX(ULongToUInt8, ULONG, UINT8)
324 DEFINE_SAFE_CONVERT_UTOX(ULongToShort, ULONG, SHORT)
325 DEFINE_SAFE_CONVERT_UTOX(ULongToUShort, ULONG, USHORT)
326 DEFINE_SAFE_CONVERT_UTOX(ULongToInt, ULONG, INT)
327 DEFINE_SAFE_CONVERT_UTOX(ULongToUInt, ULONG, UINT)
328 DEFINE_SAFE_CONVERT_UTOX(ULongToIntPtr, ULONG, INT_PTR)
329 DEFINE_SAFE_CONVERT_UTOX(ULongToUIntPtr, ULONG, UINT_PTR)
330 DEFINE_SAFE_CONVERT_UTOX(ULongToLongPtr, ULONG, LONG_PTR)
331 DEFINE_SAFE_CONVERT_UTOX(ULongPtrToULong, ULONG_PTR, ULONGLONG)
332 DEFINE_SAFE_CONVERT_UTOX(ULongLongToUInt, ULONGLONG, UINT)
333 DEFINE_SAFE_CONVERT_UTOX(ULongLongToULong, ULONGLONG, ULONG)
334 DEFINE_SAFE_CONVERT_UTOX(ULongLongToULongPtr, ULONGLONG, ULONG_PTR)
335
336
337 /* Convert signed to unsigned */
338 #define DEFINE_SAFE_CONVERT_STOU(_Name, _TypeFrom, _TypeTo) \
339 _Must_inspect_result_ \
340 __forceinline \
341 INTSAFE_RESULT \
342 INTSAFE_NAME(_Name)( \
343 _In_ _TypeFrom Input, \
344 _Out_ _Deref_out_range_(==, Input) _TypeTo *pOutput) \
345 { \
346 if ((Input >= 0) && ((_TypeTo)Input <= _TypeTo ## _MAX)) \
347 { \
348 *pOutput = (_TypeTo)Input; \
349 return INTSAFE_SUCCESS; \
350 } \
351 else \
352 { \
353 *pOutput = _TypeTo ## _ERROR; \
354 return INTSAFE_E_ARITHMETIC_OVERFLOW; \
355 } \
356 }
357
358 DEFINE_SAFE_CONVERT_STOU(Int8ToUChar, INT8, UCHAR)
359 DEFINE_SAFE_CONVERT_STOU(Int8ToUInt8, INT8, UINT8)
360 DEFINE_SAFE_CONVERT_STOU(Int8ToUShort, INT8, USHORT)
361 DEFINE_SAFE_CONVERT_STOU(Int8ToUInt, INT8, UINT)
362 DEFINE_SAFE_CONVERT_STOU(Int8ToULong, INT8, ULONG)
363 DEFINE_SAFE_CONVERT_STOU(Int8ToUIntPtr, INT8, UINT_PTR)
364 DEFINE_SAFE_CONVERT_STOU(Int8ToULongPtr, INT8, ULONG_PTR)
365 DEFINE_SAFE_CONVERT_STOU(Int8ToULongLong, INT8, ULONGLONG)
366 DEFINE_SAFE_CONVERT_STOU(ShortToUChar, SHORT, UCHAR)
367 DEFINE_SAFE_CONVERT_STOU(ShortToUInt8, SHORT, UINT8)
368 DEFINE_SAFE_CONVERT_STOU(ShortToUShort, SHORT, USHORT)
369 DEFINE_SAFE_CONVERT_STOU(ShortToUInt, SHORT, UINT)
370 DEFINE_SAFE_CONVERT_STOU(ShortToULong, SHORT, ULONG)
371 DEFINE_SAFE_CONVERT_STOU(ShortToUIntPtr, SHORT, UINT_PTR)
372 DEFINE_SAFE_CONVERT_STOU(ShortToULongPtr, SHORT, ULONG_PTR)
373 DEFINE_SAFE_CONVERT_STOU(ShortToDWordPtr, SHORT, DWORD_PTR)
374 DEFINE_SAFE_CONVERT_STOU(ShortToULongLong, SHORT, ULONGLONG)
375 DEFINE_SAFE_CONVERT_STOU(IntToUChar, INT, UCHAR)
376 DEFINE_SAFE_CONVERT_STOU(IntToUInt8, INT, UINT8)
377 DEFINE_SAFE_CONVERT_STOU(IntToUShort, INT, USHORT)
378 DEFINE_SAFE_CONVERT_STOU(IntToUInt, INT, UINT)
379 DEFINE_SAFE_CONVERT_STOU(IntToULong, INT, ULONG)
380 DEFINE_SAFE_CONVERT_STOU(IntToULongLong, INT, ULONGLONG)
381 DEFINE_SAFE_CONVERT_STOU(LongToUChar, LONG, UCHAR)
382 DEFINE_SAFE_CONVERT_STOU(LongToUInt8, LONG, UINT8)
383 DEFINE_SAFE_CONVERT_STOU(LongToUShort, LONG, USHORT)
384 DEFINE_SAFE_CONVERT_STOU(LongToUInt, LONG, UINT)
385 DEFINE_SAFE_CONVERT_STOU(LongToULong, LONG, ULONG)
386 DEFINE_SAFE_CONVERT_STOU(LongToUIntPtr, LONG, UINT_PTR)
387 DEFINE_SAFE_CONVERT_STOU(LongToULongPtr, LONG, ULONG_PTR)
388 DEFINE_SAFE_CONVERT_STOU(LongToULongLong, LONG, ULONGLONG)
389 DEFINE_SAFE_CONVERT_STOU(IntPtrToUChar, INT_PTR, UCHAR)
390 DEFINE_SAFE_CONVERT_STOU(IntPtrToUInt8, INT_PTR, UINT8)
391 DEFINE_SAFE_CONVERT_STOU(IntPtrToUShort, INT_PTR, USHORT)
392 DEFINE_SAFE_CONVERT_STOU(IntPtrToUInt, INT_PTR, UINT)
393 DEFINE_SAFE_CONVERT_STOU(IntPtrToULong, INT_PTR, ULONG)
394 DEFINE_SAFE_CONVERT_STOU(IntPtrToUIntPtr, INT_PTR, UINT_PTR)
395 DEFINE_SAFE_CONVERT_STOU(IntPtrToULongPtr, INT_PTR, ULONG_PTR)
396 DEFINE_SAFE_CONVERT_STOU(IntPtrToULongLong, INT_PTR, ULONGLONG)
397 DEFINE_SAFE_CONVERT_STOU(LongPtrToUChar, LONG_PTR, UCHAR)
398 DEFINE_SAFE_CONVERT_STOU(LongPtrToUInt8, LONG_PTR, UINT8)
399 DEFINE_SAFE_CONVERT_STOU(LongPtrToUShort, LONG_PTR, USHORT)
400 DEFINE_SAFE_CONVERT_STOU(LongPtrToUInt, LONG_PTR, UINT)
401 DEFINE_SAFE_CONVERT_STOU(LongPtrToULong, LONG_PTR, ULONG)
402 DEFINE_SAFE_CONVERT_STOU(LongPtrToUIntPtr, LONG_PTR, UINT_PTR)
403 DEFINE_SAFE_CONVERT_STOU(LongPtrToULongPtr, LONG_PTR, ULONG_PTR)
404 DEFINE_SAFE_CONVERT_STOU(LongPtrToULongLong, LONG_PTR, ULONGLONG)
405 #ifdef _CHAR_UNSIGNED
406 DEFINE_SAFE_CONVERT_STOU(ShortToChar, SHORT, UCHAR)
407 DEFINE_SAFE_CONVERT_STOU(LongPtrToChar, LONG_PTR, UCHAR)
408 #endif
409
410
411 /* Convert signed to signed */
412 #define DEFINE_SAFE_CONVERT_STOS(_Name, _TypeFrom, _TypeTo) \
413 _Must_inspect_result_ \
414 __forceinline \
415 INTSAFE_RESULT \
416 INTSAFE_NAME(_Name)( \
417 _In_ _TypeFrom Input, \
418 _Out_ _Deref_out_range_(==, Input) _TypeTo *pOutput) \
419 { \
420 if ((Input >= _TypeTo ## _MIN) && (Input <= _TypeTo ## _MAX)) \
421 { \
422 *pOutput = (_TypeTo)Input; \
423 return INTSAFE_SUCCESS; \
424 } \
425 else \
426 { \
427 *pOutput = _TypeTo ## _ERROR; \
428 return INTSAFE_E_ARITHMETIC_OVERFLOW; \
429 } \
430 }
431
432 DEFINE_SAFE_CONVERT_STOS(ShortToInt8, SHORT, INT8)
433 DEFINE_SAFE_CONVERT_STOS(IntToInt8, INT, INT8)
434 DEFINE_SAFE_CONVERT_STOS(IntToShort, INT, SHORT)
435 DEFINE_SAFE_CONVERT_STOS(LongToInt8, LONG, INT8)
436 DEFINE_SAFE_CONVERT_STOS(LongToShort, LONG, SHORT)
437 DEFINE_SAFE_CONVERT_STOS(LongToInt, LONG, INT)
438 DEFINE_SAFE_CONVERT_STOS(IntPtrToInt8, INT_PTR, INT8)
439 DEFINE_SAFE_CONVERT_STOS(IntPtrToShort, INT_PTR, SHORT)
440 DEFINE_SAFE_CONVERT_STOS(IntPtrToInt, INT_PTR, INT)
441 DEFINE_SAFE_CONVERT_STOS(IntPtrToLong, INT_PTR, LONG)
442 DEFINE_SAFE_CONVERT_STOS(IntPtrToLongPtr, INT_PTR, LONG_PTR)
443 DEFINE_SAFE_CONVERT_STOS(LongPtrToInt8, LONG_PTR, INT8)
444 DEFINE_SAFE_CONVERT_STOS(LongPtrToShort, LONG_PTR, SHORT)
445 DEFINE_SAFE_CONVERT_STOS(LongPtrToInt, LONG_PTR, INT)
446 DEFINE_SAFE_CONVERT_STOS(LongPtrToLong, LONG_PTR, LONG)
447 DEFINE_SAFE_CONVERT_STOS(LongPtrToIntPtr, LONG_PTR, INT_PTR)
448 DEFINE_SAFE_CONVERT_STOS(LongLongToLong, LONGLONG, LONG)
449 DEFINE_SAFE_CONVERT_STOS(LongLongToIntPtr, LONGLONG, INT_PTR)
450 DEFINE_SAFE_CONVERT_STOS(LongLongToLongPtr, LONGLONG, LONG_PTR)
451 #ifndef _CHAR_UNSIGNED
452 DEFINE_SAFE_CONVERT_STOS(ShortToChar, SHORT, CHAR)
453 DEFINE_SAFE_CONVERT_STOS(LongPtrToChar, LONG_PTR, CHAR)
454 #endif
455
456
457 #ifdef _NTINTSAFE_H_INCLUDED_
458
459 #define RtlInt8ToByte RtlInt8ToUInt8
460 #define RtlInt8ToUInt16 RtlInt8ToUShort
461 #define RtlInt8ToWord RtlInt8ToUShort
462 #define RtlInt8ToUInt32 RtlInt8ToUInt
463 #define RtlInt8ToDWord RtlInt8ToULong
464 #define RtlInt8ToDWordPtr RtlInt8ToULongPtr
465 #define RtlInt8ToDWordLong RtlInt8ToULongLong
466 #define RtlInt8ToULong64 RtlInt8ToULongLong
467 #define RtlInt8ToDWord64 RtlInt8ToULongLong
468 #define RtlInt8ToUInt64 RtlInt8ToULongLong
469 #define RtlInt8ToSizeT RtlInt8ToUIntPtr
470 #define RtlInt8ToSIZET RtlInt8ToULongPtr
471 #define RtlIntToSizeT RtlIntToUIntPtr
472 #define RtlIntToSIZET RtlIntToULongPtr
473 #define RtlULongToSSIZET RtlULongToLongPtr
474 #define RtlULongToByte RtlULongToUInt8
475 #define RtlULongLongToInt64 RtlULongLongToLongLong
476 #define RtlULongLongToLong64 RtlULongLongToLongLong
477 #define RtlULongLongToPtrdiffT RtlULongLongToIntPtr
478 #define RtlULongLongToSizeT RtlULongLongToUIntPtr
479 #define RtlULongLongToSSIZET RtlULongLongToLongPtr
480 #define RtlULongLongToSIZET RtlULongLongToULongPtr
481 #define RtlSIZETToULong RtlULongPtrToULong
482 #define RtlSSIZETToULongLong RtlLongPtrToULongLong
483 #define RtlSSIZETToULong RtlLongPtrToULong
484 #ifdef _WIN64
485 #define RtlIntToUIntPtr RtlIntToULongLong
486 #define RtlULongLongToIntPtr RtlULongLongToLongLong
487 #else
488 #define RtlIntToUIntPtr RtlIntToUInt
489 #define RtlULongLongToIntPtr RtlULongLongToInt
490 #define RtlULongLongToUIntPtr RtlULongLongToUInt
491 #define RtlULongLongToULongPtr RtlULongLongToULong
492 #endif
493
494 #else // _NTINTSAFE_H_INCLUDED_
495
496 #define Int8ToByte Int8ToUInt8
497 #define Int8ToUInt16 Int8ToUShort
498 #define Int8ToWord Int8ToUShort
499 #define Int8ToUInt32 Int8ToUInt
500 #define Int8ToDWord Int8ToULong
501 #define Int8ToDWordPtr Int8ToULongPtr
502 #define Int8ToDWordLong Int8ToULongLong
503 #define Int8ToULong64 Int8ToULongLong
504 #define Int8ToDWord64 Int8ToULongLong
505 #define Int8ToUInt64 Int8ToULongLong
506 #define Int8ToSizeT Int8ToUIntPtr
507 #define Int8ToSIZET Int8ToULongPtr
508 #define IntToSizeT IntToUIntPtr
509 #define IntToSIZET IntToULongPtr
510 #define ULongToSSIZET ULongToLongPtr
511 #define ULongToByte ULongToUInt8
512 #define ULongLongToInt64 ULongLongToLongLong
513 #define ULongLongToLong64 ULongLongToLongLong
514 #define ULongLongToPtrdiffT ULongLongToIntPtr
515 #define ULongLongToSizeT ULongLongToUIntPtr
516 #define ULongLongToSSIZET ULongLongToLongPtr
517 #define ULongLongToSIZET ULongLongToULongPtr
518 #define SIZETToULong ULongPtrToULong
519 #define SSIZETToULongLong LongPtrToULongLong
520 #define SSIZETToULong LongPtrToULong
521 #ifdef _WIN64
522 #define IntToUIntPtr IntToULongLong
523 #define ULongLongToIntPtr ULongLongToLongLong
524 #else
525 #define IntToUIntPtr IntToUInt
526 #define ULongLongToIntPtr ULongLongToInt
527 #define ULongLongToUIntPtr ULongLongToUInt
528 #define ULongLongToULongPtr ULongLongToULong
529 #endif
530
531 #endif // _NTINTSAFE_H_INCLUDED_
532
533
534 #define DEFINE_SAFE_ADD(_Name, _Type) \
535 _Must_inspect_result_ \
536 __forceinline \
537 INTSAFE_RESULT \
538 INTSAFE_NAME(_Name)( \
539 _In_ _Type Augend, \
540 _In_ _Type Addend, \
541 _Out_ _Deref_out_range_(==, Augend + Addend) _Type *pOutput) \
542 { \
543 if ((Augend + Addend) >= Augend) \
544 { \
545 *pOutput = Augend + Addend; \
546 return INTSAFE_SUCCESS; \
547 } \
548 else \
549 { \
550 *pOutput = _Type ## _ERROR; \
551 return INTSAFE_E_ARITHMETIC_OVERFLOW; \
552 } \
553 }
554
555 DEFINE_SAFE_ADD(UInt8Add, UINT8)
556 DEFINE_SAFE_ADD(UShortAdd, USHORT)
557 DEFINE_SAFE_ADD(UIntAdd, UINT)
558 DEFINE_SAFE_ADD(ULongAdd, ULONG)
559 DEFINE_SAFE_ADD(UIntPtrAdd, UINT_PTR)
560 DEFINE_SAFE_ADD(ULongPtrAdd, ULONG_PTR)
561 DEFINE_SAFE_ADD(DWordPtrAdd, DWORD_PTR)
562 DEFINE_SAFE_ADD(SizeTAdd, size_t)
563 DEFINE_SAFE_ADD(SIZETAdd, SIZE_T)
564 DEFINE_SAFE_ADD(ULongLongAdd, ULONGLONG)
565
566
567 #define DEFINE_SAFE_SUB(_Name, _Type) \
568 _Must_inspect_result_ \
569 __forceinline \
570 INTSAFE_RESULT \
571 INTSAFE_NAME(_Name)( \
572 _In_ _Type Minuend, \
573 _In_ _Type Subtrahend, \
574 _Out_ _Deref_out_range_(==, Minuend - Subtrahend) _Type* pOutput) \
575 { \
576 if (Minuend >= Subtrahend) \
577 { \
578 *pOutput = Minuend - Subtrahend; \
579 return INTSAFE_SUCCESS; \
580 } \
581 else \
582 { \
583 *pOutput = _Type ## _ERROR; \
584 return INTSAFE_E_ARITHMETIC_OVERFLOW; \
585 } \
586 }
587
588 DEFINE_SAFE_SUB(UInt8Sub, UINT8)
589 DEFINE_SAFE_SUB(UShortSub, USHORT)
590 DEFINE_SAFE_SUB(UIntSub, UINT)
591 DEFINE_SAFE_SUB(UIntPtrSub, UINT_PTR)
592 DEFINE_SAFE_SUB(ULongSub, ULONG)
593 DEFINE_SAFE_SUB(ULongPtrSub, ULONG_PTR)
594 DEFINE_SAFE_SUB(DWordPtrSub, DWORD_PTR)
595 DEFINE_SAFE_SUB(SizeTSub, size_t)
596 DEFINE_SAFE_SUB(SIZETSub, SIZE_T)
597 DEFINE_SAFE_SUB(ULongLongSub, ULONGLONG)
598
599
600 _Must_inspect_result_
601 __forceinline
602 INTSAFE_RESULT
603 INTSAFE_NAME(LongLongSub)(
604 _In_ LONGLONG Minuend,
605 _In_ LONGLONG Subtrahend,
606 _Out_ _Deref_out_range_(==, Minuend - Subtrahend) LONGLONG* pResult)
607 {
608 LONGLONG Result = Minuend - Subtrahend;
609
610 /* The only way the result can overflow, is when the sign of the minuend
611 and the subtrahend differ. In that case the result is expected to
612 have the same sign as the minuend, otherwise it overflowed.
613 Sign equality is checked with a binary xor operation. */
614 if ( ((Minuend ^ Subtrahend) < 0) && ((Minuend ^ Result) < 0) )
615 {
616 *pResult = LONGLONG_ERROR;
617 return INTSAFE_E_ARITHMETIC_OVERFLOW;
618 }
619 else
620 {
621 *pResult = Result;
622 return INTSAFE_SUCCESS;
623 }
624 }
625
626
627 #define DEFINE_SAFE_SUB_S(_Name, _Type1, _Type2, _Convert) \
628 _Must_inspect_result_ \
629 __forceinline \
630 INTSAFE_RESULT \
631 INTSAFE_NAME(_Name)( \
632 _In_ _Type1 Minuend, \
633 _In_ _Type1 Subtrahend, \
634 _Out_ _Deref_out_range_(==, Minuend - Subtrahend) _Type1* pOutput) \
635 { \
636 return INTSAFE_NAME(_Convert)(((_Type2)Minuend) - ((_Type2)Subtrahend), pOutput); \
637 }
638
639 DEFINE_SAFE_SUB_S(LongSub, LONG, LONGLONG, LongLongToLong)
640 #ifndef _WIN64
641 DEFINE_SAFE_SUB_S(IntPtrSub, INT_PTR, LONGLONG, LongLongToIntPtr)
642 DEFINE_SAFE_SUB_S(LongPtrSub, LONG_PTR, LONGLONG, LongLongToLongPtr)
643 #endif
644
645
646 _Must_inspect_result_
647 __forceinline
648 INTSAFE_RESULT
649 INTSAFE_NAME(ULongLongMult)(
650 _In_ ULONGLONG Multiplicand,
651 _In_ ULONGLONG Multiplier,
652 _Out_ _Deref_out_range_(==, Multiplicand * Multiplier) ULONGLONG* pOutput)
653 {
654 /* We can split the 64 bit numbers in low and high parts:
655 M1 = M1Low + M1Hi * 0x100000000
656 M2 = M2Low + M2Hi * 0x100000000
657
658 Then the multiplication looks like this:
659 M1 * M2 = (M1Low + M1Hi * 0x100000000) + (M2Low + M2Hi * 0x100000000)
660 = M1Low * M2Low
661 + M1Low * M2Hi * 0x100000000
662 + M2Low * M1Hi * 0x100000000
663 + M1Hi * M2Hi * 0x100000000 * 0x100000000
664
665 We get an overflow when
666 a) M1Hi * M2Hi != 0, so when M1Hi and M2Hi are both not 0
667 b) The product of the nonzero high part and the other low part
668 is larger than 32 bits.
669 c) The addition of the product from b) shifted left by 32 and
670 M1Low * M2Low is larger than 64 bits
671 */
672 ULONG M1Low = Multiplicand & 0xffffffff;
673 ULONG M2Low = Multiplier & 0xffffffff;
674 ULONG M1Hi = Multiplicand >> 32;
675 ULONG M2Hi = Multiplier >> 32;
676 ULONGLONG Temp;
677
678 if (M1Hi == 0)
679 {
680 Temp = UInt32x32To64(M1Low, M2Hi);
681 }
682 else if (M2Hi == 0)
683 {
684 Temp = UInt32x32To64(M1Hi, M2Low);
685 }
686 else
687 {
688 *pOutput = LONGLONG_ERROR;
689 return INTSAFE_E_ARITHMETIC_OVERFLOW;
690 }
691
692 if (Temp > ULONG_MAX)
693 {
694 *pOutput = LONGLONG_ERROR;
695 return INTSAFE_E_ARITHMETIC_OVERFLOW;
696 }
697
698 return INTSAFE_NAME(ULongLongAdd)(Temp << 32, UInt32x32To64(M1Low, M2Low), pOutput);
699 }
700
701
702 #define DEFINE_SAFE_MULT_U32(_Name, _Type, _Convert) \
703 _Must_inspect_result_ \
704 __forceinline \
705 INTSAFE_RESULT \
706 INTSAFE_NAME(_Name)( \
707 _In_ _Type Multiplicand, \
708 _In_ _Type Multiplier, \
709 _Out_ _Deref_out_range_(==, Multiplicand * Multiplier) _Type* pOutput) \
710 { \
711 ULONGLONG Result = UInt32x32To64(Multiplicand, Multiplier); \
712 return INTSAFE_NAME(_Convert)(Result, pOutput); \
713 }
714
715 DEFINE_SAFE_MULT_U32(ULongMult, ULONG, ULongLongToULong)
716 #ifndef _WIN64
717 DEFINE_SAFE_MULT_U32(SizeTMult, size_t, ULongLongToSizeT)
718 DEFINE_SAFE_MULT_U32(SIZETMult, SIZE_T, ULongLongToSIZET)
719 #endif
720
721 #define DEFINE_SAFE_MULT_U16(_Name, _Type, _Convert) \
722 __checkReturn \
723 __forceinline \
724 INTSAFE_RESULT \
725 INTSAFE_NAME(_Name)( \
726 _In_ _Type Multiplicand, \
727 _In_ _Type Multiplier, \
728 _Out_ _Deref_out_range_(==, Multiplicand * Multiplier) _Type* pOutput) \
729 { \
730 ULONG Result = ((ULONG)Multiplicand) * ((ULONG)Multiplier); \
731 return INTSAFE_NAME(_Convert)(Result, pOutput); \
732 }
733
734 DEFINE_SAFE_MULT_U16(UShortMult, USHORT, ULongToUShort)
735
736
737 #ifdef _NTINTSAFE_H_INCLUDED_
738
739 #define RtlUInt16Add RtlUShortAdd
740 #define RtlWordAdd RtlUShortAdd
741 #define RtlUInt32Add RtlUIntAdd
742 #define RtlDWordAdd RtlULongAdd
743 #define RtlDWordLongAdd RtlULongLongAdd
744 #define RtlULong64Add RtlULongLongAdd
745 #define RtlDWord64Add RtlULongLongAdd
746 #define RtlUInt64Add RtlULongLongAdd
747 #define RtlUInt16Sub RtlUShortSub
748 #define RtlWordSub RtlUShortSub
749 #define RtlUInt32Sub RtlUIntSub
750 #define RtlDWordSub RtlULongSub
751 #define RtlDWordLongSub RtlULongLongSub
752 #define RtlULong64Sub RtlULongLongSub
753 #define RtlDWord64Sub RtlULongLongSub
754 #define RtlUInt64Sub RtlULongLongSub
755 #define RtlUInt16Mult RtlUShortMult
756 #define RtlWordMult RtlUShortMult
757 #ifdef _WIN64
758 #define RtlIntPtrSub RtlLongLongSub
759 #define RtlLongPtrSub RtlLongLongSub
760 #define RtlSizeTMult RtlULongLongMult
761 #define RtlSIZETMult RtlULongLongMult
762 #else
763 #endif
764
765 #else // _NTINTSAFE_H_INCLUDED_
766
767 #define UInt16Add UShortAdd
768 #define WordAdd UShortAdd
769 #define UInt32Add UIntAdd
770 #define DWordAdd ULongAdd
771 #define DWordLongAdd ULongLongAdd
772 #define ULong64Add ULongLongAdd
773 #define DWord64Add ULongLongAdd
774 #define UInt64Add ULongLongAdd
775 #define UInt16Sub UShortSub
776 #define WordSub UShortSub
777 #define UInt32Sub UIntSub
778 #define DWordSub ULongSub
779 #define DWordLongSub ULongLongSub
780 #define ULong64Sub ULongLongSub
781 #define DWord64Sub ULongLongSub
782 #define UInt64Sub ULongLongSub
783 #define UInt16Mult UShortMult
784 #define WordMult UShortMult
785 #ifdef _WIN64
786 #define IntPtrSub LongLongSub
787 #define LongPtrSub LongLongSub
788 #define SizeTMult ULongLongMult
789 #define SIZETMult ULongLongMult
790 #else
791 #endif
792
793 #endif // _NTINTSAFE_H_INCLUDED_
794
795 #endif // !_INTSAFE_H_INCLUDED_