static SigV4Status_t copyHeaderStringToCanonicalBuffer()

in source/sigv4.c [1430:1501]


    static SigV4Status_t copyHeaderStringToCanonicalBuffer( const char * pData,
                                                            size_t dataLen,
                                                            uint32_t flags,
                                                            char separator,
                                                            CanonicalContext_t * pCanonicalRequest )
    {
        SigV4Status_t status = SigV4Success;
        size_t index = 0;
        size_t numOfBytesCopied = 0;
        size_t buffRemaining;
        size_t uxCurrBufIndex;

        assert( ( pData != NULL ) && ( dataLen > 0 ) );
        assert( pCanonicalRequest != NULL );

        buffRemaining = pCanonicalRequest->bufRemaining;
        uxCurrBufIndex = pCanonicalRequest->uxCursorIndex;

        for( index = 0; index < dataLen; index++ )
        {
            /* If the header field is not in canonical form already, we need to check
             * whether this character represents a trimmable space. */
            if( !FLAG_IS_SET( flags, SIGV4_HTTP_HEADERS_ARE_CANONICAL_FLAG ) &&
                isTrimmableSpace( pData, index, dataLen, numOfBytesCopied ) )
            {
                /* Cannot copy trimmable space into canonical request buffer. */
            }
            /* Remaining buffer space should at least accommodate the character to copy and the trailing separator character. */
            else if( buffRemaining <= 1U )
            {
                status = SigV4InsufficientMemory;
                break;
            }
            else
            {
                /* Lowercase header key only. '\n' character marks the end of the value and header value
                 * does not need to be lowercased. */
                if( separator == '\n' )
                {
                    ( ( char * ) pCanonicalRequest->pBufProcessing )[ uxCurrBufIndex ] = pData[ index ];
                }
                else
                {
                    ( ( char * ) pCanonicalRequest->pBufProcessing )[ uxCurrBufIndex ] = lowercaseCharacter( pData[ index ] );
                }

                uxCurrBufIndex++;
                numOfBytesCopied++;
                buffRemaining--;
            }
        }

        /* Check that data to be copied does not contain all spaces only. */
        if( ( status == SigV4Success ) && ( numOfBytesCopied == 0U ) )
        {
            status = SigV4InvalidParameter;
        }

        /* Add the ending separating character passed to the function.
         * Note: Space for the separator character is accounted for while copying
         * header field data to canonical request buffer. */
        if( status == SigV4Success )
        {
            assert( buffRemaining >= 1 );
            ( ( char * ) ( pCanonicalRequest->pBufProcessing ) )[ uxCurrBufIndex ] = separator;
            uxCurrBufIndex++;
            pCanonicalRequest->uxCursorIndex = uxCurrBufIndex;
            pCanonicalRequest->bufRemaining = ( buffRemaining - 1U );
        }

        return status;
    }