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>
17 // Magnitude of MIN_INT64 as expressed by a UINT64 number.
19 #define MIN_INT64_MAGNITUDE (((UINT64)(- (MIN_INT64 + 1))) + 1)
22 // Conversion functions
24 // There are three reasons for having conversion functions:
26 // 1. We are converting from a signed type to an unsigned type of the same
27 // size, or vice-versa.
29 // 2. We are converting to a smaller type, and we could therefore possibly
32 // 3. We are converting to a bigger type, and we are signed and the type we are
33 // converting to is unsigned.
37 INT8 -> UINT8 conversion
39 Converts the value specified by Operand to a value specified by Result type
40 and stores the converted value into the caller allocated output buffer
41 specified by Result. The caller must pass in a Result buffer that is at
42 least as large as the Result type.
44 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
46 If the conversion results in an overflow or an underflow condition, then
47 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
49 @param[in] Operand Operand to be converted to new type
50 @param[out] Result Pointer to the result of conversion
52 @retval RETURN_SUCCESS Successful conversion
53 @retval RETURN_BUFFER_TOO_SMALL Overflow
54 @retval RETURN_INVALID_PARAMETER Result is NULL
66 return RETURN_INVALID_PARAMETER
;
70 *Result
= (UINT8
)Operand
;
71 Status
= RETURN_SUCCESS
;
73 *Result
= UINT8_ERROR
;
74 Status
= RETURN_BUFFER_TOO_SMALL
;
81 INT8 -> CHAR8 conversion
83 Converts the value specified by Operand to a value specified by Result type
84 and stores the converted value into the caller allocated output buffer
85 specified by Result. The caller must pass in a Result buffer that is at
86 least as large as the Result type.
88 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
90 If the conversion results in an overflow or an underflow condition, then
91 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
93 @param[in] Operand Operand to be converted to new type
94 @param[out] Result Pointer to the result of conversion
96 @retval RETURN_SUCCESS Successful conversion
97 @retval RETURN_BUFFER_TOO_SMALL Overflow
98 @retval RETURN_INVALID_PARAMETER Result is NULL
107 RETURN_STATUS Status
;
109 if (Result
== NULL
) {
110 return RETURN_INVALID_PARAMETER
;
114 *Result
= (CHAR8
)Operand
;
115 Status
= RETURN_SUCCESS
;
117 *Result
= CHAR8_ERROR
;
118 Status
= RETURN_BUFFER_TOO_SMALL
;
125 INT8 -> UINT16 conversion
127 Converts the value specified by Operand to a value specified by Result type
128 and stores the converted value into the caller allocated output buffer
129 specified by Result. The caller must pass in a Result buffer that is at
130 least as large as the Result type.
132 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
134 If the conversion results in an overflow or an underflow condition, then
135 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
137 @param[in] Operand Operand to be converted to new type
138 @param[out] Result Pointer to the result of conversion
140 @retval RETURN_SUCCESS Successful conversion
141 @retval RETURN_BUFFER_TOO_SMALL Overflow
142 @retval RETURN_INVALID_PARAMETER Result is NULL
151 RETURN_STATUS Status
;
153 if (Result
== NULL
) {
154 return RETURN_INVALID_PARAMETER
;
158 *Result
= (UINT16
)Operand
;
159 Status
= RETURN_SUCCESS
;
161 *Result
= UINT16_ERROR
;
162 Status
= RETURN_BUFFER_TOO_SMALL
;
169 INT8 -> UINT32 conversion
171 Converts the value specified by Operand to a value specified by Result type
172 and stores the converted value into the caller allocated output buffer
173 specified by Result. The caller must pass in a Result buffer that is at
174 least as large as the Result type.
176 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
178 If the conversion results in an overflow or an underflow condition, then
179 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
181 @param[in] Operand Operand to be converted to new type
182 @param[out] Result Pointer to the result of conversion
184 @retval RETURN_SUCCESS Successful conversion
185 @retval RETURN_BUFFER_TOO_SMALL Overflow
186 @retval RETURN_INVALID_PARAMETER Result is NULL
195 RETURN_STATUS Status
;
197 if (Result
== NULL
) {
198 return RETURN_INVALID_PARAMETER
;
202 *Result
= (UINT32
)Operand
;
203 Status
= RETURN_SUCCESS
;
205 *Result
= UINT32_ERROR
;
206 Status
= RETURN_BUFFER_TOO_SMALL
;
213 INT8 -> UINTN conversion
215 Converts the value specified by Operand to a value specified by Result type
216 and stores the converted value into the caller allocated output buffer
217 specified by Result. The caller must pass in a Result buffer that is at
218 least as large as the Result type.
220 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
222 If the conversion results in an overflow or an underflow condition, then
223 Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
225 @param[in] Operand Operand to be converted to new type
226 @param[out] Result Pointer to the result of conversion
228 @retval RETURN_SUCCESS Successful conversion
229 @retval RETURN_BUFFER_TOO_SMALL Overflow
230 @retval RETURN_INVALID_PARAMETER Result is NULL
239 RETURN_STATUS Status
;
241 if (Result
== NULL
) {
242 return RETURN_INVALID_PARAMETER
;
246 *Result
= (UINTN
)Operand
;
247 Status
= RETURN_SUCCESS
;
249 *Result
= UINTN_ERROR
;
250 Status
= RETURN_BUFFER_TOO_SMALL
;
257 INT8 -> UINT64 conversion
259 Converts the value specified by Operand to a value specified by Result type
260 and stores the converted value into the caller allocated output buffer
261 specified by Result. The caller must pass in a Result buffer that is at
262 least as large as the Result type.
264 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
266 If the conversion results in an overflow or an underflow condition, then
267 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
269 @param[in] Operand Operand to be converted to new type
270 @param[out] Result Pointer to the result of conversion
272 @retval RETURN_SUCCESS Successful conversion
273 @retval RETURN_BUFFER_TOO_SMALL Overflow
274 @retval RETURN_INVALID_PARAMETER Result is NULL
283 RETURN_STATUS Status
;
285 if (Result
== NULL
) {
286 return RETURN_INVALID_PARAMETER
;
290 *Result
= (UINT64
)Operand
;
291 Status
= RETURN_SUCCESS
;
293 *Result
= UINT64_ERROR
;
294 Status
= RETURN_BUFFER_TOO_SMALL
;
301 UINT8 -> INT8 conversion
303 Converts the value specified by Operand to a value specified by Result type
304 and stores the converted value into the caller allocated output buffer
305 specified by Result. The caller must pass in a Result buffer that is at
306 least as large as the Result type.
308 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
310 If the conversion results in an overflow or an underflow condition, then
311 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
313 @param[in] Operand Operand to be converted to new type
314 @param[out] Result Pointer to the result of conversion
316 @retval RETURN_SUCCESS Successful conversion
317 @retval RETURN_BUFFER_TOO_SMALL Overflow
318 @retval RETURN_INVALID_PARAMETER Result is NULL
327 RETURN_STATUS Status
;
329 if (Result
== NULL
) {
330 return RETURN_INVALID_PARAMETER
;
333 if (Operand
<= MAX_INT8
) {
334 *Result
= (INT8
)Operand
;
335 Status
= RETURN_SUCCESS
;
337 *Result
= INT8_ERROR
;
338 Status
= RETURN_BUFFER_TOO_SMALL
;
345 UINT8 -> CHAR8 conversion
347 Converts the value specified by Operand to a value specified by Result type
348 and stores the converted value into the caller allocated output buffer
349 specified by Result. The caller must pass in a Result buffer that is at
350 least as large as the Result type.
352 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
354 If the conversion results in an overflow or an underflow condition, then
355 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
357 @param[in] Operand Operand to be converted to new type
358 @param[out] Result Pointer to the result of conversion
360 @retval RETURN_SUCCESS Successful conversion
361 @retval RETURN_BUFFER_TOO_SMALL Overflow
362 @retval RETURN_INVALID_PARAMETER Result is NULL
371 RETURN_STATUS Status
;
373 if (Result
== NULL
) {
374 return RETURN_INVALID_PARAMETER
;
377 if (Operand
<= MAX_INT8
) {
378 *Result
= (CHAR8
)Operand
;
379 Status
= RETURN_SUCCESS
;
381 *Result
= CHAR8_ERROR
;
382 Status
= RETURN_BUFFER_TOO_SMALL
;
389 INT16 -> INT8 conversion
391 Converts the value specified by Operand to a value specified by Result type
392 and stores the converted value into the caller allocated output buffer
393 specified by Result. The caller must pass in a Result buffer that is at
394 least as large as the Result type.
396 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
398 If the conversion results in an overflow or an underflow condition, then
399 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
401 @param[in] Operand Operand to be converted to new type
402 @param[out] Result Pointer to the result of conversion
404 @retval RETURN_SUCCESS Successful conversion
405 @retval RETURN_BUFFER_TOO_SMALL Overflow
406 @retval RETURN_INVALID_PARAMETER Result is NULL
415 RETURN_STATUS Status
;
417 if (Result
== NULL
) {
418 return RETURN_INVALID_PARAMETER
;
421 if ((Operand
>= MIN_INT8
) && (Operand
<= MAX_INT8
)) {
422 *Result
= (INT8
)Operand
;
423 Status
= RETURN_SUCCESS
;
425 *Result
= INT8_ERROR
;
426 Status
= RETURN_BUFFER_TOO_SMALL
;
433 INT16 -> CHAR8 conversion
435 Converts the value specified by Operand to a value specified by Result type
436 and stores the converted value into the caller allocated output buffer
437 specified by Result. The caller must pass in a Result buffer that is at
438 least as large as the Result type.
440 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
442 If the conversion results in an overflow or an underflow condition, then
443 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
445 @param[in] Operand Operand to be converted to new type
446 @param[out] Result Pointer to the result of conversion
448 @retval RETURN_SUCCESS Successful conversion
449 @retval RETURN_BUFFER_TOO_SMALL Overflow
450 @retval RETURN_INVALID_PARAMETER Result is NULL
459 RETURN_STATUS Status
;
461 if (Result
== NULL
) {
462 return RETURN_INVALID_PARAMETER
;
465 if ((Operand
>= 0) && (Operand
<= MAX_INT8
)) {
466 *Result
= (CHAR8
)Operand
;
467 Status
= RETURN_SUCCESS
;
469 *Result
= CHAR8_ERROR
;
470 Status
= RETURN_BUFFER_TOO_SMALL
;
477 INT16 -> UINT8 conversion
479 Converts the value specified by Operand to a value specified by Result type
480 and stores the converted value into the caller allocated output buffer
481 specified by Result. The caller must pass in a Result buffer that is at
482 least as large as the Result type.
484 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
486 If the conversion results in an overflow or an underflow condition, then
487 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
489 @param[in] Operand Operand to be converted to new type
490 @param[out] Result Pointer to the result of conversion
492 @retval RETURN_SUCCESS Successful conversion
493 @retval RETURN_BUFFER_TOO_SMALL Overflow
494 @retval RETURN_INVALID_PARAMETER Result is NULL
503 RETURN_STATUS Status
;
505 if (Result
== NULL
) {
506 return RETURN_INVALID_PARAMETER
;
509 if ((Operand
>= 0) && (Operand
<= MAX_UINT8
)) {
510 *Result
= (UINT8
)Operand
;
511 Status
= RETURN_SUCCESS
;
513 *Result
= UINT8_ERROR
;
514 Status
= RETURN_BUFFER_TOO_SMALL
;
521 INT16 -> UINT16 conversion
523 Converts the value specified by Operand to a value specified by Result type
524 and stores the converted value into the caller allocated output buffer
525 specified by Result. The caller must pass in a Result buffer that is at
526 least as large as the Result type.
528 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
530 If the conversion results in an overflow or an underflow condition, then
531 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
533 @param[in] Operand Operand to be converted to new type
534 @param[out] Result Pointer to the result of conversion
536 @retval RETURN_SUCCESS Successful conversion
537 @retval RETURN_BUFFER_TOO_SMALL Overflow
538 @retval RETURN_INVALID_PARAMETER Result is NULL
547 RETURN_STATUS Status
;
549 if (Result
== NULL
) {
550 return RETURN_INVALID_PARAMETER
;
554 *Result
= (UINT16
)Operand
;
555 Status
= RETURN_SUCCESS
;
557 *Result
= UINT16_ERROR
;
558 Status
= RETURN_BUFFER_TOO_SMALL
;
565 INT16 -> UINT32 conversion
567 Converts the value specified by Operand to a value specified by Result type
568 and stores the converted value into the caller allocated output buffer
569 specified by Result. The caller must pass in a Result buffer that is at
570 least as large as the Result type.
572 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
574 If the conversion results in an overflow or an underflow condition, then
575 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
577 @param[in] Operand Operand to be converted to new type
578 @param[out] Result Pointer to the result of conversion
580 @retval RETURN_SUCCESS Successful conversion
581 @retval RETURN_BUFFER_TOO_SMALL Overflow
582 @retval RETURN_INVALID_PARAMETER Result is NULL
591 RETURN_STATUS Status
;
593 if (Result
== NULL
) {
594 return RETURN_INVALID_PARAMETER
;
598 *Result
= (UINT32
)Operand
;
599 Status
= RETURN_SUCCESS
;
601 *Result
= UINT32_ERROR
;
602 Status
= RETURN_BUFFER_TOO_SMALL
;
609 INT16 -> UINTN conversion
611 Converts the value specified by Operand to a value specified by Result type
612 and stores the converted value into the caller allocated output buffer
613 specified by Result. The caller must pass in a Result buffer that is at
614 least as large as the Result type.
616 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
618 If the conversion results in an overflow or an underflow condition, then
619 Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
621 @param[in] Operand Operand to be converted to new type
622 @param[out] Result Pointer to the result of conversion
624 @retval RETURN_SUCCESS Successful conversion
625 @retval RETURN_BUFFER_TOO_SMALL Overflow
626 @retval RETURN_INVALID_PARAMETER Result is NULL
635 RETURN_STATUS Status
;
637 if (Result
== NULL
) {
638 return RETURN_INVALID_PARAMETER
;
642 *Result
= (UINTN
)Operand
;
643 Status
= RETURN_SUCCESS
;
645 *Result
= UINTN_ERROR
;
646 Status
= RETURN_BUFFER_TOO_SMALL
;
653 INT16 -> UINT64 conversion
655 Converts the value specified by Operand to a value specified by Result type
656 and stores the converted value into the caller allocated output buffer
657 specified by Result. The caller must pass in a Result buffer that is at
658 least as large as the Result type.
660 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
662 If the conversion results in an overflow or an underflow condition, then
663 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
665 @param[in] Operand Operand to be converted to new type
666 @param[out] Result Pointer to the result of conversion
668 @retval RETURN_SUCCESS Successful conversion
669 @retval RETURN_BUFFER_TOO_SMALL Overflow
670 @retval RETURN_INVALID_PARAMETER Result is NULL
679 RETURN_STATUS Status
;
681 if (Result
== NULL
) {
682 return RETURN_INVALID_PARAMETER
;
686 *Result
= (UINT64
)Operand
;
687 Status
= RETURN_SUCCESS
;
689 *Result
= UINT64_ERROR
;
690 Status
= RETURN_BUFFER_TOO_SMALL
;
697 UINT16 -> INT8 conversion
699 Converts the value specified by Operand to a value specified by Result type
700 and stores the converted value into the caller allocated output buffer
701 specified by Result. The caller must pass in a Result buffer that is at
702 least as large as the Result type.
704 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
706 If the conversion results in an overflow or an underflow condition, then
707 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
709 @param[in] Operand Operand to be converted to new type
710 @param[out] Result Pointer to the result of conversion
712 @retval RETURN_SUCCESS Successful conversion
713 @retval RETURN_BUFFER_TOO_SMALL Overflow
714 @retval RETURN_INVALID_PARAMETER Result is NULL
723 RETURN_STATUS Status
;
725 if (Result
== NULL
) {
726 return RETURN_INVALID_PARAMETER
;
729 if (Operand
<= MAX_INT8
) {
730 *Result
= (INT8
)Operand
;
731 Status
= RETURN_SUCCESS
;
733 *Result
= INT8_ERROR
;
734 Status
= RETURN_BUFFER_TOO_SMALL
;
741 UINT16 -> CHAR8 conversion
743 Converts the value specified by Operand to a value specified by Result type
744 and stores the converted value into the caller allocated output buffer
745 specified by Result. The caller must pass in a Result buffer that is at
746 least as large as the Result type.
748 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
750 If the conversion results in an overflow or an underflow condition, then
751 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
753 @param[in] Operand Operand to be converted to new type
754 @param[out] Result Pointer to the result of conversion
756 @retval RETURN_SUCCESS Successful conversion
757 @retval RETURN_BUFFER_TOO_SMALL Overflow
758 @retval RETURN_INVALID_PARAMETER Result is NULL
767 RETURN_STATUS Status
;
769 if (Result
== NULL
) {
770 return RETURN_INVALID_PARAMETER
;
773 if (Operand
<= MAX_INT8
) {
774 *Result
= (INT8
)Operand
;
775 Status
= RETURN_SUCCESS
;
777 *Result
= CHAR8_ERROR
;
778 Status
= RETURN_BUFFER_TOO_SMALL
;
785 UINT16 -> UINT8 conversion
787 Converts the value specified by Operand to a value specified by Result type
788 and stores the converted value into the caller allocated output buffer
789 specified by Result. The caller must pass in a Result buffer that is at
790 least as large as the Result type.
792 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
794 If the conversion results in an overflow or an underflow condition, then
795 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
797 @param[in] Operand Operand to be converted to new type
798 @param[out] Result Pointer to the result of conversion
800 @retval RETURN_SUCCESS Successful conversion
801 @retval RETURN_BUFFER_TOO_SMALL Overflow
802 @retval RETURN_INVALID_PARAMETER Result is NULL
811 RETURN_STATUS Status
;
813 if (Result
== NULL
) {
814 return RETURN_INVALID_PARAMETER
;
817 if (Operand
<= MAX_UINT8
) {
818 *Result
= (UINT8
)Operand
;
819 Status
= RETURN_SUCCESS
;
821 *Result
= UINT8_ERROR
;
822 Status
= RETURN_BUFFER_TOO_SMALL
;
829 UINT16 -> INT16 conversion
831 Converts the value specified by Operand to a value specified by Result type
832 and stores the converted value into the caller allocated output buffer
833 specified by Result. The caller must pass in a Result buffer that is at
834 least as large as the Result type.
836 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
838 If the conversion results in an overflow or an underflow condition, then
839 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
841 @param[in] Operand Operand to be converted to new type
842 @param[out] Result Pointer to the result of conversion
844 @retval RETURN_SUCCESS Successful conversion
845 @retval RETURN_BUFFER_TOO_SMALL Overflow
846 @retval RETURN_INVALID_PARAMETER Result is NULL
855 RETURN_STATUS Status
;
857 if (Result
== NULL
) {
858 return RETURN_INVALID_PARAMETER
;
861 if (Operand
<= MAX_INT16
) {
862 *Result
= (INT16
)Operand
;
863 Status
= RETURN_SUCCESS
;
865 *Result
= INT16_ERROR
;
866 Status
= RETURN_BUFFER_TOO_SMALL
;
873 INT32 -> INT8 conversion
875 Converts the value specified by Operand to a value specified by Result type
876 and stores the converted value into the caller allocated output buffer
877 specified by Result. The caller must pass in a Result buffer that is at
878 least as large as the Result type.
880 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
882 If the conversion results in an overflow or an underflow condition, then
883 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
885 @param[in] Operand Operand to be converted to new type
886 @param[out] Result Pointer to the result of conversion
888 @retval RETURN_SUCCESS Successful conversion
889 @retval RETURN_BUFFER_TOO_SMALL Overflow
890 @retval RETURN_INVALID_PARAMETER Result is NULL
899 RETURN_STATUS Status
;
901 if (Result
== NULL
) {
902 return RETURN_INVALID_PARAMETER
;
905 if ((Operand
>= MIN_INT8
) && (Operand
<= MAX_INT8
)) {
906 *Result
= (INT8
)Operand
;
907 Status
= RETURN_SUCCESS
;
909 *Result
= INT8_ERROR
;
910 Status
= RETURN_BUFFER_TOO_SMALL
;
917 INT32 -> CHAR8 conversion
919 Converts the value specified by Operand to a value specified by Result type
920 and stores the converted value into the caller allocated output buffer
921 specified by Result. The caller must pass in a Result buffer that is at
922 least as large as the Result type.
924 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
926 If the conversion results in an overflow or an underflow condition, then
927 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
929 @param[in] Operand Operand to be converted to new type
930 @param[out] Result Pointer to the result of conversion
932 @retval RETURN_SUCCESS Successful conversion
933 @retval RETURN_BUFFER_TOO_SMALL Overflow
934 @retval RETURN_INVALID_PARAMETER Result is NULL
943 RETURN_STATUS Status
;
945 if (Result
== NULL
) {
946 return RETURN_INVALID_PARAMETER
;
949 if ((Operand
>= 0) && (Operand
<= MAX_INT8
)) {
950 *Result
= (CHAR8
)Operand
;
951 Status
= RETURN_SUCCESS
;
953 *Result
= CHAR8_ERROR
;
954 Status
= RETURN_BUFFER_TOO_SMALL
;
961 INT32 -> UINT8 conversion
963 Converts the value specified by Operand to a value specified by Result type
964 and stores the converted value into the caller allocated output buffer
965 specified by Result. The caller must pass in a Result buffer that is at
966 least as large as the Result type.
968 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
970 If the conversion results in an overflow or an underflow condition, then
971 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
973 @param[in] Operand Operand to be converted to new type
974 @param[out] Result Pointer to the result of conversion
976 @retval RETURN_SUCCESS Successful conversion
977 @retval RETURN_BUFFER_TOO_SMALL Overflow
978 @retval RETURN_INVALID_PARAMETER Result is NULL
987 RETURN_STATUS Status
;
989 if (Result
== NULL
) {
990 return RETURN_INVALID_PARAMETER
;
993 if ((Operand
>= 0) && (Operand
<= MAX_UINT8
)) {
994 *Result
= (UINT8
)Operand
;
995 Status
= RETURN_SUCCESS
;
997 *Result
= UINT8_ERROR
;
998 Status
= RETURN_BUFFER_TOO_SMALL
;
1005 INT32 -> INT16 conversion
1007 Converts the value specified by Operand to a value specified by Result type
1008 and stores the converted value into the caller allocated output buffer
1009 specified by Result. The caller must pass in a Result buffer that is at
1010 least as large as the Result type.
1012 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1014 If the conversion results in an overflow or an underflow condition, then
1015 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1017 @param[in] Operand Operand to be converted to new type
1018 @param[out] Result Pointer to the result of conversion
1020 @retval RETURN_SUCCESS Successful conversion
1021 @retval RETURN_BUFFER_TOO_SMALL Overflow
1022 @retval RETURN_INVALID_PARAMETER Result is NULL
1031 RETURN_STATUS Status
;
1033 if (Result
== NULL
) {
1034 return RETURN_INVALID_PARAMETER
;
1037 if ((Operand
>= MIN_INT16
) && (Operand
<= MAX_INT16
)) {
1038 *Result
= (INT16
)Operand
;
1039 Status
= RETURN_SUCCESS
;
1041 *Result
= INT16_ERROR
;
1042 Status
= RETURN_BUFFER_TOO_SMALL
;
1049 INT32 -> UINT16 conversion
1051 Converts the value specified by Operand to a value specified by Result type
1052 and stores the converted value into the caller allocated output buffer
1053 specified by Result. The caller must pass in a Result buffer that is at
1054 least as large as the Result type.
1056 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1058 If the conversion results in an overflow or an underflow condition, then
1059 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1061 @param[in] Operand Operand to be converted to new type
1062 @param[out] Result Pointer to the result of conversion
1064 @retval RETURN_SUCCESS Successful conversion
1065 @retval RETURN_BUFFER_TOO_SMALL Overflow
1066 @retval RETURN_INVALID_PARAMETER Result is NULL
1075 RETURN_STATUS Status
;
1077 if (Result
== NULL
) {
1078 return RETURN_INVALID_PARAMETER
;
1081 if ((Operand
>= 0) && (Operand
<= MAX_UINT16
)) {
1082 *Result
= (UINT16
)Operand
;
1083 Status
= RETURN_SUCCESS
;
1085 *Result
= UINT16_ERROR
;
1086 Status
= RETURN_BUFFER_TOO_SMALL
;
1093 INT32 -> UINT32 conversion
1095 Converts the value specified by Operand to a value specified by Result type
1096 and stores the converted value into the caller allocated output buffer
1097 specified by Result. The caller must pass in a Result buffer that is at
1098 least as large as the Result type.
1100 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1102 If the conversion results in an overflow or an underflow condition, then
1103 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1105 @param[in] Operand Operand to be converted to new type
1106 @param[out] Result Pointer to the result of conversion
1108 @retval RETURN_SUCCESS Successful conversion
1109 @retval RETURN_BUFFER_TOO_SMALL Overflow
1110 @retval RETURN_INVALID_PARAMETER Result is NULL
1119 RETURN_STATUS Status
;
1121 if (Result
== NULL
) {
1122 return RETURN_INVALID_PARAMETER
;
1126 *Result
= (UINT32
)Operand
;
1127 Status
= RETURN_SUCCESS
;
1129 *Result
= UINT32_ERROR
;
1130 Status
= RETURN_BUFFER_TOO_SMALL
;
1137 INT32 -> UINT64 conversion
1139 Converts the value specified by Operand to a value specified by Result type
1140 and stores the converted value into the caller allocated output buffer
1141 specified by Result. The caller must pass in a Result buffer that is at
1142 least as large as the Result type.
1144 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1146 If the conversion results in an overflow or an underflow condition, then
1147 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1149 @param[in] Operand Operand to be converted to new type
1150 @param[out] Result Pointer to the result of conversion
1152 @retval RETURN_SUCCESS Successful conversion
1153 @retval RETURN_BUFFER_TOO_SMALL Overflow
1154 @retval RETURN_INVALID_PARAMETER Result is NULL
1163 RETURN_STATUS Status
;
1165 if (Result
== NULL
) {
1166 return RETURN_INVALID_PARAMETER
;
1170 *Result
= (UINT64
)Operand
;
1171 Status
= RETURN_SUCCESS
;
1173 *Result
= UINT64_ERROR
;
1174 Status
= RETURN_BUFFER_TOO_SMALL
;
1181 UINT32 -> INT8 conversion
1183 Converts the value specified by Operand to a value specified by Result type
1184 and stores the converted value into the caller allocated output buffer
1185 specified by Result. The caller must pass in a Result buffer that is at
1186 least as large as the Result type.
1188 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1190 If the conversion results in an overflow or an underflow condition, then
1191 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1193 @param[in] Operand Operand to be converted to new type
1194 @param[out] Result Pointer to the result of conversion
1196 @retval RETURN_SUCCESS Successful conversion
1197 @retval RETURN_BUFFER_TOO_SMALL Overflow
1198 @retval RETURN_INVALID_PARAMETER Result is NULL
1207 RETURN_STATUS Status
;
1209 if (Result
== NULL
) {
1210 return RETURN_INVALID_PARAMETER
;
1213 if (Operand
<= MAX_INT8
) {
1214 *Result
= (INT8
)Operand
;
1215 Status
= RETURN_SUCCESS
;
1217 *Result
= INT8_ERROR
;
1218 Status
= RETURN_BUFFER_TOO_SMALL
;
1225 UINT32 -> CHAR8 conversion
1227 Converts the value specified by Operand to a value specified by Result type
1228 and stores the converted value into the caller allocated output buffer
1229 specified by Result. The caller must pass in a Result buffer that is at
1230 least as large as the Result type.
1232 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1234 If the conversion results in an overflow or an underflow condition, then
1235 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1237 @param[in] Operand Operand to be converted to new type
1238 @param[out] Result Pointer to the result of conversion
1240 @retval RETURN_SUCCESS Successful conversion
1241 @retval RETURN_BUFFER_TOO_SMALL Overflow
1242 @retval RETURN_INVALID_PARAMETER Result is NULL
1251 RETURN_STATUS Status
;
1253 if (Result
== NULL
) {
1254 return RETURN_INVALID_PARAMETER
;
1257 if (Operand
<= MAX_INT8
) {
1258 *Result
= (INT8
)Operand
;
1259 Status
= RETURN_SUCCESS
;
1261 *Result
= CHAR8_ERROR
;
1262 Status
= RETURN_BUFFER_TOO_SMALL
;
1269 UINT32 -> UINT8 conversion
1271 Converts the value specified by Operand to a value specified by Result type
1272 and stores the converted value into the caller allocated output buffer
1273 specified by Result. The caller must pass in a Result buffer that is at
1274 least as large as the Result type.
1276 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1278 If the conversion results in an overflow or an underflow condition, then
1279 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1281 @param[in] Operand Operand to be converted to new type
1282 @param[out] Result Pointer to the result of conversion
1284 @retval RETURN_SUCCESS Successful conversion
1285 @retval RETURN_BUFFER_TOO_SMALL Overflow
1286 @retval RETURN_INVALID_PARAMETER Result is NULL
1295 RETURN_STATUS Status
;
1297 if (Result
== NULL
) {
1298 return RETURN_INVALID_PARAMETER
;
1301 if (Operand
<= MAX_UINT8
) {
1302 *Result
= (UINT8
)Operand
;
1303 Status
= RETURN_SUCCESS
;
1305 *Result
= UINT8_ERROR
;
1306 Status
= RETURN_BUFFER_TOO_SMALL
;
1313 UINT32 -> INT16 conversion
1315 Converts the value specified by Operand to a value specified by Result type
1316 and stores the converted value into the caller allocated output buffer
1317 specified by Result. The caller must pass in a Result buffer that is at
1318 least as large as the Result type.
1320 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1322 If the conversion results in an overflow or an underflow condition, then
1323 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1325 @param[in] Operand Operand to be converted to new type
1326 @param[out] Result Pointer to the result of conversion
1328 @retval RETURN_SUCCESS Successful conversion
1329 @retval RETURN_BUFFER_TOO_SMALL Overflow
1330 @retval RETURN_INVALID_PARAMETER Result is NULL
1339 RETURN_STATUS Status
;
1341 if (Result
== NULL
) {
1342 return RETURN_INVALID_PARAMETER
;
1345 if (Operand
<= MAX_INT16
) {
1346 *Result
= (INT16
)Operand
;
1347 Status
= RETURN_SUCCESS
;
1349 *Result
= INT16_ERROR
;
1350 Status
= RETURN_BUFFER_TOO_SMALL
;
1357 UINT32 -> UINT16 conversion
1359 Converts the value specified by Operand to a value specified by Result type
1360 and stores the converted value into the caller allocated output buffer
1361 specified by Result. The caller must pass in a Result buffer that is at
1362 least as large as the Result type.
1364 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1366 If the conversion results in an overflow or an underflow condition, then
1367 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1369 @param[in] Operand Operand to be converted to new type
1370 @param[out] Result Pointer to the result of conversion
1372 @retval RETURN_SUCCESS Successful conversion
1373 @retval RETURN_BUFFER_TOO_SMALL Overflow
1374 @retval RETURN_INVALID_PARAMETER Result is NULL
1378 SafeUint32ToUint16 (
1383 RETURN_STATUS Status
;
1385 if (Result
== NULL
) {
1386 return RETURN_INVALID_PARAMETER
;
1389 if (Operand
<= MAX_UINT16
) {
1390 *Result
= (UINT16
)Operand
;
1391 Status
= RETURN_SUCCESS
;
1393 *Result
= UINT16_ERROR
;
1394 Status
= RETURN_BUFFER_TOO_SMALL
;
1401 UINT32 -> INT32 conversion
1403 Converts the value specified by Operand to a value specified by Result type
1404 and stores the converted value into the caller allocated output buffer
1405 specified by Result. The caller must pass in a Result buffer that is at
1406 least as large as the Result type.
1408 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1410 If the conversion results in an overflow or an underflow condition, then
1411 Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1413 @param[in] Operand Operand to be converted to new type
1414 @param[out] Result Pointer to the result of conversion
1416 @retval RETURN_SUCCESS Successful conversion
1417 @retval RETURN_BUFFER_TOO_SMALL Overflow
1418 @retval RETURN_INVALID_PARAMETER Result is NULL
1427 RETURN_STATUS Status
;
1429 if (Result
== NULL
) {
1430 return RETURN_INVALID_PARAMETER
;
1433 if (Operand
<= MAX_INT32
) {
1434 *Result
= (INT32
)Operand
;
1435 Status
= RETURN_SUCCESS
;
1437 *Result
= INT32_ERROR
;
1438 Status
= RETURN_BUFFER_TOO_SMALL
;
1445 INTN -> INT8 conversion
1447 Converts the value specified by Operand to a value specified by Result type
1448 and stores the converted value into the caller allocated output buffer
1449 specified by Result. The caller must pass in a Result buffer that is at
1450 least as large as the Result type.
1452 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1454 If the conversion results in an overflow or an underflow condition, then
1455 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1457 @param[in] Operand Operand to be converted to new type
1458 @param[out] Result Pointer to the result of conversion
1460 @retval RETURN_SUCCESS Successful conversion
1461 @retval RETURN_BUFFER_TOO_SMALL Overflow
1462 @retval RETURN_INVALID_PARAMETER Result is NULL
1471 RETURN_STATUS Status
;
1473 if (Result
== NULL
) {
1474 return RETURN_INVALID_PARAMETER
;
1477 if ((Operand
>= MIN_INT8
) && (Operand
<= MAX_INT8
)) {
1478 *Result
= (INT8
)Operand
;
1479 Status
= RETURN_SUCCESS
;
1481 *Result
= INT8_ERROR
;
1482 Status
= RETURN_BUFFER_TOO_SMALL
;
1489 INTN -> CHAR8 conversion
1491 Converts the value specified by Operand to a value specified by Result type
1492 and stores the converted value into the caller allocated output buffer
1493 specified by Result. The caller must pass in a Result buffer that is at
1494 least as large as the Result type.
1496 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1498 If the conversion results in an overflow or an underflow condition, then
1499 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1501 @param[in] Operand Operand to be converted to new type
1502 @param[out] Result Pointer to the result of conversion
1504 @retval RETURN_SUCCESS Successful conversion
1505 @retval RETURN_BUFFER_TOO_SMALL Overflow
1506 @retval RETURN_INVALID_PARAMETER Result is NULL
1515 RETURN_STATUS Status
;
1517 if (Result
== NULL
) {
1518 return RETURN_INVALID_PARAMETER
;
1521 if ((Operand
>= 0) && (Operand
<= MAX_INT8
)) {
1522 *Result
= (CHAR8
)Operand
;
1523 Status
= RETURN_SUCCESS
;
1525 *Result
= CHAR8_ERROR
;
1526 Status
= RETURN_BUFFER_TOO_SMALL
;
1533 INTN -> UINT8 conversion
1535 Converts the value specified by Operand to a value specified by Result type
1536 and stores the converted value into the caller allocated output buffer
1537 specified by Result. The caller must pass in a Result buffer that is at
1538 least as large as the Result type.
1540 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1542 If the conversion results in an overflow or an underflow condition, then
1543 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1545 @param[in] Operand Operand to be converted to new type
1546 @param[out] Result Pointer to the result of conversion
1548 @retval RETURN_SUCCESS Successful conversion
1549 @retval RETURN_BUFFER_TOO_SMALL Overflow
1550 @retval RETURN_INVALID_PARAMETER Result is NULL
1559 RETURN_STATUS Status
;
1561 if (Result
== NULL
) {
1562 return RETURN_INVALID_PARAMETER
;
1565 if ((Operand
>= 0) && (Operand
<= MAX_UINT8
)) {
1566 *Result
= (UINT8
)Operand
;
1567 Status
= RETURN_SUCCESS
;
1569 *Result
= UINT8_ERROR
;
1570 Status
= RETURN_BUFFER_TOO_SMALL
;
1577 INTN -> INT16 conversion
1579 Converts the value specified by Operand to a value specified by Result type
1580 and stores the converted value into the caller allocated output buffer
1581 specified by Result. The caller must pass in a Result buffer that is at
1582 least as large as the Result type.
1584 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1586 If the conversion results in an overflow or an underflow condition, then
1587 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1589 @param[in] Operand Operand to be converted to new type
1590 @param[out] Result Pointer to the result of conversion
1592 @retval RETURN_SUCCESS Successful conversion
1593 @retval RETURN_BUFFER_TOO_SMALL Overflow
1594 @retval RETURN_INVALID_PARAMETER Result is NULL
1603 RETURN_STATUS Status
;
1605 if (Result
== NULL
) {
1606 return RETURN_INVALID_PARAMETER
;
1609 if ((Operand
>= MIN_INT16
) && (Operand
<= MAX_INT16
)) {
1610 *Result
= (INT16
)Operand
;
1611 Status
= RETURN_SUCCESS
;
1613 *Result
= INT16_ERROR
;
1614 Status
= RETURN_BUFFER_TOO_SMALL
;
1621 INTN -> UINT16 conversion
1623 Converts the value specified by Operand to a value specified by Result type
1624 and stores the converted value into the caller allocated output buffer
1625 specified by Result. The caller must pass in a Result buffer that is at
1626 least as large as the Result type.
1628 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1630 If the conversion results in an overflow or an underflow condition, then
1631 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1633 @param[in] Operand Operand to be converted to new type
1634 @param[out] Result Pointer to the result of conversion
1636 @retval RETURN_SUCCESS Successful conversion
1637 @retval RETURN_BUFFER_TOO_SMALL Overflow
1638 @retval RETURN_INVALID_PARAMETER Result is NULL
1647 RETURN_STATUS Status
;
1649 if (Result
== NULL
) {
1650 return RETURN_INVALID_PARAMETER
;
1653 if ((Operand
>= 0) && (Operand
<= MAX_UINT16
)) {
1654 *Result
= (UINT16
)Operand
;
1655 Status
= RETURN_SUCCESS
;
1657 *Result
= UINT16_ERROR
;
1658 Status
= RETURN_BUFFER_TOO_SMALL
;
1665 INTN -> UINTN conversion
1667 Converts the value specified by Operand to a value specified by Result type
1668 and stores the converted value into the caller allocated output buffer
1669 specified by Result. The caller must pass in a Result buffer that is at
1670 least as large as the Result type.
1672 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1674 If the conversion results in an overflow or an underflow condition, then
1675 Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1677 @param[in] Operand Operand to be converted to new type
1678 @param[out] Result Pointer to the result of conversion
1680 @retval RETURN_SUCCESS Successful conversion
1681 @retval RETURN_BUFFER_TOO_SMALL Overflow
1682 @retval RETURN_INVALID_PARAMETER Result is NULL
1691 RETURN_STATUS Status
;
1693 if (Result
== NULL
) {
1694 return RETURN_INVALID_PARAMETER
;
1698 *Result
= (UINTN
)Operand
;
1699 Status
= RETURN_SUCCESS
;
1701 *Result
= UINTN_ERROR
;
1702 Status
= RETURN_BUFFER_TOO_SMALL
;
1709 INTN -> UINT64 conversion
1711 Converts the value specified by Operand to a value specified by Result type
1712 and stores the converted value into the caller allocated output buffer
1713 specified by Result. The caller must pass in a Result buffer that is at
1714 least as large as the Result type.
1716 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1718 If the conversion results in an overflow or an underflow condition, then
1719 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1721 @param[in] Operand Operand to be converted to new type
1722 @param[out] Result Pointer to the result of conversion
1724 @retval RETURN_SUCCESS Successful conversion
1725 @retval RETURN_BUFFER_TOO_SMALL Overflow
1726 @retval RETURN_INVALID_PARAMETER Result is NULL
1735 RETURN_STATUS Status
;
1737 if (Result
== NULL
) {
1738 return RETURN_INVALID_PARAMETER
;
1742 *Result
= (UINT64
)Operand
;
1743 Status
= RETURN_SUCCESS
;
1745 *Result
= UINT64_ERROR
;
1746 Status
= RETURN_BUFFER_TOO_SMALL
;
1753 UINTN -> INT8 conversion
1755 Converts the value specified by Operand to a value specified by Result type
1756 and stores the converted value into the caller allocated output buffer
1757 specified by Result. The caller must pass in a Result buffer that is at
1758 least as large as the Result type.
1760 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1762 If the conversion results in an overflow or an underflow condition, then
1763 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1765 @param[in] Operand Operand to be converted to new type
1766 @param[out] Result Pointer to the result of conversion
1768 @retval RETURN_SUCCESS Successful conversion
1769 @retval RETURN_BUFFER_TOO_SMALL Overflow
1770 @retval RETURN_INVALID_PARAMETER Result is NULL
1779 RETURN_STATUS Status
;
1781 if (Result
== NULL
) {
1782 return RETURN_INVALID_PARAMETER
;
1785 if (Operand
<= MAX_INT8
) {
1786 *Result
= (INT8
)Operand
;
1787 Status
= RETURN_SUCCESS
;
1789 *Result
= INT8_ERROR
;
1790 Status
= RETURN_BUFFER_TOO_SMALL
;
1797 UINTN -> CHAR8 conversion
1799 Converts the value specified by Operand to a value specified by Result type
1800 and stores the converted value into the caller allocated output buffer
1801 specified by Result. The caller must pass in a Result buffer that is at
1802 least as large as the Result type.
1804 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1806 If the conversion results in an overflow or an underflow condition, then
1807 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1809 @param[in] Operand Operand to be converted to new type
1810 @param[out] Result Pointer to the result of conversion
1812 @retval RETURN_SUCCESS Successful conversion
1813 @retval RETURN_BUFFER_TOO_SMALL Overflow
1814 @retval RETURN_INVALID_PARAMETER Result is NULL
1823 RETURN_STATUS Status
;
1825 if (Result
== NULL
) {
1826 return RETURN_INVALID_PARAMETER
;
1829 if (Operand
<= MAX_INT8
) {
1830 *Result
= (INT8
)Operand
;
1831 Status
= RETURN_SUCCESS
;
1833 *Result
= CHAR8_ERROR
;
1834 Status
= RETURN_BUFFER_TOO_SMALL
;
1841 UINTN -> UINT8 conversion
1843 Converts the value specified by Operand to a value specified by Result type
1844 and stores the converted value into the caller allocated output buffer
1845 specified by Result. The caller must pass in a Result buffer that is at
1846 least as large as the Result type.
1848 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1850 If the conversion results in an overflow or an underflow condition, then
1851 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1853 @param[in] Operand Operand to be converted to new type
1854 @param[out] Result Pointer to the result of conversion
1856 @retval RETURN_SUCCESS Successful conversion
1857 @retval RETURN_BUFFER_TOO_SMALL Overflow
1858 @retval RETURN_INVALID_PARAMETER Result is NULL
1867 RETURN_STATUS Status
;
1869 if (Result
== NULL
) {
1870 return RETURN_INVALID_PARAMETER
;
1873 if (Operand
<= MAX_UINT8
) {
1874 *Result
= (UINT8
)Operand
;
1875 Status
= RETURN_SUCCESS
;
1877 *Result
= UINT8_ERROR
;
1878 Status
= RETURN_BUFFER_TOO_SMALL
;
1885 UINTN -> INT16 conversion
1887 Converts the value specified by Operand to a value specified by Result type
1888 and stores the converted value into the caller allocated output buffer
1889 specified by Result. The caller must pass in a Result buffer that is at
1890 least as large as the Result type.
1892 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1894 If the conversion results in an overflow or an underflow condition, then
1895 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1897 @param[in] Operand Operand to be converted to new type
1898 @param[out] Result Pointer to the result of conversion
1900 @retval RETURN_SUCCESS Successful conversion
1901 @retval RETURN_BUFFER_TOO_SMALL Overflow
1902 @retval RETURN_INVALID_PARAMETER Result is NULL
1911 RETURN_STATUS Status
;
1913 if (Result
== NULL
) {
1914 return RETURN_INVALID_PARAMETER
;
1917 if (Operand
<= MAX_INT16
) {
1918 *Result
= (INT16
)Operand
;
1919 Status
= RETURN_SUCCESS
;
1921 *Result
= INT16_ERROR
;
1922 Status
= RETURN_BUFFER_TOO_SMALL
;
1929 UINTN -> UINT16 conversion
1931 Converts the value specified by Operand to a value specified by Result type
1932 and stores the converted value into the caller allocated output buffer
1933 specified by Result. The caller must pass in a Result buffer that is at
1934 least as large as the Result type.
1936 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1938 If the conversion results in an overflow or an underflow condition, then
1939 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1941 @param[in] Operand Operand to be converted to new type
1942 @param[out] Result Pointer to the result of conversion
1944 @retval RETURN_SUCCESS Successful conversion
1945 @retval RETURN_BUFFER_TOO_SMALL Overflow
1946 @retval RETURN_INVALID_PARAMETER Result is NULL
1955 RETURN_STATUS Status
;
1957 if (Result
== NULL
) {
1958 return RETURN_INVALID_PARAMETER
;
1961 if (Operand
<= MAX_UINT16
) {
1962 *Result
= (UINT16
)Operand
;
1963 Status
= RETURN_SUCCESS
;
1965 *Result
= UINT16_ERROR
;
1966 Status
= RETURN_BUFFER_TOO_SMALL
;
1973 UINTN -> INT32 conversion
1975 Converts the value specified by Operand to a value specified by Result type
1976 and stores the converted value into the caller allocated output buffer
1977 specified by Result. The caller must pass in a Result buffer that is at
1978 least as large as the Result type.
1980 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1982 If the conversion results in an overflow or an underflow condition, then
1983 Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1985 @param[in] Operand Operand to be converted to new type
1986 @param[out] Result Pointer to the result of conversion
1988 @retval RETURN_SUCCESS Successful conversion
1989 @retval RETURN_BUFFER_TOO_SMALL Overflow
1990 @retval RETURN_INVALID_PARAMETER Result is NULL
1999 RETURN_STATUS Status
;
2001 if (Result
== NULL
) {
2002 return RETURN_INVALID_PARAMETER
;
2005 if (Operand
<= MAX_INT32
) {
2006 *Result
= (INT32
)Operand
;
2007 Status
= RETURN_SUCCESS
;
2009 *Result
= INT32_ERROR
;
2010 Status
= RETURN_BUFFER_TOO_SMALL
;
2017 UINTN -> INTN conversion
2019 Converts the value specified by Operand to a value specified by Result type
2020 and stores the converted value into the caller allocated output buffer
2021 specified by Result. The caller must pass in a Result buffer that is at
2022 least as large as the Result type.
2024 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2026 If the conversion results in an overflow or an underflow condition, then
2027 Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2029 @param[in] Operand Operand to be converted to new type
2030 @param[out] Result Pointer to the result of conversion
2032 @retval RETURN_SUCCESS Successful conversion
2033 @retval RETURN_BUFFER_TOO_SMALL Overflow
2034 @retval RETURN_INVALID_PARAMETER Result is NULL
2043 RETURN_STATUS Status
;
2045 if (Result
== NULL
) {
2046 return RETURN_INVALID_PARAMETER
;
2049 if (Operand
<= MAX_INTN
) {
2050 *Result
= (INTN
)Operand
;
2051 Status
= RETURN_SUCCESS
;
2053 *Result
= INTN_ERROR
;
2054 Status
= RETURN_BUFFER_TOO_SMALL
;
2061 INT64 -> INT8 conversion
2063 Converts the value specified by Operand to a value specified by Result type
2064 and stores the converted value into the caller allocated output buffer
2065 specified by Result. The caller must pass in a Result buffer that is at
2066 least as large as the Result type.
2068 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2070 If the conversion results in an overflow or an underflow condition, then
2071 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2073 @param[in] Operand Operand to be converted to new type
2074 @param[out] Result Pointer to the result of conversion
2076 @retval RETURN_SUCCESS Successful conversion
2077 @retval RETURN_BUFFER_TOO_SMALL Overflow
2078 @retval RETURN_INVALID_PARAMETER Result is NULL
2087 RETURN_STATUS Status
;
2089 if (Result
== NULL
) {
2090 return RETURN_INVALID_PARAMETER
;
2093 if ((Operand
>= MIN_INT8
) && (Operand
<= MAX_INT8
)) {
2094 *Result
= (INT8
)Operand
;
2095 Status
= RETURN_SUCCESS
;
2097 *Result
= INT8_ERROR
;
2098 Status
= RETURN_BUFFER_TOO_SMALL
;
2105 INT64 -> CHAR8 conversion
2107 Converts the value specified by Operand to a value specified by Result type
2108 and stores the converted value into the caller allocated output buffer
2109 specified by Result. The caller must pass in a Result buffer that is at
2110 least as large as the Result type.
2112 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2114 If the conversion results in an overflow or an underflow condition, then
2115 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2117 @param[in] Operand Operand to be converted to new type
2118 @param[out] Result Pointer to the result of conversion
2120 @retval RETURN_SUCCESS Successful conversion
2121 @retval RETURN_BUFFER_TOO_SMALL Overflow
2122 @retval RETURN_INVALID_PARAMETER Result is NULL
2131 RETURN_STATUS Status
;
2133 if (Result
== NULL
) {
2134 return RETURN_INVALID_PARAMETER
;
2137 if ((Operand
>= 0) && (Operand
<= MAX_INT8
)) {
2138 *Result
= (CHAR8
)Operand
;
2139 Status
= RETURN_SUCCESS
;
2141 *Result
= CHAR8_ERROR
;
2142 Status
= RETURN_BUFFER_TOO_SMALL
;
2149 INT64 -> UINT8 conversion
2151 Converts the value specified by Operand to a value specified by Result type
2152 and stores the converted value into the caller allocated output buffer
2153 specified by Result. The caller must pass in a Result buffer that is at
2154 least as large as the Result type.
2156 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2158 If the conversion results in an overflow or an underflow condition, then
2159 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2161 @param[in] Operand Operand to be converted to new type
2162 @param[out] Result Pointer to the result of conversion
2164 @retval RETURN_SUCCESS Successful conversion
2165 @retval RETURN_BUFFER_TOO_SMALL Overflow
2166 @retval RETURN_INVALID_PARAMETER Result is NULL
2175 RETURN_STATUS Status
;
2177 if (Result
== NULL
) {
2178 return RETURN_INVALID_PARAMETER
;
2181 if ((Operand
>= 0) && (Operand
<= MAX_UINT8
)) {
2182 *Result
= (UINT8
)Operand
;
2183 Status
= RETURN_SUCCESS
;
2185 *Result
= UINT8_ERROR
;
2186 Status
= RETURN_BUFFER_TOO_SMALL
;
2193 INT64 -> INT16 conversion
2195 Converts the value specified by Operand to a value specified by Result type
2196 and stores the converted value into the caller allocated output buffer
2197 specified by Result. The caller must pass in a Result buffer that is at
2198 least as large as the Result type.
2200 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2202 If the conversion results in an overflow or an underflow condition, then
2203 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2205 @param[in] Operand Operand to be converted to new type
2206 @param[out] Result Pointer to the result of conversion
2208 @retval RETURN_SUCCESS Successful conversion
2209 @retval RETURN_BUFFER_TOO_SMALL Overflow
2210 @retval RETURN_INVALID_PARAMETER Result is NULL
2219 RETURN_STATUS Status
;
2221 if (Result
== NULL
) {
2222 return RETURN_INVALID_PARAMETER
;
2225 if ((Operand
>= MIN_INT16
) && (Operand
<= MAX_INT16
)) {
2226 *Result
= (INT16
)Operand
;
2227 Status
= RETURN_SUCCESS
;
2229 *Result
= INT16_ERROR
;
2230 Status
= RETURN_BUFFER_TOO_SMALL
;
2237 INT64 -> UINT16 conversion
2239 Converts the value specified by Operand to a value specified by Result type
2240 and stores the converted value into the caller allocated output buffer
2241 specified by Result. The caller must pass in a Result buffer that is at
2242 least as large as the Result type.
2244 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2246 If the conversion results in an overflow or an underflow condition, then
2247 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2249 @param[in] Operand Operand to be converted to new type
2250 @param[out] Result Pointer to the result of conversion
2252 @retval RETURN_SUCCESS Successful conversion
2253 @retval RETURN_BUFFER_TOO_SMALL Overflow
2254 @retval RETURN_INVALID_PARAMETER Result is NULL
2263 RETURN_STATUS Status
;
2265 if (Result
== NULL
) {
2266 return RETURN_INVALID_PARAMETER
;
2269 if ((Operand
>= 0) && (Operand
<= MAX_UINT16
)) {
2270 *Result
= (UINT16
)Operand
;
2271 Status
= RETURN_SUCCESS
;
2273 *Result
= UINT16_ERROR
;
2274 Status
= RETURN_BUFFER_TOO_SMALL
;
2281 INT64 -> INT32 conversion
2283 Converts the value specified by Operand to a value specified by Result type
2284 and stores the converted value into the caller allocated output buffer
2285 specified by Result. The caller must pass in a Result buffer that is at
2286 least as large as the Result type.
2288 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2290 If the conversion results in an overflow or an underflow condition, then
2291 Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2293 @param[in] Operand Operand to be converted to new type
2294 @param[out] Result Pointer to the result of conversion
2296 @retval RETURN_SUCCESS Successful conversion
2297 @retval RETURN_BUFFER_TOO_SMALL Overflow
2298 @retval RETURN_INVALID_PARAMETER Result is NULL
2307 RETURN_STATUS Status
;
2309 if (Result
== NULL
) {
2310 return RETURN_INVALID_PARAMETER
;
2313 if ((Operand
>= MIN_INT32
) && (Operand
<= MAX_INT32
)) {
2314 *Result
= (INT32
)Operand
;
2315 Status
= RETURN_SUCCESS
;
2317 *Result
= INT32_ERROR
;
2318 Status
= RETURN_BUFFER_TOO_SMALL
;
2325 INT64 -> UINT32 conversion
2327 Converts the value specified by Operand to a value specified by Result type
2328 and stores the converted value into the caller allocated output buffer
2329 specified by Result. The caller must pass in a Result buffer that is at
2330 least as large as the Result type.
2332 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2334 If the conversion results in an overflow or an underflow condition, then
2335 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2337 @param[in] Operand Operand to be converted to new type
2338 @param[out] Result Pointer to the result of conversion
2340 @retval RETURN_SUCCESS Successful conversion
2341 @retval RETURN_BUFFER_TOO_SMALL Overflow
2342 @retval RETURN_INVALID_PARAMETER Result is NULL
2351 RETURN_STATUS Status
;
2353 if (Result
== NULL
) {
2354 return RETURN_INVALID_PARAMETER
;
2357 if ((Operand
>= 0) && (Operand
<= MAX_UINT32
)) {
2358 *Result
= (UINT32
)Operand
;
2359 Status
= RETURN_SUCCESS
;
2361 *Result
= UINT32_ERROR
;
2362 Status
= RETURN_BUFFER_TOO_SMALL
;
2369 INT64 -> UINT64 conversion
2371 Converts the value specified by Operand to a value specified by Result type
2372 and stores the converted value into the caller allocated output buffer
2373 specified by Result. The caller must pass in a Result buffer that is at
2374 least as large as the Result type.
2376 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2378 If the conversion results in an overflow or an underflow condition, then
2379 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2381 @param[in] Operand Operand to be converted to new type
2382 @param[out] Result Pointer to the result of conversion
2384 @retval RETURN_SUCCESS Successful conversion
2385 @retval RETURN_BUFFER_TOO_SMALL Overflow
2386 @retval RETURN_INVALID_PARAMETER Result is NULL
2395 RETURN_STATUS Status
;
2397 if (Result
== NULL
) {
2398 return RETURN_INVALID_PARAMETER
;
2402 *Result
= (UINT64
)Operand
;
2403 Status
= RETURN_SUCCESS
;
2405 *Result
= UINT64_ERROR
;
2406 Status
= RETURN_BUFFER_TOO_SMALL
;
2413 UINT64 -> INT8 conversion
2415 Converts the value specified by Operand to a value specified by Result type
2416 and stores the converted value into the caller allocated output buffer
2417 specified by Result. The caller must pass in a Result buffer that is at
2418 least as large as the Result type.
2420 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2422 If the conversion results in an overflow or an underflow condition, then
2423 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2425 @param[in] Operand Operand to be converted to new type
2426 @param[out] Result Pointer to the result of conversion
2428 @retval RETURN_SUCCESS Successful conversion
2429 @retval RETURN_BUFFER_TOO_SMALL Overflow
2430 @retval RETURN_INVALID_PARAMETER Result is NULL
2439 RETURN_STATUS Status
;
2441 if (Result
== NULL
) {
2442 return RETURN_INVALID_PARAMETER
;
2445 if (Operand
<= MAX_INT8
) {
2446 *Result
= (INT8
)Operand
;
2447 Status
= RETURN_SUCCESS
;
2449 *Result
= INT8_ERROR
;
2450 Status
= RETURN_BUFFER_TOO_SMALL
;
2457 UINT64 -> CHAR8 conversion
2459 Converts the value specified by Operand to a value specified by Result type
2460 and stores the converted value into the caller allocated output buffer
2461 specified by Result. The caller must pass in a Result buffer that is at
2462 least as large as the Result type.
2464 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2466 If the conversion results in an overflow or an underflow condition, then
2467 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2469 @param[in] Operand Operand to be converted to new type
2470 @param[out] Result Pointer to the result of conversion
2472 @retval RETURN_SUCCESS Successful conversion
2473 @retval RETURN_BUFFER_TOO_SMALL Overflow
2474 @retval RETURN_INVALID_PARAMETER Result is NULL
2483 RETURN_STATUS Status
;
2485 if (Result
== NULL
) {
2486 return RETURN_INVALID_PARAMETER
;
2489 if (Operand
<= MAX_INT8
) {
2490 *Result
= (INT8
)Operand
;
2491 Status
= RETURN_SUCCESS
;
2493 *Result
= CHAR8_ERROR
;
2494 Status
= RETURN_BUFFER_TOO_SMALL
;
2501 UINT64 -> UINT8 conversion
2503 Converts the value specified by Operand to a value specified by Result type
2504 and stores the converted value into the caller allocated output buffer
2505 specified by Result. The caller must pass in a Result buffer that is at
2506 least as large as the Result type.
2508 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2510 If the conversion results in an overflow or an underflow condition, then
2511 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2513 @param[in] Operand Operand to be converted to new type
2514 @param[out] Result Pointer to the result of conversion
2516 @retval RETURN_SUCCESS Successful conversion
2517 @retval RETURN_BUFFER_TOO_SMALL Overflow
2518 @retval RETURN_INVALID_PARAMETER Result is NULL
2527 RETURN_STATUS Status
;
2529 if (Result
== NULL
) {
2530 return RETURN_INVALID_PARAMETER
;
2533 if (Operand
<= MAX_UINT8
) {
2534 *Result
= (UINT8
)Operand
;
2535 Status
= RETURN_SUCCESS
;
2537 *Result
= UINT8_ERROR
;
2538 Status
= RETURN_BUFFER_TOO_SMALL
;
2545 UINT64 -> INT16 conversion
2547 Converts the value specified by Operand to a value specified by Result type
2548 and stores the converted value into the caller allocated output buffer
2549 specified by Result. The caller must pass in a Result buffer that is at
2550 least as large as the Result type.
2552 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2554 If the conversion results in an overflow or an underflow condition, then
2555 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2557 @param[in] Operand Operand to be converted to new type
2558 @param[out] Result Pointer to the result of conversion
2560 @retval RETURN_SUCCESS Successful conversion
2561 @retval RETURN_BUFFER_TOO_SMALL Overflow
2562 @retval RETURN_INVALID_PARAMETER Result is NULL
2571 RETURN_STATUS Status
;
2573 if (Result
== NULL
) {
2574 return RETURN_INVALID_PARAMETER
;
2577 if (Operand
<= MAX_INT16
) {
2578 *Result
= (INT16
)Operand
;
2579 Status
= RETURN_SUCCESS
;
2581 *Result
= INT16_ERROR
;
2582 Status
= RETURN_BUFFER_TOO_SMALL
;
2589 UINT64 -> UINT16 conversion
2591 Converts the value specified by Operand to a value specified by Result type
2592 and stores the converted value into the caller allocated output buffer
2593 specified by Result. The caller must pass in a Result buffer that is at
2594 least as large as the Result type.
2596 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2598 If the conversion results in an overflow or an underflow condition, then
2599 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2601 @param[in] Operand Operand to be converted to new type
2602 @param[out] Result Pointer to the result of conversion
2604 @retval RETURN_SUCCESS Successful conversion
2605 @retval RETURN_BUFFER_TOO_SMALL Overflow
2606 @retval RETURN_INVALID_PARAMETER Result is NULL
2610 SafeUint64ToUint16 (
2615 RETURN_STATUS Status
;
2617 if (Result
== NULL
) {
2618 return RETURN_INVALID_PARAMETER
;
2621 if (Operand
<= MAX_UINT16
) {
2622 *Result
= (UINT16
)Operand
;
2623 Status
= RETURN_SUCCESS
;
2625 *Result
= UINT16_ERROR
;
2626 Status
= RETURN_BUFFER_TOO_SMALL
;
2633 UINT64 -> INT32 conversion
2635 Converts the value specified by Operand to a value specified by Result type
2636 and stores the converted value into the caller allocated output buffer
2637 specified by Result. The caller must pass in a Result buffer that is at
2638 least as large as the Result type.
2640 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2642 If the conversion results in an overflow or an underflow condition, then
2643 Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2645 @param[in] Operand Operand to be converted to new type
2646 @param[out] Result Pointer to the result of conversion
2648 @retval RETURN_SUCCESS Successful conversion
2649 @retval RETURN_BUFFER_TOO_SMALL Overflow
2650 @retval RETURN_INVALID_PARAMETER Result is NULL
2659 RETURN_STATUS Status
;
2661 if (Result
== NULL
) {
2662 return RETURN_INVALID_PARAMETER
;
2665 if (Operand
<= MAX_INT32
) {
2666 *Result
= (INT32
)Operand
;
2667 Status
= RETURN_SUCCESS
;
2669 *Result
= INT32_ERROR
;
2670 Status
= RETURN_BUFFER_TOO_SMALL
;
2677 UINT64 -> UINT32 conversion
2679 Converts the value specified by Operand to a value specified by Result type
2680 and stores the converted value into the caller allocated output buffer
2681 specified by Result. The caller must pass in a Result buffer that is at
2682 least as large as the Result type.
2684 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2686 If the conversion results in an overflow or an underflow condition, then
2687 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2689 @param[in] Operand Operand to be converted to new type
2690 @param[out] Result Pointer to the result of conversion
2692 @retval RETURN_SUCCESS Successful conversion
2693 @retval RETURN_BUFFER_TOO_SMALL Overflow
2694 @retval RETURN_INVALID_PARAMETER Result is NULL
2698 SafeUint64ToUint32 (
2703 RETURN_STATUS Status
;
2705 if (Result
== NULL
) {
2706 return RETURN_INVALID_PARAMETER
;
2709 if (Operand
<= MAX_UINT32
) {
2710 *Result
= (UINT32
)Operand
;
2711 Status
= RETURN_SUCCESS
;
2713 *Result
= UINT32_ERROR
;
2714 Status
= RETURN_BUFFER_TOO_SMALL
;
2721 UINT64 -> INTN conversion
2723 Converts the value specified by Operand to a value specified by Result type
2724 and stores the converted value into the caller allocated output buffer
2725 specified by Result. The caller must pass in a Result buffer that is at
2726 least as large as the Result type.
2728 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2730 If the conversion results in an overflow or an underflow condition, then
2731 Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2733 @param[in] Operand Operand to be converted to new type
2734 @param[out] Result Pointer to the result of conversion
2736 @retval RETURN_SUCCESS Successful conversion
2737 @retval RETURN_BUFFER_TOO_SMALL Overflow
2738 @retval RETURN_INVALID_PARAMETER Result is NULL
2747 RETURN_STATUS Status
;
2749 if (Result
== NULL
) {
2750 return RETURN_INVALID_PARAMETER
;
2753 if (Operand
<= MAX_INTN
) {
2754 *Result
= (INTN
)Operand
;
2755 Status
= RETURN_SUCCESS
;
2757 *Result
= INTN_ERROR
;
2758 Status
= RETURN_BUFFER_TOO_SMALL
;
2765 UINT64 -> INT64 conversion
2767 Converts the value specified by Operand to a value specified by Result type
2768 and stores the converted value into the caller allocated output buffer
2769 specified by Result. The caller must pass in a Result buffer that is at
2770 least as large as the Result type.
2772 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2774 If the conversion results in an overflow or an underflow condition, then
2775 Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2777 @param[in] Operand Operand to be converted to new type
2778 @param[out] Result Pointer to the result of conversion
2780 @retval RETURN_SUCCESS Successful conversion
2781 @retval RETURN_BUFFER_TOO_SMALL Overflow
2782 @retval RETURN_INVALID_PARAMETER Result is NULL
2791 RETURN_STATUS Status
;
2793 if (Result
== NULL
) {
2794 return RETURN_INVALID_PARAMETER
;
2797 if (Operand
<= MAX_INT64
) {
2798 *Result
= (INT64
)Operand
;
2799 Status
= RETURN_SUCCESS
;
2801 *Result
= INT64_ERROR
;
2802 Status
= RETURN_BUFFER_TOO_SMALL
;
2809 // Addition functions
2815 Performs the requested operation using the input parameters into a value
2816 specified by Result type and stores the converted value into the caller
2817 allocated output buffer specified by Result. The caller must pass in a
2818 Result buffer that is at least as large as the Result type.
2820 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2822 If the requested operation results in an overflow or an underflow condition,
2823 then Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2825 @param[in] Augend A number to which addend will be added
2826 @param[in] Addend A number to be added to another
2827 @param[out] Result Pointer to the result of addition
2829 @retval RETURN_SUCCESS Successful addition
2830 @retval RETURN_BUFFER_TOO_SMALL Overflow
2831 @retval RETURN_INVALID_PARAMETER Result is NULL
2841 RETURN_STATUS Status
;
2843 if (Result
== NULL
) {
2844 return RETURN_INVALID_PARAMETER
;
2847 if (((UINT8
)(Augend
+ Addend
)) >= Augend
) {
2848 *Result
= (UINT8
)(Augend
+ Addend
);
2849 Status
= RETURN_SUCCESS
;
2851 *Result
= UINT8_ERROR
;
2852 Status
= RETURN_BUFFER_TOO_SMALL
;
2861 Performs the requested operation using the input parameters into a value
2862 specified by Result type and stores the converted value into the caller
2863 allocated output buffer specified by Result. The caller must pass in a
2864 Result buffer that is at least as large as the Result type.
2866 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2868 If the requested operation results in an overflow or an underflow condition,
2869 then Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2871 @param[in] Augend A number to which addend will be added
2872 @param[in] Addend A number to be added to another
2873 @param[out] Result Pointer to the result of addition
2875 @retval RETURN_SUCCESS Successful addition
2876 @retval RETURN_BUFFER_TOO_SMALL Overflow
2877 @retval RETURN_INVALID_PARAMETER Result is NULL
2887 RETURN_STATUS Status
;
2889 if (Result
== NULL
) {
2890 return RETURN_INVALID_PARAMETER
;
2893 if (((UINT16
)(Augend
+ Addend
)) >= Augend
) {
2894 *Result
= (UINT16
)(Augend
+ Addend
);
2895 Status
= RETURN_SUCCESS
;
2897 *Result
= UINT16_ERROR
;
2898 Status
= RETURN_BUFFER_TOO_SMALL
;
2907 Performs the requested operation using the input parameters into a value
2908 specified by Result type and stores the converted value into the caller
2909 allocated output buffer specified by Result. The caller must pass in a
2910 Result buffer that is at least as large as the Result type.
2912 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2914 If the requested operation results in an overflow or an underflow condition,
2915 then Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2917 @param[in] Augend A number to which addend will be added
2918 @param[in] Addend A number to be added to another
2919 @param[out] Result Pointer to the result of addition
2921 @retval RETURN_SUCCESS Successful addition
2922 @retval RETURN_BUFFER_TOO_SMALL Overflow
2923 @retval RETURN_INVALID_PARAMETER Result is NULL
2933 RETURN_STATUS Status
;
2935 if (Result
== NULL
) {
2936 return RETURN_INVALID_PARAMETER
;
2939 if ((Augend
+ Addend
) >= Augend
) {
2940 *Result
= (Augend
+ Addend
);
2941 Status
= RETURN_SUCCESS
;
2943 *Result
= UINT32_ERROR
;
2944 Status
= RETURN_BUFFER_TOO_SMALL
;
2953 Performs the requested operation using the input parameters into a value
2954 specified by Result type and stores the converted value into the caller
2955 allocated output buffer specified by Result. The caller must pass in a
2956 Result buffer that is at least as large as the Result type.
2958 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2960 If the requested operation results in an overflow or an underflow condition,
2961 then Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2963 @param[in] Augend A number to which addend will be added
2964 @param[in] Addend A number to be added to another
2965 @param[out] Result Pointer to the result of addition
2967 @retval RETURN_SUCCESS Successful addition
2968 @retval RETURN_BUFFER_TOO_SMALL Overflow
2969 @retval RETURN_INVALID_PARAMETER Result is NULL
2979 RETURN_STATUS Status
;
2981 if (Result
== NULL
) {
2982 return RETURN_INVALID_PARAMETER
;
2985 if ((Augend
+ Addend
) >= Augend
) {
2986 *Result
= (Augend
+ Addend
);
2987 Status
= RETURN_SUCCESS
;
2989 *Result
= UINT64_ERROR
;
2990 Status
= RETURN_BUFFER_TOO_SMALL
;
2997 // Subtraction functions
3003 Performs the requested operation using the input parameters into a value
3004 specified by Result type and stores the converted value into the caller
3005 allocated output buffer specified by Result. The caller must pass in a
3006 Result buffer that is at least as large as the Result type.
3008 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3010 If the requested operation results in an overflow or an underflow condition,
3011 then Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3013 @param[in] Minuend A number from which another is to be subtracted.
3014 @param[in] Subtrahend A number to be subtracted from another
3015 @param[out] Result Pointer to the result of subtraction
3017 @retval RETURN_SUCCESS Successful subtraction
3018 @retval RETURN_BUFFER_TOO_SMALL Underflow
3019 @retval RETURN_INVALID_PARAMETER Result is NULL
3025 IN UINT8 Subtrahend
,
3029 RETURN_STATUS Status
;
3031 if (Result
== NULL
) {
3032 return RETURN_INVALID_PARAMETER
;
3035 if (Minuend
>= Subtrahend
) {
3036 *Result
= (UINT8
)(Minuend
- Subtrahend
);
3037 Status
= RETURN_SUCCESS
;
3039 *Result
= UINT8_ERROR
;
3040 Status
= RETURN_BUFFER_TOO_SMALL
;
3049 Performs the requested operation using the input parameters into a value
3050 specified by Result type and stores the converted value into the caller
3051 allocated output buffer specified by Result. The caller must pass in a
3052 Result buffer that is at least as large as the Result type.
3054 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3056 If the requested operation results in an overflow or an underflow condition,
3057 then Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3059 @param[in] Minuend A number from which another is to be subtracted.
3060 @param[in] Subtrahend A number to be subtracted from another
3061 @param[out] Result Pointer to the result of subtraction
3063 @retval RETURN_SUCCESS Successful subtraction
3064 @retval RETURN_BUFFER_TOO_SMALL Underflow
3065 @retval RETURN_INVALID_PARAMETER Result is NULL
3071 IN UINT16 Subtrahend
,
3075 RETURN_STATUS Status
;
3077 if (Result
== NULL
) {
3078 return RETURN_INVALID_PARAMETER
;
3081 if (Minuend
>= Subtrahend
) {
3082 *Result
= (UINT16
)(Minuend
- Subtrahend
);
3083 Status
= RETURN_SUCCESS
;
3085 *Result
= UINT16_ERROR
;
3086 Status
= RETURN_BUFFER_TOO_SMALL
;
3095 Performs the requested operation using the input parameters into a value
3096 specified by Result type and stores the converted value into the caller
3097 allocated output buffer specified by Result. The caller must pass in a
3098 Result buffer that is at least as large as the Result type.
3100 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3102 If the requested operation results in an overflow or an underflow condition,
3103 then Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3105 @param[in] Minuend A number from which another is to be subtracted.
3106 @param[in] Subtrahend A number to be subtracted from another
3107 @param[out] Result Pointer to the result of subtraction
3109 @retval RETURN_SUCCESS Successful subtraction
3110 @retval RETURN_BUFFER_TOO_SMALL Underflow
3111 @retval RETURN_INVALID_PARAMETER Result is NULL
3117 IN UINT32 Subtrahend
,
3121 RETURN_STATUS Status
;
3123 if (Result
== NULL
) {
3124 return RETURN_INVALID_PARAMETER
;
3127 if (Minuend
>= Subtrahend
) {
3128 *Result
= (Minuend
- Subtrahend
);
3129 Status
= RETURN_SUCCESS
;
3131 *Result
= UINT32_ERROR
;
3132 Status
= RETURN_BUFFER_TOO_SMALL
;
3141 Performs the requested operation using the input parameters into a value
3142 specified by Result type and stores the converted value into the caller
3143 allocated output buffer specified by Result. The caller must pass in a
3144 Result buffer that is at least as large as the Result type.
3146 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3148 If the requested operation results in an overflow or an underflow condition,
3149 then Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3151 @param[in] Minuend A number from which another is to be subtracted.
3152 @param[in] Subtrahend A number to be subtracted from another
3153 @param[out] Result Pointer to the result of subtraction
3155 @retval RETURN_SUCCESS Successful subtraction
3156 @retval RETURN_BUFFER_TOO_SMALL Underflow
3157 @retval RETURN_INVALID_PARAMETER Result is NULL
3163 IN UINT64 Subtrahend
,
3167 RETURN_STATUS Status
;
3169 if (Result
== NULL
) {
3170 return RETURN_INVALID_PARAMETER
;
3173 if (Minuend
>= Subtrahend
) {
3174 *Result
= (Minuend
- Subtrahend
);
3175 Status
= RETURN_SUCCESS
;
3177 *Result
= UINT64_ERROR
;
3178 Status
= RETURN_BUFFER_TOO_SMALL
;
3185 // Multiplication functions
3189 UINT8 multiplication
3191 Performs the requested operation using the input parameters into a value
3192 specified by Result type and stores the converted value into the caller
3193 allocated output buffer specified by Result. The caller must pass in a
3194 Result buffer that is at least as large as the Result type.
3196 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3198 If the requested operation results in an overflow or an underflow condition,
3199 then Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3201 @param[in] Multiplicand A number that is to be multiplied by another
3202 @param[in] Multiplier A number by which the multiplicand is to be multiplied
3203 @param[out] Result Pointer to the result of multiplication
3205 @retval RETURN_SUCCESS Successful multiplication
3206 @retval RETURN_BUFFER_TOO_SMALL Overflow
3207 @retval RETURN_INVALID_PARAMETER Result is NULL
3212 IN UINT8 Multiplicand
,
3213 IN UINT8 Multiplier
,
3217 UINT32 IntermediateResult
;
3219 IntermediateResult
= ((UINT32
)Multiplicand
) *((UINT32
)Multiplier
);
3221 return SafeUint32ToUint8 (IntermediateResult
, Result
);
3225 UINT16 multiplication
3227 Performs the requested operation using the input parameters into a value
3228 specified by Result type and stores the converted value into the caller
3229 allocated output buffer specified by Result. The caller must pass in a
3230 Result buffer that is at least as large as the Result type.
3232 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3234 If the requested operation results in an overflow or an underflow condition,
3235 then Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3237 @param[in] Multiplicand A number that is to be multiplied by another
3238 @param[in] Multiplier A number by which the multiplicand is to be multiplied
3239 @param[out] Result Pointer to the result of multiplication
3241 @retval RETURN_SUCCESS Successful multiplication
3242 @retval RETURN_BUFFER_TOO_SMALL Overflow
3243 @retval RETURN_INVALID_PARAMETER Result is NULL
3248 IN UINT16 Multiplicand
,
3249 IN UINT16 Multiplier
,
3253 UINT32 IntermediateResult
;
3255 IntermediateResult
= ((UINT32
)Multiplicand
) *((UINT32
)Multiplier
);
3257 return SafeUint32ToUint16 (IntermediateResult
, Result
);
3261 UINT32 multiplication
3263 Performs the requested operation using the input parameters into a value
3264 specified by Result type and stores the converted value into the caller
3265 allocated output buffer specified by Result. The caller must pass in a
3266 Result buffer that is at least as large as the Result type.
3268 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3270 If the requested operation results in an overflow or an underflow condition,
3271 then Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3273 @param[in] Multiplicand A number that is to be multiplied by another
3274 @param[in] Multiplier A number by which the multiplicand is to be multiplied
3275 @param[out] Result Pointer to the result of multiplication
3277 @retval RETURN_SUCCESS Successful multiplication
3278 @retval RETURN_BUFFER_TOO_SMALL Overflow
3279 @retval RETURN_INVALID_PARAMETER Result is NULL
3284 IN UINT32 Multiplicand
,
3285 IN UINT32 Multiplier
,
3289 UINT64 IntermediateResult
;
3291 IntermediateResult
= ((UINT64
)Multiplicand
) *((UINT64
)Multiplier
);
3293 return SafeUint64ToUint32 (IntermediateResult
, Result
);
3297 UINT64 multiplication
3299 Performs the requested operation using the input parameters into a value
3300 specified by Result type and stores the converted value into the caller
3301 allocated output buffer specified by Result. The caller must pass in a
3302 Result buffer that is at least as large as the Result type.
3304 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3306 If the requested operation results in an overflow or an underflow condition,
3307 then Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3309 @param[in] Multiplicand A number that is to be multiplied by another
3310 @param[in] Multiplier A number by which the multiplicand is to be multiplied
3311 @param[out] Result Pointer to the result of multiplication
3313 @retval RETURN_SUCCESS Successful multiplication
3314 @retval RETURN_BUFFER_TOO_SMALL Overflow
3315 @retval RETURN_INVALID_PARAMETER Result is NULL
3320 IN UINT64 Multiplicand
,
3321 IN UINT64 Multiplier
,
3325 RETURN_STATUS Status
;
3333 UINT64 UnsignedResult
;
3335 if (Result
== NULL
) {
3336 return RETURN_INVALID_PARAMETER
;
3343 Status
= RETURN_BUFFER_TOO_SMALL
;
3346 // 64x64 into 128 is like 32.32 x 32.32.
3348 // a.b * c.d = a*(c.d) + .b*(c.d) = a*c + a*.d + .b*c + .b*.d
3349 // back in non-decimal notation where A=a*2^32 and C=c*2^32:
3350 // A*C + A*d + b*C + b*d
3351 // So there are four components to add together.
3352 // result = (a*c*2^64) + (a*d*2^32) + (b*c*2^32) + (b*d)
3354 // a * c must be 0 or there would be bits in the high 64-bits
3355 // a * d must be less than 2^32 or there would be bits in the high 64-bits
3356 // b * c must be less than 2^32 or there would be bits in the high 64-bits
3357 // then there must be no overflow of the resulting values summed up.
3359 DwordA
= (UINT32
)RShiftU64 (Multiplicand
, 32);
3360 DwordC
= (UINT32
)RShiftU64 (Multiplier
, 32);
3363 // common case -- if high dwords are both zero, no chance for overflow
3365 if ((DwordA
== 0) && (DwordC
== 0)) {
3366 DwordB
= (UINT32
)Multiplicand
;
3367 DwordD
= (UINT32
)Multiplier
;
3369 *Result
= (((UINT64
)DwordB
) *(UINT64
)DwordD
);
3370 Status
= RETURN_SUCCESS
;
3373 // a * c must be 0 or there would be bits set in the high 64-bits
3375 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
= MultU64x64 ((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
= MultU64x64 ((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
= MultU64x64 ((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
;
3420 // Signed operations
3422 // Strongly consider using unsigned numbers.
3424 // Signed numbers are often used where unsigned numbers should be used.
3425 // For example file sizes and array indices should always be unsigned.
3426 // Subtracting a larger positive signed number from a smaller positive
3427 // signed number with SafeInt32Sub will succeed, producing a negative number,
3428 // that then must not be used as an array index (but can occasionally be
3429 // used as a pointer index.) Similarly for adding a larger magnitude
3430 // negative number to a smaller magnitude positive number.
3432 // This library does not protect you from such errors. It tells you if your
3433 // integer operations overflowed, not if you are doing the right thing
3434 // with your non-overflowed integers.
3436 // Likewise you can overflow a buffer with a non-overflowed unsigned index.
3440 // Signed addition functions
3446 Performs the requested operation using the input parameters into a value
3447 specified by Result type and stores the converted value into the caller
3448 allocated output buffer specified by Result. The caller must pass in a
3449 Result buffer that is at least as large as the Result type.
3451 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3453 If the requested operation results in an overflow or an underflow condition,
3454 then Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3456 @param[in] Augend A number to which addend will be added
3457 @param[in] Addend A number to be added to another
3458 @param[out] Result Pointer to the result of addition
3460 @retval RETURN_SUCCESS Successful addition
3461 @retval RETURN_BUFFER_TOO_SMALL Overflow
3462 @retval RETURN_INVALID_PARAMETER Result is NULL
3472 return SafeInt32ToInt8 (((INT32
)Augend
) + ((INT32
)Addend
), Result
);
3478 Performs the requested operation using the input parameters into a value
3479 specified by Result type and stores the converted value into the caller
3480 allocated output buffer specified by Result. The caller must pass in a
3481 Result buffer that is at least as large as the Result type.
3483 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3485 If the requested operation results in an overflow or an underflow condition,
3486 then Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3488 @param[in] Augend A number to which addend will be added
3489 @param[in] Addend A number to be added to another
3490 @param[out] Result Pointer to the result of addition
3492 @retval RETURN_SUCCESS Successful addition
3493 @retval RETURN_BUFFER_TOO_SMALL Overflow
3494 @retval RETURN_INVALID_PARAMETER Result is NULL
3507 if (Result
== NULL
) {
3508 return RETURN_INVALID_PARAMETER
;
3511 Augend32
= (INT32
)Augend
;
3512 Addend32
= (INT32
)Addend
;
3513 if ((Augend32
< 0) || (Augend32
> MAX_INT8
)) {
3514 *Result
= CHAR8_ERROR
;
3515 return RETURN_BUFFER_TOO_SMALL
;
3518 if ((Addend32
< 0) || (Addend32
> MAX_INT8
)) {
3519 *Result
= CHAR8_ERROR
;
3520 return RETURN_BUFFER_TOO_SMALL
;
3523 return SafeInt32ToChar8 (Augend32
+ Addend32
, Result
);
3529 Performs the requested operation using the input parameters into a value
3530 specified by Result type and stores the converted value into the caller
3531 allocated output buffer specified by Result. The caller must pass in a
3532 Result buffer that is at least as large as the Result type.
3534 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3536 If the requested operation results in an overflow or an underflow condition,
3537 then Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3539 @param[in] Augend A number to which addend will be added
3540 @param[in] Addend A number to be added to another
3541 @param[out] Result Pointer to the result of addition
3543 @retval RETURN_SUCCESS Successful addition
3544 @retval RETURN_BUFFER_TOO_SMALL Overflow
3545 @retval RETURN_INVALID_PARAMETER Result is NULL
3555 return SafeInt32ToInt16 (((INT32
)Augend
) + ((INT32
)Addend
), Result
);
3561 Performs the requested operation using the input parameters into a value
3562 specified by Result type and stores the converted value into the caller
3563 allocated output buffer specified by Result. The caller must pass in a
3564 Result buffer that is at least as large as the Result type.
3566 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3568 If the requested operation results in an overflow or an underflow condition,
3569 then Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3571 @param[in] Augend A number to which addend will be added
3572 @param[in] Addend A number to be added to another
3573 @param[out] Result Pointer to the result of addition
3575 @retval RETURN_SUCCESS Successful addition
3576 @retval RETURN_BUFFER_TOO_SMALL Overflow
3577 @retval RETURN_INVALID_PARAMETER Result is NULL
3587 return SafeInt64ToInt32 (((INT64
)Augend
) + ((INT64
)Addend
), Result
);
3593 Performs the requested operation using the input parameters into a value
3594 specified by Result type and stores the converted value into the caller
3595 allocated output buffer specified by Result. The caller must pass in a
3596 Result buffer that is at least as large as the Result type.
3598 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3600 If the requested operation results in an overflow or an underflow condition,
3601 then Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3603 @param[in] Augend A number to which addend will be added
3604 @param[in] Addend A number to be added to another
3605 @param[out] Result Pointer to the result of addition
3607 @retval RETURN_SUCCESS Successful addition
3608 @retval RETURN_BUFFER_TOO_SMALL Overflow
3609 @retval RETURN_INVALID_PARAMETER Result is NULL
3619 RETURN_STATUS Status
;
3621 if (Result
== NULL
) {
3622 return RETURN_INVALID_PARAMETER
;
3626 // * An Addend of zero can never cause underflow or overflow.
3628 // * A positive Addend can only cause overflow. The overflow condition is
3630 // (Augend + Addend) > MAX_INT64
3632 // Subtracting Addend from both sides yields
3634 // Augend > (MAX_INT64 - Addend)
3636 // This condition can be coded directly in C because the RHS will neither
3637 // underflow nor overflow. That is due to the starting condition:
3639 // 0 < Addend <= MAX_INT64
3641 // Multiplying all three sides by (-1) yields
3643 // 0 > (-Addend) >= (-MAX_INT64)
3645 // Adding MAX_INT64 to all three sides yields
3647 // MAX_INT64 > (MAX_INT64 - Addend) >= 0
3649 // * A negative Addend can only cause underflow. The underflow condition is
3651 // (Augend + Addend) < MIN_INT64
3653 // Subtracting Addend from both sides yields
3655 // Augend < (MIN_INT64 - Addend)
3657 // This condition can be coded directly in C because the RHS will neither
3658 // underflow nor overflow. That is due to the starting condition:
3660 // MIN_INT64 <= Addend < 0
3662 // Multiplying all three sides by (-1) yields
3664 // (-MIN_INT64) >= (-Addend) > 0
3666 // Adding MIN_INT64 to all three sides yields
3668 // 0 >= (MIN_INT64 - Addend) > MIN_INT64
3670 if (((Addend
> 0) && (Augend
> (MAX_INT64
- Addend
))) ||
3671 ((Addend
< 0) && (Augend
< (MIN_INT64
- Addend
))))
3673 *Result
= INT64_ERROR
;
3674 Status
= RETURN_BUFFER_TOO_SMALL
;
3676 *Result
= Augend
+ Addend
;
3677 Status
= RETURN_SUCCESS
;
3684 // Signed subtraction functions
3690 Performs the requested operation using the input parameters into a value
3691 specified by Result type and stores the converted value into the caller
3692 allocated output buffer specified by Result. The caller must pass in a
3693 Result buffer that is at least as large as the Result type.
3695 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3697 If the requested operation results in an overflow or an underflow condition,
3698 then Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3700 @param[in] Minuend A number from which another is to be subtracted.
3701 @param[in] Subtrahend A number to be subtracted from another
3702 @param[out] Result Pointer to the result of subtraction
3704 @retval RETURN_SUCCESS Successful subtraction
3705 @retval RETURN_BUFFER_TOO_SMALL Underflow
3706 @retval RETURN_INVALID_PARAMETER Result is NULL
3716 return SafeInt32ToInt8 (((INT32
)Minuend
) - ((INT32
)Subtrahend
), Result
);
3722 Performs the requested operation using the input parameters into a value
3723 specified by Result type and stores the converted value into the caller
3724 allocated output buffer specified by Result. The caller must pass in a
3725 Result buffer that is at least as large as the Result type.
3727 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3729 If the requested operation results in an overflow or an underflow condition,
3730 then Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3732 @param[in] Minuend A number from which another is to be subtracted.
3733 @param[in] Subtrahend A number to be subtracted from another
3734 @param[out] Result Pointer to the result of subtraction
3736 @retval RETURN_SUCCESS Successful subtraction
3737 @retval RETURN_BUFFER_TOO_SMALL Underflow
3738 @retval RETURN_INVALID_PARAMETER Result is NULL
3744 IN CHAR8 Subtrahend
,
3751 if (Result
== NULL
) {
3752 return RETURN_INVALID_PARAMETER
;
3755 Minuend32
= (INT32
)Minuend
;
3756 Subtrahend32
= (INT32
)Subtrahend
;
3757 if ((Minuend32
< 0) || (Minuend32
> MAX_INT8
)) {
3758 *Result
= CHAR8_ERROR
;
3759 return RETURN_BUFFER_TOO_SMALL
;
3762 if ((Subtrahend32
< 0) || (Subtrahend32
> MAX_INT8
)) {
3763 *Result
= CHAR8_ERROR
;
3764 return RETURN_BUFFER_TOO_SMALL
;
3767 return SafeInt32ToChar8 (Minuend32
- Subtrahend32
, Result
);
3773 Performs the requested operation using the input parameters into a value
3774 specified by Result type and stores the converted value into the caller
3775 allocated output buffer specified by Result. The caller must pass in a
3776 Result buffer that is at least as large as the Result type.
3778 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3780 If the requested operation results in an overflow or an underflow condition,
3781 then Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3783 @param[in] Minuend A number from which another is to be subtracted.
3784 @param[in] Subtrahend A number to be subtracted from another
3785 @param[out] Result Pointer to the result of subtraction
3787 @retval RETURN_SUCCESS Successful subtraction
3788 @retval RETURN_BUFFER_TOO_SMALL Underflow
3789 @retval RETURN_INVALID_PARAMETER Result is NULL
3795 IN INT16 Subtrahend
,
3799 return SafeInt32ToInt16 (((INT32
)Minuend
) - ((INT32
)Subtrahend
), Result
);
3805 Performs the requested operation using the input parameters into a value
3806 specified by Result type and stores the converted value into the caller
3807 allocated output buffer specified by Result. The caller must pass in a
3808 Result buffer that is at least as large as the Result type.
3810 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3812 If the requested operation results in an overflow or an underflow condition,
3813 then Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3815 @param[in] Minuend A number from which another is to be subtracted.
3816 @param[in] Subtrahend A number to be subtracted from another
3817 @param[out] Result Pointer to the result of subtraction
3819 @retval RETURN_SUCCESS Successful subtraction
3820 @retval RETURN_BUFFER_TOO_SMALL Underflow
3821 @retval RETURN_INVALID_PARAMETER Result is NULL
3827 IN INT32 Subtrahend
,
3831 return SafeInt64ToInt32 (((INT64
)Minuend
) - ((INT64
)Subtrahend
), Result
);
3837 Performs the requested operation using the input parameters into a value
3838 specified by Result type and stores the converted value into the caller
3839 allocated output buffer specified by Result. The caller must pass in a
3840 Result buffer that is at least as large as the Result type.
3842 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3844 If the requested operation results in an overflow or an underflow condition,
3845 then Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3847 @param[in] Minuend A number from which another is to be subtracted.
3848 @param[in] Subtrahend A number to be subtracted from another
3849 @param[out] Result Pointer to the result of subtraction
3851 @retval RETURN_SUCCESS Successful subtraction
3852 @retval RETURN_BUFFER_TOO_SMALL Underflow
3853 @retval RETURN_INVALID_PARAMETER Result is NULL
3859 IN INT64 Subtrahend
,
3863 RETURN_STATUS Status
;
3865 if (Result
== NULL
) {
3866 return RETURN_INVALID_PARAMETER
;
3870 // * A Subtrahend of zero can never cause underflow or overflow.
3872 // * A positive Subtrahend can only cause underflow. The underflow condition
3875 // (Minuend - Subtrahend) < MIN_INT64
3877 // Adding Subtrahend to both sides yields
3879 // Minuend < (MIN_INT64 + Subtrahend)
3881 // This condition can be coded directly in C because the RHS will neither
3882 // underflow nor overflow. That is due to the starting condition:
3884 // 0 < Subtrahend <= MAX_INT64
3886 // Adding MIN_INT64 to all three sides yields
3888 // MIN_INT64 < (MIN_INT64 + Subtrahend) <= (MIN_INT64 + MAX_INT64) = -1
3890 // * A negative Subtrahend can only cause overflow. The overflow condition is
3892 // (Minuend - Subtrahend) > MAX_INT64
3894 // Adding Subtrahend to both sides yields
3896 // Minuend > (MAX_INT64 + Subtrahend)
3898 // This condition can be coded directly in C because the RHS will neither
3899 // underflow nor overflow. That is due to the starting condition:
3901 // MIN_INT64 <= Subtrahend < 0
3903 // Adding MAX_INT64 to all three sides yields
3905 // -1 = (MAX_INT64 + MIN_INT64) <= (MAX_INT64 + Subtrahend) < MAX_INT64
3907 if (((Subtrahend
> 0) && (Minuend
< (MIN_INT64
+ Subtrahend
))) ||
3908 ((Subtrahend
< 0) && (Minuend
> (MAX_INT64
+ Subtrahend
))))
3910 *Result
= INT64_ERROR
;
3911 Status
= RETURN_BUFFER_TOO_SMALL
;
3913 *Result
= Minuend
- Subtrahend
;
3914 Status
= RETURN_SUCCESS
;
3921 // Signed multiplication functions
3927 Performs the requested operation using the input parameters into a value
3928 specified by Result type and stores the converted value into the caller
3929 allocated output buffer specified by Result. The caller must pass in a
3930 Result buffer that is at least as large as the Result type.
3932 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3934 If the requested operation results in an overflow or an underflow condition,
3935 then Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3937 @param[in] Multiplicand A number that is to be multiplied by another
3938 @param[in] Multiplier A number by which the multiplicand is to be multiplied
3939 @param[out] Result Pointer to the result of multiplication
3941 @retval RETURN_SUCCESS Successful multiplication
3942 @retval RETURN_BUFFER_TOO_SMALL Overflow
3943 @retval RETURN_INVALID_PARAMETER Result is NULL
3948 IN INT8 Multiplicand
,
3953 return SafeInt32ToInt8 (((INT32
)Multiplier
) *((INT32
)Multiplicand
), Result
);
3957 CHAR8 multiplication
3959 Performs the requested operation using the input parameters into a value
3960 specified by Result type and stores the converted value into the caller
3961 allocated output buffer specified by Result. The caller must pass in a
3962 Result buffer that is at least as large as the Result type.
3964 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3966 If the requested operation results in an overflow or an underflow condition,
3967 then Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3969 @param[in] Multiplicand A number that is to be multiplied by another
3970 @param[in] Multiplier A number by which the multiplicand is to be multiplied
3971 @param[out] Result Pointer to the result of multiplication
3973 @retval RETURN_SUCCESS Successful multiplication
3974 @retval RETURN_BUFFER_TOO_SMALL Overflow
3975 @retval RETURN_INVALID_PARAMETER Result is NULL
3980 IN CHAR8 Multiplicand
,
3981 IN CHAR8 Multiplier
,
3985 INT32 Multiplicand32
;
3988 if (Result
== NULL
) {
3989 return RETURN_INVALID_PARAMETER
;
3992 Multiplicand32
= (INT32
)Multiplicand
;
3993 Multiplier32
= (INT32
)Multiplier
;
3994 if ((Multiplicand32
< 0) || (Multiplicand32
> MAX_INT8
)) {
3995 *Result
= CHAR8_ERROR
;
3996 return RETURN_BUFFER_TOO_SMALL
;
3999 if ((Multiplier32
< 0) || (Multiplier32
> MAX_INT8
)) {
4000 *Result
= CHAR8_ERROR
;
4001 return RETURN_BUFFER_TOO_SMALL
;
4004 return SafeInt32ToChar8 (Multiplicand32
* Multiplier32
, Result
);
4008 INT16 multiplication
4010 Performs the requested operation using the input parameters into a value
4011 specified by Result type and stores the converted value into the caller
4012 allocated output buffer specified by Result. The caller must pass in a
4013 Result buffer that is at least as large as the Result type.
4015 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
4017 If the requested operation results in an overflow or an underflow condition,
4018 then Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
4020 @param[in] Multiplicand A number that is to be multiplied by another
4021 @param[in] Multiplier A number by which the multiplicand is to be multiplied
4022 @param[out] Result Pointer to the result of multiplication
4024 @retval RETURN_SUCCESS Successful multiplication
4025 @retval RETURN_BUFFER_TOO_SMALL Overflow
4026 @retval RETURN_INVALID_PARAMETER Result is NULL
4031 IN INT16 Multiplicand
,
4032 IN INT16 Multiplier
,
4036 return SafeInt32ToInt16 (((INT32
)Multiplicand
) *((INT32
)Multiplier
), Result
);
4040 INT32 multiplication
4042 Performs the requested operation using the input parameters into a value
4043 specified by Result type and stores the converted value into the caller
4044 allocated output buffer specified by Result. The caller must pass in a
4045 Result buffer that is at least as large as the Result type.
4047 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
4049 If the requested operation results in an overflow or an underflow condition,
4050 then Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
4052 @param[in] Multiplicand A number that is to be multiplied by another
4053 @param[in] Multiplier A number by which the multiplicand is to be multiplied
4054 @param[out] Result Pointer to the result of multiplication
4056 @retval RETURN_SUCCESS Successful multiplication
4057 @retval RETURN_BUFFER_TOO_SMALL Overflow
4058 @retval RETURN_INVALID_PARAMETER Result is NULL
4063 IN INT32 Multiplicand
,
4064 IN INT32 Multiplier
,
4068 return SafeInt64ToInt32 (MultS64x64 (Multiplicand
, Multiplier
), Result
);
4072 INT64 multiplication
4074 Performs the requested operation using the input parameters into a value
4075 specified by Result type and stores the converted value into the caller
4076 allocated output buffer specified by Result. The caller must pass in a
4077 Result buffer that is at least as large as the Result type.
4079 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
4081 If the requested operation results in an overflow or an underflow condition,
4082 then Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
4084 @param[in] Multiplicand A number that is to be multiplied by another
4085 @param[in] Multiplier A number by which the multiplicand is to be multiplied
4086 @param[out] Result Pointer to the result of multiplication
4088 @retval RETURN_SUCCESS Successful multiplication
4089 @retval RETURN_BUFFER_TOO_SMALL Overflow
4090 @retval RETURN_INVALID_PARAMETER Result is NULL
4095 IN INT64 Multiplicand
,
4096 IN INT64 Multiplier
,
4100 RETURN_STATUS Status
;
4101 UINT64 UnsignedMultiplicand
;
4102 UINT64 UnsignedMultiplier
;
4103 UINT64 UnsignedResult
;
4105 if (Result
== NULL
) {
4106 return RETURN_INVALID_PARAMETER
;
4110 // Split into sign and magnitude, do unsigned operation, apply sign.
4112 if (Multiplicand
< 0) {
4114 // Avoid negating the most negative number.
4116 UnsignedMultiplicand
= ((UINT64
)(-(Multiplicand
+ 1))) + 1;
4118 UnsignedMultiplicand
= (UINT64
)Multiplicand
;
4121 if (Multiplier
< 0) {
4123 // Avoid negating the most negative number.
4125 UnsignedMultiplier
= ((UINT64
)(-(Multiplier
+ 1))) + 1;
4127 UnsignedMultiplier
= (UINT64
)Multiplier
;
4130 Status
= SafeUint64Mult (UnsignedMultiplicand
, UnsignedMultiplier
, &UnsignedResult
);
4131 if (!RETURN_ERROR (Status
)) {
4132 if ((Multiplicand
< 0) != (Multiplier
< 0)) {
4133 if (UnsignedResult
> MIN_INT64_MAGNITUDE
) {
4134 *Result
= INT64_ERROR
;
4135 Status
= RETURN_BUFFER_TOO_SMALL
;
4136 } else if (UnsignedResult
== MIN_INT64_MAGNITUDE
) {
4137 *Result
= MIN_INT64
;
4139 *Result
= -((INT64
)UnsignedResult
);
4142 if (UnsignedResult
> MAX_INT64
) {
4143 *Result
= INT64_ERROR
;
4144 Status
= RETURN_BUFFER_TOO_SMALL
;
4146 *Result
= (INT64
)UnsignedResult
;
4150 *Result
= INT64_ERROR
;