Skip to content

Commit

Permalink
Add constructor for pgp_dest_t
Browse files Browse the repository at this point in the history
  • Loading branch information
desvxx committed Dec 11, 2024
1 parent bd601d5 commit 2c2771f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 42 deletions.
4 changes: 3 additions & 1 deletion src/librepgp/stream-armor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,9 @@ armored_dst_close(pgp_dest_t *dst, bool discard)
rnp_result_t
init_armored_dst(pgp_dest_t *dst, pgp_dest_t *writedst, pgp_armored_msg_t msgtype)
{
if (!init_dst_common(dst, 0)) {
try {
*dst = pgp_dest_t(0);
} catch (const std::exception &e) {
return RNP_ERROR_OUT_OF_MEMORY;
}
pgp_dest_armored_param_t *param = new (std::nothrow) pgp_dest_armored_param_t();
Expand Down
63 changes: 41 additions & 22 deletions src/librepgp/stream-common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -603,20 +603,39 @@ mem_src_get_memory(pgp_source_t *src, bool own)
return param->memory;
}

bool
init_dst_common(pgp_dest_t *dst, size_t paramsize)
pgp_dest_t::pgp_dest_t(size_t paramsize)
{
memset(dst, 0, sizeof(*dst));
dst->werr = RNP_SUCCESS;
werr = RNP_SUCCESS;
if (!paramsize) {
return true;
return;
}
/* allocate param */
dst->param = calloc(1, paramsize);
if (!dst->param) {
param = calloc(1, paramsize);
if (!param) {
RNP_LOG("allocation failed");
throw rnp::rnp_exception(RNP_ERROR_OUT_OF_MEMORY);
}
}

pgp_dest_t::~pgp_dest_t()
{
if (param) {
free(param);
param = nullptr;
}
}

// pgp_dest_t constructor do the same job, but we keep this function to preserve api
bool
init_dst_common(pgp_dest_t *dst, size_t paramsize)
{
try {
*dst = pgp_dest_t(paramsize);
} catch (const std::exception &e) {
return false;
}
return dst->param;

return true;
}

void
Expand All @@ -625,26 +644,26 @@ dst_write(pgp_dest_t *dst, const void *buf, size_t len)
/* we call write function only if all previous calls succeeded */
if ((len > 0) && (dst->write) && (dst->werr == RNP_SUCCESS)) {
/* if cache non-empty and len will overflow it then fill it and write out */
if ((dst->clen > 0) && (dst->clen + len > sizeof(dst->cache))) {
memcpy(dst->cache + dst->clen, buf, sizeof(dst->cache) - dst->clen);
buf = (uint8_t *) buf + sizeof(dst->cache) - dst->clen;
len -= sizeof(dst->cache) - dst->clen;
dst->werr = dst->write(dst, dst->cache, sizeof(dst->cache));
dst->writeb += sizeof(dst->cache);
if ((dst->clen > 0) && (dst->clen + len > dst->cache.size())) {
memcpy(dst->cache.data() + dst->clen, buf, dst->cache.size() - dst->clen);
buf = (uint8_t *) buf + dst->cache.size() - dst->clen;
len -= dst->cache.size() - dst->clen;
dst->werr = dst->write(dst, dst->cache.data(), dst->cache.size());
dst->writeb += dst->cache.size();
dst->clen = 0;
if (dst->werr != RNP_SUCCESS) {
return;
}
}

/* here everything will fit into the cache or cache is empty */
if (dst->no_cache || (len > sizeof(dst->cache))) {
if (dst->no_cache || (len > dst->cache.size())) {
dst->werr = dst->write(dst, buf, len);
if (!dst->werr) {
dst->writeb += len;
}
} else {
memcpy(dst->cache + dst->clen, buf, len);
memcpy(dst->cache.data() + dst->clen, buf, len);
dst->clen += len;
}
}
Expand Down Expand Up @@ -672,7 +691,7 @@ void
dst_flush(pgp_dest_t *dst)
{
if ((dst->clen > 0) && (dst->write) && (dst->werr == RNP_SUCCESS)) {
dst->werr = dst->write(dst, dst->cache, dst->clen);
dst->werr = dst->write(dst, dst->cache.data(), dst->clen);
dst->writeb += dst->clen;
dst->clen = 0;
}
Expand Down Expand Up @@ -758,11 +777,9 @@ file_dst_close(pgp_dest_t *dst, bool discard)
static rnp_result_t
init_fd_dest(pgp_dest_t *dst, int fd, const char *path)
{
if (!init_dst_common(dst, 0)) {
return RNP_ERROR_OUT_OF_MEMORY;
}

try {
*dst = pgp_dest_t(0);

std::unique_ptr<pgp_dest_file_param_t> param(new pgp_dest_file_param_t());
param->path = path;
param->fd = fd;
Expand Down Expand Up @@ -1008,7 +1025,9 @@ init_mem_dest(pgp_dest_t *dst, void *mem, unsigned len)
{
pgp_dest_mem_param_t *param;

if (!init_dst_common(dst, sizeof(*param))) {
try {
*dst = pgp_dest_t(sizeof(*param));
} catch (const std::exception &e) {
return RNP_ERROR_OUT_OF_MEMORY;
}

Expand Down
27 changes: 15 additions & 12 deletions src/librepgp/stream-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,21 @@ rnp_result_t read_mem_src(pgp_source_t *src, pgp_source_t *readsrc);
const void *mem_src_get_memory(pgp_source_t *src, bool own = false);

typedef struct pgp_dest_t {
pgp_dest_write_func_t * write;
pgp_dest_finish_func_t *finish;
pgp_dest_close_func_t * close;
pgp_stream_type_t type;
rnp_result_t werr; /* write function may set this to some error code */

size_t writeb; /* number of bytes written */
void * param; /* source-specific additional data */
bool no_cache; /* disable write caching */
uint8_t cache[PGP_OUTPUT_CACHE_SIZE];
unsigned clen; /* number of bytes in cache */
bool finished; /* whether dst_finish was called on dest or not */
pgp_dest_write_func_t * write = nullptr;
pgp_dest_finish_func_t *finish = nullptr;
pgp_dest_close_func_t * close = nullptr;
pgp_stream_type_t type = PGP_STREAM_NULL;
rnp_result_t werr = RNP_SUCCESS; /* write function may set this to some error code */

size_t writeb = 0; /* number of bytes written */
void * param = nullptr; /* source-specific additional data */
bool no_cache = 0; /* disable write caching */
std::vector<uint8_t> cache = std::vector<uint8_t>(PGP_OUTPUT_CACHE_SIZE);
unsigned clen = 0; /* number of bytes in cache */
bool finished = 0; /* whether dst_finish was called on dest or not */

pgp_dest_t(size_t paramsize = 0);
~pgp_dest_t();
} pgp_dest_t;

/** @brief helper function to allocate memory for dest's param.
Expand Down
6 changes: 4 additions & 2 deletions src/librepgp/stream-dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,9 @@ init_indent_dest(pgp_dest_t *dst, pgp_dest_t *origdst)
{
pgp_dest_indent_param_t *param;

if (!init_dst_common(dst, sizeof(*param))) {
try {
*dst = pgp_dest_t(sizeof(*param));
} catch (const std::exception &e) {
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
}

Expand Down Expand Up @@ -1289,7 +1291,7 @@ stream_dump_sk_session_key(pgp_source_t *src, pgp_dest_t *dst)
static bool
stream_dump_get_aead_hdr(pgp_source_t *src, pgp_aead_hdr_t *hdr)
{
pgp_dest_t encdst = {};
pgp_dest_t encdst;
uint8_t encpkt[64] = {};

if (init_mem_dest(&encdst, &encpkt, sizeof(encpkt))) {
Expand Down
20 changes: 15 additions & 5 deletions src/librepgp/stream-write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,9 @@ init_partial_pkt_dst(pgp_dest_t *dst, pgp_dest_t *writedst)
{
pgp_dest_partial_param_t *param;

if (!init_dst_common(dst, sizeof(*param))) {
try {
*dst = pgp_dest_t(sizeof(*param));
} catch (const std::exception &e) {
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
}

Expand Down Expand Up @@ -988,7 +990,9 @@ init_encrypted_dst(pgp_write_handler_t *handler, pgp_dest_t *dst, pgp_dest_t *wr
}
}

if (!init_dst_common(dst, 0)) {
try {
*dst = pgp_dest_t(0);
} catch (const std::exception &e) {
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
}
try {
Expand Down Expand Up @@ -1495,7 +1499,9 @@ init_signed_dst(pgp_write_handler_t *handler, pgp_dest_t *dst, pgp_dest_t *write
/* LCOV_EXCL_END */
}

if (!init_dst_common(dst, 0)) {
try {
*dst = pgp_dest_t(0);
} catch (const std::exception &e) {
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
}
try {
Expand Down Expand Up @@ -1731,7 +1737,9 @@ init_compressed_dst(pgp_write_handler_t *handler, pgp_dest_t *dst, pgp_dest_t *w
uint8_t buf;
int zret;

if (!init_dst_common(dst, sizeof(*param))) {
try {
*dst = pgp_dest_t(sizeof(*param));
} catch (const std::exception &e) {
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
}

Expand Down Expand Up @@ -1860,7 +1868,9 @@ init_literal_dst(pgp_literal_hdr_t &hdr, pgp_dest_t *dst, pgp_dest_t *writedst)
{
pgp_dest_packet_param_t *param;

if (!init_dst_common(dst, sizeof(*param))) {
try {
*dst = pgp_dest_t(sizeof(*param));
} catch (const std::exception &e) {
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
}

Expand Down

0 comments on commit 2c2771f

Please sign in to comment.