Skip to content

Commit b0f47b6

Browse files
committed
Add constructor for pgp_dest_t
1 parent 9af62ce commit b0f47b6

File tree

5 files changed

+76
-43
lines changed

5 files changed

+76
-43
lines changed

src/librepgp/stream-armor.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,9 @@ armored_dst_close(pgp_dest_t *dst, bool discard)
10741074
rnp_result_t
10751075
init_armored_dst(pgp_dest_t *dst, pgp_dest_t *writedst, pgp_armored_msg_t msgtype)
10761076
{
1077-
if (!init_dst_common(dst, 0)) {
1077+
try {
1078+
*dst = pgp_dest_t(0);
1079+
} catch (const std::exception &e) {
10781080
return RNP_ERROR_OUT_OF_MEMORY;
10791081
}
10801082
pgp_dest_armored_param_t *param = new (std::nothrow) pgp_dest_armored_param_t();

src/librepgp/stream-common.cpp

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -604,20 +604,33 @@ mem_src_get_memory(pgp_source_t *src, bool own)
604604
return param->memory;
605605
}
606606

607-
bool
608-
init_dst_common(pgp_dest_t *dst, size_t paramsize)
607+
pgp_dest_t::pgp_dest_t(size_t paramsize)
609608
{
610-
memset(dst, 0, sizeof(*dst));
611-
dst->werr = RNP_SUCCESS;
609+
werr = RNP_SUCCESS;
612610
if (!paramsize) {
613-
return true;
611+
return;
614612
}
615613
/* allocate param */
616-
dst->param = calloc(1, paramsize);
617-
if (!dst->param) {
614+
param = calloc(1, paramsize);
615+
if (!param) {
616+
/* LCOV_EXCL_START */
618617
RNP_LOG("allocation failed");
618+
throw rnp::rnp_exception(RNP_ERROR_OUT_OF_MEMORY);
619+
/* LCOV_EXCL_END */
619620
}
620-
return dst->param;
621+
}
622+
623+
// pgp_dest_t constructor do the same job, but we keep this function to preserve api
624+
bool
625+
init_dst_common(pgp_dest_t *dst, size_t paramsize)
626+
{
627+
try {
628+
*dst = pgp_dest_t(paramsize);
629+
} catch (const std::exception &e) {
630+
return false; // LCOV_EXCL_LINE
631+
}
632+
633+
return true;
621634
}
622635

623636
void
@@ -626,26 +639,26 @@ dst_write(pgp_dest_t *dst, const void *buf, size_t len)
626639
/* we call write function only if all previous calls succeeded */
627640
if ((len > 0) && (dst->write) && (dst->werr == RNP_SUCCESS)) {
628641
/* if cache non-empty and len will overflow it then fill it and write out */
629-
if ((dst->clen > 0) && (dst->clen + len > sizeof(dst->cache))) {
630-
memcpy(dst->cache + dst->clen, buf, sizeof(dst->cache) - dst->clen);
631-
buf = (uint8_t *) buf + sizeof(dst->cache) - dst->clen;
632-
len -= sizeof(dst->cache) - dst->clen;
633-
dst->werr = dst->write(dst, dst->cache, sizeof(dst->cache));
634-
dst->writeb += sizeof(dst->cache);
642+
if ((dst->clen > 0) && (dst->clen + len > dst->cache.size())) {
643+
memcpy(dst->cache.data() + dst->clen, buf, dst->cache.size() - dst->clen);
644+
buf = (uint8_t *) buf + dst->cache.size() - dst->clen;
645+
len -= dst->cache.size() - dst->clen;
646+
dst->werr = dst->write(dst, dst->cache.data(), dst->cache.size());
647+
dst->writeb += dst->cache.size();
635648
dst->clen = 0;
636649
if (dst->werr != RNP_SUCCESS) {
637650
return;
638651
}
639652
}
640653

641654
/* here everything will fit into the cache or cache is empty */
642-
if (dst->no_cache || (len > sizeof(dst->cache))) {
655+
if (dst->no_cache || (len > dst->cache.size())) {
643656
dst->werr = dst->write(dst, buf, len);
644657
if (!dst->werr) {
645658
dst->writeb += len;
646659
}
647660
} else {
648-
memcpy(dst->cache + dst->clen, buf, len);
661+
memcpy(dst->cache.data() + dst->clen, buf, len);
649662
dst->clen += len;
650663
}
651664
}
@@ -673,7 +686,7 @@ void
673686
dst_flush(pgp_dest_t *dst)
674687
{
675688
if ((dst->clen > 0) && (dst->write) && (dst->werr == RNP_SUCCESS)) {
676-
dst->werr = dst->write(dst, dst->cache, dst->clen);
689+
dst->werr = dst->write(dst, dst->cache.data(), dst->clen);
677690
dst->writeb += dst->clen;
678691
dst->clen = 0;
679692
}
@@ -759,11 +772,9 @@ file_dst_close(pgp_dest_t *dst, bool discard)
759772
static rnp_result_t
760773
init_fd_dest(pgp_dest_t *dst, int fd, const char *path)
761774
{
762-
if (!init_dst_common(dst, 0)) {
763-
return RNP_ERROR_OUT_OF_MEMORY;
764-
}
765-
766775
try {
776+
*dst = pgp_dest_t(0);
777+
767778
std::unique_ptr<pgp_dest_file_param_t> param(new pgp_dest_file_param_t());
768779
param->path = path;
769780
param->fd = fd;
@@ -1009,8 +1020,10 @@ init_mem_dest(pgp_dest_t *dst, void *mem, unsigned len)
10091020
{
10101021
pgp_dest_mem_param_t *param;
10111022

1012-
if (!init_dst_common(dst, sizeof(*param))) {
1013-
return RNP_ERROR_OUT_OF_MEMORY;
1023+
try {
1024+
*dst = pgp_dest_t(sizeof(*param));
1025+
} catch (const std::exception &e) {
1026+
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
10141027
}
10151028

10161029
param = (pgp_dest_mem_param_t *) dst->param;

src/librepgp/stream-common.h

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -236,18 +236,24 @@ rnp_result_t read_mem_src(pgp_source_t *src, pgp_source_t *readsrc);
236236
const void *mem_src_get_memory(pgp_source_t *src, bool own = false);
237237

238238
typedef struct pgp_dest_t {
239-
pgp_dest_write_func_t * write;
240-
pgp_dest_finish_func_t *finish;
241-
pgp_dest_close_func_t * close;
242-
pgp_stream_type_t type;
243-
rnp_result_t werr; /* write function may set this to some error code */
244-
245-
size_t writeb; /* number of bytes written */
246-
void * param; /* source-specific additional data */
247-
bool no_cache; /* disable write caching */
248-
uint8_t cache[PGP_OUTPUT_CACHE_SIZE];
249-
unsigned clen; /* number of bytes in cache */
250-
bool finished; /* whether dst_finish was called on dest or not */
239+
pgp_dest_write_func_t * write = nullptr;
240+
pgp_dest_finish_func_t *finish = nullptr;
241+
pgp_dest_close_func_t * close = nullptr;
242+
pgp_stream_type_t type = PGP_STREAM_NULL;
243+
rnp_result_t werr = RNP_SUCCESS; /* write function may set this to some error code */
244+
245+
size_t writeb = 0; /* number of bytes written */
246+
void * param = nullptr; /* source-specific additional data */
247+
bool no_cache = 0; /* disable write caching */
248+
std::vector<uint8_t> cache = std::vector<uint8_t>(PGP_OUTPUT_CACHE_SIZE);
249+
unsigned clen = 0; /* number of bytes in cache */
250+
bool finished = 0; /* whether dst_finish was called on dest or not */
251+
252+
pgp_dest_t(size_t paramsize = 0);
253+
254+
pgp_dest_t &operator=(const pgp_dest_t &) = delete;
255+
pgp_dest_t(const pgp_dest_t &) = delete;
256+
pgp_dest_t &operator=(pgp_dest_t &&) = default;
251257
} pgp_dest_t;
252258

253259
/** @brief helper function to allocate memory for dest's param.

src/librepgp/stream-dump.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ init_indent_dest(pgp_dest_t *dst, pgp_dest_t *origdst)
276276
{
277277
pgp_dest_indent_param_t *param;
278278

279-
if (!init_dst_common(dst, sizeof(*param))) {
279+
try {
280+
*dst = pgp_dest_t(sizeof(*param));
281+
} catch (const std::exception &e) {
280282
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
281283
}
282284

@@ -1306,7 +1308,7 @@ stream_dump_sk_session_key(pgp_source_t *src, pgp_dest_t *dst)
13061308
static bool
13071309
stream_dump_get_aead_hdr(pgp_source_t *src, pgp_aead_hdr_t *hdr)
13081310
{
1309-
pgp_dest_t encdst = {};
1311+
pgp_dest_t encdst;
13101312
uint8_t encpkt[64] = {};
13111313

13121314
if (init_mem_dest(&encdst, &encpkt, sizeof(encpkt))) {

src/librepgp/stream-write.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,9 @@ init_partial_pkt_dst(pgp_dest_t &dst, pgp_dest_t &writedst)
235235
{
236236
pgp_dest_partial_param_t *param = NULL;
237237

238-
if (!init_dst_common(&dst, sizeof(*param))) {
238+
try {
239+
dst = pgp_dest_t(sizeof(*param));
240+
} catch (const std::exception &e) {
239241
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
240242
}
241243

@@ -1008,7 +1010,9 @@ init_encrypted_dst(rnp_ctx_t &ctx, pgp_dest_t &dst, pgp_dest_t &writedst)
10081010
return RNP_ERROR_BAD_PARAMETERS;
10091011
}
10101012

1011-
if (!init_dst_common(&dst, 0)) {
1013+
try {
1014+
dst = pgp_dest_t(0);
1015+
} catch (const std::exception &e) {
10121016
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
10131017
}
10141018
auto param = new (std::nothrow) pgp_dest_encrypted_param_t(ctx);
@@ -1483,7 +1487,9 @@ init_signed_dst(rnp_ctx_t &ctx, pgp_dest_t &dst, pgp_dest_t &writedst)
14831487
pgp_dest_signed_param_t *param = NULL;
14841488
rnp_result_t ret = RNP_ERROR_GENERIC;
14851489

1486-
if (!init_dst_common(&dst, 0)) {
1490+
try {
1491+
dst = pgp_dest_t(0);
1492+
} catch (const std::exception &e) {
14871493
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
14881494
}
14891495
try {
@@ -1719,7 +1725,9 @@ init_compressed_dst(rnp_ctx_t &ctx, pgp_dest_t &dst, pgp_dest_t &writedst)
17191725
uint8_t buf = 0;
17201726
int zret = 0;
17211727

1722-
if (!init_dst_common(&dst, sizeof(*param))) {
1728+
try {
1729+
dst = pgp_dest_t(sizeof(*param));
1730+
} catch (const std::exception &e) {
17231731
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
17241732
}
17251733

@@ -1846,7 +1854,9 @@ init_literal_dst(pgp_literal_hdr_t &hdr, pgp_dest_t &dst, pgp_dest_t &writedst)
18461854
{
18471855
pgp_dest_packet_param_t *param = NULL;
18481856

1849-
if (!init_dst_common(&dst, sizeof(*param))) {
1857+
try {
1858+
dst = pgp_dest_t(sizeof(*param));
1859+
} catch (const std::exception &e) {
18501860
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
18511861
}
18521862

0 commit comments

Comments
 (0)