2 This library provides helper functions to prevent integer overflow during
3 type conversion, addition, subtraction, and multiplication.
5 Copyright (c) 2017, Microsoft Corporation
8 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include <Library/SafeIntLib.h>
14 #include <Library/BaseLib.h>
18 // Magnitude of MIN_INT64 as expressed by a UINT64 number.
20 #define MIN_INT64_MAGNITUDE (((UINT64)(- (MIN_INT64 + 1))) + 1)
23 // Conversion functions
25 // There are three reasons for having conversion functions:
27 // 1. We are converting from a signed type to an unsigned type of the same
28 // size, or vice-versa.
30 // 2. We are converting to a smaller type, and we could therefore possibly
33 // 3. We are converting to a bigger type, and we are signed and the type we are
34 // converting to is unsigned.
38 INT8 -> UINT8 conversion
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.
45 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
50 @param[in] Operand Operand to be converted to new type
51 @param[out] Result Pointer to the result of conversion
53 @retval RETURN_SUCCESS Successful conversion
54 @retval RETURN_BUFFER_TOO_SMALL Overflow
55 @retval RETURN_INVALID_PARAMETER Result is NULL
67 return RETURN_INVALID_PARAMETER
;
71 *Result
= (UINT8
)Operand
;
72 Status
= RETURN_SUCCESS
;
74 *Result
= UINT8_ERROR
;
75 Status
= RETURN_BUFFER_TOO_SMALL
;
82 INT8 -> CHAR8 conversion
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.
89 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
94 @param[in] Operand Operand to be converted to new type
95 @param[out] Result Pointer to the result of conversion
97 @retval RETURN_SUCCESS Successful conversion
98 @retval RETURN_BUFFER_TOO_SMALL Overflow
99 @retval RETURN_INVALID_PARAMETER Result is NULL
108 RETURN_STATUS Status
;
110 if (Result
== NULL
) {
111 return RETURN_INVALID_PARAMETER
;
115 *Result
= (CHAR8
)Operand
;
116 Status
= RETURN_SUCCESS
;
118 *Result
= CHAR8_ERROR
;
119 Status
= RETURN_BUFFER_TOO_SMALL
;
126 INT8 -> UINT16 conversion
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.
133 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
138 @param[in] Operand Operand to be converted to new type
139 @param[out] Result Pointer to the result of conversion
141 @retval RETURN_SUCCESS Successful conversion
142 @retval RETURN_BUFFER_TOO_SMALL Overflow
143 @retval RETURN_INVALID_PARAMETER Result is NULL
152 RETURN_STATUS Status
;
154 if (Result
== NULL
) {
155 return RETURN_INVALID_PARAMETER
;
159 *Result
= (UINT16
)Operand
;
160 Status
= RETURN_SUCCESS
;
162 *Result
= UINT16_ERROR
;
163 Status
= RETURN_BUFFER_TOO_SMALL
;
170 INT8 -> UINT32 conversion
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.
177 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
182 @param[in] Operand Operand to be converted to new type
183 @param[out] Result Pointer to the result of conversion
185 @retval RETURN_SUCCESS Successful conversion
186 @retval RETURN_BUFFER_TOO_SMALL Overflow
187 @retval RETURN_INVALID_PARAMETER Result is NULL
196 RETURN_STATUS Status
;
198 if (Result
== NULL
) {
199 return RETURN_INVALID_PARAMETER
;
203 *Result
= (UINT32
)Operand
;
204 Status
= RETURN_SUCCESS
;
206 *Result
= UINT32_ERROR
;
207 Status
= RETURN_BUFFER_TOO_SMALL
;
214 INT8 -> UINTN conversion
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.
221 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
226 @param[in] Operand Operand to be converted to new type
227 @param[out] Result Pointer to the result of conversion
229 @retval RETURN_SUCCESS Successful conversion
230 @retval RETURN_BUFFER_TOO_SMALL Overflow
231 @retval RETURN_INVALID_PARAMETER Result is NULL
240 RETURN_STATUS Status
;
242 if (Result
== NULL
) {
243 return RETURN_INVALID_PARAMETER
;
247 *Result
= (UINTN
)Operand
;
248 Status
= RETURN_SUCCESS
;
250 *Result
= UINTN_ERROR
;
251 Status
= RETURN_BUFFER_TOO_SMALL
;
258 INT8 -> UINT64 conversion
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.
265 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
270 @param[in] Operand Operand to be converted to new type
271 @param[out] Result Pointer to the result of conversion
273 @retval RETURN_SUCCESS Successful conversion
274 @retval RETURN_BUFFER_TOO_SMALL Overflow
275 @retval RETURN_INVALID_PARAMETER Result is NULL
284 RETURN_STATUS Status
;
286 if (Result
== NULL
) {
287 return RETURN_INVALID_PARAMETER
;
291 *Result
= (UINT64
)Operand
;
292 Status
= RETURN_SUCCESS
;
294 *Result
= UINT64_ERROR
;
295 Status
= RETURN_BUFFER_TOO_SMALL
;
302 UINT8 -> INT8 conversion
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.
309 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
314 @param[in] Operand Operand to be converted to new type
315 @param[out] Result Pointer to the result of conversion
317 @retval RETURN_SUCCESS Successful conversion
318 @retval RETURN_BUFFER_TOO_SMALL Overflow
319 @retval RETURN_INVALID_PARAMETER Result is NULL
328 RETURN_STATUS Status
;
330 if (Result
== NULL
) {
331 return RETURN_INVALID_PARAMETER
;
334 if (Operand
<= MAX_INT8
) {
335 *Result
= (INT8
)Operand
;
336 Status
= RETURN_SUCCESS
;
338 *Result
= INT8_ERROR
;
339 Status
= RETURN_BUFFER_TOO_SMALL
;
346 UINT8 -> CHAR8 conversion
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.
353 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
358 @param[in] Operand Operand to be converted to new type
359 @param[out] Result Pointer to the result of conversion
361 @retval RETURN_SUCCESS Successful conversion
362 @retval RETURN_BUFFER_TOO_SMALL Overflow
363 @retval RETURN_INVALID_PARAMETER Result is NULL
372 RETURN_STATUS Status
;
374 if (Result
== NULL
) {
375 return RETURN_INVALID_PARAMETER
;
378 if (Operand
<= MAX_INT8
) {
379 *Result
= (CHAR8
)Operand
;
380 Status
= RETURN_SUCCESS
;
382 *Result
= CHAR8_ERROR
;
383 Status
= RETURN_BUFFER_TOO_SMALL
;
390 INT16 -> INT8 conversion
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.
397 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
402 @param[in] Operand Operand to be converted to new type
403 @param[out] Result Pointer to the result of conversion
405 @retval RETURN_SUCCESS Successful conversion
406 @retval RETURN_BUFFER_TOO_SMALL Overflow
407 @retval RETURN_INVALID_PARAMETER Result is NULL
416 RETURN_STATUS Status
;
418 if (Result
== NULL
) {
419 return RETURN_INVALID_PARAMETER
;
422 if ((Operand
>= MIN_INT8
) && (Operand
<= MAX_INT8
)) {
423 *Result
= (INT8
)Operand
;
424 Status
= RETURN_SUCCESS
;
426 *Result
= INT8_ERROR
;
427 Status
= RETURN_BUFFER_TOO_SMALL
;
434 INT16 -> CHAR8 conversion
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.
441 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
446 @param[in] Operand Operand to be converted to new type
447 @param[out] Result Pointer to the result of conversion
449 @retval RETURN_SUCCESS Successful conversion
450 @retval RETURN_BUFFER_TOO_SMALL Overflow
451 @retval RETURN_INVALID_PARAMETER Result is NULL
460 RETURN_STATUS Status
;
462 if (Result
== NULL
) {
463 return RETURN_INVALID_PARAMETER
;
466 if ((Operand
>= 0) && (Operand
<= MAX_INT8
)) {
467 *Result
= (CHAR8
)Operand
;
468 Status
= RETURN_SUCCESS
;
470 *Result
= CHAR8_ERROR
;
471 Status
= RETURN_BUFFER_TOO_SMALL
;
478 INT16 -> UINT8 conversion
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.
485 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
490 @param[in] Operand Operand to be converted to new type
491 @param[out] Result Pointer to the result of conversion
493 @retval RETURN_SUCCESS Successful conversion
494 @retval RETURN_BUFFER_TOO_SMALL Overflow
495 @retval RETURN_INVALID_PARAMETER Result is NULL
504 RETURN_STATUS Status
;
506 if (Result
== NULL
) {
507 return RETURN_INVALID_PARAMETER
;
510 if ((Operand
>= 0) && (Operand
<= MAX_UINT8
)) {
511 *Result
= (UINT8
)Operand
;
512 Status
= RETURN_SUCCESS
;
514 *Result
= UINT8_ERROR
;
515 Status
= RETURN_BUFFER_TOO_SMALL
;
522 INT16 -> UINT16 conversion
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.
529 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
534 @param[in] Operand Operand to be converted to new type
535 @param[out] Result Pointer to the result of conversion
537 @retval RETURN_SUCCESS Successful conversion
538 @retval RETURN_BUFFER_TOO_SMALL Overflow
539 @retval RETURN_INVALID_PARAMETER Result is NULL
548 RETURN_STATUS Status
;
550 if (Result
== NULL
) {
551 return RETURN_INVALID_PARAMETER
;
555 *Result
= (UINT16
)Operand
;
556 Status
= RETURN_SUCCESS
;
558 *Result
= UINT16_ERROR
;
559 Status
= RETURN_BUFFER_TOO_SMALL
;
566 INT16 -> UINT32 conversion
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.
573 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
578 @param[in] Operand Operand to be converted to new type
579 @param[out] Result Pointer to the result of conversion
581 @retval RETURN_SUCCESS Successful conversion
582 @retval RETURN_BUFFER_TOO_SMALL Overflow
583 @retval RETURN_INVALID_PARAMETER Result is NULL
592 RETURN_STATUS Status
;
594 if (Result
== NULL
) {
595 return RETURN_INVALID_PARAMETER
;
599 *Result
= (UINT32
)Operand
;
600 Status
= RETURN_SUCCESS
;
602 *Result
= UINT32_ERROR
;
603 Status
= RETURN_BUFFER_TOO_SMALL
;
610 INT16 -> UINTN conversion
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.
617 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
622 @param[in] Operand Operand to be converted to new type
623 @param[out] Result Pointer to the result of conversion
625 @retval RETURN_SUCCESS Successful conversion
626 @retval RETURN_BUFFER_TOO_SMALL Overflow
627 @retval RETURN_INVALID_PARAMETER Result is NULL
636 RETURN_STATUS Status
;
638 if (Result
== NULL
) {
639 return RETURN_INVALID_PARAMETER
;
643 *Result
= (UINTN
)Operand
;
644 Status
= RETURN_SUCCESS
;
646 *Result
= UINTN_ERROR
;
647 Status
= RETURN_BUFFER_TOO_SMALL
;
654 INT16 -> UINT64 conversion
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.
661 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
666 @param[in] Operand Operand to be converted to new type
667 @param[out] Result Pointer to the result of conversion
669 @retval RETURN_SUCCESS Successful conversion
670 @retval RETURN_BUFFER_TOO_SMALL Overflow
671 @retval RETURN_INVALID_PARAMETER Result is NULL
680 RETURN_STATUS Status
;
682 if (Result
== NULL
) {
683 return RETURN_INVALID_PARAMETER
;
687 *Result
= (UINT64
)Operand
;
688 Status
= RETURN_SUCCESS
;
690 *Result
= UINT64_ERROR
;
691 Status
= RETURN_BUFFER_TOO_SMALL
;
698 UINT16 -> INT8 conversion
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.
705 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
710 @param[in] Operand Operand to be converted to new type
711 @param[out] Result Pointer to the result of conversion
713 @retval RETURN_SUCCESS Successful conversion
714 @retval RETURN_BUFFER_TOO_SMALL Overflow
715 @retval RETURN_INVALID_PARAMETER Result is NULL
724 RETURN_STATUS Status
;
726 if (Result
== NULL
) {
727 return RETURN_INVALID_PARAMETER
;
730 if (Operand
<= MAX_INT8
) {
731 *Result
= (INT8
)Operand
;
732 Status
= RETURN_SUCCESS
;
734 *Result
= INT8_ERROR
;
735 Status
= RETURN_BUFFER_TOO_SMALL
;
742 UINT16 -> CHAR8 conversion
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.
749 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
754 @param[in] Operand Operand to be converted to new type
755 @param[out] Result Pointer to the result of conversion
757 @retval RETURN_SUCCESS Successful conversion
758 @retval RETURN_BUFFER_TOO_SMALL Overflow
759 @retval RETURN_INVALID_PARAMETER Result is NULL
768 RETURN_STATUS Status
;
770 if (Result
== NULL
) {
771 return RETURN_INVALID_PARAMETER
;
774 if (Operand
<= MAX_INT8
) {
775 *Result
= (INT8
)Operand
;
776 Status
= RETURN_SUCCESS
;
778 *Result
= CHAR8_ERROR
;
779 Status
= RETURN_BUFFER_TOO_SMALL
;
786 UINT16 -> UINT8 conversion
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.
793 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
798 @param[in] Operand Operand to be converted to new type
799 @param[out] Result Pointer to the result of conversion
801 @retval RETURN_SUCCESS Successful conversion
802 @retval RETURN_BUFFER_TOO_SMALL Overflow
803 @retval RETURN_INVALID_PARAMETER Result is NULL
812 RETURN_STATUS Status
;
814 if (Result
== NULL
) {
815 return RETURN_INVALID_PARAMETER
;
818 if (Operand
<= MAX_UINT8
) {
819 *Result
= (UINT8
)Operand
;
820 Status
= RETURN_SUCCESS
;
822 *Result
= UINT8_ERROR
;
823 Status
= RETURN_BUFFER_TOO_SMALL
;
830 UINT16 -> INT16 conversion
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.
837 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
842 @param[in] Operand Operand to be converted to new type
843 @param[out] Result Pointer to the result of conversion
845 @retval RETURN_SUCCESS Successful conversion
846 @retval RETURN_BUFFER_TOO_SMALL Overflow
847 @retval RETURN_INVALID_PARAMETER Result is NULL
856 RETURN_STATUS Status
;
858 if (Result
== NULL
) {
859 return RETURN_INVALID_PARAMETER
;
862 if (Operand
<= MAX_INT16
) {
863 *Result
= (INT16
)Operand
;
864 Status
= RETURN_SUCCESS
;
866 *Result
= INT16_ERROR
;
867 Status
= RETURN_BUFFER_TOO_SMALL
;
874 INT32 -> INT8 conversion
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.
881 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
886 @param[in] Operand Operand to be converted to new type
887 @param[out] Result Pointer to the result of conversion
889 @retval RETURN_SUCCESS Successful conversion
890 @retval RETURN_BUFFER_TOO_SMALL Overflow
891 @retval RETURN_INVALID_PARAMETER Result is NULL
900 RETURN_STATUS Status
;
902 if (Result
== NULL
) {
903 return RETURN_INVALID_PARAMETER
;
906 if ((Operand
>= MIN_INT8
) && (Operand
<= MAX_INT8
)) {
907 *Result
= (INT8
)Operand
;
908 Status
= RETURN_SUCCESS
;
910 *Result
= INT8_ERROR
;
911 Status
= RETURN_BUFFER_TOO_SMALL
;
918 INT32 -> CHAR8 conversion
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.
925 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
930 @param[in] Operand Operand to be converted to new type
931 @param[out] Result Pointer to the result of conversion
933 @retval RETURN_SUCCESS Successful conversion
934 @retval RETURN_BUFFER_TOO_SMALL Overflow
935 @retval RETURN_INVALID_PARAMETER Result is NULL
944 RETURN_STATUS Status
;
946 if (Result
== NULL
) {
947 return RETURN_INVALID_PARAMETER
;
950 if ((Operand
>= 0) && (Operand
<= MAX_INT8
)) {
951 *Result
= (CHAR8
)Operand
;
952 Status
= RETURN_SUCCESS
;
954 *Result
= CHAR8_ERROR
;
955 Status
= RETURN_BUFFER_TOO_SMALL
;
962 INT32 -> UINT8 conversion
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.
969 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
974 @param[in] Operand Operand to be converted to new type
975 @param[out] Result Pointer to the result of conversion
977 @retval RETURN_SUCCESS Successful conversion
978 @retval RETURN_BUFFER_TOO_SMALL Overflow
979 @retval RETURN_INVALID_PARAMETER Result is NULL
988 RETURN_STATUS Status
;
990 if (Result
== NULL
) {
991 return RETURN_INVALID_PARAMETER
;
994 if ((Operand
>= 0) && (Operand
<= MAX_UINT8
)) {
995 *Result
= (UINT8
)Operand
;
996 Status
= RETURN_SUCCESS
;
998 *Result
= UINT8_ERROR
;
999 Status
= RETURN_BUFFER_TOO_SMALL
;
1006 INT32 -> INT16 conversion
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.
1013 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
1018 @param[in] Operand Operand to be converted to new type
1019 @param[out] Result Pointer to the result of conversion
1021 @retval RETURN_SUCCESS Successful conversion
1022 @retval RETURN_BUFFER_TOO_SMALL Overflow
1023 @retval RETURN_INVALID_PARAMETER Result is NULL
1032 RETURN_STATUS Status
;
1034 if (Result
== NULL
) {
1035 return RETURN_INVALID_PARAMETER
;
1038 if ((Operand
>= MIN_INT16
) && (Operand
<= MAX_INT16
)) {
1039 *Result
= (INT16
)Operand
;
1040 Status
= RETURN_SUCCESS
;
1042 *Result
= INT16_ERROR
;
1043 Status
= RETURN_BUFFER_TOO_SMALL
;
1050 INT32 -> UINT16 conversion
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.
1057 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
1062 @param[in] Operand Operand to be converted to new type
1063 @param[out] Result Pointer to the result of conversion
1065 @retval RETURN_SUCCESS Successful conversion
1066 @retval RETURN_BUFFER_TOO_SMALL Overflow
1067 @retval RETURN_INVALID_PARAMETER Result is NULL
1076 RETURN_STATUS Status
;
1078 if (Result
== NULL
) {
1079 return RETURN_INVALID_PARAMETER
;
1082 if ((Operand
>= 0) && (Operand
<= MAX_UINT16
)) {
1083 *Result
= (UINT16
)Operand
;
1084 Status
= RETURN_SUCCESS
;
1086 *Result
= UINT16_ERROR
;
1087 Status
= RETURN_BUFFER_TOO_SMALL
;
1094 INT32 -> UINT32 conversion
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.
1101 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
1106 @param[in] Operand Operand to be converted to new type
1107 @param[out] Result Pointer to the result of conversion
1109 @retval RETURN_SUCCESS Successful conversion
1110 @retval RETURN_BUFFER_TOO_SMALL Overflow
1111 @retval RETURN_INVALID_PARAMETER Result is NULL
1120 RETURN_STATUS Status
;
1122 if (Result
== NULL
) {
1123 return RETURN_INVALID_PARAMETER
;
1127 *Result
= (UINT32
)Operand
;
1128 Status
= RETURN_SUCCESS
;
1130 *Result
= UINT32_ERROR
;
1131 Status
= RETURN_BUFFER_TOO_SMALL
;
1138 INT32 -> UINT64 conversion
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.
1145 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
1150 @param[in] Operand Operand to be converted to new type
1151 @param[out] Result Pointer to the result of conversion
1153 @retval RETURN_SUCCESS Successful conversion
1154 @retval RETURN_BUFFER_TOO_SMALL Overflow
1155 @retval RETURN_INVALID_PARAMETER Result is NULL
1164 RETURN_STATUS Status
;
1166 if (Result
== NULL
) {
1167 return RETURN_INVALID_PARAMETER
;
1171 *Result
= (UINT64
)Operand
;
1172 Status
= RETURN_SUCCESS
;
1174 *Result
= UINT64_ERROR
;
1175 Status
= RETURN_BUFFER_TOO_SMALL
;
1182 UINT32 -> INT8 conversion
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.
1189 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
1194 @param[in] Operand Operand to be converted to new type
1195 @param[out] Result Pointer to the result of conversion
1197 @retval RETURN_SUCCESS Successful conversion
1198 @retval RETURN_BUFFER_TOO_SMALL Overflow
1199 @retval RETURN_INVALID_PARAMETER Result is NULL
1208 RETURN_STATUS Status
;
1210 if (Result
== NULL
) {
1211 return RETURN_INVALID_PARAMETER
;
1214 if (Operand
<= MAX_INT8
) {
1215 *Result
= (INT8
)Operand
;
1216 Status
= RETURN_SUCCESS
;
1218 *Result
= INT8_ERROR
;
1219 Status
= RETURN_BUFFER_TOO_SMALL
;
1226 UINT32 -> CHAR8 conversion
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.
1233 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
1238 @param[in] Operand Operand to be converted to new type
1239 @param[out] Result Pointer to the result of conversion
1241 @retval RETURN_SUCCESS Successful conversion
1242 @retval RETURN_BUFFER_TOO_SMALL Overflow
1243 @retval RETURN_INVALID_PARAMETER Result is NULL
1252 RETURN_STATUS Status
;
1254 if (Result
== NULL
) {
1255 return RETURN_INVALID_PARAMETER
;
1258 if (Operand
<= MAX_INT8
) {
1259 *Result
= (INT8
)Operand
;
1260 Status
= RETURN_SUCCESS
;
1262 *Result
= CHAR8_ERROR
;
1263 Status
= RETURN_BUFFER_TOO_SMALL
;
1270 UINT32 -> UINT8 conversion
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.
1277 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
1282 @param[in] Operand Operand to be converted to new type
1283 @param[out] Result Pointer to the result of conversion
1285 @retval RETURN_SUCCESS Successful conversion
1286 @retval RETURN_BUFFER_TOO_SMALL Overflow
1287 @retval RETURN_INVALID_PARAMETER Result is NULL
1296 RETURN_STATUS Status
;
1298 if (Result
== NULL
) {
1299 return RETURN_INVALID_PARAMETER
;
1302 if (Operand
<= MAX_UINT8
) {
1303 *Result
= (UINT8
)Operand
;
1304 Status
= RETURN_SUCCESS
;
1306 *Result
= UINT8_ERROR
;
1307 Status
= RETURN_BUFFER_TOO_SMALL
;
1314 UINT32 -> INT16 conversion
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.
1321 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
1326 @param[in] Operand Operand to be converted to new type
1327 @param[out] Result Pointer to the result of conversion
1329 @retval RETURN_SUCCESS Successful conversion
1330 @retval RETURN_BUFFER_TOO_SMALL Overflow
1331 @retval RETURN_INVALID_PARAMETER Result is NULL
1340 RETURN_STATUS Status
;
1342 if (Result
== NULL
) {
1343 return RETURN_INVALID_PARAMETER
;
1346 if (Operand
<= MAX_INT16
) {
1347 *Result
= (INT16
)Operand
;
1348 Status
= RETURN_SUCCESS
;
1350 *Result
= INT16_ERROR
;
1351 Status
= RETURN_BUFFER_TOO_SMALL
;
1358 UINT32 -> UINT16 conversion
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.
1365 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
1370 @param[in] Operand Operand to be converted to new type
1371 @param[out] Result Pointer to the result of conversion
1373 @retval RETURN_SUCCESS Successful conversion
1374 @retval RETURN_BUFFER_TOO_SMALL Overflow
1375 @retval RETURN_INVALID_PARAMETER Result is NULL
1379 SafeUint32ToUint16 (
1384 RETURN_STATUS Status
;
1386 if (Result
== NULL
) {
1387 return RETURN_INVALID_PARAMETER
;
1390 if (Operand
<= MAX_UINT16
) {
1391 *Result
= (UINT16
)Operand
;
1392 Status
= RETURN_SUCCESS
;
1394 *Result
= UINT16_ERROR
;
1395 Status
= RETURN_BUFFER_TOO_SMALL
;
1402 UINT32 -> INT32 conversion
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.
1409 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
1414 @param[in] Operand Operand to be converted to new type
1415 @param[out] Result Pointer to the result of conversion
1417 @retval RETURN_SUCCESS Successful conversion
1418 @retval RETURN_BUFFER_TOO_SMALL Overflow
1419 @retval RETURN_INVALID_PARAMETER Result is NULL
1428 RETURN_STATUS Status
;
1430 if (Result
== NULL
) {
1431 return RETURN_INVALID_PARAMETER
;
1434 if (Operand
<= MAX_INT32
) {
1435 *Result
= (INT32
)Operand
;
1436 Status
= RETURN_SUCCESS
;
1438 *Result
= INT32_ERROR
;
1439 Status
= RETURN_BUFFER_TOO_SMALL
;
1446 INTN -> INT8 conversion
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.
1453 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
1458 @param[in] Operand Operand to be converted to new type
1459 @param[out] Result Pointer to the result of conversion
1461 @retval RETURN_SUCCESS Successful conversion
1462 @retval RETURN_BUFFER_TOO_SMALL Overflow
1463 @retval RETURN_INVALID_PARAMETER Result is NULL
1472 RETURN_STATUS Status
;
1474 if (Result
== NULL
) {
1475 return RETURN_INVALID_PARAMETER
;
1478 if ((Operand
>= MIN_INT8
) && (Operand
<= MAX_INT8
)) {
1479 *Result
= (INT8
)Operand
;
1480 Status
= RETURN_SUCCESS
;
1482 *Result
= INT8_ERROR
;
1483 Status
= RETURN_BUFFER_TOO_SMALL
;
1490 INTN -> CHAR8 conversion
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.
1497 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
1502 @param[in] Operand Operand to be converted to new type
1503 @param[out] Result Pointer to the result of conversion
1505 @retval RETURN_SUCCESS Successful conversion
1506 @retval RETURN_BUFFER_TOO_SMALL Overflow
1507 @retval RETURN_INVALID_PARAMETER Result is NULL
1516 RETURN_STATUS Status
;
1518 if (Result
== NULL
) {
1519 return RETURN_INVALID_PARAMETER
;
1522 if ((Operand
>= 0) && (Operand
<= MAX_INT8
)) {
1523 *Result
= (CHAR8
)Operand
;
1524 Status
= RETURN_SUCCESS
;
1526 *Result
= CHAR8_ERROR
;
1527 Status
= RETURN_BUFFER_TOO_SMALL
;
1534 INTN -> UINT8 conversion
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.
1541 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
1546 @param[in] Operand Operand to be converted to new type
1547 @param[out] Result Pointer to the result of conversion
1549 @retval RETURN_SUCCESS Successful conversion
1550 @retval RETURN_BUFFER_TOO_SMALL Overflow
1551 @retval RETURN_INVALID_PARAMETER Result is NULL
1560 RETURN_STATUS Status
;
1562 if (Result
== NULL
) {
1563 return RETURN_INVALID_PARAMETER
;
1566 if ((Operand
>= 0) && (Operand
<= MAX_UINT8
)) {
1567 *Result
= (UINT8
)Operand
;
1568 Status
= RETURN_SUCCESS
;
1570 *Result
= UINT8_ERROR
;
1571 Status
= RETURN_BUFFER_TOO_SMALL
;
1578 INTN -> INT16 conversion
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.
1585 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
1590 @param[in] Operand Operand to be converted to new type
1591 @param[out] Result Pointer to the result of conversion
1593 @retval RETURN_SUCCESS Successful conversion
1594 @retval RETURN_BUFFER_TOO_SMALL Overflow
1595 @retval RETURN_INVALID_PARAMETER Result is NULL
1604 RETURN_STATUS Status
;
1606 if (Result
== NULL
) {
1607 return RETURN_INVALID_PARAMETER
;
1610 if ((Operand
>= MIN_INT16
) && (Operand
<= MAX_INT16
)) {
1611 *Result
= (INT16
)Operand
;
1612 Status
= RETURN_SUCCESS
;
1614 *Result
= INT16_ERROR
;
1615 Status
= RETURN_BUFFER_TOO_SMALL
;
1622 INTN -> UINT16 conversion
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.
1629 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
1634 @param[in] Operand Operand to be converted to new type
1635 @param[out] Result Pointer to the result of conversion
1637 @retval RETURN_SUCCESS Successful conversion
1638 @retval RETURN_BUFFER_TOO_SMALL Overflow
1639 @retval RETURN_INVALID_PARAMETER Result is NULL
1648 RETURN_STATUS Status
;
1650 if (Result
== NULL
) {
1651 return RETURN_INVALID_PARAMETER
;
1654 if ((Operand
>= 0) && (Operand
<= MAX_UINT16
)) {
1655 *Result
= (UINT16
)Operand
;
1656 Status
= RETURN_SUCCESS
;
1658 *Result
= UINT16_ERROR
;
1659 Status
= RETURN_BUFFER_TOO_SMALL
;
1666 INTN -> UINTN conversion
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.
1673 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
1678 @param[in] Operand Operand to be converted to new type
1679 @param[out] Result Pointer to the result of conversion
1681 @retval RETURN_SUCCESS Successful conversion
1682 @retval RETURN_BUFFER_TOO_SMALL Overflow
1683 @retval RETURN_INVALID_PARAMETER Result is NULL
1692 RETURN_STATUS Status
;
1694 if (Result
== NULL
) {
1695 return RETURN_INVALID_PARAMETER
;
1699 *Result
= (UINTN
)Operand
;
1700 Status
= RETURN_SUCCESS
;
1702 *Result
= UINTN_ERROR
;
1703 Status
= RETURN_BUFFER_TOO_SMALL
;
1710 INTN -> UINT64 conversion
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.
1717 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
1722 @param[in] Operand Operand to be converted to new type
1723 @param[out] Result Pointer to the result of conversion
1725 @retval RETURN_SUCCESS Successful conversion
1726 @retval RETURN_BUFFER_TOO_SMALL Overflow
1727 @retval RETURN_INVALID_PARAMETER Result is NULL
1736 RETURN_STATUS Status
;
1738 if (Result
== NULL
) {
1739 return RETURN_INVALID_PARAMETER
;
1743 *Result
= (UINT64
)Operand
;
1744 Status
= RETURN_SUCCESS
;
1746 *Result
= UINT64_ERROR
;
1747 Status
= RETURN_BUFFER_TOO_SMALL
;
1754 UINTN -> INT8 conversion
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.
1761 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
1766 @param[in] Operand Operand to be converted to new type
1767 @param[out] Result Pointer to the result of conversion
1769 @retval RETURN_SUCCESS Successful conversion
1770 @retval RETURN_BUFFER_TOO_SMALL Overflow
1771 @retval RETURN_INVALID_PARAMETER Result is NULL
1780 RETURN_STATUS Status
;
1782 if (Result
== NULL
) {
1783 return RETURN_INVALID_PARAMETER
;
1786 if (Operand
<= MAX_INT8
) {
1787 *Result
= (INT8
)Operand
;
1788 Status
= RETURN_SUCCESS
;
1790 *Result
= INT8_ERROR
;
1791 Status
= RETURN_BUFFER_TOO_SMALL
;
1798 UINTN -> CHAR8 conversion
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.
1805 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
1810 @param[in] Operand Operand to be converted to new type
1811 @param[out] Result Pointer to the result of conversion
1813 @retval RETURN_SUCCESS Successful conversion
1814 @retval RETURN_BUFFER_TOO_SMALL Overflow
1815 @retval RETURN_INVALID_PARAMETER Result is NULL
1824 RETURN_STATUS Status
;
1826 if (Result
== NULL
) {
1827 return RETURN_INVALID_PARAMETER
;
1830 if (Operand
<= MAX_INT8
) {
1831 *Result
= (INT8
)Operand
;
1832 Status
= RETURN_SUCCESS
;
1834 *Result
= CHAR8_ERROR
;
1835 Status
= RETURN_BUFFER_TOO_SMALL
;
1842 UINTN -> UINT8 conversion
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.
1849 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
1854 @param[in] Operand Operand to be converted to new type
1855 @param[out] Result Pointer to the result of conversion
1857 @retval RETURN_SUCCESS Successful conversion
1858 @retval RETURN_BUFFER_TOO_SMALL Overflow
1859 @retval RETURN_INVALID_PARAMETER Result is NULL
1868 RETURN_STATUS Status
;
1870 if (Result
== NULL
) {
1871 return RETURN_INVALID_PARAMETER
;
1874 if (Operand
<= MAX_UINT8
) {
1875 *Result
= (UINT8
)Operand
;
1876 Status
= RETURN_SUCCESS
;
1878 *Result
= UINT8_ERROR
;
1879 Status
= RETURN_BUFFER_TOO_SMALL
;
1886 UINTN -> INT16 conversion
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.
1893 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
1898 @param[in] Operand Operand to be converted to new type
1899 @param[out] Result Pointer to the result of conversion
1901 @retval RETURN_SUCCESS Successful conversion
1902 @retval RETURN_BUFFER_TOO_SMALL Overflow
1903 @retval RETURN_INVALID_PARAMETER Result is NULL
1912 RETURN_STATUS Status
;
1914 if (Result
== NULL
) {
1915 return RETURN_INVALID_PARAMETER
;
1918 if (Operand
<= MAX_INT16
) {
1919 *Result
= (INT16
)Operand
;
1920 Status
= RETURN_SUCCESS
;
1922 *Result
= INT16_ERROR
;
1923 Status
= RETURN_BUFFER_TOO_SMALL
;
1930 UINTN -> UINT16 conversion
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.
1937 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
1942 @param[in] Operand Operand to be converted to new type
1943 @param[out] Result Pointer to the result of conversion
1945 @retval RETURN_SUCCESS Successful conversion
1946 @retval RETURN_BUFFER_TOO_SMALL Overflow
1947 @retval RETURN_INVALID_PARAMETER Result is NULL
1956 RETURN_STATUS Status
;
1958 if (Result
== NULL
) {
1959 return RETURN_INVALID_PARAMETER
;
1962 if (Operand
<= MAX_UINT16
) {
1963 *Result
= (UINT16
)Operand
;
1964 Status
= RETURN_SUCCESS
;
1966 *Result
= UINT16_ERROR
;
1967 Status
= RETURN_BUFFER_TOO_SMALL
;
1974 UINTN -> INT32 conversion
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.
1981 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
1986 @param[in] Operand Operand to be converted to new type
1987 @param[out] Result Pointer to the result of conversion
1989 @retval RETURN_SUCCESS Successful conversion
1990 @retval RETURN_BUFFER_TOO_SMALL Overflow
1991 @retval RETURN_INVALID_PARAMETER Result is NULL
2000 RETURN_STATUS Status
;
2002 if (Result
== NULL
) {
2003 return RETURN_INVALID_PARAMETER
;
2006 if (Operand
<= MAX_INT32
) {
2007 *Result
= (INT32
)Operand
;
2008 Status
= RETURN_SUCCESS
;
2010 *Result
= INT32_ERROR
;
2011 Status
= RETURN_BUFFER_TOO_SMALL
;
2018 UINTN -> INTN conversion
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.
2025 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
2030 @param[in] Operand Operand to be converted to new type
2031 @param[out] Result Pointer to the result of conversion
2033 @retval RETURN_SUCCESS Successful conversion
2034 @retval RETURN_BUFFER_TOO_SMALL Overflow
2035 @retval RETURN_INVALID_PARAMETER Result is NULL
2044 RETURN_STATUS Status
;
2046 if (Result
== NULL
) {
2047 return RETURN_INVALID_PARAMETER
;
2050 if (Operand
<= MAX_INTN
) {
2051 *Result
= (INTN
)Operand
;
2052 Status
= RETURN_SUCCESS
;
2054 *Result
= INTN_ERROR
;
2055 Status
= RETURN_BUFFER_TOO_SMALL
;
2062 INT64 -> INT8 conversion
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.
2069 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
2074 @param[in] Operand Operand to be converted to new type
2075 @param[out] Result Pointer to the result of conversion
2077 @retval RETURN_SUCCESS Successful conversion
2078 @retval RETURN_BUFFER_TOO_SMALL Overflow
2079 @retval RETURN_INVALID_PARAMETER Result is NULL
2088 RETURN_STATUS Status
;
2090 if (Result
== NULL
) {
2091 return RETURN_INVALID_PARAMETER
;
2094 if ((Operand
>= MIN_INT8
) && (Operand
<= MAX_INT8
)) {
2095 *Result
= (INT8
)Operand
;
2096 Status
= RETURN_SUCCESS
;
2098 *Result
= INT8_ERROR
;
2099 Status
= RETURN_BUFFER_TOO_SMALL
;
2106 INT64 -> CHAR8 conversion
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.
2113 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
2118 @param[in] Operand Operand to be converted to new type
2119 @param[out] Result Pointer to the result of conversion
2121 @retval RETURN_SUCCESS Successful conversion
2122 @retval RETURN_BUFFER_TOO_SMALL Overflow
2123 @retval RETURN_INVALID_PARAMETER Result is NULL
2132 RETURN_STATUS Status
;
2134 if (Result
== NULL
) {
2135 return RETURN_INVALID_PARAMETER
;
2138 if ((Operand
>= 0) && (Operand
<= MAX_INT8
)) {
2139 *Result
= (CHAR8
)Operand
;
2140 Status
= RETURN_SUCCESS
;
2142 *Result
= CHAR8_ERROR
;
2143 Status
= RETURN_BUFFER_TOO_SMALL
;
2150 INT64 -> UINT8 conversion
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.
2157 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
2162 @param[in] Operand Operand to be converted to new type
2163 @param[out] Result Pointer to the result of conversion
2165 @retval RETURN_SUCCESS Successful conversion
2166 @retval RETURN_BUFFER_TOO_SMALL Overflow
2167 @retval RETURN_INVALID_PARAMETER Result is NULL
2176 RETURN_STATUS Status
;
2178 if (Result
== NULL
) {
2179 return RETURN_INVALID_PARAMETER
;
2182 if ((Operand
>= 0) && (Operand
<= MAX_UINT8
)) {
2183 *Result
= (UINT8
)Operand
;
2184 Status
= RETURN_SUCCESS
;
2186 *Result
= UINT8_ERROR
;
2187 Status
= RETURN_BUFFER_TOO_SMALL
;
2194 INT64 -> INT16 conversion
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.
2201 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
2206 @param[in] Operand Operand to be converted to new type
2207 @param[out] Result Pointer to the result of conversion
2209 @retval RETURN_SUCCESS Successful conversion
2210 @retval RETURN_BUFFER_TOO_SMALL Overflow
2211 @retval RETURN_INVALID_PARAMETER Result is NULL
2220 RETURN_STATUS Status
;
2222 if (Result
== NULL
) {
2223 return RETURN_INVALID_PARAMETER
;
2226 if ((Operand
>= MIN_INT16
) && (Operand
<= MAX_INT16
)) {
2227 *Result
= (INT16
)Operand
;
2228 Status
= RETURN_SUCCESS
;
2230 *Result
= INT16_ERROR
;
2231 Status
= RETURN_BUFFER_TOO_SMALL
;
2238 INT64 -> UINT16 conversion
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.
2245 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
2250 @param[in] Operand Operand to be converted to new type
2251 @param[out] Result Pointer to the result of conversion
2253 @retval RETURN_SUCCESS Successful conversion
2254 @retval RETURN_BUFFER_TOO_SMALL Overflow
2255 @retval RETURN_INVALID_PARAMETER Result is NULL
2264 RETURN_STATUS Status
;
2266 if (Result
== NULL
) {
2267 return RETURN_INVALID_PARAMETER
;
2270 if ((Operand
>= 0) && (Operand
<= MAX_UINT16
)) {
2271 *Result
= (UINT16
)Operand
;
2272 Status
= RETURN_SUCCESS
;
2274 *Result
= UINT16_ERROR
;
2275 Status
= RETURN_BUFFER_TOO_SMALL
;
2282 INT64 -> INT32 conversion
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.
2289 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
2294 @param[in] Operand Operand to be converted to new type
2295 @param[out] Result Pointer to the result of conversion
2297 @retval RETURN_SUCCESS Successful conversion
2298 @retval RETURN_BUFFER_TOO_SMALL Overflow
2299 @retval RETURN_INVALID_PARAMETER Result is NULL
2308 RETURN_STATUS Status
;
2310 if (Result
== NULL
) {
2311 return RETURN_INVALID_PARAMETER
;
2314 if ((Operand
>= MIN_INT32
) && (Operand
<= MAX_INT32
)) {
2315 *Result
= (INT32
)Operand
;
2316 Status
= RETURN_SUCCESS
;
2318 *Result
= INT32_ERROR
;
2319 Status
= RETURN_BUFFER_TOO_SMALL
;
2326 INT64 -> UINT32 conversion
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.
2333 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
2338 @param[in] Operand Operand to be converted to new type
2339 @param[out] Result Pointer to the result of conversion
2341 @retval RETURN_SUCCESS Successful conversion
2342 @retval RETURN_BUFFER_TOO_SMALL Overflow
2343 @retval RETURN_INVALID_PARAMETER Result is NULL
2352 RETURN_STATUS Status
;
2354 if (Result
== NULL
) {
2355 return RETURN_INVALID_PARAMETER
;
2358 if ((Operand
>= 0) && (Operand
<= MAX_UINT32
)) {
2359 *Result
= (UINT32
)Operand
;
2360 Status
= RETURN_SUCCESS
;
2362 *Result
= UINT32_ERROR
;
2363 Status
= RETURN_BUFFER_TOO_SMALL
;
2370 INT64 -> UINT64 conversion
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.
2377 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
2382 @param[in] Operand Operand to be converted to new type
2383 @param[out] Result Pointer to the result of conversion
2385 @retval RETURN_SUCCESS Successful conversion
2386 @retval RETURN_BUFFER_TOO_SMALL Overflow
2387 @retval RETURN_INVALID_PARAMETER Result is NULL
2396 RETURN_STATUS Status
;
2398 if (Result
== NULL
) {
2399 return RETURN_INVALID_PARAMETER
;
2403 *Result
= (UINT64
)Operand
;
2404 Status
= RETURN_SUCCESS
;
2406 *Result
= UINT64_ERROR
;
2407 Status
= RETURN_BUFFER_TOO_SMALL
;
2414 UINT64 -> INT8 conversion
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.
2421 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
2426 @param[in] Operand Operand to be converted to new type
2427 @param[out] Result Pointer to the result of conversion
2429 @retval RETURN_SUCCESS Successful conversion
2430 @retval RETURN_BUFFER_TOO_SMALL Overflow
2431 @retval RETURN_INVALID_PARAMETER Result is NULL
2440 RETURN_STATUS Status
;
2442 if (Result
== NULL
) {
2443 return RETURN_INVALID_PARAMETER
;
2446 if (Operand
<= MAX_INT8
) {
2447 *Result
= (INT8
)Operand
;
2448 Status
= RETURN_SUCCESS
;
2450 *Result
= INT8_ERROR
;
2451 Status
= RETURN_BUFFER_TOO_SMALL
;
2458 UINT64 -> CHAR8 conversion
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.
2465 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
2470 @param[in] Operand Operand to be converted to new type
2471 @param[out] Result Pointer to the result of conversion
2473 @retval RETURN_SUCCESS Successful conversion
2474 @retval RETURN_BUFFER_TOO_SMALL Overflow
2475 @retval RETURN_INVALID_PARAMETER Result is NULL
2484 RETURN_STATUS Status
;
2486 if (Result
== NULL
) {
2487 return RETURN_INVALID_PARAMETER
;
2490 if (Operand
<= MAX_INT8
) {
2491 *Result
= (INT8
)Operand
;
2492 Status
= RETURN_SUCCESS
;
2494 *Result
= CHAR8_ERROR
;
2495 Status
= RETURN_BUFFER_TOO_SMALL
;
2502 UINT64 -> UINT8 conversion
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.
2509 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
2514 @param[in] Operand Operand to be converted to new type
2515 @param[out] Result Pointer to the result of conversion
2517 @retval RETURN_SUCCESS Successful conversion
2518 @retval RETURN_BUFFER_TOO_SMALL Overflow
2519 @retval RETURN_INVALID_PARAMETER Result is NULL
2528 RETURN_STATUS Status
;
2530 if (Result
== NULL
) {
2531 return RETURN_INVALID_PARAMETER
;
2534 if (Operand
<= MAX_UINT8
) {
2535 *Result
= (UINT8
)Operand
;
2536 Status
= RETURN_SUCCESS
;
2538 *Result
= UINT8_ERROR
;
2539 Status
= RETURN_BUFFER_TOO_SMALL
;
2546 UINT64 -> INT16 conversion
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.
2553 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
2558 @param[in] Operand Operand to be converted to new type
2559 @param[out] Result Pointer to the result of conversion
2561 @retval RETURN_SUCCESS Successful conversion
2562 @retval RETURN_BUFFER_TOO_SMALL Overflow
2563 @retval RETURN_INVALID_PARAMETER Result is NULL
2572 RETURN_STATUS Status
;
2574 if (Result
== NULL
) {
2575 return RETURN_INVALID_PARAMETER
;
2578 if (Operand
<= MAX_INT16
) {
2579 *Result
= (INT16
)Operand
;
2580 Status
= RETURN_SUCCESS
;
2582 *Result
= INT16_ERROR
;
2583 Status
= RETURN_BUFFER_TOO_SMALL
;
2590 UINT64 -> UINT16 conversion
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.
2597 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
2602 @param[in] Operand Operand to be converted to new type
2603 @param[out] Result Pointer to the result of conversion
2605 @retval RETURN_SUCCESS Successful conversion
2606 @retval RETURN_BUFFER_TOO_SMALL Overflow
2607 @retval RETURN_INVALID_PARAMETER Result is NULL
2611 SafeUint64ToUint16 (
2616 RETURN_STATUS Status
;
2618 if (Result
== NULL
) {
2619 return RETURN_INVALID_PARAMETER
;
2622 if (Operand
<= MAX_UINT16
) {
2623 *Result
= (UINT16
)Operand
;
2624 Status
= RETURN_SUCCESS
;
2626 *Result
= UINT16_ERROR
;
2627 Status
= RETURN_BUFFER_TOO_SMALL
;
2634 UINT64 -> INT32 conversion
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.
2641 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
2646 @param[in] Operand Operand to be converted to new type
2647 @param[out] Result Pointer to the result of conversion
2649 @retval RETURN_SUCCESS Successful conversion
2650 @retval RETURN_BUFFER_TOO_SMALL Overflow
2651 @retval RETURN_INVALID_PARAMETER Result is NULL
2660 RETURN_STATUS Status
;
2662 if (Result
== NULL
) {
2663 return RETURN_INVALID_PARAMETER
;
2666 if (Operand
<= MAX_INT32
) {
2667 *Result
= (INT32
)Operand
;
2668 Status
= RETURN_SUCCESS
;
2670 *Result
= INT32_ERROR
;
2671 Status
= RETURN_BUFFER_TOO_SMALL
;
2678 UINT64 -> UINT32 conversion
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.
2685 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
2690 @param[in] Operand Operand to be converted to new type
2691 @param[out] Result Pointer to the result of conversion
2693 @retval RETURN_SUCCESS Successful conversion
2694 @retval RETURN_BUFFER_TOO_SMALL Overflow
2695 @retval RETURN_INVALID_PARAMETER Result is NULL
2699 SafeUint64ToUint32 (
2704 RETURN_STATUS Status
;
2706 if (Result
== NULL
) {
2707 return RETURN_INVALID_PARAMETER
;
2710 if (Operand
<= MAX_UINT32
) {
2711 *Result
= (UINT32
)Operand
;
2712 Status
= RETURN_SUCCESS
;
2714 *Result
= UINT32_ERROR
;
2715 Status
= RETURN_BUFFER_TOO_SMALL
;
2722 UINT64 -> INTN conversion
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.
2729 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
2734 @param[in] Operand Operand to be converted to new type
2735 @param[out] Result Pointer to the result of conversion
2737 @retval RETURN_SUCCESS Successful conversion
2738 @retval RETURN_BUFFER_TOO_SMALL Overflow
2739 @retval RETURN_INVALID_PARAMETER Result is NULL
2748 RETURN_STATUS Status
;
2750 if (Result
== NULL
) {
2751 return RETURN_INVALID_PARAMETER
;
2754 if (Operand
<= MAX_INTN
) {
2755 *Result
= (INTN
)Operand
;
2756 Status
= RETURN_SUCCESS
;
2758 *Result
= INTN_ERROR
;
2759 Status
= RETURN_BUFFER_TOO_SMALL
;
2766 UINT64 -> INT64 conversion
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.
2773 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
2778 @param[in] Operand Operand to be converted to new type
2779 @param[out] Result Pointer to the result of conversion
2781 @retval RETURN_SUCCESS Successful conversion
2782 @retval RETURN_BUFFER_TOO_SMALL Overflow
2783 @retval RETURN_INVALID_PARAMETER Result is NULL
2792 RETURN_STATUS Status
;
2794 if (Result
== NULL
) {
2795 return RETURN_INVALID_PARAMETER
;
2798 if (Operand
<= MAX_INT64
) {
2799 *Result
= (INT64
)Operand
;
2800 Status
= RETURN_SUCCESS
;
2802 *Result
= INT64_ERROR
;
2803 Status
= RETURN_BUFFER_TOO_SMALL
;
2810 // Addition functions
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.
2821 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
2830 @retval RETURN_SUCCESS Successful addition
2831 @retval RETURN_BUFFER_TOO_SMALL Overflow
2832 @retval RETURN_INVALID_PARAMETER Result is NULL
2842 RETURN_STATUS Status
;
2844 if (Result
== NULL
) {
2845 return RETURN_INVALID_PARAMETER
;
2848 if (((UINT8
)(Augend
+ Addend
)) >= Augend
) {
2849 *Result
= (UINT8
)(Augend
+ Addend
);
2850 Status
= RETURN_SUCCESS
;
2852 *Result
= UINT8_ERROR
;
2853 Status
= RETURN_BUFFER_TOO_SMALL
;
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.
2867 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
2876 @retval RETURN_SUCCESS Successful addition
2877 @retval RETURN_BUFFER_TOO_SMALL Overflow
2878 @retval RETURN_INVALID_PARAMETER Result is NULL
2888 RETURN_STATUS Status
;
2890 if (Result
== NULL
) {
2891 return RETURN_INVALID_PARAMETER
;
2894 if (((UINT16
)(Augend
+ Addend
)) >= Augend
) {
2895 *Result
= (UINT16
)(Augend
+ Addend
);
2896 Status
= RETURN_SUCCESS
;
2898 *Result
= UINT16_ERROR
;
2899 Status
= RETURN_BUFFER_TOO_SMALL
;
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.
2913 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
2922 @retval RETURN_SUCCESS Successful addition
2923 @retval RETURN_BUFFER_TOO_SMALL Overflow
2924 @retval RETURN_INVALID_PARAMETER Result is NULL
2934 RETURN_STATUS Status
;
2936 if (Result
== NULL
) {
2937 return RETURN_INVALID_PARAMETER
;
2940 if ((Augend
+ Addend
) >= Augend
) {
2941 *Result
= (Augend
+ Addend
);
2942 Status
= RETURN_SUCCESS
;
2944 *Result
= UINT32_ERROR
;
2945 Status
= RETURN_BUFFER_TOO_SMALL
;
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.
2959 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
2968 @retval RETURN_SUCCESS Successful addition
2969 @retval RETURN_BUFFER_TOO_SMALL Overflow
2970 @retval RETURN_INVALID_PARAMETER Result is NULL
2980 RETURN_STATUS Status
;
2982 if (Result
== NULL
) {
2983 return RETURN_INVALID_PARAMETER
;
2986 if ((Augend
+ Addend
) >= Augend
) {
2987 *Result
= (Augend
+ Addend
);
2988 Status
= RETURN_SUCCESS
;
2990 *Result
= UINT64_ERROR
;
2991 Status
= RETURN_BUFFER_TOO_SMALL
;
2998 // Subtraction functions
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.
3009 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
3018 @retval RETURN_SUCCESS Successful subtraction
3019 @retval RETURN_BUFFER_TOO_SMALL Underflow
3020 @retval RETURN_INVALID_PARAMETER Result is NULL
3026 IN UINT8 Subtrahend
,
3030 RETURN_STATUS Status
;
3032 if (Result
== NULL
) {
3033 return RETURN_INVALID_PARAMETER
;
3036 if (Minuend
>= Subtrahend
) {
3037 *Result
= (UINT8
)(Minuend
- Subtrahend
);
3038 Status
= RETURN_SUCCESS
;
3040 *Result
= UINT8_ERROR
;
3041 Status
= RETURN_BUFFER_TOO_SMALL
;
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.
3055 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
3064 @retval RETURN_SUCCESS Successful subtraction
3065 @retval RETURN_BUFFER_TOO_SMALL Underflow
3066 @retval RETURN_INVALID_PARAMETER Result is NULL
3072 IN UINT16 Subtrahend
,
3076 RETURN_STATUS Status
;
3078 if (Result
== NULL
) {
3079 return RETURN_INVALID_PARAMETER
;
3082 if (Minuend
>= Subtrahend
) {
3083 *Result
= (UINT16
)(Minuend
- Subtrahend
);
3084 Status
= RETURN_SUCCESS
;
3086 *Result
= UINT16_ERROR
;
3087 Status
= RETURN_BUFFER_TOO_SMALL
;
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.
3101 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
3110 @retval RETURN_SUCCESS Successful subtraction
3111 @retval RETURN_BUFFER_TOO_SMALL Underflow
3112 @retval RETURN_INVALID_PARAMETER Result is NULL
3118 IN UINT32 Subtrahend
,
3122 RETURN_STATUS Status
;
3124 if (Result
== NULL
) {
3125 return RETURN_INVALID_PARAMETER
;
3128 if (Minuend
>= Subtrahend
) {
3129 *Result
= (Minuend
- Subtrahend
);
3130 Status
= RETURN_SUCCESS
;
3132 *Result
= UINT32_ERROR
;
3133 Status
= RETURN_BUFFER_TOO_SMALL
;
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.
3147 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
3156 @retval RETURN_SUCCESS Successful subtraction
3157 @retval RETURN_BUFFER_TOO_SMALL Underflow
3158 @retval RETURN_INVALID_PARAMETER Result is NULL
3164 IN UINT64 Subtrahend
,
3168 RETURN_STATUS Status
;
3170 if (Result
== NULL
) {
3171 return RETURN_INVALID_PARAMETER
;
3174 if (Minuend
>= Subtrahend
) {
3175 *Result
= (Minuend
- Subtrahend
);
3176 Status
= RETURN_SUCCESS
;
3178 *Result
= UINT64_ERROR
;
3179 Status
= RETURN_BUFFER_TOO_SMALL
;
3186 // Multiplication functions
3190 UINT8 multiplication
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.
3197 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
3206 @retval RETURN_SUCCESS Successful multiplication
3207 @retval RETURN_BUFFER_TOO_SMALL Overflow
3208 @retval RETURN_INVALID_PARAMETER Result is NULL
3213 IN UINT8 Multiplicand
,
3214 IN UINT8 Multiplier
,
3218 UINT32 IntermediateResult
;
3220 IntermediateResult
= ((UINT32
)Multiplicand
) *((UINT32
)Multiplier
);
3222 return SafeUint32ToUint8 (IntermediateResult
, Result
);
3226 UINT16 multiplication
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.
3233 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
3242 @retval RETURN_SUCCESS Successful multiplication
3243 @retval RETURN_BUFFER_TOO_SMALL Overflow
3244 @retval RETURN_INVALID_PARAMETER Result is NULL
3249 IN UINT16 Multiplicand
,
3250 IN UINT16 Multiplier
,
3254 UINT32 IntermediateResult
;
3256 IntermediateResult
= ((UINT32
)Multiplicand
) *((UINT32
)Multiplier
);
3258 return SafeUint32ToUint16 (IntermediateResult
, Result
);
3262 UINT32 multiplication
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.
3269 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
3278 @retval RETURN_SUCCESS Successful multiplication
3279 @retval RETURN_BUFFER_TOO_SMALL Overflow
3280 @retval RETURN_INVALID_PARAMETER Result is NULL
3285 IN UINT32 Multiplicand
,
3286 IN UINT32 Multiplier
,
3290 UINT64 IntermediateResult
;
3292 IntermediateResult
= ((UINT64
) Multiplicand
) *((UINT64
) Multiplier
);
3294 return SafeUint64ToUint32 (IntermediateResult
, Result
);
3298 UINT64 multiplication
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.
3305 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
3314 @retval RETURN_SUCCESS Successful multiplication
3315 @retval RETURN_BUFFER_TOO_SMALL Overflow
3316 @retval RETURN_INVALID_PARAMETER Result is NULL
3321 IN UINT64 Multiplicand
,
3322 IN UINT64 Multiplier
,
3326 RETURN_STATUS Status
;
3334 UINT64 UnsignedResult
;
3336 if (Result
== NULL
) {
3337 return RETURN_INVALID_PARAMETER
;
3344 Status
= RETURN_BUFFER_TOO_SMALL
;
3347 // 64x64 into 128 is like 32.32 x 32.32.
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)
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.
3360 DwordA
= (UINT32
)RShiftU64 (Multiplicand
, 32);
3361 DwordC
= (UINT32
)RShiftU64 (Multiplier
, 32);
3364 // common case -- if high dwords are both zero, no chance for overflow
3366 if ((DwordA
== 0) && (DwordC
== 0)) {
3367 DwordB
= (UINT32
)Multiplicand
;
3368 DwordD
= (UINT32
)Multiplier
;
3370 *Result
= (((UINT64
)DwordB
) *(UINT64
)DwordD
);
3371 Status
= RETURN_SUCCESS
;
3374 // a * c must be 0 or there would be bits set in the high 64-bits
3376 if ((DwordA
== 0) ||
3378 DwordD
= (UINT32
)Multiplier
;
3381 // a * d must be less than 2^32 or there would be bits set in the high 64-bits
3383 ProductAD
= (((UINT64
)DwordA
) *(UINT64
)DwordD
);
3384 if ((ProductAD
& 0xffffffff00000000) == 0) {
3385 DwordB
= (UINT32
)Multiplicand
;
3388 // b * c must be less than 2^32 or there would be bits set in the high 64-bits
3390 ProductBC
= (((UINT64
)DwordB
) *(UINT64
)DwordC
);
3391 if ((ProductBC
& 0xffffffff00000000) == 0) {
3393 // now sum them all up checking for overflow.
3394 // shifting is safe because we already checked for overflow above
3396 if (!RETURN_ERROR (SafeUint64Add (LShiftU64 (ProductBC
, 32), LShiftU64 (ProductAD
, 32), &UnsignedResult
))) {
3400 ProductBD
= (((UINT64
)DwordB
) *(UINT64
)DwordD
);
3402 if (!RETURN_ERROR (SafeUint64Add (UnsignedResult
, ProductBD
, &UnsignedResult
))) {
3403 *Result
= UnsignedResult
;
3404 Status
= RETURN_SUCCESS
;
3412 if (RETURN_ERROR (Status
)) {
3413 *Result
= UINT64_ERROR
;
3419 // Signed operations
3421 // Strongly consider using unsigned numbers.
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.
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.
3435 // Likewise you can overflow a buffer with a non-overflowed unsigned index.
3439 // Signed addition functions
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.
3450 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
3459 @retval RETURN_SUCCESS Successful addition
3460 @retval RETURN_BUFFER_TOO_SMALL Overflow
3461 @retval RETURN_INVALID_PARAMETER Result is NULL
3471 return SafeInt32ToInt8 (((INT32
)Augend
) + ((INT32
)Addend
), Result
);
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.
3482 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
3491 @retval RETURN_SUCCESS Successful addition
3492 @retval RETURN_BUFFER_TOO_SMALL Overflow
3493 @retval RETURN_INVALID_PARAMETER Result is NULL
3506 if (Result
== NULL
) {
3507 return RETURN_INVALID_PARAMETER
;
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
;
3516 if (Addend32
< 0 || Addend32
> MAX_INT8
) {
3517 *Result
= CHAR8_ERROR
;
3518 return RETURN_BUFFER_TOO_SMALL
;
3521 return SafeInt32ToChar8 (Augend32
+ Addend32
, Result
);
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.
3532 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
3541 @retval RETURN_SUCCESS Successful addition
3542 @retval RETURN_BUFFER_TOO_SMALL Overflow
3543 @retval RETURN_INVALID_PARAMETER Result is NULL
3553 return SafeInt32ToInt16 (((INT32
)Augend
) + ((INT32
)Addend
), Result
);
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.
3564 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
3573 @retval RETURN_SUCCESS Successful addition
3574 @retval RETURN_BUFFER_TOO_SMALL Overflow
3575 @retval RETURN_INVALID_PARAMETER Result is NULL
3585 return SafeInt64ToInt32 (((INT64
)Augend
) + ((INT64
)Addend
), Result
);
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.
3596 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
3605 @retval RETURN_SUCCESS Successful addition
3606 @retval RETURN_BUFFER_TOO_SMALL Overflow
3607 @retval RETURN_INVALID_PARAMETER Result is NULL
3617 RETURN_STATUS Status
;
3619 if (Result
== NULL
) {
3620 return RETURN_INVALID_PARAMETER
;
3624 // * An Addend of zero can never cause underflow or overflow.
3626 // * A positive Addend can only cause overflow. The overflow condition is
3628 // (Augend + Addend) > MAX_INT64
3630 // Subtracting Addend from both sides yields
3632 // Augend > (MAX_INT64 - Addend)
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:
3637 // 0 < Addend <= MAX_INT64
3639 // Multiplying all three sides by (-1) yields
3641 // 0 > (-Addend) >= (-MAX_INT64)
3643 // Adding MAX_INT64 to all three sides yields
3645 // MAX_INT64 > (MAX_INT64 - Addend) >= 0
3647 // * A negative Addend can only cause underflow. The underflow condition is
3649 // (Augend + Addend) < MIN_INT64
3651 // Subtracting Addend from both sides yields
3653 // Augend < (MIN_INT64 - Addend)
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:
3658 // MIN_INT64 <= Addend < 0
3660 // Multiplying all three sides by (-1) yields
3662 // (-MIN_INT64) >= (-Addend) > 0
3664 // Adding MIN_INT64 to all three sides yields
3666 // 0 >= (MIN_INT64 - Addend) > MIN_INT64
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
;
3673 *Result
= Augend
+ Addend
;
3674 Status
= RETURN_SUCCESS
;
3681 // Signed subtraction functions
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.
3692 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
3701 @retval RETURN_SUCCESS Successful subtraction
3702 @retval RETURN_BUFFER_TOO_SMALL Underflow
3703 @retval RETURN_INVALID_PARAMETER Result is NULL
3713 return SafeInt32ToInt8 (((INT32
)Minuend
) - ((INT32
)Subtrahend
), Result
);
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.
3724 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
3733 @retval RETURN_SUCCESS Successful subtraction
3734 @retval RETURN_BUFFER_TOO_SMALL Underflow
3735 @retval RETURN_INVALID_PARAMETER Result is NULL
3741 IN CHAR8 Subtrahend
,
3748 if (Result
== NULL
) {
3749 return RETURN_INVALID_PARAMETER
;
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
;
3758 if (Subtrahend32
< 0 || Subtrahend32
> MAX_INT8
) {
3759 *Result
= CHAR8_ERROR
;
3760 return RETURN_BUFFER_TOO_SMALL
;
3763 return SafeInt32ToChar8 (Minuend32
- Subtrahend32
, Result
);
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.
3774 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
3783 @retval RETURN_SUCCESS Successful subtraction
3784 @retval RETURN_BUFFER_TOO_SMALL Underflow
3785 @retval RETURN_INVALID_PARAMETER Result is NULL
3791 IN INT16 Subtrahend
,
3795 return SafeInt32ToInt16 (((INT32
)Minuend
) - ((INT32
)Subtrahend
), Result
);
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.
3806 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
3815 @retval RETURN_SUCCESS Successful subtraction
3816 @retval RETURN_BUFFER_TOO_SMALL Underflow
3817 @retval RETURN_INVALID_PARAMETER Result is NULL
3823 IN INT32 Subtrahend
,
3827 return SafeInt64ToInt32 (((INT64
)Minuend
) - ((INT64
)Subtrahend
), Result
);
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.
3838 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
3847 @retval RETURN_SUCCESS Successful subtraction
3848 @retval RETURN_BUFFER_TOO_SMALL Underflow
3849 @retval RETURN_INVALID_PARAMETER Result is NULL
3855 IN INT64 Subtrahend
,
3859 RETURN_STATUS Status
;
3861 if (Result
== NULL
) {
3862 return RETURN_INVALID_PARAMETER
;
3866 // * A Subtrahend of zero can never cause underflow or overflow.
3868 // * A positive Subtrahend can only cause underflow. The underflow condition
3871 // (Minuend - Subtrahend) < MIN_INT64
3873 // Adding Subtrahend to both sides yields
3875 // Minuend < (MIN_INT64 + Subtrahend)
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:
3880 // 0 < Subtrahend <= MAX_INT64
3882 // Adding MIN_INT64 to all three sides yields
3884 // MIN_INT64 < (MIN_INT64 + Subtrahend) <= (MIN_INT64 + MAX_INT64) = -1
3886 // * A negative Subtrahend can only cause overflow. The overflow condition is
3888 // (Minuend - Subtrahend) > MAX_INT64
3890 // Adding Subtrahend to both sides yields
3892 // Minuend > (MAX_INT64 + Subtrahend)
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:
3897 // MIN_INT64 <= Subtrahend < 0
3899 // Adding MAX_INT64 to all three sides yields
3901 // -1 = (MAX_INT64 + MIN_INT64) <= (MAX_INT64 + Subtrahend) < MAX_INT64
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
;
3908 *Result
= Minuend
- Subtrahend
;
3909 Status
= RETURN_SUCCESS
;
3916 // Signed multiplication functions
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.
3927 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
3936 @retval RETURN_SUCCESS Successful multiplication
3937 @retval RETURN_BUFFER_TOO_SMALL Overflow
3938 @retval RETURN_INVALID_PARAMETER Result is NULL
3943 IN INT8 Multiplicand
,
3948 return SafeInt32ToInt8 (((INT32
)Multiplier
) *((INT32
)Multiplicand
), Result
);
3952 CHAR8 multiplication
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.
3959 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
3968 @retval RETURN_SUCCESS Successful multiplication
3969 @retval RETURN_BUFFER_TOO_SMALL Overflow
3970 @retval RETURN_INVALID_PARAMETER Result is NULL
3975 IN CHAR8 Multiplicand
,
3976 IN CHAR8 Multiplier
,
3980 INT32 Multiplicand32
;
3983 if (Result
== NULL
) {
3984 return RETURN_INVALID_PARAMETER
;
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
;
3993 if (Multiplier32
< 0 || Multiplier32
> MAX_INT8
) {
3994 *Result
= CHAR8_ERROR
;
3995 return RETURN_BUFFER_TOO_SMALL
;
3998 return SafeInt32ToChar8 (Multiplicand32
* Multiplier32
, Result
);
4002 INT16 multiplication
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.
4009 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
4018 @retval RETURN_SUCCESS Successful multiplication
4019 @retval RETURN_BUFFER_TOO_SMALL Overflow
4020 @retval RETURN_INVALID_PARAMETER Result is NULL
4025 IN INT16 Multiplicand
,
4026 IN INT16 Multiplier
,
4030 return SafeInt32ToInt16 (((INT32
)Multiplicand
) *((INT32
)Multiplier
), Result
);
4034 INT32 multiplication
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.
4041 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
4050 @retval RETURN_SUCCESS Successful multiplication
4051 @retval RETURN_BUFFER_TOO_SMALL Overflow
4052 @retval RETURN_INVALID_PARAMETER Result is NULL
4057 IN INT32 Multiplicand
,
4058 IN INT32 Multiplier
,
4062 return SafeInt64ToInt32 (MultS64x64 (Multiplicand
, Multiplier
), Result
);
4066 INT64 multiplication
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.
4073 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
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.
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
4082 @retval RETURN_SUCCESS Successful multiplication
4083 @retval RETURN_BUFFER_TOO_SMALL Overflow
4084 @retval RETURN_INVALID_PARAMETER Result is NULL
4089 IN INT64 Multiplicand
,
4090 IN INT64 Multiplier
,
4094 RETURN_STATUS Status
;
4095 UINT64 UnsignedMultiplicand
;
4096 UINT64 UnsignedMultiplier
;
4097 UINT64 UnsignedResult
;
4099 if (Result
== NULL
) {
4100 return RETURN_INVALID_PARAMETER
;
4104 // Split into sign and magnitude, do unsigned operation, apply sign.
4106 if (Multiplicand
< 0) {
4108 // Avoid negating the most negative number.
4110 UnsignedMultiplicand
= ((UINT64
)(- (Multiplicand
+ 1))) + 1;
4112 UnsignedMultiplicand
= (UINT64
)Multiplicand
;
4115 if (Multiplier
< 0) {
4117 // Avoid negating the most negative number.
4119 UnsignedMultiplier
= ((UINT64
)(- (Multiplier
+ 1))) + 1;
4121 UnsignedMultiplier
= (UINT64
)Multiplier
;
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
;
4133 *Result
= - ((INT64
)UnsignedResult
);
4136 if (UnsignedResult
> MAX_INT64
) {
4137 *Result
= INT64_ERROR
;
4138 Status
= RETURN_BUFFER_TOO_SMALL
;
4140 *Result
= (INT64
)UnsignedResult
;
4144 *Result
= INT64_ERROR
;