-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.cpp
More file actions
204 lines (167 loc) · 4.36 KB
/
library.cpp
File metadata and controls
204 lines (167 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* RDNSLOGS 7.0
* Performs Reverse DNS lookups on multiple logfiles.
* This program is designed to work with the Analog Logfile analyzer.
*
* Supports compressed files
*
* Antonio C. Silvestri
* tonysilvestri@bytecodeman.com
*
***/
#include <iostream>
#include <ctype.h>
#include <string.h>
#include "globaldata.h"
#include "library.h"
#include "LookupIPRoutines.h"
using namespace std;
/**************************************************************/
bool nameOK(const char *str) {
if (strlen(str) == 0)
return false;
if (strcmp(str, UNKNOWNHOST) == 0)
return true;
for (; *str; str++) {
char ch = (char)tolower(*str);
if (!strchr("abcdefghijklmnopqrstuvwxyz0123456789-._", ch))
return false;
}
return true;
}
/**************************************************************
** like strcpy, except guaranteed to work with overlapping
** strings
*/
static inline void *strMove(void *d,void *s) {
return memmove(d, s, strlen((const char *)s) + 1);
}
/**************************************************************
** remove leading whitespace from a string
*/
char *rmlead(char *str) {
char *obuf;
if (str) {
for (obuf = str; *obuf && isspace(*obuf); ++obuf)
;
if (str != obuf)
strMove(str, obuf);
}
return str;
}
/**************************************************************
** remove trailing whitespace from a string
*/
char *rmtrail(char *str) {
size_t i;
if (str && 0 != (i = strlen(str))) {
while (--i >= 0) {
if (!isspace(str[i]))
break;
}
str[++i] = NULL;
}
return str;
}
/**************************************************************
** remove leading AND trailing whitespace from a string
*/
char *trim(char *str) {
return rmlead(rmtrail(str));
}
/**************************************************************
** remove any double quote characters from around a string
*/
char *rmquotes(char *str) {
str = trim(str);
if (*str == '"')
strMove(str, str + 1);
if (*(str + strlen(str) - 1) == '"')
*(str + strlen(str) - 1) = NULL;
return str;
}
/**************************************************************
** remove any parenthesis characters from around a string
*/
char *rmparen(char *str) {
str = trim(str);
if (*str == '(')
strMove(str, str + 1);
if (*(str + strlen(str) - 1) == ')')
*(str + strlen(str) - 1) = NULL;
return str;
}
/**************************************************************
** utility function to remove any leading and trailing spaces and quotes
*/
static char *stripquotes(char *str, char **next) {
char *tmp;
for (tmp = str; isspace(*tmp); tmp++)
;
if (tmp != str)
strMove(str, tmp);
if (*str == '"') {
for (tmp = str + 1; *tmp && *tmp != '"'; tmp++)
;
if (*tmp == '"')
*++tmp = NULL;
}
else {
for (tmp = str; !isspace(*tmp); tmp++)
;
if (*tmp == ' ')
*tmp = NULL;
}
if (next)
*next = tmp + 1;
return rmquotes(str);
}
/**************************************************************
** find the logfile specifier and remove any double quote characters
** from that string
*/
inline char *getLogFileSpec(char *str) {
return stripquotes(str, NULL);
}
/**************************************************************
** find the zip file type specifier and remove any double quote characters
** from that string
*/
inline char *getZipType(char *str, char *&next) {
return stripquotes(str, &next);
}
/**************************************************************
** Output Errmsg and terminate RDNSLOGS
*/
void endItAll(const char *errmsg) {
cerr << errmsg << endl;
cerr << ProductName << " Terminating" << endl;
exit(1);
}
//********************************************************************
// A case insensitive substring search
const char *stristr( const char *str, const char *substr) {
char *tmpstr = new char[strlen(str) + 1];
if (!tmpstr)
endItAll("Error in stristr memory allocation");
strcpy(tmpstr, str);
_strlwr(tmpstr);
char *tmpsubstr = new char[strlen(substr) + 1];
if (!tmpsubstr)
endItAll("Error in stristr memory allocation");
strcpy(tmpsubstr, substr);
_strlwr(tmpsubstr);
char *retval = strstr(tmpstr, tmpsubstr);
delete []tmpstr;
delete []tmpsubstr;
if (retval == NULL)
return NULL;
return str + (retval - tmpstr);
}
//********************************************************************
int myatoi(const char *p) {
int v = 0;
for (int i = 0; i < 3 && isdigit(*p); i++, p++)
v = 10 * v + *p - '0';
return v;
}