mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2025-01-12 22:05:34 +00:00
Move MPS trace macros to MBEDTLS_MPS_ namespace
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
This commit is contained in:
parent
c518c3b7bb
commit
984fbded58
|
@ -26,6 +26,8 @@
|
|||
#ifndef MBEDTLS_MPS_COMMON_H
|
||||
#define MBEDTLS_MPS_COMMON_H
|
||||
|
||||
#include "mps_error.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/**
|
||||
|
@ -90,7 +92,7 @@
|
|||
#define MBEDTLS_MPS_ENABLE_ASSERTIONS
|
||||
|
||||
/*! This flag controls whether tracing for MPS should be enabled. */
|
||||
//#define MBEDTLS_MPS_TRACE
|
||||
//#define MBEDTLS_MPS_ENABLE_TRACE
|
||||
|
||||
#if defined(MBEDTLS_MPS_STATE_VALIDATION)
|
||||
|
||||
|
@ -99,8 +101,8 @@
|
|||
{ \
|
||||
if( !(cond) ) \
|
||||
{ \
|
||||
TRACE( trace_error, string ); \
|
||||
RETURN( MBEDTLS_ERR_MPS_OPERATION_UNEXPECTED ); \
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_error, string ); \
|
||||
MBEDTLS_MPS_TRACE_RETURN( MBEDTLS_ERR_MPS_OPERATION_UNEXPECTED ); \
|
||||
} \
|
||||
} while( 0 )
|
||||
|
||||
|
@ -121,8 +123,8 @@
|
|||
{ \
|
||||
if( !(cond) ) \
|
||||
{ \
|
||||
TRACE( trace_error, string ); \
|
||||
RETURN( MBEDTLS_ERR_MPS_INTERNAL_ERROR ); \
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_error, string ); \
|
||||
MBEDTLS_MPS_TRACE_RETURN( MBEDTLS_ERR_MPS_INTERNAL_ERROR ); \
|
||||
} \
|
||||
} while( 0 )
|
||||
|
||||
|
|
|
@ -30,7 +30,7 @@
|
|||
/* TODO: The error code allocation needs to be revisited:
|
||||
*
|
||||
* - Should we make (some of) the MPS Reader error codes public?
|
||||
* If so, we need to adjust MBEDTLS_READER_MAKE_ERROR() to hit
|
||||
* If so, we need to adjust MBEDTLS_MPS_READER_MAKE_ERROR() to hit
|
||||
* a gap in the Mbed TLS public error space.
|
||||
* If not, we have to make sure we don't forward those errors
|
||||
* at the level of the public API -- no risk at the moment as
|
||||
|
@ -57,6 +57,7 @@
|
|||
|
||||
|
||||
#define MBEDTLS_ERR_MPS_OPERATION_UNEXPECTED MBEDTLS_MPS_MAKE_ERROR( 0x1 )
|
||||
#define MBEDTLS_ERR_MPS_INTERNAL_ERROR MBEDTLS_MPS_MAKE_ERROR( 0x2 )
|
||||
|
||||
/* \} name SECTION: MPS general error codes */
|
||||
|
||||
|
|
|
@ -30,9 +30,9 @@
|
|||
#define inline __inline
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_MPS_TRACE)
|
||||
static int trace_id = TRACE_BIT_READER;
|
||||
#endif /* MBEDTLS_MPS_TRACE */
|
||||
#if defined(MBEDTLS_MPS_ENABLE_TRACE)
|
||||
static int mbedtls_mps_trace_id = MBEDTLS_MPS_TRACE_BIT_READER;
|
||||
#endif /* MBEDTLS_MPS_ENABLE_TRACE */
|
||||
|
||||
/*
|
||||
* GENERAL NOTE ON CODING STYLE
|
||||
|
@ -92,18 +92,18 @@ int mbedtls_reader_init( mbedtls_reader *rd,
|
|||
unsigned char *acc,
|
||||
mbedtls_mps_size_t acc_len )
|
||||
{
|
||||
TRACE_INIT( "reader_init, acc len %u", (unsigned) acc_len );
|
||||
MBEDTLS_MPS_TRACE_INIT( "reader_init, acc len %u", (unsigned) acc_len );
|
||||
mps_reader_zero( rd );
|
||||
rd->acc = acc;
|
||||
rd->acc_len = acc_len;
|
||||
RETURN( 0 );
|
||||
MBEDTLS_MPS_TRACE_RETURN( 0 );
|
||||
}
|
||||
|
||||
int mbedtls_reader_free( mbedtls_reader *rd )
|
||||
{
|
||||
TRACE_INIT( "reader_free" );
|
||||
MBEDTLS_MPS_TRACE_INIT( "reader_free" );
|
||||
mps_reader_zero( rd );
|
||||
RETURN( 0 );
|
||||
MBEDTLS_MPS_TRACE_RETURN( 0 );
|
||||
}
|
||||
|
||||
int mbedtls_reader_feed( mbedtls_reader *rd,
|
||||
|
@ -112,11 +112,11 @@ int mbedtls_reader_feed( mbedtls_reader *rd,
|
|||
{
|
||||
unsigned char *acc;
|
||||
mbedtls_mps_size_t copy_to_acc;
|
||||
TRACE_INIT( "reader_feed, frag %p, len %u",
|
||||
MBEDTLS_MPS_TRACE_INIT( "reader_feed, frag %p, len %u",
|
||||
(void*) new_frag, (unsigned) new_frag_len );
|
||||
|
||||
if( new_frag == NULL )
|
||||
RETURN( MBEDTLS_ERR_MPS_READER_INVALID_ARG );
|
||||
MBEDTLS_MPS_TRACE_RETURN( MBEDTLS_ERR_MPS_READER_INVALID_ARG );
|
||||
|
||||
MBEDTLS_MPS_STATE_VALIDATE_RAW( rd->frag == NULL,
|
||||
"mbedtls_reader_feed() requires reader to be in producing mode" );
|
||||
|
@ -138,7 +138,8 @@ int mbedtls_reader_feed( mbedtls_reader *rd,
|
|||
if( copy_to_acc > 0 )
|
||||
memcpy( acc, new_frag, copy_to_acc );
|
||||
|
||||
TRACE( trace_comment, "Copy new data of size %u of %u into accumulator at offset %u",
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||
"Copy new data of size %u of %u into accumulator at offset %u",
|
||||
(unsigned) copy_to_acc, (unsigned) new_frag_len, (unsigned) aa );
|
||||
|
||||
/* Check if, with the new fragment, we have enough data. */
|
||||
|
@ -149,10 +150,11 @@ int mbedtls_reader_feed( mbedtls_reader *rd,
|
|||
aa += copy_to_acc;
|
||||
rd->acc_share.acc_remaining = ar;
|
||||
rd->acc_avail = aa;
|
||||
RETURN( MBEDTLS_ERR_MPS_READER_NEED_MORE );
|
||||
MBEDTLS_MPS_TRACE_RETURN( MBEDTLS_ERR_MPS_READER_NEED_MORE );
|
||||
}
|
||||
|
||||
TRACE( trace_comment, "Enough data available to serve user request" );
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||
"Enough data available to serve user request" );
|
||||
|
||||
rd->acc_share.frag_offset = aa;
|
||||
aa += copy_to_acc;
|
||||
|
@ -167,7 +169,7 @@ int mbedtls_reader_feed( mbedtls_reader *rd,
|
|||
rd->frag_len = new_frag_len;
|
||||
rd->commit = 0;
|
||||
rd->end = 0;
|
||||
RETURN( 0 );
|
||||
MBEDTLS_MPS_TRACE_RETURN( 0 );
|
||||
}
|
||||
|
||||
|
||||
|
@ -178,7 +180,8 @@ int mbedtls_reader_get( mbedtls_reader *rd,
|
|||
{
|
||||
unsigned char *frag, *acc;
|
||||
mbedtls_mps_size_t end, fo, fl, frag_fetched, frag_remaining;
|
||||
TRACE_INIT( "reader_get %p, desired %u", (void*) rd, (unsigned) desired );
|
||||
MBEDTLS_MPS_TRACE_INIT( "reader_get %p, desired %u",
|
||||
(void*) rd, (unsigned) desired );
|
||||
|
||||
frag = rd->frag;
|
||||
MBEDTLS_MPS_STATE_VALIDATE_RAW( frag != NULL,
|
||||
|
@ -193,7 +196,8 @@ int mbedtls_reader_get( mbedtls_reader *rd,
|
|||
else
|
||||
fo = rd->acc_share.frag_offset;
|
||||
|
||||
TRACE( trace_comment, "frag_off %u, end %u, acc_avail %d",
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||
"frag_off %u, end %u, acc_avail %d",
|
||||
(unsigned) fo, (unsigned) rd->end,
|
||||
acc == NULL ? -1 : (int) rd->acc_avail );
|
||||
|
||||
|
@ -201,7 +205,8 @@ int mbedtls_reader_get( mbedtls_reader *rd,
|
|||
end = rd->end;
|
||||
if( end < fo )
|
||||
{
|
||||
TRACE( trace_comment, "Serve the request from the accumulator" );
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||
"Serve the request from the accumulator" );
|
||||
if( fo - end < desired )
|
||||
{
|
||||
/* Illustration of supported and unsupported cases:
|
||||
|
@ -281,7 +286,8 @@ int mbedtls_reader_get( mbedtls_reader *rd,
|
|||
* If we believe we adhere to this restricted usage throughout
|
||||
* the library, this check is a good opportunity to
|
||||
* validate this. */
|
||||
RETURN( MBEDTLS_ERR_MPS_READER_INCONSISTENT_REQUESTS );
|
||||
MBEDTLS_MPS_TRACE_RETURN(
|
||||
MBEDTLS_ERR_MPS_READER_INCONSISTENT_REQUESTS );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -294,11 +300,12 @@ int mbedtls_reader_get( mbedtls_reader *rd,
|
|||
rd->end = end;
|
||||
rd->pending = 0;
|
||||
|
||||
RETURN( 0 );
|
||||
MBEDTLS_MPS_TRACE_RETURN( 0 );
|
||||
}
|
||||
|
||||
/* Attempt to serve the request from the current fragment */
|
||||
TRACE( trace_comment, "Serve the request from the current fragment." );
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||
"Serve the request from the current fragment." );
|
||||
|
||||
fl = rd->frag_len;
|
||||
frag_fetched = end - fo; /* The amount of data from the current fragment
|
||||
|
@ -309,7 +316,9 @@ int mbedtls_reader_get( mbedtls_reader *rd,
|
|||
/* Check if we can serve the read request from the fragment. */
|
||||
if( frag_remaining < desired )
|
||||
{
|
||||
TRACE( trace_comment, "There's not enough data in the current fragment to serve the request." );
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||
"There's not enough data in the current fragment "
|
||||
"to serve the request." );
|
||||
/* There's not enough data in the current fragment,
|
||||
* so either just RETURN what we have or fail. */
|
||||
if( buflen == NULL )
|
||||
|
@ -317,10 +326,11 @@ int mbedtls_reader_get( mbedtls_reader *rd,
|
|||
if( frag_remaining > 0 )
|
||||
{
|
||||
rd->pending = desired - frag_remaining;
|
||||
TRACE( trace_comment, "Remember to collect %u bytes before re-opening",
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||
"Remember to collect %u bytes before re-opening",
|
||||
(unsigned) rd->pending );
|
||||
}
|
||||
RETURN( MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
|
||||
MBEDTLS_MPS_TRACE_RETURN( MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
|
||||
}
|
||||
|
||||
desired = frag_remaining;
|
||||
|
@ -335,14 +345,14 @@ int mbedtls_reader_get( mbedtls_reader *rd,
|
|||
end += desired;
|
||||
rd->end = end;
|
||||
rd->pending = 0;
|
||||
RETURN( 0 );
|
||||
MBEDTLS_MPS_TRACE_RETURN( 0 );
|
||||
}
|
||||
|
||||
int mbedtls_reader_commit( mbedtls_reader *rd )
|
||||
{
|
||||
unsigned char *acc;
|
||||
mbedtls_mps_size_t aa, end, fo, shift;
|
||||
TRACE_INIT( "reader_commit" );
|
||||
MBEDTLS_MPS_TRACE_INIT( "reader_commit" );
|
||||
|
||||
MBEDTLS_MPS_STATE_VALIDATE_RAW( rd->frag != NULL,
|
||||
"mbedtls_reader_commit() requires reader to be in consuming mode" );
|
||||
|
@ -352,21 +362,24 @@ int mbedtls_reader_commit( mbedtls_reader *rd )
|
|||
|
||||
if( acc == NULL )
|
||||
{
|
||||
TRACE( trace_comment, "No accumulator, just shift end" );
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||
"No accumulator, just shift end" );
|
||||
rd->commit = end;
|
||||
RETURN( 0 );
|
||||
MBEDTLS_MPS_TRACE_RETURN( 0 );
|
||||
}
|
||||
|
||||
fo = rd->acc_share.frag_offset;
|
||||
if( end >= fo )
|
||||
{
|
||||
TRACE( trace_comment, "Started to serve fragment, get rid of accumulator" );
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||
"Started to serve fragment, get rid of accumulator" );
|
||||
shift = fo;
|
||||
aa = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
TRACE( trace_comment, "Still serving from accumulator" );
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||
"Still serving from accumulator" );
|
||||
aa = rd->acc_avail;
|
||||
shift = end;
|
||||
memmove( acc, acc + shift, aa - shift );
|
||||
|
@ -381,9 +394,10 @@ int mbedtls_reader_commit( mbedtls_reader *rd )
|
|||
rd->commit = end;
|
||||
rd->end = end;
|
||||
|
||||
TRACE( trace_comment, "Final state: (end=commit,fo,avail) = (%u,%u,%u)",
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||
"Final state: (end=commit,fo,avail) = (%u,%u,%u)",
|
||||
(unsigned) end, (unsigned) fo, (unsigned) aa );
|
||||
RETURN( 0 );
|
||||
MBEDTLS_MPS_TRACE_RETURN( 0 );
|
||||
}
|
||||
|
||||
int mbedtls_reader_reclaim( mbedtls_reader *rd,
|
||||
|
@ -392,7 +406,7 @@ int mbedtls_reader_reclaim( mbedtls_reader *rd,
|
|||
unsigned char *frag, *acc;
|
||||
mbedtls_mps_size_t pending, commit;
|
||||
mbedtls_mps_size_t al, fo, fl;
|
||||
TRACE_INIT( "reader_reclaim" );
|
||||
MBEDTLS_MPS_TRACE_INIT( "reader_reclaim" );
|
||||
|
||||
if( paused != NULL )
|
||||
*paused = 0;
|
||||
|
@ -413,27 +427,33 @@ int mbedtls_reader_reclaim( mbedtls_reader *rd,
|
|||
|
||||
if( pending == 0 )
|
||||
{
|
||||
TRACE( trace_comment, "No unsatisfied read-request has been logged." );
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||
"No unsatisfied read-request has been logged." );
|
||||
/* Check if there's data left to be consumed. */
|
||||
if( commit < fo || commit - fo < fl )
|
||||
{
|
||||
TRACE( trace_comment, "There is data left to be consumed." );
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||
"There is data left to be consumed." );
|
||||
rd->end = commit;
|
||||
RETURN( MBEDTLS_ERR_MPS_READER_DATA_LEFT );
|
||||
MBEDTLS_MPS_TRACE_RETURN( MBEDTLS_ERR_MPS_READER_DATA_LEFT );
|
||||
}
|
||||
TRACE( trace_comment, "The fragment has been completely processed and committed." );
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||
"The fragment has been completely processed and committed." );
|
||||
}
|
||||
else
|
||||
{
|
||||
mbedtls_mps_size_t frag_backup_offset;
|
||||
mbedtls_mps_size_t frag_backup_len;
|
||||
TRACE( trace_comment, "There has been an unsatisfied read-request with %u bytes overhead.",
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||
"There has been an unsatisfied read-request with %u bytes overhead.",
|
||||
(unsigned) pending );
|
||||
|
||||
if( acc == NULL )
|
||||
{
|
||||
TRACE( trace_comment, "No accumulator present" );
|
||||
RETURN( MBEDTLS_ERR_MPS_READER_NEED_ACCUMULATOR );
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||
"No accumulator present" );
|
||||
MBEDTLS_MPS_TRACE_RETURN(
|
||||
MBEDTLS_ERR_MPS_READER_NEED_ACCUMULATOR );
|
||||
}
|
||||
al = rd->acc_len;
|
||||
|
||||
|
@ -443,7 +463,8 @@ int mbedtls_reader_reclaim( mbedtls_reader *rd,
|
|||
{
|
||||
/* No, accumulator is still being processed. */
|
||||
int overflow;
|
||||
TRACE( trace_comment, "Still processing data from the accumulator" );
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||
"Still processing data from the accumulator" );
|
||||
|
||||
overflow =
|
||||
( fo + fl < fo ) || ( fo + fl + pending < fo + fl );
|
||||
|
@ -451,12 +472,16 @@ int mbedtls_reader_reclaim( mbedtls_reader *rd,
|
|||
{
|
||||
rd->end = commit;
|
||||
rd->pending = 0;
|
||||
TRACE( trace_error, "The accumulator is too small to handle the backup." );
|
||||
TRACE( trace_error, "* Remaining size: %u", (unsigned) al );
|
||||
TRACE( trace_error, "* Needed: %u (%u + %u + %u)",
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_error,
|
||||
"The accumulator is too small to handle the backup." );
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_error,
|
||||
"* Remaining size: %u", (unsigned) al );
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_error,
|
||||
"* Needed: %u (%u + %u + %u)",
|
||||
(unsigned) ( fo + fl + pending ),
|
||||
(unsigned) fo, (unsigned) fl, (unsigned) pending );
|
||||
RETURN( MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL );
|
||||
MBEDTLS_MPS_TRACE_RETURN(
|
||||
MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL );
|
||||
}
|
||||
frag_backup_offset = 0;
|
||||
frag_backup_len = fl;
|
||||
|
@ -465,7 +490,8 @@ int mbedtls_reader_reclaim( mbedtls_reader *rd,
|
|||
{
|
||||
/* Yes, the accumulator is already processed. */
|
||||
int overflow;
|
||||
TRACE( trace_comment, "The accumulator has already been processed" );
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||
"The accumulator has already been processed" );
|
||||
|
||||
frag_backup_offset = commit;
|
||||
frag_backup_len = fl - commit;
|
||||
|
@ -476,12 +502,17 @@ int mbedtls_reader_reclaim( mbedtls_reader *rd,
|
|||
{
|
||||
rd->end = commit;
|
||||
rd->pending = 0;
|
||||
TRACE( trace_error, "The accumulator is too small to handle the backup." );
|
||||
TRACE( trace_error, "* Remaining size: %u", (unsigned) ( al - fo ) );
|
||||
TRACE( trace_error, "* Needed: %u (%u + %u)",
|
||||
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_error,
|
||||
"The accumulator is too small to handle the backup." );
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_error,
|
||||
"* Remaining size: %u", (unsigned) ( al - fo ) );
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_error,
|
||||
"* Needed: %u (%u + %u)",
|
||||
(unsigned) ( frag_backup_len + pending ),
|
||||
(unsigned) frag_backup_len, (unsigned) pending );
|
||||
RETURN( MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL );
|
||||
MBEDTLS_MPS_TRACE_RETURN(
|
||||
MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -489,7 +520,8 @@ int mbedtls_reader_reclaim( mbedtls_reader *rd,
|
|||
acc += fo;
|
||||
memcpy( acc, frag, frag_backup_len );
|
||||
|
||||
TRACE( trace_comment, "Backup %u bytes into accumulator",
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||
"Backup %u bytes into accumulator",
|
||||
(unsigned) frag_backup_len );
|
||||
|
||||
rd->acc_avail = fo + frag_backup_len;
|
||||
|
@ -506,8 +538,10 @@ int mbedtls_reader_reclaim( mbedtls_reader *rd,
|
|||
rd->end = 0;
|
||||
rd->pending = 0;
|
||||
|
||||
TRACE( trace_comment, "Final state: aa %u, al %u, ar %u",
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||
"Final state: aa %u, al %u, ar %u",
|
||||
(unsigned) rd->acc_avail, (unsigned) rd->acc_len,
|
||||
(unsigned) rd->acc_share.acc_remaining );
|
||||
RETURN( 0 );
|
||||
|
||||
MBEDTLS_MPS_TRACE_RETURN( 0 );
|
||||
}
|
||||
|
|
|
@ -257,7 +257,7 @@ int mbedtls_reader_free( mbedtls_reader *reader );
|
|||
* moved to consuming state, and ownership of \p buf
|
||||
* will be passed to the reader until mbedtls_reader_reclaim()
|
||||
* is called.
|
||||
* \return \c MBEDTLS_ERR_READER_NEED_MORE if more input data is
|
||||
* \return \c MBEDTLS_ERR_MPS_READER_NEED_MORE if more input data is
|
||||
* required to fulfill a previous request to mbedtls_reader_get().
|
||||
* In this case, the reader remains in producing state and
|
||||
* takes no ownership of the provided buffer (an internal copy
|
||||
|
@ -308,7 +308,7 @@ int mbedtls_reader_reclaim( mbedtls_reader *reader,
|
|||
* (if \c buflen == \c NULL). The user hass ownership
|
||||
* of the buffer until the next call to mbedtls_reader_commit().
|
||||
* or mbedtls_reader_reclaim().
|
||||
* \return #MBEDTLS_ERR_READER_OUT_OF_DATA if there is not enough
|
||||
* \return #MBEDTLS_ERR_MPS_READER_OUT_OF_DATA if there is not enough
|
||||
* data available to serve the read request. In this case,
|
||||
* the reader remains intact, and additional data can be
|
||||
* provided by reclaiming the current input buffer via
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
|
||||
#include "mps_common.h"
|
||||
|
||||
#if defined(MBEDTLS_MPS_TRACE)
|
||||
#if defined(MBEDTLS_MPS_ENABLE_TRACE)
|
||||
|
||||
#include "mps_trace.h"
|
||||
#include <stdarg.h>
|
||||
|
@ -50,7 +50,7 @@ static char const * colors[] =
|
|||
|
||||
#define MPS_TRACE_BUF_SIZE 100
|
||||
|
||||
void trace_print_msg( int id, int line, const char *format, ... )
|
||||
void mbedtls_mps_trace_print_msg( int id, int line, const char *format, ... )
|
||||
{
|
||||
int ret;
|
||||
char str[MPS_TRACE_BUF_SIZE];
|
||||
|
@ -66,27 +66,27 @@ void trace_print_msg( int id, int line, const char *format, ... )
|
|||
}
|
||||
}
|
||||
|
||||
int trace_get_depth()
|
||||
int mbedtls_mps_trace_get_depth()
|
||||
{
|
||||
return trace_depth_;
|
||||
}
|
||||
void trace_dec_depth()
|
||||
void mbedtls_mps_trace_dec_depth()
|
||||
{
|
||||
trace_depth_--;
|
||||
}
|
||||
void trace_inc_depth()
|
||||
void mbedtls_mps_trace_inc_depth()
|
||||
{
|
||||
trace_depth_++;
|
||||
}
|
||||
|
||||
void trace_color( int id )
|
||||
void mbedtls_mps_trace_color( int id )
|
||||
{
|
||||
if( id > (int) ( sizeof( colors ) / sizeof( *colors ) ) )
|
||||
return;
|
||||
printf( "%s", colors[ id ] );
|
||||
}
|
||||
|
||||
void trace_indent( int level, trace_type ty )
|
||||
void mbedtls_mps_trace_indent( int level, mbedtls_mps_trace_type ty )
|
||||
{
|
||||
if( level > 0 )
|
||||
{
|
||||
|
@ -98,19 +98,19 @@ void trace_indent( int level, trace_type ty )
|
|||
|
||||
switch( ty )
|
||||
{
|
||||
case trace_comment:
|
||||
case mbedtls_mps_trace_comment:
|
||||
mbedtls_printf( "@ " );
|
||||
break;
|
||||
|
||||
case trace_call:
|
||||
case mbedtls_mps_trace_call:
|
||||
mbedtls_printf( "+--> " );
|
||||
break;
|
||||
|
||||
case trace_error:
|
||||
case mbedtls_mps_trace_error:
|
||||
mbedtls_printf( "E " );
|
||||
break;
|
||||
|
||||
case trace_return:
|
||||
case mbedtls_mps_trace_return:
|
||||
mbedtls_printf( "< " );
|
||||
break;
|
||||
|
||||
|
@ -119,4 +119,4 @@ void trace_indent( int level, trace_type ty )
|
|||
}
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_MPS_TRACE */
|
||||
#endif /* MBEDTLS_MPS_ENABLE_TRACE */
|
||||
|
|
|
@ -23,8 +23,8 @@
|
|||
* \brief Tracing module for MPS
|
||||
*/
|
||||
|
||||
#ifndef MBEDTLS_MPS_TRACE_H
|
||||
#define MBEDTLS_MPS_TRACE_H
|
||||
#ifndef MBEDTLS_MPS_MBEDTLS_MPS_TRACE_H
|
||||
#define MBEDTLS_MPS_MBEDTLS_MPS_TRACE_H
|
||||
|
||||
#include "common.h"
|
||||
#include "mps_common.h"
|
||||
|
@ -38,138 +38,138 @@
|
|||
#define mbedtls_vsnprintf vsnprintf
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_MPS_TRACE)
|
||||
#if defined(MBEDTLS_MPS_ENABLE_TRACE)
|
||||
|
||||
/*
|
||||
* Adapt this to enable/disable tracing output
|
||||
* from the various layers of the MPS.
|
||||
*/
|
||||
|
||||
#define TRACE_ENABLE_LAYER_1
|
||||
#define TRACE_ENABLE_LAYER_2
|
||||
#define TRACE_ENABLE_LAYER_3
|
||||
#define TRACE_ENABLE_LAYER_4
|
||||
#define TRACE_ENABLE_READER
|
||||
#define TRACE_ENABLE_WRITER
|
||||
#define MBEDTLS_MPS_TRACE_ENABLE_LAYER_1
|
||||
#define MBEDTLS_MPS_TRACE_ENABLE_LAYER_2
|
||||
#define MBEDTLS_MPS_TRACE_ENABLE_LAYER_3
|
||||
#define MBEDTLS_MPS_TRACE_ENABLE_LAYER_4
|
||||
#define MBEDTLS_MPS_TRACE_ENABLE_READER
|
||||
#define MBEDTLS_MPS_TRACE_ENABLE_WRITER
|
||||
|
||||
/*
|
||||
* To use the existing trace module, only change
|
||||
* TRACE_ENABLE_XXX above, but don't modify the
|
||||
* MBEDTLS_MPS_TRACE_ENABLE_XXX above, but don't modify the
|
||||
* rest of this file.
|
||||
*/
|
||||
|
||||
typedef enum
|
||||
{
|
||||
trace_comment,
|
||||
trace_call,
|
||||
trace_error,
|
||||
trace_return
|
||||
} trace_type;
|
||||
mbedtls_mps_trace_comment,
|
||||
mbedtls_mps_trace_call,
|
||||
mbedtls_mps_trace_error,
|
||||
mbedtls_mps_trace_return
|
||||
} mbedtls_mps_trace_type;
|
||||
|
||||
#define TRACE_BIT_LAYER_1 1
|
||||
#define TRACE_BIT_LAYER_2 2
|
||||
#define TRACE_BIT_LAYER_3 3
|
||||
#define TRACE_BIT_LAYER_4 4
|
||||
#define TRACE_BIT_WRITER 5
|
||||
#define TRACE_BIT_READER 6
|
||||
#define MBEDTLS_MPS_TRACE_BIT_LAYER_1 1
|
||||
#define MBEDTLS_MPS_TRACE_BIT_LAYER_2 2
|
||||
#define MBEDTLS_MPS_TRACE_BIT_LAYER_3 3
|
||||
#define MBEDTLS_MPS_TRACE_BIT_LAYER_4 4
|
||||
#define MBEDTLS_MPS_TRACE_BIT_WRITER 5
|
||||
#define MBEDTLS_MPS_TRACE_BIT_READER 6
|
||||
|
||||
#if defined(TRACE_ENABLE_LAYER_1)
|
||||
#define TRACE_MASK_LAYER_1 (1u << TRACE_BIT_LAYER_1 )
|
||||
#if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_1)
|
||||
#define MBEDTLS_MPS_TRACE_MASK_LAYER_1 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_1 )
|
||||
#else
|
||||
#define TRACE_MASK_LAYER_1 0
|
||||
#define MBEDTLS_MPS_TRACE_MASK_LAYER_1 0
|
||||
#endif
|
||||
|
||||
#if defined(TRACE_ENABLE_LAYER_2)
|
||||
#define TRACE_MASK_LAYER_2 (1u << TRACE_BIT_LAYER_2 )
|
||||
#if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_2)
|
||||
#define MBEDTLS_MPS_TRACE_MASK_LAYER_2 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_2 )
|
||||
#else
|
||||
#define TRACE_MASK_LAYER_2 0
|
||||
#define MBEDTLS_MPS_TRACE_MASK_LAYER_2 0
|
||||
#endif
|
||||
|
||||
#if defined(TRACE_ENABLE_LAYER_3)
|
||||
#define TRACE_MASK_LAYER_3 (1u << TRACE_BIT_LAYER_3 )
|
||||
#if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_3)
|
||||
#define MBEDTLS_MPS_TRACE_MASK_LAYER_3 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_3 )
|
||||
#else
|
||||
#define TRACE_MASK_LAYER_3 0
|
||||
#define MBEDTLS_MPS_TRACE_MASK_LAYER_3 0
|
||||
#endif
|
||||
|
||||
#if defined(TRACE_ENABLE_LAYER_4)
|
||||
#define TRACE_MASK_LAYER_4 (1u << TRACE_BIT_LAYER_4 )
|
||||
#if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_4)
|
||||
#define MBEDTLS_MPS_TRACE_MASK_LAYER_4 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_4 )
|
||||
#else
|
||||
#define TRACE_MASK_LAYER_4 0
|
||||
#define MBEDTLS_MPS_TRACE_MASK_LAYER_4 0
|
||||
#endif
|
||||
|
||||
#if defined(TRACE_ENABLE_READER)
|
||||
#define TRACE_MASK_READER (1u << TRACE_BIT_READER )
|
||||
#if defined(MBEDTLS_MPS_TRACE_ENABLE_READER)
|
||||
#define MBEDTLS_MPS_TRACE_MASK_READER (1u << MBEDTLS_MPS_TRACE_BIT_READER )
|
||||
#else
|
||||
#define TRACE_MASK_READER 0
|
||||
#define MBEDTLS_MPS_TRACE_MASK_READER 0
|
||||
#endif
|
||||
|
||||
#if defined(TRACE_ENABLE_WRITER)
|
||||
#define TRACE_MASK_WRITER (1u << TRACE_BIT_WRITER )
|
||||
#if defined(MBEDTLS_MPS_TRACE_ENABLE_WRITER)
|
||||
#define MBEDTLS_MPS_TRACE_MASK_WRITER (1u << MBEDTLS_MPS_TRACE_BIT_WRITER )
|
||||
#else
|
||||
#define TRACE_MASK_WRITER 0
|
||||
#define MBEDTLS_MPS_TRACE_MASK_WRITER 0
|
||||
#endif
|
||||
|
||||
#define TRACE_MASK ( TRACE_MASK_LAYER_1 | \
|
||||
TRACE_MASK_LAYER_2 | \
|
||||
TRACE_MASK_LAYER_3 | \
|
||||
TRACE_MASK_LAYER_4 | \
|
||||
TRACE_MASK_READER | \
|
||||
TRACE_MASK_WRITER )
|
||||
#define MBEDTLS_MPS_TRACE_MASK ( MBEDTLS_MPS_TRACE_MASK_LAYER_1 | \
|
||||
MBEDTLS_MPS_TRACE_MASK_LAYER_2 | \
|
||||
MBEDTLS_MPS_TRACE_MASK_LAYER_3 | \
|
||||
MBEDTLS_MPS_TRACE_MASK_LAYER_4 | \
|
||||
MBEDTLS_MPS_TRACE_MASK_READER | \
|
||||
MBEDTLS_MPS_TRACE_MASK_WRITER )
|
||||
|
||||
/* We have to avoid globals because E-ACSL chokes on them...
|
||||
* Wrap everything in stub functions. */
|
||||
int trace_get_depth( void );
|
||||
void trace_inc_depth( void );
|
||||
void trace_dec_depth( void );
|
||||
int mbedtls_mps_trace_get_depth( void );
|
||||
void mbedtls_mps_trace_inc_depth( void );
|
||||
void mbedtls_mps_trace_dec_depth( void );
|
||||
|
||||
void trace_color( int id );
|
||||
void trace_indent( int level, trace_type ty );
|
||||
void mbedtls_mps_trace_color( int id );
|
||||
void mbedtls_mps_trace_indent( int level, mbedtls_mps_trace_type ty );
|
||||
|
||||
void trace_print_msg( int id, int line, const char *format, ... );
|
||||
void mbedtls_mps_trace_print_msg( int id, int line, const char *format, ... );
|
||||
|
||||
#define TRACE( type, ... ) \
|
||||
#define MBEDTLS_MPS_TRACE( type, ... ) \
|
||||
do { \
|
||||
if( ! ( TRACE_MASK & ( 1u << trace_id ) ) ) \
|
||||
if( ! ( MBEDTLS_MPS_TRACE_MASK & ( 1u << mbedtls_mps_trace_id ) ) ) \
|
||||
break; \
|
||||
trace_indent( trace_get_depth(), type ); \
|
||||
trace_color( trace_id ); \
|
||||
trace_print_msg( trace_id, __LINE__, __VA_ARGS__ ); \
|
||||
trace_color( 0 ); \
|
||||
mbedtls_mps_trace_indent( mbedtls_mps_trace_get_depth(), type ); \
|
||||
mbedtls_mps_trace_color( mbedtls_mps_trace_id ); \
|
||||
mbedtls_mps_trace_print_msg( mbedtls_mps_trace_id, __LINE__, __VA_ARGS__ ); \
|
||||
mbedtls_mps_trace_color( 0 ); \
|
||||
} while( 0 )
|
||||
|
||||
#define TRACE_INIT( ... ) \
|
||||
#define MBEDTLS_MPS_TRACE_INIT( ... ) \
|
||||
do { \
|
||||
if( ! ( TRACE_MASK & ( 1u << trace_id ) ) ) \
|
||||
if( ! ( MBEDTLS_MPS_TRACE_MASK & ( 1u << mbedtls_mps_trace_id ) ) ) \
|
||||
break; \
|
||||
TRACE( trace_call, __VA_ARGS__ ); \
|
||||
trace_inc_depth(); \
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_call, __VA_ARGS__ ); \
|
||||
mbedtls_mps_trace_inc_depth(); \
|
||||
} while( 0 )
|
||||
|
||||
#define TRACE_END( val ) \
|
||||
#define MBEDTLS_MPS_TRACE_END( val ) \
|
||||
do { \
|
||||
if( ! ( TRACE_MASK & ( 1u << trace_id ) ) ) \
|
||||
if( ! ( MBEDTLS_MPS_TRACE_MASK & ( 1u << mbedtls_mps_trace_id ) ) ) \
|
||||
break; \
|
||||
TRACE( trace_return, "%d (-%#04x)", \
|
||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_return, "%d (-%#04x)", \
|
||||
(int) (val), -((unsigned)(val)) ); \
|
||||
trace_dec_depth(); \
|
||||
mbedtls_mps_trace_dec_depth(); \
|
||||
} while( 0 )
|
||||
|
||||
#define RETURN( val ) \
|
||||
#define MBEDTLS_MPS_TRACE_RETURN( val ) \
|
||||
do { \
|
||||
/* Breaks tail recursion. */ \
|
||||
int ret__ = val; \
|
||||
TRACE_END( ret__ ); \
|
||||
MBEDTLS_MPS_TRACE_END( ret__ ); \
|
||||
return( ret__ ); \
|
||||
} while( 0 )
|
||||
|
||||
#else /* MBEDTLS_MPS_TRACE */
|
||||
|
||||
#define TRACE( type, ... ) do { } while( 0 )
|
||||
#define TRACE_INIT( ... ) do { } while( 0 )
|
||||
#define TRACE_END do { } while( 0 )
|
||||
#define MBEDTLS_MPS_TRACE( type, ... ) do { } while( 0 )
|
||||
#define MBEDTLS_MPS_TRACE_INIT( ... ) do { } while( 0 )
|
||||
#define MBEDTLS_MPS_TRACE_END do { } while( 0 )
|
||||
|
||||
#define RETURN( val ) return( val );
|
||||
#define MBEDTLS_MPS_TRACE_RETURN( val ) return( val );
|
||||
|
||||
#endif /* MBEDTLS_MPS_TRACE */
|
||||
|
||||
#endif /* MBEDTLS_MPS_TRACE_H */
|
||||
#endif /* MBEDTLS_MPS_MBEDTLS_MPS_TRACE_H */
|
||||
|
|
Loading…
Reference in a new issue