blob: e1e09fd7b249fd903f265be398f4c6352a77a2a7 [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
31namespace ns3 {
32namespace CcnbParser {
33
34const uint8_t CCN_TT_BITS = 3;
35const uint8_t CCN_TT_MASK = ((1 << CCN_TT_BITS) - 1);
36const uint8_t CCN_MAX_TINY= ((1 << (7-CCN_TT_BITS)) - 1);
37const uint8_t CCN_TT_HBIT = ((uint8_t)(1 << 7));
38
39Ptr<Block> Block::ParseBlock (Buffer::Iterator &start)
40{
41 uint32_t value = 0;
42
43 // We will have problems if length field is more than 32 bits. Though it's really impossible
44 uint8_t byte = 0;
45 while (!(byte & CCN_TT_HBIT))
46 {
47 value <<= 8;
48 value += byte;
49 byte = start.ReadU8 ();
50 }
51 value <<= 4;
52 value += ( (byte&(~CCN_TT_HBIT)) >> 3);
53
54 switch (byte & CCN_TT_MASK)
55 {
56 case CCN_BLOB:
57 return Create<Blob> (start, value);
58 case CCN_UDATA:
59 return Create<Udata> (start, value);
60 case CCN_TAG:
61 return Create<Tag> (start, value);
62 case CCN_ATTR:
63 return Create<Attr> (start, value);
64 case CCN_DTAG:
65 return Create<Dtag> (start, value);
66 case CCN_DATTR:
67 return Create<Dattr> (start, value);
68 case CCN_EXT:
69 return Create<Ext> (start, value);
70 default:
Alexander Afanasyeve709f3d2011-08-21 17:55:45 -070071 throw CcnbDecodingException ();
Alexander Afanasyev8b379052011-08-21 16:58:20 -070072 }
73}
74
75}
76}