s/qmail 4.2.29a
Next generation secure email transport
Loading...
Searching...
No Matches
constmap.c
Go to the documentation of this file.
1#include "constmap.h"
2#include "alloc.h"
3#include "case.h"
4
5static constmap_hash hash(char *s,int len)
6{
7 unsigned char ch;
9 h = 5381;
10 while (len > 0) {
11 ch = *s++ - 'A';
12 if (ch <= 'Z' - 'A') ch += 'a' - 'A';
13 h = ((h << 5) + h) ^ ch;
14 --len;
15 }
16 return h;
17}
18
19char *constmap(struct constmap *cm,char *s,int len)
20{
22 int pos;
23 h = hash(s,len);
24 pos = cm->first[h & cm->mask];
25 while (pos != -1) {
26 if (h == cm->hash[pos])
27 if (len == cm->inputlen[pos])
28 if (!case_diffb(cm->input[pos],len,s))
29 return cm->input[pos] + cm->inputlen[pos] + 1;
30 pos = cm->next[pos];
31 }
32 return 0;
33}
34
35int constmap_init(struct constmap *cm,char *s,int len,int flagcolon)
36{
37 int i;
38 int j;
39 int k;
40 int pos;
42
43 cm->num = 0;
44 for (j = 0; j < len; ++j) if (!s[j]) ++cm->num;
45
46 h = 64;
47 while (h && (h < cm->num)) h += h;
48 cm->mask = h - 1;
49
50 cm->first = (int *) alloc(sizeof(int) * h);
51 if (cm->first) {
52 cm->input = (char **) alloc(sizeof(char *) * cm->num);
53 if (cm->input) {
54 cm->inputlen = (int *) alloc(sizeof(int) * cm->num);
55 if (cm->inputlen) {
56 cm->hash = (constmap_hash *) alloc(sizeof(constmap_hash) * cm->num);
57 if (cm->hash) {
58 cm->next = (int *) alloc(sizeof(int) * cm->num);
59 if (cm->next) {
60 for (h = 0; h <= cm->mask; ++h)
61 cm->first[h] = -1;
62 pos = 0;
63 i = 0;
64 for (j = 0; j < len; ++j)
65 if (!s[j]) {
66 k = j - i;
67 if (flagcolon) {
68 for (k = i; k < j; ++k)
69 if (s[k] == ':') break;
70 if (k >= j) { i = j + 1; continue; }
71 k -= i;
72 }
73 cm->input[pos] = s + i;
74 cm->inputlen[pos] = k;
75 h = hash(s + i,k);
76 cm->hash[pos] = h;
77 h &= cm->mask;
78 cm->next[pos] = cm->first[h];
79 cm->first[h] = pos;
80 ++pos;
81 i = j + 1;
82 }
83 return 1;
84 }
85 alloc_free(cm->hash);
86 }
87 alloc_free(cm->inputlen);
88 }
89 alloc_free(cm->input);
90 }
91 alloc_free(cm->first);
92 }
93 return 0;
94}
95
96int constmap_init_char(struct constmap *cm,char *s,int len,int flagcolon,char flagchar)
97{
98 int i;
99 int j;
100 int k;
101 int pos;
103
104 if (!flagchar || flagchar == 0 || flagchar == '\0') {
105 flagchar = ':';
106 }
107
108 cm->num = 0;
109 for (j = 0; j < len; ++j) if (!s[j]) ++cm->num;
110
111 h = 64;
112 while (h && (h < cm->num)) h += h;
113 cm->mask = h - 1;
114
115 cm->first = (int *) alloc(sizeof(int) * h);
116 if (cm->first) {
117 cm->input = (char **) alloc(sizeof(char *) * cm->num);
118 if (cm->input) {
119 cm->inputlen = (int *) alloc(sizeof(int) * cm->num);
120 if (cm->inputlen) {
121 cm->hash = (constmap_hash *) alloc(sizeof(constmap_hash) * cm->num);
122 if (cm->hash) {
123 cm->next = (int *) alloc(sizeof(int) * cm->num);
124 if (cm->next) {
125 for (h = 0; h <= cm->mask; ++h)
126 cm->first[h] = -1;
127 pos = 0;
128 i = 0;
129 for (j = 0; j < len; ++j)
130 if (!s[j]) {
131 k = j - i;
132 if (flagcolon) {
133 for (k = i; k < j; ++k)
134 if (s[k] == flagchar) break;
135 if (k >= j) { i = j + 1; continue; }
136 k -= i;
137 }
138 cm->input[pos] = s + i;
139 cm->inputlen[pos] = k;
140 h = hash(s + i,k);
141 cm->hash[pos] = h;
142 h &= cm->mask;
143 cm->next[pos] = cm->first[h];
144 cm->first[h] = pos;
145 ++pos;
146 i = j + 1;
147 }
148 return 1;
149 }
150 alloc_free(cm->hash);
151 }
152 alloc_free(cm->inputlen);
153 }
154 alloc_free(cm->input);
155 }
156 alloc_free(cm->first);
157 }
158 return 0;
159}
160
161void constmap_free(struct constmap *cm)
162{
163 alloc_free(cm->next);
164 alloc_free(cm->hash);
165 alloc_free(cm->inputlen);
166 alloc_free(cm->input);
167 alloc_free(cm->first);
168}
char num[FMT_ULONG]
Definition: chkspawn.c:8
int constmap_init_char(struct constmap *cm, char *s, int len, int flagcolon, char flagchar)
Definition: constmap.c:96
int constmap_init(struct constmap *cm, char *s, int len, int flagcolon)
Definition: constmap.c:35
void constmap_free()
unsigned long constmap_hash
Definition: constmap.h:4
void h(char *, int, int, int)
Definition: install.c:15
stralloc hash
Definition: qmail-dksign.c:243
int j
Definition: qmail-send.c:920
uint32_t k[64]
Definition: sha256.c:26
int * first
Definition: constmap.h:10
constmap_hash * hash
Definition: constmap.h:9
constmap_hash mask
Definition: constmap.h:8
char ** input
Definition: constmap.h:12
int * next
Definition: constmap.h:11
int * inputlen
Definition: constmap.h:13
int num
Definition: constmap.h:7