static SigV4Status_t copyHeaderStringToCanonicalBuffer()

in source/sigv4.c [1523:1595]


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

        assert( ( pData != NULL ) && ( dataLen > 0 ) );
        assert( canonicalRequest != NULL );
        assert( canonicalRequest->pBufCur != NULL );

        buffRemaining = canonicalRequest->bufRemaining;
        pCurrBufLoc = canonicalRequest->pBufCur;

        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' )
                {
                    *pCurrBufLoc = ( pData[ index ] );
                }
                else
                {
                    *pCurrBufLoc = lowercaseCharacter( pData[ index ] );
                }

                pCurrBufLoc++;
                numOfBytesCopied++;
                buffRemaining--;
            }
        }

        /* Check that data to be copied does not contain all spaces only. */
        if( ( status == SigV4Success ) && ( numOfBytesCopied == 0 ) )
        {
            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 );
            *pCurrBufLoc = separator;
            pCurrBufLoc++;
            canonicalRequest->pBufCur = pCurrBufLoc;
            canonicalRequest->bufRemaining = ( buffRemaining - 1U );
        }

        return status;
    }