]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
Use mmap() if available to read files.
authorNikos Mavrogiannopoulos <nmav@gnutls.org>
Wed, 12 Mar 2003 12:43:12 +0000 (12:43 +0000)
committerNikos Mavrogiannopoulos <nmav@gnutls.org>
Wed, 12 Mar 2003 12:43:12 +0000 (12:43 +0000)
NEWS
configure.in
lib/gnutls_dh_primes.c
lib/gnutls_x509.c
src/cli.c

diff --git a/NEWS b/NEWS
index 2f5885a4ca7d05d50963024a2fca622b46bbbe81..e7d5f68aaa9c8f9cf244f4febb157b8553badf02 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -11,6 +11,7 @@ Version 0.9.1
   by some CAs. Patch by Ian Peters <itp@ximian.com>.
 - Added an strnstr() function and the requirement in some functions to
   use null terminated PEM structures is no more.
+- Use mmap() if available to read files.
 
 Version 0.9.0 (03/03/2003)
 - This version is not binary compatible with the previous ones.
index 77b5c6eec01429237804d6e9469ba9308fcd10c6..7b98a52ec530064b8b70be343d56c4b84f825bc2 100644 (file)
@@ -151,7 +151,7 @@ AC_HEADER_TIME
 AC_CHECK_HEADERS(unistd.h pwd.h strings.h stdarg.h)
 AC_CHECK_HEADERS(sys/stat.h sys/types.h sys/socket.h)
 AC_CHECK_HEADERS(errno.h sys/time.h time.h)
-AC_CHECK_FUNCS(bzero memset memmove bcopy strnstr memcmp memcpy,,)
+AC_CHECK_FUNCS(bzero memset memmove bcopy strnstr memcmp memcpy mmap,,)
 AC_FUNC_ALLOCA
 
 
index 4575c6076cb367c435a8627852baecda1bb4ef2b..cb64cc6d7b6af5e42ea2be082ebdc33dda2bef50 100644 (file)
@@ -155,7 +155,7 @@ int gnutls_dh_params_import_raw(gnutls_dh_params dh_params, const gnutls_datum *
 int gnutls_dh_params_init(gnutls_dh_params * dh_params)
 {
 
-       (*dh_params) = gnutls_calloc(1, sizeof(gnutls_dh_params));
+       (*dh_params) = gnutls_calloc(1, sizeof(_gnutls_dh_params));
        if (*dh_params == NULL) {
                gnutls_assert();
                return GNUTLS_E_MEMORY_ERROR;
index 87f008851f6dfdb744ecd0add5867ea3ec7a1dfe..32f19e1ef4df93d6d783b3c51c239d0c9a58ac41 100644 (file)
@@ -526,36 +526,68 @@ static int read_key_mem(gnutls_certificate_credentials res, const char *key, int
 #include <fcntl.h>
 #include <errno.h>
 
-opaque * _gnutls_file_to_str( const char * file, size_t* str_size)
+#ifdef HAVE_MMAP
+# include <unistd.h>
+# include <sys/mman.h>
+#endif
+
+typedef struct {
+       opaque * data;
+       size_t size;
+       int mmaped;
+} strfile;
+
+inline static void _strfile_free( strfile *x)
+{
+       if (x->mmaped) return;
+       
+       gnutls_free( x->data);
+       x->data = NULL;
+}
+
+strfile _gnutls_file_to_str( const char * file)
 {
        int fd1 = -1;
-       opaque * ret = NULL;
        struct stat stat_st;
        size_t tot_size;
        size_t left;
+       opaque* tmp;
        ssize_t i = 0;
+       strfile null = { NULL, 0, 0 };
+       strfile ret = { NULL, 0, 0 };
        
        fd1 = open( file, 0);
        if (fd1==-1) {
                gnutls_assert();
-               return NULL;
+               return null;
        }
        
        if (fstat( fd1, &stat_st) == -1) {
                gnutls_assert();
                goto error;
        }
-       
+
        tot_size = stat_st.st_size;
-       ret = gnutls_malloc( tot_size + 1);
-       if (ret == NULL) {
+
+#ifdef HAVE_MMAP
+       if ((tmp=mmap( NULL, tot_size, PROT_READ, MAP_SHARED, fd1, 0)) != MAP_FAILED) {
+               ret.mmaped = 1;
+               ret.data = tmp;
+               ret.size = tot_size;
+               
+               return ret;
+       }
+#endif
+
+       ret.data = gnutls_malloc( tot_size);
+       if (ret.data == NULL) {
                gnutls_assert();
                goto error;
        }
        
        left = tot_size;
        while (left > 0) {
-               i = read( fd1, &ret[tot_size - left], left);
+               i = read( fd1, &ret.data[tot_size - left], left);
                if (i == -1) {
                        if (errno == EAGAIN || errno == EINTR)
                                continue;
@@ -568,18 +600,20 @@ opaque * _gnutls_file_to_str( const char * file, size_t* str_size)
                left -= i;
        }
 
-       close(fd1);
+       ret.size = tot_size - left;
 
-       *str_size = tot_size - left;
-       ret[*str_size] = 0; /* null terminated */
+       ret.mmaped = 0;
+
+       close(fd1);
 
        return ret;
        
        error:
-               gnutls_free( ret);
+                       
+               if (!ret.mmaped)
+                       gnutls_free( ret.data);
                close(fd1);
-               return NULL;
-               
+               return null;
 }
 
 /* Reads a certificate file
@@ -587,18 +621,17 @@ opaque * _gnutls_file_to_str( const char * file, size_t* str_size)
 static int read_cert_file(gnutls_certificate_credentials res, const char *certfile,
        gnutls_x509_crt_fmt type)
 {
-       size_t size;
        int ret;
-       char *x;
+       strfile x;
 
-       x = _gnutls_file_to_str( certfile, &size);
-       if (x == NULL) {
+       x = _gnutls_file_to_str( certfile);
+       if (x.data == NULL) {
                gnutls_assert();
                return GNUTLS_E_FILE_ERROR;
        }
 
-       ret = read_cert_mem( res, xsize, type);
-       gnutls_free(x);
+       ret = read_cert_mem( res, x.data, x.size, type);
+       _strfile_free(&x);
        
        return ret;
 
@@ -612,19 +645,17 @@ static int read_cert_file(gnutls_certificate_credentials res, const char *certfi
 static int read_key_file(gnutls_certificate_credentials res, const char *keyfile,
        gnutls_x509_crt_fmt type)
 {
-       size_t size;
        int ret;
-       opaque* x;
+       strfile x;
 
-       x = _gnutls_file_to_str( keyfile, &size);
-       if (x == NULL) {
+       x = _gnutls_file_to_str( keyfile);
+       if (x.data == NULL) {
                gnutls_assert();
                return GNUTLS_E_FILE_ERROR;
        }
 
-       ret = read_key_mem( res, x, size, type);
-       memset( x, 0, size);
-       gnutls_free(x);
+       ret = read_key_mem( res, x.data, x.size, type);
+       _strfile_free(&x);
        
        return ret;
 }
@@ -1040,24 +1071,22 @@ int gnutls_certificate_set_x509_trust_file(gnutls_certificate_credentials res,
                const char *cafile, gnutls_x509_crt_fmt type)
 {
        int ret, ret2;
-       size_t size;
-       opaque *x;
+       strfile x;
 
-       x = _gnutls_file_to_str( cafile, &size);
-       if (x == NULL) {
+       x = _gnutls_file_to_str( cafile);
+       if (x.data == NULL) {
                gnutls_assert();
                return GNUTLS_E_FILE_ERROR;
        }
-
        
        if (type==GNUTLS_X509_FMT_DER)
                ret = parse_der_ca_mem( &res->x509_ca_list, &res->x509_ncas,
-                       xsize);
+                       x.data, x.size);
        else
                ret = parse_pem_ca_mem( &res->x509_ca_list, &res->x509_ncas,
-                       xsize);
+                       x.data, x.size);
 
-       gnutls_free(x);
+       _strfile_free(&x);
 
        if (ret < 0) {
                gnutls_assert();
@@ -1258,23 +1287,22 @@ int gnutls_certificate_set_x509_crl_file(gnutls_certificate_credentials res,
                const char *crlfile, gnutls_x509_crt_fmt type)
 {
        int ret;
-       size_t size;
-       opaque * x;
+       strfile x;
 
-       x = _gnutls_file_to_str( crlfile, &size);
-       if (x == NULL) {
+       x = _gnutls_file_to_str( crlfile);
+       if (x.data == NULL) {
                gnutls_assert();
                return GNUTLS_E_FILE_ERROR;
        }
 
        if (type==GNUTLS_X509_FMT_DER)
                ret = parse_der_crl_mem( &res->x509_crl_list, &res->x509_ncrls,
-                       xsize);
+                       x.data, x.size);
        else
                ret = parse_pem_crl_mem( &res->x509_crl_list, &res->x509_ncrls,
-                       xsize);
+                       x.data, x.size);
        
-       gnutls_free(x);
+       _strfile_free(&x);
 
        if (ret < 0) {
                gnutls_assert();
index ae1fcd3166c128eb80f1c6634b666aa865781cec..1949d1b2e576458881d3c3fff8a527d76caad602 100644 (file)
--- a/src/cli.c
+++ b/src/cli.c
@@ -136,6 +136,9 @@ int len, i, ret;
                }
        }
 
+       if (client_certs_num > 0)
+               return 0; /* use the first one */
+
        return -1;
 
 }