@@ -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
622633void
@@ -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 }
672683dst_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)
758769static rnp_result_t
759770init_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
0 commit comments