Fix for memory leak.
[openssl.git] / crypto / conf / conf.c
index 68243e9f015324b9dc1859aeac5a831d255be574..a55ecbb6587a8521b2ce31df80d5019b23a22d19 100644 (file)
@@ -1,5 +1,5 @@
 /* crypto/conf/conf.c */
-/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
+/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
 #include <stdio.h>
 #include <errno.h>
 #include "cryptlib.h"
-#include "stack.h"
-#include "lhash.h"
-#include "conf.h"
-#include "buffer.h"
-#include "err.h"
+#include <openssl/stack.h>
+#include <openssl/lhash.h>
+#include <openssl/conf.h>
+#include <openssl/buffer.h>
+#include <openssl/err.h>
 
 #include "conf_lcl.h"
 
-#ifndef NOPROTO
 static void value_free_hash(CONF_VALUE *a, LHASH *conf);
 static void value_free_stack(CONF_VALUE *a,LHASH *conf);
 static unsigned long hash(CONF_VALUE *v);
@@ -79,36 +78,21 @@ static int str_copy(LHASH *conf,char *section,char **to, char *from);
 static char *scan_quote(char *p);
 static CONF_VALUE *new_section(LHASH *conf,char *section);
 static CONF_VALUE *get_section(LHASH *conf,char *section);
-#else
-static void value_free_hash();
-static void value_free_stack();
-static unsigned long hash();
-static int cmp();
-static char *eat_ws();
-static char *eat_alpha_numeric();
-static void clear_comments();
-static int str_copy();
-static char *scan_quote();
-static CONF_VALUE *new_section();
-static CONF_VALUE *get_section();
-#endif
-
-#define scan_esc(p)    ((*(++p) == '\0')?(p):(++p))
+#define scan_esc(p)    ((((p)[1] == '\0')?(p++):(p+=2)),p)
 
-char *CONF_version="CONF part of SSLeay 0.8.1b 29-Jun-1998";
+const char *CONF_version="CONF" OPENSSL_VERSION_PTEXT;
 
-LHASH *CONF_load(h,file,line)
-LHASH *h;
-char *file;
-long *line;
+LHASH *CONF_load(LHASH *h, char *file, long *line)
        {
        LHASH *ret=NULL;
        FILE *in=NULL;
 #define BUFSIZE        512
+       char btmp[16];
        int bufnum=0,i,ii;
        BUF_MEM *buff=NULL;
        char *s,*p,*end;
-       int again,n,eline=0;
+       int again,n;
+       long eline=0;
        CONF_VALUE *v=NULL,*vv,*tv;
        CONF_VALUE *sv=NULL;
        char *section=NULL,*buf;
@@ -121,10 +105,16 @@ long *line;
                goto err;
                }
 
+#ifdef VMS
+       in=fopen(file,"r");
+#else
        in=fopen(file,"rb");
+#endif
        if (in == NULL)
                {
-               SYSerr(SYS_F_FOPEN,errno);
+               SYSerr(SYS_F_FOPEN,get_last_sys_error());
+               ERR_set_error_data(BUF_strdup(file),
+                       ERR_TXT_MALLOCED|ERR_TXT_STRING);
                CONFerr(CONF_F_CONF_LOAD,ERR_R_SYS_LIB);
                goto err;
                }
@@ -217,12 +207,21 @@ long *line;
                if (IS_EOF(*s)) continue; /* blank line */
                if (*s == '[')
                        {
+                       char *ss;
+
                        s++;
                        start=eat_ws(s);
-                       end=eat_alpha_numeric(start);
+                       ss=start;
+again:
+                       end=eat_alpha_numeric(ss);
                        p=eat_ws(end);
                        if (*p != ']')
                                {
+                               if (*p != '\0')
+                                       {
+                                       ss=p;
+                                       goto again;
+                                       }
                                CONFerr(CONF_F_CONF_LOAD,CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
                                goto err;
                                }
@@ -326,6 +325,8 @@ err:
        if (buff != NULL) BUF_MEM_free(buff);
        if (section != NULL) Free(section);
        if (line != NULL) *line=eline;
+       sprintf(btmp,"%ld",eline);
+       ERR_add_error_data(2,"line ",btmp);
        if (in != NULL) fclose(in);
        if ((h != ret) && (ret != NULL)) CONF_free(ret);
        if (v != NULL)
@@ -337,10 +338,7 @@ err:
        return(NULL);
        }
                
-char *CONF_get_string(conf,section,name)
-LHASH *conf;
-char *section;
-char *name;
+char *CONF_get_string(LHASH *conf, char *section, char *name)
        {
        CONF_VALUE *v,vv;
        char *p;
@@ -372,9 +370,7 @@ char *name;
                return(Getenv(name));
        }
 
-static CONF_VALUE *get_section(conf,section)
-LHASH *conf;
-char *section;
+static CONF_VALUE *get_section(LHASH *conf, char *section)
        {
        CONF_VALUE *v,vv;
 
@@ -385,9 +381,7 @@ char *section;
        return(v);
        }
 
-STACK *CONF_get_section(conf,section)
-LHASH *conf;
-char *section;
+STACK *CONF_get_section(LHASH *conf, char *section)
        {
        CONF_VALUE *v;
 
@@ -398,10 +392,7 @@ char *section;
                return(NULL);
        }
 
-long CONF_get_number(conf,section,name)
-LHASH *conf;
-char *section;
-char *name;
+long CONF_get_number(LHASH *conf, char *section, char *name)
        {
        char *str;
        long ret=0;
@@ -418,8 +409,7 @@ char *name;
                }
        }
 
-void CONF_free(conf)
-LHASH *conf;
+void CONF_free(LHASH *conf)
        {
        if (conf == NULL) return;
 
@@ -434,9 +424,7 @@ LHASH *conf;
        lh_free(conf);
        }
 
-static void value_free_hash(a,conf)
-CONF_VALUE *a;
-LHASH *conf;
+static void value_free_hash(CONF_VALUE *a, LHASH *conf)
        {
        if (a->name != NULL)
                {
@@ -444,9 +432,7 @@ LHASH *conf;
                }
        }
 
-static void value_free_stack(a,conf)
-CONF_VALUE *a;
-LHASH *conf;
+static void value_free_stack(CONF_VALUE *a, LHASH *conf)
        {
        CONF_VALUE *vv;
        STACK *sk;
@@ -467,8 +453,7 @@ LHASH *conf;
        Free(a);
        }
 
-static void clear_comments(p)
-char *p;
+static void clear_comments(char *p)
        {
        char *to;
 
@@ -497,10 +482,7 @@ char *p;
                }
        }
 
-static int str_copy(conf,section,pto,from)
-LHASH *conf;
-char *section;
-char **pto,*from;
+static int str_copy(LHASH *conf, char *section, char **pto, char *from)
        {
        int q,r,rr=0,to=0,len=0;
        char *s,*e,*rp,*p,*rrp,*np,*cp,v;
@@ -614,16 +596,14 @@ err:
        return(0);
        }
 
-static char *eat_ws(p)
-char *p;
+static char *eat_ws(char *p)
        {
        while (IS_WS(*p) && (!IS_EOF(*p)))
                p++;
        return(p);
        }
 
-static char *eat_alpha_numeric(p)
-char *p;
+static char *eat_alpha_numeric(char *p)
        {
        for (;;)
                {
@@ -638,14 +618,12 @@ char *p;
                }
        }
 
-static unsigned long hash(v)
-CONF_VALUE *v;
+static unsigned long hash(CONF_VALUE *v)
        {
        return((lh_strhash(v->section)<<2)^lh_strhash(v->name));
        }
 
-static int cmp(a,b)
-CONF_VALUE *a,*b;
+static int cmp(CONF_VALUE *a, CONF_VALUE *b)
        {
        int i;
 
@@ -666,8 +644,7 @@ CONF_VALUE *a,*b;
                return((a->name == NULL)?-1:1);
        }
 
-static char *scan_quote(p)
-char *p;
+static char *scan_quote(char *p)
        {
        int q= *p;
 
@@ -685,9 +662,7 @@ char *p;
        return(p);
        }
 
-static CONF_VALUE *new_section(conf,section)
-LHASH *conf;
-char *section;
+static CONF_VALUE *new_section(LHASH *conf, char *section)
        {
        STACK *sk=NULL;
        int ok=0,i;
@@ -706,13 +681,13 @@ char *section;
        v->value=(char *)sk;
        
        vv=(CONF_VALUE *)lh_insert(conf,(char *)v);
-#ifndef WIN16
        if (vv != NULL)
                {
+#if !defined(NO_STDIO) && !defined(WIN16)
                fprintf(stderr,"internal fault\n");
+#endif
                abort();
                }
-#endif
        ok=1;
 err:
        if (!ok)