add .gitignore
[tedtools.git] / tools.c
1 /*
2  * Copyright (c) 2004 Teodor Sigaev <teodor@sigaev.ru>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *        notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *        notice, this list of conditions and the following disclaimer in the
12  *        documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *        may be used to endorse or promote products derived from this software
15  *        without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <ctype.h>
31 #include <sys/types.h>
32
33 #include "tmalloc.h"
34 #include "tlog.h"
35 #include "tools.h"
36
37 #define isXdigit(c) ( ((c)>='0' && (c)<='9') || ((c)>='A' && (c)<='F') || ((c)>='a' && (c)<='f') )
38
39 static u_int8_t str2hex[] = {
40           0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
41           0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
42           0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
43           0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  0,  0,  0,  0,  0,  0,
44           0, 10, 11, 12, 13, 14, 15,  0,  0,  0,  0,  0,  0,  0,  0,  0,
45           0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
46           0, 10, 11, 12, 13, 14, 15,  0,  0,  0,  0,  0,  0,  0,  0,  0,
47           0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
48           0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
49           0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
50           0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
51           0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
52           0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
53           0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
54           0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
55           0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0
56 }; 
57
58 u_int32_t 
59 strtox(char *src, char **end) {
60         u_int32_t res=0, i=0;
61         
62         while(src && *src && isXdigit(*src) && i<8) {
63                 res = res << 4;
64                 res |= str2hex[ *(unsigned char*)src ];
65                 src++;
66                 i++;
67         }
68
69         if ( end )
70                 *end=src;
71         return res;
72 }
73
74 u_int64_t 
75 strtox64(char *src, char **end) {
76         u_int32_t res=0, i=0;
77         
78         while(src && *src && isXdigit(*src) && i<16) {
79                 res = res << 4;
80                 res |= str2hex[ *(unsigned char*)src ];
81                 src++;
82                 i++;
83         }
84
85         if ( end )
86                 *end=src;
87         return res;
88 }
89
90 static char buf[32];
91 static char hex2str[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
92
93 char*     
94 xtostr(u_int32_t x) {
95         char *ptr=buf;
96         int i,wast=0;
97         for(i=28;i>=0;i-=4) {
98                 *ptr = hex2str[  (x>>i) & 0x0f ];
99                 if ( wast || *ptr!='0' || i==0 ) {
100                         ptr++;
101                         wast=1;
102                 }       
103         }
104         *ptr='\0';
105         return buf;
106 }
107
108 char*     
109 x64tostr(u_int64_t x) {
110         char *ptr=buf;
111         int i,wast=0;
112         for(i=60;i>=0;i-=4) {
113                 *ptr = hex2str[  (x>>i) & 0x0f ];
114                 if ( wast || *ptr!='0' || i==0 ) {
115                         ptr++;
116                         wast=1;
117                 }       
118         }
119         *ptr='\0';
120         return buf;
121 }
122
123 double 
124 timediff(struct timeval *begin, struct timeval *end) {
125         return ((double)( end->tv_sec - begin->tv_sec )) + ( (double)( end->tv_usec-begin->tv_usec ) ) / 1.0e+6; 
126 }
127
128 double 
129 elapsedtime(struct timeval *begin) {
130         struct timeval end;
131         gettimeofday(&end,NULL);
132         return timediff(begin,&end);
133 }