File: | src/lib/libcrypto/txt_db/txt_db.c |
Warning: | line 134, column 3 Value stored to 'i' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* $OpenBSD: txt_db.c,v 1.19 2023/07/08 11:28:03 beck Exp $ */ |
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 | |
63 | #include <openssl/buffer.h> |
64 | #include <openssl/txt_db.h> |
65 | |
66 | #undef BUFSIZE512 |
67 | #define BUFSIZE512 512 |
68 | |
69 | TXT_DB * |
70 | TXT_DB_read(BIO *in, int num) |
71 | { |
72 | TXT_DB *ret = NULL((void *)0); |
73 | int er = 1; |
74 | int esc = 0; |
75 | long ln = 0; |
76 | int i, add, n; |
77 | int size = BUFSIZE512; |
78 | int offset = 0; |
79 | char *p, *f; |
80 | OPENSSL_STRING *pp; |
81 | BUF_MEM *buf = NULL((void *)0); |
82 | |
83 | if ((buf = BUF_MEM_new()) == NULL((void *)0)) |
84 | goto err; |
85 | if (!BUF_MEM_grow(buf, size)) |
86 | goto err; |
87 | |
88 | if ((ret = malloc(sizeof(TXT_DB))) == NULL((void *)0)) |
89 | goto err; |
90 | ret->num_fields = num; |
91 | ret->index = NULL((void *)0); |
92 | ret->qual = NULL((void *)0); |
93 | if ((ret->data = sk_OPENSSL_PSTRING_new_null()((struct stack_st_OPENSSL_PSTRING *)sk_new_null())) == NULL((void *)0)) |
94 | goto err; |
95 | if ((ret->index = reallocarray(NULL((void *)0), num, sizeof(*ret->index))) == NULL((void *)0)) |
96 | goto err; |
97 | if ((ret->qual = reallocarray(NULL((void *)0), num, sizeof(*(ret->qual)))) == NULL((void *)0)) |
98 | goto err; |
99 | for (i = 0; i < num; i++) { |
100 | ret->index[i] = NULL((void *)0); |
101 | ret->qual[i] = NULL((void *)0); |
102 | } |
103 | |
104 | add = (num + 1)*sizeof(char *); |
105 | buf->data[size-1] = '\0'; |
106 | offset = 0; |
107 | for (;;) { |
108 | if (offset != 0) { |
109 | size += BUFSIZE512; |
110 | if (!BUF_MEM_grow_clean(buf, size)) |
111 | 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') |
117 | break; |
118 | if ((offset == 0) && (buf->data[0] == '#')) |
119 | continue; |
120 | i = strlen(&(buf->data[offset])); |
121 | offset += i; |
122 | if (buf->data[offset-1] != '\n') |
123 | continue; |
124 | else { |
125 | buf->data[offset-1] = '\0'; /* blat the '\n' */ |
126 | if (!(p = malloc(add + offset))) |
127 | goto err; |
128 | offset = 0; |
129 | } |
130 | pp = (char **)p; |
131 | p += add; |
132 | n = 0; |
133 | pp[n++] = p; |
134 | i = 0; |
Value stored to 'i' is never read | |
135 | f = buf->data; |
136 | |
137 | esc = 0; |
138 | for (;;) { |
139 | if (*f == '\0') |
140 | break; |
141 | if (*f == '\t') { |
142 | if (esc) |
143 | p--; |
144 | else { |
145 | *(p++)='\0'; |
146 | f++; |
147 | if (n >= num) |
148 | break; |
149 | pp[n++] = p; |
150 | continue; |
151 | } |
152 | } |
153 | esc=(*f == '\\'); |
154 | *(p++)= *(f++); |
155 | } |
156 | *(p++)='\0'; |
157 | if ((n != num) || (*f != '\0')) { |
158 | fprintf(stderr(&__sF[2]), "wrong number of fields on line %ld (looking for field %d, got %d, '%s' left)\n",ln,num,n,f); |
159 | er = 2; |
160 | goto err; |
161 | } |
162 | pp[n] = p; |
163 | if (!sk_OPENSSL_PSTRING_push(ret->data, pp)sk_push(((_STACK*) (1 ? ret->data : (struct stack_st_OPENSSL_PSTRING *)0)), ((void*) (1 ? pp : (OPENSSL_STRING*)0)))) { |
164 | fprintf(stderr(&__sF[2]), "failure in sk_push\n"); |
165 | er = 2; |
166 | goto err; |
167 | } |
168 | } |
169 | er = 0; |
170 | |
171 | err: |
172 | BUF_MEM_free(buf); |
173 | if (er) { |
174 | if (er == 1) |
175 | fprintf(stderr(&__sF[2]), "malloc failure\n"); |
176 | if (ret != NULL((void *)0)) { |
177 | if (ret->data != NULL((void *)0)) |
178 | sk_OPENSSL_PSTRING_free(ret->data)sk_free(((_STACK*) (1 ? ret->data : (struct stack_st_OPENSSL_PSTRING *)0))); |
179 | free(ret->index); |
180 | free(ret->qual); |
181 | free(ret); |
182 | } |
183 | return (NULL((void *)0)); |
184 | } else |
185 | return (ret); |
186 | } |
187 | LCRYPTO_ALIAS(TXT_DB_read)asm(""); |
188 | |
189 | OPENSSL_STRING * |
190 | TXT_DB_get_by_index(TXT_DB *db, int idx, OPENSSL_STRING *value) |
191 | { |
192 | OPENSSL_STRING *ret; |
193 | LHASH_OF(OPENSSL_STRING)struct lhash_st_OPENSSL_STRING *lh; |
194 | |
195 | if (idx >= db->num_fields) { |
196 | db->error = DB_ERROR_INDEX_OUT_OF_RANGE3; |
197 | return (NULL((void *)0)); |
198 | } |
199 | lh = db->index[idx]; |
200 | if (lh == NULL((void *)0)) { |
201 | db->error = DB_ERROR_NO_INDEX4; |
202 | return (NULL((void *)0)); |
203 | } |
204 | ret = lh_OPENSSL_STRING_retrieve(lh, value)((OPENSSL_STRING *)lh_retrieve(((_LHASH *)((void*) (1 ? lh : ( struct lhash_st_OPENSSL_STRING*)0))), ((void*) (1 ? value : ( OPENSSL_STRING*)0)))); |
205 | db->error = DB_ERROR_OK0; |
206 | return (ret); |
207 | } |
208 | LCRYPTO_ALIAS(TXT_DB_get_by_index)asm(""); |
209 | |
210 | int |
211 | TXT_DB_create_index(TXT_DB *db, int field, int (*qual)(OPENSSL_STRING *), |
212 | LHASH_HASH_FN_TYPE hash, LHASH_COMP_FN_TYPE cmp) |
213 | { |
214 | LHASH_OF(OPENSSL_STRING)struct lhash_st_OPENSSL_STRING *idx; |
215 | OPENSSL_STRING *r; |
216 | int i, n; |
217 | |
218 | if (field >= db->num_fields) { |
219 | db->error = DB_ERROR_INDEX_OUT_OF_RANGE3; |
220 | return (0); |
221 | } |
222 | /* FIXME: we lose type checking at this point */ |
223 | if ((idx = (LHASH_OF(OPENSSL_STRING)struct lhash_st_OPENSSL_STRING *)lh_new(hash, cmp)) == NULL((void *)0)) { |
224 | db->error = DB_ERROR_MALLOC1; |
225 | return (0); |
226 | } |
227 | n = sk_OPENSSL_PSTRING_num(db->data)sk_num(((_STACK*) (1 ? db->data : (struct stack_st_OPENSSL_PSTRING *)0))); |
228 | for (i = 0; i < n; i++) { |
229 | r = sk_OPENSSL_PSTRING_value(db->data, i)((OPENSSL_PSTRING)sk_value(((_STACK*) (1 ? db->data : (struct stack_st_OPENSSL_PSTRING*)0)), i)); |
230 | if ((qual != NULL((void *)0)) && (qual(r) == 0)) |
231 | continue; |
232 | if ((r = lh_OPENSSL_STRING_insert(idx, r)((OPENSSL_STRING *)lh_insert(((_LHASH *)((void*) (1 ? idx : ( struct lhash_st_OPENSSL_STRING*)0))), ((void*) (1 ? r : (OPENSSL_STRING *)0))))) != NULL((void *)0)) { |
233 | db->error = DB_ERROR_INDEX_CLASH2; |
234 | db->arg1 = sk_OPENSSL_PSTRING_find(db->data, r)sk_find(((_STACK*) (1 ? db->data : (struct stack_st_OPENSSL_PSTRING *)0)), ((void*) (1 ? r : (OPENSSL_STRING*)0))); |
235 | db->arg2 = i; |
236 | lh_OPENSSL_STRING_free(idx)lh_free(((_LHASH *)((void*) (1 ? idx : (struct lhash_st_OPENSSL_STRING *)0)))); |
237 | return (0); |
238 | } |
239 | } |
240 | if (db->index[field] != NULL((void *)0)) |
241 | lh_OPENSSL_STRING_free(db->index[field])lh_free(((_LHASH *)((void*) (1 ? db->index[field] : (struct lhash_st_OPENSSL_STRING*)0)))); |
242 | db->index[field] = idx; |
243 | db->qual[field] = qual; |
244 | return (1); |
245 | } |
246 | LCRYPTO_ALIAS(TXT_DB_create_index)asm(""); |
247 | |
248 | long |
249 | TXT_DB_write(BIO *out, TXT_DB *db) |
250 | { |
251 | long i, j,n, nn, l, tot = 0; |
252 | char *p, **pp, *f; |
253 | BUF_MEM *buf = NULL((void *)0); |
254 | long ret = -1; |
255 | |
256 | if ((buf = BUF_MEM_new()) == NULL((void *)0)) |
257 | goto err; |
258 | n = sk_OPENSSL_PSTRING_num(db->data)sk_num(((_STACK*) (1 ? db->data : (struct stack_st_OPENSSL_PSTRING *)0))); |
259 | nn = db->num_fields; |
260 | for (i = 0; i < n; i++) { |
261 | pp = sk_OPENSSL_PSTRING_value(db->data, i)((OPENSSL_PSTRING)sk_value(((_STACK*) (1 ? db->data : (struct stack_st_OPENSSL_PSTRING*)0)), i)); |
262 | |
263 | l = 0; |
264 | for (j = 0; j < nn; j++) { |
265 | if (pp[j] != NULL((void *)0)) |
266 | l += strlen(pp[j]); |
267 | } |
268 | if (!BUF_MEM_grow_clean(buf, (int)(l*2 + nn))) |
269 | goto err; |
270 | |
271 | p = buf->data; |
272 | for (j = 0; j < nn; j++) { |
273 | f = pp[j]; |
274 | if (f != NULL((void *)0)) |
275 | for (;;) { |
276 | if (*f == '\0') |
277 | break; |
278 | if (*f == '\t') |
279 | *(p++) = '\\'; |
280 | *(p++) = *(f++); |
281 | } |
282 | *(p++) = '\t'; |
283 | } |
284 | p[-1] = '\n'; |
285 | j = p - buf->data; |
286 | if (BIO_write(out, buf->data, (int)j) != j) |
287 | goto err; |
288 | tot += j; |
289 | } |
290 | ret = tot; |
291 | |
292 | err: |
293 | if (buf != NULL((void *)0)) |
294 | BUF_MEM_free(buf); |
295 | return (ret); |
296 | } |
297 | LCRYPTO_ALIAS(TXT_DB_write)asm(""); |
298 | |
299 | int |
300 | TXT_DB_insert(TXT_DB *db, OPENSSL_STRING *row) |
301 | { |
302 | int i; |
303 | OPENSSL_STRING *r; |
304 | |
305 | for (i = 0; i < db->num_fields; i++) { |
306 | if (db->index[i] != NULL((void *)0)) { |
307 | if ((db->qual[i] != NULL((void *)0)) && (db->qual[i](row) == 0)) |
308 | continue; |
309 | r = lh_OPENSSL_STRING_retrieve(db->index[i], row)((OPENSSL_STRING *)lh_retrieve(((_LHASH *)((void*) (1 ? db-> index[i] : (struct lhash_st_OPENSSL_STRING*)0))), ((void*) (1 ? row : (OPENSSL_STRING*)0)))); |
310 | if (r != NULL((void *)0)) { |
311 | db->error = DB_ERROR_INDEX_CLASH2; |
312 | db->arg1 = i; |
313 | db->arg_row = r; |
314 | goto err; |
315 | } |
316 | } |
317 | } |
318 | /* We have passed the index checks, now just append and insert */ |
319 | if (!sk_OPENSSL_PSTRING_push(db->data, row)sk_push(((_STACK*) (1 ? db->data : (struct stack_st_OPENSSL_PSTRING *)0)), ((void*) (1 ? row : (OPENSSL_STRING*)0)))) { |
320 | db->error = DB_ERROR_MALLOC1; |
321 | goto err; |
322 | } |
323 | |
324 | for (i = 0; i < db->num_fields; i++) { |
325 | if (db->index[i] != NULL((void *)0)) { |
326 | if ((db->qual[i] != NULL((void *)0)) && (db->qual[i](row) == 0)) |
327 | continue; |
328 | (void)lh_OPENSSL_STRING_insert(db->index[i], row)((OPENSSL_STRING *)lh_insert(((_LHASH *)((void*) (1 ? db-> index[i] : (struct lhash_st_OPENSSL_STRING*)0))), ((void*) (1 ? row : (OPENSSL_STRING*)0)))); |
329 | } |
330 | } |
331 | return (1); |
332 | |
333 | err: |
334 | return (0); |
335 | } |
336 | LCRYPTO_ALIAS(TXT_DB_insert)asm(""); |
337 | |
338 | void |
339 | TXT_DB_free(TXT_DB *db) |
340 | { |
341 | int i, n; |
342 | char **p, *max; |
343 | |
344 | if (db == NULL((void *)0)) |
345 | return; |
346 | |
347 | if (db->index != NULL((void *)0)) { |
348 | for (i = db->num_fields - 1; i >= 0; i--) |
349 | if (db->index[i] != NULL((void *)0)) |
350 | lh_OPENSSL_STRING_free(db->index[i])lh_free(((_LHASH *)((void*) (1 ? db->index[i] : (struct lhash_st_OPENSSL_STRING *)0)))); |
351 | free(db->index); |
352 | } |
353 | free(db->qual); |
354 | if (db->data != NULL((void *)0)) { |
355 | for (i = sk_OPENSSL_PSTRING_num(db->data)sk_num(((_STACK*) (1 ? db->data : (struct stack_st_OPENSSL_PSTRING *)0))) - 1; i >= 0; i--) { |
356 | /* check if any 'fields' have been allocated |
357 | * from outside of the initial block */ |
358 | p = sk_OPENSSL_PSTRING_value(db->data, i)((OPENSSL_PSTRING)sk_value(((_STACK*) (1 ? db->data : (struct stack_st_OPENSSL_PSTRING*)0)), i)); |
359 | max = p[db->num_fields]; /* last address */ |
360 | if (max == NULL((void *)0)) /* new row */ |
361 | { |
362 | for (n = 0; n < db->num_fields; n++) |
363 | free(p[n]); |
364 | } else { |
365 | for (n = 0; n < db->num_fields; n++) { |
366 | if (((p[n] < (char *)p) || |
367 | (p[n] > max)) && |
368 | (p[n] != NULL((void *)0))) |
369 | free(p[n]); |
370 | } |
371 | } |
372 | free(sk_OPENSSL_PSTRING_value(db->data, i)((OPENSSL_PSTRING)sk_value(((_STACK*) (1 ? db->data : (struct stack_st_OPENSSL_PSTRING*)0)), i))); |
373 | } |
374 | sk_OPENSSL_PSTRING_free(db->data)sk_free(((_STACK*) (1 ? db->data : (struct stack_st_OPENSSL_PSTRING *)0))); |
375 | } |
376 | free(db); |
377 | } |
378 | LCRYPTO_ALIAS(TXT_DB_free)asm(""); |