1*ebfedea0SLionel Sambuc /* $NetBSD: test_hash.c,v 1.2 2011/02/16 02:14:23 christos Exp $ */
2*ebfedea0SLionel Sambuc
3*ebfedea0SLionel Sambuc /* Copyright (c) 2010 The NetBSD Foundation, Inc.
4*ebfedea0SLionel Sambuc * All rights reserved.
5*ebfedea0SLionel Sambuc *
6*ebfedea0SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
7*ebfedea0SLionel Sambuc * by Mateusz Kocielski.
8*ebfedea0SLionel Sambuc *
9*ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
10*ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
11*ebfedea0SLionel Sambuc * are met:
12*ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14*ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
15*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
16*ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
17*ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
18*ebfedea0SLionel Sambuc * must display the following acknowledgement:
19*ebfedea0SLionel Sambuc * This product includes software developed by the NetBSD
20*ebfedea0SLionel Sambuc * Foundation, Inc. and its contributors.
21*ebfedea0SLionel Sambuc * 4. Neither the name of The NetBSD Foundation nor the names of its
22*ebfedea0SLionel Sambuc * contributors may be used to endorse or promote products derived
23*ebfedea0SLionel Sambuc * from this software without specific prior written permission.
24*ebfedea0SLionel Sambuc *
25*ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26*ebfedea0SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27*ebfedea0SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28*ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29*ebfedea0SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30*ebfedea0SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31*ebfedea0SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32*ebfedea0SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33*ebfedea0SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34*ebfedea0SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35*ebfedea0SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
36*ebfedea0SLionel Sambuc */
37*ebfedea0SLionel Sambuc
38*ebfedea0SLionel Sambuc #include <err.h>
39*ebfedea0SLionel Sambuc #include <malloc.h>
40*ebfedea0SLionel Sambuc #include <stdio.h>
41*ebfedea0SLionel Sambuc #include <stdlib.h>
42*ebfedea0SLionel Sambuc #include <string.h>
43*ebfedea0SLionel Sambuc
44*ebfedea0SLionel Sambuc #include <saslc.h>
45*ebfedea0SLionel Sambuc
46*ebfedea0SLionel Sambuc __RCSID("$NetBSD: test_hash.c,v 1.2 2011/02/16 02:14:23 christos Exp $");
47*ebfedea0SLionel Sambuc
48*ebfedea0SLionel Sambuc #define MAX_HASH_SIZE 256
49*ebfedea0SLionel Sambuc
50*ebfedea0SLionel Sambuc /*
51*ebfedea0SLionel Sambuc * List of all property keys.
52*ebfedea0SLionel Sambuc * NB: I did not list those used for debugging as collisions in that
53*ebfedea0SLionel Sambuc * case don't really hurt anything.
54*ebfedea0SLionel Sambuc */
55*ebfedea0SLionel Sambuc static const char *keys[] = {
56*ebfedea0SLionel Sambuc SASLC_PROP_AUTHCID,
57*ebfedea0SLionel Sambuc SASLC_PROP_AUTHZID,
58*ebfedea0SLionel Sambuc SASLC_PROP_BASE64IO,
59*ebfedea0SLionel Sambuc SASLC_PROP_CIPHERMASK,
60*ebfedea0SLionel Sambuc SASLC_PROP_DEBUG,
61*ebfedea0SLionel Sambuc SASLC_PROP_HOSTNAME,
62*ebfedea0SLionel Sambuc SASLC_PROP_MAXBUF,
63*ebfedea0SLionel Sambuc SASLC_PROP_PASSWD,
64*ebfedea0SLionel Sambuc SASLC_PROP_QOPMASK,
65*ebfedea0SLionel Sambuc SASLC_PROP_REALM,
66*ebfedea0SLionel Sambuc SASLC_PROP_SECURITY,
67*ebfedea0SLionel Sambuc SASLC_PROP_SERVICE,
68*ebfedea0SLionel Sambuc SASLC_PROP_SERVNAME
69*ebfedea0SLionel Sambuc };
70*ebfedea0SLionel Sambuc
71*ebfedea0SLionel Sambuc /*
72*ebfedea0SLionel Sambuc * This must match the dict.c::saslc__dict_hashval() routine.
73*ebfedea0SLionel Sambuc */
74*ebfedea0SLionel Sambuc static size_t
hash(const char * cp,size_t hsize,size_t hinit,size_t shift)75*ebfedea0SLionel Sambuc hash(const char *cp, size_t hsize, size_t hinit, size_t shift)
76*ebfedea0SLionel Sambuc {
77*ebfedea0SLionel Sambuc size_t hval;
78*ebfedea0SLionel Sambuc
79*ebfedea0SLionel Sambuc hval = hinit;
80*ebfedea0SLionel Sambuc for (/*EMPTY*/; *cp != '\0'; cp++) {
81*ebfedea0SLionel Sambuc hval <<= shift;
82*ebfedea0SLionel Sambuc hval += (size_t)*cp;
83*ebfedea0SLionel Sambuc }
84*ebfedea0SLionel Sambuc return hval % hsize;
85*ebfedea0SLionel Sambuc }
86*ebfedea0SLionel Sambuc
87*ebfedea0SLionel Sambuc static int
no_collision(size_t hsize,size_t hinit,size_t shift)88*ebfedea0SLionel Sambuc no_collision(size_t hsize, size_t hinit, size_t shift)
89*ebfedea0SLionel Sambuc {
90*ebfedea0SLionel Sambuc const char **used;
91*ebfedea0SLionel Sambuc size_t hval, i;
92*ebfedea0SLionel Sambuc
93*ebfedea0SLionel Sambuc used = calloc(sizeof(*used), hsize);
94*ebfedea0SLionel Sambuc if (used == NULL)
95*ebfedea0SLionel Sambuc err(EXIT_FAILURE, "calloc");
96*ebfedea0SLionel Sambuc for (i = 0; i < __arraycount(keys); i++) {
97*ebfedea0SLionel Sambuc hval = hash(keys[i], hsize, hinit, shift);
98*ebfedea0SLionel Sambuc if (used[hval] != 0) {
99*ebfedea0SLionel Sambuc free(used);
100*ebfedea0SLionel Sambuc return 0;
101*ebfedea0SLionel Sambuc }
102*ebfedea0SLionel Sambuc used[hval] = keys[i];
103*ebfedea0SLionel Sambuc }
104*ebfedea0SLionel Sambuc #if 0
105*ebfedea0SLionel Sambuc for (i = 0; i < hsize; i++) {
106*ebfedea0SLionel Sambuc if (used[i] != NULL)
107*ebfedea0SLionel Sambuc printf("%02zd: %s\n", i, used[i]);
108*ebfedea0SLionel Sambuc }
109*ebfedea0SLionel Sambuc #endif
110*ebfedea0SLionel Sambuc free(used);
111*ebfedea0SLionel Sambuc return 1;
112*ebfedea0SLionel Sambuc }
113*ebfedea0SLionel Sambuc
114*ebfedea0SLionel Sambuc static int __dead
usage(int rval)115*ebfedea0SLionel Sambuc usage(int rval)
116*ebfedea0SLionel Sambuc {
117*ebfedea0SLionel Sambuc
118*ebfedea0SLionel Sambuc printf("%s [<min hash size> [<max hash size>]]\n", getprogname());
119*ebfedea0SLionel Sambuc printf("min and max hash size must be >= %zu\n", __arraycount(keys));
120*ebfedea0SLionel Sambuc exit(rval);
121*ebfedea0SLionel Sambuc }
122*ebfedea0SLionel Sambuc
123*ebfedea0SLionel Sambuc static void
show_result(int brief,size_t h,size_t i,size_t s)124*ebfedea0SLionel Sambuc show_result(int brief, size_t h, size_t i, size_t s)
125*ebfedea0SLionel Sambuc {
126*ebfedea0SLionel Sambuc if (brief)
127*ebfedea0SLionel Sambuc printf("no collisions: hsize=%zu hinit=%zu shift=%zu\n", h, i, s);
128*ebfedea0SLionel Sambuc else {
129*ebfedea0SLionel Sambuc printf("#define HASH_SIZE\t%zu\n", h);
130*ebfedea0SLionel Sambuc printf("#define HASH_INIT\t%zu\n", i);
131*ebfedea0SLionel Sambuc printf("#define HASH_SHIFT\t%zu\n", s);
132*ebfedea0SLionel Sambuc }
133*ebfedea0SLionel Sambuc }
134*ebfedea0SLionel Sambuc
135*ebfedea0SLionel Sambuc int
main(int argc,char ** argv)136*ebfedea0SLionel Sambuc main(int argc, char **argv)
137*ebfedea0SLionel Sambuc {
138*ebfedea0SLionel Sambuc char *p;
139*ebfedea0SLionel Sambuc size_t i, h, s;
140*ebfedea0SLionel Sambuc size_t h_min, h_max;
141*ebfedea0SLionel Sambuc int brief;
142*ebfedea0SLionel Sambuc
143*ebfedea0SLionel Sambuc brief = argc != 1;
144*ebfedea0SLionel Sambuc h_min = 0;
145*ebfedea0SLionel Sambuc h_max = 0;
146*ebfedea0SLionel Sambuc switch (argc) {
147*ebfedea0SLionel Sambuc case 1:
148*ebfedea0SLionel Sambuc h_min = __arraycount(keys);
149*ebfedea0SLionel Sambuc h_max = MAX_HASH_SIZE;
150*ebfedea0SLionel Sambuc break;
151*ebfedea0SLionel Sambuc case 3:
152*ebfedea0SLionel Sambuc h = strtol(argv[2], &p, 0);
153*ebfedea0SLionel Sambuc if (*p != '\0' || h == 0)
154*ebfedea0SLionel Sambuc usage(-1);
155*ebfedea0SLionel Sambuc h_max = h;
156*ebfedea0SLionel Sambuc /*FALLTHROUGH*/
157*ebfedea0SLionel Sambuc case 2:
158*ebfedea0SLionel Sambuc h = strtol(argv[1], &p, 0);
159*ebfedea0SLionel Sambuc if (*p != '\0' || h == 0)
160*ebfedea0SLionel Sambuc usage(-1);
161*ebfedea0SLionel Sambuc h_min = h;
162*ebfedea0SLionel Sambuc if (argc == 2)
163*ebfedea0SLionel Sambuc h_max = h;
164*ebfedea0SLionel Sambuc break;
165*ebfedea0SLionel Sambuc default:
166*ebfedea0SLionel Sambuc usage(0);
167*ebfedea0SLionel Sambuc }
168*ebfedea0SLionel Sambuc if (h_max < __arraycount(keys) ||
169*ebfedea0SLionel Sambuc h_min < __arraycount(keys) ||
170*ebfedea0SLionel Sambuc h_max < h_min)
171*ebfedea0SLionel Sambuc usage(-1);
172*ebfedea0SLionel Sambuc
173*ebfedea0SLionel Sambuc for (h = h_min; h <= h_max; h++) {
174*ebfedea0SLionel Sambuc for (s = 0; s < 32; s++) {
175*ebfedea0SLionel Sambuc for (i = 0; i < h; i++) {
176*ebfedea0SLionel Sambuc if (no_collision(h, i, s)) {
177*ebfedea0SLionel Sambuc show_result(brief, h, i, s);
178*ebfedea0SLionel Sambuc if (argc == 1)
179*ebfedea0SLionel Sambuc return 0;
180*ebfedea0SLionel Sambuc }
181*ebfedea0SLionel Sambuc }
182*ebfedea0SLionel Sambuc }
183*ebfedea0SLionel Sambuc }
184*ebfedea0SLionel Sambuc return 0;
185*ebfedea0SLionel Sambuc }
186