21 #include "wire-ccnb.h"
24 #include <boost/foreach.hpp>
25 #include "ccnb-parser/common.h"
26 #include "ccnb-parser/visitors/name-visitor.h"
27 #include "ccnb-parser/syntax-tree/block.h"
38 #define CCN_TT_MASK ((1 << CCN_TT_BITS) - 1)
39 #define CCN_MAX_TINY ((1 << (7-CCN_TT_BITS)) - 1)
40 #define CCN_TT_HBIT ((unsigned char)(1 << 7))
43 Ccnb::AppendBlockHeader (Buffer::Iterator &start,
size_t val, uint32_t tt)
45 unsigned char buf[1+8*((
sizeof(val)+6)/7)];
46 unsigned char *p = &(buf[
sizeof(buf)-1]);
48 p[0] = (CCN_TT_HBIT & ~CcnbParser::CCN_CLOSE) |
49 ((val & CCN_MAX_TINY) << CCN_TT_BITS) |
51 val >>= (7-CCN_TT_BITS);
53 (--p)[0] = (((
unsigned char)val) & ~CCN_TT_HBIT) | CcnbParser::CCN_CLOSE;
62 Ccnb::EstimateBlockHeader (
size_t value)
64 value >>= (7-CCN_TT_BITS);
75 Ccnb::AppendNumber (Buffer::Iterator &start, uint32_t number)
77 std::ostringstream os;
81 written += AppendBlockHeader (start, os.str().size(), CcnbParser::CCN_UDATA);
82 written += os.str().size();
83 start.Write (reinterpret_cast<const unsigned char*>(os.str().c_str()), os.str().size());
89 Ccnb::EstimateNumber (uint32_t number)
91 std::ostringstream os;
93 return EstimateBlockHeader (os.str ().size ()) + os.str ().size ();
97 Ccnb::AppendCloser (Buffer::Iterator &start)
99 start.WriteU8 (CcnbParser::CCN_CLOSE);
104 Ccnb::AppendTimestampBlob (Buffer::Iterator &start,
const Time &time)
109 int required_bytes = 2;
110 intmax_t ts = time.ToInteger (Time::S) >> 4;
111 for (; required_bytes < 7 && ts != 0; ts >>= 8)
114 size_t len = AppendBlockHeader(start, required_bytes, CcnbParser::CCN_BLOB);
117 ts = time.ToInteger (Time::S) >> 4;
118 for (
int i = 0; i < required_bytes - 2; i++)
119 start.WriteU8 ( ts >> (8 * (required_bytes - 3 - i)) );
122 ts = ((time.ToInteger (Time::S) & 15) << 12) +
123 (((time.ToInteger (Time::NS) % 1000000000) / 5 * 8 + 195312) / 390625);
124 for (
int i = required_bytes - 2; i < required_bytes; i++)
125 start.WriteU8 ( ts >> (8 * (required_bytes - 1 - i)) );
127 return len + required_bytes;
131 Ccnb::EstimateTimestampBlob (
const Time &time)
133 int required_bytes = 2;
134 intmax_t ts = time.ToInteger (Time::S) >> 4;
135 for (; required_bytes < 7 && ts != 0; ts >>= 8)
138 return EstimateBlockHeader (required_bytes) + required_bytes;
142 Ccnb::AppendTaggedBlob (Buffer::Iterator &start, uint32_t dtag,
143 const uint8_t *data,
size_t size)
145 size_t written = AppendBlockHeader (start, dtag, CcnbParser::CCN_DTAG);
149 written += AppendBlockHeader (start, size, CcnbParser::CCN_BLOB);
150 start.Write (data, size);
154 written += AppendCloser (start);
161 Ccnb::AppendTaggedBlobWithPadding (Buffer::Iterator &start, uint32_t dtag,
163 const uint8_t *data,
size_t size)
168 return AppendTaggedBlob (start, dtag, data, size);
172 size_t written = AppendBlockHeader (start, dtag, CcnbParser::CCN_DTAG);
177 written += AppendBlockHeader (start, length, CcnbParser::CCN_BLOB);
178 start.Write (data, size);
179 start.WriteU8 (0, length - size);
183 written += AppendCloser (start);
190 Ccnb::EstimateTaggedBlob (uint32_t dtag,
size_t size)
193 return EstimateBlockHeader (dtag) + EstimateBlockHeader (size) + size + 1;
195 return EstimateBlockHeader (dtag) + 1;
199 Ccnb::AppendString (Buffer::Iterator &start, uint32_t dtag,
200 const std::string &
string)
202 size_t written = AppendBlockHeader (start, dtag, CcnbParser::CCN_DTAG);
204 written += AppendBlockHeader (start,
string.size (), CcnbParser::CCN_UDATA);
205 start.Write (reinterpret_cast<const uint8_t*> (
string.c_str ()),
string.size ());
206 written +=
string.size ();
208 written += AppendCloser (start);
214 Ccnb::EstimateString (uint32_t dtag,
const std::string &
string)
216 return EstimateBlockHeader (dtag) + EstimateBlockHeader (
string.size ()) +
string.size () + 1;
220 Ccnb::SerializeName (Buffer::Iterator &start,
const Name &name)
223 written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Name, CcnbParser::CCN_DTAG);
226 written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Component,
227 reinterpret_cast<const uint8_t*>(component.
buf ()), component.size());
229 written += AppendCloser (start);
234 Ccnb::SerializedSizeName (
const Name &name)
237 written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Name);
240 written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Component, component.size ());
247 Ccnb::DeserializeName (Buffer::Iterator &i)
249 Ptr<Name> name = Create<Name> ();
252 Ptr<CcnbParser::Block> root = CcnbParser::Block::ParseBlock (i);
253 root->accept (nameVisitor, GetPointer (name));
char * buf()
Get pointer to the first byte of the binary blob.
Visitor to obtain fill CcnxName object with name components.
Class to representing binary blob of NDN name component.