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 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 1. Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
20 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
24 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <Library/SafeIntLib.h>
34 // Magnitude of MIN_INT64 as expressed by a UINT64 number.
36 #define MIN_INT64_MAGNITUDE ((((UINT64) - (MIN_INT64 + 1))) + 1)
39 // Conversion functions
41 // There are three reasons for having conversion functions:
43 // 1. We are converting from a signed type to an unsigned type of the same
44 // size, or vice-versa.
46 // 2. We are converting to a smaller type, and we could therefore possibly
49 // 3. We are converting to a bigger type, and we are signed and the type we are
50 // converting to is unsigned.
54 INT8 -> UINT8 conversion
56 Converts the value specified by Operand to a value specified by Result type
57 and stores the converted value into the caller allocated output buffer
58 specified by Result. The caller must pass in a Result buffer that is at
59 least as large as the Result type.
61 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
63 If the conversion results in an overflow or an underflow condition, then
64 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
66 @param[in] Operand Operand to be converted to new type
67 @param[out] Result Pointer to the result of conversion
69 @retval RETURN_SUCCESS Successful conversion
70 @retval RETURN_BUFFER_TOO_SMALL Overflow
71 @retval RETURN_INVALID_PARAMETER Result is NULL
83 return RETURN_INVALID_PARAMETER
;
87 *Result
= (UINT8
)Operand
;
88 Status
= RETURN_SUCCESS
;
90 *Result
= UINT8_ERROR
;
91 Status
= RETURN_BUFFER_TOO_SMALL
;
98 INT8 -> CHAR8 conversion
100 Converts the value specified by Operand to a value specified by Result type
101 and stores the converted value into the caller allocated output buffer
102 specified by Result. The caller must pass in a Result buffer that is at
103 least as large as the Result type.
105 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
107 If the conversion results in an overflow or an underflow condition, then
108 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
110 @param[in] Operand Operand to be converted to new type
111 @param[out] Result Pointer to the result of conversion
113 @retval RETURN_SUCCESS Successful conversion
114 @retval RETURN_BUFFER_TOO_SMALL Overflow
115 @retval RETURN_INVALID_PARAMETER Result is NULL
124 RETURN_STATUS Status
;
126 if (Result
== NULL
) {
127 return RETURN_INVALID_PARAMETER
;
131 *Result
= (CHAR8
)Operand
;
132 Status
= RETURN_SUCCESS
;
134 *Result
= CHAR8_ERROR
;
135 Status
= RETURN_BUFFER_TOO_SMALL
;
142 INT8 -> UINT16 conversion
144 Converts the value specified by Operand to a value specified by Result type
145 and stores the converted value into the caller allocated output buffer
146 specified by Result. The caller must pass in a Result buffer that is at
147 least as large as the Result type.
149 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
151 If the conversion results in an overflow or an underflow condition, then
152 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
154 @param[in] Operand Operand to be converted to new type
155 @param[out] Result Pointer to the result of conversion
157 @retval RETURN_SUCCESS Successful conversion
158 @retval RETURN_BUFFER_TOO_SMALL Overflow
159 @retval RETURN_INVALID_PARAMETER Result is NULL
168 RETURN_STATUS Status
;
170 if (Result
== NULL
) {
171 return RETURN_INVALID_PARAMETER
;
175 *Result
= (UINT16
)Operand
;
176 Status
= RETURN_SUCCESS
;
178 *Result
= UINT16_ERROR
;
179 Status
= RETURN_BUFFER_TOO_SMALL
;
186 INT8 -> UINT32 conversion
188 Converts the value specified by Operand to a value specified by Result type
189 and stores the converted value into the caller allocated output buffer
190 specified by Result. The caller must pass in a Result buffer that is at
191 least as large as the Result type.
193 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
195 If the conversion results in an overflow or an underflow condition, then
196 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
198 @param[in] Operand Operand to be converted to new type
199 @param[out] Result Pointer to the result of conversion
201 @retval RETURN_SUCCESS Successful conversion
202 @retval RETURN_BUFFER_TOO_SMALL Overflow
203 @retval RETURN_INVALID_PARAMETER Result is NULL
212 RETURN_STATUS Status
;
214 if (Result
== NULL
) {
215 return RETURN_INVALID_PARAMETER
;
219 *Result
= (UINT32
)Operand
;
220 Status
= RETURN_SUCCESS
;
222 *Result
= UINT32_ERROR
;
223 Status
= RETURN_BUFFER_TOO_SMALL
;
230 INT8 -> UINTN conversion
232 Converts the value specified by Operand to a value specified by Result type
233 and stores the converted value into the caller allocated output buffer
234 specified by Result. The caller must pass in a Result buffer that is at
235 least as large as the Result type.
237 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
239 If the conversion results in an overflow or an underflow condition, then
240 Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
242 @param[in] Operand Operand to be converted to new type
243 @param[out] Result Pointer to the result of conversion
245 @retval RETURN_SUCCESS Successful conversion
246 @retval RETURN_BUFFER_TOO_SMALL Overflow
247 @retval RETURN_INVALID_PARAMETER Result is NULL
256 RETURN_STATUS Status
;
258 if (Result
== NULL
) {
259 return RETURN_INVALID_PARAMETER
;
263 *Result
= (UINTN
)Operand
;
264 Status
= RETURN_SUCCESS
;
266 *Result
= UINTN_ERROR
;
267 Status
= RETURN_BUFFER_TOO_SMALL
;
274 INT8 -> UINT64 conversion
276 Converts the value specified by Operand to a value specified by Result type
277 and stores the converted value into the caller allocated output buffer
278 specified by Result. The caller must pass in a Result buffer that is at
279 least as large as the Result type.
281 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
283 If the conversion results in an overflow or an underflow condition, then
284 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
286 @param[in] Operand Operand to be converted to new type
287 @param[out] Result Pointer to the result of conversion
289 @retval RETURN_SUCCESS Successful conversion
290 @retval RETURN_BUFFER_TOO_SMALL Overflow
291 @retval RETURN_INVALID_PARAMETER Result is NULL
300 RETURN_STATUS Status
;
302 if (Result
== NULL
) {
303 return RETURN_INVALID_PARAMETER
;
307 *Result
= (UINT64
)Operand
;
308 Status
= RETURN_SUCCESS
;
310 *Result
= UINT64_ERROR
;
311 Status
= RETURN_BUFFER_TOO_SMALL
;
318 UINT8 -> INT8 conversion
320 Converts the value specified by Operand to a value specified by Result type
321 and stores the converted value into the caller allocated output buffer
322 specified by Result. The caller must pass in a Result buffer that is at
323 least as large as the Result type.
325 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
327 If the conversion results in an overflow or an underflow condition, then
328 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
330 @param[in] Operand Operand to be converted to new type
331 @param[out] Result Pointer to the result of conversion
333 @retval RETURN_SUCCESS Successful conversion
334 @retval RETURN_BUFFER_TOO_SMALL Overflow
335 @retval RETURN_INVALID_PARAMETER Result is NULL
344 RETURN_STATUS Status
;
346 if (Result
== NULL
) {
347 return RETURN_INVALID_PARAMETER
;
350 if (Operand
<= MAX_INT8
) {
351 *Result
= (INT8
)Operand
;
352 Status
= RETURN_SUCCESS
;
354 *Result
= INT8_ERROR
;
355 Status
= RETURN_BUFFER_TOO_SMALL
;
362 UINT8 -> CHAR8 conversion
364 Converts the value specified by Operand to a value specified by Result type
365 and stores the converted value into the caller allocated output buffer
366 specified by Result. The caller must pass in a Result buffer that is at
367 least as large as the Result type.
369 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
371 If the conversion results in an overflow or an underflow condition, then
372 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
374 @param[in] Operand Operand to be converted to new type
375 @param[out] Result Pointer to the result of conversion
377 @retval RETURN_SUCCESS Successful conversion
378 @retval RETURN_BUFFER_TOO_SMALL Overflow
379 @retval RETURN_INVALID_PARAMETER Result is NULL
388 RETURN_STATUS Status
;
390 if (Result
== NULL
) {
391 return RETURN_INVALID_PARAMETER
;
394 if (Operand
<= MAX_INT8
) {
395 *Result
= (CHAR8
)Operand
;
396 Status
= RETURN_SUCCESS
;
398 *Result
= CHAR8_ERROR
;
399 Status
= RETURN_BUFFER_TOO_SMALL
;
406 INT16 -> INT8 conversion
408 Converts the value specified by Operand to a value specified by Result type
409 and stores the converted value into the caller allocated output buffer
410 specified by Result. The caller must pass in a Result buffer that is at
411 least as large as the Result type.
413 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
415 If the conversion results in an overflow or an underflow condition, then
416 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
418 @param[in] Operand Operand to be converted to new type
419 @param[out] Result Pointer to the result of conversion
421 @retval RETURN_SUCCESS Successful conversion
422 @retval RETURN_BUFFER_TOO_SMALL Overflow
423 @retval RETURN_INVALID_PARAMETER Result is NULL
432 RETURN_STATUS Status
;
434 if (Result
== NULL
) {
435 return RETURN_INVALID_PARAMETER
;
438 if ((Operand
>= MIN_INT8
) && (Operand
<= MAX_INT8
)) {
439 *Result
= (INT8
)Operand
;
440 Status
= RETURN_SUCCESS
;
442 *Result
= INT8_ERROR
;
443 Status
= RETURN_BUFFER_TOO_SMALL
;
450 INT16 -> CHAR8 conversion
452 Converts the value specified by Operand to a value specified by Result type
453 and stores the converted value into the caller allocated output buffer
454 specified by Result. The caller must pass in a Result buffer that is at
455 least as large as the Result type.
457 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
459 If the conversion results in an overflow or an underflow condition, then
460 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
462 @param[in] Operand Operand to be converted to new type
463 @param[out] Result Pointer to the result of conversion
465 @retval RETURN_SUCCESS Successful conversion
466 @retval RETURN_BUFFER_TOO_SMALL Overflow
467 @retval RETURN_INVALID_PARAMETER Result is NULL
476 RETURN_STATUS Status
;
478 if (Result
== NULL
) {
479 return RETURN_INVALID_PARAMETER
;
482 if ((Operand
>= 0) && (Operand
<= MAX_INT8
)) {
483 *Result
= (CHAR8
)Operand
;
484 Status
= RETURN_SUCCESS
;
486 *Result
= CHAR8_ERROR
;
487 Status
= RETURN_BUFFER_TOO_SMALL
;
494 INT16 -> UINT8 conversion
496 Converts the value specified by Operand to a value specified by Result type
497 and stores the converted value into the caller allocated output buffer
498 specified by Result. The caller must pass in a Result buffer that is at
499 least as large as the Result type.
501 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
503 If the conversion results in an overflow or an underflow condition, then
504 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
506 @param[in] Operand Operand to be converted to new type
507 @param[out] Result Pointer to the result of conversion
509 @retval RETURN_SUCCESS Successful conversion
510 @retval RETURN_BUFFER_TOO_SMALL Overflow
511 @retval RETURN_INVALID_PARAMETER Result is NULL
520 RETURN_STATUS Status
;
522 if (Result
== NULL
) {
523 return RETURN_INVALID_PARAMETER
;
526 if ((Operand
>= 0) && (Operand
<= MAX_UINT8
)) {
527 *Result
= (UINT8
)Operand
;
528 Status
= RETURN_SUCCESS
;
530 *Result
= UINT8_ERROR
;
531 Status
= RETURN_BUFFER_TOO_SMALL
;
538 INT16 -> UINT16 conversion
540 Converts the value specified by Operand to a value specified by Result type
541 and stores the converted value into the caller allocated output buffer
542 specified by Result. The caller must pass in a Result buffer that is at
543 least as large as the Result type.
545 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
547 If the conversion results in an overflow or an underflow condition, then
548 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
550 @param[in] Operand Operand to be converted to new type
551 @param[out] Result Pointer to the result of conversion
553 @retval RETURN_SUCCESS Successful conversion
554 @retval RETURN_BUFFER_TOO_SMALL Overflow
555 @retval RETURN_INVALID_PARAMETER Result is NULL
564 RETURN_STATUS Status
;
566 if (Result
== NULL
) {
567 return RETURN_INVALID_PARAMETER
;
571 *Result
= (UINT16
)Operand
;
572 Status
= RETURN_SUCCESS
;
574 *Result
= UINT16_ERROR
;
575 Status
= RETURN_BUFFER_TOO_SMALL
;
582 INT16 -> UINT32 conversion
584 Converts the value specified by Operand to a value specified by Result type
585 and stores the converted value into the caller allocated output buffer
586 specified by Result. The caller must pass in a Result buffer that is at
587 least as large as the Result type.
589 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
591 If the conversion results in an overflow or an underflow condition, then
592 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
594 @param[in] Operand Operand to be converted to new type
595 @param[out] Result Pointer to the result of conversion
597 @retval RETURN_SUCCESS Successful conversion
598 @retval RETURN_BUFFER_TOO_SMALL Overflow
599 @retval RETURN_INVALID_PARAMETER Result is NULL
608 RETURN_STATUS Status
;
610 if (Result
== NULL
) {
611 return RETURN_INVALID_PARAMETER
;
615 *Result
= (UINT32
)Operand
;
616 Status
= RETURN_SUCCESS
;
618 *Result
= UINT32_ERROR
;
619 Status
= RETURN_BUFFER_TOO_SMALL
;
626 INT16 -> UINTN conversion
628 Converts the value specified by Operand to a value specified by Result type
629 and stores the converted value into the caller allocated output buffer
630 specified by Result. The caller must pass in a Result buffer that is at
631 least as large as the Result type.
633 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
635 If the conversion results in an overflow or an underflow condition, then
636 Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
638 @param[in] Operand Operand to be converted to new type
639 @param[out] Result Pointer to the result of conversion
641 @retval RETURN_SUCCESS Successful conversion
642 @retval RETURN_BUFFER_TOO_SMALL Overflow
643 @retval RETURN_INVALID_PARAMETER Result is NULL
652 RETURN_STATUS Status
;
654 if (Result
== NULL
) {
655 return RETURN_INVALID_PARAMETER
;
659 *Result
= (UINTN
)Operand
;
660 Status
= RETURN_SUCCESS
;
662 *Result
= UINTN_ERROR
;
663 Status
= RETURN_BUFFER_TOO_SMALL
;
670 INT16 -> UINT64 conversion
672 Converts the value specified by Operand to a value specified by Result type
673 and stores the converted value into the caller allocated output buffer
674 specified by Result. The caller must pass in a Result buffer that is at
675 least as large as the Result type.
677 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
679 If the conversion results in an overflow or an underflow condition, then
680 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
682 @param[in] Operand Operand to be converted to new type
683 @param[out] Result Pointer to the result of conversion
685 @retval RETURN_SUCCESS Successful conversion
686 @retval RETURN_BUFFER_TOO_SMALL Overflow
687 @retval RETURN_INVALID_PARAMETER Result is NULL
696 RETURN_STATUS Status
;
698 if (Result
== NULL
) {
699 return RETURN_INVALID_PARAMETER
;
703 *Result
= (UINT64
)Operand
;
704 Status
= RETURN_SUCCESS
;
706 *Result
= UINT64_ERROR
;
707 Status
= RETURN_BUFFER_TOO_SMALL
;
714 UINT16 -> INT8 conversion
716 Converts the value specified by Operand to a value specified by Result type
717 and stores the converted value into the caller allocated output buffer
718 specified by Result. The caller must pass in a Result buffer that is at
719 least as large as the Result type.
721 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
723 If the conversion results in an overflow or an underflow condition, then
724 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
726 @param[in] Operand Operand to be converted to new type
727 @param[out] Result Pointer to the result of conversion
729 @retval RETURN_SUCCESS Successful conversion
730 @retval RETURN_BUFFER_TOO_SMALL Overflow
731 @retval RETURN_INVALID_PARAMETER Result is NULL
740 RETURN_STATUS Status
;
742 if (Result
== NULL
) {
743 return RETURN_INVALID_PARAMETER
;
746 if (Operand
<= MAX_INT8
) {
747 *Result
= (INT8
)Operand
;
748 Status
= RETURN_SUCCESS
;
750 *Result
= INT8_ERROR
;
751 Status
= RETURN_BUFFER_TOO_SMALL
;
758 UINT16 -> CHAR8 conversion
760 Converts the value specified by Operand to a value specified by Result type
761 and stores the converted value into the caller allocated output buffer
762 specified by Result. The caller must pass in a Result buffer that is at
763 least as large as the Result type.
765 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
767 If the conversion results in an overflow or an underflow condition, then
768 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
770 @param[in] Operand Operand to be converted to new type
771 @param[out] Result Pointer to the result of conversion
773 @retval RETURN_SUCCESS Successful conversion
774 @retval RETURN_BUFFER_TOO_SMALL Overflow
775 @retval RETURN_INVALID_PARAMETER Result is NULL
784 RETURN_STATUS Status
;
786 if (Result
== NULL
) {
787 return RETURN_INVALID_PARAMETER
;
790 if (Operand
<= MAX_INT8
) {
791 *Result
= (INT8
)Operand
;
792 Status
= RETURN_SUCCESS
;
794 *Result
= CHAR8_ERROR
;
795 Status
= RETURN_BUFFER_TOO_SMALL
;
802 UINT16 -> UINT8 conversion
804 Converts the value specified by Operand to a value specified by Result type
805 and stores the converted value into the caller allocated output buffer
806 specified by Result. The caller must pass in a Result buffer that is at
807 least as large as the Result type.
809 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
811 If the conversion results in an overflow or an underflow condition, then
812 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
814 @param[in] Operand Operand to be converted to new type
815 @param[out] Result Pointer to the result of conversion
817 @retval RETURN_SUCCESS Successful conversion
818 @retval RETURN_BUFFER_TOO_SMALL Overflow
819 @retval RETURN_INVALID_PARAMETER Result is NULL
828 RETURN_STATUS Status
;
830 if (Result
== NULL
) {
831 return RETURN_INVALID_PARAMETER
;
834 if (Operand
<= MAX_UINT8
) {
835 *Result
= (UINT8
)Operand
;
836 Status
= RETURN_SUCCESS
;
838 *Result
= UINT8_ERROR
;
839 Status
= RETURN_BUFFER_TOO_SMALL
;
846 UINT16 -> INT16 conversion
848 Converts the value specified by Operand to a value specified by Result type
849 and stores the converted value into the caller allocated output buffer
850 specified by Result. The caller must pass in a Result buffer that is at
851 least as large as the Result type.
853 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
855 If the conversion results in an overflow or an underflow condition, then
856 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
858 @param[in] Operand Operand to be converted to new type
859 @param[out] Result Pointer to the result of conversion
861 @retval RETURN_SUCCESS Successful conversion
862 @retval RETURN_BUFFER_TOO_SMALL Overflow
863 @retval RETURN_INVALID_PARAMETER Result is NULL
872 RETURN_STATUS Status
;
874 if (Result
== NULL
) {
875 return RETURN_INVALID_PARAMETER
;
878 if (Operand
<= MAX_INT16
) {
879 *Result
= (INT16
)Operand
;
880 Status
= RETURN_SUCCESS
;
882 *Result
= INT16_ERROR
;
883 Status
= RETURN_BUFFER_TOO_SMALL
;
890 INT32 -> INT8 conversion
892 Converts the value specified by Operand to a value specified by Result type
893 and stores the converted value into the caller allocated output buffer
894 specified by Result. The caller must pass in a Result buffer that is at
895 least as large as the Result type.
897 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
899 If the conversion results in an overflow or an underflow condition, then
900 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
902 @param[in] Operand Operand to be converted to new type
903 @param[out] Result Pointer to the result of conversion
905 @retval RETURN_SUCCESS Successful conversion
906 @retval RETURN_BUFFER_TOO_SMALL Overflow
907 @retval RETURN_INVALID_PARAMETER Result is NULL
916 RETURN_STATUS Status
;
918 if (Result
== NULL
) {
919 return RETURN_INVALID_PARAMETER
;
922 if ((Operand
>= MIN_INT8
) && (Operand
<= MAX_INT8
)) {
923 *Result
= (INT8
)Operand
;
924 Status
= RETURN_SUCCESS
;
926 *Result
= INT8_ERROR
;
927 Status
= RETURN_BUFFER_TOO_SMALL
;
934 INT32 -> CHAR8 conversion
936 Converts the value specified by Operand to a value specified by Result type
937 and stores the converted value into the caller allocated output buffer
938 specified by Result. The caller must pass in a Result buffer that is at
939 least as large as the Result type.
941 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
943 If the conversion results in an overflow or an underflow condition, then
944 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
946 @param[in] Operand Operand to be converted to new type
947 @param[out] Result Pointer to the result of conversion
949 @retval RETURN_SUCCESS Successful conversion
950 @retval RETURN_BUFFER_TOO_SMALL Overflow
951 @retval RETURN_INVALID_PARAMETER Result is NULL
960 RETURN_STATUS Status
;
962 if (Result
== NULL
) {
963 return RETURN_INVALID_PARAMETER
;
966 if ((Operand
>= 0) && (Operand
<= MAX_INT8
)) {
967 *Result
= (CHAR8
)Operand
;
968 Status
= RETURN_SUCCESS
;
970 *Result
= CHAR8_ERROR
;
971 Status
= RETURN_BUFFER_TOO_SMALL
;
978 INT32 -> UINT8 conversion
980 Converts the value specified by Operand to a value specified by Result type
981 and stores the converted value into the caller allocated output buffer
982 specified by Result. The caller must pass in a Result buffer that is at
983 least as large as the Result type.
985 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
987 If the conversion results in an overflow or an underflow condition, then
988 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
990 @param[in] Operand Operand to be converted to new type
991 @param[out] Result Pointer to the result of conversion
993 @retval RETURN_SUCCESS Successful conversion
994 @retval RETURN_BUFFER_TOO_SMALL Overflow
995 @retval RETURN_INVALID_PARAMETER Result is NULL
1004 RETURN_STATUS Status
;
1006 if (Result
== NULL
) {
1007 return RETURN_INVALID_PARAMETER
;
1010 if ((Operand
>= 0) && (Operand
<= MAX_UINT8
)) {
1011 *Result
= (UINT8
)Operand
;
1012 Status
= RETURN_SUCCESS
;
1014 *Result
= UINT8_ERROR
;
1015 Status
= RETURN_BUFFER_TOO_SMALL
;
1022 INT32 -> INT16 conversion
1024 Converts the value specified by Operand to a value specified by Result type
1025 and stores the converted value into the caller allocated output buffer
1026 specified by Result. The caller must pass in a Result buffer that is at
1027 least as large as the Result type.
1029 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1031 If the conversion results in an overflow or an underflow condition, then
1032 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1034 @param[in] Operand Operand to be converted to new type
1035 @param[out] Result Pointer to the result of conversion
1037 @retval RETURN_SUCCESS Successful conversion
1038 @retval RETURN_BUFFER_TOO_SMALL Overflow
1039 @retval RETURN_INVALID_PARAMETER Result is NULL
1048 RETURN_STATUS Status
;
1050 if (Result
== NULL
) {
1051 return RETURN_INVALID_PARAMETER
;
1054 if ((Operand
>= MIN_INT16
) && (Operand
<= MAX_INT16
)) {
1055 *Result
= (INT16
)Operand
;
1056 Status
= RETURN_SUCCESS
;
1058 *Result
= INT16_ERROR
;
1059 Status
= RETURN_BUFFER_TOO_SMALL
;
1066 INT32 -> UINT16 conversion
1068 Converts the value specified by Operand to a value specified by Result type
1069 and stores the converted value into the caller allocated output buffer
1070 specified by Result. The caller must pass in a Result buffer that is at
1071 least as large as the Result type.
1073 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1075 If the conversion results in an overflow or an underflow condition, then
1076 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1078 @param[in] Operand Operand to be converted to new type
1079 @param[out] Result Pointer to the result of conversion
1081 @retval RETURN_SUCCESS Successful conversion
1082 @retval RETURN_BUFFER_TOO_SMALL Overflow
1083 @retval RETURN_INVALID_PARAMETER Result is NULL
1092 RETURN_STATUS Status
;
1094 if (Result
== NULL
) {
1095 return RETURN_INVALID_PARAMETER
;
1098 if ((Operand
>= 0) && (Operand
<= MAX_UINT16
)) {
1099 *Result
= (UINT16
)Operand
;
1100 Status
= RETURN_SUCCESS
;
1102 *Result
= UINT16_ERROR
;
1103 Status
= RETURN_BUFFER_TOO_SMALL
;
1110 INT32 -> UINT32 conversion
1112 Converts the value specified by Operand to a value specified by Result type
1113 and stores the converted value into the caller allocated output buffer
1114 specified by Result. The caller must pass in a Result buffer that is at
1115 least as large as the Result type.
1117 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1119 If the conversion results in an overflow or an underflow condition, then
1120 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1122 @param[in] Operand Operand to be converted to new type
1123 @param[out] Result Pointer to the result of conversion
1125 @retval RETURN_SUCCESS Successful conversion
1126 @retval RETURN_BUFFER_TOO_SMALL Overflow
1127 @retval RETURN_INVALID_PARAMETER Result is NULL
1136 RETURN_STATUS Status
;
1138 if (Result
== NULL
) {
1139 return RETURN_INVALID_PARAMETER
;
1143 *Result
= (UINT32
)Operand
;
1144 Status
= RETURN_SUCCESS
;
1146 *Result
= UINT32_ERROR
;
1147 Status
= RETURN_BUFFER_TOO_SMALL
;
1154 INT32 -> UINT64 conversion
1156 Converts the value specified by Operand to a value specified by Result type
1157 and stores the converted value into the caller allocated output buffer
1158 specified by Result. The caller must pass in a Result buffer that is at
1159 least as large as the Result type.
1161 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1163 If the conversion results in an overflow or an underflow condition, then
1164 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1166 @param[in] Operand Operand to be converted to new type
1167 @param[out] Result Pointer to the result of conversion
1169 @retval RETURN_SUCCESS Successful conversion
1170 @retval RETURN_BUFFER_TOO_SMALL Overflow
1171 @retval RETURN_INVALID_PARAMETER Result is NULL
1180 RETURN_STATUS Status
;
1182 if (Result
== NULL
) {
1183 return RETURN_INVALID_PARAMETER
;
1187 *Result
= (UINT64
)Operand
;
1188 Status
= RETURN_SUCCESS
;
1190 *Result
= UINT64_ERROR
;
1191 Status
= RETURN_BUFFER_TOO_SMALL
;
1198 UINT32 -> INT8 conversion
1200 Converts the value specified by Operand to a value specified by Result type
1201 and stores the converted value into the caller allocated output buffer
1202 specified by Result. The caller must pass in a Result buffer that is at
1203 least as large as the Result type.
1205 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1207 If the conversion results in an overflow or an underflow condition, then
1208 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1210 @param[in] Operand Operand to be converted to new type
1211 @param[out] Result Pointer to the result of conversion
1213 @retval RETURN_SUCCESS Successful conversion
1214 @retval RETURN_BUFFER_TOO_SMALL Overflow
1215 @retval RETURN_INVALID_PARAMETER Result is NULL
1224 RETURN_STATUS Status
;
1226 if (Result
== NULL
) {
1227 return RETURN_INVALID_PARAMETER
;
1230 if (Operand
<= MAX_INT8
) {
1231 *Result
= (INT8
)Operand
;
1232 Status
= RETURN_SUCCESS
;
1234 *Result
= INT8_ERROR
;
1235 Status
= RETURN_BUFFER_TOO_SMALL
;
1242 UINT32 -> CHAR8 conversion
1244 Converts the value specified by Operand to a value specified by Result type
1245 and stores the converted value into the caller allocated output buffer
1246 specified by Result. The caller must pass in a Result buffer that is at
1247 least as large as the Result type.
1249 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1251 If the conversion results in an overflow or an underflow condition, then
1252 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1254 @param[in] Operand Operand to be converted to new type
1255 @param[out] Result Pointer to the result of conversion
1257 @retval RETURN_SUCCESS Successful conversion
1258 @retval RETURN_BUFFER_TOO_SMALL Overflow
1259 @retval RETURN_INVALID_PARAMETER Result is NULL
1268 RETURN_STATUS Status
;
1270 if (Result
== NULL
) {
1271 return RETURN_INVALID_PARAMETER
;
1274 if (Operand
<= MAX_INT8
) {
1275 *Result
= (INT8
)Operand
;
1276 Status
= RETURN_SUCCESS
;
1278 *Result
= CHAR8_ERROR
;
1279 Status
= RETURN_BUFFER_TOO_SMALL
;
1286 UINT32 -> UINT8 conversion
1288 Converts the value specified by Operand to a value specified by Result type
1289 and stores the converted value into the caller allocated output buffer
1290 specified by Result. The caller must pass in a Result buffer that is at
1291 least as large as the Result type.
1293 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1295 If the conversion results in an overflow or an underflow condition, then
1296 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1298 @param[in] Operand Operand to be converted to new type
1299 @param[out] Result Pointer to the result of conversion
1301 @retval RETURN_SUCCESS Successful conversion
1302 @retval RETURN_BUFFER_TOO_SMALL Overflow
1303 @retval RETURN_INVALID_PARAMETER Result is NULL
1312 RETURN_STATUS Status
;
1314 if (Result
== NULL
) {
1315 return RETURN_INVALID_PARAMETER
;
1318 if (Operand
<= MAX_UINT8
) {
1319 *Result
= (UINT8
)Operand
;
1320 Status
= RETURN_SUCCESS
;
1322 *Result
= UINT8_ERROR
;
1323 Status
= RETURN_BUFFER_TOO_SMALL
;
1330 UINT32 -> INT16 conversion
1332 Converts the value specified by Operand to a value specified by Result type
1333 and stores the converted value into the caller allocated output buffer
1334 specified by Result. The caller must pass in a Result buffer that is at
1335 least as large as the Result type.
1337 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1339 If the conversion results in an overflow or an underflow condition, then
1340 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1342 @param[in] Operand Operand to be converted to new type
1343 @param[out] Result Pointer to the result of conversion
1345 @retval RETURN_SUCCESS Successful conversion
1346 @retval RETURN_BUFFER_TOO_SMALL Overflow
1347 @retval RETURN_INVALID_PARAMETER Result is NULL
1356 RETURN_STATUS Status
;
1358 if (Result
== NULL
) {
1359 return RETURN_INVALID_PARAMETER
;
1362 if (Operand
<= MAX_INT16
) {
1363 *Result
= (INT16
)Operand
;
1364 Status
= RETURN_SUCCESS
;
1366 *Result
= INT16_ERROR
;
1367 Status
= RETURN_BUFFER_TOO_SMALL
;
1374 UINT32 -> UINT16 conversion
1376 Converts the value specified by Operand to a value specified by Result type
1377 and stores the converted value into the caller allocated output buffer
1378 specified by Result. The caller must pass in a Result buffer that is at
1379 least as large as the Result type.
1381 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1383 If the conversion results in an overflow or an underflow condition, then
1384 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1386 @param[in] Operand Operand to be converted to new type
1387 @param[out] Result Pointer to the result of conversion
1389 @retval RETURN_SUCCESS Successful conversion
1390 @retval RETURN_BUFFER_TOO_SMALL Overflow
1391 @retval RETURN_INVALID_PARAMETER Result is NULL
1395 SafeUint32ToUint16 (
1400 RETURN_STATUS Status
;
1402 if (Result
== NULL
) {
1403 return RETURN_INVALID_PARAMETER
;
1406 if (Operand
<= MAX_UINT16
) {
1407 *Result
= (UINT16
)Operand
;
1408 Status
= RETURN_SUCCESS
;
1410 *Result
= UINT16_ERROR
;
1411 Status
= RETURN_BUFFER_TOO_SMALL
;
1418 UINT32 -> INT32 conversion
1420 Converts the value specified by Operand to a value specified by Result type
1421 and stores the converted value into the caller allocated output buffer
1422 specified by Result. The caller must pass in a Result buffer that is at
1423 least as large as the Result type.
1425 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1427 If the conversion results in an overflow or an underflow condition, then
1428 Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1430 @param[in] Operand Operand to be converted to new type
1431 @param[out] Result Pointer to the result of conversion
1433 @retval RETURN_SUCCESS Successful conversion
1434 @retval RETURN_BUFFER_TOO_SMALL Overflow
1435 @retval RETURN_INVALID_PARAMETER Result is NULL
1444 RETURN_STATUS Status
;
1446 if (Result
== NULL
) {
1447 return RETURN_INVALID_PARAMETER
;
1450 if (Operand
<= MAX_INT32
) {
1451 *Result
= (INT32
)Operand
;
1452 Status
= RETURN_SUCCESS
;
1454 *Result
= INT32_ERROR
;
1455 Status
= RETURN_BUFFER_TOO_SMALL
;
1462 INTN -> INT8 conversion
1464 Converts the value specified by Operand to a value specified by Result type
1465 and stores the converted value into the caller allocated output buffer
1466 specified by Result. The caller must pass in a Result buffer that is at
1467 least as large as the Result type.
1469 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1471 If the conversion results in an overflow or an underflow condition, then
1472 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1474 @param[in] Operand Operand to be converted to new type
1475 @param[out] Result Pointer to the result of conversion
1477 @retval RETURN_SUCCESS Successful conversion
1478 @retval RETURN_BUFFER_TOO_SMALL Overflow
1479 @retval RETURN_INVALID_PARAMETER Result is NULL
1488 RETURN_STATUS Status
;
1490 if (Result
== NULL
) {
1491 return RETURN_INVALID_PARAMETER
;
1494 if ((Operand
>= MIN_INT8
) && (Operand
<= MAX_INT8
)) {
1495 *Result
= (INT8
)Operand
;
1496 Status
= RETURN_SUCCESS
;
1498 *Result
= INT8_ERROR
;
1499 Status
= RETURN_BUFFER_TOO_SMALL
;
1506 INTN -> CHAR8 conversion
1508 Converts the value specified by Operand to a value specified by Result type
1509 and stores the converted value into the caller allocated output buffer
1510 specified by Result. The caller must pass in a Result buffer that is at
1511 least as large as the Result type.
1513 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1515 If the conversion results in an overflow or an underflow condition, then
1516 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1518 @param[in] Operand Operand to be converted to new type
1519 @param[out] Result Pointer to the result of conversion
1521 @retval RETURN_SUCCESS Successful conversion
1522 @retval RETURN_BUFFER_TOO_SMALL Overflow
1523 @retval RETURN_INVALID_PARAMETER Result is NULL
1532 RETURN_STATUS Status
;
1534 if (Result
== NULL
) {
1535 return RETURN_INVALID_PARAMETER
;
1538 if ((Operand
>= 0) && (Operand
<= MAX_INT8
)) {
1539 *Result
= (CHAR8
)Operand
;
1540 Status
= RETURN_SUCCESS
;
1542 *Result
= CHAR8_ERROR
;
1543 Status
= RETURN_BUFFER_TOO_SMALL
;
1550 INTN -> UINT8 conversion
1552 Converts the value specified by Operand to a value specified by Result type
1553 and stores the converted value into the caller allocated output buffer
1554 specified by Result. The caller must pass in a Result buffer that is at
1555 least as large as the Result type.
1557 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1559 If the conversion results in an overflow or an underflow condition, then
1560 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1562 @param[in] Operand Operand to be converted to new type
1563 @param[out] Result Pointer to the result of conversion
1565 @retval RETURN_SUCCESS Successful conversion
1566 @retval RETURN_BUFFER_TOO_SMALL Overflow
1567 @retval RETURN_INVALID_PARAMETER Result is NULL
1576 RETURN_STATUS Status
;
1578 if (Result
== NULL
) {
1579 return RETURN_INVALID_PARAMETER
;
1582 if ((Operand
>= 0) && (Operand
<= MAX_UINT8
)) {
1583 *Result
= (UINT8
)Operand
;
1584 Status
= RETURN_SUCCESS
;
1586 *Result
= UINT8_ERROR
;
1587 Status
= RETURN_BUFFER_TOO_SMALL
;
1594 INTN -> INT16 conversion
1596 Converts the value specified by Operand to a value specified by Result type
1597 and stores the converted value into the caller allocated output buffer
1598 specified by Result. The caller must pass in a Result buffer that is at
1599 least as large as the Result type.
1601 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1603 If the conversion results in an overflow or an underflow condition, then
1604 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1606 @param[in] Operand Operand to be converted to new type
1607 @param[out] Result Pointer to the result of conversion
1609 @retval RETURN_SUCCESS Successful conversion
1610 @retval RETURN_BUFFER_TOO_SMALL Overflow
1611 @retval RETURN_INVALID_PARAMETER Result is NULL
1620 RETURN_STATUS Status
;
1622 if (Result
== NULL
) {
1623 return RETURN_INVALID_PARAMETER
;
1626 if ((Operand
>= MIN_INT16
) && (Operand
<= MAX_INT16
)) {
1627 *Result
= (INT16
)Operand
;
1628 Status
= RETURN_SUCCESS
;
1630 *Result
= INT16_ERROR
;
1631 Status
= RETURN_BUFFER_TOO_SMALL
;
1638 INTN -> UINT16 conversion
1640 Converts the value specified by Operand to a value specified by Result type
1641 and stores the converted value into the caller allocated output buffer
1642 specified by Result. The caller must pass in a Result buffer that is at
1643 least as large as the Result type.
1645 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1647 If the conversion results in an overflow or an underflow condition, then
1648 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1650 @param[in] Operand Operand to be converted to new type
1651 @param[out] Result Pointer to the result of conversion
1653 @retval RETURN_SUCCESS Successful conversion
1654 @retval RETURN_BUFFER_TOO_SMALL Overflow
1655 @retval RETURN_INVALID_PARAMETER Result is NULL
1664 RETURN_STATUS Status
;
1666 if (Result
== NULL
) {
1667 return RETURN_INVALID_PARAMETER
;
1670 if ((Operand
>= 0) && (Operand
<= MAX_UINT16
)) {
1671 *Result
= (UINT16
)Operand
;
1672 Status
= RETURN_SUCCESS
;
1674 *Result
= UINT16_ERROR
;
1675 Status
= RETURN_BUFFER_TOO_SMALL
;
1682 INTN -> UINTN conversion
1684 Converts the value specified by Operand to a value specified by Result type
1685 and stores the converted value into the caller allocated output buffer
1686 specified by Result. The caller must pass in a Result buffer that is at
1687 least as large as the Result type.
1689 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1691 If the conversion results in an overflow or an underflow condition, then
1692 Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1694 @param[in] Operand Operand to be converted to new type
1695 @param[out] Result Pointer to the result of conversion
1697 @retval RETURN_SUCCESS Successful conversion
1698 @retval RETURN_BUFFER_TOO_SMALL Overflow
1699 @retval RETURN_INVALID_PARAMETER Result is NULL
1708 RETURN_STATUS Status
;
1710 if (Result
== NULL
) {
1711 return RETURN_INVALID_PARAMETER
;
1715 *Result
= (UINTN
)Operand
;
1716 Status
= RETURN_SUCCESS
;
1718 *Result
= UINTN_ERROR
;
1719 Status
= RETURN_BUFFER_TOO_SMALL
;
1726 INTN -> UINT64 conversion
1728 Converts the value specified by Operand to a value specified by Result type
1729 and stores the converted value into the caller allocated output buffer
1730 specified by Result. The caller must pass in a Result buffer that is at
1731 least as large as the Result type.
1733 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1735 If the conversion results in an overflow or an underflow condition, then
1736 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1738 @param[in] Operand Operand to be converted to new type
1739 @param[out] Result Pointer to the result of conversion
1741 @retval RETURN_SUCCESS Successful conversion
1742 @retval RETURN_BUFFER_TOO_SMALL Overflow
1743 @retval RETURN_INVALID_PARAMETER Result is NULL
1752 RETURN_STATUS Status
;
1754 if (Result
== NULL
) {
1755 return RETURN_INVALID_PARAMETER
;
1759 *Result
= (UINT64
)Operand
;
1760 Status
= RETURN_SUCCESS
;
1762 *Result
= UINT64_ERROR
;
1763 Status
= RETURN_BUFFER_TOO_SMALL
;
1770 UINTN -> INT8 conversion
1772 Converts the value specified by Operand to a value specified by Result type
1773 and stores the converted value into the caller allocated output buffer
1774 specified by Result. The caller must pass in a Result buffer that is at
1775 least as large as the Result type.
1777 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1779 If the conversion results in an overflow or an underflow condition, then
1780 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1782 @param[in] Operand Operand to be converted to new type
1783 @param[out] Result Pointer to the result of conversion
1785 @retval RETURN_SUCCESS Successful conversion
1786 @retval RETURN_BUFFER_TOO_SMALL Overflow
1787 @retval RETURN_INVALID_PARAMETER Result is NULL
1796 RETURN_STATUS Status
;
1798 if (Result
== NULL
) {
1799 return RETURN_INVALID_PARAMETER
;
1802 if (Operand
<= MAX_INT8
) {
1803 *Result
= (INT8
)Operand
;
1804 Status
= RETURN_SUCCESS
;
1806 *Result
= INT8_ERROR
;
1807 Status
= RETURN_BUFFER_TOO_SMALL
;
1814 UINTN -> CHAR8 conversion
1816 Converts the value specified by Operand to a value specified by Result type
1817 and stores the converted value into the caller allocated output buffer
1818 specified by Result. The caller must pass in a Result buffer that is at
1819 least as large as the Result type.
1821 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1823 If the conversion results in an overflow or an underflow condition, then
1824 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1826 @param[in] Operand Operand to be converted to new type
1827 @param[out] Result Pointer to the result of conversion
1829 @retval RETURN_SUCCESS Successful conversion
1830 @retval RETURN_BUFFER_TOO_SMALL Overflow
1831 @retval RETURN_INVALID_PARAMETER Result is NULL
1840 RETURN_STATUS Status
;
1842 if (Result
== NULL
) {
1843 return RETURN_INVALID_PARAMETER
;
1846 if (Operand
<= MAX_INT8
) {
1847 *Result
= (INT8
)Operand
;
1848 Status
= RETURN_SUCCESS
;
1850 *Result
= CHAR8_ERROR
;
1851 Status
= RETURN_BUFFER_TOO_SMALL
;
1858 UINTN -> UINT8 conversion
1860 Converts the value specified by Operand to a value specified by Result type
1861 and stores the converted value into the caller allocated output buffer
1862 specified by Result. The caller must pass in a Result buffer that is at
1863 least as large as the Result type.
1865 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1867 If the conversion results in an overflow or an underflow condition, then
1868 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1870 @param[in] Operand Operand to be converted to new type
1871 @param[out] Result Pointer to the result of conversion
1873 @retval RETURN_SUCCESS Successful conversion
1874 @retval RETURN_BUFFER_TOO_SMALL Overflow
1875 @retval RETURN_INVALID_PARAMETER Result is NULL
1884 RETURN_STATUS Status
;
1886 if (Result
== NULL
) {
1887 return RETURN_INVALID_PARAMETER
;
1890 if (Operand
<= MAX_UINT8
) {
1891 *Result
= (UINT8
)Operand
;
1892 Status
= RETURN_SUCCESS
;
1894 *Result
= UINT8_ERROR
;
1895 Status
= RETURN_BUFFER_TOO_SMALL
;
1902 UINTN -> INT16 conversion
1904 Converts the value specified by Operand to a value specified by Result type
1905 and stores the converted value into the caller allocated output buffer
1906 specified by Result. The caller must pass in a Result buffer that is at
1907 least as large as the Result type.
1909 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1911 If the conversion results in an overflow or an underflow condition, then
1912 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1914 @param[in] Operand Operand to be converted to new type
1915 @param[out] Result Pointer to the result of conversion
1917 @retval RETURN_SUCCESS Successful conversion
1918 @retval RETURN_BUFFER_TOO_SMALL Overflow
1919 @retval RETURN_INVALID_PARAMETER Result is NULL
1928 RETURN_STATUS Status
;
1930 if (Result
== NULL
) {
1931 return RETURN_INVALID_PARAMETER
;
1934 if (Operand
<= MAX_INT16
) {
1935 *Result
= (INT16
)Operand
;
1936 Status
= RETURN_SUCCESS
;
1938 *Result
= INT16_ERROR
;
1939 Status
= RETURN_BUFFER_TOO_SMALL
;
1946 UINTN -> UINT16 conversion
1948 Converts the value specified by Operand to a value specified by Result type
1949 and stores the converted value into the caller allocated output buffer
1950 specified by Result. The caller must pass in a Result buffer that is at
1951 least as large as the Result type.
1953 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1955 If the conversion results in an overflow or an underflow condition, then
1956 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1958 @param[in] Operand Operand to be converted to new type
1959 @param[out] Result Pointer to the result of conversion
1961 @retval RETURN_SUCCESS Successful conversion
1962 @retval RETURN_BUFFER_TOO_SMALL Overflow
1963 @retval RETURN_INVALID_PARAMETER Result is NULL
1972 RETURN_STATUS Status
;
1974 if (Result
== NULL
) {
1975 return RETURN_INVALID_PARAMETER
;
1978 if (Operand
<= MAX_UINT16
) {
1979 *Result
= (UINT16
)Operand
;
1980 Status
= RETURN_SUCCESS
;
1982 *Result
= UINT16_ERROR
;
1983 Status
= RETURN_BUFFER_TOO_SMALL
;
1990 UINTN -> INT32 conversion
1992 Converts the value specified by Operand to a value specified by Result type
1993 and stores the converted value into the caller allocated output buffer
1994 specified by Result. The caller must pass in a Result buffer that is at
1995 least as large as the Result type.
1997 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1999 If the conversion results in an overflow or an underflow condition, then
2000 Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2002 @param[in] Operand Operand to be converted to new type
2003 @param[out] Result Pointer to the result of conversion
2005 @retval RETURN_SUCCESS Successful conversion
2006 @retval RETURN_BUFFER_TOO_SMALL Overflow
2007 @retval RETURN_INVALID_PARAMETER Result is NULL
2016 RETURN_STATUS Status
;
2018 if (Result
== NULL
) {
2019 return RETURN_INVALID_PARAMETER
;
2022 if (Operand
<= MAX_INT32
) {
2023 *Result
= (INT32
)Operand
;
2024 Status
= RETURN_SUCCESS
;
2026 *Result
= INT32_ERROR
;
2027 Status
= RETURN_BUFFER_TOO_SMALL
;
2034 UINTN -> INTN conversion
2036 Converts the value specified by Operand to a value specified by Result type
2037 and stores the converted value into the caller allocated output buffer
2038 specified by Result. The caller must pass in a Result buffer that is at
2039 least as large as the Result type.
2041 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2043 If the conversion results in an overflow or an underflow condition, then
2044 Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2046 @param[in] Operand Operand to be converted to new type
2047 @param[out] Result Pointer to the result of conversion
2049 @retval RETURN_SUCCESS Successful conversion
2050 @retval RETURN_BUFFER_TOO_SMALL Overflow
2051 @retval RETURN_INVALID_PARAMETER Result is NULL
2060 RETURN_STATUS Status
;
2062 if (Result
== NULL
) {
2063 return RETURN_INVALID_PARAMETER
;
2066 if (Operand
<= MAX_INTN
) {
2067 *Result
= (INTN
)Operand
;
2068 Status
= RETURN_SUCCESS
;
2070 *Result
= INTN_ERROR
;
2071 Status
= RETURN_BUFFER_TOO_SMALL
;
2078 INT64 -> INT8 conversion
2080 Converts the value specified by Operand to a value specified by Result type
2081 and stores the converted value into the caller allocated output buffer
2082 specified by Result. The caller must pass in a Result buffer that is at
2083 least as large as the Result type.
2085 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2087 If the conversion results in an overflow or an underflow condition, then
2088 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2090 @param[in] Operand Operand to be converted to new type
2091 @param[out] Result Pointer to the result of conversion
2093 @retval RETURN_SUCCESS Successful conversion
2094 @retval RETURN_BUFFER_TOO_SMALL Overflow
2095 @retval RETURN_INVALID_PARAMETER Result is NULL
2104 RETURN_STATUS Status
;
2106 if (Result
== NULL
) {
2107 return RETURN_INVALID_PARAMETER
;
2110 if ((Operand
>= MIN_INT8
) && (Operand
<= MAX_INT8
)) {
2111 *Result
= (INT8
)Operand
;
2112 Status
= RETURN_SUCCESS
;
2114 *Result
= INT8_ERROR
;
2115 Status
= RETURN_BUFFER_TOO_SMALL
;
2122 INT64 -> CHAR8 conversion
2124 Converts the value specified by Operand to a value specified by Result type
2125 and stores the converted value into the caller allocated output buffer
2126 specified by Result. The caller must pass in a Result buffer that is at
2127 least as large as the Result type.
2129 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2131 If the conversion results in an overflow or an underflow condition, then
2132 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2134 @param[in] Operand Operand to be converted to new type
2135 @param[out] Result Pointer to the result of conversion
2137 @retval RETURN_SUCCESS Successful conversion
2138 @retval RETURN_BUFFER_TOO_SMALL Overflow
2139 @retval RETURN_INVALID_PARAMETER Result is NULL
2148 RETURN_STATUS Status
;
2150 if (Result
== NULL
) {
2151 return RETURN_INVALID_PARAMETER
;
2154 if ((Operand
>= 0) && (Operand
<= MAX_INT8
)) {
2155 *Result
= (CHAR8
)Operand
;
2156 Status
= RETURN_SUCCESS
;
2158 *Result
= CHAR8_ERROR
;
2159 Status
= RETURN_BUFFER_TOO_SMALL
;
2166 INT64 -> UINT8 conversion
2168 Converts the value specified by Operand to a value specified by Result type
2169 and stores the converted value into the caller allocated output buffer
2170 specified by Result. The caller must pass in a Result buffer that is at
2171 least as large as the Result type.
2173 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2175 If the conversion results in an overflow or an underflow condition, then
2176 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2178 @param[in] Operand Operand to be converted to new type
2179 @param[out] Result Pointer to the result of conversion
2181 @retval RETURN_SUCCESS Successful conversion
2182 @retval RETURN_BUFFER_TOO_SMALL Overflow
2183 @retval RETURN_INVALID_PARAMETER Result is NULL
2192 RETURN_STATUS Status
;
2194 if (Result
== NULL
) {
2195 return RETURN_INVALID_PARAMETER
;
2198 if ((Operand
>= 0) && (Operand
<= MAX_UINT8
)) {
2199 *Result
= (UINT8
)Operand
;
2200 Status
= RETURN_SUCCESS
;
2202 *Result
= UINT8_ERROR
;
2203 Status
= RETURN_BUFFER_TOO_SMALL
;
2210 INT64 -> INT16 conversion
2212 Converts the value specified by Operand to a value specified by Result type
2213 and stores the converted value into the caller allocated output buffer
2214 specified by Result. The caller must pass in a Result buffer that is at
2215 least as large as the Result type.
2217 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2219 If the conversion results in an overflow or an underflow condition, then
2220 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2222 @param[in] Operand Operand to be converted to new type
2223 @param[out] Result Pointer to the result of conversion
2225 @retval RETURN_SUCCESS Successful conversion
2226 @retval RETURN_BUFFER_TOO_SMALL Overflow
2227 @retval RETURN_INVALID_PARAMETER Result is NULL
2236 RETURN_STATUS Status
;
2238 if (Result
== NULL
) {
2239 return RETURN_INVALID_PARAMETER
;
2242 if ((Operand
>= MIN_INT16
) && (Operand
<= MAX_INT16
)) {
2243 *Result
= (INT16
)Operand
;
2244 Status
= RETURN_SUCCESS
;
2246 *Result
= INT16_ERROR
;
2247 Status
= RETURN_BUFFER_TOO_SMALL
;
2254 INT64 -> UINT16 conversion
2256 Converts the value specified by Operand to a value specified by Result type
2257 and stores the converted value into the caller allocated output buffer
2258 specified by Result. The caller must pass in a Result buffer that is at
2259 least as large as the Result type.
2261 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2263 If the conversion results in an overflow or an underflow condition, then
2264 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2266 @param[in] Operand Operand to be converted to new type
2267 @param[out] Result Pointer to the result of conversion
2269 @retval RETURN_SUCCESS Successful conversion
2270 @retval RETURN_BUFFER_TOO_SMALL Overflow
2271 @retval RETURN_INVALID_PARAMETER Result is NULL
2280 RETURN_STATUS Status
;
2282 if (Result
== NULL
) {
2283 return RETURN_INVALID_PARAMETER
;
2286 if ((Operand
>= 0) && (Operand
<= MAX_UINT16
)) {
2287 *Result
= (UINT16
)Operand
;
2288 Status
= RETURN_SUCCESS
;
2290 *Result
= UINT16_ERROR
;
2291 Status
= RETURN_BUFFER_TOO_SMALL
;
2298 INT64 -> INT32 conversion
2300 Converts the value specified by Operand to a value specified by Result type
2301 and stores the converted value into the caller allocated output buffer
2302 specified by Result. The caller must pass in a Result buffer that is at
2303 least as large as the Result type.
2305 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2307 If the conversion results in an overflow or an underflow condition, then
2308 Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2310 @param[in] Operand Operand to be converted to new type
2311 @param[out] Result Pointer to the result of conversion
2313 @retval RETURN_SUCCESS Successful conversion
2314 @retval RETURN_BUFFER_TOO_SMALL Overflow
2315 @retval RETURN_INVALID_PARAMETER Result is NULL
2324 RETURN_STATUS Status
;
2326 if (Result
== NULL
) {
2327 return RETURN_INVALID_PARAMETER
;
2330 if ((Operand
>= MIN_INT32
) && (Operand
<= MAX_INT32
)) {
2331 *Result
= (INT32
)Operand
;
2332 Status
= RETURN_SUCCESS
;
2334 *Result
= INT32_ERROR
;
2335 Status
= RETURN_BUFFER_TOO_SMALL
;
2342 INT64 -> UINT32 conversion
2344 Converts the value specified by Operand to a value specified by Result type
2345 and stores the converted value into the caller allocated output buffer
2346 specified by Result. The caller must pass in a Result buffer that is at
2347 least as large as the Result type.
2349 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2351 If the conversion results in an overflow or an underflow condition, then
2352 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2354 @param[in] Operand Operand to be converted to new type
2355 @param[out] Result Pointer to the result of conversion
2357 @retval RETURN_SUCCESS Successful conversion
2358 @retval RETURN_BUFFER_TOO_SMALL Overflow
2359 @retval RETURN_INVALID_PARAMETER Result is NULL
2368 RETURN_STATUS Status
;
2370 if (Result
== NULL
) {
2371 return RETURN_INVALID_PARAMETER
;
2374 if ((Operand
>= 0) && (Operand
<= MAX_UINT32
)) {
2375 *Result
= (UINT32
)Operand
;
2376 Status
= RETURN_SUCCESS
;
2378 *Result
= UINT32_ERROR
;
2379 Status
= RETURN_BUFFER_TOO_SMALL
;
2386 INT64 -> UINT64 conversion
2388 Converts the value specified by Operand to a value specified by Result type
2389 and stores the converted value into the caller allocated output buffer
2390 specified by Result. The caller must pass in a Result buffer that is at
2391 least as large as the Result type.
2393 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2395 If the conversion results in an overflow or an underflow condition, then
2396 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2398 @param[in] Operand Operand to be converted to new type
2399 @param[out] Result Pointer to the result of conversion
2401 @retval RETURN_SUCCESS Successful conversion
2402 @retval RETURN_BUFFER_TOO_SMALL Overflow
2403 @retval RETURN_INVALID_PARAMETER Result is NULL
2412 RETURN_STATUS Status
;
2414 if (Result
== NULL
) {
2415 return RETURN_INVALID_PARAMETER
;
2419 *Result
= (UINT64
)Operand
;
2420 Status
= RETURN_SUCCESS
;
2422 *Result
= UINT64_ERROR
;
2423 Status
= RETURN_BUFFER_TOO_SMALL
;
2430 UINT64 -> INT8 conversion
2432 Converts the value specified by Operand to a value specified by Result type
2433 and stores the converted value into the caller allocated output buffer
2434 specified by Result. The caller must pass in a Result buffer that is at
2435 least as large as the Result type.
2437 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2439 If the conversion results in an overflow or an underflow condition, then
2440 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2442 @param[in] Operand Operand to be converted to new type
2443 @param[out] Result Pointer to the result of conversion
2445 @retval RETURN_SUCCESS Successful conversion
2446 @retval RETURN_BUFFER_TOO_SMALL Overflow
2447 @retval RETURN_INVALID_PARAMETER Result is NULL
2456 RETURN_STATUS Status
;
2458 if (Result
== NULL
) {
2459 return RETURN_INVALID_PARAMETER
;
2462 if (Operand
<= MAX_INT8
) {
2463 *Result
= (INT8
)Operand
;
2464 Status
= RETURN_SUCCESS
;
2466 *Result
= INT8_ERROR
;
2467 Status
= RETURN_BUFFER_TOO_SMALL
;
2474 UINT64 -> CHAR8 conversion
2476 Converts the value specified by Operand to a value specified by Result type
2477 and stores the converted value into the caller allocated output buffer
2478 specified by Result. The caller must pass in a Result buffer that is at
2479 least as large as the Result type.
2481 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2483 If the conversion results in an overflow or an underflow condition, then
2484 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2486 @param[in] Operand Operand to be converted to new type
2487 @param[out] Result Pointer to the result of conversion
2489 @retval RETURN_SUCCESS Successful conversion
2490 @retval RETURN_BUFFER_TOO_SMALL Overflow
2491 @retval RETURN_INVALID_PARAMETER Result is NULL
2500 RETURN_STATUS Status
;
2502 if (Result
== NULL
) {
2503 return RETURN_INVALID_PARAMETER
;
2506 if (Operand
<= MAX_INT8
) {
2507 *Result
= (INT8
)Operand
;
2508 Status
= RETURN_SUCCESS
;
2510 *Result
= CHAR8_ERROR
;
2511 Status
= RETURN_BUFFER_TOO_SMALL
;
2518 UINT64 -> UINT8 conversion
2520 Converts the value specified by Operand to a value specified by Result type
2521 and stores the converted value into the caller allocated output buffer
2522 specified by Result. The caller must pass in a Result buffer that is at
2523 least as large as the Result type.
2525 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2527 If the conversion results in an overflow or an underflow condition, then
2528 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2530 @param[in] Operand Operand to be converted to new type
2531 @param[out] Result Pointer to the result of conversion
2533 @retval RETURN_SUCCESS Successful conversion
2534 @retval RETURN_BUFFER_TOO_SMALL Overflow
2535 @retval RETURN_INVALID_PARAMETER Result is NULL
2544 RETURN_STATUS Status
;
2546 if (Result
== NULL
) {
2547 return RETURN_INVALID_PARAMETER
;
2550 if (Operand
<= MAX_UINT8
) {
2551 *Result
= (UINT8
)Operand
;
2552 Status
= RETURN_SUCCESS
;
2554 *Result
= UINT8_ERROR
;
2555 Status
= RETURN_BUFFER_TOO_SMALL
;
2562 UINT64 -> INT16 conversion
2564 Converts the value specified by Operand to a value specified by Result type
2565 and stores the converted value into the caller allocated output buffer
2566 specified by Result. The caller must pass in a Result buffer that is at
2567 least as large as the Result type.
2569 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2571 If the conversion results in an overflow or an underflow condition, then
2572 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2574 @param[in] Operand Operand to be converted to new type
2575 @param[out] Result Pointer to the result of conversion
2577 @retval RETURN_SUCCESS Successful conversion
2578 @retval RETURN_BUFFER_TOO_SMALL Overflow
2579 @retval RETURN_INVALID_PARAMETER Result is NULL
2588 RETURN_STATUS Status
;
2590 if (Result
== NULL
) {
2591 return RETURN_INVALID_PARAMETER
;
2594 if (Operand
<= MAX_INT16
) {
2595 *Result
= (INT16
)Operand
;
2596 Status
= RETURN_SUCCESS
;
2598 *Result
= INT16_ERROR
;
2599 Status
= RETURN_BUFFER_TOO_SMALL
;
2606 UINT64 -> UINT16 conversion
2608 Converts the value specified by Operand to a value specified by Result type
2609 and stores the converted value into the caller allocated output buffer
2610 specified by Result. The caller must pass in a Result buffer that is at
2611 least as large as the Result type.
2613 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2615 If the conversion results in an overflow or an underflow condition, then
2616 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2618 @param[in] Operand Operand to be converted to new type
2619 @param[out] Result Pointer to the result of conversion
2621 @retval RETURN_SUCCESS Successful conversion
2622 @retval RETURN_BUFFER_TOO_SMALL Overflow
2623 @retval RETURN_INVALID_PARAMETER Result is NULL
2627 SafeUint64ToUint16 (
2632 RETURN_STATUS Status
;
2634 if (Result
== NULL
) {
2635 return RETURN_INVALID_PARAMETER
;
2638 if (Operand
<= MAX_UINT16
) {
2639 *Result
= (UINT16
)Operand
;
2640 Status
= RETURN_SUCCESS
;
2642 *Result
= UINT16_ERROR
;
2643 Status
= RETURN_BUFFER_TOO_SMALL
;
2650 UINT64 -> INT32 conversion
2652 Converts the value specified by Operand to a value specified by Result type
2653 and stores the converted value into the caller allocated output buffer
2654 specified by Result. The caller must pass in a Result buffer that is at
2655 least as large as the Result type.
2657 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2659 If the conversion results in an overflow or an underflow condition, then
2660 Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2662 @param[in] Operand Operand to be converted to new type
2663 @param[out] Result Pointer to the result of conversion
2665 @retval RETURN_SUCCESS Successful conversion
2666 @retval RETURN_BUFFER_TOO_SMALL Overflow
2667 @retval RETURN_INVALID_PARAMETER Result is NULL
2676 RETURN_STATUS Status
;
2678 if (Result
== NULL
) {
2679 return RETURN_INVALID_PARAMETER
;
2682 if (Operand
<= MAX_INT32
) {
2683 *Result
= (INT32
)Operand
;
2684 Status
= RETURN_SUCCESS
;
2686 *Result
= INT32_ERROR
;
2687 Status
= RETURN_BUFFER_TOO_SMALL
;
2694 UINT64 -> UINT32 conversion
2696 Converts the value specified by Operand to a value specified by Result type
2697 and stores the converted value into the caller allocated output buffer
2698 specified by Result. The caller must pass in a Result buffer that is at
2699 least as large as the Result type.
2701 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2703 If the conversion results in an overflow or an underflow condition, then
2704 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2706 @param[in] Operand Operand to be converted to new type
2707 @param[out] Result Pointer to the result of conversion
2709 @retval RETURN_SUCCESS Successful conversion
2710 @retval RETURN_BUFFER_TOO_SMALL Overflow
2711 @retval RETURN_INVALID_PARAMETER Result is NULL
2715 SafeUint64ToUint32 (
2720 RETURN_STATUS Status
;
2722 if (Result
== NULL
) {
2723 return RETURN_INVALID_PARAMETER
;
2726 if (Operand
<= MAX_UINT32
) {
2727 *Result
= (UINT32
)Operand
;
2728 Status
= RETURN_SUCCESS
;
2730 *Result
= UINT32_ERROR
;
2731 Status
= RETURN_BUFFER_TOO_SMALL
;
2738 UINT64 -> INTN conversion
2740 Converts the value specified by Operand to a value specified by Result type
2741 and stores the converted value into the caller allocated output buffer
2742 specified by Result. The caller must pass in a Result buffer that is at
2743 least as large as the Result type.
2745 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2747 If the conversion results in an overflow or an underflow condition, then
2748 Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2750 @param[in] Operand Operand to be converted to new type
2751 @param[out] Result Pointer to the result of conversion
2753 @retval RETURN_SUCCESS Successful conversion
2754 @retval RETURN_BUFFER_TOO_SMALL Overflow
2755 @retval RETURN_INVALID_PARAMETER Result is NULL
2764 RETURN_STATUS Status
;
2766 if (Result
== NULL
) {
2767 return RETURN_INVALID_PARAMETER
;
2770 if (Operand
<= MAX_INTN
) {
2771 *Result
= (INTN
)Operand
;
2772 Status
= RETURN_SUCCESS
;
2774 *Result
= INTN_ERROR
;
2775 Status
= RETURN_BUFFER_TOO_SMALL
;
2782 UINT64 -> INT64 conversion
2784 Converts the value specified by Operand to a value specified by Result type
2785 and stores the converted value into the caller allocated output buffer
2786 specified by Result. The caller must pass in a Result buffer that is at
2787 least as large as the Result type.
2789 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2791 If the conversion results in an overflow or an underflow condition, then
2792 Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2794 @param[in] Operand Operand to be converted to new type
2795 @param[out] Result Pointer to the result of conversion
2797 @retval RETURN_SUCCESS Successful conversion
2798 @retval RETURN_BUFFER_TOO_SMALL Overflow
2799 @retval RETURN_INVALID_PARAMETER Result is NULL
2808 RETURN_STATUS Status
;
2810 if (Result
== NULL
) {
2811 return RETURN_INVALID_PARAMETER
;
2814 if (Operand
<= MAX_INT64
) {
2815 *Result
= (INT64
)Operand
;
2816 Status
= RETURN_SUCCESS
;
2818 *Result
= INT64_ERROR
;
2819 Status
= RETURN_BUFFER_TOO_SMALL
;
2826 // Addition functions
2832 Performs the requested operation using the input parameters into a value
2833 specified by Result type and stores the converted value into the caller
2834 allocated output buffer specified by Result. The caller must pass in a
2835 Result buffer that is at least as large as the Result type.
2837 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2839 If the requested operation results in an overflow or an underflow condition,
2840 then Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2842 @param[in] Augend A number to which addend will be added
2843 @param[in] Addend A number to be added to another
2844 @param[out] Result Pointer to the result of addition
2846 @retval RETURN_SUCCESS Successful addition
2847 @retval RETURN_BUFFER_TOO_SMALL Overflow
2848 @retval RETURN_INVALID_PARAMETER Result is NULL
2858 RETURN_STATUS Status
;
2860 if (Result
== NULL
) {
2861 return RETURN_INVALID_PARAMETER
;
2864 if (((UINT8
)(Augend
+ Addend
)) >= Augend
) {
2865 *Result
= (UINT8
)(Augend
+ Addend
);
2866 Status
= RETURN_SUCCESS
;
2868 *Result
= UINT8_ERROR
;
2869 Status
= RETURN_BUFFER_TOO_SMALL
;
2878 Performs the requested operation using the input parameters into a value
2879 specified by Result type and stores the converted value into the caller
2880 allocated output buffer specified by Result. The caller must pass in a
2881 Result buffer that is at least as large as the Result type.
2883 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2885 If the requested operation results in an overflow or an underflow condition,
2886 then Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2888 @param[in] Augend A number to which addend will be added
2889 @param[in] Addend A number to be added to another
2890 @param[out] Result Pointer to the result of addition
2892 @retval RETURN_SUCCESS Successful addition
2893 @retval RETURN_BUFFER_TOO_SMALL Overflow
2894 @retval RETURN_INVALID_PARAMETER Result is NULL
2904 RETURN_STATUS Status
;
2906 if (Result
== NULL
) {
2907 return RETURN_INVALID_PARAMETER
;
2910 if (((UINT16
)(Augend
+ Addend
)) >= Augend
) {
2911 *Result
= (UINT16
)(Augend
+ Addend
);
2912 Status
= RETURN_SUCCESS
;
2914 *Result
= UINT16_ERROR
;
2915 Status
= RETURN_BUFFER_TOO_SMALL
;
2924 Performs the requested operation using the input parameters into a value
2925 specified by Result type and stores the converted value into the caller
2926 allocated output buffer specified by Result. The caller must pass in a
2927 Result buffer that is at least as large as the Result type.
2929 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2931 If the requested operation results in an overflow or an underflow condition,
2932 then Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2934 @param[in] Augend A number to which addend will be added
2935 @param[in] Addend A number to be added to another
2936 @param[out] Result Pointer to the result of addition
2938 @retval RETURN_SUCCESS Successful addition
2939 @retval RETURN_BUFFER_TOO_SMALL Overflow
2940 @retval RETURN_INVALID_PARAMETER Result is NULL
2950 RETURN_STATUS Status
;
2952 if (Result
== NULL
) {
2953 return RETURN_INVALID_PARAMETER
;
2956 if ((Augend
+ Addend
) >= Augend
) {
2957 *Result
= (Augend
+ Addend
);
2958 Status
= RETURN_SUCCESS
;
2960 *Result
= UINT32_ERROR
;
2961 Status
= RETURN_BUFFER_TOO_SMALL
;
2970 Performs the requested operation using the input parameters into a value
2971 specified by Result type and stores the converted value into the caller
2972 allocated output buffer specified by Result. The caller must pass in a
2973 Result buffer that is at least as large as the Result type.
2975 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2977 If the requested operation results in an overflow or an underflow condition,
2978 then Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2980 @param[in] Augend A number to which addend will be added
2981 @param[in] Addend A number to be added to another
2982 @param[out] Result Pointer to the result of addition
2984 @retval RETURN_SUCCESS Successful addition
2985 @retval RETURN_BUFFER_TOO_SMALL Overflow
2986 @retval RETURN_INVALID_PARAMETER Result is NULL
2996 RETURN_STATUS Status
;
2998 if (Result
== NULL
) {
2999 return RETURN_INVALID_PARAMETER
;
3002 if ((Augend
+ Addend
) >= Augend
) {
3003 *Result
= (Augend
+ Addend
);
3004 Status
= RETURN_SUCCESS
;
3006 *Result
= UINT64_ERROR
;
3007 Status
= RETURN_BUFFER_TOO_SMALL
;
3014 // Subtraction functions
3020 Performs the requested operation using the input parameters into a value
3021 specified by Result type and stores the converted value into the caller
3022 allocated output buffer specified by Result. The caller must pass in a
3023 Result buffer that is at least as large as the Result type.
3025 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3027 If the requested operation results in an overflow or an underflow condition,
3028 then Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3030 @param[in] Minuend A number from which another is to be subtracted.
3031 @param[in] Subtrahend A number to be subtracted from another
3032 @param[out] Result Pointer to the result of subtraction
3034 @retval RETURN_SUCCESS Successful subtraction
3035 @retval RETURN_BUFFER_TOO_SMALL Underflow
3036 @retval RETURN_INVALID_PARAMETER Result is NULL
3042 IN UINT8 Subtrahend
,
3046 RETURN_STATUS Status
;
3048 if (Result
== NULL
) {
3049 return RETURN_INVALID_PARAMETER
;
3052 if (Minuend
>= Subtrahend
) {
3053 *Result
= (UINT8
)(Minuend
- Subtrahend
);
3054 Status
= RETURN_SUCCESS
;
3056 *Result
= UINT8_ERROR
;
3057 Status
= RETURN_BUFFER_TOO_SMALL
;
3066 Performs the requested operation using the input parameters into a value
3067 specified by Result type and stores the converted value into the caller
3068 allocated output buffer specified by Result. The caller must pass in a
3069 Result buffer that is at least as large as the Result type.
3071 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3073 If the requested operation results in an overflow or an underflow condition,
3074 then Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3076 @param[in] Minuend A number from which another is to be subtracted.
3077 @param[in] Subtrahend A number to be subtracted from another
3078 @param[out] Result Pointer to the result of subtraction
3080 @retval RETURN_SUCCESS Successful subtraction
3081 @retval RETURN_BUFFER_TOO_SMALL Underflow
3082 @retval RETURN_INVALID_PARAMETER Result is NULL
3088 IN UINT16 Subtrahend
,
3092 RETURN_STATUS Status
;
3094 if (Result
== NULL
) {
3095 return RETURN_INVALID_PARAMETER
;
3098 if (Minuend
>= Subtrahend
) {
3099 *Result
= (UINT16
)(Minuend
- Subtrahend
);
3100 Status
= RETURN_SUCCESS
;
3102 *Result
= UINT16_ERROR
;
3103 Status
= RETURN_BUFFER_TOO_SMALL
;
3112 Performs the requested operation using the input parameters into a value
3113 specified by Result type and stores the converted value into the caller
3114 allocated output buffer specified by Result. The caller must pass in a
3115 Result buffer that is at least as large as the Result type.
3117 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3119 If the requested operation results in an overflow or an underflow condition,
3120 then Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3122 @param[in] Minuend A number from which another is to be subtracted.
3123 @param[in] Subtrahend A number to be subtracted from another
3124 @param[out] Result Pointer to the result of subtraction
3126 @retval RETURN_SUCCESS Successful subtraction
3127 @retval RETURN_BUFFER_TOO_SMALL Underflow
3128 @retval RETURN_INVALID_PARAMETER Result is NULL
3134 IN UINT32 Subtrahend
,
3138 RETURN_STATUS Status
;
3140 if (Result
== NULL
) {
3141 return RETURN_INVALID_PARAMETER
;
3144 if (Minuend
>= Subtrahend
) {
3145 *Result
= (Minuend
- Subtrahend
);
3146 Status
= RETURN_SUCCESS
;
3148 *Result
= UINT32_ERROR
;
3149 Status
= RETURN_BUFFER_TOO_SMALL
;
3158 Performs the requested operation using the input parameters into a value
3159 specified by Result type and stores the converted value into the caller
3160 allocated output buffer specified by Result. The caller must pass in a
3161 Result buffer that is at least as large as the Result type.
3163 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3165 If the requested operation results in an overflow or an underflow condition,
3166 then Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3168 @param[in] Minuend A number from which another is to be subtracted.
3169 @param[in] Subtrahend A number to be subtracted from another
3170 @param[out] Result Pointer to the result of subtraction
3172 @retval RETURN_SUCCESS Successful subtraction
3173 @retval RETURN_BUFFER_TOO_SMALL Underflow
3174 @retval RETURN_INVALID_PARAMETER Result is NULL
3180 IN UINT64 Subtrahend
,
3184 RETURN_STATUS Status
;
3186 if (Result
== NULL
) {
3187 return RETURN_INVALID_PARAMETER
;
3190 if (Minuend
>= Subtrahend
) {
3191 *Result
= (Minuend
- Subtrahend
);
3192 Status
= RETURN_SUCCESS
;
3194 *Result
= UINT64_ERROR
;
3195 Status
= RETURN_BUFFER_TOO_SMALL
;
3202 // Multiplication functions
3206 UINT8 multiplication
3208 Performs the requested operation using the input parameters into a value
3209 specified by Result type and stores the converted value into the caller
3210 allocated output buffer specified by Result. The caller must pass in a
3211 Result buffer that is at least as large as the Result type.
3213 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3215 If the requested operation results in an overflow or an underflow condition,
3216 then Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3218 @param[in] Multiplicand A number that is to be multiplied by another
3219 @param[in] Multiplier A number by which the multiplicand is to be multiplied
3220 @param[out] Result Pointer to the result of multiplication
3222 @retval RETURN_SUCCESS Successful multiplication
3223 @retval RETURN_BUFFER_TOO_SMALL Overflow
3224 @retval RETURN_INVALID_PARAMETER Result is NULL
3229 IN UINT8 Multiplicand
,
3230 IN UINT8 Multiplier
,
3234 UINT32 IntermediateResult
;
3236 IntermediateResult
= ((UINT32
)Multiplicand
) *((UINT32
)Multiplier
);
3238 return SafeUint32ToUint8 (IntermediateResult
, Result
);
3242 UINT16 multiplication
3244 Performs the requested operation using the input parameters into a value
3245 specified by Result type and stores the converted value into the caller
3246 allocated output buffer specified by Result. The caller must pass in a
3247 Result buffer that is at least as large as the Result type.
3249 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3251 If the requested operation results in an overflow or an underflow condition,
3252 then Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3254 @param[in] Multiplicand A number that is to be multiplied by another
3255 @param[in] Multiplier A number by which the multiplicand is to be multiplied
3256 @param[out] Result Pointer to the result of multiplication
3258 @retval RETURN_SUCCESS Successful multiplication
3259 @retval RETURN_BUFFER_TOO_SMALL Overflow
3260 @retval RETURN_INVALID_PARAMETER Result is NULL
3265 IN UINT16 Multiplicand
,
3266 IN UINT16 Multiplier
,
3270 UINT32 IntermediateResult
;
3272 IntermediateResult
= ((UINT32
)Multiplicand
) *((UINT32
)Multiplier
);
3274 return SafeUint32ToUint16 (IntermediateResult
, Result
);
3278 UINT32 multiplication
3280 Performs the requested operation using the input parameters into a value
3281 specified by Result type and stores the converted value into the caller
3282 allocated output buffer specified by Result. The caller must pass in a
3283 Result buffer that is at least as large as the Result type.
3285 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3287 If the requested operation results in an overflow or an underflow condition,
3288 then Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3290 @param[in] Multiplicand A number that is to be multiplied by another
3291 @param[in] Multiplier A number by which the multiplicand is to be multiplied
3292 @param[out] Result Pointer to the result of multiplication
3294 @retval RETURN_SUCCESS Successful multiplication
3295 @retval RETURN_BUFFER_TOO_SMALL Overflow
3296 @retval RETURN_INVALID_PARAMETER Result is NULL
3301 IN UINT32 Multiplicand
,
3302 IN UINT32 Multiplier
,
3306 UINT64 IntermediateResult
;
3308 IntermediateResult
= ((UINT64
) Multiplicand
) *((UINT64
) Multiplier
);
3310 return SafeUint64ToUint32 (IntermediateResult
, Result
);
3314 UINT64 multiplication
3316 Performs the requested operation using the input parameters into a value
3317 specified by Result type and stores the converted value into the caller
3318 allocated output buffer specified by Result. The caller must pass in a
3319 Result buffer that is at least as large as the Result type.
3321 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3323 If the requested operation results in an overflow or an underflow condition,
3324 then Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3326 @param[in] Multiplicand A number that is to be multiplied by another
3327 @param[in] Multiplier A number by which the multiplicand is to be multiplied
3328 @param[out] Result Pointer to the result of multiplication
3330 @retval RETURN_SUCCESS Successful multiplication
3331 @retval RETURN_BUFFER_TOO_SMALL Overflow
3332 @retval RETURN_INVALID_PARAMETER Result is NULL
3337 IN UINT64 Multiplicand
,
3338 IN UINT64 Multiplier
,
3342 RETURN_STATUS Status
;
3350 UINT64 UnsignedResult
;
3352 if (Result
== NULL
) {
3353 return RETURN_INVALID_PARAMETER
;
3360 Status
= RETURN_BUFFER_TOO_SMALL
;
3363 // 64x64 into 128 is like 32.32 x 32.32.
3365 // a.b * c.d = a*(c.d) + .b*(c.d) = a*c + a*.d + .b*c + .b*.d
3366 // back in non-decimal notation where A=a*2^32 and C=c*2^32:
3367 // A*C + A*d + b*C + b*d
3368 // So there are four components to add together.
3369 // result = (a*c*2^64) + (a*d*2^32) + (b*c*2^32) + (b*d)
3371 // a * c must be 0 or there would be bits in the high 64-bits
3372 // a * d must be less than 2^32 or there would be bits in the high 64-bits
3373 // b * c must be less than 2^32 or there would be bits in the high 64-bits
3374 // then there must be no overflow of the resulting values summed up.
3376 DwordA
= (UINT32
)(Multiplicand
>> 32);
3377 DwordC
= (UINT32
)(Multiplier
>> 32);
3380 // common case -- if high dwords are both zero, no chance for overflow
3382 if ((DwordA
== 0) && (DwordC
== 0)) {
3383 DwordB
= (UINT32
)Multiplicand
;
3384 DwordD
= (UINT32
)Multiplier
;
3386 *Result
= (((UINT64
)DwordB
) *(UINT64
)DwordD
);
3387 Status
= RETURN_SUCCESS
;
3390 // a * c must be 0 or there would be bits set in the high 64-bits
3392 if ((DwordA
== 0) ||
3394 DwordD
= (UINT32
)Multiplier
;
3397 // a * d must be less than 2^32 or there would be bits set in the high 64-bits
3399 ProductAD
= (((UINT64
)DwordA
) *(UINT64
)DwordD
);
3400 if ((ProductAD
& 0xffffffff00000000) == 0) {
3401 DwordB
= (UINT32
)Multiplicand
;
3404 // b * c must be less than 2^32 or there would be bits set in the high 64-bits
3406 ProductBC
= (((UINT64
)DwordB
) *(UINT64
)DwordC
);
3407 if ((ProductBC
& 0xffffffff00000000) == 0) {
3409 // now sum them all up checking for overflow.
3410 // shifting is safe because we already checked for overflow above
3412 if (!RETURN_ERROR (SafeUint64Add (ProductBC
<< 32, ProductAD
<< 32, &UnsignedResult
))) {
3416 ProductBD
= (((UINT64
)DwordB
) *(UINT64
)DwordD
);
3418 if (!RETURN_ERROR (SafeUint64Add (UnsignedResult
, ProductBD
, &UnsignedResult
))) {
3419 *Result
= UnsignedResult
;
3420 Status
= RETURN_SUCCESS
;
3428 if (RETURN_ERROR (Status
)) {
3429 *Result
= UINT64_ERROR
;
3435 // Signed operations
3437 // Strongly consider using unsigned numbers.
3439 // Signed numbers are often used where unsigned numbers should be used.
3440 // For example file sizes and array indices should always be unsigned.
3441 // Subtracting a larger positive signed number from a smaller positive
3442 // signed number with SafeInt32Sub will succeed, producing a negative number,
3443 // that then must not be used as an array index (but can occasionally be
3444 // used as a pointer index.) Similarly for adding a larger magnitude
3445 // negative number to a smaller magnitude positive number.
3447 // This library does not protect you from such errors. It tells you if your
3448 // integer operations overflowed, not if you are doing the right thing
3449 // with your non-overflowed integers.
3451 // Likewise you can overflow a buffer with a non-overflowed unsigned index.
3455 // Signed addition functions
3461 Performs the requested operation using the input parameters into a value
3462 specified by Result type and stores the converted value into the caller
3463 allocated output buffer specified by Result. The caller must pass in a
3464 Result buffer that is at least as large as the Result type.
3466 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3468 If the requested operation results in an overflow or an underflow condition,
3469 then Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3471 @param[in] Augend A number to which addend will be added
3472 @param[in] Addend A number to be added to another
3473 @param[out] Result Pointer to the result of addition
3475 @retval RETURN_SUCCESS Successful addition
3476 @retval RETURN_BUFFER_TOO_SMALL Overflow
3477 @retval RETURN_INVALID_PARAMETER Result is NULL
3487 return SafeInt32ToInt8 (((INT32
)Augend
) + ((INT32
)Addend
), Result
);
3493 Performs the requested operation using the input parameters into a value
3494 specified by Result type and stores the converted value into the caller
3495 allocated output buffer specified by Result. The caller must pass in a
3496 Result buffer that is at least as large as the Result type.
3498 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3500 If the requested operation results in an overflow or an underflow condition,
3501 then Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3503 @param[in] Augend A number to which addend will be added
3504 @param[in] Addend A number to be added to another
3505 @param[out] Result Pointer to the result of addition
3507 @retval RETURN_SUCCESS Successful addition
3508 @retval RETURN_BUFFER_TOO_SMALL Overflow
3509 @retval RETURN_INVALID_PARAMETER Result is NULL
3522 if (Result
== NULL
) {
3523 return RETURN_INVALID_PARAMETER
;
3526 Augend32
= (INT32
)Augend
;
3527 Addend32
= (INT32
)Addend
;
3528 if (Augend32
< 0 || Augend32
> MAX_INT8
) {
3529 *Result
= CHAR8_ERROR
;
3530 return RETURN_BUFFER_TOO_SMALL
;
3532 if (Addend32
< 0 || Addend32
> MAX_INT8
) {
3533 *Result
= CHAR8_ERROR
;
3534 return RETURN_BUFFER_TOO_SMALL
;
3537 return SafeInt32ToChar8 (Augend32
+ Addend32
, Result
);
3543 Performs the requested operation using the input parameters into a value
3544 specified by Result type and stores the converted value into the caller
3545 allocated output buffer specified by Result. The caller must pass in a
3546 Result buffer that is at least as large as the Result type.
3548 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3550 If the requested operation results in an overflow or an underflow condition,
3551 then Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3553 @param[in] Augend A number to which addend will be added
3554 @param[in] Addend A number to be added to another
3555 @param[out] Result Pointer to the result of addition
3557 @retval RETURN_SUCCESS Successful addition
3558 @retval RETURN_BUFFER_TOO_SMALL Overflow
3559 @retval RETURN_INVALID_PARAMETER Result is NULL
3569 return SafeInt32ToInt16 (((INT32
)Augend
) + ((INT32
)Addend
), Result
);
3575 Performs the requested operation using the input parameters into a value
3576 specified by Result type and stores the converted value into the caller
3577 allocated output buffer specified by Result. The caller must pass in a
3578 Result buffer that is at least as large as the Result type.
3580 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3582 If the requested operation results in an overflow or an underflow condition,
3583 then Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3585 @param[in] Augend A number to which addend will be added
3586 @param[in] Addend A number to be added to another
3587 @param[out] Result Pointer to the result of addition
3589 @retval RETURN_SUCCESS Successful addition
3590 @retval RETURN_BUFFER_TOO_SMALL Overflow
3591 @retval RETURN_INVALID_PARAMETER Result is NULL
3601 return SafeInt64ToInt32 (((INT64
)Augend
) + ((INT64
)Addend
), Result
);
3607 Performs the requested operation using the input parameters into a value
3608 specified by Result type and stores the converted value into the caller
3609 allocated output buffer specified by Result. The caller must pass in a
3610 Result buffer that is at least as large as the Result type.
3612 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3614 If the requested operation results in an overflow or an underflow condition,
3615 then Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3617 @param[in] Augend A number to which addend will be added
3618 @param[in] Addend A number to be added to another
3619 @param[out] Result Pointer to the result of addition
3621 @retval RETURN_SUCCESS Successful addition
3622 @retval RETURN_BUFFER_TOO_SMALL Overflow
3623 @retval RETURN_INVALID_PARAMETER Result is NULL
3633 RETURN_STATUS Status
;
3635 if (Result
== NULL
) {
3636 return RETURN_INVALID_PARAMETER
;
3640 // * An Addend of zero can never cause underflow or overflow.
3642 // * A positive Addend can only cause overflow. The overflow condition is
3644 // (Augend + Addend) > MAX_INT64
3646 // Subtracting Addend from both sides yields
3648 // Augend > (MAX_INT64 - Addend)
3650 // This condition can be coded directly in C because the RHS will neither
3651 // underflow nor overflow. That is due to the starting condition:
3653 // 0 < Addend <= MAX_INT64
3655 // Multiplying all three sides by (-1) yields
3657 // 0 > (-Addend) >= (-MAX_INT64)
3659 // Adding MAX_INT64 to all three sides yields
3661 // MAX_INT64 > (MAX_INT64 - Addend) >= 0
3663 // * A negative Addend can only cause underflow. The underflow condition is
3665 // (Augend + Addend) < MIN_INT64
3667 // Subtracting Addend from both sides yields
3669 // Augend < (MIN_INT64 - Addend)
3671 // This condition can be coded directly in C because the RHS will neither
3672 // underflow nor overflow. That is due to the starting condition:
3674 // MIN_INT64 <= Addend < 0
3676 // Multiplying all three sides by (-1) yields
3678 // (-MIN_INT64) >= (-Addend) > 0
3680 // Adding MIN_INT64 to all three sides yields
3682 // 0 >= (MIN_INT64 - Addend) > MIN_INT64
3684 if (((Addend
> 0) && (Augend
> (MAX_INT64
- Addend
))) ||
3685 ((Addend
< 0) && (Augend
< (MIN_INT64
- Addend
)))) {
3686 *Result
= INT64_ERROR
;
3687 Status
= RETURN_BUFFER_TOO_SMALL
;
3689 *Result
= Augend
+ Addend
;
3690 Status
= RETURN_SUCCESS
;
3697 // Signed subtraction functions
3703 Performs the requested operation using the input parameters into a value
3704 specified by Result type and stores the converted value into the caller
3705 allocated output buffer specified by Result. The caller must pass in a
3706 Result buffer that is at least as large as the Result type.
3708 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3710 If the requested operation results in an overflow or an underflow condition,
3711 then Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3713 @param[in] Minuend A number from which another is to be subtracted.
3714 @param[in] Subtrahend A number to be subtracted from another
3715 @param[out] Result Pointer to the result of subtraction
3717 @retval RETURN_SUCCESS Successful subtraction
3718 @retval RETURN_BUFFER_TOO_SMALL Underflow
3719 @retval RETURN_INVALID_PARAMETER Result is NULL
3729 return SafeInt32ToInt8 (((INT32
)Minuend
) - ((INT32
)Subtrahend
), Result
);
3735 Performs the requested operation using the input parameters into a value
3736 specified by Result type and stores the converted value into the caller
3737 allocated output buffer specified by Result. The caller must pass in a
3738 Result buffer that is at least as large as the Result type.
3740 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3742 If the requested operation results in an overflow or an underflow condition,
3743 then Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3745 @param[in] Minuend A number from which another is to be subtracted.
3746 @param[in] Subtrahend A number to be subtracted from another
3747 @param[out] Result Pointer to the result of subtraction
3749 @retval RETURN_SUCCESS Successful subtraction
3750 @retval RETURN_BUFFER_TOO_SMALL Underflow
3751 @retval RETURN_INVALID_PARAMETER Result is NULL
3757 IN CHAR8 Subtrahend
,
3764 if (Result
== NULL
) {
3765 return RETURN_INVALID_PARAMETER
;
3768 Minuend32
= (INT32
)Minuend
;
3769 Subtrahend32
= (INT32
)Subtrahend
;
3770 if (Minuend32
< 0 || Minuend32
> MAX_INT8
) {
3771 *Result
= CHAR8_ERROR
;
3772 return RETURN_BUFFER_TOO_SMALL
;
3774 if (Subtrahend32
< 0 || Subtrahend32
> MAX_INT8
) {
3775 *Result
= CHAR8_ERROR
;
3776 return RETURN_BUFFER_TOO_SMALL
;
3779 return SafeInt32ToChar8 (Minuend32
- Subtrahend32
, Result
);
3785 Performs the requested operation using the input parameters into a value
3786 specified by Result type and stores the converted value into the caller
3787 allocated output buffer specified by Result. The caller must pass in a
3788 Result buffer that is at least as large as the Result type.
3790 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3792 If the requested operation results in an overflow or an underflow condition,
3793 then Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3795 @param[in] Minuend A number from which another is to be subtracted.
3796 @param[in] Subtrahend A number to be subtracted from another
3797 @param[out] Result Pointer to the result of subtraction
3799 @retval RETURN_SUCCESS Successful subtraction
3800 @retval RETURN_BUFFER_TOO_SMALL Underflow
3801 @retval RETURN_INVALID_PARAMETER Result is NULL
3807 IN INT16 Subtrahend
,
3811 return SafeInt32ToInt16 (((INT32
)Minuend
) - ((INT32
)Subtrahend
), Result
);
3817 Performs the requested operation using the input parameters into a value
3818 specified by Result type and stores the converted value into the caller
3819 allocated output buffer specified by Result. The caller must pass in a
3820 Result buffer that is at least as large as the Result type.
3822 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3824 If the requested operation results in an overflow or an underflow condition,
3825 then Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3827 @param[in] Minuend A number from which another is to be subtracted.
3828 @param[in] Subtrahend A number to be subtracted from another
3829 @param[out] Result Pointer to the result of subtraction
3831 @retval RETURN_SUCCESS Successful subtraction
3832 @retval RETURN_BUFFER_TOO_SMALL Underflow
3833 @retval RETURN_INVALID_PARAMETER Result is NULL
3839 IN INT32 Subtrahend
,
3843 return SafeInt64ToInt32 (((INT64
)Minuend
) - ((INT64
)Subtrahend
), Result
);
3849 Performs the requested operation using the input parameters into a value
3850 specified by Result type and stores the converted value into the caller
3851 allocated output buffer specified by Result. The caller must pass in a
3852 Result buffer that is at least as large as the Result type.
3854 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3856 If the requested operation results in an overflow or an underflow condition,
3857 then Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3859 @param[in] Minuend A number from which another is to be subtracted.
3860 @param[in] Subtrahend A number to be subtracted from another
3861 @param[out] Result Pointer to the result of subtraction
3863 @retval RETURN_SUCCESS Successful subtraction
3864 @retval RETURN_BUFFER_TOO_SMALL Underflow
3865 @retval RETURN_INVALID_PARAMETER Result is NULL
3871 IN INT64 Subtrahend
,
3875 RETURN_STATUS Status
;
3877 if (Result
== NULL
) {
3878 return RETURN_INVALID_PARAMETER
;
3882 // * A Subtrahend of zero can never cause underflow or overflow.
3884 // * A positive Subtrahend can only cause underflow. The underflow condition
3887 // (Minuend - Subtrahend) < MIN_INT64
3889 // Adding Subtrahend to both sides yields
3891 // Minuend < (MIN_INT64 + Subtrahend)
3893 // This condition can be coded directly in C because the RHS will neither
3894 // underflow nor overflow. That is due to the starting condition:
3896 // 0 < Subtrahend <= MAX_INT64
3898 // Adding MIN_INT64 to all three sides yields
3900 // MIN_INT64 < (MIN_INT64 + Subtrahend) <= (MIN_INT64 + MAX_INT64) = -1
3902 // * A negative Subtrahend can only cause overflow. The overflow condition is
3904 // (Minuend - Subtrahend) > MAX_INT64
3906 // Adding Subtrahend to both sides yields
3908 // Minuend > (MAX_INT64 + Subtrahend)
3910 // This condition can be coded directly in C because the RHS will neither
3911 // underflow nor overflow. That is due to the starting condition:
3913 // MIN_INT64 <= Subtrahend < 0
3915 // Adding MAX_INT64 to all three sides yields
3917 // -1 = (MAX_INT64 + MIN_INT64) <= (MAX_INT64 + Subtrahend) < MAX_INT64
3919 if (((Subtrahend
> 0) && (Minuend
< (MIN_INT64
+ Subtrahend
))) ||
3920 ((Subtrahend
< 0) && (Minuend
> (MAX_INT64
+ Subtrahend
)))) {
3921 *Result
= INT64_ERROR
;
3922 Status
= RETURN_BUFFER_TOO_SMALL
;
3924 *Result
= Minuend
- Subtrahend
;
3925 Status
= RETURN_SUCCESS
;
3932 // Signed multiplication functions
3938 Performs the requested operation using the input parameters into a value
3939 specified by Result type and stores the converted value into the caller
3940 allocated output buffer specified by Result. The caller must pass in a
3941 Result buffer that is at least as large as the Result type.
3943 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3945 If the requested operation results in an overflow or an underflow condition,
3946 then Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3948 @param[in] Multiplicand A number that is to be multiplied by another
3949 @param[in] Multiplier A number by which the multiplicand is to be multiplied
3950 @param[out] Result Pointer to the result of multiplication
3952 @retval RETURN_SUCCESS Successful multiplication
3953 @retval RETURN_BUFFER_TOO_SMALL Overflow
3954 @retval RETURN_INVALID_PARAMETER Result is NULL
3959 IN INT8 Multiplicand
,
3964 return SafeInt32ToInt8 (((INT32
)Multiplier
) *((INT32
)Multiplicand
), Result
);
3968 CHAR8 multiplication
3970 Performs the requested operation using the input parameters into a value
3971 specified by Result type and stores the converted value into the caller
3972 allocated output buffer specified by Result. The caller must pass in a
3973 Result buffer that is at least as large as the Result type.
3975 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3977 If the requested operation results in an overflow or an underflow condition,
3978 then Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3980 @param[in] Multiplicand A number that is to be multiplied by another
3981 @param[in] Multiplier A number by which the multiplicand is to be multiplied
3982 @param[out] Result Pointer to the result of multiplication
3984 @retval RETURN_SUCCESS Successful multiplication
3985 @retval RETURN_BUFFER_TOO_SMALL Overflow
3986 @retval RETURN_INVALID_PARAMETER Result is NULL
3991 IN CHAR8 Multiplicand
,
3992 IN CHAR8 Multiplier
,
3996 INT32 Multiplicand32
;
3999 if (Result
== NULL
) {
4000 return RETURN_INVALID_PARAMETER
;
4003 Multiplicand32
= (INT32
)Multiplicand
;
4004 Multiplier32
= (INT32
)Multiplier
;
4005 if (Multiplicand32
< 0 || Multiplicand32
> MAX_INT8
) {
4006 *Result
= CHAR8_ERROR
;
4007 return RETURN_BUFFER_TOO_SMALL
;
4009 if (Multiplier32
< 0 || Multiplier32
> MAX_INT8
) {
4010 *Result
= CHAR8_ERROR
;
4011 return RETURN_BUFFER_TOO_SMALL
;
4014 return SafeInt32ToChar8 (Multiplicand32
* Multiplier32
, Result
);
4018 INT16 multiplication
4020 Performs the requested operation using the input parameters into a value
4021 specified by Result type and stores the converted value into the caller
4022 allocated output buffer specified by Result. The caller must pass in a
4023 Result buffer that is at least as large as the Result type.
4025 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
4027 If the requested operation results in an overflow or an underflow condition,
4028 then Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
4030 @param[in] Multiplicand A number that is to be multiplied by another
4031 @param[in] Multiplier A number by which the multiplicand is to be multiplied
4032 @param[out] Result Pointer to the result of multiplication
4034 @retval RETURN_SUCCESS Successful multiplication
4035 @retval RETURN_BUFFER_TOO_SMALL Overflow
4036 @retval RETURN_INVALID_PARAMETER Result is NULL
4041 IN INT16 Multiplicand
,
4042 IN INT16 Multiplier
,
4046 return SafeInt32ToInt16 (((INT32
)Multiplicand
) *((INT32
)Multiplier
), Result
);
4050 INT32 multiplication
4052 Performs the requested operation using the input parameters into a value
4053 specified by Result type and stores the converted value into the caller
4054 allocated output buffer specified by Result. The caller must pass in a
4055 Result buffer that is at least as large as the Result type.
4057 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
4059 If the requested operation results in an overflow or an underflow condition,
4060 then Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
4062 @param[in] Multiplicand A number that is to be multiplied by another
4063 @param[in] Multiplier A number by which the multiplicand is to be multiplied
4064 @param[out] Result Pointer to the result of multiplication
4066 @retval RETURN_SUCCESS Successful multiplication
4067 @retval RETURN_BUFFER_TOO_SMALL Overflow
4068 @retval RETURN_INVALID_PARAMETER Result is NULL
4073 IN INT32 Multiplicand
,
4074 IN INT32 Multiplier
,
4078 return SafeInt64ToInt32 (((INT64
)Multiplicand
) *((INT64
)Multiplier
), Result
);
4082 INT64 multiplication
4084 Performs the requested operation using the input parameters into a value
4085 specified by Result type and stores the converted value into the caller
4086 allocated output buffer specified by Result. The caller must pass in a
4087 Result buffer that is at least as large as the Result type.
4089 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
4091 If the requested operation results in an overflow or an underflow condition,
4092 then Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
4094 @param[in] Multiplicand A number that is to be multiplied by another
4095 @param[in] Multiplier A number by which the multiplicand is to be multiplied
4096 @param[out] Result Pointer to the result of multiplication
4098 @retval RETURN_SUCCESS Successful multiplication
4099 @retval RETURN_BUFFER_TOO_SMALL Overflow
4100 @retval RETURN_INVALID_PARAMETER Result is NULL
4105 IN INT64 Multiplicand
,
4106 IN INT64 Multiplier
,
4110 RETURN_STATUS Status
;
4111 UINT64 UnsignedMultiplicand
;
4112 UINT64 UnsignedMultiplier
;
4113 UINT64 UnsignedResult
;
4115 if (Result
== NULL
) {
4116 return RETURN_INVALID_PARAMETER
;
4120 // Split into sign and magnitude, do unsigned operation, apply sign.
4122 if (Multiplicand
< 0) {
4124 // Avoid negating the most negative number.
4126 UnsignedMultiplicand
= ((UINT64
)(- (Multiplicand
+ 1))) + 1;
4128 UnsignedMultiplicand
= (UINT64
)Multiplicand
;
4131 if (Multiplier
< 0) {
4133 // Avoid negating the most negative number.
4135 UnsignedMultiplier
= ((UINT64
)(- (Multiplier
+ 1))) + 1;
4137 UnsignedMultiplier
= (UINT64
)Multiplier
;
4140 Status
= SafeUint64Mult (UnsignedMultiplicand
, UnsignedMultiplier
, &UnsignedResult
);
4141 if (!RETURN_ERROR (Status
)) {
4142 if ((Multiplicand
< 0) != (Multiplier
< 0)) {
4143 if (UnsignedResult
> MIN_INT64_MAGNITUDE
) {
4144 *Result
= INT64_ERROR
;
4145 Status
= RETURN_BUFFER_TOO_SMALL
;
4147 *Result
= - ((INT64
)UnsignedResult
);
4150 if (UnsignedResult
> MAX_INT64
) {
4151 *Result
= INT64_ERROR
;
4152 Status
= RETURN_BUFFER_TOO_SMALL
;
4154 *Result
= (INT64
)UnsignedResult
;
4158 *Result
= INT64_ERROR
;