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>
31 #include <Library/BaseLib.h>
35 // Magnitude of MIN_INT64 as expressed by a UINT64 number.
37 #define MIN_INT64_MAGNITUDE (((UINT64)(- (MIN_INT64 + 1))) + 1)
40 // Conversion functions
42 // There are three reasons for having conversion functions:
44 // 1. We are converting from a signed type to an unsigned type of the same
45 // size, or vice-versa.
47 // 2. We are converting to a smaller type, and we could therefore possibly
50 // 3. We are converting to a bigger type, and we are signed and the type we are
51 // converting to is unsigned.
55 INT8 -> UINT8 conversion
57 Converts the value specified by Operand to a value specified by Result type
58 and stores the converted value into the caller allocated output buffer
59 specified by Result. The caller must pass in a Result buffer that is at
60 least as large as the Result type.
62 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
64 If the conversion results in an overflow or an underflow condition, then
65 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
67 @param[in] Operand Operand to be converted to new type
68 @param[out] Result Pointer to the result of conversion
70 @retval RETURN_SUCCESS Successful conversion
71 @retval RETURN_BUFFER_TOO_SMALL Overflow
72 @retval RETURN_INVALID_PARAMETER Result is NULL
84 return RETURN_INVALID_PARAMETER
;
88 *Result
= (UINT8
)Operand
;
89 Status
= RETURN_SUCCESS
;
91 *Result
= UINT8_ERROR
;
92 Status
= RETURN_BUFFER_TOO_SMALL
;
99 INT8 -> CHAR8 conversion
101 Converts the value specified by Operand to a value specified by Result type
102 and stores the converted value into the caller allocated output buffer
103 specified by Result. The caller must pass in a Result buffer that is at
104 least as large as the Result type.
106 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
108 If the conversion results in an overflow or an underflow condition, then
109 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
111 @param[in] Operand Operand to be converted to new type
112 @param[out] Result Pointer to the result of conversion
114 @retval RETURN_SUCCESS Successful conversion
115 @retval RETURN_BUFFER_TOO_SMALL Overflow
116 @retval RETURN_INVALID_PARAMETER Result is NULL
125 RETURN_STATUS Status
;
127 if (Result
== NULL
) {
128 return RETURN_INVALID_PARAMETER
;
132 *Result
= (CHAR8
)Operand
;
133 Status
= RETURN_SUCCESS
;
135 *Result
= CHAR8_ERROR
;
136 Status
= RETURN_BUFFER_TOO_SMALL
;
143 INT8 -> UINT16 conversion
145 Converts the value specified by Operand to a value specified by Result type
146 and stores the converted value into the caller allocated output buffer
147 specified by Result. The caller must pass in a Result buffer that is at
148 least as large as the Result type.
150 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
152 If the conversion results in an overflow or an underflow condition, then
153 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
155 @param[in] Operand Operand to be converted to new type
156 @param[out] Result Pointer to the result of conversion
158 @retval RETURN_SUCCESS Successful conversion
159 @retval RETURN_BUFFER_TOO_SMALL Overflow
160 @retval RETURN_INVALID_PARAMETER Result is NULL
169 RETURN_STATUS Status
;
171 if (Result
== NULL
) {
172 return RETURN_INVALID_PARAMETER
;
176 *Result
= (UINT16
)Operand
;
177 Status
= RETURN_SUCCESS
;
179 *Result
= UINT16_ERROR
;
180 Status
= RETURN_BUFFER_TOO_SMALL
;
187 INT8 -> UINT32 conversion
189 Converts the value specified by Operand to a value specified by Result type
190 and stores the converted value into the caller allocated output buffer
191 specified by Result. The caller must pass in a Result buffer that is at
192 least as large as the Result type.
194 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
196 If the conversion results in an overflow or an underflow condition, then
197 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
199 @param[in] Operand Operand to be converted to new type
200 @param[out] Result Pointer to the result of conversion
202 @retval RETURN_SUCCESS Successful conversion
203 @retval RETURN_BUFFER_TOO_SMALL Overflow
204 @retval RETURN_INVALID_PARAMETER Result is NULL
213 RETURN_STATUS Status
;
215 if (Result
== NULL
) {
216 return RETURN_INVALID_PARAMETER
;
220 *Result
= (UINT32
)Operand
;
221 Status
= RETURN_SUCCESS
;
223 *Result
= UINT32_ERROR
;
224 Status
= RETURN_BUFFER_TOO_SMALL
;
231 INT8 -> UINTN conversion
233 Converts the value specified by Operand to a value specified by Result type
234 and stores the converted value into the caller allocated output buffer
235 specified by Result. The caller must pass in a Result buffer that is at
236 least as large as the Result type.
238 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
240 If the conversion results in an overflow or an underflow condition, then
241 Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
243 @param[in] Operand Operand to be converted to new type
244 @param[out] Result Pointer to the result of conversion
246 @retval RETURN_SUCCESS Successful conversion
247 @retval RETURN_BUFFER_TOO_SMALL Overflow
248 @retval RETURN_INVALID_PARAMETER Result is NULL
257 RETURN_STATUS Status
;
259 if (Result
== NULL
) {
260 return RETURN_INVALID_PARAMETER
;
264 *Result
= (UINTN
)Operand
;
265 Status
= RETURN_SUCCESS
;
267 *Result
= UINTN_ERROR
;
268 Status
= RETURN_BUFFER_TOO_SMALL
;
275 INT8 -> UINT64 conversion
277 Converts the value specified by Operand to a value specified by Result type
278 and stores the converted value into the caller allocated output buffer
279 specified by Result. The caller must pass in a Result buffer that is at
280 least as large as the Result type.
282 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
284 If the conversion results in an overflow or an underflow condition, then
285 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
287 @param[in] Operand Operand to be converted to new type
288 @param[out] Result Pointer to the result of conversion
290 @retval RETURN_SUCCESS Successful conversion
291 @retval RETURN_BUFFER_TOO_SMALL Overflow
292 @retval RETURN_INVALID_PARAMETER Result is NULL
301 RETURN_STATUS Status
;
303 if (Result
== NULL
) {
304 return RETURN_INVALID_PARAMETER
;
308 *Result
= (UINT64
)Operand
;
309 Status
= RETURN_SUCCESS
;
311 *Result
= UINT64_ERROR
;
312 Status
= RETURN_BUFFER_TOO_SMALL
;
319 UINT8 -> INT8 conversion
321 Converts the value specified by Operand to a value specified by Result type
322 and stores the converted value into the caller allocated output buffer
323 specified by Result. The caller must pass in a Result buffer that is at
324 least as large as the Result type.
326 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
328 If the conversion results in an overflow or an underflow condition, then
329 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
331 @param[in] Operand Operand to be converted to new type
332 @param[out] Result Pointer to the result of conversion
334 @retval RETURN_SUCCESS Successful conversion
335 @retval RETURN_BUFFER_TOO_SMALL Overflow
336 @retval RETURN_INVALID_PARAMETER Result is NULL
345 RETURN_STATUS Status
;
347 if (Result
== NULL
) {
348 return RETURN_INVALID_PARAMETER
;
351 if (Operand
<= MAX_INT8
) {
352 *Result
= (INT8
)Operand
;
353 Status
= RETURN_SUCCESS
;
355 *Result
= INT8_ERROR
;
356 Status
= RETURN_BUFFER_TOO_SMALL
;
363 UINT8 -> CHAR8 conversion
365 Converts the value specified by Operand to a value specified by Result type
366 and stores the converted value into the caller allocated output buffer
367 specified by Result. The caller must pass in a Result buffer that is at
368 least as large as the Result type.
370 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
372 If the conversion results in an overflow or an underflow condition, then
373 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
375 @param[in] Operand Operand to be converted to new type
376 @param[out] Result Pointer to the result of conversion
378 @retval RETURN_SUCCESS Successful conversion
379 @retval RETURN_BUFFER_TOO_SMALL Overflow
380 @retval RETURN_INVALID_PARAMETER Result is NULL
389 RETURN_STATUS Status
;
391 if (Result
== NULL
) {
392 return RETURN_INVALID_PARAMETER
;
395 if (Operand
<= MAX_INT8
) {
396 *Result
= (CHAR8
)Operand
;
397 Status
= RETURN_SUCCESS
;
399 *Result
= CHAR8_ERROR
;
400 Status
= RETURN_BUFFER_TOO_SMALL
;
407 INT16 -> INT8 conversion
409 Converts the value specified by Operand to a value specified by Result type
410 and stores the converted value into the caller allocated output buffer
411 specified by Result. The caller must pass in a Result buffer that is at
412 least as large as the Result type.
414 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
416 If the conversion results in an overflow or an underflow condition, then
417 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
419 @param[in] Operand Operand to be converted to new type
420 @param[out] Result Pointer to the result of conversion
422 @retval RETURN_SUCCESS Successful conversion
423 @retval RETURN_BUFFER_TOO_SMALL Overflow
424 @retval RETURN_INVALID_PARAMETER Result is NULL
433 RETURN_STATUS Status
;
435 if (Result
== NULL
) {
436 return RETURN_INVALID_PARAMETER
;
439 if ((Operand
>= MIN_INT8
) && (Operand
<= MAX_INT8
)) {
440 *Result
= (INT8
)Operand
;
441 Status
= RETURN_SUCCESS
;
443 *Result
= INT8_ERROR
;
444 Status
= RETURN_BUFFER_TOO_SMALL
;
451 INT16 -> CHAR8 conversion
453 Converts the value specified by Operand to a value specified by Result type
454 and stores the converted value into the caller allocated output buffer
455 specified by Result. The caller must pass in a Result buffer that is at
456 least as large as the Result type.
458 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
460 If the conversion results in an overflow or an underflow condition, then
461 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
463 @param[in] Operand Operand to be converted to new type
464 @param[out] Result Pointer to the result of conversion
466 @retval RETURN_SUCCESS Successful conversion
467 @retval RETURN_BUFFER_TOO_SMALL Overflow
468 @retval RETURN_INVALID_PARAMETER Result is NULL
477 RETURN_STATUS Status
;
479 if (Result
== NULL
) {
480 return RETURN_INVALID_PARAMETER
;
483 if ((Operand
>= 0) && (Operand
<= MAX_INT8
)) {
484 *Result
= (CHAR8
)Operand
;
485 Status
= RETURN_SUCCESS
;
487 *Result
= CHAR8_ERROR
;
488 Status
= RETURN_BUFFER_TOO_SMALL
;
495 INT16 -> UINT8 conversion
497 Converts the value specified by Operand to a value specified by Result type
498 and stores the converted value into the caller allocated output buffer
499 specified by Result. The caller must pass in a Result buffer that is at
500 least as large as the Result type.
502 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
504 If the conversion results in an overflow or an underflow condition, then
505 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
507 @param[in] Operand Operand to be converted to new type
508 @param[out] Result Pointer to the result of conversion
510 @retval RETURN_SUCCESS Successful conversion
511 @retval RETURN_BUFFER_TOO_SMALL Overflow
512 @retval RETURN_INVALID_PARAMETER Result is NULL
521 RETURN_STATUS Status
;
523 if (Result
== NULL
) {
524 return RETURN_INVALID_PARAMETER
;
527 if ((Operand
>= 0) && (Operand
<= MAX_UINT8
)) {
528 *Result
= (UINT8
)Operand
;
529 Status
= RETURN_SUCCESS
;
531 *Result
= UINT8_ERROR
;
532 Status
= RETURN_BUFFER_TOO_SMALL
;
539 INT16 -> UINT16 conversion
541 Converts the value specified by Operand to a value specified by Result type
542 and stores the converted value into the caller allocated output buffer
543 specified by Result. The caller must pass in a Result buffer that is at
544 least as large as the Result type.
546 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
548 If the conversion results in an overflow or an underflow condition, then
549 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
551 @param[in] Operand Operand to be converted to new type
552 @param[out] Result Pointer to the result of conversion
554 @retval RETURN_SUCCESS Successful conversion
555 @retval RETURN_BUFFER_TOO_SMALL Overflow
556 @retval RETURN_INVALID_PARAMETER Result is NULL
565 RETURN_STATUS Status
;
567 if (Result
== NULL
) {
568 return RETURN_INVALID_PARAMETER
;
572 *Result
= (UINT16
)Operand
;
573 Status
= RETURN_SUCCESS
;
575 *Result
= UINT16_ERROR
;
576 Status
= RETURN_BUFFER_TOO_SMALL
;
583 INT16 -> UINT32 conversion
585 Converts the value specified by Operand to a value specified by Result type
586 and stores the converted value into the caller allocated output buffer
587 specified by Result. The caller must pass in a Result buffer that is at
588 least as large as the Result type.
590 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
592 If the conversion results in an overflow or an underflow condition, then
593 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
595 @param[in] Operand Operand to be converted to new type
596 @param[out] Result Pointer to the result of conversion
598 @retval RETURN_SUCCESS Successful conversion
599 @retval RETURN_BUFFER_TOO_SMALL Overflow
600 @retval RETURN_INVALID_PARAMETER Result is NULL
609 RETURN_STATUS Status
;
611 if (Result
== NULL
) {
612 return RETURN_INVALID_PARAMETER
;
616 *Result
= (UINT32
)Operand
;
617 Status
= RETURN_SUCCESS
;
619 *Result
= UINT32_ERROR
;
620 Status
= RETURN_BUFFER_TOO_SMALL
;
627 INT16 -> UINTN conversion
629 Converts the value specified by Operand to a value specified by Result type
630 and stores the converted value into the caller allocated output buffer
631 specified by Result. The caller must pass in a Result buffer that is at
632 least as large as the Result type.
634 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
636 If the conversion results in an overflow or an underflow condition, then
637 Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
639 @param[in] Operand Operand to be converted to new type
640 @param[out] Result Pointer to the result of conversion
642 @retval RETURN_SUCCESS Successful conversion
643 @retval RETURN_BUFFER_TOO_SMALL Overflow
644 @retval RETURN_INVALID_PARAMETER Result is NULL
653 RETURN_STATUS Status
;
655 if (Result
== NULL
) {
656 return RETURN_INVALID_PARAMETER
;
660 *Result
= (UINTN
)Operand
;
661 Status
= RETURN_SUCCESS
;
663 *Result
= UINTN_ERROR
;
664 Status
= RETURN_BUFFER_TOO_SMALL
;
671 INT16 -> UINT64 conversion
673 Converts the value specified by Operand to a value specified by Result type
674 and stores the converted value into the caller allocated output buffer
675 specified by Result. The caller must pass in a Result buffer that is at
676 least as large as the Result type.
678 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
680 If the conversion results in an overflow or an underflow condition, then
681 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
683 @param[in] Operand Operand to be converted to new type
684 @param[out] Result Pointer to the result of conversion
686 @retval RETURN_SUCCESS Successful conversion
687 @retval RETURN_BUFFER_TOO_SMALL Overflow
688 @retval RETURN_INVALID_PARAMETER Result is NULL
697 RETURN_STATUS Status
;
699 if (Result
== NULL
) {
700 return RETURN_INVALID_PARAMETER
;
704 *Result
= (UINT64
)Operand
;
705 Status
= RETURN_SUCCESS
;
707 *Result
= UINT64_ERROR
;
708 Status
= RETURN_BUFFER_TOO_SMALL
;
715 UINT16 -> INT8 conversion
717 Converts the value specified by Operand to a value specified by Result type
718 and stores the converted value into the caller allocated output buffer
719 specified by Result. The caller must pass in a Result buffer that is at
720 least as large as the Result type.
722 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
724 If the conversion results in an overflow or an underflow condition, then
725 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
727 @param[in] Operand Operand to be converted to new type
728 @param[out] Result Pointer to the result of conversion
730 @retval RETURN_SUCCESS Successful conversion
731 @retval RETURN_BUFFER_TOO_SMALL Overflow
732 @retval RETURN_INVALID_PARAMETER Result is NULL
741 RETURN_STATUS Status
;
743 if (Result
== NULL
) {
744 return RETURN_INVALID_PARAMETER
;
747 if (Operand
<= MAX_INT8
) {
748 *Result
= (INT8
)Operand
;
749 Status
= RETURN_SUCCESS
;
751 *Result
= INT8_ERROR
;
752 Status
= RETURN_BUFFER_TOO_SMALL
;
759 UINT16 -> CHAR8 conversion
761 Converts the value specified by Operand to a value specified by Result type
762 and stores the converted value into the caller allocated output buffer
763 specified by Result. The caller must pass in a Result buffer that is at
764 least as large as the Result type.
766 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
768 If the conversion results in an overflow or an underflow condition, then
769 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
771 @param[in] Operand Operand to be converted to new type
772 @param[out] Result Pointer to the result of conversion
774 @retval RETURN_SUCCESS Successful conversion
775 @retval RETURN_BUFFER_TOO_SMALL Overflow
776 @retval RETURN_INVALID_PARAMETER Result is NULL
785 RETURN_STATUS Status
;
787 if (Result
== NULL
) {
788 return RETURN_INVALID_PARAMETER
;
791 if (Operand
<= MAX_INT8
) {
792 *Result
= (INT8
)Operand
;
793 Status
= RETURN_SUCCESS
;
795 *Result
= CHAR8_ERROR
;
796 Status
= RETURN_BUFFER_TOO_SMALL
;
803 UINT16 -> UINT8 conversion
805 Converts the value specified by Operand to a value specified by Result type
806 and stores the converted value into the caller allocated output buffer
807 specified by Result. The caller must pass in a Result buffer that is at
808 least as large as the Result type.
810 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
812 If the conversion results in an overflow or an underflow condition, then
813 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
815 @param[in] Operand Operand to be converted to new type
816 @param[out] Result Pointer to the result of conversion
818 @retval RETURN_SUCCESS Successful conversion
819 @retval RETURN_BUFFER_TOO_SMALL Overflow
820 @retval RETURN_INVALID_PARAMETER Result is NULL
829 RETURN_STATUS Status
;
831 if (Result
== NULL
) {
832 return RETURN_INVALID_PARAMETER
;
835 if (Operand
<= MAX_UINT8
) {
836 *Result
= (UINT8
)Operand
;
837 Status
= RETURN_SUCCESS
;
839 *Result
= UINT8_ERROR
;
840 Status
= RETURN_BUFFER_TOO_SMALL
;
847 UINT16 -> INT16 conversion
849 Converts the value specified by Operand to a value specified by Result type
850 and stores the converted value into the caller allocated output buffer
851 specified by Result. The caller must pass in a Result buffer that is at
852 least as large as the Result type.
854 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
856 If the conversion results in an overflow or an underflow condition, then
857 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
859 @param[in] Operand Operand to be converted to new type
860 @param[out] Result Pointer to the result of conversion
862 @retval RETURN_SUCCESS Successful conversion
863 @retval RETURN_BUFFER_TOO_SMALL Overflow
864 @retval RETURN_INVALID_PARAMETER Result is NULL
873 RETURN_STATUS Status
;
875 if (Result
== NULL
) {
876 return RETURN_INVALID_PARAMETER
;
879 if (Operand
<= MAX_INT16
) {
880 *Result
= (INT16
)Operand
;
881 Status
= RETURN_SUCCESS
;
883 *Result
= INT16_ERROR
;
884 Status
= RETURN_BUFFER_TOO_SMALL
;
891 INT32 -> INT8 conversion
893 Converts the value specified by Operand to a value specified by Result type
894 and stores the converted value into the caller allocated output buffer
895 specified by Result. The caller must pass in a Result buffer that is at
896 least as large as the Result type.
898 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
900 If the conversion results in an overflow or an underflow condition, then
901 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
903 @param[in] Operand Operand to be converted to new type
904 @param[out] Result Pointer to the result of conversion
906 @retval RETURN_SUCCESS Successful conversion
907 @retval RETURN_BUFFER_TOO_SMALL Overflow
908 @retval RETURN_INVALID_PARAMETER Result is NULL
917 RETURN_STATUS Status
;
919 if (Result
== NULL
) {
920 return RETURN_INVALID_PARAMETER
;
923 if ((Operand
>= MIN_INT8
) && (Operand
<= MAX_INT8
)) {
924 *Result
= (INT8
)Operand
;
925 Status
= RETURN_SUCCESS
;
927 *Result
= INT8_ERROR
;
928 Status
= RETURN_BUFFER_TOO_SMALL
;
935 INT32 -> CHAR8 conversion
937 Converts the value specified by Operand to a value specified by Result type
938 and stores the converted value into the caller allocated output buffer
939 specified by Result. The caller must pass in a Result buffer that is at
940 least as large as the Result type.
942 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
944 If the conversion results in an overflow or an underflow condition, then
945 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
947 @param[in] Operand Operand to be converted to new type
948 @param[out] Result Pointer to the result of conversion
950 @retval RETURN_SUCCESS Successful conversion
951 @retval RETURN_BUFFER_TOO_SMALL Overflow
952 @retval RETURN_INVALID_PARAMETER Result is NULL
961 RETURN_STATUS Status
;
963 if (Result
== NULL
) {
964 return RETURN_INVALID_PARAMETER
;
967 if ((Operand
>= 0) && (Operand
<= MAX_INT8
)) {
968 *Result
= (CHAR8
)Operand
;
969 Status
= RETURN_SUCCESS
;
971 *Result
= CHAR8_ERROR
;
972 Status
= RETURN_BUFFER_TOO_SMALL
;
979 INT32 -> UINT8 conversion
981 Converts the value specified by Operand to a value specified by Result type
982 and stores the converted value into the caller allocated output buffer
983 specified by Result. The caller must pass in a Result buffer that is at
984 least as large as the Result type.
986 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
988 If the conversion results in an overflow or an underflow condition, then
989 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
991 @param[in] Operand Operand to be converted to new type
992 @param[out] Result Pointer to the result of conversion
994 @retval RETURN_SUCCESS Successful conversion
995 @retval RETURN_BUFFER_TOO_SMALL Overflow
996 @retval RETURN_INVALID_PARAMETER Result is NULL
1005 RETURN_STATUS Status
;
1007 if (Result
== NULL
) {
1008 return RETURN_INVALID_PARAMETER
;
1011 if ((Operand
>= 0) && (Operand
<= MAX_UINT8
)) {
1012 *Result
= (UINT8
)Operand
;
1013 Status
= RETURN_SUCCESS
;
1015 *Result
= UINT8_ERROR
;
1016 Status
= RETURN_BUFFER_TOO_SMALL
;
1023 INT32 -> INT16 conversion
1025 Converts the value specified by Operand to a value specified by Result type
1026 and stores the converted value into the caller allocated output buffer
1027 specified by Result. The caller must pass in a Result buffer that is at
1028 least as large as the Result type.
1030 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1032 If the conversion results in an overflow or an underflow condition, then
1033 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1035 @param[in] Operand Operand to be converted to new type
1036 @param[out] Result Pointer to the result of conversion
1038 @retval RETURN_SUCCESS Successful conversion
1039 @retval RETURN_BUFFER_TOO_SMALL Overflow
1040 @retval RETURN_INVALID_PARAMETER Result is NULL
1049 RETURN_STATUS Status
;
1051 if (Result
== NULL
) {
1052 return RETURN_INVALID_PARAMETER
;
1055 if ((Operand
>= MIN_INT16
) && (Operand
<= MAX_INT16
)) {
1056 *Result
= (INT16
)Operand
;
1057 Status
= RETURN_SUCCESS
;
1059 *Result
= INT16_ERROR
;
1060 Status
= RETURN_BUFFER_TOO_SMALL
;
1067 INT32 -> UINT16 conversion
1069 Converts the value specified by Operand to a value specified by Result type
1070 and stores the converted value into the caller allocated output buffer
1071 specified by Result. The caller must pass in a Result buffer that is at
1072 least as large as the Result type.
1074 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1076 If the conversion results in an overflow or an underflow condition, then
1077 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1079 @param[in] Operand Operand to be converted to new type
1080 @param[out] Result Pointer to the result of conversion
1082 @retval RETURN_SUCCESS Successful conversion
1083 @retval RETURN_BUFFER_TOO_SMALL Overflow
1084 @retval RETURN_INVALID_PARAMETER Result is NULL
1093 RETURN_STATUS Status
;
1095 if (Result
== NULL
) {
1096 return RETURN_INVALID_PARAMETER
;
1099 if ((Operand
>= 0) && (Operand
<= MAX_UINT16
)) {
1100 *Result
= (UINT16
)Operand
;
1101 Status
= RETURN_SUCCESS
;
1103 *Result
= UINT16_ERROR
;
1104 Status
= RETURN_BUFFER_TOO_SMALL
;
1111 INT32 -> UINT32 conversion
1113 Converts the value specified by Operand to a value specified by Result type
1114 and stores the converted value into the caller allocated output buffer
1115 specified by Result. The caller must pass in a Result buffer that is at
1116 least as large as the Result type.
1118 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1120 If the conversion results in an overflow or an underflow condition, then
1121 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1123 @param[in] Operand Operand to be converted to new type
1124 @param[out] Result Pointer to the result of conversion
1126 @retval RETURN_SUCCESS Successful conversion
1127 @retval RETURN_BUFFER_TOO_SMALL Overflow
1128 @retval RETURN_INVALID_PARAMETER Result is NULL
1137 RETURN_STATUS Status
;
1139 if (Result
== NULL
) {
1140 return RETURN_INVALID_PARAMETER
;
1144 *Result
= (UINT32
)Operand
;
1145 Status
= RETURN_SUCCESS
;
1147 *Result
= UINT32_ERROR
;
1148 Status
= RETURN_BUFFER_TOO_SMALL
;
1155 INT32 -> UINT64 conversion
1157 Converts the value specified by Operand to a value specified by Result type
1158 and stores the converted value into the caller allocated output buffer
1159 specified by Result. The caller must pass in a Result buffer that is at
1160 least as large as the Result type.
1162 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1164 If the conversion results in an overflow or an underflow condition, then
1165 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1167 @param[in] Operand Operand to be converted to new type
1168 @param[out] Result Pointer to the result of conversion
1170 @retval RETURN_SUCCESS Successful conversion
1171 @retval RETURN_BUFFER_TOO_SMALL Overflow
1172 @retval RETURN_INVALID_PARAMETER Result is NULL
1181 RETURN_STATUS Status
;
1183 if (Result
== NULL
) {
1184 return RETURN_INVALID_PARAMETER
;
1188 *Result
= (UINT64
)Operand
;
1189 Status
= RETURN_SUCCESS
;
1191 *Result
= UINT64_ERROR
;
1192 Status
= RETURN_BUFFER_TOO_SMALL
;
1199 UINT32 -> INT8 conversion
1201 Converts the value specified by Operand to a value specified by Result type
1202 and stores the converted value into the caller allocated output buffer
1203 specified by Result. The caller must pass in a Result buffer that is at
1204 least as large as the Result type.
1206 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1208 If the conversion results in an overflow or an underflow condition, then
1209 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1211 @param[in] Operand Operand to be converted to new type
1212 @param[out] Result Pointer to the result of conversion
1214 @retval RETURN_SUCCESS Successful conversion
1215 @retval RETURN_BUFFER_TOO_SMALL Overflow
1216 @retval RETURN_INVALID_PARAMETER Result is NULL
1225 RETURN_STATUS Status
;
1227 if (Result
== NULL
) {
1228 return RETURN_INVALID_PARAMETER
;
1231 if (Operand
<= MAX_INT8
) {
1232 *Result
= (INT8
)Operand
;
1233 Status
= RETURN_SUCCESS
;
1235 *Result
= INT8_ERROR
;
1236 Status
= RETURN_BUFFER_TOO_SMALL
;
1243 UINT32 -> CHAR8 conversion
1245 Converts the value specified by Operand to a value specified by Result type
1246 and stores the converted value into the caller allocated output buffer
1247 specified by Result. The caller must pass in a Result buffer that is at
1248 least as large as the Result type.
1250 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1252 If the conversion results in an overflow or an underflow condition, then
1253 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1255 @param[in] Operand Operand to be converted to new type
1256 @param[out] Result Pointer to the result of conversion
1258 @retval RETURN_SUCCESS Successful conversion
1259 @retval RETURN_BUFFER_TOO_SMALL Overflow
1260 @retval RETURN_INVALID_PARAMETER Result is NULL
1269 RETURN_STATUS Status
;
1271 if (Result
== NULL
) {
1272 return RETURN_INVALID_PARAMETER
;
1275 if (Operand
<= MAX_INT8
) {
1276 *Result
= (INT8
)Operand
;
1277 Status
= RETURN_SUCCESS
;
1279 *Result
= CHAR8_ERROR
;
1280 Status
= RETURN_BUFFER_TOO_SMALL
;
1287 UINT32 -> UINT8 conversion
1289 Converts the value specified by Operand to a value specified by Result type
1290 and stores the converted value into the caller allocated output buffer
1291 specified by Result. The caller must pass in a Result buffer that is at
1292 least as large as the Result type.
1294 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1296 If the conversion results in an overflow or an underflow condition, then
1297 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1299 @param[in] Operand Operand to be converted to new type
1300 @param[out] Result Pointer to the result of conversion
1302 @retval RETURN_SUCCESS Successful conversion
1303 @retval RETURN_BUFFER_TOO_SMALL Overflow
1304 @retval RETURN_INVALID_PARAMETER Result is NULL
1313 RETURN_STATUS Status
;
1315 if (Result
== NULL
) {
1316 return RETURN_INVALID_PARAMETER
;
1319 if (Operand
<= MAX_UINT8
) {
1320 *Result
= (UINT8
)Operand
;
1321 Status
= RETURN_SUCCESS
;
1323 *Result
= UINT8_ERROR
;
1324 Status
= RETURN_BUFFER_TOO_SMALL
;
1331 UINT32 -> INT16 conversion
1333 Converts the value specified by Operand to a value specified by Result type
1334 and stores the converted value into the caller allocated output buffer
1335 specified by Result. The caller must pass in a Result buffer that is at
1336 least as large as the Result type.
1338 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1340 If the conversion results in an overflow or an underflow condition, then
1341 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1343 @param[in] Operand Operand to be converted to new type
1344 @param[out] Result Pointer to the result of conversion
1346 @retval RETURN_SUCCESS Successful conversion
1347 @retval RETURN_BUFFER_TOO_SMALL Overflow
1348 @retval RETURN_INVALID_PARAMETER Result is NULL
1357 RETURN_STATUS Status
;
1359 if (Result
== NULL
) {
1360 return RETURN_INVALID_PARAMETER
;
1363 if (Operand
<= MAX_INT16
) {
1364 *Result
= (INT16
)Operand
;
1365 Status
= RETURN_SUCCESS
;
1367 *Result
= INT16_ERROR
;
1368 Status
= RETURN_BUFFER_TOO_SMALL
;
1375 UINT32 -> UINT16 conversion
1377 Converts the value specified by Operand to a value specified by Result type
1378 and stores the converted value into the caller allocated output buffer
1379 specified by Result. The caller must pass in a Result buffer that is at
1380 least as large as the Result type.
1382 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1384 If the conversion results in an overflow or an underflow condition, then
1385 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1387 @param[in] Operand Operand to be converted to new type
1388 @param[out] Result Pointer to the result of conversion
1390 @retval RETURN_SUCCESS Successful conversion
1391 @retval RETURN_BUFFER_TOO_SMALL Overflow
1392 @retval RETURN_INVALID_PARAMETER Result is NULL
1396 SafeUint32ToUint16 (
1401 RETURN_STATUS Status
;
1403 if (Result
== NULL
) {
1404 return RETURN_INVALID_PARAMETER
;
1407 if (Operand
<= MAX_UINT16
) {
1408 *Result
= (UINT16
)Operand
;
1409 Status
= RETURN_SUCCESS
;
1411 *Result
= UINT16_ERROR
;
1412 Status
= RETURN_BUFFER_TOO_SMALL
;
1419 UINT32 -> INT32 conversion
1421 Converts the value specified by Operand to a value specified by Result type
1422 and stores the converted value into the caller allocated output buffer
1423 specified by Result. The caller must pass in a Result buffer that is at
1424 least as large as the Result type.
1426 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1428 If the conversion results in an overflow or an underflow condition, then
1429 Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1431 @param[in] Operand Operand to be converted to new type
1432 @param[out] Result Pointer to the result of conversion
1434 @retval RETURN_SUCCESS Successful conversion
1435 @retval RETURN_BUFFER_TOO_SMALL Overflow
1436 @retval RETURN_INVALID_PARAMETER Result is NULL
1445 RETURN_STATUS Status
;
1447 if (Result
== NULL
) {
1448 return RETURN_INVALID_PARAMETER
;
1451 if (Operand
<= MAX_INT32
) {
1452 *Result
= (INT32
)Operand
;
1453 Status
= RETURN_SUCCESS
;
1455 *Result
= INT32_ERROR
;
1456 Status
= RETURN_BUFFER_TOO_SMALL
;
1463 INTN -> INT8 conversion
1465 Converts the value specified by Operand to a value specified by Result type
1466 and stores the converted value into the caller allocated output buffer
1467 specified by Result. The caller must pass in a Result buffer that is at
1468 least as large as the Result type.
1470 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1472 If the conversion results in an overflow or an underflow condition, then
1473 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1475 @param[in] Operand Operand to be converted to new type
1476 @param[out] Result Pointer to the result of conversion
1478 @retval RETURN_SUCCESS Successful conversion
1479 @retval RETURN_BUFFER_TOO_SMALL Overflow
1480 @retval RETURN_INVALID_PARAMETER Result is NULL
1489 RETURN_STATUS Status
;
1491 if (Result
== NULL
) {
1492 return RETURN_INVALID_PARAMETER
;
1495 if ((Operand
>= MIN_INT8
) && (Operand
<= MAX_INT8
)) {
1496 *Result
= (INT8
)Operand
;
1497 Status
= RETURN_SUCCESS
;
1499 *Result
= INT8_ERROR
;
1500 Status
= RETURN_BUFFER_TOO_SMALL
;
1507 INTN -> CHAR8 conversion
1509 Converts the value specified by Operand to a value specified by Result type
1510 and stores the converted value into the caller allocated output buffer
1511 specified by Result. The caller must pass in a Result buffer that is at
1512 least as large as the Result type.
1514 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1516 If the conversion results in an overflow or an underflow condition, then
1517 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1519 @param[in] Operand Operand to be converted to new type
1520 @param[out] Result Pointer to the result of conversion
1522 @retval RETURN_SUCCESS Successful conversion
1523 @retval RETURN_BUFFER_TOO_SMALL Overflow
1524 @retval RETURN_INVALID_PARAMETER Result is NULL
1533 RETURN_STATUS Status
;
1535 if (Result
== NULL
) {
1536 return RETURN_INVALID_PARAMETER
;
1539 if ((Operand
>= 0) && (Operand
<= MAX_INT8
)) {
1540 *Result
= (CHAR8
)Operand
;
1541 Status
= RETURN_SUCCESS
;
1543 *Result
= CHAR8_ERROR
;
1544 Status
= RETURN_BUFFER_TOO_SMALL
;
1551 INTN -> UINT8 conversion
1553 Converts the value specified by Operand to a value specified by Result type
1554 and stores the converted value into the caller allocated output buffer
1555 specified by Result. The caller must pass in a Result buffer that is at
1556 least as large as the Result type.
1558 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1560 If the conversion results in an overflow or an underflow condition, then
1561 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1563 @param[in] Operand Operand to be converted to new type
1564 @param[out] Result Pointer to the result of conversion
1566 @retval RETURN_SUCCESS Successful conversion
1567 @retval RETURN_BUFFER_TOO_SMALL Overflow
1568 @retval RETURN_INVALID_PARAMETER Result is NULL
1577 RETURN_STATUS Status
;
1579 if (Result
== NULL
) {
1580 return RETURN_INVALID_PARAMETER
;
1583 if ((Operand
>= 0) && (Operand
<= MAX_UINT8
)) {
1584 *Result
= (UINT8
)Operand
;
1585 Status
= RETURN_SUCCESS
;
1587 *Result
= UINT8_ERROR
;
1588 Status
= RETURN_BUFFER_TOO_SMALL
;
1595 INTN -> INT16 conversion
1597 Converts the value specified by Operand to a value specified by Result type
1598 and stores the converted value into the caller allocated output buffer
1599 specified by Result. The caller must pass in a Result buffer that is at
1600 least as large as the Result type.
1602 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1604 If the conversion results in an overflow or an underflow condition, then
1605 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1607 @param[in] Operand Operand to be converted to new type
1608 @param[out] Result Pointer to the result of conversion
1610 @retval RETURN_SUCCESS Successful conversion
1611 @retval RETURN_BUFFER_TOO_SMALL Overflow
1612 @retval RETURN_INVALID_PARAMETER Result is NULL
1621 RETURN_STATUS Status
;
1623 if (Result
== NULL
) {
1624 return RETURN_INVALID_PARAMETER
;
1627 if ((Operand
>= MIN_INT16
) && (Operand
<= MAX_INT16
)) {
1628 *Result
= (INT16
)Operand
;
1629 Status
= RETURN_SUCCESS
;
1631 *Result
= INT16_ERROR
;
1632 Status
= RETURN_BUFFER_TOO_SMALL
;
1639 INTN -> UINT16 conversion
1641 Converts the value specified by Operand to a value specified by Result type
1642 and stores the converted value into the caller allocated output buffer
1643 specified by Result. The caller must pass in a Result buffer that is at
1644 least as large as the Result type.
1646 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1648 If the conversion results in an overflow or an underflow condition, then
1649 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1651 @param[in] Operand Operand to be converted to new type
1652 @param[out] Result Pointer to the result of conversion
1654 @retval RETURN_SUCCESS Successful conversion
1655 @retval RETURN_BUFFER_TOO_SMALL Overflow
1656 @retval RETURN_INVALID_PARAMETER Result is NULL
1665 RETURN_STATUS Status
;
1667 if (Result
== NULL
) {
1668 return RETURN_INVALID_PARAMETER
;
1671 if ((Operand
>= 0) && (Operand
<= MAX_UINT16
)) {
1672 *Result
= (UINT16
)Operand
;
1673 Status
= RETURN_SUCCESS
;
1675 *Result
= UINT16_ERROR
;
1676 Status
= RETURN_BUFFER_TOO_SMALL
;
1683 INTN -> UINTN conversion
1685 Converts the value specified by Operand to a value specified by Result type
1686 and stores the converted value into the caller allocated output buffer
1687 specified by Result. The caller must pass in a Result buffer that is at
1688 least as large as the Result type.
1690 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1692 If the conversion results in an overflow or an underflow condition, then
1693 Result is set to UINTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1695 @param[in] Operand Operand to be converted to new type
1696 @param[out] Result Pointer to the result of conversion
1698 @retval RETURN_SUCCESS Successful conversion
1699 @retval RETURN_BUFFER_TOO_SMALL Overflow
1700 @retval RETURN_INVALID_PARAMETER Result is NULL
1709 RETURN_STATUS Status
;
1711 if (Result
== NULL
) {
1712 return RETURN_INVALID_PARAMETER
;
1716 *Result
= (UINTN
)Operand
;
1717 Status
= RETURN_SUCCESS
;
1719 *Result
= UINTN_ERROR
;
1720 Status
= RETURN_BUFFER_TOO_SMALL
;
1727 INTN -> UINT64 conversion
1729 Converts the value specified by Operand to a value specified by Result type
1730 and stores the converted value into the caller allocated output buffer
1731 specified by Result. The caller must pass in a Result buffer that is at
1732 least as large as the Result type.
1734 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1736 If the conversion results in an overflow or an underflow condition, then
1737 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1739 @param[in] Operand Operand to be converted to new type
1740 @param[out] Result Pointer to the result of conversion
1742 @retval RETURN_SUCCESS Successful conversion
1743 @retval RETURN_BUFFER_TOO_SMALL Overflow
1744 @retval RETURN_INVALID_PARAMETER Result is NULL
1753 RETURN_STATUS Status
;
1755 if (Result
== NULL
) {
1756 return RETURN_INVALID_PARAMETER
;
1760 *Result
= (UINT64
)Operand
;
1761 Status
= RETURN_SUCCESS
;
1763 *Result
= UINT64_ERROR
;
1764 Status
= RETURN_BUFFER_TOO_SMALL
;
1771 UINTN -> INT8 conversion
1773 Converts the value specified by Operand to a value specified by Result type
1774 and stores the converted value into the caller allocated output buffer
1775 specified by Result. The caller must pass in a Result buffer that is at
1776 least as large as the Result type.
1778 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1780 If the conversion results in an overflow or an underflow condition, then
1781 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1783 @param[in] Operand Operand to be converted to new type
1784 @param[out] Result Pointer to the result of conversion
1786 @retval RETURN_SUCCESS Successful conversion
1787 @retval RETURN_BUFFER_TOO_SMALL Overflow
1788 @retval RETURN_INVALID_PARAMETER Result is NULL
1797 RETURN_STATUS Status
;
1799 if (Result
== NULL
) {
1800 return RETURN_INVALID_PARAMETER
;
1803 if (Operand
<= MAX_INT8
) {
1804 *Result
= (INT8
)Operand
;
1805 Status
= RETURN_SUCCESS
;
1807 *Result
= INT8_ERROR
;
1808 Status
= RETURN_BUFFER_TOO_SMALL
;
1815 UINTN -> CHAR8 conversion
1817 Converts the value specified by Operand to a value specified by Result type
1818 and stores the converted value into the caller allocated output buffer
1819 specified by Result. The caller must pass in a Result buffer that is at
1820 least as large as the Result type.
1822 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1824 If the conversion results in an overflow or an underflow condition, then
1825 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1827 @param[in] Operand Operand to be converted to new type
1828 @param[out] Result Pointer to the result of conversion
1830 @retval RETURN_SUCCESS Successful conversion
1831 @retval RETURN_BUFFER_TOO_SMALL Overflow
1832 @retval RETURN_INVALID_PARAMETER Result is NULL
1841 RETURN_STATUS Status
;
1843 if (Result
== NULL
) {
1844 return RETURN_INVALID_PARAMETER
;
1847 if (Operand
<= MAX_INT8
) {
1848 *Result
= (INT8
)Operand
;
1849 Status
= RETURN_SUCCESS
;
1851 *Result
= CHAR8_ERROR
;
1852 Status
= RETURN_BUFFER_TOO_SMALL
;
1859 UINTN -> UINT8 conversion
1861 Converts the value specified by Operand to a value specified by Result type
1862 and stores the converted value into the caller allocated output buffer
1863 specified by Result. The caller must pass in a Result buffer that is at
1864 least as large as the Result type.
1866 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1868 If the conversion results in an overflow or an underflow condition, then
1869 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1871 @param[in] Operand Operand to be converted to new type
1872 @param[out] Result Pointer to the result of conversion
1874 @retval RETURN_SUCCESS Successful conversion
1875 @retval RETURN_BUFFER_TOO_SMALL Overflow
1876 @retval RETURN_INVALID_PARAMETER Result is NULL
1885 RETURN_STATUS Status
;
1887 if (Result
== NULL
) {
1888 return RETURN_INVALID_PARAMETER
;
1891 if (Operand
<= MAX_UINT8
) {
1892 *Result
= (UINT8
)Operand
;
1893 Status
= RETURN_SUCCESS
;
1895 *Result
= UINT8_ERROR
;
1896 Status
= RETURN_BUFFER_TOO_SMALL
;
1903 UINTN -> INT16 conversion
1905 Converts the value specified by Operand to a value specified by Result type
1906 and stores the converted value into the caller allocated output buffer
1907 specified by Result. The caller must pass in a Result buffer that is at
1908 least as large as the Result type.
1910 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1912 If the conversion results in an overflow or an underflow condition, then
1913 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1915 @param[in] Operand Operand to be converted to new type
1916 @param[out] Result Pointer to the result of conversion
1918 @retval RETURN_SUCCESS Successful conversion
1919 @retval RETURN_BUFFER_TOO_SMALL Overflow
1920 @retval RETURN_INVALID_PARAMETER Result is NULL
1929 RETURN_STATUS Status
;
1931 if (Result
== NULL
) {
1932 return RETURN_INVALID_PARAMETER
;
1935 if (Operand
<= MAX_INT16
) {
1936 *Result
= (INT16
)Operand
;
1937 Status
= RETURN_SUCCESS
;
1939 *Result
= INT16_ERROR
;
1940 Status
= RETURN_BUFFER_TOO_SMALL
;
1947 UINTN -> UINT16 conversion
1949 Converts the value specified by Operand to a value specified by Result type
1950 and stores the converted value into the caller allocated output buffer
1951 specified by Result. The caller must pass in a Result buffer that is at
1952 least as large as the Result type.
1954 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
1956 If the conversion results in an overflow or an underflow condition, then
1957 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
1959 @param[in] Operand Operand to be converted to new type
1960 @param[out] Result Pointer to the result of conversion
1962 @retval RETURN_SUCCESS Successful conversion
1963 @retval RETURN_BUFFER_TOO_SMALL Overflow
1964 @retval RETURN_INVALID_PARAMETER Result is NULL
1973 RETURN_STATUS Status
;
1975 if (Result
== NULL
) {
1976 return RETURN_INVALID_PARAMETER
;
1979 if (Operand
<= MAX_UINT16
) {
1980 *Result
= (UINT16
)Operand
;
1981 Status
= RETURN_SUCCESS
;
1983 *Result
= UINT16_ERROR
;
1984 Status
= RETURN_BUFFER_TOO_SMALL
;
1991 UINTN -> INT32 conversion
1993 Converts the value specified by Operand to a value specified by Result type
1994 and stores the converted value into the caller allocated output buffer
1995 specified by Result. The caller must pass in a Result buffer that is at
1996 least as large as the Result type.
1998 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2000 If the conversion results in an overflow or an underflow condition, then
2001 Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2003 @param[in] Operand Operand to be converted to new type
2004 @param[out] Result Pointer to the result of conversion
2006 @retval RETURN_SUCCESS Successful conversion
2007 @retval RETURN_BUFFER_TOO_SMALL Overflow
2008 @retval RETURN_INVALID_PARAMETER Result is NULL
2017 RETURN_STATUS Status
;
2019 if (Result
== NULL
) {
2020 return RETURN_INVALID_PARAMETER
;
2023 if (Operand
<= MAX_INT32
) {
2024 *Result
= (INT32
)Operand
;
2025 Status
= RETURN_SUCCESS
;
2027 *Result
= INT32_ERROR
;
2028 Status
= RETURN_BUFFER_TOO_SMALL
;
2035 UINTN -> INTN conversion
2037 Converts the value specified by Operand to a value specified by Result type
2038 and stores the converted value into the caller allocated output buffer
2039 specified by Result. The caller must pass in a Result buffer that is at
2040 least as large as the Result type.
2042 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2044 If the conversion results in an overflow or an underflow condition, then
2045 Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2047 @param[in] Operand Operand to be converted to new type
2048 @param[out] Result Pointer to the result of conversion
2050 @retval RETURN_SUCCESS Successful conversion
2051 @retval RETURN_BUFFER_TOO_SMALL Overflow
2052 @retval RETURN_INVALID_PARAMETER Result is NULL
2061 RETURN_STATUS Status
;
2063 if (Result
== NULL
) {
2064 return RETURN_INVALID_PARAMETER
;
2067 if (Operand
<= MAX_INTN
) {
2068 *Result
= (INTN
)Operand
;
2069 Status
= RETURN_SUCCESS
;
2071 *Result
= INTN_ERROR
;
2072 Status
= RETURN_BUFFER_TOO_SMALL
;
2079 INT64 -> INT8 conversion
2081 Converts the value specified by Operand to a value specified by Result type
2082 and stores the converted value into the caller allocated output buffer
2083 specified by Result. The caller must pass in a Result buffer that is at
2084 least as large as the Result type.
2086 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2088 If the conversion results in an overflow or an underflow condition, then
2089 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2091 @param[in] Operand Operand to be converted to new type
2092 @param[out] Result Pointer to the result of conversion
2094 @retval RETURN_SUCCESS Successful conversion
2095 @retval RETURN_BUFFER_TOO_SMALL Overflow
2096 @retval RETURN_INVALID_PARAMETER Result is NULL
2105 RETURN_STATUS Status
;
2107 if (Result
== NULL
) {
2108 return RETURN_INVALID_PARAMETER
;
2111 if ((Operand
>= MIN_INT8
) && (Operand
<= MAX_INT8
)) {
2112 *Result
= (INT8
)Operand
;
2113 Status
= RETURN_SUCCESS
;
2115 *Result
= INT8_ERROR
;
2116 Status
= RETURN_BUFFER_TOO_SMALL
;
2123 INT64 -> CHAR8 conversion
2125 Converts the value specified by Operand to a value specified by Result type
2126 and stores the converted value into the caller allocated output buffer
2127 specified by Result. The caller must pass in a Result buffer that is at
2128 least as large as the Result type.
2130 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2132 If the conversion results in an overflow or an underflow condition, then
2133 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2135 @param[in] Operand Operand to be converted to new type
2136 @param[out] Result Pointer to the result of conversion
2138 @retval RETURN_SUCCESS Successful conversion
2139 @retval RETURN_BUFFER_TOO_SMALL Overflow
2140 @retval RETURN_INVALID_PARAMETER Result is NULL
2149 RETURN_STATUS Status
;
2151 if (Result
== NULL
) {
2152 return RETURN_INVALID_PARAMETER
;
2155 if ((Operand
>= 0) && (Operand
<= MAX_INT8
)) {
2156 *Result
= (CHAR8
)Operand
;
2157 Status
= RETURN_SUCCESS
;
2159 *Result
= CHAR8_ERROR
;
2160 Status
= RETURN_BUFFER_TOO_SMALL
;
2167 INT64 -> UINT8 conversion
2169 Converts the value specified by Operand to a value specified by Result type
2170 and stores the converted value into the caller allocated output buffer
2171 specified by Result. The caller must pass in a Result buffer that is at
2172 least as large as the Result type.
2174 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2176 If the conversion results in an overflow or an underflow condition, then
2177 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2179 @param[in] Operand Operand to be converted to new type
2180 @param[out] Result Pointer to the result of conversion
2182 @retval RETURN_SUCCESS Successful conversion
2183 @retval RETURN_BUFFER_TOO_SMALL Overflow
2184 @retval RETURN_INVALID_PARAMETER Result is NULL
2193 RETURN_STATUS Status
;
2195 if (Result
== NULL
) {
2196 return RETURN_INVALID_PARAMETER
;
2199 if ((Operand
>= 0) && (Operand
<= MAX_UINT8
)) {
2200 *Result
= (UINT8
)Operand
;
2201 Status
= RETURN_SUCCESS
;
2203 *Result
= UINT8_ERROR
;
2204 Status
= RETURN_BUFFER_TOO_SMALL
;
2211 INT64 -> INT16 conversion
2213 Converts the value specified by Operand to a value specified by Result type
2214 and stores the converted value into the caller allocated output buffer
2215 specified by Result. The caller must pass in a Result buffer that is at
2216 least as large as the Result type.
2218 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2220 If the conversion results in an overflow or an underflow condition, then
2221 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2223 @param[in] Operand Operand to be converted to new type
2224 @param[out] Result Pointer to the result of conversion
2226 @retval RETURN_SUCCESS Successful conversion
2227 @retval RETURN_BUFFER_TOO_SMALL Overflow
2228 @retval RETURN_INVALID_PARAMETER Result is NULL
2237 RETURN_STATUS Status
;
2239 if (Result
== NULL
) {
2240 return RETURN_INVALID_PARAMETER
;
2243 if ((Operand
>= MIN_INT16
) && (Operand
<= MAX_INT16
)) {
2244 *Result
= (INT16
)Operand
;
2245 Status
= RETURN_SUCCESS
;
2247 *Result
= INT16_ERROR
;
2248 Status
= RETURN_BUFFER_TOO_SMALL
;
2255 INT64 -> UINT16 conversion
2257 Converts the value specified by Operand to a value specified by Result type
2258 and stores the converted value into the caller allocated output buffer
2259 specified by Result. The caller must pass in a Result buffer that is at
2260 least as large as the Result type.
2262 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2264 If the conversion results in an overflow or an underflow condition, then
2265 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2267 @param[in] Operand Operand to be converted to new type
2268 @param[out] Result Pointer to the result of conversion
2270 @retval RETURN_SUCCESS Successful conversion
2271 @retval RETURN_BUFFER_TOO_SMALL Overflow
2272 @retval RETURN_INVALID_PARAMETER Result is NULL
2281 RETURN_STATUS Status
;
2283 if (Result
== NULL
) {
2284 return RETURN_INVALID_PARAMETER
;
2287 if ((Operand
>= 0) && (Operand
<= MAX_UINT16
)) {
2288 *Result
= (UINT16
)Operand
;
2289 Status
= RETURN_SUCCESS
;
2291 *Result
= UINT16_ERROR
;
2292 Status
= RETURN_BUFFER_TOO_SMALL
;
2299 INT64 -> INT32 conversion
2301 Converts the value specified by Operand to a value specified by Result type
2302 and stores the converted value into the caller allocated output buffer
2303 specified by Result. The caller must pass in a Result buffer that is at
2304 least as large as the Result type.
2306 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2308 If the conversion results in an overflow or an underflow condition, then
2309 Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2311 @param[in] Operand Operand to be converted to new type
2312 @param[out] Result Pointer to the result of conversion
2314 @retval RETURN_SUCCESS Successful conversion
2315 @retval RETURN_BUFFER_TOO_SMALL Overflow
2316 @retval RETURN_INVALID_PARAMETER Result is NULL
2325 RETURN_STATUS Status
;
2327 if (Result
== NULL
) {
2328 return RETURN_INVALID_PARAMETER
;
2331 if ((Operand
>= MIN_INT32
) && (Operand
<= MAX_INT32
)) {
2332 *Result
= (INT32
)Operand
;
2333 Status
= RETURN_SUCCESS
;
2335 *Result
= INT32_ERROR
;
2336 Status
= RETURN_BUFFER_TOO_SMALL
;
2343 INT64 -> UINT32 conversion
2345 Converts the value specified by Operand to a value specified by Result type
2346 and stores the converted value into the caller allocated output buffer
2347 specified by Result. The caller must pass in a Result buffer that is at
2348 least as large as the Result type.
2350 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2352 If the conversion results in an overflow or an underflow condition, then
2353 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2355 @param[in] Operand Operand to be converted to new type
2356 @param[out] Result Pointer to the result of conversion
2358 @retval RETURN_SUCCESS Successful conversion
2359 @retval RETURN_BUFFER_TOO_SMALL Overflow
2360 @retval RETURN_INVALID_PARAMETER Result is NULL
2369 RETURN_STATUS Status
;
2371 if (Result
== NULL
) {
2372 return RETURN_INVALID_PARAMETER
;
2375 if ((Operand
>= 0) && (Operand
<= MAX_UINT32
)) {
2376 *Result
= (UINT32
)Operand
;
2377 Status
= RETURN_SUCCESS
;
2379 *Result
= UINT32_ERROR
;
2380 Status
= RETURN_BUFFER_TOO_SMALL
;
2387 INT64 -> UINT64 conversion
2389 Converts the value specified by Operand to a value specified by Result type
2390 and stores the converted value into the caller allocated output buffer
2391 specified by Result. The caller must pass in a Result buffer that is at
2392 least as large as the Result type.
2394 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2396 If the conversion results in an overflow or an underflow condition, then
2397 Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2399 @param[in] Operand Operand to be converted to new type
2400 @param[out] Result Pointer to the result of conversion
2402 @retval RETURN_SUCCESS Successful conversion
2403 @retval RETURN_BUFFER_TOO_SMALL Overflow
2404 @retval RETURN_INVALID_PARAMETER Result is NULL
2413 RETURN_STATUS Status
;
2415 if (Result
== NULL
) {
2416 return RETURN_INVALID_PARAMETER
;
2420 *Result
= (UINT64
)Operand
;
2421 Status
= RETURN_SUCCESS
;
2423 *Result
= UINT64_ERROR
;
2424 Status
= RETURN_BUFFER_TOO_SMALL
;
2431 UINT64 -> INT8 conversion
2433 Converts the value specified by Operand to a value specified by Result type
2434 and stores the converted value into the caller allocated output buffer
2435 specified by Result. The caller must pass in a Result buffer that is at
2436 least as large as the Result type.
2438 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2440 If the conversion results in an overflow or an underflow condition, then
2441 Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2443 @param[in] Operand Operand to be converted to new type
2444 @param[out] Result Pointer to the result of conversion
2446 @retval RETURN_SUCCESS Successful conversion
2447 @retval RETURN_BUFFER_TOO_SMALL Overflow
2448 @retval RETURN_INVALID_PARAMETER Result is NULL
2457 RETURN_STATUS Status
;
2459 if (Result
== NULL
) {
2460 return RETURN_INVALID_PARAMETER
;
2463 if (Operand
<= MAX_INT8
) {
2464 *Result
= (INT8
)Operand
;
2465 Status
= RETURN_SUCCESS
;
2467 *Result
= INT8_ERROR
;
2468 Status
= RETURN_BUFFER_TOO_SMALL
;
2475 UINT64 -> CHAR8 conversion
2477 Converts the value specified by Operand to a value specified by Result type
2478 and stores the converted value into the caller allocated output buffer
2479 specified by Result. The caller must pass in a Result buffer that is at
2480 least as large as the Result type.
2482 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2484 If the conversion results in an overflow or an underflow condition, then
2485 Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2487 @param[in] Operand Operand to be converted to new type
2488 @param[out] Result Pointer to the result of conversion
2490 @retval RETURN_SUCCESS Successful conversion
2491 @retval RETURN_BUFFER_TOO_SMALL Overflow
2492 @retval RETURN_INVALID_PARAMETER Result is NULL
2501 RETURN_STATUS Status
;
2503 if (Result
== NULL
) {
2504 return RETURN_INVALID_PARAMETER
;
2507 if (Operand
<= MAX_INT8
) {
2508 *Result
= (INT8
)Operand
;
2509 Status
= RETURN_SUCCESS
;
2511 *Result
= CHAR8_ERROR
;
2512 Status
= RETURN_BUFFER_TOO_SMALL
;
2519 UINT64 -> UINT8 conversion
2521 Converts the value specified by Operand to a value specified by Result type
2522 and stores the converted value into the caller allocated output buffer
2523 specified by Result. The caller must pass in a Result buffer that is at
2524 least as large as the Result type.
2526 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2528 If the conversion results in an overflow or an underflow condition, then
2529 Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2531 @param[in] Operand Operand to be converted to new type
2532 @param[out] Result Pointer to the result of conversion
2534 @retval RETURN_SUCCESS Successful conversion
2535 @retval RETURN_BUFFER_TOO_SMALL Overflow
2536 @retval RETURN_INVALID_PARAMETER Result is NULL
2545 RETURN_STATUS Status
;
2547 if (Result
== NULL
) {
2548 return RETURN_INVALID_PARAMETER
;
2551 if (Operand
<= MAX_UINT8
) {
2552 *Result
= (UINT8
)Operand
;
2553 Status
= RETURN_SUCCESS
;
2555 *Result
= UINT8_ERROR
;
2556 Status
= RETURN_BUFFER_TOO_SMALL
;
2563 UINT64 -> INT16 conversion
2565 Converts the value specified by Operand to a value specified by Result type
2566 and stores the converted value into the caller allocated output buffer
2567 specified by Result. The caller must pass in a Result buffer that is at
2568 least as large as the Result type.
2570 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2572 If the conversion results in an overflow or an underflow condition, then
2573 Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2575 @param[in] Operand Operand to be converted to new type
2576 @param[out] Result Pointer to the result of conversion
2578 @retval RETURN_SUCCESS Successful conversion
2579 @retval RETURN_BUFFER_TOO_SMALL Overflow
2580 @retval RETURN_INVALID_PARAMETER Result is NULL
2589 RETURN_STATUS Status
;
2591 if (Result
== NULL
) {
2592 return RETURN_INVALID_PARAMETER
;
2595 if (Operand
<= MAX_INT16
) {
2596 *Result
= (INT16
)Operand
;
2597 Status
= RETURN_SUCCESS
;
2599 *Result
= INT16_ERROR
;
2600 Status
= RETURN_BUFFER_TOO_SMALL
;
2607 UINT64 -> UINT16 conversion
2609 Converts the value specified by Operand to a value specified by Result type
2610 and stores the converted value into the caller allocated output buffer
2611 specified by Result. The caller must pass in a Result buffer that is at
2612 least as large as the Result type.
2614 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2616 If the conversion results in an overflow or an underflow condition, then
2617 Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2619 @param[in] Operand Operand to be converted to new type
2620 @param[out] Result Pointer to the result of conversion
2622 @retval RETURN_SUCCESS Successful conversion
2623 @retval RETURN_BUFFER_TOO_SMALL Overflow
2624 @retval RETURN_INVALID_PARAMETER Result is NULL
2628 SafeUint64ToUint16 (
2633 RETURN_STATUS Status
;
2635 if (Result
== NULL
) {
2636 return RETURN_INVALID_PARAMETER
;
2639 if (Operand
<= MAX_UINT16
) {
2640 *Result
= (UINT16
)Operand
;
2641 Status
= RETURN_SUCCESS
;
2643 *Result
= UINT16_ERROR
;
2644 Status
= RETURN_BUFFER_TOO_SMALL
;
2651 UINT64 -> INT32 conversion
2653 Converts the value specified by Operand to a value specified by Result type
2654 and stores the converted value into the caller allocated output buffer
2655 specified by Result. The caller must pass in a Result buffer that is at
2656 least as large as the Result type.
2658 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2660 If the conversion results in an overflow or an underflow condition, then
2661 Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2663 @param[in] Operand Operand to be converted to new type
2664 @param[out] Result Pointer to the result of conversion
2666 @retval RETURN_SUCCESS Successful conversion
2667 @retval RETURN_BUFFER_TOO_SMALL Overflow
2668 @retval RETURN_INVALID_PARAMETER Result is NULL
2677 RETURN_STATUS Status
;
2679 if (Result
== NULL
) {
2680 return RETURN_INVALID_PARAMETER
;
2683 if (Operand
<= MAX_INT32
) {
2684 *Result
= (INT32
)Operand
;
2685 Status
= RETURN_SUCCESS
;
2687 *Result
= INT32_ERROR
;
2688 Status
= RETURN_BUFFER_TOO_SMALL
;
2695 UINT64 -> UINT32 conversion
2697 Converts the value specified by Operand to a value specified by Result type
2698 and stores the converted value into the caller allocated output buffer
2699 specified by Result. The caller must pass in a Result buffer that is at
2700 least as large as the Result type.
2702 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2704 If the conversion results in an overflow or an underflow condition, then
2705 Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2707 @param[in] Operand Operand to be converted to new type
2708 @param[out] Result Pointer to the result of conversion
2710 @retval RETURN_SUCCESS Successful conversion
2711 @retval RETURN_BUFFER_TOO_SMALL Overflow
2712 @retval RETURN_INVALID_PARAMETER Result is NULL
2716 SafeUint64ToUint32 (
2721 RETURN_STATUS Status
;
2723 if (Result
== NULL
) {
2724 return RETURN_INVALID_PARAMETER
;
2727 if (Operand
<= MAX_UINT32
) {
2728 *Result
= (UINT32
)Operand
;
2729 Status
= RETURN_SUCCESS
;
2731 *Result
= UINT32_ERROR
;
2732 Status
= RETURN_BUFFER_TOO_SMALL
;
2739 UINT64 -> INTN conversion
2741 Converts the value specified by Operand to a value specified by Result type
2742 and stores the converted value into the caller allocated output buffer
2743 specified by Result. The caller must pass in a Result buffer that is at
2744 least as large as the Result type.
2746 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2748 If the conversion results in an overflow or an underflow condition, then
2749 Result is set to INTN_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2751 @param[in] Operand Operand to be converted to new type
2752 @param[out] Result Pointer to the result of conversion
2754 @retval RETURN_SUCCESS Successful conversion
2755 @retval RETURN_BUFFER_TOO_SMALL Overflow
2756 @retval RETURN_INVALID_PARAMETER Result is NULL
2765 RETURN_STATUS Status
;
2767 if (Result
== NULL
) {
2768 return RETURN_INVALID_PARAMETER
;
2771 if (Operand
<= MAX_INTN
) {
2772 *Result
= (INTN
)Operand
;
2773 Status
= RETURN_SUCCESS
;
2775 *Result
= INTN_ERROR
;
2776 Status
= RETURN_BUFFER_TOO_SMALL
;
2783 UINT64 -> INT64 conversion
2785 Converts the value specified by Operand to a value specified by Result type
2786 and stores the converted value into the caller allocated output buffer
2787 specified by Result. The caller must pass in a Result buffer that is at
2788 least as large as the Result type.
2790 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2792 If the conversion results in an overflow or an underflow condition, then
2793 Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2795 @param[in] Operand Operand to be converted to new type
2796 @param[out] Result Pointer to the result of conversion
2798 @retval RETURN_SUCCESS Successful conversion
2799 @retval RETURN_BUFFER_TOO_SMALL Overflow
2800 @retval RETURN_INVALID_PARAMETER Result is NULL
2809 RETURN_STATUS Status
;
2811 if (Result
== NULL
) {
2812 return RETURN_INVALID_PARAMETER
;
2815 if (Operand
<= MAX_INT64
) {
2816 *Result
= (INT64
)Operand
;
2817 Status
= RETURN_SUCCESS
;
2819 *Result
= INT64_ERROR
;
2820 Status
= RETURN_BUFFER_TOO_SMALL
;
2827 // Addition functions
2833 Performs the requested operation using the input parameters into a value
2834 specified by Result type and stores the converted value into the caller
2835 allocated output buffer specified by Result. The caller must pass in a
2836 Result buffer that is at least as large as the Result type.
2838 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2840 If the requested operation results in an overflow or an underflow condition,
2841 then Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2843 @param[in] Augend A number to which addend will be added
2844 @param[in] Addend A number to be added to another
2845 @param[out] Result Pointer to the result of addition
2847 @retval RETURN_SUCCESS Successful addition
2848 @retval RETURN_BUFFER_TOO_SMALL Overflow
2849 @retval RETURN_INVALID_PARAMETER Result is NULL
2859 RETURN_STATUS Status
;
2861 if (Result
== NULL
) {
2862 return RETURN_INVALID_PARAMETER
;
2865 if (((UINT8
)(Augend
+ Addend
)) >= Augend
) {
2866 *Result
= (UINT8
)(Augend
+ Addend
);
2867 Status
= RETURN_SUCCESS
;
2869 *Result
= UINT8_ERROR
;
2870 Status
= RETURN_BUFFER_TOO_SMALL
;
2879 Performs the requested operation using the input parameters into a value
2880 specified by Result type and stores the converted value into the caller
2881 allocated output buffer specified by Result. The caller must pass in a
2882 Result buffer that is at least as large as the Result type.
2884 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2886 If the requested operation results in an overflow or an underflow condition,
2887 then Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2889 @param[in] Augend A number to which addend will be added
2890 @param[in] Addend A number to be added to another
2891 @param[out] Result Pointer to the result of addition
2893 @retval RETURN_SUCCESS Successful addition
2894 @retval RETURN_BUFFER_TOO_SMALL Overflow
2895 @retval RETURN_INVALID_PARAMETER Result is NULL
2905 RETURN_STATUS Status
;
2907 if (Result
== NULL
) {
2908 return RETURN_INVALID_PARAMETER
;
2911 if (((UINT16
)(Augend
+ Addend
)) >= Augend
) {
2912 *Result
= (UINT16
)(Augend
+ Addend
);
2913 Status
= RETURN_SUCCESS
;
2915 *Result
= UINT16_ERROR
;
2916 Status
= RETURN_BUFFER_TOO_SMALL
;
2925 Performs the requested operation using the input parameters into a value
2926 specified by Result type and stores the converted value into the caller
2927 allocated output buffer specified by Result. The caller must pass in a
2928 Result buffer that is at least as large as the Result type.
2930 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2932 If the requested operation results in an overflow or an underflow condition,
2933 then Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2935 @param[in] Augend A number to which addend will be added
2936 @param[in] Addend A number to be added to another
2937 @param[out] Result Pointer to the result of addition
2939 @retval RETURN_SUCCESS Successful addition
2940 @retval RETURN_BUFFER_TOO_SMALL Overflow
2941 @retval RETURN_INVALID_PARAMETER Result is NULL
2951 RETURN_STATUS Status
;
2953 if (Result
== NULL
) {
2954 return RETURN_INVALID_PARAMETER
;
2957 if ((Augend
+ Addend
) >= Augend
) {
2958 *Result
= (Augend
+ Addend
);
2959 Status
= RETURN_SUCCESS
;
2961 *Result
= UINT32_ERROR
;
2962 Status
= RETURN_BUFFER_TOO_SMALL
;
2971 Performs the requested operation using the input parameters into a value
2972 specified by Result type and stores the converted value into the caller
2973 allocated output buffer specified by Result. The caller must pass in a
2974 Result buffer that is at least as large as the Result type.
2976 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
2978 If the requested operation results in an overflow or an underflow condition,
2979 then Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
2981 @param[in] Augend A number to which addend will be added
2982 @param[in] Addend A number to be added to another
2983 @param[out] Result Pointer to the result of addition
2985 @retval RETURN_SUCCESS Successful addition
2986 @retval RETURN_BUFFER_TOO_SMALL Overflow
2987 @retval RETURN_INVALID_PARAMETER Result is NULL
2997 RETURN_STATUS Status
;
2999 if (Result
== NULL
) {
3000 return RETURN_INVALID_PARAMETER
;
3003 if ((Augend
+ Addend
) >= Augend
) {
3004 *Result
= (Augend
+ Addend
);
3005 Status
= RETURN_SUCCESS
;
3007 *Result
= UINT64_ERROR
;
3008 Status
= RETURN_BUFFER_TOO_SMALL
;
3015 // Subtraction functions
3021 Performs the requested operation using the input parameters into a value
3022 specified by Result type and stores the converted value into the caller
3023 allocated output buffer specified by Result. The caller must pass in a
3024 Result buffer that is at least as large as the Result type.
3026 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3028 If the requested operation results in an overflow or an underflow condition,
3029 then Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3031 @param[in] Minuend A number from which another is to be subtracted.
3032 @param[in] Subtrahend A number to be subtracted from another
3033 @param[out] Result Pointer to the result of subtraction
3035 @retval RETURN_SUCCESS Successful subtraction
3036 @retval RETURN_BUFFER_TOO_SMALL Underflow
3037 @retval RETURN_INVALID_PARAMETER Result is NULL
3043 IN UINT8 Subtrahend
,
3047 RETURN_STATUS Status
;
3049 if (Result
== NULL
) {
3050 return RETURN_INVALID_PARAMETER
;
3053 if (Minuend
>= Subtrahend
) {
3054 *Result
= (UINT8
)(Minuend
- Subtrahend
);
3055 Status
= RETURN_SUCCESS
;
3057 *Result
= UINT8_ERROR
;
3058 Status
= RETURN_BUFFER_TOO_SMALL
;
3067 Performs the requested operation using the input parameters into a value
3068 specified by Result type and stores the converted value into the caller
3069 allocated output buffer specified by Result. The caller must pass in a
3070 Result buffer that is at least as large as the Result type.
3072 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3074 If the requested operation results in an overflow or an underflow condition,
3075 then Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3077 @param[in] Minuend A number from which another is to be subtracted.
3078 @param[in] Subtrahend A number to be subtracted from another
3079 @param[out] Result Pointer to the result of subtraction
3081 @retval RETURN_SUCCESS Successful subtraction
3082 @retval RETURN_BUFFER_TOO_SMALL Underflow
3083 @retval RETURN_INVALID_PARAMETER Result is NULL
3089 IN UINT16 Subtrahend
,
3093 RETURN_STATUS Status
;
3095 if (Result
== NULL
) {
3096 return RETURN_INVALID_PARAMETER
;
3099 if (Minuend
>= Subtrahend
) {
3100 *Result
= (UINT16
)(Minuend
- Subtrahend
);
3101 Status
= RETURN_SUCCESS
;
3103 *Result
= UINT16_ERROR
;
3104 Status
= RETURN_BUFFER_TOO_SMALL
;
3113 Performs the requested operation using the input parameters into a value
3114 specified by Result type and stores the converted value into the caller
3115 allocated output buffer specified by Result. The caller must pass in a
3116 Result buffer that is at least as large as the Result type.
3118 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3120 If the requested operation results in an overflow or an underflow condition,
3121 then Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3123 @param[in] Minuend A number from which another is to be subtracted.
3124 @param[in] Subtrahend A number to be subtracted from another
3125 @param[out] Result Pointer to the result of subtraction
3127 @retval RETURN_SUCCESS Successful subtraction
3128 @retval RETURN_BUFFER_TOO_SMALL Underflow
3129 @retval RETURN_INVALID_PARAMETER Result is NULL
3135 IN UINT32 Subtrahend
,
3139 RETURN_STATUS Status
;
3141 if (Result
== NULL
) {
3142 return RETURN_INVALID_PARAMETER
;
3145 if (Minuend
>= Subtrahend
) {
3146 *Result
= (Minuend
- Subtrahend
);
3147 Status
= RETURN_SUCCESS
;
3149 *Result
= UINT32_ERROR
;
3150 Status
= RETURN_BUFFER_TOO_SMALL
;
3159 Performs the requested operation using the input parameters into a value
3160 specified by Result type and stores the converted value into the caller
3161 allocated output buffer specified by Result. The caller must pass in a
3162 Result buffer that is at least as large as the Result type.
3164 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3166 If the requested operation results in an overflow or an underflow condition,
3167 then Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3169 @param[in] Minuend A number from which another is to be subtracted.
3170 @param[in] Subtrahend A number to be subtracted from another
3171 @param[out] Result Pointer to the result of subtraction
3173 @retval RETURN_SUCCESS Successful subtraction
3174 @retval RETURN_BUFFER_TOO_SMALL Underflow
3175 @retval RETURN_INVALID_PARAMETER Result is NULL
3181 IN UINT64 Subtrahend
,
3185 RETURN_STATUS Status
;
3187 if (Result
== NULL
) {
3188 return RETURN_INVALID_PARAMETER
;
3191 if (Minuend
>= Subtrahend
) {
3192 *Result
= (Minuend
- Subtrahend
);
3193 Status
= RETURN_SUCCESS
;
3195 *Result
= UINT64_ERROR
;
3196 Status
= RETURN_BUFFER_TOO_SMALL
;
3203 // Multiplication functions
3207 UINT8 multiplication
3209 Performs the requested operation using the input parameters into a value
3210 specified by Result type and stores the converted value into the caller
3211 allocated output buffer specified by Result. The caller must pass in a
3212 Result buffer that is at least as large as the Result type.
3214 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3216 If the requested operation results in an overflow or an underflow condition,
3217 then Result is set to UINT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3219 @param[in] Multiplicand A number that is to be multiplied by another
3220 @param[in] Multiplier A number by which the multiplicand is to be multiplied
3221 @param[out] Result Pointer to the result of multiplication
3223 @retval RETURN_SUCCESS Successful multiplication
3224 @retval RETURN_BUFFER_TOO_SMALL Overflow
3225 @retval RETURN_INVALID_PARAMETER Result is NULL
3230 IN UINT8 Multiplicand
,
3231 IN UINT8 Multiplier
,
3235 UINT32 IntermediateResult
;
3237 IntermediateResult
= ((UINT32
)Multiplicand
) *((UINT32
)Multiplier
);
3239 return SafeUint32ToUint8 (IntermediateResult
, Result
);
3243 UINT16 multiplication
3245 Performs the requested operation using the input parameters into a value
3246 specified by Result type and stores the converted value into the caller
3247 allocated output buffer specified by Result. The caller must pass in a
3248 Result buffer that is at least as large as the Result type.
3250 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3252 If the requested operation results in an overflow or an underflow condition,
3253 then Result is set to UINT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3255 @param[in] Multiplicand A number that is to be multiplied by another
3256 @param[in] Multiplier A number by which the multiplicand is to be multiplied
3257 @param[out] Result Pointer to the result of multiplication
3259 @retval RETURN_SUCCESS Successful multiplication
3260 @retval RETURN_BUFFER_TOO_SMALL Overflow
3261 @retval RETURN_INVALID_PARAMETER Result is NULL
3266 IN UINT16 Multiplicand
,
3267 IN UINT16 Multiplier
,
3271 UINT32 IntermediateResult
;
3273 IntermediateResult
= ((UINT32
)Multiplicand
) *((UINT32
)Multiplier
);
3275 return SafeUint32ToUint16 (IntermediateResult
, Result
);
3279 UINT32 multiplication
3281 Performs the requested operation using the input parameters into a value
3282 specified by Result type and stores the converted value into the caller
3283 allocated output buffer specified by Result. The caller must pass in a
3284 Result buffer that is at least as large as the Result type.
3286 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3288 If the requested operation results in an overflow or an underflow condition,
3289 then Result is set to UINT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3291 @param[in] Multiplicand A number that is to be multiplied by another
3292 @param[in] Multiplier A number by which the multiplicand is to be multiplied
3293 @param[out] Result Pointer to the result of multiplication
3295 @retval RETURN_SUCCESS Successful multiplication
3296 @retval RETURN_BUFFER_TOO_SMALL Overflow
3297 @retval RETURN_INVALID_PARAMETER Result is NULL
3302 IN UINT32 Multiplicand
,
3303 IN UINT32 Multiplier
,
3307 UINT64 IntermediateResult
;
3309 IntermediateResult
= ((UINT64
) Multiplicand
) *((UINT64
) Multiplier
);
3311 return SafeUint64ToUint32 (IntermediateResult
, Result
);
3315 UINT64 multiplication
3317 Performs the requested operation using the input parameters into a value
3318 specified by Result type and stores the converted value into the caller
3319 allocated output buffer specified by Result. The caller must pass in a
3320 Result buffer that is at least as large as the Result type.
3322 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3324 If the requested operation results in an overflow or an underflow condition,
3325 then Result is set to UINT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3327 @param[in] Multiplicand A number that is to be multiplied by another
3328 @param[in] Multiplier A number by which the multiplicand is to be multiplied
3329 @param[out] Result Pointer to the result of multiplication
3331 @retval RETURN_SUCCESS Successful multiplication
3332 @retval RETURN_BUFFER_TOO_SMALL Overflow
3333 @retval RETURN_INVALID_PARAMETER Result is NULL
3338 IN UINT64 Multiplicand
,
3339 IN UINT64 Multiplier
,
3343 RETURN_STATUS Status
;
3351 UINT64 UnsignedResult
;
3353 if (Result
== NULL
) {
3354 return RETURN_INVALID_PARAMETER
;
3361 Status
= RETURN_BUFFER_TOO_SMALL
;
3364 // 64x64 into 128 is like 32.32 x 32.32.
3366 // a.b * c.d = a*(c.d) + .b*(c.d) = a*c + a*.d + .b*c + .b*.d
3367 // back in non-decimal notation where A=a*2^32 and C=c*2^32:
3368 // A*C + A*d + b*C + b*d
3369 // So there are four components to add together.
3370 // result = (a*c*2^64) + (a*d*2^32) + (b*c*2^32) + (b*d)
3372 // a * c must be 0 or there would be bits in the high 64-bits
3373 // a * d must be less than 2^32 or there would be bits in the high 64-bits
3374 // b * c must be less than 2^32 or there would be bits in the high 64-bits
3375 // then there must be no overflow of the resulting values summed up.
3377 DwordA
= (UINT32
)RShiftU64 (Multiplicand
, 32);
3378 DwordC
= (UINT32
)RShiftU64 (Multiplier
, 32);
3381 // common case -- if high dwords are both zero, no chance for overflow
3383 if ((DwordA
== 0) && (DwordC
== 0)) {
3384 DwordB
= (UINT32
)Multiplicand
;
3385 DwordD
= (UINT32
)Multiplier
;
3387 *Result
= (((UINT64
)DwordB
) *(UINT64
)DwordD
);
3388 Status
= RETURN_SUCCESS
;
3391 // a * c must be 0 or there would be bits set in the high 64-bits
3393 if ((DwordA
== 0) ||
3395 DwordD
= (UINT32
)Multiplier
;
3398 // a * d must be less than 2^32 or there would be bits set in the high 64-bits
3400 ProductAD
= (((UINT64
)DwordA
) *(UINT64
)DwordD
);
3401 if ((ProductAD
& 0xffffffff00000000) == 0) {
3402 DwordB
= (UINT32
)Multiplicand
;
3405 // b * c must be less than 2^32 or there would be bits set in the high 64-bits
3407 ProductBC
= (((UINT64
)DwordB
) *(UINT64
)DwordC
);
3408 if ((ProductBC
& 0xffffffff00000000) == 0) {
3410 // now sum them all up checking for overflow.
3411 // shifting is safe because we already checked for overflow above
3413 if (!RETURN_ERROR (SafeUint64Add (LShiftU64 (ProductBC
, 32), LShiftU64 (ProductAD
, 32), &UnsignedResult
))) {
3417 ProductBD
= (((UINT64
)DwordB
) *(UINT64
)DwordD
);
3419 if (!RETURN_ERROR (SafeUint64Add (UnsignedResult
, ProductBD
, &UnsignedResult
))) {
3420 *Result
= UnsignedResult
;
3421 Status
= RETURN_SUCCESS
;
3429 if (RETURN_ERROR (Status
)) {
3430 *Result
= UINT64_ERROR
;
3436 // Signed operations
3438 // Strongly consider using unsigned numbers.
3440 // Signed numbers are often used where unsigned numbers should be used.
3441 // For example file sizes and array indices should always be unsigned.
3442 // Subtracting a larger positive signed number from a smaller positive
3443 // signed number with SafeInt32Sub will succeed, producing a negative number,
3444 // that then must not be used as an array index (but can occasionally be
3445 // used as a pointer index.) Similarly for adding a larger magnitude
3446 // negative number to a smaller magnitude positive number.
3448 // This library does not protect you from such errors. It tells you if your
3449 // integer operations overflowed, not if you are doing the right thing
3450 // with your non-overflowed integers.
3452 // Likewise you can overflow a buffer with a non-overflowed unsigned index.
3456 // Signed addition functions
3462 Performs the requested operation using the input parameters into a value
3463 specified by Result type and stores the converted value into the caller
3464 allocated output buffer specified by Result. The caller must pass in a
3465 Result buffer that is at least as large as the Result type.
3467 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3469 If the requested operation results in an overflow or an underflow condition,
3470 then Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3472 @param[in] Augend A number to which addend will be added
3473 @param[in] Addend A number to be added to another
3474 @param[out] Result Pointer to the result of addition
3476 @retval RETURN_SUCCESS Successful addition
3477 @retval RETURN_BUFFER_TOO_SMALL Overflow
3478 @retval RETURN_INVALID_PARAMETER Result is NULL
3488 return SafeInt32ToInt8 (((INT32
)Augend
) + ((INT32
)Addend
), Result
);
3494 Performs the requested operation using the input parameters into a value
3495 specified by Result type and stores the converted value into the caller
3496 allocated output buffer specified by Result. The caller must pass in a
3497 Result buffer that is at least as large as the Result type.
3499 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3501 If the requested operation results in an overflow or an underflow condition,
3502 then Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3504 @param[in] Augend A number to which addend will be added
3505 @param[in] Addend A number to be added to another
3506 @param[out] Result Pointer to the result of addition
3508 @retval RETURN_SUCCESS Successful addition
3509 @retval RETURN_BUFFER_TOO_SMALL Overflow
3510 @retval RETURN_INVALID_PARAMETER Result is NULL
3523 if (Result
== NULL
) {
3524 return RETURN_INVALID_PARAMETER
;
3527 Augend32
= (INT32
)Augend
;
3528 Addend32
= (INT32
)Addend
;
3529 if (Augend32
< 0 || Augend32
> MAX_INT8
) {
3530 *Result
= CHAR8_ERROR
;
3531 return RETURN_BUFFER_TOO_SMALL
;
3533 if (Addend32
< 0 || Addend32
> MAX_INT8
) {
3534 *Result
= CHAR8_ERROR
;
3535 return RETURN_BUFFER_TOO_SMALL
;
3538 return SafeInt32ToChar8 (Augend32
+ Addend32
, Result
);
3544 Performs the requested operation using the input parameters into a value
3545 specified by Result type and stores the converted value into the caller
3546 allocated output buffer specified by Result. The caller must pass in a
3547 Result buffer that is at least as large as the Result type.
3549 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3551 If the requested operation results in an overflow or an underflow condition,
3552 then Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3554 @param[in] Augend A number to which addend will be added
3555 @param[in] Addend A number to be added to another
3556 @param[out] Result Pointer to the result of addition
3558 @retval RETURN_SUCCESS Successful addition
3559 @retval RETURN_BUFFER_TOO_SMALL Overflow
3560 @retval RETURN_INVALID_PARAMETER Result is NULL
3570 return SafeInt32ToInt16 (((INT32
)Augend
) + ((INT32
)Addend
), Result
);
3576 Performs the requested operation using the input parameters into a value
3577 specified by Result type and stores the converted value into the caller
3578 allocated output buffer specified by Result. The caller must pass in a
3579 Result buffer that is at least as large as the Result type.
3581 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3583 If the requested operation results in an overflow or an underflow condition,
3584 then Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3586 @param[in] Augend A number to which addend will be added
3587 @param[in] Addend A number to be added to another
3588 @param[out] Result Pointer to the result of addition
3590 @retval RETURN_SUCCESS Successful addition
3591 @retval RETURN_BUFFER_TOO_SMALL Overflow
3592 @retval RETURN_INVALID_PARAMETER Result is NULL
3602 return SafeInt64ToInt32 (((INT64
)Augend
) + ((INT64
)Addend
), Result
);
3608 Performs the requested operation using the input parameters into a value
3609 specified by Result type and stores the converted value into the caller
3610 allocated output buffer specified by Result. The caller must pass in a
3611 Result buffer that is at least as large as the Result type.
3613 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3615 If the requested operation results in an overflow or an underflow condition,
3616 then Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3618 @param[in] Augend A number to which addend will be added
3619 @param[in] Addend A number to be added to another
3620 @param[out] Result Pointer to the result of addition
3622 @retval RETURN_SUCCESS Successful addition
3623 @retval RETURN_BUFFER_TOO_SMALL Overflow
3624 @retval RETURN_INVALID_PARAMETER Result is NULL
3634 RETURN_STATUS Status
;
3636 if (Result
== NULL
) {
3637 return RETURN_INVALID_PARAMETER
;
3641 // * An Addend of zero can never cause underflow or overflow.
3643 // * A positive Addend can only cause overflow. The overflow condition is
3645 // (Augend + Addend) > MAX_INT64
3647 // Subtracting Addend from both sides yields
3649 // Augend > (MAX_INT64 - Addend)
3651 // This condition can be coded directly in C because the RHS will neither
3652 // underflow nor overflow. That is due to the starting condition:
3654 // 0 < Addend <= MAX_INT64
3656 // Multiplying all three sides by (-1) yields
3658 // 0 > (-Addend) >= (-MAX_INT64)
3660 // Adding MAX_INT64 to all three sides yields
3662 // MAX_INT64 > (MAX_INT64 - Addend) >= 0
3664 // * A negative Addend can only cause underflow. The underflow condition is
3666 // (Augend + Addend) < MIN_INT64
3668 // Subtracting Addend from both sides yields
3670 // Augend < (MIN_INT64 - Addend)
3672 // This condition can be coded directly in C because the RHS will neither
3673 // underflow nor overflow. That is due to the starting condition:
3675 // MIN_INT64 <= Addend < 0
3677 // Multiplying all three sides by (-1) yields
3679 // (-MIN_INT64) >= (-Addend) > 0
3681 // Adding MIN_INT64 to all three sides yields
3683 // 0 >= (MIN_INT64 - Addend) > MIN_INT64
3685 if (((Addend
> 0) && (Augend
> (MAX_INT64
- Addend
))) ||
3686 ((Addend
< 0) && (Augend
< (MIN_INT64
- Addend
)))) {
3687 *Result
= INT64_ERROR
;
3688 Status
= RETURN_BUFFER_TOO_SMALL
;
3690 *Result
= Augend
+ Addend
;
3691 Status
= RETURN_SUCCESS
;
3698 // Signed subtraction functions
3704 Performs the requested operation using the input parameters into a value
3705 specified by Result type and stores the converted value into the caller
3706 allocated output buffer specified by Result. The caller must pass in a
3707 Result buffer that is at least as large as the Result type.
3709 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3711 If the requested operation results in an overflow or an underflow condition,
3712 then Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3714 @param[in] Minuend A number from which another is to be subtracted.
3715 @param[in] Subtrahend A number to be subtracted from another
3716 @param[out] Result Pointer to the result of subtraction
3718 @retval RETURN_SUCCESS Successful subtraction
3719 @retval RETURN_BUFFER_TOO_SMALL Underflow
3720 @retval RETURN_INVALID_PARAMETER Result is NULL
3730 return SafeInt32ToInt8 (((INT32
)Minuend
) - ((INT32
)Subtrahend
), Result
);
3736 Performs the requested operation using the input parameters into a value
3737 specified by Result type and stores the converted value into the caller
3738 allocated output buffer specified by Result. The caller must pass in a
3739 Result buffer that is at least as large as the Result type.
3741 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3743 If the requested operation results in an overflow or an underflow condition,
3744 then Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3746 @param[in] Minuend A number from which another is to be subtracted.
3747 @param[in] Subtrahend A number to be subtracted from another
3748 @param[out] Result Pointer to the result of subtraction
3750 @retval RETURN_SUCCESS Successful subtraction
3751 @retval RETURN_BUFFER_TOO_SMALL Underflow
3752 @retval RETURN_INVALID_PARAMETER Result is NULL
3758 IN CHAR8 Subtrahend
,
3765 if (Result
== NULL
) {
3766 return RETURN_INVALID_PARAMETER
;
3769 Minuend32
= (INT32
)Minuend
;
3770 Subtrahend32
= (INT32
)Subtrahend
;
3771 if (Minuend32
< 0 || Minuend32
> MAX_INT8
) {
3772 *Result
= CHAR8_ERROR
;
3773 return RETURN_BUFFER_TOO_SMALL
;
3775 if (Subtrahend32
< 0 || Subtrahend32
> MAX_INT8
) {
3776 *Result
= CHAR8_ERROR
;
3777 return RETURN_BUFFER_TOO_SMALL
;
3780 return SafeInt32ToChar8 (Minuend32
- Subtrahend32
, Result
);
3786 Performs the requested operation using the input parameters into a value
3787 specified by Result type and stores the converted value into the caller
3788 allocated output buffer specified by Result. The caller must pass in a
3789 Result buffer that is at least as large as the Result type.
3791 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3793 If the requested operation results in an overflow or an underflow condition,
3794 then Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3796 @param[in] Minuend A number from which another is to be subtracted.
3797 @param[in] Subtrahend A number to be subtracted from another
3798 @param[out] Result Pointer to the result of subtraction
3800 @retval RETURN_SUCCESS Successful subtraction
3801 @retval RETURN_BUFFER_TOO_SMALL Underflow
3802 @retval RETURN_INVALID_PARAMETER Result is NULL
3808 IN INT16 Subtrahend
,
3812 return SafeInt32ToInt16 (((INT32
)Minuend
) - ((INT32
)Subtrahend
), Result
);
3818 Performs the requested operation using the input parameters into a value
3819 specified by Result type and stores the converted value into the caller
3820 allocated output buffer specified by Result. The caller must pass in a
3821 Result buffer that is at least as large as the Result type.
3823 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3825 If the requested operation results in an overflow or an underflow condition,
3826 then Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3828 @param[in] Minuend A number from which another is to be subtracted.
3829 @param[in] Subtrahend A number to be subtracted from another
3830 @param[out] Result Pointer to the result of subtraction
3832 @retval RETURN_SUCCESS Successful subtraction
3833 @retval RETURN_BUFFER_TOO_SMALL Underflow
3834 @retval RETURN_INVALID_PARAMETER Result is NULL
3840 IN INT32 Subtrahend
,
3844 return SafeInt64ToInt32 (((INT64
)Minuend
) - ((INT64
)Subtrahend
), Result
);
3850 Performs the requested operation using the input parameters into a value
3851 specified by Result type and stores the converted value into the caller
3852 allocated output buffer specified by Result. The caller must pass in a
3853 Result buffer that is at least as large as the Result type.
3855 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3857 If the requested operation results in an overflow or an underflow condition,
3858 then Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3860 @param[in] Minuend A number from which another is to be subtracted.
3861 @param[in] Subtrahend A number to be subtracted from another
3862 @param[out] Result Pointer to the result of subtraction
3864 @retval RETURN_SUCCESS Successful subtraction
3865 @retval RETURN_BUFFER_TOO_SMALL Underflow
3866 @retval RETURN_INVALID_PARAMETER Result is NULL
3872 IN INT64 Subtrahend
,
3876 RETURN_STATUS Status
;
3878 if (Result
== NULL
) {
3879 return RETURN_INVALID_PARAMETER
;
3883 // * A Subtrahend of zero can never cause underflow or overflow.
3885 // * A positive Subtrahend can only cause underflow. The underflow condition
3888 // (Minuend - Subtrahend) < MIN_INT64
3890 // Adding Subtrahend to both sides yields
3892 // Minuend < (MIN_INT64 + Subtrahend)
3894 // This condition can be coded directly in C because the RHS will neither
3895 // underflow nor overflow. That is due to the starting condition:
3897 // 0 < Subtrahend <= MAX_INT64
3899 // Adding MIN_INT64 to all three sides yields
3901 // MIN_INT64 < (MIN_INT64 + Subtrahend) <= (MIN_INT64 + MAX_INT64) = -1
3903 // * A negative Subtrahend can only cause overflow. The overflow condition is
3905 // (Minuend - Subtrahend) > MAX_INT64
3907 // Adding Subtrahend to both sides yields
3909 // Minuend > (MAX_INT64 + Subtrahend)
3911 // This condition can be coded directly in C because the RHS will neither
3912 // underflow nor overflow. That is due to the starting condition:
3914 // MIN_INT64 <= Subtrahend < 0
3916 // Adding MAX_INT64 to all three sides yields
3918 // -1 = (MAX_INT64 + MIN_INT64) <= (MAX_INT64 + Subtrahend) < MAX_INT64
3920 if (((Subtrahend
> 0) && (Minuend
< (MIN_INT64
+ Subtrahend
))) ||
3921 ((Subtrahend
< 0) && (Minuend
> (MAX_INT64
+ Subtrahend
)))) {
3922 *Result
= INT64_ERROR
;
3923 Status
= RETURN_BUFFER_TOO_SMALL
;
3925 *Result
= Minuend
- Subtrahend
;
3926 Status
= RETURN_SUCCESS
;
3933 // Signed multiplication functions
3939 Performs the requested operation using the input parameters into a value
3940 specified by Result type and stores the converted value into the caller
3941 allocated output buffer specified by Result. The caller must pass in a
3942 Result buffer that is at least as large as the Result type.
3944 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3946 If the requested operation results in an overflow or an underflow condition,
3947 then Result is set to INT8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3949 @param[in] Multiplicand A number that is to be multiplied by another
3950 @param[in] Multiplier A number by which the multiplicand is to be multiplied
3951 @param[out] Result Pointer to the result of multiplication
3953 @retval RETURN_SUCCESS Successful multiplication
3954 @retval RETURN_BUFFER_TOO_SMALL Overflow
3955 @retval RETURN_INVALID_PARAMETER Result is NULL
3960 IN INT8 Multiplicand
,
3965 return SafeInt32ToInt8 (((INT32
)Multiplier
) *((INT32
)Multiplicand
), Result
);
3969 CHAR8 multiplication
3971 Performs the requested operation using the input parameters into a value
3972 specified by Result type and stores the converted value into the caller
3973 allocated output buffer specified by Result. The caller must pass in a
3974 Result buffer that is at least as large as the Result type.
3976 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
3978 If the requested operation results in an overflow or an underflow condition,
3979 then Result is set to CHAR8_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
3981 @param[in] Multiplicand A number that is to be multiplied by another
3982 @param[in] Multiplier A number by which the multiplicand is to be multiplied
3983 @param[out] Result Pointer to the result of multiplication
3985 @retval RETURN_SUCCESS Successful multiplication
3986 @retval RETURN_BUFFER_TOO_SMALL Overflow
3987 @retval RETURN_INVALID_PARAMETER Result is NULL
3992 IN CHAR8 Multiplicand
,
3993 IN CHAR8 Multiplier
,
3997 INT32 Multiplicand32
;
4000 if (Result
== NULL
) {
4001 return RETURN_INVALID_PARAMETER
;
4004 Multiplicand32
= (INT32
)Multiplicand
;
4005 Multiplier32
= (INT32
)Multiplier
;
4006 if (Multiplicand32
< 0 || Multiplicand32
> MAX_INT8
) {
4007 *Result
= CHAR8_ERROR
;
4008 return RETURN_BUFFER_TOO_SMALL
;
4010 if (Multiplier32
< 0 || Multiplier32
> MAX_INT8
) {
4011 *Result
= CHAR8_ERROR
;
4012 return RETURN_BUFFER_TOO_SMALL
;
4015 return SafeInt32ToChar8 (Multiplicand32
* Multiplier32
, Result
);
4019 INT16 multiplication
4021 Performs the requested operation using the input parameters into a value
4022 specified by Result type and stores the converted value into the caller
4023 allocated output buffer specified by Result. The caller must pass in a
4024 Result buffer that is at least as large as the Result type.
4026 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
4028 If the requested operation results in an overflow or an underflow condition,
4029 then Result is set to INT16_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
4031 @param[in] Multiplicand A number that is to be multiplied by another
4032 @param[in] Multiplier A number by which the multiplicand is to be multiplied
4033 @param[out] Result Pointer to the result of multiplication
4035 @retval RETURN_SUCCESS Successful multiplication
4036 @retval RETURN_BUFFER_TOO_SMALL Overflow
4037 @retval RETURN_INVALID_PARAMETER Result is NULL
4042 IN INT16 Multiplicand
,
4043 IN INT16 Multiplier
,
4047 return SafeInt32ToInt16 (((INT32
)Multiplicand
) *((INT32
)Multiplier
), Result
);
4051 INT32 multiplication
4053 Performs the requested operation using the input parameters into a value
4054 specified by Result type and stores the converted value into the caller
4055 allocated output buffer specified by Result. The caller must pass in a
4056 Result buffer that is at least as large as the Result type.
4058 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
4060 If the requested operation results in an overflow or an underflow condition,
4061 then Result is set to INT32_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
4063 @param[in] Multiplicand A number that is to be multiplied by another
4064 @param[in] Multiplier A number by which the multiplicand is to be multiplied
4065 @param[out] Result Pointer to the result of multiplication
4067 @retval RETURN_SUCCESS Successful multiplication
4068 @retval RETURN_BUFFER_TOO_SMALL Overflow
4069 @retval RETURN_INVALID_PARAMETER Result is NULL
4074 IN INT32 Multiplicand
,
4075 IN INT32 Multiplier
,
4079 return SafeInt64ToInt32 (MultS64x64 (Multiplicand
, Multiplier
), Result
);
4083 INT64 multiplication
4085 Performs the requested operation using the input parameters into a value
4086 specified by Result type and stores the converted value into the caller
4087 allocated output buffer specified by Result. The caller must pass in a
4088 Result buffer that is at least as large as the Result type.
4090 If Result is NULL, RETURN_INVALID_PARAMETER is returned.
4092 If the requested operation results in an overflow or an underflow condition,
4093 then Result is set to INT64_ERROR and RETURN_BUFFER_TOO_SMALL is returned.
4095 @param[in] Multiplicand A number that is to be multiplied by another
4096 @param[in] Multiplier A number by which the multiplicand is to be multiplied
4097 @param[out] Result Pointer to the result of multiplication
4099 @retval RETURN_SUCCESS Successful multiplication
4100 @retval RETURN_BUFFER_TOO_SMALL Overflow
4101 @retval RETURN_INVALID_PARAMETER Result is NULL
4106 IN INT64 Multiplicand
,
4107 IN INT64 Multiplier
,
4111 RETURN_STATUS Status
;
4112 UINT64 UnsignedMultiplicand
;
4113 UINT64 UnsignedMultiplier
;
4114 UINT64 UnsignedResult
;
4116 if (Result
== NULL
) {
4117 return RETURN_INVALID_PARAMETER
;
4121 // Split into sign and magnitude, do unsigned operation, apply sign.
4123 if (Multiplicand
< 0) {
4125 // Avoid negating the most negative number.
4127 UnsignedMultiplicand
= ((UINT64
)(- (Multiplicand
+ 1))) + 1;
4129 UnsignedMultiplicand
= (UINT64
)Multiplicand
;
4132 if (Multiplier
< 0) {
4134 // Avoid negating the most negative number.
4136 UnsignedMultiplier
= ((UINT64
)(- (Multiplier
+ 1))) + 1;
4138 UnsignedMultiplier
= (UINT64
)Multiplier
;
4141 Status
= SafeUint64Mult (UnsignedMultiplicand
, UnsignedMultiplier
, &UnsignedResult
);
4142 if (!RETURN_ERROR (Status
)) {
4143 if ((Multiplicand
< 0) != (Multiplier
< 0)) {
4144 if (UnsignedResult
> MIN_INT64_MAGNITUDE
) {
4145 *Result
= INT64_ERROR
;
4146 Status
= RETURN_BUFFER_TOO_SMALL
;
4147 } else if (UnsignedResult
== MIN_INT64_MAGNITUDE
) {
4148 *Result
= MIN_INT64
;
4150 *Result
= - ((INT64
)UnsignedResult
);
4153 if (UnsignedResult
> MAX_INT64
) {
4154 *Result
= INT64_ERROR
;
4155 Status
= RETURN_BUFFER_TOO_SMALL
;
4157 *Result
= (INT64
)UnsignedResult
;
4161 *Result
= INT64_ERROR
;