This commit was generated by cvs2svn to track changes on a CVS vendor
[openssl.git] / crypto / tmdiff.c
1 /* crypto/tmdiff.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 #include <stdio.h>
59 #include <stdlib.h>
60
61 #ifndef MSDOS
62 #  ifndef WIN32
63 #  define TIMES
64 #  endif
65 #endif
66
67 #ifndef VMS
68 #  ifndef _IRIX
69 #   include <time.h>
70 #  endif
71 #  ifdef TIMES
72 #    include <sys/types.h>
73 #    include <sys/times.h>
74 #  endif
75 #else /* VMS */
76 #  include <types.h>
77         struct tms {
78                 time_t tms_utime;
79                 time_t tms_stime;
80                 time_t tms_uchild;      /* I dunno...  */
81                 time_t tms_uchildsys;   /* so these names are a guess :-) */
82                 }
83 #endif /* VMS */
84
85 #ifdef sun
86 #include <limits.h>
87 #include <sys/param.h>
88 #endif
89
90 #ifndef TIMES
91 #include <sys/timeb.h>
92 #endif
93
94 #ifdef WIN32
95 #include <windows.h>
96 #endif
97
98 /* The following if from times(3) man page.  It may need to be changed */
99 #ifndef HZ
100 # ifndef CLK_TCK
101 #  ifndef _BSD_CLK_TCK_ /* FreeBSD hack */
102 #   ifndef VMS
103 #    define HZ  100.0
104 #   else /* VMS */
105 #    define HZ  100.0
106 #   endif
107 #  else /* _BSD_CLK_TCK_ */
108 #   define HZ ((double)_BSD_CLK_TCK_)
109 #  endif
110 # else /* CLK_TCK */
111 #  define HZ ((double)CLK_TCK)
112 # endif
113 #endif
114
115 typedef struct ms_tm
116         {
117 #ifdef TIMES
118         struct tms ms_tms;
119 #else
120 #  ifdef WIN32
121         HANDLE thread_id;
122         FILETIME ms_win32;
123 #  else
124         struct timeb ms_timeb;
125 #  endif
126 #endif
127         } MS_TM;
128
129 char *ms_time_init()
130         {
131         MS_TM *ret;
132
133         ret=malloc(sizeof(MS_TM));
134         if (ret == NULL)
135                 return(NULL);
136         memset(ret,0,sizeof(MS_TM));
137 #ifdef WIN32
138         ret->thread_id=GetCurrentThread();
139 #endif
140         return((char *)ret);
141         }
142
143 void ms_time_final(a)
144 char *a;
145         {
146         if (a != NULL)
147                 free(a);
148         }
149
150 void ms_time_get(a)
151 char *a;
152         {
153         MS_TM *tm=(MS_TM *)a;
154     FILETIME tmpa,tmpb,tmpc;
155
156 #ifdef TIMES
157     printf("AAA\n");
158         times(&tm->ms_tms);
159 #else
160 #  ifdef WIN32
161         GetThreadTimes(tm->thread_id,&tmpa,&tmpb,&tmpc,&(tm->ms_win32));
162 #  else
163     printf("CCC\n");
164         ftime(tm->ms_timeb);
165 #  endif
166 #endif
167         }
168
169 double ms_time_diff(ap,bp)
170 char *ap,*bp;
171         {
172         MS_TM *a=(MS_TM *)ap;
173         MS_TM *b=(MS_TM *)bp;
174         double ret;
175
176 #ifdef TIMES
177         ret=(b->ms_tms.tms_utime-a->ms_tms.tms_utime)/HZ;
178 #else
179 # ifdef WIN32
180         ret =(double)(b->ms_win32.dwHighDateTime&0x000fffff)*10+
181                 b->ms_win32.dwLowDateTime/1e7;
182         ret-=(double)(a->ms_win32.dwHighDateTime&0x000fffff)*10+a->ms_win32.dwLowDateTime/1e7;
183 # else
184         ret=     (double)(b->time-a->time)+
185                 ((double)((unsigned long)b->mullitm-(unsigned long)))/1000.0;
186 #  endif
187 #endif
188         return((ret < 0.0000001)?0.0000001:ret);
189         }
190
191 int ms_time_cmp(ap,bp)
192 char *ap,*bp;
193         {
194         MS_TM *a=(MS_TM *)ap,*b=(MS_TM *)bp;
195         double d;
196         int ret;
197
198 #ifdef TIMES
199         d=(b->ms_tms.tms_utime-a->ms_tms.tms_utime)/HZ;
200 #else
201 # ifdef WIN32
202         d =(b->ms_win32.dwHighDateTime&0x000fffff)*10+b->ms_win32.dwLowDateTime/1e7;
203         d-=(a->ms_win32.dwHighDateTime&0x000fffff)*10+a->ms_win32.dwLowDateTime/1e7;
204 # else
205         d=       (double)(b->time-a->time)+
206                 ((double)((unsigned long)b->mullitm-(unsigned long)))/1000.0;
207 #  endif
208 #endif
209         if (d == 0.0)
210                 ret=0;
211         else if (d < 0)
212                 ret= -1;
213         else
214                 ret=1;
215         return(ret);
216         }
217