]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseSafeIntLib/SafeIntLib.c
MdePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdePkg / Library / BaseSafeIntLib / SafeIntLib.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
dc9b2a57 14#include <Library/BaseLib.h>\r
d7a09cb8
SB
15\r
16\r
17//\r
18// Magnitude of MIN_INT64 as expressed by a UINT64 number.\r
19//\r
8c33cc0e 20#define MIN_INT64_MAGNITUDE (((UINT64)(- (MIN_INT64 + 1))) + 1)\r
d7a09cb8
SB
21\r
22//\r
23// Conversion functions\r
24//\r
25// There are three reasons for having conversion functions:\r
26//\r
27// 1. We are converting from a signed type to an unsigned type of the same\r
28// size, or vice-versa.\r
29//\r
30// 2. We are converting to a smaller type, and we could therefore possibly\r
31// overflow.\r
32//\r
33// 3. We are converting to a bigger type, and we are signed and the type we are\r
34// converting to is unsigned.\r
35//\r
36\r
37/**\r
38 INT8 -> UINT8 conversion\r
39\r
40 Converts the value specified by Operand to a value specified by Result type\r
41 and stores the converted value into the caller allocated output buffer\r
42 specified by Result. The caller must pass in a Result buffer that is at\r
43 least as large as the Result type.\r
44\r
45 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
46\r
47 If the conversion results in an overflow or an underflow condition, then\r
48 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
49\r
50 @param[in] Operand Operand to be converted to new type\r
51 @param[out] Result Pointer to the result of conversion\r
52\r
53 @retval RETURN_SUCCESS Successful conversion\r
54 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
55 @retval RETURN_INVALID_PARAMETER Result is NULL\r
56**/\r
57RETURN_STATUS\r
58EFIAPI\r
59SafeInt8ToUint8 (\r
60 IN INT8 Operand,\r
61 OUT UINT8 *Result\r
62 )\r
63{\r
64 RETURN_STATUS Status;\r
65\r
66 if (Result == NULL) {\r
67 return RETURN_INVALID_PARAMETER;\r
68 }\r
69\r
70 if (Operand >= 0) {\r
71 *Result = (UINT8)Operand;\r
72 Status = RETURN_SUCCESS;\r
73 } else {\r
74 *Result = UINT8_ERROR;\r
75 Status = RETURN_BUFFER_TOO_SMALL;\r
76 }\r
77\r
78 return Status;\r
79}\r
80\r
81/**\r
82 INT8 -> CHAR8 conversion\r
83\r
84 Converts the value specified by Operand to a value specified by Result type\r
85 and stores the converted value into the caller allocated output buffer\r
86 specified by Result. The caller must pass in a Result buffer that is at\r
87 least as large as the Result type.\r
88\r
89 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
90\r
91 If the conversion results in an overflow or an underflow condition, then\r
92 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
93\r
94 @param[in] Operand Operand to be converted to new type\r
95 @param[out] Result Pointer to the result of conversion\r
96\r
97 @retval RETURN_SUCCESS Successful conversion\r
98 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
99 @retval RETURN_INVALID_PARAMETER Result is NULL\r
100**/\r
101RETURN_STATUS\r
102EFIAPI\r
103SafeInt8ToChar8 (\r
104 IN INT8 Operand,\r
105 OUT CHAR8 *Result\r
106 )\r
107{\r
108 RETURN_STATUS Status;\r
109\r
110 if (Result == NULL) {\r
111 return RETURN_INVALID_PARAMETER;\r
112 }\r
113\r
114 if (Operand >= 0) {\r
115 *Result = (CHAR8)Operand;\r
116 Status = RETURN_SUCCESS;\r
117 } else {\r
118 *Result = CHAR8_ERROR;\r
119 Status = RETURN_BUFFER_TOO_SMALL;\r
120 }\r
121\r
122 return Status;\r
123}\r
124\r
125/**\r
126 INT8 -> UINT16 conversion\r
127\r
128 Converts the value specified by Operand to a value specified by Result type\r
129 and stores the converted value into the caller allocated output buffer\r
130 specified by Result. The caller must pass in a Result buffer that is at\r
131 least as large as the Result type.\r
132\r
133 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
134\r
135 If the conversion results in an overflow or an underflow condition, then\r
136 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
137\r
138 @param[in] Operand Operand to be converted to new type\r
139 @param[out] Result Pointer to the result of conversion\r
140\r
141 @retval RETURN_SUCCESS Successful conversion\r
142 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
143 @retval RETURN_INVALID_PARAMETER Result is NULL\r
144**/\r
145RETURN_STATUS\r
146EFIAPI\r
147SafeInt8ToUint16 (\r
148 IN INT8 Operand,\r
149 OUT UINT16 *Result\r
150 )\r
151{\r
152 RETURN_STATUS Status;\r
153\r
154 if (Result == NULL) {\r
155 return RETURN_INVALID_PARAMETER;\r
156 }\r
157\r
158 if (Operand >= 0) {\r
159 *Result = (UINT16)Operand;\r
160 Status = RETURN_SUCCESS;\r
161 } else {\r
162 *Result = UINT16_ERROR;\r
163 Status = RETURN_BUFFER_TOO_SMALL;\r
164 }\r
165\r
166 return Status;\r
167}\r
168\r
169/**\r
170 INT8 -> UINT32 conversion\r
171\r
172 Converts the value specified by Operand to a value specified by Result type\r
173 and stores the converted value into the caller allocated output buffer\r
174 specified by Result. The caller must pass in a Result buffer that is at\r
175 least as large as the Result type.\r
176\r
177 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
178\r
179 If the conversion results in an overflow or an underflow condition, then\r
180 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
181\r
182 @param[in] Operand Operand to be converted to new type\r
183 @param[out] Result Pointer to the result of conversion\r
184\r
185 @retval RETURN_SUCCESS Successful conversion\r
186 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
187 @retval RETURN_INVALID_PARAMETER Result is NULL\r
188**/\r
189RETURN_STATUS\r
190EFIAPI\r
191SafeInt8ToUint32 (\r
192 IN INT8 Operand,\r
193 OUT UINT32 *Result\r
194 )\r
195{\r
196 RETURN_STATUS Status;\r
197\r
198 if (Result == NULL) {\r
199 return RETURN_INVALID_PARAMETER;\r
200 }\r
201\r
202 if (Operand >= 0) {\r
203 *Result = (UINT32)Operand;\r
204 Status = RETURN_SUCCESS;\r
205 } else {\r
206 *Result = UINT32_ERROR;\r
207 Status = RETURN_BUFFER_TOO_SMALL;\r
208 }\r
209\r
210 return Status;\r
211}\r
212\r
213/**\r
214 INT8 -> UINTN conversion\r
215\r
216 Converts the value specified by Operand to a value specified by Result type\r
217 and stores the converted value into the caller allocated output buffer\r
218 specified by Result. The caller must pass in a Result buffer that is at\r
219 least as large as the Result type.\r
220\r
221 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
222\r
223 If the conversion results in an overflow or an underflow condition, then\r
224 Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
225\r
226 @param[in] Operand Operand to be converted to new type\r
227 @param[out] Result Pointer to the result of conversion\r
228\r
229 @retval RETURN_SUCCESS Successful conversion\r
230 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
231 @retval RETURN_INVALID_PARAMETER Result is NULL\r
232**/\r
233RETURN_STATUS\r
234EFIAPI\r
235SafeInt8ToUintn (\r
236 IN INT8 Operand,\r
237 OUT UINTN *Result\r
238 )\r
239{\r
240 RETURN_STATUS Status;\r
241\r
242 if (Result == NULL) {\r
243 return RETURN_INVALID_PARAMETER;\r
244 }\r
245\r
246 if (Operand >= 0) {\r
247 *Result = (UINTN)Operand;\r
248 Status = RETURN_SUCCESS;\r
249 } else {\r
250 *Result = UINTN_ERROR;\r
251 Status = RETURN_BUFFER_TOO_SMALL;\r
252 }\r
253\r
254 return Status;\r
255}\r
256\r
257/**\r
258 INT8 -> UINT64 conversion\r
259\r
260 Converts the value specified by Operand to a value specified by Result type\r
261 and stores the converted value into the caller allocated output buffer\r
262 specified by Result. The caller must pass in a Result buffer that is at\r
263 least as large as the Result type.\r
264\r
265 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
266\r
267 If the conversion results in an overflow or an underflow condition, then\r
268 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
269\r
270 @param[in] Operand Operand to be converted to new type\r
271 @param[out] Result Pointer to the result of conversion\r
272\r
273 @retval RETURN_SUCCESS Successful conversion\r
274 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
275 @retval RETURN_INVALID_PARAMETER Result is NULL\r
276**/\r
277RETURN_STATUS\r
278EFIAPI\r
279SafeInt8ToUint64 (\r
280 IN INT8 Operand,\r
281 OUT UINT64 *Result\r
282 )\r
283{\r
284 RETURN_STATUS Status;\r
285\r
286 if (Result == NULL) {\r
287 return RETURN_INVALID_PARAMETER;\r
288 }\r
289\r
290 if (Operand >= 0) {\r
291 *Result = (UINT64)Operand;\r
292 Status = RETURN_SUCCESS;\r
293 } else {\r
294 *Result = UINT64_ERROR;\r
295 Status = RETURN_BUFFER_TOO_SMALL;\r
296 }\r
297\r
298 return Status;\r
299}\r
300\r
301/**\r
302 UINT8 -> INT8 conversion\r
303\r
304 Converts the value specified by Operand to a value specified by Result type\r
305 and stores the converted value into the caller allocated output buffer\r
306 specified by Result. The caller must pass in a Result buffer that is at\r
307 least as large as the Result type.\r
308\r
309 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
310\r
311 If the conversion results in an overflow or an underflow condition, then\r
312 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
313\r
314 @param[in] Operand Operand to be converted to new type\r
315 @param[out] Result Pointer to the result of conversion\r
316\r
317 @retval RETURN_SUCCESS Successful conversion\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
323SafeUint8ToInt8 (\r
324 IN UINT8 Operand,\r
325 OUT INT8 *Result\r
326 )\r
327{\r
328 RETURN_STATUS Status;\r
329\r
330 if (Result == NULL) {\r
331 return RETURN_INVALID_PARAMETER;\r
332 }\r
333\r
334 if (Operand <= MAX_INT8) {\r
335 *Result = (INT8)Operand;\r
336 Status = RETURN_SUCCESS;\r
337 } else {\r
338 *Result = INT8_ERROR;\r
339 Status = RETURN_BUFFER_TOO_SMALL;\r
340 }\r
341\r
342 return Status;\r
343}\r
344\r
345/**\r
346 UINT8 -> CHAR8 conversion\r
347\r
348 Converts the value specified by Operand to a value specified by Result type\r
349 and stores the converted value into the caller allocated output buffer\r
350 specified by Result. The caller must pass in a Result buffer that is at\r
351 least as large as the Result type.\r
352\r
353 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
354\r
355 If the conversion results in an overflow or an underflow condition, then\r
356 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
357\r
358 @param[in] Operand Operand to be converted to new type\r
359 @param[out] Result Pointer to the result of conversion\r
360\r
361 @retval RETURN_SUCCESS Successful conversion\r
362 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
363 @retval RETURN_INVALID_PARAMETER Result is NULL\r
364**/\r
365RETURN_STATUS\r
366EFIAPI\r
367SafeUint8ToChar8 (\r
368 IN UINT8 Operand,\r
369 OUT CHAR8 *Result\r
370 )\r
371{\r
372 RETURN_STATUS Status;\r
373\r
374 if (Result == NULL) {\r
375 return RETURN_INVALID_PARAMETER;\r
376 }\r
377\r
378 if (Operand <= MAX_INT8) {\r
379 *Result = (CHAR8)Operand;\r
380 Status = RETURN_SUCCESS;\r
381 } else {\r
382 *Result = CHAR8_ERROR;\r
383 Status = RETURN_BUFFER_TOO_SMALL;\r
384 }\r
385\r
386 return Status;\r
387}\r
388\r
389/**\r
390 INT16 -> INT8 conversion\r
391\r
392 Converts the value specified by Operand to a value specified by Result type\r
393 and stores the converted value into the caller allocated output buffer\r
394 specified by Result. The caller must pass in a Result buffer that is at\r
395 least as large as the Result type.\r
396\r
397 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
398\r
399 If the conversion results in an overflow or an underflow condition, then\r
400 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
401\r
402 @param[in] Operand Operand to be converted to new type\r
403 @param[out] Result Pointer to the result of conversion\r
404\r
405 @retval RETURN_SUCCESS Successful conversion\r
406 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
407 @retval RETURN_INVALID_PARAMETER Result is NULL\r
408**/\r
409RETURN_STATUS\r
410EFIAPI\r
411SafeInt16ToInt8 (\r
412 IN INT16 Operand,\r
413 OUT INT8 *Result\r
414 )\r
415{\r
416 RETURN_STATUS Status;\r
417\r
418 if (Result == NULL) {\r
419 return RETURN_INVALID_PARAMETER;\r
420 }\r
421\r
422 if ((Operand >= MIN_INT8) && (Operand <= MAX_INT8)) {\r
423 *Result = (INT8)Operand;\r
424 Status = RETURN_SUCCESS;\r
425 } else {\r
426 *Result = INT8_ERROR;\r
427 Status = RETURN_BUFFER_TOO_SMALL;\r
428 }\r
429\r
430 return Status;\r
431}\r
432\r
433/**\r
434 INT16 -> CHAR8 conversion\r
435\r
436 Converts the value specified by Operand to a value specified by Result type\r
437 and stores the converted value into the caller allocated output buffer\r
438 specified by Result. The caller must pass in a Result buffer that is at\r
439 least as large as the Result type.\r
440\r
441 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
442\r
443 If the conversion results in an overflow or an underflow condition, then\r
444 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
445\r
446 @param[in] Operand Operand to be converted to new type\r
447 @param[out] Result Pointer to the result of conversion\r
448\r
449 @retval RETURN_SUCCESS Successful conversion\r
450 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
451 @retval RETURN_INVALID_PARAMETER Result is NULL\r
452**/\r
453RETURN_STATUS\r
454EFIAPI\r
455SafeInt16ToChar8 (\r
456 IN INT16 Operand,\r
457 OUT CHAR8 *Result\r
458 )\r
459{\r
460 RETURN_STATUS Status;\r
461\r
462 if (Result == NULL) {\r
463 return RETURN_INVALID_PARAMETER;\r
464 }\r
465\r
466 if ((Operand >= 0) && (Operand <= MAX_INT8)) {\r
467 *Result = (CHAR8)Operand;\r
468 Status = RETURN_SUCCESS;\r
469 } else {\r
470 *Result = CHAR8_ERROR;\r
471 Status = RETURN_BUFFER_TOO_SMALL;\r
472 }\r
473\r
474 return Status;\r
475}\r
476\r
477/**\r
478 INT16 -> UINT8 conversion\r
479\r
480 Converts the value specified by Operand to a value specified by Result type\r
481 and stores the converted value into the caller allocated output buffer\r
482 specified by Result. The caller must pass in a Result buffer that is at\r
483 least as large as the Result type.\r
484\r
485 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
486\r
487 If the conversion results in an overflow or an underflow condition, then\r
488 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
489\r
490 @param[in] Operand Operand to be converted to new type\r
491 @param[out] Result Pointer to the result of conversion\r
492\r
493 @retval RETURN_SUCCESS Successful conversion\r
494 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
495 @retval RETURN_INVALID_PARAMETER Result is NULL\r
496**/\r
497RETURN_STATUS\r
498EFIAPI\r
499SafeInt16ToUint8 (\r
500 IN INT16 Operand,\r
501 OUT UINT8 *Result\r
502 )\r
503{\r
504 RETURN_STATUS Status;\r
505\r
506 if (Result == NULL) {\r
507 return RETURN_INVALID_PARAMETER;\r
508 }\r
509\r
510 if ((Operand >= 0) && (Operand <= MAX_UINT8)) {\r
511 *Result = (UINT8)Operand;\r
512 Status = RETURN_SUCCESS;\r
513 } else {\r
514 *Result = UINT8_ERROR;\r
515 Status = RETURN_BUFFER_TOO_SMALL;\r
516 }\r
517\r
518 return Status;\r
519}\r
520\r
521/**\r
522 INT16 -> UINT16 conversion\r
523\r
524 Converts the value specified by Operand to a value specified by Result type\r
525 and stores the converted value into the caller allocated output buffer\r
526 specified by Result. The caller must pass in a Result buffer that is at\r
527 least as large as the Result type.\r
528\r
529 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
530\r
531 If the conversion results in an overflow or an underflow condition, then\r
532 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
533\r
534 @param[in] Operand Operand to be converted to new type\r
535 @param[out] Result Pointer to the result of conversion\r
536\r
537 @retval RETURN_SUCCESS Successful conversion\r
538 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
539 @retval RETURN_INVALID_PARAMETER Result is NULL\r
540**/\r
541RETURN_STATUS\r
542EFIAPI\r
543SafeInt16ToUint16 (\r
544 IN INT16 Operand,\r
545 OUT UINT16 *Result\r
546 )\r
547{\r
548 RETURN_STATUS Status;\r
549\r
550 if (Result == NULL) {\r
551 return RETURN_INVALID_PARAMETER;\r
552 }\r
553\r
554 if (Operand >= 0) {\r
555 *Result = (UINT16)Operand;\r
556 Status = RETURN_SUCCESS;\r
557 } else {\r
558 *Result = UINT16_ERROR;\r
559 Status = RETURN_BUFFER_TOO_SMALL;\r
560 }\r
561\r
562 return Status;\r
563}\r
564\r
565/**\r
566 INT16 -> UINT32 conversion\r
567\r
568 Converts the value specified by Operand to a value specified by Result type\r
569 and stores the converted value into the caller allocated output buffer\r
570 specified by Result. The caller must pass in a Result buffer that is at\r
571 least as large as the Result type.\r
572\r
573 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
574\r
575 If the conversion results in an overflow or an underflow condition, then\r
576 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
577\r
578 @param[in] Operand Operand to be converted to new type\r
579 @param[out] Result Pointer to the result of conversion\r
580\r
581 @retval RETURN_SUCCESS Successful conversion\r
582 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
583 @retval RETURN_INVALID_PARAMETER Result is NULL\r
584**/\r
585RETURN_STATUS\r
586EFIAPI\r
587SafeInt16ToUint32 (\r
588 IN INT16 Operand,\r
589 OUT UINT32 *Result\r
590 )\r
591{\r
592 RETURN_STATUS Status;\r
593\r
594 if (Result == NULL) {\r
595 return RETURN_INVALID_PARAMETER;\r
596 }\r
597\r
598 if (Operand >= 0) {\r
599 *Result = (UINT32)Operand;\r
600 Status = RETURN_SUCCESS;\r
601 } else {\r
602 *Result = UINT32_ERROR;\r
603 Status = RETURN_BUFFER_TOO_SMALL;\r
604 }\r
605\r
606 return Status;\r
607}\r
608\r
609/**\r
610 INT16 -> UINTN conversion\r
611\r
612 Converts the value specified by Operand to a value specified by Result type\r
613 and stores the converted value into the caller allocated output buffer\r
614 specified by Result. The caller must pass in a Result buffer that is at\r
615 least as large as the Result type.\r
616\r
617 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
618\r
619 If the conversion results in an overflow or an underflow condition, then\r
620 Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
621\r
622 @param[in] Operand Operand to be converted to new type\r
623 @param[out] Result Pointer to the result of conversion\r
624\r
625 @retval RETURN_SUCCESS Successful conversion\r
626 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
627 @retval RETURN_INVALID_PARAMETER Result is NULL\r
628**/\r
629RETURN_STATUS\r
630EFIAPI\r
631SafeInt16ToUintn (\r
632 IN INT16 Operand,\r
633 OUT UINTN *Result\r
634 )\r
635{\r
636 RETURN_STATUS Status;\r
637\r
638 if (Result == NULL) {\r
639 return RETURN_INVALID_PARAMETER;\r
640 }\r
641\r
642 if (Operand >= 0) {\r
643 *Result = (UINTN)Operand;\r
644 Status = RETURN_SUCCESS;\r
645 } else {\r
646 *Result = UINTN_ERROR;\r
647 Status = RETURN_BUFFER_TOO_SMALL;\r
648 }\r
649\r
650 return Status;\r
651}\r
652\r
653/**\r
654 INT16 -> UINT64 conversion\r
655\r
656 Converts the value specified by Operand to a value specified by Result type\r
657 and stores the converted value into the caller allocated output buffer\r
658 specified by Result. The caller must pass in a Result buffer that is at\r
659 least as large as the Result type.\r
660\r
661 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
662\r
663 If the conversion results in an overflow or an underflow condition, then\r
664 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
665\r
666 @param[in] Operand Operand to be converted to new type\r
667 @param[out] Result Pointer to the result of conversion\r
668\r
669 @retval RETURN_SUCCESS Successful conversion\r
670 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
671 @retval RETURN_INVALID_PARAMETER Result is NULL\r
672**/\r
673RETURN_STATUS\r
674EFIAPI\r
675SafeInt16ToUint64 (\r
676 IN INT16 Operand,\r
677 OUT UINT64 *Result\r
678 )\r
679{\r
680 RETURN_STATUS Status;\r
681\r
682 if (Result == NULL) {\r
683 return RETURN_INVALID_PARAMETER;\r
684 }\r
685\r
686 if (Operand >= 0) {\r
687 *Result = (UINT64)Operand;\r
688 Status = RETURN_SUCCESS;\r
689 } else {\r
690 *Result = UINT64_ERROR;\r
691 Status = RETURN_BUFFER_TOO_SMALL;\r
692 }\r
693\r
694 return Status;\r
695}\r
696\r
697/**\r
698 UINT16 -> INT8 conversion\r
699\r
700 Converts the value specified by Operand to a value specified by Result type\r
701 and stores the converted value into the caller allocated output buffer\r
702 specified by Result. The caller must pass in a Result buffer that is at\r
703 least as large as the Result type.\r
704\r
705 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
706\r
707 If the conversion results in an overflow or an underflow condition, then\r
708 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
709\r
710 @param[in] Operand Operand to be converted to new type\r
711 @param[out] Result Pointer to the result of conversion\r
712\r
713 @retval RETURN_SUCCESS Successful conversion\r
714 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
715 @retval RETURN_INVALID_PARAMETER Result is NULL\r
716**/\r
717RETURN_STATUS\r
718EFIAPI\r
719SafeUint16ToInt8 (\r
720 IN UINT16 Operand,\r
721 OUT INT8 *Result\r
722 )\r
723{\r
724 RETURN_STATUS Status;\r
725\r
726 if (Result == NULL) {\r
727 return RETURN_INVALID_PARAMETER;\r
728 }\r
729\r
730 if (Operand <= MAX_INT8) {\r
731 *Result = (INT8)Operand;\r
732 Status = RETURN_SUCCESS;\r
733 } else {\r
734 *Result = INT8_ERROR;\r
735 Status = RETURN_BUFFER_TOO_SMALL;\r
736 }\r
737\r
738 return Status;\r
739}\r
740\r
741/**\r
742 UINT16 -> CHAR8 conversion\r
743\r
744 Converts the value specified by Operand to a value specified by Result type\r
745 and stores the converted value into the caller allocated output buffer\r
746 specified by Result. The caller must pass in a Result buffer that is at\r
747 least as large as the Result type.\r
748\r
749 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
750\r
751 If the conversion results in an overflow or an underflow condition, then\r
752 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
753\r
754 @param[in] Operand Operand to be converted to new type\r
755 @param[out] Result Pointer to the result of conversion\r
756\r
757 @retval RETURN_SUCCESS Successful conversion\r
758 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
759 @retval RETURN_INVALID_PARAMETER Result is NULL\r
760**/\r
761RETURN_STATUS\r
762EFIAPI\r
763SafeUint16ToChar8 (\r
764 IN UINT16 Operand,\r
765 OUT CHAR8 *Result\r
766 )\r
767{\r
768 RETURN_STATUS Status;\r
769\r
770 if (Result == NULL) {\r
771 return RETURN_INVALID_PARAMETER;\r
772 }\r
773\r
774 if (Operand <= MAX_INT8) {\r
775 *Result = (INT8)Operand;\r
776 Status = RETURN_SUCCESS;\r
777 } else {\r
778 *Result = CHAR8_ERROR;\r
779 Status = RETURN_BUFFER_TOO_SMALL;\r
780 }\r
781\r
782 return Status;\r
783}\r
784\r
785/**\r
786 UINT16 -> UINT8 conversion\r
787\r
788 Converts the value specified by Operand to a value specified by Result type\r
789 and stores the converted value into the caller allocated output buffer\r
790 specified by Result. The caller must pass in a Result buffer that is at\r
791 least as large as the Result type.\r
792\r
793 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
794\r
795 If the conversion results in an overflow or an underflow condition, then\r
796 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
797\r
798 @param[in] Operand Operand to be converted to new type\r
799 @param[out] Result Pointer to the result of conversion\r
800\r
801 @retval RETURN_SUCCESS Successful conversion\r
802 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
803 @retval RETURN_INVALID_PARAMETER Result is NULL\r
804**/\r
805RETURN_STATUS\r
806EFIAPI\r
807SafeUint16ToUint8 (\r
808 IN UINT16 Operand,\r
809 OUT UINT8 *Result\r
810 )\r
811{\r
812 RETURN_STATUS Status;\r
813\r
814 if (Result == NULL) {\r
815 return RETURN_INVALID_PARAMETER;\r
816 }\r
817\r
818 if (Operand <= MAX_UINT8) {\r
819 *Result = (UINT8)Operand;\r
820 Status = RETURN_SUCCESS;\r
821 } else {\r
822 *Result = UINT8_ERROR;\r
823 Status = RETURN_BUFFER_TOO_SMALL;\r
824 }\r
825\r
826 return Status;\r
827}\r
828\r
829/**\r
830 UINT16 -> INT16 conversion\r
831\r
832 Converts the value specified by Operand to a value specified by Result type\r
833 and stores the converted value into the caller allocated output buffer\r
834 specified by Result. The caller must pass in a Result buffer that is at\r
835 least as large as the Result type.\r
836\r
837 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
838\r
839 If the conversion results in an overflow or an underflow condition, then\r
840 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
841\r
842 @param[in] Operand Operand to be converted to new type\r
843 @param[out] Result Pointer to the result of conversion\r
844\r
845 @retval RETURN_SUCCESS Successful conversion\r
846 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
847 @retval RETURN_INVALID_PARAMETER Result is NULL\r
848**/\r
849RETURN_STATUS\r
850EFIAPI\r
851SafeUint16ToInt16 (\r
852 IN UINT16 Operand,\r
853 OUT INT16 *Result\r
854 )\r
855{\r
856 RETURN_STATUS Status;\r
857\r
858 if (Result == NULL) {\r
859 return RETURN_INVALID_PARAMETER;\r
860 }\r
861\r
862 if (Operand <= MAX_INT16) {\r
863 *Result = (INT16)Operand;\r
864 Status = RETURN_SUCCESS;\r
865 } else {\r
866 *Result = INT16_ERROR;\r
867 Status = RETURN_BUFFER_TOO_SMALL;\r
868 }\r
869\r
870 return Status;\r
871}\r
872\r
873/**\r
874 INT32 -> INT8 conversion\r
875\r
876 Converts the value specified by Operand to a value specified by Result type\r
877 and stores the converted value into the caller allocated output buffer\r
878 specified by Result. The caller must pass in a Result buffer that is at\r
879 least as large as the Result type.\r
880\r
881 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
882\r
883 If the conversion results in an overflow or an underflow condition, then\r
884 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
885\r
886 @param[in] Operand Operand to be converted to new type\r
887 @param[out] Result Pointer to the result of conversion\r
888\r
889 @retval RETURN_SUCCESS Successful conversion\r
890 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
891 @retval RETURN_INVALID_PARAMETER Result is NULL\r
892**/\r
893RETURN_STATUS\r
894EFIAPI\r
895SafeInt32ToInt8 (\r
896 IN INT32 Operand,\r
897 OUT INT8 *Result\r
898 )\r
899{\r
900 RETURN_STATUS Status;\r
901\r
902 if (Result == NULL) {\r
903 return RETURN_INVALID_PARAMETER;\r
904 }\r
905\r
906 if ((Operand >= MIN_INT8) && (Operand <= MAX_INT8)) {\r
907 *Result = (INT8)Operand;\r
908 Status = RETURN_SUCCESS;\r
909 } else {\r
910 *Result = INT8_ERROR;\r
911 Status = RETURN_BUFFER_TOO_SMALL;\r
912 }\r
913\r
914 return Status;\r
915}\r
916\r
917/**\r
918 INT32 -> CHAR8 conversion\r
919\r
920 Converts the value specified by Operand to a value specified by Result type\r
921 and stores the converted value into the caller allocated output buffer\r
922 specified by Result. The caller must pass in a Result buffer that is at\r
923 least as large as the Result type.\r
924\r
925 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
926\r
927 If the conversion results in an overflow or an underflow condition, then\r
928 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
929\r
930 @param[in] Operand Operand to be converted to new type\r
931 @param[out] Result Pointer to the result of conversion\r
932\r
933 @retval RETURN_SUCCESS Successful conversion\r
934 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
935 @retval RETURN_INVALID_PARAMETER Result is NULL\r
936**/\r
937RETURN_STATUS\r
938EFIAPI\r
939SafeInt32ToChar8 (\r
940 IN INT32 Operand,\r
941 OUT CHAR8 *Result\r
942 )\r
943{\r
944 RETURN_STATUS Status;\r
945\r
946 if (Result == NULL) {\r
947 return RETURN_INVALID_PARAMETER;\r
948 }\r
949\r
950 if ((Operand >= 0) && (Operand <= MAX_INT8)) {\r
951 *Result = (CHAR8)Operand;\r
952 Status = RETURN_SUCCESS;\r
953 } else {\r
954 *Result = CHAR8_ERROR;\r
955 Status = RETURN_BUFFER_TOO_SMALL;\r
956 }\r
957\r
958 return Status;\r
959}\r
960\r
961/**\r
962 INT32 -> UINT8 conversion\r
963\r
964 Converts the value specified by Operand to a value specified by Result type\r
965 and stores the converted value into the caller allocated output buffer\r
966 specified by Result. The caller must pass in a Result buffer that is at\r
967 least as large as the Result type.\r
968\r
969 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
970\r
971 If the conversion results in an overflow or an underflow condition, then\r
972 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
973\r
974 @param[in] Operand Operand to be converted to new type\r
975 @param[out] Result Pointer to the result of conversion\r
976\r
977 @retval RETURN_SUCCESS Successful conversion\r
978 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
979 @retval RETURN_INVALID_PARAMETER Result is NULL\r
980**/\r
981RETURN_STATUS\r
982EFIAPI\r
983SafeInt32ToUint8 (\r
984 IN INT32 Operand,\r
985 OUT UINT8 *Result\r
986 )\r
987{\r
988 RETURN_STATUS Status;\r
989\r
990 if (Result == NULL) {\r
991 return RETURN_INVALID_PARAMETER;\r
992 }\r
993\r
994 if ((Operand >= 0) && (Operand <= MAX_UINT8)) {\r
995 *Result = (UINT8)Operand;\r
996 Status = RETURN_SUCCESS;\r
997 } else {\r
998 *Result = UINT8_ERROR;\r
999 Status = RETURN_BUFFER_TOO_SMALL;\r
1000 }\r
1001\r
1002 return Status;\r
1003}\r
1004\r
1005/**\r
1006 INT32 -> INT16 conversion\r
1007\r
1008 Converts the value specified by Operand to a value specified by Result type\r
1009 and stores the converted value into the caller allocated output buffer\r
1010 specified by Result. The caller must pass in a Result buffer that is at\r
1011 least as large as the Result type.\r
1012\r
1013 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1014\r
1015 If the conversion results in an overflow or an underflow condition, then\r
1016 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1017\r
1018 @param[in] Operand Operand to be converted to new type\r
1019 @param[out] Result Pointer to the result of conversion\r
1020\r
1021 @retval RETURN_SUCCESS Successful conversion\r
1022 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1023 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1024**/\r
1025RETURN_STATUS\r
1026EFIAPI\r
1027SafeInt32ToInt16 (\r
1028 IN INT32 Operand,\r
1029 OUT INT16 *Result\r
1030 )\r
1031{\r
1032 RETURN_STATUS Status;\r
1033\r
1034 if (Result == NULL) {\r
1035 return RETURN_INVALID_PARAMETER;\r
1036 }\r
1037\r
1038 if ((Operand >= MIN_INT16) && (Operand <= MAX_INT16)) {\r
1039 *Result = (INT16)Operand;\r
1040 Status = RETURN_SUCCESS;\r
1041 } else {\r
1042 *Result = INT16_ERROR;\r
1043 Status = RETURN_BUFFER_TOO_SMALL;\r
1044 }\r
1045\r
1046 return Status;\r
1047}\r
1048\r
1049/**\r
1050 INT32 -> UINT16 conversion\r
1051\r
1052 Converts the value specified by Operand to a value specified by Result type\r
1053 and stores the converted value into the caller allocated output buffer\r
1054 specified by Result. The caller must pass in a Result buffer that is at\r
1055 least as large as the Result type.\r
1056\r
1057 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1058\r
1059 If the conversion results in an overflow or an underflow condition, then\r
1060 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1061\r
1062 @param[in] Operand Operand to be converted to new type\r
1063 @param[out] Result Pointer to the result of conversion\r
1064\r
1065 @retval RETURN_SUCCESS Successful conversion\r
1066 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1067 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1068**/\r
1069RETURN_STATUS\r
1070EFIAPI\r
1071SafeInt32ToUint16 (\r
1072 IN INT32 Operand,\r
1073 OUT UINT16 *Result\r
1074 )\r
1075{\r
1076 RETURN_STATUS Status;\r
1077\r
1078 if (Result == NULL) {\r
1079 return RETURN_INVALID_PARAMETER;\r
1080 }\r
1081\r
1082 if ((Operand >= 0) && (Operand <= MAX_UINT16)) {\r
1083 *Result = (UINT16)Operand;\r
1084 Status = RETURN_SUCCESS;\r
1085 } else {\r
1086 *Result = UINT16_ERROR;\r
1087 Status = RETURN_BUFFER_TOO_SMALL;\r
1088 }\r
1089\r
1090 return Status;\r
1091}\r
1092\r
1093/**\r
1094 INT32 -> UINT32 conversion\r
1095\r
1096 Converts the value specified by Operand to a value specified by Result type\r
1097 and stores the converted value into the caller allocated output buffer\r
1098 specified by Result. The caller must pass in a Result buffer that is at\r
1099 least as large as the Result type.\r
1100\r
1101 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1102\r
1103 If the conversion results in an overflow or an underflow condition, then\r
1104 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1105\r
1106 @param[in] Operand Operand to be converted to new type\r
1107 @param[out] Result Pointer to the result of conversion\r
1108\r
1109 @retval RETURN_SUCCESS Successful conversion\r
1110 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1111 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1112**/\r
1113RETURN_STATUS\r
1114EFIAPI\r
1115SafeInt32ToUint32 (\r
1116 IN INT32 Operand,\r
1117 OUT UINT32 *Result\r
1118 )\r
1119{\r
1120 RETURN_STATUS Status;\r
1121\r
1122 if (Result == NULL) {\r
1123 return RETURN_INVALID_PARAMETER;\r
1124 }\r
1125\r
1126 if (Operand >= 0) {\r
1127 *Result = (UINT32)Operand;\r
1128 Status = RETURN_SUCCESS;\r
1129 } else {\r
1130 *Result = UINT32_ERROR;\r
1131 Status = RETURN_BUFFER_TOO_SMALL;\r
1132 }\r
1133\r
1134 return Status;\r
1135}\r
1136\r
1137/**\r
1138 INT32 -> UINT64 conversion\r
1139\r
1140 Converts the value specified by Operand to a value specified by Result type\r
1141 and stores the converted value into the caller allocated output buffer\r
1142 specified by Result. The caller must pass in a Result buffer that is at\r
1143 least as large as the Result type.\r
1144\r
1145 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1146\r
1147 If the conversion results in an overflow or an underflow condition, then\r
1148 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1149\r
1150 @param[in] Operand Operand to be converted to new type\r
1151 @param[out] Result Pointer to the result of conversion\r
1152\r
1153 @retval RETURN_SUCCESS Successful conversion\r
1154 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1155 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1156**/\r
1157RETURN_STATUS\r
1158EFIAPI\r
1159SafeInt32ToUint64 (\r
1160 IN INT32 Operand,\r
1161 OUT UINT64 *Result\r
1162 )\r
1163{\r
1164 RETURN_STATUS Status;\r
1165\r
1166 if (Result == NULL) {\r
1167 return RETURN_INVALID_PARAMETER;\r
1168 }\r
1169\r
1170 if (Operand >= 0) {\r
1171 *Result = (UINT64)Operand;\r
1172 Status = RETURN_SUCCESS;\r
1173 } else {\r
1174 *Result = UINT64_ERROR;\r
1175 Status = RETURN_BUFFER_TOO_SMALL;\r
1176 }\r
1177\r
1178 return Status;\r
1179}\r
1180\r
1181/**\r
1182 UINT32 -> INT8 conversion\r
1183\r
1184 Converts the value specified by Operand to a value specified by Result type\r
1185 and stores the converted value into the caller allocated output buffer\r
1186 specified by Result. The caller must pass in a Result buffer that is at\r
1187 least as large as the Result type.\r
1188\r
1189 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1190\r
1191 If the conversion results in an overflow or an underflow condition, then\r
1192 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1193\r
1194 @param[in] Operand Operand to be converted to new type\r
1195 @param[out] Result Pointer to the result of conversion\r
1196\r
1197 @retval RETURN_SUCCESS Successful conversion\r
1198 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1199 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1200**/\r
1201RETURN_STATUS\r
1202EFIAPI\r
1203SafeUint32ToInt8 (\r
1204 IN UINT32 Operand,\r
1205 OUT INT8 *Result\r
1206 )\r
1207{\r
1208 RETURN_STATUS Status;\r
1209\r
1210 if (Result == NULL) {\r
1211 return RETURN_INVALID_PARAMETER;\r
1212 }\r
1213\r
1214 if (Operand <= MAX_INT8) {\r
1215 *Result = (INT8)Operand;\r
1216 Status = RETURN_SUCCESS;\r
1217 } else {\r
1218 *Result = INT8_ERROR;\r
1219 Status = RETURN_BUFFER_TOO_SMALL;\r
1220 }\r
1221\r
1222 return Status;\r
1223}\r
1224\r
1225/**\r
1226 UINT32 -> CHAR8 conversion\r
1227\r
1228 Converts the value specified by Operand to a value specified by Result type\r
1229 and stores the converted value into the caller allocated output buffer\r
1230 specified by Result. The caller must pass in a Result buffer that is at\r
1231 least as large as the Result type.\r
1232\r
1233 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1234\r
1235 If the conversion results in an overflow or an underflow condition, then\r
1236 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1237\r
1238 @param[in] Operand Operand to be converted to new type\r
1239 @param[out] Result Pointer to the result of conversion\r
1240\r
1241 @retval RETURN_SUCCESS Successful conversion\r
1242 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1243 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1244**/\r
1245RETURN_STATUS\r
1246EFIAPI\r
1247SafeUint32ToChar8 (\r
1248 IN UINT32 Operand,\r
1249 OUT CHAR8 *Result\r
1250 )\r
1251{\r
1252 RETURN_STATUS Status;\r
1253\r
1254 if (Result == NULL) {\r
1255 return RETURN_INVALID_PARAMETER;\r
1256 }\r
1257\r
1258 if (Operand <= MAX_INT8) {\r
1259 *Result = (INT8)Operand;\r
1260 Status = RETURN_SUCCESS;\r
1261 } else {\r
1262 *Result = CHAR8_ERROR;\r
1263 Status = RETURN_BUFFER_TOO_SMALL;\r
1264 }\r
1265\r
1266 return Status;\r
1267}\r
1268\r
1269/**\r
1270 UINT32 -> UINT8 conversion\r
1271\r
1272 Converts the value specified by Operand to a value specified by Result type\r
1273 and stores the converted value into the caller allocated output buffer\r
1274 specified by Result. The caller must pass in a Result buffer that is at\r
1275 least as large as the Result type.\r
1276\r
1277 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1278\r
1279 If the conversion results in an overflow or an underflow condition, then\r
1280 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1281\r
1282 @param[in] Operand Operand to be converted to new type\r
1283 @param[out] Result Pointer to the result of conversion\r
1284\r
1285 @retval RETURN_SUCCESS Successful conversion\r
1286 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1287 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1288**/\r
1289RETURN_STATUS\r
1290EFIAPI\r
1291SafeUint32ToUint8 (\r
1292 IN UINT32 Operand,\r
1293 OUT UINT8 *Result\r
1294 )\r
1295{\r
1296 RETURN_STATUS Status;\r
1297\r
1298 if (Result == NULL) {\r
1299 return RETURN_INVALID_PARAMETER;\r
1300 }\r
1301\r
1302 if (Operand <= MAX_UINT8) {\r
1303 *Result = (UINT8)Operand;\r
1304 Status = RETURN_SUCCESS;\r
1305 } else {\r
1306 *Result = UINT8_ERROR;\r
1307 Status = RETURN_BUFFER_TOO_SMALL;\r
1308 }\r
1309\r
1310 return Status;\r
1311}\r
1312\r
1313/**\r
1314 UINT32 -> INT16 conversion\r
1315\r
1316 Converts the value specified by Operand to a value specified by Result type\r
1317 and stores the converted value into the caller allocated output buffer\r
1318 specified by Result. The caller must pass in a Result buffer that is at\r
1319 least as large as the Result type.\r
1320\r
1321 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1322\r
1323 If the conversion results in an overflow or an underflow condition, then\r
1324 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1325\r
1326 @param[in] Operand Operand to be converted to new type\r
1327 @param[out] Result Pointer to the result of conversion\r
1328\r
1329 @retval RETURN_SUCCESS Successful conversion\r
1330 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1331 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1332**/\r
1333RETURN_STATUS\r
1334EFIAPI\r
1335SafeUint32ToInt16 (\r
1336 IN UINT32 Operand,\r
1337 OUT INT16 *Result\r
1338 )\r
1339{\r
1340 RETURN_STATUS Status;\r
1341\r
1342 if (Result == NULL) {\r
1343 return RETURN_INVALID_PARAMETER;\r
1344 }\r
1345\r
1346 if (Operand <= MAX_INT16) {\r
1347 *Result = (INT16)Operand;\r
1348 Status = RETURN_SUCCESS;\r
1349 } else {\r
1350 *Result = INT16_ERROR;\r
1351 Status = RETURN_BUFFER_TOO_SMALL;\r
1352 }\r
1353\r
1354 return Status;\r
1355}\r
1356\r
1357/**\r
1358 UINT32 -> UINT16 conversion\r
1359\r
1360 Converts the value specified by Operand to a value specified by Result type\r
1361 and stores the converted value into the caller allocated output buffer\r
1362 specified by Result. The caller must pass in a Result buffer that is at\r
1363 least as large as the Result type.\r
1364\r
1365 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1366\r
1367 If the conversion results in an overflow or an underflow condition, then\r
1368 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1369\r
1370 @param[in] Operand Operand to be converted to new type\r
1371 @param[out] Result Pointer to the result of conversion\r
1372\r
1373 @retval RETURN_SUCCESS Successful conversion\r
1374 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1375 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1376**/\r
1377RETURN_STATUS\r
1378EFIAPI\r
1379SafeUint32ToUint16 (\r
1380 IN UINT32 Operand,\r
1381 OUT UINT16 *Result\r
1382 )\r
1383{\r
1384 RETURN_STATUS Status;\r
1385\r
1386 if (Result == NULL) {\r
1387 return RETURN_INVALID_PARAMETER;\r
1388 }\r
1389\r
1390 if (Operand <= MAX_UINT16) {\r
1391 *Result = (UINT16)Operand;\r
1392 Status = RETURN_SUCCESS;\r
1393 } else {\r
1394 *Result = UINT16_ERROR;\r
1395 Status = RETURN_BUFFER_TOO_SMALL;\r
1396 }\r
1397\r
1398 return Status;\r
1399}\r
1400\r
1401/**\r
1402 UINT32 -> INT32 conversion\r
1403\r
1404 Converts the value specified by Operand to a value specified by Result type\r
1405 and stores the converted value into the caller allocated output buffer\r
1406 specified by Result. The caller must pass in a Result buffer that is at\r
1407 least as large as the Result type.\r
1408\r
1409 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1410\r
1411 If the conversion results in an overflow or an underflow condition, then\r
1412 Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1413\r
1414 @param[in] Operand Operand to be converted to new type\r
1415 @param[out] Result Pointer to the result of conversion\r
1416\r
1417 @retval RETURN_SUCCESS Successful conversion\r
1418 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1419 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1420**/\r
1421RETURN_STATUS\r
1422EFIAPI\r
1423SafeUint32ToInt32 (\r
1424 IN UINT32 Operand,\r
1425 OUT INT32 *Result\r
1426 )\r
1427{\r
1428 RETURN_STATUS Status;\r
1429\r
1430 if (Result == NULL) {\r
1431 return RETURN_INVALID_PARAMETER;\r
1432 }\r
1433\r
1434 if (Operand <= MAX_INT32) {\r
1435 *Result = (INT32)Operand;\r
1436 Status = RETURN_SUCCESS;\r
1437 } else {\r
1438 *Result = INT32_ERROR;\r
1439 Status = RETURN_BUFFER_TOO_SMALL;\r
1440 }\r
1441\r
1442 return Status;\r
1443}\r
1444\r
1445/**\r
1446 INTN -> INT8 conversion\r
1447\r
1448 Converts the value specified by Operand to a value specified by Result type\r
1449 and stores the converted value into the caller allocated output buffer\r
1450 specified by Result. The caller must pass in a Result buffer that is at\r
1451 least as large as the Result type.\r
1452\r
1453 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1454\r
1455 If the conversion results in an overflow or an underflow condition, then\r
1456 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1457\r
1458 @param[in] Operand Operand to be converted to new type\r
1459 @param[out] Result Pointer to the result of conversion\r
1460\r
1461 @retval RETURN_SUCCESS Successful conversion\r
1462 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1463 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1464**/\r
1465RETURN_STATUS\r
1466EFIAPI\r
1467SafeIntnToInt8 (\r
1468 IN INTN Operand,\r
1469 OUT INT8 *Result\r
1470 )\r
1471{\r
1472 RETURN_STATUS Status;\r
1473\r
1474 if (Result == NULL) {\r
1475 return RETURN_INVALID_PARAMETER;\r
1476 }\r
1477\r
1478 if ((Operand >= MIN_INT8) && (Operand <= MAX_INT8)) {\r
1479 *Result = (INT8)Operand;\r
1480 Status = RETURN_SUCCESS;\r
1481 } else {\r
1482 *Result = INT8_ERROR;\r
1483 Status = RETURN_BUFFER_TOO_SMALL;\r
1484 }\r
1485\r
1486 return Status;\r
1487}\r
1488\r
1489/**\r
1490 INTN -> CHAR8 conversion\r
1491\r
1492 Converts the value specified by Operand to a value specified by Result type\r
1493 and stores the converted value into the caller allocated output buffer\r
1494 specified by Result. The caller must pass in a Result buffer that is at\r
1495 least as large as the Result type.\r
1496\r
1497 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1498\r
1499 If the conversion results in an overflow or an underflow condition, then\r
1500 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1501\r
1502 @param[in] Operand Operand to be converted to new type\r
1503 @param[out] Result Pointer to the result of conversion\r
1504\r
1505 @retval RETURN_SUCCESS Successful conversion\r
1506 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1507 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1508**/\r
1509RETURN_STATUS\r
1510EFIAPI\r
1511SafeIntnToChar8 (\r
1512 IN INTN Operand,\r
1513 OUT CHAR8 *Result\r
1514 )\r
1515{\r
1516 RETURN_STATUS Status;\r
1517\r
1518 if (Result == NULL) {\r
1519 return RETURN_INVALID_PARAMETER;\r
1520 }\r
1521\r
1522 if ((Operand >= 0) && (Operand <= MAX_INT8)) {\r
1523 *Result = (CHAR8)Operand;\r
1524 Status = RETURN_SUCCESS;\r
1525 } else {\r
1526 *Result = CHAR8_ERROR;\r
1527 Status = RETURN_BUFFER_TOO_SMALL;\r
1528 }\r
1529\r
1530 return Status;\r
1531}\r
1532\r
1533/**\r
1534 INTN -> UINT8 conversion\r
1535\r
1536 Converts the value specified by Operand to a value specified by Result type\r
1537 and stores the converted value into the caller allocated output buffer\r
1538 specified by Result. The caller must pass in a Result buffer that is at\r
1539 least as large as the Result type.\r
1540\r
1541 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1542\r
1543 If the conversion results in an overflow or an underflow condition, then\r
1544 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1545\r
1546 @param[in] Operand Operand to be converted to new type\r
1547 @param[out] Result Pointer to the result of conversion\r
1548\r
1549 @retval RETURN_SUCCESS Successful conversion\r
1550 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1551 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1552**/\r
1553RETURN_STATUS\r
1554EFIAPI\r
1555SafeIntnToUint8 (\r
1556 IN INTN Operand,\r
1557 OUT UINT8 *Result\r
1558 )\r
1559{\r
1560 RETURN_STATUS Status;\r
1561\r
1562 if (Result == NULL) {\r
1563 return RETURN_INVALID_PARAMETER;\r
1564 }\r
1565\r
1566 if ((Operand >= 0) && (Operand <= MAX_UINT8)) {\r
1567 *Result = (UINT8)Operand;\r
1568 Status = RETURN_SUCCESS;\r
1569 } else {\r
1570 *Result = UINT8_ERROR;\r
1571 Status = RETURN_BUFFER_TOO_SMALL;\r
1572 }\r
1573\r
1574 return Status;\r
1575}\r
1576\r
1577/**\r
1578 INTN -> INT16 conversion\r
1579\r
1580 Converts the value specified by Operand to a value specified by Result type\r
1581 and stores the converted value into the caller allocated output buffer\r
1582 specified by Result. The caller must pass in a Result buffer that is at\r
1583 least as large as the Result type.\r
1584\r
1585 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1586\r
1587 If the conversion results in an overflow or an underflow condition, then\r
1588 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1589\r
1590 @param[in] Operand Operand to be converted to new type\r
1591 @param[out] Result Pointer to the result of conversion\r
1592\r
1593 @retval RETURN_SUCCESS Successful conversion\r
1594 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1595 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1596**/\r
1597RETURN_STATUS\r
1598EFIAPI\r
1599SafeIntnToInt16 (\r
1600 IN INTN Operand,\r
1601 OUT INT16 *Result\r
1602 )\r
1603{\r
1604 RETURN_STATUS Status;\r
1605\r
1606 if (Result == NULL) {\r
1607 return RETURN_INVALID_PARAMETER;\r
1608 }\r
1609\r
1610 if ((Operand >= MIN_INT16) && (Operand <= MAX_INT16)) {\r
1611 *Result = (INT16)Operand;\r
1612 Status = RETURN_SUCCESS;\r
1613 } else {\r
1614 *Result = INT16_ERROR;\r
1615 Status = RETURN_BUFFER_TOO_SMALL;\r
1616 }\r
1617\r
1618 return Status;\r
1619}\r
1620\r
1621/**\r
1622 INTN -> UINT16 conversion\r
1623\r
1624 Converts the value specified by Operand to a value specified by Result type\r
1625 and stores the converted value into the caller allocated output buffer\r
1626 specified by Result. The caller must pass in a Result buffer that is at\r
1627 least as large as the Result type.\r
1628\r
1629 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1630\r
1631 If the conversion results in an overflow or an underflow condition, then\r
1632 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1633\r
1634 @param[in] Operand Operand to be converted to new type\r
1635 @param[out] Result Pointer to the result of conversion\r
1636\r
1637 @retval RETURN_SUCCESS Successful conversion\r
1638 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1639 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1640**/\r
1641RETURN_STATUS\r
1642EFIAPI\r
1643SafeIntnToUint16 (\r
1644 IN INTN Operand,\r
1645 OUT UINT16 *Result\r
1646 )\r
1647{\r
1648 RETURN_STATUS Status;\r
1649\r
1650 if (Result == NULL) {\r
1651 return RETURN_INVALID_PARAMETER;\r
1652 }\r
1653\r
1654 if ((Operand >= 0) && (Operand <= MAX_UINT16)) {\r
1655 *Result = (UINT16)Operand;\r
1656 Status = RETURN_SUCCESS;\r
1657 } else {\r
1658 *Result = UINT16_ERROR;\r
1659 Status = RETURN_BUFFER_TOO_SMALL;\r
1660 }\r
1661\r
1662 return Status;\r
1663}\r
1664\r
1665/**\r
1666 INTN -> UINTN conversion\r
1667\r
1668 Converts the value specified by Operand to a value specified by Result type\r
1669 and stores the converted value into the caller allocated output buffer\r
1670 specified by Result. The caller must pass in a Result buffer that is at\r
1671 least as large as the Result type.\r
1672\r
1673 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1674\r
1675 If the conversion results in an overflow or an underflow condition, then\r
1676 Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1677\r
1678 @param[in] Operand Operand to be converted to new type\r
1679 @param[out] Result Pointer to the result of conversion\r
1680\r
1681 @retval RETURN_SUCCESS Successful conversion\r
1682 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1683 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1684**/\r
1685RETURN_STATUS\r
1686EFIAPI\r
1687SafeIntnToUintn (\r
1688 IN INTN Operand,\r
1689 OUT UINTN *Result\r
1690 )\r
1691{\r
1692 RETURN_STATUS Status;\r
1693\r
1694 if (Result == NULL) {\r
1695 return RETURN_INVALID_PARAMETER;\r
1696 }\r
1697\r
1698 if (Operand >= 0) {\r
1699 *Result = (UINTN)Operand;\r
1700 Status = RETURN_SUCCESS;\r
1701 } else {\r
1702 *Result = UINTN_ERROR;\r
1703 Status = RETURN_BUFFER_TOO_SMALL;\r
1704 }\r
1705\r
1706 return Status;\r
1707}\r
1708\r
1709/**\r
1710 INTN -> UINT64 conversion\r
1711\r
1712 Converts the value specified by Operand to a value specified by Result type\r
1713 and stores the converted value into the caller allocated output buffer\r
1714 specified by Result. The caller must pass in a Result buffer that is at\r
1715 least as large as the Result type.\r
1716\r
1717 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1718\r
1719 If the conversion results in an overflow or an underflow condition, then\r
1720 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1721\r
1722 @param[in] Operand Operand to be converted to new type\r
1723 @param[out] Result Pointer to the result of conversion\r
1724\r
1725 @retval RETURN_SUCCESS Successful conversion\r
1726 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1727 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1728**/\r
1729RETURN_STATUS\r
1730EFIAPI\r
1731SafeIntnToUint64 (\r
1732 IN INTN Operand,\r
1733 OUT UINT64 *Result\r
1734 )\r
1735{\r
1736 RETURN_STATUS Status;\r
1737\r
1738 if (Result == NULL) {\r
1739 return RETURN_INVALID_PARAMETER;\r
1740 }\r
1741\r
1742 if (Operand >= 0) {\r
1743 *Result = (UINT64)Operand;\r
1744 Status = RETURN_SUCCESS;\r
1745 } else {\r
1746 *Result = UINT64_ERROR;\r
1747 Status = RETURN_BUFFER_TOO_SMALL;\r
1748 }\r
1749\r
1750 return Status;\r
1751}\r
1752\r
1753/**\r
1754 UINTN -> INT8 conversion\r
1755\r
1756 Converts the value specified by Operand to a value specified by Result type\r
1757 and stores the converted value into the caller allocated output buffer\r
1758 specified by Result. The caller must pass in a Result buffer that is at\r
1759 least as large as the Result type.\r
1760\r
1761 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1762\r
1763 If the conversion results in an overflow or an underflow condition, then\r
1764 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1765\r
1766 @param[in] Operand Operand to be converted to new type\r
1767 @param[out] Result Pointer to the result of conversion\r
1768\r
1769 @retval RETURN_SUCCESS Successful conversion\r
1770 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1771 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1772**/\r
1773RETURN_STATUS\r
1774EFIAPI\r
1775SafeUintnToInt8 (\r
1776 IN UINTN Operand,\r
1777 OUT INT8 *Result\r
1778 )\r
1779{\r
1780 RETURN_STATUS Status;\r
1781\r
1782 if (Result == NULL) {\r
1783 return RETURN_INVALID_PARAMETER;\r
1784 }\r
1785\r
1786 if (Operand <= MAX_INT8) {\r
1787 *Result = (INT8)Operand;\r
1788 Status = RETURN_SUCCESS;\r
1789 } else {\r
1790 *Result = INT8_ERROR;\r
1791 Status = RETURN_BUFFER_TOO_SMALL;\r
1792 }\r
1793\r
1794 return Status;\r
1795}\r
1796\r
1797/**\r
1798 UINTN -> CHAR8 conversion\r
1799\r
1800 Converts the value specified by Operand to a value specified by Result type\r
1801 and stores the converted value into the caller allocated output buffer\r
1802 specified by Result. The caller must pass in a Result buffer that is at\r
1803 least as large as the Result type.\r
1804\r
1805 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1806\r
1807 If the conversion results in an overflow or an underflow condition, then\r
1808 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1809\r
1810 @param[in] Operand Operand to be converted to new type\r
1811 @param[out] Result Pointer to the result of conversion\r
1812\r
1813 @retval RETURN_SUCCESS Successful conversion\r
1814 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1815 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1816**/\r
1817RETURN_STATUS\r
1818EFIAPI\r
1819SafeUintnToChar8 (\r
1820 IN UINTN Operand,\r
1821 OUT CHAR8 *Result\r
1822 )\r
1823{\r
1824 RETURN_STATUS Status;\r
1825\r
1826 if (Result == NULL) {\r
1827 return RETURN_INVALID_PARAMETER;\r
1828 }\r
1829\r
1830 if (Operand <= MAX_INT8) {\r
1831 *Result = (INT8)Operand;\r
1832 Status = RETURN_SUCCESS;\r
1833 } else {\r
1834 *Result = CHAR8_ERROR;\r
1835 Status = RETURN_BUFFER_TOO_SMALL;\r
1836 }\r
1837\r
1838 return Status;\r
1839}\r
1840\r
1841/**\r
1842 UINTN -> UINT8 conversion\r
1843\r
1844 Converts the value specified by Operand to a value specified by Result type\r
1845 and stores the converted value into the caller allocated output buffer\r
1846 specified by Result. The caller must pass in a Result buffer that is at\r
1847 least as large as the Result type.\r
1848\r
1849 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1850\r
1851 If the conversion results in an overflow or an underflow condition, then\r
1852 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1853\r
1854 @param[in] Operand Operand to be converted to new type\r
1855 @param[out] Result Pointer to the result of conversion\r
1856\r
1857 @retval RETURN_SUCCESS Successful conversion\r
1858 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1859 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1860**/\r
1861RETURN_STATUS\r
1862EFIAPI\r
1863SafeUintnToUint8 (\r
1864 IN UINTN Operand,\r
1865 OUT UINT8 *Result\r
1866 )\r
1867{\r
1868 RETURN_STATUS Status;\r
1869\r
1870 if (Result == NULL) {\r
1871 return RETURN_INVALID_PARAMETER;\r
1872 }\r
1873\r
1874 if (Operand <= MAX_UINT8) {\r
1875 *Result = (UINT8)Operand;\r
1876 Status = RETURN_SUCCESS;\r
1877 } else {\r
1878 *Result = UINT8_ERROR;\r
1879 Status = RETURN_BUFFER_TOO_SMALL;\r
1880 }\r
1881\r
1882 return Status;\r
1883}\r
1884\r
1885/**\r
1886 UINTN -> INT16 conversion\r
1887\r
1888 Converts the value specified by Operand to a value specified by Result type\r
1889 and stores the converted value into the caller allocated output buffer\r
1890 specified by Result. The caller must pass in a Result buffer that is at\r
1891 least as large as the Result type.\r
1892\r
1893 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1894\r
1895 If the conversion results in an overflow or an underflow condition, then\r
1896 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1897\r
1898 @param[in] Operand Operand to be converted to new type\r
1899 @param[out] Result Pointer to the result of conversion\r
1900\r
1901 @retval RETURN_SUCCESS Successful conversion\r
1902 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1903 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1904**/\r
1905RETURN_STATUS\r
1906EFIAPI\r
1907SafeUintnToInt16 (\r
1908 IN UINTN Operand,\r
1909 OUT INT16 *Result\r
1910 )\r
1911{\r
1912 RETURN_STATUS Status;\r
1913\r
1914 if (Result == NULL) {\r
1915 return RETURN_INVALID_PARAMETER;\r
1916 }\r
1917\r
1918 if (Operand <= MAX_INT16) {\r
1919 *Result = (INT16)Operand;\r
1920 Status = RETURN_SUCCESS;\r
1921 } else {\r
1922 *Result = INT16_ERROR;\r
1923 Status = RETURN_BUFFER_TOO_SMALL;\r
1924 }\r
1925\r
1926 return Status;\r
1927}\r
1928\r
1929/**\r
1930 UINTN -> UINT16 conversion\r
1931\r
1932 Converts the value specified by Operand to a value specified by Result type\r
1933 and stores the converted value into the caller allocated output buffer\r
1934 specified by Result. The caller must pass in a Result buffer that is at\r
1935 least as large as the Result type.\r
1936\r
1937 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1938\r
1939 If the conversion results in an overflow or an underflow condition, then\r
1940 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1941\r
1942 @param[in] Operand Operand to be converted to new type\r
1943 @param[out] Result Pointer to the result of conversion\r
1944\r
1945 @retval RETURN_SUCCESS Successful conversion\r
1946 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1947 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1948**/\r
1949RETURN_STATUS\r
1950EFIAPI\r
1951SafeUintnToUint16 (\r
1952 IN UINTN Operand,\r
1953 OUT UINT16 *Result\r
1954 )\r
1955{\r
1956 RETURN_STATUS Status;\r
1957\r
1958 if (Result == NULL) {\r
1959 return RETURN_INVALID_PARAMETER;\r
1960 }\r
1961\r
1962 if (Operand <= MAX_UINT16) {\r
1963 *Result = (UINT16)Operand;\r
1964 Status = RETURN_SUCCESS;\r
1965 } else {\r
1966 *Result = UINT16_ERROR;\r
1967 Status = RETURN_BUFFER_TOO_SMALL;\r
1968 }\r
1969\r
1970 return Status;\r
1971}\r
1972\r
1973/**\r
1974 UINTN -> INT32 conversion\r
1975\r
1976 Converts the value specified by Operand to a value specified by Result type\r
1977 and stores the converted value into the caller allocated output buffer\r
1978 specified by Result. The caller must pass in a Result buffer that is at\r
1979 least as large as the Result type.\r
1980\r
1981 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
1982\r
1983 If the conversion results in an overflow or an underflow condition, then\r
1984 Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
1985\r
1986 @param[in] Operand Operand to be converted to new type\r
1987 @param[out] Result Pointer to the result of conversion\r
1988\r
1989 @retval RETURN_SUCCESS Successful conversion\r
1990 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
1991 @retval RETURN_INVALID_PARAMETER Result is NULL\r
1992**/\r
1993RETURN_STATUS\r
1994EFIAPI\r
1995SafeUintnToInt32 (\r
1996 IN UINTN Operand,\r
1997 OUT INT32 *Result\r
1998 )\r
1999{\r
2000 RETURN_STATUS Status;\r
2001\r
2002 if (Result == NULL) {\r
2003 return RETURN_INVALID_PARAMETER;\r
2004 }\r
2005\r
2006 if (Operand <= MAX_INT32) {\r
2007 *Result = (INT32)Operand;\r
2008 Status = RETURN_SUCCESS;\r
2009 } else {\r
2010 *Result = INT32_ERROR;\r
2011 Status = RETURN_BUFFER_TOO_SMALL;\r
2012 }\r
2013\r
2014 return Status;\r
2015}\r
2016\r
2017/**\r
2018 UINTN -> INTN conversion\r
2019\r
2020 Converts the value specified by Operand to a value specified by Result type\r
2021 and stores the converted value into the caller allocated output buffer\r
2022 specified by Result. The caller must pass in a Result buffer that is at\r
2023 least as large as the Result type.\r
2024\r
2025 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2026\r
2027 If the conversion results in an overflow or an underflow condition, then\r
2028 Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2029\r
2030 @param[in] Operand Operand to be converted to new type\r
2031 @param[out] Result Pointer to the result of conversion\r
2032\r
2033 @retval RETURN_SUCCESS Successful conversion\r
2034 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2035 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2036**/\r
2037RETURN_STATUS\r
2038EFIAPI\r
2039SafeUintnToIntn (\r
2040 IN UINTN Operand,\r
2041 OUT INTN *Result\r
2042 )\r
2043{\r
2044 RETURN_STATUS Status;\r
2045\r
2046 if (Result == NULL) {\r
2047 return RETURN_INVALID_PARAMETER;\r
2048 }\r
2049\r
2050 if (Operand <= MAX_INTN) {\r
2051 *Result = (INTN)Operand;\r
2052 Status = RETURN_SUCCESS;\r
2053 } else {\r
2054 *Result = INTN_ERROR;\r
2055 Status = RETURN_BUFFER_TOO_SMALL;\r
2056 }\r
2057\r
2058 return Status;\r
2059}\r
2060\r
2061/**\r
2062 INT64 -> INT8 conversion\r
2063\r
2064 Converts the value specified by Operand to a value specified by Result type\r
2065 and stores the converted value into the caller allocated output buffer\r
2066 specified by Result. The caller must pass in a Result buffer that is at\r
2067 least as large as the Result type.\r
2068\r
2069 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2070\r
2071 If the conversion results in an overflow or an underflow condition, then\r
2072 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2073\r
2074 @param[in] Operand Operand to be converted to new type\r
2075 @param[out] Result Pointer to the result of conversion\r
2076\r
2077 @retval RETURN_SUCCESS Successful conversion\r
2078 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2079 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2080**/\r
2081RETURN_STATUS\r
2082EFIAPI\r
2083SafeInt64ToInt8 (\r
2084 IN INT64 Operand,\r
2085 OUT INT8 *Result\r
2086 )\r
2087{\r
2088 RETURN_STATUS Status;\r
2089\r
2090 if (Result == NULL) {\r
2091 return RETURN_INVALID_PARAMETER;\r
2092 }\r
2093\r
2094 if ((Operand >= MIN_INT8) && (Operand <= MAX_INT8)) {\r
2095 *Result = (INT8)Operand;\r
2096 Status = RETURN_SUCCESS;\r
2097 } else {\r
2098 *Result = INT8_ERROR;\r
2099 Status = RETURN_BUFFER_TOO_SMALL;\r
2100 }\r
2101\r
2102 return Status;\r
2103}\r
2104\r
2105/**\r
2106 INT64 -> CHAR8 conversion\r
2107\r
2108 Converts the value specified by Operand to a value specified by Result type\r
2109 and stores the converted value into the caller allocated output buffer\r
2110 specified by Result. The caller must pass in a Result buffer that is at\r
2111 least as large as the Result type.\r
2112\r
2113 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2114\r
2115 If the conversion results in an overflow or an underflow condition, then\r
2116 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2117\r
2118 @param[in] Operand Operand to be converted to new type\r
2119 @param[out] Result Pointer to the result of conversion\r
2120\r
2121 @retval RETURN_SUCCESS Successful conversion\r
2122 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2123 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2124**/\r
2125RETURN_STATUS\r
2126EFIAPI\r
2127SafeInt64ToChar8 (\r
2128 IN INT64 Operand,\r
2129 OUT CHAR8 *Result\r
2130 )\r
2131{\r
2132 RETURN_STATUS Status;\r
2133\r
2134 if (Result == NULL) {\r
2135 return RETURN_INVALID_PARAMETER;\r
2136 }\r
2137\r
2138 if ((Operand >= 0) && (Operand <= MAX_INT8)) {\r
2139 *Result = (CHAR8)Operand;\r
2140 Status = RETURN_SUCCESS;\r
2141 } else {\r
2142 *Result = CHAR8_ERROR;\r
2143 Status = RETURN_BUFFER_TOO_SMALL;\r
2144 }\r
2145\r
2146 return Status;\r
2147}\r
2148\r
2149/**\r
2150 INT64 -> UINT8 conversion\r
2151\r
2152 Converts the value specified by Operand to a value specified by Result type\r
2153 and stores the converted value into the caller allocated output buffer\r
2154 specified by Result. The caller must pass in a Result buffer that is at\r
2155 least as large as the Result type.\r
2156\r
2157 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2158\r
2159 If the conversion results in an overflow or an underflow condition, then\r
2160 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2161\r
2162 @param[in] Operand Operand to be converted to new type\r
2163 @param[out] Result Pointer to the result of conversion\r
2164\r
2165 @retval RETURN_SUCCESS Successful conversion\r
2166 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2167 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2168**/\r
2169RETURN_STATUS\r
2170EFIAPI\r
2171SafeInt64ToUint8 (\r
2172 IN INT64 Operand,\r
2173 OUT UINT8 *Result\r
2174 )\r
2175{\r
2176 RETURN_STATUS Status;\r
2177\r
2178 if (Result == NULL) {\r
2179 return RETURN_INVALID_PARAMETER;\r
2180 }\r
2181\r
2182 if ((Operand >= 0) && (Operand <= MAX_UINT8)) {\r
2183 *Result = (UINT8)Operand;\r
2184 Status = RETURN_SUCCESS;\r
2185 } else {\r
2186 *Result = UINT8_ERROR;\r
2187 Status = RETURN_BUFFER_TOO_SMALL;\r
2188 }\r
2189\r
2190 return Status;\r
2191}\r
2192\r
2193/**\r
2194 INT64 -> INT16 conversion\r
2195\r
2196 Converts the value specified by Operand to a value specified by Result type\r
2197 and stores the converted value into the caller allocated output buffer\r
2198 specified by Result. The caller must pass in a Result buffer that is at\r
2199 least as large as the Result type.\r
2200\r
2201 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2202\r
2203 If the conversion results in an overflow or an underflow condition, then\r
2204 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2205\r
2206 @param[in] Operand Operand to be converted to new type\r
2207 @param[out] Result Pointer to the result of conversion\r
2208\r
2209 @retval RETURN_SUCCESS Successful conversion\r
2210 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2211 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2212**/\r
2213RETURN_STATUS\r
2214EFIAPI\r
2215SafeInt64ToInt16 (\r
2216 IN INT64 Operand,\r
2217 OUT INT16 *Result\r
2218 )\r
2219{\r
2220 RETURN_STATUS Status;\r
2221\r
2222 if (Result == NULL) {\r
2223 return RETURN_INVALID_PARAMETER;\r
2224 }\r
2225\r
2226 if ((Operand >= MIN_INT16) && (Operand <= MAX_INT16)) {\r
2227 *Result = (INT16)Operand;\r
2228 Status = RETURN_SUCCESS;\r
2229 } else {\r
2230 *Result = INT16_ERROR;\r
2231 Status = RETURN_BUFFER_TOO_SMALL;\r
2232 }\r
2233\r
2234 return Status;\r
2235}\r
2236\r
2237/**\r
2238 INT64 -> UINT16 conversion\r
2239\r
2240 Converts the value specified by Operand to a value specified by Result type\r
2241 and stores the converted value into the caller allocated output buffer\r
2242 specified by Result. The caller must pass in a Result buffer that is at\r
2243 least as large as the Result type.\r
2244\r
2245 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2246\r
2247 If the conversion results in an overflow or an underflow condition, then\r
2248 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2249\r
2250 @param[in] Operand Operand to be converted to new type\r
2251 @param[out] Result Pointer to the result of conversion\r
2252\r
2253 @retval RETURN_SUCCESS Successful conversion\r
2254 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2255 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2256**/\r
2257RETURN_STATUS\r
2258EFIAPI\r
2259SafeInt64ToUint16 (\r
2260 IN INT64 Operand,\r
2261 OUT UINT16 *Result\r
2262 )\r
2263{\r
2264 RETURN_STATUS Status;\r
2265\r
2266 if (Result == NULL) {\r
2267 return RETURN_INVALID_PARAMETER;\r
2268 }\r
2269\r
2270 if ((Operand >= 0) && (Operand <= MAX_UINT16)) {\r
2271 *Result = (UINT16)Operand;\r
2272 Status = RETURN_SUCCESS;\r
2273 } else {\r
2274 *Result = UINT16_ERROR;\r
2275 Status = RETURN_BUFFER_TOO_SMALL;\r
2276 }\r
2277\r
2278 return Status;\r
2279}\r
2280\r
2281/**\r
2282 INT64 -> INT32 conversion\r
2283\r
2284 Converts the value specified by Operand to a value specified by Result type\r
2285 and stores the converted value into the caller allocated output buffer\r
2286 specified by Result. The caller must pass in a Result buffer that is at\r
2287 least as large as the Result type.\r
2288\r
2289 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2290\r
2291 If the conversion results in an overflow or an underflow condition, then\r
2292 Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2293\r
2294 @param[in] Operand Operand to be converted to new type\r
2295 @param[out] Result Pointer to the result of conversion\r
2296\r
2297 @retval RETURN_SUCCESS Successful conversion\r
2298 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2299 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2300**/\r
2301RETURN_STATUS\r
2302EFIAPI\r
2303SafeInt64ToInt32 (\r
2304 IN INT64 Operand,\r
2305 OUT INT32 *Result\r
2306 )\r
2307{\r
2308 RETURN_STATUS Status;\r
2309\r
2310 if (Result == NULL) {\r
2311 return RETURN_INVALID_PARAMETER;\r
2312 }\r
2313\r
2314 if ((Operand >= MIN_INT32) && (Operand <= MAX_INT32)) {\r
2315 *Result = (INT32)Operand;\r
2316 Status = RETURN_SUCCESS;\r
2317 } else {\r
2318 *Result = INT32_ERROR;\r
2319 Status = RETURN_BUFFER_TOO_SMALL;\r
2320 }\r
2321\r
2322 return Status;\r
2323}\r
2324\r
2325/**\r
2326 INT64 -> UINT32 conversion\r
2327\r
2328 Converts the value specified by Operand to a value specified by Result type\r
2329 and stores the converted value into the caller allocated output buffer\r
2330 specified by Result. The caller must pass in a Result buffer that is at\r
2331 least as large as the Result type.\r
2332\r
2333 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2334\r
2335 If the conversion results in an overflow or an underflow condition, then\r
2336 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2337\r
2338 @param[in] Operand Operand to be converted to new type\r
2339 @param[out] Result Pointer to the result of conversion\r
2340\r
2341 @retval RETURN_SUCCESS Successful conversion\r
2342 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2343 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2344**/\r
2345RETURN_STATUS\r
2346EFIAPI\r
2347SafeInt64ToUint32 (\r
2348 IN INT64 Operand,\r
2349 OUT UINT32 *Result\r
2350 )\r
2351{\r
2352 RETURN_STATUS Status;\r
2353\r
2354 if (Result == NULL) {\r
2355 return RETURN_INVALID_PARAMETER;\r
2356 }\r
2357\r
2358 if ((Operand >= 0) && (Operand <= MAX_UINT32)) {\r
2359 *Result = (UINT32)Operand;\r
2360 Status = RETURN_SUCCESS;\r
2361 } else {\r
2362 *Result = UINT32_ERROR;\r
2363 Status = RETURN_BUFFER_TOO_SMALL;\r
2364 }\r
2365\r
2366 return Status;\r
2367}\r
2368\r
2369/**\r
2370 INT64 -> UINT64 conversion\r
2371\r
2372 Converts the value specified by Operand to a value specified by Result type\r
2373 and stores the converted value into the caller allocated output buffer\r
2374 specified by Result. The caller must pass in a Result buffer that is at\r
2375 least as large as the Result type.\r
2376\r
2377 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2378\r
2379 If the conversion results in an overflow or an underflow condition, then\r
2380 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2381\r
2382 @param[in] Operand Operand to be converted to new type\r
2383 @param[out] Result Pointer to the result of conversion\r
2384\r
2385 @retval RETURN_SUCCESS Successful conversion\r
2386 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2387 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2388**/\r
2389RETURN_STATUS\r
2390EFIAPI\r
2391SafeInt64ToUint64 (\r
2392 IN INT64 Operand,\r
2393 OUT UINT64 *Result\r
2394 )\r
2395{\r
2396 RETURN_STATUS Status;\r
2397\r
2398 if (Result == NULL) {\r
2399 return RETURN_INVALID_PARAMETER;\r
2400 }\r
2401\r
2402 if (Operand >= 0) {\r
2403 *Result = (UINT64)Operand;\r
2404 Status = RETURN_SUCCESS;\r
2405 } else {\r
2406 *Result = UINT64_ERROR;\r
2407 Status = RETURN_BUFFER_TOO_SMALL;\r
2408 }\r
2409\r
2410 return Status;\r
2411}\r
2412\r
2413/**\r
2414 UINT64 -> INT8 conversion\r
2415\r
2416 Converts the value specified by Operand to a value specified by Result type\r
2417 and stores the converted value into the caller allocated output buffer\r
2418 specified by Result. The caller must pass in a Result buffer that is at\r
2419 least as large as the Result type.\r
2420\r
2421 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2422\r
2423 If the conversion results in an overflow or an underflow condition, then\r
2424 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2425\r
2426 @param[in] Operand Operand to be converted to new type\r
2427 @param[out] Result Pointer to the result of conversion\r
2428\r
2429 @retval RETURN_SUCCESS Successful conversion\r
2430 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2431 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2432**/\r
2433RETURN_STATUS\r
2434EFIAPI\r
2435SafeUint64ToInt8 (\r
2436 IN UINT64 Operand,\r
2437 OUT INT8 *Result\r
2438 )\r
2439{\r
2440 RETURN_STATUS Status;\r
2441\r
2442 if (Result == NULL) {\r
2443 return RETURN_INVALID_PARAMETER;\r
2444 }\r
2445\r
2446 if (Operand <= MAX_INT8) {\r
2447 *Result = (INT8)Operand;\r
2448 Status = RETURN_SUCCESS;\r
2449 } else {\r
2450 *Result = INT8_ERROR;\r
2451 Status = RETURN_BUFFER_TOO_SMALL;\r
2452 }\r
2453\r
2454 return Status;\r
2455}\r
2456\r
2457/**\r
2458 UINT64 -> CHAR8 conversion\r
2459\r
2460 Converts the value specified by Operand to a value specified by Result type\r
2461 and stores the converted value into the caller allocated output buffer\r
2462 specified by Result. The caller must pass in a Result buffer that is at\r
2463 least as large as the Result type.\r
2464\r
2465 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2466\r
2467 If the conversion results in an overflow or an underflow condition, then\r
2468 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2469\r
2470 @param[in] Operand Operand to be converted to new type\r
2471 @param[out] Result Pointer to the result of conversion\r
2472\r
2473 @retval RETURN_SUCCESS Successful conversion\r
2474 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2475 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2476**/\r
2477RETURN_STATUS\r
2478EFIAPI\r
2479SafeUint64ToChar8 (\r
2480 IN UINT64 Operand,\r
2481 OUT CHAR8 *Result\r
2482 )\r
2483{\r
2484 RETURN_STATUS Status;\r
2485\r
2486 if (Result == NULL) {\r
2487 return RETURN_INVALID_PARAMETER;\r
2488 }\r
2489\r
2490 if (Operand <= MAX_INT8) {\r
2491 *Result = (INT8)Operand;\r
2492 Status = RETURN_SUCCESS;\r
2493 } else {\r
2494 *Result = CHAR8_ERROR;\r
2495 Status = RETURN_BUFFER_TOO_SMALL;\r
2496 }\r
2497\r
2498 return Status;\r
2499}\r
2500\r
2501/**\r
2502 UINT64 -> UINT8 conversion\r
2503\r
2504 Converts the value specified by Operand to a value specified by Result type\r
2505 and stores the converted value into the caller allocated output buffer\r
2506 specified by Result. The caller must pass in a Result buffer that is at\r
2507 least as large as the Result type.\r
2508\r
2509 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2510\r
2511 If the conversion results in an overflow or an underflow condition, then\r
2512 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2513\r
2514 @param[in] Operand Operand to be converted to new type\r
2515 @param[out] Result Pointer to the result of conversion\r
2516\r
2517 @retval RETURN_SUCCESS Successful conversion\r
2518 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2519 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2520**/\r
2521RETURN_STATUS\r
2522EFIAPI\r
2523SafeUint64ToUint8 (\r
2524 IN UINT64 Operand,\r
2525 OUT UINT8 *Result\r
2526 )\r
2527{\r
2528 RETURN_STATUS Status;\r
2529\r
2530 if (Result == NULL) {\r
2531 return RETURN_INVALID_PARAMETER;\r
2532 }\r
2533\r
2534 if (Operand <= MAX_UINT8) {\r
2535 *Result = (UINT8)Operand;\r
2536 Status = RETURN_SUCCESS;\r
2537 } else {\r
2538 *Result = UINT8_ERROR;\r
2539 Status = RETURN_BUFFER_TOO_SMALL;\r
2540 }\r
2541\r
2542 return Status;\r
2543}\r
2544\r
2545/**\r
2546 UINT64 -> INT16 conversion\r
2547\r
2548 Converts the value specified by Operand to a value specified by Result type\r
2549 and stores the converted value into the caller allocated output buffer\r
2550 specified by Result. The caller must pass in a Result buffer that is at\r
2551 least as large as the Result type.\r
2552\r
2553 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2554\r
2555 If the conversion results in an overflow or an underflow condition, then\r
2556 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2557\r
2558 @param[in] Operand Operand to be converted to new type\r
2559 @param[out] Result Pointer to the result of conversion\r
2560\r
2561 @retval RETURN_SUCCESS Successful conversion\r
2562 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2563 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2564**/\r
2565RETURN_STATUS\r
2566EFIAPI\r
2567SafeUint64ToInt16 (\r
2568 IN UINT64 Operand,\r
2569 OUT INT16 *Result\r
2570 )\r
2571{\r
2572 RETURN_STATUS Status;\r
2573\r
2574 if (Result == NULL) {\r
2575 return RETURN_INVALID_PARAMETER;\r
2576 }\r
2577\r
2578 if (Operand <= MAX_INT16) {\r
2579 *Result = (INT16)Operand;\r
2580 Status = RETURN_SUCCESS;\r
2581 } else {\r
2582 *Result = INT16_ERROR;\r
2583 Status = RETURN_BUFFER_TOO_SMALL;\r
2584 }\r
2585\r
2586 return Status;\r
2587}\r
2588\r
2589/**\r
2590 UINT64 -> UINT16 conversion\r
2591\r
2592 Converts the value specified by Operand to a value specified by Result type\r
2593 and stores the converted value into the caller allocated output buffer\r
2594 specified by Result. The caller must pass in a Result buffer that is at\r
2595 least as large as the Result type.\r
2596\r
2597 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2598\r
2599 If the conversion results in an overflow or an underflow condition, then\r
2600 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2601\r
2602 @param[in] Operand Operand to be converted to new type\r
2603 @param[out] Result Pointer to the result of conversion\r
2604\r
2605 @retval RETURN_SUCCESS Successful conversion\r
2606 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2607 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2608**/\r
2609RETURN_STATUS\r
2610EFIAPI\r
2611SafeUint64ToUint16 (\r
2612 IN UINT64 Operand,\r
2613 OUT UINT16 *Result\r
2614 )\r
2615{\r
2616 RETURN_STATUS Status;\r
2617\r
2618 if (Result == NULL) {\r
2619 return RETURN_INVALID_PARAMETER;\r
2620 }\r
2621\r
2622 if (Operand <= MAX_UINT16) {\r
2623 *Result = (UINT16)Operand;\r
2624 Status = RETURN_SUCCESS;\r
2625 } else {\r
2626 *Result = UINT16_ERROR;\r
2627 Status = RETURN_BUFFER_TOO_SMALL;\r
2628 }\r
2629\r
2630 return Status;\r
2631}\r
2632\r
2633/**\r
2634 UINT64 -> INT32 conversion\r
2635\r
2636 Converts the value specified by Operand to a value specified by Result type\r
2637 and stores the converted value into the caller allocated output buffer\r
2638 specified by Result. The caller must pass in a Result buffer that is at\r
2639 least as large as the Result type.\r
2640\r
2641 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2642\r
2643 If the conversion results in an overflow or an underflow condition, then\r
2644 Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2645\r
2646 @param[in] Operand Operand to be converted to new type\r
2647 @param[out] Result Pointer to the result of conversion\r
2648\r
2649 @retval RETURN_SUCCESS Successful conversion\r
2650 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2651 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2652**/\r
2653RETURN_STATUS\r
2654EFIAPI\r
2655SafeUint64ToInt32 (\r
2656 IN UINT64 Operand,\r
2657 OUT INT32 *Result\r
2658 )\r
2659{\r
2660 RETURN_STATUS Status;\r
2661\r
2662 if (Result == NULL) {\r
2663 return RETURN_INVALID_PARAMETER;\r
2664 }\r
2665\r
2666 if (Operand <= MAX_INT32) {\r
2667 *Result = (INT32)Operand;\r
2668 Status = RETURN_SUCCESS;\r
2669 } else {\r
2670 *Result = INT32_ERROR;\r
2671 Status = RETURN_BUFFER_TOO_SMALL;\r
2672 }\r
2673\r
2674 return Status;\r
2675}\r
2676\r
2677/**\r
2678 UINT64 -> UINT32 conversion\r
2679\r
2680 Converts the value specified by Operand to a value specified by Result type\r
2681 and stores the converted value into the caller allocated output buffer\r
2682 specified by Result. The caller must pass in a Result buffer that is at\r
2683 least as large as the Result type.\r
2684\r
2685 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2686\r
2687 If the conversion results in an overflow or an underflow condition, then\r
2688 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2689\r
2690 @param[in] Operand Operand to be converted to new type\r
2691 @param[out] Result Pointer to the result of conversion\r
2692\r
2693 @retval RETURN_SUCCESS Successful conversion\r
2694 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2695 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2696**/\r
2697RETURN_STATUS\r
2698EFIAPI\r
2699SafeUint64ToUint32 (\r
2700 IN UINT64 Operand,\r
2701 OUT UINT32 *Result\r
2702 )\r
2703{\r
2704 RETURN_STATUS Status;\r
2705\r
2706 if (Result == NULL) {\r
2707 return RETURN_INVALID_PARAMETER;\r
2708 }\r
2709\r
2710 if (Operand <= MAX_UINT32) {\r
2711 *Result = (UINT32)Operand;\r
2712 Status = RETURN_SUCCESS;\r
2713 } else {\r
2714 *Result = UINT32_ERROR;\r
2715 Status = RETURN_BUFFER_TOO_SMALL;\r
2716 }\r
2717\r
2718 return Status;\r
2719}\r
2720\r
2721/**\r
2722 UINT64 -> INTN conversion\r
2723\r
2724 Converts the value specified by Operand to a value specified by Result type\r
2725 and stores the converted value into the caller allocated output buffer\r
2726 specified by Result. The caller must pass in a Result buffer that is at\r
2727 least as large as the Result type.\r
2728\r
2729 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2730\r
2731 If the conversion results in an overflow or an underflow condition, then\r
2732 Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2733\r
2734 @param[in] Operand Operand to be converted to new type\r
2735 @param[out] Result Pointer to the result of conversion\r
2736\r
2737 @retval RETURN_SUCCESS Successful conversion\r
2738 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2739 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2740**/\r
2741RETURN_STATUS\r
2742EFIAPI\r
2743SafeUint64ToIntn (\r
2744 IN UINT64 Operand,\r
2745 OUT INTN *Result\r
2746 )\r
2747{\r
2748 RETURN_STATUS Status;\r
2749\r
2750 if (Result == NULL) {\r
2751 return RETURN_INVALID_PARAMETER;\r
2752 }\r
2753\r
2754 if (Operand <= MAX_INTN) {\r
2755 *Result = (INTN)Operand;\r
2756 Status = RETURN_SUCCESS;\r
2757 } else {\r
2758 *Result = INTN_ERROR;\r
2759 Status = RETURN_BUFFER_TOO_SMALL;\r
2760 }\r
2761\r
2762 return Status;\r
2763}\r
2764\r
2765/**\r
2766 UINT64 -> INT64 conversion\r
2767\r
2768 Converts the value specified by Operand to a value specified by Result type\r
2769 and stores the converted value into the caller allocated output buffer\r
2770 specified by Result. The caller must pass in a Result buffer that is at\r
2771 least as large as the Result type.\r
2772\r
2773 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2774\r
2775 If the conversion results in an overflow or an underflow condition, then\r
2776 Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2777\r
2778 @param[in] Operand Operand to be converted to new type\r
2779 @param[out] Result Pointer to the result of conversion\r
2780\r
2781 @retval RETURN_SUCCESS Successful conversion\r
2782 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2783 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2784**/\r
2785RETURN_STATUS\r
2786EFIAPI\r
2787SafeUint64ToInt64 (\r
2788 IN UINT64 Operand,\r
2789 OUT INT64 *Result\r
2790 )\r
2791{\r
2792 RETURN_STATUS Status;\r
2793\r
2794 if (Result == NULL) {\r
2795 return RETURN_INVALID_PARAMETER;\r
2796 }\r
2797\r
2798 if (Operand <= MAX_INT64) {\r
2799 *Result = (INT64)Operand;\r
2800 Status = RETURN_SUCCESS;\r
2801 } else {\r
2802 *Result = INT64_ERROR;\r
2803 Status = RETURN_BUFFER_TOO_SMALL;\r
2804 }\r
2805\r
2806 return Status;\r
2807}\r
2808\r
2809//\r
2810// Addition functions\r
2811//\r
2812\r
2813/**\r
2814 UINT8 addition\r
2815\r
2816 Performs the requested operation using the input parameters into a value\r
2817 specified by Result type and stores the converted value into the caller\r
2818 allocated output buffer specified by Result. The caller must pass in a\r
2819 Result buffer that is at least as large as the Result type.\r
2820\r
2821 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2822\r
2823 If the requested operation results in an overflow or an underflow condition,\r
2824 then Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2825\r
2826 @param[in] Augend A number to which addend will be added\r
2827 @param[in] Addend A number to be added to another\r
2828 @param[out] Result Pointer to the result of addition\r
2829\r
2830 @retval RETURN_SUCCESS Successful addition\r
2831 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2832 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2833**/\r
2834RETURN_STATUS\r
2835EFIAPI\r
2836SafeUint8Add (\r
2837 IN UINT8 Augend,\r
2838 IN UINT8 Addend,\r
2839 OUT UINT8 *Result\r
2840 )\r
2841{\r
2842 RETURN_STATUS Status;\r
2843\r
2844 if (Result == NULL) {\r
2845 return RETURN_INVALID_PARAMETER;\r
2846 }\r
2847\r
2848 if (((UINT8)(Augend + Addend)) >= Augend) {\r
2849 *Result = (UINT8)(Augend + Addend);\r
2850 Status = RETURN_SUCCESS;\r
2851 } else {\r
2852 *Result = UINT8_ERROR;\r
2853 Status = RETURN_BUFFER_TOO_SMALL;\r
2854 }\r
2855\r
2856 return Status;\r
2857}\r
2858\r
2859/**\r
2860 UINT16 addition\r
2861\r
2862 Performs the requested operation using the input parameters into a value\r
2863 specified by Result type and stores the converted value into the caller\r
2864 allocated output buffer specified by Result. The caller must pass in a\r
2865 Result buffer that is at least as large as the Result type.\r
2866\r
2867 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2868\r
2869 If the requested operation results in an overflow or an underflow condition,\r
2870 then Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2871\r
2872 @param[in] Augend A number to which addend will be added\r
2873 @param[in] Addend A number to be added to another\r
2874 @param[out] Result Pointer to the result of addition\r
2875\r
2876 @retval RETURN_SUCCESS Successful addition\r
2877 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2878 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2879**/\r
2880RETURN_STATUS\r
2881EFIAPI\r
2882SafeUint16Add (\r
2883 IN UINT16 Augend,\r
2884 IN UINT16 Addend,\r
2885 OUT UINT16 *Result\r
2886 )\r
2887{\r
2888 RETURN_STATUS Status;\r
2889\r
2890 if (Result == NULL) {\r
2891 return RETURN_INVALID_PARAMETER;\r
2892 }\r
2893\r
2894 if (((UINT16)(Augend + Addend)) >= Augend) {\r
2895 *Result = (UINT16)(Augend + Addend);\r
2896 Status = RETURN_SUCCESS;\r
2897 } else {\r
2898 *Result = UINT16_ERROR;\r
2899 Status = RETURN_BUFFER_TOO_SMALL;\r
2900 }\r
2901\r
2902 return Status;\r
2903}\r
2904\r
2905/**\r
2906 UINT32 addition\r
2907\r
2908 Performs the requested operation using the input parameters into a value\r
2909 specified by Result type and stores the converted value into the caller\r
2910 allocated output buffer specified by Result. The caller must pass in a\r
2911 Result buffer that is at least as large as the Result type.\r
2912\r
2913 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2914\r
2915 If the requested operation results in an overflow or an underflow condition,\r
2916 then Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2917\r
2918 @param[in] Augend A number to which addend will be added\r
2919 @param[in] Addend A number to be added to another\r
2920 @param[out] Result Pointer to the result of addition\r
2921\r
2922 @retval RETURN_SUCCESS Successful addition\r
2923 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2924 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2925**/\r
2926RETURN_STATUS\r
2927EFIAPI\r
2928SafeUint32Add (\r
2929 IN UINT32 Augend,\r
2930 IN UINT32 Addend,\r
2931 OUT UINT32 *Result\r
2932 )\r
2933{\r
2934 RETURN_STATUS Status;\r
2935\r
2936 if (Result == NULL) {\r
2937 return RETURN_INVALID_PARAMETER;\r
2938 }\r
2939\r
2940 if ((Augend + Addend) >= Augend) {\r
2941 *Result = (Augend + Addend);\r
2942 Status = RETURN_SUCCESS;\r
2943 } else {\r
2944 *Result = UINT32_ERROR;\r
2945 Status = RETURN_BUFFER_TOO_SMALL;\r
2946 }\r
2947\r
2948 return Status;\r
2949}\r
2950\r
2951/**\r
2952 UINT64 addition\r
2953\r
2954 Performs the requested operation using the input parameters into a value\r
2955 specified by Result type and stores the converted value into the caller\r
2956 allocated output buffer specified by Result. The caller must pass in a\r
2957 Result buffer that is at least as large as the Result type.\r
2958\r
2959 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
2960\r
2961 If the requested operation results in an overflow or an underflow condition,\r
2962 then Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
2963\r
2964 @param[in] Augend A number to which addend will be added\r
2965 @param[in] Addend A number to be added to another\r
2966 @param[out] Result Pointer to the result of addition\r
2967\r
2968 @retval RETURN_SUCCESS Successful addition\r
2969 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
2970 @retval RETURN_INVALID_PARAMETER Result is NULL\r
2971**/\r
2972RETURN_STATUS\r
2973EFIAPI\r
2974SafeUint64Add (\r
2975 IN UINT64 Augend,\r
2976 IN UINT64 Addend,\r
2977 OUT UINT64 *Result\r
2978 )\r
2979{\r
2980 RETURN_STATUS Status;\r
2981\r
2982 if (Result == NULL) {\r
2983 return RETURN_INVALID_PARAMETER;\r
2984 }\r
2985\r
2986 if ((Augend + Addend) >= Augend) {\r
2987 *Result = (Augend + Addend);\r
2988 Status = RETURN_SUCCESS;\r
2989 } else {\r
2990 *Result = UINT64_ERROR;\r
2991 Status = RETURN_BUFFER_TOO_SMALL;\r
2992 }\r
2993\r
2994 return Status;\r
2995}\r
2996\r
2997//\r
2998// Subtraction functions\r
2999//\r
3000\r
3001/**\r
3002 UINT8 subtraction\r
3003\r
3004 Performs the requested operation using the input parameters into a value\r
3005 specified by Result type and stores the converted value into the caller\r
3006 allocated output buffer specified by Result. The caller must pass in a\r
3007 Result buffer that is at least as large as the Result type.\r
3008\r
3009 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3010\r
3011 If the requested operation results in an overflow or an underflow condition,\r
3012 then Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3013\r
3014 @param[in] Minuend A number from which another is to be subtracted.\r
3015 @param[in] Subtrahend A number to be subtracted from another\r
3016 @param[out] Result Pointer to the result of subtraction\r
3017\r
3018 @retval RETURN_SUCCESS Successful subtraction\r
3019 @retval RETURN_BUFFER_TOO_SMALL Underflow\r
3020 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3021**/\r
3022RETURN_STATUS\r
3023EFIAPI\r
3024SafeUint8Sub (\r
3025 IN UINT8 Minuend,\r
3026 IN UINT8 Subtrahend,\r
3027 OUT UINT8 *Result\r
3028 )\r
3029{\r
3030 RETURN_STATUS Status;\r
3031\r
3032 if (Result == NULL) {\r
3033 return RETURN_INVALID_PARAMETER;\r
3034 }\r
3035\r
3036 if (Minuend >= Subtrahend) {\r
3037 *Result = (UINT8)(Minuend - Subtrahend);\r
3038 Status = RETURN_SUCCESS;\r
3039 } else {\r
3040 *Result = UINT8_ERROR;\r
3041 Status = RETURN_BUFFER_TOO_SMALL;\r
3042 }\r
3043\r
3044 return Status;\r
3045}\r
3046\r
3047/**\r
3048 UINT16 subtraction\r
3049\r
3050 Performs the requested operation using the input parameters into a value\r
3051 specified by Result type and stores the converted value into the caller\r
3052 allocated output buffer specified by Result. The caller must pass in a\r
3053 Result buffer that is at least as large as the Result type.\r
3054\r
3055 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3056\r
3057 If the requested operation results in an overflow or an underflow condition,\r
3058 then Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3059\r
3060 @param[in] Minuend A number from which another is to be subtracted.\r
3061 @param[in] Subtrahend A number to be subtracted from another\r
3062 @param[out] Result Pointer to the result of subtraction\r
3063\r
3064 @retval RETURN_SUCCESS Successful subtraction\r
3065 @retval RETURN_BUFFER_TOO_SMALL Underflow\r
3066 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3067**/\r
3068RETURN_STATUS\r
3069EFIAPI\r
3070SafeUint16Sub (\r
3071 IN UINT16 Minuend,\r
3072 IN UINT16 Subtrahend,\r
3073 OUT UINT16 *Result\r
3074 )\r
3075{\r
3076 RETURN_STATUS Status;\r
3077\r
3078 if (Result == NULL) {\r
3079 return RETURN_INVALID_PARAMETER;\r
3080 }\r
3081\r
3082 if (Minuend >= Subtrahend) {\r
3083 *Result = (UINT16)(Minuend - Subtrahend);\r
3084 Status = RETURN_SUCCESS;\r
3085 } else {\r
3086 *Result = UINT16_ERROR;\r
3087 Status = RETURN_BUFFER_TOO_SMALL;\r
3088 }\r
3089\r
3090 return Status;\r
3091}\r
3092\r
3093/**\r
3094 UINT32 subtraction\r
3095\r
3096 Performs the requested operation using the input parameters into a value\r
3097 specified by Result type and stores the converted value into the caller\r
3098 allocated output buffer specified by Result. The caller must pass in a\r
3099 Result buffer that is at least as large as the Result type.\r
3100\r
3101 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3102\r
3103 If the requested operation results in an overflow or an underflow condition,\r
3104 then Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3105\r
3106 @param[in] Minuend A number from which another is to be subtracted.\r
3107 @param[in] Subtrahend A number to be subtracted from another\r
3108 @param[out] Result Pointer to the result of subtraction\r
3109\r
3110 @retval RETURN_SUCCESS Successful subtraction\r
3111 @retval RETURN_BUFFER_TOO_SMALL Underflow\r
3112 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3113**/\r
3114RETURN_STATUS\r
3115EFIAPI\r
3116SafeUint32Sub (\r
3117 IN UINT32 Minuend,\r
3118 IN UINT32 Subtrahend,\r
3119 OUT UINT32 *Result\r
3120 )\r
3121{\r
3122 RETURN_STATUS Status;\r
3123\r
3124 if (Result == NULL) {\r
3125 return RETURN_INVALID_PARAMETER;\r
3126 }\r
3127\r
3128 if (Minuend >= Subtrahend) {\r
3129 *Result = (Minuend - Subtrahend);\r
3130 Status = RETURN_SUCCESS;\r
3131 } else {\r
3132 *Result = UINT32_ERROR;\r
3133 Status = RETURN_BUFFER_TOO_SMALL;\r
3134 }\r
3135\r
3136 return Status;\r
3137}\r
3138\r
3139/**\r
3140 UINT64 subtraction\r
3141\r
3142 Performs the requested operation using the input parameters into a value\r
3143 specified by Result type and stores the converted value into the caller\r
3144 allocated output buffer specified by Result. The caller must pass in a\r
3145 Result buffer that is at least as large as the Result type.\r
3146\r
3147 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3148\r
3149 If the requested operation results in an overflow or an underflow condition,\r
3150 then Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3151\r
3152 @param[in] Minuend A number from which another is to be subtracted.\r
3153 @param[in] Subtrahend A number to be subtracted from another\r
3154 @param[out] Result Pointer to the result of subtraction\r
3155\r
3156 @retval RETURN_SUCCESS Successful subtraction\r
3157 @retval RETURN_BUFFER_TOO_SMALL Underflow\r
3158 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3159**/\r
3160RETURN_STATUS\r
3161EFIAPI\r
3162SafeUint64Sub (\r
3163 IN UINT64 Minuend,\r
3164 IN UINT64 Subtrahend,\r
3165 OUT UINT64 *Result\r
3166 )\r
3167{\r
3168 RETURN_STATUS Status;\r
3169\r
3170 if (Result == NULL) {\r
3171 return RETURN_INVALID_PARAMETER;\r
3172 }\r
3173\r
3174 if (Minuend >= Subtrahend) {\r
3175 *Result = (Minuend - Subtrahend);\r
3176 Status = RETURN_SUCCESS;\r
3177 } else {\r
3178 *Result = UINT64_ERROR;\r
3179 Status = RETURN_BUFFER_TOO_SMALL;\r
3180 }\r
3181\r
3182 return Status;\r
3183}\r
3184\r
3185//\r
3186// Multiplication functions\r
3187//\r
3188\r
3189/**\r
3190 UINT8 multiplication\r
3191\r
3192 Performs the requested operation using the input parameters into a value\r
3193 specified by Result type and stores the converted value into the caller\r
3194 allocated output buffer specified by Result. The caller must pass in a\r
3195 Result buffer that is at least as large as the Result type.\r
3196\r
3197 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3198\r
3199 If the requested operation results in an overflow or an underflow condition,\r
3200 then Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3201\r
3202 @param[in] Multiplicand A number that is to be multiplied by another\r
3203 @param[in] Multiplier A number by which the multiplicand is to be multiplied\r
3204 @param[out] Result Pointer to the result of multiplication\r
3205\r
3206 @retval RETURN_SUCCESS Successful multiplication\r
3207 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
3208 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3209**/\r
3210RETURN_STATUS\r
3211EFIAPI\r
3212SafeUint8Mult (\r
3213 IN UINT8 Multiplicand,\r
3214 IN UINT8 Multiplier,\r
3215 OUT UINT8 *Result\r
3216 )\r
3217{\r
3218 UINT32 IntermediateResult;\r
3219\r
3220 IntermediateResult = ((UINT32)Multiplicand) *((UINT32)Multiplier);\r
3221\r
3222 return SafeUint32ToUint8 (IntermediateResult, Result);\r
3223}\r
3224\r
3225/**\r
3226 UINT16 multiplication\r
3227\r
3228 Performs the requested operation using the input parameters into a value\r
3229 specified by Result type and stores the converted value into the caller\r
3230 allocated output buffer specified by Result. The caller must pass in a\r
3231 Result buffer that is at least as large as the Result type.\r
3232\r
3233 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3234\r
3235 If the requested operation results in an overflow or an underflow condition,\r
3236 then Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3237\r
3238 @param[in] Multiplicand A number that is to be multiplied by another\r
3239 @param[in] Multiplier A number by which the multiplicand is to be multiplied\r
3240 @param[out] Result Pointer to the result of multiplication\r
3241\r
3242 @retval RETURN_SUCCESS Successful multiplication\r
3243 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
3244 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3245**/\r
3246RETURN_STATUS\r
3247EFIAPI\r
3248SafeUint16Mult (\r
3249 IN UINT16 Multiplicand,\r
3250 IN UINT16 Multiplier,\r
3251 OUT UINT16 *Result\r
3252 )\r
3253{\r
3254 UINT32 IntermediateResult;\r
3255\r
3256 IntermediateResult = ((UINT32)Multiplicand) *((UINT32)Multiplier);\r
3257\r
3258 return SafeUint32ToUint16 (IntermediateResult, Result);\r
3259}\r
3260\r
3261/**\r
3262 UINT32 multiplication\r
3263\r
3264 Performs the requested operation using the input parameters into a value\r
3265 specified by Result type and stores the converted value into the caller\r
3266 allocated output buffer specified by Result. The caller must pass in a\r
3267 Result buffer that is at least as large as the Result type.\r
3268\r
3269 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3270\r
3271 If the requested operation results in an overflow or an underflow condition,\r
3272 then Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3273\r
3274 @param[in] Multiplicand A number that is to be multiplied by another\r
3275 @param[in] Multiplier A number by which the multiplicand is to be multiplied\r
3276 @param[out] Result Pointer to the result of multiplication\r
3277\r
3278 @retval RETURN_SUCCESS Successful multiplication\r
3279 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
3280 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3281**/\r
3282RETURN_STATUS\r
3283EFIAPI\r
3284SafeUint32Mult (\r
3285 IN UINT32 Multiplicand,\r
3286 IN UINT32 Multiplier,\r
3287 OUT UINT32 *Result\r
3288 )\r
3289{\r
3290 UINT64 IntermediateResult;\r
3291\r
3292 IntermediateResult = ((UINT64) Multiplicand) *((UINT64) Multiplier);\r
3293\r
3294 return SafeUint64ToUint32 (IntermediateResult, Result);\r
3295}\r
3296\r
3297/**\r
3298 UINT64 multiplication\r
3299\r
3300 Performs the requested operation using the input parameters into a value\r
3301 specified by Result type and stores the converted value into the caller\r
3302 allocated output buffer specified by Result. The caller must pass in a\r
3303 Result buffer that is at least as large as the Result type.\r
3304\r
3305 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3306\r
3307 If the requested operation results in an overflow or an underflow condition,\r
3308 then Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3309\r
3310 @param[in] Multiplicand A number that is to be multiplied by another\r
3311 @param[in] Multiplier A number by which the multiplicand is to be multiplied\r
3312 @param[out] Result Pointer to the result of multiplication\r
3313\r
3314 @retval RETURN_SUCCESS Successful multiplication\r
3315 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
3316 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3317**/\r
3318RETURN_STATUS\r
3319EFIAPI\r
3320SafeUint64Mult (\r
3321 IN UINT64 Multiplicand,\r
3322 IN UINT64 Multiplier,\r
3323 OUT UINT64 *Result\r
3324 )\r
3325{\r
3326 RETURN_STATUS Status;\r
3327 UINT32 DwordA;\r
3328 UINT32 DwordB;\r
3329 UINT32 DwordC;\r
3330 UINT32 DwordD;\r
3331 UINT64 ProductAD;\r
3332 UINT64 ProductBC;\r
3333 UINT64 ProductBD;\r
3334 UINT64 UnsignedResult;\r
3335\r
3336 if (Result == NULL) {\r
3337 return RETURN_INVALID_PARAMETER;\r
3338 }\r
3339\r
3340 ProductAD = 0;\r
3341 ProductBC = 0;\r
3342 ProductBD = 0;\r
3343 UnsignedResult = 0;\r
3344 Status = RETURN_BUFFER_TOO_SMALL;\r
3345\r
3346 //\r
3347 // 64x64 into 128 is like 32.32 x 32.32.\r
3348 //\r
3349 // a.b * c.d = a*(c.d) + .b*(c.d) = a*c + a*.d + .b*c + .b*.d\r
3350 // back in non-decimal notation where A=a*2^32 and C=c*2^32:\r
3351 // A*C + A*d + b*C + b*d\r
3352 // So there are four components to add together.\r
3353 // result = (a*c*2^64) + (a*d*2^32) + (b*c*2^32) + (b*d)\r
3354 //\r
3355 // a * c must be 0 or there would be bits in the high 64-bits\r
3356 // a * d must be less than 2^32 or there would be bits in the high 64-bits\r
3357 // b * c must be less than 2^32 or there would be bits in the high 64-bits\r
3358 // then there must be no overflow of the resulting values summed up.\r
3359 //\r
dc9b2a57
DB
3360 DwordA = (UINT32)RShiftU64 (Multiplicand, 32);\r
3361 DwordC = (UINT32)RShiftU64 (Multiplier, 32);\r
d7a09cb8
SB
3362\r
3363 //\r
3364 // common case -- if high dwords are both zero, no chance for overflow\r
3365 //\r
3366 if ((DwordA == 0) && (DwordC == 0)) {\r
3367 DwordB = (UINT32)Multiplicand;\r
3368 DwordD = (UINT32)Multiplier;\r
3369\r
3370 *Result = (((UINT64)DwordB) *(UINT64)DwordD);\r
3371 Status = RETURN_SUCCESS;\r
3372 } else {\r
3373 //\r
3374 // a * c must be 0 or there would be bits set in the high 64-bits\r
3375 //\r
3376 if ((DwordA == 0) ||\r
3377 (DwordC == 0)) {\r
3378 DwordD = (UINT32)Multiplier;\r
3379\r
3380 //\r
3381 // a * d must be less than 2^32 or there would be bits set in the high 64-bits\r
3382 //\r
3383 ProductAD = (((UINT64)DwordA) *(UINT64)DwordD);\r
3384 if ((ProductAD & 0xffffffff00000000) == 0) {\r
3385 DwordB = (UINT32)Multiplicand;\r
3386\r
3387 //\r
3388 // b * c must be less than 2^32 or there would be bits set in the high 64-bits\r
3389 //\r
3390 ProductBC = (((UINT64)DwordB) *(UINT64)DwordC);\r
3391 if ((ProductBC & 0xffffffff00000000) == 0) {\r
3392 //\r
3393 // now sum them all up checking for overflow.\r
3394 // shifting is safe because we already checked for overflow above\r
3395 //\r
dc9b2a57 3396 if (!RETURN_ERROR (SafeUint64Add (LShiftU64 (ProductBC, 32), LShiftU64 (ProductAD, 32), &UnsignedResult))) {\r
d7a09cb8
SB
3397 //\r
3398 // b * d\r
3399 //\r
3400 ProductBD = (((UINT64)DwordB) *(UINT64)DwordD);\r
3401\r
3402 if (!RETURN_ERROR (SafeUint64Add (UnsignedResult, ProductBD, &UnsignedResult))) {\r
3403 *Result = UnsignedResult;\r
3404 Status = RETURN_SUCCESS;\r
3405 }\r
3406 }\r
3407 }\r
3408 }\r
3409 }\r
3410 }\r
3411\r
3412 if (RETURN_ERROR (Status)) {\r
3413 *Result = UINT64_ERROR;\r
3414 }\r
3415 return Status;\r
3416}\r
3417\r
3418//\r
3419// Signed operations\r
3420//\r
3421// Strongly consider using unsigned numbers.\r
3422//\r
3423// Signed numbers are often used where unsigned numbers should be used.\r
3424// For example file sizes and array indices should always be unsigned.\r
3425// Subtracting a larger positive signed number from a smaller positive\r
3426// signed number with SafeInt32Sub will succeed, producing a negative number,\r
3427// that then must not be used as an array index (but can occasionally be\r
3428// used as a pointer index.) Similarly for adding a larger magnitude\r
3429// negative number to a smaller magnitude positive number.\r
3430//\r
3431// This library does not protect you from such errors. It tells you if your\r
3432// integer operations overflowed, not if you are doing the right thing\r
3433// with your non-overflowed integers.\r
3434//\r
3435// Likewise you can overflow a buffer with a non-overflowed unsigned index.\r
3436//\r
3437\r
3438//\r
3439// Signed addition functions\r
3440//\r
3441\r
3442/**\r
3443 INT8 Addition\r
3444\r
3445 Performs the requested operation using the input parameters into a value\r
3446 specified by Result type and stores the converted value into the caller\r
3447 allocated output buffer specified by Result. The caller must pass in a\r
3448 Result buffer that is at least as large as the Result type.\r
3449\r
3450 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3451\r
3452 If the requested operation results in an overflow or an underflow condition,\r
3453 then Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3454\r
3455 @param[in] Augend A number to which addend will be added\r
3456 @param[in] Addend A number to be added to another\r
3457 @param[out] Result Pointer to the result of addition\r
3458\r
3459 @retval RETURN_SUCCESS Successful addition\r
3460 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
3461 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3462**/\r
3463RETURN_STATUS\r
3464EFIAPI\r
3465SafeInt8Add (\r
3466 IN INT8 Augend,\r
3467 IN INT8 Addend,\r
3468 OUT INT8 *Result\r
3469 )\r
3470{\r
3471 return SafeInt32ToInt8 (((INT32)Augend) + ((INT32)Addend), Result);\r
3472}\r
3473\r
3474/**\r
3475 CHAR8 Addition\r
3476\r
3477 Performs the requested operation using the input parameters into a value\r
3478 specified by Result type and stores the converted value into the caller\r
3479 allocated output buffer specified by Result. The caller must pass in a\r
3480 Result buffer that is at least as large as the Result type.\r
3481\r
3482 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3483\r
3484 If the requested operation results in an overflow or an underflow condition,\r
3485 then Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3486\r
3487 @param[in] Augend A number to which addend will be added\r
3488 @param[in] Addend A number to be added to another\r
3489 @param[out] Result Pointer to the result of addition\r
3490\r
3491 @retval RETURN_SUCCESS Successful addition\r
3492 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
3493 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3494**/\r
3495RETURN_STATUS\r
3496EFIAPI\r
3497SafeChar8Add (\r
3498 IN CHAR8 Augend,\r
3499 IN CHAR8 Addend,\r
3500 OUT CHAR8 *Result\r
3501 )\r
3502{\r
3503 INT32 Augend32;\r
3504 INT32 Addend32;\r
3505\r
3506 if (Result == NULL) {\r
3507 return RETURN_INVALID_PARAMETER;\r
3508 }\r
3509\r
3510 Augend32 = (INT32)Augend;\r
3511 Addend32 = (INT32)Addend;\r
3512 if (Augend32 < 0 || Augend32 > MAX_INT8) {\r
3513 *Result = CHAR8_ERROR;\r
3514 return RETURN_BUFFER_TOO_SMALL;\r
3515 }\r
3516 if (Addend32 < 0 || Addend32 > MAX_INT8) {\r
3517 *Result = CHAR8_ERROR;\r
3518 return RETURN_BUFFER_TOO_SMALL;\r
3519 }\r
3520\r
3521 return SafeInt32ToChar8 (Augend32 + Addend32, Result);\r
3522}\r
3523\r
3524/**\r
3525 INT16 Addition\r
3526\r
3527 Performs the requested operation using the input parameters into a value\r
3528 specified by Result type and stores the converted value into the caller\r
3529 allocated output buffer specified by Result. The caller must pass in a\r
3530 Result buffer that is at least as large as the Result type.\r
3531\r
3532 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3533\r
3534 If the requested operation results in an overflow or an underflow condition,\r
3535 then Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3536\r
3537 @param[in] Augend A number to which addend will be added\r
3538 @param[in] Addend A number to be added to another\r
3539 @param[out] Result Pointer to the result of addition\r
3540\r
3541 @retval RETURN_SUCCESS Successful addition\r
3542 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
3543 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3544**/\r
3545RETURN_STATUS\r
3546EFIAPI\r
3547SafeInt16Add (\r
3548 IN INT16 Augend,\r
3549 IN INT16 Addend,\r
3550 OUT INT16 *Result\r
3551 )\r
3552{\r
3553 return SafeInt32ToInt16 (((INT32)Augend) + ((INT32)Addend), Result);\r
3554}\r
3555\r
3556/**\r
3557 INT32 Addition\r
3558\r
3559 Performs the requested operation using the input parameters into a value\r
3560 specified by Result type and stores the converted value into the caller\r
3561 allocated output buffer specified by Result. The caller must pass in a\r
3562 Result buffer that is at least as large as the Result type.\r
3563\r
3564 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3565\r
3566 If the requested operation results in an overflow or an underflow condition,\r
3567 then Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3568\r
3569 @param[in] Augend A number to which addend will be added\r
3570 @param[in] Addend A number to be added to another\r
3571 @param[out] Result Pointer to the result of addition\r
3572\r
3573 @retval RETURN_SUCCESS Successful addition\r
3574 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
3575 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3576**/\r
3577RETURN_STATUS\r
3578EFIAPI\r
3579SafeInt32Add (\r
3580 IN INT32 Augend,\r
3581 IN INT32 Addend,\r
3582 OUT INT32 *Result\r
3583 )\r
3584{\r
3585 return SafeInt64ToInt32 (((INT64)Augend) + ((INT64)Addend), Result);\r
3586}\r
3587\r
3588/**\r
3589 INT64 Addition\r
3590\r
3591 Performs the requested operation using the input parameters into a value\r
3592 specified by Result type and stores the converted value into the caller\r
3593 allocated output buffer specified by Result. The caller must pass in a\r
3594 Result buffer that is at least as large as the Result type.\r
3595\r
3596 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3597\r
3598 If the requested operation results in an overflow or an underflow condition,\r
3599 then Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3600\r
3601 @param[in] Augend A number to which addend will be added\r
3602 @param[in] Addend A number to be added to another\r
3603 @param[out] Result Pointer to the result of addition\r
3604\r
3605 @retval RETURN_SUCCESS Successful addition\r
3606 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
3607 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3608**/\r
3609RETURN_STATUS\r
3610EFIAPI\r
3611SafeInt64Add (\r
3612 IN INT64 Augend,\r
3613 IN INT64 Addend,\r
3614 OUT INT64 *Result\r
3615 )\r
3616{\r
3617 RETURN_STATUS Status;\r
d7a09cb8
SB
3618\r
3619 if (Result == NULL) {\r
3620 return RETURN_INVALID_PARAMETER;\r
3621 }\r
3622\r
d7a09cb8 3623 //\r
41bfaffd
LE
3624 // * An Addend of zero can never cause underflow or overflow.\r
3625 //\r
3626 // * A positive Addend can only cause overflow. The overflow condition is\r
3627 //\r
3628 // (Augend + Addend) > MAX_INT64\r
3629 //\r
3630 // Subtracting Addend from both sides yields\r
3631 //\r
3632 // Augend > (MAX_INT64 - Addend)\r
3633 //\r
3634 // This condition can be coded directly in C because the RHS will neither\r
3635 // underflow nor overflow. That is due to the starting condition:\r
3636 //\r
3637 // 0 < Addend <= MAX_INT64\r
3638 //\r
3639 // Multiplying all three sides by (-1) yields\r
3640 //\r
3641 // 0 > (-Addend) >= (-MAX_INT64)\r
3642 //\r
3643 // Adding MAX_INT64 to all three sides yields\r
3644 //\r
3645 // MAX_INT64 > (MAX_INT64 - Addend) >= 0\r
3646 //\r
3647 // * A negative Addend can only cause underflow. The underflow condition is\r
3648 //\r
3649 // (Augend + Addend) < MIN_INT64\r
3650 //\r
3651 // Subtracting Addend from both sides yields\r
3652 //\r
3653 // Augend < (MIN_INT64 - Addend)\r
3654 //\r
3655 // This condition can be coded directly in C because the RHS will neither\r
3656 // underflow nor overflow. That is due to the starting condition:\r
3657 //\r
3658 // MIN_INT64 <= Addend < 0\r
3659 //\r
3660 // Multiplying all three sides by (-1) yields\r
3661 //\r
3662 // (-MIN_INT64) >= (-Addend) > 0\r
3663 //\r
3664 // Adding MIN_INT64 to all three sides yields\r
3665 //\r
3666 // 0 >= (MIN_INT64 - Addend) > MIN_INT64\r
d7a09cb8 3667 //\r
41bfaffd
LE
3668 if (((Addend > 0) && (Augend > (MAX_INT64 - Addend))) ||\r
3669 ((Addend < 0) && (Augend < (MIN_INT64 - Addend)))) {\r
d7a09cb8
SB
3670 *Result = INT64_ERROR;\r
3671 Status = RETURN_BUFFER_TOO_SMALL;\r
3672 } else {\r
41bfaffd 3673 *Result = Augend + Addend;\r
d7a09cb8
SB
3674 Status = RETURN_SUCCESS;\r
3675 }\r
3676\r
3677 return Status;\r
3678}\r
3679\r
3680//\r
3681// Signed subtraction functions\r
3682//\r
3683\r
3684/**\r
3685 INT8 Subtraction\r
3686\r
3687 Performs the requested operation using the input parameters into a value\r
3688 specified by Result type and stores the converted value into the caller\r
3689 allocated output buffer specified by Result. The caller must pass in a\r
3690 Result buffer that is at least as large as the Result type.\r
3691\r
3692 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3693\r
3694 If the requested operation results in an overflow or an underflow condition,\r
3695 then Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3696\r
3697 @param[in] Minuend A number from which another is to be subtracted.\r
3698 @param[in] Subtrahend A number to be subtracted from another\r
3699 @param[out] Result Pointer to the result of subtraction\r
3700\r
3701 @retval RETURN_SUCCESS Successful subtraction\r
3702 @retval RETURN_BUFFER_TOO_SMALL Underflow\r
3703 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3704**/\r
3705RETURN_STATUS\r
3706EFIAPI\r
3707SafeInt8Sub (\r
3708 IN INT8 Minuend,\r
3709 IN INT8 Subtrahend,\r
3710 OUT INT8 *Result\r
3711 )\r
3712{\r
3713 return SafeInt32ToInt8 (((INT32)Minuend) - ((INT32)Subtrahend), Result);\r
3714}\r
3715\r
3716/**\r
3717 CHAR8 Subtraction\r
3718\r
3719 Performs the requested operation using the input parameters into a value\r
3720 specified by Result type and stores the converted value into the caller\r
3721 allocated output buffer specified by Result. The caller must pass in a\r
3722 Result buffer that is at least as large as the Result type.\r
3723\r
3724 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3725\r
3726 If the requested operation results in an overflow or an underflow condition,\r
3727 then Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3728\r
3729 @param[in] Minuend A number from which another is to be subtracted.\r
3730 @param[in] Subtrahend A number to be subtracted from another\r
3731 @param[out] Result Pointer to the result of subtraction\r
3732\r
3733 @retval RETURN_SUCCESS Successful subtraction\r
3734 @retval RETURN_BUFFER_TOO_SMALL Underflow\r
3735 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3736**/\r
3737RETURN_STATUS\r
3738EFIAPI\r
3739SafeChar8Sub (\r
3740 IN CHAR8 Minuend,\r
3741 IN CHAR8 Subtrahend,\r
3742 OUT CHAR8 *Result\r
3743 )\r
3744{\r
3745 INT32 Minuend32;\r
3746 INT32 Subtrahend32;\r
3747\r
3748 if (Result == NULL) {\r
3749 return RETURN_INVALID_PARAMETER;\r
3750 }\r
3751\r
3752 Minuend32 = (INT32)Minuend;\r
3753 Subtrahend32 = (INT32)Subtrahend;\r
3754 if (Minuend32 < 0 || Minuend32 > MAX_INT8) {\r
3755 *Result = CHAR8_ERROR;\r
3756 return RETURN_BUFFER_TOO_SMALL;\r
3757 }\r
3758 if (Subtrahend32 < 0 || Subtrahend32 > MAX_INT8) {\r
3759 *Result = CHAR8_ERROR;\r
3760 return RETURN_BUFFER_TOO_SMALL;\r
3761 }\r
3762\r
3763 return SafeInt32ToChar8 (Minuend32 - Subtrahend32, Result);\r
3764}\r
3765\r
3766/**\r
3767 INT16 Subtraction\r
3768\r
3769 Performs the requested operation using the input parameters into a value\r
3770 specified by Result type and stores the converted value into the caller\r
3771 allocated output buffer specified by Result. The caller must pass in a\r
3772 Result buffer that is at least as large as the Result type.\r
3773\r
3774 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3775\r
3776 If the requested operation results in an overflow or an underflow condition,\r
3777 then Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3778\r
3779 @param[in] Minuend A number from which another is to be subtracted.\r
3780 @param[in] Subtrahend A number to be subtracted from another\r
3781 @param[out] Result Pointer to the result of subtraction\r
3782\r
3783 @retval RETURN_SUCCESS Successful subtraction\r
3784 @retval RETURN_BUFFER_TOO_SMALL Underflow\r
3785 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3786**/\r
3787RETURN_STATUS\r
3788EFIAPI\r
3789SafeInt16Sub (\r
3790 IN INT16 Minuend,\r
3791 IN INT16 Subtrahend,\r
3792 OUT INT16 *Result\r
3793 )\r
3794{\r
3795 return SafeInt32ToInt16 (((INT32)Minuend) - ((INT32)Subtrahend), Result);\r
3796}\r
3797\r
3798/**\r
3799 INT32 Subtraction\r
3800\r
3801 Performs the requested operation using the input parameters into a value\r
3802 specified by Result type and stores the converted value into the caller\r
3803 allocated output buffer specified by Result. The caller must pass in a\r
3804 Result buffer that is at least as large as the Result type.\r
3805\r
3806 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3807\r
3808 If the requested operation results in an overflow or an underflow condition,\r
3809 then Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3810\r
3811 @param[in] Minuend A number from which another is to be subtracted.\r
3812 @param[in] Subtrahend A number to be subtracted from another\r
3813 @param[out] Result Pointer to the result of subtraction\r
3814\r
3815 @retval RETURN_SUCCESS Successful subtraction\r
3816 @retval RETURN_BUFFER_TOO_SMALL Underflow\r
3817 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3818**/\r
3819RETURN_STATUS\r
3820EFIAPI\r
3821SafeInt32Sub (\r
3822 IN INT32 Minuend,\r
3823 IN INT32 Subtrahend,\r
3824 OUT INT32 *Result\r
3825 )\r
3826{\r
3827 return SafeInt64ToInt32 (((INT64)Minuend) - ((INT64)Subtrahend), Result);\r
3828}\r
3829\r
3830/**\r
3831 INT64 Subtraction\r
3832\r
3833 Performs the requested operation using the input parameters into a value\r
3834 specified by Result type and stores the converted value into the caller\r
3835 allocated output buffer specified by Result. The caller must pass in a\r
3836 Result buffer that is at least as large as the Result type.\r
3837\r
3838 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3839\r
3840 If the requested operation results in an overflow or an underflow condition,\r
3841 then Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3842\r
3843 @param[in] Minuend A number from which another is to be subtracted.\r
3844 @param[in] Subtrahend A number to be subtracted from another\r
3845 @param[out] Result Pointer to the result of subtraction\r
3846\r
3847 @retval RETURN_SUCCESS Successful subtraction\r
3848 @retval RETURN_BUFFER_TOO_SMALL Underflow\r
3849 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3850**/\r
3851RETURN_STATUS\r
3852EFIAPI\r
3853SafeInt64Sub (\r
3854 IN INT64 Minuend,\r
3855 IN INT64 Subtrahend,\r
3856 OUT INT64 *Result\r
3857 )\r
3858{\r
3859 RETURN_STATUS Status;\r
d7a09cb8
SB
3860\r
3861 if (Result == NULL) {\r
3862 return RETURN_INVALID_PARAMETER;\r
3863 }\r
3864\r
d7a09cb8 3865 //\r
54c7728a
LE
3866 // * A Subtrahend of zero can never cause underflow or overflow.\r
3867 //\r
3868 // * A positive Subtrahend can only cause underflow. The underflow condition\r
3869 // is:\r
3870 //\r
3871 // (Minuend - Subtrahend) < MIN_INT64\r
3872 //\r
3873 // Adding Subtrahend to both sides yields\r
3874 //\r
3875 // Minuend < (MIN_INT64 + Subtrahend)\r
3876 //\r
3877 // This condition can be coded directly in C because the RHS will neither\r
3878 // underflow nor overflow. That is due to the starting condition:\r
3879 //\r
3880 // 0 < Subtrahend <= MAX_INT64\r
3881 //\r
3882 // Adding MIN_INT64 to all three sides yields\r
3883 //\r
3884 // MIN_INT64 < (MIN_INT64 + Subtrahend) <= (MIN_INT64 + MAX_INT64) = -1\r
d7a09cb8 3885 //\r
54c7728a
LE
3886 // * A negative Subtrahend can only cause overflow. The overflow condition is\r
3887 //\r
3888 // (Minuend - Subtrahend) > MAX_INT64\r
3889 //\r
3890 // Adding Subtrahend to both sides yields\r
3891 //\r
3892 // Minuend > (MAX_INT64 + Subtrahend)\r
3893 //\r
3894 // This condition can be coded directly in C because the RHS will neither\r
3895 // underflow nor overflow. That is due to the starting condition:\r
3896 //\r
3897 // MIN_INT64 <= Subtrahend < 0\r
3898 //\r
3899 // Adding MAX_INT64 to all three sides yields\r
3900 //\r
3901 // -1 = (MAX_INT64 + MIN_INT64) <= (MAX_INT64 + Subtrahend) < MAX_INT64\r
3902 //\r
3903 if (((Subtrahend > 0) && (Minuend < (MIN_INT64 + Subtrahend))) ||\r
3904 ((Subtrahend < 0) && (Minuend > (MAX_INT64 + Subtrahend)))) {\r
d7a09cb8
SB
3905 *Result = INT64_ERROR;\r
3906 Status = RETURN_BUFFER_TOO_SMALL;\r
3907 } else {\r
54c7728a 3908 *Result = Minuend - Subtrahend;\r
d7a09cb8
SB
3909 Status = RETURN_SUCCESS;\r
3910 }\r
3911\r
3912 return Status;\r
3913}\r
3914\r
3915//\r
3916// Signed multiplication functions\r
3917//\r
3918\r
3919/**\r
3920 INT8 multiplication\r
3921\r
3922 Performs the requested operation using the input parameters into a value\r
3923 specified by Result type and stores the converted value into the caller\r
3924 allocated output buffer specified by Result. The caller must pass in a\r
3925 Result buffer that is at least as large as the Result type.\r
3926\r
3927 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3928\r
3929 If the requested operation results in an overflow or an underflow condition,\r
3930 then Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3931\r
3932 @param[in] Multiplicand A number that is to be multiplied by another\r
3933 @param[in] Multiplier A number by which the multiplicand is to be multiplied\r
3934 @param[out] Result Pointer to the result of multiplication\r
3935\r
3936 @retval RETURN_SUCCESS Successful multiplication\r
3937 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
3938 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3939**/\r
3940RETURN_STATUS\r
3941EFIAPI\r
3942SafeInt8Mult (\r
3943 IN INT8 Multiplicand,\r
3944 IN INT8 Multiplier,\r
3945 OUT INT8 *Result\r
3946 )\r
3947{\r
3948 return SafeInt32ToInt8 (((INT32)Multiplier) *((INT32)Multiplicand), Result);\r
3949}\r
3950\r
3951/**\r
3952 CHAR8 multiplication\r
3953\r
3954 Performs the requested operation using the input parameters into a value\r
3955 specified by Result type and stores the converted value into the caller\r
3956 allocated output buffer specified by Result. The caller must pass in a\r
3957 Result buffer that is at least as large as the Result type.\r
3958\r
3959 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
3960\r
3961 If the requested operation results in an overflow or an underflow condition,\r
3962 then Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
3963\r
3964 @param[in] Multiplicand A number that is to be multiplied by another\r
3965 @param[in] Multiplier A number by which the multiplicand is to be multiplied\r
3966 @param[out] Result Pointer to the result of multiplication\r
3967\r
3968 @retval RETURN_SUCCESS Successful multiplication\r
3969 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
3970 @retval RETURN_INVALID_PARAMETER Result is NULL\r
3971**/\r
3972RETURN_STATUS\r
3973EFIAPI\r
3974SafeChar8Mult (\r
3975 IN CHAR8 Multiplicand,\r
3976 IN CHAR8 Multiplier,\r
3977 OUT CHAR8 *Result\r
3978 )\r
3979{\r
3980 INT32 Multiplicand32;\r
3981 INT32 Multiplier32;\r
3982\r
3983 if (Result == NULL) {\r
3984 return RETURN_INVALID_PARAMETER;\r
3985 }\r
3986\r
3987 Multiplicand32 = (INT32)Multiplicand;\r
3988 Multiplier32 = (INT32)Multiplier;\r
3989 if (Multiplicand32 < 0 || Multiplicand32 > MAX_INT8) {\r
3990 *Result = CHAR8_ERROR;\r
3991 return RETURN_BUFFER_TOO_SMALL;\r
3992 }\r
3993 if (Multiplier32 < 0 || Multiplier32 > MAX_INT8) {\r
3994 *Result = CHAR8_ERROR;\r
3995 return RETURN_BUFFER_TOO_SMALL;\r
3996 }\r
3997\r
3998 return SafeInt32ToChar8 (Multiplicand32 * Multiplier32, Result);\r
3999}\r
4000\r
4001/**\r
4002 INT16 multiplication\r
4003\r
4004 Performs the requested operation using the input parameters into a value\r
4005 specified by Result type and stores the converted value into the caller\r
4006 allocated output buffer specified by Result. The caller must pass in a\r
4007 Result buffer that is at least as large as the Result type.\r
4008\r
4009 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
4010\r
4011 If the requested operation results in an overflow or an underflow condition,\r
4012 then Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
4013\r
4014 @param[in] Multiplicand A number that is to be multiplied by another\r
4015 @param[in] Multiplier A number by which the multiplicand is to be multiplied\r
4016 @param[out] Result Pointer to the result of multiplication\r
4017\r
4018 @retval RETURN_SUCCESS Successful multiplication\r
4019 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
4020 @retval RETURN_INVALID_PARAMETER Result is NULL\r
4021**/\r
4022RETURN_STATUS\r
4023EFIAPI\r
4024SafeInt16Mult (\r
4025 IN INT16 Multiplicand,\r
4026 IN INT16 Multiplier,\r
4027 OUT INT16 *Result\r
4028 )\r
4029{\r
4030 return SafeInt32ToInt16 (((INT32)Multiplicand) *((INT32)Multiplier), Result);\r
4031}\r
4032\r
4033/**\r
4034 INT32 multiplication\r
4035\r
4036 Performs the requested operation using the input parameters into a value\r
4037 specified by Result type and stores the converted value into the caller\r
4038 allocated output buffer specified by Result. The caller must pass in a\r
4039 Result buffer that is at least as large as the Result type.\r
4040\r
4041 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
4042\r
4043 If the requested operation results in an overflow or an underflow condition,\r
4044 then Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
4045\r
4046 @param[in] Multiplicand A number that is to be multiplied by another\r
4047 @param[in] Multiplier A number by which the multiplicand is to be multiplied\r
4048 @param[out] Result Pointer to the result of multiplication\r
4049\r
4050 @retval RETURN_SUCCESS Successful multiplication\r
4051 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
4052 @retval RETURN_INVALID_PARAMETER Result is NULL\r
4053**/\r
4054RETURN_STATUS\r
4055EFIAPI\r
4056SafeInt32Mult (\r
4057 IN INT32 Multiplicand,\r
4058 IN INT32 Multiplier,\r
4059 OUT INT32 *Result\r
4060 )\r
4061{\r
dc9b2a57 4062 return SafeInt64ToInt32 (MultS64x64 (Multiplicand, Multiplier), Result);\r
d7a09cb8
SB
4063}\r
4064\r
4065/**\r
4066 INT64 multiplication\r
4067\r
4068 Performs the requested operation using the input parameters into a value\r
4069 specified by Result type and stores the converted value into the caller\r
4070 allocated output buffer specified by Result. The caller must pass in a\r
4071 Result buffer that is at least as large as the Result type.\r
4072\r
4073 If Result is NULL, RETURN_INVALID_PARAMETER is returned.\r
4074\r
4075 If the requested operation results in an overflow or an underflow condition,\r
4076 then Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.\r
4077\r
4078 @param[in] Multiplicand A number that is to be multiplied by another\r
4079 @param[in] Multiplier A number by which the multiplicand is to be multiplied\r
4080 @param[out] Result Pointer to the result of multiplication\r
4081\r
4082 @retval RETURN_SUCCESS Successful multiplication\r
4083 @retval RETURN_BUFFER_TOO_SMALL Overflow\r
4084 @retval RETURN_INVALID_PARAMETER Result is NULL\r
4085**/\r
4086RETURN_STATUS\r
4087EFIAPI\r
4088SafeInt64Mult (\r
4089 IN INT64 Multiplicand,\r
4090 IN INT64 Multiplier,\r
4091 OUT INT64 *Result\r
4092 )\r
4093{\r
4094 RETURN_STATUS Status;\r
4095 UINT64 UnsignedMultiplicand;\r
4096 UINT64 UnsignedMultiplier;\r
4097 UINT64 UnsignedResult;\r
4098\r
4099 if (Result == NULL) {\r
4100 return RETURN_INVALID_PARAMETER;\r
4101 }\r
4102\r
4103 //\r
4104 // Split into sign and magnitude, do unsigned operation, apply sign.\r
4105 //\r
4106 if (Multiplicand < 0) {\r
4107 //\r
4108 // Avoid negating the most negative number.\r
4109 //\r
4110 UnsignedMultiplicand = ((UINT64)(- (Multiplicand + 1))) + 1;\r
4111 } else {\r
4112 UnsignedMultiplicand = (UINT64)Multiplicand;\r
4113 }\r
4114\r
4115 if (Multiplier < 0) {\r
4116 //\r
4117 // Avoid negating the most negative number.\r
4118 //\r
4119 UnsignedMultiplier = ((UINT64)(- (Multiplier + 1))) + 1;\r
4120 } else {\r
4121 UnsignedMultiplier = (UINT64)Multiplier;\r
4122 }\r
4123\r
4124 Status = SafeUint64Mult (UnsignedMultiplicand, UnsignedMultiplier, &UnsignedResult);\r
4125 if (!RETURN_ERROR (Status)) {\r
4126 if ((Multiplicand < 0) != (Multiplier < 0)) {\r
4127 if (UnsignedResult > MIN_INT64_MAGNITUDE) {\r
4128 *Result = INT64_ERROR;\r
4129 Status = RETURN_BUFFER_TOO_SMALL;\r
75505d16
LE
4130 } else if (UnsignedResult == MIN_INT64_MAGNITUDE) {\r
4131 *Result = MIN_INT64;\r
d7a09cb8
SB
4132 } else {\r
4133 *Result = - ((INT64)UnsignedResult);\r
4134 }\r
4135 } else {\r
4136 if (UnsignedResult > MAX_INT64) {\r
4137 *Result = INT64_ERROR;\r
4138 Status = RETURN_BUFFER_TOO_SMALL;\r
4139 } else {\r
4140 *Result = (INT64)UnsignedResult;\r
4141 }\r
4142 }\r
4143 } else {\r
4144 *Result = INT64_ERROR;\r
4145 }\r
4146 return Status;\r
4147}\r
4148\r