Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions cdoc/Crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ EncryptionConsumer::EncryptionConsumer(DataConsumer &dst, const EVP_CIPHER *ciph
}

result_t
EncryptionConsumer::write(const uint8_t *src, size_t size)
EncryptionConsumer::write(const uint8_t *src, size_t size) noexcept try
{
if(!src || size == 0)
return OK;
Expand All @@ -460,10 +460,12 @@ EncryptionConsumer::write(const uint8_t *src, size_t size)
if(SSL_FAILED(EVP_CipherUpdate(ctx.get(), buf.data(), &len, src, int(size)), "EVP_CipherUpdate"))
return CRYPTO_ERROR;
return dst.write(buf.data(), size_t(len));
} catch(...) {
return OUTPUT_STREAM_ERROR;
}

result_t
EncryptionConsumer::writeAAD(const std::vector<uint8_t> &data)
EncryptionConsumer::writeAAD(const std::vector<uint8_t> &data) noexcept
{
int len = 0;
if(SSL_FAILED(EVP_CipherUpdate(ctx.get(), nullptr, &len, data.data(), int(data.size())), "EVP_CipherUpdate"))
Expand All @@ -472,7 +474,7 @@ EncryptionConsumer::writeAAD(const std::vector<uint8_t> &data)
}

result_t
EncryptionConsumer::close()
EncryptionConsumer::close() noexcept try
{
buf.resize(std::max<size_t>(buf.size(), size_t(EVP_CIPHER_CTX_block_size(ctx.get()))));
int len = int(buf.size());
Expand All @@ -498,6 +500,8 @@ EncryptionConsumer::close()
return IO_ERROR;
}
return OK;
} catch(...) {
return OUTPUT_STREAM_ERROR;
}

DecryptionSource::DecryptionSource(DataSource &src, const std::string &method, const std::vector<unsigned char> &key, size_t ivLen)
Expand Down Expand Up @@ -531,7 +535,7 @@ result_t DecryptionSource::updateAAD(const std::vector<uint8_t> &data)
return OK;
}

result_t DecryptionSource::read(unsigned char *dst, size_t size)
result_t DecryptionSource::read(unsigned char *dst, size_t size) noexcept
{
if (error != OK)
return error;
Expand Down
14 changes: 7 additions & 7 deletions cdoc/Crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ struct EncryptionConsumer final : public DataConsumer {
EncryptionConsumer(DataConsumer &dst, const std::string &method, const Crypto::Key &key);
EncryptionConsumer(DataConsumer &dst, const EVP_CIPHER *cipher, const Crypto::Key &key);
CDOC_DISABLE_MOVE_COPY(EncryptionConsumer)
result_t write(const uint8_t *src, size_t size) final;
result_t writeAAD(const std::vector<uint8_t> &data);
result_t close() final;
bool isError() final { return error != OK || dst.isError(); }
result_t write(const uint8_t *src, size_t size) noexcept final;
result_t writeAAD(const std::vector<uint8_t> &data) noexcept;
result_t close() noexcept final;
bool isError() noexcept final { return error != OK || dst.isError(); }

private:
unique_free_t<EVP_CIPHER_CTX> ctx;
Expand All @@ -131,11 +131,11 @@ struct DecryptionSource final : public DataSource {
DecryptionSource(DataSource &src, const EVP_CIPHER *cipher, const std::vector<unsigned char> &key, size_t ivLen = 0);
CDOC_DISABLE_MOVE_COPY(DecryptionSource)

result_t read(unsigned char* dst, size_t size) final;
result_t read(unsigned char* dst, size_t size) noexcept final;
result_t updateAAD(const std::vector<uint8_t>& data);
result_t close();
bool isError() final { return error != OK || src.isError(); }
bool isEof() final { return src.isEof(); }
bool isError() noexcept final { return error != OK || src.isError(); }
bool isEof() noexcept final { return src.isEof(); }

private:
unique_free_t<EVP_CIPHER_CTX> ctx;
Expand Down
9 changes: 6 additions & 3 deletions cdoc/DDocReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@ struct DDocFileListConsumer : public libcdoc::MultiDataConsumer {
std::vector<DDOCReader::File> &files;

DDocFileListConsumer(std::vector<DDOCReader::File> &_files): files(_files) {}
int64_t write(const uint8_t *src, size_t size) final {
int64_t write(const uint8_t *src, size_t size) noexcept final try {
DDOCReader::File& file = files.back();
file.data.insert(file.data.end(), src, src + size);
return size;
} catch(...) {
return OUTPUT_STREAM_ERROR;
}
libcdoc::result_t close() final { return libcdoc::OK; }
bool isError() final { return false; }

libcdoc::result_t close() noexcept final { return libcdoc::OK; }
bool isError() noexcept final { return false; }
libcdoc::result_t open(const std::string& name, int64_t /*size*/) final {
files.push_back({name, "application/octet-stream", {}});
return libcdoc::OK;
Expand Down
19 changes: 11 additions & 8 deletions cdoc/Io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include "Io.h"

#include <array>

namespace libcdoc {

static constexpr size_t BLOCK_SIZE = 65536;
Expand Down Expand Up @@ -55,16 +57,15 @@ DataSource::getLastErrorStr(result_t code) const
}

int64_t
DataConsumer::writeAll(DataSource& src)
DataConsumer::writeAll(DataSource& src) noexcept
{
static const size_t BUF_SIZE = 64 * 1024;
uint8_t buf[BUF_SIZE];
std::array<uint8_t,64 * 1024> buf{};
size_t total_read = 0;
while (!src.isEof()) {
int64_t n_read = src.read(buf, BUF_SIZE);
int64_t n_read = src.read(buf.data(), buf.size());
if (n_read < 0) return n_read;
if (n_read > 0) {
int64_t n_written = write(buf, n_read);
int64_t n_written = write(buf.data(), n_read);
if (n_written < 0) return n_written;
total_read += n_written;
}
Expand Down Expand Up @@ -102,22 +103,24 @@ FileListSource::FileListSource(const std::string& base, const std::vector<std::s
}

int64_t
FileListSource::read(uint8_t *dst, size_t size)
FileListSource::read(uint8_t *dst, size_t size) noexcept try
{
if ((_current < 0) || (_current >= _files.size())) return WORKFLOW_ERROR;
_ifs.read((char *) dst, size);
return (_ifs.bad()) ? INPUT_STREAM_ERROR : _ifs.gcount();
} catch(...) {
return INPUT_STREAM_ERROR;
}

bool
FileListSource::isError()
FileListSource::isError() noexcept
{
if ((_current < 0) || (_current >= _files.size())) return OK;
return _ifs.bad();
}

bool
FileListSource::isEof()
FileListSource::isEof() noexcept
{
if (_current < 0) return false;
if (_current >= _files.size()) return true;
Expand Down
78 changes: 42 additions & 36 deletions cdoc/Io.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ struct CDOC_EXPORT DataConsumer {
* @param size the number of bytes to write
* @return size or error code
*/
virtual result_t write(const uint8_t *src, size_t size) = 0;
virtual result_t write(const uint8_t *src, size_t size) noexcept = 0;
/**
* @brief informs DataConsumer that the writing is finished
* @return error code or OK
*/
virtual result_t close() = 0;
virtual result_t close() noexcept = 0;
/**
* @brief checks whether DataSource is in error state
* @return true if error state
*/
virtual bool isError() = 0;
virtual bool isError() noexcept = 0;
/**
* @brief get textual description of the last error
*
Expand All @@ -74,15 +74,15 @@ struct CDOC_EXPORT DataConsumer {
* @param src a vector
* @return vector size or error code
*/
result_t write(const std::vector<uint8_t>& src) {
result_t write(const std::vector<uint8_t>& src) noexcept {
return write(src.data(), src.size());
}
/**
* @brief write all bytes in string
* @param src a string
* @return string length or error code
*/
result_t write(const std::string& src) {
result_t write(const std::string& src) noexcept {
return write((const uint8_t *) src.data(), src.size());
}
/**
Expand All @@ -93,7 +93,7 @@ struct CDOC_EXPORT DataConsumer {
* @param src the input DataSource
* @return the number of bytes copied or error
*/
result_t writeAll(DataSource& src);
result_t writeAll(DataSource& src) noexcept;

DataConsumer (const DataConsumer&) = delete;
DataConsumer& operator= (const DataConsumer&) = delete;
Expand Down Expand Up @@ -128,17 +128,17 @@ struct CDOC_EXPORT DataSource {
* @param size the number of bytes to read
* @return the number of bytes read or error code
*/
virtual result_t read(uint8_t *dst, size_t size) { return NOT_IMPLEMENTED; }
virtual result_t read(uint8_t *dst, size_t size) noexcept { return NOT_IMPLEMENTED; }
/**
* @brief check whether DataConsumer is in error state
* @return true if error state
*/
virtual bool isError() { return true; }
virtual bool isError() noexcept { return true; }
/**
* @brief check whether DataConsumer is reached to the end of data
* @return true if end of stream
*/
virtual bool isEof() { return true; }
virtual bool isEof() noexcept { return true; }
/**
* @brief get textual description of the last error
*
Expand Down Expand Up @@ -169,7 +169,7 @@ struct CDOC_EXPORT DataSource {
* @param dst the destination DataConsumer
* @return error code or OK
*/
result_t readAll(DataConsumer& dst) {
result_t readAll(DataConsumer& dst) noexcept {
return dst.writeAll(*this);
}

Expand Down Expand Up @@ -214,14 +214,14 @@ struct CDOC_EXPORT ChainedConsumer : public DataConsumer {
~ChainedConsumer() {
if (_owned) delete _dst;
}
result_t write(const uint8_t *src, size_t size) override {
result_t write(const uint8_t *src, size_t size) noexcept override {
return _dst->write(src, size);
}
result_t close() override {
result_t close() noexcept override {
if (_owned) return _dst->close();
return OK;
}
bool isError() override {
bool isError() noexcept override {
return _dst->isError();
}
protected:
Expand All @@ -234,13 +234,13 @@ struct CDOC_EXPORT ChainedSource : public DataSource {
~ChainedSource() {
if (_owned) delete _src;
}
result_t read(uint8_t *dst, size_t size) {
result_t read(uint8_t *dst, size_t size) noexcept override {
return _src->read(dst, size);
}
bool isError() {
bool isError() noexcept override {
return _src->isError();
}
bool isEof() {
bool isEof() noexcept override {
return _src->isEof();
}
protected:
Expand All @@ -264,13 +264,15 @@ struct CDOC_EXPORT IStreamSource : public DataSource {
return bool(_ifs->bad()) ? INPUT_STREAM_ERROR : OK;
}

result_t read(uint8_t *dst, size_t size) {
result_t read(uint8_t *dst, size_t size) noexcept override try {
_ifs->read((char *) dst, size);
return (_ifs->bad()) ? INPUT_STREAM_ERROR : _ifs->gcount();
} catch(...) {
return INPUT_STREAM_ERROR;
}

bool isError() { return _ifs->bad(); }
bool isEof() { return _ifs->eof(); }
bool isError() noexcept override { return _ifs->bad(); }
bool isEof() noexcept override { return _ifs->eof(); }
protected:
std::istream *_ifs;
bool _owned;
Expand All @@ -285,17 +287,17 @@ struct CDOC_EXPORT OStreamConsumer : public DataConsumer {
if (_owned) delete _ofs;
}

result_t write(const uint8_t *src, size_t size) {
result_t write(const uint8_t *src, size_t size) noexcept override {
_ofs->write((const char *) src, size);
return (_ofs->bad()) ? OUTPUT_STREAM_ERROR : size;
}

result_t close() {
result_t close() noexcept override {
_ofs->flush();
return (_ofs->bad()) ? OUTPUT_STREAM_ERROR : OK;
}

bool isError() { return _ofs->bad(); }
bool isError() noexcept override { return _ofs->bad(); }
protected:
std::ostream *_ofs;
bool _owned;
Expand All @@ -310,28 +312,30 @@ struct CDOC_EXPORT VectorSource : public DataSource {
return OK;
}

result_t read(uint8_t *dst, size_t size) override {
result_t read(uint8_t *dst, size_t size) noexcept override {
size = std::min<size_t>(size, _data.size() - _ptr);
std::copy_n(_data.cbegin() + _ptr, size, dst);
_ptr += size;
return size;
}

bool isError() override { return false; }
bool isEof() override { return _ptr >= _data.size(); }
bool isError() noexcept override { return false; }
bool isEof() noexcept override { return _ptr >= _data.size(); }
protected:
const std::vector<uint8_t>& _data;
size_t _ptr;
};

struct CDOC_EXPORT VectorConsumer : public DataConsumer {
VectorConsumer(std::vector<uint8_t>& data) : _data(data) {}
result_t write(const uint8_t *src, size_t size) override final {
result_t write(const uint8_t *src, size_t size) noexcept final try {
_data.insert(_data.end(), src, src + size);
return size;
} catch(...) {
return OUTPUT_STREAM_ERROR;
}
result_t close() override final { return OK; }
virtual bool isError() override final { return false; }
result_t close() noexcept final { return OK; }
virtual bool isError() noexcept final { return false; }
protected:
std::vector<uint8_t>& _data;
};
Expand All @@ -340,15 +344,17 @@ struct CDOC_EXPORT FileListConsumer : public MultiDataConsumer {
FileListConsumer(const std::string& base_path) {
base = base_path;
}
result_t write(const uint8_t *src, size_t size) override final {
result_t write(const uint8_t *src, size_t size) noexcept final try {
ofs.write((const char *) src, size);
return (ofs.bad()) ? OUTPUT_STREAM_ERROR : size;
} catch(...) {
return OUTPUT_STREAM_ERROR;
}
result_t close() override final {
result_t close() noexcept final {
ofs.close();
return (ofs.bad()) ? OUTPUT_STREAM_ERROR : OK;
}
bool isError() override final {
bool isError() noexcept final {
return ofs.bad();
}
result_t open(const std::string& name, int64_t size) override final {
Expand Down Expand Up @@ -378,11 +384,11 @@ struct CDOC_EXPORT FileListConsumer : public MultiDataConsumer {

struct CDOC_EXPORT FileListSource : public MultiDataSource {
FileListSource(const std::string& base, const std::vector<std::string>& files);
result_t read(uint8_t *dst, size_t size) override final;
bool isError() override final;
bool isEof() override final;
result_t getNumComponents() override final;
result_t next(std::string& name, int64_t& size) override final;
result_t read(uint8_t *dst, size_t size) noexcept final;
bool isError() noexcept final;
bool isEof() noexcept final;
result_t getNumComponents() final;
result_t next(std::string& name, int64_t& size) final;
protected:
std::filesystem::path _base;
const std::vector<std::string>& _files;
Expand Down
Loading