source: EcnlProtoTool/trunk/openssl-1.1.0e/crypto/o_time.c@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 11.1 KB
Line 
1/*
2 * Copyright 2001-2016 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
14#ifdef OPENSSL_SYS_VMS
15# if __CRTL_VER >= 70000000 && \
16 (defined _POSIX_C_SOURCE || !defined _ANSI_C_SOURCE)
17# define VMS_GMTIME_OK
18# endif
19# ifndef VMS_GMTIME_OK
20# include <libdtdef.h>
21# include <lib$routines.h>
22# include <lnmdef.h>
23# include <starlet.h>
24# include <descrip.h>
25# include <stdlib.h>
26# endif /* ndef VMS_GMTIME_OK */
27
28
29/*
30 * Needed to pick up the correct definitions and declarations in some of the
31 * DEC C Header Files (*.H).
32 */
33# define __NEW_STARLET 1
34
35# if (defined(__alpha) || defined(__ia64))
36# include <iledef.h>
37# else
38
39/* VAX */
40typedef struct _ile3 { /* Copied from ILEDEF.H for Alpha */
41# pragma __nomember_alignment
42 unsigned short int ile3$w_length; /* Length of buffer in bytes */
43 unsigned short int ile3$w_code; /* Item code value */
44 void *ile3$ps_bufaddr; /* Buffer address */
45 unsigned short int *ile3$ps_retlen_addr; /* Address of word for returned length */
46} ILE3;
47# endif /* alpha || ia64 */
48#endif /* OPENSSL_SYS_VMS */
49
50struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)
51{
52 struct tm *ts = NULL;
53
54#if defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32) && (!defined(OPENSSL_SYS_VMS) || defined(gmtime_r)) && !defined(OPENSSL_SYS_MACOSX)
55 if (gmtime_r(timer, result) == NULL)
56 return NULL;
57 ts = result;
58#elif !defined(OPENSSL_SYS_VMS) || defined(VMS_GMTIME_OK)
59 ts = gmtime(timer);
60 if (ts == NULL)
61 return NULL;
62
63 memcpy(result, ts, sizeof(struct tm));
64 ts = result;
65#endif
66#if defined( OPENSSL_SYS_VMS) && !defined( VMS_GMTIME_OK)
67 if (ts == NULL) {
68 static $DESCRIPTOR(tabnam, "LNM$DCL_LOGICAL");
69 static $DESCRIPTOR(lognam, "SYS$TIMEZONE_DIFFERENTIAL");
70 char logvalue[256];
71 unsigned int reslen = 0;
72# if __INITIAL_POINTER_SIZE == 64
73 ILEB_64 itemlist[2], *pitem;
74# else
75 ILE3 itemlist[2], *pitem;
76# endif
77 int status;
78 time_t t;
79
80
81 /*
82 * Setup an itemlist for the call to $TRNLNM - Translate Logical Name.
83 */
84 pitem = itemlist;
85
86# if __INITIAL_POINTER_SIZE == 64
87 pitem->ileb_64$w_mbo = 1;
88 pitem->ileb_64$w_code = LNM$_STRING;
89 pitem->ileb_64$l_mbmo = -1;
90 pitem->ileb_64$q_length = sizeof (logvalue);
91 pitem->ileb_64$pq_bufaddr = logvalue;
92 pitem->ileb_64$pq_retlen_addr = (unsigned __int64 *) &reslen;
93 pitem++;
94 /* Last item of the item list is null terminated */
95 pitem->ileb_64$q_length = pitem->ileb_64$w_code = 0;
96# else
97 pitem->ile3$w_length = sizeof (logvalue);
98 pitem->ile3$w_code = LNM$_STRING;
99 pitem->ile3$ps_bufaddr = logvalue;
100 pitem->ile3$ps_retlen_addr = (unsigned short int *) &reslen;
101 pitem++;
102 /* Last item of the item list is null terminated */
103 pitem->ile3$w_length = pitem->ile3$w_code = 0;
104# endif
105
106
107 /* Get the value for SYS$TIMEZONE_DIFFERENTIAL */
108 status = sys$trnlnm(0, &tabnam, &lognam, 0, itemlist);
109 if (!(status & 1))
110 return NULL;
111 logvalue[reslen] = '\0';
112
113 t = *timer;
114
115 /* The following is extracted from the DEC C header time.h */
116 /*
117 ** Beginning in OpenVMS Version 7.0 mktime, time, ctime, strftime
118 ** have two implementations. One implementation is provided
119 ** for compatibility and deals with time in terms of local time,
120 ** the other __utc_* deals with time in terms of UTC.
121 */
122 /*
123 * We use the same conditions as in said time.h to check if we should
124 * assume that t contains local time (and should therefore be
125 * adjusted) or UTC (and should therefore be left untouched).
126 */
127# if __CRTL_VER < 70000000 || defined _VMS_V6_SOURCE
128 /* Get the numerical value of the equivalence string */
129 status = atoi(logvalue);
130
131 /* and use it to move time to GMT */
132 t -= status;
133# endif
134
135 /* then convert the result to the time structure */
136
137 /*
138 * Since there was no gmtime_r() to do this stuff for us, we have to
139 * do it the hard way.
140 */
141 {
142 /*-
143 * The VMS epoch is the astronomical Smithsonian date,
144 if I remember correctly, which is November 17, 1858.
145 Furthermore, time is measure in tenths of microseconds
146 and stored in quadwords (64 bit integers). unix_epoch
147 below is January 1st 1970 expressed as a VMS time. The
148 following code was used to get this number:
149
150 #include <stdio.h>
151 #include <stdlib.h>
152 #include <lib$routines.h>
153 #include <starlet.h>
154
155 main()
156 {
157 unsigned long systime[2];
158 unsigned short epoch_values[7] =
159 { 1970, 1, 1, 0, 0, 0, 0 };
160
161 lib$cvt_vectim(epoch_values, systime);
162
163 printf("%u %u", systime[0], systime[1]);
164 }
165 */
166 unsigned long unix_epoch[2] = { 1273708544, 8164711 };
167 unsigned long deltatime[2];
168 unsigned long systime[2];
169 struct vms_vectime {
170 short year, month, day, hour, minute, second, centi_second;
171 } time_values;
172 long operation;
173
174 /*
175 * Turn the number of seconds since January 1st 1970 to an
176 * internal delta time. Note that lib$cvt_to_internal_time() will
177 * assume that t is signed, and will therefore break on 32-bit
178 * systems some time in 2038.
179 */
180 operation = LIB$K_DELTA_SECONDS;
181 status = lib$cvt_to_internal_time(&operation, &t, deltatime);
182
183 /*
184 * Add the delta time with the Unix epoch and we have the current
185 * UTC time in internal format
186 */
187 status = lib$add_times(unix_epoch, deltatime, systime);
188
189 /* Turn the internal time into a time vector */
190 status = sys$numtim(&time_values, systime);
191
192 /* Fill in the struct tm with the result */
193 result->tm_sec = time_values.second;
194 result->tm_min = time_values.minute;
195 result->tm_hour = time_values.hour;
196 result->tm_mday = time_values.day;
197 result->tm_mon = time_values.month - 1;
198 result->tm_year = time_values.year - 1900;
199
200 operation = LIB$K_DAY_OF_WEEK;
201 status = lib$cvt_from_internal_time(&operation,
202 &result->tm_wday, systime);
203 result->tm_wday %= 7;
204
205 operation = LIB$K_DAY_OF_YEAR;
206 status = lib$cvt_from_internal_time(&operation,
207 &result->tm_yday, systime);
208 result->tm_yday--;
209
210 result->tm_isdst = 0; /* There's no way to know... */
211
212 ts = result;
213 }
214 }
215#endif
216 return ts;
217}
218
219/*
220 * Take a tm structure and add an offset to it. This avoids any OS issues
221 * with restricted date types and overflows which cause the year 2038
222 * problem.
223 */
224
225#define SECS_PER_DAY (24 * 60 * 60)
226
227static long date_to_julian(int y, int m, int d);
228static void julian_to_date(long jd, int *y, int *m, int *d);
229static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
230 long *pday, int *psec);
231
232int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
233{
234 int time_sec, time_year, time_month, time_day;
235 long time_jd;
236
237 /* Convert time and offset into Julian day and seconds */
238 if (!julian_adj(tm, off_day, offset_sec, &time_jd, &time_sec))
239 return 0;
240
241 /* Convert Julian day back to date */
242
243 julian_to_date(time_jd, &time_year, &time_month, &time_day);
244
245 if (time_year < 1900 || time_year > 9999)
246 return 0;
247
248 /* Update tm structure */
249
250 tm->tm_year = time_year - 1900;
251 tm->tm_mon = time_month - 1;
252 tm->tm_mday = time_day;
253
254 tm->tm_hour = time_sec / 3600;
255 tm->tm_min = (time_sec / 60) % 60;
256 tm->tm_sec = time_sec % 60;
257
258 return 1;
259
260}
261
262int OPENSSL_gmtime_diff(int *pday, int *psec,
263 const struct tm *from, const struct tm *to)
264{
265 int from_sec, to_sec, diff_sec;
266 long from_jd, to_jd, diff_day;
267 if (!julian_adj(from, 0, 0, &from_jd, &from_sec))
268 return 0;
269 if (!julian_adj(to, 0, 0, &to_jd, &to_sec))
270 return 0;
271 diff_day = to_jd - from_jd;
272 diff_sec = to_sec - from_sec;
273 /* Adjust differences so both positive or both negative */
274 if (diff_day > 0 && diff_sec < 0) {
275 diff_day--;
276 diff_sec += SECS_PER_DAY;
277 }
278 if (diff_day < 0 && diff_sec > 0) {
279 diff_day++;
280 diff_sec -= SECS_PER_DAY;
281 }
282
283 if (pday)
284 *pday = (int)diff_day;
285 if (psec)
286 *psec = diff_sec;
287
288 return 1;
289
290}
291
292/* Convert tm structure and offset into julian day and seconds */
293static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
294 long *pday, int *psec)
295{
296 int offset_hms, offset_day;
297 long time_jd;
298 int time_year, time_month, time_day;
299 /* split offset into days and day seconds */
300 offset_day = offset_sec / SECS_PER_DAY;
301 /* Avoid sign issues with % operator */
302 offset_hms = offset_sec - (offset_day * SECS_PER_DAY);
303 offset_day += off_day;
304 /* Add current time seconds to offset */
305 offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
306 /* Adjust day seconds if overflow */
307 if (offset_hms >= SECS_PER_DAY) {
308 offset_day++;
309 offset_hms -= SECS_PER_DAY;
310 } else if (offset_hms < 0) {
311 offset_day--;
312 offset_hms += SECS_PER_DAY;
313 }
314
315 /*
316 * Convert date of time structure into a Julian day number.
317 */
318
319 time_year = tm->tm_year + 1900;
320 time_month = tm->tm_mon + 1;
321 time_day = tm->tm_mday;
322
323 time_jd = date_to_julian(time_year, time_month, time_day);
324
325 /* Work out Julian day of new date */
326 time_jd += offset_day;
327
328 if (time_jd < 0)
329 return 0;
330
331 *pday = time_jd;
332 *psec = offset_hms;
333 return 1;
334}
335
336/*
337 * Convert date to and from julian day Uses Fliegel & Van Flandern algorithm
338 */
339static long date_to_julian(int y, int m, int d)
340{
341 return (1461 * (y + 4800 + (m - 14) / 12)) / 4 +
342 (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 -
343 (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + d - 32075;
344}
345
346static void julian_to_date(long jd, int *y, int *m, int *d)
347{
348 long L = jd + 68569;
349 long n = (4 * L) / 146097;
350 long i, j;
351
352 L = L - (146097 * n + 3) / 4;
353 i = (4000 * (L + 1)) / 1461001;
354 L = L - (1461 * i) / 4 + 31;
355 j = (80 * L) / 2447;
356 *d = L - (2447 * j) / 80;
357 L = j / 11;
358 *m = j + 2 - (12 * L);
359 *y = 100 * (n - 49) + i + L;
360}
Note: See TracBrowser for help on using the repository browser.