1 /*-
2 * Copyright (c) 2000-2005 MAEKAWA Masahide <maekawa@cvsync.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/types.h>
31
32 #include <string.h>
33 #include <strings.h>
34
35 #include "compat_stdbool.h"
36 #include "compat_stdint.h"
37 #include "compat_inttypes.h"
38 #include "compat_strings.h"
39
40 #include "hash.h"
41
42 struct hashent {
43 const char *name;
44 size_t namelen;
45 int type;
46 };
47
48 static const struct hashent hashents[] = {
49 { "MD5", 3, HASH_MD5 },
50 #if defined(HAVE_RIPEMD160)
51 { "RIPEMD160", 9, HASH_RIPEMD160 },
52 #endif /* defined(HAVE_RIPEMD160) */
53 #if defined(HAVE_SHA1)
54 { "SHA1", 4, HASH_SHA1 },
55 #endif /* defined(HAVE_SHA1) */
56 #if defined(HAVE_TIGER192)
57 { "TIGER192", 8, HASH_TIGER192 },
58 #endif /* defined(HAVE_TIGER192) */
59 { NULL, 0, HASH_UNSPEC }
60 };
61
62 extern const struct hash_args MD5_args;
63 #if defined(HAVE_RIPEMD160)
64 extern const struct hash_args RIPEMD160_args;
65 #endif /* defined(HAVE_RIPEMD160) */
66 #if defined(HAVE_SHA1)
67 extern const struct hash_args SHA1_args;
68 #endif /* defined(HAVE_SHA1) */
69 #if defined(HAVE_TIGER192)
70 extern const struct hash_args TIGER192_args;
71 #endif /* defined(HAVE_TIGER192) */
72
73 int
hash_pton(const char * name,size_t namelen)74 hash_pton(const char *name, size_t namelen)
75 {
76 const struct hashent *h;
77
78 for (h = hashents ; h->name != NULL ; h++) {
79 if ((h->namelen == namelen) &&
80 (strncasecmp(h->name, name, namelen) == 0)) {
81 return (h->type);
82 }
83 }
84
85 return (HASH_UNSPEC);
86 }
87
88 size_t
hash_ntop(int type,void * buffer,size_t bufsize)89 hash_ntop(int type, void *buffer, size_t bufsize)
90 {
91 const struct hashent *h;
92
93 for (h = hashents ; h->name != NULL ; h++) {
94 if (h->type == type) {
95 if (h->namelen >= bufsize)
96 break;
97 (void)memcpy(buffer, h->name, h->namelen);
98 return (h->namelen);
99 }
100 }
101
102 return (0);
103 }
104
105 bool
hash_set(int type,const struct hash_args ** args)106 hash_set(int type, const struct hash_args **args)
107 {
108 switch (type) {
109 case HASH_MD5:
110 *args = &MD5_args;
111 break;
112 #if defined(HAVE_RIPEMD160)
113 case HASH_RIPEMD160:
114 *args = &RIPEMD160_args;
115 break;
116 #endif /* defined(HAVE_RIPEMD160) */
117 #if defined(HAVE_SHA1)
118 case HASH_SHA1:
119 *args = &SHA1_args;
120 break;
121 #endif /* defined(HAVE_SHA1) */
122 #if defined(HAVE_TIGER192)
123 case HASH_TIGER192:
124 *args = &TIGER192_args;
125 break;
126 #endif /* defined(HAVE_TIGER192) */
127 default:
128 return (false);
129 }
130
131 return (true);
132 }
133