src/pEp_internal.h
changeset 1746 498b0671ff4e
parent 1648 158e65c83311
child 1752 6991834e731a
child 1753 01faac16cec6
child 1816 dc9bc5fa3f18
     1.1 --- a/src/pEp_internal.h	Tue May 02 09:32:38 2017 +0200
     1.2 +++ b/src/pEp_internal.h	Wed May 03 18:11:49 2017 +0200
     1.3 @@ -189,7 +189,109 @@
     1.4  }
     1.5  #endif
     1.6  
     1.7 +typedef enum _normalize_hex_rest_t {
     1.8 +    accept_hex,
     1.9 +    ignore_hex,
    1.10 +    reject_hex
    1.11 +} normalize_hex_res_t;
    1.12 +
    1.13 +static inline normalize_hex_res_t _normalize_hex(char *hex) 
    1.14 +{
    1.15 +    if (*hex >= '0' && *hex <= '9')
    1.16 +        return accept_hex;
    1.17 +
    1.18 +    if (*hex >= 'A' && *hex <= 'F') {
    1.19 +        *hex += 'a' - 'A';
    1.20 +        return accept_hex;
    1.21 +    }
    1.22 +
    1.23 +    if (*hex >= 'a' && *hex <= 'f') 
    1.24 +        return accept_hex;
    1.25 +
    1.26 +    if (*hex == ' ') 
    1.27 +        return ignore_hex;
    1.28 +
    1.29 +    return reject_hex;
    1.30 +}
    1.31 +
    1.32  // Space tolerant and case insensitive fingerprint string compare
    1.33 +static inline PEP_STATUS _compare_fprs(
    1.34 +        const char* fpra,
    1.35 +        size_t fpras,
    1.36 +        const char* fprb,
    1.37 +        size_t fprbs,
    1.38 +        int* comparison)
    1.39 +{
    1.40 +
    1.41 +    size_t ai = 0;
    1.42 +    size_t bi = 0;
    1.43 +    size_t significant = 0;
    1.44 +    int _comparison = 0;
    1.45 +    const int _FULL_FINGERPRINT_LENGTH = 40;
    1.46 +   
    1.47 +    // First compare every non-ignored chars until an end is reached
    1.48 +    while(ai < fpras && bi < fprbs)
    1.49 +    {
    1.50 +        char fprac = fpra[ai];
    1.51 +        char fprbc = fprb[bi];
    1.52 +        normalize_hex_res_t fprah = _normalize_hex(&fprac);
    1.53 +        normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
    1.54 +
    1.55 +        if(fprah == reject_hex || fprbh == reject_hex)
    1.56 +            return PEP_ILLEGAL_VALUE;
    1.57 +
    1.58 +        if ( fprah == ignore_hex )
    1.59 +        {
    1.60 +            ai++;
    1.61 +        }
    1.62 +        else if ( fprbh == ignore_hex )
    1.63 +        {
    1.64 +            bi++;
    1.65 +        }
    1.66 +        else
    1.67 +        {
    1.68 +            if(fprac != fprbc && _comparison == 0 )
    1.69 +            {
    1.70 +                _comparison = fprac > fprbc ? 1 : -1;
    1.71 +            }
    1.72 +
    1.73 +            significant++;
    1.74 +            ai++;
    1.75 +            bi++;
    1.76 +
    1.77 +        } 
    1.78 +    }
    1.79 +
    1.80 +    // Bail out if we didn't got enough significnt chars
    1.81 +    if (significant != _FULL_FINGERPRINT_LENGTH )
    1.82 +        return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
    1.83 +
    1.84 +    // Then purge remaining chars, all must be ignored chars
    1.85 +    while ( ai < fpras )
    1.86 +    {
    1.87 +        char fprac = fpra[ai];
    1.88 +        normalize_hex_res_t fprah = _normalize_hex(&fprac);
    1.89 +        if( fprah == reject_hex )
    1.90 +            return PEP_ILLEGAL_VALUE;
    1.91 +        if ( fprah != ignore_hex )
    1.92 +            return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
    1.93 +        ai++;
    1.94 +    }
    1.95 +    while ( bi < fprbs )
    1.96 +    {
    1.97 +        char fprbc = fprb[bi];
    1.98 +        normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
    1.99 +        if( fprbh == reject_hex )
   1.100 +            return PEP_ILLEGAL_VALUE;
   1.101 +        if ( fprbh != ignore_hex )
   1.102 +            return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   1.103 +        bi++;
   1.104 +    }
   1.105 +
   1.106 +    *comparison = _comparison;
   1.107 +    return PEP_STATUS_OK;
   1.108 +}
   1.109 +
   1.110  static inline int _same_fpr(
   1.111          const char* fpra,
   1.112          size_t fpras,
   1.113 @@ -197,35 +299,10 @@
   1.114          size_t fprbs
   1.115      )
   1.116  {
   1.117 -    size_t ai = 0;
   1.118 -    size_t bi = 0;
   1.119 -    
   1.120 -    do
   1.121 -    {
   1.122 -        if(fpra[ai] == 0 || fprb[bi] == 0)
   1.123 -        {
   1.124 -            return 0;
   1.125 -        }
   1.126 -        else if(fpra[ai] == ' ')
   1.127 -        {
   1.128 -            ai++;
   1.129 -        }
   1.130 -        else if(fprb[bi] == ' ')
   1.131 -        {
   1.132 -            bi++;
   1.133 -        }
   1.134 -        else if(toupper(fpra[ai]) == toupper(fprb[bi]))
   1.135 -        {
   1.136 -            ai++;
   1.137 -            bi++;
   1.138 -        }
   1.139 -        else
   1.140 -        {
   1.141 -            return 0;
   1.142 -        }
   1.143 -        
   1.144 -    }
   1.145 -    while(ai < fpras && bi < fprbs);
   1.146 -    
   1.147 -    return ai == fpras && bi == fprbs;
   1.148 +    // illegal values are ignored, and considered not same.
   1.149 +    int comparison = 1;
   1.150 +
   1.151 +    _compare_fprs(fpra, fpras, fprb, fprbs, &comparison);
   1.152 +
   1.153 +    return comparison == 0;
   1.154  }