ENGINE-109: trustwords for id pair function added. ENGINE-109
authorKrista Grothoff <krista@pep-project.org>
Tue, 18 Oct 2016 00:32:01 +0200
branchENGINE-109
changeset 12934f598c491fdf
parent 1292 c310459ec5d3
child 1294 a171d8b09669
ENGINE-109: trustwords for id pair function added.
src/pEpEngine.c
src/pEpEngine.h
test/trustwords_test.cc
     1.1 --- a/src/pEpEngine.c	Mon Oct 17 22:38:01 2016 +0200
     1.2 +++ b/src/pEpEngine.c	Tue Oct 18 00:32:01 2016 +0200
     1.3 @@ -879,7 +879,7 @@
     1.4  
     1.5  DYNAMIC_API PEP_STATUS trustwords_for_id_pair(
     1.6      PEP_SESSION session, pEp_identity* id1, pEp_identity* id2,
     1.7 -    char* lang, char **words, size_t *wsize, int max_words_per_id
     1.8 +    const char* lang, char **words, size_t *wsize, int max_words_per_id
     1.9  )
    1.10  {
    1.11      assert(session);
    1.12 @@ -910,20 +910,47 @@
    1.13      if (source1 > source2) {
    1.14          status = trustwords(session, source2, lang, &first_set, &first_wsize, max_words_per_id);
    1.15          if (status != PEP_STATUS_OK)
    1.16 -            return status;
    1.17 +            goto error_release;
    1.18          status = trustwords(session, source1, lang, &second_set, &second_wsize, max_words_per_id); 
    1.19          if (status != PEP_STATUS_OK)
    1.20 -            return status;
    1.21 +            goto error_release;
    1.22      }
    1.23      else {
    1.24          status = trustwords(session, source1, lang, &first_set, &first_wsize, max_words_per_id);
    1.25          if (status != PEP_STATUS_OK)
    1.26 -            return status;
    1.27 +            goto error_release;
    1.28          status = trustwords(session, source2, lang, &second_set, &second_wsize, max_words_per_id); 
    1.29          if (status != PEP_STATUS_OK)
    1.30 -            return status;
    1.31 +            goto error_release;
    1.32 +    }
    1.33 +    size_t _wsize = first_wsize + second_wsize;
    1.34 +    
    1.35 +    char* _retstr = calloc(1, _wsize + 1);
    1.36 +
    1.37 +    size_t len = strlcpy(_retstr, first_set, _wsize);
    1.38 +    if (len >= _wsize) {
    1.39 +        status = PEP_UNKNOWN_ERROR;
    1.40 +        goto error_release;
    1.41 +    }
    1.42 +    strlcat(_retstr, second_set, _wsize);
    1.43 +    if (len >= _wsize){
    1.44 +        status = PEP_UNKNOWN_ERROR;
    1.45 +        goto error_release;
    1.46      }
    1.47      
    1.48 +    *words = _retstr;
    1.49 +    *wsize = _wsize;
    1.50 +    status = PEP_STATUS_OK;
    1.51 +    
    1.52 +    goto the_end;
    1.53 +    
    1.54 +error_release:
    1.55 +    free(_retstr);
    1.56 +    
    1.57 +the_end:
    1.58 +    free(first_set);
    1.59 +    free(second_set);
    1.60 +    return status;
    1.61  }
    1.62  
    1.63  
     2.1 --- a/src/pEpEngine.h	Mon Oct 17 22:38:01 2016 +0200
     2.2 +++ b/src/pEpEngine.h	Tue Oct 18 00:32:01 2016 +0200
     2.3 @@ -530,6 +530,44 @@
     2.4          PEP_SESSION session, const pEp_identity *identity
     2.5      );
     2.6  
     2.7 +// trustwords_for_id_pair() - get full trustwords string for a *pair* of identities
     2.8 +//
     2.9 +//    parameters:
    2.10 +//        session (in)        session handle
    2.11 +//        id1 (in)            identity of first party in communication - fpr can't be NULL  
    2.12 +//        id2 (in)            identity of second party in communication - fpr can't be NULL
    2.13 +//        lang (in)           C string with ISO 639-1 language code
    2.14 +//        words (out)         pointer to C string with all trustwords UTF-8 encoded,
    2.15 +//                            separated by a blank each
    2.16 +//                            NULL if language is not supported or trustword
    2.17 +//                            wordlist is damaged or unavailable
    2.18 +//        wsize (out)         length of full trustwords string
    2.19 +//        max_words_per_id (in) generate at most max_words_per_id *per input identity* (output
    2.20 +//                              string can have 2 times this value)
    2.21 +//                              if max_words_per_id == 0 there is no such limit
    2.22 +//
    2.23 +//    return value:
    2.24 +//        PEP_STATUS_OK            trustwords retrieved
    2.25 +//        PEP_OUT_OF_MEMORY        out of memory
    2.26 +//        PEP_TRUSTWORD_NOT_FOUND  at least one trustword not found
    2.27 +//
    2.28 +//    caveat:
    2.29 +//        the word pointer goes to the ownership of the caller
    2.30 +//        the caller is responsible to free() it (on Windoze use pEp_free())
    2.31 +//
    2.32 +//  Warning from trustwords(), as this function uses trustwords() as an underlying function:
    2.33 +//  DON'T USE THIS FUNCTION FROM HIGH LEVEL LANGUAGES!
    2.34 +//
    2.35 +//  Better implement a simple one in the adapter yourself using trustword(), and
    2.36 +//  return a list of trustwords.
    2.37 +//  This function is provided for being used by C and C++ programs only.
    2.38 +
    2.39 +DYNAMIC_API PEP_STATUS trustwords_for_id_pair(
    2.40 +    PEP_SESSION session, pEp_identity* id1, pEp_identity* id2,
    2.41 +    const char* lang, char **words, size_t *wsize, int max_words_per_id
    2.42 +);
    2.43 +
    2.44 +
    2.45  // set_device_group() - update own person's device group
    2.46  //
    2.47  //    parameters:
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/trustwords_test.cc	Tue Oct 18 00:32:01 2016 +0200
     3.3 @@ -0,0 +1,67 @@
     3.4 +#include <iostream>
     3.5 +#include <string>
     3.6 +#include <assert.h>
     3.7 +#include "pEpEngine.h"
     3.8 +
     3.9 +using namespace std;
    3.10 +
    3.11 +int main() {
    3.12 +    cout << "\n*** trustwords_for_id_pair test ***\n\n";
    3.13 +
    3.14 +    PEP_SESSION session;
    3.15 +    
    3.16 +    cout << "calling init()\n";
    3.17 +    PEP_STATUS status1 = init(&session);   
    3.18 +    assert(status1 == PEP_STATUS_OK);
    3.19 +    assert(session);
    3.20 +    cout << "init() completed.\n";
    3.21 +        
    3.22 +    pEp_identity* identity1  = new_identity(
    3.23 +        "leon.schumacher@digitalekho.com",
    3.24 +        "8BD08954C74D830EEFFB5DEB2682A17F7C87F73D",
    3.25 +        "23",
    3.26 +        "Leon Schumacher");
    3.27 +    
    3.28 +    pEp_identity* identity2 = new_identity(
    3.29 +        "krista@kgrothoff.org",
    3.30 +        "62D4932086185C15917B72D30571AFBCA5493553",
    3.31 +        "blargh",
    3.32 +        "Krista Grothoff");
    3.33 +    
    3.34 +    string fingerprint1 = identity1->fpr;
    3.35 +    string fingerprint2 = identity2->fpr;
    3.36 +    char* words1;
    3.37 +    char* words2;
    3.38 +    char* full_wordlist;
    3.39 +    size_t wsize1;
    3.40 +    size_t wsize2;
    3.41 +    size_t wsize_full;
    3.42 +    
    3.43 +    cout << "\nfinding German trustwords for " << fingerprint1 << "...\n";
    3.44 +    trustwords(session, fingerprint1.c_str(), "de", &words1, &wsize1, 5);
    3.45 +    assert(words1);
    3.46 +    cout << words1 << "\n";
    3.47 +
    3.48 +    cout << "\nfinding German trustwords for " << fingerprint2 << "...\n";
    3.49 +    trustwords(session, fingerprint2.c_str(), "de", &words2, &wsize2, 5);
    3.50 +    assert(words2);
    3.51 +    cout << words2 << "\n";
    3.52 +
    3.53 +    cout << "\nfinding German trustwords for " << identity1->address << " and " << identity2->address << "...\n";
    3.54 +    trustwords_for_id_pair(session, identity1, identity2, "de", &full_wordlist, &wsize_full, 5);
    3.55 +    assert(full_wordlist);
    3.56 +    cout << full_wordlist << "\n";
    3.57 +    
    3.58 +    
    3.59 +    pEp_free(words1);
    3.60 +    pEp_free(words2);
    3.61 +    pEp_free(full_wordlist);
    3.62 +    
    3.63 +    
    3.64 +    free(identity1);
    3.65 +    free(identity2);
    3.66 +    cout << "calling release()\n";
    3.67 +    release(session);
    3.68 +    return 0;
    3.69 +}
    3.70 +