@@ -603,20 +603,31 @@ mem_src_get_memory(pgp_source_t *src, bool own)
603
603
return param->memory ;
604
604
}
605
605
606
- bool
607
- init_dst_common (pgp_dest_t *dst, size_t paramsize)
606
+ pgp_dest_t ::pgp_dest_t (size_t paramsize)
608
607
{
609
- memset (dst, 0 , sizeof (*dst));
610
- dst->werr = RNP_SUCCESS;
608
+ werr = RNP_SUCCESS;
611
609
if (!paramsize) {
612
- return true ;
610
+ return ;
613
611
}
614
612
/* allocate param */
615
- dst-> param = calloc (1 , paramsize);
616
- if (!dst-> param ) {
613
+ param = calloc (1 , paramsize);
614
+ if (!param) {
617
615
RNP_LOG (" allocation failed" );
616
+ throw rnp::rnp_exception (RNP_ERROR_OUT_OF_MEMORY);
618
617
}
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 ;
620
631
}
621
632
622
633
void
@@ -625,26 +636,26 @@ dst_write(pgp_dest_t *dst, const void *buf, size_t len)
625
636
/* we call write function only if all previous calls succeeded */
626
637
if ((len > 0 ) && (dst->write ) && (dst->werr == RNP_SUCCESS)) {
627
638
/* 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 ( );
634
645
dst->clen = 0 ;
635
646
if (dst->werr != RNP_SUCCESS) {
636
647
return ;
637
648
}
638
649
}
639
650
640
651
/* 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 ( ))) {
642
653
dst->werr = dst->write (dst, buf, len);
643
654
if (!dst->werr ) {
644
655
dst->writeb += len;
645
656
}
646
657
} else {
647
- memcpy (dst->cache + dst->clen , buf, len);
658
+ memcpy (dst->cache . data () + dst->clen , buf, len);
648
659
dst->clen += len;
649
660
}
650
661
}
672
683
dst_flush (pgp_dest_t *dst)
673
684
{
674
685
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 );
676
687
dst->writeb += dst->clen ;
677
688
dst->clen = 0 ;
678
689
}
@@ -758,11 +769,9 @@ file_dst_close(pgp_dest_t *dst, bool discard)
758
769
static rnp_result_t
759
770
init_fd_dest (pgp_dest_t *dst, int fd, const char *path)
760
771
{
761
- if (!init_dst_common (dst, 0 )) {
762
- return RNP_ERROR_OUT_OF_MEMORY;
763
- }
764
-
765
772
try {
773
+ *dst = pgp_dest_t (0 );
774
+
766
775
std::unique_ptr<pgp_dest_file_param_t > param (new pgp_dest_file_param_t ());
767
776
param->path = path;
768
777
param->fd = fd;
@@ -1008,7 +1017,9 @@ init_mem_dest(pgp_dest_t *dst, void *mem, unsigned len)
1008
1017
{
1009
1018
pgp_dest_mem_param_t *param;
1010
1019
1011
- if (!init_dst_common (dst, sizeof (*param))) {
1020
+ try {
1021
+ *dst = pgp_dest_t (sizeof (*param));
1022
+ } catch (const std::exception &e) {
1012
1023
return RNP_ERROR_OUT_OF_MEMORY;
1013
1024
}
1014
1025
0 commit comments