2 BIO_s_mem issues solved:
- BIO mem read without reallocation - reading by parts becomes faster;
- flag added to rewind read write BIO mem on reset.
OpenSSL self-test report:
OpenSSL version: 1.1.0-dev
Last change: State machine rewrite. The state machine code has been ...
Options: -march=pentium -Wa,--noexecstack no-deprecated
no-ec_nistp_64_gcc_128 no-gmp no-jpake no-md2 no-rc5 no-sctp no-shared
no-ssl-trace no-store no-unit-test no-zlib no-zlib-dynamic static-engine
OS (uname): Linux kirill-Lenovo-B570e 3.13.0-68-generic #111-Ubuntu
SMP Fri Nov 6 18:18:09 UTC 2015 i686 i686 i686 GNU/Linux
OS (config): i686-whatever-linux2
Target (default): linux-elf
Target: linux-elf
Compiler: Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-linux-gnu/4.8/lto-wrapper
Target: i686-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
4.8.4-2ubuntu1~14.04'
--with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs
--enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr
--program-suffix=-4.8 --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls
--with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug
--enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap
--enable-plugin --with-system-zlib --disable-browser-plugin
--enable-java-awt=gtk --enable-gtk-cairo
--with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-i386/jre
--enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-i386
--with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-i386
--with-arch-directory=i386 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar
--enable-objc-gc --enable-targets=all --enable-multiarch --disable-werror
--with-arch-32=i686 --with-multilib-list=m32,m64,mx32 --with-tune=generic
--enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu
--target=i686-linux-gnu
Thread model: posix
gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04)
Test passed.
--
Best Regards,
Kirill Marinushkin
>From dd9e8382a6dcb4423377f4846cdf429d34ad8d29 Mon Sep 17 00:00:00 2001
From: Kirill Marinushkin <k.marinush...@gmail.com>
Date: Sun, 22 Nov 2015 15:04:14 +0100
Subject: [PATCH] BIO mem read without reallocation; flag added to rewind read
write BIO mem on reset
---
crypto/bio/bss_mem.c | 97 +++++++++++++++++++++++++++++++++++-------------
doc/crypto/BIO_s_mem.pod | 13 ++-----
include/openssl/bio.h | 11 +++++-
3 files changed, 84 insertions(+), 37 deletions(-)
diff --git a/crypto/bio/bss_mem.c b/crypto/bio/bss_mem.c
index 485a8bf..2fc853a 100644
--- a/crypto/bio/bss_mem.c
+++ b/crypto/bio/bss_mem.c
@@ -61,6 +61,7 @@
#include "internal/cryptlib.h"
#include <openssl/bio.h>
+
static int mem_write(BIO *h, const char *buf, int num);
static int mem_read(BIO *h, char *buf, int size);
static int mem_puts(BIO *h, const char *str);
@@ -69,6 +70,8 @@ static long mem_ctrl(BIO *h, int cmd, long arg1, void *arg2);
static int mem_new(BIO *h);
static int secmem_new(BIO *h);
static int mem_free(BIO *data);
+static int mem_buf_free(BIO *data, int free_all);
+static int mem_buf_sync(BIO *h);
static BIO_METHOD mem_method = {
BIO_TYPE_MEM,
"memory buffer",
@@ -113,6 +116,7 @@ BIO *BIO_new_mem_buf(void *buf, int len)
{
BIO *ret;
BUF_MEM *b;
+ BIO_BUF_MEM *bb;
size_t sz;
if (buf == NULL) {
@@ -122,10 +126,12 @@ BIO *BIO_new_mem_buf(void *buf, int len)
sz = (len < 0) ? strlen(buf) : (size_t)len;
if ((ret = BIO_new(BIO_s_mem())) == NULL)
return NULL;
- b = (BUF_MEM *)ret->ptr;
+ bb = (BIO_BUF_MEM *)ret->ptr;
+ b = bb->buf;
b->data = buf;
b->length = sz;
b->max = sz;
+ memcpy(bb->readp, bb->buf, sizeof(BUF_MEM));
ret->flags |= BIO_FLAGS_MEM_RDONLY;
/* Since this is static data retrying wont help */
ret->num = 0;
@@ -134,14 +140,19 @@ BIO *BIO_new_mem_buf(void *buf, int len)
static int mem_init(BIO *bi, unsigned long flags)
{
- BUF_MEM *b;
+ BIO_BUF_MEM *bb;
- if ((b = BUF_MEM_new_ex(flags)) == NULL)
+ if ((bb = (BIO_BUF_MEM*)malloc(sizeof(BIO_BUF_MEM))) == NULL)
+ return(0);
+ if ((bb->buf = BUF_MEM_new_ex(flags)) == NULL)
+ return(0);
+ if ((bb->readp = (BUF_MEM*)malloc(sizeof(BUF_MEM))) == NULL)
return(0);
+ memcpy(bb->readp, bb->buf, sizeof(BUF_MEM));
bi->shutdown = 1;
bi->init = 1;
bi->num = -1;
- bi->ptr = (char *)b;
+ bi->ptr = (char *)bb;
return(1);
}
@@ -157,37 +168,60 @@ static int secmem_new(BIO *bi)
static int mem_free(BIO *a)
{
+ return (mem_buf_free(a, 1));
+}
+
+static int mem_buf_free(BIO *a, int free_all)
+{
if (a == NULL)
return (0);
if (a->shutdown) {
if ((a->init) && (a->ptr != NULL)) {
BUF_MEM *b;
- b = (BUF_MEM *)a->ptr;
- if (a->flags & BIO_FLAGS_MEM_RDONLY)
- b->data = NULL;
- BUF_MEM_free(b);
+ BIO_BUF_MEM *bb;
+ if((bb = (BIO_BUF_MEM *)a->ptr) != NULL) {
+ b = bb->buf;
+ if (a->flags & BIO_FLAGS_MEM_RDONLY)
+ b->data = NULL;
+ BUF_MEM_free(b);
+ if(free_all)
+ free(bb);
+ }
a->ptr = NULL;
}
}
return (1);
}
+/*
+ * Reallocate memory buffer if read pointer differs
+ */
+static int mem_buf_sync(BIO *b) {
+ if((b != NULL) && (b->init) && (b->ptr != NULL)) {
+ BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
+ if(bbm->readp->data != bbm->buf->data) {
+ memmove(&(bbm->buf->data[0]), &(bbm->readp->data[0]), bbm->readp->length);
+ bbm->buf->length = bbm->readp->length;
+ bbm->readp->data = bbm->buf->data;
+ }
+ }
+ return (0);
+}
+
static int mem_read(BIO *b, char *out, int outl)
{
int ret = -1;
BUF_MEM *bm;
+ BIO_BUF_MEM *bbm;
- bm = (BUF_MEM *)b->ptr;
+ bbm = (BIO_BUF_MEM *)b->ptr;
+ bm = bbm->readp;
BIO_clear_retry_flags(b);
ret = (outl >= 0 && (size_t)outl > bm->length) ? (int)bm->length : outl;
if ((out != NULL) && (ret > 0)) {
memcpy(out, bm->data, ret);
bm->length -= ret;
- if (b->flags & BIO_FLAGS_MEM_RDONLY)
- bm->data += ret;
- else {
- memmove(&(bm->data[0]), &(bm->data[ret]), bm->length);
- }
+ bm->data += ret;
} else if (bm->length == 0) {
ret = b->num;
if (ret != 0)
@@ -200,9 +234,9 @@ static int mem_write(BIO *b, const char *in, int inl)
{
int ret = -1;
int blen;
- BUF_MEM *bm;
+ BIO_BUF_MEM *bbm;
- bm = (BUF_MEM *)b->ptr;
+ bbm = (BIO_BUF_MEM *)b->ptr;
if (in == NULL) {
BIOerr(BIO_F_MEM_WRITE, BIO_R_NULL_PARAMETER);
goto end;
@@ -214,10 +248,12 @@ static int mem_write(BIO *b, const char *in, int inl)
}
BIO_clear_retry_flags(b);
- blen = bm->length;
- if (BUF_MEM_grow_clean(bm, blen + inl) == 0)
+ blen = bbm->readp->length;
+ mem_buf_sync(b);
+ if (BUF_MEM_grow_clean(bbm->buf, blen + inl) == 0)
goto end;
- memcpy(&(bm->data[blen]), in, inl);
+ memcpy(&(bbm->buf->data[blen]), in, inl);
+ memcpy(bbm->readp, bbm->buf, sizeof(BUF_MEM));
ret = inl;
end:
return (ret);
@@ -228,28 +264,32 @@ static long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
long ret = 1;
char **pptr;
- BUF_MEM *bm = (BUF_MEM *)b->ptr;
+ BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
+ BUF_MEM *bm;
switch (cmd) {
case BIO_CTRL_RESET:
+ bm = bbm->buf;
if (bm->data != NULL) {
/* For read only case reset to the start again */
- if (b->flags & BIO_FLAGS_MEM_RDONLY) {
- bm->data -= bm->max - bm->length;
+ if ((b->flags & BIO_FLAGS_MEM_RDONLY) || (b->flags & BIO_FLAGS_NONCLEAR_RST)) {
bm->length = bm->max;
} else {
memset(bm->data, 0, bm->max);
bm->length = 0;
}
+ memcpy(bbm->readp, bbm->buf, sizeof(BUF_MEM));
}
break;
case BIO_CTRL_EOF:
+ bm = bbm->readp;
ret = (long)(bm->length == 0);
break;
case BIO_C_SET_BUF_MEM_EOF_RETURN:
b->num = (int)num;
break;
case BIO_CTRL_INFO:
+ bm = bbm->readp;
ret = (long)bm->length;
if (ptr != NULL) {
pptr = (char **)ptr;
@@ -257,12 +297,16 @@ static long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
}
break;
case BIO_C_SET_BUF_MEM:
- mem_free(b);
+ mem_buf_free(b, 0);
b->shutdown = (int)num;
- b->ptr = ptr;
+ bbm->buf = ptr;
+ memcpy(bbm->readp, bbm->buf, sizeof(BUF_MEM));
+ b->ptr = bbm;
break;
case BIO_C_GET_BUF_MEM_PTR:
if (ptr != NULL) {
+ mem_buf_sync(b);
+ bm = bbm->readp;
pptr = (char **)ptr;
*pptr = (char *)bm;
}
@@ -278,6 +322,7 @@ static long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
ret = 0L;
break;
case BIO_CTRL_PENDING:
+ bm = bbm->readp;
ret = (long)bm->length;
break;
case BIO_CTRL_DUP:
@@ -298,7 +343,8 @@ static int mem_gets(BIO *bp, char *buf, int size)
int i, j;
int ret = -1;
char *p;
- BUF_MEM *bm = (BUF_MEM *)bp->ptr;
+ BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)bp->ptr;
+ BUF_MEM *bm = bbm->readp;
BIO_clear_retry_flags(bp);
j = bm->length;
@@ -320,7 +366,6 @@ static int mem_gets(BIO *bp, char *buf, int size)
* 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';
diff --git a/doc/crypto/BIO_s_mem.pod b/doc/crypto/BIO_s_mem.pod
index 1aa7e6e..4a6ba0f 100644
--- a/doc/crypto/BIO_s_mem.pod
+++ b/doc/crypto/BIO_s_mem.pod
@@ -39,9 +39,10 @@ Memory BIOs support BIO_gets() and BIO_puts().
If the BIO_CLOSE flag is set when a memory BIO is freed then the underlying
BUF_MEM structure is also freed.
-Calling BIO_reset() on a read write memory BIO clears any data in it. On a
-read only BIO it restores the BIO to its original state and the read only
-data can be read again.
+Calling BIO_reset() on a read write memory BIO clears any data in it if the
+flag BIO_FLAGS_NONCLEAR_RST is not set. On a read only BIO or if the flag
+BIO_FLAGS_NONCLEAR_RST is set it restores the BIO to its original state and
+the data can be read again.
BIO_eof() is true if no data is in the BIO.
@@ -90,12 +91,6 @@ give undefined results, including perhaps a program crash.
There should be an option to set the maximum size of a memory BIO.
-There should be a way to "rewind" a read write BIO without destroying
-its contents.
-
-The copying operation should not occur after every small read of a large BIO
-to improve efficiency.
-
=head1 EXAMPLE
Create a memory BIO and write some data to it:
diff --git a/include/openssl/bio.h b/include/openssl/bio.h
index 09a9510..37b892b 100644
--- a/include/openssl/bio.h
+++ b/include/openssl/bio.h
@@ -217,10 +217,12 @@ extern "C" {
# define BIO_FLAGS_BASE64_NO_NL 0x100
/*
- * This is used with memory BIOs: it means we shouldn't free up or change the
- * data in any way.
+ * This is used with memory BIOs:
+ * BIO_FLAGS_MEM_RDONLY means we shouldn't free up or change the data in any way;
+ * BIO_FLAGS_NONCLEAR_RST means we should't clear data on reset.
*/
# define BIO_FLAGS_MEM_RDONLY 0x200
+# define BIO_FLAGS_NONCLEAR_RST 0x400
typedef struct bio_st BIO;
@@ -382,6 +384,11 @@ struct bio_dgram_sctp_prinfo {
};
# endif
+typedef struct bio_buf_mem {
+ BUF_MEM *buf; /* allocated buffer */
+ BUF_MEM *readp; /* read pointer */
+} BIO_BUF_MEM;
+
/* connect BIO stuff */
# define BIO_CONN_S_BEFORE 1
# define BIO_CONN_S_GET_IP 2
--
1.9.1
_______________________________________________
openssl-bugs-mod mailing list
openssl-bugs-...@openssl.org
https://mta.openssl.org/mailman/listinfo/openssl-bugs-mod
_______________________________________________
openssl-dev mailing list
To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-dev