Blame lang/cpp/src/data.cpp

Packit Service 672cf4
/*
Packit Service 672cf4
  data.cpp - wraps a gpgme data object
Packit Service 672cf4
  Copyright (C) 2003 Klarälvdalens Datakonsult AB
Packit Service 672cf4
Packit Service 672cf4
  This file is part of GPGME++.
Packit Service 672cf4
Packit Service 672cf4
  GPGME++ is free software; you can redistribute it and/or
Packit Service 672cf4
  modify it under the terms of the GNU Library General Public
Packit Service 672cf4
  License as published by the Free Software Foundation; either
Packit Service 672cf4
  version 2 of the License, or (at your option) any later version.
Packit Service 672cf4
Packit Service 672cf4
  GPGME++ is distributed in the hope that it will be useful,
Packit Service 672cf4
  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 672cf4
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 672cf4
  GNU Library General Public License for more details.
Packit Service 672cf4
Packit Service 672cf4
  You should have received a copy of the GNU Library General Public License
Packit Service 672cf4
  along with GPGME++; see the file COPYING.LIB.  If not, write to the
Packit Service 672cf4
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Packit Service 672cf4
  Boston, MA 02110-1301, USA.
Packit Service 672cf4
*/
Packit Service 672cf4
Packit Service 672cf4
#ifdef HAVE_CONFIG_H
Packit Service 672cf4
 #include "config.h"
Packit Service 672cf4
#endif
Packit Service 672cf4
Packit Service 672cf4
#include "data_p.h"
Packit Service 672cf4
#include "context_p.h"
Packit Service 672cf4
#include <error.h>
Packit Service 672cf4
#include <interfaces/dataprovider.h>
Packit Service 672cf4
Packit Service 672cf4
#include <gpgme.h>
Packit Service 672cf4
Packit Service 672cf4
#ifndef NDEBUG
Packit Service 672cf4
#include <iostream>
Packit Service 672cf4
#endif
Packit Service 672cf4
Packit Service 672cf4
GpgME::Data::Private::~Private()
Packit Service 672cf4
{
Packit Service 672cf4
    if (data) {
Packit Service 672cf4
        gpgme_data_release(data);
Packit Service 672cf4
    }
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
const GpgME::Data::Null GpgME::Data::null;
Packit Service 672cf4
Packit Service 672cf4
GpgME::Data::Data()
Packit Service 672cf4
{
Packit Service 672cf4
    gpgme_data_t data;
Packit Service 672cf4
    const gpgme_error_t e = gpgme_data_new(&data);
Packit Service 0ef63b
    d.reset(new Private(e ? nullptr : data));
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
GpgME::Data::Data(const Null &)
Packit Service 0ef63b
    : d(new Private(nullptr))
Packit Service 672cf4
{
Packit Service 672cf4
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
GpgME::Data::Data(gpgme_data_t data)
Packit Service 672cf4
    : d(new Private(data))
Packit Service 672cf4
{
Packit Service 672cf4
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
GpgME::Data::Data(const char *buffer, size_t size, bool copy)
Packit Service 672cf4
{
Packit Service 672cf4
    gpgme_data_t data;
Packit Service 672cf4
    const gpgme_error_t e = gpgme_data_new_from_mem(&data, buffer, size, int(copy));
Packit Service 672cf4
    std::string sizestr = std::to_string(size);
Packit Service 672cf4
    // Ignore errors as this is optional
Packit Service 672cf4
    gpgme_data_set_flag(data, "size-hint", sizestr.c_str());
Packit Service 0ef63b
    d.reset(new Private(e ? nullptr : data));
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
GpgME::Data::Data(const char *filename)
Packit Service 672cf4
{
Packit Service 672cf4
    gpgme_data_t data;
Packit Service 672cf4
    const gpgme_error_t e = gpgme_data_new(&data);
Packit Service 0ef63b
    d.reset(new Private(e ? nullptr : data));
Packit Service 672cf4
    if (!e) {
Packit Service 672cf4
        setFileName(filename);
Packit Service 672cf4
    }
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
GpgME::Data::Data(const char *filename, off_t offset, size_t length)
Packit Service 672cf4
{
Packit Service 672cf4
    gpgme_data_t data;
Packit Service 0ef63b
    const gpgme_error_t e = gpgme_data_new_from_filepart(&data, filename, nullptr, offset, length);
Packit Service 0ef63b
    d.reset(new Private(e ? nullptr : data));
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
GpgME::Data::Data(FILE *fp)
Packit Service 672cf4
{
Packit Service 672cf4
    gpgme_data_t data;
Packit Service 672cf4
    const gpgme_error_t e = gpgme_data_new_from_stream(&data, fp);
Packit Service 0ef63b
    d.reset(new Private(e ? nullptr : data));
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
GpgME::Data::Data(FILE *fp, off_t offset, size_t length)
Packit Service 672cf4
{
Packit Service 672cf4
    gpgme_data_t data;
Packit Service 0ef63b
    const gpgme_error_t e = gpgme_data_new_from_filepart(&data, nullptr, fp, offset, length);
Packit Service 0ef63b
    d.reset(new Private(e ? nullptr : data));
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
GpgME::Data::Data(int fd)
Packit Service 672cf4
{
Packit Service 672cf4
    gpgme_data_t data;
Packit Service 672cf4
    const gpgme_error_t e = gpgme_data_new_from_fd(&data, fd);
Packit Service 0ef63b
    d.reset(new Private(e ? nullptr : data));
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
GpgME::Data::Data(DataProvider *dp)
Packit Service 672cf4
{
Packit Service 672cf4
    d.reset(new Private);
Packit Service 672cf4
    if (!dp) {
Packit Service 672cf4
        return;
Packit Service 672cf4
    }
Packit Service 672cf4
    if (!dp->isSupported(DataProvider::Read)) {
Packit Service 0ef63b
        d->cbs.read = nullptr;
Packit Service 672cf4
    }
Packit Service 672cf4
    if (!dp->isSupported(DataProvider::Write)) {
Packit Service 0ef63b
        d->cbs.write = nullptr;
Packit Service 672cf4
    }
Packit Service 672cf4
    if (!dp->isSupported(DataProvider::Seek)) {
Packit Service 0ef63b
        d->cbs.seek = nullptr;
Packit Service 672cf4
    }
Packit Service 672cf4
    if (!dp->isSupported(DataProvider::Release)) {
Packit Service 0ef63b
        d->cbs.release = nullptr;
Packit Service 672cf4
    }
Packit Service 672cf4
    const gpgme_error_t e = gpgme_data_new_from_cbs(&d->data, &d->cbs, dp);
Packit Service 672cf4
    if (e) {
Packit Service 0ef63b
        d->data = nullptr;
Packit Service 672cf4
    }
Packit Service 672cf4
    if (dp->isSupported(DataProvider::Seek)) {
Packit Service 672cf4
        off_t size = seek(0, SEEK_END);
Packit Service 672cf4
        seek(0, SEEK_SET);
Packit Service 672cf4
        std::string sizestr = std::to_string(size);
Packit Service 672cf4
        // Ignore errors as this is optional
Packit Service 672cf4
        gpgme_data_set_flag(d->data, "size-hint", sizestr.c_str());
Packit Service 672cf4
    }
Packit Service 672cf4
#ifndef NDEBUG
Packit Service 672cf4
    //std::cerr << "GpgME::Data(): DataProvider supports: "
Packit Service 672cf4
    //    << ( d->cbs.read ? "read" : "no read" ) << ", "
Packit Service 672cf4
    //    << ( d->cbs.write ? "write" : "no write" ) << ", "
Packit Service 672cf4
    //    << ( d->cbs.seek ? "seek" : "no seek" ) << ", "
Packit Service 672cf4
    //    << ( d->cbs.release ? "release" : "no release" ) << std::endl;
Packit Service 672cf4
#endif
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
bool GpgME::Data::isNull() const
Packit Service 672cf4
{
Packit Service 672cf4
    return !d || !d->data;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
GpgME::Data::Encoding GpgME::Data::encoding() const
Packit Service 672cf4
{
Packit Service 672cf4
    switch (gpgme_data_get_encoding(d->data)) {
Packit Service 672cf4
    case GPGME_DATA_ENCODING_NONE:   return AutoEncoding;
Packit Service 672cf4
    case GPGME_DATA_ENCODING_BINARY: return BinaryEncoding;
Packit Service 672cf4
    case GPGME_DATA_ENCODING_BASE64: return Base64Encoding;
Packit Service 672cf4
    case GPGME_DATA_ENCODING_ARMOR:  return ArmorEncoding;
Packit Service 672cf4
    case GPGME_DATA_ENCODING_MIME:   return MimeEncoding;
Packit Service 672cf4
    case GPGME_DATA_ENCODING_URL:    return UrlEncoding;
Packit Service 672cf4
    case GPGME_DATA_ENCODING_URLESC: return UrlEscEncoding;
Packit Service 672cf4
    case GPGME_DATA_ENCODING_URL0:   return Url0Encoding;
Packit Service 672cf4
    }
Packit Service 672cf4
    return AutoEncoding;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
GpgME::Error GpgME::Data::setEncoding(Encoding enc)
Packit Service 672cf4
{
Packit Service 672cf4
    gpgme_data_encoding_t ge = GPGME_DATA_ENCODING_NONE;
Packit Service 672cf4
    switch (enc) {
Packit Service 672cf4
    case AutoEncoding:   ge = GPGME_DATA_ENCODING_NONE;   break;
Packit Service 672cf4
    case BinaryEncoding: ge = GPGME_DATA_ENCODING_BINARY; break;
Packit Service 672cf4
    case Base64Encoding: ge = GPGME_DATA_ENCODING_BASE64; break;
Packit Service 672cf4
    case ArmorEncoding:  ge = GPGME_DATA_ENCODING_ARMOR;  break;
Packit Service 672cf4
    case MimeEncoding:   ge = GPGME_DATA_ENCODING_MIME;  break;
Packit Service 672cf4
    case UrlEncoding:    ge = GPGME_DATA_ENCODING_URL; break;
Packit Service 672cf4
    case UrlEscEncoding: ge = GPGME_DATA_ENCODING_URLESC; break;
Packit Service 672cf4
    case Url0Encoding:   ge = GPGME_DATA_ENCODING_URL0; break;
Packit Service 672cf4
    }
Packit Service 672cf4
    return Error(gpgme_data_set_encoding(d->data, ge));
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
GpgME::Data::Type GpgME::Data::type() const
Packit Service 672cf4
{
Packit Service 672cf4
    if (isNull()) {
Packit Service 672cf4
        return Invalid;
Packit Service 672cf4
    }
Packit Service 672cf4
    switch (gpgme_data_identify(d->data, 0)) {
Packit Service 672cf4
    case GPGME_DATA_TYPE_INVALID:       return Invalid;
Packit Service 672cf4
    case GPGME_DATA_TYPE_UNKNOWN:       return Unknown;
Packit Service 672cf4
    case GPGME_DATA_TYPE_PGP_SIGNED:    return PGPSigned;
Packit Service 672cf4
    case GPGME_DATA_TYPE_PGP_OTHER:     return PGPOther;
Packit Service 672cf4
    case GPGME_DATA_TYPE_PGP_KEY:       return PGPKey;
Packit Service 672cf4
    case GPGME_DATA_TYPE_CMS_SIGNED:    return CMSSigned;
Packit Service 672cf4
    case GPGME_DATA_TYPE_CMS_ENCRYPTED: return CMSEncrypted;
Packit Service 672cf4
    case GPGME_DATA_TYPE_CMS_OTHER:     return CMSOther;
Packit Service 672cf4
    case GPGME_DATA_TYPE_X509_CERT:     return X509Cert;
Packit Service 672cf4
    case GPGME_DATA_TYPE_PKCS12:        return PKCS12;
Packit Service 672cf4
    case GPGME_DATA_TYPE_PGP_ENCRYPTED: return PGPEncrypted;
Packit Service 672cf4
    case GPGME_DATA_TYPE_PGP_SIGNATURE: return PGPSignature;
Packit Service 672cf4
    }
Packit Service 672cf4
    return Invalid;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
char *GpgME::Data::fileName() const
Packit Service 672cf4
{
Packit Service 672cf4
    return gpgme_data_get_file_name(d->data);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
GpgME::Error GpgME::Data::setFileName(const char *name)
Packit Service 672cf4
{
Packit Service 672cf4
    return Error(gpgme_data_set_file_name(d->data, name));
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
ssize_t GpgME::Data::read(void *buffer, size_t length)
Packit Service 672cf4
{
Packit Service 672cf4
    return gpgme_data_read(d->data, buffer, length);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
ssize_t GpgME::Data::write(const void *buffer, size_t length)
Packit Service 672cf4
{
Packit Service 672cf4
    return gpgme_data_write(d->data, buffer, length);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
off_t GpgME::Data::seek(off_t offset, int whence)
Packit Service 672cf4
{
Packit Service 672cf4
    return gpgme_data_seek(d->data, offset, whence);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 0ef63b
GpgME::Error GpgME::Data::rewind()
Packit Service 0ef63b
{
Packit Service 0ef63b
    return Error(gpgme_data_rewind(d->data));
Packit Service 0ef63b
}
Packit Service 0ef63b
Packit Service 672cf4
std::vector<GpgME::Key> GpgME::Data::toKeys(Protocol proto) const
Packit Service 672cf4
{
Packit Service 672cf4
    std::vector<GpgME::Key> ret;
Packit Service 672cf4
    if (isNull()) {
Packit Service 672cf4
        return ret;
Packit Service 672cf4
    }
Packit Service 672cf4
    auto ctx = GpgME::Context::createForProtocol(proto);
Packit Service 672cf4
    if (!ctx) {
Packit Service 672cf4
        return ret;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
    if (gpgme_op_keylist_from_data_start (ctx->impl()->ctx, d->data, 0)) {
Packit Service 672cf4
        return ret;
Packit Service 672cf4
    }
Packit Service 672cf4
Packit Service 672cf4
    gpgme_key_t key;
Packit Service 672cf4
    while (!gpgme_op_keylist_next (ctx->impl()->ctx, &key)) {
Packit Service 672cf4
        ret.push_back(GpgME::Key(key, false));
Packit Service 672cf4
    }
Packit Service 0ef63b
    gpgme_data_seek (d->data, 0, SEEK_SET);
Packit Service 0ef63b
Packit Service 672cf4
    delete ctx;
Packit Service 672cf4
    return ret;
Packit Service 672cf4
}
Packit Service 0ef63b
Packit Service 0ef63b
std::string GpgME::Data::toString()
Packit Service 0ef63b
{
Packit Service 0ef63b
  std::string ret;
Packit Service 0ef63b
  char buf[4096];
Packit Service 0ef63b
  size_t nread;
Packit Service 0ef63b
  seek (0, SEEK_SET);
Packit Service 0ef63b
  while ((nread = read (buf, 4096)) > 0)
Packit Service 0ef63b
    {
Packit Service 0ef63b
      ret += std::string (buf, nread);
Packit Service 0ef63b
    }
Packit Service 0ef63b
  seek (0, SEEK_SET);
Packit Service 0ef63b
  return ret;
Packit Service 0ef63b
}