FreeRTOS-Plus/ThirdParty/WolfSSL-FIPS-Ready/src/bio.c
/* Writes data to a WOLFSSL_BIO structure
 *
 * bio  structure to write to
 * data holds the data to be written
 * len  length of data buffer
 *
 * returns the amount written in bytes on success
 */
Writes data to a WOLFSSL_BIO structure
bio  structure to write to
data holds the data to be written
len  length of data buffer

returns the amount written in bytes on success
Writes data to a WOLFSSL_BIO structure bio structure to write to data holds the data to be written len length of data buffer returns the amount written in bytes on success
int wolfSSL_BIO_write(WOLFSSL_BIO* bio, const void* data, int len)
{
    int  ret = 0;
    int  retB64 = 0;
    WOLFSSL_BIO* front = bio;
    void*  frmt   = NULL;
    word32 frmtSz = 0;
    WOLFSSL_ENTER("wolfSSL_BIO_write");
    if (front != NULL && front->infoCb != NULL) {
        ret = (int)front->infoCb(front, WOLFSSL_BIO_CB_WRITE,
                (const char*)data, len, 0, 1);
        if (ret <= 0) {
            return ret;
        }
    }
    while (bio != NULL && ret >= 0) {
        if (bio->method && bio->method->writeCb) {
            ret = bio->method->writeCb(bio, (const char*)data, len);
        }
        if (bio->type == WOLFSSL_BIO_BASE64) {
#if defined(WOLFSSL_BASE64_ENCODE)
            word32 sz = 0;
            if (bio->flags & WOLFSSL_BIO_FLAG_BASE64_NO_NL) {
                if (Base64_Encode_NoNl((const byte*)data, len, NULL,
                            &sz) != LENGTH_ONLY_E) {
                    WOLFSSL_MSG("Error with base 64 get length");
                    ret = SSL_FATAL_ERROR;
                }
            }
            else {
                if (Base64_Encode((const byte*)data, len, NULL, &sz) !=
                    LENGTH_ONLY_E) {
                    WOLFSSL_MSG("Error with base 64 get length");
                    ret = SSL_FATAL_ERROR;
                }
            }
            if (frmt == NULL && sz > 0 && ret != SSL_FATAL_ERROR) {
                frmt = (void*)XMALLOC(sz, front->heap,
                        DYNAMIC_TYPE_TMP_BUFFER);
                if (frmt == NULL) {
                    WOLFSSL_MSG("Memory error");
                    ret = SSL_FATAL_ERROR;
                }
                frmtSz = sz;
            }
            else if (sz > frmtSz) {
                frmt = (void*)XREALLOC(frmt, sz, front->heap,
                        DYNAMIC_TYPE_TMP_BUFFER);
                if (frmt == NULL) {
                    WOLFSSL_MSG("Memory error");
                    ret = SSL_FATAL_ERROR;
                }
                data = frmt;
                len  = frmtSz;
                frmtSz = sz;
            }
#endif 
            if (ret >= 0) {
                retB64 = wolfSSL_BIO_BASE64_write(bio, data, (word32)len,
                         (byte*)frmt, &frmtSz);
                data = frmt;
                len  = frmtSz;
            }
        }
        if (bio && bio->type == WOLFSSL_BIO_BIO) {
            ret = wolfSSL_BIO_BIO_write(bio, data, len);
        }
        if (bio && bio->type == WOLFSSL_BIO_MEMORY) {
            ret = wolfSSL_BIO_MEMORY_write(bio, data, len);
        }
    #ifndef NO_FILESYSTEM
        if (bio && bio->type == WOLFSSL_BIO_FILE) {
            ret = (int)XFWRITE(data, 1, len, (XFILE)bio->ptr);
        }
    #endif
    #ifndef WOLFCRYPT_ONLY
        if (bio && bio->type == WOLFSSL_BIO_SSL) {
            if (front->eof) {
                ret = SSL_FATAL_ERROR;
            }
            else {
                ret = wolfSSL_BIO_SSL_write(bio, data, len, front);
            }
        }
        if (bio && bio->type == WOLFSSL_BIO_MD) {
            if (bio->next != NULL) { 
                ret = wolfSSL_BIO_MD_write(bio, data, len);
            }
        }
    #endif 
        bio = bio->next;
    }
    if (front != NULL && front->infoCb != NULL) {
        ret = (int)front->infoCb(front,
                                 WOLFSSL_BIO_CB_WRITE | WOLFSSL_BIO_CB_RETURN,
                                 (const char*)data, 0, 0, ret);
    }
    if (frmt != NULL) {
        XFREE(frmt, front->heap, DYNAMIC_TYPE_TMP_BUFFER);
    }
    if (retB64 != 0)
        return retB64;
    else
        return ret;
}
int wolfSSL_BIO_write ( WOLFSSL_BIO * bio , const void * data , int len ) { int ret = 0 ; int retB64 = 0 ; WOLFSSL_BIO * front = bio ; void * frmt = NULL ; word32 frmtSz = 0 ; WOLFSSL_ENTER ( " " ) ; if ( front != NULL && front -> infoCb != NULL ) { ret = ( int ) front -> infoCb ( front , WOLFSSL_BIO_CB_WRITE , ( const char * ) data , len , 0 , 1 ) ; if ( ret <= 0 ) { return ret ; } } while ( bio != NULL && ret >= 0 ) { if ( bio -> method && bio -> method -> writeCb ) { ret = bio -> method -> writeCb ( bio , ( const char * ) data , len ) ; } if ( bio -> type == WOLFSSL_BIO_BASE64 ) { #if defined ( WOLFSSL_BASE64_ENCODE ) word32 sz = 0 ; if ( bio -> flags & WOLFSSL_BIO_FLAG_BASE64_NO_NL ) { if ( Base64_Encode_NoNl ( ( const byte * ) data , len , NULL , & sz ) != LENGTH_ONLY_E ) { WOLFSSL_MSG ( " " ) ; ret = SSL_FATAL_ERROR ; } } else { if ( Base64_Encode ( ( const byte * ) data , len , NULL , & sz ) != LENGTH_ONLY_E ) { WOLFSSL_MSG ( " " ) ; ret = SSL_FATAL_ERROR ; } } if ( frmt == NULL && sz > 0 && ret != SSL_FATAL_ERROR ) { frmt = ( void * ) XMALLOC ( sz , front -> heap , DYNAMIC_TYPE_TMP_BUFFER ) ; if ( frmt == NULL ) { WOLFSSL_MSG ( " " ) ; ret = SSL_FATAL_ERROR ; } frmtSz = sz ; } else if ( sz > frmtSz ) { frmt = ( void * ) XREALLOC ( frmt , sz , front -> heap , DYNAMIC_TYPE_TMP_BUFFER ) ; if ( frmt == NULL ) { WOLFSSL_MSG ( " " ) ; ret = SSL_FATAL_ERROR ; } data = frmt ; len = frmtSz ; frmtSz = sz ; } #endif if ( ret >= 0 ) { retB64 = wolfSSL_BIO_BASE64_write ( bio , data , ( word32 ) len , ( byte * ) frmt , & frmtSz ) ; data = frmt ; len = frmtSz ; } } if ( bio && bio -> type == WOLFSSL_BIO_BIO ) { ret = wolfSSL_BIO_BIO_write ( bio , data , len ) ; } if ( bio && bio -> type == WOLFSSL_BIO_MEMORY ) { ret = wolfSSL_BIO_MEMORY_write ( bio , data , len ) ; } #ifndef NO_FILESYSTEM if ( bio && bio -> type == WOLFSSL_BIO_FILE ) { ret = ( int ) XFWRITE ( data , 1 , len , ( XFILE ) bio -> ptr ) ; } #endif #ifndef WOLFCRYPT_ONLY if ( bio && bio -> type == WOLFSSL_BIO_SSL ) { if ( front -> eof ) { ret = SSL_FATAL_ERROR ; } else { ret = wolfSSL_BIO_SSL_write ( bio , data , len , front ) ; } } if ( bio && bio -> type == WOLFSSL_BIO_MD ) { if ( bio -> next != NULL ) { ret = wolfSSL_BIO_MD_write ( bio , data , len ) ; } } #endif bio = bio -> next ; } if ( front != NULL && front -> infoCb != NULL ) { ret = ( int ) front -> infoCb ( front , WOLFSSL_BIO_CB_WRITE | WOLFSSL_BIO_CB_RETURN , ( const char * ) data , 0 , 0 , ret ) ; } if ( frmt != NULL ) { XFREE ( frmt , front -> heap , DYNAMIC_TYPE_TMP_BUFFER ) ; } if ( retB64 != 0 ) return retB64 ; else return ret ; }
Writes data to a WOLFSSL_BIO structure
 bio  structure to write to
 data holds the data to be written
 len  length of data buffer
Writes data to a WOLFSSL_BIO structure bio structure to write to data holds the data to be written len length of data buffer
/* info cb, abort if user returns <= 0*/ /* check for custom write */ /* check for formatting */ /* since frmt already existed then data should point to knew
                   formatted buffer */ /* defined(WOLFSSL_BASE64_ENCODE) */ /* change so that data is formatted buffer */ /* write bios */ /* already got eof, again is error */ /* data passing through MD BIO */ /* WOLFCRYPT_ONLY */ /* advance to the next bio in list */ /* info cb, user can override return value */