Skip to content

Commit

Permalink
Update pgp_key_t::add_sig() to allow insertion of signature in the be…
Browse files Browse the repository at this point in the history
…ginning of a list.
  • Loading branch information
ni4 committed Dec 27, 2023
1 parent 351bf1d commit ac3723d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
26 changes: 19 additions & 7 deletions src/lib/pgp-key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,9 +720,10 @@ pgp_userid_t::has_sig(const pgp_sig_id_t &id) const
}

void
pgp_userid_t::add_sig(const pgp_sig_id_t &sig)
pgp_userid_t::add_sig(const pgp_sig_id_t &sig, bool begin)
{
sigs_.push_back(sig);
size_t idx = begin ? 0 : sigs_.size();
sigs_.insert(sigs_.begin() + idx, sig);
}

void
Expand Down Expand Up @@ -941,18 +942,29 @@ pgp_key_t::replace_sig(const pgp_sig_id_t &id, const pgp_signature_t &newsig)
}

pgp_subsig_t &
pgp_key_t::add_sig(const pgp_signature_t &sig, size_t uid)
pgp_key_t::add_sig(const pgp_signature_t &sig, size_t uid, bool begin)
{
const pgp_sig_id_t sigid = sig.get_id();
sigs_map_.erase(sigid);
pgp_subsig_t &res = sigs_map_.emplace(std::make_pair(sigid, sig)).first->second;
res.uid = uid;
sigs_.push_back(sigid);
if (uid == PGP_UID_NONE) {
keysigs_.push_back(sigid);
} else {
uids_[uid].add_sig(sigid);
size_t idx = begin ? 0 : keysigs_.size();
sigs_.insert(sigs_.begin() + idx, sigid);
keysigs_.insert(keysigs_.begin() + idx, sigid);
return res;
}

/* Calculate correct position in sigs_ */
size_t idx = keysigs_.size();
for (size_t u = 0; u < uid; u++) {
idx += uids_[u].sig_count();
}
if (!begin) {
idx += uids_[uid].sig_count();
}
sigs_.insert(sigs_.begin() + idx, sigid);
uids_[uid].add_sig(sigid, begin);
return res;
}

Expand Down
6 changes: 4 additions & 2 deletions src/lib/pgp-key.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ typedef struct pgp_userid_t {
size_t sig_count() const;
const pgp_sig_id_t &get_sig(size_t idx) const;
bool has_sig(const pgp_sig_id_t &id) const;
void add_sig(const pgp_sig_id_t &sig);
void add_sig(const pgp_sig_id_t &sig, bool begin = false);
void replace_sig(const pgp_sig_id_t &id, const pgp_sig_id_t &newsig);
bool del_sig(const pgp_sig_id_t &id);
void clear_sigs();
Expand Down Expand Up @@ -188,7 +188,9 @@ struct pgp_key_t {
pgp_subsig_t & replace_sig(const pgp_sig_id_t &id, const pgp_signature_t &newsig);
pgp_subsig_t & get_sig(const pgp_sig_id_t &id);
const pgp_subsig_t &get_sig(const pgp_sig_id_t &id) const;
pgp_subsig_t & add_sig(const pgp_signature_t &sig, size_t uid = PGP_UID_NONE);
pgp_subsig_t & add_sig(const pgp_signature_t &sig,
size_t uid = PGP_UID_NONE,
bool begin = false);
bool del_sig(const pgp_sig_id_t &sigid);
size_t del_sigs(const std::vector<pgp_sig_id_t> &sigs);
size_t keysig_count() const;
Expand Down

0 comments on commit ac3723d

Please sign in to comment.