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