]>
Commit | Line | Data |
---|---|---|
d7a09cb8 SB |
1 | /** @file\r |
2 | This library provides helper functions to prevent integer overflow during\r | |
3 | type conversion, addition, subtraction, and multiplication.\r | |
4 | \r | |
5 | Copyright (c) 2017, Microsoft Corporation\r | |
6 | \r | |
7 | All rights reserved.\r | |
9344f092 | 8 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
d7a09cb8 SB |
9 | \r |
10 | **/\r | |
11 | \r | |
12 | #include <Base.h>\r | |
13 | #include <Library/SafeIntLib.h>\r | |
14 | \r | |
15 | /**\r | |
16 | INT32 -> UINTN conversion\r | |
17 | \r | |
18 | Converts the value specified by Operand to a value specified by Result type\r | |
19 | and stores the converted value into the caller allocated output buffer\r | |
20 | specified by Result. The caller must pass in a Result buffer that is at\r | |
21 | least as large as the Result type.\r | |
22 | \r | |
23 | If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r | |
24 | \r | |
25 | If the conversion results in an overflow or an underflow condition, then\r | |
26 | Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r | |
27 | \r | |
28 | @param[in] Operand Operand to be converted to new type\r | |
29 | @param[out] Result Pointer to the result of conversion\r | |
30 | \r | |
31 | @retval RETURN_SUCCESS Successful conversion\r | |
32 | @retval RETURN_BUFFER_TOO_SMALL Overflow\r | |
33 | @retval RETURN_INVALID_PARAMETER Result is NULL\r | |
34 | **/\r | |
35 | RETURN_STATUS\r | |
36 | EFIAPI\r | |
37 | SafeInt32ToUintn (\r | |
38 | IN INT32 Operand,\r | |
39 | OUT UINTN *Result\r | |
40 | )\r | |
41 | {\r | |
42 | if (sizeof (UINTN) == sizeof (UINT32)) {\r | |
43 | return SafeInt32ToUint32 (Operand, (UINT32 *)Result);\r | |
44 | }\r | |
2f88bd3a MK |
45 | \r |
46 | return SafeInt32ToUint64 (Operand, (UINT64 *)Result);\r | |
d7a09cb8 SB |
47 | }\r |
48 | \r | |
49 | /**\r | |
50 | UINT32 -> INTN conversion\r | |
51 | \r | |
52 | Converts the value specified by Operand to a value specified by Result type\r | |
53 | and stores the converted value into the caller allocated output buffer\r | |
54 | specified by Result. The caller must pass in a Result buffer that is at\r | |
55 | least as large as the Result type.\r | |
56 | \r | |
57 | If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r | |
58 | \r | |
59 | If the conversion results in an overflow or an underflow condition, then\r | |
60 | Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r | |
61 | \r | |
62 | @param[in] Operand Operand to be converted to new type\r | |
63 | @param[out] Result Pointer to the result of conversion\r | |
64 | \r | |
65 | @retval RETURN_SUCCESS Successful conversion\r | |
66 | @retval RETURN_BUFFER_TOO_SMALL Overflow\r | |
67 | @retval RETURN_INVALID_PARAMETER Result is NULL\r | |
68 | **/\r | |
69 | RETURN_STATUS\r | |
70 | EFIAPI\r | |
71 | SafeUint32ToIntn (\r | |
72 | IN UINT32 Operand,\r | |
73 | OUT INTN *Result\r | |
74 | )\r | |
75 | {\r | |
76 | if (Result == NULL) {\r | |
77 | return RETURN_INVALID_PARAMETER;\r | |
78 | }\r | |
79 | \r | |
80 | if (sizeof (UINTN) == sizeof (UINT32)) {\r | |
81 | return SafeUint32ToInt32 (Operand, (INT32 *)Result);\r | |
82 | }\r | |
2f88bd3a | 83 | \r |
d7a09cb8 SB |
84 | *Result = Operand;\r |
85 | return RETURN_SUCCESS;\r | |
86 | }\r | |
87 | \r | |
88 | /**\r | |
89 | INTN -> INT32 conversion\r | |
90 | \r | |
91 | Converts the value specified by Operand to a value specified by Result type\r | |
92 | and stores the converted value into the caller allocated output buffer\r | |
93 | specified by Result. The caller must pass in a Result buffer that is at\r | |
94 | least as large as the Result type.\r | |
95 | \r | |
96 | If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r | |
97 | \r | |
98 | If the conversion results in an overflow or an underflow condition, then\r | |
99 | Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r | |
100 | \r | |
101 | @param[in] Operand Operand to be converted to new type\r | |
102 | @param[out] Result Pointer to the result of conversion\r | |
103 | \r | |
104 | @retval RETURN_SUCCESS Successful conversion\r | |
105 | @retval RETURN_BUFFER_TOO_SMALL Overflow\r | |
106 | @retval RETURN_INVALID_PARAMETER Result is NULL\r | |
107 | **/\r | |
108 | RETURN_STATUS\r | |
109 | EFIAPI\r | |
110 | SafeIntnToInt32 (\r | |
111 | IN INTN Operand,\r | |
112 | OUT INT32 *Result\r | |
113 | )\r | |
114 | {\r | |
115 | if (Result == NULL) {\r | |
116 | return RETURN_INVALID_PARAMETER;\r | |
117 | }\r | |
118 | \r | |
119 | if (sizeof (UINTN) == sizeof (UINT32)) {\r | |
120 | *Result = (INT32)Operand;\r | |
121 | return RETURN_SUCCESS;\r | |
122 | }\r | |
2f88bd3a MK |
123 | \r |
124 | return SafeInt64ToInt32 ((INT64)Operand, Result);\r | |
d7a09cb8 SB |
125 | }\r |
126 | \r | |
127 | /**\r | |
128 | INTN -> UINT32 conversion\r | |
129 | \r | |
130 | Converts the value specified by Operand to a value specified by Result type\r | |
131 | and stores the converted value into the caller allocated output buffer\r | |
132 | specified by Result. The caller must pass in a Result buffer that is at\r | |
133 | least as large as the Result type.\r | |
134 | \r | |
135 | If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r | |
136 | \r | |
137 | If the conversion results in an overflow or an underflow condition, then\r | |
138 | Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r | |
139 | \r | |
140 | @param[in] Operand Operand to be converted to new type\r | |
141 | @param[out] Result Pointer to the result of conversion\r | |
142 | \r | |
143 | @retval RETURN_SUCCESS Successful conversion\r | |
144 | @retval RETURN_BUFFER_TOO_SMALL Overflow\r | |
145 | @retval RETURN_INVALID_PARAMETER Result is NULL\r | |
146 | **/\r | |
147 | RETURN_STATUS\r | |
148 | EFIAPI\r | |
149 | SafeIntnToUint32 (\r | |
150 | IN INTN Operand,\r | |
151 | OUT UINT32 *Result\r | |
152 | )\r | |
153 | {\r | |
154 | RETURN_STATUS Status;\r | |
155 | \r | |
156 | if (Result == NULL) {\r | |
157 | return RETURN_INVALID_PARAMETER;\r | |
158 | }\r | |
159 | \r | |
160 | if (sizeof (UINTN) == sizeof (UINT32)) {\r | |
161 | if (Operand >= 0) {\r | |
162 | *Result = (UINT32)Operand;\r | |
2f88bd3a | 163 | Status = RETURN_SUCCESS;\r |
d7a09cb8 SB |
164 | } else {\r |
165 | *Result = UINT32_ERROR;\r | |
2f88bd3a | 166 | Status = RETURN_BUFFER_TOO_SMALL;\r |
d7a09cb8 SB |
167 | }\r |
168 | \r | |
169 | return Status;\r | |
170 | }\r | |
2f88bd3a | 171 | \r |
d7a09cb8 SB |
172 | return SafeInt64ToUint32 ((INT64)Operand, Result);\r |
173 | }\r | |
174 | \r | |
175 | /**\r | |
176 | UINTN -> UINT32 conversion\r | |
177 | \r | |
178 | Converts the value specified by Operand to a value specified by Result type\r | |
179 | and stores the converted value into the caller allocated output buffer\r | |
180 | specified by Result. The caller must pass in a Result buffer that is at\r | |
181 | least as large as the Result type.\r | |
182 | \r | |
183 | If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r | |
184 | \r | |
185 | If the conversion results in an overflow or an underflow condition, then\r | |
186 | Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r | |
187 | \r | |
188 | @param[in] Operand Operand to be converted to new type\r | |
189 | @param[out] Result Pointer to the result of conversion\r | |
190 | \r | |
191 | @retval RETURN_SUCCESS Successful conversion\r | |
192 | @retval RETURN_BUFFER_TOO_SMALL Overflow\r | |
193 | @retval RETURN_INVALID_PARAMETER Result is NULL\r | |
194 | **/\r | |
195 | RETURN_STATUS\r | |
196 | EFIAPI\r | |
197 | SafeUintnToUint32 (\r | |
198 | IN UINTN Operand,\r | |
199 | OUT UINT32 *Result\r | |
200 | )\r | |
201 | {\r | |
202 | if (Result == NULL) {\r | |
203 | return RETURN_INVALID_PARAMETER;\r | |
204 | }\r | |
205 | \r | |
206 | if (sizeof (UINTN) == sizeof (UINT32)) {\r | |
207 | *Result = (UINT32)Operand;\r | |
208 | return RETURN_SUCCESS;\r | |
209 | }\r | |
2f88bd3a | 210 | \r |
d7a09cb8 SB |
211 | return SafeUint64ToUint32 ((UINT64)Operand, Result);\r |
212 | }\r | |
213 | \r | |
214 | /**\r | |
215 | UINTN -> INT64 conversion\r | |
216 | \r | |
217 | Converts the value specified by Operand to a value specified by Result type\r | |
218 | and stores the converted value into the caller allocated output buffer\r | |
219 | specified by Result. The caller must pass in a Result buffer that is at\r | |
220 | least as large as the Result type.\r | |
221 | \r | |
222 | If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r | |
223 | \r | |
224 | If the conversion results in an overflow or an underflow condition, then\r | |
225 | Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r | |
226 | \r | |
227 | @param[in] Operand Operand to be converted to new type\r | |
228 | @param[out] Result Pointer to the result of conversion\r | |
229 | \r | |
230 | @retval RETURN_SUCCESS Successful conversion\r | |
231 | @retval RETURN_BUFFER_TOO_SMALL Overflow\r | |
232 | @retval RETURN_INVALID_PARAMETER Result is NULL\r | |
233 | **/\r | |
234 | RETURN_STATUS\r | |
235 | EFIAPI\r | |
236 | SafeUintnToInt64 (\r | |
237 | IN UINTN Operand,\r | |
238 | OUT INT64 *Result\r | |
239 | )\r | |
240 | {\r | |
241 | if (Result == NULL) {\r | |
242 | return RETURN_INVALID_PARAMETER;\r | |
243 | }\r | |
244 | \r | |
245 | if (sizeof (UINTN) == sizeof (UINT32)) {\r | |
246 | *Result = (INT64)Operand;\r | |
247 | return RETURN_SUCCESS;\r | |
248 | }\r | |
2f88bd3a | 249 | \r |
d7a09cb8 SB |
250 | return SafeUint64ToInt64 ((UINT64)Operand, Result);\r |
251 | }\r | |
252 | \r | |
253 | /**\r | |
254 | INT64 -> INTN conversion\r | |
255 | \r | |
256 | Converts the value specified by Operand to a value specified by Result type\r | |
257 | and stores the converted value into the caller allocated output buffer\r | |
258 | specified by Result. The caller must pass in a Result buffer that is at\r | |
259 | least as large as the Result type.\r | |
260 | \r | |
261 | If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r | |
262 | \r | |
263 | If the conversion results in an overflow or an underflow condition, then\r | |
264 | Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r | |
265 | \r | |
266 | @param[in] Operand Operand to be converted to new type\r | |
267 | @param[out] Result Pointer to the result of conversion\r | |
268 | \r | |
269 | @retval RETURN_SUCCESS Successful conversion\r | |
270 | @retval RETURN_BUFFER_TOO_SMALL Overflow\r | |
271 | @retval RETURN_INVALID_PARAMETER Result is NULL\r | |
272 | **/\r | |
273 | RETURN_STATUS\r | |
274 | EFIAPI\r | |
275 | SafeInt64ToIntn (\r | |
276 | IN INT64 Operand,\r | |
277 | OUT INTN *Result\r | |
278 | )\r | |
279 | {\r | |
280 | if (Result == NULL) {\r | |
281 | return RETURN_INVALID_PARAMETER;\r | |
282 | }\r | |
283 | \r | |
284 | if (sizeof (UINTN) == sizeof (UINT32)) {\r | |
285 | return SafeInt64ToInt32 (Operand, (INT32 *)Result);\r | |
286 | }\r | |
2f88bd3a | 287 | \r |
d7a09cb8 SB |
288 | *Result = (INTN)Operand;\r |
289 | return RETURN_SUCCESS;\r | |
290 | }\r | |
291 | \r | |
292 | /**\r | |
293 | INT64 -> UINTN conversion\r | |
294 | \r | |
295 | Converts the value specified by Operand to a value specified by Result type\r | |
296 | and stores the converted value into the caller allocated output buffer\r | |
297 | specified by Result. The caller must pass in a Result buffer that is at\r | |
298 | least as large as the Result type.\r | |
299 | \r | |
300 | If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r | |
301 | \r | |
302 | If the conversion results in an overflow or an underflow condition, then\r | |
303 | Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r | |
304 | \r | |
305 | @param[in] Operand Operand to be converted to new type\r | |
306 | @param[out] Result Pointer to the result of conversion\r | |
307 | \r | |
308 | @retval RETURN_SUCCESS Successful conversion\r | |
309 | @retval RETURN_BUFFER_TOO_SMALL Overflow\r | |
310 | @retval RETURN_INVALID_PARAMETER Result is NULL\r | |
311 | **/\r | |
312 | RETURN_STATUS\r | |
313 | EFIAPI\r | |
314 | SafeInt64ToUintn (\r | |
315 | IN INT64 Operand,\r | |
316 | OUT UINTN *Result\r | |
317 | )\r | |
318 | {\r | |
319 | if (sizeof (UINTN) == sizeof (UINT32)) {\r | |
320 | return SafeInt64ToUint32 (Operand, (UINT32 *)Result);\r | |
321 | }\r | |
2f88bd3a | 322 | \r |
d7a09cb8 SB |
323 | return SafeInt64ToUint64 (Operand, (UINT64 *)Result);\r |
324 | }\r | |
325 | \r | |
326 | /**\r | |
327 | UINT64 -> UINTN conversion\r | |
328 | \r | |
329 | Converts the value specified by Operand to a value specified by Result type\r | |
330 | and stores the converted value into the caller allocated output buffer\r | |
331 | specified by Result. The caller must pass in a Result buffer that is at\r | |
332 | least as large as the Result type.\r | |
333 | \r | |
334 | If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r | |
335 | \r | |
336 | If the conversion results in an overflow or an underflow condition, then\r | |
337 | Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r | |
338 | \r | |
339 | @param[in] Operand Operand to be converted to new type\r | |
340 | @param[out] Result Pointer to the result of conversion\r | |
341 | \r | |
342 | @retval RETURN_SUCCESS Successful conversion\r | |
343 | @retval RETURN_BUFFER_TOO_SMALL Overflow\r | |
344 | @retval RETURN_INVALID_PARAMETER Result is NULL\r | |
345 | **/\r | |
346 | RETURN_STATUS\r | |
347 | EFIAPI\r | |
348 | SafeUint64ToUintn (\r | |
349 | IN UINT64 Operand,\r | |
350 | OUT UINTN *Result\r | |
351 | )\r | |
352 | {\r | |
353 | if (Result == NULL) {\r | |
354 | return RETURN_INVALID_PARAMETER;\r | |
355 | }\r | |
356 | \r | |
357 | if (sizeof (UINTN) == sizeof (UINT32)) {\r | |
2f88bd3a | 358 | return SafeUint64ToUint32 ((UINT64)Operand, (UINT32 *)Result);\r |
d7a09cb8 | 359 | }\r |
2f88bd3a | 360 | \r |
d7a09cb8 SB |
361 | *Result = Operand;\r |
362 | return RETURN_SUCCESS;\r | |
363 | }\r | |
364 | \r | |
365 | /**\r | |
366 | UINTN addition\r | |
367 | \r | |
368 | Performs the requested operation using the input parameters into a value\r | |
369 | specified by Result type and stores the converted value into the caller\r | |
370 | allocated output buffer specified by Result. The caller must pass in a\r | |
371 | Result buffer that is at least as large as the Result type.\r | |
372 | \r | |
373 | If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r | |
374 | \r | |
375 | If the requested operation results in an overflow or an underflow condition,\r | |
376 | then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r | |
377 | \r | |
378 | @param[in] Augend A number to which addend will be added\r | |
379 | @param[in] Addend A number to be added to another\r | |
380 | @param[out] Result Pointer to the result of addition\r | |
381 | \r | |
382 | @retval RETURN_SUCCESS Successful addition\r | |
383 | @retval RETURN_BUFFER_TOO_SMALL Overflow\r | |
384 | @retval RETURN_INVALID_PARAMETER Result is NULL\r | |
385 | **/\r | |
386 | RETURN_STATUS\r | |
387 | EFIAPI\r | |
388 | SafeUintnAdd (\r | |
389 | IN UINTN Augend,\r | |
390 | IN UINTN Addend,\r | |
391 | OUT UINTN *Result\r | |
392 | )\r | |
393 | {\r | |
394 | RETURN_STATUS Status;\r | |
395 | \r | |
396 | if (Result == NULL) {\r | |
397 | return RETURN_INVALID_PARAMETER;\r | |
398 | }\r | |
399 | \r | |
400 | if (sizeof (UINTN) == sizeof (UINT32)) {\r | |
401 | if ((UINT32)(Augend + Addend) >= Augend) {\r | |
402 | *Result = (Augend + Addend);\r | |
2f88bd3a | 403 | Status = RETURN_SUCCESS;\r |
d7a09cb8 SB |
404 | } else {\r |
405 | *Result = UINTN_ERROR;\r | |
2f88bd3a | 406 | Status = RETURN_BUFFER_TOO_SMALL;\r |
d7a09cb8 SB |
407 | }\r |
408 | \r | |
409 | return Status;\r | |
410 | }\r | |
2f88bd3a | 411 | \r |
d7a09cb8 SB |
412 | return SafeUint64Add ((UINT64)Augend, (UINT64)Addend, (UINT64 *)Result);\r |
413 | }\r | |
414 | \r | |
415 | /**\r | |
416 | UINTN subtraction\r | |
417 | \r | |
418 | Performs the requested operation using the input parameters into a value\r | |
419 | specified by Result type and stores the converted value into the caller\r | |
420 | allocated output buffer specified by Result. The caller must pass in a\r | |
421 | Result buffer that is at least as large as the Result type.\r | |
422 | \r | |
423 | If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r | |
424 | \r | |
425 | If the requested operation results in an overflow or an underflow condition,\r | |
426 | then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r | |
427 | \r | |
428 | @param[in] Minuend A number from which another is to be subtracted.\r | |
429 | @param[in] Subtrahend A number to be subtracted from another\r | |
430 | @param[out] Result Pointer to the result of subtraction\r | |
431 | \r | |
432 | @retval RETURN_SUCCESS Successful subtraction\r | |
433 | @retval RETURN_BUFFER_TOO_SMALL Underflow\r | |
434 | @retval RETURN_INVALID_PARAMETER Result is NULL\r | |
435 | **/\r | |
436 | RETURN_STATUS\r | |
437 | EFIAPI\r | |
438 | SafeUintnSub (\r | |
439 | IN UINTN Minuend,\r | |
440 | IN UINTN Subtrahend,\r | |
441 | OUT UINTN *Result\r | |
442 | )\r | |
443 | {\r | |
444 | RETURN_STATUS Status;\r | |
445 | \r | |
446 | if (Result == NULL) {\r | |
447 | return RETURN_INVALID_PARAMETER;\r | |
448 | }\r | |
449 | \r | |
450 | if (sizeof (UINTN) == sizeof (UINT32)) {\r | |
451 | if (Minuend >= Subtrahend) {\r | |
452 | *Result = (Minuend - Subtrahend);\r | |
2f88bd3a | 453 | Status = RETURN_SUCCESS;\r |
d7a09cb8 SB |
454 | } else {\r |
455 | *Result = UINTN_ERROR;\r | |
2f88bd3a | 456 | Status = RETURN_BUFFER_TOO_SMALL;\r |
d7a09cb8 SB |
457 | }\r |
458 | \r | |
459 | return Status;\r | |
460 | }\r | |
2f88bd3a | 461 | \r |
d7a09cb8 SB |
462 | return SafeUint64Sub ((UINT64)Minuend, (UINT64)Subtrahend, (UINT64 *)Result);\r |
463 | }\r | |
464 | \r | |
465 | /**\r | |
466 | UINTN multiplication\r | |
467 | \r | |
468 | Performs the requested operation using the input parameters into a value\r | |
469 | specified by Result type and stores the converted value into the caller\r | |
470 | allocated output buffer specified by Result. The caller must pass in a\r | |
471 | Result buffer that is at least as large as the Result type.\r | |
472 | \r | |
473 | If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r | |
474 | \r | |
475 | If the requested operation results in an overflow or an underflow condition,\r | |
476 | then Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r | |
477 | \r | |
478 | @param[in] Multiplicand A number that is to be multiplied by another\r | |
479 | @param[in] Multiplier A number by which the multiplicand is to be multiplied\r | |
480 | @param[out] Result Pointer to the result of multiplication\r | |
481 | \r | |
482 | @retval RETURN_SUCCESS Successful multiplication\r | |
483 | @retval RETURN_BUFFER_TOO_SMALL Overflow\r | |
484 | @retval RETURN_INVALID_PARAMETER Result is NULL\r | |
485 | **/\r | |
486 | RETURN_STATUS\r | |
487 | EFIAPI\r | |
488 | SafeUintnMult (\r | |
489 | IN UINTN Multiplicand,\r | |
490 | IN UINTN Multiplier,\r | |
491 | OUT UINTN *Result\r | |
492 | )\r | |
493 | {\r | |
494 | UINT64 IntermediateResult;\r | |
495 | \r | |
496 | if (sizeof (UINTN) == sizeof (UINT32)) {\r | |
2f88bd3a | 497 | IntermediateResult = ((UINT64)Multiplicand) *((UINT64)Multiplier);\r |
d7a09cb8 SB |
498 | \r |
499 | return SafeUint64ToUintn (IntermediateResult, Result);\r | |
500 | }\r | |
2f88bd3a | 501 | \r |
d7a09cb8 SB |
502 | return SafeUint64Mult ((UINT64)Multiplicand, (UINT64)Multiplier, (UINT64 *)Result);\r |
503 | }\r | |
504 | \r | |
505 | /**\r | |
506 | INTN Addition\r | |
507 | \r | |
508 | Performs the requested operation using the input parameters into a value\r | |
509 | specified by Result type and stores the converted value into the caller\r | |
510 | allocated output buffer specified by Result. The caller must pass in a\r | |
511 | Result buffer that is at least as large as the Result type.\r | |
512 | \r | |
513 | If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r | |
514 | \r | |
515 | If the requested operation results in an overflow or an underflow condition,\r | |
516 | then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r | |
517 | \r | |
518 | @param[in] Augend A number to which addend will be added\r | |
519 | @param[in] Addend A number to be added to another\r | |
520 | @param[out] Result Pointer to the result of addition\r | |
521 | \r | |
522 | @retval RETURN_SUCCESS Successful addition\r | |
523 | @retval RETURN_BUFFER_TOO_SMALL Overflow\r | |
524 | @retval RETURN_INVALID_PARAMETER Result is NULL\r | |
525 | **/\r | |
526 | RETURN_STATUS\r | |
527 | EFIAPI\r | |
528 | SafeIntnAdd (\r | |
529 | IN INTN Augend,\r | |
530 | IN INTN Addend,\r | |
531 | OUT INTN *Result\r | |
532 | )\r | |
533 | {\r | |
534 | if (sizeof (UINTN) == sizeof (UINT32)) {\r | |
535 | return SafeInt64ToIntn (((INT64)Augend) + ((INT64)Addend), Result);\r | |
536 | }\r | |
2f88bd3a | 537 | \r |
d7a09cb8 SB |
538 | return SafeInt64Add ((INT64)Augend, (INT64)Addend, (INT64 *)Result);\r |
539 | }\r | |
540 | \r | |
541 | /**\r | |
542 | INTN Subtraction\r | |
543 | \r | |
544 | Performs the requested operation using the input parameters into a value\r | |
545 | specified by Result type and stores the converted value into the caller\r | |
546 | allocated output buffer specified by Result. The caller must pass in a\r | |
547 | Result buffer that is at least as large as the Result type.\r | |
548 | \r | |
549 | If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r | |
550 | \r | |
551 | If the requested operation results in an overflow or an underflow condition,\r | |
552 | then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r | |
553 | \r | |
554 | @param[in] Minuend A number from which another is to be subtracted.\r | |
555 | @param[in] Subtrahend A number to be subtracted from another\r | |
556 | @param[out] Result Pointer to the result of subtraction\r | |
557 | \r | |
558 | @retval RETURN_SUCCESS Successful subtraction\r | |
559 | @retval RETURN_BUFFER_TOO_SMALL Underflow\r | |
560 | @retval RETURN_INVALID_PARAMETER Result is NULL\r | |
561 | **/\r | |
562 | RETURN_STATUS\r | |
563 | EFIAPI\r | |
564 | SafeIntnSub (\r | |
565 | IN INTN Minuend,\r | |
566 | IN INTN Subtrahend,\r | |
567 | OUT INTN *Result\r | |
568 | )\r | |
569 | {\r | |
570 | if (sizeof (UINTN) == sizeof (UINT32)) {\r | |
571 | return SafeInt64ToIntn (((INT64)Minuend) - ((INT64)Subtrahend), Result);\r | |
572 | }\r | |
2f88bd3a | 573 | \r |
d7a09cb8 SB |
574 | return SafeInt64Sub ((INT64)Minuend, (INT64)Subtrahend, (INT64 *)Result);\r |
575 | }\r | |
576 | \r | |
577 | /**\r | |
578 | INTN multiplication\r | |
579 | \r | |
580 | Performs the requested operation using the input parameters into a value\r | |
581 | specified by Result type and stores the converted value into the caller\r | |
582 | allocated output buffer specified by Result. The caller must pass in a\r | |
583 | Result buffer that is at least as large as the Result type.\r | |
584 | \r | |
585 | If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r | |
586 | \r | |
587 | If the requested operation results in an overflow or an underflow condition,\r | |
588 | then Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r | |
589 | \r | |
590 | @param[in] Multiplicand A number that is to be multiplied by another\r | |
591 | @param[in] Multiplier A number by which the multiplicand is to be multiplied\r | |
592 | @param[out] Result Pointer to the result of multiplication\r | |
593 | \r | |
594 | @retval RETURN_SUCCESS Successful multiplication\r | |
595 | @retval RETURN_BUFFER_TOO_SMALL Overflow\r | |
596 | @retval RETURN_INVALID_PARAMETER Result is NULL\r | |
597 | **/\r | |
598 | RETURN_STATUS\r | |
599 | EFIAPI\r | |
600 | SafeIntnMult (\r | |
601 | IN INTN Multiplicand,\r | |
602 | IN INTN Multiplier,\r | |
603 | OUT INTN *Result\r | |
604 | )\r | |
605 | {\r | |
606 | if (sizeof (UINTN) == sizeof (UINT32)) {\r | |
607 | return SafeInt64ToIntn (((INT64)Multiplicand) *((INT64)Multiplier), Result);\r | |
608 | }\r | |
2f88bd3a | 609 | \r |
d7a09cb8 SB |
610 | return SafeInt64Mult ((INT64)Multiplicand, (INT64)Multiplier, (INT64 *)Result);\r |
611 | }\r |