10Sstevel@tonic-gate /* crypto/tmdiff.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate #include <stdio.h>
590Sstevel@tonic-gate #include <stdlib.h>
600Sstevel@tonic-gate #include "cryptlib.h"
610Sstevel@tonic-gate #include <openssl/tmdiff.h>
620Sstevel@tonic-gate #if !defined(OPENSSL_SYS_MSDOS)
630Sstevel@tonic-gate #include OPENSSL_UNISTD
640Sstevel@tonic-gate #endif
650Sstevel@tonic-gate
660Sstevel@tonic-gate #ifdef TIMEB
670Sstevel@tonic-gate #undef OPENSSL_SYS_WIN32
680Sstevel@tonic-gate #undef TIMES
690Sstevel@tonic-gate #endif
700Sstevel@tonic-gate
710Sstevel@tonic-gate #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN32) && !(defined(OPENSSL_SYS_VMS) || defined(__DECC)) && !defined(OPENSSL_SYS_MACOSX_RHAPSODY) && !defined(OPENSSL_SYS_VXWORKS)
720Sstevel@tonic-gate # define TIMES
730Sstevel@tonic-gate #endif
740Sstevel@tonic-gate
75*2139Sjp161948 #ifdef OPENSSL_SYS_NETWARE
76*2139Sjp161948 #undef TIMES
77*2139Sjp161948 #endif
78*2139Sjp161948
79*2139Sjp161948 #if !defined(_IRIX) || defined (OPENSSL_SYS_NETWARE)
800Sstevel@tonic-gate # include <time.h>
810Sstevel@tonic-gate #endif
820Sstevel@tonic-gate #ifdef TIMES
830Sstevel@tonic-gate # include <sys/types.h>
840Sstevel@tonic-gate # include <sys/times.h>
850Sstevel@tonic-gate #endif
860Sstevel@tonic-gate
870Sstevel@tonic-gate /* Depending on the VMS version, the tms structure is perhaps defined.
880Sstevel@tonic-gate The __TMS macro will show if it was. If it wasn't defined, we should
890Sstevel@tonic-gate undefine TIMES, since that tells the rest of the program how things
900Sstevel@tonic-gate should be handled. -- Richard Levitte */
910Sstevel@tonic-gate #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
920Sstevel@tonic-gate #undef TIMES
930Sstevel@tonic-gate #endif
940Sstevel@tonic-gate
950Sstevel@tonic-gate #if defined(sun) || defined(__ultrix)
960Sstevel@tonic-gate #define _POSIX_SOURCE
970Sstevel@tonic-gate #include <limits.h>
980Sstevel@tonic-gate #include <sys/param.h>
990Sstevel@tonic-gate #endif
1000Sstevel@tonic-gate
101*2139Sjp161948 #if !defined(TIMES) && !defined(OPENSSL_SYS_VXWORKS) && !defined(OPENSSL_SYS_NETWARE)
1020Sstevel@tonic-gate #include <sys/timeb.h>
1030Sstevel@tonic-gate #endif
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate #ifdef OPENSSL_SYS_WIN32
1060Sstevel@tonic-gate #include <windows.h>
1070Sstevel@tonic-gate #endif
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate /* The following if from times(3) man page. It may need to be changed */
1100Sstevel@tonic-gate #ifndef HZ
1110Sstevel@tonic-gate # if defined(_SC_CLK_TCK) \
1120Sstevel@tonic-gate && (!defined(OPENSSL_SYS_VMS) || __CTRL_VER >= 70000000)
113*2139Sjp161948 /* # define HZ ((double)sysconf(_SC_CLK_TCK)) */
114*2139Sjp161948 # define HZ sysconf(_SC_CLK_TCK)
1150Sstevel@tonic-gate # else
1160Sstevel@tonic-gate # ifndef CLK_TCK
1170Sstevel@tonic-gate # ifndef _BSD_CLK_TCK_ /* FreeBSD hack */
1180Sstevel@tonic-gate # define HZ 100.0
1190Sstevel@tonic-gate # else /* _BSD_CLK_TCK_ */
1200Sstevel@tonic-gate # define HZ ((double)_BSD_CLK_TCK_)
1210Sstevel@tonic-gate # endif
1220Sstevel@tonic-gate # else /* CLK_TCK */
1230Sstevel@tonic-gate # define HZ ((double)CLK_TCK)
1240Sstevel@tonic-gate # endif
1250Sstevel@tonic-gate # endif
1260Sstevel@tonic-gate #endif
1270Sstevel@tonic-gate
128*2139Sjp161948 struct ms_tm
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate #ifdef TIMES
1310Sstevel@tonic-gate struct tms ms_tms;
1320Sstevel@tonic-gate #else
1330Sstevel@tonic-gate # ifdef OPENSSL_SYS_WIN32
1340Sstevel@tonic-gate HANDLE thread_id;
1350Sstevel@tonic-gate FILETIME ms_win32;
136*2139Sjp161948 # elif defined (OPENSSL_SYS_NETWARE)
137*2139Sjp161948 clock_t ms_clock;
1380Sstevel@tonic-gate # else
1390Sstevel@tonic-gate # ifdef OPENSSL_SYS_VXWORKS
1400Sstevel@tonic-gate unsigned long ticks;
1410Sstevel@tonic-gate # else
1420Sstevel@tonic-gate struct timeb ms_timeb;
1430Sstevel@tonic-gate # endif
1440Sstevel@tonic-gate # endif
1450Sstevel@tonic-gate #endif
146*2139Sjp161948 };
1470Sstevel@tonic-gate
ms_time_new(void)148*2139Sjp161948 MS_TM *ms_time_new(void)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate MS_TM *ret;
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate ret=(MS_TM *)OPENSSL_malloc(sizeof(MS_TM));
1530Sstevel@tonic-gate if (ret == NULL)
1540Sstevel@tonic-gate return(NULL);
1550Sstevel@tonic-gate memset(ret,0,sizeof(MS_TM));
1560Sstevel@tonic-gate #ifdef OPENSSL_SYS_WIN32
1570Sstevel@tonic-gate ret->thread_id=GetCurrentThread();
1580Sstevel@tonic-gate #endif
159*2139Sjp161948 return ret;
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate
ms_time_free(MS_TM * a)162*2139Sjp161948 void ms_time_free(MS_TM *a)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate if (a != NULL)
1650Sstevel@tonic-gate OPENSSL_free(a);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate
ms_time_get(MS_TM * tm)168*2139Sjp161948 void ms_time_get(MS_TM *tm)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate #ifdef OPENSSL_SYS_WIN32
1710Sstevel@tonic-gate FILETIME tmpa,tmpb,tmpc;
1720Sstevel@tonic-gate #endif
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate #ifdef TIMES
1750Sstevel@tonic-gate times(&tm->ms_tms);
1760Sstevel@tonic-gate #else
1770Sstevel@tonic-gate # ifdef OPENSSL_SYS_WIN32
1780Sstevel@tonic-gate GetThreadTimes(tm->thread_id,&tmpa,&tmpb,&tmpc,&(tm->ms_win32));
179*2139Sjp161948 # elif defined (OPENSSL_SYS_NETWARE)
180*2139Sjp161948 tm->ms_clock = clock();
1810Sstevel@tonic-gate # else
1820Sstevel@tonic-gate # ifdef OPENSSL_SYS_VXWORKS
1830Sstevel@tonic-gate tm->ticks = tickGet();
1840Sstevel@tonic-gate # else
1850Sstevel@tonic-gate ftime(&tm->ms_timeb);
1860Sstevel@tonic-gate # endif
1870Sstevel@tonic-gate # endif
1880Sstevel@tonic-gate #endif
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate
ms_time_diff(MS_TM * a,MS_TM * b)191*2139Sjp161948 double ms_time_diff(MS_TM *a, MS_TM *b)
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate double ret;
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate #ifdef TIMES
196*2139Sjp161948 ret = HZ;
197*2139Sjp161948 ret = (b->ms_tms.tms_utime-a->ms_tms.tms_utime) / ret;
1980Sstevel@tonic-gate #else
1990Sstevel@tonic-gate # ifdef OPENSSL_SYS_WIN32
2000Sstevel@tonic-gate {
2010Sstevel@tonic-gate #ifdef __GNUC__
2020Sstevel@tonic-gate signed long long la,lb;
2030Sstevel@tonic-gate #else
2040Sstevel@tonic-gate signed _int64 la,lb;
2050Sstevel@tonic-gate #endif
2060Sstevel@tonic-gate la=a->ms_win32.dwHighDateTime;
2070Sstevel@tonic-gate lb=b->ms_win32.dwHighDateTime;
2080Sstevel@tonic-gate la<<=32;
2090Sstevel@tonic-gate lb<<=32;
2100Sstevel@tonic-gate la+=a->ms_win32.dwLowDateTime;
2110Sstevel@tonic-gate lb+=b->ms_win32.dwLowDateTime;
2120Sstevel@tonic-gate ret=((double)(lb-la))/1e7;
2130Sstevel@tonic-gate }
214*2139Sjp161948 # elif defined (OPENSSL_SYS_NETWARE)
215*2139Sjp161948 ret= (double)(b->ms_clock - a->ms_clock);
2160Sstevel@tonic-gate # else
2170Sstevel@tonic-gate # ifdef OPENSSL_SYS_VXWORKS
2180Sstevel@tonic-gate ret = (double)(b->ticks - a->ticks) / (double)sysClkRateGet();
2190Sstevel@tonic-gate # else
2200Sstevel@tonic-gate ret= (double)(b->ms_timeb.time-a->ms_timeb.time)+
2210Sstevel@tonic-gate (((double)b->ms_timeb.millitm)-
2220Sstevel@tonic-gate ((double)a->ms_timeb.millitm))/1000.0;
2230Sstevel@tonic-gate # endif
2240Sstevel@tonic-gate # endif
2250Sstevel@tonic-gate #endif
2260Sstevel@tonic-gate return((ret < 0.0000001)?0.0000001:ret);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate
ms_time_cmp(const MS_TM * a,const MS_TM * b)229*2139Sjp161948 int ms_time_cmp(const MS_TM *a, const MS_TM *b)
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate double d;
2320Sstevel@tonic-gate int ret;
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate #ifdef TIMES
235*2139Sjp161948 d = HZ;
236*2139Sjp161948 d = (b->ms_tms.tms_utime-a->ms_tms.tms_utime) / d;
2370Sstevel@tonic-gate #else
2380Sstevel@tonic-gate # ifdef OPENSSL_SYS_WIN32
2390Sstevel@tonic-gate d =(b->ms_win32.dwHighDateTime&0x000fffff)*10+b->ms_win32.dwLowDateTime/1e7;
2400Sstevel@tonic-gate d-=(a->ms_win32.dwHighDateTime&0x000fffff)*10+a->ms_win32.dwLowDateTime/1e7;
241*2139Sjp161948 # elif defined (OPENSSL_SYS_NETWARE)
242*2139Sjp161948 d= (double)(b->ms_clock - a->ms_clock);
2430Sstevel@tonic-gate # else
2440Sstevel@tonic-gate # ifdef OPENSSL_SYS_VXWORKS
2450Sstevel@tonic-gate d = (b->ticks - a->ticks);
2460Sstevel@tonic-gate # else
2470Sstevel@tonic-gate d= (double)(b->ms_timeb.time-a->ms_timeb.time)+
2480Sstevel@tonic-gate (((double)b->ms_timeb.millitm)-(double)a->ms_timeb.millitm)/1000.0;
2490Sstevel@tonic-gate # endif
2500Sstevel@tonic-gate # endif
2510Sstevel@tonic-gate #endif
2520Sstevel@tonic-gate if (d == 0.0)
2530Sstevel@tonic-gate ret=0;
2540Sstevel@tonic-gate else if (d < 0)
2550Sstevel@tonic-gate ret= -1;
2560Sstevel@tonic-gate else
2570Sstevel@tonic-gate ret=1;
2580Sstevel@tonic-gate return(ret);
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate
261