Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -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: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | */ |
| 20 | |
| 21 | #include "ccnb-parser-block.h" |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 22 | |
| 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 Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 30 | |
| 31 | namespace ns3 { |
| 32 | namespace CcnbParser { |
| 33 | |
| 34 | const uint8_t CCN_TT_BITS = 3; |
| 35 | const uint8_t CCN_TT_MASK = ((1 << CCN_TT_BITS) - 1); |
| 36 | const uint8_t CCN_MAX_TINY= ((1 << (7-CCN_TT_BITS)) - 1); |
| 37 | const uint8_t CCN_TT_HBIT = ((uint8_t)(1 << 7)); |
| 38 | |
| 39 | Ptr<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; |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 45 | while (!start.IsEnd() && !(byte & CCN_TT_HBIT)) |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 46 | { |
| 47 | value <<= 8; |
| 48 | value += byte; |
| 49 | byte = start.ReadU8 (); |
| 50 | } |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 51 | if (start.IsEnd()) |
| 52 | CcnbDecodingException (); |
| 53 | |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 54 | value <<= 4; |
| 55 | value += ( (byte&(~CCN_TT_HBIT)) >> 3); |
| 56 | |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 57 | /** |
| 58 | * Huh. After fighting with NS-3, it became apparent that Create<T>(...) construct |
| 59 | * doesn't work with references. Just simply doesn't work. wtf? |
| 60 | */ |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 61 | switch (byte & CCN_TT_MASK) |
| 62 | { |
| 63 | case CCN_BLOB: |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 64 | return Ptr<Blob> (new Blob(start, value)); |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 65 | case CCN_UDATA: |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 66 | return Ptr<Udata> (new Udata(start, value)); |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 67 | case CCN_TAG: |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 68 | return Ptr<Tag> (new Tag(start, value)); |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 69 | case CCN_ATTR: |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 70 | return Ptr<Attr> (new Attr(start, value)); |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 71 | case CCN_DTAG: |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 72 | return Ptr<Dtag> (new Dtag(start, value)); |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 73 | case CCN_DATTR: |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 74 | return Ptr<Dattr> (new Dattr(start, value)); |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 75 | case CCN_EXT: |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 76 | return Ptr<Ext> (new Ext(start, value)); |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 77 | default: |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 78 | throw CcnbDecodingException (); |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | } |
| 83 | } |