o_time.c revision 120631
1193323Sed/* crypto/o_time.c -*- mode:C; c-file-style: "eay" -*- */
2193323Sed/* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
3193323Sed * project 2001.
4193323Sed */
5193323Sed/* ====================================================================
6193323Sed * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
7193323Sed *
8193323Sed * Redistribution and use in source and binary forms, with or without
9193323Sed * modification, are permitted provided that the following conditions
10193323Sed * are met:
11193323Sed *
12193323Sed * 1. Redistributions of source code must retain the above copyright
13193323Sed *    notice, this list of conditions and the following disclaimer.
14193323Sed *
15193323Sed * 2. Redistributions in binary form must reproduce the above copyright
16193323Sed *    notice, this list of conditions and the following disclaimer in
17193323Sed *    the documentation and/or other materials provided with the
18193323Sed *    distribution.
19235633Sdim *
20193323Sed * 3. All advertising materials mentioning features or use of this
21245431Sdim *    software must display the following acknowledgment:
22245431Sdim *    "This product includes software developed by the OpenSSL Project
23245431Sdim *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24245431Sdim *
25245431Sdim * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26245431Sdim *    endorse or promote products derived from this software without
27245431Sdim *    prior written permission. For written permission, please contact
28245431Sdim *    licensing@OpenSSL.org.
29245431Sdim *
30245431Sdim * 5. Products derived from this software may not be called "OpenSSL"
31245431Sdim *    nor may "OpenSSL" appear in their names without prior written
32245431Sdim *    permission of the OpenSSL Project.
33245431Sdim *
34193323Sed * 6. Redistributions of any form whatsoever must retain the following
35193323Sed *    acknowledgment:
36193323Sed *    "This product includes software developed by the OpenSSL Project
37193323Sed *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38193323Sed *
39193323Sed * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40193323Sed * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41193323Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42193323Sed * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43193323Sed * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44193323Sed * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45193323Sed * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46193323Sed * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47193323Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48193323Sed * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49193323Sed * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50193323Sed * OF THE POSSIBILITY OF SUCH DAMAGE.
51193323Sed * ====================================================================
52193323Sed *
53193323Sed * This product includes cryptographic software written by Eric Young
54193323Sed * (eay@cryptsoft.com).  This product includes software written by Tim
55193323Sed * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <openssl/e_os2.h>
60#include <string.h>
61#include "o_time.h"
62
63#ifdef OPENSSL_SYS_VMS
64# include <libdtdef.h>
65# include <lib$routines.h>
66# include <lnmdef.h>
67# include <starlet.h>
68# include <descrip.h>
69# include <stdlib.h>
70#endif
71
72struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)
73	{
74	struct tm *ts = NULL;
75
76#if defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_OS2) && !defined(__CYGWIN32__) && (!defined(OPENSSL_SYS_VMS) || defined(gmtime_r)) && !defined(OPENSSL_SYS_MACOSX) && !defined(OPENSSL_SYS_SUNOS)
77	/* should return &data, but doesn't on some systems,
78	   so we don't even look at the return value */
79	gmtime_r(timer,result);
80	ts = result;
81#elif !defined(OPENSSL_SYS_VMS)
82	ts = gmtime(timer);
83	if (ts == NULL)
84		return NULL;
85
86	memcpy(result, ts, sizeof(struct tm));
87	ts = result;
88#endif
89#ifdef OPENSSL_SYS_VMS
90	if (ts == NULL)
91		{
92		static $DESCRIPTOR(tabnam,"LNM$DCL_LOGICAL");
93		static $DESCRIPTOR(lognam,"SYS$TIMEZONE_DIFFERENTIAL");
94		char logvalue[256];
95		unsigned int reslen = 0;
96		struct {
97			short buflen;
98			short code;
99			void *bufaddr;
100			unsigned int *reslen;
101		} itemlist[] = {
102			{ 0, LNM$_STRING, 0, 0 },
103			{ 0, 0, 0, 0 },
104		};
105		int status;
106		time_t t;
107
108		/* Get the value for SYS$TIMEZONE_DIFFERENTIAL */
109		itemlist[0].buflen = sizeof(logvalue);
110		itemlist[0].bufaddr = logvalue;
111		itemlist[0].reslen = &reslen;
112		status = sys$trnlnm(0, &tabnam, &lognam, 0, itemlist);
113		if (!(status & 1))
114			return NULL;
115		logvalue[reslen] = '\0';
116
117		/* Get the numerical value of the equivalence string */
118		status = atoi(logvalue);
119
120		/* and use it to move time to GMT */
121		t = *timer - status;
122
123		/* then convert the result to the time structure */
124#ifndef OPENSSL_THREADS
125		ts=(struct tm *)localtime(&t);
126#else
127		/* Since there was no gmtime_r() to do this stuff for us,
128		   we have to do it the hard way. */
129		{
130		/* The VMS epoch is the astronomical Smithsonian date,
131		   if I remember correctly, which is November 17, 1858.
132		   Furthermore, time is measure in thenths of microseconds
133		   and stored in quadwords (64 bit integers).  unix_epoch
134		   below is January 1st 1970 expressed as a VMS time.  The
135		   following code was used to get this number:
136
137		   #include <stdio.h>
138		   #include <stdlib.h>
139		   #include <lib$routines.h>
140		   #include <starlet.h>
141
142		   main()
143		   {
144		     unsigned long systime[2];
145		     unsigned short epoch_values[7] =
146		       { 1970, 1, 1, 0, 0, 0, 0 };
147
148		     lib$cvt_vectim(epoch_values, systime);
149
150		     printf("%u %u", systime[0], systime[1]);
151		   }
152		*/
153		unsigned long unix_epoch[2] = { 1273708544, 8164711 };
154		unsigned long deltatime[2];
155		unsigned long systime[2];
156		struct vms_vectime
157			{
158			short year, month, day, hour, minute, second,
159				centi_second;
160			} time_values;
161		long operation;
162
163		/* Turn the number of seconds since January 1st 1970 to
164		   an internal delta time.
165		   Note that lib$cvt_to_internal_time() will assume
166		   that t is signed, and will therefore break on 32-bit
167		   systems some time in 2038.
168		*/
169		operation = LIB$K_DELTA_SECONDS;
170		status = lib$cvt_to_internal_time(&operation,
171			&t, deltatime);
172
173		/* Add the delta time with the Unix epoch and we have
174		   the current UTC time in internal format */
175		status = lib$add_times(unix_epoch, deltatime, systime);
176
177		/* Turn the internal time into a time vector */
178		status = sys$numtim(&time_values, systime);
179
180		/* Fill in the struct tm with the result */
181		result->tm_sec = time_values.second;
182		result->tm_min = time_values.minute;
183		result->tm_hour = time_values.hour;
184		result->tm_mday = time_values.day;
185		result->tm_mon = time_values.month - 1;
186		result->tm_year = time_values.year - 1900;
187
188		operation = LIB$K_DAY_OF_WEEK;
189		status = lib$cvt_from_internal_time(&operation,
190			&result->tm_wday, systime);
191		result->tm_wday %= 7;
192
193		operation = LIB$K_DAY_OF_YEAR;
194		status = lib$cvt_from_internal_time(&operation,
195			&result->tm_yday, systime);
196		result->tm_yday--;
197
198		result->tm_isdst = 0; /* There's no way to know... */
199
200		ts = result;
201#endif
202		}
203		}
204#endif
205	return ts;
206	}
207