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