File: mkzonedb.c

package info (click to toggle)
isdnutils 1%3A3.25%2Bdfsg1-10
  • links: PTS
  • area: main
  • in suites: buster
  • size: 20,008 kB
  • sloc: ansic: 98,061; sh: 19,044; perl: 4,945; makefile: 3,152; cpp: 2,708; tcl: 22
file content (377 lines) | stat: -rw-r--r-- 8,323 bytes parent folder | download | duplicates (2)
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * Make zone datafile
 *
 * Copyright 1999 by Leopold Toetsch <lt@toetsch.at>
 *
 * SYNOPSIS
 * mkzonedb -r Zonefile -d database [-v] [-V] [-o Oz] [-l L]
 *
 * 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

static char progversion[] = "1.25";

/*
 * Changes:
 *
 * 1.25 2001.06.06 lt fixed sparc
 *
 * 1.23 1999.10.12 lt moved /CC/code handling to destination
 *
 * 1.11 1999.07.08 lt added support for NL
 *
 *      in NL areacode may be shorter than actual areacodenumber
 *      in this case \tLEN is appended to text
 *
 */

#define STANDALONE

#define _MKZONEDB_C_

#include <limits.h>

#include <stdlib.h>
#include <stdio.h>
#ifdef __GLIBC__
# define __USE_GNU  /* for declaration of basename() */
#endif
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#if !defined(__GLIBC__) && !defined(basename)
extern const char *basename (const char *name);
#endif

#include "config.h"
#include "common.h"
#include "upack.h"


void usage(char *argv[]) {
	fprintf(stderr, "%s: -r RedZonefile -d Database [ -v ] [ -V ] [ -o Localzone ] [ -l Len ]\n",
		basename(argv[0]));
	exit(EXIT_FAILURE);
}

static int (*zones)[3];
static int *numbers;
static bool verbose=false;
static bool stdoutisatty=true;  /* assume the worst :-) */
static int table[256];
static int tablelen, keylen, keydigs, maxnum;
static int n, nn;
static int ortszone=1;
static int numlen;

static void read_rzfile(char *rf) {
	int i;
	char line[BUFSIZ], *p, *op;
	FILE *fp;
	int from,to,z;

        if (strcmp(rf, "-")) {  /* use stdin? */
            if ((fp=fopen(rf, "r")) == 0) {
		fprintf(stderr, "Couldn't read '%s'\n", rf);
		exit(EXIT_FAILURE);
            }
	}
        else {
            fp = stdin;
        }
	maxnum = numlen>=5 ? 40000 : 10000;
	if ((numbers = calloc(maxnum+1, sizeof(int))) == 0) {
		fprintf(stderr, "Out of mem\n");
		exit(EXIT_FAILURE);
	}
	n=i=keylen=keydigs=0;
	if (verbose)
		printf("Reading %s\n", rf);

	while (fgets(line, BUFSIZ, fp)) {
		if (verbose && stdoutisatty && (n % 1000) == 0) {
			printf("%d\r", n);
			fflush(stdout);
		}
		if (!*line)
			break;
		if (strlen(line)>40)
			fprintf(stderr, "Possible junk in line %d", n);
		from = strtoul(line, &p, 10);
		if (p-line > keydigs)
			keydigs=p-line;
		p++;
		op = p;
		to = strtoul(p, &p, 10);
		if (p-op > keydigs)
			keydigs=p-op;
		p++;
		z = strtoul(p, &p, 10);
		if (z > 127) {
			fprintf(stderr, "Something is wrong with this file (line %d)\n", n);
			exit(EXIT_FAILURE);
		}
		if ((zones = realloc(zones, (n+1)*3*sizeof(int))) == 0) {
			fprintf(stderr, "Out of mem\n");
			exit(EXIT_FAILURE);
		}
		zones[n][0]=from;
		zones[n][1]=to;
		zones[n][2]=z;
		if (from > keylen)
			keylen=from;
		if (to > keylen)
			keylen=to;
		if(to>maxnum) {
			maxnum=to;
			if ((numbers = realloc(numbers,(maxnum+1)*sizeof(int))) == 0) {
				fprintf(stderr, "Out of mem\n");
				exit(EXIT_FAILURE);
			}
		}
		numbers[to]++;
		n++;
	}
        if (fp != stdin) {
            fclose(fp);
        }
	free(rf);
	maxnum=keylen<maxnum?keylen:maxnum;
}

/* get the 256 top used nums in table */
static void make_table() {
	int i, j, k;
	tablelen = 0;
	if (verbose)
		printf("%d\nSorting\n", n);
	nn = maxnum;
	if (keylen < nn)
		nn = keylen;
	for (j=0; j<256; j++) {
		int max = 0;
		k = -1;
		for (i=0; i<=nn; i++) {
			if (numbers[i] > max) {
				k = i;
				max = numbers[i];
			}
		}
		if (k == -1) {
			nn = j;
			break;
		}
		numbers[k]=0;
		table[j] = k;
		if (k > tablelen)
			tablelen = k;
	}
	free(numbers);
	if (nn > 256)
		nn = 256;
}

static void write_db(char * df) {
	_DB db;
	datum key, value;
	UL ul, kul;
	US us, kus;
	int ofrom;
	int vlen;
	US count;
	char *val, *p;
	int i, j;
	char version[80];

	if (verbose)
		printf("Writing\n");
	if((db=OPEN(df,WRITE)) == 0) {
		fprintf(stderr, "Can't create '%s' - %s\n", df, GET_ERR);
		exit(EXIT_FAILURE);
	}
	/* tablelen .. len of table entries */
	/* keylen = keysize */
	if (maxnum > keylen)
		keylen=maxnum;
	keylen = keylen > 0xffff ? 4 : 2;
	tablelen = tablelen > 0xffff ? 4 : tablelen > 0xff ? 2 : 1;

	/* write version & table */
	key.dptr = "vErSiO";
	key.dsize = 7;
	/* version of zone.c must be not smaller than dataversion */
	sprintf(version,"V1.25 K%c C%c N%d T%d O%d L%d",
		keylen==2?'S':'L',tablelen==1?'C':tablelen==2?'S':'L',
		nn,n, ortszone, numlen?numlen:keydigs);
	value.dptr = version;
	value.dsize = strlen(version)+1;
	if(STORE(db, key, value)) {
		fprintf(stderr, "Error storing version key - %s\n", GET_ERR);
		exit(EXIT_FAILURE);
	}

	if ((p = val = calloc(nn, tablelen)) == 0) {
		fprintf(stderr, "Out of mem\n");
		exit(EXIT_FAILURE);
	}
	key.dptr = "_tAbLe\0";
	key.dsize = 7;
	for (i=0; i<nn; i++)
		if(tablelen==1)
			*p++ = (UC)table[i];
		else if(tablelen == 2) {
		  	// *((US*)p)++ = (US)table[i];
			tools_pack16(p, (US)table[i]);
		  	p += 2;
		}
		else {
			// *((UL*)p)++ = (UL)table[i];
			tools_pack32(p, (UL)table[i]);
		  	p += 4;
		}
	value.dptr = val;
	value.dsize = nn*tablelen;
	if(STORE(db, key, value)) {
		fprintf(stderr, "Error storing table key - %s\n", GET_ERR);
		exit(EXIT_FAILURE);
	}
	free(val);

	/* and write data */
	val = malloc(2); /* size of count */
	vlen = 2;
	ofrom = -1;
	count = 0;
	for (i=0; i<n; i++) {
		bool found = false;
		UC uc;
		unsigned char buf[4];

		if (verbose && stdoutisatty && (i % 1000) == 0) {
			printf("%d\r", i);
			fflush(stdout);
		}
		if (ofrom != -1 && ofrom != zones[i][0]) {
			// *((US*)val) = count;
			tools_pack16(val, count);
			value.dptr = val;
			value.dsize = vlen;
			if(STORE(db, key, value)) {
				fprintf(stderr, "Error storing key '%d' - %s\n",ofrom,GET_ERR);
				exit(EXIT_FAILURE);
			}
			free(value.dptr); /* the realloced val */
		}
		if (ofrom != zones[i][0]) {
			count = 0;
			val = malloc(2); /* size */
			vlen = 2;
			/* set up key */
			ofrom = zones[i][0];
			if (keylen == 4) {
				kul = (UL)ofrom;
				tools_pack32(buf, kul);
				key.dptr = buf;
			}
			else {
				kus = (US)ofrom;
				tools_pack16(buf, kus);
				key.dptr = buf;
			}
			key.dsize = keylen;
		}
		count++;
		for (j=0; j<nn; j++)
			if(table[j] == zones[i][1]) {
				found = true;
				val = realloc(val, vlen+2);
				uc = (UC)zones[i][2];
				val[vlen++] = uc;
				uc = (UC)j;
				val[vlen++] = uc;
				break;
			}
		if (!found) {
			val = realloc(val, vlen+1+keylen);
			zones[i][2] |= 128;
			uc = (UC)zones[i][2];
			val[vlen++] = uc;
			if(keylen == 2) {
				us = (US)zones[i][1];
				// *((US*)(&val[vlen])) = us;
				tools_pack16(&val[vlen], us);
			}
			else {
				ul = (UL)zones[i][1];
				// *((UL*)(&val[vlen])) = ul;
				tools_pack32(&val[vlen], ul);
			}
			vlen+=keylen;
		}
	}
	if(verbose)
		printf("%d\n", i);
	/* write last */
    	// *((US*)val) = count;
	tools_pack16(val, count);

	value.dptr = val;
	value.dsize = vlen;
	STORE(db, key, value);
	free(value.dptr);
	CLOSE(db);
	free(zones);
	free(df);
}

int main (int argc, char *argv[])
{
	char *df=0;
	char *rf=0;
	int c;

	if (argc < 2)
		usage(argv);
	while ( (c=getopt(argc, argv, "vVr:d:o:l:")) != EOF) {
		switch (c) {
			case 'v' : verbose = true; break;
			case 'V' : printf("%s: V%s Db=%s\n",
				basename(argv[0]), progversion, dbv); exit(1);
			case 'd' : df = strdup(optarg); break;
			case 'r' : rf = strdup(optarg); break;
			case 'o' : ortszone = atoi(optarg); break;
			case 'l' : numlen = atoi(optarg); break;
		}
	}
        if (!rf || !df)
            usage(argv);

        if (verbose)
            stdoutisatty = isatty(fileno(stdout));

	read_rzfile(rf);
	make_table();
	write_db(df);

	/* Uff this got longer as I thought,
	   C is a real low level language -
	   now it's clear, why I prefer Perl
	*/
	return(EXIT_SUCCESS);
}