[openssl-commits] [openssl] master update
Rich Salz
rsalz at openssl.org
Sat Apr 2 20:57:32 UTC 2016
The branch master has been updated
via d3e6d6bcdfb725a57f94330367e7b752036c7a2c (commit)
via fc9755ee0d9590510df48b5fbac7cea0b6792b1f (commit)
via 9fe9d0461ea4bcc42cd75a30f013fa61b5407b93 (commit)
from 6b888643105e37d340d509b98023b4779653c8a7 (commit)
- Log -----------------------------------------------------------------
commit d3e6d6bcdfb725a57f94330367e7b752036c7a2c
Author: Kirill Marinushkin <k.marinushkin at gmail.com>
Date: Wed Mar 30 18:39:42 2016 +0200
moved structure bio_buf_mem_st from headers to bss_mem.c
Reviewed-by: Richard Levitte <levitte at openssl.org>
Reviewed-by: Rich Salz <rsalz at openssl.org>
commit fc9755ee0d9590510df48b5fbac7cea0b6792b1f
Author: Kirill Marinushkin <k.marinushkin at gmail.com>
Date: Wed Mar 16 22:05:59 2016 +0100
sizeof() updated to cover coding style
Reviewed-by: Richard Levitte <levitte at openssl.org>
Reviewed-by: Rich Salz <rsalz at openssl.org>
commit 9fe9d0461ea4bcc42cd75a30f013fa61b5407b93
Author: Kirill Marinushkin <k.marinushkin at gmail.com>
Date: Sun Mar 13 13:20:52 2016 +0100
Optimized BIO mem read - without reallocation
Currently on every BIO mem read operation the remaining data is reallocated.
This commit solves the issue.
BIO mem structure includes additional pointer to the read position.
On every read the pointer moves instead of reallocating the memory for the remaining data.
Reallocation accures before write and some ioctl operations, if the read pointer doesn't point on the beginning of the buffer.
Also the flag is added to rewind the read pointer without losing the data.
Reviewed-by: Richard Levitte <levitte at openssl.org>
Reviewed-by: Rich Salz <rsalz at openssl.org>
-----------------------------------------------------------------------
Summary of changes:
crypto/bio/bss_mem.c | 111 ++++++++++++++++++++++++++++++++++-------------
doc/crypto/BIO_s_mem.pod | 13 ++----
include/openssl/bio.h | 6 ++-
3 files changed, 89 insertions(+), 41 deletions(-)
diff --git a/crypto/bio/bss_mem.c b/crypto/bio/bss_mem.c
index 460e070..46bd034 100644
--- a/crypto/bio/bss_mem.c
+++ b/crypto/bio/bss_mem.c
@@ -68,6 +68,9 @@ 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 const BIO_METHOD mem_method = {
BIO_TYPE_MEM,
"memory buffer",
@@ -80,6 +83,7 @@ static const BIO_METHOD mem_method = {
mem_free,
NULL,
};
+
static const BIO_METHOD secmem_method = {
BIO_TYPE_MEM,
"secure memory buffer",
@@ -93,6 +97,12 @@ static const BIO_METHOD secmem_method = {
NULL,
};
+/* BIO memory stores buffer and read pointer */
+typedef struct bio_buf_mem_st {
+ struct buf_mem_st *buf; /* allocated buffer */
+ struct buf_mem_st *readp; /* read pointer */
+} BIO_BUF_MEM;
+
/*
* bio->num is used to hold the value to return on 'empty', if it is 0,
* should_retry is not set
@@ -112,6 +122,7 @@ BIO *BIO_new_mem_buf(const void *buf, int len)
{
BIO *ret;
BUF_MEM *b;
+ BIO_BUF_MEM *bb;
size_t sz;
if (buf == NULL) {
@@ -121,11 +132,13 @@ BIO *BIO_new_mem_buf(const 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;
/* Cast away const and trust in the MEM_RDONLY flag. */
b->data = (void *)buf;
b->length = sz;
b->max = sz;
+ *bb->readp = *bb->buf;
ret->flags |= BIO_FLAGS_MEM_RDONLY;
/* Since this is static data retrying wont help */
ret->num = 0;
@@ -134,14 +147,19 @@ BIO *BIO_new_mem_buf(const void *buf, int len)
static int mem_init(BIO *bi, unsigned long flags)
{
- BUF_MEM *b;
+ BIO_BUF_MEM *bb = OPENSSL_zalloc(sizeof(*bb));
- if ((b = BUF_MEM_new_ex(flags)) == NULL)
+ if (bb == NULL)
return(0);
+ if ((bb->buf = BUF_MEM_new_ex(flags)) == NULL)
+ return(0);
+ if ((bb->readp = OPENSSL_zalloc(sizeof(*bb->readp))) == NULL)
+ return(0);
+ *bb->readp = *bb->buf;
bi->shutdown = 1;
bi->init = 1;
bi->num = -1;
- bi->ptr = (char *)b;
+ bi->ptr = (char *)bb;
return(1);
}
@@ -157,37 +175,63 @@ 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 = (BIO_BUF_MEM *)a->ptr;
+
+ if(bb != NULL) {
+ b = bb->buf;
+ if (a->flags & BIO_FLAGS_MEM_RDONLY)
+ b->data = NULL;
+ BUF_MEM_free(b);
+ if(free_all) {
+ OPENSSL_free(bb->readp);
+ OPENSSL_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, bbm->readp->data, 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 = (BIO_BUF_MEM *)b->ptr;
+ BUF_MEM *bm = bbm->readp;
- bm = (BUF_MEM *)b->ptr;
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,24 +244,23 @@ static int mem_write(BIO *b, const char *in, int inl)
{
int ret = -1;
int blen;
- BUF_MEM *bm;
+ BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
- bm = (BUF_MEM *)b->ptr;
if (in == NULL) {
BIOerr(BIO_F_MEM_WRITE, BIO_R_NULL_PARAMETER);
goto end;
}
-
if (b->flags & BIO_FLAGS_MEM_RDONLY) {
BIOerr(BIO_F_MEM_WRITE, BIO_R_WRITE_TO_READ_ONLY_BIO);
goto end;
}
-
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);
+ *bbm->readp = *bbm->buf;
ret = inl;
end:
return (ret);
@@ -227,29 +270,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;
}
+ *bbm->readp = *bbm->buf;
}
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 +303,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;
+ *bbm->readp = *bbm->buf;
+ 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;
}
@@ -273,11 +323,11 @@ static long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
case BIO_CTRL_SET_CLOSE:
b->shutdown = (int)num;
break;
-
case BIO_CTRL_WPENDING:
ret = 0L;
break;
case BIO_CTRL_PENDING:
+ bm = bbm->readp;
ret = (long)bm->length;
break;
case BIO_CTRL_DUP:
@@ -298,7 +348,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;
diff --git a/doc/crypto/BIO_s_mem.pod b/doc/crypto/BIO_s_mem.pod
index 03e291d..84abb29 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 a32c2d3..e0c6380 100644
--- a/include/openssl/bio.h
+++ b/include/openssl/bio.h
@@ -216,10 +216,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 union bio_addr_st BIO_ADDR;
typedef struct bio_addrinfo_st BIO_ADDRINFO;
More information about the openssl-commits
mailing list