sl@0: /* crypto/tmdiff.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: #include sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include sl@0: #if !defined(OPENSSL_SYS_MSDOS) sl@0: #include OPENSSL_UNISTD sl@0: #endif sl@0: sl@0: sl@0: #ifdef TIMEB sl@0: #undef OPENSSL_SYS_WIN32 sl@0: #undef TIMES sl@0: #endif sl@0: sl@0: #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: # define TIMES sl@0: #endif sl@0: sl@0: #ifdef OPENSSL_SYS_NETWARE sl@0: #undef TIMES sl@0: #endif sl@0: sl@0: sl@0: #if !defined(_IRIX) || defined (OPENSSL_SYS_NETWARE) sl@0: # include sl@0: #endif sl@0: sl@0: sl@0: sl@0: #ifdef TIMES sl@0: # include sl@0: # include sl@0: #endif sl@0: sl@0: /* Depending on the VMS version, the tms structure is perhaps defined. sl@0: The __TMS macro will show if it was. If it wasn't defined, we should sl@0: undefine TIMES, since that tells the rest of the program how things sl@0: should be handled. -- Richard Levitte */ sl@0: #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS) sl@0: #undef TIMES sl@0: #endif sl@0: sl@0: #if defined(sun) || defined(__ultrix) sl@0: #define _POSIX_SOURCE sl@0: #include sl@0: #include sl@0: #endif sl@0: sl@0: #if !defined(TIMES) && !defined(OPENSSL_SYS_VXWORKS) && !defined(OPENSSL_SYS_NETWARE) sl@0: #include sl@0: #endif sl@0: sl@0: #ifdef OPENSSL_SYS_WIN32 sl@0: #include sl@0: #endif sl@0: sl@0: /* The following if from times(3) man page. It may need to be changed */ sl@0: #ifndef HZ sl@0: # if defined(_SC_CLK_TCK) \ sl@0: && (!defined(OPENSSL_SYS_VMS) || __CTRL_VER >= 70000000) sl@0: /* # define HZ ((double)sysconf(_SC_CLK_TCK)) */ sl@0: //# define HZ sysconf(_SC_CLK_TCK) TODO to uncomment N COMMENT NEXT line sl@0: # define HZ 100.0 sl@0: # else sl@0: # ifndef CLK_TCK sl@0: # ifndef _BSD_CLK_TCK_ /* FreeBSD hack */ sl@0: # define HZ 100.0 sl@0: # else /* _BSD_CLK_TCK_ */ sl@0: # define HZ ((double)_BSD_CLK_TCK_) sl@0: # endif sl@0: # else /* CLK_TCK */ sl@0: # define HZ ((double)CLK_TCK) sl@0: # endif sl@0: # endif sl@0: #endif sl@0: sl@0: struct ms_tm sl@0: { sl@0: #ifdef TIMES sl@0: struct tms ms_tms; sl@0: #else sl@0: # ifdef OPENSSL_SYS_WIN32 sl@0: HANDLE thread_id; sl@0: FILETIME ms_win32; sl@0: # elif defined (OPENSSL_SYS_NETWARE) sl@0: clock_t ms_clock; sl@0: # else sl@0: # ifdef OPENSSL_SYS_VXWORKS sl@0: unsigned long ticks; sl@0: # else sl@0: struct timeb ms_timeb; sl@0: # endif sl@0: # endif sl@0: #endif sl@0: }; sl@0: sl@0: EXPORT_C MS_TM *ms_time_new(void) sl@0: { sl@0: MS_TM *ret; sl@0: sl@0: ret=(MS_TM *)OPENSSL_malloc(sizeof(MS_TM)); sl@0: if (ret == NULL) sl@0: return(NULL); sl@0: memset(ret,0,sizeof(MS_TM)); sl@0: #ifdef OPENSSL_SYS_WIN32 sl@0: ret->thread_id=GetCurrentThread(); sl@0: #endif sl@0: return ret; sl@0: } sl@0: sl@0: EXPORT_C void ms_time_free(MS_TM *a) sl@0: { sl@0: if (a != NULL) sl@0: OPENSSL_free(a); sl@0: } sl@0: sl@0: EXPORT_C void ms_time_get(MS_TM *tm) sl@0: { sl@0: #ifdef OPENSSL_SYS_WIN32 sl@0: FILETIME tmpa,tmpb,tmpc; sl@0: #endif sl@0: sl@0: #ifdef TIMES sl@0: //times(&tm->ms_tms);TODO to uncomment sl@0: #else sl@0: # ifdef OPENSSL_SYS_WIN32 sl@0: GetThreadTimes(tm->thread_id,&tmpa,&tmpb,&tmpc,&(tm->ms_win32)); sl@0: # elif defined (OPENSSL_SYS_NETWARE) sl@0: tm->ms_clock = clock(); sl@0: # else sl@0: # ifdef OPENSSL_SYS_VXWORKS sl@0: tm->ticks = tickGet(); sl@0: # else sl@0: ftime(&tm->ms_timeb); sl@0: # endif sl@0: # endif sl@0: #endif sl@0: } sl@0: sl@0: EXPORT_C double ms_time_diff(MS_TM *a, MS_TM *b) sl@0: { sl@0: double ret; sl@0: sl@0: #ifdef TIMES sl@0: ret = HZ; sl@0: ret = (b->ms_tms.tms_utime-a->ms_tms.tms_utime) / ret; sl@0: #else sl@0: # ifdef OPENSSL_SYS_WIN32 sl@0: { sl@0: #ifdef __GNUC__ sl@0: signed long long la,lb; sl@0: #else sl@0: signed _int64 la,lb; sl@0: #endif sl@0: la=a->ms_win32.dwHighDateTime; sl@0: lb=b->ms_win32.dwHighDateTime; sl@0: la<<=32; sl@0: lb<<=32; sl@0: la+=a->ms_win32.dwLowDateTime; sl@0: lb+=b->ms_win32.dwLowDateTime; sl@0: ret=((double)(lb-la))/1e7; sl@0: } sl@0: # elif defined (OPENSSL_SYS_NETWARE) sl@0: ret= (double)(b->ms_clock - a->ms_clock); sl@0: # else sl@0: # ifdef OPENSSL_SYS_VXWORKS sl@0: ret = (double)(b->ticks - a->ticks) / (double)sysClkRateGet(); sl@0: # else sl@0: ret= (double)(b->ms_timeb.time-a->ms_timeb.time)+ sl@0: (((double)b->ms_timeb.millitm)- sl@0: ((double)a->ms_timeb.millitm))/1000.0; sl@0: # endif sl@0: # endif sl@0: #endif sl@0: return((ret < 0.0000001)?0.0000001:ret); sl@0: } sl@0: sl@0: EXPORT_C int ms_time_cmp(const MS_TM *a, const MS_TM *b) sl@0: { sl@0: double d; sl@0: int ret; sl@0: sl@0: #ifdef TIMES sl@0: d = HZ; sl@0: d = (b->ms_tms.tms_utime-a->ms_tms.tms_utime) / d; sl@0: #else sl@0: # ifdef OPENSSL_SYS_WIN32 sl@0: d =(b->ms_win32.dwHighDateTime&0x000fffff)*10+b->ms_win32.dwLowDateTime/1e7; sl@0: d-=(a->ms_win32.dwHighDateTime&0x000fffff)*10+a->ms_win32.dwLowDateTime/1e7; sl@0: # elif defined (OPENSSL_SYS_NETWARE) sl@0: d= (double)(b->ms_clock - a->ms_clock); sl@0: # else sl@0: # ifdef OPENSSL_SYS_VXWORKS sl@0: d = (b->ticks - a->ticks); sl@0: # else sl@0: d= (double)(b->ms_timeb.time-a->ms_timeb.time)+ sl@0: (((double)b->ms_timeb.millitm)-(double)a->ms_timeb.millitm)/1000.0; sl@0: # endif sl@0: # endif sl@0: #endif sl@0: if (d == 0.0) sl@0: ret=0; sl@0: else if (d < 0) sl@0: ret= -1; sl@0: else sl@0: ret=1; sl@0: return(ret); sl@0: } sl@0: