adding GnuPG _version_test
authorVolker Birk <vb@pep.foundation>
Fri, 19 Jan 2018 16:38:33 +0100
changeset 24272a3bdb936c1a
parent 2424 5fb4f5f2a5c2
child 2428 52bd368c51aa
adding GnuPG _version_test
src/pgp_gpg.c
     1.1 --- a/src/pgp_gpg.c	Fri Jan 19 16:05:07 2018 +0100
     1.2 +++ b/src/pgp_gpg.c	Fri Jan 19 16:38:33 2018 +0100
     1.3 @@ -174,6 +174,55 @@
     1.4      return false;
     1.5  }
     1.6  
     1.7 +static PEP_STATUS _version_test(const char *s)
     1.8 +{
     1.9 +    char *_s = strdup(s);
    1.10 +    if (!_s)
    1.11 +        return PEP_OUT_OF_MEMORY;
    1.12 +
    1.13 +    int major;
    1.14 +    int minor;
    1.15 +    int revision;
    1.16 +
    1.17 +    char *lasts = NULL;
    1.18 +    char *p = strtok_r(_s, ".", &lasts);
    1.19 +    if (!p)
    1.20 +        goto unsupported;
    1.21 +    else
    1.22 +        major = atoi(p);
    1.23 +
    1.24 +    p = strtok_r(NULL, ".", &lasts);
    1.25 +    if (!p)
    1.26 +        goto unsupported;
    1.27 +    else
    1.28 +        minor = atoi(p);
    1.29 +
    1.30 +    p = strtok_r(NULL, ".", &lasts);
    1.31 +    if (!p)
    1.32 +        goto unsupported;
    1.33 +    else
    1.34 +        revision = atoi(p);
    1.35 +
    1.36 +    free(_s);
    1.37 +    _s = NULL;
    1.38 +
    1.39 +    if (major > 2)
    1.40 +        return PEP_STATUS_OK;
    1.41 +
    1.42 +    if (major == 2 && minor > 1)
    1.43 +        return PEP_STATUS_OK;
    1.44 +
    1.45 +    if (major == 2 && minor == 0 && revision == 30)
    1.46 +        return PEP_STATUS_OK;
    1.47 +
    1.48 +    if (major == 2 && minor == 1 && revision >= 17)
    1.49 +        return PEP_STATUS_OK;
    1.50 +
    1.51 +unsupported:
    1.52 +    free(_s);
    1.53 +    return PEP_UNSUPPORTED_GPG_VERSION;
    1.54 +}
    1.55 +
    1.56  PEP_STATUS pgp_init(PEP_SESSION session, bool in_first)
    1.57  {
    1.58      PEP_STATUS status = PEP_STATUS_OK;
    1.59 @@ -238,6 +287,24 @@
    1.60  
    1.61          memset(&gpg, 0, sizeof(struct gpg_s));
    1.62  
    1.63 +        gpg.gpgme_get_engine_info
    1.64 +            = (gpgme_get_engine_info_t) (intptr_t) dlsym(gpgme,
    1.65 +            "gpgme_get_engine_info");
    1.66 +        assert(gpg.gpgme_get_engine_info);
    1.67 +
    1.68 +        gpgme_engine_info_t info;
    1.69 +        int err = gpg.gpgme_get_engine_info(&info);
    1.70 +        assert(err == GPG_ERR_NO_ERROR);
    1.71 +        if (err != GPG_ERR_NO_ERROR)
    1.72 +            return PEP_OUT_OF_MEMORY;
    1.73 +
    1.74 +        if (!info->version)
    1.75 +            return PEP_CANNOT_DETERMINE_GPG_VERSION;
    1.76 +
    1.77 +        status = _version_test(info->version);
    1.78 +        if (status != PEP_STATUS_OK)
    1.79 +            return status;
    1.80 +
    1.81          gpg.gpgme_set_locale
    1.82              = (gpgme_set_locale_t) (intptr_t) dlsym(gpgme,
    1.83              "gpgme_set_locale");
    1.84 @@ -256,11 +323,6 @@
    1.85              = (gpgme_release_t) (intptr_t) dlsym(gpgme, "gpgme_release");
    1.86          assert(gpg.gpgme_release);
    1.87  
    1.88 -        gpg.gpgme_get_engine_info
    1.89 -            = (gpgme_get_engine_info_t) (intptr_t) dlsym(gpgme,
    1.90 -            "gpgme_get_engine_info");
    1.91 -        assert(gpg.gpgme_get_engine_info);
    1.92 -
    1.93          gpg.gpgme_set_protocol
    1.94              = (gpgme_set_protocol_t) (intptr_t) dlsym(gpgme,
    1.95              "gpgme_set_protocol");
    1.96 @@ -473,15 +535,6 @@
    1.97  
    1.98      gpg.gpgme_set_armor(session->ctx, 1);
    1.99  
   1.100 -    gpgme_engine_info_t info;
   1.101 -    int err = gpg.gpgme_get_engine_info(&info);
   1.102 -    assert(err == GPG_ERR_NO_ERROR);
   1.103 -    if (err != GPG_ERR_NO_ERROR)
   1.104 -        return PEP_OUT_OF_MEMORY;
   1.105 -
   1.106 -    if (!info->version)
   1.107 -        return PEP_CANNOT_DETERMINE_GPG_VERSION;
   1.108 -
   1.109      return PEP_STATUS_OK;
   1.110  
   1.111  pep_error: