OpenCoverage

o_time.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/o_time.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/*-
2 * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.-
3 *-
4 * Licensed under the OpenSSL license (the "License"). You may not use-
5 * this file except in compliance with the License. You can obtain a copy-
6 * in the file LICENSE in the source distribution or at-
7 * https://www.openssl.org/source/license.html-
8 */-
9-
10#include <openssl/e_os2.h>-
11#include <string.h>-
12#include <openssl/crypto.h>-
13-
14struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)-
15{-
16 struct tm *ts = NULL;-
17-
18#if defined(OPENSSL_THREADS) && defined(OPENSSL_SYS_VMS)-
19 {-
20 /*-
21 * On VMS, gmtime_r() takes a 32-bit pointer as second argument.-
22 * Since we can't know that |result| is in a space that can easily-
23 * translate to a 32-bit pointer, we must store temporarily on stack-
24 * and copy the result. The stack is always reachable with 32-bit-
25 * pointers.-
26 */-
27#if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE-
28# pragma pointer_size save-
29# pragma pointer_size 32-
30#endif-
31 struct tm data, *ts2 = &data;-
32#if defined OPENSSL_SYS_VMS && __INITIAL_POINTER_SIZE-
33# pragma pointer_size restore-
34#endif-
35 if (gmtime_r(timer, ts2) == NULL)-
36 return NULL;-
37 memcpy(result, ts2, sizeof(struct tm));-
38 ts = result;-
39 }-
40#elif defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_MACOSX)-
41 if (gmtime_r(timer, result) == NULL)
gmtime_r(timer...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 8010838 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-8010838
42 return NULL;
never executed: return ((void *)0) ;
0
43 ts = result;-
44#elif defined (OPENSSL_SYS_WINDOWS) && defined(_MSC_VER) && _MSC_VER >= 1400-
45 if (gmtime_s(result, timer))-
46 return NULL;-
47 ts = result;-
48#else-
49 ts = gmtime(timer);-
50 if (ts == NULL)-
51 return NULL;-
52-
53 memcpy(result, ts, sizeof(struct tm));-
54 ts = result;-
55#endif-
56 return ts;
executed 8010838 times by 1 test: return ts;
Executed by:
  • libcrypto.so.1.1
8010838
57}-
58-
59/*-
60 * Take a tm structure and add an offset to it. This avoids any OS issues-
61 * with restricted date types and overflows which cause the year 2038-
62 * problem.-
63 */-
64-
65#define SECS_PER_DAY (24 * 60 * 60)-
66-
67static long date_to_julian(int y, int m, int d);-
68static void julian_to_date(long jd, int *y, int *m, int *d);-
69static int julian_adj(const struct tm *tm, int off_day, long offset_sec,-
70 long *pday, int *psec);-
71-
72int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)-
73{-
74 int time_sec, time_year, time_month, time_day;-
75 long time_jd;-
76-
77 /* Convert time and offset into Julian day and seconds */-
78 if (!julian_adj(tm, off_day, offset_sec, &time_jd, &time_sec))
!julian_adj(tm...jd, &time_sec)Description
TRUEevaluated 225 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 4004477 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
225-4004477
79 return 0;
executed 225 times by 1 test: return 0;
Executed by:
  • libcrypto.so.1.1
225
80-
81 /* Convert Julian day back to date */-
82-
83 julian_to_date(time_jd, &time_year, &time_month, &time_day);-
84-
85 if (time_year < 1900 || time_year > 9999)
time_year < 1900Description
TRUEevaluated 224 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 4004253 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
time_year > 9999Description
TRUEevaluated 302 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 4003951 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
224-4004253
86 return 0;
executed 526 times by 1 test: return 0;
Executed by:
  • libcrypto.so.1.1
526
87-
88 /* Update tm structure */-
89-
90 tm->tm_year = time_year - 1900;-
91 tm->tm_mon = time_month - 1;-
92 tm->tm_mday = time_day;-
93-
94 tm->tm_hour = time_sec / 3600;-
95 tm->tm_min = (time_sec / 60) % 60;-
96 tm->tm_sec = time_sec % 60;-
97-
98 return 1;
executed 4003951 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
4003951
99-
100}-
101-
102int OPENSSL_gmtime_diff(int *pday, int *psec,-
103 const struct tm *from, const struct tm *to)-
104{-
105 int from_sec, to_sec, diff_sec;-
106 long from_jd, to_jd, diff_day;-
107 if (!julian_adj(from, 0, 0, &from_jd, &from_sec))
!julian_adj(fr...jd, &from_sec)Description
TRUEnever evaluated
FALSEevaluated 4008425 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-4008425
108 return 0;
never executed: return 0;
0
109 if (!julian_adj(to, 0, 0, &to_jd, &to_sec))
!julian_adj(to...o_jd, &to_sec)Description
TRUEnever evaluated
FALSEevaluated 4008425 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
0-4008425
110 return 0;
never executed: return 0;
0
111 diff_day = to_jd - from_jd;-
112 diff_sec = to_sec - from_sec;-
113 /* Adjust differences so both positive or both negative */-
114 if (diff_day > 0 && diff_sec < 0) {
diff_day > 0Description
TRUEevaluated 1984741 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 2023684 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
diff_sec < 0Description
TRUEevaluated 1546905 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 437836 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
437836-2023684
115 diff_day--;-
116 diff_sec += SECS_PER_DAY;-
117 }
executed 1546905 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
1546905
118 if (diff_day < 0 && diff_sec > 0) {
diff_day < 0Description
TRUEevaluated 1936844 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 2071581 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
diff_sec > 0Description
TRUEevaluated 435463 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 1501381 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
435463-2071581
119 diff_day++;-
120 diff_sec -= SECS_PER_DAY;-
121 }
executed 435463 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
435463
122-
123 if (pday)
pdayDescription
TRUEevaluated 4008425 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-4008425
124 *pday = (int)diff_day;
executed 4008425 times by 1 test: *pday = (int)diff_day;
Executed by:
  • libcrypto.so.1.1
4008425
125 if (psec)
psecDescription
TRUEevaluated 4008425 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEnever evaluated
0-4008425
126 *psec = diff_sec;
executed 4008425 times by 1 test: *psec = diff_sec;
Executed by:
  • libcrypto.so.1.1
4008425
127-
128 return 1;
executed 4008425 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
4008425
129-
130}-
131-
132/* Convert tm structure and offset into julian day and seconds */-
133static int julian_adj(const struct tm *tm, int off_day, long offset_sec,-
134 long *pday, int *psec)-
135{-
136 int offset_hms, offset_day;-
137 long time_jd;-
138 int time_year, time_month, time_day;-
139 /* split offset into days and day seconds */-
140 offset_day = offset_sec / SECS_PER_DAY;-
141 /* Avoid sign issues with % operator */-
142 offset_hms = offset_sec - (offset_day * SECS_PER_DAY);-
143 offset_day += off_day;-
144 /* Add current time seconds to offset */-
145 offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;-
146 /* Adjust day seconds if overflow */-
147 if (offset_hms >= SECS_PER_DAY) {
offset_hms >= (24 * 60 * 60)Description
TRUEevaluated 1545110 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 10476442 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
1545110-10476442
148 offset_day++;-
149 offset_hms -= SECS_PER_DAY;-
150 } else if (offset_hms < 0) {
executed 1545110 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
offset_hms < 0Description
TRUEevaluated 435160 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 10041282 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
435160-10041282
151 offset_day--;-
152 offset_hms += SECS_PER_DAY;-
153 }
executed 435160 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
435160
154-
155 /*-
156 * Convert date of time structure into a Julian day number.-
157 */-
158-
159 time_year = tm->tm_year + 1900;-
160 time_month = tm->tm_mon + 1;-
161 time_day = tm->tm_mday;-
162-
163 time_jd = date_to_julian(time_year, time_month, time_day);-
164-
165 /* Work out Julian day of new date */-
166 time_jd += offset_day;-
167-
168 if (time_jd < 0)
time_jd < 0Description
TRUEevaluated 225 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
FALSEevaluated 12021327 times by 1 test
Evaluated by:
  • libcrypto.so.1.1
225-12021327
169 return 0;
executed 225 times by 1 test: return 0;
Executed by:
  • libcrypto.so.1.1
225
170-
171 *pday = time_jd;-
172 *psec = offset_hms;-
173 return 1;
executed 12021327 times by 1 test: return 1;
Executed by:
  • libcrypto.so.1.1
12021327
174}-
175-
176/*-
177 * Convert date to and from julian day Uses Fliegel & Van Flandern algorithm-
178 */-
179static long date_to_julian(int y, int m, int d)-
180{-
181 return (1461 * (y + 4800 + (m - 14) / 12)) / 4 +
executed 12021552 times by 1 test: return (1461 * (y + 4800 + (m - 14) / 12)) / 4 + (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 - (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + d - 32075;
Executed by:
  • libcrypto.so.1.1
12021552
182 (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 -
executed 12021552 times by 1 test: return (1461 * (y + 4800 + (m - 14) / 12)) / 4 + (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 - (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + d - 32075;
Executed by:
  • libcrypto.so.1.1
12021552
183 (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + d - 32075;
executed 12021552 times by 1 test: return (1461 * (y + 4800 + (m - 14) / 12)) / 4 + (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 - (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + d - 32075;
Executed by:
  • libcrypto.so.1.1
12021552
184}-
185-
186static void julian_to_date(long jd, int *y, int *m, int *d)-
187{-
188 long L = jd + 68569;-
189 long n = (4 * L) / 146097;-
190 long i, j;-
191-
192 L = L - (146097 * n + 3) / 4;-
193 i = (4000 * (L + 1)) / 1461001;-
194 L = L - (1461 * i) / 4 + 31;-
195 j = (80 * L) / 2447;-
196 *d = L - (2447 * j) / 80;-
197 L = j / 11;-
198 *m = j + 2 - (12 * L);-
199 *y = 100 * (n - 49) + i + L;-
200}
executed 4004477 times by 1 test: end of block
Executed by:
  • libcrypto.so.1.1
4004477
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2