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