summaryrefslogtreecommitdiffstats
path: root/contrib/bind/bin/named/db_dict.c
blob: a0b89216fd658ca6b6dc3aee47ba297eb903bdfd (plain)
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
#if !defined(lint) && !defined(SABER)
static char rcsid[] = "$Id: db_dict.c,v 8.1 1997/09/26 17:55:40 halley Exp $";
#endif /* not lint */

/*
 * Portions Copyright (c) 1997 by Internet Software Consortium.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

#include "port_before.h"

#include <sys/types.h>
#include <sys/uio.h>
#include <sys/param.h>
#include <sys/stat.h>

#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>

#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <isc/eventlib.h>
#include <isc/logging.h>
#include <isc/memcluster.h>

#include "port_after.h"

#include "named.h"

#define	DICT_BLOCKBITS	8
#define	DICT_BLOCKSHIFT	16
#define	DICT_BLOCKMAX	(1 << DICT_BLOCKBITS)
#define	DICT_OFFSETBITS	16
#define	DICT_OFFSETSHIFT 0
#define	DICT_OFFSETMAX	(1 << DICT_OFFSETBITS)

#define	DICT_CONSUMED(Length) ((Length) + 1)
#define	DICT_INDEX(Block,Offset) (((Block) << DICT_BLOCKSHIFT) | \
				  ((Offset) << DICT_OFFSETSHIFT))

static int		dict_new(const char *, int);

static char *		blocks[DICT_BLOCKMAX];
static int		offsets[DICT_BLOCKMAX];
static int		cur_block = 0;
static int		cur_offset = -1;

int
dict_lookup(const char *text, int length, int flags) {
	int block, offset, ret;

	/* XXX this is a proof of concept, some kind of hash is needed. */
	for (block = 0; block <= cur_block; block++) {
		const char *cur = &blocks[block][0];
		const char *end = &blocks[block][offsets[block]];

		while (cur < end) {
			int xlength = *cur;

			if (xlength == length &&
			    memcmp(cur+1, text, length) == 0)
				return (DICT_INDEX(block, offset));
			cur += DICT_CONSUMED(length);
		}
	}
	if ((flags & DICT_INSERT_P) != 0)
		return (dict_new(text, length));
	return (-ENOENT);
}

static int
dict_new(const char *text, int length) {
	int ret;

	if (length < 0 || length > DICT_MAXLENGTH)
		return (-E2BIG);
	if (cur_offset + DICT_CONSUMED(length) >= DICT_OFFSETMAX) {
		if (cur_block + 1 == DICT_BLOCKMAX)
			return (-ENOSPC);
		cur_block++;
		blocks[cur_block] = memget(DICT_OFFSETMAX);
		if (blocks[cur_block] == NULL)
			return (-ENOMEM);
		cur_offset = 0;
	}
	assert(cur_offset >= 0);
	assert(cur_offset + DICT_CONSUMED(length) < DICT_OFFSETMAX);
	ret = DICT_INDEX(cur_block, cur_offset);
	blocks[cur_block][cur_offset] = length;
	memcpy(&blocks[cur_block][cur_offset+1], text, length);
	cur_offset += DICT_CONSUMED(length);
	offsets[cur_block] = cur_offset;
	return (ret);
}
OpenPOWER on IntegriCloud