sl@0
|
1 |
/* crypto/txt_db/txt_db.c */
|
sl@0
|
2 |
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
*
|
sl@0
|
5 |
* This package is an SSL implementation written
|
sl@0
|
6 |
* by Eric Young (eay@cryptsoft.com).
|
sl@0
|
7 |
* The implementation was written so as to conform with Netscapes SSL.
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* This library is free for commercial and non-commercial use as long as
|
sl@0
|
10 |
* the following conditions are aheared to. The following conditions
|
sl@0
|
11 |
* apply to all code found in this distribution, be it the RC4, RSA,
|
sl@0
|
12 |
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
sl@0
|
13 |
* included with this distribution is covered by the same copyright terms
|
sl@0
|
14 |
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
sl@0
|
15 |
*
|
sl@0
|
16 |
* Copyright remains Eric Young's, and as such any Copyright notices in
|
sl@0
|
17 |
* the code are not to be removed.
|
sl@0
|
18 |
* If this package is used in a product, Eric Young should be given attribution
|
sl@0
|
19 |
* as the author of the parts of the library used.
|
sl@0
|
20 |
* This can be in the form of a textual message at program startup or
|
sl@0
|
21 |
* in documentation (online or textual) provided with the package.
|
sl@0
|
22 |
*
|
sl@0
|
23 |
* Redistribution and use in source and binary forms, with or without
|
sl@0
|
24 |
* modification, are permitted provided that the following conditions
|
sl@0
|
25 |
* are met:
|
sl@0
|
26 |
* 1. Redistributions of source code must retain the copyright
|
sl@0
|
27 |
* notice, this list of conditions and the following disclaimer.
|
sl@0
|
28 |
* 2. Redistributions in binary form must reproduce the above copyright
|
sl@0
|
29 |
* notice, this list of conditions and the following disclaimer in the
|
sl@0
|
30 |
* documentation and/or other materials provided with the distribution.
|
sl@0
|
31 |
* 3. All advertising materials mentioning features or use of this software
|
sl@0
|
32 |
* must display the following acknowledgement:
|
sl@0
|
33 |
* "This product includes cryptographic software written by
|
sl@0
|
34 |
* Eric Young (eay@cryptsoft.com)"
|
sl@0
|
35 |
* The word 'cryptographic' can be left out if the rouines from the library
|
sl@0
|
36 |
* being used are not cryptographic related :-).
|
sl@0
|
37 |
* 4. If you include any Windows specific code (or a derivative thereof) from
|
sl@0
|
38 |
* the apps directory (application code) you must include an acknowledgement:
|
sl@0
|
39 |
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
sl@0
|
40 |
*
|
sl@0
|
41 |
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
sl@0
|
42 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
sl@0
|
43 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
sl@0
|
44 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
sl@0
|
45 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
sl@0
|
46 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
sl@0
|
47 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
sl@0
|
48 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
sl@0
|
49 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
sl@0
|
50 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
sl@0
|
51 |
* SUCH DAMAGE.
|
sl@0
|
52 |
*
|
sl@0
|
53 |
* The licence and distribution terms for any publically available version or
|
sl@0
|
54 |
* derivative of this code cannot be changed. i.e. this code cannot simply be
|
sl@0
|
55 |
* copied and put under another distribution licence
|
sl@0
|
56 |
* [including the GNU Public Licence.]
|
sl@0
|
57 |
*/
|
sl@0
|
58 |
|
sl@0
|
59 |
#include <stdio.h>
|
sl@0
|
60 |
#include <stdlib.h>
|
sl@0
|
61 |
#include <string.h>
|
sl@0
|
62 |
#include "cryptlib.h"
|
sl@0
|
63 |
#include <openssl/buffer.h>
|
sl@0
|
64 |
#include <openssl/txt_db.h>
|
sl@0
|
65 |
|
sl@0
|
66 |
#undef BUFSIZE
|
sl@0
|
67 |
#define BUFSIZE 512
|
sl@0
|
68 |
|
sl@0
|
69 |
const char TXT_DB_version[]="TXT_DB" OPENSSL_VERSION_PTEXT;
|
sl@0
|
70 |
|
sl@0
|
71 |
EXPORT_C TXT_DB *TXT_DB_read(BIO *in, int num)
|
sl@0
|
72 |
{
|
sl@0
|
73 |
TXT_DB *ret=NULL;
|
sl@0
|
74 |
int er=1;
|
sl@0
|
75 |
int esc=0;
|
sl@0
|
76 |
long ln=0;
|
sl@0
|
77 |
int i,add,n;
|
sl@0
|
78 |
int size=BUFSIZE;
|
sl@0
|
79 |
int offset=0;
|
sl@0
|
80 |
char *p,**pp,*f;
|
sl@0
|
81 |
BUF_MEM *buf=NULL;
|
sl@0
|
82 |
|
sl@0
|
83 |
if ((buf=BUF_MEM_new()) == NULL) goto err;
|
sl@0
|
84 |
if (!BUF_MEM_grow(buf,size)) goto err;
|
sl@0
|
85 |
|
sl@0
|
86 |
if ((ret=(TXT_DB *)OPENSSL_malloc(sizeof(TXT_DB))) == NULL)
|
sl@0
|
87 |
goto err;
|
sl@0
|
88 |
ret->num_fields=num;
|
sl@0
|
89 |
ret->index=NULL;
|
sl@0
|
90 |
ret->qual=NULL;
|
sl@0
|
91 |
if ((ret->data=sk_new_null()) == NULL)
|
sl@0
|
92 |
goto err;
|
sl@0
|
93 |
if ((ret->index=(LHASH **)OPENSSL_malloc(sizeof(LHASH *)*num)) == NULL)
|
sl@0
|
94 |
goto err;
|
sl@0
|
95 |
if ((ret->qual=(int (**)(char **))OPENSSL_malloc(sizeof(int (**)(char **))*num)) == NULL)
|
sl@0
|
96 |
goto err;
|
sl@0
|
97 |
for (i=0; i<num; i++)
|
sl@0
|
98 |
{
|
sl@0
|
99 |
ret->index[i]=NULL;
|
sl@0
|
100 |
ret->qual[i]=NULL;
|
sl@0
|
101 |
}
|
sl@0
|
102 |
|
sl@0
|
103 |
add=(num+1)*sizeof(char *);
|
sl@0
|
104 |
buf->data[size-1]='\0';
|
sl@0
|
105 |
offset=0;
|
sl@0
|
106 |
for (;;)
|
sl@0
|
107 |
{
|
sl@0
|
108 |
if (offset != 0)
|
sl@0
|
109 |
{
|
sl@0
|
110 |
size+=BUFSIZE;
|
sl@0
|
111 |
if (!BUF_MEM_grow_clean(buf,size)) goto err;
|
sl@0
|
112 |
}
|
sl@0
|
113 |
buf->data[offset]='\0';
|
sl@0
|
114 |
BIO_gets(in,&(buf->data[offset]),size-offset);
|
sl@0
|
115 |
ln++;
|
sl@0
|
116 |
if (buf->data[offset] == '\0') break;
|
sl@0
|
117 |
if ((offset == 0) && (buf->data[0] == '#')) continue;
|
sl@0
|
118 |
i=strlen(&(buf->data[offset]));
|
sl@0
|
119 |
offset+=i;
|
sl@0
|
120 |
if (buf->data[offset-1] != '\n')
|
sl@0
|
121 |
continue;
|
sl@0
|
122 |
else
|
sl@0
|
123 |
{
|
sl@0
|
124 |
buf->data[offset-1]='\0'; /* blat the '\n' */
|
sl@0
|
125 |
if (!(p=(char *)OPENSSL_malloc(add+offset))) goto err;
|
sl@0
|
126 |
offset=0;
|
sl@0
|
127 |
}
|
sl@0
|
128 |
pp=(char **)p;
|
sl@0
|
129 |
p+=add;
|
sl@0
|
130 |
n=0;
|
sl@0
|
131 |
pp[n++]=p;
|
sl@0
|
132 |
i=0;
|
sl@0
|
133 |
f=buf->data;
|
sl@0
|
134 |
|
sl@0
|
135 |
esc=0;
|
sl@0
|
136 |
for (;;)
|
sl@0
|
137 |
{
|
sl@0
|
138 |
if (*f == '\0') break;
|
sl@0
|
139 |
if (*f == '\t')
|
sl@0
|
140 |
{
|
sl@0
|
141 |
if (esc)
|
sl@0
|
142 |
p--;
|
sl@0
|
143 |
else
|
sl@0
|
144 |
{
|
sl@0
|
145 |
*(p++)='\0';
|
sl@0
|
146 |
f++;
|
sl@0
|
147 |
if (n >= num) break;
|
sl@0
|
148 |
pp[n++]=p;
|
sl@0
|
149 |
continue;
|
sl@0
|
150 |
}
|
sl@0
|
151 |
}
|
sl@0
|
152 |
esc=(*f == '\\');
|
sl@0
|
153 |
*(p++)= *(f++);
|
sl@0
|
154 |
}
|
sl@0
|
155 |
*(p++)='\0';
|
sl@0
|
156 |
if ((n != num) || (*f != '\0'))
|
sl@0
|
157 |
{
|
sl@0
|
158 |
#if !defined(OPENSSL_NO_STDIO) && !defined(OPENSSL_SYS_WIN16) /* temporaty fix :-( */
|
sl@0
|
159 |
fprintf(stderr,"wrong number of fields on line %ld (looking for field %d, got %d, '%s' left)\n",ln,num,n,f);
|
sl@0
|
160 |
#endif
|
sl@0
|
161 |
er=2;
|
sl@0
|
162 |
goto err;
|
sl@0
|
163 |
}
|
sl@0
|
164 |
pp[n]=p;
|
sl@0
|
165 |
if (!sk_push(ret->data,(char *)pp))
|
sl@0
|
166 |
{
|
sl@0
|
167 |
#if !defined(OPENSSL_NO_STDIO) && !defined(OPENSSL_SYS_WIN16) /* temporaty fix :-( */
|
sl@0
|
168 |
fprintf(stderr,"failure in sk_push\n");
|
sl@0
|
169 |
#endif
|
sl@0
|
170 |
er=2;
|
sl@0
|
171 |
goto err;
|
sl@0
|
172 |
}
|
sl@0
|
173 |
}
|
sl@0
|
174 |
er=0;
|
sl@0
|
175 |
err:
|
sl@0
|
176 |
BUF_MEM_free(buf);
|
sl@0
|
177 |
if (er)
|
sl@0
|
178 |
{
|
sl@0
|
179 |
#if !defined(OPENSSL_NO_STDIO) && !defined(OPENSSL_SYS_WIN16)
|
sl@0
|
180 |
if (er == 1) fprintf(stderr,"OPENSSL_malloc failure\n");
|
sl@0
|
181 |
#endif
|
sl@0
|
182 |
if (ret != NULL)
|
sl@0
|
183 |
{
|
sl@0
|
184 |
if (ret->data != NULL) sk_free(ret->data);
|
sl@0
|
185 |
if (ret->index != NULL) OPENSSL_free(ret->index);
|
sl@0
|
186 |
if (ret->qual != NULL) OPENSSL_free(ret->qual);
|
sl@0
|
187 |
if (ret != NULL) OPENSSL_free(ret);
|
sl@0
|
188 |
}
|
sl@0
|
189 |
return(NULL);
|
sl@0
|
190 |
}
|
sl@0
|
191 |
else
|
sl@0
|
192 |
return(ret);
|
sl@0
|
193 |
}
|
sl@0
|
194 |
|
sl@0
|
195 |
EXPORT_C char **TXT_DB_get_by_index(TXT_DB *db, int idx, char **value)
|
sl@0
|
196 |
{
|
sl@0
|
197 |
char **ret;
|
sl@0
|
198 |
LHASH *lh;
|
sl@0
|
199 |
|
sl@0
|
200 |
if (idx >= db->num_fields)
|
sl@0
|
201 |
{
|
sl@0
|
202 |
db->error=DB_ERROR_INDEX_OUT_OF_RANGE;
|
sl@0
|
203 |
return(NULL);
|
sl@0
|
204 |
}
|
sl@0
|
205 |
lh=db->index[idx];
|
sl@0
|
206 |
if (lh == NULL)
|
sl@0
|
207 |
{
|
sl@0
|
208 |
db->error=DB_ERROR_NO_INDEX;
|
sl@0
|
209 |
return(NULL);
|
sl@0
|
210 |
}
|
sl@0
|
211 |
ret=(char **)lh_retrieve(lh,value);
|
sl@0
|
212 |
db->error=DB_ERROR_OK;
|
sl@0
|
213 |
return(ret);
|
sl@0
|
214 |
}
|
sl@0
|
215 |
|
sl@0
|
216 |
EXPORT_C int TXT_DB_create_index(TXT_DB *db, int field, int (*qual)(char **),
|
sl@0
|
217 |
LHASH_HASH_FN_TYPE hash, LHASH_COMP_FN_TYPE cmp)
|
sl@0
|
218 |
{
|
sl@0
|
219 |
LHASH *idx;
|
sl@0
|
220 |
char **r;
|
sl@0
|
221 |
int i,n;
|
sl@0
|
222 |
|
sl@0
|
223 |
if (field >= db->num_fields)
|
sl@0
|
224 |
{
|
sl@0
|
225 |
db->error=DB_ERROR_INDEX_OUT_OF_RANGE;
|
sl@0
|
226 |
return(0);
|
sl@0
|
227 |
}
|
sl@0
|
228 |
if ((idx=lh_new(hash,cmp)) == NULL)
|
sl@0
|
229 |
{
|
sl@0
|
230 |
db->error=DB_ERROR_MALLOC;
|
sl@0
|
231 |
return(0);
|
sl@0
|
232 |
}
|
sl@0
|
233 |
n=sk_num(db->data);
|
sl@0
|
234 |
for (i=0; i<n; i++)
|
sl@0
|
235 |
{
|
sl@0
|
236 |
r=(char **)sk_value(db->data,i);
|
sl@0
|
237 |
if ((qual != NULL) && (qual(r) == 0)) continue;
|
sl@0
|
238 |
if ((r=lh_insert(idx,r)) != NULL)
|
sl@0
|
239 |
{
|
sl@0
|
240 |
db->error=DB_ERROR_INDEX_CLASH;
|
sl@0
|
241 |
db->arg1=sk_find(db->data,(char *)r);
|
sl@0
|
242 |
db->arg2=i;
|
sl@0
|
243 |
lh_free(idx);
|
sl@0
|
244 |
return(0);
|
sl@0
|
245 |
}
|
sl@0
|
246 |
}
|
sl@0
|
247 |
if (db->index[field] != NULL) lh_free(db->index[field]);
|
sl@0
|
248 |
db->index[field]=idx;
|
sl@0
|
249 |
db->qual[field]=qual;
|
sl@0
|
250 |
return(1);
|
sl@0
|
251 |
}
|
sl@0
|
252 |
|
sl@0
|
253 |
EXPORT_C long TXT_DB_write(BIO *out, TXT_DB *db)
|
sl@0
|
254 |
{
|
sl@0
|
255 |
long i,j,n,nn,l,tot=0;
|
sl@0
|
256 |
char *p,**pp,*f;
|
sl@0
|
257 |
BUF_MEM *buf=NULL;
|
sl@0
|
258 |
long ret= -1;
|
sl@0
|
259 |
|
sl@0
|
260 |
if ((buf=BUF_MEM_new()) == NULL)
|
sl@0
|
261 |
goto err;
|
sl@0
|
262 |
n=sk_num(db->data);
|
sl@0
|
263 |
nn=db->num_fields;
|
sl@0
|
264 |
for (i=0; i<n; i++)
|
sl@0
|
265 |
{
|
sl@0
|
266 |
pp=(char **)sk_value(db->data,i);
|
sl@0
|
267 |
|
sl@0
|
268 |
l=0;
|
sl@0
|
269 |
for (j=0; j<nn; j++)
|
sl@0
|
270 |
{
|
sl@0
|
271 |
if (pp[j] != NULL)
|
sl@0
|
272 |
l+=strlen(pp[j]);
|
sl@0
|
273 |
}
|
sl@0
|
274 |
if (!BUF_MEM_grow_clean(buf,(int)(l*2+nn))) goto err;
|
sl@0
|
275 |
|
sl@0
|
276 |
p=buf->data;
|
sl@0
|
277 |
for (j=0; j<nn; j++)
|
sl@0
|
278 |
{
|
sl@0
|
279 |
f=pp[j];
|
sl@0
|
280 |
if (f != NULL)
|
sl@0
|
281 |
for (;;)
|
sl@0
|
282 |
{
|
sl@0
|
283 |
if (*f == '\0') break;
|
sl@0
|
284 |
if (*f == '\t') *(p++)='\\';
|
sl@0
|
285 |
*(p++)= *(f++);
|
sl@0
|
286 |
}
|
sl@0
|
287 |
*(p++)='\t';
|
sl@0
|
288 |
}
|
sl@0
|
289 |
p[-1]='\n';
|
sl@0
|
290 |
j=p-buf->data;
|
sl@0
|
291 |
if (BIO_write(out,buf->data,(int)j) != j)
|
sl@0
|
292 |
goto err;
|
sl@0
|
293 |
tot+=j;
|
sl@0
|
294 |
}
|
sl@0
|
295 |
ret=tot;
|
sl@0
|
296 |
err:
|
sl@0
|
297 |
if (buf != NULL) BUF_MEM_free(buf);
|
sl@0
|
298 |
return(ret);
|
sl@0
|
299 |
}
|
sl@0
|
300 |
|
sl@0
|
301 |
EXPORT_C int TXT_DB_insert(TXT_DB *db, char **row)
|
sl@0
|
302 |
{
|
sl@0
|
303 |
int i;
|
sl@0
|
304 |
char **r;
|
sl@0
|
305 |
|
sl@0
|
306 |
for (i=0; i<db->num_fields; i++)
|
sl@0
|
307 |
{
|
sl@0
|
308 |
if (db->index[i] != NULL)
|
sl@0
|
309 |
{
|
sl@0
|
310 |
if ((db->qual[i] != NULL) &&
|
sl@0
|
311 |
(db->qual[i](row) == 0)) continue;
|
sl@0
|
312 |
r=(char **)lh_retrieve(db->index[i],row);
|
sl@0
|
313 |
if (r != NULL)
|
sl@0
|
314 |
{
|
sl@0
|
315 |
db->error=DB_ERROR_INDEX_CLASH;
|
sl@0
|
316 |
db->arg1=i;
|
sl@0
|
317 |
db->arg_row=r;
|
sl@0
|
318 |
goto err;
|
sl@0
|
319 |
}
|
sl@0
|
320 |
}
|
sl@0
|
321 |
}
|
sl@0
|
322 |
/* We have passed the index checks, now just append and insert */
|
sl@0
|
323 |
if (!sk_push(db->data,(char *)row))
|
sl@0
|
324 |
{
|
sl@0
|
325 |
db->error=DB_ERROR_MALLOC;
|
sl@0
|
326 |
goto err;
|
sl@0
|
327 |
}
|
sl@0
|
328 |
|
sl@0
|
329 |
for (i=0; i<db->num_fields; i++)
|
sl@0
|
330 |
{
|
sl@0
|
331 |
if (db->index[i] != NULL)
|
sl@0
|
332 |
{
|
sl@0
|
333 |
if ((db->qual[i] != NULL) &&
|
sl@0
|
334 |
(db->qual[i](row) == 0)) continue;
|
sl@0
|
335 |
lh_insert(db->index[i],row);
|
sl@0
|
336 |
}
|
sl@0
|
337 |
}
|
sl@0
|
338 |
return(1);
|
sl@0
|
339 |
err:
|
sl@0
|
340 |
return(0);
|
sl@0
|
341 |
}
|
sl@0
|
342 |
|
sl@0
|
343 |
EXPORT_C void TXT_DB_free(TXT_DB *db)
|
sl@0
|
344 |
{
|
sl@0
|
345 |
int i,n;
|
sl@0
|
346 |
char **p,*max;
|
sl@0
|
347 |
|
sl@0
|
348 |
if(db == NULL)
|
sl@0
|
349 |
return;
|
sl@0
|
350 |
|
sl@0
|
351 |
if (db->index != NULL)
|
sl@0
|
352 |
{
|
sl@0
|
353 |
for (i=db->num_fields-1; i>=0; i--)
|
sl@0
|
354 |
if (db->index[i] != NULL) lh_free(db->index[i]);
|
sl@0
|
355 |
OPENSSL_free(db->index);
|
sl@0
|
356 |
}
|
sl@0
|
357 |
if (db->qual != NULL)
|
sl@0
|
358 |
OPENSSL_free(db->qual);
|
sl@0
|
359 |
if (db->data != NULL)
|
sl@0
|
360 |
{
|
sl@0
|
361 |
for (i=sk_num(db->data)-1; i>=0; i--)
|
sl@0
|
362 |
{
|
sl@0
|
363 |
/* check if any 'fields' have been allocated
|
sl@0
|
364 |
* from outside of the initial block */
|
sl@0
|
365 |
p=(char **)sk_value(db->data,i);
|
sl@0
|
366 |
max=p[db->num_fields]; /* last address */
|
sl@0
|
367 |
if (max == NULL) /* new row */
|
sl@0
|
368 |
{
|
sl@0
|
369 |
for (n=0; n<db->num_fields; n++)
|
sl@0
|
370 |
if (p[n] != NULL) OPENSSL_free(p[n]);
|
sl@0
|
371 |
}
|
sl@0
|
372 |
else
|
sl@0
|
373 |
{
|
sl@0
|
374 |
for (n=0; n<db->num_fields; n++)
|
sl@0
|
375 |
{
|
sl@0
|
376 |
if (((p[n] < (char *)p) || (p[n] > max))
|
sl@0
|
377 |
&& (p[n] != NULL))
|
sl@0
|
378 |
OPENSSL_free(p[n]);
|
sl@0
|
379 |
}
|
sl@0
|
380 |
}
|
sl@0
|
381 |
OPENSSL_free(sk_value(db->data,i));
|
sl@0
|
382 |
}
|
sl@0
|
383 |
sk_free(db->data);
|
sl@0
|
384 |
}
|
sl@0
|
385 |
OPENSSL_free(db);
|
sl@0
|
386 |
}
|