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