blob: ac0d2858b7a5b3db77cd2aad030a108a5c2449d6 [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
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070021#include "block.h"
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070022
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070023#include "blob.h"
24#include "udata.h"
25#include "tag.h"
26#include "dtag.h"
27#include "attr.h"
28#include "dattr.h"
29#include "ext.h"
Alexander Afanasyev8b379052011-08-21 16:58:20 -070030
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080031#include "ns3/log.h"
32
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070033NS_LOG_COMPONENT_DEFINE ("ndn.CcnbParser.Block");
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080034
Alexander Afanasyev1043c702013-07-15 16:21:09 -070035NDN_NAMESPACE_BEGIN
36
37namespace wire {
Alexander Afanasyev8b379052011-08-21 16:58:20 -070038namespace CcnbParser {
39
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070040/// @cond include_hidden
Alexander Afanasyev8b379052011-08-21 16:58:20 -070041const uint8_t CCN_TT_BITS = 3;
42const uint8_t CCN_TT_MASK = ((1 << CCN_TT_BITS) - 1);
43const uint8_t CCN_MAX_TINY= ((1 << (7-CCN_TT_BITS)) - 1);
44const uint8_t CCN_TT_HBIT = ((uint8_t)(1 << 7));
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070045/// @endcond
Alexander Afanasyev8b379052011-08-21 16:58:20 -070046
Alexander Afanasyeve91ab752011-08-31 19:13:40 -070047// int Block::counter = 0;
48
Alexander Afanasyevb4bf3ef2013-07-30 10:40:55 -070049Ptr<Block> Block::ParseBlock (Buffer::Iterator &start, bool dontParseBlock)
Alexander Afanasyev8b379052011-08-21 16:58:20 -070050{
Alexander Afanasyeve91ab752011-08-31 19:13:40 -070051 // std::cout << "<< pos: " << counter << "\n";
Alexander Afanasyev8b379052011-08-21 16:58:20 -070052 uint32_t value = 0;
53
54 // We will have problems if length field is more than 32 bits. Though it's really impossible
55 uint8_t byte = 0;
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070056 while (!start.IsEnd() && !(byte & CCN_TT_HBIT))
Alexander Afanasyev8b379052011-08-21 16:58:20 -070057 {
Alexander Afanasyevc2b57282013-07-15 18:19:28 -070058 value <<= 7;
Alexander Afanasyev8b379052011-08-21 16:58:20 -070059 value += byte;
60 byte = start.ReadU8 ();
Alexander Afanasyeve91ab752011-08-31 19:13:40 -070061 // Block::counter ++;
Alexander Afanasyev8b379052011-08-21 16:58:20 -070062 }
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070063 if (start.IsEnd())
64 CcnbDecodingException ();
Alexander Afanasyevb4bf3ef2013-07-30 10:40:55 -070065
66 if (dontParseBlock)
67 {
68 return 0;
69 }
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070070
Alexander Afanasyev8b379052011-08-21 16:58:20 -070071 value <<= 4;
72 value += ( (byte&(~CCN_TT_HBIT)) >> 3);
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080073
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070074 /**
75 * Huh. After fighting with NS-3, it became apparent that Create<T>(...) construct
76 * doesn't work with references. Just simply doesn't work. wtf?
77 */
Alexander Afanasyev8b379052011-08-21 16:58:20 -070078 switch (byte & CCN_TT_MASK)
79 {
80 case CCN_BLOB:
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080081 return Ptr<Blob> (new Blob(start, value), false);
Alexander Afanasyev8b379052011-08-21 16:58:20 -070082 case CCN_UDATA:
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080083 return Ptr<Udata> (new Udata(start, value), false);
Alexander Afanasyev8b379052011-08-21 16:58:20 -070084 case CCN_TAG:
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080085 return Ptr<Tag> (new Tag(start, value), false);
Alexander Afanasyev8b379052011-08-21 16:58:20 -070086 case CCN_ATTR:
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080087 return Ptr<Attr> (new Attr(start, value), false);
Alexander Afanasyev8b379052011-08-21 16:58:20 -070088 case CCN_DTAG:
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080089 return Ptr<Dtag> (new Dtag(start, value), false);
Alexander Afanasyev8b379052011-08-21 16:58:20 -070090 case CCN_DATTR:
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080091 return Ptr<Dattr> (new Dattr(start, value), false);
Alexander Afanasyev8b379052011-08-21 16:58:20 -070092 case CCN_EXT:
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080093 return Ptr<Ext> (new Ext(start, value), false);
Alexander Afanasyev8b379052011-08-21 16:58:20 -070094 default:
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070095 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -070096 }
97}
98
Alexander Afanasyev795f9b52011-11-21 11:47:35 -080099Block::~Block ()
100{
Alexander Afanasyev795f9b52011-11-21 11:47:35 -0800101}
102
Alexander Afanasyev1043c702013-07-15 16:21:09 -0700103} // namespace CcnbParser
104} // namespace wire
105
106NDN_NAMESPACE_END