Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: |
| 19 | */ |
| 20 | |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 21 | #include "ccnx-encoding-helper.h" |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 22 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 23 | #include "ns3/ccnx-name-components.h" |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 24 | #include "ns3/ccnx-interest-header.h" |
| 25 | #include "ns3/ccnx-content-object-header.h" |
| 26 | |
| 27 | #include <sstream> |
Alexander Afanasyev | 2a5df20 | 2011-08-15 22:39:05 -0700 | [diff] [blame] | 28 | #include <boost/foreach.hpp> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 29 | |
| 30 | namespace ns3 { |
| 31 | |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 32 | size_t |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 33 | CcnxEncodingHelper::Serialize (Buffer::Iterator start, const CcnxInterestHeader &interest) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 34 | { |
| 35 | size_t written = 0; |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 36 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Interest, CcnbParser::CCN_DTAG); // <Interest> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 37 | |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 38 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Name, CcnbParser::CCN_DTAG); // <Name> |
Alexander Afanasyev | 2a5df20 | 2011-08-15 22:39:05 -0700 | [diff] [blame] | 39 | written += AppendNameComponents (start, interest.GetName()); // <Component>...</Component>... |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 40 | written += AppendCloser (start); // </Name> |
| 41 | |
| 42 | if (interest.GetMinSuffixComponents() >= 0) |
| 43 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 44 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_MinSuffixComponents, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 45 | written += AppendNumber (start, interest.GetMinSuffixComponents ()); |
| 46 | written += AppendCloser (start); |
| 47 | } |
| 48 | if (interest.GetMaxSuffixComponents() >= 0) |
| 49 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 50 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_MaxSuffixComponents, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 51 | written += AppendNumber (start, interest.GetMaxSuffixComponents ()); |
| 52 | written += AppendCloser (start); |
| 53 | } |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 54 | if (interest.IsEnabledExclude() && interest.GetExclude().size() > 0) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 55 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 56 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Exclude, CcnbParser::CCN_DTAG); // <Exclude> |
Alexander Afanasyev | 2a5df20 | 2011-08-15 22:39:05 -0700 | [diff] [blame] | 57 | written += AppendNameComponents (start, interest.GetExclude()); // <Component>...</Component>... |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 58 | written += AppendCloser (start); // </Exclude> |
| 59 | } |
| 60 | if (interest.IsEnabledChildSelector()) |
| 61 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 62 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_ChildSelector, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 63 | written += AppendNumber (start, 1); |
| 64 | written += AppendCloser (start); |
| 65 | } |
| 66 | if (interest.IsEnabledAnswerOriginKind()) |
| 67 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 68 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_AnswerOriginKind, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 69 | written += AppendNumber (start, 1); |
| 70 | written += AppendCloser (start); |
| 71 | } |
| 72 | if (interest.GetScope() >= 0) |
| 73 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 74 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Scope, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 75 | written += AppendNumber (start, interest.GetScope ()); |
| 76 | written += AppendCloser (start); |
| 77 | } |
| 78 | if (!interest.GetInterestLifetime().IsZero()) |
| 79 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 80 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_InterestLifetime, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 81 | written += AppendTimestampBlob (start, interest.GetInterestLifetime()); |
| 82 | written += AppendCloser (start); |
| 83 | } |
| 84 | if (interest.GetNonce()>0) |
| 85 | { |
| 86 | uint32_t nonce = interest.GetNonce(); |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 87 | written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Nonce, |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 88 | reinterpret_cast<const uint8_t*>(&nonce), |
| 89 | sizeof(nonce)); |
| 90 | } |
| 91 | written += AppendCloser (start); // </Interest> |
| 92 | |
| 93 | return written; |
| 94 | } |
| 95 | |
| 96 | size_t |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 97 | CcnxEncodingHelper::GetSerializedSize (const CcnxInterestHeader &interest) |
| 98 | { |
| 99 | size_t written = 0; |
| 100 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Interest); // <Interest> |
| 101 | |
| 102 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Name); // <Name> |
| 103 | written += EstimateNameComponents (interest.GetName()); // <Component>...</Component>... |
| 104 | written += 1; // </Name> |
| 105 | |
| 106 | if (interest.GetMinSuffixComponents() >= 0) |
| 107 | { |
| 108 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_MinSuffixComponents); |
| 109 | written += EstimateNumber (interest.GetMinSuffixComponents ()); |
| 110 | written += 1; |
| 111 | } |
| 112 | if (interest.GetMaxSuffixComponents() >= 0) |
| 113 | { |
| 114 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_MaxSuffixComponents); |
| 115 | written += EstimateNumber (interest.GetMaxSuffixComponents ()); |
| 116 | written += 1; |
| 117 | } |
| 118 | if (interest.IsEnabledExclude() && interest.GetExclude().size() > 0) |
| 119 | { |
| 120 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Exclude); |
| 121 | written += EstimateNameComponents (interest.GetExclude()); // <Component>...</Component>... |
| 122 | written += 1; // </Exclude> |
| 123 | } |
| 124 | if (interest.IsEnabledChildSelector()) |
| 125 | { |
| 126 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_ChildSelector); |
| 127 | written += EstimateNumber (1); |
| 128 | written += 1; |
| 129 | } |
| 130 | if (interest.IsEnabledAnswerOriginKind()) |
| 131 | { |
| 132 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_AnswerOriginKind); |
| 133 | written += EstimateNumber (1); |
| 134 | written += 1; |
| 135 | } |
| 136 | if (interest.GetScope() >= 0) |
| 137 | { |
| 138 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Scope); |
| 139 | written += EstimateNumber (interest.GetScope ()); |
| 140 | written += 1; |
| 141 | } |
| 142 | if (!interest.GetInterestLifetime().IsZero()) |
| 143 | { |
| 144 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_InterestLifetime); |
| 145 | written += EstimateTimestampBlob (interest.GetInterestLifetime()); |
| 146 | written += 1; |
| 147 | } |
| 148 | if (interest.GetNonce()>0) |
| 149 | { |
| 150 | written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Nonce, sizeof(uint32_t)); |
| 151 | } |
| 152 | written += 1; // </Interest> |
| 153 | |
| 154 | return written; |
| 155 | } |
| 156 | |
| 157 | size_t |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 158 | CcnxEncodingHelper::Serialize (Buffer::Iterator start, const CcnxContentObjectHeader &contentObject) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 159 | { |
| 160 | size_t written = 0; |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 161 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_ContentObject, CcnbParser::CCN_DTAG); // <ContentObject> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 162 | |
| 163 | // fake signature |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 164 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Signature, CcnbParser::CCN_DTAG); // <Signature> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 165 | // Signature ::= DigestAlgorithm? |
| 166 | // Witness? |
| 167 | // SignatureBits |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 168 | written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_SignatureBits, 0, 0); // <SignatureBits /> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 169 | written += AppendCloser (start); // </Signature> |
| 170 | |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 171 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Name, CcnbParser::CCN_DTAG); // <Name> |
Alexander Afanasyev | 2a5df20 | 2011-08-15 22:39:05 -0700 | [diff] [blame] | 172 | written += AppendNameComponents (start, contentObject.GetName()); // <Component>...</Component>... |
| 173 | written += AppendCloser (start); // </Name> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 174 | |
| 175 | // fake signature |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 176 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_SignedInfo, CcnbParser::CCN_DTAG); // <SignedInfo> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 177 | // SignedInfo ::= PublisherPublicKeyDigest |
| 178 | // Timestamp |
| 179 | // Type? |
| 180 | // FreshnessSeconds? |
| 181 | // FinalBlockID? |
| 182 | // KeyLocator? |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 183 | written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_PublisherPublicKeyDigest, 0, 0); // <PublisherPublicKeyDigest /> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 184 | written += AppendCloser (start); // </SignedInfo> |
| 185 | |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 186 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Content, CcnbParser::CCN_DTAG); // <Content> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 187 | |
Alexander Afanasyev | e91ab75 | 2011-08-31 19:13:40 -0700 | [diff] [blame^] | 188 | // there are no closing tags !!! |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 189 | return written; |
| 190 | } |
| 191 | |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 192 | size_t |
| 193 | CcnxEncodingHelper::GetSerializedSize (const CcnxContentObjectHeader &contentObject) |
| 194 | { |
| 195 | size_t written = 0; |
| 196 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_ContentObject); // <ContentObject> |
| 197 | |
| 198 | // fake signature |
| 199 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Signature); // <Signature> |
| 200 | // Signature ::= DigestAlgorithm? |
| 201 | // Witness? |
| 202 | // SignatureBits |
| 203 | written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_SignatureBits, 0); // <SignatureBits /> |
| 204 | written += 1; // </Signature> |
| 205 | |
| 206 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Name); // <Name> |
| 207 | written += EstimateNameComponents (contentObject.GetName()); // <Component>...</Component>... |
| 208 | written += 1; // </Name> |
| 209 | |
| 210 | // fake signature |
| 211 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_SignedInfo); // <SignedInfo> |
| 212 | // SignedInfo ::= PublisherPublicKeyDigest |
| 213 | // Timestamp |
| 214 | // Type? |
| 215 | // FreshnessSeconds? |
| 216 | // FinalBlockID? |
| 217 | // KeyLocator? |
| 218 | written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_PublisherPublicKeyDigest, 0); // <PublisherPublicKeyDigest /> |
| 219 | written += 1; // </SignedInfo> |
| 220 | |
| 221 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Content); // <Content> |
| 222 | |
Alexander Afanasyev | e91ab75 | 2011-08-31 19:13:40 -0700 | [diff] [blame^] | 223 | // there are no closing tags !!! |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 224 | return written; |
| 225 | } |
| 226 | |
| 227 | ////////////////////////////////////////////////////////////////////// |
| 228 | ////////////////////////////////////////////////////////////////////// |
| 229 | ////////////////////////////////////////////////////////////////////// |
| 230 | |
| 231 | #define CCN_TT_BITS 3 |
| 232 | #define CCN_TT_MASK ((1 << CCN_TT_BITS) - 1) |
| 233 | #define CCN_MAX_TINY ((1 << (7-CCN_TT_BITS)) - 1) |
| 234 | #define CCN_TT_HBIT ((unsigned char)(1 << 7)) |
| 235 | |
| 236 | size_t |
| 237 | CcnxEncodingHelper::AppendBlockHeader (Buffer::Iterator &start, size_t val, CcnbParser::ccn_tt tt) |
| 238 | { |
| 239 | unsigned char buf[1+8*((sizeof(val)+6)/7)]; |
| 240 | unsigned char *p = &(buf[sizeof(buf)-1]); |
| 241 | size_t n = 1; |
| 242 | p[0] = (CCN_TT_HBIT & ~CcnbParser::CCN_CLOSE) | |
| 243 | ((val & CCN_MAX_TINY) << CCN_TT_BITS) | |
| 244 | (CCN_TT_MASK & tt); |
| 245 | val >>= (7-CCN_TT_BITS); |
| 246 | while (val != 0) { |
| 247 | (--p)[0] = (((unsigned char)val) & ~CCN_TT_HBIT) | CcnbParser::CCN_CLOSE; |
| 248 | n++; |
| 249 | val >>= 7; |
| 250 | } |
| 251 | start.Write (p,n); |
| 252 | return n; |
| 253 | } |
| 254 | |
| 255 | size_t |
| 256 | CcnxEncodingHelper::EstimateBlockHeader (size_t value) |
| 257 | { |
| 258 | value >>= (7-CCN_TT_BITS); |
| 259 | size_t n = 1; |
| 260 | while (value>0) |
| 261 | { |
| 262 | value >>= 7; |
| 263 | n++; |
| 264 | } |
| 265 | return n; |
| 266 | } |
| 267 | |
| 268 | size_t |
| 269 | CcnxEncodingHelper::AppendNumber (Buffer::Iterator &start, uint32_t number) |
| 270 | { |
| 271 | std::ostringstream os; |
| 272 | os << number; |
| 273 | |
| 274 | size_t written = 0; |
| 275 | written += AppendBlockHeader (start, os.str().size(), CcnbParser::CCN_UDATA); |
| 276 | written += os.str().size(); |
| 277 | start.Write (reinterpret_cast<const unsigned char*>(os.str().c_str()), os.str().size()); |
| 278 | |
| 279 | return written; |
| 280 | } |
| 281 | |
| 282 | size_t |
| 283 | CcnxEncodingHelper::EstimateNumber (uint32_t number) |
| 284 | { |
| 285 | std::ostringstream os; |
| 286 | os << number; |
| 287 | return EstimateBlockHeader (os.str ().size ()) + os.str ().size (); |
| 288 | } |
| 289 | |
| 290 | size_t |
| 291 | CcnxEncodingHelper::AppendCloser (Buffer::Iterator &start) |
| 292 | { |
| 293 | start.WriteU8 (CcnbParser::CCN_CLOSE); |
| 294 | return 1; |
| 295 | } |
| 296 | |
| 297 | size_t |
| 298 | CcnxEncodingHelper::AppendNameComponents (Buffer::Iterator &start, const CcnxNameComponents &name) |
| 299 | { |
| 300 | size_t written = 0; |
| 301 | BOOST_FOREACH (const std::string &component, name.GetComponents()) |
| 302 | { |
| 303 | written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Component, |
| 304 | reinterpret_cast<const uint8_t*>(component.c_str()), component.size()); |
| 305 | } |
| 306 | return written; |
| 307 | } |
| 308 | |
| 309 | size_t |
| 310 | CcnxEncodingHelper::EstimateNameComponents (const CcnxNameComponents &name) |
| 311 | { |
| 312 | size_t written = 0; |
| 313 | BOOST_FOREACH (const std::string &component, name.GetComponents()) |
| 314 | { |
| 315 | written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Component, component.size()); |
| 316 | } |
| 317 | return written; |
| 318 | } |
| 319 | |
| 320 | size_t |
| 321 | CcnxEncodingHelper::AppendTimestampBlob (Buffer::Iterator &start, const Time &time) |
| 322 | { |
| 323 | // the original function implements Markers... thought not sure what are these markers for... |
| 324 | |
| 325 | // Determine miminal number of bytes required to store the timestamp |
| 326 | int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough |
| 327 | intmax_t ts = time.ToInteger (Time::S) >> 4; |
| 328 | for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes? |
| 329 | required_bytes++; |
| 330 | |
| 331 | size_t len = AppendBlockHeader(start, required_bytes, CcnbParser::CCN_BLOB); |
| 332 | |
| 333 | // write part with seconds |
| 334 | ts = time.ToInteger (Time::S) >> 4; |
| 335 | for (int i = 0; i < required_bytes - 2; i++) |
| 336 | start.WriteU8 ( ts >> (8 * (required_bytes - 3 - i)) ); |
| 337 | |
| 338 | /* arithmetic contortions are to avoid overflowing 31 bits */ |
| 339 | ts = ((time.ToInteger (Time::S) & 15) << 12) + |
| 340 | (((time.ToInteger (Time::NS) % 1000000000) / 5 * 8 + 195312) / 390625); |
| 341 | for (int i = required_bytes - 2; i < required_bytes; i++) |
| 342 | start.WriteU8 ( ts >> (8 * (required_bytes - 1 - i)) ); |
| 343 | |
| 344 | return len + required_bytes; |
| 345 | } |
| 346 | |
| 347 | size_t |
| 348 | CcnxEncodingHelper::EstimateTimestampBlob (const Time &time) |
| 349 | { |
| 350 | int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough |
| 351 | intmax_t ts = time.ToInteger (Time::S) >> 4; |
| 352 | for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes? |
| 353 | required_bytes++; |
| 354 | |
| 355 | return EstimateBlockHeader (required_bytes) + required_bytes; |
| 356 | } |
| 357 | |
| 358 | size_t |
| 359 | CcnxEncodingHelper::AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag, |
| 360 | const uint8_t *data, size_t size) |
| 361 | { |
| 362 | size_t written = AppendBlockHeader (start, dtag, CcnbParser::CCN_DTAG); |
| 363 | /* 2 */ |
| 364 | if (size>0) |
| 365 | { |
| 366 | written += AppendBlockHeader (start, size, CcnbParser::CCN_BLOB); |
| 367 | start.Write (data, size); |
| 368 | written += size; |
| 369 | /* size */ |
| 370 | } |
| 371 | written += AppendCloser (start); |
| 372 | /* 1 */ |
| 373 | |
| 374 | return written; |
| 375 | } |
| 376 | |
| 377 | size_t |
| 378 | CcnxEncodingHelper::EstimateTaggedBlob (CcnbParser::ccn_dtag dtag, size_t size) |
| 379 | { |
Alexander Afanasyev | e91ab75 | 2011-08-31 19:13:40 -0700 | [diff] [blame^] | 380 | if (size>0) |
| 381 | return EstimateBlockHeader (dtag) + EstimateBlockHeader (size) + size + 1; |
| 382 | else |
| 383 | return EstimateBlockHeader (dtag) + 1; |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | |
| 387 | |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 388 | } // namespace ns3 |