Coverage Report

Created: 2025-07-18 06:18

/src/rnp/src/lib/userid.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2017-2025 [Ribose Inc](https://www.ribose.com).
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
18
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24
 * POSSIBILITY OF SUCH DAMAGE.
25
 */
26
27
#include "userid.hpp"
28
#include <algorithm>
29
#include <stdexcept>
30
31
namespace rnp {
32
33
0
UserID::UserID(const pgp_userid_pkt_t &uidpkt) : UserID()
34
0
{
35
    /* copy packet data */
36
0
    pkt = uidpkt;
37
0
    rawpkt = RawPacket(uidpkt);
38
    /* populate uid string */
39
0
    if (uidpkt.tag == PGP_PKT_USER_ID) {
40
0
        str.assign(uidpkt.uid.data(), uidpkt.uid.data() + uidpkt.uid.size());
41
0
    } else {
42
0
        str = "(photo)";
43
0
    }
44
0
}
45
46
size_t
47
UserID::sig_count() const
48
0
{
49
0
    return sigs_.size();
50
0
}
51
52
const pgp::SigID &
53
UserID::get_sig(size_t idx) const
54
0
{
55
0
    return sigs_.at(idx);
56
0
}
57
58
bool
59
UserID::has_sig(const pgp::SigID &id) const
60
0
{
61
0
    return std::find(sigs_.begin(), sigs_.end(), id) != sigs_.end();
62
0
}
63
64
void
65
UserID::add_sig(const pgp::SigID &sig, bool begin)
66
0
{
67
0
    size_t idx = begin ? 0 : sigs_.size();
68
0
    sigs_.insert(sigs_.begin() + idx, sig);
69
0
}
70
71
void
72
UserID::replace_sig(const pgp::SigID &id, const pgp::SigID &newsig)
73
0
{
74
0
    auto it = std::find(sigs_.begin(), sigs_.end(), id);
75
0
    if (it == sigs_.end()) {
76
0
        throw std::invalid_argument("id");
77
0
    }
78
0
    *it = newsig;
79
0
}
80
81
bool
82
UserID::del_sig(const pgp::SigID &id)
83
0
{
84
0
    auto it = std::find(sigs_.begin(), sigs_.end(), id);
85
0
    if (it == sigs_.end()) {
86
0
        return false;
87
0
    }
88
0
    sigs_.erase(it);
89
0
    return true;
90
0
}
91
92
void
93
UserID::clear_sigs()
94
0
{
95
0
    sigs_.clear();
96
0
}
97
98
} // namespace rnp