os/ossrv/ssl/libcrypto/src/crypto/tmdiff.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/tmdiff.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,266 @@
     1.4 +/* crypto/tmdiff.c */
     1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * This package is an SSL implementation written
     1.9 + * by Eric Young (eay@cryptsoft.com).
    1.10 + * The implementation was written so as to conform with Netscapes SSL.
    1.11 + * 
    1.12 + * This library is free for commercial and non-commercial use as long as
    1.13 + * the following conditions are aheared to.  The following conditions
    1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
    1.15 + * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
    1.16 + * included with this distribution is covered by the same copyright terms
    1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
    1.18 + * 
    1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
    1.20 + * the code are not to be removed.
    1.21 + * If this package is used in a product, Eric Young should be given attribution
    1.22 + * as the author of the parts of the library used.
    1.23 + * This can be in the form of a textual message at program startup or
    1.24 + * in documentation (online or textual) provided with the package.
    1.25 + * 
    1.26 + * Redistribution and use in source and binary forms, with or without
    1.27 + * modification, are permitted provided that the following conditions
    1.28 + * are met:
    1.29 + * 1. Redistributions of source code must retain the copyright
    1.30 + *    notice, this list of conditions and the following disclaimer.
    1.31 + * 2. Redistributions in binary form must reproduce the above copyright
    1.32 + *    notice, this list of conditions and the following disclaimer in the
    1.33 + *    documentation and/or other materials provided with the distribution.
    1.34 + * 3. All advertising materials mentioning features or use of this software
    1.35 + *    must display the following acknowledgement:
    1.36 + *    "This product includes cryptographic software written by
    1.37 + *     Eric Young (eay@cryptsoft.com)"
    1.38 + *    The word 'cryptographic' can be left out if the rouines from the library
    1.39 + *    being used are not cryptographic related :-).
    1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from 
    1.41 + *    the apps directory (application code) you must include an acknowledgement:
    1.42 + *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
    1.43 + * 
    1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
    1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.47 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
    1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.54 + * SUCH DAMAGE.
    1.55 + * 
    1.56 + * The licence and distribution terms for any publically available version or
    1.57 + * derivative of this code cannot be changed.  i.e. this code cannot simply be
    1.58 + * copied and put under another distribution licence
    1.59 + * [including the GNU Public Licence.]
    1.60 + */
    1.61 +#include <stdio.h>
    1.62 +#include <stdlib.h>
    1.63 +#include "cryptlib.h"
    1.64 +#include <openssl/tmdiff.h>
    1.65 +#if !defined(OPENSSL_SYS_MSDOS)
    1.66 +#include OPENSSL_UNISTD
    1.67 +#endif
    1.68 +
    1.69 +
    1.70 +#ifdef TIMEB
    1.71 +#undef OPENSSL_SYS_WIN32
    1.72 +#undef TIMES
    1.73 +#endif
    1.74 +
    1.75 +#if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN32) && !(defined(OPENSSL_SYS_VMS) || defined(__DECC)) && !defined(OPENSSL_SYS_MACOSX_RHAPSODY) && !defined(OPENSSL_SYS_VXWORKS)
    1.76 +# define TIMES
    1.77 +#endif
    1.78 +
    1.79 +#ifdef OPENSSL_SYS_NETWARE
    1.80 +#undef TIMES
    1.81 +#endif
    1.82 +
    1.83 +
    1.84 +#if !defined(_IRIX) || defined (OPENSSL_SYS_NETWARE)
    1.85 +#  include <time.h>
    1.86 +#endif
    1.87 +
    1.88 +
    1.89 +
    1.90 +#ifdef TIMES
    1.91 +#  include <sys/types.h>
    1.92 +#  include <sys/times.h>
    1.93 +#endif
    1.94 +
    1.95 +/* Depending on the VMS version, the tms structure is perhaps defined.
    1.96 +   The __TMS macro will show if it was.  If it wasn't defined, we should
    1.97 +   undefine TIMES, since that tells the rest of the program how things
    1.98 +   should be handled.				-- Richard Levitte */
    1.99 +#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
   1.100 +#undef TIMES
   1.101 +#endif
   1.102 +
   1.103 +#if defined(sun) || defined(__ultrix)
   1.104 +#define _POSIX_SOURCE
   1.105 +#include <limits.h>
   1.106 +#include <sys/param.h>
   1.107 +#endif
   1.108 +
   1.109 +#if !defined(TIMES) && !defined(OPENSSL_SYS_VXWORKS) && !defined(OPENSSL_SYS_NETWARE)
   1.110 +#include <sys/timeb.h>
   1.111 +#endif
   1.112 +
   1.113 +#ifdef OPENSSL_SYS_WIN32
   1.114 +#include <windows.h>
   1.115 +#endif
   1.116 +
   1.117 +/* The following if from times(3) man page.  It may need to be changed */
   1.118 +#ifndef HZ
   1.119 +# if defined(_SC_CLK_TCK) \
   1.120 +     && (!defined(OPENSSL_SYS_VMS) || __CTRL_VER >= 70000000)
   1.121 +/* #  define HZ ((double)sysconf(_SC_CLK_TCK)) */
   1.122 +//#  define HZ sysconf(_SC_CLK_TCK) TODO  to uncomment N COMMENT NEXT line
   1.123 +#  define HZ 100.0
   1.124 +# else
   1.125 +#  ifndef CLK_TCK
   1.126 +#   ifndef _BSD_CLK_TCK_ /* FreeBSD hack */
   1.127 +#    define HZ  100.0
   1.128 +#   else /* _BSD_CLK_TCK_ */
   1.129 +#    define HZ ((double)_BSD_CLK_TCK_)
   1.130 +#   endif
   1.131 +#  else /* CLK_TCK */
   1.132 +#   define HZ ((double)CLK_TCK)
   1.133 +#  endif
   1.134 +# endif
   1.135 +#endif
   1.136 +
   1.137 +struct ms_tm
   1.138 +	{
   1.139 +#ifdef TIMES
   1.140 +	struct tms ms_tms;
   1.141 +#else
   1.142 +#  ifdef OPENSSL_SYS_WIN32
   1.143 +	HANDLE thread_id;
   1.144 +	FILETIME ms_win32;
   1.145 +#  elif defined (OPENSSL_SYS_NETWARE)
   1.146 +   clock_t ms_clock;
   1.147 +#  else
   1.148 +#    ifdef OPENSSL_SYS_VXWORKS
   1.149 +          unsigned long ticks;
   1.150 +#    else
   1.151 +	struct timeb ms_timeb;
   1.152 +#    endif
   1.153 +#  endif
   1.154 +#endif
   1.155 +	};
   1.156 +
   1.157 +EXPORT_C MS_TM *ms_time_new(void)
   1.158 +	{
   1.159 +	MS_TM *ret;
   1.160 +
   1.161 +	ret=(MS_TM *)OPENSSL_malloc(sizeof(MS_TM));
   1.162 +	if (ret == NULL)
   1.163 +		return(NULL);
   1.164 +	memset(ret,0,sizeof(MS_TM));
   1.165 +#ifdef OPENSSL_SYS_WIN32
   1.166 +	ret->thread_id=GetCurrentThread();
   1.167 +#endif
   1.168 +	return ret;
   1.169 +	}
   1.170 +
   1.171 +EXPORT_C void ms_time_free(MS_TM *a)
   1.172 +	{
   1.173 +	if (a != NULL)
   1.174 +		OPENSSL_free(a);
   1.175 +	}
   1.176 +
   1.177 +EXPORT_C void ms_time_get(MS_TM *tm)
   1.178 +	{
   1.179 +#ifdef OPENSSL_SYS_WIN32
   1.180 +	FILETIME tmpa,tmpb,tmpc;
   1.181 +#endif
   1.182 +
   1.183 +#ifdef TIMES
   1.184 +	//times(&tm->ms_tms);TODO  to uncomment
   1.185 +#else
   1.186 +#  ifdef OPENSSL_SYS_WIN32
   1.187 +	GetThreadTimes(tm->thread_id,&tmpa,&tmpb,&tmpc,&(tm->ms_win32));
   1.188 +#  elif defined (OPENSSL_SYS_NETWARE)
   1.189 +   tm->ms_clock = clock();
   1.190 +#  else
   1.191 +#    ifdef OPENSSL_SYS_VXWORKS
   1.192 +        tm->ticks = tickGet();
   1.193 +#    else
   1.194 +	ftime(&tm->ms_timeb);
   1.195 +#    endif
   1.196 +#  endif
   1.197 +#endif
   1.198 +	}
   1.199 +
   1.200 +EXPORT_C double ms_time_diff(MS_TM *a, MS_TM *b)
   1.201 +	{
   1.202 +	double ret;
   1.203 +
   1.204 +#ifdef TIMES
   1.205 +	ret = HZ;
   1.206 +	ret = (b->ms_tms.tms_utime-a->ms_tms.tms_utime) / ret;
   1.207 +#else
   1.208 +# ifdef OPENSSL_SYS_WIN32
   1.209 +	{
   1.210 +#ifdef __GNUC__
   1.211 +	signed long long la,lb;
   1.212 +#else
   1.213 +	signed _int64 la,lb;
   1.214 +#endif
   1.215 +	la=a->ms_win32.dwHighDateTime;
   1.216 +	lb=b->ms_win32.dwHighDateTime;
   1.217 +	la<<=32;
   1.218 +	lb<<=32;
   1.219 +	la+=a->ms_win32.dwLowDateTime;
   1.220 +	lb+=b->ms_win32.dwLowDateTime;
   1.221 +	ret=((double)(lb-la))/1e7;
   1.222 +	}
   1.223 +# elif defined (OPENSSL_SYS_NETWARE)
   1.224 +    ret= (double)(b->ms_clock - a->ms_clock);
   1.225 +# else
   1.226 +#  ifdef OPENSSL_SYS_VXWORKS
   1.227 +        ret = (double)(b->ticks - a->ticks) / (double)sysClkRateGet();
   1.228 +#  else
   1.229 +	ret=	 (double)(b->ms_timeb.time-a->ms_timeb.time)+
   1.230 +		(((double)b->ms_timeb.millitm)-
   1.231 +		((double)a->ms_timeb.millitm))/1000.0;
   1.232 +#  endif
   1.233 +# endif
   1.234 +#endif
   1.235 +	return((ret < 0.0000001)?0.0000001:ret);
   1.236 +	}
   1.237 +
   1.238 +EXPORT_C int ms_time_cmp(const MS_TM *a, const MS_TM *b)
   1.239 +	{
   1.240 +	double d;
   1.241 +	int ret;
   1.242 +
   1.243 +#ifdef TIMES
   1.244 +	d = HZ;
   1.245 +	d = (b->ms_tms.tms_utime-a->ms_tms.tms_utime) / d;
   1.246 +#else
   1.247 +# ifdef OPENSSL_SYS_WIN32
   1.248 +	d =(b->ms_win32.dwHighDateTime&0x000fffff)*10+b->ms_win32.dwLowDateTime/1e7;
   1.249 +	d-=(a->ms_win32.dwHighDateTime&0x000fffff)*10+a->ms_win32.dwLowDateTime/1e7;
   1.250 +# elif defined (OPENSSL_SYS_NETWARE)
   1.251 +    d= (double)(b->ms_clock - a->ms_clock);
   1.252 +# else
   1.253 +#  ifdef OPENSSL_SYS_VXWORKS
   1.254 +        d = (b->ticks - a->ticks);
   1.255 +#  else
   1.256 +	d=	 (double)(b->ms_timeb.time-a->ms_timeb.time)+
   1.257 +		(((double)b->ms_timeb.millitm)-(double)a->ms_timeb.millitm)/1000.0;
   1.258 +#  endif
   1.259 +# endif
   1.260 +#endif
   1.261 +	if (d == 0.0)
   1.262 +		ret=0;
   1.263 +	else if (d < 0)
   1.264 +		ret= -1;
   1.265 +	else
   1.266 +		ret=1;
   1.267 +	return(ret);
   1.268 +	}
   1.269 +