static SigV4Status_t generateCanonicalURI()

in source/sigv4.c [1398:1466]


    static SigV4Status_t generateCanonicalURI( const char * pUri,
                                               size_t uriLen,
                                               bool encodeTwice,
                                               CanonicalContext_t * pCanonicalRequest )
    {
        SigV4Status_t returnStatus = SigV4Success;
        char * pBufLoc = NULL;
        size_t encodedLen = 0U;

        assert( pUri != NULL );
        assert( pCanonicalRequest != NULL );
        assert( pCanonicalRequest->pBufCur != NULL );

        pBufLoc = pCanonicalRequest->pBufCur;
        encodedLen = pCanonicalRequest->bufRemaining;

        /* If the canonical URI needs to be encoded twice, then we encode once here,
         * and again at the end of the buffer. Afterwards, the second encode is copied
         * to overwrite the first one. */
        returnStatus = encodeURI( pUri, uriLen, pBufLoc, &encodedLen, false, false );

        if( returnStatus == SigV4Success )
        {
            if( encodeTwice )
            {
                size_t doubleEncodedLen = pCanonicalRequest->bufRemaining - encodedLen;

                /* Note that the result of encoding the URI a second time must be
                 * written to a different position in the buffer. It should not be done
                 * at an overlapping position of the single-encoded URI. Once written,
                 * the double-encoded URI is moved to the starting location of the single-encoded URI. */
                returnStatus = encodeURI( pBufLoc,
                                          encodedLen,
                                          pBufLoc + encodedLen,
                                          &doubleEncodedLen,
                                          false,
                                          false );

                if( returnStatus == SigV4Success )
                {
                    ( void ) memmove( pBufLoc, pBufLoc + encodedLen, doubleEncodedLen );
                    pBufLoc += doubleEncodedLen;
                    pCanonicalRequest->bufRemaining -= doubleEncodedLen;
                }
            }
            else
            {
                pBufLoc += encodedLen;
                pCanonicalRequest->bufRemaining -= encodedLen;
            }
        }

        if( returnStatus == SigV4Success )
        {
            if( pCanonicalRequest->bufRemaining < 1U )
            {
                returnStatus = SigV4InsufficientMemory;
                LOG_INSUFFICIENT_MEMORY_ERROR( "write newline character after canonical URI", 1U );
            }
            else
            {
                *pBufLoc = LINEFEED_CHAR;
                pCanonicalRequest->pBufCur = pBufLoc + 1U;
                pCanonicalRequest->bufRemaining -= 1U;
            }
        }

        return returnStatus;
    }