Submitted by: Jeremy Shapiro <jnshapir@us.ibm.com>
authorDr. Stephen Henson <steve@openssl.org>
Sat, 7 Mar 2009 16:58:43 +0000 (16:58 +0000)
committerDr. Stephen Henson <steve@openssl.org>
Sat, 7 Mar 2009 16:58:43 +0000 (16:58 +0000)
Reviewed by: steve@openssl.org

Improve efficientcy of mem_gets().

CHANGES
crypto/bio/bss_mem.c

diff --git a/CHANGES b/CHANGES
index fb9868fa47e9341f9ac4841e910563d1b4126064..514193682366656fba4a116d5cf0eb95a6f6a83f 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -4,6 +4,10 @@
 
  Changes between 0.9.8j and 0.9.8k  [xx XXX xxxx]
 
+  *) Improve efficiency of mem_gets: don't search whole buffer each time
+     for a '\n'
+     [Jeremy Shapiro <jnshapir@us.ibm.com>]
+
   *) New -hex option for openssl rand.
      [Matthieu Herrb]
 
index a4edb711aec1a37389e7db77726ae89f1f67dd50..e7ab9cb3a3f86ee12ce17f470b44ba9a19092c55 100644 (file)
@@ -284,6 +284,7 @@ static int mem_gets(BIO *bp, char *buf, int size)
 
        BIO_clear_retry_flags(bp);
        j=bm->length;
+       if ((size-1) < j) j=size-1;
        if (j <= 0)
                {
                *buf='\0';
@@ -292,17 +293,18 @@ static int mem_gets(BIO *bp, char *buf, int size)
        p=bm->data;
        for (i=0; i<j; i++)
                {
-               if (p[i] == '\n') break;
-               }
-       if (i == j)
-               {
-               BIO_set_retry_read(bp);
-               /* return(-1);  change the semantics 0.6.6a */ 
+               if (p[i] == '\n')
+                       {
+                       i++;
+                       break;
+                       }
                }
-       else
-               i++;
-       /* i is the max to copy */
-       if ((size-1) < i) i=size-1;
+
+       /*
+        * i is now the max num of bytes to copy, either j or up to
+        * and including the first newline
+        */ 
+
        i=mem_read(bp,buf,i);
        if (i > 0) buf[i]='\0';
        ret=i;