txt_db.c 9.7 KB

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