|
|
|
1 |
|
/****************************************************************** |
2 |
|
* * |
3 |
|
* intsafe.h -- This module defines helper functions to prevent * |
4 |
|
* integer overflow bugs. * |
5 |
|
* * |
6 |
|
* Copyright (c) Microsoft Corp. All rights reserved. * |
7 |
|
* * |
8 |
|
******************************************************************/ |
9 |
|
#ifndef _INTSAFE_H_INCLUDED_ |
10 |
|
#define _INTSAFE_H_INCLUDED_ |
11 |
|
|
12 |
|
#if (_MSC_VER > 1000) |
13 |
|
#pragma once |
14 |
|
#endif |
15 |
|
|
16 |
|
#include <specstrings.h> // for __in, etc. |
17 |
|
|
18 |
|
#if !defined(_W64) |
19 |
|
#if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && (_MSC_VER >= 1300) |
20 |
|
#define _W64 __w64 |
21 |
|
#else |
22 |
|
#define _W64 |
23 |
|
#endif |
24 |
|
#endif |
25 |
|
|
26 |
|
// |
27 |
|
// typedefs |
28 |
|
// |
29 |
|
typedef char CHAR; |
30 |
|
typedef signed char INT8; |
31 |
|
typedef unsigned char UCHAR; |
32 |
|
typedef unsigned char UINT8; |
33 |
|
typedef unsigned char BYTE; |
34 |
|
typedef short SHORT; |
35 |
|
typedef signed short INT16; |
36 |
|
typedef unsigned short USHORT; |
37 |
|
typedef unsigned short UINT16; |
38 |
|
typedef unsigned short WORD; |
39 |
|
typedef int INT; |
40 |
|
typedef signed int INT32; |
41 |
|
typedef unsigned int UINT; |
42 |
|
typedef unsigned int UINT32; |
43 |
|
typedef long LONG; |
44 |
|
typedef unsigned long ULONG; |
45 |
|
typedef unsigned long DWORD; |
46 |
|
typedef __int64 LONGLONG; |
47 |
|
typedef __int64 LONG64; |
48 |
|
typedef signed __int64 INT64; |
49 |
|
typedef unsigned __int64 ULONGLONG; |
50 |
|
typedef unsigned __int64 DWORDLONG; |
51 |
|
typedef unsigned __int64 ULONG64; |
52 |
|
typedef unsigned __int64 DWORD64; |
53 |
|
typedef unsigned __int64 UINT64; |
54 |
|
|
55 |
|
#if (__midl > 501) |
56 |
|
typedef [public] __int3264 INT_PTR; |
57 |
|
typedef [public] unsigned __int3264 UINT_PTR; |
58 |
|
typedef [public] __int3264 LONG_PTR; |
59 |
|
typedef [public] unsigned __int3264 ULONG_PTR; |
60 |
|
#else |
61 |
|
#ifdef _WIN64 |
62 |
|
typedef __int64 INT_PTR; |
63 |
|
typedef unsigned __int64 UINT_PTR; |
64 |
|
typedef __int64 LONG_PTR; |
65 |
|
typedef unsigned __int64 ULONG_PTR; |
66 |
|
#else |
67 |
|
typedef _W64 int INT_PTR; |
68 |
|
typedef _W64 unsigned int UINT_PTR; |
69 |
|
typedef _W64 long LONG_PTR; |
70 |
|
typedef _W64 unsigned long ULONG_PTR; |
71 |
|
#endif // WIN64 |
72 |
|
#endif // (__midl > 501) |
73 |
|
|
74 |
|
#ifdef _WIN64 |
75 |
|
typedef __int64 ptrdiff_t; |
76 |
|
typedef unsigned __int64 size_t; |
77 |
|
#else |
78 |
|
typedef _W64 int ptrdiff_t; |
79 |
|
typedef _W64 unsigned int size_t; |
80 |
|
#endif |
81 |
|
|
82 |
|
typedef ULONG_PTR DWORD_PTR; |
83 |
|
typedef LONG_PTR SSIZE_T; |
84 |
|
typedef ULONG_PTR SIZE_T; |
85 |
|
|
86 |
|
#if defined(_AMD64_) |
87 |
|
#ifdef __cplusplus |
88 |
|
extern "C" { |
89 |
|
#endif |
90 |
|
|
91 |
|
#define UnsignedMultiply128 _umul128 |
92 |
|
|
93 |
|
ULONG64 |
94 |
|
UnsignedMultiply128 ( |
95 |
|
__in ULONG64 Multiplier, |
96 |
|
__in ULONG64 Multiplicand, |
97 |
|
__out __deref_out_range(==,Multiplier * Multiplicand) ULONG64 *HighProduct |
98 |
|
); |
99 |
|
#pragma intrinsic(_umul128) |
100 |
|
|
101 |
|
#ifdef __cplusplus |
102 |
|
} |
103 |
|
#endif |
104 |
|
#endif // _AMD64_ |
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
typedef __success(return >= 0) long HRESULT; |
109 |
|
|
110 |
|
#define SUCCEEDED(hr) (((HRESULT)(hr)) >= 0) |
111 |
|
#define FAILED(hr) (((HRESULT)(hr)) < 0) |
112 |
|
|
113 |
|
#define S_OK ((HRESULT)0L) |
114 |
|
|
115 |
|
#define INTSAFE_E_ARITHMETIC_OVERFLOW ((HRESULT)0x80070216L) // 0x216 = 534 = ERROR_ARITHMETIC_OVERFLOW |
116 |
|
#ifndef SORTPP_PASS |
117 |
|
// compiletime asserts (failure results in error C2118: negative subscript) |
118 |
|
#define C_ASSERT(e) typedef char __C_ASSERT__[(e)?1:-1] |
119 |
|
#else |
120 |
|
#define C_ASSERT(e) |
121 |
|
#endif |
122 |
|
|
123 |
|
// |
124 |
|
// UInt32x32To64 macro |
125 |
|
// |
126 |
|
#if defined(MIDL_PASS) || defined(RC_INVOKED) || defined(_M_CEE_PURE) \ |
127 |
|
|| defined(_68K_) || defined(_MPPC_) \ |
128 |
|
|| defined(_M_IA64) || defined(_M_AMD64) |
129 |
|
#define UInt32x32To64(a, b) (((unsigned __int64)((unsigned int)(a))) * ((unsigned __int64)((unsigned int)(b)))) |
130 |
|
#elif defined(_M_IX86) |
131 |
|
#define UInt32x32To64(a, b) ((unsigned __int64)(((unsigned __int64)((unsigned int)(a))) * ((unsigned int)(b)))) |
132 |
|
#else |
133 |
|
#error Must define a target architecture. |
134 |
|
#endif |
135 |
|
|
136 |
|
// |
137 |
|
// Min/Max type values |
138 |
|
// |
139 |
|
#define INT8_MIN (-127i8 - 1) |
140 |
|
#define SHORT_MIN (-32768) |
141 |
|
#define INT16_MIN (-32767i16 - 1) |
142 |
|
#define INT_MIN (-2147483647 - 1) |
143 |
|
#define INT32_MIN (-2147483647i32 - 1) |
144 |
|
#define LONG_MIN (-2147483647L - 1) |
145 |
|
#define LONGLONG_MIN (-9223372036854775807i64 - 1) |
146 |
|
#define LONG64_MIN (-9223372036854775807i64 - 1) |
147 |
|
#define INT64_MIN (-9223372036854775807i64 - 1) |
148 |
|
#define INT128_MIN (-170141183460469231731687303715884105727i128 - 1) |
149 |
|
|
150 |
|
#ifdef _WIN64 |
151 |
|
#define INT_PTR_MIN (-9223372036854775807i64 - 1) |
152 |
|
#define LONG_PTR_MIN (-9223372036854775807i64 - 1) |
153 |
|
#define PTRDIFF_T_MIN (-9223372036854775807i64 - 1) |
154 |
|
#define SSIZE_T_MIN (-9223372036854775807i64 - 1) |
155 |
|
#else |
156 |
|
#define INT_PTR_MIN (-2147483647 - 1) |
157 |
|
#define LONG_PTR_MIN (-2147483647L - 1) |
158 |
|
#define PTRDIFF_T_MIN (-2147483647 - 1) |
159 |
|
#define SSIZE_T_MIN (-2147483647L - 1) |
160 |
|
#endif |
161 |
|
|
162 |
|
#define INT8_MAX 127i8 |
163 |
|
#define UINT8_MAX 0xffui8 |
164 |
|
#define BYTE_MAX 0xff |
165 |
|
#define SHORT_MAX 32767 |
166 |
|
#define INT16_MAX 32767i16 |
167 |
|
#define USHORT_MAX 0xffff |
168 |
|
#define UINT16_MAX 0xffffui16 |
169 |
|
#define WORD_MAX 0xffff |
170 |
|
#define INT_MAX 2147483647 |
171 |
|
#define INT32_MAX 2147483647i32 |
172 |
|
#define UINT_MAX 0xffffffff |
173 |
|
#define UINT32_MAX 0xffffffffui32 |
174 |
|
#define LONG_MAX 2147483647L |
175 |
|
#define ULONG_MAX 0xffffffffUL |
176 |
|
#define DWORD_MAX 0xffffffffUL |
177 |
|
#define LONGLONG_MAX 9223372036854775807i64 |
178 |
|
#define LONG64_MAX 9223372036854775807i64 |
179 |
|
#define INT64_MAX 9223372036854775807i64 |
180 |
|
#define ULONGLONG_MAX 0xffffffffffffffffui64 |
181 |
|
#define DWORDLONG_MAX 0xffffffffffffffffui64 |
182 |
|
#define ULONG64_MAX 0xffffffffffffffffui64 |
183 |
|
#define DWORD64_MAX 0xffffffffffffffffui64 |
184 |
|
#define UINT64_MAX 0xffffffffffffffffui64 |
185 |
|
#define INT128_MAX 170141183460469231731687303715884105727i128 |
186 |
|
#define UINT128_MAX 0xffffffffffffffffffffffffffffffffui128 |
187 |
|
|
188 |
|
#undef SIZE_T_MAX |
189 |
|
|
190 |
|
#ifdef _WIN64 |
191 |
|
#define INT_PTR_MAX 9223372036854775807i64 |
192 |
|
#define UINT_PTR_MAX 0xffffffffffffffffui64 |
193 |
|
#define LONG_PTR_MAX 9223372036854775807i64 |
194 |
|
#define ULONG_PTR_MAX 0xffffffffffffffffui64 |
195 |
|
#define DWORD_PTR_MAX 0xffffffffffffffffui64 |
196 |
|
#define PTRDIFF_T_MAX 9223372036854775807i64 |
197 |
|
#define SIZE_T_MAX 0xffffffffffffffffui64 |
198 |
|
#define SSIZE_T_MAX 9223372036854775807i64 |
199 |
|
#define _SIZE_T_MAX 0xffffffffffffffffui64 |
200 |
|
#else |
201 |
|
#define INT_PTR_MAX 2147483647 |
202 |
|
#define UINT_PTR_MAX 0xffffffff |
203 |
|
#define LONG_PTR_MAX 2147483647L |
204 |
|
#define ULONG_PTR_MAX 0xffffffffUL |
205 |
|
#define DWORD_PTR_MAX 0xffffffffUL |
206 |
|
#define PTRDIFF_T_MAX 2147483647 |
207 |
|
#define SIZE_T_MAX 0xffffffff |
208 |
|
#define SSIZE_T_MAX 2147483647L |
209 |
|
#define _SIZE_T_MAX 0xffffffffUL |
210 |
|
#endif |
211 |
|
|
212 |
|
|
213 |
|
// |
214 |
|
// It is common for -1 to be used as an error value |
215 |
|
// |
216 |
|
#define INT8_ERROR (-1i8) |
217 |
|
#define UINT8_ERROR 0xffui8 |
218 |
|
#define BYTE_ERROR 0xff |
219 |
|
#define SHORT_ERROR (-1) |
220 |
|
#define INT16_ERROR (-1i16) |
221 |
|
#define USHORT_ERROR 0xffff |
222 |
|
#define UINT16_ERROR 0xffffui16 |
223 |
|
#define WORD_ERROR 0xffff |
224 |
|
#define INT_ERROR (-1) |
225 |
|
#define INT32_ERROR (-1i32) |