-
Notifications
You must be signed in to change notification settings - Fork 2
/
rig.cc
303 lines (274 loc) · 7.62 KB
/
rig.cc
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* This is the source code for RIG, a program to generate fake identities.
* Copyright (c) 1999 Ian Turner
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA. */
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <assert.h>
using namespace std;
template <class T>
T getrandnum(T max)
{
static ifstream urandev("/dev/urandom");
T rval;
if (urandev) {
/* /dev/urandom works. */
urandev.read((char*)&rval, sizeof(rval));
} else {
/* this ifdef is messy but does the right thing - we only try /dev/random
* if it is defined. */
#ifdef DEVRANDOM
static ifstream randev("/dev/random");
if (randev) {
/* /dev/random works. */
randev.read(&rval, sizeof(rval));
} else
#endif
{
/* neither /dev/random nor /dev/urandom work; fallback to rand(). */
static bool seeded = false;
if (!seeded) {
srand(time(NULL) ^ (((unsigned int) getpid()) << 15));
seeded = true;
}
unsigned int written = 0;
char* buf = (char*)(void*)(&rval);
while (written < sizeof(rval)) {
int tmp = rand();
unsigned int bytestocopy = sizeof(rval) - written;
if (bytestocopy > sizeof(int)) {
bytestocopy = sizeof(int);
}
memcpy(buf+written, &tmp, bytestocopy);
written += bytestocopy;
}
}
}
if (max == 0)
return max;
else
return rval % max;
}
template <class T>
typename T::value_type getrandpart(T& a)
{
return a[getrandnum(a.size() - 1)];
}
struct place {
string city, state;
int ZIP;
short int areacode;
};
ostream& operator<< (ostream& out, const place& a)
{
out << a.city << ", " << a.state << " "
<< setfill('0') << setw(5) << a.ZIP << endl
<< "(" << setfill('0') << setw(3) << a.areacode
<< ") xxx-xxxx";
return out;
}
istream& operator>> (istream& in, place& a)
{
in >> a.city >> a.state >> a.areacode >> a.ZIP;
return in;
}
struct wholeline {
string name;
};
ostream& operator<< (ostream& out, const wholeline& a)
{
out << a.name;
return out;
}
istream& operator>> (istream& in, wholeline& a)
{
char temp = 'a';
for (;;) {
temp = in.get();
if (temp == '\n' || temp == '\r' || temp == '0' || !in)
break;
a.name += temp;
}
return in;
}
/* Returns number of records gotten. */
template <class T>
int readfile(T& dest, const char * origin)
{
int counter = 0;
ifstream filetoread(origin);
while (filetoread) {
typename T::value_type temp;
filetoread >> temp;
dest.push_back(temp);
counter++;
}
return counter;
}
void printusage()
{
cerr << "USAGE: rig [-f | -m ] [ -d datadir ] [ -c num ]" << endl
<< " datadir - Directory where data files can be found." << endl
<< " If datadir is not specified, " DATADIR " is used as the" << endl
<< " default directory." << endl
<< " num - print num identities." << endl
<< " -f and -m specify gender of generated identities." << endl << endl;
exit(-1);
}
int printfileerror(string filename)
{
cerr << "Unable to read file " << filename << endl << endl;
printusage();
exit(-2);
}
/* Holds information about a particular option */
struct option_data {
/* The character to activate this option. */
char option;
/* Does this option take an operand? */
enum operand_t {ALWAYS, NEVER, OPTIONAL};
operand_t operand;
/* Function to call with the operand.
* First argument option, second operand (or NULL). */
void (*handler) (char, const char*);
static void parse_options(int, const char* const[],
int, const option_data*);
};
void option_data::parse_options(int argc, const char* const argv[],
int numopts,
const option_data config[]) {
for (int i = 1; i < argc; i ++) {
const char* cp = argv[i];
if (*cp != '-')
printusage();
cp ++;
do {
int j;
for (j = 0; j < numopts; j ++) {
if (*cp == config[j].option) {
const char* operand = NULL;
if (config[j].operand != NEVER) {
if (*(cp+1) != '\0') {
operand = cp+1;
cp = NULL;
} else if (i + 1 < argc && argv[i+1][0] != '-') {
i ++;
operand = argv[i];
cp = NULL;
}
}
switch (config[j].operand) {
case ALWAYS:
if (operand == NULL)
printusage();
else
(*(config[j].handler))(config[j].option, operand);
break;
case NEVER:
case OPTIONAL:
(*(config[j].handler))(config[j].option, operand);
break;
}
break;
}
}
if (j == numopts)
printusage();
if (cp != NULL && *cp != '\0')
cp ++;
} while (cp != NULL && *cp != '\0');
}
}
string locdataidx;
long int numids;
bool male, female;
void set_datadir(char option, const char* dir) {
assert(option == 'd');
locdataidx = dir;
}
void set_number(char option, const char* num) {
assert(option == 'c');
char * tail;
numids = strtol(num, &tail, 0);
if (errno || tail == num) {
printusage();
}
// Check that there was no extra stuff in the argument.
while (*tail != '\0') {
if (!(*tail == ' ' || *tail == '\t' || *tail == '\n'))
printusage();
tail ++;
}
}
void set_gender(char option, const char* arg) {
assert((option == 'm' || option == 'f') && arg == NULL);
if (option == 'm') {
male = true;
} else if (option == 'f') {
female = true;
}
}
const option_data option_list[] = {{ 'd', option_data::ALWAYS, set_datadir },
{ 'c', option_data::ALWAYS, set_number },
{ 'f', option_data::NEVER, set_gender },
{ 'm', option_data::NEVER, set_gender }};
int main(int argc, char *argv[])
{
string streetidx, mnamesidx, fnamesidx, lnamesidx;
male = female = false;
numids = 1;
locdataidx = DATADIR;
// four options
option_data::parse_options(argc, argv, 4, option_list);
if (!female && !male)
female = male = true;
streetidx = locdataidx + "/street.idx";
mnamesidx = locdataidx + "/mnames.idx";
fnamesidx = locdataidx + "/fnames.idx";
lnamesidx = locdataidx + "/lnames.idx";
locdataidx += "/locdata.idx";
vector<string> firstname, lastname;
vector<wholeline> street;
vector<place> location;
if (readfile(location, locdataidx.c_str()) == 0)
printfileerror(locdataidx);
if (readfile(street, streetidx.c_str()) == 0)
printfileerror(streetidx);
if (male) {
if (readfile(firstname, mnamesidx.c_str()) == 0)
printfileerror(streetidx);
}
if (female) {
if (readfile(firstname, fnamesidx.c_str()) == 0)
printfileerror(fnamesidx);
}
if (readfile(lastname, lnamesidx.c_str()) == 0)
printfileerror(lnamesidx);
for (int i = 1; i <= numids; i ++) {
cout << getrandpart(firstname) << " " << getrandpart(lastname) << endl
<< getrandnum(1024u) + 1 << " " << getrandpart(street) << endl
<< getrandpart(location) << endl;
if (i != numids)
cout << endl;
}
}