LCOV - code coverage report
Current view: top level - lang/cpp/src - encryptionresult.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 19 69 27.5 %
Date: 2018-11-15 08:49:49 Functions: 7 20 35.0 %

          Line data    Source code
       1             : /*
       2             :   encryptionresult.cpp - wraps a gpgme verify result
       3             :   Copyright (C) 2004 Klarälvdalens Datakonsult AB
       4             :   2016 Bundesamt für Sicherheit in der Informationstechnik
       5             :   Software engineering by Intevation GmbH
       6             : 
       7             :   This file is part of GPGME++.
       8             : 
       9             :   GPGME++ is free software; you can redistribute it and/or
      10             :   modify it under the terms of the GNU Library General Public
      11             :   License as published by the Free Software Foundation; either
      12             :   version 2 of the License, or (at your option) any later version.
      13             : 
      14             :   GPGME++ is distributed in the hope that it will be useful,
      15             :   but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :   GNU Library General Public License for more details.
      18             : 
      19             :   You should have received a copy of the GNU Library General Public License
      20             :   along with GPGME++; see the file COPYING.LIB.  If not, write to the
      21             :   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
      22             :   Boston, MA 02110-1301, USA.
      23             : */
      24             : 
      25             : #ifdef HAVE_CONFIG_H
      26             :  #include "config.h"
      27             : #endif
      28             : 
      29             : #include <encryptionresult.h>
      30             : #include "result_p.h"
      31             : #include "util.h"
      32             : 
      33             : #include <gpgme.h>
      34             : 
      35             : #include <cstring>
      36             : #include <cstdlib>
      37             : #include <istream>
      38             : #include <algorithm>
      39             : #include <iterator>
      40             : 
      41             : #include <string.h>
      42             : 
      43             : class GpgME::EncryptionResult::Private
      44             : {
      45             : public:
      46           4 :     explicit Private(const gpgme_encrypt_result_t r)
      47           4 :     {
      48           4 :         if (!r) {
      49           0 :             return;
      50             :         }
      51           4 :         for (gpgme_invalid_key_t ik = r->invalid_recipients ; ik ; ik = ik->next) {
      52           0 :             gpgme_invalid_key_t copy = new _gpgme_invalid_key(*ik);
      53           0 :             if (ik->fpr) {
      54           0 :                 copy->fpr = strdup(ik->fpr);
      55             :             }
      56           0 :             copy->next = 0;
      57           0 :             invalid.push_back(copy);
      58             :         }
      59             :     }
      60           4 :     ~Private()
      61           4 :     {
      62           4 :         for (std::vector<gpgme_invalid_key_t>::iterator it = invalid.begin() ; it != invalid.end() ; ++it) {
      63           0 :             std::free((*it)->fpr);
      64           0 :             delete *it; *it = 0;
      65             :         }
      66           4 :     }
      67             : 
      68             :     std::vector<gpgme_invalid_key_t> invalid;
      69             : };
      70             : 
      71           0 : GpgME::EncryptionResult::EncryptionResult(gpgme_ctx_t ctx, int error)
      72           0 :     : GpgME::Result(error), d()
      73             : {
      74           0 :     init(ctx);
      75           0 : }
      76             : 
      77           4 : GpgME::EncryptionResult::EncryptionResult(gpgme_ctx_t ctx, const Error &error)
      78           4 :     : GpgME::Result(error), d()
      79             : {
      80           4 :     init(ctx);
      81           4 : }
      82             : 
      83           4 : void GpgME::EncryptionResult::init(gpgme_ctx_t ctx)
      84             : {
      85           4 :     if (!ctx) {
      86           0 :         return;
      87             :     }
      88           4 :     gpgme_encrypt_result_t res = gpgme_op_encrypt_result(ctx);
      89           4 :     if (!res) {
      90           0 :         return;
      91             :     }
      92           4 :     d.reset(new Private(res));
      93             : }
      94             : 
      95           8 : make_standard_stuff(EncryptionResult)
      96             : 
      97           0 : unsigned int GpgME::EncryptionResult::numInvalidRecipients() const
      98             : {
      99           0 :     return d ? d->invalid.size() : 0 ;
     100             : }
     101             : 
     102           0 : GpgME::InvalidRecipient GpgME::EncryptionResult::invalidEncryptionKey(unsigned int idx) const
     103             : {
     104           0 :     return InvalidRecipient(d, idx);
     105             : }
     106             : 
     107           0 : std::vector<GpgME::InvalidRecipient> GpgME::EncryptionResult::invalidEncryptionKeys() const
     108             : {
     109           0 :     if (!d) {
     110           0 :         return std::vector<GpgME::InvalidRecipient>();
     111             :     }
     112           0 :     std::vector<GpgME::InvalidRecipient> result;
     113           0 :     result.reserve(d->invalid.size());
     114           0 :     for (unsigned int i = 0 ; i < d->invalid.size() ; ++i) {
     115           0 :         result.push_back(InvalidRecipient(d, i));
     116             :     }
     117           0 :     return result;
     118             : }
     119             : 
     120           0 : GpgME::InvalidRecipient::InvalidRecipient(const std::shared_ptr<EncryptionResult::Private> &parent, unsigned int i)
     121           0 :     : d(parent), idx(i)
     122             : {
     123             : 
     124           0 : }
     125             : 
     126           0 : GpgME::InvalidRecipient::InvalidRecipient() : d(), idx(0) {}
     127             : 
     128           0 : bool GpgME::InvalidRecipient::isNull() const
     129             : {
     130           0 :     return !d || idx >= d->invalid.size() ;
     131             : }
     132             : 
     133           0 : const char *GpgME::InvalidRecipient::fingerprint() const
     134             : {
     135           0 :     return isNull() ? 0 : d->invalid[idx]->fpr ;
     136             : }
     137             : 
     138           0 : GpgME::Error GpgME::InvalidRecipient::reason() const
     139             : {
     140           0 :     return Error(isNull() ? 0 : d->invalid[idx]->reason);
     141             : }
     142             : 
     143           0 : std::ostream &GpgME::operator<<(std::ostream &os, const EncryptionResult &result)
     144             : {
     145           0 :     os << "GpgME::EncryptionResult(";
     146           0 :     if (!result.isNull()) {
     147           0 :         os << "\n error:        " << result.error()
     148           0 :            << "\n invalid recipients:\n";
     149           0 :         const std::vector<InvalidRecipient> ir = result.invalidEncryptionKeys();
     150             :         std::copy(ir.begin(), ir.end(),
     151           0 :                   std::ostream_iterator<InvalidRecipient>(os, "\n"));
     152             :     }
     153           0 :     return os << ')';
     154             : }
     155             : 
     156           0 : std::ostream &GpgME::operator<<(std::ostream &os, const InvalidRecipient &ir)
     157             : {
     158           0 :     os << "GpgME::InvalidRecipient(";
     159           0 :     if (!ir.isNull()) {
     160             :         os << "\n fingerprint: " << protect(ir.fingerprint())
     161           0 :            << "\n reason:      " << ir.reason()
     162           0 :            << '\n';
     163             :     }
     164           0 :     return os << ')';
     165          24 : }

Generated by: LCOV version 1.13