int ssl3_write_pending(SSL *s, int type, const unsigned char *buf, size_t len,
                       size_t *written)
{
    int i;
    SSL3_BUFFER *wb = s->rlayer.wbuf;
    size_t currbuf = 0;
    size_t tmpwrit = 0;
    if ((s->rlayer.wpend_tot > len)
        || ((s->rlayer.wpend_buf != buf) &&
            !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER))
        || (s->rlayer.wpend_type != type)) {
        SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BAD_WRITE_RETRY);
        return -1;
    }
    for (;;) {
        if (SSL3_BUFFER_get_left(&wb[currbuf]) == 0
            && currbuf < s->rlayer.numwpipes - 1) {
            currbuf++;
            continue;
        }
        clear_sys_error();
        if (s->wbio != NULL) {
            s->rwstate = SSL_WRITING;
            i = BIO_write(s->wbio, (char *)
                          &(SSL3_BUFFER_get_buf(&wb[currbuf])
                            [SSL3_BUFFER_get_offset(&wb[currbuf])]),
                          (unsigned int)SSL3_BUFFER_get_left(&wb[currbuf]));
            if (i >= 0)
                tmpwrit = i;
        } else {
            SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BIO_NOT_SET);
            i = -1;
        }
        if (i > 0 && tmpwrit == SSL3_BUFFER_get_left(&wb[currbuf])) {
            SSL3_BUFFER_set_left(&wb[currbuf], 0);
            SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit);
            if (currbuf + 1 < s->rlayer.numwpipes)
                continue;
            s->rwstate = SSL_NOTHING;
            *written = s->rlayer.wpend_ret;
            return 1;
        } else if (i <= 0) {
            if (SSL_IS_DTLS(s)) {
                SSL3_BUFFER_set_left(&wb[currbuf], 0);
            }
            return -1;
        }
        SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit);
        SSL3_BUFFER_sub_left(&wb[currbuf], tmpwrit);
    }
}
['int do_ssl3_write(SSL *s, int type, const unsigned char *buf,\n                  size_t *pipelens, size_t numpipes,\n                  int create_empty_fragment, size_t *written)\n{\n    unsigned char *outbuf[SSL_MAX_PIPELINES], *plen[SSL_MAX_PIPELINES];\n    SSL3_RECORD wr[SSL_MAX_PIPELINES];\n    int i, mac_size, clear = 0;\n    size_t prefix_len = 0;\n    int eivlen;\n    size_t align = 0;\n    SSL3_BUFFER *wb;\n    SSL_SESSION *sess;\n    size_t totlen = 0;\n    size_t j;\n    for (j = 0; j < numpipes; j++)\n        totlen += pipelens[j];\n    if (RECORD_LAYER_write_pending(&s->rlayer))\n        return ssl3_write_pending(s, type, buf, totlen, written);\n    if (s->s3->alert_dispatch) {\n        i = s->method->ssl_dispatch_alert(s);\n        if (i <= 0)\n            return (i);\n    }\n    if (s->rlayer.numwpipes < numpipes)\n        if (!ssl3_setup_write_buffer(s, numpipes, 0))\n            return -1;\n    if (totlen == 0 && !create_empty_fragment)\n        return 0;\n    sess = s->session;\n    if ((sess == NULL) ||\n        (s->enc_write_ctx == NULL) || (EVP_MD_CTX_md(s->write_hash) == NULL)) {\n        clear = s->enc_write_ctx ? 0 : 1;\n        mac_size = 0;\n    } else {\n        mac_size = EVP_MD_CTX_size(s->write_hash);\n        if (mac_size < 0)\n            goto err;\n    }\n    if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done) {\n        if (s->s3->need_empty_fragments && type == SSL3_RT_APPLICATION_DATA) {\n            size_t tmppipelen = 0;\n            int ret;\n            ret = do_ssl3_write(s, type, buf, &tmppipelen, 1, 1, &prefix_len);\n            if (ret <= 0)\n                goto err;\n            if (prefix_len >\n                (SSL3_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD)) {\n                SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);\n                goto err;\n            }\n        }\n        s->s3->empty_fragment_done = 1;\n    }\n    if (create_empty_fragment) {\n        wb = &s->rlayer.wbuf[0];\n#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0\n        align = (size_t)SSL3_BUFFER_get_buf(wb) + 2 * SSL3_RT_HEADER_LENGTH;\n        align = SSL3_ALIGN_PAYLOAD - 1 - ((align - 1) % SSL3_ALIGN_PAYLOAD);\n#endif\n        outbuf[0] = SSL3_BUFFER_get_buf(wb) + align;\n        SSL3_BUFFER_set_offset(wb, align);\n    } else if (prefix_len) {\n        wb = &s->rlayer.wbuf[0];\n        outbuf[0] = SSL3_BUFFER_get_buf(wb) + SSL3_BUFFER_get_offset(wb)\n            + prefix_len;\n    } else {\n        for (j = 0; j < numpipes; j++) {\n            wb = &s->rlayer.wbuf[j];\n#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0\n            align = (size_t)SSL3_BUFFER_get_buf(wb) + SSL3_RT_HEADER_LENGTH;\n            align = SSL3_ALIGN_PAYLOAD - 1 - ((align - 1) % SSL3_ALIGN_PAYLOAD);\n#endif\n            outbuf[j] = SSL3_BUFFER_get_buf(wb) + align;\n            SSL3_BUFFER_set_offset(wb, align);\n        }\n    }\n    if (s->enc_write_ctx && SSL_USE_EXPLICIT_IV(s)) {\n        int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx);\n        if (mode == EVP_CIPH_CBC_MODE) {\n            eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx);\n            if (eivlen <= 1)\n                eivlen = 0;\n        }\n        else if (mode == EVP_CIPH_GCM_MODE)\n            eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;\n        else if (mode == EVP_CIPH_CCM_MODE)\n            eivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;\n        else\n            eivlen = 0;\n    } else\n        eivlen = 0;\n    totlen = 0;\n    memset(wr, 0, sizeof wr);\n    for (j = 0; j < numpipes; j++) {\n        *(outbuf[j]++) = type & 0xff;\n        SSL3_RECORD_set_type(&wr[j], type);\n        *(outbuf[j]++) = (s->version >> 8);\n        if (SSL_get_state(s) == TLS_ST_CW_CLNT_HELLO\n            && !s->renegotiate && TLS1_get_version(s) > TLS1_VERSION)\n            *(outbuf[j]++) = 0x1;\n        else\n            *(outbuf[j]++) = s->version & 0xff;\n        plen[j] = outbuf[j];\n        outbuf[j] += 2;\n        SSL3_RECORD_set_data(&wr[j], outbuf[j] + eivlen);\n        SSL3_RECORD_set_length(&wr[j], pipelens[j]);\n        SSL3_RECORD_set_input(&wr[j], (unsigned char *)&buf[totlen]);\n        totlen += pipelens[j];\n        if (s->compress != NULL) {\n            if (!ssl3_do_compress(s, &wr[j])) {\n                SSLerr(SSL_F_DO_SSL3_WRITE, SSL_R_COMPRESSION_FAILURE);\n                goto err;\n            }\n        } else {\n            memcpy(wr[j].data, wr[j].input, wr[j].length);\n            SSL3_RECORD_reset_input(&wr[j]);\n        }\n        if (!SSL_USE_ETM(s) && mac_size != 0) {\n            if (!s->method->ssl3_enc->mac(s, &wr[j],\n                                          &(outbuf[j][wr[j].length + eivlen]),\n                                          1))\n                goto err;\n            SSL3_RECORD_add_length(&wr[j], mac_size);\n        }\n        SSL3_RECORD_set_data(&wr[j], outbuf[j]);\n        SSL3_RECORD_reset_input(&wr[j]);\n        if (eivlen) {\n            SSL3_RECORD_add_length(&wr[j], eivlen);\n        }\n    }\n    if (s->method->ssl3_enc->enc(s, wr, numpipes, 1) < 1)\n        goto err;\n    for (j = 0; j < numpipes; j++) {\n        if (SSL_USE_ETM(s) && mac_size != 0) {\n            if (!s->method->ssl3_enc->mac(s, &wr[j],\n                                          outbuf[j] + wr[j].length, 1))\n                goto err;\n            SSL3_RECORD_add_length(&wr[j], mac_size);\n        }\n        s2n(SSL3_RECORD_get_length(&wr[j]), plen[j]);\n        if (s->msg_callback)\n            s->msg_callback(1, 0, SSL3_RT_HEADER, plen[j] - 5, 5, s,\n                            s->msg_callback_arg);\n        SSL3_RECORD_set_type(&wr[j], type);\n        SSL3_RECORD_add_length(&wr[j], SSL3_RT_HEADER_LENGTH);\n        if (create_empty_fragment) {\n            if (j > 0) {\n                SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);\n                goto err;\n            }\n            *written = SSL3_RECORD_get_length(wr);\n            return 1;\n        }\n        SSL3_BUFFER_set_left(&s->rlayer.wbuf[j],\n                             prefix_len + SSL3_RECORD_get_length(&wr[j]));\n    }\n    s->rlayer.wpend_tot = totlen;\n    s->rlayer.wpend_buf = buf;\n    s->rlayer.wpend_type = type;\n    s->rlayer.wpend_ret = totlen;\n    return ssl3_write_pending(s, type, buf, totlen, written);\n err:\n    return -1;\n}', 'int ssl3_write_pending(SSL *s, int type, const unsigned char *buf, size_t len,\n                       size_t *written)\n{\n    int i;\n    SSL3_BUFFER *wb = s->rlayer.wbuf;\n    size_t currbuf = 0;\n    size_t tmpwrit = 0;\n    if ((s->rlayer.wpend_tot > len)\n        || ((s->rlayer.wpend_buf != buf) &&\n            !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER))\n        || (s->rlayer.wpend_type != type)) {\n        SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BAD_WRITE_RETRY);\n        return -1;\n    }\n    for (;;) {\n        if (SSL3_BUFFER_get_left(&wb[currbuf]) == 0\n            && currbuf < s->rlayer.numwpipes - 1) {\n            currbuf++;\n            continue;\n        }\n        clear_sys_error();\n        if (s->wbio != NULL) {\n            s->rwstate = SSL_WRITING;\n            i = BIO_write(s->wbio, (char *)\n                          &(SSL3_BUFFER_get_buf(&wb[currbuf])\n                            [SSL3_BUFFER_get_offset(&wb[currbuf])]),\n                          (unsigned int)SSL3_BUFFER_get_left(&wb[currbuf]));\n            if (i >= 0)\n                tmpwrit = i;\n        } else {\n            SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BIO_NOT_SET);\n            i = -1;\n        }\n        if (i > 0 && tmpwrit == SSL3_BUFFER_get_left(&wb[currbuf])) {\n            SSL3_BUFFER_set_left(&wb[currbuf], 0);\n            SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit);\n            if (currbuf + 1 < s->rlayer.numwpipes)\n                continue;\n            s->rwstate = SSL_NOTHING;\n            *written = s->rlayer.wpend_ret;\n            return 1;\n        } else if (i <= 0) {\n            if (SSL_IS_DTLS(s)) {\n                SSL3_BUFFER_set_left(&wb[currbuf], 0);\n            }\n            return -1;\n        }\n        SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit);\n        SSL3_BUFFER_sub_left(&wb[currbuf], tmpwrit);\n    }\n}']