blob: a4c6a44c75fc7c6ab69969974cc3a9545c0b1aae [file] [log] [blame]
Qiuhan Ding0cfc1512015-02-17 17:44:11 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
Varun Patila24bd3e2020-11-24 10:08:33 +05303 * Copyright (c) 2020, Regents of the University of California
Qiuhan Ding0cfc1512015-02-17 17:44:11 -08004 *
5 * BSD license, See the LICENSE file for more information
6 *
7 * Author: Qiuhan Ding <qiuhanding@cs.ucla.edu>
8 */
9
10#include "endorse-collection.hpp"
11
12namespace chronochat {
13
14BOOST_CONCEPT_ASSERT((ndn::WireEncodable<EndorseCollection>));
15BOOST_CONCEPT_ASSERT((ndn::WireDecodable<EndorseCollection>));
16
17EndorseCollection::EndorseCollection()
18{
19}
20
21EndorseCollection::EndorseCollection(const Block& endorseWire)
22{
23 this->wireDecode(endorseWire);
24}
25
Varun Patil3d850902020-11-23 12:19:14 +053026template<ndn::encoding::Tag T>
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080027size_t
28EndorseCollection::wireEncode(ndn::EncodingImpl<T>& block) const
29{
30 size_t totalLength = 0;
31
32 // EndorseCollection := ENDORSE-COLLECTION-TYPE TLV-LENGTH
33 // EndorseCollectionEntry+
34 //
35 // EndorseCollectionEntry := ENDORSE-COLLECTION-ENTRY-TYPE TLV-LENGTH
36 // Name
37 // Hash
38 //
39 // Hash := HASH-TYPE TLV-LENGTH
40 // String
41
42 // Entries
43 size_t entryLength = 0;
44 for (std::vector<CollectionEntry>::const_reverse_iterator it = m_entries.rbegin();
45 it != m_entries.rend(); it++) {
46 // Hash
47 const uint8_t* dataWire = reinterpret_cast<const uint8_t*>(it->hash.c_str());
48 entryLength += block.prependByteArrayBlock(tlv::Hash, dataWire, it->hash.length());
49 // CertName
50 entryLength += it->certName.wireEncode(block);
51 // Entry
52 entryLength += block.prependVarNumber(entryLength);
53 entryLength += block.prependVarNumber(tlv::EndorseCollectionEntry);
54 totalLength += entryLength;
55 entryLength = 0;
56 }
57
58 // Profile
59 totalLength += block.prependVarNumber(totalLength);
60 totalLength += block.prependVarNumber(tlv::EndorseCollection);
61
62 return totalLength;
63
64}
65
66const Block&
67EndorseCollection::wireEncode() const
68{
69 ndn::EncodingEstimator estimator;
70 size_t estimatedSize = wireEncode(estimator);
71
72 ndn::EncodingBuffer buffer(estimatedSize, 0);
73 wireEncode(buffer);
74
75 m_wire = buffer.block();
76 m_wire.parse();
77
78 return m_wire;
79}
80
81void
82EndorseCollection::wireDecode(const Block& endorseWire)
83{
84 m_wire = endorseWire;
85 m_wire.parse();
86 m_entries.clear();
87
88 if (m_wire.type() != tlv::EndorseCollection)
Varun Patila24bd3e2020-11-24 10:08:33 +053089 NDN_THROW(Error("Unexpected TLV number when decoding endorse collection packet"));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080090
91 Block::element_const_iterator i = m_wire.elements_begin();
92 if (i == m_wire.elements_end())
Varun Patila24bd3e2020-11-24 10:08:33 +053093 NDN_THROW(Error("Missing Endorse Collection Entry"));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080094 if (i->type() != tlv::EndorseCollectionEntry)
Varun Patila24bd3e2020-11-24 10:08:33 +053095 NDN_THROW(Error("Expect Endorse Collection Entry but get TLV Type " + std::to_string(i->type())));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080096
97 while (i != m_wire.elements_end() && i->type() == tlv::EndorseCollectionEntry) {
98 CollectionEntry entry;
99 Block temp = *i;
100 temp.parse();
101 Block::element_const_iterator j = temp.elements_begin();
102 if (j == temp.elements_end())
Varun Patila24bd3e2020-11-24 10:08:33 +0530103 NDN_THROW(Error("Missing Cert Name"));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800104 if (j->type() != tlv::Name)
Varun Patila24bd3e2020-11-24 10:08:33 +0530105 NDN_THROW(Error("Expect Cert Name but get TLV Type " + std::to_string(j->type())));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800106
107 entry.certName.wireDecode(*j);
108
109 ++j;
110 if (j == temp.elements_end())
Varun Patila24bd3e2020-11-24 10:08:33 +0530111 NDN_THROW(Error("Missing Hash"));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800112 if (j->type() != tlv::Hash)
Varun Patila24bd3e2020-11-24 10:08:33 +0530113 NDN_THROW(Error("Expect Hash but get TLV Type " + std::to_string(j->type())));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800114
115 entry.hash = std::string(reinterpret_cast<const char* >(j->value()),
116 j->value_size());
117 ++j;
Varun Patila24bd3e2020-11-24 10:08:33 +0530118 if (j != temp.elements_end())
119 NDN_THROW(Error("Unexpected element"));
120
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800121 m_entries.push_back(entry);
122 ++i;
123 }
124
Varun Patila24bd3e2020-11-24 10:08:33 +0530125 if (i != m_wire.elements_end())
126 NDN_THROW(Error("Unexpected element"));
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800127}
128
129void
130EndorseCollection::addCollectionEntry(const Name& certName, const std::string& hash) {
131 CollectionEntry entry;
132 entry.certName = certName;
133 entry.hash = hash;
134 m_entries.push_back(entry);
135}
136
137} // namespace chronochat