LHASH revamp. make depend.
[openssl.git] / 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=OPENSSL_malloc(sizeof(*ret->index)*num)) == NULL)
94                 goto err;
95         if ((ret->qual=OPENSSL_malloc(sizeof(*(ret->qual))*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 != NULL)
183                         {
184                         if (ret->data != NULL) sk_free(ret->data);
185                         if (ret->index != NULL) OPENSSL_free(ret->index);
186                         if (ret->qual != NULL) OPENSSL_free(ret->qual);
187                         if (ret != NULL) OPENSSL_free(ret);
188                         }
189                 return(NULL);
190                 }
191         else
192                 return(ret);
193         }
194
195 STRING *TXT_DB_get_by_index(TXT_DB *db, int idx, STRING *value)
196         {
197         STRING *ret;
198         LHASH_OF(STRING) *lh;
199
200         if (idx >= db->num_fields)
201                 {
202                 db->error=DB_ERROR_INDEX_OUT_OF_RANGE;
203                 return(NULL);
204                 }
205         lh=db->index[idx];
206         if (lh == NULL)
207                 {
208                 db->error=DB_ERROR_NO_INDEX;
209                 return(NULL);
210                 }
211         ret=lh_STRING_retrieve(lh,value);
212         db->error=DB_ERROR_OK;
213         return(ret);
214         }
215
216 int TXT_DB_create_index(TXT_DB *db, int field, int (*qual)(STRING *),
217                         LHASH_HASH_FN_TYPE hash, LHASH_COMP_FN_TYPE cmp)
218         {
219         LHASH_OF(STRING) *idx;
220         STRING *r;
221         int i,n;
222
223         if (field >= db->num_fields)
224                 {
225                 db->error=DB_ERROR_INDEX_OUT_OF_RANGE;
226                 return(0);
227                 }
228         /* FIXME: we lose type checking at this point */
229         if ((idx=(LHASH_OF(STRING) *)lh_new(hash,cmp)) == NULL)
230                 {
231                 db->error=DB_ERROR_MALLOC;
232                 return(0);
233                 }
234         n=sk_num(db->data);
235         for (i=0; i<n; i++)
236                 {
237                 r=(STRING *)sk_value(db->data,i);
238                 if ((qual != NULL) && (qual(r) == 0)) continue;
239                 if ((r=lh_STRING_insert(idx,r)) != NULL)
240                         {
241                         db->error=DB_ERROR_INDEX_CLASH;
242                         db->arg1=sk_find(db->data,(char *)r);
243                         db->arg2=i;
244                         lh_STRING_free(idx);
245                         return(0);
246                         }
247                 }
248         if (db->index[field] != NULL) lh_STRING_free(db->index[field]);
249         db->index[field]=idx;
250         db->qual[field]=qual;
251         return(1);
252         }
253
254 long TXT_DB_write(BIO *out, TXT_DB *db)
255         {
256         long i,j,n,nn,l,tot=0;
257         char *p,**pp,*f;
258         BUF_MEM *buf=NULL;
259         long ret= -1;
260
261         if ((buf=BUF_MEM_new()) == NULL)
262                 goto err;
263         n=sk_num(db->data);
264         nn=db->num_fields;
265         for (i=0; i<n; i++)
266                 {
267                 pp=(char **)sk_value(db->data,i);
268
269                 l=0;
270                 for (j=0; j<nn; j++)
271                         {
272                         if (pp[j] != NULL)
273                                 l+=strlen(pp[j]);
274                         }
275                 if (!BUF_MEM_grow_clean(buf,(int)(l*2+nn))) goto err;
276
277                 p=buf->data;
278                 for (j=0; j<nn; j++)
279                         {
280                         f=pp[j];
281                         if (f != NULL)
282                                 for (;;) 
283                                         {
284                                         if (*f == '\0') break;
285                                         if (*f == '\t') *(p++)='\\';
286                                         *(p++)= *(f++);
287                                         }
288                         *(p++)='\t';
289                         }
290                 p[-1]='\n';
291                 j=p-buf->data;
292                 if (BIO_write(out,buf->data,(int)j) != j)
293                         goto err;
294                 tot+=j;
295                 }
296         ret=tot;
297 err:
298         if (buf != NULL) BUF_MEM_free(buf);
299         return(ret);
300         }
301
302 int TXT_DB_insert(TXT_DB *db, STRING *row)
303         {
304         int i;
305         STRING *r;
306
307         for (i=0; i<db->num_fields; i++)
308                 {
309                 if (db->index[i] != NULL)
310                         {
311                         if ((db->qual[i] != NULL) &&
312                                 (db->qual[i](row) == 0)) continue;
313                         r=lh_STRING_retrieve(db->index[i],row);
314                         if (r != NULL)
315                                 {
316                                 db->error=DB_ERROR_INDEX_CLASH;
317                                 db->arg1=i;
318                                 db->arg_row=r;
319                                 goto err;
320                                 }
321                         }
322                 }
323         /* We have passed the index checks, now just append and insert */
324         if (!sk_push(db->data,(char *)row))
325                 {
326                 db->error=DB_ERROR_MALLOC;
327                 goto err;
328                 }
329
330         for (i=0; i<db->num_fields; i++)
331                 {
332                 if (db->index[i] != NULL)
333                         {
334                         if ((db->qual[i] != NULL) &&
335                                 (db->qual[i](row) == 0)) continue;
336                         lh_STRING_insert(db->index[i],row);
337                         }
338                 }
339         return(1);
340 err:
341         return(0);
342         }
343
344 void TXT_DB_free(TXT_DB *db)
345         {
346         int i,n;
347         char **p,*max;
348
349         if(db == NULL)
350             return;
351
352         if (db->index != NULL)
353                 {
354                 for (i=db->num_fields-1; i>=0; i--)
355                         if (db->index[i] != NULL) lh_STRING_free(db->index[i]);
356                 OPENSSL_free(db->index);
357                 }
358         if (db->qual != NULL)
359                 OPENSSL_free(db->qual);
360         if (db->data != NULL)
361                 {
362                 for (i=sk_num(db->data)-1; i>=0; i--)
363                         {
364                         /* check if any 'fields' have been allocated
365                          * from outside of the initial block */
366                         p=(char **)sk_value(db->data,i);
367                         max=p[db->num_fields]; /* last address */
368                         if (max == NULL) /* new row */
369                                 {
370                                 for (n=0; n<db->num_fields; n++)
371                                         if (p[n] != NULL) OPENSSL_free(p[n]);
372                                 }
373                         else
374                                 {
375                                 for (n=0; n<db->num_fields; n++)
376                                         {
377                                         if (((p[n] < (char *)p) || (p[n] > max))
378                                                 && (p[n] != NULL))
379                                                 OPENSSL_free(p[n]);
380                                         }
381                                 }
382                         OPENSSL_free(sk_value(db->data,i));
383                         }
384                 sk_free(db->data);
385                 }
386         OPENSSL_free(db);
387         }