Safe API Framework
Layered API framework for safety-related applications (ERTMS RBC reference targeting CENELEC EN 50128 SIL 4)
Loading...
Searching...
No Matches
sapi_cast.c
Go to the documentation of this file.
1
10
12
13sapi_status_t sapi_cast_i8_to_i16(int8_t in, int16_t *out)
14{
15 if (out == NULL)
16 {
18 }
19 *out = (int16_t)in;
20 return SAPI_STATUS_OK;
21}
22
23sapi_status_t sapi_cast_i8_to_i32(int8_t in, int32_t *out)
24{
25 if (out == NULL)
26 {
28 }
29 *out = (int32_t)in;
30 return SAPI_STATUS_OK;
31}
32
33sapi_status_t sapi_cast_i8_to_i64(int8_t in, int64_t *out)
34{
35 if (out == NULL)
36 {
38 }
39 *out = (int64_t)in;
40 return SAPI_STATUS_OK;
41}
42
43sapi_status_t sapi_cast_i8_to_u8(int8_t in, uint8_t *out)
44{
45 if (out == NULL)
46 {
48 }
49 int64_t widened = (int64_t)in;
50 if (widened < 0)
51 {
53 }
54 *out = (uint8_t)in;
55 return SAPI_STATUS_OK;
56}
57
58sapi_status_t sapi_cast_i8_to_u16(int8_t in, uint16_t *out)
59{
60 if (out == NULL)
61 {
63 }
64 int64_t widened = (int64_t)in;
65 if (widened < 0)
66 {
68 }
69 *out = (uint16_t)in;
70 return SAPI_STATUS_OK;
71}
72
73sapi_status_t sapi_cast_i8_to_u32(int8_t in, uint32_t *out)
74{
75 if (out == NULL)
76 {
78 }
79 int64_t widened = (int64_t)in;
80 if (widened < 0)
81 {
83 }
84 *out = (uint32_t)in;
85 return SAPI_STATUS_OK;
86}
87
88sapi_status_t sapi_cast_i8_to_u64(int8_t in, uint64_t *out)
89{
90 if (out == NULL)
91 {
93 }
94 int64_t widened = (int64_t)in;
95 if (widened < 0)
96 {
98 }
99 *out = (uint64_t)in;
100 return SAPI_STATUS_OK;
101}
102
103sapi_status_t sapi_cast_i8_to_size(int8_t in, size_t *out)
104{
105 if (out == NULL)
106 {
108 }
109 int64_t widened = (int64_t)in;
110 if (widened < 0)
111 {
113 }
114#if SIZE_MAX < UINT64_MAX
115 if ((uint64_t)widened > (uint64_t)SIZE_MAX)
116 {
118 }
119#endif
120 *out = (size_t)in;
121 return SAPI_STATUS_OK;
122}
123
124sapi_status_t sapi_cast_i16_to_i8(int16_t in, int8_t *out)
125{
126 if (out == NULL)
127 {
129 }
130 int64_t widened = (int64_t)in;
131 if (widened < (int64_t)INT8_MIN)
132 {
134 }
135 if (widened > (int64_t)INT8_MAX)
136 {
138 }
139 *out = (int8_t)in;
140 return SAPI_STATUS_OK;
141}
142
143sapi_status_t sapi_cast_i16_to_i32(int16_t in, int32_t *out)
144{
145 if (out == NULL)
146 {
148 }
149 *out = (int32_t)in;
150 return SAPI_STATUS_OK;
151}
152
153sapi_status_t sapi_cast_i16_to_i64(int16_t in, int64_t *out)
154{
155 if (out == NULL)
156 {
158 }
159 *out = (int64_t)in;
160 return SAPI_STATUS_OK;
161}
162
163sapi_status_t sapi_cast_i16_to_u8(int16_t in, uint8_t *out)
164{
165 if (out == NULL)
166 {
168 }
169 int64_t widened = (int64_t)in;
170 if (widened < 0)
171 {
173 }
174 if ((uint64_t)widened > (uint64_t)UINT8_MAX)
175 {
177 }
178 *out = (uint8_t)in;
179 return SAPI_STATUS_OK;
180}
181
182sapi_status_t sapi_cast_i16_to_u16(int16_t in, uint16_t *out)
183{
184 if (out == NULL)
185 {
187 }
188 int64_t widened = (int64_t)in;
189 if (widened < 0)
190 {
192 }
193 *out = (uint16_t)in;
194 return SAPI_STATUS_OK;
195}
196
197sapi_status_t sapi_cast_i16_to_u32(int16_t in, uint32_t *out)
198{
199 if (out == NULL)
200 {
202 }
203 int64_t widened = (int64_t)in;
204 if (widened < 0)
205 {
207 }
208 *out = (uint32_t)in;
209 return SAPI_STATUS_OK;
210}
211
212sapi_status_t sapi_cast_i16_to_u64(int16_t in, uint64_t *out)
213{
214 if (out == NULL)
215 {
217 }
218 int64_t widened = (int64_t)in;
219 if (widened < 0)
220 {
222 }
223 *out = (uint64_t)in;
224 return SAPI_STATUS_OK;
225}
226
227sapi_status_t sapi_cast_i16_to_size(int16_t in, size_t *out)
228{
229 if (out == NULL)
230 {
232 }
233 int64_t widened = (int64_t)in;
234 if (widened < 0)
235 {
237 }
238#if SIZE_MAX < UINT64_MAX
239 if ((uint64_t)widened > (uint64_t)SIZE_MAX)
240 {
242 }
243#endif
244 *out = (size_t)in;
245 return SAPI_STATUS_OK;
246}
247
248sapi_status_t sapi_cast_i32_to_i8(int32_t in, int8_t *out)
249{
250 if (out == NULL)
251 {
253 }
254 int64_t widened = (int64_t)in;
255 if (widened < (int64_t)INT8_MIN)
256 {
258 }
259 if (widened > (int64_t)INT8_MAX)
260 {
262 }
263 *out = (int8_t)in;
264 return SAPI_STATUS_OK;
265}
266
267sapi_status_t sapi_cast_i32_to_i16(int32_t in, int16_t *out)
268{
269 if (out == NULL)
270 {
272 }
273 int64_t widened = (int64_t)in;
274 if (widened < (int64_t)INT16_MIN)
275 {
277 }
278 if (widened > (int64_t)INT16_MAX)
279 {
281 }
282 *out = (int16_t)in;
283 return SAPI_STATUS_OK;
284}
285
286sapi_status_t sapi_cast_i32_to_i64(int32_t in, int64_t *out)
287{
288 if (out == NULL)
289 {
291 }
292 *out = (int64_t)in;
293 return SAPI_STATUS_OK;
294}
295
296sapi_status_t sapi_cast_i32_to_u8(int32_t in, uint8_t *out)
297{
298 if (out == NULL)
299 {
301 }
302 int64_t widened = (int64_t)in;
303 if (widened < 0)
304 {
306 }
307 if ((uint64_t)widened > (uint64_t)UINT8_MAX)
308 {
310 }
311 *out = (uint8_t)in;
312 return SAPI_STATUS_OK;
313}
314
315sapi_status_t sapi_cast_i32_to_u16(int32_t in, uint16_t *out)
316{
317 if (out == NULL)
318 {
320 }
321 int64_t widened = (int64_t)in;
322 if (widened < 0)
323 {
325 }
326 if ((uint64_t)widened > (uint64_t)UINT16_MAX)
327 {
329 }
330 *out = (uint16_t)in;
331 return SAPI_STATUS_OK;
332}
333
334sapi_status_t sapi_cast_i32_to_u32(int32_t in, uint32_t *out)
335{
336 if (out == NULL)
337 {
339 }
340 int64_t widened = (int64_t)in;
341 if (widened < 0)
342 {
344 }
345 *out = (uint32_t)in;
346 return SAPI_STATUS_OK;
347}
348
349sapi_status_t sapi_cast_i32_to_u64(int32_t in, uint64_t *out)
350{
351 if (out == NULL)
352 {
354 }
355 int64_t widened = (int64_t)in;
356 if (widened < 0)
357 {
359 }
360 *out = (uint64_t)in;
361 return SAPI_STATUS_OK;
362}
363
364sapi_status_t sapi_cast_i32_to_size(int32_t in, size_t *out)
365{
366 if (out == NULL)
367 {
369 }
370 int64_t widened = (int64_t)in;
371 if (widened < 0)
372 {
374 }
375#if SIZE_MAX < UINT64_MAX
376 if ((uint64_t)widened > (uint64_t)SIZE_MAX)
377 {
379 }
380#endif
381 *out = (size_t)in;
382 return SAPI_STATUS_OK;
383}
384
385sapi_status_t sapi_cast_i64_to_i8(int64_t in, int8_t *out)
386{
387 if (out == NULL)
388 {
390 }
391 int64_t widened = (int64_t)in;
392 if (widened < (int64_t)INT8_MIN)
393 {
395 }
396 if (widened > (int64_t)INT8_MAX)
397 {
399 }
400 *out = (int8_t)in;
401 return SAPI_STATUS_OK;
402}
403
404sapi_status_t sapi_cast_i64_to_i16(int64_t in, int16_t *out)
405{
406 if (out == NULL)
407 {
409 }
410 int64_t widened = (int64_t)in;
411 if (widened < (int64_t)INT16_MIN)
412 {
414 }
415 if (widened > (int64_t)INT16_MAX)
416 {
418 }
419 *out = (int16_t)in;
420 return SAPI_STATUS_OK;
421}
422
423sapi_status_t sapi_cast_i64_to_i32(int64_t in, int32_t *out)
424{
425 if (out == NULL)
426 {
428 }
429 int64_t widened = (int64_t)in;
430 if (widened < (int64_t)INT32_MIN)
431 {
433 }
434 if (widened > (int64_t)INT32_MAX)
435 {
437 }
438 *out = (int32_t)in;
439 return SAPI_STATUS_OK;
440}
441
442sapi_status_t sapi_cast_i64_to_u8(int64_t in, uint8_t *out)
443{
444 if (out == NULL)
445 {
447 }
448 int64_t widened = (int64_t)in;
449 if (widened < 0)
450 {
452 }
453 if ((uint64_t)widened > (uint64_t)UINT8_MAX)
454 {
456 }
457 *out = (uint8_t)in;
458 return SAPI_STATUS_OK;
459}
460
461sapi_status_t sapi_cast_i64_to_u16(int64_t in, uint16_t *out)
462{
463 if (out == NULL)
464 {
466 }
467 int64_t widened = (int64_t)in;
468 if (widened < 0)
469 {
471 }
472 if ((uint64_t)widened > (uint64_t)UINT16_MAX)
473 {
475 }
476 *out = (uint16_t)in;
477 return SAPI_STATUS_OK;
478}
479
480sapi_status_t sapi_cast_i64_to_u32(int64_t in, uint32_t *out)
481{
482 if (out == NULL)
483 {
485 }
486 int64_t widened = (int64_t)in;
487 if (widened < 0)
488 {
490 }
491 if ((uint64_t)widened > (uint64_t)UINT32_MAX)
492 {
494 }
495 *out = (uint32_t)in;
496 return SAPI_STATUS_OK;
497}
498
499sapi_status_t sapi_cast_i64_to_u64(int64_t in, uint64_t *out)
500{
501 if (out == NULL)
502 {
504 }
505 int64_t widened = (int64_t)in;
506 if (widened < 0)
507 {
509 }
510 *out = (uint64_t)in;
511 return SAPI_STATUS_OK;
512}
513
514sapi_status_t sapi_cast_i64_to_size(int64_t in, size_t *out)
515{
516 if (out == NULL)
517 {
519 }
520 int64_t widened = (int64_t)in;
521 if (widened < 0)
522 {
524 }
525#if SIZE_MAX < UINT64_MAX
526 if ((uint64_t)widened > (uint64_t)SIZE_MAX)
527 {
529 }
530#endif
531 *out = (size_t)in;
532 return SAPI_STATUS_OK;
533}
534
535sapi_status_t sapi_cast_u8_to_i8(uint8_t in, int8_t *out)
536{
537 if (out == NULL)
538 {
540 }
541 uint64_t widened = (uint64_t)in;
542 if (widened > (uint64_t)INT8_MAX)
543 {
545 }
546 *out = (int8_t)in;
547 return SAPI_STATUS_OK;
548}
549
550sapi_status_t sapi_cast_u8_to_i16(uint8_t in, int16_t *out)
551{
552 if (out == NULL)
553 {
555 }
556 *out = (int16_t)in;
557 return SAPI_STATUS_OK;
558}
559
560sapi_status_t sapi_cast_u8_to_i32(uint8_t in, int32_t *out)
561{
562 if (out == NULL)
563 {
565 }
566 *out = (int32_t)in;
567 return SAPI_STATUS_OK;
568}
569
570sapi_status_t sapi_cast_u8_to_i64(uint8_t in, int64_t *out)
571{
572 if (out == NULL)
573 {
575 }
576 *out = (int64_t)in;
577 return SAPI_STATUS_OK;
578}
579
580sapi_status_t sapi_cast_u8_to_u16(uint8_t in, uint16_t *out)
581{
582 if (out == NULL)
583 {
585 }
586 *out = (uint16_t)in;
587 return SAPI_STATUS_OK;
588}
589
590sapi_status_t sapi_cast_u8_to_u32(uint8_t in, uint32_t *out)
591{
592 if (out == NULL)
593 {
595 }
596 *out = (uint32_t)in;
597 return SAPI_STATUS_OK;
598}
599
600sapi_status_t sapi_cast_u8_to_u64(uint8_t in, uint64_t *out)
601{
602 if (out == NULL)
603 {
605 }
606 *out = (uint64_t)in;
607 return SAPI_STATUS_OK;
608}
609
610sapi_status_t sapi_cast_u8_to_size(uint8_t in, size_t *out)
611{
612 if (out == NULL)
613 {
615 }
616#if SIZE_MAX < UINT64_MAX
617 {
618 uint64_t widened = (uint64_t)in;
619 if (widened > (uint64_t)SIZE_MAX)
620 {
622 }
623 }
624#endif
625 *out = (size_t)in;
626 return SAPI_STATUS_OK;
627}
628
629sapi_status_t sapi_cast_u16_to_i8(uint16_t in, int8_t *out)
630{
631 if (out == NULL)
632 {
634 }
635 uint64_t widened = (uint64_t)in;
636 if (widened > (uint64_t)INT8_MAX)
637 {
639 }
640 *out = (int8_t)in;
641 return SAPI_STATUS_OK;
642}
643
644sapi_status_t sapi_cast_u16_to_i16(uint16_t in, int16_t *out)
645{
646 if (out == NULL)
647 {
649 }
650 uint64_t widened = (uint64_t)in;
651 if (widened > (uint64_t)INT16_MAX)
652 {
654 }
655 *out = (int16_t)in;
656 return SAPI_STATUS_OK;
657}
658
659sapi_status_t sapi_cast_u16_to_i32(uint16_t in, int32_t *out)
660{
661 if (out == NULL)
662 {
664 }
665 *out = (int32_t)in;
666 return SAPI_STATUS_OK;
667}
668
669sapi_status_t sapi_cast_u16_to_i64(uint16_t in, int64_t *out)
670{
671 if (out == NULL)
672 {
674 }
675 *out = (int64_t)in;
676 return SAPI_STATUS_OK;
677}
678
679sapi_status_t sapi_cast_u16_to_u8(uint16_t in, uint8_t *out)
680{
681 if (out == NULL)
682 {
684 }
685 uint64_t widened = (uint64_t)in;
686 if (widened > (uint64_t)UINT8_MAX)
687 {
689 }
690 *out = (uint8_t)in;
691 return SAPI_STATUS_OK;
692}
693
694sapi_status_t sapi_cast_u16_to_u32(uint16_t in, uint32_t *out)
695{
696 if (out == NULL)
697 {
699 }
700 *out = (uint32_t)in;
701 return SAPI_STATUS_OK;
702}
703
704sapi_status_t sapi_cast_u16_to_u64(uint16_t in, uint64_t *out)
705{
706 if (out == NULL)
707 {
709 }
710 *out = (uint64_t)in;
711 return SAPI_STATUS_OK;
712}
713
714sapi_status_t sapi_cast_u16_to_size(uint16_t in, size_t *out)
715{
716 if (out == NULL)
717 {
719 }
720#if SIZE_MAX < UINT64_MAX
721 {
722 uint64_t widened = (uint64_t)in;
723 if (widened > (uint64_t)SIZE_MAX)
724 {
726 }
727 }
728#endif
729 *out = (size_t)in;
730 return SAPI_STATUS_OK;
731}
732
733sapi_status_t sapi_cast_u32_to_i8(uint32_t in, int8_t *out)
734{
735 if (out == NULL)
736 {
738 }
739 uint64_t widened = (uint64_t)in;
740 if (widened > (uint64_t)INT8_MAX)
741 {
743 }
744 *out = (int8_t)in;
745 return SAPI_STATUS_OK;
746}
747
748sapi_status_t sapi_cast_u32_to_i16(uint32_t in, int16_t *out)
749{
750 if (out == NULL)
751 {
753 }
754 uint64_t widened = (uint64_t)in;
755 if (widened > (uint64_t)INT16_MAX)
756 {
758 }
759 *out = (int16_t)in;
760 return SAPI_STATUS_OK;
761}
762
763sapi_status_t sapi_cast_u32_to_i32(uint32_t in, int32_t *out)
764{
765 if (out == NULL)
766 {
768 }
769 uint64_t widened = (uint64_t)in;
770 if (widened > (uint64_t)INT32_MAX)
771 {
773 }
774 *out = (int32_t)in;
775 return SAPI_STATUS_OK;
776}
777
778sapi_status_t sapi_cast_u32_to_i64(uint32_t in, int64_t *out)
779{
780 if (out == NULL)
781 {
783 }
784 *out = (int64_t)in;
785 return SAPI_STATUS_OK;
786}
787
788sapi_status_t sapi_cast_u32_to_u8(uint32_t in, uint8_t *out)
789{
790 if (out == NULL)
791 {
793 }
794 uint64_t widened = (uint64_t)in;
795 if (widened > (uint64_t)UINT8_MAX)
796 {
798 }
799 *out = (uint8_t)in;
800 return SAPI_STATUS_OK;
801}
802
803sapi_status_t sapi_cast_u32_to_u16(uint32_t in, uint16_t *out)
804{
805 if (out == NULL)
806 {
808 }
809 uint64_t widened = (uint64_t)in;
810 if (widened > (uint64_t)UINT16_MAX)
811 {
813 }
814 *out = (uint16_t)in;
815 return SAPI_STATUS_OK;
816}
817
818sapi_status_t sapi_cast_u32_to_u64(uint32_t in, uint64_t *out)
819{
820 if (out == NULL)
821 {
823 }
824 *out = (uint64_t)in;
825 return SAPI_STATUS_OK;
826}
827
828sapi_status_t sapi_cast_u32_to_size(uint32_t in, size_t *out)
829{
830 if (out == NULL)
831 {
833 }
834#if SIZE_MAX < UINT64_MAX
835 {
836 uint64_t widened = (uint64_t)in;
837 if (widened > (uint64_t)SIZE_MAX)
838 {
840 }
841 }
842#endif
843 *out = (size_t)in;
844 return SAPI_STATUS_OK;
845}
846
847sapi_status_t sapi_cast_u64_to_i8(uint64_t in, int8_t *out)
848{
849 if (out == NULL)
850 {
852 }
853 uint64_t widened = (uint64_t)in;
854 if (widened > (uint64_t)INT8_MAX)
855 {
857 }
858 *out = (int8_t)in;
859 return SAPI_STATUS_OK;
860}
861
862sapi_status_t sapi_cast_u64_to_i16(uint64_t in, int16_t *out)
863{
864 if (out == NULL)
865 {
867 }
868 uint64_t widened = (uint64_t)in;
869 if (widened > (uint64_t)INT16_MAX)
870 {
872 }
873 *out = (int16_t)in;
874 return SAPI_STATUS_OK;
875}
876
877sapi_status_t sapi_cast_u64_to_i32(uint64_t in, int32_t *out)
878{
879 if (out == NULL)
880 {
882 }
883 uint64_t widened = (uint64_t)in;
884 if (widened > (uint64_t)INT32_MAX)
885 {
887 }
888 *out = (int32_t)in;
889 return SAPI_STATUS_OK;
890}
891
892sapi_status_t sapi_cast_u64_to_i64(uint64_t in, int64_t *out)
893{
894 if (out == NULL)
895 {
897 }
898 uint64_t widened = (uint64_t)in;
899 if (widened > (uint64_t)INT64_MAX)
900 {
902 }
903 *out = (int64_t)in;
904 return SAPI_STATUS_OK;
905}
906
907sapi_status_t sapi_cast_u64_to_u8(uint64_t in, uint8_t *out)
908{
909 if (out == NULL)
910 {
912 }
913 uint64_t widened = (uint64_t)in;
914 if (widened > (uint64_t)UINT8_MAX)
915 {
917 }
918 *out = (uint8_t)in;
919 return SAPI_STATUS_OK;
920}
921
922sapi_status_t sapi_cast_u64_to_u16(uint64_t in, uint16_t *out)
923{
924 if (out == NULL)
925 {
927 }
928 uint64_t widened = (uint64_t)in;
929 if (widened > (uint64_t)UINT16_MAX)
930 {
932 }
933 *out = (uint16_t)in;
934 return SAPI_STATUS_OK;
935}
936
937sapi_status_t sapi_cast_u64_to_u32(uint64_t in, uint32_t *out)
938{
939 if (out == NULL)
940 {
942 }
943 uint64_t widened = (uint64_t)in;
944 if (widened > (uint64_t)UINT32_MAX)
945 {
947 }
948 *out = (uint32_t)in;
949 return SAPI_STATUS_OK;
950}
951
952sapi_status_t sapi_cast_u64_to_size(uint64_t in, size_t *out)
953{
954 if (out == NULL)
955 {
957 }
958#if SIZE_MAX < UINT64_MAX
959 {
960 uint64_t widened = (uint64_t)in;
961 if (widened > (uint64_t)SIZE_MAX)
962 {
964 }
965 }
966#endif
967 *out = (size_t)in;
968 return SAPI_STATUS_OK;
969}
970
971sapi_status_t sapi_cast_size_to_i8(size_t in, int8_t *out)
972{
973 if (out == NULL)
974 {
976 }
977 /* size_t is assumed to fit within uint64_t on any supported target (ADR-003). */
978 uint64_t widened = (uint64_t)in;
979 if (widened > (uint64_t)INT8_MAX)
980 {
982 }
983 *out = (int8_t)in;
984 return SAPI_STATUS_OK;
985}
986
987sapi_status_t sapi_cast_size_to_i16(size_t in, int16_t *out)
988{
989 if (out == NULL)
990 {
992 }
993 /* size_t is assumed to fit within uint64_t on any supported target (ADR-003). */
994 uint64_t widened = (uint64_t)in;
995 if (widened > (uint64_t)INT16_MAX)
996 {
998 }
999 *out = (int16_t)in;
1000 return SAPI_STATUS_OK;
1001}
1002
1003sapi_status_t sapi_cast_size_to_i32(size_t in, int32_t *out)
1004{
1005 if (out == NULL)
1006 {
1008 }
1009 /* size_t is assumed to fit within uint64_t on any supported target (ADR-003). */
1010 uint64_t widened = (uint64_t)in;
1011 if (widened > (uint64_t)INT32_MAX)
1012 {
1014 }
1015 *out = (int32_t)in;
1016 return SAPI_STATUS_OK;
1017}
1018
1019sapi_status_t sapi_cast_size_to_i64(size_t in, int64_t *out)
1020{
1021 if (out == NULL)
1022 {
1024 }
1025 /* size_t is assumed to fit within uint64_t on any supported target (ADR-003). */
1026 uint64_t widened = (uint64_t)in;
1027 if (widened > (uint64_t)INT64_MAX)
1028 {
1030 }
1031 *out = (int64_t)in;
1032 return SAPI_STATUS_OK;
1033}
1034
1035sapi_status_t sapi_cast_size_to_u8(size_t in, uint8_t *out)
1036{
1037 if (out == NULL)
1038 {
1040 }
1041 /* size_t is assumed to fit within uint64_t on any supported target (ADR-003). */
1042 uint64_t widened = (uint64_t)in;
1043 if (widened > (uint64_t)UINT8_MAX)
1044 {
1046 }
1047 *out = (uint8_t)in;
1048 return SAPI_STATUS_OK;
1049}
1050
1051sapi_status_t sapi_cast_size_to_u16(size_t in, uint16_t *out)
1052{
1053 if (out == NULL)
1054 {
1056 }
1057 /* size_t is assumed to fit within uint64_t on any supported target (ADR-003). */
1058 uint64_t widened = (uint64_t)in;
1059 if (widened > (uint64_t)UINT16_MAX)
1060 {
1062 }
1063 *out = (uint16_t)in;
1064 return SAPI_STATUS_OK;
1065}
1066
1067sapi_status_t sapi_cast_size_to_u32(size_t in, uint32_t *out)
1068{
1069 if (out == NULL)
1070 {
1072 }
1073 /* size_t is assumed to fit within uint64_t on any supported target (ADR-003). */
1074 uint64_t widened = (uint64_t)in;
1075 if (widened > (uint64_t)UINT32_MAX)
1076 {
1078 }
1079 *out = (uint32_t)in;
1080 return SAPI_STATUS_OK;
1081}
1082
1083sapi_status_t sapi_cast_size_to_u64(size_t in, uint64_t *out)
1084{
1085 if (out == NULL)
1086 {
1088 }
1089 /* size_t is assumed to fit within uint64_t on any supported target (ADR-003). */
1090 *out = (uint64_t)in;
1091 return SAPI_STATUS_OK;
1092}
1093
1094sapi_status_t sapi_cast_checked_add_u32(uint32_t a, uint32_t b, uint32_t *out)
1095{
1096 if (out == NULL)
1097 {
1099 }
1100 /* a + b cannot overflow uint32_t arithmetic itself here because both
1101 * operands are first widened to uint64_t - the overflow check below
1102 * catches what would have wrapped had the addition stayed at uint32_t
1103 * width. */
1104 uint64_t widened = (uint64_t)a + (uint64_t)b;
1105 if (widened > (uint64_t)UINT32_MAX)
1106 {
1108 "sapi_cast_checked_add_u32: addition overflowed uint32_t");
1110 }
1111 *out = (uint32_t)widened;
1112 return SAPI_STATUS_OK;
1113}
1114
1115sapi_status_t sapi_cast_checked_sub_u32(uint32_t a, uint32_t b, uint32_t *out)
1116{
1117 if (out == NULL)
1118 {
1120 }
1121 if (b > a)
1122 {
1124 "sapi_cast_checked_sub_u32: subtraction underflowed uint32_t");
1126 }
1127 *out = a - b;
1128 return SAPI_STATUS_OK;
1129}
1130
1131sapi_status_t sapi_cast_checked_mul_u32(uint32_t a, uint32_t b, uint32_t *out)
1132{
1133 if (out == NULL)
1134 {
1136 }
1137 uint64_t widened = (uint64_t)a * (uint64_t)b;
1138 if (widened > (uint64_t)UINT32_MAX)
1139 {
1141 "sapi_cast_checked_mul_u32: multiplication overflowed uint32_t");
1143 }
1144 *out = (uint32_t)widened;
1145 return SAPI_STATUS_OK;
1146}
1147
1148sapi_status_t sapi_cast_checked_add_size(size_t a, size_t b, size_t *out)
1149{
1150 if (out == NULL)
1151 {
1153 }
1154 /* SIZE_MAX - a cannot underflow: a is already known <= SIZE_MAX, so
1155 * this subtraction is always well-defined and gives the largest b
1156 * that would not overflow. */
1157 if (b > (SIZE_MAX - a))
1158 {
1160 "sapi_cast_checked_add_size: addition overflowed size_t");
1162 }
1163 *out = a + b;
1164 return SAPI_STATUS_OK;
1165}
1166
1167sapi_status_t sapi_cast_checked_sub_size(size_t a, size_t b, size_t *out)
1168{
1169 if (out == NULL)
1170 {
1172 }
1173 if (b > a)
1174 {
1176 "sapi_cast_checked_sub_size: subtraction underflowed size_t");
1178 }
1179 *out = a - b;
1180 return SAPI_STATUS_OK;
1181}
1182
1183sapi_status_t sapi_cast_checked_mul_size(size_t a, size_t b, size_t *out)
1184{
1185 if (out == NULL)
1186 {
1188 }
1189 if ((a != 0U) && (b > (SIZE_MAX / a)))
1190 {
1192 "sapi_cast_checked_mul_size: multiplication overflowed size_t");
1194 }
1195 *out = a * b;
1196 return SAPI_STATUS_OK;
1197}
1198
1199sapi_status_t sapi_cast_bounds_check(size_t index, size_t count)
1200{
1201 if (index >= count)
1202 {
1204 "sapi_cast_bounds_check: index out of range");
1206 }
1207 return SAPI_STATUS_OK;
1208}
sapi_status_t sapi_cast_u8_to_u32(uint8_t in, uint32_t *out)
Checked cast from uint8_t to uint32_t.
Definition sapi_cast.c:590
sapi_status_t sapi_cast_i16_to_u32(int16_t in, uint32_t *out)
Checked cast from int16_t to uint32_t.
Definition sapi_cast.c:197
sapi_status_t sapi_cast_i8_to_size(int8_t in, size_t *out)
Checked cast from int8_t to size_t.
Definition sapi_cast.c:103
sapi_status_t sapi_cast_u8_to_i8(uint8_t in, int8_t *out)
Checked cast from uint8_t to int8_t.
Definition sapi_cast.c:535
sapi_status_t sapi_cast_i16_to_size(int16_t in, size_t *out)
Checked cast from int16_t to size_t.
Definition sapi_cast.c:227
sapi_status_t sapi_cast_u32_to_size(uint32_t in, size_t *out)
Checked cast from uint32_t to size_t.
Definition sapi_cast.c:828
sapi_status_t sapi_cast_i16_to_u64(int16_t in, uint64_t *out)
Checked cast from int16_t to uint64_t.
Definition sapi_cast.c:212
sapi_status_t sapi_cast_u64_to_u8(uint64_t in, uint8_t *out)
Checked cast from uint64_t to uint8_t.
Definition sapi_cast.c:907
sapi_status_t sapi_cast_checked_sub_u32(uint32_t a, uint32_t b, uint32_t *out)
Checked subtraction: *out = a - b, only if b <= a (no underflow).
Definition sapi_cast.c:1115
sapi_status_t sapi_cast_checked_mul_size(size_t a, size_t b, size_t *out)
Checked multiplication: *out = a * b, only if the result fits size_t.
Definition sapi_cast.c:1183
sapi_status_t sapi_cast_size_to_u8(size_t in, uint8_t *out)
Checked cast from size_t to uint8_t.
Definition sapi_cast.c:1035
sapi_status_t sapi_cast_u8_to_i32(uint8_t in, int32_t *out)
Checked cast from uint8_t to int32_t.
Definition sapi_cast.c:560
sapi_status_t sapi_cast_size_to_u32(size_t in, uint32_t *out)
Checked cast from size_t to uint32_t.
Definition sapi_cast.c:1067
sapi_status_t sapi_cast_bounds_check(size_t index, size_t count)
Checked array/index bounds check: is index a valid index into an array of count elements?
Definition sapi_cast.c:1199
sapi_status_t sapi_cast_size_to_i8(size_t in, int8_t *out)
Checked cast from size_t to int8_t.
Definition sapi_cast.c:971
sapi_status_t sapi_cast_i64_to_u32(int64_t in, uint32_t *out)
Checked cast from int64_t to uint32_t.
Definition sapi_cast.c:480
sapi_status_t sapi_cast_u32_to_i16(uint32_t in, int16_t *out)
Checked cast from uint32_t to int16_t.
Definition sapi_cast.c:748
sapi_status_t sapi_cast_u16_to_u64(uint16_t in, uint64_t *out)
Checked cast from uint16_t to uint64_t.
Definition sapi_cast.c:704
sapi_status_t sapi_cast_u8_to_size(uint8_t in, size_t *out)
Checked cast from uint8_t to size_t.
Definition sapi_cast.c:610
sapi_status_t sapi_cast_u64_to_i16(uint64_t in, int16_t *out)
Checked cast from uint64_t to int16_t.
Definition sapi_cast.c:862
sapi_status_t sapi_cast_i32_to_u16(int32_t in, uint16_t *out)
Checked cast from int32_t to uint16_t.
Definition sapi_cast.c:315
sapi_status_t sapi_cast_u8_to_u16(uint8_t in, uint16_t *out)
Checked cast from uint8_t to uint16_t.
Definition sapi_cast.c:580
sapi_status_t sapi_cast_u32_to_i64(uint32_t in, int64_t *out)
Checked cast from uint32_t to int64_t.
Definition sapi_cast.c:778
sapi_status_t sapi_cast_u16_to_size(uint16_t in, size_t *out)
Checked cast from uint16_t to size_t.
Definition sapi_cast.c:714
sapi_status_t sapi_cast_i16_to_i64(int16_t in, int64_t *out)
Checked cast from int16_t to int64_t.
Definition sapi_cast.c:153
sapi_status_t sapi_cast_u64_to_u16(uint64_t in, uint16_t *out)
Checked cast from uint64_t to uint16_t.
Definition sapi_cast.c:922
sapi_status_t sapi_cast_u8_to_i16(uint8_t in, int16_t *out)
Checked cast from uint8_t to int16_t.
Definition sapi_cast.c:550
sapi_status_t sapi_cast_i8_to_i32(int8_t in, int32_t *out)
Checked cast from int8_t to int32_t.
Definition sapi_cast.c:23
sapi_status_t sapi_cast_size_to_i16(size_t in, int16_t *out)
Checked cast from size_t to int16_t.
Definition sapi_cast.c:987
sapi_status_t sapi_cast_u64_to_i64(uint64_t in, int64_t *out)
Checked cast from uint64_t to int64_t.
Definition sapi_cast.c:892
sapi_status_t sapi_cast_i64_to_i16(int64_t in, int16_t *out)
Checked cast from int64_t to int16_t.
Definition sapi_cast.c:404
sapi_status_t sapi_cast_i8_to_i16(int8_t in, int16_t *out)
Checked cast from int8_t to int16_t.
Definition sapi_cast.c:13
sapi_status_t sapi_cast_i32_to_u8(int32_t in, uint8_t *out)
Checked cast from int32_t to uint8_t.
Definition sapi_cast.c:296
sapi_status_t sapi_cast_size_to_u16(size_t in, uint16_t *out)
Checked cast from size_t to uint16_t.
Definition sapi_cast.c:1051
sapi_status_t sapi_cast_u8_to_u64(uint8_t in, uint64_t *out)
Checked cast from uint8_t to uint64_t.
Definition sapi_cast.c:600
sapi_status_t sapi_cast_u32_to_i8(uint32_t in, int8_t *out)
Checked cast from uint32_t to int8_t.
Definition sapi_cast.c:733
sapi_status_t sapi_cast_size_to_u64(size_t in, uint64_t *out)
Checked cast from size_t to uint64_t.
Definition sapi_cast.c:1083
sapi_status_t sapi_cast_u16_to_u32(uint16_t in, uint32_t *out)
Checked cast from uint16_t to uint32_t.
Definition sapi_cast.c:694
sapi_status_t sapi_cast_i64_to_i8(int64_t in, int8_t *out)
Checked cast from int64_t to int8_t.
Definition sapi_cast.c:385
sapi_status_t sapi_cast_u64_to_size(uint64_t in, size_t *out)
Checked cast from uint64_t to size_t.
Definition sapi_cast.c:952
sapi_status_t sapi_cast_u16_to_i32(uint16_t in, int32_t *out)
Checked cast from uint16_t to int32_t.
Definition sapi_cast.c:659
sapi_status_t sapi_cast_u32_to_i32(uint32_t in, int32_t *out)
Checked cast from uint32_t to int32_t.
Definition sapi_cast.c:763
sapi_status_t sapi_cast_i8_to_u16(int8_t in, uint16_t *out)
Checked cast from int8_t to uint16_t.
Definition sapi_cast.c:58
sapi_status_t sapi_cast_u32_to_u8(uint32_t in, uint8_t *out)
Checked cast from uint32_t to uint8_t.
Definition sapi_cast.c:788
sapi_status_t sapi_cast_checked_add_size(size_t a, size_t b, size_t *out)
Checked addition: *out = a + b, only if the result fits size_t.
Definition sapi_cast.c:1148
sapi_status_t sapi_cast_u16_to_i64(uint16_t in, int64_t *out)
Checked cast from uint16_t to int64_t.
Definition sapi_cast.c:669
sapi_status_t sapi_cast_i16_to_u16(int16_t in, uint16_t *out)
Checked cast from int16_t to uint16_t.
Definition sapi_cast.c:182
sapi_status_t sapi_cast_u16_to_u8(uint16_t in, uint8_t *out)
Checked cast from uint16_t to uint8_t.
Definition sapi_cast.c:679
sapi_status_t sapi_cast_u32_to_u16(uint32_t in, uint16_t *out)
Checked cast from uint32_t to uint16_t.
Definition sapi_cast.c:803
sapi_status_t sapi_cast_i64_to_u64(int64_t in, uint64_t *out)
Checked cast from int64_t to uint64_t.
Definition sapi_cast.c:499
sapi_status_t sapi_cast_i32_to_i16(int32_t in, int16_t *out)
Checked cast from int32_t to int16_t.
Definition sapi_cast.c:267
sapi_status_t sapi_cast_u64_to_u32(uint64_t in, uint32_t *out)
Checked cast from uint64_t to uint32_t.
Definition sapi_cast.c:937
sapi_status_t sapi_cast_i64_to_u16(int64_t in, uint16_t *out)
Checked cast from int64_t to uint16_t.
Definition sapi_cast.c:461
sapi_status_t sapi_cast_i8_to_u32(int8_t in, uint32_t *out)
Checked cast from int8_t to uint32_t.
Definition sapi_cast.c:73
sapi_status_t sapi_cast_i64_to_i32(int64_t in, int32_t *out)
Checked cast from int64_t to int32_t.
Definition sapi_cast.c:423
sapi_status_t sapi_cast_size_to_i32(size_t in, int32_t *out)
Checked cast from size_t to int32_t.
Definition sapi_cast.c:1003
sapi_status_t sapi_cast_i16_to_u8(int16_t in, uint8_t *out)
Checked cast from int16_t to uint8_t.
Definition sapi_cast.c:163
sapi_status_t sapi_cast_i32_to_i64(int32_t in, int64_t *out)
Checked cast from int32_t to int64_t.
Definition sapi_cast.c:286
sapi_status_t sapi_cast_i16_to_i8(int16_t in, int8_t *out)
Checked cast from int16_t to int8_t.
Definition sapi_cast.c:124
sapi_status_t sapi_cast_u16_to_i8(uint16_t in, int8_t *out)
Checked cast from uint16_t to int8_t.
Definition sapi_cast.c:629
sapi_status_t sapi_cast_i32_to_u64(int32_t in, uint64_t *out)
Checked cast from int32_t to uint64_t.
Definition sapi_cast.c:349
sapi_status_t sapi_cast_u8_to_i64(uint8_t in, int64_t *out)
Checked cast from uint8_t to int64_t.
Definition sapi_cast.c:570
sapi_status_t sapi_cast_size_to_i64(size_t in, int64_t *out)
Checked cast from size_t to int64_t.
Definition sapi_cast.c:1019
sapi_status_t sapi_cast_i8_to_u64(int8_t in, uint64_t *out)
Checked cast from int8_t to uint64_t.
Definition sapi_cast.c:88
sapi_status_t sapi_cast_i8_to_i64(int8_t in, int64_t *out)
Checked cast from int8_t to int64_t.
Definition sapi_cast.c:33
sapi_status_t sapi_cast_u64_to_i8(uint64_t in, int8_t *out)
Checked cast from uint64_t to int8_t.
Definition sapi_cast.c:847
sapi_status_t sapi_cast_i64_to_size(int64_t in, size_t *out)
Checked cast from int64_t to size_t.
Definition sapi_cast.c:514
sapi_status_t sapi_cast_checked_add_u32(uint32_t a, uint32_t b, uint32_t *out)
Checked addition: *out = a + b, only if the result fits uint32_t.
Definition sapi_cast.c:1094
sapi_status_t sapi_cast_checked_mul_u32(uint32_t a, uint32_t b, uint32_t *out)
Checked multiplication: *out = a * b, only if the result fits uint32_t.
Definition sapi_cast.c:1131
sapi_status_t sapi_cast_i32_to_size(int32_t in, size_t *out)
Checked cast from int32_t to size_t.
Definition sapi_cast.c:364
sapi_status_t sapi_cast_u16_to_i16(uint16_t in, int16_t *out)
Checked cast from uint16_t to int16_t.
Definition sapi_cast.c:644
sapi_status_t sapi_cast_u32_to_u64(uint32_t in, uint64_t *out)
Checked cast from uint32_t to uint64_t.
Definition sapi_cast.c:818
sapi_status_t sapi_cast_u64_to_i32(uint64_t in, int32_t *out)
Checked cast from uint64_t to int32_t.
Definition sapi_cast.c:877
sapi_status_t sapi_cast_i64_to_u8(int64_t in, uint8_t *out)
Checked cast from int64_t to uint8_t.
Definition sapi_cast.c:442
sapi_status_t sapi_cast_i32_to_u32(int32_t in, uint32_t *out)
Checked cast from int32_t to uint32_t.
Definition sapi_cast.c:334
sapi_status_t sapi_cast_i16_to_i32(int16_t in, int32_t *out)
Checked cast from int16_t to int32_t.
Definition sapi_cast.c:143
sapi_status_t sapi_cast_i32_to_i8(int32_t in, int8_t *out)
Checked cast from int32_t to int8_t.
Definition sapi_cast.c:248
sapi_status_t sapi_cast_checked_sub_size(size_t a, size_t b, size_t *out)
Checked subtraction: *out = a - b, only if b <= a (no underflow).
Definition sapi_cast.c:1167
sapi_status_t sapi_cast_i8_to_u8(int8_t in, uint8_t *out)
Checked cast from int8_t to uint8_t.
Definition sapi_cast.c:43
void sapi_safety_violation_report(sapi_safety_violation_kind_t kind, const char *file, int32_t line, const char *message)
Reports one violation to the registered handler, if any.
@ SAPI_SAFETY_VIOLATION_OUT_OF_RANGE
@ SAPI_SAFETY_VIOLATION_OVERFLOW
sapi_status_t
Common result/status codes.
Definition sapi_status.h:27
@ SAPI_STATUS_INVALID_PARAM
Definition sapi_status.h:29
@ SAPI_STATUS_VALUE_OUT_OF_RANGE
Definition sapi_status.h:39
@ SAPI_STATUS_OK
Definition sapi_status.h:28
Checked integer casting between all fixed-width types and size_t (ADR-003). Every conversion in the c...
Opt-in notification hook for the three new safety primitives (safe pointer, checked integer arithmeti...