Debian Codegraph @

Cryptlib/SysCall/TimerWrapper.c

raw
1 /** @file
2 C Run-Time Libraries (CRT) Time Management Routines Wrapper Implementation
3 for OpenSSL-based Cryptographic Library (used in DXE & RUNTIME).
4
5 Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include <OpenSslSupport.h>
17
18 //
19 // -- Time Management Routines --
20 //
21
22 #define IsLeap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
23 #define SECSPERMIN (60)
24 #define SECSPERHOUR (60 * 60)
25 #define SECSPERDAY (24 * SECSPERHOUR)
26
27 //
28 // The arrays give the cumulative number of days up to the first of the
29 // month number used as the index (1 -> 12) for regular and leap years.
30 // The value at index 13 is for the whole year.
31 //
32 UINTN CumulativeDays[2][14] = {
33 {
34 0,
35 0,
36 31,
37 31 + 28,
38 31 + 28 + 31,
39 31 + 28 + 31 + 30,
40 31 + 28 + 31 + 30 + 31,
41 31 + 28 + 31 + 30 + 31 + 30,
42 31 + 28 + 31 + 30 + 31 + 30 + 31,
43 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
44 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
45 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
46 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
47 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31
48 },
49 {
50 0,
51 0,
52 31,
53 31 + 29,
54 31 + 29 + 31,
55 31 + 29 + 31 + 30,
56 31 + 29 + 31 + 30 + 31,
57 31 + 29 + 31 + 30 + 31 + 30,
58 31 + 29 + 31 + 30 + 31 + 30 + 31,
59 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,
60 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
61 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
62 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
63 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31
64 }
65 };
66
67 /* Get the system time as seconds elapsed since midnight, January 1, 1970. */
68 //INTN time(
69 // INTN *timer
70 // )
71 time_t time (time_t *timer)
72 {
73 EFI_TIME Time;
74 time_t CalTime;
75 UINTN Year;
76
77 //
78 // Get the current time and date information
79 //
80 uefi_call_wrapper(RT->GetTime, 2, &Time, NULL);
81
82 //
83 // Years Handling
84 // UTime should now be set to 00:00:00 on Jan 1 of the current year.
85 //
86 for (Year = 1970, CalTime = 0; Year != Time.Year; Year++) {
87 CalTime = CalTime + (time_t)(CumulativeDays[IsLeap(Year)][13] * SECSPERDAY);
88 }
89
90 //
91 // Add in number of seconds for current Month, Day, Hour, Minute, Seconds, and TimeZone adjustment
92 //
93 CalTime = CalTime +
94 (time_t)((Time.TimeZone != EFI_UNSPECIFIED_TIMEZONE) ? (Time.TimeZone * 60) : 0) +
95 (time_t)(CumulativeDays[IsLeap(Time.Year)][Time.Month] * SECSPERDAY) +
96 (time_t)(((Time.Day > 0) ? Time.Day - 1 : 0) * SECSPERDAY) +
97 (time_t)(Time.Hour * SECSPERHOUR) +
98 (time_t)(Time.Minute * 60) +
99 (time_t)Time.Second;
100
101 if (timer != NULL) {
102 *timer = CalTime;
103 }
104
105 return CalTime;
106 }
107
108 //
109 // Convert a time value from type time_t to struct tm.
110 //
111 struct tm * gmtime (const time_t *timer)
112 {
113 struct tm *GmTime;
114 UINT16 DayNo;
115 UINT16 DayRemainder;
116 time_t Year;
117 time_t YearNo;
118 UINT16 TotalDays;
119 UINT16 MonthNo;
120
121 if (timer == NULL) {
122 return NULL;
123 }
124
125 GmTime = malloc (sizeof (struct tm));
126 if (GmTime == NULL) {
127 return NULL;
128 }
129
130 ZeroMem ((VOID *) GmTime, (UINTN) sizeof (struct tm));
131
132 DayNo = (UINT16) (*timer / SECSPERDAY);
133 DayRemainder = (UINT16) (*timer % SECSPERDAY);
134
135 GmTime->tm_sec = (int) (DayRemainder % SECSPERMIN);
136 GmTime->tm_min = (int) ((DayRemainder % SECSPERHOUR) / SECSPERMIN);
137 GmTime->tm_hour = (int) (DayRemainder / SECSPERHOUR);
138 GmTime->tm_wday = (int) ((DayNo + 4) % 7);
139
140 for (Year = 1970, YearNo = 0; DayNo > 0; Year++) {
141 TotalDays = (UINT16) (IsLeap (Year) ? 366 : 365);
142 if (DayNo >= TotalDays) {
143 DayNo = (UINT16) (DayNo - TotalDays);
144 YearNo++;
145 } else {
146 break;
147 }
148 }
149
150 GmTime->tm_year = (int) (YearNo + (1970 - 1900));
151 GmTime->tm_yday = (int) DayNo;
152
153 for (MonthNo = 12; MonthNo > 1; MonthNo--) {
154 if (DayNo >= CumulativeDays[IsLeap(Year)][MonthNo]) {
155 DayNo = (UINT16) (DayNo - (UINT16) (CumulativeDays[IsLeap(Year)][MonthNo]));
156 break;
157 }
158 }
159
160 GmTime->tm_mon = (int) MonthNo - 1;
161 GmTime->tm_mday = (int) DayNo + 1;
162
163 GmTime->tm_isdst = 0;
164 GmTime->tm_gmtoff = 0;
165 GmTime->tm_zone = NULL;
166
167 return GmTime;
168 }