adding get_trustwords_for_fprs() sync
authorVolker Birk <vb@pep-project.org>
Sat, 25 May 2019 13:41:57 +0200
branchsync
changeset 3743a66f640552a4
parent 3739 523fb9710660
child 3744 2333b0b01c8c
adding get_trustwords_for_fprs()
src/message_api.c
src/message_api.h
     1.1 --- a/src/message_api.c	Tue May 21 07:04:51 2019 +0200
     1.2 +++ b/src/message_api.c	Sat May 25 13:41:57 2019 +0200
     1.3 @@ -4207,7 +4207,7 @@
     1.4      return num_to_asciihex(xor_num);
     1.5  }
     1.6  
     1.7 -static char* skip_separators(char* current, char* begin) {
     1.8 +static const char* skip_separators(const char* current, const char* begin) {
     1.9      while (current >= begin) {
    1.10          /* .:,;-_ ' ' - [2c-2e] [3a-3b] [20] [5f] */
    1.11          char check_char = *current;
    1.12 @@ -4245,71 +4245,73 @@
    1.13  }
    1.14  
    1.15  DYNAMIC_API PEP_STATUS get_trustwords(
    1.16 -    PEP_SESSION session, const pEp_identity* id1, const pEp_identity* id2,
    1.17 -    const char* lang, char **words, size_t *wsize, bool full
    1.18 -)
    1.19 +        PEP_SESSION session, const pEp_identity* id1, const pEp_identity* id2,
    1.20 +        const char* lang, char **words, size_t *wsize, bool full
    1.21 +    )
    1.22  {
    1.23 -    assert(session);
    1.24 -    assert(id1);
    1.25 -    assert(id2);
    1.26 -    assert(id1->fpr);
    1.27 -    assert(id2->fpr);
    1.28 -    assert(words);
    1.29 -    assert(wsize);
    1.30 -
    1.31 -    int SHORT_NUM_TWORDS = 5; 
    1.32 -    
    1.33 +    assert(session && id1 && id1->fpr && id2 && id2->fpr&& lang && words &&
    1.34 +            wsize);
    1.35 +    if (!(session && id1 && id1->fpr && id2 && id2->fpr&& lang && words &&
    1.36 +                wsize))
    1.37 +        return PEP_ILLEGAL_VALUE;
    1.38 +
    1.39 +    return get_trustwords_for_fprs(session, id1->fpr, id2->fpr, lang, words,
    1.40 +            wsize, full);
    1.41 +}
    1.42 +
    1.43 +DYNAMIC_API PEP_STATUS get_trustwords_for_fprs(
    1.44 +        PEP_SESSION session, const char* fpr1, const char* fpr2,
    1.45 +        const char* lang, char **words, size_t *wsize, bool full
    1.46 +    )
    1.47 +{
    1.48 +    assert(session && fpr1 && fpr2 && words && wsize);
    1.49 +    if (!(session && fpr1 && fpr2 && words && wsize))
    1.50 +        return PEP_ILLEGAL_VALUE;
    1.51 +
    1.52 +    const int SHORT_NUM_TWORDS = 5; 
    1.53      PEP_STATUS status = PEP_STATUS_OK;
    1.54      
    1.55 -    if (!(session && id1 && id2 && words && wsize) ||
    1.56 -        !(id1->fpr) || (!id2->fpr))
    1.57 -        return PEP_ILLEGAL_VALUE;
    1.58 -
    1.59 -    char *source1 = id1->fpr;
    1.60 -    char *source2 = id2->fpr;
    1.61 -
    1.62 -    int source1_len = strlen(source1);
    1.63 -    int source2_len = strlen(source2);
    1.64 -    int max_len;
    1.65 -        
    1.66      *words = NULL;    
    1.67      *wsize = 0;
    1.68  
    1.69 -    max_len = (source1_len > source2_len ? source1_len : source2_len);
    1.70 +    int fpr1_len = strlen(fpr1);
    1.71 +    int fpr2_len = strlen(fpr2);
    1.72 +        
    1.73 +    int max_len = (fpr1_len > fpr2_len ? fpr1_len : fpr2_len);
    1.74      
    1.75      char* XORed_fpr = (char*)(calloc(max_len + 1, 1));
    1.76      *(XORed_fpr + max_len) = '\0';
    1.77      char* result_curr = XORed_fpr + max_len - 1;
    1.78 -    char* source1_curr = source1 + source1_len - 1;
    1.79 -    char* source2_curr = source2 + source2_len - 1;
    1.80 -
    1.81 -    while (source1 <= source1_curr && source2 <= source2_curr) {
    1.82 -        source1_curr = skip_separators(source1_curr, source1);
    1.83 -        source2_curr = skip_separators(source2_curr, source2);
    1.84 +    const char* fpr1_curr = fpr1 + fpr1_len - 1;
    1.85 +    const char* fpr2_curr = fpr2 + fpr2_len - 1;
    1.86 +
    1.87 +    while (fpr1 <= fpr1_curr && fpr2 <= fpr2_curr) {
    1.88 +        fpr1_curr = skip_separators(fpr1_curr, fpr1);
    1.89 +        fpr2_curr = skip_separators(fpr2_curr, fpr2);
    1.90          
    1.91 -        if (source1_curr < source1 || source2_curr < source2)
    1.92 +        if (fpr1_curr < fpr1 || fpr2_curr < fpr2)
    1.93              break;
    1.94              
    1.95 -        char xor_hex = xor_hex_chars(*source1_curr, *source2_curr);
    1.96 +        char xor_hex = xor_hex_chars(*fpr1_curr, *fpr2_curr);
    1.97          if (xor_hex == '\0') {
    1.98              status = PEP_ILLEGAL_VALUE;
    1.99              goto error_release;
   1.100          }
   1.101          
   1.102          *result_curr = xor_hex;
   1.103 -        result_curr--; source1_curr--; source2_curr--;
   1.104 +        result_curr--; fpr1_curr--; fpr2_curr--;
   1.105      }
   1.106  
   1.107 -    char* remainder_start = NULL;
   1.108 -    char* remainder_curr = NULL;
   1.109 +    const char* remainder_start = NULL;
   1.110 +    const char* remainder_curr = NULL;
   1.111      
   1.112 -    if (source1 <= source1_curr) {
   1.113 -        remainder_start = source1;
   1.114 -        remainder_curr = source1_curr;
   1.115 +    if (fpr1 <= fpr1_curr) {
   1.116 +        remainder_start = fpr1;
   1.117 +        remainder_curr = fpr1_curr;
   1.118      }
   1.119 -    else if (source2 <= source2_curr) {
   1.120 -        remainder_start = source2;
   1.121 -        remainder_curr = source2_curr;
   1.122 +    else if (fpr2 <= fpr2_curr) {
   1.123 +        remainder_start = fpr2;
   1.124 +        remainder_curr = fpr2_curr;
   1.125      }
   1.126      if (remainder_curr) {
   1.127          while (remainder_start <= remainder_curr) {
     2.1 --- a/src/message_api.h	Tue May 21 07:04:51 2019 +0200
     2.2 +++ b/src/message_api.h	Sat May 25 13:41:57 2019 +0200
     2.3 @@ -388,9 +388,9 @@
     2.4  //        the caller is responsible to free() it (on Windoze use pEp_free())
     2.5  //
     2.6  DYNAMIC_API PEP_STATUS get_trustwords(
     2.7 -    PEP_SESSION session, const pEp_identity* id1, const pEp_identity* id2,
     2.8 -    const char* lang, char **words, size_t *wsize, bool full
     2.9 -);
    2.10 +        PEP_SESSION session, const pEp_identity* id1, const pEp_identity* id2,
    2.11 +        const char* lang, char **words, size_t *wsize, bool full
    2.12 +    );
    2.13  
    2.14  
    2.15  // get_message_trustwords() - get full trustwords string for message sender and reciever identities 
    2.16 @@ -421,12 +421,42 @@
    2.17  //        the caller is responsible to free() it (on Windoze use pEp_free())
    2.18  //
    2.19  DYNAMIC_API PEP_STATUS get_message_trustwords(
    2.20 -    PEP_SESSION session, 
    2.21 -    message *msg,
    2.22 -    stringlist_t *keylist,
    2.23 -    pEp_identity* received_by,
    2.24 -    const char* lang, char **words, bool full
    2.25 -);
    2.26 +        PEP_SESSION session, 
    2.27 +        message *msg,
    2.28 +        stringlist_t *keylist,
    2.29 +        pEp_identity* received_by,
    2.30 +        const char* lang, char **words, bool full
    2.31 +    );
    2.32 +
    2.33 +// get_trustwords_for_fprs() - get full trustwords string for a pair of fingerprints
    2.34 +//
    2.35 +//    parameters:
    2.36 +//        session (in)        session handle
    2.37 +//        fpr1 (in)           fingerprint 1
    2.38 +//        fpr2 (in)           fingerprint 2
    2.39 +//        lang (in)           C string with ISO 639-1 language code
    2.40 +//        words (out)         pointer to C string with all trustwords UTF-8 encoded,
    2.41 +//                            separated by a blank each
    2.42 +//                            NULL if language is not supported or trustword
    2.43 +//                            wordlist is damaged or unavailable
    2.44 +//        wsize (out)         length of full trustwords string
    2.45 +//        full (in)           if true, generate ALL trustwords for these identities.
    2.46 +//                            else, generate a fixed-size subset. (TODO: fixed-minimum-entropy
    2.47 +//                            subset in next version)
    2.48 +//
    2.49 +//    return value:
    2.50 +//        PEP_STATUS_OK            trustwords retrieved
    2.51 +//        PEP_OUT_OF_MEMORY        out of memory
    2.52 +//        PEP_TRUSTWORD_NOT_FOUND  at least one trustword not found
    2.53 +//
    2.54 +//    caveat:
    2.55 +//        the word pointer goes to the ownership of the caller
    2.56 +//        the caller is responsible to free() it (on Windoze use pEp_free())
    2.57 +//
    2.58 +DYNAMIC_API PEP_STATUS get_trustwords_for_fprs(
    2.59 +        PEP_SESSION session, const char* fpr1, const char* fpr2,
    2.60 +        const char* lang, char **words, size_t *wsize, bool full
    2.61 +    );
    2.62  
    2.63  // re_evaluate_message_rating() - re-evaluate already decrypted message rating
    2.64  //