1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
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)"
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
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.]
61 #include "internal/cryptlib.h"
62 #include <openssl/buffer.h>
63 #include <openssl/txt_db.h>
68 TXT_DB
*TXT_DB_read(BIO
*in
, int num
)
80 if ((buf
= BUF_MEM_new()) == NULL
)
82 if (!BUF_MEM_grow(buf
, size
))
85 if ((ret
= OPENSSL_malloc(sizeof(*ret
))) == NULL
)
87 ret
->num_fields
= num
;
90 if ((ret
->data
= sk_OPENSSL_PSTRING_new_null()) == NULL
)
92 if ((ret
->index
= OPENSSL_malloc(sizeof(*ret
->index
) * num
)) == NULL
)
94 if ((ret
->qual
= OPENSSL_malloc(sizeof(*(ret
->qual
)) * num
)) == NULL
)
96 for (i
= 0; i
< num
; i
++) {
101 add
= (num
+ 1) * sizeof(char *);
102 buf
->data
[size
- 1] = '\0';
107 if (!BUF_MEM_grow_clean(buf
, size
))
110 buf
->data
[offset
] = '\0';
111 BIO_gets(in
, &(buf
->data
[offset
]), size
- offset
);
113 if (buf
->data
[offset
] == '\0')
115 if ((offset
== 0) && (buf
->data
[0] == '#'))
117 i
= strlen(&(buf
->data
[offset
]));
119 if (buf
->data
[offset
- 1] != '\n')
122 buf
->data
[offset
- 1] = '\0'; /* blat the '\n' */
123 if ((p
= OPENSSL_malloc(add
+ offset
)) == NULL
)
154 if ((n
!= num
) || (*f
!= '\0')) {
155 ret
->error
= DB_ERROR_WRONG_NUM_FIELDS
;
159 if (!sk_OPENSSL_PSTRING_push(ret
->data
, pp
))
167 sk_OPENSSL_PSTRING_free(ret
->data
);
168 OPENSSL_free(ret
->index
);
169 OPENSSL_free(ret
->qual
);
175 OPENSSL_STRING
*TXT_DB_get_by_index(TXT_DB
*db
, int idx
,
176 OPENSSL_STRING
*value
)
179 LHASH_OF(OPENSSL_STRING
) *lh
;
181 if (idx
>= db
->num_fields
) {
182 db
->error
= DB_ERROR_INDEX_OUT_OF_RANGE
;
187 db
->error
= DB_ERROR_NO_INDEX
;
190 ret
= lh_OPENSSL_STRING_retrieve(lh
, value
);
191 db
->error
= DB_ERROR_OK
;
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
)
198 LHASH_OF(OPENSSL_STRING
) *idx
;
202 if (field
>= db
->num_fields
) {
203 db
->error
= DB_ERROR_INDEX_OUT_OF_RANGE
;
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
;
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))
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
);
220 lh_OPENSSL_STRING_free(idx
);
224 lh_OPENSSL_STRING_free(db
->index
[field
]);
225 db
->index
[field
] = idx
;
226 db
->qual
[field
] = qual
;
230 long TXT_DB_write(BIO
*out
, TXT_DB
*db
)
232 long i
, j
, n
, nn
, l
, tot
= 0;
237 if ((buf
= BUF_MEM_new()) == NULL
)
239 n
= sk_OPENSSL_PSTRING_num(db
->data
);
241 for (i
= 0; i
< n
; i
++) {
242 pp
= sk_OPENSSL_PSTRING_value(db
->data
, i
);
245 for (j
= 0; j
< nn
; j
++) {
249 if (!BUF_MEM_grow_clean(buf
, (int)(l
* 2 + nn
)))
253 for (j
= 0; j
< nn
; j
++) {
267 if (BIO_write(out
, buf
->data
, (int)j
) != j
)
277 int TXT_DB_insert(TXT_DB
*db
, OPENSSL_STRING
*row
)
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))
286 r
= lh_OPENSSL_STRING_retrieve(db
->index
[i
], row
);
288 db
->error
= DB_ERROR_INDEX_CLASH
;
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
;
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))
305 (void)lh_OPENSSL_STRING_insert(db
->index
[i
], row
);
313 void TXT_DB_free(TXT_DB
*db
)
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
);
326 OPENSSL_free(db
->qual
);
327 if (db
->data
!= NULL
) {
328 for (i
= sk_OPENSSL_PSTRING_num(db
->data
) - 1; i
>= 0; i
--) {
330 * check if any 'fields' have been allocated from outside of the
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
++)
339 for (n
= 0; n
< db
->num_fields
; n
++) {
340 if (((p
[n
] < (char *)p
) || (p
[n
] > max
)))
344 OPENSSL_free(sk_OPENSSL_PSTRING_value(db
->data
, i
));
346 sk_OPENSSL_PSTRING_free(db
->data
);