sl@0
|
1 |
/* crypto/tmdiff.c */
|
sl@0
|
2 |
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
*
|
sl@0
|
5 |
* This package is an SSL implementation written
|
sl@0
|
6 |
* by Eric Young (eay@cryptsoft.com).
|
sl@0
|
7 |
* The implementation was written so as to conform with Netscapes SSL.
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* This library is free for commercial and non-commercial use as long as
|
sl@0
|
10 |
* the following conditions are aheared to. The following conditions
|
sl@0
|
11 |
* apply to all code found in this distribution, be it the RC4, RSA,
|
sl@0
|
12 |
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
sl@0
|
13 |
* included with this distribution is covered by the same copyright terms
|
sl@0
|
14 |
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
sl@0
|
15 |
*
|
sl@0
|
16 |
* Copyright remains Eric Young's, and as such any Copyright notices in
|
sl@0
|
17 |
* the code are not to be removed.
|
sl@0
|
18 |
* If this package is used in a product, Eric Young should be given attribution
|
sl@0
|
19 |
* as the author of the parts of the library used.
|
sl@0
|
20 |
* This can be in the form of a textual message at program startup or
|
sl@0
|
21 |
* in documentation (online or textual) provided with the package.
|
sl@0
|
22 |
*
|
sl@0
|
23 |
* Redistribution and use in source and binary forms, with or without
|
sl@0
|
24 |
* modification, are permitted provided that the following conditions
|
sl@0
|
25 |
* are met:
|
sl@0
|
26 |
* 1. Redistributions of source code must retain the copyright
|
sl@0
|
27 |
* notice, this list of conditions and the following disclaimer.
|
sl@0
|
28 |
* 2. Redistributions in binary form must reproduce the above copyright
|
sl@0
|
29 |
* notice, this list of conditions and the following disclaimer in the
|
sl@0
|
30 |
* documentation and/or other materials provided with the distribution.
|
sl@0
|
31 |
* 3. All advertising materials mentioning features or use of this software
|
sl@0
|
32 |
* must display the following acknowledgement:
|
sl@0
|
33 |
* "This product includes cryptographic software written by
|
sl@0
|
34 |
* Eric Young (eay@cryptsoft.com)"
|
sl@0
|
35 |
* The word 'cryptographic' can be left out if the rouines from the library
|
sl@0
|
36 |
* being used are not cryptographic related :-).
|
sl@0
|
37 |
* 4. If you include any Windows specific code (or a derivative thereof) from
|
sl@0
|
38 |
* the apps directory (application code) you must include an acknowledgement:
|
sl@0
|
39 |
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
sl@0
|
40 |
*
|
sl@0
|
41 |
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
sl@0
|
42 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
sl@0
|
43 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
sl@0
|
44 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
sl@0
|
45 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
sl@0
|
46 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
sl@0
|
47 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
sl@0
|
48 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
sl@0
|
49 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
sl@0
|
50 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
sl@0
|
51 |
* SUCH DAMAGE.
|
sl@0
|
52 |
*
|
sl@0
|
53 |
* The licence and distribution terms for any publically available version or
|
sl@0
|
54 |
* derivative of this code cannot be changed. i.e. this code cannot simply be
|
sl@0
|
55 |
* copied and put under another distribution licence
|
sl@0
|
56 |
* [including the GNU Public Licence.]
|
sl@0
|
57 |
*/
|
sl@0
|
58 |
#include <stdio.h>
|
sl@0
|
59 |
#include <stdlib.h>
|
sl@0
|
60 |
#include "cryptlib.h"
|
sl@0
|
61 |
#include <openssl/tmdiff.h>
|
sl@0
|
62 |
#if !defined(OPENSSL_SYS_MSDOS)
|
sl@0
|
63 |
#include OPENSSL_UNISTD
|
sl@0
|
64 |
#endif
|
sl@0
|
65 |
|
sl@0
|
66 |
|
sl@0
|
67 |
#ifdef TIMEB
|
sl@0
|
68 |
#undef OPENSSL_SYS_WIN32
|
sl@0
|
69 |
#undef TIMES
|
sl@0
|
70 |
#endif
|
sl@0
|
71 |
|
sl@0
|
72 |
#if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN32) && !(defined(OPENSSL_SYS_VMS) || defined(__DECC)) && !defined(OPENSSL_SYS_MACOSX_RHAPSODY) && !defined(OPENSSL_SYS_VXWORKS)
|
sl@0
|
73 |
# define TIMES
|
sl@0
|
74 |
#endif
|
sl@0
|
75 |
|
sl@0
|
76 |
#ifdef OPENSSL_SYS_NETWARE
|
sl@0
|
77 |
#undef TIMES
|
sl@0
|
78 |
#endif
|
sl@0
|
79 |
|
sl@0
|
80 |
|
sl@0
|
81 |
#if !defined(_IRIX) || defined (OPENSSL_SYS_NETWARE)
|
sl@0
|
82 |
# include <time.h>
|
sl@0
|
83 |
#endif
|
sl@0
|
84 |
|
sl@0
|
85 |
|
sl@0
|
86 |
|
sl@0
|
87 |
#ifdef TIMES
|
sl@0
|
88 |
# include <sys/types.h>
|
sl@0
|
89 |
# include <sys/times.h>
|
sl@0
|
90 |
#endif
|
sl@0
|
91 |
|
sl@0
|
92 |
/* Depending on the VMS version, the tms structure is perhaps defined.
|
sl@0
|
93 |
The __TMS macro will show if it was. If it wasn't defined, we should
|
sl@0
|
94 |
undefine TIMES, since that tells the rest of the program how things
|
sl@0
|
95 |
should be handled. -- Richard Levitte */
|
sl@0
|
96 |
#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
|
sl@0
|
97 |
#undef TIMES
|
sl@0
|
98 |
#endif
|
sl@0
|
99 |
|
sl@0
|
100 |
#if defined(sun) || defined(__ultrix)
|
sl@0
|
101 |
#define _POSIX_SOURCE
|
sl@0
|
102 |
#include <limits.h>
|
sl@0
|
103 |
#include <sys/param.h>
|
sl@0
|
104 |
#endif
|
sl@0
|
105 |
|
sl@0
|
106 |
#if !defined(TIMES) && !defined(OPENSSL_SYS_VXWORKS) && !defined(OPENSSL_SYS_NETWARE)
|
sl@0
|
107 |
#include <sys/timeb.h>
|
sl@0
|
108 |
#endif
|
sl@0
|
109 |
|
sl@0
|
110 |
#ifdef OPENSSL_SYS_WIN32
|
sl@0
|
111 |
#include <windows.h>
|
sl@0
|
112 |
#endif
|
sl@0
|
113 |
|
sl@0
|
114 |
/* The following if from times(3) man page. It may need to be changed */
|
sl@0
|
115 |
#ifndef HZ
|
sl@0
|
116 |
# if defined(_SC_CLK_TCK) \
|
sl@0
|
117 |
&& (!defined(OPENSSL_SYS_VMS) || __CTRL_VER >= 70000000)
|
sl@0
|
118 |
/* # define HZ ((double)sysconf(_SC_CLK_TCK)) */
|
sl@0
|
119 |
//# define HZ sysconf(_SC_CLK_TCK) TODO to uncomment N COMMENT NEXT line
|
sl@0
|
120 |
# define HZ 100.0
|
sl@0
|
121 |
# else
|
sl@0
|
122 |
# ifndef CLK_TCK
|
sl@0
|
123 |
# ifndef _BSD_CLK_TCK_ /* FreeBSD hack */
|
sl@0
|
124 |
# define HZ 100.0
|
sl@0
|
125 |
# else /* _BSD_CLK_TCK_ */
|
sl@0
|
126 |
# define HZ ((double)_BSD_CLK_TCK_)
|
sl@0
|
127 |
# endif
|
sl@0
|
128 |
# else /* CLK_TCK */
|
sl@0
|
129 |
# define HZ ((double)CLK_TCK)
|
sl@0
|
130 |
# endif
|
sl@0
|
131 |
# endif
|
sl@0
|
132 |
#endif
|
sl@0
|
133 |
|
sl@0
|
134 |
struct ms_tm
|
sl@0
|
135 |
{
|
sl@0
|
136 |
#ifdef TIMES
|
sl@0
|
137 |
struct tms ms_tms;
|
sl@0
|
138 |
#else
|
sl@0
|
139 |
# ifdef OPENSSL_SYS_WIN32
|
sl@0
|
140 |
HANDLE thread_id;
|
sl@0
|
141 |
FILETIME ms_win32;
|
sl@0
|
142 |
# elif defined (OPENSSL_SYS_NETWARE)
|
sl@0
|
143 |
clock_t ms_clock;
|
sl@0
|
144 |
# else
|
sl@0
|
145 |
# ifdef OPENSSL_SYS_VXWORKS
|
sl@0
|
146 |
unsigned long ticks;
|
sl@0
|
147 |
# else
|
sl@0
|
148 |
struct timeb ms_timeb;
|
sl@0
|
149 |
# endif
|
sl@0
|
150 |
# endif
|
sl@0
|
151 |
#endif
|
sl@0
|
152 |
};
|
sl@0
|
153 |
|
sl@0
|
154 |
EXPORT_C MS_TM *ms_time_new(void)
|
sl@0
|
155 |
{
|
sl@0
|
156 |
MS_TM *ret;
|
sl@0
|
157 |
|
sl@0
|
158 |
ret=(MS_TM *)OPENSSL_malloc(sizeof(MS_TM));
|
sl@0
|
159 |
if (ret == NULL)
|
sl@0
|
160 |
return(NULL);
|
sl@0
|
161 |
memset(ret,0,sizeof(MS_TM));
|
sl@0
|
162 |
#ifdef OPENSSL_SYS_WIN32
|
sl@0
|
163 |
ret->thread_id=GetCurrentThread();
|
sl@0
|
164 |
#endif
|
sl@0
|
165 |
return ret;
|
sl@0
|
166 |
}
|
sl@0
|
167 |
|
sl@0
|
168 |
EXPORT_C void ms_time_free(MS_TM *a)
|
sl@0
|
169 |
{
|
sl@0
|
170 |
if (a != NULL)
|
sl@0
|
171 |
OPENSSL_free(a);
|
sl@0
|
172 |
}
|
sl@0
|
173 |
|
sl@0
|
174 |
EXPORT_C void ms_time_get(MS_TM *tm)
|
sl@0
|
175 |
{
|
sl@0
|
176 |
#ifdef OPENSSL_SYS_WIN32
|
sl@0
|
177 |
FILETIME tmpa,tmpb,tmpc;
|
sl@0
|
178 |
#endif
|
sl@0
|
179 |
|
sl@0
|
180 |
#ifdef TIMES
|
sl@0
|
181 |
//times(&tm->ms_tms);TODO to uncomment
|
sl@0
|
182 |
#else
|
sl@0
|
183 |
# ifdef OPENSSL_SYS_WIN32
|
sl@0
|
184 |
GetThreadTimes(tm->thread_id,&tmpa,&tmpb,&tmpc,&(tm->ms_win32));
|
sl@0
|
185 |
# elif defined (OPENSSL_SYS_NETWARE)
|
sl@0
|
186 |
tm->ms_clock = clock();
|
sl@0
|
187 |
# else
|
sl@0
|
188 |
# ifdef OPENSSL_SYS_VXWORKS
|
sl@0
|
189 |
tm->ticks = tickGet();
|
sl@0
|
190 |
# else
|
sl@0
|
191 |
ftime(&tm->ms_timeb);
|
sl@0
|
192 |
# endif
|
sl@0
|
193 |
# endif
|
sl@0
|
194 |
#endif
|
sl@0
|
195 |
}
|
sl@0
|
196 |
|
sl@0
|
197 |
EXPORT_C double ms_time_diff(MS_TM *a, MS_TM *b)
|
sl@0
|
198 |
{
|
sl@0
|
199 |
double ret;
|
sl@0
|
200 |
|
sl@0
|
201 |
#ifdef TIMES
|
sl@0
|
202 |
ret = HZ;
|
sl@0
|
203 |
ret = (b->ms_tms.tms_utime-a->ms_tms.tms_utime) / ret;
|
sl@0
|
204 |
#else
|
sl@0
|
205 |
# ifdef OPENSSL_SYS_WIN32
|
sl@0
|
206 |
{
|
sl@0
|
207 |
#ifdef __GNUC__
|
sl@0
|
208 |
signed long long la,lb;
|
sl@0
|
209 |
#else
|
sl@0
|
210 |
signed _int64 la,lb;
|
sl@0
|
211 |
#endif
|
sl@0
|
212 |
la=a->ms_win32.dwHighDateTime;
|
sl@0
|
213 |
lb=b->ms_win32.dwHighDateTime;
|
sl@0
|
214 |
la<<=32;
|
sl@0
|
215 |
lb<<=32;
|
sl@0
|
216 |
la+=a->ms_win32.dwLowDateTime;
|
sl@0
|
217 |
lb+=b->ms_win32.dwLowDateTime;
|
sl@0
|
218 |
ret=((double)(lb-la))/1e7;
|
sl@0
|
219 |
}
|
sl@0
|
220 |
# elif defined (OPENSSL_SYS_NETWARE)
|
sl@0
|
221 |
ret= (double)(b->ms_clock - a->ms_clock);
|
sl@0
|
222 |
# else
|
sl@0
|
223 |
# ifdef OPENSSL_SYS_VXWORKS
|
sl@0
|
224 |
ret = (double)(b->ticks - a->ticks) / (double)sysClkRateGet();
|
sl@0
|
225 |
# else
|
sl@0
|
226 |
ret= (double)(b->ms_timeb.time-a->ms_timeb.time)+
|
sl@0
|
227 |
(((double)b->ms_timeb.millitm)-
|
sl@0
|
228 |
((double)a->ms_timeb.millitm))/1000.0;
|
sl@0
|
229 |
# endif
|
sl@0
|
230 |
# endif
|
sl@0
|
231 |
#endif
|
sl@0
|
232 |
return((ret < 0.0000001)?0.0000001:ret);
|
sl@0
|
233 |
}
|
sl@0
|
234 |
|
sl@0
|
235 |
EXPORT_C int ms_time_cmp(const MS_TM *a, const MS_TM *b)
|
sl@0
|
236 |
{
|
sl@0
|
237 |
double d;
|
sl@0
|
238 |
int ret;
|
sl@0
|
239 |
|
sl@0
|
240 |
#ifdef TIMES
|
sl@0
|
241 |
d = HZ;
|
sl@0
|
242 |
d = (b->ms_tms.tms_utime-a->ms_tms.tms_utime) / d;
|
sl@0
|
243 |
#else
|
sl@0
|
244 |
# ifdef OPENSSL_SYS_WIN32
|
sl@0
|
245 |
d =(b->ms_win32.dwHighDateTime&0x000fffff)*10+b->ms_win32.dwLowDateTime/1e7;
|
sl@0
|
246 |
d-=(a->ms_win32.dwHighDateTime&0x000fffff)*10+a->ms_win32.dwLowDateTime/1e7;
|
sl@0
|
247 |
# elif defined (OPENSSL_SYS_NETWARE)
|
sl@0
|
248 |
d= (double)(b->ms_clock - a->ms_clock);
|
sl@0
|
249 |
# else
|
sl@0
|
250 |
# ifdef OPENSSL_SYS_VXWORKS
|
sl@0
|
251 |
d = (b->ticks - a->ticks);
|
sl@0
|
252 |
# else
|
sl@0
|
253 |
d= (double)(b->ms_timeb.time-a->ms_timeb.time)+
|
sl@0
|
254 |
(((double)b->ms_timeb.millitm)-(double)a->ms_timeb.millitm)/1000.0;
|
sl@0
|
255 |
# endif
|
sl@0
|
256 |
# endif
|
sl@0
|
257 |
#endif
|
sl@0
|
258 |
if (d == 0.0)
|
sl@0
|
259 |
ret=0;
|
sl@0
|
260 |
else if (d < 0)
|
sl@0
|
261 |
ret= -1;
|
sl@0
|
262 |
else
|
sl@0
|
263 |
ret=1;
|
sl@0
|
264 |
return(ret);
|
sl@0
|
265 |
}
|
sl@0
|
266 |
|