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 | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame^] | 81 | written += AppendTimestampBlob (start, interest.GetInterestLifetime ()); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 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 | } |
Ilya Moiseenko | d1f1951 | 2011-11-16 14:31:19 -0800 | [diff] [blame] | 91 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame^] | 92 | if (interest.GetNack ()>0) |
Ilya Moiseenko | d1f1951 | 2011-11-16 14:31:19 -0800 | [diff] [blame] | 93 | { |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame^] | 94 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Nack, CcnbParser::CCN_DTAG); |
| 95 | written += AppendNumber (start, interest.GetNack ()); |
Ilya Moiseenko | d1f1951 | 2011-11-16 14:31:19 -0800 | [diff] [blame] | 96 | written += AppendCloser (start); |
| 97 | } |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 98 | written += AppendCloser (start); // </Interest> |
| 99 | |
| 100 | return written; |
| 101 | } |
| 102 | |
| 103 | size_t |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 104 | CcnxEncodingHelper::GetSerializedSize (const CcnxInterestHeader &interest) |
| 105 | { |
| 106 | size_t written = 0; |
| 107 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Interest); // <Interest> |
| 108 | |
| 109 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Name); // <Name> |
| 110 | written += EstimateNameComponents (interest.GetName()); // <Component>...</Component>... |
| 111 | written += 1; // </Name> |
| 112 | |
| 113 | if (interest.GetMinSuffixComponents() >= 0) |
| 114 | { |
| 115 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_MinSuffixComponents); |
| 116 | written += EstimateNumber (interest.GetMinSuffixComponents ()); |
| 117 | written += 1; |
| 118 | } |
| 119 | if (interest.GetMaxSuffixComponents() >= 0) |
| 120 | { |
| 121 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_MaxSuffixComponents); |
| 122 | written += EstimateNumber (interest.GetMaxSuffixComponents ()); |
| 123 | written += 1; |
| 124 | } |
| 125 | if (interest.IsEnabledExclude() && interest.GetExclude().size() > 0) |
| 126 | { |
| 127 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Exclude); |
| 128 | written += EstimateNameComponents (interest.GetExclude()); // <Component>...</Component>... |
| 129 | written += 1; // </Exclude> |
| 130 | } |
| 131 | if (interest.IsEnabledChildSelector()) |
| 132 | { |
| 133 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_ChildSelector); |
| 134 | written += EstimateNumber (1); |
| 135 | written += 1; |
| 136 | } |
| 137 | if (interest.IsEnabledAnswerOriginKind()) |
| 138 | { |
| 139 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_AnswerOriginKind); |
| 140 | written += EstimateNumber (1); |
| 141 | written += 1; |
| 142 | } |
| 143 | if (interest.GetScope() >= 0) |
| 144 | { |
| 145 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Scope); |
| 146 | written += EstimateNumber (interest.GetScope ()); |
| 147 | written += 1; |
| 148 | } |
| 149 | if (!interest.GetInterestLifetime().IsZero()) |
| 150 | { |
| 151 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_InterestLifetime); |
| 152 | written += EstimateTimestampBlob (interest.GetInterestLifetime()); |
| 153 | written += 1; |
| 154 | } |
| 155 | if (interest.GetNonce()>0) |
| 156 | { |
| 157 | written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Nonce, sizeof(uint32_t)); |
| 158 | } |
Ilya Moiseenko | aa17c8e | 2011-10-28 13:04:30 -0700 | [diff] [blame] | 159 | if (interest.IsNack () ) |
| 160 | { |
| 161 | written += EstimateBlockHeader (CcnbParser::NDN_DTAG_Nack); |
| 162 | written += EstimateNumber (1); |
| 163 | written += 1; |
| 164 | } |
| 165 | if (interest.IsCongested () ) |
| 166 | { |
| 167 | written += EstimateBlockHeader (CcnbParser::NDN_DTAG_Congested); |
| 168 | written += EstimateNumber (1); |
| 169 | written += 1; |
| 170 | } |
| 171 | |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 172 | written += 1; // </Interest> |
| 173 | |
| 174 | return written; |
| 175 | } |
| 176 | |
| 177 | size_t |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 178 | CcnxEncodingHelper::Serialize (Buffer::Iterator start, const CcnxContentObjectHeader &contentObject) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 179 | { |
| 180 | size_t written = 0; |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 181 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_ContentObject, CcnbParser::CCN_DTAG); // <ContentObject> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 182 | |
| 183 | // fake signature |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 184 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Signature, CcnbParser::CCN_DTAG); // <Signature> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 185 | // Signature ::= DigestAlgorithm? |
| 186 | // Witness? |
| 187 | // SignatureBits |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 188 | written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_SignatureBits, 0, 0); // <SignatureBits /> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 189 | written += AppendCloser (start); // </Signature> |
| 190 | |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 191 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Name, CcnbParser::CCN_DTAG); // <Name> |
Alexander Afanasyev | 2a5df20 | 2011-08-15 22:39:05 -0700 | [diff] [blame] | 192 | written += AppendNameComponents (start, contentObject.GetName()); // <Component>...</Component>... |
| 193 | written += AppendCloser (start); // </Name> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 194 | |
| 195 | // fake signature |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 196 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_SignedInfo, CcnbParser::CCN_DTAG); // <SignedInfo> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 197 | // SignedInfo ::= PublisherPublicKeyDigest |
| 198 | // Timestamp |
| 199 | // Type? |
| 200 | // FreshnessSeconds? |
| 201 | // FinalBlockID? |
| 202 | // KeyLocator? |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 203 | written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_PublisherPublicKeyDigest, 0, 0); // <PublisherPublicKeyDigest /> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 204 | written += AppendCloser (start); // </SignedInfo> |
| 205 | |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 206 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Content, CcnbParser::CCN_DTAG); // <Content> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 207 | |
Alexander Afanasyev | e91ab75 | 2011-08-31 19:13:40 -0700 | [diff] [blame] | 208 | // there are no closing tags !!! |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 209 | return written; |
| 210 | } |
| 211 | |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 212 | size_t |
| 213 | CcnxEncodingHelper::GetSerializedSize (const CcnxContentObjectHeader &contentObject) |
| 214 | { |
| 215 | size_t written = 0; |
| 216 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_ContentObject); // <ContentObject> |
| 217 | |
| 218 | // fake signature |
| 219 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Signature); // <Signature> |
| 220 | // Signature ::= DigestAlgorithm? |
| 221 | // Witness? |
| 222 | // SignatureBits |
| 223 | written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_SignatureBits, 0); // <SignatureBits /> |
| 224 | written += 1; // </Signature> |
| 225 | |
| 226 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Name); // <Name> |
| 227 | written += EstimateNameComponents (contentObject.GetName()); // <Component>...</Component>... |
| 228 | written += 1; // </Name> |
| 229 | |
| 230 | // fake signature |
| 231 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_SignedInfo); // <SignedInfo> |
| 232 | // SignedInfo ::= PublisherPublicKeyDigest |
| 233 | // Timestamp |
| 234 | // Type? |
| 235 | // FreshnessSeconds? |
| 236 | // FinalBlockID? |
| 237 | // KeyLocator? |
| 238 | written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_PublisherPublicKeyDigest, 0); // <PublisherPublicKeyDigest /> |
| 239 | written += 1; // </SignedInfo> |
| 240 | |
| 241 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Content); // <Content> |
| 242 | |
Alexander Afanasyev | e91ab75 | 2011-08-31 19:13:40 -0700 | [diff] [blame] | 243 | // there are no closing tags !!! |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 244 | return written; |
| 245 | } |
| 246 | |
| 247 | ////////////////////////////////////////////////////////////////////// |
| 248 | ////////////////////////////////////////////////////////////////////// |
| 249 | ////////////////////////////////////////////////////////////////////// |
| 250 | |
| 251 | #define CCN_TT_BITS 3 |
| 252 | #define CCN_TT_MASK ((1 << CCN_TT_BITS) - 1) |
| 253 | #define CCN_MAX_TINY ((1 << (7-CCN_TT_BITS)) - 1) |
| 254 | #define CCN_TT_HBIT ((unsigned char)(1 << 7)) |
| 255 | |
| 256 | size_t |
| 257 | CcnxEncodingHelper::AppendBlockHeader (Buffer::Iterator &start, size_t val, CcnbParser::ccn_tt tt) |
| 258 | { |
| 259 | unsigned char buf[1+8*((sizeof(val)+6)/7)]; |
| 260 | unsigned char *p = &(buf[sizeof(buf)-1]); |
| 261 | size_t n = 1; |
| 262 | p[0] = (CCN_TT_HBIT & ~CcnbParser::CCN_CLOSE) | |
| 263 | ((val & CCN_MAX_TINY) << CCN_TT_BITS) | |
| 264 | (CCN_TT_MASK & tt); |
| 265 | val >>= (7-CCN_TT_BITS); |
| 266 | while (val != 0) { |
| 267 | (--p)[0] = (((unsigned char)val) & ~CCN_TT_HBIT) | CcnbParser::CCN_CLOSE; |
| 268 | n++; |
| 269 | val >>= 7; |
| 270 | } |
| 271 | start.Write (p,n); |
| 272 | return n; |
| 273 | } |
| 274 | |
| 275 | size_t |
| 276 | CcnxEncodingHelper::EstimateBlockHeader (size_t value) |
| 277 | { |
| 278 | value >>= (7-CCN_TT_BITS); |
| 279 | size_t n = 1; |
| 280 | while (value>0) |
| 281 | { |
| 282 | value >>= 7; |
| 283 | n++; |
| 284 | } |
| 285 | return n; |
| 286 | } |
| 287 | |
| 288 | size_t |
| 289 | CcnxEncodingHelper::AppendNumber (Buffer::Iterator &start, uint32_t number) |
| 290 | { |
| 291 | std::ostringstream os; |
| 292 | os << number; |
| 293 | |
| 294 | size_t written = 0; |
| 295 | written += AppendBlockHeader (start, os.str().size(), CcnbParser::CCN_UDATA); |
| 296 | written += os.str().size(); |
| 297 | start.Write (reinterpret_cast<const unsigned char*>(os.str().c_str()), os.str().size()); |
| 298 | |
| 299 | return written; |
| 300 | } |
| 301 | |
| 302 | size_t |
| 303 | CcnxEncodingHelper::EstimateNumber (uint32_t number) |
| 304 | { |
| 305 | std::ostringstream os; |
| 306 | os << number; |
| 307 | return EstimateBlockHeader (os.str ().size ()) + os.str ().size (); |
| 308 | } |
| 309 | |
| 310 | size_t |
| 311 | CcnxEncodingHelper::AppendCloser (Buffer::Iterator &start) |
| 312 | { |
| 313 | start.WriteU8 (CcnbParser::CCN_CLOSE); |
| 314 | return 1; |
| 315 | } |
| 316 | |
| 317 | size_t |
| 318 | CcnxEncodingHelper::AppendNameComponents (Buffer::Iterator &start, const CcnxNameComponents &name) |
| 319 | { |
| 320 | size_t written = 0; |
| 321 | BOOST_FOREACH (const std::string &component, name.GetComponents()) |
| 322 | { |
| 323 | written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Component, |
| 324 | reinterpret_cast<const uint8_t*>(component.c_str()), component.size()); |
| 325 | } |
| 326 | return written; |
| 327 | } |
| 328 | |
| 329 | size_t |
| 330 | CcnxEncodingHelper::EstimateNameComponents (const CcnxNameComponents &name) |
| 331 | { |
| 332 | size_t written = 0; |
| 333 | BOOST_FOREACH (const std::string &component, name.GetComponents()) |
| 334 | { |
| 335 | written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Component, component.size()); |
| 336 | } |
| 337 | return written; |
| 338 | } |
| 339 | |
| 340 | size_t |
| 341 | CcnxEncodingHelper::AppendTimestampBlob (Buffer::Iterator &start, const Time &time) |
| 342 | { |
| 343 | // the original function implements Markers... thought not sure what are these markers for... |
| 344 | |
| 345 | // Determine miminal number of bytes required to store the timestamp |
| 346 | int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough |
| 347 | intmax_t ts = time.ToInteger (Time::S) >> 4; |
| 348 | for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes? |
| 349 | required_bytes++; |
| 350 | |
| 351 | size_t len = AppendBlockHeader(start, required_bytes, CcnbParser::CCN_BLOB); |
| 352 | |
| 353 | // write part with seconds |
| 354 | ts = time.ToInteger (Time::S) >> 4; |
| 355 | for (int i = 0; i < required_bytes - 2; i++) |
| 356 | start.WriteU8 ( ts >> (8 * (required_bytes - 3 - i)) ); |
| 357 | |
| 358 | /* arithmetic contortions are to avoid overflowing 31 bits */ |
| 359 | ts = ((time.ToInteger (Time::S) & 15) << 12) + |
| 360 | (((time.ToInteger (Time::NS) % 1000000000) / 5 * 8 + 195312) / 390625); |
| 361 | for (int i = required_bytes - 2; i < required_bytes; i++) |
| 362 | start.WriteU8 ( ts >> (8 * (required_bytes - 1 - i)) ); |
| 363 | |
| 364 | return len + required_bytes; |
| 365 | } |
| 366 | |
| 367 | size_t |
| 368 | CcnxEncodingHelper::EstimateTimestampBlob (const Time &time) |
| 369 | { |
| 370 | int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough |
| 371 | intmax_t ts = time.ToInteger (Time::S) >> 4; |
| 372 | for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes? |
| 373 | required_bytes++; |
| 374 | |
| 375 | return EstimateBlockHeader (required_bytes) + required_bytes; |
| 376 | } |
| 377 | |
| 378 | size_t |
| 379 | CcnxEncodingHelper::AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag, |
| 380 | const uint8_t *data, size_t size) |
| 381 | { |
| 382 | size_t written = AppendBlockHeader (start, dtag, CcnbParser::CCN_DTAG); |
| 383 | /* 2 */ |
| 384 | if (size>0) |
| 385 | { |
| 386 | written += AppendBlockHeader (start, size, CcnbParser::CCN_BLOB); |
| 387 | start.Write (data, size); |
| 388 | written += size; |
| 389 | /* size */ |
| 390 | } |
| 391 | written += AppendCloser (start); |
| 392 | /* 1 */ |
| 393 | |
| 394 | return written; |
| 395 | } |
| 396 | |
| 397 | size_t |
| 398 | CcnxEncodingHelper::EstimateTaggedBlob (CcnbParser::ccn_dtag dtag, size_t size) |
| 399 | { |
Alexander Afanasyev | e91ab75 | 2011-08-31 19:13:40 -0700 | [diff] [blame] | 400 | if (size>0) |
| 401 | return EstimateBlockHeader (dtag) + EstimateBlockHeader (size) + size + 1; |
| 402 | else |
| 403 | return EstimateBlockHeader (dtag) + 1; |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | |
| 407 | |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 408 | } // namespace ns3 |