Skip to content

Commit c8f9f2a

Browse files
committed
Add constructor for pgp_dest_t
1 parent bd601d5 commit c8f9f2a

File tree

5 files changed

+73
-42
lines changed

5 files changed

+73
-42
lines changed

src/librepgp/stream-armor.cpp

+3-1
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

+33-22
Original file line numberDiff line numberDiff line change
@@ -603,20 +603,31 @@ mem_src_get_memory(pgp_source_t *src, bool own)
603603
return param->memory;
604604
}
605605

606-
bool
607-
init_dst_common(pgp_dest_t *dst, size_t paramsize)
606+
pgp_dest_t::pgp_dest_t(size_t paramsize)
608607
{
609-
memset(dst, 0, sizeof(*dst));
610-
dst->werr = RNP_SUCCESS;
608+
werr = RNP_SUCCESS;
611609
if (!paramsize) {
612-
return true;
610+
return;
613611
}
614612
/* allocate param */
615-
dst->param = calloc(1, paramsize);
616-
if (!dst->param) {
613+
param = calloc(1, paramsize);
614+
if (!param) {
617615
RNP_LOG("allocation failed");
616+
throw rnp::rnp_exception(RNP_ERROR_OUT_OF_MEMORY);
618617
}
619-
return dst->param;
618+
}
619+
620+
// pgp_dest_t constructor do the same job, but we keep this function to preserve api
621+
bool
622+
init_dst_common(pgp_dest_t *dst, size_t paramsize)
623+
{
624+
try {
625+
*dst = pgp_dest_t(paramsize);
626+
} catch (const std::exception &e) {
627+
return false;
628+
}
629+
630+
return true;
620631
}
621632

622633
void
@@ -625,26 +636,26 @@ dst_write(pgp_dest_t *dst, const void *buf, size_t len)
625636
/* we call write function only if all previous calls succeeded */
626637
if ((len > 0) && (dst->write) && (dst->werr == RNP_SUCCESS)) {
627638
/* if cache non-empty and len will overflow it then fill it and write out */
628-
if ((dst->clen > 0) && (dst->clen + len > sizeof(dst->cache))) {
629-
memcpy(dst->cache + dst->clen, buf, sizeof(dst->cache) - dst->clen);
630-
buf = (uint8_t *) buf + sizeof(dst->cache) - dst->clen;
631-
len -= sizeof(dst->cache) - dst->clen;
632-
dst->werr = dst->write(dst, dst->cache, sizeof(dst->cache));
633-
dst->writeb += sizeof(dst->cache);
639+
if ((dst->clen > 0) && (dst->clen + len > dst->cache.size())) {
640+
memcpy(dst->cache.data() + dst->clen, buf, dst->cache.size() - dst->clen);
641+
buf = (uint8_t *) buf + dst->cache.size() - dst->clen;
642+
len -= dst->cache.size() - dst->clen;
643+
dst->werr = dst->write(dst, dst->cache.data(), dst->cache.size());
644+
dst->writeb += dst->cache.size();
634645
dst->clen = 0;
635646
if (dst->werr != RNP_SUCCESS) {
636647
return;
637648
}
638649
}
639650

640651
/* here everything will fit into the cache or cache is empty */
641-
if (dst->no_cache || (len > sizeof(dst->cache))) {
652+
if (dst->no_cache || (len > dst->cache.size())) {
642653
dst->werr = dst->write(dst, buf, len);
643654
if (!dst->werr) {
644655
dst->writeb += len;
645656
}
646657
} else {
647-
memcpy(dst->cache + dst->clen, buf, len);
658+
memcpy(dst->cache.data() + dst->clen, buf, len);
648659
dst->clen += len;
649660
}
650661
}
@@ -672,7 +683,7 @@ void
672683
dst_flush(pgp_dest_t *dst)
673684
{
674685
if ((dst->clen > 0) && (dst->write) && (dst->werr == RNP_SUCCESS)) {
675-
dst->werr = dst->write(dst, dst->cache, dst->clen);
686+
dst->werr = dst->write(dst, dst->cache.data(), dst->clen);
676687
dst->writeb += dst->clen;
677688
dst->clen = 0;
678689
}
@@ -758,11 +769,9 @@ file_dst_close(pgp_dest_t *dst, bool discard)
758769
static rnp_result_t
759770
init_fd_dest(pgp_dest_t *dst, int fd, const char *path)
760771
{
761-
if (!init_dst_common(dst, 0)) {
762-
return RNP_ERROR_OUT_OF_MEMORY;
763-
}
764-
765772
try {
773+
*dst = pgp_dest_t(0);
774+
766775
std::unique_ptr<pgp_dest_file_param_t> param(new pgp_dest_file_param_t());
767776
param->path = path;
768777
param->fd = fd;
@@ -1008,7 +1017,9 @@ init_mem_dest(pgp_dest_t *dst, void *mem, unsigned len)
10081017
{
10091018
pgp_dest_mem_param_t *param;
10101019

1011-
if (!init_dst_common(dst, sizeof(*param))) {
1020+
try {
1021+
*dst = pgp_dest_t(sizeof(*param));
1022+
} catch (const std::exception &e) {
10121023
return RNP_ERROR_OUT_OF_MEMORY;
10131024
}
10141025

src/librepgp/stream-common.h

+18-12
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

+4-2
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,9 @@ init_indent_dest(pgp_dest_t *dst, pgp_dest_t *origdst)
275275
{
276276
pgp_dest_indent_param_t *param;
277277

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

@@ -1289,7 +1291,7 @@ stream_dump_sk_session_key(pgp_source_t *src, pgp_dest_t *dst)
12891291
static bool
12901292
stream_dump_get_aead_hdr(pgp_source_t *src, pgp_aead_hdr_t *hdr)
12911293
{
1292-
pgp_dest_t encdst = {};
1294+
pgp_dest_t encdst;
12931295
uint8_t encpkt[64] = {};
12941296

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

src/librepgp/stream-write.cpp

+15-5
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,9 @@ init_partial_pkt_dst(pgp_dest_t *dst, pgp_dest_t *writedst)
232232
{
233233
pgp_dest_partial_param_t *param;
234234

235-
if (!init_dst_common(dst, sizeof(*param))) {
235+
try {
236+
*dst = pgp_dest_t(sizeof(*param));
237+
} catch (const std::exception &e) {
236238
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
237239
}
238240

@@ -988,7 +990,9 @@ init_encrypted_dst(pgp_write_handler_t *handler, pgp_dest_t *dst, pgp_dest_t *wr
988990
}
989991
}
990992

991-
if (!init_dst_common(dst, 0)) {
993+
try {
994+
*dst = pgp_dest_t(0);
995+
} catch (const std::exception &e) {
992996
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
993997
}
994998
try {
@@ -1495,7 +1499,9 @@ init_signed_dst(pgp_write_handler_t *handler, pgp_dest_t *dst, pgp_dest_t *write
14951499
/* LCOV_EXCL_END */
14961500
}
14971501

1498-
if (!init_dst_common(dst, 0)) {
1502+
try {
1503+
*dst = pgp_dest_t(0);
1504+
} catch (const std::exception &e) {
14991505
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
15001506
}
15011507
try {
@@ -1731,7 +1737,9 @@ init_compressed_dst(pgp_write_handler_t *handler, pgp_dest_t *dst, pgp_dest_t *w
17311737
uint8_t buf;
17321738
int zret;
17331739

1734-
if (!init_dst_common(dst, sizeof(*param))) {
1740+
try {
1741+
*dst = pgp_dest_t(sizeof(*param));
1742+
} catch (const std::exception &e) {
17351743
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
17361744
}
17371745

@@ -1860,7 +1868,9 @@ init_literal_dst(pgp_literal_hdr_t &hdr, pgp_dest_t *dst, pgp_dest_t *writedst)
18601868
{
18611869
pgp_dest_packet_param_t *param;
18621870

1863-
if (!init_dst_common(dst, sizeof(*param))) {
1871+
try {
1872+
*dst = pgp_dest_t(sizeof(*param));
1873+
} catch (const std::exception &e) {
18641874
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
18651875
}
18661876

0 commit comments

Comments
 (0)