xref: /openbmc/libpldm/src/utils.c (revision 93a997d2d2b3933fd008a8b04d2955cd4cf0eed7)
1 /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
2 #include "utils.h"
3 
4 #include <libpldm/base.h>
5 
day_map(uint8_t month)6 static int day_map(uint8_t month)
7 {
8 	switch (month) {
9 	case 1:
10 		return 31;
11 	case 2:
12 		return 28;
13 	case 3:
14 		return 31;
15 	case 4:
16 		return 30;
17 	case 5:
18 		return 31;
19 	case 6:
20 		return 30;
21 	case 7:
22 	case 8:
23 		return 31;
24 	case 9:
25 		return 30;
26 	case 10:
27 		return 31;
28 	case 11:
29 		return 30;
30 	case 12:
31 		return 31;
32 	default:
33 		return 0;
34 	}
35 }
36 
is_time_legal(uint8_t seconds,uint8_t minutes,uint8_t hours,uint8_t day,uint8_t month,uint16_t year)37 bool is_time_legal(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day,
38 		   uint8_t month, uint16_t year)
39 {
40 	if (month < 1 || month > 12) {
41 		return false;
42 	}
43 	int rday = day_map(month);
44 	if (month == 2 &&
45 	    ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) {
46 		rday += 1;
47 	}
48 	if (year < 1970 || day < 1 || day > rday || seconds > 59 ||
49 	    minutes > 59 || hours > 23) {
50 		return false;
51 	}
52 	return true;
53 }
54 
is_transfer_flag_valid(uint8_t transfer_flag)55 bool is_transfer_flag_valid(uint8_t transfer_flag)
56 {
57 	switch (transfer_flag) {
58 	case PLDM_START:
59 	case PLDM_MIDDLE:
60 	case PLDM_END:
61 	case PLDM_START_AND_END:
62 		return true;
63 
64 	default:
65 		return false;
66 	}
67 }
68