diff options
author | Matt Caswell <matt@openssl.org> | 2016-10-20 16:18:39 +0200 |
---|---|---|
committer | Matt Caswell <matt@openssl.org> | 2016-10-28 10:48:54 +0200 |
commit | 3befffa39dbaf2688d823fcf2bdfc07d2487be48 (patch) | |
tree | 6fcba25e3d78c48e8c00966a8085b83472395ae1 /crypto/bio | |
parent | Create BIO_read_ex() which handles size_t arguments (diff) | |
download | openssl-3befffa39dbaf2688d823fcf2bdfc07d2487be48.tar.xz openssl-3befffa39dbaf2688d823fcf2bdfc07d2487be48.zip |
Create BIO_write_ex() which handles size_t arguments
Also extend BIO_METHOD to be able to supply an implementation for the new
BIO_write_ex function.
Reviewed-by: Richard Levitte <levitte@openssl.org>
Diffstat (limited to 'crypto/bio')
-rw-r--r-- | crypto/bio/bf_buff.c | 2 | ||||
-rw-r--r-- | crypto/bio/bf_lbuf.c | 2 | ||||
-rw-r--r-- | crypto/bio/bf_nbio.c | 2 | ||||
-rw-r--r-- | crypto/bio/bf_null.c | 2 | ||||
-rw-r--r-- | crypto/bio/bio_err.c | 1 | ||||
-rw-r--r-- | crypto/bio/bio_lib.c | 49 | ||||
-rw-r--r-- | crypto/bio/bio_meth.c | 36 | ||||
-rw-r--r-- | crypto/bio/bss_acpt.c | 2 | ||||
-rw-r--r-- | crypto/bio/bss_bio.c | 2 | ||||
-rw-r--r-- | crypto/bio/bss_conn.c | 2 | ||||
-rw-r--r-- | crypto/bio/bss_dgram.c | 2 | ||||
-rw-r--r-- | crypto/bio/bss_fd.c | 2 | ||||
-rw-r--r-- | crypto/bio/bss_file.c | 2 | ||||
-rw-r--r-- | crypto/bio/bss_log.c | 2 | ||||
-rw-r--r-- | crypto/bio/bss_mem.c | 4 | ||||
-rw-r--r-- | crypto/bio/bss_null.c | 2 | ||||
-rw-r--r-- | crypto/bio/bss_sock.c | 2 |
17 files changed, 101 insertions, 15 deletions
diff --git a/crypto/bio/bf_buff.c b/crypto/bio/bf_buff.c index fc0b8fa648..7a73095488 100644 --- a/crypto/bio/bf_buff.c +++ b/crypto/bio/bf_buff.c @@ -25,6 +25,8 @@ static long buffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp); static const BIO_METHOD methods_buffer = { BIO_TYPE_BUFFER, "buffer", + /* TODO: Convert to new style write function */ + bwrite_conv, buffer_write, /* TODO: Convert to new style read function */ bread_conv, diff --git a/crypto/bio/bf_lbuf.c b/crypto/bio/bf_lbuf.c index c3b1a1ff3e..0cee526bfd 100644 --- a/crypto/bio/bf_lbuf.c +++ b/crypto/bio/bf_lbuf.c @@ -30,6 +30,8 @@ static long linebuffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp); static const BIO_METHOD methods_linebuffer = { BIO_TYPE_LINEBUFFER, "linebuffer", + /* TODO: Convert to new style write function */ + bwrite_conv, linebuffer_write, /* TODO: Convert to new style read function */ bread_conv, diff --git a/crypto/bio/bf_nbio.c b/crypto/bio/bf_nbio.c index 32698432c6..1d98a942f3 100644 --- a/crypto/bio/bf_nbio.c +++ b/crypto/bio/bf_nbio.c @@ -34,6 +34,8 @@ typedef struct nbio_test_st { static const BIO_METHOD methods_nbiof = { BIO_TYPE_NBIO_TEST, "non-blocking IO test filter", + /* TODO: Convert to new style write function */ + bwrite_conv, nbiof_write, /* TODO: Convert to new style read function */ bread_conv, diff --git a/crypto/bio/bf_null.c b/crypto/bio/bf_null.c index ed7bd98d75..ff2a04ccd8 100644 --- a/crypto/bio/bf_null.c +++ b/crypto/bio/bf_null.c @@ -27,6 +27,8 @@ static long nullf_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp); static const BIO_METHOD methods_nullf = { BIO_TYPE_NULL_FILTER, "NULL filter", + /* TODO: Convert to new style write function */ + bwrite_conv, nullf_write, /* TODO: Convert to new style read function */ bread_conv, diff --git a/crypto/bio/bio_err.c b/crypto/bio/bio_err.c index 21f7b4c0b9..68399aa54a 100644 --- a/crypto/bio/bio_err.c +++ b/crypto/bio/bio_err.c @@ -50,6 +50,7 @@ static ERR_STRING_DATA BIO_str_functs[] = { {ERR_FUNC(BIO_F_BIO_SOCK_INFO), "BIO_sock_info"}, {ERR_FUNC(BIO_F_BIO_SOCK_INIT), "BIO_sock_init"}, {ERR_FUNC(BIO_F_BIO_WRITE), "BIO_write"}, + {ERR_FUNC(BIO_F_BIO_WRITE_EX), "BIO_write_ex"}, {ERR_FUNC(BIO_F_BUFFER_CTRL), "buffer_ctrl"}, {ERR_FUNC(BIO_F_CONN_CTRL), "conn_ctrl"}, {ERR_FUNC(BIO_F_CONN_STATE), "conn_state"}, diff --git a/crypto/bio/bio_lib.c b/crypto/bio/bio_lib.c index c3633f255f..a2cbbfd7a0 100644 --- a/crypto/bio/bio_lib.c +++ b/crypto/bio/bio_lib.c @@ -284,35 +284,54 @@ int BIO_read_ex(BIO *b, void *out, size_t outl, size_t *read) int BIO_write(BIO *b, const void *in, int inl) { - int i; - long (*cb) (BIO *, int, const char *, int, long, long); + size_t written; + int ret; + + if (inl < 0) + return 0; + + ret = BIO_write_ex(b, in, (size_t)inl, &written); + + if (ret > 0) { + /* *written should always be <= inl */ + ret = (int)written; + } + + return ret; +} + +int BIO_write_ex(BIO *b, const void *in, size_t inl, size_t *written) +{ + int ret; if (b == NULL) return (0); - cb = b->callback; if ((b->method == NULL) || (b->method->bwrite == NULL)) { - BIOerr(BIO_F_BIO_WRITE, BIO_R_UNSUPPORTED_METHOD); + BIOerr(BIO_F_BIO_WRITE_EX, BIO_R_UNSUPPORTED_METHOD); return (-2); } - if ((cb != NULL) && - ((i = (int)cb(b, BIO_CB_WRITE, in, inl, 0L, 1L)) <= 0)) - return (i); + if ((b->callback != NULL || b->callback_ex != NULL) && + ((ret = bio_call_callback(b, BIO_CB_WRITE, in, inl, 0, 0L, 1L, written, + NULL)) <= 0)) + return ret; if (!b->init) { - BIOerr(BIO_F_BIO_WRITE, BIO_R_UNINITIALIZED); - return (-2); + BIOerr(BIO_F_BIO_WRITE_EX, BIO_R_UNINITIALIZED); + return -2; } - i = b->method->bwrite(b, in, inl); + ret = b->method->bwrite(b, in, inl, written); - if (i > 0) - b->num_write += (uint64_t)i; + if (ret > 0) + b->num_write += (uint64_t)*written; - if (cb != NULL) - i = (int)cb(b, BIO_CB_WRITE | BIO_CB_RETURN, in, inl, 0L, (long)i); - return (i); + if (b->callback != NULL || b->callback_ex != NULL) + ret = bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, in, inl, 0, + 0L, ret, written, NULL); + + return ret; } int BIO_puts(BIO *b, const char *in) diff --git a/crypto/bio/bio_meth.c b/crypto/bio/bio_meth.c index c10f8d01d1..7b4c340cdf 100644 --- a/crypto/bio/bio_meth.c +++ b/crypto/bio/bio_meth.c @@ -51,12 +51,47 @@ void BIO_meth_free(BIO_METHOD *biom) int (*BIO_meth_get_write(BIO_METHOD *biom)) (BIO *, const char *, int) { + return biom->bwrite_old; +} + +int (*BIO_meth_get_write_ex(BIO_METHOD *biom)) (BIO *, const char *, size_t, + size_t *) +{ return biom->bwrite; } +/* Conversion for old style bwrite to new style */ +int bwrite_conv(BIO *bio, const char *in, size_t inl, size_t *written) +{ + int ret; + + if (inl > INT_MAX) + return 0; + + ret = bio->method->bwrite_old(bio, in, (int)inl); + + if (ret <= 0) { + *written = 0; + return ret; + } + + *written = (size_t)ret; + + return 1; +} + int BIO_meth_set_write(BIO_METHOD *biom, int (*bwrite) (BIO *, const char *, int)) { + biom->bwrite_old = bwrite; + biom->bwrite = bwrite_conv; + return 1; +} + +int BIO_meth_set_write_ex(BIO_METHOD *biom, + int (*bwrite) (BIO *, const char *, size_t, size_t *)) +{ + biom->bwrite_old = NULL; biom->bwrite = bwrite; return 1; } @@ -102,6 +137,7 @@ int BIO_meth_set_read(BIO_METHOD *biom, int BIO_meth_set_read_ex(BIO_METHOD *biom, int (*bread) (BIO *, char *, size_t, size_t *)) { + biom->bread_old = NULL; biom->bread = bread; return 1; } diff --git a/crypto/bio/bss_acpt.c b/crypto/bio/bss_acpt.c index 5151ff61c6..e490fcdf73 100644 --- a/crypto/bio/bss_acpt.c +++ b/crypto/bio/bss_acpt.c @@ -54,6 +54,8 @@ static void BIO_ACCEPT_free(BIO_ACCEPT *a); static const BIO_METHOD methods_acceptp = { BIO_TYPE_ACCEPT, "socket accept", + /* TODO: Convert to new style write function */ + bwrite_conv, acpt_write, /* TODO: Convert to new style read function */ bread_conv, diff --git a/crypto/bio/bss_bio.c b/crypto/bio/bss_bio.c index ce775601f0..9fa47600c9 100644 --- a/crypto/bio/bss_bio.c +++ b/crypto/bio/bss_bio.c @@ -39,6 +39,8 @@ static void bio_destroy_pair(BIO *bio); static const BIO_METHOD methods_biop = { BIO_TYPE_BIO, "BIO pair", + /* TODO: Convert to new style write function */ + bwrite_conv, bio_write, /* TODO: Convert to new style read function */ bread_conv, diff --git a/crypto/bio/bss_conn.c b/crypto/bio/bss_conn.c index eff3f68144..ddbc8967f6 100644 --- a/crypto/bio/bss_conn.c +++ b/crypto/bio/bss_conn.c @@ -58,6 +58,8 @@ void BIO_CONNECT_free(BIO_CONNECT *a); static const BIO_METHOD methods_connectp = { BIO_TYPE_CONNECT, "socket connect", + /* TODO: Convert to new style write function */ + bwrite_conv, conn_write, /* TODO: Convert to new style read function */ bread_conv, diff --git a/crypto/bio/bss_dgram.c b/crypto/bio/bss_dgram.c index 6e5d482774..89936ff177 100644 --- a/crypto/bio/bss_dgram.c +++ b/crypto/bio/bss_dgram.c @@ -73,6 +73,8 @@ static void get_current_time(struct timeval *t); static const BIO_METHOD methods_dgramp = { BIO_TYPE_DGRAM, "datagram socket", + /* TODO: Convert to new style write function */ + bwrite_conv, dgram_write, /* TODO: Convert to new style read function */ bread_conv, diff --git a/crypto/bio/bss_fd.c b/crypto/bio/bss_fd.c index 78bbfd6f3b..0f003cd89b 100644 --- a/crypto/bio/bss_fd.c +++ b/crypto/bio/bss_fd.c @@ -59,6 +59,8 @@ int BIO_fd_should_retry(int s); static const BIO_METHOD methods_fdp = { BIO_TYPE_FD, "file descriptor", + /* TODO: Convert to new style write function */ + bwrite_conv, fd_write, /* TODO: Convert to new style read function */ bread_conv, diff --git a/crypto/bio/bss_file.c b/crypto/bio/bss_file.c index 00684ae640..87a6f396bd 100644 --- a/crypto/bio/bss_file.c +++ b/crypto/bio/bss_file.c @@ -51,6 +51,8 @@ static int file_free(BIO *data); static const BIO_METHOD methods_filep = { BIO_TYPE_FILE, "FILE pointer", + /* TODO: Convert to new style write function */ + bwrite_conv, file_write, /* TODO: Convert to new style read function */ bread_conv, diff --git a/crypto/bio/bss_log.c b/crypto/bio/bss_log.c index f262cc9d18..963371ed25 100644 --- a/crypto/bio/bss_log.c +++ b/crypto/bio/bss_log.c @@ -86,6 +86,8 @@ static void xcloselog(BIO *bp); static const BIO_METHOD methods_slg = { BIO_TYPE_MEM, "syslog", + /* TODO: Convert to new style write function */ + bwrite_conv, slg_write, NULL, NULL, diff --git a/crypto/bio/bss_mem.c b/crypto/bio/bss_mem.c index 81f7fc6d9e..38ffb1025d 100644 --- a/crypto/bio/bss_mem.c +++ b/crypto/bio/bss_mem.c @@ -26,6 +26,8 @@ static int mem_buf_sync(BIO *h); static const BIO_METHOD mem_method = { BIO_TYPE_MEM, "memory buffer", + /* TODO: Convert to new style write function */ + bwrite_conv, mem_write, /* TODO: Convert to new style read function */ bread_conv, @@ -41,6 +43,8 @@ static const BIO_METHOD mem_method = { static const BIO_METHOD secmem_method = { BIO_TYPE_MEM, "secure memory buffer", + /* TODO: Convert to new style write function */ + bwrite_conv, mem_write, /* TODO: Convert to new style read function */ bread_conv, diff --git a/crypto/bio/bss_null.c b/crypto/bio/bss_null.c index 90c9ee1618..d197a609bd 100644 --- a/crypto/bio/bss_null.c +++ b/crypto/bio/bss_null.c @@ -22,6 +22,8 @@ static int null_free(BIO *data); static const BIO_METHOD null_method = { BIO_TYPE_NULL, "NULL", + /* TODO: Convert to new style write function */ + bwrite_conv, null_write, /* TODO: Convert to new style read function */ bread_conv, diff --git a/crypto/bio/bss_sock.c b/crypto/bio/bss_sock.c index 42f0f90b65..c47b160bb0 100644 --- a/crypto/bio/bss_sock.c +++ b/crypto/bio/bss_sock.c @@ -38,6 +38,8 @@ int BIO_sock_should_retry(int s); static const BIO_METHOD methods_sockp = { BIO_TYPE_SOCKET, "socket", + /* TODO: Convert to new style write function */ + bwrite_conv, sock_write, /* TODO: Convert to new style read function */ bread_conv, |