Implementing visitor pattern for CCNx message parsing. Needs debugging.

Altering src/network/model/buffer* to support more Iterator features
diff --git a/helper/ccnx-coding-helper.h b/helper/ccnx-coding-helper.h
deleted file mode 100644
index 2d46a14..0000000
--- a/helper/ccnx-coding-helper.h
+++ /dev/null
@@ -1,245 +0,0 @@
-/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
-/*
- * Copyright (c) 2011 University of California, Los Angeles
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation;
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- *
- * Author: Ilya Moiseenko <iliamo@cs.ucla.edu>
- */
-
-#ifndef _CCNX_CODING_HELPER_H_
-#define _CCNX_CODING_HELPER_H_
-
-#include <sys/types.h>
-#include "ns3/ptr.h"
-#include "ns3/nstime.h"
-#include "ns3/buffer.h"
-
-namespace ns3 {
-
-namespace Name{ class Components; }
-
-class CcnxInterestHeader;
-class CcnxContentObjectHeader;
-  
-/**
- * Helper to encode/decode ccnb formatted CCNx message
- *
- */
-class CcnxCodingHelper
-{
-public:
-  static size_t
-  Serialize (Buffer::Iterator start, const CcnxInterestHeader &interest);
-
-  static size_t
-  Serialize (Buffer::Iterator start, const CcnxContentObjectHeader &contentObject);
-
-private:
-  /**
-   * Type tag for a ccnb start marker.
-   *
-   * \see http://www.ccnx.org/releases/latest/doc/technical/DTAG.html
-   */
-  enum ccn_tt {
-    CCN_EXT,        /**< starts composite extension - numval is subtype */
-    CCN_TAG,        /**< starts composite - numval is tagnamelen-1 */ 
-    CCN_DTAG,       /**< starts composite - numval is tagdict index (enum ccn_dtag) */
-    CCN_ATTR,       /**< attribute - numval is attrnamelen-1, value follows */
-    CCN_DATTR,      /**< attribute numval is attrdict index */
-    CCN_BLOB,       /**< opaque binary data - numval is byte count */
-    CCN_UDATA,      /**< UTF-8 encoded character data - numval is byte count */
-    CCN_NO_TOKEN    /**< should not occur in encoding */
-  };
-
-  /** CCN_CLOSE terminates composites */
-  enum {CCN_CLOSE = 0};
-
-  // enum ccn_ext_subtype {
-  //   /* skip smallest values for now */
-  //   CCN_PROCESSING_INSTRUCTIONS = 16 /* <?name:U value:U?> */
-  // };
-
-  /**
-   * DTAG identifies ccnb-encoded elements.
-   *
-   * \see http://www.ccnx.org/releases/latest/doc/technical/DTAG.html
-   */
-  enum ccn_dtag {
-    CCN_DTAG_Any = 13,
-    CCN_DTAG_Name = 14,
-    CCN_DTAG_Component = 15,
-    CCN_DTAG_Certificate = 16,
-    CCN_DTAG_Collection = 17,
-    CCN_DTAG_CompleteName = 18,
-    CCN_DTAG_Content = 19,
-    CCN_DTAG_SignedInfo = 20,
-    CCN_DTAG_ContentDigest = 21,
-    CCN_DTAG_ContentHash = 22,
-    CCN_DTAG_Count = 24,
-    CCN_DTAG_Header = 25,
-    CCN_DTAG_Interest = 26,	/* 20090915 */
-    CCN_DTAG_Key = 27,
-    CCN_DTAG_KeyLocator = 28,
-    CCN_DTAG_KeyName = 29,
-    CCN_DTAG_Length = 30,
-    CCN_DTAG_Link = 31,
-    CCN_DTAG_LinkAuthenticator = 32,
-    CCN_DTAG_NameComponentCount = 33,	/* DeprecatedInInterest */
-    CCN_DTAG_RootDigest = 36,
-    CCN_DTAG_Signature = 37,
-    CCN_DTAG_Start = 38,
-    CCN_DTAG_Timestamp = 39,
-    CCN_DTAG_Type = 40,
-    CCN_DTAG_Nonce = 41,
-    CCN_DTAG_Scope = 42,
-    CCN_DTAG_Exclude = 43,
-    CCN_DTAG_Bloom = 44,
-    CCN_DTAG_BloomSeed = 45,
-    CCN_DTAG_AnswerOriginKind = 47,
-    CCN_DTAG_InterestLifetime = 48,
-    CCN_DTAG_Witness = 53,
-    CCN_DTAG_SignatureBits = 54,
-    CCN_DTAG_DigestAlgorithm = 55,
-    CCN_DTAG_BlockSize = 56,
-    CCN_DTAG_FreshnessSeconds = 58,
-    CCN_DTAG_FinalBlockID = 59,
-    CCN_DTAG_PublisherPublicKeyDigest = 60,
-    CCN_DTAG_PublisherCertificateDigest = 61,
-    CCN_DTAG_PublisherIssuerKeyDigest = 62,
-    CCN_DTAG_PublisherIssuerCertificateDigest = 63,
-    CCN_DTAG_ContentObject = 64,	/* 20090915 */
-    CCN_DTAG_WrappedKey = 65,
-    CCN_DTAG_WrappingKeyIdentifier = 66,
-    CCN_DTAG_WrapAlgorithm = 67,
-    CCN_DTAG_KeyAlgorithm = 68,
-    CCN_DTAG_Label = 69,
-    CCN_DTAG_EncryptedKey = 70,
-    CCN_DTAG_EncryptedNonceKey = 71,
-    CCN_DTAG_WrappingKeyName = 72,
-    CCN_DTAG_Action = 73,
-    CCN_DTAG_FaceID = 74,
-    CCN_DTAG_IPProto = 75,
-    CCN_DTAG_Host = 76,
-    CCN_DTAG_Port = 77,
-    CCN_DTAG_MulticastInterface = 78,
-    CCN_DTAG_ForwardingFlags = 79,
-    CCN_DTAG_FaceInstance = 80,
-    CCN_DTAG_ForwardingEntry = 81,
-    CCN_DTAG_MulticastTTL = 82,
-    CCN_DTAG_MinSuffixComponents = 83,
-    CCN_DTAG_MaxSuffixComponents = 84,
-    CCN_DTAG_ChildSelector = 85,
-    CCN_DTAG_RepositoryInfo = 86,
-    CCN_DTAG_Version = 87,
-    CCN_DTAG_RepositoryVersion = 88,
-    CCN_DTAG_GlobalPrefix = 89,
-    CCN_DTAG_LocalName = 90,
-    CCN_DTAG_Policy = 91,
-    CCN_DTAG_Namespace = 92,
-    CCN_DTAG_GlobalPrefixName = 93,
-    CCN_DTAG_PolicyVersion = 94,
-    CCN_DTAG_KeyValueSet = 95,
-    CCN_DTAG_KeyValuePair = 96,
-    CCN_DTAG_IntegerValue = 97,
-    CCN_DTAG_DecimalValue = 98,
-    CCN_DTAG_StringValue = 99,
-    CCN_DTAG_BinaryValue = 100,
-    CCN_DTAG_NameValue = 101,
-    CCN_DTAG_Entry = 102,
-    CCN_DTAG_ACL = 103,
-    CCN_DTAG_ParameterizedName = 104,
-    CCN_DTAG_Prefix = 105,
-    CCN_DTAG_Suffix = 106,
-    CCN_DTAG_Root = 107,
-    CCN_DTAG_ProfileName = 108,
-    CCN_DTAG_Parameters = 109,
-    CCN_DTAG_InfoString = 110,
-    CCN_DTAG_StatusResponse = 112,
-    CCN_DTAG_StatusCode = 113,
-    CCN_DTAG_StatusText = 114,
-    CCN_DTAG_SequenceNumber = 256,
-    CCN_DTAG_CCNProtocolDataUnit = 17702112
-  };
-
-  /**
-   * The decoder state is one of these, possibly with some
-   * additional bits set for internal use.  A complete parse
-   * ends up in state 0 or an error state.  Not all possible
-   * error states are listed here.
-   */
-  enum ccn_decoder_state {
-    CCN_DSTATE_INITIAL = 0,
-    CCN_DSTATE_NEWTOKEN,
-    CCN_DSTATE_NUMVAL,
-    CCN_DSTATE_UDATA,
-    CCN_DSTATE_TAGNAME,
-    CCN_DSTATE_ATTRNAME,
-    CCN_DSTATE_BLOB,
-    /* All error states are negative */
-    CCN_DSTATE_ERR_OVERFLOW = -1,
-    CCN_DSTATE_ERR_ATTR     = -2,       
-    CCN_DSTATE_ERR_CODING   = -3,
-    CCN_DSTATE_ERR_NEST     = -4, 
-    CCN_DSTATE_ERR_BUG      = -5
-  };
-
-
-private:
-  static size_t
-  AppendBlockHeader (Buffer::Iterator start, size_t value, ccn_tt block_type);
-
-  static size_t
-  AppendNumber (Buffer::Iterator start, uint32_t number);
-
-  static size_t
-  AppendCloser (Buffer::Iterator start);
-
-  static size_t
-  AppendNameComponents (Buffer::Iterator start, const Name::Components &name);
-
-  /**
-   * Append a binary timestamp as a BLOB using the ccn binary
-   * Timestamp representation (12-bit fraction).
-   *
-   * @param start start iterator of  the buffer to append to.
-   * @param time - Time object
-   *
-   * @returns written length
-   */
-  static size_t
-  AppendTimestampBlob (Buffer::Iterator start, Time time);
-
-  /**
-   * Append a tagged BLOB
-   *
-   * This is a ccnb-encoded element with containing the BLOB as content
-   *
-   * @param start start iterator of  the buffer to append to.
-   * @param dtag is the element's dtab
-   * @param data points to the binary data
-   * @param size is the size of the data, in bytes
-   *
-   * @returns written length
-   */
-  static size_t
-  AppendTaggedBlob (Buffer::Iterator start, ccn_dtag dtag,
-                    const uint8_t *data, size_t size);
-  
-};
-
-} // namespace ns3
-
-#endif // _CCNX_CODING_HELPER_H_
-
diff --git a/helper/ccnx-decoding-helper.cc b/helper/ccnx-decoding-helper.cc
new file mode 100644
index 0000000..e853c7d
--- /dev/null
+++ b/helper/ccnx-decoding-helper.cc
@@ -0,0 +1,667 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2011 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: 
+ */
+
+#include "ccnx-decoding-helper.h"
+
+#include "ns3/ccnx.h"
+#include "ns3/name-components.h"
+#include "ns3/ccnx-interest-header.h"
+#include "ns3/ccnx-content-object-header.h"
+
+#include <sstream>
+#include <boost/foreach.hpp>
+
+namespace ns3 {
+
+CcnxParser::InterestVisitor CcnxDecodingHelper::m_interestVisitor;
+CcnxParser::ContentObjectVisitor CcnxDecodingHelper::m_contentObjectVisitor;
+
+size_t
+CcnxDecodingHelper::Deserialize (Buffer::Iterator start, const CcnxInterestHeader &interest)
+{
+  Buffer::Iterator i = start;
+  Ptr<CcnxParser::Block> root = CcnxParser::Block::ParseBlock (i);
+  root->accept (m_interestVisitor, interest);
+
+  return i.GetDistanceFrom (start);
+}
+
+size_t
+CcnxDecodingHelper::Deserialize (Buffer::Iterator start, const CcnxContentObjectHeader &contentObject)
+{
+  Buffer::Iterator i = start;
+  Ptr<CcnxParser::Block> root = CcnxParser::Block::ParseBlock (i);
+  root->accept (m_contentObjectVisitor, contentObject);
+
+  return i.GetDistanceFrom (start);
+}
+
+
+//////////////////////////////////////////////////////////////////////
+
+const uint8_t CCN_TT_BITS = 3;
+const uint8_t CCN_TT_MASK = ((1 << CCN_TT_BITS) - 1);
+const uint8_t CCN_MAX_TINY= ((1 << (7-CCN_TT_BITS)) - 1);
+const uint8_t CCN_TT_HBIT = ((uint8_t)(1 << 7));
+
+namespace CcnxParser {
+
+Ptr<Block> Block::ParseBlock (Buffer::Iterator &start)
+{
+  uint32_t value = 0;
+
+  // We will have problems if length field is more than 32 bits. Though it's really impossible
+  uint8_t byte = 0;
+  while (!(byte & CCN_TT_HBIT))
+    {
+      value <<= 8;
+      value += byte;
+      byte = start.ReadU8 ();
+    }
+  value <<= 4;
+  value += ( (byte&(~CCN_TT_HBIT)) >> 3);
+
+  switch (byte & CCN_TT_MASK)
+    {
+    case Ccnx::CCN_BLOB:
+      return Create<Blob> (start, value);
+    case Ccnx::CCN_UDATA:
+      return Create<Udata> (start, value);
+    case Ccnx::CCN_TAG:
+      return Create<Tag> (start, value);
+    case Ccnx::CCN_ATTR:
+      return Create<Attr> (start, value);
+    case Ccnx::CCN_DTAG:
+      return Create<Dtag> (start, value);
+    case Ccnx::CCN_DATTR:
+      return Create<Dattr> (start, value);
+    case Ccnx::CCN_EXT:
+      return Create<Ext> (start, value);
+    default:
+      throw CcnxDecodingException ();
+    }
+}
+
+Blob::Blob (Buffer::Iterator &start, uint32_t length)
+{
+  start.Read (m_blob.Begin (), length);
+}
+
+Udata::Udata (Buffer::Iterator &start, uint32_t length)
+{
+  // Ideally, the code should look like this. Unfortunately, we don't have normal compatible iterators
+  // Buffer::Iterator realStart = start;
+  // start.Next (length); // advancing forward
+  // m_udata.assign (realStart, start/*actually, it is the end*/);
+
+  m_udata.reserve (length+1); //just in case we will need \0 at the end later
+  // this is actually the way Read method is implemented in network/src/buffer.cc
+  for (uint32_t i = 0; i < length; i++)
+    {
+      m_udata.append (reinterpret_cast<const char*>(start.ReadU8 ()));
+    }
+}
+
+// length length in octets of UTF-8 encoding of tag name - 1 (minimum tag name length is 1) 
+Tag::Tag (Buffer::Iterator &start, uint32_t length)
+{
+  m_tag.reserve (length+2); // extra byte for potential \0 at the end
+  for (uint32_t i = 0; i < (length+1); i++)
+    {
+      m_tag.append (reinterpret_cast<const char*>(start.ReadU8 ()));
+    }
+  
+  while (!start.IsEnd () && start.PeekU8 ()!=Ccnx::CCN_CLOSE)
+    {
+      m_nestedBlocks.push_back (Block::ParseBlock (start));
+    }
+  if (start.IsEnd ())
+      throw CcnxDecodingException ();
+
+  start.ReadU8 (); // read CCN_CLOSE
+}
+
+// length length in octets of UTF-8 encoding of tag name - 1 (minimum tag name length is 1) 
+Attr::Attr (Buffer::Iterator &start, uint32_t length)
+{
+  m_attr.reserve (length+2); // extra byte for potential \0 at the end
+  for (uint32_t i = 0; i < (length+1); i++)
+    {
+      m_attr.append (reinterpret_cast<const char*>(start.ReadU8 ()));
+    }
+  m_value = DynamicCast<Udata> (Block::ParseBlock (start));
+  if (m_value == 0)
+    throw CcnxDecodingException (); // "ATTR must be followed by UDATA field"
+}
+
+Dtag::Dtag (Buffer::Iterator &start, uint32_t dtag)
+{
+  m_dtag = dtag;
+
+  /**
+   * Hack
+   *
+   * Stop processing after encountering <Content> dtag.  Actual
+   * content (including virtual payload) will be stored in Packet
+   * buffer
+   */
+  if (dtag == Ccnx::CCN_DTAG_Content)
+    return; // hack #1. Do not process nesting block for <Content>
+  
+  while (!start.IsEnd () && start.PeekU8 ()!=Ccnx::CCN_CLOSE)
+    {
+      m_nestedBlocks.push_back (Block::ParseBlock (start));
+
+      // hack #2. Stop processing nested blocks if last block was <Content>
+      if (m_dtag == Ccnx::CCN_DTAG_ContentObject && // we are in <ContentObject>
+          DynamicCast<Dtag> (m_nestedBlocks.back())!=0 && // last block is DTAG
+          DynamicCast<Dtag> (m_nestedBlocks.back())->m_dtag == Ccnx::CCN_DTAG_Content) 
+        {
+          return; 
+        }
+    }
+  if (start.IsEnd ())
+      throw CcnxDecodingException ();
+
+  start.ReadU8 (); // read CCN_CLOSE
+}
+
+// dictionary attributes are not used (yet?) in CCNx 
+Dattr::Dattr (Buffer::Iterator &start, uint32_t dattr)
+{
+  m_dattr = dattr;
+  m_value = DynamicCast<Udata> (Block::ParseBlock (start));
+  if (m_value == 0)
+    throw CcnxDecodingException (); // "ATTR must be followed by UDATA field"
+}
+
+Ext::Ext (Buffer::Iterator &start, uint32_t extSubtype)
+{
+  m_extSubtype = extSubtype;
+}
+
+void
+DepthFirstVisitor::visit (Blob &n)
+{
+  // Buffer n.m_blob;
+}
+ 
+void
+DepthFirstVisitor::visit (Udata &n)
+{
+  // std::string n.m_udata;
+}
+ 
+void
+DepthFirstVisitor::visit (Tag &n)
+{
+  // std::string n.m_tag;
+  // std::list<Ptr<Block> > n.m_nestedBlocks;
+  BOOST_FOREACH (Ptr<Block> block, n.m_nestedBlocks)
+    {
+      block->accept (*this);
+    }
+}
+ 
+void
+DepthFirstVisitor::visit (Attr &n)
+{
+  // std::string n.m_attr;
+  // Ptr<Udata> n.m_value;
+}
+ 
+void
+DepthFirstVisitor::visit (Dtag &n)
+{
+  // uint32_t n.m_dtag;
+  // std::list<Ptr<Block> > n.m_nestedBlocks;
+  BOOST_FOREACH (Ptr<Block> block, n.m_nestedBlocks)
+    {
+      block->accept (*this);
+    }
+}
+ 
+void
+DepthFirstVisitor::visit (Dattr &n)
+{
+  // uint32_t n.m_dattr;
+  // Ptr<Udata> n.m_value;
+}
+ 
+void
+DepthFirstVisitor::visit (Ext &n)
+{
+  // uint64_t n.m_extSubtype;
+}
+
+//////////////////////////////////////////////////////////////////////
+ 
+boost::any
+GJNoArguDepthFirstVisitor::visit (Blob &n)
+{
+  // Buffer n.m_blob;
+  return n.m_blob;
+}
+ 
+boost::any
+GJNoArguDepthFirstVisitor::visit (Udata &n)
+{
+  // std::string n.m_udata;
+  return n.m_udata;
+}
+ 
+boost::any
+GJNoArguDepthFirstVisitor::visit (Tag &n)
+{
+  // std::string n.m_tag;
+  // std::list<Ptr<Block> > n.m_nestedBlocks;
+  BOOST_FOREACH (Ptr<Block> block, n.m_nestedBlocks)
+    {
+      block->accept (*this);
+    }
+  return boost::any();
+}
+ 
+boost::any
+GJNoArguDepthFirstVisitor::visit (Attr &n)
+{
+  // std::string n.m_attr;
+  // Ptr<Udata> n.m_value;
+  return boost::any(
+                    std::pair<std::string,std::string> (
+                                                        n.m_attr,
+                                                        boost::any_cast<std::string> (n.m_value->accept (*this))
+                                                        ));
+}
+ 
+boost::any
+GJNoArguDepthFirstVisitor::visit (Dtag &n)
+{
+  // uint32_t n.m_dtag;
+  // std::list<Ptr<Block> > n.m_nestedBlocks;
+  BOOST_FOREACH (Ptr<Block> block, n.m_nestedBlocks)
+    {
+      block->accept (*this);
+    }
+  return boost::any();
+}
+ 
+boost::any
+GJNoArguDepthFirstVisitor::visit (Dattr &n)
+{
+  // uint32_t n.m_dattr;
+  // Ptr<Udata> n.m_value;
+  return boost::any(
+                    std::pair<uint32_t,std::string> (
+                                                     n.m_dattr,
+                                                     boost::any_cast<std::string> (n.m_value->accept (*this))
+                                                     ));
+}
+ 
+boost::any
+GJNoArguDepthFirstVisitor::visit (Ext &n)
+{
+  // uint64_t n.m_extSubtype;
+  return n.m_extSubtype;
+}
+
+//////////////////////////////////////////////////////////////////////
+
+void
+GJVoidDepthFirstVisitor::visit (Blob &n, boost::any param)
+{
+  // Buffer n.m_blob;
+}
+ 
+void
+GJVoidDepthFirstVisitor::visit (Udata &n, boost::any param)
+{
+  // std::string n.m_udata;
+}
+ 
+void
+GJVoidDepthFirstVisitor::visit (Tag &n, boost::any param)
+{
+  // std::string n.m_tag;
+  // std::list<Ptr<Block> > n.m_nestedBlocks;
+  BOOST_FOREACH (Ptr<Block> block, n.m_nestedBlocks)
+    {
+      block->accept (*this, param);
+    }
+}
+ 
+void
+GJVoidDepthFirstVisitor::visit (Attr &n, boost::any param)
+{
+  // std::string n.m_attr;
+  // Ptr<Udata> n.m_value;
+}
+ 
+void
+GJVoidDepthFirstVisitor::visit (Dtag &n, boost::any param)
+{
+  // uint32_t n.m_dtag;
+  // std::list<Ptr<Block> > n.m_nestedBlocks;
+  BOOST_FOREACH (Ptr<Block> block, n.m_nestedBlocks)
+    {
+      block->accept (*this, param);
+    }
+}
+ 
+void
+GJVoidDepthFirstVisitor::visit (Dattr &n, boost::any param)
+{
+  // uint32_t n.m_dattr;
+  // Ptr<Udata> n.m_value;
+}
+ 
+void
+GJVoidDepthFirstVisitor::visit (Ext &n, boost::any param)
+{
+  // uint64_t n.m_extSubtype;
+}
+
+//////////////////////////////////////////////////////////////////////
+ 
+boost::any
+GJDepthFirstVisitor::visit (Blob &n, boost::any param)
+{
+  // Buffer n.m_blob;
+  return n.m_blob;
+}
+ 
+boost::any
+GJDepthFirstVisitor::visit (Udata &n, boost::any param)
+{
+  // std::string n.m_udata;
+  return n.m_udata;
+}
+ 
+boost::any
+GJDepthFirstVisitor::visit (Tag &n, boost::any param)
+{
+  // std::string n.m_tag;
+  // std::list<Ptr<Block> > n.m_nestedBlocks;
+  BOOST_FOREACH (Ptr<Block> block, n.m_nestedBlocks)
+    {
+      block->accept (*this, param);
+    }
+  return boost::any();
+}
+ 
+boost::any
+GJDepthFirstVisitor::visit (Attr &n, boost::any param)
+{
+  // std::string n.m_attr;
+  // Ptr<Udata> n.m_value;
+  return boost::any(
+                    std::pair<std::string,std::string> (
+                                                        n.m_attr,
+                                                        boost::any_cast<std::string> (n.m_value->accept (*this,param))
+                                                        ));
+}
+ 
+boost::any
+GJDepthFirstVisitor::visit (Dtag &n, boost::any param)
+{
+  // uint32_t n.m_dtag;
+  // std::list<Ptr<Block> > n.m_nestedBlocks;
+  BOOST_FOREACH (Ptr<Block> block, n.m_nestedBlocks)
+    {
+      block->accept (*this, param);
+    }
+  return boost::any();
+}
+ 
+boost::any
+GJDepthFirstVisitor::visit (Dattr &n, boost::any param)
+{
+  // uint32_t n.m_dattr;
+  // Ptr<Udata> n.m_value;
+  return boost::any(
+                    std::pair<uint32_t,std::string> (
+                                                     n.m_dattr,
+                                                     boost::any_cast<std::string> (n.m_value->accept (*this,param))
+                                                     ));
+}
+ 
+boost::any
+GJDepthFirstVisitor::visit (Ext &n, boost::any param)
+{
+  // uint64_t n.m_extSubtype;
+  return n.m_extSubtype;
+}
+
+//////////////////////////////////////////////////////////////////////
+
+boost::any
+NonNegativeIntegerVisitor::visit (Blob &n) //to throw parsing error
+{
+  // Buffer n.m_blob;
+  throw CcnxDecodingException ();
+}
+
+boost::any
+NonNegativeIntegerVisitor::visit (Udata &n)
+{
+  // std::string n.m_udata;
+  std::istringstream is (n.m_udata);
+  int32_t value;
+  is >> value;
+  if (value<0) // value should be non-negative
+    throw CcnxDecodingException ();
+
+  return static_cast<uint32_t> (value);
+}
+
+
+//////////////////////////////////////////////////////////////////////
+
+boost::any
+StringVisitor::visit (Blob &n) //to throw parsing error
+{
+  // Buffer n.m_blob;
+  throw CcnxDecodingException ();
+}
+
+boost::any
+StringVisitor::visit (Udata &n)
+{
+  // std::string n.m_udata;
+  return n.m_udata;
+}
+
+//////////////////////////////////////////////////////////////////////
+
+StringVisitor NameComponentsVisitor::m_stringVisitor; 
+
+void
+NameComponentsVisitor::visit (Dtag &n, boost::any param/*should be Name::Components&*/)
+{
+  // uint32_t n.m_dtag;
+  // std::list<Ptr<Block> > n.m_nestedBlocks;
+  Name::Components &components = boost::any_cast<Name::Components&> (param);
+
+  switch (n.m_dtag)
+    {
+    case Ccnx::CCN_DTAG_Component:
+      if (n.m_nestedBlocks.size()!=1) // should be exactly one UDATA inside this tag
+        throw CcnxDecodingException ();
+      components.Add (
+                      boost::any_cast<std::string> ((*n.m_nestedBlocks.begin())->accept(
+                                                                                        m_stringVisitor
+                                                                                        )));
+      break;
+    default:
+      // ignore any other components
+      // when parsing Exclude, there could be <Any /> and <Bloom /> tags
+      break;
+    }
+}
+ 
+//////////////////////////////////////////////////////////////////////
+
+NonNegativeIntegerVisitor InterestVisitor::m_nonNegativeIntegerVisitor;
+NameComponentsVisitor     InterestVisitor::m_nameComponentsVisitor;
+
+// We don't really care about any other fields
+void
+InterestVisitor::visit (Dtag &n, boost::any param/*should be CcnxInterestHeader&*/)
+{
+  // uint32_t n.m_dtag;
+  // std::list<Ptr<Block> > n.m_nestedBlocks;
+  CcnxInterestHeader &interest = boost::any_cast<CcnxInterestHeader&> (param);
+  
+  switch (n.m_dtag)
+    {
+    case Ccnx::CCN_DTAG_Interest:
+      // process nested blocks
+      BOOST_FOREACH (Ptr<Block> block, n.m_nestedBlocks)
+        {
+          block->accept (*this, param);
+        }
+      break;
+    case Ccnx::CCN_DTAG_Name:
+      {
+        // process name components
+        Ptr<Name::Components> name = Create<Name::Components> ();
+        
+        BOOST_FOREACH (Ptr<Block> block, n.m_nestedBlocks)
+          {
+            block->accept (m_nameComponentsVisitor, *name);
+          }
+        interest.SetName (name);
+        break;
+      }
+    case Ccnx::CCN_DTAG_MinSuffixComponents:
+      if (n.m_nestedBlocks.size()!=1) // should be exactly one UDATA inside this tag
+        throw CcnxDecodingException ();
+      interest.SetMinSuffixComponents (
+               boost::any_cast<uint32_t> (
+                                          (*n.m_nestedBlocks.begin())->accept(
+                                                                           m_nonNegativeIntegerVisitor
+                                                                           )));
+      break;
+    case Ccnx::CCN_DTAG_MaxSuffixComponents:
+      if (n.m_nestedBlocks.size()!=1) // should be exactly one UDATA inside this tag
+        throw CcnxDecodingException ();
+      interest.SetMaxSuffixComponents (
+               boost::any_cast<uint32_t> (
+                                          (*n.m_nestedBlocks.begin())->accept(
+                                                                           m_nonNegativeIntegerVisitor
+                                                                           )));
+      break;
+    case Ccnx::CCN_DTAG_Exclude:
+      {
+        // process exclude components
+        Ptr<Name::Components> exclude = Create<Name::Components> ();
+        
+        BOOST_FOREACH (Ptr<Block> block, n.m_nestedBlocks)
+          {
+            block->accept (m_nameComponentsVisitor, *exclude);
+          }
+        interest.SetExclude (exclude);
+        break;
+      }
+    case Ccnx::CCN_DTAG_ChildSelector:
+      if (n.m_nestedBlocks.size()!=1) // should be exactly one UDATA inside this tag
+        throw CcnxDecodingException ();
+
+      interest.SetChildSelector (
+               1 == boost::any_cast<uint32_t> (
+                                          (*n.m_nestedBlocks.begin())->accept(
+                                                                           m_nonNegativeIntegerVisitor
+                                                                           )));
+      break;
+    case Ccnx::CCN_DTAG_AnswerOriginKind:
+      if (n.m_nestedBlocks.size()!=1) // should be exactly one UDATA inside this tag
+        throw CcnxDecodingException ();
+      interest.SetAnswerOriginKind (
+               1 == boost::any_cast<uint32_t> (
+                                          (*n.m_nestedBlocks.begin())->accept(
+                                                                           m_nonNegativeIntegerVisitor
+                                                                           )));
+      break;
+    case Ccnx::CCN_DTAG_Scope: 
+      if (n.m_nestedBlocks.size()!=1) // should be exactly one UDATA inside this tag
+        throw CcnxDecodingException ();
+      interest.SetScope (
+               boost::any_cast<uint32_t> (
+                                          (*n.m_nestedBlocks.begin())->accept(
+                                                                           m_nonNegativeIntegerVisitor
+                                                                           )));
+      break;
+    case Ccnx::CCN_DTAG_InterestLifetime:
+      if (n.m_nestedBlocks.size()!=1) // should be exactly one UDATA inside this tag
+        throw CcnxDecodingException ();
+      break;
+    case Ccnx::CCN_DTAG_Nonce:
+      if (n.m_nestedBlocks.size()!=1) // should be exactly one UDATA inside this tag
+        throw CcnxDecodingException ();
+      break;
+    }
+}
+
+//////////////////////////////////////////////////////////////////////
+
+NameComponentsVisitor ContentObjectVisitor::m_nameComponentsVisitor;
+
+// We don't really care about any other fields
+void
+ContentObjectVisitor::visit (Dtag &n, boost::any param/*should be CcnxContentObjectHeader&*/)
+{
+  // uint32_t n.m_dtag;
+  // std::list<Ptr<Block> > n.m_nestedBlocks;
+  CcnxContentObjectHeader &contentObject = boost::any_cast<CcnxContentObjectHeader&> (param);
+  
+  switch (n.m_dtag)
+    {
+    case Ccnx::CCN_DTAG_ContentObject:
+      // process nested blocks
+      BOOST_FOREACH (Ptr<Block> block, n.m_nestedBlocks)
+        {
+          block->accept (*this, param);
+        }
+      break;
+    case Ccnx::CCN_DTAG_Name:
+      {
+        // process name components
+        Ptr<Name::Components> name = Create<Name::Components> ();
+        
+        BOOST_FOREACH (Ptr<Block> block, n.m_nestedBlocks)
+          {
+            block->accept (m_nameComponentsVisitor, *name);
+          }
+        contentObject.SetName (name);
+        break;
+      }
+    case Ccnx::CCN_DTAG_Signature: // ignoring
+      break;
+    case Ccnx::CCN_DTAG_SignedInfo: // ignoring
+      break;
+    case Ccnx::CCN_DTAG_Content: // !!! HACK
+      // This hack was necessary for memory optimizations (i.e., content is virtual payload)
+      NS_ASSERT_MSG (n.m_nestedBlocks.size() == 0, "Parser should have stopped just after processing <Content> tag");
+      break;
+    }
+}
+
+} // namespace CcnxParser
+} // namespace ns3
diff --git a/helper/ccnx-decoding-helper.h b/helper/ccnx-decoding-helper.h
new file mode 100644
index 0000000..e89766c
--- /dev/null
+++ b/helper/ccnx-decoding-helper.h
@@ -0,0 +1,332 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2011 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: 
+ */
+
+#ifndef _CCNX_DECODING_HELPER_H_
+#define _CCNX_DECODING_HELPER_H_
+
+#include <sys/types.h>
+#include <boost/any.hpp>
+#include <list>
+
+#include "ns3/ptr.h"
+#include "ns3/nstime.h"
+#include "ns3/buffer.h"
+#include "ns3/simple-ref-count.h"
+
+
+namespace ns3 {
+
+namespace Name{ class Components; }
+
+class CcnxInterestHeader;
+class CcnxContentObjectHeader;
+
+namespace CcnxParser {
+class InterestVisitor;
+class ContentObjectVisitor;
+}
+
+/**
+ * Helper to encode/decode ccnb formatted CCNx message
+ *
+ */
+class CcnxDecodingHelper
+{
+public:
+  static size_t
+  Deserialize (Buffer::Iterator start, const CcnxInterestHeader &interest);
+
+  static size_t
+  Deserialize (Buffer::Iterator start, const CcnxContentObjectHeader &contentObject);
+  
+private:
+  static CcnxParser::InterestVisitor m_interestVisitor;
+  static CcnxParser::ContentObjectVisitor m_contentObjectVisitor;
+};
+
+namespace CcnxParser {
+
+class Block;
+class Blob;
+class Udata;
+class Tag;
+class Attr;
+class Dtag;
+class Dattr;
+class Ext;
+
+class Visitor
+{
+public:
+  virtual void visit (Blob& )=0;
+  virtual void visit (Udata&)=0;
+  virtual void visit (Tag&  )=0;
+  virtual void visit (Attr& )=0;
+  virtual void visit (Dtag& )=0;
+  virtual void visit (Dattr&)=0;
+  virtual void visit (Ext&  )=0;
+};
+  
+class GJVisitor
+{
+public:
+  virtual boost::any visit (Blob&,  boost::any)=0;
+  virtual boost::any visit (Udata&, boost::any)=0;
+  virtual boost::any visit (Tag&,   boost::any)=0;
+  virtual boost::any visit (Attr&,  boost::any)=0;
+  virtual boost::any visit (Dtag&,  boost::any)=0;
+  virtual boost::any visit (Dattr&, boost::any)=0;
+  virtual boost::any visit (Ext&,   boost::any)=0;
+};
+  
+class GJNoArguVisitor
+{
+public:
+  virtual boost::any visit (Blob& )=0;
+  virtual boost::any visit (Udata&)=0;
+  virtual boost::any visit (Tag&  )=0;
+  virtual boost::any visit (Attr& )=0;
+  virtual boost::any visit (Dtag& )=0;
+  virtual boost::any visit (Dattr&)=0;
+  virtual boost::any visit (Ext&  )=0;
+};
+
+class GJVoidVisitor
+{
+public:
+  virtual void visit (Blob&,  boost::any)=0;
+  virtual void visit (Udata&, boost::any)=0;
+  virtual void visit (Tag&,   boost::any)=0;
+  virtual void visit (Attr&,  boost::any)=0;
+  virtual void visit (Dtag&,  boost::any)=0;
+  virtual void visit (Dattr&, boost::any)=0;
+  virtual void visit (Ext&,   boost::any)=0;
+};
+
+class Block : public SimpleRefCount<Block>
+{
+public:
+  /**
+   * Parsing block header and creating an appropriate object
+   */
+  static Ptr<Block>
+  ParseBlock (Buffer::Iterator &start);
+  
+  virtual void accept( Visitor &v )                           =0;
+  virtual void accept (GJVoidVisitor &v, boost::any param)    =0;
+  virtual boost::any accept( GJNoArguVisitor &v )             =0;
+  virtual boost::any accept( GJVisitor &v, boost::any param ) =0;
+};
+
+class Blob : public Block
+{
+public:
+  Blob (Buffer::Iterator &start, uint32_t length);
+  
+  virtual void accept( Visitor &v )                           { v.visit( *this ); }
+  virtual void accept( GJVoidVisitor &v, boost::any param )   { v.visit( *this, param ); }
+  virtual boost::any accept( GJNoArguVisitor &v )             { return v.visit( *this ); }
+  virtual boost::any accept( GJVisitor &v, boost::any param ) { return v.visit( *this, param ); }
+
+  Buffer m_blob;
+};
+
+class Udata : public Block
+{
+public:
+  Udata (Buffer::Iterator &start, uint32_t length);
+  
+  virtual void accept( Visitor &v )                           { v.visit( *this ); }
+  virtual void accept( GJVoidVisitor &v, boost::any param )   { v.visit( *this, param ); }
+  virtual boost::any accept( GJNoArguVisitor &v )             { return v.visit( *this ); }
+  virtual boost::any accept( GJVisitor &v, boost::any param ) { return v.visit( *this, param ); }
+
+  std::string m_udata;
+};
+
+class Tag : public Block
+{
+public:
+  Tag (Buffer::Iterator &start, uint32_t length);
+
+  virtual void accept( Visitor &v )                           { v.visit( *this ); }
+  virtual void accept( GJVoidVisitor &v, boost::any param )   { v.visit( *this, param ); }
+  virtual boost::any accept( GJNoArguVisitor &v )             { return v.visit( *this ); }
+  virtual boost::any accept( GJVisitor &v, boost::any param ) { return v.visit( *this, param ); }
+
+  std::string m_tag;
+  std::list<Ptr<Block> > m_nestedBlocks;
+};
+
+class Attr : public Block
+{
+public:
+  Attr (Buffer::Iterator &start, uint32_t length);
+  
+  virtual void accept( Visitor &v )                           { v.visit( *this ); }
+  virtual void accept( GJVoidVisitor &v, boost::any param )   { v.visit( *this, param ); }
+  virtual boost::any accept( GJNoArguVisitor &v )             { return v.visit( *this ); }
+  virtual boost::any accept( GJVisitor &v, boost::any param ) { return v.visit( *this, param ); }
+
+  std::string m_attr;
+  Ptr<Udata> m_value;
+};
+
+class Dtag : public Block
+{
+public:
+  Dtag (Buffer::Iterator &start, uint32_t dtag);
+
+  virtual void accept( Visitor &v )                           { v.visit( *this ); }
+  virtual void accept( GJVoidVisitor &v, boost::any param )   { v.visit( *this, param ); }
+  virtual boost::any accept( GJNoArguVisitor &v )             { return v.visit( *this ); }
+  virtual boost::any accept( GJVisitor &v, boost::any param ) { return v.visit( *this, param ); }
+
+  uint32_t m_dtag;
+  std::list<Ptr<Block> > m_nestedBlocks;
+};
+
+class Dattr : public Block
+{
+public:
+  Dattr (Buffer::Iterator &start, uint32_t dattr);
+
+  virtual void accept( Visitor &v )                           { v.visit( *this ); }
+  virtual void accept( GJVoidVisitor &v, boost::any param )   { v.visit( *this, param ); }
+  virtual boost::any accept( GJNoArguVisitor &v )             { return v.visit( *this ); }
+  virtual boost::any accept( GJVisitor &v, boost::any param ) { return v.visit( *this, param ); }
+
+  uint32_t m_dattr;
+  Ptr<Udata> m_value;
+};
+
+class Ext : public Block
+{
+public:
+  Ext (Buffer::Iterator &start, uint32_t extSubtype);
+
+  virtual void accept( Visitor &v )                           { v.visit( *this ); }
+  virtual void accept( GJVoidVisitor &v, boost::any param )   { v.visit( *this, param ); }
+  virtual boost::any accept( GJNoArguVisitor &v )             { return v.visit( *this ); }
+  virtual boost::any accept( GJVisitor &v, boost::any param ) { return v.visit( *this, param ); }
+
+  uint64_t m_extSubtype;
+};
+
+class DepthFirstVisitor : public Visitor
+{
+public:
+  virtual void visit (Blob& );
+  virtual void visit (Udata&);
+  virtual void visit (Tag&  );
+  virtual void visit (Attr& );
+  virtual void visit (Dtag& );
+  virtual void visit (Dattr&);
+  virtual void visit (Ext&  );
+};
+
+class GJDepthFirstVisitor : public GJVisitor
+{
+public:
+  virtual boost::any visit (Blob&,  boost::any);
+  virtual boost::any visit (Udata&, boost::any);
+  virtual boost::any visit (Tag&,   boost::any);
+  virtual boost::any visit (Attr&,  boost::any);
+  virtual boost::any visit (Dtag&,  boost::any);
+  virtual boost::any visit (Dattr&, boost::any);
+  virtual boost::any visit (Ext&,   boost::any);
+};
+
+class GJNoArguDepthFirstVisitor : public GJNoArguVisitor
+{
+public:
+  virtual boost::any visit (Blob& );
+  virtual boost::any visit (Udata&);
+  virtual boost::any visit (Tag&  );
+  virtual boost::any visit (Attr& );
+  virtual boost::any visit (Dtag& );
+  virtual boost::any visit (Dattr&);
+  virtual boost::any visit (Ext&  );
+};
+
+class GJVoidDepthFirstVisitor : public GJVoidVisitor
+{
+public:
+  virtual void visit (Blob&,  boost::any);
+  virtual void visit (Udata&, boost::any);
+  virtual void visit (Tag&,   boost::any);
+  virtual void visit (Attr&,  boost::any);
+  virtual void visit (Dtag&,  boost::any);
+  virtual void visit (Dattr&, boost::any);
+  virtual void visit (Ext&,   boost::any);
+};
+
+// class NameComponentsVisitor : public 
+
+class NonNegativeIntegerVisitor : public GJNoArguDepthFirstVisitor
+{
+public:
+  virtual boost::any visit (Blob &n); //to throw parsing error
+  virtual boost::any visit (Udata &n);
+};
+
+class StringVisitor : public GJNoArguDepthFirstVisitor
+{
+public:
+  virtual boost::any visit (Blob &n); //to throw parsing error
+  virtual boost::any visit (Udata &n);
+};
+
+class NameComponentsVisitor : public GJVoidDepthFirstVisitor
+{
+public:
+  virtual void visit (Dtag &n, boost::any param/*should be Name::Components*/);
+private:
+  static StringVisitor m_stringVisitor; 
+};
+
+class InterestVisitor : public GJVoidDepthFirstVisitor
+{
+public:
+  virtual void visit (Dtag &n, boost::any param/*should be CcnxInterestHeader&*/);
+  
+private:
+  static NonNegativeIntegerVisitor m_nonNegativeIntegerVisitor;
+  static NameComponentsVisitor     m_nameComponentsVisitor;
+};
+
+class ContentObjectVisitor : public GJVoidDepthFirstVisitor
+{
+public:
+  virtual void visit (Dtag &n, boost::any param/*should be CcnxContentObjectHeader&*/);
+  
+private:
+  static NameComponentsVisitor     m_nameComponentsVisitor;
+};
+
+
+class CcnxDecodingException {};
+
+} // namespace CcnxParser
+
+} // namespace ns3
+
+#endif // _CCNX_DECODING_HELPER_H_
+
diff --git a/helper/ccnx-coding-helper.cc b/helper/ccnx-encoding-helper.cc
similarity index 66%
rename from helper/ccnx-coding-helper.cc
rename to helper/ccnx-encoding-helper.cc
index 7cab398..21ca9ae 100644
--- a/helper/ccnx-coding-helper.cc
+++ b/helper/ccnx-encoding-helper.cc
@@ -18,7 +18,7 @@
  * Author: 
  */
 
-#include "ccnx-coding-helper.h"
+#include "ccnx-encoding-helper.h"
 
 #include "ns3/name-components.h"
 #include "ns3/ccnx-interest-header.h"
@@ -35,17 +35,17 @@
 #define CCN_TT_HBIT ((unsigned char)(1 << 7))
 
 size_t
-CcnxCodingHelper::AppendBlockHeader (Buffer::Iterator start, size_t val, enum ccn_tt tt)
+CcnxEncodingHelper::AppendBlockHeader (Buffer::Iterator start, size_t val, Ccnx::ccn_tt tt)
 {
   unsigned char buf[1+8*((sizeof(val)+6)/7)];
   unsigned char *p = &(buf[sizeof(buf)-1]);
   size_t n = 1;
-  p[0] = (CCN_TT_HBIT & ~CCN_CLOSE) |
+  p[0] = (CCN_TT_HBIT & ~Ccnx::CCN_CLOSE) |
   ((val & CCN_MAX_TINY) << CCN_TT_BITS) |
   (CCN_TT_MASK & tt);
   val >>= (7-CCN_TT_BITS);
   while (val != 0) {
-    (--p)[0] = (((unsigned char)val) & ~CCN_TT_HBIT) | CCN_CLOSE;
+    (--p)[0] = (((unsigned char)val) & ~CCN_TT_HBIT) | Ccnx::CCN_CLOSE;
     n++;
     val >>= 7;
   }
@@ -54,13 +54,13 @@
 }
 
 size_t
-CcnxCodingHelper::AppendNumber (Buffer::Iterator start, uint32_t number)
+CcnxEncodingHelper::AppendNumber (Buffer::Iterator start, uint32_t number)
 {
   std::ostringstream os;
   os << number;
 
   size_t written = 0;
-  written += AppendBlockHeader (start, os.str().size(), CCN_UDATA);
+  written += AppendBlockHeader (start, os.str().size(), Ccnx::CCN_UDATA);
   written += os.str().size();
   start.Write (reinterpret_cast<const unsigned char*>(os.str().c_str()), os.str().size());
 
@@ -69,26 +69,26 @@
 
   
 size_t
-CcnxCodingHelper::CcnxCodingHelper::AppendCloser (Buffer::Iterator start)
+CcnxEncodingHelper::CcnxEncodingHelper::AppendCloser (Buffer::Iterator start)
 {
-  start.WriteU8 (CCN_CLOSE);
+  start.WriteU8 (Ccnx::CCN_CLOSE);
   return 1;
 }
 
 size_t
-CcnxCodingHelper::AppendNameComponents (Buffer::Iterator start, const Name::Components &name)
+CcnxEncodingHelper::AppendNameComponents (Buffer::Iterator start, const Name::Components &name)
 {
   size_t written = 0;
   BOOST_FOREACH (const std::string &component, name.GetComponents())
     {
-      written += AppendTaggedBlob (start, CCN_DTAG_Component,
+      written += AppendTaggedBlob (start, Ccnx::CCN_DTAG_Component,
                                    reinterpret_cast<const uint8_t*>(component.c_str()), component.size());
     }
   return written;
 }
 
 size_t
-CcnxCodingHelper::AppendTimestampBlob (Buffer::Iterator start, Time time)
+CcnxEncodingHelper::AppendTimestampBlob (Buffer::Iterator start, Time time)
 {
   // the original function implements Markers... thought not sure what are these markers for...
 
@@ -98,7 +98,7 @@
   for (;  required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes?
      required_bytes++;
   
-  size_t len = AppendBlockHeader(start, required_bytes, CCN_BLOB);
+  size_t len = AppendBlockHeader(start, required_bytes, Ccnx::CCN_BLOB);
 
   // write part with seconds
   ts = time.ToInteger (Time::S) >> 4;
@@ -114,13 +114,13 @@
 }
 
 size_t
-CcnxCodingHelper::AppendTaggedBlob (Buffer::Iterator start, ccn_dtag dtag,
+CcnxEncodingHelper::AppendTaggedBlob (Buffer::Iterator start, Ccnx::ccn_dtag dtag,
                   const uint8_t *data, size_t size)
 {
-  size_t written = AppendBlockHeader (start, dtag, CCN_DTAG);
+  size_t written = AppendBlockHeader (start, dtag, Ccnx::CCN_DTAG);
   if (size>0)
     {
-      written += AppendBlockHeader (start, size, CCN_BLOB);
+      written += AppendBlockHeader (start, size, Ccnx::CCN_BLOB);
       start.Write (data, size);
       written += size;
     }
@@ -131,61 +131,61 @@
 
 
 size_t
-CcnxCodingHelper::Serialize (Buffer::Iterator start, const CcnxInterestHeader &interest)
+CcnxEncodingHelper::Serialize (Buffer::Iterator start, const CcnxInterestHeader &interest)
 {
   size_t written = 0;
-  written += AppendBlockHeader (start, CCN_DTAG_Interest, CCN_DTAG); // <Interest>
+  written += AppendBlockHeader (start, Ccnx::CCN_DTAG_Interest, Ccnx::CCN_DTAG); // <Interest>
   
-  written += AppendBlockHeader (start, CCN_DTAG_Name, CCN_DTAG); // <Name>
+  written += AppendBlockHeader (start, Ccnx::CCN_DTAG_Name, Ccnx::CCN_DTAG); // <Name>
   written += AppendNameComponents (start, interest.GetName());                // <Component>...</Component>...
   written += AppendCloser (start);                               // </Name>
 
   if (interest.GetMinSuffixComponents() >= 0)
     {
-      written += AppendBlockHeader (start, CCN_DTAG_MinSuffixComponents, CCN_DTAG);
+      written += AppendBlockHeader (start, Ccnx::CCN_DTAG_MinSuffixComponents, Ccnx::CCN_DTAG);
       written += AppendNumber (start, interest.GetMinSuffixComponents ());
       written += AppendCloser (start);
     }
   if (interest.GetMaxSuffixComponents() >= 0)
     {
-      written += AppendBlockHeader (start, CCN_DTAG_MaxSuffixComponents, CCN_DTAG);
+      written += AppendBlockHeader (start, Ccnx::CCN_DTAG_MaxSuffixComponents, Ccnx::CCN_DTAG);
       written += AppendNumber (start, interest.GetMaxSuffixComponents ());
       written += AppendCloser (start);
     }
   if (interest.GetExclude().size() > 0)
     {
-      written += AppendBlockHeader (start, CCN_DTAG_Exclude, CCN_DTAG); // <Exclude>
+      written += AppendBlockHeader (start, Ccnx::CCN_DTAG_Exclude, Ccnx::CCN_DTAG); // <Exclude>
       written += AppendNameComponents (start, interest.GetExclude());                // <Component>...</Component>...
       written += AppendCloser (start);                                  // </Exclude>
     }
   if (interest.IsEnabledChildSelector())
     {
-      written += AppendBlockHeader (start, CCN_DTAG_ChildSelector, CCN_DTAG);
+      written += AppendBlockHeader (start, Ccnx::CCN_DTAG_ChildSelector, Ccnx::CCN_DTAG);
       written += AppendNumber (start, 1);
       written += AppendCloser (start);
     }
   if (interest.IsEnabledAnswerOriginKind())
     {
-      written += AppendBlockHeader (start, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
+      written += AppendBlockHeader (start, Ccnx::CCN_DTAG_AnswerOriginKind, Ccnx::CCN_DTAG);
       written += AppendNumber (start, 1);
       written += AppendCloser (start);
     }
   if (interest.GetScope() >= 0)
     {
-      written += AppendBlockHeader (start, CCN_DTAG_Scope, CCN_DTAG);
+      written += AppendBlockHeader (start, Ccnx::CCN_DTAG_Scope, Ccnx::CCN_DTAG);
       written += AppendNumber (start, interest.GetScope ());
       written += AppendCloser (start);
     }
   if (!interest.GetInterestLifetime().IsZero())
     {
-      written += AppendBlockHeader (start, CCN_DTAG_InterestLifetime, CCN_DTAG);
+      written += AppendBlockHeader (start, Ccnx::CCN_DTAG_InterestLifetime, Ccnx::CCN_DTAG);
       written += AppendTimestampBlob (start, interest.GetInterestLifetime());
       written += AppendCloser (start);
     }
   if (interest.GetNonce()>0)
     {
       uint32_t nonce = interest.GetNonce();
-      written += AppendTaggedBlob (start, CCN_DTAG_Nonce,
+      written += AppendTaggedBlob (start, Ccnx::CCN_DTAG_Nonce,
                                    reinterpret_cast<const uint8_t*>(&nonce),
                                    sizeof(nonce));
     }
@@ -195,35 +195,35 @@
 }
 
 size_t
-CcnxCodingHelper::Serialize (Buffer::Iterator start, const CcnxContentObjectHeader &contentObject)
+CcnxEncodingHelper::Serialize (Buffer::Iterator start, const CcnxContentObjectHeader &contentObject)
 {
   size_t written = 0;
-  written += AppendBlockHeader (start, CCN_DTAG_ContentObject, CCN_DTAG); // <ContentObject>
+  written += AppendBlockHeader (start, Ccnx::CCN_DTAG_ContentObject, Ccnx::CCN_DTAG); // <ContentObject>
 
   // fake signature
-  written += AppendBlockHeader (start, CCN_DTAG_Signature, CCN_DTAG); // <Signature>
+  written += AppendBlockHeader (start, Ccnx::CCN_DTAG_Signature, Ccnx::CCN_DTAG); // <Signature>
   // Signature ::= DigestAlgorithm? 
   //               Witness?         
   //               SignatureBits   
-  written += AppendTaggedBlob (start, CCN_DTAG_SignatureBits, 0, 0);      // <SignatureBits />
+  written += AppendTaggedBlob (start, Ccnx::CCN_DTAG_SignatureBits, 0, 0);      // <SignatureBits />
   written += AppendCloser (start);                                    // </Signature>  
 
-  written += AppendBlockHeader (start, CCN_DTAG_Name, CCN_DTAG);    // <Name>
+  written += AppendBlockHeader (start, Ccnx::CCN_DTAG_Name, Ccnx::CCN_DTAG);    // <Name>
   written += AppendNameComponents (start, contentObject.GetName()); //   <Component>...</Component>...
   written += AppendCloser (start);                                  // </Name>  
 
   // fake signature
-  written += AppendBlockHeader (start, CCN_DTAG_SignedInfo, CCN_DTAG); // <SignedInfo>
+  written += AppendBlockHeader (start, Ccnx::CCN_DTAG_SignedInfo, Ccnx::CCN_DTAG); // <SignedInfo>
   // SignedInfo ::= PublisherPublicKeyDigest
   //                Timestamp
   //                Type?
   //                FreshnessSeconds?
   //                FinalBlockID?
   //                KeyLocator?
-  written += AppendTaggedBlob (start, CCN_DTAG_PublisherPublicKeyDigest, 0, 0); // <PublisherPublicKeyDigest />
+  written += AppendTaggedBlob (start, Ccnx::CCN_DTAG_PublisherPublicKeyDigest, 0, 0); // <PublisherPublicKeyDigest />
   written += AppendCloser (start);                                     // </SignedInfo>
 
-  written += AppendBlockHeader (start, CCN_DTAG_Content, CCN_DTAG); // <Content>
+  written += AppendBlockHeader (start, Ccnx::CCN_DTAG_Content, Ccnx::CCN_DTAG); // <Content>
 
   // there is no closing tag !!!
   return written;
diff --git a/helper/ccnx-encoding-helper.h b/helper/ccnx-encoding-helper.h
new file mode 100644
index 0000000..90fad22
--- /dev/null
+++ b/helper/ccnx-encoding-helper.h
@@ -0,0 +1,97 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2011 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: 
+ */
+
+#ifndef _CCNX_ENCODING_HELPER_H_
+#define _CCNX_ENCODING_HELPER_H_
+
+#include <sys/types.h>
+
+#include "ns3/ccnx.h"
+#include "ns3/ptr.h"
+#include "ns3/nstime.h"
+#include "ns3/buffer.h"
+
+namespace ns3 {
+
+namespace Name{ class Components; }
+
+class CcnxInterestHeader;
+class CcnxContentObjectHeader;
+  
+/**
+ * Helper to encode/decode ccnb formatted CCNx message
+ *
+ */
+class CcnxEncodingHelper
+{
+public:
+  static size_t
+  Serialize (Buffer::Iterator start, const CcnxInterestHeader &interest);
+
+  static size_t
+  Serialize (Buffer::Iterator start, const CcnxContentObjectHeader &contentObject);
+
+private:
+  static size_t
+  AppendBlockHeader (Buffer::Iterator start, size_t value, Ccnx::ccn_tt block_type);
+
+  static size_t
+  AppendNumber (Buffer::Iterator start, uint32_t number);
+
+  static size_t
+  AppendCloser (Buffer::Iterator start);
+
+  static size_t
+  AppendNameComponents (Buffer::Iterator start, const Name::Components &name);
+
+  /**
+   * Append a binary timestamp as a BLOB using the ccn binary
+   * Timestamp representation (12-bit fraction).
+   *
+   * @param start start iterator of  the buffer to append to.
+   * @param time - Time object
+   *
+   * @returns written length
+   */
+  static size_t
+  AppendTimestampBlob (Buffer::Iterator start, Time time);
+
+  /**
+   * Append a tagged BLOB
+   *
+   * This is a ccnb-encoded element with containing the BLOB as content
+   *
+   * @param start start iterator of  the buffer to append to.
+   * @param dtag is the element's dtab
+   * @param data points to the binary data
+   * @param size is the size of the data, in bytes
+   *
+   * @returns written length
+   */
+  static size_t
+  AppendTaggedBlob (Buffer::Iterator start, Ccnx::ccn_dtag dtag,
+                    const uint8_t *data, size_t size);
+  
+};
+
+} // namespace ns3
+
+#endif // _CCNX_ENCODING_HELPER_H_
+
diff --git a/model/ccnx-content-object-header.cc b/model/ccnx-content-object-header.cc
index a3852a2..e567207 100644
--- a/model/ccnx-content-object-header.cc
+++ b/model/ccnx-content-object-header.cc
@@ -22,7 +22,8 @@
 #include "ccnx-content-object-header.h"
 
 #include "ns3/log.h"
-#include "ns3/ccnx-coding-helper.h"
+#include "ns3/ccnx-encoding-helper.h"
+#include "ns3/ccnx-decoding-helper.h"
 
 NS_LOG_COMPONENT_DEFINE ("CcnxContentObjectHeader");
 
@@ -64,19 +65,19 @@
   // Unfortunately, two serializations are required, unless we can pre-calculate header length... which is not trivial
   Buffer tmp;
   
-  return CcnxCodingHelper::Serialize (tmp.Begin(), *this);
+  return CcnxEncodingHelper::Serialize (tmp.Begin(), *this);
 }
     
 void
 CcnxContentObjectHeader::Serialize (Buffer::Iterator start) const
 {
-  CcnxCodingHelper::Serialize (start, *this);
+  CcnxEncodingHelper::Serialize (start, *this);
 }
 
 uint32_t
 CcnxContentObjectHeader::Deserialize (Buffer::Iterator start)
 {
-  return 0; // the most complicated part is here
+  return CcnxDecodingHelper::Deserialize (start, *this); // \todo Debugging is necessary
 }
   
 TypeId
@@ -101,7 +102,7 @@
 TypeId
 CcnxContentObjectTail::GetTypeId (void)
 {
-  static TypeId tid = TypeId ("ns3::CcnxContentObjectHeader")
+  static TypeId tid = TypeId ("ns3::CcnxContentObjectTail")
     .SetParent<Header> ()
     .AddConstructor<CcnxContentObjectHeader> ()
     ;
@@ -139,10 +140,10 @@
 {
   Buffer::Iterator i = start;
   uint8_t __attribute__ ((unused)) closing_tag_content = i.ReadU8 ();
-  NS_ASSERT_MSG (closing_tag_content==0, "Should be closing tag </Content> (0x00)");
+  NS_ASSERT_MSG (closing_tag_content==0, "Should be a closing tag </Content> (0x00)");
 
   uint8_t __attribute__ ((unused)) closing_tag_content_object = i.ReadU8 ();
-  NS_ASSERT_MSG (closing_tag_content_object==0, "Should be closing tag </ContentObject> (0x00)");
+  NS_ASSERT_MSG (closing_tag_content_object==0, "Should be a closing tag </ContentObject> (0x00)");
 
   return 2;
 }
diff --git a/model/ccnx-interest-header.cc b/model/ccnx-interest-header.cc
index cab33ea..02ad9af 100644
--- a/model/ccnx-interest-header.cc
+++ b/model/ccnx-interest-header.cc
@@ -26,7 +26,8 @@
 #include "ccnx-interest-header.h"
 
 #include "ns3/log.h"
-#include "ns3/ccnx-coding-helper.h"
+#include "ns3/ccnx-encoding-helper.h"
+#include "ns3/ccnx-decoding-helper.h"
 
 NS_LOG_COMPONENT_DEFINE ("CcnxInterestHeader");
 
@@ -171,19 +172,19 @@
   // unfortunately, 2 serialization required...
   Buffer tmp;
   
-  return CcnxCodingHelper::Serialize (tmp.Begin(), *this);
+  return CcnxEncodingHelper::Serialize (tmp.Begin(), *this);
 }
     
 void
 CcnxInterestHeader::Serialize (Buffer::Iterator start) const
 {
-  CcnxCodingHelper::Serialize (start, *this);
+  CcnxEncodingHelper::Serialize (start, *this);
 }
 
 uint32_t
 CcnxInterestHeader::Deserialize (Buffer::Iterator start)
 {
-  return 0; // the most complicated part is here
+  return CcnxDecodingHelper::Deserialize (start, *this); // \todo Debugging is necessary
 }
 
 TypeId
diff --git a/model/ccnx.h b/model/ccnx.h
index 12c45d2..1e1aa0b 100644
--- a/model/ccnx.h
+++ b/model/ccnx.h
@@ -161,6 +161,155 @@
    * ignored during Ccnx forwarding.
    */
   virtual void SetDown (uint32_t face) = 0;
+
+public:
+    /**
+   * Type tag for a ccnb start marker.
+   *
+   * \see http://www.ccnx.org/releases/latest/doc/technical/DTAG.html
+   */
+  enum ccn_tt {
+    CCN_EXT,        /**< starts composite extension - numval is subtype */
+    CCN_TAG,        /**< starts composite - numval is tagnamelen-1 */ 
+    CCN_DTAG,       /**< starts composite - numval is tagdict index (enum ccn_dtag) */
+    CCN_ATTR,       /**< attribute - numval is attrnamelen-1, value follows */
+    CCN_DATTR,      /**< attribute numval is attrdict index */
+    CCN_BLOB,       /**< opaque binary data - numval is byte count */
+    CCN_UDATA,      /**< UTF-8 encoded character data - numval is byte count */
+    CCN_NO_TOKEN    /**< should not occur in encoding */
+  };
+
+  /** CCN_CLOSE terminates composites */
+  enum {CCN_CLOSE = 0};
+
+  // enum ccn_ext_subtype {
+  //   /* skip smallest values for now */
+  //   CCN_PROCESSING_INSTRUCTIONS = 16 /* <?name:U value:U?> */
+  // };
+
+  /**
+   * DTAG identifies ccnb-encoded elements.
+   *
+   * \see http://www.ccnx.org/releases/latest/doc/technical/DTAG.html
+   */
+  enum ccn_dtag {
+    CCN_DTAG_Any = 13,
+    CCN_DTAG_Name = 14,
+    CCN_DTAG_Component = 15,
+    CCN_DTAG_Certificate = 16,
+    CCN_DTAG_Collection = 17,
+    CCN_DTAG_CompleteName = 18,
+    CCN_DTAG_Content = 19,
+    CCN_DTAG_SignedInfo = 20,
+    CCN_DTAG_ContentDigest = 21,
+    CCN_DTAG_ContentHash = 22,
+    CCN_DTAG_Count = 24,
+    CCN_DTAG_Header = 25,
+    CCN_DTAG_Interest = 26,	/* 20090915 */
+    CCN_DTAG_Key = 27,
+    CCN_DTAG_KeyLocator = 28,
+    CCN_DTAG_KeyName = 29,
+    CCN_DTAG_Length = 30,
+    CCN_DTAG_Link = 31,
+    CCN_DTAG_LinkAuthenticator = 32,
+    CCN_DTAG_NameComponentCount = 33,	/* DeprecatedInInterest */
+    CCN_DTAG_RootDigest = 36,
+    CCN_DTAG_Signature = 37,
+    CCN_DTAG_Start = 38,
+    CCN_DTAG_Timestamp = 39,
+    CCN_DTAG_Type = 40,
+    CCN_DTAG_Nonce = 41,
+    CCN_DTAG_Scope = 42,
+    CCN_DTAG_Exclude = 43,
+    CCN_DTAG_Bloom = 44,
+    CCN_DTAG_BloomSeed = 45,
+    CCN_DTAG_AnswerOriginKind = 47,
+    CCN_DTAG_InterestLifetime = 48,
+    CCN_DTAG_Witness = 53,
+    CCN_DTAG_SignatureBits = 54,
+    CCN_DTAG_DigestAlgorithm = 55,
+    CCN_DTAG_BlockSize = 56,
+    CCN_DTAG_FreshnessSeconds = 58,
+    CCN_DTAG_FinalBlockID = 59,
+    CCN_DTAG_PublisherPublicKeyDigest = 60,
+    CCN_DTAG_PublisherCertificateDigest = 61,
+    CCN_DTAG_PublisherIssuerKeyDigest = 62,
+    CCN_DTAG_PublisherIssuerCertificateDigest = 63,
+    CCN_DTAG_ContentObject = 64,	/* 20090915 */
+    CCN_DTAG_WrappedKey = 65,
+    CCN_DTAG_WrappingKeyIdentifier = 66,
+    CCN_DTAG_WrapAlgorithm = 67,
+    CCN_DTAG_KeyAlgorithm = 68,
+    CCN_DTAG_Label = 69,
+    CCN_DTAG_EncryptedKey = 70,
+    CCN_DTAG_EncryptedNonceKey = 71,
+    CCN_DTAG_WrappingKeyName = 72,
+    CCN_DTAG_Action = 73,
+    CCN_DTAG_FaceID = 74,
+    CCN_DTAG_IPProto = 75,
+    CCN_DTAG_Host = 76,
+    CCN_DTAG_Port = 77,
+    CCN_DTAG_MulticastInterface = 78,
+    CCN_DTAG_ForwardingFlags = 79,
+    CCN_DTAG_FaceInstance = 80,
+    CCN_DTAG_ForwardingEntry = 81,
+    CCN_DTAG_MulticastTTL = 82,
+    CCN_DTAG_MinSuffixComponents = 83,
+    CCN_DTAG_MaxSuffixComponents = 84,
+    CCN_DTAG_ChildSelector = 85,
+    CCN_DTAG_RepositoryInfo = 86,
+    CCN_DTAG_Version = 87,
+    CCN_DTAG_RepositoryVersion = 88,
+    CCN_DTAG_GlobalPrefix = 89,
+    CCN_DTAG_LocalName = 90,
+    CCN_DTAG_Policy = 91,
+    CCN_DTAG_Namespace = 92,
+    CCN_DTAG_GlobalPrefixName = 93,
+    CCN_DTAG_PolicyVersion = 94,
+    CCN_DTAG_KeyValueSet = 95,
+    CCN_DTAG_KeyValuePair = 96,
+    CCN_DTAG_IntegerValue = 97,
+    CCN_DTAG_DecimalValue = 98,
+    CCN_DTAG_StringValue = 99,
+    CCN_DTAG_BinaryValue = 100,
+    CCN_DTAG_NameValue = 101,
+    CCN_DTAG_Entry = 102,
+    CCN_DTAG_ACL = 103,
+    CCN_DTAG_ParameterizedName = 104,
+    CCN_DTAG_Prefix = 105,
+    CCN_DTAG_Suffix = 106,
+    CCN_DTAG_Root = 107,
+    CCN_DTAG_ProfileName = 108,
+    CCN_DTAG_Parameters = 109,
+    CCN_DTAG_InfoString = 110,
+    CCN_DTAG_StatusResponse = 112,
+    CCN_DTAG_StatusCode = 113,
+    CCN_DTAG_StatusText = 114,
+    CCN_DTAG_SequenceNumber = 256,
+    CCN_DTAG_CCNProtocolDataUnit = 17702112
+  };
+
+  /**
+   * The decoder state is one of these, possibly with some
+   * additional bits set for internal use.  A complete parse
+   * ends up in state 0 or an error state.  Not all possible
+   * error states are listed here.
+   */
+  enum ccn_decoder_state {
+    CCN_DSTATE_INITIAL = 0,
+    CCN_DSTATE_NEWTOKEN,
+    CCN_DSTATE_NUMVAL,
+    CCN_DSTATE_UDATA,
+    CCN_DSTATE_TAGNAME,
+    CCN_DSTATE_ATTRNAME,
+    CCN_DSTATE_BLOB,
+    /* All error states are negative */
+    CCN_DSTATE_ERR_OVERFLOW = -1,
+    CCN_DSTATE_ERR_ATTR     = -2,       
+    CCN_DSTATE_ERR_CODING   = -3,
+    CCN_DSTATE_ERR_NEST     = -4, 
+    CCN_DSTATE_ERR_BUG      = -5
+  };
 };
 
 } // namespace ns3 
diff --git a/model/name-components.h b/model/name-components.h
index d9b3f0a..0ea4821 100644
--- a/model/name-components.h
+++ b/model/name-components.h
@@ -36,7 +36,11 @@
   Components (const std::string &s);
   ~Components ();
   
-  Components& operator () (const std::string &s);
+  inline void
+  Add (const std::string &s);
+       
+  Components&
+  operator () (const std::string &s);
 
   const std::list<std::string> &
   GetComponents () const;
@@ -70,6 +74,12 @@
   return m_prefix.size ();
 }
   
+void
+Components::Add (const std::string &s)
+{
+  (*this) (s);
+}
+  
   
 } // Namespace Name
 } // namespace ns3