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