blob: 805582aa7ceb4744be2bb3aa2ffbde2b15ba8765 [file] [log] [blame]
Alexander Afanasyev8b379052011-08-21 16:58:20 -07001/* -*- 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: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
21#include "ccnb-parser-block.h"
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070022
23#include "ccnb-parser-blob.h"
24#include "ccnb-parser-udata.h"
25#include "ccnb-parser-tag.h"
26#include "ccnb-parser-dtag.h"
27#include "ccnb-parser-attr.h"
28#include "ccnb-parser-dattr.h"
29#include "ccnb-parser-ext.h"
Alexander Afanasyev8b379052011-08-21 16:58:20 -070030
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080031#include "ns3/log.h"
32
33NS_LOG_COMPONENT_DEFINE ("CcnbParserBlock");
34
Alexander Afanasyev8b379052011-08-21 16:58:20 -070035namespace ns3 {
36namespace CcnbParser {
37
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070038/// @cond include_hidden
Alexander Afanasyev8b379052011-08-21 16:58:20 -070039const uint8_t CCN_TT_BITS = 3;
40const uint8_t CCN_TT_MASK = ((1 << CCN_TT_BITS) - 1);
41const uint8_t CCN_MAX_TINY= ((1 << (7-CCN_TT_BITS)) - 1);
42const uint8_t CCN_TT_HBIT = ((uint8_t)(1 << 7));
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070043/// @endcond
Alexander Afanasyev8b379052011-08-21 16:58:20 -070044
Alexander Afanasyeve91ab752011-08-31 19:13:40 -070045// int Block::counter = 0;
46
Alexander Afanasyev8b379052011-08-21 16:58:20 -070047Ptr<Block> Block::ParseBlock (Buffer::Iterator &start)
48{
Alexander Afanasyeve91ab752011-08-31 19:13:40 -070049 // std::cout << "<< pos: " << counter << "\n";
Alexander Afanasyev8b379052011-08-21 16:58:20 -070050 uint32_t value = 0;
51
52 // We will have problems if length field is more than 32 bits. Though it's really impossible
53 uint8_t byte = 0;
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070054 while (!start.IsEnd() && !(byte & CCN_TT_HBIT))
Alexander Afanasyev8b379052011-08-21 16:58:20 -070055 {
56 value <<= 8;
57 value += byte;
58 byte = start.ReadU8 ();
Alexander Afanasyeve91ab752011-08-31 19:13:40 -070059 // Block::counter ++;
Alexander Afanasyev8b379052011-08-21 16:58:20 -070060 }
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070061 if (start.IsEnd())
62 CcnbDecodingException ();
63
Alexander Afanasyev8b379052011-08-21 16:58:20 -070064 value <<= 4;
65 value += ( (byte&(~CCN_TT_HBIT)) >> 3);
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080066
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070067 /**
68 * Huh. After fighting with NS-3, it became apparent that Create<T>(...) construct
69 * doesn't work with references. Just simply doesn't work. wtf?
70 */
Alexander Afanasyev8b379052011-08-21 16:58:20 -070071 switch (byte & CCN_TT_MASK)
72 {
73 case CCN_BLOB:
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080074 return Ptr<Blob> (new Blob(start, value), false);
Alexander Afanasyev8b379052011-08-21 16:58:20 -070075 case CCN_UDATA:
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080076 return Ptr<Udata> (new Udata(start, value), false);
Alexander Afanasyev8b379052011-08-21 16:58:20 -070077 case CCN_TAG:
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080078 return Ptr<Tag> (new Tag(start, value), false);
Alexander Afanasyev8b379052011-08-21 16:58:20 -070079 case CCN_ATTR:
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080080 return Ptr<Attr> (new Attr(start, value), false);
Alexander Afanasyev8b379052011-08-21 16:58:20 -070081 case CCN_DTAG:
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080082 return Ptr<Dtag> (new Dtag(start, value), false);
Alexander Afanasyev8b379052011-08-21 16:58:20 -070083 case CCN_DATTR:
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080084 return Ptr<Dattr> (new Dattr(start, value), false);
Alexander Afanasyev8b379052011-08-21 16:58:20 -070085 case CCN_EXT:
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080086 return Ptr<Ext> (new Ext(start, value), false);
Alexander Afanasyev8b379052011-08-21 16:58:20 -070087 default:
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070088 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -070089 }
90}
91
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080092Block::~Block ()
93{
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080094}
95
Alexander Afanasyev8b379052011-08-21 16:58:20 -070096}
97}