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