Improve readability of MPS reader implementation
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
This commit is contained in:
parent
0bea62f2d7
commit
f81e41f1e4
1 changed files with 90 additions and 45 deletions
|
@ -66,6 +66,54 @@ static int mbedtls_mps_trace_id = MBEDTLS_MPS_TRACE_BIT_READER;
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
static inline int mps_reader_is_accumulating(
|
||||||
|
mbedtls_mps_reader const *rd )
|
||||||
|
{
|
||||||
|
mbedtls_mps_size_t ar;
|
||||||
|
if( rd->acc == NULL )
|
||||||
|
return( 0 );
|
||||||
|
|
||||||
|
ar = rd->acc_share.acc_remaining;
|
||||||
|
return( ar > 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int mps_reader_is_producing(
|
||||||
|
mbedtls_mps_reader const *rd )
|
||||||
|
{
|
||||||
|
unsigned char *frag = rd->frag;
|
||||||
|
return( frag == NULL );
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int mps_reader_is_consuming(
|
||||||
|
mbedtls_mps_reader const *rd )
|
||||||
|
{
|
||||||
|
return( !mps_reader_is_producing( rd ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline mbedtls_mps_size_t mps_reader_get_fragment_offset(
|
||||||
|
mbedtls_mps_reader const *rd )
|
||||||
|
{
|
||||||
|
unsigned char *acc = rd->acc;
|
||||||
|
mbedtls_mps_size_t fo;
|
||||||
|
|
||||||
|
if( acc == NULL )
|
||||||
|
return( 0 );
|
||||||
|
|
||||||
|
fo = rd->acc_share.frag_offset;
|
||||||
|
return( fo );
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline mbedtls_mps_size_t mps_reader_serving_from_accumulator(
|
||||||
|
mbedtls_mps_reader const *rd )
|
||||||
|
{
|
||||||
|
mbedtls_mps_size_t fo, end;
|
||||||
|
|
||||||
|
fo = mps_reader_get_fragment_offset( rd );
|
||||||
|
end = rd->end;
|
||||||
|
|
||||||
|
return( end < fo );
|
||||||
|
}
|
||||||
|
|
||||||
static inline void mps_reader_zero( mbedtls_mps_reader *rd )
|
static inline void mps_reader_zero( mbedtls_mps_reader *rd )
|
||||||
{
|
{
|
||||||
/* A plain memset() would likely be more efficient,
|
/* A plain memset() would likely be more efficient,
|
||||||
|
@ -92,7 +140,9 @@ int mbedtls_mps_reader_init( mbedtls_mps_reader *rd,
|
||||||
unsigned char *acc,
|
unsigned char *acc,
|
||||||
mbedtls_mps_size_t acc_len )
|
mbedtls_mps_size_t acc_len )
|
||||||
{
|
{
|
||||||
MBEDTLS_MPS_TRACE_INIT( "reader_init, acc len %u", (unsigned) acc_len );
|
MBEDTLS_MPS_TRACE_INIT( "mbedtls_mps_reader_init" );
|
||||||
|
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||||
|
"* Accumulator size: %u bytes", (unsigned) acc_len );
|
||||||
mps_reader_zero( rd );
|
mps_reader_zero( rd );
|
||||||
rd->acc = acc;
|
rd->acc = acc;
|
||||||
rd->acc_len = acc_len;
|
rd->acc_len = acc_len;
|
||||||
|
@ -101,7 +151,7 @@ int mbedtls_mps_reader_init( mbedtls_mps_reader *rd,
|
||||||
|
|
||||||
int mbedtls_mps_reader_free( mbedtls_mps_reader *rd )
|
int mbedtls_mps_reader_free( mbedtls_mps_reader *rd )
|
||||||
{
|
{
|
||||||
MBEDTLS_MPS_TRACE_INIT( "reader_free" );
|
MBEDTLS_MPS_TRACE_INIT( "mbedtls_mps_reader_free" );
|
||||||
mps_reader_zero( rd );
|
mps_reader_zero( rd );
|
||||||
MBEDTLS_MPS_TRACE_RETURN( 0 );
|
MBEDTLS_MPS_TRACE_RETURN( 0 );
|
||||||
}
|
}
|
||||||
|
@ -110,31 +160,31 @@ int mbedtls_mps_reader_feed( mbedtls_mps_reader *rd,
|
||||||
unsigned char *new_frag,
|
unsigned char *new_frag,
|
||||||
mbedtls_mps_size_t new_frag_len )
|
mbedtls_mps_size_t new_frag_len )
|
||||||
{
|
{
|
||||||
unsigned char *acc;
|
|
||||||
mbedtls_mps_size_t copy_to_acc;
|
mbedtls_mps_size_t copy_to_acc;
|
||||||
MBEDTLS_MPS_TRACE_INIT( "reader_feed, frag %p, len %u",
|
MBEDTLS_MPS_TRACE_INIT( "mbedtls_mps_reader_feed" );
|
||||||
(void*) new_frag, (unsigned) new_frag_len );
|
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||||
|
"* Fragment length: %u bytes", (unsigned) new_frag_len );
|
||||||
|
|
||||||
if( new_frag == NULL )
|
if( new_frag == NULL )
|
||||||
MBEDTLS_MPS_TRACE_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_MPS_STATE_VALIDATE_RAW( mps_reader_is_producing( rd ),
|
||||||
"mbedtls_mps_reader_feed() requires reader to be in producing mode" );
|
"mbedtls_mps_reader_feed() requires reader to be in producing mode" );
|
||||||
|
|
||||||
acc = rd->acc;
|
if( mps_reader_is_accumulating( rd ) )
|
||||||
if( acc != NULL )
|
|
||||||
{
|
{
|
||||||
mbedtls_mps_size_t aa, ar;
|
unsigned char *acc = rd->acc;
|
||||||
|
mbedtls_mps_size_t ar = rd->acc_share.acc_remaining;
|
||||||
|
mbedtls_mps_size_t aa = rd->acc_avail;
|
||||||
|
|
||||||
ar = rd->acc_share.acc_remaining;
|
/* Skip over parts of the accumulator that have already been filled. */
|
||||||
aa = rd->acc_avail;
|
acc += aa;
|
||||||
|
|
||||||
copy_to_acc = ar;
|
copy_to_acc = ar;
|
||||||
if( copy_to_acc > new_frag_len )
|
if( copy_to_acc > new_frag_len )
|
||||||
copy_to_acc = new_frag_len;
|
copy_to_acc = new_frag_len;
|
||||||
|
|
||||||
acc += aa;
|
/* Copy new contents to accumulator. */
|
||||||
|
|
||||||
if( copy_to_acc > 0 )
|
if( copy_to_acc > 0 )
|
||||||
memcpy( acc, new_frag, copy_to_acc );
|
memcpy( acc, new_frag, copy_to_acc );
|
||||||
|
|
||||||
|
@ -146,21 +196,24 @@ int mbedtls_mps_reader_feed( mbedtls_mps_reader *rd,
|
||||||
ar -= copy_to_acc;
|
ar -= copy_to_acc;
|
||||||
if( ar > 0 )
|
if( ar > 0 )
|
||||||
{
|
{
|
||||||
/* Need more data */
|
/* We need to accumulate more data. Stay in producing mode. */
|
||||||
aa += copy_to_acc;
|
aa += copy_to_acc;
|
||||||
rd->acc_share.acc_remaining = ar;
|
rd->acc_share.acc_remaining = ar;
|
||||||
rd->acc_avail = aa;
|
rd->acc_avail = aa;
|
||||||
MBEDTLS_MPS_TRACE_RETURN( MBEDTLS_ERR_MPS_READER_NEED_MORE );
|
MBEDTLS_MPS_TRACE_RETURN( MBEDTLS_ERR_MPS_READER_NEED_MORE );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* We have filled the accumulator: Move to consuming mode. */
|
||||||
|
|
||||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||||
"Enough data available to serve user request" );
|
"Enough data available to serve user request" );
|
||||||
|
|
||||||
|
/* Remember overlap of accumulator and fragment. */
|
||||||
rd->acc_share.frag_offset = aa;
|
rd->acc_share.frag_offset = aa;
|
||||||
aa += copy_to_acc;
|
aa += copy_to_acc;
|
||||||
rd->acc_avail = aa;
|
rd->acc_avail = aa;
|
||||||
}
|
}
|
||||||
else
|
else /* Not accumulating */
|
||||||
{
|
{
|
||||||
rd->acc_share.frag_offset = 0;
|
rd->acc_share.frag_offset = 0;
|
||||||
}
|
}
|
||||||
|
@ -178,33 +231,23 @@ int mbedtls_mps_reader_get( mbedtls_mps_reader *rd,
|
||||||
unsigned char **buffer,
|
unsigned char **buffer,
|
||||||
mbedtls_mps_size_t *buflen )
|
mbedtls_mps_size_t *buflen )
|
||||||
{
|
{
|
||||||
unsigned char *frag, *acc;
|
unsigned char *frag;
|
||||||
mbedtls_mps_size_t end, fo, fl, frag_fetched, frag_remaining;
|
mbedtls_mps_size_t fl, fo, end, frag_fetched, frag_remaining;
|
||||||
MBEDTLS_MPS_TRACE_INIT( "reader_get %p, desired %u",
|
MBEDTLS_MPS_TRACE_INIT( "mbedtls_mps_reader_get" );
|
||||||
(void*) rd, (unsigned) desired );
|
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||||
|
"* Bytes requested: %u", (unsigned) desired );
|
||||||
|
|
||||||
frag = rd->frag;
|
MBEDTLS_MPS_STATE_VALIDATE_RAW( mps_reader_is_consuming( rd ),
|
||||||
MBEDTLS_MPS_STATE_VALIDATE_RAW( frag != NULL,
|
|
||||||
"mbedtls_mps_reader_get() requires reader to be in consuming mode" );
|
"mbedtls_mps_reader_get() requires reader to be in consuming mode" );
|
||||||
|
|
||||||
/* The fragment offset indicates the offset of the fragment
|
end = rd->end;
|
||||||
* from the accmulator, if the latter is present. Use a offset
|
fo = mps_reader_get_fragment_offset( rd );
|
||||||
* of \c 0 if no accumulator is used. */
|
|
||||||
acc = rd->acc;
|
|
||||||
if( acc == NULL )
|
|
||||||
fo = 0;
|
|
||||||
else
|
|
||||||
fo = rd->acc_share.frag_offset;
|
|
||||||
|
|
||||||
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 );
|
|
||||||
|
|
||||||
/* Check if we're still serving from the accumulator. */
|
/* Check if we're still serving from the accumulator. */
|
||||||
end = rd->end;
|
if( mps_reader_serving_from_accumulator( rd ) )
|
||||||
if( end < fo )
|
|
||||||
{
|
{
|
||||||
|
unsigned char *acc;
|
||||||
|
|
||||||
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_comment,
|
||||||
"Serve the request from the accumulator" );
|
"Serve the request from the accumulator" );
|
||||||
if( fo - end < desired )
|
if( fo - end < desired )
|
||||||
|
@ -291,7 +334,9 @@ int mbedtls_mps_reader_get( mbedtls_mps_reader *rd,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
acc = rd->acc;
|
||||||
acc += end;
|
acc += end;
|
||||||
|
|
||||||
*buffer = acc;
|
*buffer = acc;
|
||||||
if( buflen != NULL )
|
if( buflen != NULL )
|
||||||
*buflen = desired;
|
*buflen = desired;
|
||||||
|
@ -310,7 +355,6 @@ int mbedtls_mps_reader_get( mbedtls_mps_reader *rd,
|
||||||
fl = rd->frag_len;
|
fl = rd->frag_len;
|
||||||
frag_fetched = end - fo; /* The amount of data from the current fragment
|
frag_fetched = end - fo; /* The amount of data from the current fragment
|
||||||
* that has already been passed to the user. */
|
* that has already been passed to the user. */
|
||||||
frag += frag_fetched;
|
|
||||||
frag_remaining = fl - frag_fetched; /* Remaining data in fragment */
|
frag_remaining = fl - frag_fetched; /* Remaining data in fragment */
|
||||||
|
|
||||||
/* Check if we can serve the read request from the fragment. */
|
/* Check if we can serve the read request from the fragment. */
|
||||||
|
@ -338,6 +382,10 @@ int mbedtls_mps_reader_get( mbedtls_mps_reader *rd,
|
||||||
|
|
||||||
/* There's enough data in the current fragment to serve the
|
/* There's enough data in the current fragment to serve the
|
||||||
* (potentially modified) read request. */
|
* (potentially modified) read request. */
|
||||||
|
|
||||||
|
frag = rd->frag;
|
||||||
|
frag += frag_fetched;
|
||||||
|
|
||||||
*buffer = frag;
|
*buffer = frag;
|
||||||
if( buflen != NULL )
|
if( buflen != NULL )
|
||||||
*buflen = desired;
|
*buflen = desired;
|
||||||
|
@ -351,8 +399,8 @@ int mbedtls_mps_reader_get( mbedtls_mps_reader *rd,
|
||||||
int mbedtls_mps_reader_commit( mbedtls_mps_reader *rd )
|
int mbedtls_mps_reader_commit( mbedtls_mps_reader *rd )
|
||||||
{
|
{
|
||||||
mbedtls_mps_size_t end;
|
mbedtls_mps_size_t end;
|
||||||
MBEDTLS_MPS_TRACE_INIT( "reader_commit" );
|
MBEDTLS_MPS_TRACE_INIT( "mbedtls_mps_reader_commit" );
|
||||||
MBEDTLS_MPS_STATE_VALIDATE_RAW( rd->frag != NULL,
|
MBEDTLS_MPS_STATE_VALIDATE_RAW( mps_reader_is_consuming( rd ),
|
||||||
"mbedtls_mps_reader_commit() requires reader to be in consuming mode" );
|
"mbedtls_mps_reader_commit() requires reader to be in consuming mode" );
|
||||||
|
|
||||||
end = rd->end;
|
end = rd->end;
|
||||||
|
@ -372,19 +420,16 @@ int mbedtls_mps_reader_reclaim( mbedtls_mps_reader *rd,
|
||||||
if( paused != NULL )
|
if( paused != NULL )
|
||||||
*paused = 0;
|
*paused = 0;
|
||||||
|
|
||||||
frag = rd->frag;
|
MBEDTLS_MPS_STATE_VALIDATE_RAW( mps_reader_is_consuming( rd ),
|
||||||
MBEDTLS_MPS_STATE_VALIDATE_RAW( frag != NULL,
|
|
||||||
"mbedtls_mps_reader_reclaim() requires reader to be in consuming mode" );
|
"mbedtls_mps_reader_reclaim() requires reader to be in consuming mode" );
|
||||||
|
|
||||||
|
frag = rd->frag;
|
||||||
acc = rd->acc;
|
acc = rd->acc;
|
||||||
pending = rd->pending;
|
pending = rd->pending;
|
||||||
commit = rd->commit;
|
commit = rd->commit;
|
||||||
fl = rd->frag_len;
|
fl = rd->frag_len;
|
||||||
|
|
||||||
if( acc == NULL )
|
fo = mps_reader_get_fragment_offset( rd );
|
||||||
fo = 0;
|
|
||||||
else
|
|
||||||
fo = rd->acc_share.frag_offset;
|
|
||||||
|
|
||||||
if( pending == 0 )
|
if( pending == 0 )
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue