@ -11,11 +11,6 @@
*/
*/
# include "srslte/asn1/asn1_utils.h"
# include "srslte/asn1/asn1_utils.h"
# include "srslte/common/logmap.h"
# include "srslte/srslog/bundled/fmt/core.h"
# include <cmath>
# include <stdarg.h> /* va_list, va_start, va_arg, va_end */
# include <stdio.h>
namespace asn1 {
namespace asn1 {
@ -23,70 +18,16 @@ namespace asn1 {
logging
logging
* * * * * * * * * * * * * * * * * * * * * * * */
* * * * * * * * * * * * * * * * * * * * * * * */
void vlog_print ( srslte : : LOG_LEVEL_ENUM log_level , const char * format , va_list args )
{
char * args_msg = nullptr ;
if ( vasprintf ( & args_msg , format , args ) > 0 ) {
switch ( log_level ) {
case srslte : : LOG_LEVEL_ERROR :
srslte : : logmap : : get ( " ASN1 " ) - > error ( " %s " , args_msg ) ;
break ;
case srslte : : LOG_LEVEL_WARNING :
srslte : : logmap : : get ( " ASN1 " ) - > warning ( " %s " , args_msg ) ;
break ;
case srslte : : LOG_LEVEL_INFO :
srslte : : logmap : : get ( " ASN1 " ) - > info ( " %s " , args_msg ) ;
break ;
case srslte : : LOG_LEVEL_DEBUG :
srslte : : logmap : : get ( " ASN1 " ) - > debug ( " %s " , args_msg ) ;
default :
break ;
}
}
if ( args_msg ) {
free ( args_msg ) ;
}
}
void log_error ( const char * format , . . . )
{
va_list args ;
va_start ( args , format ) ;
vlog_print ( srslte : : LOG_LEVEL_ERROR , format , args ) ;
va_end ( args ) ;
}
void log_warning ( const char * format , . . . )
{
va_list args ;
va_start ( args , format ) ;
vlog_print ( srslte : : LOG_LEVEL_WARNING , format , args ) ;
va_end ( args ) ;
}
void log_info ( const char * format , . . . )
{
va_list args ;
va_start ( args , format ) ;
vlog_print ( srslte : : LOG_LEVEL_INFO , format , args ) ;
va_end ( args ) ;
}
void log_debug ( const char * format , . . . )
{
va_list args ;
va_start ( args , format ) ;
vlog_print ( srslte : : LOG_LEVEL_DEBUG , format , args ) ;
va_end ( args ) ;
}
void warn_assert ( bool cond , const char * filename , int lineno )
void warn_assert ( bool cond , const char * filename , int lineno )
{
{
if ( cond ) {
if ( cond ) {
log_warning ( " Assertion in [%s][%d] failed. \n " , filename , lineno ) ;
log_warning ( " Assertion in [%s][%d] failed. " , filename , lineno ) ;
}
}
}
}
void invalid_enum_number ( int value , const char * name )
void invalid_enum_number ( int value , const char * name )
{
{
log_error ( " The provided enum value=%d of type %s cannot be translated into a number \n " , value , name ) ;
log_error ( " The provided enum value=%d of type %s cannot be translated into a number " , value , name ) ;
}
}
void assert_choice_type ( uint32_t val , uint32_t choice_id )
void assert_choice_type ( uint32_t val , uint32_t choice_id )
@ -99,7 +40,7 @@ void assert_choice_type(uint32_t val, uint32_t choice_id)
void assert_choice_type ( const std : : string & access_type , const std : : string & current_type , const std : : string & choice_type )
void assert_choice_type ( const std : : string & access_type , const std : : string & current_type , const std : : string & choice_type )
{
{
if ( access_type ! = current_type ) {
if ( access_type ! = current_type ) {
log_error ( " Invalid field access for choice type \" %s \" ( \" %s \" != \" %s \" ) \n " ,
log_error ( " Invalid field access for choice type \" %s \" ( \" %s \" != \" %s \" ) " ,
choice_type . c_str ( ) ,
choice_type . c_str ( ) ,
access_type . c_str ( ) ,
access_type . c_str ( ) ,
current_type . c_str ( ) ) ;
current_type . c_str ( ) ) ;
@ -114,13 +55,13 @@ void log_error_code(SRSASN_CODE code, const char* filename, int line)
{
{
switch ( code ) {
switch ( code ) {
case SRSASN_ERROR_ENCODE_FAIL :
case SRSASN_ERROR_ENCODE_FAIL :
log_error ( " [%s][%d] Encoding failure. \n " , filename , line ) ;
log_error ( " [%s][%d] Encoding failure. " , filename , line ) ;
break ;
break ;
case SRSASN_ERROR_DECODE_FAIL :
case SRSASN_ERROR_DECODE_FAIL :
log_error ( " [%s][%d] Decoding failure. \n " , filename , line ) ;
log_error ( " [%s][%d] Decoding failure. " , filename , line ) ;
break ;
break ;
default :
default :
log_warning ( " [%s][%d] SRSASN_CODE=%d not recognized. \n " , filename , line , ( int ) code ) ;
log_warning ( " [%s][%d] SRSASN_CODE=%d not recognized. " , filename , line , ( int ) code ) ;
}
}
}
}
@ -128,9 +69,9 @@ const char* convert_enum_idx(const char* array[], uint32_t nof_types, uint32_t e
{
{
if ( enum_val > = nof_types ) {
if ( enum_val > = nof_types ) {
if ( enum_val = = nof_types ) {
if ( enum_val = = nof_types ) {
log_error ( " The enum of type %s was not initialized. \n " , enum_type ) ;
log_error ( " The enum of type %s was not initialized. " , enum_type ) ;
} else {
} else {
log_error ( " The enum value=%d of type %s is not valid. \n " , enum_val , enum_type ) ;
log_error ( " The enum value=%d of type %s is not valid. " , enum_val , enum_type ) ;
}
}
return " " ;
return " " ;
}
}
@ -142,9 +83,9 @@ ItemType map_enum_number(ItemType* array, uint32_t nof_types, uint32_t enum_val,
{
{
if ( enum_val > = nof_types ) {
if ( enum_val > = nof_types ) {
if ( enum_val = = nof_types ) {
if ( enum_val = = nof_types ) {
log_error ( " The enum of type %s is not initialized. \n " , enum_type ) ;
log_error ( " The enum of type %s is not initialized. " , enum_type ) ;
} else {
} else {
log_error ( " The enum value=%d of type %s cannot be converted to a number. \n " , enum_val , enum_type ) ;
log_error ( " The enum value=%d of type %s cannot be converted to a number. " , enum_val , enum_type ) ;
}
}
return 0 ;
return 0 ;
}
}
@ -202,13 +143,13 @@ int bit_ref_impl<Ptr>::distance_bytes() const
SRSASN_CODE bit_ref : : pack ( uint64_t val , uint32_t n_bits )
SRSASN_CODE bit_ref : : pack ( uint64_t val , uint32_t n_bits )
{
{
if ( n_bits > = 64 ) {
if ( n_bits > = 64 ) {
log_error ( " This method only supports packing up to 64 bits \n " ) ;
log_error ( " This method only supports packing up to 64 bits " ) ;
return SRSASN_ERROR_ENCODE_FAIL ;
return SRSASN_ERROR_ENCODE_FAIL ;
}
}
uint64_t mask ;
uint64_t mask ;
while ( n_bits > 0 ) {
while ( n_bits > 0 ) {
if ( ptr > = max_ptr ) {
if ( ptr > = max_ptr ) {
log_error ( " Buffer size limit was achieved \n " ) ;
log_error ( " Buffer size limit was achieved " ) ;
return SRSASN_ERROR_ENCODE_FAIL ;
return SRSASN_ERROR_ENCODE_FAIL ;
}
}
mask = ( ( 1ul < < n_bits ) - 1ul ) ;
mask = ( ( 1ul < < n_bits ) - 1ul ) ;
@ -234,13 +175,13 @@ template <typename T, typename Ptr>
SRSASN_CODE unpack_bits ( T & val , Ptr & ptr , uint8_t & offset , const uint8_t * max_ptr , uint32_t n_bits )
SRSASN_CODE unpack_bits ( T & val , Ptr & ptr , uint8_t & offset , const uint8_t * max_ptr , uint32_t n_bits )
{
{
if ( n_bits > sizeof ( T ) * 8 ) {
if ( n_bits > sizeof ( T ) * 8 ) {
log_error ( " This method only supports unpacking up to %d bits \n " , ( int ) sizeof ( T ) * 8 ) ;
log_error ( " This method only supports unpacking up to %d bits " , ( int ) sizeof ( T ) * 8 ) ;
return SRSASN_ERROR_DECODE_FAIL ;
return SRSASN_ERROR_DECODE_FAIL ;
}
}
val = 0 ;
val = 0 ;
while ( n_bits > 0 ) {
while ( n_bits > 0 ) {
if ( ptr > = max_ptr ) {
if ( ptr > = max_ptr ) {
log_error ( " Buffer size limit was achieved \n " ) ;
log_error ( " Buffer size limit was achieved " ) ;
return SRSASN_ERROR_DECODE_FAIL ;
return SRSASN_ERROR_DECODE_FAIL ;
}
}
if ( ( uint32_t ) ( 8 - offset ) > n_bits ) {
if ( ( uint32_t ) ( 8 - offset ) > n_bits ) {
@ -302,7 +243,7 @@ SRSASN_CODE bit_ref_impl<Ptr>::unpack_bytes(uint8_t* buf, uint32_t n_bytes)
return SRSASN_SUCCESS ;
return SRSASN_SUCCESS ;
}
}
if ( ptr + n_bytes > = max_ptr ) {
if ( ptr + n_bytes > = max_ptr ) {
log_error ( " Buffer size limit was achieved \n " ) ;
log_error ( " Buffer size limit was achieved " ) ;
return SRSASN_ERROR_DECODE_FAIL ;
return SRSASN_ERROR_DECODE_FAIL ;
}
}
if ( offset = = 0 ) {
if ( offset = = 0 ) {
@ -323,7 +264,7 @@ SRSASN_CODE bit_ref_impl<Ptr>::align_bytes()
if ( offset = = 0 )
if ( offset = = 0 )
return SRSASN_SUCCESS ;
return SRSASN_SUCCESS ;
if ( ptr > = max_ptr ) {
if ( ptr > = max_ptr ) {
log_error ( " Buffer size limit was achieved \n " ) ;
log_error ( " Buffer size limit was achieved " ) ;
return SRSASN_ERROR_DECODE_FAIL ;
return SRSASN_ERROR_DECODE_FAIL ;
}
}
offset = 0 ;
offset = 0 ;
@ -339,7 +280,7 @@ SRSASN_CODE bit_ref_impl<Ptr>::advance_bits(uint32_t n_bits)
uint32_t bytes_offset = floorf ( ( offset + n_bits ) / 8.0f ) ;
uint32_t bytes_offset = floorf ( ( offset + n_bits ) / 8.0f ) ;
if ( ptr + bytes_required > = max_ptr ) {
if ( ptr + bytes_required > = max_ptr ) {
log_error ( " Buffer size limit was achieved \n " ) ;
log_error ( " Buffer size limit was achieved " ) ;
return SRSASN_ERROR_DECODE_FAIL ;
return SRSASN_ERROR_DECODE_FAIL ;
}
}
ptr + = bytes_offset ;
ptr + = bytes_offset ;
@ -365,7 +306,7 @@ SRSASN_CODE bit_ref::pack_bytes(const uint8_t* buf, uint32_t n_bytes)
return SRSASN_SUCCESS ;
return SRSASN_SUCCESS ;
}
}
if ( ptr + n_bytes > = max_ptr ) {
if ( ptr + n_bytes > = max_ptr ) {
log_error ( " Buffer size limit was achieved \n " ) ;
log_error ( " Buffer size limit was achieved " ) ;
return SRSASN_ERROR_ENCODE_FAIL ;
return SRSASN_ERROR_ENCODE_FAIL ;
}
}
if ( offset = = 0 ) {
if ( offset = = 0 ) {
@ -385,7 +326,7 @@ SRSASN_CODE bit_ref::align_bytes_zero()
if ( offset = = 0 )
if ( offset = = 0 )
return SRSASN_SUCCESS ;
return SRSASN_SUCCESS ;
if ( ptr > = max_ptr ) {
if ( ptr > = max_ptr ) {
log_error ( " Buffer size limit was achieved \n " ) ;
log_error ( " Buffer size limit was achieved " ) ;
return SRSASN_ERROR_ENCODE_FAIL ;
return SRSASN_ERROR_ENCODE_FAIL ;
}
}
auto mask = static_cast < uint8_t > ( 256u - ( 1u < < ( 8u - offset ) ) ) ;
auto mask = static_cast < uint8_t > ( 256u - ( 1u < < ( 8u - offset ) ) ) ;
@ -403,7 +344,7 @@ SRSASN_CODE pack_unsupported_ext_flag(bit_ref& bref, bool ext)
{
{
HANDLE_CODE ( bref . pack ( ext , 1 ) ) ;
HANDLE_CODE ( bref . pack ( ext , 1 ) ) ;
if ( ext ) {
if ( ext ) {
log_error ( " ASN extensions not currently supported \n " ) ;
log_error ( " ASN extensions not currently supported " ) ;
return SRSASN_ERROR_ENCODE_FAIL ;
return SRSASN_ERROR_ENCODE_FAIL ;
}
}
return SRSASN_SUCCESS ;
return SRSASN_SUCCESS ;
@ -413,7 +354,7 @@ SRSASN_CODE unpack_unsupported_ext_flag(bool& ext, bit_ref& bref)
{
{
SRSASN_CODE ret = bref . unpack ( ext , 1 ) ;
SRSASN_CODE ret = bref . unpack ( ext , 1 ) ;
if ( ext ) {
if ( ext ) {
log_error ( " ASN extensions not currently supported \n " ) ;
log_error ( " ASN extensions not currently supported " ) ;
return SRSASN_ERROR_DECODE_FAIL ;
return SRSASN_ERROR_DECODE_FAIL ;
}
}
return ret ;
return ret ;
@ -447,7 +388,7 @@ SRSASN_CODE pack_enum(bit_ref& bref, uint32_t e, uint32_t nof_types, uint32_t no
{
{
if ( e > = nof_types ) {
if ( e > = nof_types ) {
log_error (
log_error (
" The provided enum is not within the range of possible values (%u>=%u) \n " , ( unsigned ) e , ( unsigned ) nof_types ) ;
" The provided enum is not within the range of possible values (%u>=%u) " , ( unsigned ) e , ( unsigned ) nof_types ) ;
return SRSASN_ERROR_ENCODE_FAIL ;
return SRSASN_ERROR_ENCODE_FAIL ;
}
}
SRSASN_CODE ret ;
SRSASN_CODE ret ;
@ -482,7 +423,7 @@ ValOrError unpack_enum(uint32_t nof_types, uint32_t nof_exts, bool has_ext, cbit
ret . code = bref . unpack ( ret . val , nof_bits ) ;
ret . code = bref . unpack ( ret . val , nof_bits ) ;
}
}
if ( ret . val > = nof_types ) {
if ( ret . val > = nof_types ) {
log_error ( " The provided enum is not within the range of possible values (%u>=%u) \n " ,
log_error ( " The provided enum is not within the range of possible values (%u>=%u) " ,
( unsigned ) ret . val ,
( unsigned ) ret . val ,
( unsigned ) nof_types ) ;
( unsigned ) nof_types ) ;
ret . code = SRSASN_ERROR_DECODE_FAIL ;
ret . code = SRSASN_ERROR_DECODE_FAIL ;
@ -508,7 +449,7 @@ template <class IntType>
SRSASN_CODE pack_constrained_whole_number ( bit_ref & bref , IntType n , IntType lb , IntType ub , bool aligned )
SRSASN_CODE pack_constrained_whole_number ( bit_ref & bref , IntType n , IntType lb , IntType ub , bool aligned )
{
{
if ( ub < lb or n < lb or n > ub ) {
if ( ub < lb or n < lb or n > ub ) {
log_error ( " The condition lb <= n <= ub (%ld <= %ld <= %ld) was not met \n " , ( long ) lb , ( long ) n , ( long ) ub ) ;
log_error ( " The condition lb <= n <= ub (%ld <= %ld <= %ld) was not met " , ( long ) lb , ( long ) n , ( long ) ub ) ;
return SRSASN_ERROR_ENCODE_FAIL ;
return SRSASN_ERROR_ENCODE_FAIL ;
}
}
uint64_t ra = ( uint64_t ) ( ub - lb ) + 1 ; // NOTE: Can overflow if IntType is kept
uint64_t ra = ( uint64_t ) ( ub - lb ) + 1 ; // NOTE: Can overflow if IntType is kept
@ -574,7 +515,7 @@ template <class IntType>
SRSASN_CODE unpack_constrained_whole_number ( IntType & n , cbit_ref & bref , IntType lb , IntType ub , bool aligned )
SRSASN_CODE unpack_constrained_whole_number ( IntType & n , cbit_ref & bref , IntType lb , IntType ub , bool aligned )
{
{
if ( ub < lb ) {
if ( ub < lb ) {
log_error ( " The condition lb <= ub (%ld <= %ld) was not met \n " , ( long ) lb , ( long ) ub ) ;
log_error ( " The condition lb <= ub (%ld <= %ld) was not met " , ( long ) lb , ( long ) ub ) ;
return SRSASN_ERROR_DECODE_FAIL ;
return SRSASN_ERROR_DECODE_FAIL ;
}
}
uint64_t ra = ( uint64_t ) ( ub - lb ) + 1 ; // NOTE: Can overflow if IntType is kept.
uint64_t ra = ( uint64_t ) ( ub - lb ) + 1 ; // NOTE: Can overflow if IntType is kept.
@ -588,7 +529,7 @@ SRSASN_CODE unpack_constrained_whole_number(IntType& n, cbit_ref& bref, IntType
HANDLE_CODE ( bref . unpack ( n , n_bits ) ) ;
HANDLE_CODE ( bref . unpack ( n , n_bits ) ) ;
n + = lb ;
n + = lb ;
if ( n > ub ) {
if ( n > ub ) {
log_error ( " The condition lb <= n <= ub (%ld <= %ld <= %ld) was not met \n " , ( long ) lb , ( long ) n , ( long ) ub ) ;
log_error ( " The condition lb <= n <= ub (%ld <= %ld <= %ld) was not met " , ( long ) lb , ( long ) n , ( long ) ub ) ;
return SRSASN_ERROR_DECODE_FAIL ;
return SRSASN_ERROR_DECODE_FAIL ;
}
}
} else {
} else {
@ -644,7 +585,7 @@ SRSASN_CODE pack_norm_small_non_neg_whole_number(bit_ref& bref, UintType n)
HANDLE_CODE ( bref . pack ( n , 7 ) ) ; // [1 bit: 0 | 6 bit: n]
HANDLE_CODE ( bref . pack ( n , 7 ) ) ; // [1 bit: 0 | 6 bit: n]
} else {
} else {
HANDLE_CODE ( bref . pack ( 1 , 1 ) ) ;
HANDLE_CODE ( bref . pack ( 1 , 1 ) ) ;
log_error ( " Long small integers not supported \n " ) ;
log_error ( " Long small integers not supported " ) ;
return SRSASN_ERROR_ENCODE_FAIL ;
return SRSASN_ERROR_ENCODE_FAIL ;
}
}
return SRSASN_SUCCESS ;
return SRSASN_SUCCESS ;
@ -658,7 +599,7 @@ SRSASN_CODE unpack_norm_small_non_neg_whole_number(UintType& n, cbit_ref& bref)
if ( not ext ) {
if ( not ext ) {
ret = bref . unpack ( n , 6 ) ;
ret = bref . unpack ( n , 6 ) ;
} else {
} else {
log_error ( " Long small integers not supported \n " ) ;
log_error ( " Long small integers not supported " ) ;
return SRSASN_ERROR_DECODE_FAIL ;
return SRSASN_ERROR_DECODE_FAIL ;
}
}
return ret ;
return ret ;
@ -785,7 +726,7 @@ SRSASN_CODE pack_length(bit_ref& bref, uint32_t val, bool aligned)
HANDLE_CODE ( bref . pack ( 0b10 , 2 ) ) ;
HANDLE_CODE ( bref . pack ( 0b10 , 2 ) ) ;
HANDLE_CODE ( bref . pack ( val , 14 ) ) ;
HANDLE_CODE ( bref . pack ( val , 14 ) ) ;
} else {
} else {
log_error ( " Not handling sizes longer than 16383 octets \n " ) ;
log_error ( " Not handling sizes longer than 16383 octets " ) ;
return SRSASN_ERROR_ENCODE_FAIL ;
return SRSASN_ERROR_ENCODE_FAIL ;
}
}
} else {
} else {
@ -824,7 +765,7 @@ SRSASN_CODE unpack_length(uint32_t& val, cbit_ref& bref, bool aligned)
if ( not ext ) {
if ( not ext ) {
ret = bref . unpack ( val , 14 ) ;
ret = bref . unpack ( val , 14 ) ;
} else {
} else {
log_error ( " Not handling octet strings longer than 16383 octets \n " ) ;
log_error ( " Not handling octet strings longer than 16383 octets " ) ;
val = 0 ;
val = 0 ;
return SRSASN_ERROR_DECODE_FAIL ;
return SRSASN_ERROR_DECODE_FAIL ;
}
}
@ -864,7 +805,7 @@ SRSASN_CODE pack_integer(bit_ref& bref, IntType n, IntType lb, IntType ub, bool
if ( has_ext ) {
if ( has_ext ) {
HANDLE_CODE ( bref . pack ( not within_bounds , 1 ) ) ;
HANDLE_CODE ( bref . pack ( not within_bounds , 1 ) ) ;
} else if ( not within_bounds ) {
} else if ( not within_bounds ) {
log_error ( " The condition lb <= n <= ub (%ld <= %ld <= %ld) was not met \n " , ( long ) lb , ( long ) n , ( long ) ub ) ;
log_error ( " The condition lb <= n <= ub (%ld <= %ld <= %ld) was not met " , ( long ) lb , ( long ) n , ( long ) ub ) ;
return SRSASN_ERROR_ENCODE_FAIL ;
return SRSASN_ERROR_ENCODE_FAIL ;
}
}
bool lower_bounded = lb ! = std : : numeric_limits < IntType > : : min ( ) or std : : is_unsigned < IntType > : : value ;
bool lower_bounded = lb ! = std : : numeric_limits < IntType > : : min ( ) or std : : is_unsigned < IntType > : : value ;
@ -1012,7 +953,7 @@ template struct integer_packer<uint64_t>;
uint64_t octstring_to_number ( const uint8_t * ptr , uint32_t nbytes )
uint64_t octstring_to_number ( const uint8_t * ptr , uint32_t nbytes )
{
{
if ( nbytes > 8 ) {
if ( nbytes > 8 ) {
log_error ( " octstring of size=%d does not fit in an uint64_t \n " , nbytes ) ;
log_error ( " octstring of size=%d does not fit in an uint64_t " , nbytes ) ;
return 0 ;
return 0 ;
}
}
uint64_t val = 0 ;
uint64_t val = 0 ;
@ -1025,7 +966,7 @@ uint64_t octstring_to_number(const uint8_t* ptr, uint32_t nbytes)
void number_to_octstring ( uint8_t * ptr , uint64_t number , uint32_t nbytes )
void number_to_octstring ( uint8_t * ptr , uint64_t number , uint32_t nbytes )
{
{
if ( nbytes > 8 ) {
if ( nbytes > 8 ) {
log_error ( " octstring of size=%d does not fit in an uint64_t \n " , nbytes ) ;
log_error ( " octstring of size=%d does not fit in an uint64_t " , nbytes ) ;
return ;
return ;
}
}
for ( uint32_t i = 0 ; i < nbytes ; + + i ) {
for ( uint32_t i = 0 ; i < nbytes ; + + i ) {
@ -1054,7 +995,7 @@ std::string octstring_to_string(const uint8_t* ptr, uint32_t N)
void string_to_octstring ( uint8_t * ptr , const std : : string & str )
void string_to_octstring ( uint8_t * ptr , const std : : string & str )
{
{
if ( str . size ( ) % 2 ! = 0 ) {
if ( str . size ( ) % 2 ! = 0 ) {
log_warning ( " The provided hex string size=%zd is not a multiple of 2 \n ." , str . size ( ) ) ;
log_warning ( " The provided hex string size=%zd is not a multiple of 2 ." , str . size ( ) ) ;
}
}
char cstr [ ] = " \0 \0 \0 " ;
char cstr [ ] = " \0 \0 \0 " ;
for ( uint32_t i = 0 ; i < str . size ( ) ; i + = 2 ) {
for ( uint32_t i = 0 ; i < str . size ( ) ; i + = 2 ) {
@ -1123,12 +1064,12 @@ SRSASN_CODE pack_length_prefix(bit_ref& bref,
bool is_aligned = false )
bool is_aligned = false )
{
{
if ( has_ext and ub = = std : : numeric_limits < uint32_t > : : max ( ) ) {
if ( has_ext and ub = = std : : numeric_limits < uint32_t > : : max ( ) ) {
log_error ( " has extension marker but it is an unbounded prefix size \n " ) ;
log_error ( " has extension marker but it is an unbounded prefix size " ) ;
return SRSASN_ERROR_ENCODE_FAIL ;
return SRSASN_ERROR_ENCODE_FAIL ;
}
}
bool within_bounds = len > = lb and len < = ub ;
bool within_bounds = len > = lb and len < = ub ;
if ( not within_bounds and not has_ext ) {
if ( not within_bounds and not has_ext ) {
log_error ( " bitstring length=%d is not within bounds [%d, %d] \n " , len , lb , ub ) ;
log_error ( " bitstring length=%d is not within bounds [%d, %d] " , len , lb , ub ) ;
return SRSASN_ERROR_ENCODE_FAIL ;
return SRSASN_ERROR_ENCODE_FAIL ;
}
}
@ -1159,7 +1100,7 @@ SRSASN_CODE pack_length_prefix(bit_ref& bref,
SRSASN_CODE pack_bitfield ( bit_ref & bref , const uint8_t * buf , uint32_t nbits , uint32_t lb , uint32_t ub , bool is_aligned )
SRSASN_CODE pack_bitfield ( bit_ref & bref , const uint8_t * buf , uint32_t nbits , uint32_t lb , uint32_t ub , bool is_aligned )
{
{
if ( nbits = = 0 ) {
if ( nbits = = 0 ) {
log_error ( " Invalid bitstring size=%d \n " , nbits ) ;
log_error ( " Invalid bitstring size=%d " , nbits ) ;
return SRSASN_ERROR_ENCODE_FAIL ;
return SRSASN_ERROR_ENCODE_FAIL ;
}
}
if ( is_aligned and ( lb ! = ub or ub > 16 ) ) {
if ( is_aligned and ( lb ! = ub or ub > 16 ) ) {
@ -1222,7 +1163,7 @@ SRSASN_CODE unpack_length_prefix(uint32_t& len, cbit_ref& bref, uint32_t lb, uin
SRSASN_CODE unpack_bitfield ( uint8_t * buf , cbit_ref & bref , uint32_t n , uint32_t lb , uint32_t ub , bool is_aligned )
SRSASN_CODE unpack_bitfield ( uint8_t * buf , cbit_ref & bref , uint32_t n , uint32_t lb , uint32_t ub , bool is_aligned )
{
{
if ( n > ASN_64K ) {
if ( n > ASN_64K ) {
log_error ( " bitstrings longer than 64K not supported \n " ) ;
log_error ( " bitstrings longer than 64K not supported " ) ;
return SRSASN_ERROR_DECODE_FAIL ;
return SRSASN_ERROR_DECODE_FAIL ;
}
}
if ( n = = 0 ) {
if ( n = = 0 ) {
@ -1245,7 +1186,7 @@ SRSASN_CODE unpack_bitfield(uint8_t* buf, cbit_ref& bref, uint32_t n, uint32_t l
void from_number ( uint8_t * ptr , uint64_t number , uint32_t nbits )
void from_number ( uint8_t * ptr , uint64_t number , uint32_t nbits )
{
{
if ( nbits > 64u ) {
if ( nbits > 64u ) {
log_error ( " bitstring of size=%d does not fit in an uint64_t \n " , nbits ) ;
log_error ( " bitstring of size=%d does not fit in an uint64_t " , nbits ) ;
return ;
return ;
}
}
uint32_t nof_bytes = ceil_frac ( nbits , 8u ) ;
uint32_t nof_bytes = ceil_frac ( nbits , 8u ) ;
@ -1270,7 +1211,7 @@ std::string to_string(const uint8_t* ptr, uint32_t nbits)
uint64_t to_number ( const uint8_t * ptr , uint32_t nbits )
uint64_t to_number ( const uint8_t * ptr , uint32_t nbits )
{
{
if ( nbits > 64u ) {
if ( nbits > 64u ) {
log_error ( " bitstring of size=%d does not fit in an uint64_t \n " , nbits ) ;
log_error ( " bitstring of size=%d does not fit in an uint64_t " , nbits ) ;
return 0 ;
return 0 ;
}
}
uint64_t val = 0 ;
uint64_t val = 0 ;
@ -1288,11 +1229,11 @@ uint64_t to_number(const uint8_t* ptr, uint32_t nbits)
* * * * * * * * * * * * * * * * * * * * */
* * * * * * * * * * * * * * * * * * * * */
void log_invalid_access_choice_id ( uint32_t val , uint32_t choice_id )
void log_invalid_access_choice_id ( uint32_t val , uint32_t choice_id )
{
{
log_error ( " The access choide id is invalid (%zd!=%zd) \n " , ( size_t ) val , ( size_t ) choice_id ) ;
log_error ( " The access choide id is invalid (%zd!=%zd) " , ( size_t ) val , ( size_t ) choice_id ) ;
}
}
void log_invalid_choice_id ( uint32_t val , const char * choice_type )
void log_invalid_choice_id ( uint32_t val , const char * choice_type )
{
{
log_error ( " Invalid choice id=%zd for choice type %s \n " , ( size_t ) val , choice_type ) ;
log_error ( " Invalid choice id=%zd for choice type %s " , ( size_t ) val , choice_type ) ;
}
}
/*********************
/*********************
@ -1329,7 +1270,7 @@ pack(bit_ref& bref, const std::string& s, size_t lb, size_t ub, size_t alb, size
if ( not within_limits ) {
if ( not within_limits ) {
// TODO: print error
// TODO: print error
// NOTE: This should be valid for exts
// NOTE: This should be valid for exts
log_error ( " The PrintableString size=%zd is not within the limits [%zd, %zd] \n " , s . size ( ) , alb , aub ) ;
log_error ( " The PrintableString size=%zd is not within the limits [%zd, %zd] " , s . size ( ) , alb , aub ) ;
return SRSASN_ERROR_ENCODE_FAIL ;
return SRSASN_ERROR_ENCODE_FAIL ;
}
}
size_t b = asn_string_utils : : get_nof_bits_per_char ( lb , ub , aligned ) ;
size_t b = asn_string_utils : : get_nof_bits_per_char ( lb , ub , aligned ) ;
@ -1366,7 +1307,7 @@ SRSASN_CODE unpack(std::string& s, cbit_ref& bref, size_t lb, size_t ub, size_t
bool is_ext ;
bool is_ext ;
HANDLE_CODE ( bref . unpack ( is_ext , 1 ) ) ;
HANDLE_CODE ( bref . unpack ( is_ext , 1 ) ) ;
if ( is_ext ) {
if ( is_ext ) {
log_error ( " Extension of PrintableString not supported \n " ) ;
log_error ( " Extension of PrintableString not supported " ) ;
return SRSASN_ERROR_DECODE_FAIL ;
return SRSASN_ERROR_DECODE_FAIL ;
}
}
}
}
@ -1489,7 +1430,7 @@ varlength_field_pack_guard::~varlength_field_pack_guard()
// check how many bytes were written in total
// check how many bytes were written in total
uint32_t nof_bytes = bref_tracker - > distance ( bref0 ) / ( uint32_t ) 8 ;
uint32_t nof_bytes = bref_tracker - > distance ( bref0 ) / ( uint32_t ) 8 ;
if ( nof_bytes > sizeof ( buffer ) ) {
if ( nof_bytes > sizeof ( buffer ) ) {
log_error ( " The packed variable sized field is too long for the reserved buffer (%zd > %zd) \n " ,
log_error ( " The packed variable sized field is too long for the reserved buffer (%zd > %zd) " ,
( size_t ) nof_bytes ,
( size_t ) nof_bytes ,
sizeof ( buffer ) ) ;
sizeof ( buffer ) ) ;
}
}